-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (154 loc) · 4.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2014 by f4lk0r_
// Licensed under the GPLv3, see the License.txt file for details
package main
import (
//janimo's textsecure library. Documentation here: https://godoc.org/github.com/janimo/textsecure
ts "github.com/janimo/textsecure"
// //go ncurses library. Documentation here: https://godoc.org/code.google.com/p/goncurses
gc "code.google.com/p/goncurses"
"log"
// "reflect"
"time"
"strconv"
"bytes"
)
//Global variables that make things way more convenient, rather than passing copies all the time
var(
// contactsWin *gc.Window
// messageWinBorder *gc.Window
globalMsgWin *gc.Window
// inputBorderWin *gc.Window
globalInputWin *gc.Window
// menu_items []*gc.MenuItem
// contactMenu *gc.Menu
// contactsMenuWin *gc.Window
debugLog = startDebugLogger()
inputBuffer []byte
currentContact string
msgWinSize_x int
msgWinSize_y int
pass string
placer = -1
)
// getName returns the local contact name corresponding to a phone number,
// or failing to find a contact the phone number itself
func getName(tel string) string {
if n, ok := telToName[tel]; ok {
return n
}
return tel
}
// getTel returns the local contact telephone number corresponding to a name,
// or failing to find a contact the name itself
func getTel(name string) string {
if t,ok := nameToTel[name]; ok {
return t
}
return name
}
var telToName map[string]string
var nameToTel map[string]string
// In addition to sending a message using janimo's library, also clears screen and resets buffer
func sendMsg (inputWin *gc.Window, msgWin *gc.Window) {
if len(inputBuffer) != 0 {
msg := string(inputBuffer)
to := currentContact
err := ts.SendMessage(to,msg)
if err != nil {
gc.End()
log.Fatal("SendMessage failed yo: ",err)
}
var b bytes.Buffer
t := time.Now()
if t.Hour() < 10 {
b.WriteString("0")
}
b.WriteString(strconv.Itoa(t.Hour()))
b.WriteString(":")
if t.Minute() < 10 {
b.WriteString("0")
}
b.WriteString(strconv.Itoa(t.Minute()))
b.WriteString(": ")
b.WriteString(msg)
printToMsgWindow(b.String(),msgWin, true)
b.Reset()
insertMessage("You",currentContact,[]byte(msg),nil)
inputBuffer = []byte{}
inputWin.Erase()
}
}
// free memory for various things at the end of the program
func cleanup (menu_items []*gc.MenuItem, contactMenu *gc.Menu) {
for i:=0; i <len(menu_items); i++ {
menu_items[i].Free()
}
contactMenu.UnPost()
contactMenu.Free()
}
// creates a curses based TUI for the textsecure library
func main() {
client := &ts.Client{
RootDir: ".",
ReadLine: ts.ConsoleReadLine,
MessageHandler: recieveMessage,
}
ts.Setup(client)
stdscr, err := gc.Init()
if err != nil {
log.Fatal("Error initializing curses:", err)
}
defer gc.End()
configCurses(stdscr)
for i:=0;i<3;i++ {
doHello(stdscr)
var locked bool = passphraseUnlock(client)
if locked == false {
stdscr.ColorOn(1)
stdscr.MovePrint(0,0,"Password incorrect, hit any key...")
stdscr.ColorOff(1)
stdscr.GetChar()
stdscr.Clear()
stdscr.Refresh()
} else {
stdscr.Clear()
stdscr.Refresh()
break
}
if i == 2 {
log.Fatal("Password wrong too many times. Exiting...")
}
}
db = setupDatabase()
contacts, err := ts.GetRegisteredContacts()
if err != nil {
log.Fatal("Could not get contacts: %s\n", err)
}
telToName = make(map[string]string)
for _, c := range contacts {
telToName[c.Tel] = c.Name
}
nameToTel = make(map[string]string)
for _, c := range contacts {
nameToTel[c.Name] = c.Tel
}
contactsWin, messageWinBorder, msgWin, inputBorderWin, inputWin := createMainWindows(stdscr)
menu_items, contactMenu, contactsMenuWin := makeContactsMenu(contacts, contactsWin)
globalInputWin = inputWin
globalMsgWin = msgWin
contactsMenuWin.Touch()
contactMenu.Post()
contactsWin.Refresh()
messageWinBorder.Refresh()
inputBorderWin.Refresh()
msgWin.Refresh()
msgWinSize_y,msgWinSize_x = msgWin.MaxYX()
//msgWin.MovePrint(0,0,"test")
//msgWin.Refresh()
currentContact = getTel(contactMenu.Current(nil).Name())
changeContact(contactsMenuWin,contactMenu)
inputWin.Move(0,0)
go ts.ListenForMessages()
inputHandler(inputWin, stdscr, contactsMenuWin, contactMenu, msgWin)
cleanup(menu_items, contactMenu)
}