fca-nayan-r 1.4.8

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

Potentially problematic release.


This version of fca-nayan-r might be problematic. Click here for more details.

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