zuka-chat-bot 1.5.2

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