-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
284 lines (241 loc) · 8.73 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Receive and send messages via commandline.
package main
import (
"bufio"
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"unicode/utf8"
"github.com/o3ma/o3"
)
func main() {
pass, idpath, abpath, pubnick, pubnickSet, rid, testMsg, createID := parseArgs()
tr, tid, ctx, receiveMsgChan, sendMsgChan := initialise(pass, idpath, abpath, pubnick, pubnickSet, createID)
go receiveLoop(tid, ctx, receiveMsgChan, sendMsgChan)
sendTestMsg(tr, abpath, rid, testMsg, ctx, sendMsgChan)
sendLoop(tr, abpath, ctx, sendMsgChan)
}
func parseArgs() ([]byte, string, string, string, bool, string, string, bool) {
cmdlnPubnick := flag.String("nickname", "parrot", "The nickname for the account (max. 32 chars).")
cmdlnConfdir := flag.String("confdir", "", "Path to the configuration directory.")
cmdlnPass := flag.String("pass", "01234567", "A string which should be at least 8 chars long (else may cause problems).")
cmdlnHexPass := flag.String("hexpass", "", "Like --pass but in hexidecimal. Overrides --pass option. E.g.: 4d7954696e795057")
cmdlnTestID := flag.String("testid", "", "Send \"testmsg\" to this ID (8 character hex string).")
cmdlnTestMsg := flag.String("testmsg", "Say something!", "Send this message to \"testid\".")
cmdlnCreateID := flag.Bool( "createid", false, "Create a new ID if nessesary without asking for confirmation.")
flag.Parse()
flagset := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { flagset[f.Name]=true } )
var (
pass = []byte{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37} // 01234567
idpath = "threema.id"
abpath = "address.book"
)
cmdlnPubnickVal := *cmdlnPubnick
if len(cmdlnPubnickVal) > 32 { cmdlnPubnickVal = cmdlnPubnickVal[0:32] }
pubnick := cmdlnPubnickVal
var pubnickSet bool = !!flagset["nickname"]
rid := ""
ridRegex := regexp.MustCompile("\\A[0-9A-Z]{8}\\z")
cmdlnTestIDVal := ridRegex.FindString(strings.ToUpper(*cmdlnTestID))
if cmdlnTestIDVal != "" && len(cmdlnTestIDVal) == 8 { rid = cmdlnTestIDVal }
testMsg := *cmdlnTestMsg
if flagset["hexpass"] {
hexpass, err := hex.DecodeString(*cmdlnHexPass)
if err == nil {
pass = hexpass
} else {
fmt.Printf(" Error decoding hexpass!\n")
os.Exit(1)
}
} else {
pass = []byte(*cmdlnPass)
}
if utf8.RuneCountInString(string(pass)) < 8 { fmt.Print(" Warning: Password should have at least 8 characters to avoid problems with original Threema client!\n") }
if (*cmdlnConfdir) != "" {
idpath = (*cmdlnConfdir) + "/" + idpath
abpath = (*cmdlnConfdir) + "/" + abpath
}
createId := *cmdlnCreateID
return pass, idpath, abpath, pubnick, pubnickSet, rid, testMsg, createId
}
func initialise(pass []byte, idpath string, abpath string, pubnick string, pubnickSet bool, createID bool) (o3.ThreemaRest, o3.ThreemaID, o3.SessionContext, <-chan o3.ReceivedMsg, chan<- o3.Message) {
var (
tr o3.ThreemaRest
tid o3.ThreemaID
)
// check whether an id file exists or else create a new one
if _, err := os.Stat(idpath); err != nil {
if !createID {
reader := bufio.NewReader(os.Stdin)
fmt.Print(" No existing ID found. Create a new one? Enter YES (upper case) or NO: ")
text, _ := reader.ReadString('\n')
createID = (text == "YES\n")
}
if createID {
var err error
tid, err = tr.CreateIdentity()
if err != nil {
fmt.Println(" CreateIdentity failed!")
log.Fatal(err)
}
fmt.Printf(" Saving ID to %s\n", idpath)
err = tid.SaveToFile(idpath, pass)
if err != nil {
fmt.Println(" Saving ID failed!")
log.Fatal(err)
}
} else {
os.Exit(1)
}
} else {
fmt.Printf(" Loading ID from %s\n", idpath)
tid, err = o3.LoadIDFromFile(idpath, pass)
if err != nil {
log.Fatal(err)
}
}
fmt.Printf(" Using ID: %s\n", tid.String())
if pubnickSet {
tid.Nick = o3.NewPubNick(pubnick)
fmt.Printf(" Setting nickname to: %s\n", pubnick)
}
ctx := o3.NewSessionContext(tid)
//check if we can load an addressbook
if _, err := os.Stat(abpath); !os.IsNotExist(err) {
fmt.Printf(" Loading addressbook from: %s\n", abpath)
err = ctx.ID.Contacts.ImportFrom(abpath)
if err != nil {
fmt.Println(" Loading addressbook failed!")
log.Fatal(err)
}
}
// let the session begin
fmt.Println(" Starting session")
sendMsgChan, receiveMsgChan, err := ctx.Run()
if err != nil {
log.Fatal(err)
}
return tr, tid, ctx, receiveMsgChan, sendMsgChan
}
func sendTestMsg(tr o3.ThreemaRest, abpath string, rid string, testMsg string, ctx o3.SessionContext, sendMsgChan chan<- o3.Message) {
// check if we know the remote ID for
// (just demonstration purposes \bc sending and receiving functions do this lookup for us)
if rid != "" {
if _, b := ctx.ID.Contacts.Get(rid); b == false {
//retrieve the ID from Threema's servers
myID := o3.NewIDString(rid)
fmt.Printf(" Retrieving %s from directory server\n", myID.String())
myContact, err := tr.GetContactByID(myID)
if err != nil {
log.Fatal(err)
}
// add them to our address book
ctx.ID.Contacts.Add(myContact)
//and save the address book
fmt.Printf(" Saving addressbook to: %s\n", abpath)
err = ctx.ID.Contacts.SaveTo(abpath)
if err != nil {
fmt.Println(" Saving addressbook failed!")
log.Fatal(err)
}
}
}
// send our initial message to our recipient
if rid != "" {
fmt.Println(" Sending initial message to " + rid + ": " + testMsg)
err := ctx.SendTextMessage(rid, testMsg, sendMsgChan)
if err != nil {
log.Fatal(err)
}
}
}
func receiveLoop(tid o3.ThreemaID, ctx o3.SessionContext, receiveMsgChan <-chan o3.ReceivedMsg, sendMsgChan chan<- o3.Message) {
// handle incoming messages
for receivedMessage := range receiveMsgChan {
if receivedMessage.Err != nil {
fmt.Printf(" Error Receiving Message: %s\n", receivedMessage.Err)
continue
}
switch msg := receivedMessage.Msg.(type) {
case o3.ImageMessage:
// display the image if you like
case o3.AudioMessage:
// play the audio if you like
case o3.TextMessage:
// Print text message.
fmt.Printf("Message from %s: %s\n--------------------\n\n", msg.Sender(), msg.Text())
confirmMsg(ctx, msg, sendMsgChan)
case o3.GroupTextMessage:
fmt.Printf(" %s for Group [%x] created by [%s]:\n%s\n", msg.Sender(), msg.GroupID(), msg.GroupCreator(), msg.Text())
case o3.GroupManageSetNameMessage:
fmt.Printf(" Group [%x] is now called %s\n", msg.GroupID(), msg.Name())
case o3.GroupManageSetMembersMessage:
fmt.Printf(" Group [%x] now includes %v\n", msg.GroupID(), msg.Members())
case o3.GroupMemberLeftMessage:
fmt.Printf(" Member [%s] left the Group [%x]\n", msg.Sender(), msg.GroupID())
case o3.DeliveryReceiptMessage:
fmt.Printf(" Message [%x] has been acknowledged by the server.\n", msg.MsgID())
case o3.TypingNotificationMessage:
fmt.Printf(" Typing Notification from %s: [%x]\n", msg.Sender(), msg.OnOff)
default:
fmt.Printf(" Unknown message type from: %s\nContent: %#v", msg.Sender(), msg)
}
}
}
func sendLoop(tr o3.ThreemaRest, abpath string, ctx o3.SessionContext, sendMsgChan chan<- o3.Message) {
reader := bufio.NewReader(os.Stdin)
fmt.Println(" Sending thread startet! Send messages via: ---ID---MESSAGE (e.g. 1337ABCDHello World!)")
for {
rid_msg, _ := reader.ReadString('\n')
idValid := false
if len(rid_msg) >= 8 {
rid := rid_msg[0:8]
msg := rid_msg[8:len(rid_msg)-1]
ridRegex := regexp.MustCompile("\\A[0-9A-Z]{8}\\z")
rid = ridRegex.FindString(strings.ToUpper(rid))
if rid != "" && len(rid) == 8 {
idValid = true
if _, b := ctx.ID.Contacts.Get(rid); b == false {
//retrieve the ID from Threema's servers
myID := o3.NewIDString(rid)
fmt.Printf(" Retrieving %s from directory server\n", myID.String())
myContact, err := tr.GetContactByID(myID)
if err != nil {
log.Fatal(err)
}
// add them to our address book
ctx.ID.Contacts.Add(myContact)
//and save the address book
fmt.Printf(" Saving addressbook to: %s\n", abpath)
err = ctx.ID.Contacts.SaveTo(abpath)
if err != nil {
fmt.Println(" Saving addressbook failed!")
log.Fatal(err)
}
}
fmt.Println(" Sending message to " + rid + ".")
err := ctx.SendTextMessage(rid, msg, sendMsgChan)
if err != nil {
log.Fatal(err)
}
}
}
if !idValid {
fmt.Println(" ID is invalid!")
}
}
}
func confirmMsg(ctx o3.SessionContext, msg o3.TextMessage, sendMsgChan chan<- o3.Message) {
// confirm to the sender that we received the message
// this is how one can send messages manually without helper functions like "SendTextMessage"
drm, err := o3.NewDeliveryReceiptMessage(&ctx, msg.Sender().String(), msg.ID(), o3.MSGDELIVERED)
if err != nil {
log.Fatal(err)
}
sendMsgChan <- drm
}