ws-rapido 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/index.js +477 -0
  2. package/package.json +46 -0
  3. package/src/addExternalModule.js +25 -0
  4. package/src/addUserToGroup.js +115 -0
  5. package/src/changeAdminStatus.js +103 -0
  6. package/src/changeArchivedStatus.js +55 -0
  7. package/src/changeAvatar.js +136 -0
  8. package/src/changeAvatarV2.js +86 -0
  9. package/src/changeBio.js +76 -0
  10. package/src/changeBlockedStatus.js +49 -0
  11. package/src/changeBlockedStatusMqtt.js +80 -0
  12. package/src/changeCover.js +72 -0
  13. package/src/changeGroupImage.js +135 -0
  14. package/src/changeName.js +78 -0
  15. package/src/changeNickname.js +59 -0
  16. package/src/changeThreadColor.js +65 -0
  17. package/src/changeThreadEmoji.js +55 -0
  18. package/src/changeUsername.js +58 -0
  19. package/src/createCommentPost.js +229 -0
  20. package/src/createNewGroup.js +88 -0
  21. package/src/createPoll.js +71 -0
  22. package/src/createPost.js +275 -0
  23. package/src/data/getThreadInfo.json +1 -0
  24. package/src/deleteMessage.js +56 -0
  25. package/src/deleteThread.js +56 -0
  26. package/src/editMessage.js +76 -0
  27. package/src/follow.js +74 -0
  28. package/src/forwardAttachment.js +60 -0
  29. package/src/getAccess.js +111 -0
  30. package/src/getAvatarUser.js +78 -0
  31. package/src/getBotInitialData.js +43 -0
  32. package/src/getCtx.js +5 -0
  33. package/src/getCurrentUserID.js +7 -0
  34. package/src/getEmojiUrl.js +29 -0
  35. package/src/getFriendsList.js +83 -0
  36. package/src/getMessage.js +835 -0
  37. package/src/getOptions.js +5 -0
  38. package/src/getRegion.js +7 -0
  39. package/src/getThreadHistory.js +680 -0
  40. package/src/getThreadHistoryDeprecated.js +93 -0
  41. package/src/getThreadInfo.js +227 -0
  42. package/src/getThreadInfoDeprecated.js +80 -0
  43. package/src/getThreadList.js +269 -0
  44. package/src/getThreadListDeprecated.js +75 -0
  45. package/src/getThreadPictures.js +79 -0
  46. package/src/getUID.js +122 -0
  47. package/src/getUserID.js +66 -0
  48. package/src/getUserInfo.js +82 -0
  49. package/src/handleFriendRequest.js +57 -0
  50. package/src/handleMessageRequest.js +65 -0
  51. package/src/httpGet.js +64 -0
  52. package/src/httpPost.js +64 -0
  53. package/src/httpPostFormData.js +70 -0
  54. package/src/listenMqtt.js +674 -0
  55. package/src/listenNotification.js +85 -0
  56. package/src/logout.js +75 -0
  57. package/src/markAsDelivered.js +55 -0
  58. package/src/markAsRead.js +85 -0
  59. package/src/markAsReadAll.js +50 -0
  60. package/src/markAsSeen.js +61 -0
  61. package/src/muteThread.js +52 -0
  62. package/src/pinMessage.js +59 -0
  63. package/src/refreshFb_dtsg.js +89 -0
  64. package/src/removeUserFromGroup.js +79 -0
  65. package/src/resolvePhotoUrl.js +45 -0
  66. package/src/searchForThread.js +53 -0
  67. package/src/searchStickers.js +53 -0
  68. package/src/sendMessage.js +442 -0
  69. package/src/sendMessageMqtt.js +316 -0
  70. package/src/sendTypingIndicator.js +28 -0
  71. package/src/setMessageReaction.js +122 -0
  72. package/src/setMessageReactionMqtt.js +62 -0
  73. package/src/setPostReaction.js +108 -0
  74. package/src/setProfileGuard.js +44 -0
  75. package/src/setStoryReaction.js +64 -0
  76. package/src/setTitle.js +90 -0
  77. package/src/shareContact.js +110 -0
  78. package/src/shareLink.js +59 -0
  79. package/src/stopListenMqtt.js +23 -0
  80. package/src/threadColors.js +131 -0
  81. package/src/unfriend.js +52 -0
  82. package/src/unsendMessage.js +45 -0
  83. package/src/uploadAttachment.js +94 -0
  84. package/utils.js +1441 -0
