fca-smart-shankar 12.0.0 → 13.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 (285) hide show
  1. package/.replit +12 -3
  2. package/.upm/store.json +1 -0
  3. package/CHANGELOG.md +2 -0
  4. package/README.md +0 -1
  5. package/generated-icon.png +0 -0
  6. package/index.js +507 -437
  7. package/package.json +378 -42
  8. package/replit.nix +3 -1
  9. package/shankar-fca.json +4 -0
  10. package/src/addUserToGroup.js +3 -3
  11. package/src/changeAdminStatus.js +2 -2
  12. package/src/changeArchivedStatus.js +2 -2
  13. package/src/changeAvatar.js +8 -8
  14. package/src/changeAvatarV2.js +1 -1
  15. package/src/changeBio.js +4 -4
  16. package/src/changeBlockedStatus.js +2 -2
  17. package/src/changeCover.js +2 -2
  18. package/src/changeGroupImage.js +4 -4
  19. package/src/changeName.js +2 -2
  20. package/src/changeNickname.js +2 -2
  21. package/src/changeThreadColor.js +3 -3
  22. package/src/changeThreadEmoji.js +2 -2
  23. package/src/changeUsername.js +3 -3
  24. package/src/createCommentPost.js +5 -5
  25. package/src/createNewGroup.js +5 -5
  26. package/src/createPoll.js +2 -2
  27. package/src/createPost.js +6 -5
  28. package/src/deleteMessage.js +2 -2
  29. package/src/deleteThread.js +2 -2
  30. package/src/editMessage.js +30 -18
  31. package/src/follow.js +8 -8
  32. package/src/forwardAttachment.js +2 -2
  33. package/src/getAccess.js +2 -2
  34. package/src/getAvatarUser.js +2 -2
  35. package/src/getCurrentUserID.js +4 -4
  36. package/src/getFriendsList.js +3 -3
  37. package/src/getMessage.js +2 -2
  38. package/src/getThreadHistory.js +2 -2
  39. package/src/getThreadHistoryDeprecated.js +2 -2
  40. package/src/getThreadInfo.js +1 -1
  41. package/src/getThreadInfoDeprecated.js +2 -2
  42. package/src/getThreadList.js +3 -3
  43. package/src/getThreadListDeprecated.js +3 -3
  44. package/src/getThreadPictures.js +2 -2
  45. package/src/getUID.js +4 -4
  46. package/src/getUserID.js +3 -3
  47. package/src/getUserInfo.js +52 -18
  48. package/src/handleFriendRequest.js +5 -5
  49. package/src/handleMessageRequest.js +2 -2
  50. package/src/httpGet.js +3 -3
  51. package/src/httpPost.js +3 -3
  52. package/src/httpPostFormData.js +3 -3
  53. package/src/listenMqtt.js +648 -659
  54. package/src/listenNotification.js +1 -1
  55. package/src/logout.js +3 -3
  56. package/src/markAsDelivered.js +5 -2
  57. package/src/markAsRead.js +5 -2
  58. package/src/markAsReadAll.js +2 -2
  59. package/src/markAsSeen.js +2 -2
  60. package/src/muteThread.js +2 -2
  61. package/src/refreshFb_dtsg.js +3 -3
  62. package/src/removeUserFromGroup.js +2 -2
  63. package/src/resolvePhotoUrl.js +2 -2
  64. package/src/searchStickers.js +1 -1
  65. package/src/sendMessage.js +11 -8
  66. package/src/sendMessageMqtt.js +4 -4
  67. package/src/sendTypingIndicator.js +14 -5
  68. package/src/sendTypingIndicatorV2.js +1 -1
  69. package/src/setMessageReaction.js +3 -3
  70. package/src/setPostReaction.js +4 -4
  71. package/src/setProfileGuard.js +3 -3
  72. package/src/setStoryReaction.js +1 -1
  73. package/src/setTitle.js +3 -3
  74. package/src/shareContact.js +1 -1
  75. package/src/shareLink.js +1 -1
  76. package/src/stopListenMqtt.js +5 -2
  77. package/src/unfriend.js +2 -2
  78. package/src/unsendMessage.js +2 -2
  79. package/src/uploadAttachment.js +2 -2
  80. package/test/data/shareAttach.js +146 -0
  81. package/test/data/something.mov +0 -0
  82. package/test/data/test.png +0 -0
  83. package/test/data/test.txt +7 -0
  84. package/test/test-page.js +1 -1
  85. package/utils.js +564 -391
  86. package/.cache/nix/binary-cache-v6.sqlite +0 -0
  87. package/.cache/nix/binary-cache-v6.sqlite-journal +0 -0
  88. package/.cache/replit/env/latest +0 -125
  89. package/.cache/replit/env/latest.json +0 -1
  90. package/.cache/replit/modules/nix.res +0 -1
  91. package/.cache/replit/modules/nodejs-20.res +0 -1
  92. package/.cache/replit/modules/replit.res +0 -1
  93. package/.cache/replit/modules.stamp +0 -0
  94. package/.cache/replit/nix/env.json +0 -1
  95. package/.cache/typescript/5.6/node_modules/.package-lock.json +0 -171
  96. package/.cache/typescript/5.6/node_modules/@types/bluebird/LICENSE +0 -21
  97. package/.cache/typescript/5.6/node_modules/@types/bluebird/README.md +0 -15
  98. package/.cache/typescript/5.6/node_modules/@types/bluebird/index.d.ts +0 -1365
  99. package/.cache/typescript/5.6/node_modules/@types/bluebird/package.json +0 -25
  100. package/.cache/typescript/5.6/node_modules/@types/caseless/LICENSE +0 -21
  101. package/.cache/typescript/5.6/node_modules/@types/caseless/README.md +0 -48
  102. package/.cache/typescript/5.6/node_modules/@types/caseless/index.d.ts +0 -29
  103. package/.cache/typescript/5.6/node_modules/@types/caseless/package.json +0 -35
  104. package/.cache/typescript/5.6/node_modules/@types/cheerio/LICENSE +0 -21
  105. package/.cache/typescript/5.6/node_modules/@types/cheerio/README.md +0 -15
  106. package/.cache/typescript/5.6/node_modules/@types/cheerio/index.d.ts +0 -318
  107. package/.cache/typescript/5.6/node_modules/@types/cheerio/package.json +0 -71
  108. package/.cache/typescript/5.6/node_modules/@types/mocha/LICENSE +0 -21
  109. package/.cache/typescript/5.6/node_modules/@types/mocha/README.md +0 -15
  110. package/.cache/typescript/5.6/node_modules/@types/mocha/index.d.ts +0 -2929
  111. package/.cache/typescript/5.6/node_modules/@types/mocha/package.json +0 -56
  112. package/.cache/typescript/5.6/node_modules/@types/node/LICENSE +0 -21
  113. package/.cache/typescript/5.6/node_modules/@types/node/README.md +0 -15
  114. package/.cache/typescript/5.6/node_modules/@types/node/assert/strict.d.ts +0 -8
  115. package/.cache/typescript/5.6/node_modules/@types/node/assert.d.ts +0 -1040
  116. package/.cache/typescript/5.6/node_modules/@types/node/async_hooks.d.ts +0 -541
  117. package/.cache/typescript/5.6/node_modules/@types/node/buffer.buffer.d.ts +0 -385
  118. package/.cache/typescript/5.6/node_modules/@types/node/buffer.d.ts +0 -1933
  119. package/.cache/typescript/5.6/node_modules/@types/node/child_process.d.ts +0 -1549
  120. package/.cache/typescript/5.6/node_modules/@types/node/cluster.d.ts +0 -579
  121. package/.cache/typescript/5.6/node_modules/@types/node/compatibility/disposable.d.ts +0 -16
  122. package/.cache/typescript/5.6/node_modules/@types/node/compatibility/index.d.ts +0 -9
  123. package/.cache/typescript/5.6/node_modules/@types/node/compatibility/indexable.d.ts +0 -23
  124. package/.cache/typescript/5.6/node_modules/@types/node/compatibility/iterators.d.ts +0 -21
  125. package/.cache/typescript/5.6/node_modules/@types/node/console.d.ts +0 -452
  126. package/.cache/typescript/5.6/node_modules/@types/node/constants.d.ts +0 -19
  127. package/.cache/typescript/5.6/node_modules/@types/node/crypto.d.ts +0 -4475
  128. package/.cache/typescript/5.6/node_modules/@types/node/dgram.d.ts +0 -596
  129. package/.cache/typescript/5.6/node_modules/@types/node/diagnostics_channel.d.ts +0 -554
  130. package/.cache/typescript/5.6/node_modules/@types/node/dns/promises.d.ts +0 -476
  131. package/.cache/typescript/5.6/node_modules/@types/node/dns.d.ts +0 -865
  132. package/.cache/typescript/5.6/node_modules/@types/node/dom-events.d.ts +0 -124
  133. package/.cache/typescript/5.6/node_modules/@types/node/domain.d.ts +0 -170
  134. package/.cache/typescript/5.6/node_modules/@types/node/events.d.ts +0 -931
  135. package/.cache/typescript/5.6/node_modules/@types/node/fs/promises.d.ts +0 -1275
  136. package/.cache/typescript/5.6/node_modules/@types/node/fs.d.ts +0 -4396
  137. package/.cache/typescript/5.6/node_modules/@types/node/globals.d.ts +0 -566
  138. package/.cache/typescript/5.6/node_modules/@types/node/globals.typedarray.d.ts +0 -21
  139. package/.cache/typescript/5.6/node_modules/@types/node/http.d.ts +0 -1958
  140. package/.cache/typescript/5.6/node_modules/@types/node/http2.d.ts +0 -2558
  141. package/.cache/typescript/5.6/node_modules/@types/node/https.d.ts +0 -543
  142. package/.cache/typescript/5.6/node_modules/@types/node/index.d.ts +0 -92
  143. package/.cache/typescript/5.6/node_modules/@types/node/inspector.d.ts +0 -3966
  144. package/.cache/typescript/5.6/node_modules/@types/node/module.d.ts +0 -402
  145. package/.cache/typescript/5.6/node_modules/@types/node/net.d.ts +0 -1001
  146. package/.cache/typescript/5.6/node_modules/@types/node/os.d.ts +0 -495
  147. package/.cache/typescript/5.6/node_modules/@types/node/package.json +0 -220
  148. package/.cache/typescript/5.6/node_modules/@types/node/path.d.ts +0 -200
  149. package/.cache/typescript/5.6/node_modules/@types/node/perf_hooks.d.ts +0 -965
  150. package/.cache/typescript/5.6/node_modules/@types/node/process.d.ts +0 -1963
  151. package/.cache/typescript/5.6/node_modules/@types/node/punycode.d.ts +0 -117
  152. package/.cache/typescript/5.6/node_modules/@types/node/querystring.d.ts +0 -153
  153. package/.cache/typescript/5.6/node_modules/@types/node/readline/promises.d.ts +0 -162
  154. package/.cache/typescript/5.6/node_modules/@types/node/readline.d.ts +0 -589
  155. package/.cache/typescript/5.6/node_modules/@types/node/repl.d.ts +0 -430
  156. package/.cache/typescript/5.6/node_modules/@types/node/sea.d.ts +0 -153
  157. package/.cache/typescript/5.6/node_modules/@types/node/sqlite.d.ts +0 -213
  158. package/.cache/typescript/5.6/node_modules/@types/node/stream/consumers.d.ts +0 -12
  159. package/.cache/typescript/5.6/node_modules/@types/node/stream/promises.d.ts +0 -90
  160. package/.cache/typescript/5.6/node_modules/@types/node/stream/web.d.ts +0 -609
  161. package/.cache/typescript/5.6/node_modules/@types/node/stream.d.ts +0 -1726
  162. package/.cache/typescript/5.6/node_modules/@types/node/string_decoder.d.ts +0 -67
  163. package/.cache/typescript/5.6/node_modules/@types/node/test.d.ts +0 -2248
  164. package/.cache/typescript/5.6/node_modules/@types/node/timers/promises.d.ts +0 -97
  165. package/.cache/typescript/5.6/node_modules/@types/node/timers.d.ts +0 -240
  166. package/.cache/typescript/5.6/node_modules/@types/node/tls.d.ts +0 -1226
  167. package/.cache/typescript/5.6/node_modules/@types/node/trace_events.d.ts +0 -197
  168. package/.cache/typescript/5.6/node_modules/@types/node/ts5.6/buffer.buffer.d.ts +0 -385
  169. package/.cache/typescript/5.6/node_modules/@types/node/ts5.6/globals.typedarray.d.ts +0 -19
  170. package/.cache/typescript/5.6/node_modules/@types/node/ts5.6/index.d.ts +0 -92
  171. package/.cache/typescript/5.6/node_modules/@types/node/tty.d.ts +0 -208
  172. package/.cache/typescript/5.6/node_modules/@types/node/url.d.ts +0 -972
  173. package/.cache/typescript/5.6/node_modules/@types/node/util.d.ts +0 -2371
  174. package/.cache/typescript/5.6/node_modules/@types/node/v8.d.ts +0 -808
  175. package/.cache/typescript/5.6/node_modules/@types/node/vm.d.ts +0 -976
  176. package/.cache/typescript/5.6/node_modules/@types/node/wasi.d.ts +0 -181
  177. package/.cache/typescript/5.6/node_modules/@types/node/worker_threads.d.ts +0 -745
  178. package/.cache/typescript/5.6/node_modules/@types/node/zlib.d.ts +0 -539
  179. package/.cache/typescript/5.6/node_modules/@types/request/LICENSE +0 -21
  180. package/.cache/typescript/5.6/node_modules/@types/request/README.md +0 -15
  181. package/.cache/typescript/5.6/node_modules/@types/request/index.d.ts +0 -395
  182. package/.cache/typescript/5.6/node_modules/@types/request/package.json +0 -70
  183. package/.cache/typescript/5.6/node_modules/@types/tough-cookie/LICENSE +0 -21
  184. package/.cache/typescript/5.6/node_modules/@types/tough-cookie/README.md +0 -15
  185. package/.cache/typescript/5.6/node_modules/@types/tough-cookie/index.d.ts +0 -321
  186. package/.cache/typescript/5.6/node_modules/@types/tough-cookie/package.json +0 -35
  187. package/.cache/typescript/5.6/node_modules/asynckit/LICENSE +0 -21
  188. package/.cache/typescript/5.6/node_modules/asynckit/README.md +0 -233
  189. package/.cache/typescript/5.6/node_modules/asynckit/bench.js +0 -76
  190. package/.cache/typescript/5.6/node_modules/asynckit/index.js +0 -6
  191. package/.cache/typescript/5.6/node_modules/asynckit/lib/abort.js +0 -29
  192. package/.cache/typescript/5.6/node_modules/asynckit/lib/async.js +0 -34
  193. package/.cache/typescript/5.6/node_modules/asynckit/lib/defer.js +0 -26
  194. package/.cache/typescript/5.6/node_modules/asynckit/lib/iterate.js +0 -75
  195. package/.cache/typescript/5.6/node_modules/asynckit/lib/readable_asynckit.js +0 -91
  196. package/.cache/typescript/5.6/node_modules/asynckit/lib/readable_parallel.js +0 -25
  197. package/.cache/typescript/5.6/node_modules/asynckit/lib/readable_serial.js +0 -25
  198. package/.cache/typescript/5.6/node_modules/asynckit/lib/readable_serial_ordered.js +0 -29
  199. package/.cache/typescript/5.6/node_modules/asynckit/lib/state.js +0 -37
  200. package/.cache/typescript/5.6/node_modules/asynckit/lib/streamify.js +0 -141
  201. package/.cache/typescript/5.6/node_modules/asynckit/lib/terminator.js +0 -29
  202. package/.cache/typescript/5.6/node_modules/asynckit/package.json +0 -63
  203. package/.cache/typescript/5.6/node_modules/asynckit/parallel.js +0 -43
  204. package/.cache/typescript/5.6/node_modules/asynckit/serial.js +0 -17
  205. package/.cache/typescript/5.6/node_modules/asynckit/serialOrdered.js +0 -75
  206. package/.cache/typescript/5.6/node_modules/asynckit/stream.js +0 -21
  207. package/.cache/typescript/5.6/node_modules/combined-stream/License +0 -19
  208. package/.cache/typescript/5.6/node_modules/combined-stream/Readme.md +0 -138
  209. package/.cache/typescript/5.6/node_modules/combined-stream/lib/combined_stream.js +0 -208
  210. package/.cache/typescript/5.6/node_modules/combined-stream/package.json +0 -25
  211. package/.cache/typescript/5.6/node_modules/combined-stream/yarn.lock +0 -17
  212. package/.cache/typescript/5.6/node_modules/delayed-stream/License +0 -19
  213. package/.cache/typescript/5.6/node_modules/delayed-stream/Makefile +0 -7
  214. package/.cache/typescript/5.6/node_modules/delayed-stream/Readme.md +0 -141
  215. package/.cache/typescript/5.6/node_modules/delayed-stream/lib/delayed_stream.js +0 -107
  216. package/.cache/typescript/5.6/node_modules/delayed-stream/package.json +0 -27
  217. package/.cache/typescript/5.6/node_modules/form-data/License +0 -19
  218. package/.cache/typescript/5.6/node_modules/form-data/Readme.md +0 -350
  219. package/.cache/typescript/5.6/node_modules/form-data/index.d.ts +0 -51
  220. package/.cache/typescript/5.6/node_modules/form-data/lib/browser.js +0 -2
  221. package/.cache/typescript/5.6/node_modules/form-data/lib/form_data.js +0 -484
  222. package/.cache/typescript/5.6/node_modules/form-data/lib/populate.js +0 -10
  223. package/.cache/typescript/5.6/node_modules/form-data/package.json +0 -72
  224. package/.cache/typescript/5.6/node_modules/mime-db/HISTORY.md +0 -507
  225. package/.cache/typescript/5.6/node_modules/mime-db/LICENSE +0 -23
  226. package/.cache/typescript/5.6/node_modules/mime-db/README.md +0 -100
  227. package/.cache/typescript/5.6/node_modules/mime-db/db.json +0 -8519
  228. package/.cache/typescript/5.6/node_modules/mime-db/index.js +0 -12
  229. package/.cache/typescript/5.6/node_modules/mime-db/package.json +0 -60
  230. package/.cache/typescript/5.6/node_modules/mime-types/HISTORY.md +0 -397
  231. package/.cache/typescript/5.6/node_modules/mime-types/LICENSE +0 -23
  232. package/.cache/typescript/5.6/node_modules/mime-types/README.md +0 -113
  233. package/.cache/typescript/5.6/node_modules/mime-types/index.js +0 -188
  234. package/.cache/typescript/5.6/node_modules/mime-types/package.json +0 -44
  235. package/.cache/typescript/5.6/node_modules/safe-buffer/LICENSE +0 -21
  236. package/.cache/typescript/5.6/node_modules/safe-buffer/README.md +0 -584
  237. package/.cache/typescript/5.6/node_modules/safe-buffer/index.d.ts +0 -187
  238. package/.cache/typescript/5.6/node_modules/safe-buffer/index.js +0 -65
  239. package/.cache/typescript/5.6/node_modules/safe-buffer/package.json +0 -51
  240. package/.cache/typescript/5.6/node_modules/types-registry/README.md +0 -2
  241. package/.cache/typescript/5.6/node_modules/types-registry/index.json +0 -1
  242. package/.cache/typescript/5.6/node_modules/types-registry/package.json +0 -20
  243. package/.cache/typescript/5.6/node_modules/undici-types/LICENSE +0 -21
  244. package/.cache/typescript/5.6/node_modules/undici-types/README.md +0 -6
  245. package/.cache/typescript/5.6/node_modules/undici-types/agent.d.ts +0 -31
  246. package/.cache/typescript/5.6/node_modules/undici-types/api.d.ts +0 -43
  247. package/.cache/typescript/5.6/node_modules/undici-types/balanced-pool.d.ts +0 -29
  248. package/.cache/typescript/5.6/node_modules/undici-types/cache.d.ts +0 -36
  249. package/.cache/typescript/5.6/node_modules/undici-types/client.d.ts +0 -108
  250. package/.cache/typescript/5.6/node_modules/undici-types/connector.d.ts +0 -34
  251. package/.cache/typescript/5.6/node_modules/undici-types/content-type.d.ts +0 -21
  252. package/.cache/typescript/5.6/node_modules/undici-types/cookies.d.ts +0 -28
  253. package/.cache/typescript/5.6/node_modules/undici-types/diagnostics-channel.d.ts +0 -66
  254. package/.cache/typescript/5.6/node_modules/undici-types/dispatcher.d.ts +0 -255
  255. package/.cache/typescript/5.6/node_modules/undici-types/env-http-proxy-agent.d.ts +0 -21
  256. package/.cache/typescript/5.6/node_modules/undici-types/errors.d.ts +0 -149
  257. package/.cache/typescript/5.6/node_modules/undici-types/eventsource.d.ts +0 -61
  258. package/.cache/typescript/5.6/node_modules/undici-types/fetch.d.ts +0 -209
  259. package/.cache/typescript/5.6/node_modules/undici-types/file.d.ts +0 -39
  260. package/.cache/typescript/5.6/node_modules/undici-types/filereader.d.ts +0 -54
  261. package/.cache/typescript/5.6/node_modules/undici-types/formdata.d.ts +0 -108
  262. package/.cache/typescript/5.6/node_modules/undici-types/global-dispatcher.d.ts +0 -9
  263. package/.cache/typescript/5.6/node_modules/undici-types/global-origin.d.ts +0 -7
  264. package/.cache/typescript/5.6/node_modules/undici-types/handlers.d.ts +0 -15
  265. package/.cache/typescript/5.6/node_modules/undici-types/header.d.ts +0 -4
  266. package/.cache/typescript/5.6/node_modules/undici-types/index.d.ts +0 -71
  267. package/.cache/typescript/5.6/node_modules/undici-types/interceptors.d.ts +0 -17
  268. package/.cache/typescript/5.6/node_modules/undici-types/mock-agent.d.ts +0 -50
  269. package/.cache/typescript/5.6/node_modules/undici-types/mock-client.d.ts +0 -25
  270. package/.cache/typescript/5.6/node_modules/undici-types/mock-errors.d.ts +0 -12
  271. package/.cache/typescript/5.6/node_modules/undici-types/mock-interceptor.d.ts +0 -93
  272. package/.cache/typescript/5.6/node_modules/undici-types/mock-pool.d.ts +0 -25
  273. package/.cache/typescript/5.6/node_modules/undici-types/package.json +0 -55
  274. package/.cache/typescript/5.6/node_modules/undici-types/patch.d.ts +0 -33
  275. package/.cache/typescript/5.6/node_modules/undici-types/pool-stats.d.ts +0 -19
  276. package/.cache/typescript/5.6/node_modules/undici-types/pool.d.ts +0 -39
  277. package/.cache/typescript/5.6/node_modules/undici-types/proxy-agent.d.ts +0 -28
  278. package/.cache/typescript/5.6/node_modules/undici-types/readable.d.ts +0 -60
  279. package/.cache/typescript/5.6/node_modules/undici-types/retry-agent.d.ts +0 -8
  280. package/.cache/typescript/5.6/node_modules/undici-types/retry-handler.d.ts +0 -116
  281. package/.cache/typescript/5.6/node_modules/undici-types/util.d.ts +0 -18
  282. package/.cache/typescript/5.6/node_modules/undici-types/webidl.d.ts +0 -222
  283. package/.cache/typescript/5.6/node_modules/undici-types/websocket.d.ts +0 -150
  284. package/.cache/typescript/5.6/package-lock.json +0 -183
  285. package/.cache/typescript/5.6/package.json +0 -1
