fca-naughty2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/workflows/publish.yml +20 -0
  3. package/Extra/Database/index.js +469 -0
  4. package/Extra/ExtraAddons.js +80 -0
  5. package/Extra/ExtraFindUID.js +62 -0
  6. package/Extra/ExtraGetThread.js +118 -0
  7. package/Extra/ExtraScreenShot.js +430 -0
  8. package/Extra/ExtraUptimeRobot.js +38 -0
  9. package/Extra/Html/Classic/script.js +119 -0
  10. package/Extra/Html/Classic/style.css +8 -0
  11. package/Extra/Security/Index.js +146 -0
  12. package/Extra/Security/Step_1.js +6 -0
  13. package/Extra/Security/Step_2.js +22 -0
  14. package/Extra/Security/Step_3.js +22 -0
  15. package/Extra/Src/Change_Environment.js +24 -0
  16. package/Extra/Src/Check_Update.js +67 -0
  17. package/Extra/Src/History.js +115 -0
  18. package/Extra/Src/Instant_Update.js +65 -0
  19. package/Extra/Src/Last-Run.js +65 -0
  20. package/Extra/Src/Premium.js +81 -0
  21. package/Extra/Src/Release_Memory.js +41 -0
  22. package/Extra/Src/uuid.js +137 -0
  23. package/Func/AcceptAgreement.js +32 -0
  24. package/Func/ClearCache.js +64 -0
  25. package/Func/ReportV1.js +54 -0
  26. package/LICENSE +24 -0
  27. package/Language/index.json +206 -0
  28. package/Main.js +1099 -0
  29. package/README.md +140 -0
  30. package/SECURITY.md +17 -0
  31. package/broadcast.js +40 -0
  32. package/index.js +350 -0
  33. package/logger.js +66 -0
  34. package/package.json +94 -0
  35. package/src/Dev_Horizon_Data.js +125 -0
  36. package/src/Premium.js +30 -0
  37. package/src/Screenshot.js +83 -0
  38. package/src/addExternalModule.js +16 -0
  39. package/src/addUserToGroup.js +79 -0
  40. package/src/changeAdminStatus.js +79 -0
  41. package/src/changeArchivedStatus.js +41 -0
  42. package/src/changeAvt.js +85 -0
  43. package/src/changeBio.js +65 -0
  44. package/src/changeBlockedStatus.js +36 -0
  45. package/src/changeGroupImage.js +106 -0
  46. package/src/changeNickname.js +45 -0
  47. package/src/changeThreadColor.js +62 -0
  48. package/src/changeThreadEmoji.js +42 -0
  49. package/src/createNewGroup.js +70 -0
  50. package/src/createPoll.js +60 -0
  51. package/src/deleteMessage.js +45 -0
  52. package/src/deleteThread.js +43 -0
  53. package/src/forwardAttachment.js +48 -0
  54. package/src/getAccessToken.js +28 -0
  55. package/src/getCurrentUserID.js +7 -0
  56. package/src/getEmojiUrl.js +27 -0
  57. package/src/getFriendsList.js +73 -0
  58. package/src/getMessage.js +80 -0
  59. package/src/getThreadHistory.js +537 -0
  60. package/src/getThreadInfo.js +412 -0
  61. package/src/getThreadList.js +213 -0
  62. package/src/getThreadMain.js +220 -0
  63. package/src/getThreadPictures.js +59 -0
  64. package/src/getUID.js +59 -0
  65. package/src/getUserID.js +62 -0
  66. package/src/getUserInfo.js +113 -0
  67. package/src/getUserInfoMain.js +65 -0
  68. package/src/getUserInfoV2.js +32 -0
  69. package/src/getUserInfoV3.js +63 -0
  70. package/src/getUserInfoV4.js +55 -0
  71. package/src/getUserInfoV5.js +61 -0
  72. package/src/handleFriendRequest.js +46 -0
  73. package/src/handleMessageRequest.js +49 -0
  74. package/src/httpGet.js +49 -0
  75. package/src/httpPost.js +48 -0
  76. package/src/httpPostFormData.js +41 -0
  77. package/src/listenMqtt.js +697 -0
  78. package/src/logout.js +68 -0
  79. package/src/markAsDelivered.js +48 -0
  80. package/src/markAsRead.js +70 -0
  81. package/src/markAsReadAll.js +43 -0
  82. package/src/markAsSeen.js +51 -0
  83. package/src/muteThread.js +47 -0
  84. package/src/removeUserFromGroup.js +49 -0
  85. package/src/resolvePhotoUrl.js +37 -0
  86. package/src/searchForThread.js +43 -0
  87. package/src/sendMessage.js +334 -0
  88. package/src/sendTypingIndicator.js +80 -0
  89. package/src/setMessageReaction.js +109 -0
  90. package/src/setPostReaction.js +102 -0
  91. package/src/setTitle.js +74 -0
  92. package/src/threadColors.js +39 -0
  93. package/src/unfriend.js +43 -0
  94. package/src/unsendMessage.js +40 -0
  95. package/test/Database_Test.js +4 -0
  96. package/test/Db2.js +530 -0
  97. package/test/Horizon_Database/A_README.md +1 -0
  98. package/test/Horizon_Database/Database.db +0 -0
  99. package/test/data/shareAttach.js +146 -0
  100. package/test/data/something.mov +0 -0
  101. package/test/data/test.png +0 -0
  102. package/test/data/test.txt +7 -0
  103. package/test/env/.env +0 -0
  104. package/test/example-config.json +18 -0
  105. package/test/example-db.db +0 -0
  106. package/test/memoryleak.js +18 -0
  107. package/test/test-page.js +140 -0
  108. package/test/test.js +385 -0
  109. package/test/testv2.js +18 -0
  110. package/utils.js +1628 -0
