meta-horizonn 1.1.7 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/listenMqtt.js CHANGED
@@ -1,926 +1,926 @@
1
- /* eslint-disable linebreak-style */
2
- 'use strict';
3
-
4
- const utils = require('../utils');
5
- const log = require('npmlog');
6
- const mqtt = require('mqtt');
7
- const WebSocket = require('ws');
8
- const HttpsProxyAgent = require('https-proxy-agent');
9
- const EventEmitter = require('events');
10
- const Duplexify = require('duplexify');
11
- const {
12
- Transform
13
- } = require('stream');
14
- var identity = function() {};
15
- var form = {};
16
- var getSeqID = function() {};
17
- global.Fca.Data.MsgCount = new Map();
18
- global.Fca.Data.event = new Map();
19
-
20
- const topics = ['/ls_req', '/ls_resp', '/legacy_web', '/webrtc', '/rtc_multi', '/onevc', '/br_sr', '/sr_res', '/t_ms', '/thread_typing', '/orca_typing_notifications', '/notify_disconnect', '/orca_presence', '/inbox', '/mercury', '/messaging_events', '/orca_message_notifications', '/pp', '/webrtc_response'];
21
-
22
- let WebSocket_Global;
23
-
24
- function buildProxy() {
25
- const Proxy = new Transform({
26
- objectMode: false,
27
- transform(chunk, enc, next) {
28
- if (WebSocket_Global.readyState !== WebSocket_Global.OPEN) {
29
- return next();
30
- }
31
-
32
- let data;
33
- if (typeof chunk === 'string') {
34
- data = Buffer.from(chunk, 'utf8');
35
- } else {
36
- data = chunk;
37
- }
38
-
39
- WebSocket_Global.send(data);
40
- next();
41
- },
42
- flush(done) {
43
- WebSocket_Global.close();
44
- done();
45
- },
46
- writev(chunks, cb) {
47
- const buffers = chunks.map(({ chunk }) => {
48
- if (typeof chunk === 'string') {
49
- return Buffer.from(chunk, 'utf8');
50
- }
51
- return chunk;
52
- });
53
- this._write(Buffer.concat(buffers), 'binary', cb);
54
- },
55
- });
56
- return Proxy;
57
- }
58
-
59
- function buildStream(options, WebSocket, Proxy) {
60
- const Stream = Duplexify(undefined, undefined, options);
61
- Stream.socket = WebSocket;
62
-
63
- WebSocket.onclose = () => {
64
- Stream.end();
65
- Stream.destroy();
66
- };
67
-
68
- WebSocket.onerror = (err) => {
69
- Stream.destroy(err);
70
- };
71
-
72
- WebSocket.onmessage = (event) => {
73
- const data = event.data instanceof ArrayBuffer ? Buffer.from(event.data) : Buffer.from(event.data, 'utf8');
74
- Stream.push(data);
75
- };
76
-
77
- WebSocket.onopen = () => {
78
- Stream.setReadable(Proxy);
79
- Stream.setWritable(Proxy);
80
- Stream.emit('connect');
81
- };
82
-
83
- WebSocket_Global = WebSocket;
84
- Proxy.on('close', () => WebSocket.close());
85
-
86
- return Stream;
87
- }
88
-
89
- function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
90
- const chatOn = ctx.globalOptions.online;
91
- const foreground = false;
92
-
93
- const sessionID = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + 1;
94
- const username = {
95
- u: ctx.userID,
96
- s: sessionID,
97
- chat_on: chatOn,
98
- fg: foreground,
99
- d: utils.getGUID(),
100
- ct: 'websocket',
101
- aid: '219994525426954',
102
- aids: null,
103
- mqtt_sid: '',
104
- cp: 3,
105
- ecp: 10,
106
- st: [],
107
- pm: [],
108
- dc: '',
109
- no_auto_fg: true,
110
- gas: null,
111
- pack: [],
112
- p: null,
113
- php_override: ""
114
- };
115
-
116
- const cookies = ctx.jar.getCookies('https://www.facebook.com').join('; ');
117
-
118
- let host;
119
- if (ctx.mqttEndpoint) {
120
- host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
121
- } else if (ctx.region) {
122
- host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLowerCase()}&sid=${sessionID}`;
123
- } else {
124
- host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
125
- }
126
-
127
- const options = {
128
- clientId: 'mqttwsclient',
129
- protocolId: 'MQIsdp',
130
- protocolVersion: 3,
131
- username: JSON.stringify(username),
132
- clean: true,
133
- wsOptions: {
134
- headers: {
135
- Cookie: cookies,
136
- Origin: 'https://www.facebook.com',
137
- 'User-Agent': ctx.globalOptions.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
138
- Referer: 'https://www.facebook.com/',
139
- Host: new URL(host).hostname,
140
- },
141
- origin: 'https://www.facebook.com',
142
- protocolVersion: 13,
143
- binaryType: 'arraybuffer',
144
- },
145
- keepalive: 60,
146
- reschedulePings: true,
147
- reconnectPeriod: 3,
148
- };
149
-
150
- if (ctx.globalOptions.proxy !== undefined) {
151
- const agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
152
- options.wsOptions.agent = agent;
153
- }
154
-
155
- ctx.mqttClient = new mqtt.Client(() => buildStream(options, new WebSocket(host, options.wsOptions), buildProxy()), options);
156
- global.mqttClient = ctx.mqttClient;
157
-
158
- global.mqttClient.on('error', (err) => {
159
- log.error('listenMqtt', err);
160
- global.mqttClient.end();
161
-
162
- if (ctx.globalOptions.autoReconnect) {
163
- getSeqID();
164
- } else {
165
- globalCallback({
166
- type: 'stop_listen',
167
- error: 'Server Đã Sập - Auto Restart'
168
- }, null);
169
- return process.exit(1);
170
- }
171
- });
172
-
173
- global.mqttClient.on('connect', () => {
174
- if (!global.Fca.Data.Setup || global.Fca.Data.Setup === undefined) {
175
- if (global.Fca.Require.FastConfig.RestartMQTT_Minutes !== 0 && global.Fca.Data.StopListening !== true) {
176
- global.Fca.Data.Setup = true;
177
- setTimeout(() => {
178
- global.Fca.Require.logger.Warning('Closing MQTT Client...');
179
- ctx.mqttClient.end();
180
- global.Fca.Require.logger.Warning('Reconnecting MQTT Client...');
181
- global.Fca.Data.Setup = false;
182
- getSeqID();
183
- }, Number(global.Fca.Require.FastConfig.RestartMQTT_Minutes) * 60 * 1000);
184
- }
185
- }
186
-
187
- if (process.env.OnStatus === undefined) {
188
- global.Fca.Require.logger.Normal('Bạn Đang Sài Phiên Bản: Premium Access');
189
-
190
- if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) === 0) {
191
- // something
192
- } else if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) < 10) {
193
- log.warn('AutoRestartMinutes', 'The number of minutes to automatically restart must be more than 10 minutes');
194
- } else if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) < 0) {
195
- log.warn('AutoRestartMinutes', 'Invalid auto-restart minutes!');
196
- } else {
197
- global.Fca.Require.logger.Normal(global.Fca.getText(global.Fca.Require.Language.Src.AutoRestart, global.Fca.Require.FastConfig.AutoRestartMinutes));
198
- global.Fca.Require.logger.Normal(`Auto Restart MQTT Client After: ${global.Fca.Require.FastConfig.RestartMQTT_Minutes} Minutes`);
199
- setTimeout(() => {
200
- global.Fca.Require.logger.Normal(global.Fca.Require.Language.Src.OnRestart);
201
- process.exit(1);
202
- }, Number(global.Fca.Require.FastConfig.AutoRestartMinutes) * 60000);
203
- }
204
- require('../broadcast').startBroadcasting();
205
- const MemoryManager = require('../Extra/Src/Release_Memory');
206
- const path = require('path');
207
-
208
- const SettingMemoryManager = {
209
- warningThreshold: 0.7,
210
- releaseThreshold: 0.8,
211
- maxThreshold: 0.9,
212
- interval: 60 * 1000,
213
- logLevel: 'warn',
214
- logFile: path.join(process.cwd(), 'Horizon_Database' ,'memory.log'),
215
- smartReleaseEnabled: true,
216
- allowLog: (global.Fca.Require.FastConfig.AntiStuckAndMemoryLeak.LogFile.Use || false)
217
- };
218
-
219
- const memoryManager = new MemoryManager(SettingMemoryManager);
220
-
221
- memoryManager.autoStart(60 * 60 * 1000);
222
-
223
- if (global.Fca.Require.FastConfig.AntiStuckAndMemoryLeak.AutoRestart.Use) {
224
- memoryManager.onMaxMemory(function() {
225
- global.Fca.Require.logger.Warning('Memory Usage >= 90% - Auto Restart Avoid Crash');
226
- process.exit(1);
227
- });
228
- }
229
- process.env.OnStatus = true;
230
- }
231
-
232
- topics.forEach((topicsub) => global.mqttClient.subscribe(topicsub));
233
-
234
-
235
- let topic;
236
- const queue = {
237
- sync_api_version: 11,
238
- max_deltas_able_to_process: 100,
239
- delta_batch_size: 500,
240
- encoding: 'JSON',
241
- entity_fbid: ctx.userID,
242
- };
243
-
244
- topic = "/messenger_sync_create_queue";
245
- queue.initial_titan_sequence_id = ctx.lastSeqId;
246
- queue.device_params = null;
247
-
248
- global.mqttClient.publish(topic, JSON.stringify(queue), {
249
- qos: 1,
250
- retain: false
251
- });
252
-
253
- var rTimeout = setTimeout(function() {
254
- global.mqttClient.end();
255
- getSeqID();
256
- }, 3000);
257
-
258
- ctx.tmsWait = function() {
259
- clearTimeout(rTimeout);
260
- ctx.globalOptions.emitReady ? globalCallback({
261
- type: "ready",
262
- error: null
263
- }) : '';
264
- delete ctx.tmsWait;
265
- };
266
- });
267
-
268
- const HandleMessage = function(topic, message, _packet) {
269
- const jsonMessage = JSON.parse(message.toString());
270
- if (topic === "/t_ms") {
271
- if (ctx.tmsWait && typeof ctx.tmsWait == "function") ctx.tmsWait();
272
-
273
- if (jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
274
- ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
275
- ctx.syncToken = jsonMessage.syncToken;
276
- }
277
-
278
- if (jsonMessage.lastIssuedSeqId) ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
279
- //If it contains more than 1 delta
280
- for (var i in jsonMessage.deltas) {
281
- var delta = jsonMessage.deltas[i];
282
- parseDelta(defaultFuncs, api, ctx, globalCallback, {
283
- "delta": delta
284
- });
285
- }
286
- } else if (topic === "/ls_resp") {
287
- const payload = JSON.parse(jsonMessage.payload); //'{"name":null,"step":[1,[1,[4,0,1,[5,"taskExists",[19,"415"]]],[23,[2,0],[1,[5,"replaceOptimsiticMessage","7192532113093667880","mid.$gABfX5li9LA6VdUymnWPRAdlkiawo"]]]],[1,[4,0,1,[5,"taskExists",[19,"415"]]],[23,[2,0],[1,[5,"mailboxTaskCompletionApiOnTaskCompletion",[19,"415"],true]]]],[1,[4,0,1,[5,"taskExists",[19,"415"]]],[23,[2,0],[1,[5,"removeTask",[19,"415"],[9]]]]]]}'
288
- const request_ID = jsonMessage.request_id;
289
-
290
- if (ctx.callback_Task[request_ID] != undefined && ctx.callback_Task[request_ID].type != undefined) {
291
- const {
292
- callback,
293
- type
294
- } = ctx.callback_Task[request_ID];
295
- const Data = new getRespData(type, payload);
296
- if (!callback) {
297
- return;
298
- }
299
- else if (!Data) {
300
- callback("Something went wrong 🐳", null);
301
- } else {
302
- callback(null, Data);
303
- }
304
- }
305
- } else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
306
- var typ = {
307
- type: "typ",
308
- isTyping: !!jsonMessage.state,
309
- from: jsonMessage.sender_fbid.toString(),
310
- threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
311
- };
312
- (function() {
313
- globalCallback(null, typ);
314
- })();
315
- } else if (topic === "/orca_presence") {
316
- if (!ctx.globalOptions.updatePresence) {
317
- for (var i in jsonMessage.list) {
318
- var data = jsonMessage.list[i];
319
- var userID = data["u"];
320
-
321
- var presence = {
322
- type: "presence",
323
- userID: userID.toString(),
324
- // Convert to ms
325
- timestamp: data["l"] * 1000,
326
- statuses: data["p"]
327
- };
328
- (function() {
329
- globalCallback(null, presence);
330
- })();
331
- }
332
- }
333
- }
334
-
335
- };
336
-
337
- global.mqttClient.on('message', HandleMessage);
338
-
339
- process.on('SIGINT', () => {
340
- LogUptime();
341
- process.kill(process.pid);
342
- });
343
- process.on('exit', LogUptime);
344
- }
345
-
346
- function getRespData(Type, payload) {
347
- try {
348
- switch (Type) {
349
- case "sendMqttMessage": {
350
- return {
351
- type: Type,
352
- threadID: payload.step[1][2][2][1][2], //this is sick bro
353
- messageID: payload.step[1][2][2][1][3],
354
- payload: payload.step[1][2]
355
- };
356
- }
357
- default: { //!very LAZY :> cook yourself
358
- return {
359
- Data: payload.step[1][2][2][1],
360
- type: Type,
361
- payload: payload.step[1][2]
362
- };
363
- }
364
- }
365
- } catch (e) {
366
- return null;
367
- }
368
- }
369
-
370
- function LogUptime() {
371
- const uptime = process.uptime();
372
- const {
373
- join
374
- } = require('path');
375
- const filePath = join(__dirname, '../CountTime.json');
376
-
377
- let time1;
378
- if (global.Fca.Require.fs.existsSync(filePath)) {
379
- time1 = Number(global.Fca.Require.fs.readFileSync(filePath, 'utf8')) || 0;
380
- } else {
381
- time1 = 0;
382
- }
383
-
384
- global.Fca.Require.fs.writeFileSync(filePath, String(Number(uptime) + time1), 'utf8');
385
- }
386
-
387
- if (global.Fca.Require.FastConfig.AntiGetInfo.AntiGetThreadInfo) {
388
- setInterval(() => {
389
- try {
390
- const { updateMessageCount, getData, hasData } = require('../Extra/ExtraGetThread');
391
- const Data = global.Fca.Data.MsgCount;
392
- const Arr = Array.from(Data.keys());
393
- for (let i of Arr) {
394
- const Count = parseInt(Data.get(i));
395
- if (hasData(i)) {
396
- let x = getData(i);
397
- x.messageCount += Count;
398
- updateMessageCount(i, x);
399
- Data.delete(i);
400
- }
401
- }
402
-
403
- } catch (e) {
404
- console.log(e);
405
- }
406
- }, 30 * 1000);
407
- }
408
-
409
- function parseDelta(defaultFuncs, api, ctx, globalCallback, {
410
- delta
411
- }) {
412
- if (delta.class === 'NewMessage') {
413
- if (ctx.globalOptions.pageID && ctx.globalOptions.pageID !== delta.queue) return;
414
-
415
- const resolveAttachmentUrl = (i) => {
416
- if (!delta.attachments || i === delta.attachments.length || utils.getType(delta.attachments) !== 'Array') {
417
- let fmtMsg;
418
- try {
419
- fmtMsg = utils.formatDeltaMessage(delta);
420
- } catch (err) {
421
- return log.error('Lỗi Nhẹ', err);
422
- }
423
-
424
- if (fmtMsg) {
425
- const isGroup = fmtMsg.isGroup;
426
- const threadID = fmtMsg.threadID;
427
- const messageID = fmtMsg.messageID;
428
-
429
- global.Fca.Data.event.set("Data", {
430
- isGroup,
431
- threadID,
432
- messageID
433
- });
434
-
435
- if (global.Fca.Require.FastConfig.AntiGetInfo.AntiGetThreadInfo) {
436
- global.Fca.Data.MsgCount.set(fmtMsg.threadID, ((global.Fca.Data.MsgCount.get(fmtMsg.threadID)) + 1 || 1));
437
- }
438
-
439
- if (ctx.globalOptions.autoMarkDelivery) {
440
- markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
441
- }
442
-
443
- if (!ctx.globalOptions.selfListen && fmtMsg.senderID === ctx.userID) return;
444
- globalCallback(null, fmtMsg);
445
- }
446
- } else {
447
- const attachment = delta.attachments[i];
448
- if (attachment.mercury.attach_type === 'photo') {
449
- api.resolvePhotoUrl(attachment.fbid, (err, url) => {
450
- if (!err) attachment.mercury.metadata.url = url;
451
- resolveAttachmentUrl(i + 1);
452
- });
453
- } else {
454
- resolveAttachmentUrl(i + 1);
455
- }
456
- }
457
- };
458
-
459
- resolveAttachmentUrl(0);
460
- } else if (delta.class === 'ClientPayload') {
461
- const clientPayload = utils.decodeClientPayload(delta.payload);
462
- if (clientPayload && clientPayload.deltas) {
463
- for (const delta of clientPayload.deltas) {
464
- if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
465
- const messageReaction = {
466
- type: 'message_reaction',
467
- threadID: (delta.deltaMessageReaction.threadKey.threadFbId ? delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey.otherUserFbId).toString(),
468
- messageID: delta.deltaMessageReaction.messageId,
469
- reaction: delta.deltaMessageReaction.reaction,
470
- senderID: delta.deltaMessageReaction.senderId.toString(),
471
- userID: delta.deltaMessageReaction.userId.toString(),
472
- };
473
- globalCallback(null, messageReaction);
474
- } else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
475
- const messageUnsend = {
476
- type: 'message_unsend',
477
- threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ? delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey.otherUserFbId).toString(),
478
- messageID: delta.deltaRecallMessageData.messageID,
479
- senderID: delta.deltaRecallMessageData.senderID.toString(),
480
- deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
481
- timestamp: delta.deltaRecallMessageData.timestamp,
482
- };
483
- globalCallback(null, messageUnsend);
484
- } else if (delta.deltaMessageReply) {
485
- const mdata =
486
- delta.deltaMessageReply.message === undefined ?
487
- [] :
488
- delta.deltaMessageReply.message.data === undefined ?
489
- [] :
490
- delta.deltaMessageReply.message.data.prng === undefined ?
491
- [] :
492
- JSON.parse(delta.deltaMessageReply.message.data.prng);
493
-
494
- const m_id = mdata.map((u) => u.i);
495
- const m_offset = mdata.map((u) => u.o);
496
- const m_length = mdata.map((u) => u.l);
497
-
498
- const mentions = {};
499
- for (let i = 0; i < m_id.length; i++) {
500
- mentions[m_id[i]] = (delta.deltaMessageReply.message.body || '').substring(m_offset[i], m_offset[i] + m_length[i]);
501
- }
502
-
503
- const callbackToReturn = {
504
- type: 'message_reply',
505
- threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey.otherUserFbId).toString(),
506
- messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
507
- senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
508
- attachments: ( delta.deltaMessageReply.message.attachments || [] )
509
- .map((att) => {
510
- const mercury = JSON.parse(att.mercuryJSON);
511
- Object.assign(att, mercury);
512
- return att;
513
- })
514
- .map((att) => {
515
- let x;
516
- try {
517
- x = utils._formatAttachment(att);
518
- } catch (ex) {
519
- x = att;
520
- x.error = ex;
521
- x.type = 'unknown';
522
- }
523
- return x;
524
- }),
525
- args: (delta.deltaMessageReply.message.body || '').trim().split(/\s+/),
526
- body: delta.deltaMessageReply.message.body || '',
527
- isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
528
- mentions,
529
- timestamp: parseInt(delta.deltaMessageReply.message.messageMetadata.timestamp),
530
- participantIDs: (delta.deltaMessageReply.message.participants || []).map((e) => e.toString()),
531
- };
532
-
533
- if (delta.deltaMessageReply.repliedToMessage) {
534
- const mdata =
535
- delta.deltaMessageReply.repliedToMessage === undefined ?
536
- [] :
537
- delta.deltaMessageReply.repliedToMessage.data === undefined ?
538
- [] :
539
- delta.deltaMessageReply.repliedToMessage.data.prng === undefined ?
540
- [] :
541
- JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
542
-
543
- const m_id = mdata.map((u) => u.i);
544
- const m_offset = mdata.map((u) => u.o);
545
- const m_length = mdata.map((u) => u.l);
546
-
547
- const rmentions = {};
548
- for (let i = 0; i < m_id.length; i++) {
549
- rmentions[m_id[i]] = (delta.deltaMessageReply.repliedToMessage.body || '').substring(m_offset[i], m_offset[i] + m_length[i]);
550
- }
551
-
552
- callbackToReturn.messageReply = {
553
- threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.otherUserFbId).toString(),
554
- messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
555
- senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
556
- attachments: delta.deltaMessageReply.repliedToMessage.attachments
557
- .map((att) => {
558
- const mercury = JSON.parse(att.mercuryJSON);
559
- Object.assign(att, mercury);
560
- return att;
561
- })
562
- .map((att) => {
563
- let x;
564
- try {
565
- x = utils._formatAttachment(att);
566
- } catch (ex) {
567
- x = att;
568
- x.error = ex;
569
- x.type = 'unknown';
570
- }
571
- return x;
572
- }),
573
- args: (delta.deltaMessageReply.repliedToMessage.body || '').trim().split(/\s+/),
574
- body: delta.deltaMessageReply.repliedToMessage.body || '',
575
- isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
576
- mentions: rmentions,
577
- timestamp: parseInt(delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp),
578
- participantIDs: (delta.deltaMessageReply.repliedToMessage.participants || []).map((e) => e.toString()),
579
- };
580
- } else if (delta.deltaMessageReply.replyToMessageId) {
581
- return defaultFuncs
582
- .post('https://www.facebook.com/api/graphqlbatch/', ctx.jar, {
583
- av: ctx.globalOptions.pageID,
584
- queries: JSON.stringify({
585
- o0: {
586
- doc_id: '2848441488556444',
587
- query_params: {
588
- thread_and_message_id: {
589
- thread_id: callbackToReturn.threadID,
590
- message_id: delta.deltaMessageReply.replyToMessageId.id,
591
- },
592
- },
593
- },
594
- }),
595
- })
596
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
597
- .then((resData) => {
598
- if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
599
- if (resData[resData.length - 1].successful_results === 0) throw {
600
- error: 'forcedFetch: there was no successful_results',
601
- res: resData
602
- };
603
- const fetchData = resData[0].o0.data.message;
604
- const mobj = {};
605
-
606
- for (const n in fetchData.message.ranges) {
607
- mobj[fetchData.message.ranges[n].entity.id] = (fetchData.message.text || '').substr(fetchData.message.ranges[n].offset, fetchData.message.ranges[n].length);
608
- }
609
- callbackToReturn.messageReply = {
610
- type: 'Message',
611
- threadID: callbackToReturn.threadID,
612
- messageID: fetchData.message_id,
613
- senderID: fetchData.message_sender.id.toString(),
614
- attachments: fetchData.message.blob_attachment.map((att) => utils._formatAttachment({
615
- blob_attachment: att
616
- })),
617
- args: (fetchData.message.text || '').trim().split(/\s+/) || [],
618
- body: fetchData.message.text || '',
619
- isGroup: callbackToReturn.isGroup,
620
- mentions: mobj,
621
- timestamp: parseInt(fetchData.timestamp_precise),
622
- };
623
- })
624
-
625
- .catch((err) => log.error('forcedFetch', err))
626
- .finally(() => {
627
- if (ctx.globalOptions.autoMarkDelivery) {
628
- markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
629
- }
630
-
631
- if (!ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID) return;
632
- globalCallback(null, callbackToReturn);
633
- });
634
- } else {
635
- callbackToReturn.delta = delta;
636
- }
637
- if (ctx.globalOptions.autoMarkDelivery) {
638
- markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
639
- }
640
-
641
- if (!ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID) return;
642
- globalCallback(null, callbackToReturn);
643
- }
644
- }
645
-
646
- return;
647
- }
648
- }
649
- switch (delta.class) {
650
- case 'ReadReceipt': {
651
- let fmtMsg;
652
- try {
653
- fmtMsg = utils.formatDeltaReadReceipt(delta);
654
- } catch (err) {
655
- return log.error('Lỗi Nhẹ', err);
656
- }
657
- globalCallback(null, fmtMsg);
658
- break;
659
- }
660
- case 'AdminTextMessage': {
661
- switch (delta.type) {
662
- case 'joinable_group_link_mode_change':
663
- case 'magic_words':
664
- case 'pin_messages_v2':
665
- case 'change_thread_theme':
666
- case 'change_thread_icon':
667
- case 'change_thread_nickname':
668
- case 'change_thread_admins':
669
- case 'change_thread_approval_mode':
670
- case 'group_poll':
671
- case 'messenger_call_log':
672
- case 'participant_joined_group_call': {
673
- let fmtMsg;
674
- try {
675
- fmtMsg = utils.formatDeltaEvent(delta);
676
- } catch (err) {
677
- console.log(delta);
678
- return log.error('Lỗi Nhẹ', err);
679
- }
680
- globalCallback(null, fmtMsg);
681
- break;
682
- }
683
- }
684
- break;
685
- }
686
-
687
- //For group images
688
- case 'ForcedFetch': {
689
- if (!delta.threadKey) return;
690
- const mid = delta.messageId;
691
- const tid = delta.threadKey.threadFbId;
692
-
693
- if (mid && tid) {
694
- const form = {
695
- av: ctx.globalOptions.pageID,
696
- queries: JSON.stringify({
697
- o0: {
698
- doc_id: '2848441488556444',
699
- query_params: {
700
- thread_and_message_id: {
701
- thread_id: tid.toString(),
702
- message_id: mid,
703
- },
704
- },
705
- },
706
- }),
707
- };
708
- defaultFuncs
709
- .post('https://www.facebook.com/api/graphqlbatch/', ctx.jar, form)
710
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
711
- .then((resData) => {
712
- if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
713
-
714
- if (resData[resData.length - 1].successful_results === 0) throw {
715
- error: 'forcedFetch: there was no successful_results',
716
- res: resData
717
- };
718
-
719
- const fetchData = resData[0].o0.data.message;
720
-
721
- if (utils.getType(fetchData) === 'Object') {
722
- log.info('forcedFetch', fetchData);
723
- switch (fetchData.__typename) {
724
- case 'ThreadImageMessage':
725
- if (!ctx.globalOptions.selfListen && fetchData.message_sender.id.toString() === ctx.userID) return;
726
- if (!ctx.loggedIn) return;
727
-
728
- globalCallback(null, {
729
- type: 'change_thread_image',
730
- threadID: utils.formatID(tid.toString()),
731
- snippet: fetchData.snippet,
732
- timestamp: fetchData.timestamp_precise,
733
- author: fetchData.message_sender.id,
734
- image: {
735
- attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
736
- width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
737
- height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
738
- url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri,
739
- },
740
- });
741
- break;
742
- case 'UserMessage': {
743
- const event = {
744
- type: 'message',
745
- senderID: utils.formatID(fetchData.message_sender.id),
746
- body: fetchData.message.text || '',
747
- threadID: utils.formatID(tid.toString()),
748
- messageID: fetchData.message_id,
749
- attachments: [{
750
- type: 'share',
751
- ID: fetchData.extensible_attachment.legacy_attachment_id,
752
- url: fetchData.extensible_attachment.story_attachment.url,
753
- title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
754
- description: fetchData.extensible_attachment.story_attachment.description.text,
755
- source: fetchData.extensible_attachment.story_attachment.source,
756
- image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
757
- width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
758
- height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
759
- playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
760
- duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
761
- subattachments: fetchData.extensible_attachment.subattachments,
762
- properties: fetchData.extensible_attachment.story_attachment.properties,
763
- }],
764
- mentions: {},
765
- timestamp: parseInt(fetchData.timestamp_precise),
766
- isGroup: (fetchData.message_sender.id !== tid.toString()),
767
- };
768
-
769
- log.info('ff-Return', event);
770
- globalCallback(null, event);
771
- break;
772
- }
773
- default:
774
- log.error('forcedFetch', fetchData);
775
- }
776
- } else {
777
- log.error('forcedFetch', fetchData);
778
- }
779
- })
780
- .catch((err) => log.error('forcedFetch', err));
781
- }
782
- break;
783
- }
784
- case 'ThreadName':
785
- case 'ParticipantsAddedToGroupThread':
786
- case 'ParticipantLeftGroupThread': {
787
- let formattedEvent;
788
- try {
789
- formattedEvent = utils.formatDeltaEvent(delta);
790
- } catch (err) {
791
- console.log(err);
792
- return log.error('Lỗi Nhẹ', err);
793
- }
794
-
795
- if (!ctx.globalOptions.selfListen && formattedEvent.author.toString() === ctx.userID) return;
796
- if (!ctx.loggedIn) return;
797
- globalCallback(null, formattedEvent);
798
- break;
799
- }
800
- case 'NewMessage': {
801
- if (delta.attachments !== undefined && delta.attachments.length === 1 && delta.attachments[0].mercury.extensible_attachment !== undefined && delta.attachments[0].mercury.extensible_attachment.story_attachment.style_list != null && delta.attachments[0].mercury.extensible_attachment.story_attachment.style_list.includes('message_live_location')) {
802
- delta.class = 'UserLocation';
803
- let fmtMsg;
804
- try {
805
- fmtMsg = utils.formatDeltaEvent(delta);
806
- } catch (err) {
807
- console.log(delta);
808
- return log.error('Lỗi Nhẹ', err);
809
- }
810
- globalCallback(null, fmtMsg);
811
- }
812
- break;
813
- }
814
- }
815
- }
816
-
817
- function markDelivery(ctx, api, threadID, messageID) {
818
- if (threadID && messageID) {
819
- api.markAsDelivered(threadID, messageID, (err) => {
820
- if (err) log.error('markAsDelivered', err);
821
- else {
822
- if (ctx.globalOptions.autoMarkRead) {
823
- api.markAsRead(threadID, (err) => {
824
- if (err) log.error('markAsDelivered', err);
825
- });
826
- }
827
- }
828
- });
829
- }
830
- }
831
-
832
-
833
-
834
- module.exports = function(defaultFuncs, api, ctx) {
835
- var globalCallback = identity;
836
- getSeqID = function getSeqID() {
837
- ctx.t_mqttCalled = false;
838
- defaultFuncs
839
- .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
840
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
841
- .then((resData) => {
842
- if (utils.getType(resData) != "Array") {
843
- if (global.Fca.Require.FastConfig.AutoLogin) {
844
- return global.Fca.Require.logger.Warning(global.Fca.Require.Language.Index.AutoLogin, function() {
845
- return global.Fca.Action('AutoLogin');
846
- });
847
- } else if (!global.Fca.Require.FastConfig.AutoLogin) {
848
- return global.Fca.Require.logger.Error(global.Fca.Require.Language.Index.ErrAppState);
849
- }
850
- return;
851
- } else {
852
- if (resData && resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
853
- if (resData[resData.length - 1].successful_results === 0) throw {
854
- error: "getSeqId: there was no successful_results",
855
- res: resData
856
- };
857
- if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
858
- ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
859
- listenMqtt(defaultFuncs, api, ctx, globalCallback);
860
- } else throw {
861
- error: "getSeqId: no sync_sequence_id found.",
862
- res: resData
863
- };
864
- }
865
- })
866
- .catch((err) => {
867
- log.error("getSeqId", err);
868
- if (utils.getType(err) == "Object" && err.error === global.Fca.Require.Language.Index.ErrAppState) ctx.loggedIn = false;
869
- return globalCallback(err);
870
- });
871
- };
872
-
873
- return function(callback) {
874
- class MessageEmitter extends EventEmitter {
875
- stopListening(callback) {
876
- callback = callback || (() => {});
877
- globalCallback = identity;
878
- if (ctx.mqttClient) {
879
- ctx.mqttClient.unsubscribe("/webrtc");
880
- ctx.mqttClient.unsubscribe("/rtc_multi");
881
- ctx.mqttClient.unsubscribe("/onevc");
882
- ctx.mqttClient.publish("/browser_close", "{}");
883
- ctx.mqttClient.end(false, function(...data) {
884
- ctx.mqttClient = undefined;
885
- });
886
- }
887
- global.Fca.Data.StopListening = true;
888
- }
889
- }
890
-
891
- var msgEmitter = new MessageEmitter();
892
- globalCallback = (callback || function(error, message) {
893
- if (error) return msgEmitter.emit("error", error);
894
- msgEmitter.emit("message", message);
895
- });
896
-
897
- //Reset some stuff
898
- if (!ctx.firstListen) ctx.lastSeqId = null;
899
- ctx.syncToken = undefined;
900
- ctx.t_mqttCalled = false;
901
-
902
- //Same request as getThreadList
903
- form = {
904
- av: ctx.globalOptions.pageID,
905
- queries: JSON.stringify({
906
- o0: {
907
- doc_id: '3336396659757871',
908
- query_params: {
909
- limit: 1,
910
- before: null,
911
- tags: ['INBOX'],
912
- includeDeliveryReceipts: false,
913
- includeSeqID: true,
914
- },
915
- },
916
- }),
917
- };
918
-
919
-
920
- if (!ctx.firstListen || !ctx.lastSeqId) getSeqID();
921
- else listenMqtt(defaultFuncs, api, ctx, globalCallback);
922
- ctx.firstListen = false;
923
-
924
- return msgEmitter;
925
- };
1
+ /* eslint-disable linebreak-style */
2
+ 'use strict';
3
+
4
+ const utils = require('../utils');
5
+ const log = require('npmlog');
6
+ const mqtt = require('mqtt');
7
+ const WebSocket = require('ws');
8
+ const HttpsProxyAgent = require('https-proxy-agent');
9
+ const EventEmitter = require('events');
10
+ const Duplexify = require('duplexify');
11
+ const {
12
+ Transform
13
+ } = require('stream');
14
+ var identity = function() {};
15
+ var form = {};
16
+ var getSeqID = function() {};
17
+ global.Fca.Data.MsgCount = new Map();
18
+ global.Fca.Data.event = new Map();
19
+
20
+ const topics = ['/ls_req', '/ls_resp', '/legacy_web', '/webrtc', '/rtc_multi', '/onevc', '/br_sr', '/sr_res', '/t_ms', '/thread_typing', '/orca_typing_notifications', '/notify_disconnect', '/orca_presence', '/inbox', '/mercury', '/messaging_events', '/orca_message_notifications', '/pp', '/webrtc_response'];
21
+
22
+ let WebSocket_Global;
23
+
24
+ function buildProxy() {
25
+ const Proxy = new Transform({
26
+ objectMode: false,
27
+ transform(chunk, enc, next) {
28
+ if (WebSocket_Global.readyState !== WebSocket_Global.OPEN) {
29
+ return next();
30
+ }
31
+
32
+ let data;
33
+ if (typeof chunk === 'string') {
34
+ data = Buffer.from(chunk, 'utf8');
35
+ } else {
36
+ data = chunk;
37
+ }
38
+
39
+ WebSocket_Global.send(data);
40
+ next();
41
+ },
42
+ flush(done) {
43
+ WebSocket_Global.close();
44
+ done();
45
+ },
46
+ writev(chunks, cb) {
47
+ const buffers = chunks.map(({ chunk }) => {
48
+ if (typeof chunk === 'string') {
49
+ return Buffer.from(chunk, 'utf8');
50
+ }
51
+ return chunk;
52
+ });
53
+ this._write(Buffer.concat(buffers), 'binary', cb);
54
+ },
55
+ });
56
+ return Proxy;
57
+ }
58
+
59
+ function buildStream(options, WebSocket, Proxy) {
60
+ const Stream = Duplexify(undefined, undefined, options);
61
+ Stream.socket = WebSocket;
62
+
63
+ WebSocket.onclose = () => {
64
+ Stream.end();
65
+ Stream.destroy();
66
+ };
67
+
68
+ WebSocket.onerror = (err) => {
69
+ Stream.destroy(err);
70
+ };
71
+
72
+ WebSocket.onmessage = (event) => {
73
+ const data = event.data instanceof ArrayBuffer ? Buffer.from(event.data) : Buffer.from(event.data, 'utf8');
74
+ Stream.push(data);
75
+ };
76
+
77
+ WebSocket.onopen = () => {
78
+ Stream.setReadable(Proxy);
79
+ Stream.setWritable(Proxy);
80
+ Stream.emit('connect');
81
+ };
82
+
83
+ WebSocket_Global = WebSocket;
84
+ Proxy.on('close', () => WebSocket.close());
85
+
86
+ return Stream;
87
+ }
88
+
89
+ function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
90
+ const chatOn = ctx.globalOptions.online;
91
+ const foreground = false;
92
+
93
+ const sessionID = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + 1;
94
+ const username = {
95
+ u: ctx.userID,
96
+ s: sessionID,
97
+ chat_on: chatOn,
98
+ fg: foreground,
99
+ d: utils.getGUID(),
100
+ ct: 'websocket',
101
+ aid: '219994525426954',
102
+ aids: null,
103
+ mqtt_sid: '',
104
+ cp: 3,
105
+ ecp: 10,
106
+ st: [],
107
+ pm: [],
108
+ dc: '',
109
+ no_auto_fg: true,
110
+ gas: null,
111
+ pack: [],
112
+ p: null,
113
+ php_override: ""
114
+ };
115
+
116
+ const cookies = ctx.jar.getCookies('https://www.facebook.com').join('; ');
117
+
118
+ let host;
119
+ if (ctx.mqttEndpoint) {
120
+ host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
121
+ } else if (ctx.region) {
122
+ host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLowerCase()}&sid=${sessionID}`;
123
+ } else {
124
+ host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
125
+ }
126
+
127
+ const options = {
128
+ clientId: 'mqttwsclient',
129
+ protocolId: 'MQIsdp',
130
+ protocolVersion: 3,
131
+ username: JSON.stringify(username),
132
+ clean: true,
133
+ wsOptions: {
134
+ headers: {
135
+ Cookie: cookies,
136
+ Origin: 'https://www.facebook.com',
137
+ 'User-Agent': ctx.globalOptions.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
138
+ Referer: 'https://www.facebook.com/',
139
+ Host: new URL(host).hostname,
140
+ },
141
+ origin: 'https://www.facebook.com',
142
+ protocolVersion: 13,
143
+ binaryType: 'arraybuffer',
144
+ },
145
+ keepalive: 60,
146
+ reschedulePings: true,
147
+ reconnectPeriod: 3,
148
+ };
149
+
150
+ if (ctx.globalOptions.proxy !== undefined) {
151
+ const agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
152
+ options.wsOptions.agent = agent;
153
+ }
154
+
155
+ ctx.mqttClient = new mqtt.Client(() => buildStream(options, new WebSocket(host, options.wsOptions), buildProxy()), options);
156
+ global.mqttClient = ctx.mqttClient;
157
+
158
+ global.mqttClient.on('error', (err) => {
159
+ log.error('listenMqtt', err);
160
+ global.mqttClient.end();
161
+
162
+ if (ctx.globalOptions.autoReconnect) {
163
+ getSeqID();
164
+ } else {
165
+ globalCallback({
166
+ type: 'stop_listen',
167
+ error: 'Server Đã Sập - Auto Restart'
168
+ }, null);
169
+ return process.exit(1);
170
+ }
171
+ });
172
+
173
+ global.mqttClient.on('connect', () => {
174
+ if (!global.Fca.Data.Setup || global.Fca.Data.Setup === undefined) {
175
+ if (global.Fca.Require.FastConfig.RestartMQTT_Minutes !== 0 && global.Fca.Data.StopListening !== true) {
176
+ global.Fca.Data.Setup = true;
177
+ setTimeout(() => {
178
+ global.Fca.Require.logger.Warning('Closing MQTT Client...');
179
+ ctx.mqttClient.end();
180
+ global.Fca.Require.logger.Warning('Reconnecting MQTT Client...');
181
+ global.Fca.Data.Setup = false;
182
+ getSeqID();
183
+ }, Number(global.Fca.Require.FastConfig.RestartMQTT_Minutes) * 60 * 1000);
184
+ }
185
+ }
186
+
187
+ if (process.env.OnStatus === undefined) {
188
+ global.Fca.Require.logger.Normal('Bạn Đang Sài Phiên Bản: Premium Access');
189
+
190
+ if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) === 0) {
191
+ // something
192
+ } else if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) < 10) {
193
+ log.warn('AutoRestartMinutes', 'The number of minutes to automatically restart must be more than 10 minutes');
194
+ } else if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) < 0) {
195
+ log.warn('AutoRestartMinutes', 'Invalid auto-restart minutes!');
196
+ } else {
197
+ global.Fca.Require.logger.Normal(global.Fca.getText(global.Fca.Require.Language.Src.AutoRestart, global.Fca.Require.FastConfig.AutoRestartMinutes));
198
+ global.Fca.Require.logger.Normal(`Auto Restart MQTT Client After: ${global.Fca.Require.FastConfig.RestartMQTT_Minutes} Minutes`);
199
+ setTimeout(() => {
200
+ global.Fca.Require.logger.Normal(global.Fca.Require.Language.Src.OnRestart);
201
+ process.exit(1);
202
+ }, Number(global.Fca.Require.FastConfig.AutoRestartMinutes) * 60000);
203
+ }
204
+ require('../broadcast').startBroadcasting();
205
+ const MemoryManager = require('../Extra/Src/Release_Memory');
206
+ const path = require('path');
207
+
208
+ const SettingMemoryManager = {
209
+ warningThreshold: 0.7,
210
+ releaseThreshold: 0.8,
211
+ maxThreshold: 0.9,
212
+ interval: 60 * 1000,
213
+ logLevel: 'warn',
214
+ logFile: path.join(process.cwd(), 'Horizon_Database' ,'memory.log'),
215
+ smartReleaseEnabled: true,
216
+ allowLog: (global.Fca.Require.FastConfig.AntiStuckAndMemoryLeak.LogFile.Use || false)
217
+ };
218
+
219
+ const memoryManager = new MemoryManager(SettingMemoryManager);
220
+
221
+ memoryManager.autoStart(60 * 60 * 1000);
222
+
223
+ if (global.Fca.Require.FastConfig.AntiStuckAndMemoryLeak.AutoRestart.Use) {
224
+ memoryManager.onMaxMemory(function() {
225
+ global.Fca.Require.logger.Warning('Memory Usage >= 90% - Auto Restart Avoid Crash');
226
+ process.exit(1);
227
+ });
228
+ }
229
+ process.env.OnStatus = true;
230
+ }
231
+
232
+ topics.forEach((topicsub) => global.mqttClient.subscribe(topicsub));
233
+
234
+
235
+ let topic;
236
+ const queue = {
237
+ sync_api_version: 11,
238
+ max_deltas_able_to_process: 100,
239
+ delta_batch_size: 500,
240
+ encoding: 'JSON',
241
+ entity_fbid: ctx.userID,
242
+ };
243
+
244
+ topic = "/messenger_sync_create_queue";
245
+ queue.initial_titan_sequence_id = ctx.lastSeqId;
246
+ queue.device_params = null;
247
+
248
+ global.mqttClient.publish(topic, JSON.stringify(queue), {
249
+ qos: 1,
250
+ retain: false
251
+ });
252
+
253
+ var rTimeout = setTimeout(function() {
254
+ global.mqttClient.end();
255
+ getSeqID();
256
+ }, 3000);
257
+
258
+ ctx.tmsWait = function() {
259
+ clearTimeout(rTimeout);
260
+ ctx.globalOptions.emitReady ? globalCallback({
261
+ type: "ready",
262
+ error: null
263
+ }) : '';
264
+ delete ctx.tmsWait;
265
+ };
266
+ });
267
+
268
+ const HandleMessage = function(topic, message, _packet) {
269
+ const jsonMessage = JSON.parse(message.toString());
270
+ if (topic === "/t_ms") {
271
+ if (ctx.tmsWait && typeof ctx.tmsWait == "function") ctx.tmsWait();
272
+
273
+ if (jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
274
+ ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
275
+ ctx.syncToken = jsonMessage.syncToken;
276
+ }
277
+
278
+ if (jsonMessage.lastIssuedSeqId) ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
279
+ //If it contains more than 1 delta
280
+ for (var i in jsonMessage.deltas) {
281
+ var delta = jsonMessage.deltas[i];
282
+ parseDelta(defaultFuncs, api, ctx, globalCallback, {
283
+ "delta": delta
284
+ });
285
+ }
286
+ } else if (topic === "/ls_resp") {
287
+ const payload = JSON.parse(jsonMessage.payload); //'{"name":null,"step":[1,[1,[4,0,1,[5,"taskExists",[19,"415"]]],[23,[2,0],[1,[5,"replaceOptimsiticMessage","7192532113093667880","mid.$gABfX5li9LA6VdUymnWPRAdlkiawo"]]]],[1,[4,0,1,[5,"taskExists",[19,"415"]]],[23,[2,0],[1,[5,"mailboxTaskCompletionApiOnTaskCompletion",[19,"415"],true]]]],[1,[4,0,1,[5,"taskExists",[19,"415"]]],[23,[2,0],[1,[5,"removeTask",[19,"415"],[9]]]]]]}'
288
+ const request_ID = jsonMessage.request_id;
289
+
290
+ if (ctx.callback_Task[request_ID] != undefined && ctx.callback_Task[request_ID].type != undefined) {
291
+ const {
292
+ callback,
293
+ type
294
+ } = ctx.callback_Task[request_ID];
295
+ const Data = new getRespData(type, payload);
296
+ if (!callback) {
297
+ return;
298
+ }
299
+ else if (!Data) {
300
+ callback("Something went wrong 🐳", null);
301
+ } else {
302
+ callback(null, Data);
303
+ }
304
+ }
305
+ } else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
306
+ var typ = {
307
+ type: "typ",
308
+ isTyping: !!jsonMessage.state,
309
+ from: jsonMessage.sender_fbid.toString(),
310
+ threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
311
+ };
312
+ (function() {
313
+ globalCallback(null, typ);
314
+ })();
315
+ } else if (topic === "/orca_presence") {
316
+ if (!ctx.globalOptions.updatePresence) {
317
+ for (var i in jsonMessage.list) {
318
+ var data = jsonMessage.list[i];
319
+ var userID = data["u"];
320
+
321
+ var presence = {
322
+ type: "presence",
323
+ userID: userID.toString(),
324
+ // Convert to ms
325
+ timestamp: data["l"] * 1000,
326
+ statuses: data["p"]
327
+ };
328
+ (function() {
329
+ globalCallback(null, presence);
330
+ })();
331
+ }
332
+ }
333
+ }
334
+
335
+ };
336
+
337
+ global.mqttClient.on('message', HandleMessage);
338
+
339
+ process.on('SIGINT', () => {
340
+ LogUptime();
341
+ process.kill(process.pid);
342
+ });
343
+ process.on('exit', LogUptime);
344
+ }
345
+
346
+ function getRespData(Type, payload) {
347
+ try {
348
+ switch (Type) {
349
+ case "sendMqttMessage": {
350
+ return {
351
+ type: Type,
352
+ threadID: payload.step[1][2][2][1][2], //this is sick bro
353
+ messageID: payload.step[1][2][2][1][3],
354
+ payload: payload.step[1][2]
355
+ };
356
+ }
357
+ default: { //!very LAZY :> cook yourself
358
+ return {
359
+ Data: payload.step[1][2][2][1],
360
+ type: Type,
361
+ payload: payload.step[1][2]
362
+ };
363
+ }
364
+ }
365
+ } catch (e) {
366
+ return null;
367
+ }
368
+ }
369
+
370
+ function LogUptime() {
371
+ const uptime = process.uptime();
372
+ const {
373
+ join
374
+ } = require('path');
375
+ const filePath = join(__dirname, '../CountTime.json');
376
+
377
+ let time1;
378
+ if (global.Fca.Require.fs.existsSync(filePath)) {
379
+ time1 = Number(global.Fca.Require.fs.readFileSync(filePath, 'utf8')) || 0;
380
+ } else {
381
+ time1 = 0;
382
+ }
383
+
384
+ global.Fca.Require.fs.writeFileSync(filePath, String(Number(uptime) + time1), 'utf8');
385
+ }
386
+
387
+ if (global.Fca.Require.FastConfig.AntiGetInfo.AntiGetThreadInfo) {
388
+ setInterval(() => {
389
+ try {
390
+ const { updateMessageCount, getData, hasData } = require('../Extra/ExtraGetThread');
391
+ const Data = global.Fca.Data.MsgCount;
392
+ const Arr = Array.from(Data.keys());
393
+ for (let i of Arr) {
394
+ const Count = parseInt(Data.get(i));
395
+ if (hasData(i)) {
396
+ let x = getData(i);
397
+ x.messageCount += Count;
398
+ updateMessageCount(i, x);
399
+ Data.delete(i);
400
+ }
401
+ }
402
+
403
+ } catch (e) {
404
+ console.log(e);
405
+ }
406
+ }, 30 * 1000);
407
+ }
408
+
409
+ function parseDelta(defaultFuncs, api, ctx, globalCallback, {
410
+ delta
411
+ }) {
412
+ if (delta.class === 'NewMessage') {
413
+ if (ctx.globalOptions.pageID && ctx.globalOptions.pageID !== delta.queue) return;
414
+
415
+ const resolveAttachmentUrl = (i) => {
416
+ if (!delta.attachments || i === delta.attachments.length || utils.getType(delta.attachments) !== 'Array') {
417
+ let fmtMsg;
418
+ try {
419
+ fmtMsg = utils.formatDeltaMessage(delta);
420
+ } catch (err) {
421
+ return log.error('Lỗi Nhẹ', err);
422
+ }
423
+
424
+ if (fmtMsg) {
425
+ const isGroup = fmtMsg.isGroup;
426
+ const threadID = fmtMsg.threadID;
427
+ const messageID = fmtMsg.messageID;
428
+
429
+ global.Fca.Data.event.set("Data", {
430
+ isGroup,
431
+ threadID,
432
+ messageID
433
+ });
434
+
435
+ if (global.Fca.Require.FastConfig.AntiGetInfo.AntiGetThreadInfo) {
436
+ global.Fca.Data.MsgCount.set(fmtMsg.threadID, ((global.Fca.Data.MsgCount.get(fmtMsg.threadID)) + 1 || 1));
437
+ }
438
+
439
+ if (ctx.globalOptions.autoMarkDelivery) {
440
+ markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
441
+ }
442
+
443
+ if (!ctx.globalOptions.selfListen && fmtMsg.senderID === ctx.userID) return;
444
+ globalCallback(null, fmtMsg);
445
+ }
446
+ } else {
447
+ const attachment = delta.attachments[i];
448
+ if (attachment.mercury.attach_type === 'photo') {
449
+ api.resolvePhotoUrl(attachment.fbid, (err, url) => {
450
+ if (!err) attachment.mercury.metadata.url = url;
451
+ resolveAttachmentUrl(i + 1);
452
+ });
453
+ } else {
454
+ resolveAttachmentUrl(i + 1);
455
+ }
456
+ }
457
+ };
458
+
459
+ resolveAttachmentUrl(0);
460
+ } else if (delta.class === 'ClientPayload') {
461
+ const clientPayload = utils.decodeClientPayload(delta.payload);
462
+ if (clientPayload && clientPayload.deltas) {
463
+ for (const delta of clientPayload.deltas) {
464
+ if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
465
+ const messageReaction = {
466
+ type: 'message_reaction',
467
+ threadID: (delta.deltaMessageReaction.threadKey.threadFbId ? delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey.otherUserFbId).toString(),
468
+ messageID: delta.deltaMessageReaction.messageId,
469
+ reaction: delta.deltaMessageReaction.reaction,
470
+ senderID: delta.deltaMessageReaction.senderId.toString(),
471
+ userID: delta.deltaMessageReaction.userId.toString(),
472
+ };
473
+ globalCallback(null, messageReaction);
474
+ } else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
475
+ const messageUnsend = {
476
+ type: 'message_unsend',
477
+ threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ? delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey.otherUserFbId).toString(),
478
+ messageID: delta.deltaRecallMessageData.messageID,
479
+ senderID: delta.deltaRecallMessageData.senderID.toString(),
480
+ deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
481
+ timestamp: delta.deltaRecallMessageData.timestamp,
482
+ };
483
+ globalCallback(null, messageUnsend);
484
+ } else if (delta.deltaMessageReply) {
485
+ const mdata =
486
+ delta.deltaMessageReply.message === undefined ?
487
+ [] :
488
+ delta.deltaMessageReply.message.data === undefined ?
489
+ [] :
490
+ delta.deltaMessageReply.message.data.prng === undefined ?
491
+ [] :
492
+ JSON.parse(delta.deltaMessageReply.message.data.prng);
493
+
494
+ const m_id = mdata.map((u) => u.i);
495
+ const m_offset = mdata.map((u) => u.o);
496
+ const m_length = mdata.map((u) => u.l);
497
+
498
+ const mentions = {};
499
+ for (let i = 0; i < m_id.length; i++) {
500
+ mentions[m_id[i]] = (delta.deltaMessageReply.message.body || '').substring(m_offset[i], m_offset[i] + m_length[i]);
501
+ }
502
+
503
+ const callbackToReturn = {
504
+ type: 'message_reply',
505
+ threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey.otherUserFbId).toString(),
506
+ messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
507
+ senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
508
+ attachments: ( delta.deltaMessageReply.message.attachments || [] )
509
+ .map((att) => {
510
+ const mercury = JSON.parse(att.mercuryJSON);
511
+ Object.assign(att, mercury);
512
+ return att;
513
+ })
514
+ .map((att) => {
515
+ let x;
516
+ try {
517
+ x = utils._formatAttachment(att);
518
+ } catch (ex) {
519
+ x = att;
520
+ x.error = ex;
521
+ x.type = 'unknown';
522
+ }
523
+ return x;
524
+ }),
525
+ args: (delta.deltaMessageReply.message.body || '').trim().split(/\s+/),
526
+ body: delta.deltaMessageReply.message.body || '',
527
+ isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
528
+ mentions,
529
+ timestamp: parseInt(delta.deltaMessageReply.message.messageMetadata.timestamp),
530
+ participantIDs: (delta.deltaMessageReply.message.participants || []).map((e) => e.toString()),
531
+ };
532
+
533
+ if (delta.deltaMessageReply.repliedToMessage) {
534
+ const mdata =
535
+ delta.deltaMessageReply.repliedToMessage === undefined ?
536
+ [] :
537
+ delta.deltaMessageReply.repliedToMessage.data === undefined ?
538
+ [] :
539
+ delta.deltaMessageReply.repliedToMessage.data.prng === undefined ?
540
+ [] :
541
+ JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
542
+
543
+ const m_id = mdata.map((u) => u.i);
544
+ const m_offset = mdata.map((u) => u.o);
545
+ const m_length = mdata.map((u) => u.l);
546
+
547
+ const rmentions = {};
548
+ for (let i = 0; i < m_id.length; i++) {
549
+ rmentions[m_id[i]] = (delta.deltaMessageReply.repliedToMessage.body || '').substring(m_offset[i], m_offset[i] + m_length[i]);
550
+ }
551
+
552
+ callbackToReturn.messageReply = {
553
+ threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.otherUserFbId).toString(),
554
+ messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
555
+ senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
556
+ attachments: delta.deltaMessageReply.repliedToMessage.attachments
557
+ .map((att) => {
558
+ const mercury = JSON.parse(att.mercuryJSON);
559
+ Object.assign(att, mercury);
560
+ return att;
561
+ })
562
+ .map((att) => {
563
+ let x;
564
+ try {
565
+ x = utils._formatAttachment(att);
566
+ } catch (ex) {
567
+ x = att;
568
+ x.error = ex;
569
+ x.type = 'unknown';
570
+ }
571
+ return x;
572
+ }),
573
+ args: (delta.deltaMessageReply.repliedToMessage.body || '').trim().split(/\s+/),
574
+ body: delta.deltaMessageReply.repliedToMessage.body || '',
575
+ isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
576
+ mentions: rmentions,
577
+ timestamp: parseInt(delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp),
578
+ participantIDs: (delta.deltaMessageReply.repliedToMessage.participants || []).map((e) => e.toString()),
579
+ };
580
+ } else if (delta.deltaMessageReply.replyToMessageId) {
581
+ return defaultFuncs
582
+ .post('https://www.facebook.com/api/graphqlbatch/', ctx.jar, {
583
+ av: ctx.globalOptions.pageID,
584
+ queries: JSON.stringify({
585
+ o0: {
586
+ doc_id: '2848441488556444',
587
+ query_params: {
588
+ thread_and_message_id: {
589
+ thread_id: callbackToReturn.threadID,
590
+ message_id: delta.deltaMessageReply.replyToMessageId.id,
591
+ },
592
+ },
593
+ },
594
+ }),
595
+ })
596
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
597
+ .then((resData) => {
598
+ if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
599
+ if (resData[resData.length - 1].successful_results === 0) throw {
600
+ error: 'forcedFetch: there was no successful_results',
601
+ res: resData
602
+ };
603
+ const fetchData = resData[0].o0.data.message;
604
+ const mobj = {};
605
+
606
+ for (const n in fetchData.message.ranges) {
607
+ mobj[fetchData.message.ranges[n].entity.id] = (fetchData.message.text || '').substr(fetchData.message.ranges[n].offset, fetchData.message.ranges[n].length);
608
+ }
609
+ callbackToReturn.messageReply = {
610
+ type: 'Message',
611
+ threadID: callbackToReturn.threadID,
612
+ messageID: fetchData.message_id,
613
+ senderID: fetchData.message_sender.id.toString(),
614
+ attachments: fetchData.message.blob_attachment.map((att) => utils._formatAttachment({
615
+ blob_attachment: att
616
+ })),
617
+ args: (fetchData.message.text || '').trim().split(/\s+/) || [],
618
+ body: fetchData.message.text || '',
619
+ isGroup: callbackToReturn.isGroup,
620
+ mentions: mobj,
621
+ timestamp: parseInt(fetchData.timestamp_precise),
622
+ };
623
+ })
624
+
625
+ .catch((err) => log.error('forcedFetch', err))
626
+ .finally(() => {
627
+ if (ctx.globalOptions.autoMarkDelivery) {
628
+ markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
629
+ }
630
+
631
+ if (!ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID) return;
632
+ globalCallback(null, callbackToReturn);
633
+ });
634
+ } else {
635
+ callbackToReturn.delta = delta;
636
+ }
637
+ if (ctx.globalOptions.autoMarkDelivery) {
638
+ markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
639
+ }
640
+
641
+ if (!ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID) return;
642
+ globalCallback(null, callbackToReturn);
643
+ }
644
+ }
645
+
646
+ return;
647
+ }
648
+ }
649
+ switch (delta.class) {
650
+ case 'ReadReceipt': {
651
+ let fmtMsg;
652
+ try {
653
+ fmtMsg = utils.formatDeltaReadReceipt(delta);
654
+ } catch (err) {
655
+ return log.error('Lỗi Nhẹ', err);
656
+ }
657
+ globalCallback(null, fmtMsg);
658
+ break;
659
+ }
660
+ case 'AdminTextMessage': {
661
+ switch (delta.type) {
662
+ case 'joinable_group_link_mode_change':
663
+ case 'magic_words':
664
+ case 'pin_messages_v2':
665
+ case 'change_thread_theme':
666
+ case 'change_thread_icon':
667
+ case 'change_thread_nickname':
668
+ case 'change_thread_admins':
669
+ case 'change_thread_approval_mode':
670
+ case 'group_poll':
671
+ case 'messenger_call_log':
672
+ case 'participant_joined_group_call': {
673
+ let fmtMsg;
674
+ try {
675
+ fmtMsg = utils.formatDeltaEvent(delta);
676
+ } catch (err) {
677
+ console.log(delta);
678
+ return log.error('Lỗi Nhẹ', err);
679
+ }
680
+ globalCallback(null, fmtMsg);
681
+ break;
682
+ }
683
+ }
684
+ break;
685
+ }
686
+
687
+ //For group images
688
+ case 'ForcedFetch': {
689
+ if (!delta.threadKey) return;
690
+ const mid = delta.messageId;
691
+ const tid = delta.threadKey.threadFbId;
692
+
693
+ if (mid && tid) {
694
+ const form = {
695
+ av: ctx.globalOptions.pageID,
696
+ queries: JSON.stringify({
697
+ o0: {
698
+ doc_id: '2848441488556444',
699
+ query_params: {
700
+ thread_and_message_id: {
701
+ thread_id: tid.toString(),
702
+ message_id: mid,
703
+ },
704
+ },
705
+ },
706
+ }),
707
+ };
708
+ defaultFuncs
709
+ .post('https://www.facebook.com/api/graphqlbatch/', ctx.jar, form)
710
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
711
+ .then((resData) => {
712
+ if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
713
+
714
+ if (resData[resData.length - 1].successful_results === 0) throw {
715
+ error: 'forcedFetch: there was no successful_results',
716
+ res: resData
717
+ };
718
+
719
+ const fetchData = resData[0].o0.data.message;
720
+
721
+ if (utils.getType(fetchData) === 'Object') {
722
+ log.info('forcedFetch', fetchData);
723
+ switch (fetchData.__typename) {
724
+ case 'ThreadImageMessage':
725
+ if (!ctx.globalOptions.selfListen && fetchData.message_sender.id.toString() === ctx.userID) return;
726
+ if (!ctx.loggedIn) return;
727
+
728
+ globalCallback(null, {
729
+ type: 'change_thread_image',
730
+ threadID: utils.formatID(tid.toString()),
731
+ snippet: fetchData.snippet,
732
+ timestamp: fetchData.timestamp_precise,
733
+ author: fetchData.message_sender.id,
734
+ image: {
735
+ attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
736
+ width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
737
+ height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
738
+ url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri,
739
+ },
740
+ });
741
+ break;
742
+ case 'UserMessage': {
743
+ const event = {
744
+ type: 'message',
745
+ senderID: utils.formatID(fetchData.message_sender.id),
746
+ body: fetchData.message.text || '',
747
+ threadID: utils.formatID(tid.toString()),
748
+ messageID: fetchData.message_id,
749
+ attachments: [{
750
+ type: 'share',
751
+ ID: fetchData.extensible_attachment.legacy_attachment_id,
752
+ url: fetchData.extensible_attachment.story_attachment.url,
753
+ title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
754
+ description: fetchData.extensible_attachment.story_attachment.description.text,
755
+ source: fetchData.extensible_attachment.story_attachment.source,
756
+ image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
757
+ width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
758
+ height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
759
+ playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
760
+ duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
761
+ subattachments: fetchData.extensible_attachment.subattachments,
762
+ properties: fetchData.extensible_attachment.story_attachment.properties,
763
+ }],
764
+ mentions: {},
765
+ timestamp: parseInt(fetchData.timestamp_precise),
766
+ isGroup: (fetchData.message_sender.id !== tid.toString()),
767
+ };
768
+
769
+ log.info('ff-Return', event);
770
+ globalCallback(null, event);
771
+ break;
772
+ }
773
+ default:
774
+ log.error('forcedFetch', fetchData);
775
+ }
776
+ } else {
777
+ log.error('forcedFetch', fetchData);
778
+ }
779
+ })
780
+ .catch((err) => log.error('forcedFetch', err));
781
+ }
782
+ break;
783
+ }
784
+ case 'ThreadName':
785
+ case 'ParticipantsAddedToGroupThread':
786
+ case 'ParticipantLeftGroupThread': {
787
+ let formattedEvent;
788
+ try {
789
+ formattedEvent = utils.formatDeltaEvent(delta);
790
+ } catch (err) {
791
+ console.log(err);
792
+ return log.error('Lỗi Nhẹ', err);
793
+ }
794
+
795
+ if (!ctx.globalOptions.selfListen && formattedEvent.author.toString() === ctx.userID) return;
796
+ if (!ctx.loggedIn) return;
797
+ globalCallback(null, formattedEvent);
798
+ break;
799
+ }
800
+ case 'NewMessage': {
801
+ if (delta.attachments !== undefined && delta.attachments.length === 1 && delta.attachments[0].mercury.extensible_attachment !== undefined && delta.attachments[0].mercury.extensible_attachment.story_attachment.style_list != null && delta.attachments[0].mercury.extensible_attachment.story_attachment.style_list.includes('message_live_location')) {
802
+ delta.class = 'UserLocation';
803
+ let fmtMsg;
804
+ try {
805
+ fmtMsg = utils.formatDeltaEvent(delta);
806
+ } catch (err) {
807
+ console.log(delta);
808
+ return log.error('Lỗi Nhẹ', err);
809
+ }
810
+ globalCallback(null, fmtMsg);
811
+ }
812
+ break;
813
+ }
814
+ }
815
+ }
816
+
817
+ function markDelivery(ctx, api, threadID, messageID) {
818
+ if (threadID && messageID) {
819
+ api.markAsDelivered(threadID, messageID, (err) => {
820
+ if (err) log.error('markAsDelivered', err);
821
+ else {
822
+ if (ctx.globalOptions.autoMarkRead) {
823
+ api.markAsRead(threadID, (err) => {
824
+ if (err) log.error('markAsDelivered', err);
825
+ });
826
+ }
827
+ }
828
+ });
829
+ }
830
+ }
831
+
832
+
833
+
834
+ module.exports = function(defaultFuncs, api, ctx) {
835
+ var globalCallback = identity;
836
+ getSeqID = function getSeqID() {
837
+ ctx.t_mqttCalled = false;
838
+ defaultFuncs
839
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
840
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
841
+ .then((resData) => {
842
+ if (utils.getType(resData) != "Array") {
843
+ if (global.Fca.Require.FastConfig.AutoLogin) {
844
+ return global.Fca.Require.logger.Warning(global.Fca.Require.Language.Index.AutoLogin, function() {
845
+ return global.Fca.Action('AutoLogin');
846
+ });
847
+ } else if (!global.Fca.Require.FastConfig.AutoLogin) {
848
+ return global.Fca.Require.logger.Error(global.Fca.Require.Language.Index.ErrAppState);
849
+ }
850
+ return;
851
+ } else {
852
+ if (resData && resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
853
+ if (resData[resData.length - 1].successful_results === 0) throw {
854
+ error: "getSeqId: there was no successful_results",
855
+ res: resData
856
+ };
857
+ if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
858
+ ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
859
+ listenMqtt(defaultFuncs, api, ctx, globalCallback);
860
+ } else throw {
861
+ error: "getSeqId: no sync_sequence_id found.",
862
+ res: resData
863
+ };
864
+ }
865
+ })
866
+ .catch((err) => {
867
+ log.error("getSeqId", err);
868
+ if (utils.getType(err) == "Object" && err.error === global.Fca.Require.Language.Index.ErrAppState) ctx.loggedIn = false;
869
+ return globalCallback(err);
870
+ });
871
+ };
872
+
873
+ return function(callback) {
874
+ class MessageEmitter extends EventEmitter {
875
+ stopListening(callback) {
876
+ callback = callback || (() => {});
877
+ globalCallback = identity;
878
+ if (ctx.mqttClient) {
879
+ ctx.mqttClient.unsubscribe("/webrtc");
880
+ ctx.mqttClient.unsubscribe("/rtc_multi");
881
+ ctx.mqttClient.unsubscribe("/onevc");
882
+ ctx.mqttClient.publish("/browser_close", "{}");
883
+ ctx.mqttClient.end(false, function(...data) {
884
+ ctx.mqttClient = undefined;
885
+ });
886
+ }
887
+ global.Fca.Data.StopListening = true;
888
+ }
889
+ }
890
+
891
+ var msgEmitter = new MessageEmitter();
892
+ globalCallback = (callback || function(error, message) {
893
+ if (error) return msgEmitter.emit("error", error);
894
+ msgEmitter.emit("message", message);
895
+ });
896
+
897
+ //Reset some stuff
898
+ if (!ctx.firstListen) ctx.lastSeqId = null;
899
+ ctx.syncToken = undefined;
900
+ ctx.t_mqttCalled = false;
901
+
902
+ //Same request as getThreadList
903
+ form = {
904
+ av: ctx.globalOptions.pageID,
905
+ queries: JSON.stringify({
906
+ o0: {
907
+ doc_id: '3336396659757871',
908
+ query_params: {
909
+ limit: 1,
910
+ before: null,
911
+ tags: ['INBOX'],
912
+ includeDeliveryReceipts: false,
913
+ includeSeqID: true,
914
+ },
915
+ },
916
+ }),
917
+ };
918
+
919
+
920
+ if (!ctx.firstListen || !ctx.lastSeqId) getSeqID();
921
+ else listenMqtt(defaultFuncs, api, ctx, globalCallback);
922
+ ctx.firstListen = false;
923
+
924
+ return msgEmitter;
925
+ };
926
926
  };