package/utils.js CHANGED
@@ -1,112 +1,185 @@
1
-
2
- /* eslint-disable no-prototype-builtins */
1
+ /* eslint-disable no-prototype-builtins */
3
2
  "use strict";
4
3
 
5
- let request = require("request").defaults({ jar: true });
4
+ let request = promisifyPromise(require("request").defaults({ jar: true, proxy: process.env.FB_PROXY }));
6
5
  const stream = require("stream");
6
+ const log = require("npmlog");
7
7
  const querystring = require("querystring");
8
8
  const url = require("url");
9
9
 
10
- function setProxy(proxy) {
11
- if (typeof proxy == 'string')
12
- request = require("request").defaults({ jar: !0, proxy });
13
- else
14
- request = require('request').defaults({ jar: !0 });
15
- return;
10
+ class CustomError extends Error {
11
+ constructor(obj) {
12
+ if (typeof obj === 'string')
13
+ obj = { message: obj };
14
+ if (typeof obj !== 'object' || obj === null)
15
+ throw new TypeError('Object required');
16
+ obj.message ? super(obj.message) : super();
17
+ Object.assign(this, obj);
18
+ }
19
+ }
20
+
21
+ function callbackToPromise(func) {
22
+ return function (...args) {
23
+ return new Promise((resolve, reject) => {
24
+ func(...args, (err, data) => {
25
+ if (err)
26
+ reject(err);
27
+ else
28
+ resolve(data);
29
+ });
30
+ });
31
+ };
32
+ }
33
+
34
+ function isHasCallback(func) {
35
+ if (typeof func !== "function")
36
+ return false;
37
+ return func.toString().split("\n")[0].match(/(callback|cb)\s*\)/) !== null;
38
+ }
39
+
40
+ // replace for bluebird.promisify (but this only applies best to the `request` package)
41
+ function promisifyPromise(promise) {
42
+ const keys = Object.keys(promise);
43
+ let promise_;
44
+ if (
45
+ typeof promise === "function"
46
+ && isHasCallback(promise)
47
+ )
48
+ promise_ = callbackToPromise(promise);
49
+ else
50
+ promise_ = promise;
51
+
52
+ for (const key of keys) {
53
+ if (!promise[key]?.toString)
54
+ continue;
55
+
56
+ if (
57
+ typeof promise[key] === "function"
58
+ && isHasCallback(promise[key])
59
+ ) {
60
+ promise_[key] = callbackToPromise(promise[key]);
61
+ }
62
+ else {
63
+ promise_[key] = promise[key];
64
+ }
65
+ }
66
+
67
+ return promise_;
68
+ }
69
+
70
+ // replace for bluebird.delay
71
+ function delay(ms) {
72
+ return new Promise(resolve => setTimeout(resolve, ms));
73
+ }
74
+
75
+ // replace for bluebird.try
76
+ function tryPromise(tryFunc) {
77
+ return new Promise((resolve, reject) => {
78
+ try {
79
+ resolve(tryFunc());
80
+ } catch (error) {
81
+ reject(error);
82
+ }
83
+ });
84
+ }
85
+
86
+ function setProxy(url) {
87
+ if (typeof url == "undefined")
88
+ return request = promisifyPromise(require("request").defaults({
89
+ jar: true
90
+ }));
91
+ return request = promisifyPromise(require("request").defaults({
92
+ jar: true,
93
+ proxy: url
94
+ }));
16
95
  }