@@ -0,0 +1,697 @@
1
+ /* eslint-disable no-redeclare */
2
+ "use strict";
3
+ var utils = require("../utils");
4
+ var log = require("npmlog");
5
+ var mqtt = require('mqtt');
6
+ var websocket = require('websocket-stream');
7
+ var HttpsProxyAgent = require('https-proxy-agent');
8
+ const EventEmitter = require('events');
9
+ var identity = function () { };
10
+ var form = {};
11
+ var getSeqID = function () { };
12
+
13
+ var topics = ["/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"];
14
+
15
+ /* [ Noti ? ]
16
+ ! "/br_sr", //Notification
17
+ * => Need to publish /br_sr right after this
18
+
19
+ ! "/notify_disconnect",
20
+ * => Need to publish /messenger_sync_create_queue right after this
21
+
22
+ ! "/orca_presence",
23
+ * => Will receive /sr_res right here.
24
+ */
25
+
26
+ function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
27
+ //Don't really know what this does but I think it's for the active state?
28
+ //TODO: Move to ctx when implemented
29
+ var chatOn = ctx.globalOptions.online;
30
+ var foreground = false;
31
+
32
+ var sessionID = Math.floor(Math.random() * 9007199254740991) + 1;
33
+ 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: []};
34
+ var cookies = ctx.jar.getCookies('https://www.facebook.com').join("; ");
35
+
36
+ var host;
37
+ if (ctx.mqttEndpoint) host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
38
+ else if (ctx.region) host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLocaleLowerCase()}&sid=${sessionID}`;
39
+ else host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
40
+
41
+ var options = {
42
+ clientId: "mqttwsclient",
43
+ protocolId: 'MQIsdp',
44
+ protocolVersion: 3,
45
+ username: JSON.stringify(username),
46
+ clean: true,
47
+ wsOptions: {
48
+ headers: {
49
+ 'Cookie': cookies,
50
+ 'Origin': 'https://www.facebook.com',
51
+ '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'),
52
+ 'Referer': 'https://www.facebook.com/',
53
+ 'Host': new URL(host).hostname //'edge-chat.facebook.com'
54
+ },
55
+ origin: 'https://www.facebook.com',
56
+ protocolVersion: 13
57
+ },
58
+ keepalive: 60,
59
+ reschedulePings: true,
60
+ reconnectPeriod: 3
61
+ };
62
+
63
+ if (typeof ctx.globalOptions.proxy != "undefined") {
64
+ var agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
65
+ options.wsOptions.agent = agent;
66
+ }
67
+ ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
68
+ global.mqttClient = ctx.mqttClient;
69
+
70
+ global.mqttClient.on('error', function (err) {
71
+ log.error("listenMqtt", err);
72
+ global.mqttClient.end();
73
+
74
+ if (ctx.globalOptions.autoReconnect) getSeqID();
75
+ else {
76
+ globalCallback({ type: "stop_listen", error: "Server Đã Sập - Auto Restart" }, null);
77
+ return process.exit(1);
78
+ }
79
+ });
80
+
81
+ global.mqttClient.on('connect', function () {
82
+ if (!global.Fca.Data.Setup || global.Fca.Data.Setup == undefined) {
83
+ if (global.Fca.Require.FastConfig.RestartMQTT_Minutes != 0 && global.Fca.Data.StopListening != true) {
84
+ global.Fca.Data.Setup = true;
85
+ setTimeout(() => {
86
+ global.Fca.Require.logger.Warning("Closing MQTT Client...");
87
+ ctx.mqttClient.end();
88
+ global.Fca.Require.logger.Warning("Reconnecting MQTT Client...");
89
+ global.Fca.Data.Setup = false;
90
+ getSeqID();
91
+ }, Number(global.Fca.Require.FastConfig.RestartMQTT_Minutes) * 60 * 1000);
92
+ }
93
+ }
94
+ if (process.env.OnStatus == undefined) {
95
+ global.Fca.Require.logger.Normal(global.Fca.Data.PremText || "Hiện Status Lỗi :s");
96
+ if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) == 0) {
97
+ // something
98
+ }
99
+ else if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes < 10)) {
100
+ log.warn("AutoRestartMinutes","The number of minutes to automatically restart must be more than 10 minutes");
101
+ }
102
+ else if (Number(global.Fca.Require.FastConfig.AutoRestartMinutes) < 0) {
103
+ log.warn("AutoRestartMinutes","Invalid auto-restart minutes!");
104
+ }
105
+ else {
106
+ global.Fca.Require.logger.Normal(global.Fca.getText(global.Fca.Require.Language.Src.AutoRestart,global.Fca.Require.FastConfig.AutoRestartMinutes));
107
+ global.Fca.Require.logger.Normal("Auto Restart MQTT Client After: " + global.Fca.Require.FastConfig.RestartMQTT_Minutes + " Minutes");
108
+ setInterval(() => {
109
+ global.Fca.Require.logger.Normal(global.Fca.Require.Language.Src.OnRestart);
110
+ process.exit(1);
111
+ }, Number(global.Fca.Require.FastConfig.AutoRestartMinutes) * 60000);
112
+ }
113
+ require('../broadcast');
114
+ process.env.OnStatus = true;
115
+ }
116
+
117
+ topics.forEach(topicsub => global.mqttClient.subscribe(topicsub));
118
+
119
+ var topic;
120
+ var queue = {
121
+ sync_api_version: 11,
122
+ max_deltas_able_to_process: 100,
123
+ delta_batch_size: 500,
124
+ encoding: "JSON",
125
+ entity_fbid: ctx.userID,
126
+ };
127
+
128
+ topic = "/messenger_sync_create_queue";
129
+ queue.initial_titan_sequence_id = ctx.lastSeqId;
130
+ queue.device_params = null;
131
+
132
+ global.mqttClient.publish(topic, JSON.stringify(queue), { qos: 1, retain: false });
133
+
134
+ var rTimeout = setTimeout(function () {
135
+ global.mqttClient.end();
136
+ getSeqID();
137
+ }, 3000);
138
+
139
+ ctx.tmsWait = function () {
140
+ clearTimeout(rTimeout);
141
+ ctx.globalOptions.emitReady ? globalCallback({type: "ready",error: null}) : '';
142
+ delete ctx.tmsWait;
143
+ };
144
+ });
145
+
146
+ global.mqttClient.on('message', function (topic, message, _packet) {
147
+ const jsonMessage = JSON.parse(message.toString());
148
+
149
+ if (topic === "/t_ms") {
150
+ if (ctx.tmsWait && typeof ctx.tmsWait == "function") ctx.tmsWait();
151
+
152
+ if (jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
153
+ ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
154
+ ctx.syncToken = jsonMessage.syncToken;
155
+ }
156
+
157
+ if (jsonMessage.lastIssuedSeqId) ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
158
+ //If it contains more than 1 delta
159
+ for (var i in jsonMessage.deltas) {
160
+ var delta = jsonMessage.deltas[i];
161
+ parseDelta(defaultFuncs, api, ctx, globalCallback, { "delta": delta });
162
+ }
163
+ } else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
164
+ var typ = {
165
+ type: "typ",
166
+ isTyping: !!jsonMessage.state,
167
+ from: jsonMessage.sender_fbid.toString(),
168
+ threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
169
+ };
170
+ (function () { globalCallback(null, typ); })();
171
+ } else if (topic === "/orca_presence") {
172
+ if (!ctx.globalOptions.updatePresence) {
173
+ for (var i in jsonMessage.list) {
174
+ var data = jsonMessage.list[i];
175
+ var userID = data["u"];
176
+
177
+ var presence = {
178
+ type: "presence",
179
+ userID: userID.toString(),
180
+ //Convert to ms
181
+ timestamp: data["l"] * 1000,
182
+ statuses: data["p"]
183
+ };
184
+ (function () { globalCallback(null, presence); })();
185
+ }
186
+ }
187
+ }
188
+
189
+ });
190
+
191
+ process.on('SIGINT', function () {
192
+ LogUptime();process.kill(process.pid);
193
+ });
194
+
195
+ process.on('exit', () => {
196
+ LogUptime();
197
+ });
198
+
199
+ }
200
+
201
+ function LogUptime() {
202
+ var uptime = process.uptime();
203
+ var { join } = require('path');
204
+ if (global.Fca.Require.fs.existsSync(join(__dirname, '../CountTime.json'))) {
205
+ var Time1 = (Number(global.Fca.Require.fs.readFileSync(join(__dirname, '../CountTime.json'), 'utf8')) || 0);
206
+ global.Fca.Require.fs.writeFileSync(join(__dirname, '../CountTime.json'), String(Number(uptime) + Time1), 'utf8');
207
+ }
208
+ else {
209
+ var Time1 = 0;
210
+ global.Fca.Require.fs.writeFileSync(join(__dirname, '../CountTime.json'), String(Number(uptime) + Time1), 'utf8');
211
+ }
212
+ }
213
+ function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
214
+ if (v.delta.class == "NewMessage") {
215
+ //Not tested for pages
216
+ if (ctx.globalOptions.pageID && ctx.globalOptions.pageID != v.queue) return;
217
+
218
+ (function resolveAttachmentUrl(i) {
219
+ if (v.delta.attachments && (i == v.delta.attachments.length)) {
220
+ var fmtMsg;
221
+ try {
222
+ fmtMsg = utils.formatDeltaMessage(v);
223
+ } catch (err) {
224
+ return log.error("Lỗi Nhẹ", err);
225
+ }
226
+ global.Fca.Data.event = fmtMsg;
227
+ try {
228
+ if (process.env.HalzionVersion == 1973) {
229
+ var { updateMessageCount,getData,hasData } = require('../Extra/ExtraGetThread');
230
+ if (hasData(fmtMsg.threadID)) {
231
+ var x = getData(fmtMsg.threadID);
232
+ x.messageCount+=1;
233
+ updateMessageCount(fmtMsg.threadID,x);
234
+ }
235
+ }
236
+ }
237
+ catch (e) {
238
+ //temp
239
+ }
240
+ if (fmtMsg)
241
+ if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
242
+
243
+ return !ctx.globalOptions.selfListen && fmtMsg.senderID === ctx.userID ? undefined : (function () { globalCallback(null, fmtMsg); })();
244
+ } else {
245
+ if (v.delta.attachments && (v.delta.attachments[i].mercury.attach_type == "photo")) {
246
+ api.resolvePhotoUrl(v.delta.attachments[i].fbid, (err, url) => {
247
+ if (!err) v.delta.attachments[i].mercury.metadata.url = url;
248
+ return resolveAttachmentUrl(i + 1);
249
+ });
250
+ } else return resolveAttachmentUrl(i + 1);
251
+ }
252
+ })(0);
253
+ }
254
+
255
+ if (v.delta.class == "ClientPayload") {
256
+ var clientPayload = utils.decodeClientPayload(v.delta.payload);
257
+ if (clientPayload && clientPayload.deltas) {
258
+ for (var i in clientPayload.deltas) {
259
+ var delta = clientPayload.deltas[i];
260
+ if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
261
+ (function () {
262
+ globalCallback(null, {
263
+ type: "message_reaction",
264
+ threadID: (delta.deltaMessageReaction.threadKey.threadFbId ? delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey.otherUserFbId).toString(),
265
+ messageID: delta.deltaMessageReaction.messageId,
266
+ reaction: delta.deltaMessageReaction.reaction,
267
+ senderID: delta.deltaMessageReaction.senderId.toString(),
268
+ userID: delta.deltaMessageReaction.userId.toString()
269
+ });
270
+ })();
271
+ } else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
272
+ (function () {
273
+ globalCallback(null, {
274
+ type: "message_unsend",
275
+ threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ? delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey.otherUserFbId).toString(),
276
+ messageID: delta.deltaRecallMessageData.messageID,
277
+ senderID: delta.deltaRecallMessageData.senderID.toString(),
278
+ deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
279
+ timestamp: delta.deltaRecallMessageData.timestamp
280
+ });
281
+ })();
282
+ } else if (delta.deltaMessageReply) {
283
+ //Mention block - #1
284
+ var mdata =
285
+ delta.deltaMessageReply.message === undefined ? [] :
286
+ delta.deltaMessageReply.message.data === undefined ? [] :
287
+ delta.deltaMessageReply.message.data.prng === undefined ? [] :
288
+ JSON.parse(delta.deltaMessageReply.message.data.prng);
289
+ var m_id = mdata.map(u => u.i);
290
+ var m_offset = mdata.map(u => u.o);
291
+ var m_length = mdata.map(u => u.l);
292
+
293
+ var mentions = {};
294
+
295
+ 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]);
296
+ //Mention block - 1#
297
+ var callbackToReturn = {
298
+ type: "message_reply",
299
+ threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey.otherUserFbId).toString(),
300
+ messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
301
+ senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
302
+ attachments: delta.deltaMessageReply.message.attachments.map(function (att) {
303
+ var mercury = JSON.parse(att.mercuryJSON);
304
+ Object.assign(att, mercury);
305
+ return att;
306
+ }).map(att => {
307
+ var x;
308
+ try {
309
+ x = utils._formatAttachment(att);
310
+ } catch (ex) {
311
+ x = att;
312
+ x.error = ex;
313
+ x.type = "unknown";
314
+ }
315
+ return x;
316
+ }),
317
+ args: (delta.deltaMessageReply.message.body || "").trim().split(/\s+/),
318
+ body: (delta.deltaMessageReply.message.body || ""),
319
+ isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
320
+ mentions: mentions,
321
+ timestamp: delta.deltaMessageReply.message.messageMetadata.timestamp,
322
+ participantIDs: (delta.deltaMessageReply.message.participants || []).map(e => e.toString())
323
+ };
324
+
325
+ if (delta.deltaMessageReply.repliedToMessage) {
326
+ //Mention block - #2
327
+ mdata =
328
+ delta.deltaMessageReply.repliedToMessage === undefined ? [] :
329
+ delta.deltaMessageReply.repliedToMessage.data === undefined ? [] :
330
+ delta.deltaMessageReply.repliedToMessage.data.prng === undefined ? [] :
331
+ JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
332
+ m_id = mdata.map(u => u.i);
333
+ m_offset = mdata.map(u => u.o);
334
+ m_length = mdata.map(u => u.l);
335
+
336
+ var rmentions = {};
337
+
338
+ 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]);
339
+ //Mention block - 2#
340
+ callbackToReturn.messageReply = {
341
+ threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.otherUserFbId).toString(),
342
+ messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
343
+ senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
344
+ attachments: delta.deltaMessageReply.repliedToMessage.attachments.map(function (att) {
345
+ var mercury = JSON.parse(att.mercuryJSON);
346
+ Object.assign(att, mercury);
347
+ return att;
348
+ }).map(att => {
349
+ var x;
350
+ try {
351
+ x = utils._formatAttachment(att);
352
+ } catch (ex) {
353
+ x = att;
354
+ x.error = ex;
355
+ x.type = "unknown";
356
+ }
357
+ return x;
358
+ }),
359
+ args: (delta.deltaMessageReply.repliedToMessage.body || "").trim().split(/\s+/),
360
+ body: delta.deltaMessageReply.repliedToMessage.body || "",
361
+ isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
362
+ mentions: rmentions,
363
+ timestamp: delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp,
364
+ participantIDs: (delta.deltaMessageReply.repliedToMessage.participants || []).map(e => e.toString())
365
+ };
366
+ } else if (delta.deltaMessageReply.replyToMessageId) {
367
+ return defaultFuncs
368
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, {
369
+ "av": ctx.globalOptions.pageID,
370
+ "queries": JSON.stringify({
371
+ "o0": {
372
+ //Using the same doc_id as forcedFetch
373
+ "doc_id": "2848441488556444",
374
+ "query_params": {
375
+ "thread_and_message_id": {
376
+ "thread_id": callbackToReturn.threadID,
377
+ "message_id": delta.deltaMessageReply.replyToMessageId.id,
378
+ }
379
+ }
380
+ }
381
+ })
382
+ })
383
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
384
+ .then((resData) => {
385
+ if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
386
+ if (resData[resData.length - 1].successful_results === 0) throw { error: "forcedFetch: there was no successful_results", res: resData };
387
+ var fetchData = resData[0].o0.data.message;
388
+ var mobj = {};
389
+ 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);
390
+
391
+ callbackToReturn.messageReply = {
392
+ type: "Message",
393
+ threadID: callbackToReturn.threadID,
394
+ messageID: fetchData.message_id,
395
+ senderID: fetchData.message_sender.id.toString(),
396
+ attachments: fetchData.message.blob_attachment.map(att => {
397
+ var x;
398
+ try {
399
+ x = utils._formatAttachment({ blob_attachment: att });
400
+ } catch (ex) {
401
+ x = att;
402
+ x.error = ex;
403
+ x.type = "unknown";
404
+ }
405
+ return x;
406
+ }),
407
+ args: (fetchData.message.text || "").trim().split(/\s+/) || [],
408
+ body: fetchData.message.text || "",
409
+ isGroup: callbackToReturn.isGroup,
410
+ mentions: mobj,
411
+ timestamp: parseInt(fetchData.timestamp_precise)
412
+ };
413
+ })
414
+ .catch(err => log.error("forcedFetch", err))
415
+ .finally(function () {
416
+ if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
417
+ !ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID ? undefined : (function () { globalCallback(null, callbackToReturn); })();
418
+ });
419
+ } else callbackToReturn.delta = delta;
420
+
421
+ if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
422
+
423
+ return !ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID ? undefined : (function () { globalCallback(null, callbackToReturn); })();
424
+ }
425
+ }
426
+ return;
427
+ }
428
+ }
429
+
430
+ if (v.delta.class !== "NewMessage" && !ctx.globalOptions.listenEvents) return;
431
+ switch (v.delta.class) {
432
+ case "ReadReceipt":
433
+ var fmtMsg;
434
+ try {
435
+ fmtMsg = utils.formatDeltaReadReceipt(v.delta);
436
+ } catch (err) {
437
+ return log.error("Lỗi Nhẹ", err);
438
+ }
439
+ return (function () { globalCallback(null, fmtMsg); })();
440
+ case "AdminTextMessage":
441
+ switch (v.delta.type) {
442
+ case "joinable_group_link_mode_change":
443
+ case "magic_words":
444
+ case "change_thread_theme":
445
+ case "change_thread_icon":
446
+ case "change_thread_nickname":
447
+ case "change_thread_admins":
448
+ case "change_thread_approval_mode":
449
+ case "group_poll":
450
+ case "messenger_call_log":
451
+ case "participant_joined_group_call":
452
+ var fmtMsg;
453
+ try {
454
+ fmtMsg = utils.formatDeltaEvent(v.delta);
455
+ } catch (err) {
456
+ return log.error("Lỗi Nhẹ", err);
457
+ }
458
+ return (function () { globalCallback(null, fmtMsg); })();
459
+ default:
460
+ return;
461
+ }
462
+ //For group images
463
+ case "ForcedFetch":
464
+ if (!v.delta.threadKey) return;
465
+ var mid = v.delta.messageId;
466
+ var tid = v.delta.threadKey.threadFbId;
467
+ if (mid && tid) {
468
+ const form = {
469
+ "av": ctx.globalOptions.pageID,
470
+ "queries": JSON.stringify({
471
+ "o0": {
472
+ //This doc_id is valid as of March 25, 2020
473
+ "doc_id": "2848441488556444",
474
+ "query_params": {
475
+ "thread_and_message_id": {
476
+ "thread_id": tid.toString(),
477
+ "message_id": mid,
478
+ }
479
+ }
480
+ }
481
+ })
482
+ };
483
+
484
+ defaultFuncs
485
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
486
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
487
+ .then((resData) => {
488
+ if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
489
+
490
+ if (resData[resData.length - 1].successful_results === 0) throw { error: "forcedFetch: there was no successful_results", res: resData };
491
+
492
+ var fetchData = resData[0].o0.data.message;
493
+
494
+ if (utils.getType(fetchData) == "Object") {
495
+ log.info("forcedFetch", fetchData);
496
+ switch (fetchData.__typename) {
497
+ case "ThreadImageMessage":
498
+ (!ctx.globalOptions.selfListen &&
499
+ fetchData.message_sender.id.toString() === ctx.userID) ||
500
+ !ctx.loggedIn ?
501
+ undefined :
502
+ (function () {
503
+ globalCallback(null, {
504
+ type: "change_thread_image",
505
+ threadID: utils.formatID(tid.toString()),
506
+ snippet: fetchData.snippet,
507
+ timestamp: fetchData.timestamp_precise,
508
+ author: fetchData.message_sender.id,
509
+ image: {
510
+ attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
511
+ width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
512
+ height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
513
+ url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri
514
+ }
515
+ });
516
+ })();
517
+ break;
518
+ case "UserMessage":
519
+ log.info("ff-Return", {
520
+ type: "message",
521
+ senderID: utils.formatID(fetchData.message_sender.id),
522
+ body: fetchData.message.text || "",
523
+ threadID: utils.formatID(tid.toString()),
524
+ messageID: fetchData.message_id,
525
+ attachments: [{
526
+ type: "share",
527
+ ID: fetchData.extensible_attachment.legacy_attachment_id,
528
+ url: fetchData.extensible_attachment.story_attachment.url,
529
+
530
+ title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
531
+ description: fetchData.extensible_attachment.story_attachment.description.text,
532
+ source: fetchData.extensible_attachment.story_attachment.source,
533
+
534
+ image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
535
+ width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
536
+ height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
537
+ playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
538
+ duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
539
+
540
+ subattachments: fetchData.extensible_attachment.subattachments,
541
+ properties: fetchData.extensible_attachment.story_attachment.properties,
542
+ }],
543
+ mentions: {},
544
+ timestamp: parseInt(fetchData.timestamp_precise),
545
+ isGroup: (fetchData.message_sender.id != tid.toString())
546
+ });
547
+ globalCallback(null, {
548
+ type: "message",
549
+ senderID: utils.formatID(fetchData.message_sender.id),
550
+ body: fetchData.message.text || "",
551
+ threadID: utils.formatID(tid.toString()),
552
+ messageID: fetchData.message_id,
553
+ attachments: [{
554
+ type: "share",
555
+ ID: fetchData.extensible_attachment.legacy_attachment_id,
556
+ url: fetchData.extensible_attachment.story_attachment.url,
557
+
558
+ title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
559
+ description: fetchData.extensible_attachment.story_attachment.description.text,
560
+ source: fetchData.extensible_attachment.story_attachment.source,
561
+
562
+ image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
563
+ width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
564
+ height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
565
+ playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
566
+ duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
567
+
568
+ subattachments: fetchData.extensible_attachment.subattachments,
569
+ properties: fetchData.extensible_attachment.story_attachment.properties,
570
+ }],
571
+ mentions: {},
572
+ timestamp: parseInt(fetchData.timestamp_precise),
573
+ isGroup: (fetchData.message_sender.id != tid.toString())
574
+ });
575
+ }
576
+ } else log.error("forcedFetch", fetchData);
577
+ })
578
+ .catch((err) => log.error("forcedFetch", err));
579
+ }
580
+ break;
581
+ case "ThreadName":
582
+ case "ParticipantsAddedToGroupThread":
583
+ case "ParticipantLeftGroupThread":
584
+ var formattedEvent;
585
+ try {
586
+ formattedEvent = utils.formatDeltaEvent(v.delta);
587
+ } catch (err) {
588
+ return log.error("Lỗi Nhẹ", err);
589
+ }
590
+ return (!ctx.globalOptions.selfListen && formattedEvent.author.toString() === ctx.userID) || !ctx.loggedIn ? undefined : (function () { globalCallback(null, formattedEvent); })();
591
+ }
592
+ }
593
+
594
+
595
+ function markDelivery(ctx, api, threadID, messageID) {
596
+ if (threadID && messageID) {
597
+ api.markAsDelivered(threadID, messageID, (err) => {
598
+ if (err) log.error("markAsDelivered", err);
599
+ else {
600
+ if (ctx.globalOptions.autoMarkRead) {
601
+ api.markAsRead(threadID, (err) => {
602
+ if (err) log.error("markAsDelivered", err);
603
+ });
604
+ }
605
+ }
606
+ });
607
+ }
608
+ }
609
+
610
+ module.exports = function (defaultFuncs, api, ctx) {
611
+ var globalCallback = identity;
612
+ getSeqID = function getSeqID() {
613
+ ctx.t_mqttCalled = false;
614
+ defaultFuncs
615
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
616
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
617
+ .then((resData) => {
618
+ if (utils.getType(resData) != "Array") {
619
+ if (global.Fca.Require.FastConfig.AutoLogin) {
620
+ return global.Fca.Require.logger.Warning(global.Fca.Require.Language.Index.AutoLogin, function() {
621
+ return global.Fca.Action('AutoLogin');
622
+ });
623
+ }
624
+ else if (!global.Fca.Require.FastConfig.AutoLogin) {
625
+ return global.Fca.Require.logger.Error(global.Fca.Require.Language.Index.ErrAppState);
626
+ }
627
+ return;
628
+ }
629
+ else {
630
+ if (resData && resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
631
+ if (resData[resData.length - 1].successful_results === 0) throw { error: "getSeqId: there was no successful_results", res: resData };
632
+ if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
633
+ ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
634
+ listenMqtt(defaultFuncs, api, ctx, globalCallback);
635
+ }
636
+ else throw { error: "getSeqId: no sync_sequence_id found.", res: resData };
637
+ }
638
+ })
639
+ .catch((err) => {
640
+ log.error("getSeqId", err);
641
+ if (utils.getType(err) == "Object" && err.error === global.Fca.Require.Language.Index.ErrAppState) ctx.loggedIn = false;
642
+ return globalCallback(err);
643
+ });
644
+ };
645
+
646
+ return function (callback) {
647
+ class MessageEmitter extends EventEmitter {
648
+ stopListening(callback) {
649
+ callback = callback || (() => { });
650
+ globalCallback = identity;
651
+ if (ctx.mqttClient) {
652
+ ctx.mqttClient.unsubscribe("/webrtc");
653
+ ctx.mqttClient.unsubscribe("/rtc_multi");
654
+ ctx.mqttClient.unsubscribe("/onevc");
655
+ ctx.mqttClient.publish("/browser_close", "{}");
656
+ ctx.mqttClient.end(false, function (...data) {
657
+ ctx.mqttClient = undefined;
658
+ });
659
+ }
660
+ global.Fca.Data.StopListening = true;
661
+ }
662
+ }
663
+
664
+ var msgEmitter = new MessageEmitter();
665
+ globalCallback = (callback || function (error, message) {
666
+ if (error) return msgEmitter.emit("error", error);
667
+ msgEmitter.emit("message", message);
668
+ });
669
+
670
+ //Reset some stuff
671
+ if (!ctx.firstListen) ctx.lastSeqId = null;
672
+ ctx.syncToken = undefined;
673
+ ctx.t_mqttCalled = false;
674
+
675
+ //Same request as getThreadList
676
+ form = {
677
+ "av": ctx.globalOptions.pageID,
678
+ "queries": JSON.stringify({
679
+ "o0": {
680
+ "doc_id": "3336396659757871",
681
+ "query_params": {
682
+ "limit": 1,
683
+ "before": null,
684
+ "tags": ["INBOX"],
685
+ "includeDeliveryReceipts": false,
686
+ "includeSeqID": true
687
+ }
688
+ }
689
+ })
690
+ };
691
+
692
+ if (!ctx.firstListen || !ctx.lastSeqId) getSeqID();
693
+ else listenMqtt(defaultFuncs, api, ctx, globalCallback);
694
+ ctx.firstListen = false;
695
+ return msgEmitter;
696
+ };
697
+ };