-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathevents.go
483 lines (431 loc) · 11.5 KB
/
events.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package fbmsgr
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/url"
"strconv"
"sync"
"time"
"github.com/unixpickle/essentials"
)
const pollErrTimeout = time.Second * 5
// An Event is a notification pushed to the client by the
// server.
type Event interface{}
// A MessageEvent is an Event containing a new message.
type MessageEvent struct {
// MessageID is a unique ID used to distinguish a message
// from others in a chat log.
MessageID string
// Body is the text in the message.
// It is "" if the message contains no text.
Body string
// Attachments contains the message's attachments.
Attachments []Attachment
// SenderFBID is the fbid of the sending user.
// This may be the current user, especially if the user
// sent the message from a different device.
SenderFBID string
// If non-empty, this specifies the group chat ID.
GroupThread string
// If non-empty, this specifies the other user in a
// one-on-one chat (as opposed to a group chat).
OtherUser string
}
// A BuddyEvent is an Event containing information about a
// buddy's updated status.
type BuddyEvent struct {
FBID string
LastActive time.Time
}
// A TypingEvent indicates that a user has started or
// stopped typing.
type TypingEvent struct {
// SenderFBID is the user who is typing.
SenderFBID string
// Typing indicates whether or not the user is typing.
Typing bool
// If non-empty, this specifies the group chat ID.
GroupThread string
}
// DeleteMessageEvent indicates that a message has been
// deleted.
type DeleteMessageEvent struct {
MessageIDs []string
// If non-empty, this specifies the group chat ID.
GroupThread string
// If non-empty, this specifies the other user in a
// one-on-one chat (as opposed to a group chat).
OtherUser string
}
// An EventStream is a live stream of events.
//
// Create an event stream using Session.EventStream().
// Destroy an event stream using EventStream.Close().
type EventStream struct {
session *Session
evtChan chan Event
ctx context.Context
cancel context.CancelFunc
lock sync.RWMutex
err error
closed bool
}
func newEventStream(s *Session, closed bool) *EventStream {
res := &EventStream{
session: s,
evtChan: make(chan Event, 1),
closed: closed,
}
res.ctx, res.cancel = context.WithCancel(context.Background())
if closed {
res.cancel()
close(res.evtChan)
} else {
go res.poll()
}
return res
}
// Chan returns a channel of events for the stream.
//
// The channel is closed if the stream is closed or if an
// error is encountered.
func (e *EventStream) Chan() <-chan Event {
return e.evtChan
}
// Error returns the first error encountered while reading
// the stream.
func (e *EventStream) Error() error {
e.lock.RLock()
defer e.lock.RUnlock()
return e.err
}
// Close closes the stream.
//
// This will cause the event channel to be closed.
// However, the result from Error() will not be changed.
func (e *EventStream) Close() error {
e.lock.Lock()
defer e.lock.Unlock()
if e.closed {
return nil
}
e.closed = true
e.cancel()
return nil
}
func (e *EventStream) poll() {
defer close(e.evtChan)
host, err := e.callReconnect()
if err != nil {
e.pollFailed(errors.New("reconnect: " + err.Error()))
return
}
pool, token, err := e.fetchPollingInfo(host)
if err != nil {
e.pollFailed(err)
return
}
var seq int
startTime := time.Now().Unix()
for !e.checkClosed() {
values := url.Values{}
values.Set("cap", "8")
values.Set("cb", "anuk")
values.Set("channel", "p_"+e.session.userID)
values.Set("clientid", "3342de8f")
values.Set("idle", strconv.FormatInt(time.Now().Unix()-startTime, 10))
values.Set("isq", "243")
values.Set("msgr_region", "FRC")
values.Set("msgs_recv", strconv.Itoa(seq))
values.Set("partition", "-2")
values.Set("pws", "fresh")
values.Set("qp", "y")
values.Set("seq", strconv.Itoa(seq))
values.Set("state", "offline")
values.Set("uid", e.session.userID)
values.Set("viewer_uid", e.session.userID)
values.Set("sticky_pool", pool)
values.Set("sticky_token", token)
u := "https://0-edge-chat.messenger.com/pull?" + values.Encode()
response, err := e.session.jsonForGetContext(e.ctx, u)
if e.checkClosed() {
return
}
if err != nil {
time.Sleep(pollErrTimeout)
continue
}
msgs, newSeq, err := parseMessages(response)
if newSeq > 0 {
seq = newSeq
}
if err != nil {
time.Sleep(pollErrTimeout)
} else {
e.dispatchMessages(msgs)
}
}
}
func (e *EventStream) dispatchMessages(msgs []map[string]interface{}) {
for _, m := range msgs {
t, ok := m["type"].(string)
if !ok {
continue
}
switch t {
case "delta":
e.dispatchDelta(m)
case "buddylist_overlay":
e.dispatchBuddylistOverlay(m)
case "ttyp", "typ":
e.dispatchTyping(m)
}
}
}
func (e *EventStream) dispatchDelta(obj map[string]interface{}) {
var deltaObj struct {
Delta struct {
Class string `json:"class"`
// For message events.
Body string `json:"body"`
Attachments []map[string]interface{} `json:"attachments"`
Meta struct {
Actor string `json:"actorFbId"`
MessageID string `json:"messageId"`
ThreadKey struct {
ThreadFBID string `json:"threadFbId"`
OtherUser string `json:"otherUserFbId"`
} `json:"threadKey"`
} `json:"messageMetadata"`
// For delete events.
MessageIDs []string `json:"messageIds"`
ThreadKey struct {
ThreadFBID string `json:"threadFbId"`
OtherUser string `json:"otherUserFbId"`
} `json:"threadKey"`
} `json:"delta"`
}
if putJSONIntoObject(obj, &deltaObj) != nil {
return
}
if deltaObj.Delta.Class == "MessageDelete" {
e.emitEvent(DeleteMessageEvent{
MessageIDs: deltaObj.Delta.MessageIDs,
GroupThread: deltaObj.Delta.ThreadKey.ThreadFBID,
OtherUser: deltaObj.Delta.ThreadKey.OtherUser,
})
return
}
if (len(deltaObj.Delta.Attachments) == 0 && deltaObj.Delta.Body == "") ||
deltaObj.Delta.Class != "NewMessage" {
return
}
var attachments []Attachment
for _, a := range deltaObj.Delta.Attachments {
attachments = append(attachments, decodeAttachment(a))
}
e.emitEvent(MessageEvent{
MessageID: deltaObj.Delta.Meta.MessageID,
Body: deltaObj.Delta.Body,
Attachments: attachments,
SenderFBID: deltaObj.Delta.Meta.Actor,
GroupThread: deltaObj.Delta.Meta.ThreadKey.ThreadFBID,
OtherUser: deltaObj.Delta.Meta.ThreadKey.OtherUser,
})
}
func (e *EventStream) dispatchBuddylistOverlay(obj map[string]interface{}) {
var deltaObj struct {
Overlay map[string]struct {
LastActive float64 `json:"la"`
} `json:"overlay"`
}
if putJSONIntoObject(obj, &deltaObj) != nil {
return
}
for user, info := range deltaObj.Overlay {
e.emitEvent(BuddyEvent{
FBID: user,
LastActive: time.Unix(int64(info.LastActive), 0),
})
}
}
func (e *EventStream) dispatchTyping(m map[string]interface{}) {
var obj struct {
State int `json:"st"`
From float64 `json:"from"`
ThreadFBID float64 `json:"thread_fbid"`
Type string `json:"type"`
}
if putJSONIntoObject(m, &obj) != nil {
return
}
if obj.Type == "ttyp" {
e.emitEvent(TypingEvent{
SenderFBID: floatIDToString(obj.From),
Typing: obj.State == 1,
GroupThread: floatIDToString(obj.ThreadFBID),
})
} else {
e.emitEvent(TypingEvent{
SenderFBID: floatIDToString(obj.From),
Typing: obj.State == 1,
})
}
}
func (e *EventStream) checkClosed() bool {
select {
case <-e.ctx.Done():
return true
default:
return false
}
}
func (e *EventStream) emitEvent(evt Event) {
select {
case e.evtChan <- evt:
case <-e.ctx.Done():
}
}
func (e *EventStream) pollFailed(err error) {
e.lock.Lock()
e.err = err
e.lock.Unlock()
}
func (e *EventStream) fetchPollingInfo(host string) (stickyPool, stickyToken string, err error) {
values := url.Values{}
values.Set("cap", "8")
cbStr := ""
e.session.randLock.Lock()
for i := 0; i < 4; i++ {
cbStr += string(byte(e.session.randGen.Intn(26)) + 'a')
}
e.session.randLock.Unlock()
values.Set("cb", cbStr)
values.Set("channel", "p_"+e.session.userID)
values.Set("clientid", "3342de8f")
values.Set("idle", "0")
values.Set("msgr_region", "FRC")
values.Set("msgs_recv", "0")
values.Set("partition", "-2")
values.Set("pws", "fresh")
values.Set("qp", "y")
values.Set("seq", "0")
values.Set("state", "offline")
values.Set("uid", e.session.userID)
values.Set("viewer_uid", e.session.userID)
u := "https://0-" + host + ".messenger.com/pull?" + values.Encode()
response, err := e.session.jsonForGet(u)
if err != nil {
return "", "", err
}
var respObj struct {
Type string `json:"t"`
LbInfo *struct {
Sticky string `json:"sticky"`
Pool string `json:"pool"`
} `json:"lb_info"`
}
if err := json.Unmarshal(response, &respObj); err != nil {
return "", "", errors.New("parse init JSON: " + err.Error())
}
if respObj.Type == "lb" && respObj.LbInfo != nil {
return respObj.LbInfo.Pool, respObj.LbInfo.Sticky, nil
}
return "", "", errors.New("unexpected initial polling response")
}
func (e *EventStream) callReconnect() (host string, err error) {
values, err := e.session.commonParams()
if err != nil {
return "", err
}
values.Set("reason", "6")
u := "https://www.messenger.com/ajax/presence/reconnect.php?" + values.Encode()
response, err := e.session.jsonForGet(u)
if err != nil {
return "", err
}
var respObj struct {
Payload struct {
Host string `json:"host"`
} `json:"payload"`
}
if err := json.Unmarshal(response, &respObj); err != nil {
return "", err
}
return respObj.Payload.Host, nil
}
// EventStream creates a new EventStream for the session.
//
// You must close the result when you are done with it.
func (s *Session) EventStream() *EventStream {
return newEventStream(s, false)
}
// ReadEvent reads the next event from a default event
// stream.
// The first call will create the default event stream.
//
// ReadEvent is present for backward-compatibility.
// You should consider using the EventStream API rather
// than ReadEvent.
//
// If the stream is closed or fails with an error, a nil
// event is returned with an error (io.EOF if the read
// only failed because the stream was closed).
func (s *Session) ReadEvent() (Event, error) {
s.defaultStreamLock.Lock()
if s.defaultStream == nil {
s.defaultStream = s.EventStream()
}
stream := s.defaultStream
s.defaultStreamLock.Unlock()
if evt, ok := <-stream.Chan(); ok {
return evt, nil
}
err := essentials.AddCtx("fbmsgr: read event", stream.Error())
if err == nil {
err = io.EOF
}
return nil, err
}
// Close cleans up the session's resources.
// Any running EventStreams created from this session
// should be closed separately.
//
// This closes the default event stream, meaning that all
// ReadEvent calls will fail after Close() is finished.
func (s *Session) Close() error {
s.defaultStreamLock.Lock()
if s.defaultStream != nil {
s.defaultStream.Close()
} else {
s.defaultStream = newEventStream(s, true)
}
s.defaultStreamLock.Unlock()
return nil
}
// parseMessages extracts all of the "msg" payloads from a
// polled event body.
func parseMessages(data []byte) (list []map[string]interface{}, newSeq int, err error) {
reader := json.NewDecoder(bytes.NewBuffer(data))
for reader.More() {
var objVal struct {
Type string `json:"t"`
Seq int `json:"seq"`
Messages []map[string]interface{} `json:"ms"`
}
if err := reader.Decode(&objVal); err != nil {
return nil, 0, err
}
if objVal.Seq > newSeq {
newSeq = objVal.Seq
}
if objVal.Type == "msg" {
list = append(list, objVal.Messages...)
}
}
return
}