This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconnection.go
388 lines (324 loc) · 9.05 KB
/
connection.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package main
import (
"context"
"fmt"
"io"
"log"
"math"
"net"
"strings"
"time"
"whapp-irc/ircconnection"
"whapp-irc/timestampmap"
"whapp-irc/types"
"whapp-irc/util"
"whapp-irc/whapp"
)
// queue up to ten irc messages, this is especially helpful to answer PINGs in
// time, and during connection setup.
const ircMessageQueueSize = 10
// A Connection represents the internal state of a whapp-irc connection.
type Connection struct {
WI *whapp.Instance
Chats *types.ChatList
irc *ircconnection.Connection
timestampMap *timestampmap.Map
me whapp.Me
localStorage map[string]string
}
// BindSocket binds the given TCP connection.
func BindSocket(socket *net.TCPConn) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
irc := ircconnection.HandleConnection(ctx, socket)
// when the irc connection dies or the context is cancelled, kill
// everything off
go func() {
select {
case <-irc.StopChannel():
case <-ctx.Done():
}
cancel()
}()
// wait for the client to send a nickname
select {
case <-ctx.Done():
return nil
case <-irc.NickSetChannel():
}
if irc.Nick() == "" {
return fmt.Errorf("nickname can't be empty")
}
// send welcome message
if err := irc.WriteListNow([]string{
fmt.Sprintf(":whapp-irc 001 %s :Welcome to whapp-irc, %s.", irc.Nick(), irc.Nick()),
fmt.Sprintf(":whapp-irc 002 %s :Your host is whapp-irc.", irc.Nick()),
fmt.Sprintf(":whapp-irc 003 %s :This server was created %s.", irc.Nick(), startTime),
fmt.Sprintf(":whapp-irc 004 %s :", irc.Nick()),
fmt.Sprintf(":whapp-irc 005 %s PREFIX=(qo)~@ CHARSET=UTF-8 :are supported by this server", irc.Nick()),
fmt.Sprintf(":whapp-irc 375 %s :The server is running on commit %s", irc.Nick(), commit),
fmt.Sprintf(":whapp-irc 372 %s :Enjoy the ride.", irc.Nick()),
fmt.Sprintf(":whapp-irc 376 %s :End of /MOTD command.", irc.Nick()),
}); err != nil {
return err
}
var user types.User
if found, err := userDb.GetItem(
irc.Nick(),
&user,
); err == nil && found && user.Password != "" {
// we've found an user with a password, so the current connection should
// also provide a password.
// passErr notifies the connection that the provided password is
// incorrect, or none have been provided but should've been.
passErr := func(goodErr error) error {
msg := fmt.Sprintf(":whapp-irc 464 %s :Password incorrect", irc.Nick())
if err := irc.WriteNow(msg); err != nil {
return err
}
return goodErr
}
select {
case <-ctx.Done():
return nil
case <-time.After(5 * time.Second):
err := fmt.Errorf("password expected, but client timed out")
return passErr(err)
case <-irc.PassSetChannel():
if irc.Pass() != user.Password {
err := fmt.Errorf("client provided password incorrect")
return passErr(err)
}
}
}
// setup bridge and connection
conn, err := setupConnection(ctx, irc)
if err != nil {
irc.Status("error setting up whapp bridge: " + err.Error())
return err
}
// now that we have set-up the bridge...
// actually handle most of the IRC messages
go func() {
defer cancel()
ircReceiveCh := conn.irc.ReceiveChannel()
for {
select {
case <-ctx.Done():
return
case msg, ok := <-ircReceiveCh:
if !ok {
return
}
if err := conn.handleIRCCommand(ctx, msg); err != nil {
log.Printf("error handling new irc message: %s\n", err)
if err == io.ErrClosedPipe {
return
}
continue
}
}
}
}()
// we want to wait until we've finished negotiation, since when we send a
// replay we want to know if the user has servertime and even if they want a
// replay at all.
// if negotiation hasn't started yet, we just skip through (we figure the
// client doesn't support IRCv3, since normally negotiation occurs fairly
// early in the connection)
started, ok := conn.irc.Caps.WaitNegotiation(ctx)
if !ok {
return nil
} else if !started {
str := "IRCv3 capabilities negotiation has not started, " +
"this is probably a non IRCv3 compatible client."
log.Printf(str)
}
// replay older messages
empty := conn.timestampMap.Length() == 0
for _, item := range conn.Chats.List(false) {
c := item.Chat
prevTimestamp, found := conn.timestampMap.Get(c.ID)
if empty || !conn.hasReplay() {
conn.timestampMap.Set(c.ID, c.RawChat.Timestamp)
continue
} else if c.RawChat.Timestamp <= prevTimestamp {
continue
}
if !found {
// fetch all older messages
prevTimestamp = math.MinInt64
}
messages, err := c.RawChat.GetMessagesFromChatTillDate(
ctx,
conn.WI,
prevTimestamp,
)
if err != nil {
log.Printf("error while loading earlier messages: %s\n", err.Error())
return err
}
for _, msg := range messages {
if msg.Timestamp <= prevTimestamp {
continue
}
err := conn.handleWhappMessageReplay(ctx, msg)
util.LogIfErr("error handling older whapp message", err)
}
}
go conn.saveDatabaseEntry()
conn.irc.Status("ready for new messages")
// handle logging out on whatsapp web, this happens when the user removes
// the bridge client on their phone.
go func() {
defer cancel()
resCh, errCh := conn.WI.ListenLoggedIn(ctx, 3*time.Second)
for {
select {
case <-ctx.Done():
return
case err := <-errCh:
util.LogIfErr("error while listening for whatsapp loggedin state", err)
return
case res := <-resCh:
if res {
continue
}
conn.irc.Status("logged out of whatsapp")
return
}
}
}()
// listen for new WhatsApp messages
go func() {
defer cancel()
messageCh, errCh := conn.WI.ListenForMessages(
ctx,
500*time.Millisecond,
)
queue := GetMessageQueue(ctx, messageCh, 50)
for {
select {
case <-ctx.Done():
return
case err := <-errCh:
util.LogIfErr("error while listening for whatsapp messages", err)
return
case msgRes := <-<-queue:
if msgRes.Err == nil {
msgRes.Err = conn.handleWhappMessage(
ctx,
msgRes.Message,
handlerNormal,
)
}
util.LogIfErr("error handling new whapp message", msgRes.Err)
}
}
}()
// now just wait until we have to shutdown.
<-ctx.Done()
log.Printf("connection ended: %s\n", ctx.Err())
return nil
}
func (conn *Connection) joinChat(item types.ChatListItem) error {
chat := item.Chat
// sanity checks
if chat == nil {
return fmt.Errorf("chat is nil")
} else if !chat.IsGroupChat {
return fmt.Errorf("not a group chat")
} else if chat.Joined {
return nil
}
// identifier sanity check
identifier := item.Identifier
if identifier == "" || identifier == "#" {
return fmt.Errorf("identifier is empty, chat.Name is %s", chat.Name)
}
// send JOIN to client
str := fmt.Sprintf(":%s JOIN %s", conn.irc.Nick(), identifier)
if err := conn.irc.WriteNow(str); err != nil {
return err
}
// send chat name and description (if any) as topic
topic := fmt.Sprintf(":whapp-irc 332 %s %s :%s", conn.irc.Nick(), identifier, chat.Name)
if desc := chat.RawChat.Description; desc != nil {
if d := strings.TrimSpace(desc.Description); d != "" {
d = strings.Replace(d, "\n", " ", -1)
topic = fmt.Sprintf("%s: %s", topic, d)
}
}
conn.irc.WriteNow(topic)
// send chat members to client
names := make([]string, 0)
for _, participant := range chat.Participants {
if participant.Contact.IsMe {
if participant.IsSuperAdmin {
conn.irc.WriteNow(fmt.Sprintf(":whapp-irc MODE %s +q %s", identifier, conn.irc.Nick()))
} else if participant.IsAdmin {
conn.irc.WriteNow(fmt.Sprintf(":whapp-irc MODE %s +o %s", identifier, conn.irc.Nick()))
}
continue
}
prefix := ""
if participant.IsSuperAdmin {
prefix = "~"
} else if participant.IsAdmin {
prefix = "@"
}
names = append(names, prefix+participant.SafeName())
}
str = fmt.Sprintf(":whapp-irc 353 %s @ %s :%s", conn.irc.Nick(), identifier, strings.Join(names, " "))
if err := conn.irc.WriteNow(str); err != nil {
return err
}
str = fmt.Sprintf(":whapp-irc 366 %s %s :End of /NAMES list.", conn.irc.Nick(), identifier)
if err := conn.irc.WriteNow(str); err != nil {
return err
}
chat.Joined = true
return nil
}
func (conn *Connection) convertChat(
chat whapp.Chat,
participants []whapp.Participant,
) *types.Chat {
converted := make([]types.Participant, len(participants))
for i, p := range participants {
converted[i] = types.Participant(p)
}
return &types.Chat{
ID: chat.ID,
Name: chat.Title(),
IsGroupChat: chat.IsGroupChat,
Participants: converted,
RawChat: chat,
}
}
func (conn *Connection) addChat(chat *types.Chat) types.ChatListItem {
item, isNew := conn.Chats.Add(chat)
if isNew {
go conn.saveDatabaseEntry()
}
if item.Chat.IsGroupChat {
log.Printf(
"%-30s %3d participants\n",
item.Identifier,
len(item.Chat.Participants),
)
} else {
log.Println(item.Identifier)
}
return item
}
func (conn *Connection) saveDatabaseEntry() error {
err := userDb.SaveItem(conn.irc.Nick(), types.User{
Password: conn.irc.Pass(),
LocalStorage: conn.localStorage,
LastReceivedReceipts: conn.timestampMap.GetCopy(),
Chats: conn.Chats.List(true),
})
util.LogIfErr("error while updating user entry", err)
return err
}