package/utils.js ADDED
@@ -0,0 +1,1441 @@
1
+ /* eslint-disable no-prototype-builtins */
2
+ "use strict";
3
+
4
+ const chalk = require("chalk");
5
+ const gradient = require("gradient-string");
6
+ const echaceb = gradient(["#0061ff", "#681297"]);
7
+ const ws = echaceb("ws3-fca");
8
+ const getRandom = arr => arr[Math.floor(Math.random() * arr.length)];
9
+ const defaultUserAgent = "facebookexternalhit/1.1";
10
+ const windowsUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
11
+ function randomUserAgent() {
12
+ const platform = {
13
+ platform: ['Windows NT 10.0; Win64; x64', 'Macintosh; Intel Mac OS X 14.7; rv:132.0'],
14
+ browsers: {
15
+ chrome: ['122.0.0.0', '121.0.0.0'],
16
+ firefox: ['123.0', '122.0'],
17
+ edge: ['122.0.2365.92']
18
+ }
19
+ };
20
+ const browserName = getRandom(Object.keys(platform.browsers));
21
+ const version = getRandom(platform.browsers[browserName]);
22
+ const plat = getRandom(platform.platform);
23
+ const userAgentArray = [
24
+ defaultUserAgent,
25
+ windowsUserAgent,
26
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
27
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Firefox/45.0",
28
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
29
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7",
30
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8",
31
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.3",
32
+ ];
33
+ const ua = getRandom([
34
+ browserName === 'firefox' ? `Mozilla/5.0 (${plat}) Gecko/20100101 Firefox/${version}` : `Mozilla/5.0 (${plat}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Safari/537.36`,
35
+ getRandom(userAgentArray)
36
+ ]);
37
+ return ua;
38
+ }
39
+ const headers = {
40
+ "content-type": "application/x-www-form-urlencoded",
41
+ "referer": "https://www.facebook.com/",
42
+ "origin": "https://www.facebook.com",
43
+ "connection": "keep-alive",
44
+ "Sec-Fetch-Site": "same-origin",
45
+ "Sec-Fetch-User": "?1"
46
+ };
47
+ let request = require("request").defaults({
48
+ jar: true
49
+ });
50
+ const stream = require("stream");
51
+ const querystring = require("querystring");
52
+ const url = require("url");
53
+ function setProxy(proxy) {
54
+ request = require("request").defaults({
55
+ jar: true,
56
+ ...(proxy && {
57
+ proxy
58
+ })
59
+ });
60
+ return;
61
+ }
62
+
63
+ function getHeaders(url, options, ctx, customHeader) {
64
+ const headers1 = {
65
+ "host": new URL(url).hostname,
66
+ ...headers,
67
+ "User-Agent": customHeader?.customUserAgent ?? options?.userAgent ?? defaultUserAgent
68
+ }
69
+ /*if (headers1["User-Agent"]) {
70
+ delete headers1["User-Agent"];
71
+ headers1["User-Agent"] = customHeader?.customUserAgent ?? options?.userAgent ?? defaultUserAgent;
72
+ */
73
+ if (ctx && ctx.region) headers1["X-MSGR-Region"] = ctx.region;
74
+ if (customHeader) {
75
+ Object.assign(headers1, customHeader);
76
+ if (customHeader.noRef) delete headers1.referer;
77
+ }
78
+ return headers1;
79
+ }
80
+
81
+
82
+ function isReadableStream(obj) {
83
+ return obj instanceof stream.Stream && typeof obj._read == "function" && getType(obj._readableState) == "Object";
84
+ }
85
+
86
+ function cleanGet(url) {
87
+ let callback;
88
+ var returnPromise = new Promise(function(resolve, reject) {
89
+ callback = (error, res) => error ? reject(error) : resolve(res);
90
+ });
91
+ request.get(url, { timeout: 60000 }, callback);
92
+ return returnPromise;
93
+ }
94
+
95
+ function get(url, jar, qs, options, ctx, customHeader) {
96
+ let callback;
97
+ var returnPromise = new Promise(function (resolve, reject) {
98
+ callback = (error, res) => error ? reject(error) : resolve(res);
99
+ });
100
+ if (getType(qs) == "Object")
101
+ for (let prop in qs) {
102
+ if (getType(qs[prop]) == 'Object')
103
+ qs[prop] = JSON.stringify(qs[prop]);
104
+ }
105
+ var op = {
106
+ headers: getHeaders(url, options, ctx, customHeader),
107
+ timeout: 60000,
108
+ qs,
109
+ jar,
110
+ gzip: true
111
+ }
112
+
113
+ request.get(url, op, callback);
114
+
115
+ return returnPromise;
116
+ }
117
+
118
+ function post(url, jar, form, options, ctx, customHeader) {
119
+ let callback;
120
+ var returnPromise = new Promise(function (resolve, reject) {
121
+ callback = (error, res) => error ? reject(error) : resolve(res);
122
+ });
123
+
124
+ var op = {
125
+ headers: getHeaders(url, options, ctx, customHeader),
126
+ timeout: 60000,
127
+ form,
128
+ jar,
129
+ gzip: true
130
+ }
131
+
132
+ request.post(url, op, callback);
133
+
134
+ return returnPromise;
135
+ }
136
+
137
+ function postFormData(url, jar, form, qs, options, ctx) {
138
+ let callback;
139
+ var returnPromise = new Promise(function (resolve, reject) {
140
+ callback = (error, res) => error ? reject(error) : resolve(res);
141
+ });
142
+ if (getType(qs) == "Object")
143
+ for (let prop in qs) {
144
+ if (getType(qs[prop]) == 'Object')
145
+ qs[prop] = JSON.stringify(qs[prop]);
146
+ }
147
+ var op = {
148
+ headers: getHeaders(url, options, ctx, {
149
+ 'content-type': 'multipart/form-data'
150
+ }),
151
+ timeout: 60000,
152
+ formData: form,
153
+ qs,
154
+ jar,
155
+ gzip: true
156
+ }
157
+
158
+ request.post(url, op, callback);
159
+
160
+ return returnPromise;
161
+ }
162
+
163
+
164
+ function padZeros(val, len) {
165
+ val = String(val);
166
+ len = len || 2;
167
+ while (val.length < len) val = "0" + val;
168
+ return val;
169
+ }
170
+
171
+ function generateThreadingID(clientID) {
172
+ const k = Date.now();
173
+ const l = Math.floor(Math.random() * 4294967295);
174
+ const m = clientID;
175
+ return "<" + k + ":" + l + "-" + m + "@mail.projektitan.com>";
176
+ }
177
+
178
+ function binaryToDecimal(data) {
179
+ let ret = "";
180
+ while (data !== "0") {
181
+ let end = 0;
182
+ let fullName = "";
183
+ let i = 0;
184
+ for (; i < data.length; i++) {
185
+ end = 2 * end + parseInt(data[i], 10);
186
+ if (end >= 10) {
187
+ fullName += "1";
188
+ end -= 10;
189
+ }
190
+ else {
191
+ fullName += "0";
192
+ }
193
+ }
194
+ ret = end.toString() + ret;
195
+ data = fullName.slice(fullName.indexOf("1"));
196
+ }
197
+ return ret;
198
+ }
199
+
200
+ function generateOfflineThreadingID() {
201
+ const ret = Date.now();
202
+ const value = Math.floor(Math.random() * 4294967295);
203
+ const str = ("0000000000000000000000" + value.toString(2)).slice(-22);
204
+ const msgs = ret.toString(2) + str;
205
+ return binaryToDecimal(msgs);
206
+ }
207
+
208
+ let h;
209
+ const i = {};
210
+ const j = {
211
+ _: "%",
212
+ A: "%2",
213
+ B: "000",
214
+ C: "%7d",
215
+ D: "%7b%22",
216
+ E: "%2c%22",
217
+ F: "%22%3a",
218
+ G: "%2c%22ut%22%3a1",
219
+ H: "%2c%22bls%22%3a",
220
+ I: "%2c%22n%22%3a%22%",
221
+ J: "%22%3a%7b%22i%22%3a0%7d",
222
+ K: "%2c%22pt%22%3a0%2c%22vis%22%3a",
223
+ L: "%2c%22ch%22%3a%7b%22h%22%3a%22",
224
+ M: "%7b%22v%22%3a2%2c%22time%22%3a1",
225
+ N: ".channel%22%2c%22sub%22%3a%5b",
226
+ O: "%2c%22sb%22%3a1%2c%22t%22%3a%5b",
227
+ P: "%2c%22ud%22%3a100%2c%22lc%22%3a0",
228
+ Q: "%5d%2c%22f%22%3anull%2c%22uct%22%3a",
229
+ R: ".channel%22%2c%22sub%22%3a%5b1%5d",
230
+ S: "%22%2c%22m%22%3a0%7d%2c%7b%22i%22%3a",
231
+ T: "%2c%22blc%22%3a1%2c%22snd%22%3a1%2c%22ct%22%3a",
232
+ U: "%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
233
+ V: "%2c%22blc%22%3a0%2c%22snd%22%3a0%2c%22ct%22%3a",
234
+ W: "%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a",
235
+ X: "%2c%22ri%22%3a0%7d%2c%22state%22%3a%7b%22p%22%3a0%2c%22ut%22%3a1",
236
+ Y: "%2c%22pt%22%3a0%2c%22vis%22%3a1%2c%22bls%22%3a0%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
237
+ Z: "%2c%22sb%22%3a1%2c%22t%22%3a%5b%5d%2c%22f%22%3anull%2c%22uct%22%3a0%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a"
238
+ };
239
+ (function() {
240
+ const l = [];
241
+ for (const m in j) {
242
+ i[j[m]] = m;
243
+ l.push(j[m]);
244
+ }
245
+ l.reverse();
246
+ h = new RegExp(l.join("|"), "g");
247
+ })();
248
+
249
+ function presenceEncode(str) {
250
+ return encodeURIComponent(str)
251
+ .replace(/([_A-Z])|%../g, function(m, n) {
252
+ return n ? "%" + n.charCodeAt(0).toString(16) : m;
253
+ })
254
+ .toLowerCase()
255
+ .replace(h, function(m) {
256
+ return i[m];
257
+ });
258
+ }
259
+
260
+ // eslint-disable-next-line no-unused-vars
261
+ function presenceDecode(str) {
262
+ return decodeURIComponent(
263
+ str.replace(/[_A-Z]/g, function(m) {
264
+ return j[m];
265
+ })
266
+ );
267
+ }
268
+
269
+ function generatePresence(userID) {
270
+ const time = Date.now();
271
+ return (
272
+ "E" +
273
+ presenceEncode(
274
+ JSON.stringify({
275
+ v: 3,
276
+ time: parseInt(time / 1000, 10),
277
+ user: userID,
278
+ state: {
279
+ ut: 0,
280
+ t2: [],
281
+ lm2: null,
282
+ uct2: time,
283
+ tr: null,
284
+ tw: Math.floor(Math.random() * 4294967295) + 1,
285
+ at: time
286
+ },
287
+ ch: {
288
+ ["p_" + userID]: 0
289
+ }
290
+ })
291
+ )
292
+ );
293
+ }
294
+
295
+ function generateAccessiblityCookie() {
296
+ const time = Date.now();
297
+ return encodeURIComponent(
298
+ JSON.stringify({
299
+ sr: 0,
300
+ "sr-ts": time,
301
+ jk: 0,
302
+ "jk-ts": time,
303
+ kb: 0,
304
+ "kb-ts": time,
305
+ hcm: 0,
306
+ "hcm-ts": time
307
+ })
308
+ );
309
+ }
310
+
311
+ function getGUID() {
312
+ /** @type {number} */
313
+ let sectionLength = Date.now();
314
+ /** @type {string} */
315
+ const id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
316
+ /** @type {number} */
317
+ const r = Math.floor((sectionLength + Math.random() * 16) % 16);
318
+ /** @type {number} */
319
+ sectionLength = Math.floor(sectionLength / 16);
320
+ /** @type {string} */
321
+ const _guid = (c == "x" ? r : (r & 7) | 8).toString(16);
322
+ return _guid;
323
+ });
324
+ return id;
325
+ }
326
+
327
+ function getExtension(original_extension, fullFileName = "") {
328
+ if (original_extension) {
329
+ return original_extension;
330
+ }
331
+ else {
332
+ const extension = fullFileName.split(".").pop();
333
+ if (extension === fullFileName) {
334
+ return "";
335
+ }
336
+ else {
337
+ return extension;
338
+ }
339
+ }
340
+ }
341
+
342
+ function _formatAttachment(attachment1, attachment2) {
343
+ // TODO: THIS IS REALLY BAD
344
+ // This is an attempt at fixing Facebook's inconsistencies. Sometimes they give us
345
+ // two attachment objects, but sometimes only one. They each contain part of the
346
+ // data that you'd want so we merge them for convenience.
347
+ // Instead of having a bunch of if statements guarding every access to image_data,
348
+ // we set it to empty object and use the fact that it'll return undefined.
349
+ const fullFileName = attachment1.filename;
350
+ const fileSize = Number(attachment1.fileSize || 0);
351
+ const durationVideo = attachment1.genericMetadata ? Number(attachment1.genericMetadata.videoLength) : undefined;
352
+ const durationAudio = attachment1.genericMetadata ? Number(attachment1.genericMetadata.duration) : undefined;
353
+ const mimeType = attachment1.mimeType;
354
+
355
+ attachment2 = attachment2 || { id: "", image_data: {} };
356
+ attachment1 = attachment1.mercury || attachment1;
357
+ let blob = attachment1.blob_attachment || attachment1.sticker_attachment;
358
+ let type =
359
+ blob && blob.__typename ? blob.__typename : attachment1.attach_type;
360
+ if (!type && attachment1.sticker_attachment) {
361
+ type = "StickerAttachment";
362
+ blob = attachment1.sticker_attachment;
363
+ }
364
+ else if (!type && attachment1.extensible_attachment) {
365
+ if (
366
+ attachment1.extensible_attachment.story_attachment &&
367
+ attachment1.extensible_attachment.story_attachment.target &&
368
+ attachment1.extensible_attachment.story_attachment.target.__typename &&
369
+ attachment1.extensible_attachment.story_attachment.target.__typename === "MessageLocation"
370
+ ) {
371
+ type = "MessageLocation";
372
+ }
373
+ else {
374
+ type = "ExtensibleAttachment";
375
+ }
376
+
377
+ blob = attachment1.extensible_attachment;
378
+ }
379
+ // TODO: Determine whether "sticker", "photo", "file" etc are still used
380
+ // KEEP IN SYNC WITH getThreadHistory
381
+ switch (type) {
382
+ case "sticker":
383
+ return {
384
+ type: "sticker",
385
+ ID: attachment1.metadata.stickerID.toString(),
386
+ url: attachment1.url,
387
+
388
+ packID: attachment1.metadata.packID.toString(),
389
+ spriteUrl: attachment1.metadata.spriteURI,
390
+ spriteUrl2x: attachment1.metadata.spriteURI2x,
391
+ width: attachment1.metadata.width,
392
+ height: attachment1.metadata.height,
393
+
394
+ caption: attachment2.caption,
395
+ description: attachment2.description,
396
+
397
+ frameCount: attachment1.metadata.frameCount,
398
+ frameRate: attachment1.metadata.frameRate,
399
+ framesPerRow: attachment1.metadata.framesPerRow,
400
+ framesPerCol: attachment1.metadata.framesPerCol,
401
+
402
+ stickerID: attachment1.metadata.stickerID.toString(), // @Legacy
403
+ spriteURI: attachment1.metadata.spriteURI, // @Legacy
404
+ spriteURI2x: attachment1.metadata.spriteURI2x // @Legacy
405
+ };
406
+ case "file":
407
+ return {
408
+ type: "file",
409
+ ID: attachment2.id.toString(),
410
+ fullFileName: fullFileName,
411
+ filename: attachment1.name,
412
+ fileSize: fileSize,
413
+ original_extension: getExtension(attachment1.original_extension, fullFileName),
414
+ mimeType: mimeType,
415
+ url: attachment1.url,
416
+
417
+ isMalicious: attachment2.is_malicious,
418
+ contentType: attachment2.mime_type,
419
+
420
+ name: attachment1.name // @Legacy
421
+ };
422
+ case "photo":
423
+ return {
424
+ type: "photo",
425
+ ID: attachment1.metadata.fbid.toString(),
426
+ filename: attachment1.fileName,
427
+ fullFileName: fullFileName,
428
+ fileSize: fileSize,
429
+ original_extension: getExtension(attachment1.original_extension, fullFileName),
430
+ mimeType: mimeType,
431
+ thumbnailUrl: attachment1.thumbnail_url,
432
+
433
+ previewUrl: attachment1.preview_url,
434
+ previewWidth: attachment1.preview_width,
435
+ previewHeight: attachment1.preview_height,
436
+
437
+ largePreviewUrl: attachment1.large_preview_url,
438
+ largePreviewWidth: attachment1.large_preview_width,
439
+ largePreviewHeight: attachment1.large_preview_height,
440
+
441
+ url: attachment1.metadata.url, // @Legacy
442
+ width: attachment1.metadata.dimensions.split(",")[0], // @Legacy
443
+ height: attachment1.metadata.dimensions.split(",")[1], // @Legacy
444
+ name: fullFileName // @Legacy
445
+ };
446
+ case "animated_image":
447
+ return {
448
+ type: "animated_image",
449
+ ID: attachment2.id.toString(),
450
+ filename: attachment2.filename,
451
+ fullFileName: fullFileName,
452
+ original_extension: getExtension(attachment2.original_extension, fullFileName),
453
+ mimeType: mimeType,
454
+
455
+ previewUrl: attachment1.preview_url,
456
+ previewWidth: attachment1.preview_width,
457
+ previewHeight: attachment1.preview_height,
458
+
459
+ url: attachment2.image_data.url,
460
+ width: attachment2.image_data.width,
461
+ height: attachment2.image_data.height,
462
+
463
+ name: attachment1.name, // @Legacy
464
+ facebookUrl: attachment1.url, // @Legacy
465
+ thumbnailUrl: attachment1.thumbnail_url, // @Legacy
466
+ rawGifImage: attachment2.image_data.raw_gif_image, // @Legacy
467
+ rawWebpImage: attachment2.image_data.raw_webp_image, // @Legacy
468
+ animatedGifUrl: attachment2.image_data.animated_gif_url, // @Legacy
469
+ animatedGifPreviewUrl: attachment2.image_data.animated_gif_preview_url, // @Legacy
470
+ animatedWebpUrl: attachment2.image_data.animated_webp_url, // @Legacy
471
+ animatedWebpPreviewUrl: attachment2.image_data.animated_webp_preview_url // @Legacy
472
+ };
473
+ case "share":
474
+ return {
475
+ type: "share",
476
+ ID: attachment1.share.share_id.toString(),
477
+ url: attachment2.href,
478
+
479
+ title: attachment1.share.title,
480
+ description: attachment1.share.description,
481
+ source: attachment1.share.source,
482
+
483
+ image: attachment1.share.media.image,
484
+ width: attachment1.share.media.image_size.width,
485
+ height: attachment1.share.media.image_size.height,
486
+ playable: attachment1.share.media.playable,
487
+ duration: attachment1.share.media.duration,
488
+
489
+ subattachments: attachment1.share.subattachments,
490
+ properties: {},
491
+
492
+ animatedImageSize: attachment1.share.media.animated_image_size, // @Legacy
493
+ facebookUrl: attachment1.share.uri, // @Legacy
494
+ target: attachment1.share.target, // @Legacy
495
+ styleList: attachment1.share.style_list // @Legacy
496
+ };
497
+ case "video":
498
+ return {
499
+ type: "video",
500
+ ID: attachment1.metadata.fbid.toString(),
501
+ filename: attachment1.name,
502
+ fullFileName: fullFileName,
503
+ original_extension: getExtension(attachment1.original_extension, fullFileName),
504
+ mimeType: mimeType,
505
+ duration: durationVideo,
506
+
507
+ previewUrl: attachment1.preview_url,
508
+ previewWidth: attachment1.preview_width,
509
+ previewHeight: attachment1.preview_height,
510
+
511
+ url: attachment1.url,
512
+ width: attachment1.metadata.dimensions.width,
513
+ height: attachment1.metadata.dimensions.height,
514
+
515
+ videoType: "unknown",
516
+
517
+ thumbnailUrl: attachment1.thumbnail_url // @Legacy
518
+ };
519
+ case "error":
520
+ return {
521
+ type: "error",
522
+
523
+ // Save error attachments because we're unsure of their format,
524
+ // and whether there are cases they contain something useful for debugging.
525
+ attachment1: attachment1,
526
+ attachment2: attachment2
527
+ };
528
+ case "MessageImage":
529
+ return {
530
+ type: "photo",
531
+ ID: blob.legacy_attachment_id,
532
+ filename: blob.filename,
533
+ fullFileName: fullFileName,
534
+ fileSize: fileSize,
535
+ original_extension: getExtension(blob.original_extension, fullFileName),
536
+ mimeType: mimeType,
537
+ thumbnailUrl: blob.thumbnail.uri,
538
+
539
+ previewUrl: blob.preview.uri,
540
+ previewWidth: blob.preview.width,
541
+ previewHeight: blob.preview.height,
542
+
543
+ largePreviewUrl: blob.large_preview.uri,
544
+ largePreviewWidth: blob.large_preview.width,
545
+ largePreviewHeight: blob.large_preview.height,
546
+
547
+ url: blob.large_preview.uri, // @Legacy
548
+ width: blob.original_dimensions.x, // @Legacy
549
+ height: blob.original_dimensions.y, // @Legacy
550
+ name: blob.filename // @Legacy
551
+ };
552
+ case "MessageAnimatedImage":
553
+ return {
554
+ type: "animated_image",
555
+ ID: blob.legacy_attachment_id,
556
+ filename: blob.filename,
557
+ fullFileName: fullFileName,
558
+ original_extension: getExtension(blob.original_extension, fullFileName),
559
+ mimeType: mimeType,
560
+
561
+ previewUrl: blob.preview_image.uri,
562
+ previewWidth: blob.preview_image.width,
563
+ previewHeight: blob.preview_image.height,
564
+
565
+ url: blob.animated_image.uri,
566
+ width: blob.animated_image.width,
567
+ height: blob.animated_image.height,
568
+
569
+ thumbnailUrl: blob.preview_image.uri, // @Legacy
570
+ name: blob.filename, // @Legacy
571
+ facebookUrl: blob.animated_image.uri, // @Legacy
572
+ rawGifImage: blob.animated_image.uri, // @Legacy
573
+ animatedGifUrl: blob.animated_image.uri, // @Legacy
574
+ animatedGifPreviewUrl: blob.preview_image.uri, // @Legacy
575
+ animatedWebpUrl: blob.animated_image.uri, // @Legacy
576
+ animatedWebpPreviewUrl: blob.preview_image.uri // @Legacy
577
+ };
578
+ case "MessageVideo":
579
+ return {
580
+ type: "video",
581
+ ID: blob.legacy_attachment_id,
582
+ filename: blob.filename,
583
+ fullFileName: fullFileName,
584
+ original_extension: getExtension(blob.original_extension, fullFileName),
585
+ fileSize: fileSize,
586
+ duration: durationVideo,
587
+ mimeType: mimeType,
588
+
589
+ previewUrl: blob.large_image.uri,
590
+ previewWidth: blob.large_image.width,
591
+ previewHeight: blob.large_image.height,
592
+
593
+ url: blob.playable_url,
594
+ width: blob.original_dimensions.x,
595
+ height: blob.original_dimensions.y,
596
+
597
+ videoType: blob.video_type.toLowerCase(),
598
+
599
+ thumbnailUrl: blob.large_image.uri // @Legacy
600
+ };
601
+ case "MessageAudio":
602
+ return {
603
+ type: "audio",
604
+ ID: blob.url_shimhash,
605
+ filename: blob.filename,
606
+ fullFileName: fullFileName,
607
+ fileSize: fileSize,
608
+ duration: durationAudio,
609
+ original_extension: getExtension(blob.original_extension, fullFileName),
610
+ mimeType: mimeType,
611
+
612
+ audioType: blob.audio_type,
613
+ url: blob.playable_url,
614
+
615
+ isVoiceMail: blob.is_voicemail
616
+ };
617
+ case "StickerAttachment":
618
+ case "Sticker":
619
+ return {
620
+ type: "sticker",
621
+ ID: blob.id,
622
+ url: blob.url,
623
+
624
+ packID: blob.pack ? blob.pack.id : null,
625
+ spriteUrl: blob.sprite_image,
626
+ spriteUrl2x: blob.sprite_image_2x,
627
+ width: blob.width,
628
+ height: blob.height,
629
+
630
+ caption: blob.label,
631
+ description: blob.label,
632
+
633
+ frameCount: blob.frame_count,
634
+ frameRate: blob.frame_rate,
635
+ framesPerRow: blob.frames_per_row,
636
+ framesPerCol: blob.frames_per_column,
637
+
638
+ stickerID: blob.id, // @Legacy
639
+ spriteURI: blob.sprite_image, // @Legacy
640
+ spriteURI2x: blob.sprite_image_2x // @Legacy
641
+ };
642
+ case "MessageLocation":
643
+ var urlAttach = blob.story_attachment.url;
644
+ var mediaAttach = blob.story_attachment.media;
645
+
646
+ var u = querystring.parse(url.parse(urlAttach).query).u;
647
+ var where1 = querystring.parse(url.parse(u).query).where1;
648
+ var address = where1.split(", ");
649
+
650
+ var latitude;
651
+ var longitude;
652
+
653
+ try {
654
+ latitude = Number.parseFloat(address[0]);
655
+ longitude = Number.parseFloat(address[1]);
656
+ } catch (err) {
657
+ /* empty */
658
+ }
659
+
660
+ var imageUrl;
661
+ var width;
662
+ var height;
663
+
664
+ if (mediaAttach && mediaAttach.image) {
665
+ imageUrl = mediaAttach.image.uri;
666
+ width = mediaAttach.image.width;
667
+ height = mediaAttach.image.height;
668
+ }
669
+
670
+ return {
671
+ type: "location",
672
+ ID: blob.legacy_attachment_id,
673
+ latitude: latitude,
674
+ longitude: longitude,
675
+ image: imageUrl,
676
+ width: width,
677
+ height: height,
678
+ url: u || urlAttach,
679
+ address: where1,
680
+
681
+ facebookUrl: blob.story_attachment.url, // @Legacy
682
+ target: blob.story_attachment.target, // @Legacy
683
+ styleList: blob.story_attachment.style_list // @Legacy
684
+ };
685
+ case "ExtensibleAttachment":
686
+ return {
687
+ type: "share",
688
+ ID: blob.legacy_attachment_id,
689
+ url: blob.story_attachment.url,
690
+
691
+ title: blob.story_attachment.title_with_entities.text,
692
+ description:
693
+ blob.story_attachment.description &&
694
+ blob.story_attachment.description.text,
695
+ source: blob.story_attachment.source ?
696
+ blob.story_attachment.source.text :
697
+ null,
698
+
699
+ image:
700
+ blob.story_attachment.media &&
701
+ blob.story_attachment.media.image &&
702
+ blob.story_attachment.media.image.uri,
703
+ width:
704
+ blob.story_attachment.media &&
705
+ blob.story_attachment.media.image &&
706
+ blob.story_attachment.media.image.width,
707
+ height:
708
+ blob.story_attachment.media &&
709
+ blob.story_attachment.media.image &&
710
+ blob.story_attachment.media.image.height,
711
+ playable:
712
+ blob.story_attachment.media &&
713
+ blob.story_attachment.media.is_playable,
714
+ duration:
715
+ blob.story_attachment.media &&
716
+ blob.story_attachment.media.playable_duration_in_ms,
717
+ playableUrl:
718
+ blob.story_attachment.media == null ?
719
+ null :
720
+ blob.story_attachment.media.playable_url,
721
+
722
+ subattachments: blob.story_attachment.subattachments,
723
+ properties: blob.story_attachment.properties.reduce(function(obj, cur) {
724
+ obj[cur.key] = cur.value.text;
725
+ return obj;
726
+ }, {}),
727
+
728
+ facebookUrl: blob.story_attachment.url, // @Legacy
729
+ target: blob.story_attachment.target, // @Legacy
730
+ styleList: blob.story_attachment.style_list // @Legacy
731
+ };
732
+ case "MessageFile":
733
+ return {
734
+ type: "file",
735
+ ID: blob.message_file_fbid,
736
+ fullFileName: fullFileName,
737
+ filename: blob.filename,
738
+ fileSize: fileSize,
739
+ mimeType: blob.mimetype,
740
+ original_extension: blob.original_extension || fullFileName.split(".").pop(),
741
+
742
+ url: blob.url,
743
+ isMalicious: blob.is_malicious,
744
+ contentType: blob.content_type,
745
+
746
+ name: blob.filename
747
+ };
748
+ default:
749
+ throw new Error(
750
+ "unrecognized attach_file of type " +
751
+ type +
752
+ "`" +
753
+ JSON.stringify(attachment1, null, 4) +
754
+ " attachment2: " +
755
+ JSON.stringify(attachment2, null, 4) +
756
+ "`"
757
+ );
758
+ }
759
+ }
760
+
761
+ function formatAttachment(attachments, attachmentIds, attachmentMap, shareMap) {
762
+ attachmentMap = shareMap || attachmentMap;
763
+ return attachments ?
764
+ attachments.map(function(val, i) {
765
+ if (
766
+ !attachmentMap ||
767
+ !attachmentIds ||
768
+ !attachmentMap[attachmentIds[i]]
769
+ ) {
770
+ return _formatAttachment(val);
771
+ }
772
+ return _formatAttachment(val, attachmentMap[attachmentIds[i]]);
773
+ }) : [];
774
+ }
775
+
776
+ function formatDeltaMessage(m) {
777
+ const md = m.delta.messageMetadata;
778
+
779
+ const mdata =
780
+ m.delta.data === undefined ? [] :
781
+ m.delta.data.prng === undefined ? [] :
782
+ JSON.parse(m.delta.data.prng);
783
+ const m_id = mdata.map(u => u.i);
784
+ const m_offset = mdata.map(u => u.o);
785
+ const m_length = mdata.map(u => u.l);
786
+ const mentions = {};
787
+ for (let i = 0; i < m_id.length; i++) {
788
+ mentions[m_id[i]] = m.delta.body.substring(
789
+ m_offset[i],
790
+ m_offset[i] + m_length[i]
791
+ );
792
+ }
793
+
794
+ return {
795
+ type: "message",
796
+ senderID: formatID(md.actorFbId.toString()),
797
+ body: m.delta.body || "",
798
+ threadID: formatID(
799
+ (md.threadKey.threadFbId || md.threadKey.otherUserFbId).toString()
800
+ ),
801
+ messageID: md.messageId,
802
+ attachments: (m.delta.attachments || []).map(v => _formatAttachment(v)),
803
+ mentions: mentions,
804
+ timestamp: md.timestamp,
805
+ isGroup: !!md.threadKey.threadFbId,
806
+ participantIDs: m.delta.participants
807
+ };
808
+ }
809
+
810
+ function formatID(id) {
811
+ if (id != undefined && id != null) {
812
+ return id.replace(/(fb)?id[:.]/, "");
813
+ }
814
+ else {
815
+ return id;
816
+ }
817
+ }
818
+
819
+ function formatMessage(m) {
820
+ const originalMessage = m.message ? m.message : m;
821
+ const obj = {
822
+ type: "message",
823
+ senderName: originalMessage.sender_name,
824
+ senderID: formatID(originalMessage.sender_fbid.toString()),
825
+ participantNames: originalMessage.group_thread_info ?
826
+ originalMessage.group_thread_info.participant_names : [originalMessage.sender_name.split(" ")[0]],
827
+ participantIDs: originalMessage.group_thread_info ?
828
+ originalMessage.group_thread_info.participant_ids.map(function(v) {
829
+ return formatID(v.toString());
830
+ }) : [formatID(originalMessage.sender_fbid)],
831
+ body: originalMessage.body || "",
832
+ threadID: formatID(
833
+ (
834
+ originalMessage.thread_fbid || originalMessage.other_user_fbid
835
+ ).toString()
836
+ ),
837
+ threadName: originalMessage.group_thread_info ?
838
+ originalMessage.group_thread_info.name : originalMessage.sender_name,
839
+ location: originalMessage.coordinates ? originalMessage.coordinates : null,
840
+ messageID: originalMessage.mid ?
841
+ originalMessage.mid.toString() : originalMessage.message_id,
842
+ attachments: formatAttachment(
843
+ originalMessage.attachments,
844
+ originalMessage.attachmentIds,
845
+ originalMessage.attachment_map,
846
+ originalMessage.share_map
847
+ ),
848
+ timestamp: originalMessage.timestamp,
849
+ timestampAbsolute: originalMessage.timestamp_absolute,
850
+ timestampRelative: originalMessage.timestamp_relative,
851
+ timestampDatetime: originalMessage.timestamp_datetime,
852
+ tags: originalMessage.tags,
853
+ reactions: originalMessage.reactions ? originalMessage.reactions : [],
854
+ isUnread: originalMessage.is_unread
855
+ };
856
+
857
+ if (m.type === "pages_messaging")
858
+ obj.pageID = m.realtime_viewer_fbid.toString();
859
+ obj.isGroup = obj.participantIDs.length > 2;
860
+
861
+ return obj;
862
+ }
863
+
864
+ function formatEvent(m) {
865
+ const originalMessage = m.message ? m.message : m;
866
+ let logMessageType = originalMessage.log_message_type;
867
+ let logMessageData;
868
+ if (logMessageType === "log:generic-admin-text") {
869
+ logMessageData = originalMessage.log_message_data.untypedData;
870
+ logMessageType = getAdminTextMessageType(
871
+ originalMessage.log_message_data.message_type
872
+ );
873
+ }
874
+ else {
875
+ logMessageData = originalMessage.log_message_data;
876
+ }
877
+
878
+ return Object.assign(formatMessage(originalMessage), {
879
+ type: "event",
880
+ logMessageType: logMessageType,
881
+ logMessageData: logMessageData,
882
+ logMessageBody: originalMessage.log_message_body
883
+ });
884
+ }
885
+
886
+ function formatHistoryMessage(m) {
887
+ switch (m.action_type) {
888
+ case "ma-type:log-message":
889
+ return formatEvent(m);
890
+ default:
891
+ return formatMessage(m);
892
+ }
893
+ }
894
+
895
+ // Get a more readable message type for AdminTextMessages
896
+ function getAdminTextMessageType(type) {
897
+ switch (type) {
898
+ case 'unpin_messages_v2':
899
+ return 'log:unpin-message';
900
+ case 'pin_messages_v2':
901
+ return 'log:pin-message';
902
+ case "change_thread_theme":
903
+ return "log:thread-color";
904
+ case "change_thread_icon":
905
+ case 'change_thread_quick_reaction':
906
+ return "log:thread-icon";
907
+ case "change_thread_nickname":
908
+ return "log:user-nickname";
909
+ case "change_thread_admins":
910
+ return "log:thread-admins";
911
+ case "group_poll":
912
+ return "log:thread-poll";
913
+ case "change_thread_approval_mode":
914
+ return "log:thread-approval-mode";
915
+ case "messenger_call_log":
916
+ case "participant_joined_group_call":
917
+ return "log:thread-call";
918
+ default:
919
+ return type;
920
+ }
921
+ }
922
+
923
+ function formatDeltaEvent(m) {
924
+ let logMessageType;
925
+ let logMessageData;
926
+
927
+ // log:thread-color => {theme_color}
928
+ // log:user-nickname => {participant_id, nickname}
929
+ // log:thread-icon => {thread_icon}
930
+ // log:thread-name => {name}
931
+ // log:subscribe => {addedParticipants - [Array]}
932
+ // log:unsubscribe => {leftParticipantFbId}
933
+
934
+ switch (m.class) {
935
+ case "AdminTextMessage":
936
+ logMessageData = m.untypedData;
937
+ logMessageType = getAdminTextMessageType(m.type);
938
+ break;
939
+ case "ThreadName":
940
+ logMessageType = "log:thread-name";
941
+ logMessageData = { name: m.name };
942
+ break;
943
+ case "ParticipantsAddedToGroupThread":
944
+ logMessageType = "log:subscribe";
945
+ logMessageData = { addedParticipants: m.addedParticipants };
946
+ break;
947
+ case "ParticipantLeftGroupThread":
948
+ logMessageType = "log:unsubscribe";
949
+ logMessageData = { leftParticipantFbId: m.leftParticipantFbId };
950
+ break;
951
+ case "ApprovalQueue":
952
+ logMessageType = "log:approval-queue";
953
+ logMessageData = {
954
+ approvalQueue: {
955
+ action: m.action,
956
+ recipientFbId: m.recipientFbId,
957
+ requestSource: m.requestSource,
958
+ ...m.messageMetadata
959
+ }
960
+ };
961
+ }
962
+ return {
963
+ type: "event",
964
+ threadID: formatID(
965
+ (
966
+ m.messageMetadata.threadKey.threadFbId ||
967
+ m.messageMetadata.threadKey.otherUserFbId
968
+ ).toString()
969
+ ),
970
+ messageID: m.messageMetadata.messageId.toString(),
971
+ logMessageType,
972
+ logMessageData,
973
+ logMessageBody: m.messageMetadata.adminText,
974
+ timestamp: m.messageMetadata.timestamp,
975
+ author: m.messageMetadata.actorFbId,
976
+ participantIDs: m.participants
977
+ };
978
+ }
979
+
980
+ function formatTyp(event) {
981
+ return {
982
+ isTyping: !!event.st,
983
+ from: event.from.toString(),
984
+ threadID: formatID(
985
+ (event.to || event.thread_fbid || event.from).toString()
986
+ ),
987
+ // When receiving typ indication from mobile, `from_mobile` isn't set.
988
+ // If it is, we just use that value.
989
+ fromMobile: event.hasOwnProperty("from_mobile") ? event.from_mobile : true,
990
+ userID: (event.realtime_viewer_fbid || event.from).toString(),
991
+ type: "typ"
992
+ };
993
+ }
994
+
995
+ function formatDeltaReadReceipt(delta) {
996
+ // otherUserFbId seems to be used as both the readerID and the threadID in a 1-1 chat.
997
+ // In a group chat actorFbId is used for the reader and threadFbId for the thread.
998
+ return {
999
+ reader: (delta.threadKey.otherUserFbId || delta.actorFbId).toString(),
1000
+ time: delta.actionTimestampMs,
1001
+ threadID: formatID(
1002
+ (delta.threadKey.otherUserFbId || delta.threadKey.threadFbId).toString()
1003
+ ),
1004
+ type: "read_receipt"
1005
+ };
1006
+ }
1007
+
1008
+ function formatReadReceipt(event) {
1009
+ return {
1010
+ reader: event.reader.toString(),
1011
+ time: event.time,
1012
+ threadID: formatID((event.thread_fbid || event.reader).toString()),
1013
+ type: "read_receipt"
1014
+ };
1015
+ }
1016
+
1017
+ function formatRead(event) {
1018
+ return {
1019
+ threadID: formatID(
1020
+ (
1021
+ (event.chat_ids && event.chat_ids[0]) ||
1022
+ (event.thread_fbids && event.thread_fbids[0])
1023
+ ).toString()
1024
+ ),
1025
+ time: event.timestamp,
1026
+ type: "read"
1027
+ };
1028
+ }
1029
+
1030
+ function getFrom(str, startToken, endToken) {
1031
+ const start = str.indexOf(startToken) + startToken.length;
1032
+ if (start < startToken.length) return "";
1033
+
1034
+ const lastHalf = str.substring(start);
1035
+ const end = lastHalf.indexOf(endToken);
1036
+ if (end === -1) {
1037
+ throw Error(
1038
+ "Could not find endTime `" + endToken + "` in the given string."
1039
+ );
1040
+ }
1041
+ return lastHalf.substring(0, end);
1042
+ }
1043
+
1044
+ function makeParsable(html) {
1045
+ const withoutForLoop = html.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, "");
1046
+
1047
+ // (What the fuck FB, why windows style newlines?)
1048
+ // So sometimes FB will send us base multiple objects in the same response.
1049
+ // They're all valid JSON, one after the other, at the top level. We detect
1050
+ // that and make it parse-able by JSON.parse.
1051
+ // Ben - July 15th 2017
1052
+ //
1053
+ // It turns out that Facebook may insert random number of spaces before
1054
+ // next object begins (issue #616)
1055
+ // rav_kr - 2018-03-19
1056
+ const maybeMultipleObjects = withoutForLoop.split(/\}\r\n *\{/);
1057
+ if (maybeMultipleObjects.length === 1) return maybeMultipleObjects;
1058
+
1059
+ return "[" + maybeMultipleObjects.join("},{") + "]";
1060
+ }
1061
+
1062
+ function arrToForm(form) {
1063
+ return arrayToObject(
1064
+ form,
1065
+ function(v) {
1066
+ return v.name;
1067
+ },
1068
+ function(v) {
1069
+ return v.val;
1070
+ }
1071
+ );
1072
+ }
1073
+
1074
+ function arrayToObject(arr, getKey, getValue) {
1075
+ return arr.reduce(function(acc, val) {
1076
+ acc[getKey(val)] = getValue(val);
1077
+ return acc;
1078
+ }, {});
1079
+ }
1080
+
1081
+ function getSignatureID() {
1082
+ return Math.floor(Math.random() * 2147483648).toString(16);
1083
+ }
1084
+
1085
+ function generateTimestampRelative() {
1086
+ const d = new Date();
1087
+ return d.getHours() + ":" + padZeros(d.getMinutes());
1088
+ }
1089
+
1090
+ function makeDefaults(html, userID, ctx) {
1091
+ let reqCounter = 1;
1092
+ const fb_dtsg = getFrom(html, 'name="fb_dtsg" value="', '"');
1093
+
1094
+ let ttstamp = "2";
1095
+ for (let i = 0; i < fb_dtsg.length; i++) {
1096
+ ttstamp += fb_dtsg.charCodeAt(i);
1097
+ }
1098
+ const revision = getFrom(html, 'revision":', ",");
1099
+
1100
+ function mergeWithDefaults(obj) {
1101
+ const newObj = {
1102
+ av: userID,
1103
+ __user: userID,
1104
+ __req: (reqCounter++).toString(36),
1105
+ __rev: revision,
1106
+ __a: 1,
1107
+ fb_dtsg: ctx.fb_dtsg || fb_dtsg,
1108
+ jazoest: ctx.ttstamp || ttstamp
1109
+ }
1110
+
1111
+ if (!obj) return newObj;
1112
+
1113
+ for (var prop in obj) {
1114
+ if (obj.hasOwnProperty(prop)) {
1115
+ if (!newObj[prop])
1116
+ newObj[prop] = obj[prop];
1117
+ }
1118
+ }
1119
+
1120
+ return newObj;
1121
+ }
1122
+
1123
+ return {
1124
+ get: (url, jar, qs, ctxx, customHeader = {}) => get(url, jar, mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx, customHeader),
1125
+ post: (url, jar, form, ctxx, customHeader = {}) => post(url, jar, mergeWithDefaults(form), ctx.globalOptions, ctxx || ctx, customHeader),
1126
+ postFormData: (url, jar, form, qs, ctxx) => postFormData(url, jar, mergeWithDefaults(form), mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx)
1127
+ };
1128
+ }
1129
+
1130
+ function parseAndCheckLogin(ctx, http, retryCount) {
1131
+ var delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
1132
+ var _try = (tryData) => new Promise(function(resolve, reject) {
1133
+ try {
1134
+ resolve(tryData());
1135
+ } catch (error) {
1136
+ reject(error);
1137
+ }
1138
+ });
1139
+ if (retryCount == undefined) retryCount = 0;
1140
+
1141
+ return function(data) {
1142
+ function any() {
1143
+ if (data.statusCode >= 500 && data.statusCode < 600) {
1144
+ if (retryCount >= 5) {
1145
+ const err = new Error("Request retry failed. Check the `res` and `statusCode` property on this error.");
1146
+ err.statusCode = data.statusCode;
1147
+ err.res = data.body;
1148
+ err.error = "Request retry failed. Check the `res` and `statusCode` property on this error.";
1149
+ throw err;
1150
+ }
1151
+ retryCount++;
1152
+ const retryTime = Math.floor(Math.random() * 5000);
1153
+ console.warn("parseAndCheckLogin", "Got status code " + data.statusCode + " - " + retryCount + ". attempt to retry in " + retryTime + " milliseconds...");
1154
+ const url = data.request.uri.protocol + "//" + data.request.uri.hostname + data.request.uri.pathname;
1155
+ if (data.request.headers["content-type"].split(";")[0] === "multipart/form-data") {
1156
+ return delay(retryTime)
1157
+ .then(function() {
1158
+ return http
1159
+ .postFormData(url, ctx.jar, data.request.formData);
1160
+ })
1161
+ .then(parseAndCheckLogin(ctx, http, retryCount));
1162
+ }
1163
+ else {
1164
+ return delay(retryTime)
1165
+ .then(function() {
1166
+ return http
1167
+ .post(url, ctx.jar, data.request.formData);
1168
+ })
1169
+ .then(parseAndCheckLogin(ctx, http, retryCount));
1170
+ }
1171
+ }
1172
+
1173
+ if (data.statusCode === 404) return;
1174
+
1175
+ if (data.statusCode !== 200)
1176
+ throw new Error("parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.");
1177
+
1178
+ let res = null;
1179
+ try {
1180
+ res = JSON.parse(makeParsable(data.body));
1181
+ } catch (e) {
1182
+ const err = new Error("JSON.parse error. Check the `detail` property on this error.");
1183
+ err.error = "JSON.parse error. Check the `detail` property on this error.";
1184
+ err.detail = e;
1185
+ err.res = data.body;
1186
+ throw err;
1187
+ }
1188
+
1189
+ // In some cases the response contains only a redirect URL which should be followed
1190
+ if (res.redirect && data.request.method === "GET") {
1191
+ return http
1192
+ .get(res.redirect, ctx.jar)
1193
+ .then(parseAndCheckLogin(ctx, http));
1194
+ }
1195
+
1196
+ // TODO: handle multiple cookies?
1197
+ if (res.jsmods && res.jsmods.require && Array.isArray(res.jsmods.require[0]) && res.jsmods.require[0][0] === "Cookie") {
1198
+ res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace("_js_", "");
1199
+ const requireCookie = res.jsmods.require[0][3];
1200
+ ctx.jar.setCookie(formatCookie(requireCookie, "facebook"), "https://www.facebook.com");
1201
+ ctx.jar.setCookie(formatCookie(requireCookie, "messenger"), "https://www.messenger.com");
1202
+ }
1203
+
1204
+ // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1205
+ // one for the next requests.
1206
+ if (res.jsmods && Array.isArray(res.jsmods.require)) {
1207
+ const arr = res.jsmods.require;
1208
+ for (const i in arr) {
1209
+ if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1210
+ ctx.fb_dtsg = arr[i][3][0];
1211
+
1212
+ // Update ttstamp since that depends on fb_dtsg
1213
+ ctx.ttstamp = "2";
1214
+ for (let j = 0; j < ctx.fb_dtsg.length; j++) {
1215
+ ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1216
+ }
1217
+ }
1218
+ }
1219
+ }
1220
+
1221
+ if (res.error === 1357001) {
1222
+ const err = new Error('Facebook blocked the login');
1223
+ err.error = "Not logged in.";
1224
+ throw err;
1225
+ }
1226
+ return res;
1227
+ }
1228
+ return _try(any);
1229
+ };
1230
+ }
1231
+
1232
+ function saveCookies(jar) {
1233
+ return function(res) {
1234
+ const cookies = res.headers["set-cookie"] || [];
1235
+ cookies.forEach(function(c) {
1236
+ if (c.indexOf(".facebook.com") > -1) {
1237
+ jar.setCookie(c, "https://www.facebook.com");
1238
+ }
1239
+ const c2 = c.replace(/domain=\.facebook\.com/, "domain=.messenger.com");
1240
+ jar.setCookie(c2, "https://www.messenger.com");
1241
+ });
1242
+ return res;
1243
+ };
1244
+ }
1245
+
1246
+ const NUM_TO_MONTH = [
1247
+ "Jan",
1248
+ "Feb",
1249
+ "Mar",
1250
+ "Apr",
1251
+ "May",
1252
+ "Jun",
1253
+ "Jul",
1254
+ "Aug",
1255
+ "Sep",
1256
+ "Oct",
1257
+ "Nov",
1258
+ "Dec"
1259
+ ];
1260
+ const NUM_TO_DAY = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
1261
+
1262
+ function formatDate(date) {
1263
+ let d = date.getUTCDate();
1264
+ d = d >= 10 ? d : "0" + d;
1265
+ let h = date.getUTCHours();
1266
+ h = h >= 10 ? h : "0" + h;
1267
+ let m = date.getUTCMinutes();
1268
+ m = m >= 10 ? m : "0" + m;
1269
+ let s = date.getUTCSeconds();
1270
+ s = s >= 10 ? s : "0" + s;
1271
+ return (
1272
+ NUM_TO_DAY[date.getUTCDay()] +
1273
+ ", " +
1274
+ d +
1275
+ " " +
1276
+ NUM_TO_MONTH[date.getUTCMonth()] +
1277
+ " " +
1278
+ date.getUTCFullYear() +
1279
+ " " +
1280
+ h +
1281
+ ":" +
1282
+ m +
1283
+ ":" +
1284
+ s +
1285
+ " GMT"
1286
+ );
1287
+ }
1288
+
1289
+ function formatCookie(arr, url) {
1290
+ return (
1291
+ arr[0] + "=" + arr[1] + "; Path=" + arr[3] + "; Domain=" + url + ".com"
1292
+ );
1293
+ }
1294
+
1295
+ function formatThread(data) {
1296
+ return {
1297
+ threadID: formatID(data.thread_fbid.toString()),
1298
+ participants: data.participants.map(formatID),
1299
+ participantIDs: data.participants.map(formatID),
1300
+ name: data.name,
1301
+ nicknames: data.custom_nickname,
1302
+ snippet: data.snippet,
1303
+ snippetAttachments: data.snippet_attachments,
1304
+ snippetSender: formatID((data.snippet_sender || "").toString()),
1305
+ unreadCount: data.unread_count,
1306
+ messageCount: data.message_count,
1307
+ imageSrc: data.image_src,
1308
+ timestamp: data.timestamp,
1309
+ serverTimestamp: data.server_timestamp, // what is this?
1310
+ muteUntil: data.mute_until,
1311
+ isCanonicalUser: data.is_canonical_user,
1312
+ isCanonical: data.is_canonical,
1313
+ isSubscribed: data.is_subscribed,
1314
+ folder: data.folder,
1315
+ isArchived: data.is_archived,
1316
+ recipientsLoadable: data.recipients_loadable,
1317
+ hasEmailParticipant: data.has_email_participant,
1318
+ readOnly: data.read_only,
1319
+ canReply: data.can_reply,
1320
+ cannotReplyReason: data.cannot_reply_reason,
1321
+ lastMessageTimestamp: data.last_message_timestamp,
1322
+ lastReadTimestamp: data.last_read_timestamp,
1323
+ lastMessageType: data.last_message_type,
1324
+ emoji: data.custom_like_icon,
1325
+ color: data.custom_color,
1326
+ adminIDs: data.admin_ids,
1327
+ threadType: data.thread_type
1328
+ };
1329
+ }
1330
+
1331
+ function getType(obj) {
1332
+ return Object.prototype.toString.call(obj).slice(8, -1);
1333
+ }
1334
+
1335
+ function formatProxyPresence(presence, userID) {
1336
+ if (presence.lat === undefined || presence.p === undefined) return null;
1337
+ return {
1338
+ type: "presence",
1339
+ timestamp: presence.lat * 1000,
1340
+ userID: userID,
1341
+ statuses: presence.p
1342
+ };
1343
+ }
1344
+
1345
+ function formatPresence(presence, userID) {
1346
+ return {
1347
+ type: "presence",
1348
+ timestamp: presence.la * 1000,
1349
+ userID: userID,
1350
+ statuses: presence.a
1351
+ };
1352
+ }
1353
+
1354
+ function decodeClientPayload(payload) {
1355
+ /*
1356
+ Special function which Client using to "encode" clients JSON payload
1357
+ */
1358
+ return JSON.parse(String.fromCharCode.apply(null, payload));
1359
+ }
1360
+
1361
+ function getAppState(jar) {
1362
+ return jar
1363
+ .getCookies("https://www.facebook.com")
1364
+ .concat(jar.getCookies("https://www.messenger.com"));
1365
+ }
1366
+
1367
+ function getAccessFromBusiness(jar, Options) {
1368
+ return function(res) {
1369
+ var html = res ? res.body : null;
1370
+ return get('https://business.facebook.com/content_management', jar, null, Options, null, { noRef: true })
1371
+ .then(function(res) {
1372
+ var token = /"accessToken":"([^.]+)","clientID":/g.exec(res.body)[1];
1373
+ return [html, token];
1374
+ })
1375
+ .catch(function() {
1376
+ return [html, null];
1377
+ });
1378
+ }
1379
+ }
1380
+
1381
+ const meta = prop => new RegExp(`<meta property="${prop}" content="([^"]*)"`);
1382
+ module.exports = {
1383
+ //logs
1384
+ log(...args) {
1385
+ console.log(ws, chalk.green.bold("[LOG]"), ...args);
1386
+ },
1387
+ error(...args) {
1388
+ console.error(ws, chalk.red.bold("[ERROR]"), ...args);
1389
+ },
1390
+ warn(...args) {
1391
+ console.warn(ws, chalk.yellow.bold("[WARNING]"), ...args);
1392
+ },
1393
+ //end logs
1394
+ isReadableStream,
1395
+ cleanGet,
1396
+ get,
1397
+ post,
1398
+ postFormData,
1399
+ generateThreadingID,
1400
+ generateOfflineThreadingID,
1401
+ getGUID,
1402
+ getFrom,
1403
+ makeParsable,
1404
+ arrToForm,
1405
+ getSignatureID,
1406
+ getJar: request.jar,
1407
+ generateTimestampRelative,
1408
+ makeDefaults,
1409
+ parseAndCheckLogin,
1410
+ saveCookies,
1411
+ getType,
1412
+ _formatAttachment,
1413
+ formatHistoryMessage,
1414
+ formatID,
1415
+ formatMessage,
1416
+ formatDeltaEvent,
1417
+ formatDeltaMessage,
1418
+ formatProxyPresence,
1419
+ formatPresence,
1420
+ formatTyp,
1421
+ formatDeltaReadReceipt,
1422
+ formatCookie,
1423
+ formatThread,
1424
+ formatReadReceipt,
1425
+ formatRead,
1426
+ generatePresence,
1427
+ generateAccessiblityCookie,
1428
+ formatDate,
1429
+ decodeClientPayload,
1430
+ getAppState,
1431
+ getAdminTextMessageType,
1432
+ setProxy,
1433
+ getAccessFromBusiness,
1434
+ presenceDecode,
1435
+ presenceEncode,
1436
+ headers,
1437
+ defaultUserAgent,
1438
+ windowsUserAgent,
1439
+ randomUserAgent,
1440
+ meta
1441
+ };