chatbot-nvt 1.6.0

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