official-zach-remade 32.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of official-zach-remade might be problematic. Click here for more details.

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