metacord 0.0.1-security → 1.2.0-Beta

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

Potentially problematic release.


This version of metacord might be problematic. Click here for more details.

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