17
96
 
18
97
  function getHeaders(url, options, ctx, customHeader) {
19
- var headers = {
98
+ const headers = {
20
99
  "Content-Type": "application/x-www-form-urlencoded",
21
100
  Referer: "https://www.facebook.com/",
22
- Host: new URL(url).hostname,
101
+ Host: url.replace("https://", "").split("/")[0],
23
102
  Origin: "https://www.facebook.com",
24
103
  "User-Agent": options.userAgent,
25
104
  Connection: "keep-alive",
26
- "Sec-Fetch-Site": "same-origin",
27
- 'Sec-Fetch-User': '?1'
105
+ "sec-fetch-site": "same-origin"
28
106
  };
29
107
  if (customHeader) {
30
108
  Object.assign(headers, customHeader);
31
- if (customHeader.noRef)
32
- delete headers.Referer;
33
109
  }
34
- if (ctx && ctx.region)
35
- headers["X-MSGR-Region"] = ctx.region;
110
+ if (ctx && ctx.region) {
111
+ headers["X-MSGR-Region"] = ctx.region;
112
+ }
36
113
 
37
114
  return headers;
38
115
  }
39
116
 
40
117
  function isReadableStream(obj) {
41
- return obj instanceof stream.Stream && typeof obj._read == "function" && getType(obj._readableState) == "Object";
118
+ return (
119
+ obj instanceof stream.Stream &&
120
+ (getType(obj._read) === "Function" ||
121
+ getType(obj._read) === "AsyncFunction") &&
122
+ getType(obj._readableState) === "Object"
123
+ );
42
124
  }
43
125
 
44
- function get(url, jar, qs, options, ctx, customHeader) {
45
- let callback;
46
- var returnPromise = new Promise(function (resolve, reject) {
47
- callback = (error, res) => error ? reject(error) : resolve(res);
48
- });
49
- if (getType(qs) == "Object")
50
- for (let prop in qs) {
51
- if (getType(qs[prop]) == 'Object')
52
- qs[prop] = JSON.stringify(qs[prop]);
53
- }
54
- var op = {
55
- headers: getHeaders(url, options, ctx, customHeader),
56
- timeout: 60000,
57
- qs,
58
- jar,
59
- gzip: !0
126
+ function get(url, jar, qs, options, ctx) {
127
+ // I'm still confused about this
128
+ if (getType(qs) === "Object") {
129
+ for (const prop in qs) {
130
+ if (qs.hasOwnProperty(prop) && getType(qs[prop]) === "Object") {
131
+ qs[prop] = JSON.stringify(qs[prop]);
132
+ }
133
+ }
60
134
  }
135
+ const op = {
136
+ headers: getHeaders(url, options, ctx),
137
+ timeout: 60000,
138
+ qs: qs,
139
+ url: url,
140
+ method: "GET",
141
+ jar: jar,
142
+ gzip: true
143
+ };
61
144
 
62
- request.get(url, op, callback);
63
-
64
- return returnPromise;
145
+ return request(op).then(function (res) {
146
+ return Array.isArray(res) ? res[0] : res;
147
+ });
65
148
  }
66
149
 
67
150
  function post(url, jar, form, options, ctx, customHeader) {
68
- let callback;
69
- var returnPromise = new Promise(function (resolve, reject) {
70
- callback = (error, res) => error ? reject(error) : resolve(res);
71
- });
72
-
73
- var op = {
74
- headers: getHeaders(url, options, ctx, customHeader),
75
- timeout: 60000,
76
- form,
77
- jar,
78
- gzip: !0
79
- }
80
-
81
- request.post(url, op, callback);
151
+ const op = {
152
+ headers: getHeaders(url, options, ctx, customHeader),
153
+ timeout: 60000,
154
+ url: url,
155
+ method: "POST",
156
+ form: form,
157
+ jar: jar,
158
+ gzip: true
159
+ };
82
160
 
83
- return returnPromise;
161
+ return request(op).then(function (res) {
162
+ return Array.isArray(res) ? res[0] : res;
163
+ });
84
164
  }
85
165
 
86
166
  function postFormData(url, jar, form, qs, options, ctx) {
87
- let callback;
88
- var returnPromise = new Promise(function (resolve, reject) {
89
- callback = (error, res) => error ? reject(error) : resolve(res);
90
- });
91
- if (getType(qs) == "Object")
92
- for (let prop in qs) {
93
- if (getType(qs[prop]) == 'Object')
94
- qs[prop] = JSON.stringify(qs[prop]);
95
- }
96
- var op = {
97
- headers: getHeaders(url, options, ctx, {
98
- 'Content-Type': 'multipart/form-data'
99
- }),
167
+ const headers = getHeaders(url, options, ctx);
168
+ headers["Content-Type"] = "multipart/form-data";
169
+ const op = {
170
+ headers: headers,
100
171
  timeout: 60000,
172
+ url: url,
173
+ method: "POST",
101
174
  formData: form,
102
- qs,
103
- jar,
104
- gzip: !0
105
- }
106
-
107
- request.post(url, op, callback);
175
+ qs: qs,
176
+ jar: jar,
177
+ gzip: true
178
+ };
108
179
 
109
- return returnPromise;
180
+ return request(op).then(function (res) {
181
+ return Array.isArray(res) ? res[0] : res;
182
+ });
110
183
  }
111
184
 
112
185
  function padZeros(val, len) {
@@ -709,57 +782,61 @@ function _formatAttachment(attachment1, attachment2) {
709
782
  }
710
783
 
711
784
  function formatAttachment(attachments, attachmentIds, attachmentMap, shareMap) {
712
- attachmentMap = shareMap || attachmentMap;
713
- return attachments
714
- ? attachments.map(function (val, i) {
715
- if (
716
- !attachmentMap ||
717
- !attachmentIds ||
718
- !attachmentMap[attachmentIds[i]]
719
- ) {
720
- return _formatAttachment(val);
721
- }
722
- return _formatAttachment(val, attachmentMap[attachmentIds[i]]);
723
- })
724
- : [];
785
+ attachmentMap = shareMap || attachmentMap;
786
+ return attachments
787
+ ? attachments.map(function (val, i) {
788
+ if (
789
+ !attachmentMap ||
790
+ !attachmentIds ||
791
+ !attachmentMap[attachmentIds[i]]
792
+ ) {
793
+ return _formatAttachment(val);
794
+ }
795
+ return _formatAttachment(val, attachmentMap[attachmentIds[i]]);
796
+ })
797
+ : [];
725
798
  }
726
799
 
727
800
  function formatDeltaMessage(m) {
728
- const md = m.delta.messageMetadata;
729
-
730
- const mdata =
731
- m.delta.data === undefined
732
- ? []
733
- : m.delta.data.prng === undefined
734
- ? []
735
- : JSON.parse(m.delta.data.prng);
736
- const m_id = mdata.map(u => u.i);
737
- const m_offset = mdata.map(u => u.o);
738
- const m_length = mdata.map(u => u.l);
739
- const mentions = {};
740
- for (let i = 0; i < m_id.length; i++) {
741
- mentions[m_id[i]] = m.delta.body.substring(
742
- m_offset[i],
743
- m_offset[i] + m_length[i]
744
- );
745
- }
801
+ var md = m.delta.messageMetadata;
802
+
803
+ var mdata =
804
+ m.delta.data === undefined
805
+ ? []
806
+ : m.delta.data.prng === undefined
807
+ ? []
808
+ : JSON.parse(m.delta.data.prng);
809
+ var m_id = mdata.map(u => u.i);
810
+ var m_offset = mdata.map(u => u.o);
811
+ var m_length = mdata.map(u => u.l);
812
+ var mentions = {};
813
+ var body = m.delta.body || "";
814
+ var args = body == "" ? [] : body.trim().split(/\s+/);
815
+ for (var i = 0; i < m_id.length; i++) {
816
+ mentions[m_id[i]] = m.delta.body.substring(
817
+ m_offset[i],
818
+ m_offset[i] + m_length[i]
819
+ );
820
+ }
746
821
 
747
- return {
748
- type: "message",
749
- senderID: formatID(md.actorFbId.toString()),
750
- body: m.delta.body || "",
751
- threadID: formatID(
752
- (md.threadKey.threadFbId || md.threadKey.otherUserFbId).toString()
753
- ),
754
- messageID: md.messageId,
755
- attachments: (m.delta.attachments || []).map(v => _formatAttachment(v)),
756
- mentions: mentions,
757
- timestamp: md.timestamp,
758
- isGroup: !!md.threadKey.threadFbId,
759
- participantIDs: m.delta.participants
760
- };
822
+ return {
823
+ type: "message",
824
+ senderID: formatID(md.actorFbId.toString()),
825
+ threadID: formatID(
826
+ (md.threadKey.threadFbId || md.threadKey.otherUserFbId).toString()
827
+ ),
828
+ args: args,
829
+ body: body,
830
+ messageID: md.messageId,
831
+ attachments: (m.delta.attachments || []).map(v => _formatAttachment(v)),
832
+ mentions: mentions,
833
+ timestamp: md.timestamp,
834
+ isGroup: !!md.threadKey.threadFbId,
835
+ participantIDs: m.delta.participants || []
836
+ };
761
837
  }
762
838
 
839
+
763
840
  function formatID(id) {
764
841
  if (id != undefined && id != null) {
765
842
  return id.replace(/(fb)?id[:.]/, "");
@@ -770,74 +847,73 @@ function formatID(id) {
770
847
  }
771
848
 
772
849
  function formatMessage(m) {
773
- const originalMessage = m.message ? m.message : m;
774
- const obj = {
775
- type: "message",
776
- senderName: originalMessage.sender_name,
777
- senderID: formatID(originalMessage.sender_fbid.toString()),
778
- participantNames: originalMessage.group_thread_info
779
- ? originalMessage.group_thread_info.participant_names
780
- : [originalMessage.sender_name.split(" ")[0]],
781
- participantIDs: originalMessage.group_thread_info
782
- ? originalMessage.group_thread_info.participant_ids.map(function (v) {
783
- return formatID(v.toString());
784
- })
785
- : [formatID(originalMessage.sender_fbid)],
786
- body: originalMessage.body || "",
787
- threadID: formatID(
788
- (
789
- originalMessage.thread_fbid || originalMessage.other_user_fbid
790
- ).toString()
791
- ),
792
- threadName: originalMessage.group_thread_info
793
- ? originalMessage.group_thread_info.name
794
- : originalMessage.sender_name,
795
- location: originalMessage.coordinates ? originalMessage.coordinates : null,
796
- messageID: originalMessage.mid
797
- ? originalMessage.mid.toString()
798
- : originalMessage.message_id,
799
- attachments: formatAttachment(
800
- originalMessage.attachments,
801
- originalMessage.attachmentIds,
802
- originalMessage.attachment_map,
803
- originalMessage.share_map
804
- ),
805
- timestamp: originalMessage.timestamp,
806
- timestampAbsolute: originalMessage.timestamp_absolute,
807
- timestampRelative: originalMessage.timestamp_relative,
808
- timestampDatetime: originalMessage.timestamp_datetime,
809
- tags: originalMessage.tags,
810
- reactions: originalMessage.reactions ? originalMessage.reactions : [],
811
- isUnread: originalMessage.is_unread
812
- };
813
-
814
- if (m.type === "pages_messaging")
815
- obj.pageID = m.realtime_viewer_fbid.toString();
816
- obj.isGroup = obj.participantIDs.length > 2;
817
-
818
- return obj;
850
+ var originalMessage = m.message ? m.message : m;
851
+ var obj = {
852
+ type: "message",
853
+ senderName: originalMessage.sender_name,
854
+ senderID: formatID(originalMessage.sender_fbid.toString()),
855
+ participantNames: originalMessage.group_thread_info
856
+ ? originalMessage.group_thread_info.participant_names
857
+ : [originalMessage.sender_name.split(" ")[0]],
858
+ participantIDs: originalMessage.group_thread_info
859
+ ? originalMessage.group_thread_info.participant_ids.map(function (v) {
860
+ return formatID(v.toString());
861
+ })
862
+ : [formatID(originalMessage.sender_fbid)],
863
+ body: originalMessage.body || "",
864
+ threadID: formatID(
865
+ (
866
+ originalMessage.thread_fbid || originalMessage.other_user_fbid
867
+ ).toString()
868
+ ),
869
+ threadName: originalMessage.group_thread_info
870
+ ? originalMessage.group_thread_info.name
871
+ : originalMessage.sender_name,
872
+ location: originalMessage.coordinates ? originalMessage.coordinates : null,
873
+ messageID: originalMessage.mid
874
+ ? originalMessage.mid.toString()
875
+ : originalMessage.message_id,
876
+ attachments: formatAttachment(
877
+ originalMessage.attachments,
878
+ originalMessage.attachmentIds,
879
+ originalMessage.attachment_map,
880
+ originalMessage.share_map
881
+ ),
882
+ timestamp: originalMessage.timestamp,
883
+ timestampAbsolute: originalMessage.timestamp_absolute,
884
+ timestampRelative: originalMessage.timestamp_relative,
885
+ timestampDatetime: originalMessage.timestamp_datetime,
886
+ tags: originalMessage.tags,
887
+ reactions: originalMessage.reactions ? originalMessage.reactions : [],
888
+ isUnread: originalMessage.is_unread
889
+ };
890
+
891
+ if (m.type === "pages_messaging")
892
+ obj.pageID = m.realtime_viewer_fbid.toString();
893
+ obj.isGroup = obj.participantIDs.length > 2;
894
+
895
+ return obj;
819
896
  }
820
897
 
821
898
  function formatEvent(m) {
822
- const originalMessage = m.message ? m.message : m;
823
- let logMessageType = originalMessage.log_message_type;
824
- let logMessageData;
825
- if (logMessageType === "log:generic-admin-text") {
826
- logMessageData = originalMessage.log_message_data.untypedData;
827
- logMessageType = getAdminTextMessageType(
828
- originalMessage.log_message_data.message_type
829
- );
830
- }
831
- else {
832
- logMessageData = originalMessage.log_message_data;
833
- }
899
+ var originalMessage = m.message ? m.message : m;
900
+ var logMessageType = originalMessage.log_message_type;
901
+ var logMessageData;
902
+ if (logMessageType === "log:generic-admin-text") {
903
+ logMessageData = originalMessage.log_message_data.untypedData;
904
+ logMessageType = getAdminTextMessageType(
905
+ originalMessage.log_message_data.message_type
906
+ );
907
+ } else {
908
+ logMessageData = originalMessage.log_message_data;
909
+ }
834
910
 
835
- return Object.assign(formatMessage(originalMessage), {
836
- type: "event",
837
- logMessageType: logMessageType,
838
- logMessageData: logMessageData,
839
- logMessageBody: originalMessage.log_message_body
840
- });
911
+ return Object.assign(formatMessage(originalMessage), {
912
+ type: "event",
913
+ logMessageType: logMessageType,
914
+ logMessageData: logMessageData,
915
+ logMessageBody: originalMessage.log_message_body
916
+ });
841
917
  }
842
918
 
843
919
  function formatHistoryMessage(m) {
@@ -851,87 +927,71 @@ function formatHistoryMessage(m) {
851
927
 
852
928
  // Get a more readable message type for AdminTextMessages
853
929
  function getAdminTextMessageType(type) {
854
- switch (type) {
855
- case 'unpin_messages_v2':
856
- return 'log:unpin-message';
857
- case 'pin_messages_v2':
858
- return 'log:pin-message';
859
- case "change_thread_theme":
860
- return "log:thread-color";
861
- case "change_thread_icon":
862
- case 'change_thread_quick_reaction':
863
- return "log:thread-icon";
864
- case "change_thread_nickname":
865
- return "log:user-nickname";
866
- case "change_thread_admins":
867
- return "log:thread-admins";
868
- case "group_poll":
869
- return "log:thread-poll";
870
- case "change_thread_approval_mode":
871
- return "log:thread-approval-mode";
872
- case "messenger_call_log":
873
- case "participant_joined_group_call":
874
- return "log:thread-call";
875
- default:
876
- return type;
877
- }
930
+ switch (type) {
931
+ case "change_thread_theme":
932
+ return "log:thread-color";
933
+ case "change_thread_quick_reaction":
934
+ return "log:thread-icon";
935
+ case "change_thread_nickname":
936
+ return "log:user-nickname";
937
+ case "change_thread_admins":
938
+ return "log:thread-admins";
939
+ case "group_poll":
940
+ return "log:thread-poll";
941
+ case "change_thread_approval_mode":
942
+ return "log:thread-approval-mode";
943
+ case "messenger_call_log":
944
+ case "participant_joined_group_call":
945
+ return "log:thread-call";
946
+ default:
947
+ return type;
948
+ }
878
949
  }
879
950
 
880
951
  function formatDeltaEvent(m) {
881
- let logMessageType;
882
- let logMessageData;
883
-
884
- // log:thread-color => {theme_color}
885
- // log:user-nickname => {participant_id, nickname}
886
- // log:thread-icon => {thread_icon}
887
- // log:thread-name => {name}
888
- // log:subscribe => {addedParticipants - [Array]}
889
- // log:unsubscribe => {leftParticipantFbId}
890
-
891
- switch (m.class) {
892
- case "AdminTextMessage":
893
- logMessageData = m.untypedData;
894
- logMessageType = getAdminTextMessageType(m.type);
895
- break;
896
- case "ThreadName":
897
- logMessageType = "log:thread-name";
898
- logMessageData = { name: m.name };
899
- break;
900
- case "ParticipantsAddedToGroupThread":
901
- logMessageType = "log:subscribe";
902
- logMessageData = { addedParticipants: m.addedParticipants };
903
- break;
904
- case "ParticipantLeftGroupThread":
905
- logMessageType = "log:unsubscribe";
906
- logMessageData = { leftParticipantFbId: m.leftParticipantFbId };
907
- break;
908
- case "ApprovalQueue":
909
- logMessageType = "log:approval-queue";
910
- logMessageData = {
911
- approvalQueue: {
912
- action: m.action,
913
- recipientFbId: m.recipientFbId,
914
- requestSource: m.requestSource,
915
- ...m.messageMetadata
916
- }
917
- };
918
- }
919
- return {
920
- type: "event",
921
- threadID: formatID(
922
- (
923
- m.messageMetadata.threadKey.threadFbId ||
924
- m.messageMetadata.threadKey.otherUserFbId
925
- ).toString()
926
- ),
927
- messageID: m.messageMetadata.messageId.toString(),
928
- logMessageType,
929
- logMessageData,
930
- logMessageBody: m.messageMetadata.adminText,
931
- timestamp: m.messageMetadata.timestamp,
932
- author: m.messageMetadata.actorFbId,
933
- participantIDs: m.participants
934
- };
952
+ var logMessageType;
953
+ var logMessageData;
954
+
955
+ // log:thread-color => {theme_color}
956
+ // log:user-nickname => {participant_id, nickname}
957
+ // log:thread-icon => {thread_icon}
958
+ // log:thread-name => {name}
959
+ // log:subscribe => {addedParticipants - [Array]}
960
+ // log:unsubscribe => {leftParticipantFbId}
961
+
962
+ switch (m.class) {
963
+ case "AdminTextMessage":
964
+ logMessageType = getAdminTextMessageType(m.type);
965
+ logMessageData = m.untypedData;
966
+ break;
967
+ case "ThreadName":
968
+ logMessageType = "log:thread-name";
969
+ logMessageData = { name: m.name };
970
+ break;
971
+ case "ParticipantsAddedToGroupThread":
972
+ logMessageType = "log:subscribe";
973
+ logMessageData = { addedParticipants: m.addedParticipants };
974
+ break;
975
+ case "ParticipantLeftGroupThread":
976
+ logMessageType = "log:unsubscribe";
977
+ logMessageData = { leftParticipantFbId: m.leftParticipantFbId };
978
+ break;
979
+ }
980
+
981
+ return {
982
+ type: "event",
983
+ threadID: formatID(
984
+ (
985
+ m.messageMetadata.threadKey.threadFbId ||
986
+ m.messageMetadata.threadKey.otherUserFbId
987
+ ).toString()
988
+ ),
989
+ logMessageType: logMessageType,
990
+ logMessageData: logMessageData,
991
+ logMessageBody: m.messageMetadata.adminText,
992
+ author: m.messageMetadata.actorFbId,
993
+ participantIDs: m.participants || []
994
+ };
935
995
  }
936
996
 
937
997
  function formatTyp(event) {
@@ -991,7 +1051,7 @@ function getFrom(str, startToken, endToken) {
991
1051
  const lastHalf = str.substring(start);
992
1052
  const end = lastHalf.indexOf(endToken);
993
1053
  if (end === -1) {
994
- throw Error(
1054
+ throw new Error(
995
1055
  "Could not find endTime `" + endToken + "` in the given string."
996
1056
  );
997
1057
  }
@@ -1048,6 +1108,21 @@ function makeDefaults(html, userID, ctx) {
1048
1108
  let reqCounter = 1;
1049
1109
  const fb_dtsg = getFrom(html, 'name="fb_dtsg" value="', '"');
1050
1110
 
1111
+ // @Hack Ok we've done hacky things, this is definitely on top 5.
1112
+ // We totally assume the object is flat and try parsing until a }.
1113
+ // If it works though it's cool because we get a bunch of extra data things.
1114
+ //
1115
+ // Update: we don't need this. Leaving it in in case we ever do.
1116
+ // Ben - July 15th 2017
1117
+
1118
+ // var siteData = getFrom(html, "[\"SiteData\",[],", "},");
1119
+ // try {
1120
+ // siteData = JSON.parse(siteData + "}");
1121
+ // } catch(e) {
1122
+ // log.warn("makeDefaults", "Couldn't parse SiteData. Won't have access to some variables.");
1123
+ // siteData = {};
1124
+ // }
1125
+
1051
1126
  let ttstamp = "2";
1052
1127
  for (let i = 0; i < fb_dtsg.length; i++) {
1053
1128
  ttstamp += fb_dtsg.charCodeAt(i);
@@ -1055,108 +1130,189 @@ function makeDefaults(html, userID, ctx) {
1055
1130
  const revision = getFrom(html, 'revision":', ",");
1056
1131
 
1057
1132
  function mergeWithDefaults(obj) {
1133
+ // @TODO This is missing a key called __dyn.
1134
+ // After some investigation it seems like __dyn is some sort of set that FB
1135
+ // calls BitMap. It seems like certain responses have a "define" key in the
1136
+ // res.jsmods arrays. I think the code iterates over those and calls `set`
1137
+ // on the bitmap for each of those keys. Then it calls
1138
+ // bitmap.toCompressedString() which returns what __dyn is.
1139
+ //
1140
+ // So far the API has been working without this.
1141
+ //
1142
+ // Ben - July 15th 2017
1058
1143
  const newObj = {
1059
- av: userID,
1060
1144
  __user: userID,
1061
1145
  __req: (reqCounter++).toString(36),
1062
1146
  __rev: revision,
1063
1147
  __a: 1,
1064
- fb_dtsg: ctx.fb_dtsg || fb_dtsg,
1065
- jazoest: ctx.ttstamp || ttstamp
1066
- }
1148
+ // __af: siteData.features,
1149
+ fb_dtsg: ctx.fb_dtsg ? ctx.fb_dtsg : fb_dtsg,
1150
+ jazoest: ctx.ttstamp ? ctx.ttstamp : ttstamp
1151
+ // __spin_r: siteData.__spin_r,
1152
+ // __spin_b: siteData.__spin_b,
1153
+ // __spin_t: siteData.__spin_t,
1154
+ };
1155
+
1156
+ // @TODO this is probably not needed.
1157
+ // Ben - July 15th 2017
1158
+ // if (siteData.be_key) {
1159
+ // newObj[siteData.be_key] = siteData.be_mode;
1160
+ // }
1161
+ // if (siteData.pkg_cohort_key) {
1162
+ // newObj[siteData.pkg_cohort_key] = siteData.pkg_cohort;
1163
+ // }
1067
1164
 
1068
1165
  if (!obj) return newObj;
1069
1166
 
1070
- for (var prop in obj) {
1167
+ for (const prop in obj) {
1071
1168
  if (obj.hasOwnProperty(prop)) {
1072
- if (!newObj[prop])
1073
- newObj[prop] = obj[prop];
1169
+ if (!newObj[prop]) {
1170
+ newObj[prop] = obj[prop];
1171
+ }
1074
1172
  }
1075
1173
  }
1076
1174
 
1077
1175
  return newObj;
1078
1176
  }
1079
1177
 
1178
+ function postWithDefaults(url, jar, form, ctxx, customHeader = {}) {
1179
+ return post(url, jar, mergeWithDefaults(form), ctx.globalOptions, ctxx || ctx, customHeader);
1180
+ }
1181
+
1182
+ function getWithDefaults(url, jar, qs, ctxx, customHeader = {}) {
1183
+ return get(url, jar, mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx, customHeader);
1184
+ }
1185
+
1186
+ function postFormDataWithDefault(url, jar, form, qs, ctxx) {
1187
+ return postFormData(
1188
+ url,
1189
+ jar,
1190
+ mergeWithDefaults(form),
1191
+ mergeWithDefaults(qs),
1192
+ ctx.globalOptions,
1193
+ ctxx || ctx
1194
+ );
1195
+ }
1196
+
1080
1197
  return {
1081
- get: (url, jar, qs, ctxx, customHeader = {}) => get(url, jar, mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx, customHeader),
1082
- post: (url, jar, form, ctxx, customHeader = {}) => post(url, jar, mergeWithDefaults(form), ctx.globalOptions, ctxx || ctx, customHeader),
1083
- postFormData: (url, jar, form, qs, ctxx) => postFormData(url, jar, mergeWithDefaults(form), mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx)
1198
+ get: getWithDefaults,
1199
+ post: postWithDefaults,
1200
+ postFormData: postFormDataWithDefault
1084
1201
  };
1085
1202
  }
1086
1203
 
1087
- function parseAndCheckLogin(ctx, http, retryCount) {
1088
- var delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
1089
- var _try = (tryData) => new Promise(function (resolve, reject) {
1090
- try {
1091
- resolve(tryData());
1092
- } catch (error) {
1093
- reject(error);
1094
- }
1095
- });
1096
- if (retryCount == undefined) retryCount = 0;
1097
-
1204
+ //Fix parseAndCheckLogin
1205
+ function parseAndCheckLogin(ctx, defaultFuncs, retryCount, sourceCall) {
1206
+ if (retryCount == undefined) {
1207
+ retryCount = 0;
1208
+ }
1209
+ if (sourceCall == undefined) {
1210
+ try {
1211
+ throw new Error();
1212
+ }
1213
+ catch (e) {
1214
+ sourceCall = e;
1215
+ }
1216
+ }
1098
1217
  return function (data) {
1099
- function any() {
1100
- if (data.statusCode >= 500 && data.statusCode < 600) {
1101
- if (retryCount >= 5) {
1102
- const err = new Error("Request retry failed. Check the `res` and `statusCode` property on this error.");
1103
- err.statusCode = data.statusCode;
1104
- err.res = data.body;
1105
- err.error = "Request retry failed. Check the `res` and `statusCode` property on this error.";
1106
- throw err;
1107
- }
1108
- retryCount++;
1109
- const retryTime = Math.floor(Math.random() * 5000);
1110
- console.warn("parseAndCheckLogin", "Got status code " + data.statusCode + " - " + retryCount + ". attempt to retry in " + retryTime + " milliseconds...");
1111
- const url = data.request.uri.protocol + "//" + data.request.uri.hostname + data.request.uri.pathname;
1112
- if (data.request.headers["Content-Type"].split(";")[0] === "multipart/form-data") {
1218
+ return tryPromise(function () {
1219
+ log.verbose("parseAndCheckLogin", data.body);
1220
+ if (data.statusCode >= 500 && data.statusCode < 600) {
1221
+ if (retryCount >= 5) {
1222
+ throw new CustomError({
1223
+ message: "Request retry failed. Check the `res` and `statusCode` property on this error.",
1224
+ statusCode: data.statusCode,
1225
+ res: data.body,
1226
+ error: "Request retry failed. Check the `res` and `statusCode` property on this error.",
1227
+ sourceCall: sourceCall
1228
+ });
1229
+ }
1230
+ retryCount++;
1231
+ const retryTime = Math.floor(Math.random() * 5000);
1232
+ log.warn(
1233
+ "parseAndCheckLogin",
1234
+ "Got status code " +
1235
+ data.statusCode +
1236
+ " - " +
1237
+ retryCount +
1238
+ ". attempt to retry in " +
1239
+ retryTime +
1240
+ " milliseconds..."
1241
+ );
1242
+ const url =
1243
+ data.request.uri.protocol +
1244
+ "//" +
1245
+ data.request.uri.hostname +
1246
+ data.request.uri.pathname;
1247
+ if (
1248
+ data.request.headers["Content-Type"].split(";")[0] ===
1249
+ "multipart/form-data"
1250
+ ) {
1113
1251
  return delay(retryTime)
1114
- .then(function () {
1115
- return http
1116
- .postFormData(url, ctx.jar, data.request.formData);
1117
- })
1118
- .then(parseAndCheckLogin(ctx, http, retryCount));
1119
- }
1120
- else {
1121
- return delay(retryTime)
1122
- .then(function () {
1123
- return http
1124
- .post(url, ctx.jar, data.request.formData);
1125
- })
1126
- .then(parseAndCheckLogin(ctx, http, retryCount));
1127
- }
1128
- }
1129
-
1130
- if (data.statusCode === 404) return;
1131
-
1132
- if (data.statusCode !== 200)
1133
- throw new Error("parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.");
1134
-
1135
- let res = null;
1252
+ .then(function () {
1253
+ return defaultFuncs.postFormData(
1254
+ url,
1255
+ ctx.jar,
1256
+ data.request.formData,
1257
+ {}
1258
+ );
1259
+ })
1260
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount, sourceCall));
1261
+ }
1262
+ else {
1263
+ return delay(retryTime)
1264
+ .then(function () {
1265
+ return defaultFuncs.post(url, ctx.jar, data.request.formData);
1266
+ })
1267
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount, sourceCall));
1268
+ }
1269
+ }
1270
+ if (data.statusCose === 404) return;
1271
+ if (data.statusCode !== 200)
1272
+ throw new CustomError({
1273
+ message: "parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.",
1274
+ statusCode: data.statusCode,
1275
+ res: data.body,
1276
+ error: "parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.",
1277
+ sourceCall: sourceCall
1278
+ });
1279
+
1280
+ let res = null;
1136
1281
  try {
1137
1282
  res = JSON.parse(makeParsable(data.body));
1138
- } catch (e) {
1139
- const err = new Error("JSON.parse error. Check the `detail` property on this error.");
1140
- err.error = "JSON.parse error. Check the `detail` property on this error.";
1141
- err.detail = e;
1142
- err.res = data.body;
1143
- throw err;
1283
+ } catch (e) {
1284
+ throw new CustomError({
1285
+ message: "JSON.parse error. Check the `detail` property on this error.",
1286
+ detail: e,
1287
+ res: data.body,
1288
+ error: "JSON.parse error. Check the `detail` property on this error.",
1289
+ sourceCall: sourceCall
1290
+ });
1144
1291
  }
1145
1292
 
1146
1293
  // In some cases the response contains only a redirect URL which should be followed
1147
1294
  if (res.redirect && data.request.method === "GET") {
1148
- return http
1295
+ return defaultFuncs
1149
1296
  .get(res.redirect, ctx.jar)
1150
- .then(parseAndCheckLogin(ctx, http));
1151
- }
1297
+ .then(parseAndCheckLogin(ctx, defaultFuncs, undefined, sourceCall));
1298
+ }
1152
1299
 
1153
1300
  // TODO: handle multiple cookies?
1154
- if (res.jsmods && res.jsmods.require && Array.isArray(res.jsmods.require[0]) && res.jsmods.require[0][0] === "Cookie") {
1155
- res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace("_js_", "");
1156
- const requireCookie = res.jsmods.require[0][3];
1157
- ctx.jar.setCookie(formatCookie(requireCookie, "facebook"), "https://www.facebook.com");
1158
- ctx.jar.setCookie(formatCookie(requireCookie, "messenger"), "https://www.messenger.com");
1159
- }
1301
+ if (
1302
+ res.jsmods &&
1303
+ res.jsmods.require &&
1304
+ Array.isArray(res.jsmods.require[0]) &&
1305
+ res.jsmods.require[0][0] === "Cookie"
1306
+ ) {
1307
+ res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace(
1308
+ "_js_",
1309
+ ""
1310
+ );
1311
+ const cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1312
+ const cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1313
+ ctx.jar.setCookie(cookie, "https://www.facebook.com");
1314
+ ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1315
+ }
1160
1316
 
1161
1317
  // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1162
1318
  // one for the next requests.
@@ -1176,13 +1332,16 @@ function parseAndCheckLogin(ctx, http, retryCount) {
1176
1332
  }
1177
1333
 
1178
1334
  if (res.error === 1357001) {
1179
- const err = new Error('Facebook blocked the login');
1180
- err.error = "Not logged in.";
1181
- throw err;
1335
+ throw new CustomError({
1336
+ message: "Facebook blocked login. Please visit https://facebook.com and check your account.",
1337
+ error: "Not logged in.",
1338
+ res: res,
1339
+ statusCode: data.statusCode,
1340
+ sourceCall: sourceCall
1341
+ });
1182
1342
  }
1183
1343
  return res;
1184
- }
1185
- return _try(any);
1344
+ });
1186
1345
  };
1187
1346
  }
1188
1347
 
@@ -1249,39 +1408,39 @@ function formatCookie(arr, url) {
1249
1408
  }
1250
1409
 
1251
1410
  function formatThread(data) {
1252
- return {
1253
- threadID: formatID(data.thread_fbid.toString()),
1254
- participants: data.participants.map(formatID),
1255
- participantIDs: data.participants.map(formatID),
1256
- name: data.name,
1257
- nicknames: data.custom_nickname,
1258
- snippet: data.snippet,
1259
- snippetAttachments: data.snippet_attachments,
1260
- snippetSender: formatID((data.snippet_sender || "").toString()),
1261
- unreadCount: data.unread_count,
1262
- messageCount: data.message_count,
1263
- imageSrc: data.image_src,
1264
- timestamp: data.timestamp,
1265
- serverTimestamp: data.server_timestamp, // what is this?
1266
- muteUntil: data.mute_until,
1267
- isCanonicalUser: data.is_canonical_user,
1268
- isCanonical: data.is_canonical,
1269
- isSubscribed: data.is_subscribed,
1270
- folder: data.folder,
1271
- isArchived: data.is_archived,
1272
- recipientsLoadable: data.recipients_loadable,
1273
- hasEmailParticipant: data.has_email_participant,
1274
- readOnly: data.read_only,
1275
- canReply: data.can_reply,
1276
- cannotReplyReason: data.cannot_reply_reason,
1277
- lastMessageTimestamp: data.last_message_timestamp,
1278
- lastReadTimestamp: data.last_read_timestamp,
1279
- lastMessageType: data.last_message_type,
1280
- emoji: data.custom_like_icon,
1281
- color: data.custom_color,
1282
- adminIDs: data.admin_ids,
1283
- threadType: data.thread_type
1284
- };
1411
+ return {
1412
+ threadID: formatID(data.thread_fbid.toString()),
1413
+ participants: data.participants.map(formatID),
1414
+ participantIDs: data.participants.map(formatID),
1415
+ name: data.name,
1416
+ nicknames: data.custom_nickname,
1417
+ snippet: data.snippet,
1418
+ snippetAttachments: data.snippet_attachments,
1419
+ snippetSender: formatID((data.snippet_sender || "").toString()),
1420
+ unreadCount: data.unread_count,
1421
+ messageCount: data.message_count,
1422
+ imageSrc: data.image_src,
1423
+ timestamp: data.timestamp,
1424
+ serverTimestamp: data.server_timestamp, // what is this?
1425
+ muteUntil: data.mute_until,
1426
+ isCanonicalUser: data.is_canonical_user,
1427
+ isCanonical: data.is_canonical,
1428
+ isSubscribed: data.is_subscribed,
1429
+ folder: data.folder,
1430
+ isArchived: data.is_archived,
1431
+ recipientsLoadable: data.recipients_loadable,
1432
+ hasEmailParticipant: data.has_email_participant,
1433
+ readOnly: data.read_only,
1434
+ canReply: data.can_reply,
1435
+ cannotReplyReason: data.cannot_reply_reason,
1436
+ lastMessageTimestamp: data.last_message_timestamp,
1437
+ lastReadTimestamp: data.last_read_timestamp,
1438
+ lastMessageType: data.last_message_type,
1439
+ emoji: data.custom_like_icon,
1440
+ color: data.custom_color,
1441
+ adminIDs: data.admin_ids,
1442
+ threadType: data.thread_type
1443
+ };
1285
1444
  }
1286
1445
 
1287
1446
  function getType(obj) {
@@ -1307,6 +1466,34 @@ function formatPresence(presence, userID) {
1307
1466
  };
1308
1467
  }
1309
1468
 
1469
+ function getFroms(str, startToken, endToken) {
1470
+ //advanced search by kanzuuuuuuuuuu
1471
+ let results = [];
1472
+ let currentIndex = 0;
1473
+
1474
+ while (true) {
1475
+ let start = str.indexOf(startToken, currentIndex);
1476
+ if (start === -1) break;
1477
+
1478
+ start += startToken.length;
1479
+
1480
+ let lastHalf = str.substring(start);
1481
+ let end = lastHalf.indexOf(endToken);
1482
+
1483
+ if (end === -1) {
1484
+ if (results.length === 0) {
1485
+ throw Error("Could not find endToken `" + endToken + "` in the given string.");
1486
+ }
1487
+ break;
1488
+ }
1489
+
1490
+ results.push(lastHalf.substring(0, end));
1491
+ currentIndex = start + end + endToken.length;
1492
+ }
1493
+
1494
+ return results.length === 0 ? "" : results.length === 1 ? results[0] : results;
1495
+ }
1496
+
1310
1497
  function decodeClientPayload(payload) {
1311
1498
  /*
1312
1499
  Special function which Client using to "encode" clients JSON payload
@@ -1315,26 +1502,14 @@ function decodeClientPayload(payload) {
1315
1502
  }
1316
1503
 
1317
1504
  function getAppState(jar) {
1318
- return jar
1319
- .getCookies("https://www.facebook.com")
1320
- .concat(jar.getCookies("https://www.messenger.com"));
1321
- }
1322
-
1323
- function getAccessFromBusiness(jar, Options) {
1324
- return function (res) {
1325
- var html = res ? res.body : null;
1326
- return get('https://business.facebook.com/content_management', jar, null, Options, null, { noRef: true })
1327
- .then(function (res) {
1328
- var token = /"accessToken":"([^.]+)","clientID":/g.exec(res.body)[1];
1329
- return [html, token];
1330
- })
1331
- .catch(function () {
1332
- return [html, null];
1333
- });
1334
- }
1505
+ return jar
1506
+ .getCookies("https://www.facebook.com")
1507
+ .concat(jar.getCookies("https://facebook.com"))
1508
+ .concat(jar.getCookies("https://www.messenger.com"));
1335
1509
  }
1336
1510
 
1337
1511
  module.exports = {
1512
+ CustomError,
1338
1513
  isReadableStream,
1339
1514
  get,
1340
1515
  post,
@@ -1373,7 +1548,5 @@ module.exports = {
1373
1548
  getAppState,
1374
1549
  getAdminTextMessageType,
1375
1550
  setProxy,
1376
- getAccessFromBusiness,
1377
- presenceDecode,
1378
- presenceEncode
1379
- }
1551
+ getFroms
1552
+ };