@bellachu/xteambail 2.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 (413) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +5479 -0
  5. package/WAProto/fix-imports.js +38 -0
  6. package/WAProto/index.d.ts +14017 -0
  7. package/WAProto/index.js +2213 -0
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/index.d.ts +75 -0
  10. package/lib/Defaults/index.d.ts.map +1 -0
  11. package/lib/Defaults/index.js +139 -0
  12. package/lib/Defaults/index.js.map +1 -0
  13. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  14. package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  15. package/lib/Signal/Group/ciphertext-message.js +19 -0
  16. package/lib/Signal/Group/ciphertext-message.js.map +1 -0
  17. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  18. package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  19. package/lib/Signal/Group/group-session-builder.js +38 -0
  20. package/lib/Signal/Group/group-session-builder.js.map +1 -0
  21. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  22. package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  23. package/lib/Signal/Group/group_cipher.js +87 -0
  24. package/lib/Signal/Group/group_cipher.js.map +1 -0
  25. package/lib/Signal/Group/index.d.ts +12 -0
  26. package/lib/Signal/Group/index.d.ts.map +1 -0
  27. package/lib/Signal/Group/index.js +79 -0
  28. package/lib/Signal/Group/index.js.map +1 -0
  29. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  30. package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  31. package/lib/Signal/Group/keyhelper.js +27 -0
  32. package/lib/Signal/Group/keyhelper.js.map +1 -0
  33. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  34. package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  35. package/lib/Signal/Group/sender-chain-key.js +33 -0
  36. package/lib/Signal/Group/sender-chain-key.js.map +1 -0
  37. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  38. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  39. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  40. package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  41. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  42. package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  43. package/lib/Signal/Group/sender-key-message.js +68 -0
  44. package/lib/Signal/Group/sender-key-message.js.map +1 -0
  45. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  46. package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  47. package/lib/Signal/Group/sender-key-name.js +54 -0
  48. package/lib/Signal/Group/sender-key-name.js.map +1 -0
  49. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  50. package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  51. package/lib/Signal/Group/sender-key-record.js +48 -0
  52. package/lib/Signal/Group/sender-key-record.js.map +1 -0
  53. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  54. package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  55. package/lib/Signal/Group/sender-key-state.js +88 -0
  56. package/lib/Signal/Group/sender-key-state.js.map +1 -0
  57. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  58. package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  59. package/lib/Signal/Group/sender-message-key.js +33 -0
  60. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  61. package/lib/Signal/libsignal.d.ts +5 -0
  62. package/lib/Signal/libsignal.d.ts.map +1 -0
  63. package/lib/Signal/libsignal.js +575 -0
  64. package/lib/Signal/libsignal.js.map +1 -0
  65. package/lib/Signal/lid-mapping.d.ts +23 -0
  66. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  67. package/lib/Signal/lid-mapping.js +309 -0
  68. package/lib/Signal/lid-mapping.js.map +1 -0
  69. package/lib/Socket/Client/index.d.ts +3 -0
  70. package/lib/Socket/Client/index.d.ts.map +1 -0
  71. package/lib/Socket/Client/index.js +27 -0
  72. package/lib/Socket/Client/index.js.map +1 -0
  73. package/lib/Socket/Client/types.d.ts +16 -0
  74. package/lib/Socket/Client/types.d.ts.map +1 -0
  75. package/lib/Socket/Client/types.js +18 -0
  76. package/lib/Socket/Client/types.js.map +1 -0
  77. package/lib/Socket/Client/websocket.d.ts +13 -0
  78. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  79. package/lib/Socket/Client/websocket.js +62 -0
  80. package/lib/Socket/Client/websocket.js.map +1 -0
  81. package/lib/Socket/business.d.ts +217 -0
  82. package/lib/Socket/business.d.ts.map +1 -0
  83. package/lib/Socket/business.js +368 -0
  84. package/lib/Socket/business.js.map +1 -0
  85. package/lib/Socket/chats.d.ts +124 -0
  86. package/lib/Socket/chats.d.ts.map +1 -0
  87. package/lib/Socket/chats.js +1340 -0
  88. package/lib/Socket/chats.js.map +1 -0
  89. package/lib/Socket/communities.d.ts +273 -0
  90. package/lib/Socket/communities.d.ts.map +1 -0
  91. package/lib/Socket/communities.js +522 -0
  92. package/lib/Socket/communities.js.map +1 -0
  93. package/lib/Socket/groups.d.ts +161 -0
  94. package/lib/Socket/groups.d.ts.map +1 -0
  95. package/lib/Socket/groups.js +430 -0
  96. package/lib/Socket/groups.js.map +1 -0
  97. package/lib/Socket/index.d.ts +260 -0
  98. package/lib/Socket/index.d.ts.map +1 -0
  99. package/lib/Socket/index.js +11 -0
  100. package/lib/Socket/index.js.map +1 -0
  101. package/lib/Socket/luxu.js +422 -0
  102. package/lib/Socket/messages-recv.d.ts +203 -0
  103. package/lib/Socket/messages-recv.d.ts.map +1 -0
  104. package/lib/Socket/messages-recv.js +2374 -0
  105. package/lib/Socket/messages-recv.js.map +1 -0
  106. package/lib/Socket/messages-send.d.ts +199 -0
  107. package/lib/Socket/messages-send.d.ts.map +1 -0
  108. package/lib/Socket/messages-send.js +1502 -0
  109. package/lib/Socket/messages-send.js.map +1 -0
  110. package/lib/Socket/mex.d.ts +3 -0
  111. package/lib/Socket/mex.d.ts.map +1 -0
  112. package/lib/Socket/mex.js +55 -0
  113. package/lib/Socket/mex.js.map +1 -0
  114. package/lib/Socket/newsletter.d.ts +170 -0
  115. package/lib/Socket/newsletter.d.ts.map +1 -0
  116. package/lib/Socket/newsletter.js +238 -0
  117. package/lib/Socket/newsletter.js.map +1 -0
  118. package/lib/Socket/socket.d.ts +59 -0
  119. package/lib/Socket/socket.d.ts.map +1 -0
  120. package/lib/Socket/socket.js +1022 -0
  121. package/lib/Socket/socket.js.map +1 -0
  122. package/lib/Store/index.d.ts +3 -0
  123. package/lib/Store/index.js +10 -0
  124. package/lib/Store/make-cache-manager-store.d.ts +13 -0
  125. package/lib/Store/make-cache-manager-store.js +82 -0
  126. package/lib/Store/make-in-memory-store.d.ts +118 -0
  127. package/lib/Store/make-in-memory-store.js +429 -0
  128. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  129. package/lib/Store/make-ordered-dictionary.js +81 -0
  130. package/lib/Store/object-repository.d.ts +10 -0
  131. package/lib/Store/object-repository.js +27 -0
  132. package/lib/Types/Auth.d.ts +117 -0
  133. package/lib/Types/Auth.d.ts.map +1 -0
  134. package/lib/Types/Auth.js +5 -0
  135. package/lib/Types/Auth.js.map +1 -0
  136. package/lib/Types/Bussines.d.ts +25 -0
  137. package/lib/Types/Bussines.d.ts.map +1 -0
  138. package/lib/Types/Bussines.js +5 -0
  139. package/lib/Types/Bussines.js.map +1 -0
  140. package/lib/Types/Call.d.ts +15 -0
  141. package/lib/Types/Call.d.ts.map +1 -0
  142. package/lib/Types/Call.js +5 -0
  143. package/lib/Types/Call.js.map +1 -0
  144. package/lib/Types/Chat.d.ts +124 -0
  145. package/lib/Types/Chat.d.ts.map +1 -0
  146. package/lib/Types/Chat.js +8 -0
  147. package/lib/Types/Chat.js.map +1 -0
  148. package/lib/Types/Contact.d.ts +26 -0
  149. package/lib/Types/Contact.d.ts.map +1 -0
  150. package/lib/Types/Contact.js +5 -0
  151. package/lib/Types/Contact.js.map +1 -0
  152. package/lib/Types/Events.d.ts +256 -0
  153. package/lib/Types/Events.d.ts.map +1 -0
  154. package/lib/Types/Events.js +3 -0
  155. package/lib/Types/Events.js.map +1 -0
  156. package/lib/Types/GroupMetadata.d.ts +71 -0
  157. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  158. package/lib/Types/GroupMetadata.js +5 -0
  159. package/lib/Types/GroupMetadata.js.map +1 -0
  160. package/lib/Types/Label.d.ts +47 -0
  161. package/lib/Types/Label.d.ts.map +1 -0
  162. package/lib/Types/Label.js +31 -0
  163. package/lib/Types/Label.js.map +1 -0
  164. package/lib/Types/LabelAssociation.d.ts +30 -0
  165. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  166. package/lib/Types/LabelAssociation.js +13 -0
  167. package/lib/Types/LabelAssociation.js.map +1 -0
  168. package/lib/Types/Message.d.ts +320 -0
  169. package/lib/Types/Message.d.ts.map +1 -0
  170. package/lib/Types/Message.js +23 -0
  171. package/lib/Types/Message.js.map +1 -0
  172. package/lib/Types/Mex.d.ts +141 -0
  173. package/lib/Types/Mex.d.ts.map +1 -0
  174. package/lib/Types/Mex.js +43 -0
  175. package/lib/Types/Mex.js.map +1 -0
  176. package/lib/Types/Product.d.ts +79 -0
  177. package/lib/Types/Product.d.ts.map +1 -0
  178. package/lib/Types/Product.js +5 -0
  179. package/lib/Types/Product.js.map +1 -0
  180. package/lib/Types/Signal.d.ts +87 -0
  181. package/lib/Types/Signal.d.ts.map +1 -0
  182. package/lib/Types/Signal.js +3 -0
  183. package/lib/Types/Signal.js.map +1 -0
  184. package/lib/Types/Socket.d.ts +136 -0
  185. package/lib/Types/Socket.d.ts.map +1 -0
  186. package/lib/Types/Socket.js +4 -0
  187. package/lib/Types/Socket.js.map +1 -0
  188. package/lib/Types/State.d.ts +97 -0
  189. package/lib/Types/State.d.ts.map +1 -0
  190. package/lib/Types/State.js +62 -0
  191. package/lib/Types/State.js.map +1 -0
  192. package/lib/Types/USync.d.ts +26 -0
  193. package/lib/Types/USync.d.ts.map +1 -0
  194. package/lib/Types/USync.js +3 -0
  195. package/lib/Types/USync.js.map +1 -0
  196. package/lib/Types/index.d.ts +65 -0
  197. package/lib/Types/index.d.ts.map +1 -0
  198. package/lib/Types/index.js +167 -0
  199. package/lib/Types/index.js.map +1 -0
  200. package/lib/Utils/auth-utils.d.ts +24 -0
  201. package/lib/Utils/auth-utils.d.ts.map +1 -0
  202. package/lib/Utils/auth-utils.js +332 -0
  203. package/lib/Utils/auth-utils.js.map +1 -0
  204. package/lib/Utils/browser-utils.d.ts +4 -0
  205. package/lib/Utils/browser-utils.d.ts.map +1 -0
  206. package/lib/Utils/browser-utils.js +35 -0
  207. package/lib/Utils/browser-utils.js.map +1 -0
  208. package/lib/Utils/business.d.ts +23 -0
  209. package/lib/Utils/business.d.ts.map +1 -0
  210. package/lib/Utils/business.js +247 -0
  211. package/lib/Utils/business.js.map +1 -0
  212. package/lib/Utils/chat-utils.d.ts +100 -0
  213. package/lib/Utils/chat-utils.d.ts.map +1 -0
  214. package/lib/Utils/chat-utils.js +971 -0
  215. package/lib/Utils/chat-utils.js.map +1 -0
  216. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  217. package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
  218. package/lib/Utils/companion-reg-client-utils.js +43 -0
  219. package/lib/Utils/companion-reg-client-utils.js.map +1 -0
  220. package/lib/Utils/crypto.d.ts +37 -0
  221. package/lib/Utils/crypto.d.ts.map +1 -0
  222. package/lib/Utils/crypto.js +160 -0
  223. package/lib/Utils/crypto.js.map +1 -0
  224. package/lib/Utils/decode-wa-message.d.ts +66 -0
  225. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  226. package/lib/Utils/decode-wa-message.js +380 -0
  227. package/lib/Utils/decode-wa-message.js.map +1 -0
  228. package/lib/Utils/event-buffer.d.ts +36 -0
  229. package/lib/Utils/event-buffer.d.ts.map +1 -0
  230. package/lib/Utils/event-buffer.js +666 -0
  231. package/lib/Utils/event-buffer.js.map +1 -0
  232. package/lib/Utils/generics.d.ts +91 -0
  233. package/lib/Utils/generics.d.ts.map +1 -0
  234. package/lib/Utils/generics.js +449 -0
  235. package/lib/Utils/generics.js.map +1 -0
  236. package/lib/Utils/history.d.ts +24 -0
  237. package/lib/Utils/history.d.ts.map +1 -0
  238. package/lib/Utils/history.js +160 -0
  239. package/lib/Utils/history.js.map +1 -0
  240. package/lib/Utils/identity-change-handler.d.ts +44 -0
  241. package/lib/Utils/identity-change-handler.d.ts.map +1 -0
  242. package/lib/Utils/identity-change-handler.js +94 -0
  243. package/lib/Utils/identity-change-handler.js.map +1 -0
  244. package/lib/Utils/index.d.ts +22 -0
  245. package/lib/Utils/index.d.ts.map +1 -0
  246. package/lib/Utils/index.js +236 -0
  247. package/lib/Utils/index.js.map +1 -0
  248. package/lib/Utils/link-preview.d.ts +21 -0
  249. package/lib/Utils/link-preview.d.ts.map +1 -0
  250. package/lib/Utils/link-preview.js +102 -0
  251. package/lib/Utils/link-preview.js.map +1 -0
  252. package/lib/Utils/logger.d.ts +12 -0
  253. package/lib/Utils/logger.d.ts.map +1 -0
  254. package/lib/Utils/logger.js +11 -0
  255. package/lib/Utils/logger.js.map +1 -0
  256. package/lib/Utils/lt-hash.d.ts +8 -0
  257. package/lib/Utils/lt-hash.d.ts.map +1 -0
  258. package/lib/Utils/lt-hash.js +14 -0
  259. package/lib/Utils/lt-hash.js.map +1 -0
  260. package/lib/Utils/make-mutex.d.ts +9 -0
  261. package/lib/Utils/make-mutex.d.ts.map +1 -0
  262. package/lib/Utils/make-mutex.js +43 -0
  263. package/lib/Utils/make-mutex.js.map +1 -0
  264. package/lib/Utils/message-retry-manager.d.ts +115 -0
  265. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  266. package/lib/Utils/message-retry-manager.js +283 -0
  267. package/lib/Utils/message-retry-manager.js.map +1 -0
  268. package/lib/Utils/messages-media.d.ts +133 -0
  269. package/lib/Utils/messages-media.d.ts.map +1 -0
  270. package/lib/Utils/messages-media.js +914 -0
  271. package/lib/Utils/messages-media.js.map +1 -0
  272. package/lib/Utils/messages.d.ts +91 -0
  273. package/lib/Utils/messages.d.ts.map +1 -0
  274. package/lib/Utils/messages.js +963 -0
  275. package/lib/Utils/messages.js.map +1 -0
  276. package/lib/Utils/noise-handler.d.ts +20 -0
  277. package/lib/Utils/noise-handler.d.ts.map +1 -0
  278. package/lib/Utils/noise-handler.js +238 -0
  279. package/lib/Utils/noise-handler.js.map +1 -0
  280. package/lib/Utils/offline-node-processor.d.ts +17 -0
  281. package/lib/Utils/offline-node-processor.d.ts.map +1 -0
  282. package/lib/Utils/offline-node-processor.js +54 -0
  283. package/lib/Utils/offline-node-processor.js.map +1 -0
  284. package/lib/Utils/pre-key-manager.d.ts +28 -0
  285. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  286. package/lib/Utils/pre-key-manager.js +109 -0
  287. package/lib/Utils/pre-key-manager.js.map +1 -0
  288. package/lib/Utils/process-message.d.ts +60 -0
  289. package/lib/Utils/process-message.d.ts.map +1 -0
  290. package/lib/Utils/process-message.js +710 -0
  291. package/lib/Utils/process-message.js.map +1 -0
  292. package/lib/Utils/reporting-utils.d.ts +11 -0
  293. package/lib/Utils/reporting-utils.d.ts.map +1 -0
  294. package/lib/Utils/reporting-utils.js +586 -0
  295. package/lib/Utils/reporting-utils.js.map +1 -0
  296. package/lib/Utils/signal.d.ts +47 -0
  297. package/lib/Utils/signal.d.ts.map +1 -0
  298. package/lib/Utils/signal.js +279 -0
  299. package/lib/Utils/signal.js.map +1 -0
  300. package/lib/Utils/stanza-ack.d.ts +11 -0
  301. package/lib/Utils/stanza-ack.d.ts.map +1 -0
  302. package/lib/Utils/stanza-ack.js +47 -0
  303. package/lib/Utils/stanza-ack.js.map +1 -0
  304. package/lib/Utils/sync-action-utils.d.ts +19 -0
  305. package/lib/Utils/sync-action-utils.d.ts.map +1 -0
  306. package/lib/Utils/sync-action-utils.js +61 -0
  307. package/lib/Utils/sync-action-utils.js.map +1 -0
  308. package/lib/Utils/tc-token-utils.d.ts +37 -0
  309. package/lib/Utils/tc-token-utils.d.ts.map +1 -0
  310. package/lib/Utils/tc-token-utils.js +176 -0
  311. package/lib/Utils/tc-token-utils.js.map +1 -0
  312. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  313. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  314. package/lib/Utils/use-multi-file-auth-state.js +124 -0
  315. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  316. package/lib/Utils/validate-connection.d.ts +11 -0
  317. package/lib/Utils/validate-connection.d.ts.map +1 -0
  318. package/lib/Utils/validate-connection.js +229 -0
  319. package/lib/Utils/validate-connection.js.map +1 -0
  320. package/lib/WABinary/constants.d.ts +28 -0
  321. package/lib/WABinary/constants.d.ts.map +1 -0
  322. package/lib/WABinary/constants.js +42 -0
  323. package/lib/WABinary/constants.js.map +1 -0
  324. package/lib/WABinary/decode.d.ts +7 -0
  325. package/lib/WABinary/decode.d.ts.map +1 -0
  326. package/lib/WABinary/decode.js +271 -0
  327. package/lib/WABinary/decode.js.map +1 -0
  328. package/lib/WABinary/encode.d.ts +3 -0
  329. package/lib/WABinary/encode.d.ts.map +1 -0
  330. package/lib/WABinary/encode.js +226 -0
  331. package/lib/WABinary/encode.js.map +1 -0
  332. package/lib/WABinary/generic-utils.d.ts +15 -0
  333. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  334. package/lib/WABinary/generic-utils.js +219 -0
  335. package/lib/WABinary/generic-utils.js.map +1 -0
  336. package/lib/WABinary/index.d.ts +6 -0
  337. package/lib/WABinary/index.d.ts.map +1 -0
  338. package/lib/WABinary/index.js +60 -0
  339. package/lib/WABinary/index.js.map +1 -0
  340. package/lib/WABinary/jid-utils.d.ts +48 -0
  341. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  342. package/lib/WABinary/jid-utils.js +121 -0
  343. package/lib/WABinary/jid-utils.js.map +1 -0
  344. package/lib/WABinary/types.d.ts +19 -0
  345. package/lib/WABinary/types.d.ts.map +1 -0
  346. package/lib/WABinary/types.js +4 -0
  347. package/lib/WABinary/types.js.map +1 -0
  348. package/lib/WAM/BinaryInfo.d.ts +9 -0
  349. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  350. package/lib/WAM/BinaryInfo.js +17 -0
  351. package/lib/WAM/BinaryInfo.js.map +1 -0
  352. package/lib/WAM/constants.d.ts +40 -0
  353. package/lib/WAM/constants.d.ts.map +1 -0
  354. package/lib/WAM/constants.js +1 -0
  355. package/lib/WAM/constants.js.map +1 -0
  356. package/lib/WAM/encode.d.ts +3 -0
  357. package/lib/WAM/encode.d.ts.map +1 -0
  358. package/lib/WAM/encode.js +150 -0
  359. package/lib/WAM/encode.js.map +1 -0
  360. package/lib/WAM/index.d.ts +4 -0
  361. package/lib/WAM/index.d.ts.map +1 -0
  362. package/lib/WAM/index.js +38 -0
  363. package/lib/WAM/index.js.map +1 -0
  364. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  365. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  366. package/lib/WAUSync/Protocols/USyncContactProtocol.js +63 -0
  367. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  368. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  369. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  370. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
  371. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  372. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  373. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  374. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +34 -0
  375. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  376. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  377. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  378. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +43 -0
  379. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  380. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  381. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
  382. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +32 -0
  383. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
  384. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  385. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  386. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +68 -0
  387. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  388. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  389. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  390. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +37 -0
  391. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  392. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  393. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  394. package/lib/WAUSync/Protocols/index.js +60 -0
  395. package/lib/WAUSync/Protocols/index.js.map +1 -0
  396. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  397. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  398. package/lib/WAUSync/USyncQuery.js +103 -0
  399. package/lib/WAUSync/USyncQuery.js.map +1 -0
  400. package/lib/WAUSync/USyncUser.d.ts +17 -0
  401. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  402. package/lib/WAUSync/USyncUser.js +38 -0
  403. package/lib/WAUSync/USyncUser.js.map +1 -0
  404. package/lib/WAUSync/index.d.ts +4 -0
  405. package/lib/WAUSync/index.d.ts.map +1 -0
  406. package/lib/WAUSync/index.js +38 -0
  407. package/lib/WAUSync/index.js.map +1 -0
  408. package/lib/index.d.ts +13 -0
  409. package/lib/index.d.ts.map +1 -0
  410. package/lib/index.js +133 -0
  411. package/lib/index.js.map +1 -0
  412. package/package.json +72 -0
  413. package/scripts/patch-deps.js +35 -0
@@ -0,0 +1,2374 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.makeMessagesRecvSocket = void 0;
7
+ var _nodeCache = _interopRequireDefault(require("@cacheable/node-cache"));
8
+ var _boom = require("@hapi/boom");
9
+ var _crypto = require("crypto");
10
+ var _long = _interopRequireDefault(require("long"));
11
+ var _index = require("../../WAProto/index.js");
12
+ var _index2 = require("../Defaults/index.js");
13
+ var _index3 = require("../Types/index.js");
14
+ var _index4 = require("../Utils/index.js");
15
+ var _makeMutex = require("../Utils/make-mutex.js");
16
+ var _offlineNodeProcessor = require("../Utils/offline-node-processor.js");
17
+ var _stanzaAck = require("../Utils/stanza-ack.js");
18
+ var _tcTokenUtils = require("../Utils/tc-token-utils.js");
19
+ var _index5 = require("../WABinary/index.js");
20
+ var _groups = require("./groups.js");
21
+ var _messagesSend = require("./messages-send.js");
22
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
23
+ const ENFORCEMENT_TYPE_VALUES = new Set(Object.values(_index3.ReachoutTimelockEnforcementType));
24
+ function isValidEnforcementType(value) {
25
+ return typeof value === 'string' && ENFORCEMENT_TYPE_VALUES.has(value);
26
+ }
27
+ const makeMessagesRecvSocket = config => {
28
+ const {
29
+ logger,
30
+ retryRequestDelayMs,
31
+ maxMsgRetryCount,
32
+ getMessage,
33
+ shouldIgnoreJid,
34
+ enableAutoSessionRecreation
35
+ } = config;
36
+ const sock = (0, _messagesSend.makeMessagesSocket)(config);
37
+ const {
38
+ userDevicesCache,
39
+ devicesMutex,
40
+ ev,
41
+ authState,
42
+ ws,
43
+ messageMutex,
44
+ notificationMutex,
45
+ receiptMutex,
46
+ signalRepository,
47
+ query,
48
+ upsertMessage,
49
+ resyncAppState,
50
+ onUnexpectedError,
51
+ assertSessions,
52
+ sendNode,
53
+ sendMessage,
54
+ relayMessage,
55
+ sendReceipt,
56
+ uploadPreKeys,
57
+ sendPeerDataOperationMessage,
58
+ messageRetryManager,
59
+ registerSocketEndHandler,
60
+ issuePrivacyTokens,
61
+ fetchAccountReachoutTimelock,
62
+ placeholderResendCache
63
+ } = sock;
64
+ const getLIDForPN = signalRepository.lidMapping.getLIDForPN.bind(signalRepository.lidMapping);
65
+ /** this mutex ensures that each retryRequest will wait for the previous one to finish */
66
+ const retryMutex = (0, _makeMutex.makeMutex)();
67
+ const msgRetryCache = config.msgRetryCounterCache || new _nodeCache.default({
68
+ stdTTL: _index2.DEFAULT_CACHE_TTLS.MSG_RETRY,
69
+ // 1 hour
70
+ useClones: false
71
+ });
72
+ const callOfferCache = config.callOfferCache || new _nodeCache.default({
73
+ stdTTL: _index2.DEFAULT_CACHE_TTLS.CALL_OFFER,
74
+ // 5 mins
75
+ useClones: false
76
+ });
77
+ // Debounce identity-change session refreshes per JID to avoid bursts
78
+ const identityAssertDebounce = new _nodeCache.default({
79
+ stdTTL: 5,
80
+ useClones: false
81
+ });
82
+ let sendActiveReceipts = false;
83
+ const fetchMessageHistory = async (count, oldestMsgKey, oldestMsgTimestamp) => {
84
+ if (!authState.creds.me?.id) {
85
+ throw new _boom.Boom('Not authenticated');
86
+ }
87
+ const pdoMessage = {
88
+ historySyncOnDemandRequest: {
89
+ chatJid: oldestMsgKey.remoteJid,
90
+ oldestMsgFromMe: oldestMsgKey.fromMe,
91
+ oldestMsgId: oldestMsgKey.id,
92
+ oldestMsgTimestampMs: oldestMsgTimestamp,
93
+ onDemandMsgCount: count
94
+ },
95
+ peerDataOperationRequestType: _index.proto.Message.PeerDataOperationRequestType.HISTORY_SYNC_ON_DEMAND
96
+ };
97
+ return sendPeerDataOperationMessage(pdoMessage);
98
+ };
99
+ const requestPlaceholderResend = async (messageKey, msgData) => {
100
+ if (!authState.creds.me?.id) {
101
+ throw new _boom.Boom('Not authenticated');
102
+ }
103
+ if (await placeholderResendCache.get(messageKey?.id)) {
104
+ logger.debug({
105
+ messageKey
106
+ }, 'already requested resend');
107
+ return;
108
+ } else {
109
+ // Store original message data so PDO response handler can preserve
110
+ // metadata (LID details, timestamps, etc.) that the phone may omit
111
+ await placeholderResendCache.set(messageKey?.id, msgData || true);
112
+ }
113
+ await (0, _index4.delay)(2000);
114
+ if (!(await placeholderResendCache.get(messageKey?.id))) {
115
+ logger.debug({
116
+ messageKey
117
+ }, 'message received while resend requested');
118
+ return 'RESOLVED';
119
+ }
120
+ const pdoMessage = {
121
+ placeholderMessageResendRequest: [{
122
+ messageKey
123
+ }],
124
+ peerDataOperationRequestType: _index.proto.Message.PeerDataOperationRequestType.PLACEHOLDER_MESSAGE_RESEND
125
+ };
126
+ setTimeout(async () => {
127
+ if (await placeholderResendCache.get(messageKey?.id)) {
128
+ logger.debug({
129
+ messageKey
130
+ }, 'PDO message without response after 8 seconds. Phone possibly offline');
131
+ await placeholderResendCache.del(messageKey?.id);
132
+ }
133
+ }, 8000);
134
+ return sendPeerDataOperationMessage(pdoMessage);
135
+ };
136
+ const handleMexNotification = async node => {
137
+ const updateNode = (0, _index5.getBinaryNodeChild)(node, 'update');
138
+ if (updateNode) {
139
+ const opName = updateNode.attrs?.op_name;
140
+ if (!opName) {
141
+ logger.warn({
142
+ node: (0, _index5.binaryNodeToString)(node)
143
+ }, 'mex notification missing op_name, fallback to legacy');
144
+ await handleLegacyMexNewsletterNotification(node);
145
+ return;
146
+ }
147
+ let mexResponse;
148
+ try {
149
+ mexResponse = JSON.parse(updateNode.content.toString());
150
+ } catch (error) {
151
+ logger.error({
152
+ err: error,
153
+ opName
154
+ }, 'failed to parse mex notification JSON');
155
+ return;
156
+ }
157
+ if (mexResponse.errors?.length) {
158
+ logger.warn({
159
+ errors: mexResponse.errors,
160
+ opName
161
+ }, 'mex notification has GQL errors');
162
+ return;
163
+ }
164
+ const data = mexResponse.data;
165
+ if (!data) {
166
+ logger.warn({
167
+ opName
168
+ }, 'mex notification has null data');
169
+ return;
170
+ }
171
+ logger.debug({
172
+ opName
173
+ }, 'processing mex notification');
174
+ switch (opName) {
175
+ case 'NotificationUserReachoutTimelockUpdate':
176
+ handleReachoutTimelockNotification(data);
177
+ break;
178
+ case 'MessageCappingInfoNotification':
179
+ handleMessageCappingNotification(data);
180
+ break;
181
+ // newsletter ops still use the legacy <mex> child structure
182
+ case 'NotificationNewsletterUpdate':
183
+ case 'NotificationLinkedProfilesUpdates':
184
+ case 'NotificationNewsletterAdminPromote':
185
+ case 'NotificationNewsletterAdminDemote':
186
+ case 'NotificationNewsletterUserSettingChange':
187
+ case 'NotificationNewsletterJoin':
188
+ case 'NotificationNewsletterLeave':
189
+ case 'NotificationNewsletterStateChange':
190
+ case 'NotificationNewsletterAdminMetadataUpdate':
191
+ case 'NotificationNewsletterOwnerUpdate':
192
+ case 'NotificationNewsletterAdminInviteRevoke':
193
+ case 'NotificationNewsletterWamoSubStatusChange':
194
+ case 'NotificationNewsletterBlockUser':
195
+ case 'NotificationNewsletterPaidPartnership':
196
+ case 'NotificationNewsletterMilestone':
197
+ case 'NewsletterResponseStateUpdate':
198
+ await handleLegacyMexNewsletterNotification(node);
199
+ break;
200
+ default:
201
+ logger.debug({
202
+ opName
203
+ }, 'unhandled mex notification');
204
+ break;
205
+ }
206
+ return;
207
+ }
208
+ await handleLegacyMexNewsletterNotification(node);
209
+ };
210
+ const handleReachoutTimelockNotification = data => {
211
+ const payload = data.xwa2_notify_account_reachout_timelock;
212
+ if (!payload) {
213
+ logger.warn('reachout timelock notification missing payload');
214
+ return;
215
+ }
216
+ if (!payload.is_active) {
217
+ logger.info('reachout timelock restriction lifted');
218
+ ev.emit('connection.update', {
219
+ reachoutTimeLock: {
220
+ isActive: false,
221
+ enforcementType: _index3.ReachoutTimelockEnforcementType.DEFAULT
222
+ }
223
+ });
224
+ return;
225
+ }
226
+ // WA Web defaults to now+60s when the server omits the expiry
227
+ const timeEnforcementEnds = payload.time_enforcement_ends ? new Date(parseInt(payload.time_enforcement_ends, 10) * 1000) : new Date(Date.now() + 60000);
228
+ const enforcementType = isValidEnforcementType(payload.enforcement_type) ? payload.enforcement_type : _index3.ReachoutTimelockEnforcementType.DEFAULT;
229
+ logger.info({
230
+ enforcementType,
231
+ timeEnforcementEnds
232
+ }, 'reachout timelock restriction set');
233
+ ev.emit('connection.update', {
234
+ reachoutTimeLock: {
235
+ isActive: true,
236
+ timeEnforcementEnds,
237
+ enforcementType
238
+ }
239
+ });
240
+ };
241
+ const handleMessageCappingNotification = data => {
242
+ const payload = data.xwa2_notify_new_chat_messages_capping_info_update;
243
+ if (!payload) {
244
+ logger.warn('message capping notification missing payload');
245
+ return;
246
+ }
247
+ logger.info({
248
+ payload
249
+ }, 'received message capping update');
250
+ ev.emit('message-capping.update', payload);
251
+ };
252
+ const handleLegacyMexNewsletterNotification = async node => {
253
+ const mexNode = (0, _index5.getBinaryNodeChild)(node, 'mex');
254
+ const updateNode = mexNode?.content ? null : (0, _index5.getBinaryNodeChild)(node, 'update') || (0, _index5.getAllBinaryNodeChildren)(node)[0];
255
+ const payloadNode = mexNode?.content ? mexNode : updateNode;
256
+ if (!payloadNode?.content) {
257
+ logger.warn({
258
+ node: (0, _index5.binaryNodeToString)(node)
259
+ }, 'invalid mex newsletter notification');
260
+ return;
261
+ }
262
+ let data;
263
+ try {
264
+ const payloadContent = payloadNode.content;
265
+ if (Array.isArray(payloadContent)) {
266
+ logger.warn({
267
+ payloadNode
268
+ }, 'invalid mex newsletter notification payload format');
269
+ return;
270
+ }
271
+ const contentBuf = typeof payloadContent === 'string' ? Buffer.from(payloadContent, 'binary') : Buffer.from(payloadContent);
272
+ data = JSON.parse(contentBuf.toString());
273
+ } catch (error) {
274
+ logger.error({
275
+ err: error,
276
+ node: (0, _index5.binaryNodeToString)(node)
277
+ }, 'failed to parse mex newsletter notification');
278
+ return;
279
+ }
280
+ const operation = data?.operation ?? payloadNode?.attrs?.op_name;
281
+ let updates = data?.updates;
282
+ if (!updates) {
283
+ const linkedProfiles = data?.data?.xwa2_notify_linked_profiles;
284
+ if (linkedProfiles) {
285
+ updates = [linkedProfiles];
286
+ }
287
+ }
288
+ if (!updates || !operation) {
289
+ logger.warn({
290
+ data
291
+ }, 'invalid mex newsletter notification content');
292
+ return;
293
+ }
294
+ logger.info({
295
+ operation,
296
+ updates
297
+ }, 'got mex newsletter notification');
298
+ switch (operation) {
299
+ case 'NotificationNewsletterUpdate':
300
+ for (const update of updates) {
301
+ if (update.jid && update.settings && Object.keys(update.settings).length > 0) {
302
+ ev.emit('newsletter-settings.update', {
303
+ id: update.jid,
304
+ update: update.settings
305
+ });
306
+ }
307
+ }
308
+ break;
309
+ case 'NotificationNewsletterAdminPromote':
310
+ for (const update of updates) {
311
+ if (update.jid && update.user) {
312
+ ev.emit('newsletter-participants.update', {
313
+ id: update.jid,
314
+ author: node.attrs.from,
315
+ user: update.user,
316
+ new_role: 'ADMIN',
317
+ action: 'promote'
318
+ });
319
+ }
320
+ }
321
+ break;
322
+ case 'NotificationLinkedProfilesUpdates':
323
+ for (const update of updates) {
324
+ const lid = update?.jid;
325
+ const addedProfiles = Array.isArray(update?.added_profiles) ? update.added_profiles : [];
326
+ const mappings = [];
327
+ for (const profile of addedProfiles) {
328
+ const pn = typeof profile === 'string' ? profile : profile?.pn ?? profile?.jid ?? null;
329
+ if (lid && pn) {
330
+ const mapping = {
331
+ lid,
332
+ pn
333
+ };
334
+ ev.emit('lid-mapping.update', mapping);
335
+ mappings.push(mapping);
336
+ }
337
+ }
338
+ await signalRepository.lidMapping.storeLIDPNMappings(mappings);
339
+ }
340
+ break;
341
+ default:
342
+ logger.info({
343
+ operation,
344
+ data
345
+ }, 'unhandled mex newsletter notification');
346
+ break;
347
+ }
348
+ };
349
+ // Handles newsletter notifications
350
+ const handleNewsletterNotification = async node => {
351
+ const from = node.attrs.from;
352
+ const children = (0, _index5.getAllBinaryNodeChildren)(node);
353
+ const author = node.attrs.participant;
354
+ for (const child of children) {
355
+ logger.debug({
356
+ from,
357
+ child
358
+ }, 'got newsletter notification');
359
+ switch (child.tag) {
360
+ case 'reaction':
361
+ {
362
+ const reactionUpdate = {
363
+ id: from,
364
+ server_id: child.attrs.message_id,
365
+ reaction: {
366
+ code: (0, _index5.getBinaryNodeChildString)(child, 'reaction'),
367
+ count: 1
368
+ }
369
+ };
370
+ ev.emit('newsletter.reaction', reactionUpdate);
371
+ break;
372
+ }
373
+ case 'view':
374
+ {
375
+ const viewUpdate = {
376
+ id: from,
377
+ server_id: child.attrs.message_id,
378
+ count: parseInt(child.content?.toString() || '0', 10)
379
+ };
380
+ ev.emit('newsletter.view', viewUpdate);
381
+ break;
382
+ }
383
+ case 'participant':
384
+ {
385
+ const participantUpdate = {
386
+ id: from,
387
+ author,
388
+ user: child.attrs.jid,
389
+ action: child.attrs.action,
390
+ new_role: child.attrs.role
391
+ };
392
+ ev.emit('newsletter-participants.update', participantUpdate);
393
+ break;
394
+ }
395
+ case 'update':
396
+ {
397
+ const settingsNode = (0, _index5.getBinaryNodeChild)(child, 'settings');
398
+ if (settingsNode) {
399
+ const update = {};
400
+ const nameNode = (0, _index5.getBinaryNodeChild)(settingsNode, 'name');
401
+ if (nameNode?.content) update.name = nameNode.content.toString();
402
+ const descriptionNode = (0, _index5.getBinaryNodeChild)(settingsNode, 'description');
403
+ if (descriptionNode?.content) update.description = descriptionNode.content.toString();
404
+ ev.emit('newsletter-settings.update', {
405
+ id: from,
406
+ update
407
+ });
408
+ }
409
+ break;
410
+ }
411
+ case 'message':
412
+ {
413
+ const plaintextNode = (0, _index5.getBinaryNodeChild)(child, 'plaintext');
414
+ if (plaintextNode?.content) {
415
+ try {
416
+ const contentBuf = typeof plaintextNode.content === 'string' ? Buffer.from(plaintextNode.content, 'binary') : Buffer.from(plaintextNode.content);
417
+ const messageProto = _index.proto.Message.decode(contentBuf).toJSON();
418
+ const fullMessage = _index.proto.WebMessageInfo.fromObject({
419
+ key: {
420
+ remoteJid: from,
421
+ id: child.attrs.message_id || child.attrs.server_id,
422
+ fromMe: false // TODO: is this really true though
423
+ },
424
+ message: messageProto,
425
+ messageTimestamp: +child.attrs.t
426
+ }).toJSON();
427
+ await upsertMessage(fullMessage, 'append');
428
+ logger.debug('Processed plaintext newsletter message');
429
+ } catch (error) {
430
+ logger.error({
431
+ error
432
+ }, 'Failed to decode plaintext newsletter message');
433
+ }
434
+ }
435
+ break;
436
+ }
437
+ default:
438
+ logger.warn({
439
+ node,
440
+ child
441
+ }, 'Unknown newsletter notification child');
442
+ break;
443
+ }
444
+ }
445
+ };
446
+ const sendMessageAck = async (node, errorCode) => {
447
+ const stanza = (0, _stanzaAck.buildAckStanza)(node, errorCode, authState.creds.me.id);
448
+ logger.debug({
449
+ recv: {
450
+ tag: node.tag,
451
+ attrs: node.attrs
452
+ },
453
+ sent: stanza.attrs
454
+ }, 'sent ack');
455
+ await sendNode(stanza);
456
+ };
457
+ const rejectCall = async (callId, callFrom) => {
458
+ const stanza = {
459
+ tag: 'call',
460
+ attrs: {
461
+ from: authState.creds.me.id,
462
+ to: callFrom
463
+ },
464
+ content: [{
465
+ tag: 'reject',
466
+ attrs: {
467
+ 'call-id': callId,
468
+ 'call-creator': callFrom,
469
+ count: '0'
470
+ },
471
+ content: undefined
472
+ }]
473
+ };
474
+ await query(stanza);
475
+ };
476
+ const sendRetryRequest = async (node, forceIncludeKeys = false) => {
477
+ const {
478
+ fullMessage
479
+ } = (0, _index4.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '');
480
+ const {
481
+ key: msgKey
482
+ } = fullMessage;
483
+ const msgId = msgKey.id;
484
+ if (messageRetryManager) {
485
+ // Check if we've exceeded max retries using the new system
486
+ if (messageRetryManager.hasExceededMaxRetries(msgId)) {
487
+ logger.debug({
488
+ msgId
489
+ }, 'reached retry limit with new retry manager, clearing');
490
+ messageRetryManager.markRetryFailed(msgId);
491
+ return;
492
+ }
493
+ // Increment retry count using new system
494
+ const retryCount = messageRetryManager.incrementRetryCount(msgId);
495
+ // Use the new retry count for the rest of the logic
496
+ const key = `${msgId}:${msgKey?.participant}`;
497
+ await msgRetryCache.set(key, retryCount);
498
+ } else {
499
+ // Fallback to old system
500
+ const key = `${msgId}:${msgKey?.participant}`;
501
+ let retryCount = (await msgRetryCache.get(key)) || 0;
502
+ if (retryCount >= maxMsgRetryCount) {
503
+ logger.debug({
504
+ retryCount,
505
+ msgId
506
+ }, 'reached retry limit, clearing');
507
+ await msgRetryCache.del(key);
508
+ return;
509
+ }
510
+ retryCount += 1;
511
+ await msgRetryCache.set(key, retryCount);
512
+ }
513
+ const key = `${msgId}:${msgKey?.participant}`;
514
+ const retryCount = (await msgRetryCache.get(key)) || 1;
515
+ const {
516
+ account,
517
+ signedPreKey,
518
+ signedIdentityKey: identityKey
519
+ } = authState.creds;
520
+ const fromJid = node.attrs.from;
521
+ // Check if we should recreate the session
522
+ let shouldRecreateSession = false;
523
+ let recreateReason = '';
524
+ if (enableAutoSessionRecreation && messageRetryManager && retryCount > 1) {
525
+ try {
526
+ // Check if we have a session with this JID
527
+ const sessionId = signalRepository.jidToSignalProtocolAddress(fromJid);
528
+ const hasSession = await signalRepository.validateSession(fromJid);
529
+ const result = messageRetryManager.shouldRecreateSession(fromJid, hasSession.exists);
530
+ shouldRecreateSession = result.recreate;
531
+ recreateReason = result.reason;
532
+ if (shouldRecreateSession) {
533
+ logger.debug({
534
+ fromJid,
535
+ retryCount,
536
+ reason: recreateReason
537
+ }, 'recreating session for retry');
538
+ // Delete existing session to force recreation
539
+ await authState.keys.set({
540
+ session: {
541
+ [sessionId]: null
542
+ }
543
+ });
544
+ forceIncludeKeys = true;
545
+ }
546
+ } catch (error) {
547
+ logger.warn({
548
+ error,
549
+ fromJid
550
+ }, 'failed to check session recreation');
551
+ }
552
+ }
553
+ if (retryCount <= 2) {
554
+ // Use new retry manager for phone requests if available
555
+ if (messageRetryManager) {
556
+ // Schedule phone request with delay (like whatsmeow)
557
+ messageRetryManager.schedulePhoneRequest(msgId, async () => {
558
+ try {
559
+ const requestId = await requestPlaceholderResend(msgKey);
560
+ logger.debug(`sendRetryRequest: requested placeholder resend (${requestId}) for message ${msgId} (scheduled)`);
561
+ } catch (error) {
562
+ logger.warn({
563
+ error,
564
+ msgId
565
+ }, 'failed to send scheduled phone request');
566
+ }
567
+ });
568
+ } else {
569
+ // Fallback to immediate request
570
+ const msgId = await requestPlaceholderResend(msgKey);
571
+ logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId}`);
572
+ }
573
+ }
574
+ const deviceIdentity = (0, _index4.encodeSignedDeviceIdentity)(account, true);
575
+ await authState.keys.transaction(async () => {
576
+ const receipt = {
577
+ tag: 'receipt',
578
+ attrs: {
579
+ id: msgId,
580
+ type: 'retry',
581
+ to: node.attrs.from
582
+ },
583
+ content: [{
584
+ tag: 'retry',
585
+ attrs: {
586
+ count: retryCount.toString(),
587
+ id: node.attrs.id,
588
+ t: node.attrs.t,
589
+ v: '1',
590
+ // ADD ERROR FIELD
591
+ error: '0'
592
+ }
593
+ }, {
594
+ tag: 'registration',
595
+ attrs: {},
596
+ content: (0, _index4.encodeBigEndian)(authState.creds.registrationId)
597
+ }]
598
+ };
599
+ if (node.attrs.recipient) {
600
+ receipt.attrs.recipient = node.attrs.recipient;
601
+ }
602
+ if (node.attrs.participant) {
603
+ receipt.attrs.participant = node.attrs.participant;
604
+ }
605
+ if (retryCount > 1 || forceIncludeKeys || shouldRecreateSession) {
606
+ const {
607
+ update,
608
+ preKeys
609
+ } = await (0, _index4.getNextPreKeys)(authState, 1);
610
+ const [keyId] = Object.keys(preKeys);
611
+ const key = preKeys[+keyId];
612
+ const content = receipt.content;
613
+ content.push({
614
+ tag: 'keys',
615
+ attrs: {},
616
+ content: [{
617
+ tag: 'type',
618
+ attrs: {},
619
+ content: Buffer.from(_index2.KEY_BUNDLE_TYPE)
620
+ }, {
621
+ tag: 'identity',
622
+ attrs: {},
623
+ content: identityKey.public
624
+ }, (0, _index4.xmppPreKey)(key, +keyId), (0, _index4.xmppSignedPreKey)(signedPreKey), {
625
+ tag: 'device-identity',
626
+ attrs: {},
627
+ content: deviceIdentity
628
+ }]
629
+ });
630
+ ev.emit('creds.update', update);
631
+ }
632
+ await sendNode(receipt);
633
+ logger.info({
634
+ msgAttrs: node.attrs,
635
+ retryCount
636
+ }, 'sent retry receipt');
637
+ }, authState?.creds?.me?.id || 'sendRetryRequest');
638
+ };
639
+ // Mirrors WAWeb/Handle/PreKeyLow.js: skip a re-issued notification with the same stanza id.
640
+ const inFlightPreKeyLow = new Set();
641
+ /**
642
+ * Fire-and-forget tctoken re-issuance after a peer's device identity changed.
643
+ * Mirrors WAWebSendTcTokenWhenDeviceIdentityChange — runs in parallel with
644
+ * the session refresh (not after it).
645
+ */
646
+ const reissueTcTokenAfterIdentityChange = from => {
647
+ void (async () => {
648
+ const normalizedJid = (0, _index5.jidNormalizedUser)(from);
649
+ const tcJid = await (0, _tcTokenUtils.resolveTcTokenJid)(normalizedJid, getLIDForPN);
650
+ const tcTokenData = await authState.keys.get('tctoken', [tcJid]);
651
+ const senderTs = tcTokenData?.[tcJid]?.senderTimestamp;
652
+ if (senderTs === null || senderTs === undefined || (0, _tcTokenUtils.isTcTokenExpired)(senderTs)) {
653
+ return;
654
+ }
655
+ logger.debug({
656
+ jid: normalizedJid,
657
+ senderTimestamp: senderTs
658
+ }, 'identity changed, re-issuing tctoken');
659
+ const getPNForLID = signalRepository.lidMapping.getPNForLID.bind(signalRepository.lidMapping);
660
+ const issueJid = await (0, _tcTokenUtils.resolveIssuanceJid)(normalizedJid, sock.serverProps.lidTrustedTokenIssueToLid, getLIDForPN, getPNForLID);
661
+ const result = await issuePrivacyTokens([issueJid], senderTs);
662
+ await (0, _tcTokenUtils.storeTcTokensFromIqResult)({
663
+ result,
664
+ fallbackJid: tcJid,
665
+ keys: authState.keys,
666
+ getLIDForPN,
667
+ onNewJidStored: trackTcTokenJid
668
+ });
669
+ })().catch(err => {
670
+ logger.debug({
671
+ jid: from,
672
+ err: err?.message
673
+ }, 'failed to re-issue tctoken after identity change');
674
+ });
675
+ };
676
+ const handleEncryptNotification = async node => {
677
+ const from = node.attrs.from;
678
+ if (from === _index5.S_WHATSAPP_NET) {
679
+ const stanzaId = node.attrs.id;
680
+ if (stanzaId && inFlightPreKeyLow.has(stanzaId)) {
681
+ return;
682
+ }
683
+ const countChild = (0, _index5.getBinaryNodeChild)(node, 'count');
684
+ const count = +countChild.attrs.value;
685
+ const shouldUploadMorePreKeys = count < _index2.MIN_PREKEY_COUNT;
686
+ logger.debug({
687
+ count,
688
+ shouldUploadMorePreKeys
689
+ }, 'recv pre-key count');
690
+ if (shouldUploadMorePreKeys) {
691
+ if (stanzaId) inFlightPreKeyLow.add(stanzaId);
692
+ try {
693
+ await uploadPreKeys();
694
+ } finally {
695
+ if (stanzaId) inFlightPreKeyLow.delete(stanzaId);
696
+ }
697
+ }
698
+ } else {
699
+ const result = await (0, _index4.handleIdentityChange)(node, {
700
+ meId: authState.creds.me?.id,
701
+ meLid: authState.creds.me?.lid,
702
+ validateSession: signalRepository.validateSession,
703
+ assertSessions,
704
+ debounceCache: identityAssertDebounce,
705
+ logger,
706
+ onBeforeSessionRefresh: reissueTcTokenAfterIdentityChange
707
+ });
708
+ if (result.action === 'no_identity_node') {
709
+ logger.info({
710
+ node
711
+ }, 'unknown encrypt notification');
712
+ }
713
+ }
714
+ };
715
+ const handleGroupNotification = (fullNode, child, msg) => {
716
+ // TODO: Support PN/LID (Here is only LID now)
717
+ const actingParticipantLid = fullNode.attrs.participant;
718
+ const actingParticipantPn = fullNode.attrs.participant_pn;
719
+ const actingParticipantUsername = fullNode.attrs.participant_username;
720
+ const affectedParticipantLid = (0, _index5.getBinaryNodeChild)(child, 'participant')?.attrs?.jid || actingParticipantLid;
721
+ const affectedParticipantPn = (0, _index5.getBinaryNodeChild)(child, 'participant')?.attrs?.phone_number || actingParticipantPn;
722
+ switch (child?.tag) {
723
+ case 'create':
724
+ const metadata = (0, _groups.extractGroupMetadata)(child);
725
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_CREATE;
726
+ msg.messageStubParameters = [metadata.subject];
727
+ msg.key = {
728
+ participant: metadata.owner,
729
+ participantAlt: metadata.ownerPn
730
+ };
731
+ ev.emit('chats.upsert', [{
732
+ id: metadata.id,
733
+ name: metadata.subject,
734
+ conversationTimestamp: metadata.creation
735
+ }]);
736
+ ev.emit('groups.upsert', [{
737
+ ...metadata,
738
+ author: actingParticipantLid,
739
+ authorPn: actingParticipantPn,
740
+ authorUsername: actingParticipantUsername
741
+ }]);
742
+ break;
743
+ case 'ephemeral':
744
+ case 'not_ephemeral':
745
+ msg.message = {
746
+ protocolMessage: {
747
+ type: _index.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
748
+ ephemeralExpiration: +(child.attrs.expiration || 0)
749
+ }
750
+ };
751
+ break;
752
+ case 'modify':
753
+ const oldNumber = (0, _index5.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
754
+ msg.messageStubParameters = oldNumber || [];
755
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER;
756
+ break;
757
+ case 'promote':
758
+ case 'demote':
759
+ case 'remove':
760
+ case 'add':
761
+ case 'leave':
762
+ const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;
763
+ msg.messageStubType = _index3.WAMessageStubType[stubType];
764
+ const participants = (0, _index5.getBinaryNodeChildren)(child, 'participant').map(({
765
+ attrs
766
+ }) => {
767
+ // TODO: Store LID MAPPINGS
768
+ return {
769
+ id: attrs.jid,
770
+ phoneNumber: (0, _index5.isLidUser)(attrs.jid) && (0, _index5.isPnUser)(attrs.phone_number) ? attrs.phone_number : undefined,
771
+ lid: (0, _index5.isPnUser)(attrs.jid) && (0, _index5.isLidUser)(attrs.lid) ? attrs.lid : undefined,
772
+ username: attrs.participant_username || attrs.username || undefined,
773
+ admin: attrs.type || null
774
+ };
775
+ });
776
+ if (participants.length === 1 && (
777
+ // if recv. "remove" message and sender removed themselves
778
+ // mark as left
779
+ (0, _index5.areJidsSameUser)(participants[0].id, actingParticipantLid) || (0, _index5.areJidsSameUser)(participants[0].id, actingParticipantPn)) && child.tag === 'remove') {
780
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;
781
+ }
782
+ msg.messageStubParameters = participants.map(a => JSON.stringify(a));
783
+ break;
784
+ case 'subject':
785
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_CHANGE_SUBJECT;
786
+ msg.messageStubParameters = [child.attrs.subject];
787
+ break;
788
+ case 'description':
789
+ const description = (0, _index5.getBinaryNodeChild)(child, 'body')?.content?.toString();
790
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_CHANGE_DESCRIPTION;
791
+ msg.messageStubParameters = description ? [description] : undefined;
792
+ break;
793
+ case 'announcement':
794
+ case 'not_announcement':
795
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;
796
+ msg.messageStubParameters = [child.tag === 'announcement' ? 'on' : 'off'];
797
+ break;
798
+ case 'locked':
799
+ case 'unlocked':
800
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_CHANGE_RESTRICT;
801
+ msg.messageStubParameters = [child.tag === 'locked' ? 'on' : 'off'];
802
+ break;
803
+ case 'invite':
804
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_CHANGE_INVITE_LINK;
805
+ msg.messageStubParameters = [child.attrs.code];
806
+ break;
807
+ case 'member_add_mode':
808
+ const addMode = child.content;
809
+ if (addMode) {
810
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_MEMBER_ADD_MODE;
811
+ msg.messageStubParameters = [addMode.toString()];
812
+ }
813
+ break;
814
+ case 'membership_approval_mode':
815
+ const approvalMode = (0, _index5.getBinaryNodeChild)(child, 'group_join');
816
+ if (approvalMode) {
817
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE;
818
+ msg.messageStubParameters = [approvalMode.attrs.state];
819
+ }
820
+ break;
821
+ case 'created_membership_requests':
822
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
823
+ msg.messageStubParameters = [JSON.stringify({
824
+ lid: affectedParticipantLid,
825
+ pn: affectedParticipantPn
826
+ }), 'created', child.attrs.request_method];
827
+ break;
828
+ case 'revoked_membership_requests':
829
+ const isDenied = (0, _index5.areJidsSameUser)(affectedParticipantLid, actingParticipantLid);
830
+ // TODO: LIDMAPPING SUPPORT
831
+ msg.messageStubType = _index3.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
832
+ msg.messageStubParameters = [JSON.stringify({
833
+ lid: affectedParticipantLid,
834
+ pn: affectedParticipantPn
835
+ }), isDenied ? 'revoked' : 'rejected'];
836
+ break;
837
+ }
838
+ };
839
+ const handleDevicesNotification = async node => {
840
+ const [child] = (0, _index5.getAllBinaryNodeChildren)(node);
841
+ const from = (0, _index5.jidNormalizedUser)(node.attrs.from);
842
+ if (!child) {
843
+ logger.debug({
844
+ from
845
+ }, 'devices notification missing child, skipping');
846
+ return;
847
+ }
848
+ const tag = child.tag;
849
+ const deviceHash = child.attrs.device_hash;
850
+ const devices = (0, _index5.getBinaryNodeChildren)(child, 'device');
851
+ if ((0, _index5.areJidsSameUser)(from, authState.creds.me.id) || (0, _index5.areJidsSameUser)(from, authState.creds.me.lid)) {
852
+ const deviceJids = devices.map(d => d.attrs.jid);
853
+ logger.info({
854
+ deviceJids
855
+ }, 'got my own devices');
856
+ }
857
+ if (!devices.length) {
858
+ logger.debug({
859
+ from,
860
+ tag
861
+ }, 'no devices in notification, skipping');
862
+ return;
863
+ }
864
+ const decoded = [];
865
+ for (const d of devices) {
866
+ const jid = d.attrs.jid;
867
+ if (!jid) continue;
868
+ const parts = (0, _index5.jidDecode)(jid);
869
+ if (!parts) {
870
+ logger.debug({
871
+ jid
872
+ }, 'failed to decode device jid, skipping');
873
+ continue;
874
+ }
875
+ decoded.push({
876
+ jid,
877
+ user: parts.user,
878
+ server: parts.server,
879
+ device: parts.device
880
+ });
881
+ }
882
+ if (!decoded.length) return;
883
+ await devicesMutex.mutex(async () => {
884
+ const byUser = new Map();
885
+ for (const d of decoded) {
886
+ const list = byUser.get(d.user) || [];
887
+ list.push(d);
888
+ byUser.set(d.user, list);
889
+ }
890
+ for (const [user, entries] of byUser) {
891
+ if (tag === 'update') {
892
+ logger.debug({
893
+ user
894
+ }, `${user}'s device list updated, dropping cached devices`);
895
+ await userDevicesCache?.del(user);
896
+ continue;
897
+ }
898
+ if (tag === 'remove') {
899
+ await signalRepository.deleteSession(entries.map(e => e.jid));
900
+ }
901
+ const existingCache = (await userDevicesCache?.get(user)) || [];
902
+ if (!existingCache.length) {
903
+ // No baseline yet; skip applying the delta so getUSyncDevices can
904
+ // later fetch the full device list. Caching just the notification
905
+ // entries would make a partial list look authoritative.
906
+ logger.debug({
907
+ user,
908
+ tag
909
+ }, 'device list not cached, deferring to USync refresh');
910
+ continue;
911
+ }
912
+ const affected = new Set(entries.map(e => e.device));
913
+ let updatedDevices;
914
+ switch (tag) {
915
+ case 'add':
916
+ logger.info({
917
+ deviceHash,
918
+ count: entries.length
919
+ }, 'devices added');
920
+ updatedDevices = [...existingCache.filter(d => !affected.has(d.device)), ...entries.map(e => ({
921
+ user: e.user,
922
+ server: e.server,
923
+ device: e.device
924
+ }))];
925
+ break;
926
+ case 'remove':
927
+ logger.info({
928
+ deviceHash,
929
+ count: entries.length
930
+ }, 'devices removed');
931
+ updatedDevices = existingCache.filter(d => !affected.has(d.device));
932
+ break;
933
+ default:
934
+ logger.debug({
935
+ tag
936
+ }, 'Unknown device list change tag');
937
+ continue;
938
+ }
939
+ if (updatedDevices.length === 0) {
940
+ await userDevicesCache?.del(user);
941
+ } else {
942
+ await userDevicesCache?.set(user, updatedDevices);
943
+ }
944
+ }
945
+ });
946
+ };
947
+ const processNotification = async node => {
948
+ const result = {};
949
+ const [child] = (0, _index5.getAllBinaryNodeChildren)(node);
950
+ const nodeType = node.attrs.type;
951
+ const from = (0, _index5.jidNormalizedUser)(node.attrs.from);
952
+ switch (nodeType) {
953
+ case 'newsletter':
954
+ await handleNewsletterNotification(node);
955
+ break;
956
+ case 'mex':
957
+ await handleMexNotification(node);
958
+ break;
959
+ case 'w:gp2':
960
+ // TODO: HANDLE PARTICIPANT_PN
961
+ handleGroupNotification(node, child, result);
962
+ break;
963
+ case 'mediaretry':
964
+ const event = (0, _index4.decodeMediaRetryNode)(node);
965
+ ev.emit('messages.media-update', [event]);
966
+ break;
967
+ case 'encrypt':
968
+ await handleEncryptNotification(node);
969
+ break;
970
+ case 'devices':
971
+ try {
972
+ await handleDevicesNotification(node);
973
+ } catch (error) {
974
+ logger.error({
975
+ error,
976
+ node
977
+ }, 'failed to handle devices notification');
978
+ }
979
+ break;
980
+ case 'server_sync':
981
+ const update = (0, _index5.getBinaryNodeChild)(node, 'collection');
982
+ if (update) {
983
+ const name = update.attrs.name;
984
+ await resyncAppState([name], false);
985
+ }
986
+ break;
987
+ case 'picture':
988
+ const setPicture = (0, _index5.getBinaryNodeChild)(node, 'set');
989
+ const delPicture = (0, _index5.getBinaryNodeChild)(node, 'delete');
990
+ // TODO: WAJIDHASH stuff proper support inhouse
991
+ ev.emit('contacts.update', [{
992
+ id: (0, _index5.jidNormalizedUser)(node?.attrs?.from) || (setPicture || delPicture)?.attrs?.hash || '',
993
+ imgUrl: setPicture ? 'changed' : 'removed'
994
+ }]);
995
+ if ((0, _index5.isJidGroup)(from)) {
996
+ const node = setPicture || delPicture;
997
+ result.messageStubType = _index3.WAMessageStubType.GROUP_CHANGE_ICON;
998
+ if (setPicture) {
999
+ result.messageStubParameters = [setPicture.attrs.id];
1000
+ }
1001
+ result.participant = node?.attrs.author;
1002
+ result.key = {
1003
+ ...(result.key || {}),
1004
+ participant: setPicture?.attrs.author
1005
+ };
1006
+ }
1007
+ break;
1008
+ case 'account_sync':
1009
+ if (child.tag === 'disappearing_mode') {
1010
+ const newDuration = +child.attrs.duration;
1011
+ const timestamp = +child.attrs.t;
1012
+ logger.info({
1013
+ newDuration
1014
+ }, 'updated account disappearing mode');
1015
+ ev.emit('creds.update', {
1016
+ accountSettings: {
1017
+ ...authState.creds.accountSettings,
1018
+ defaultDisappearingMode: {
1019
+ ephemeralExpiration: newDuration,
1020
+ ephemeralSettingTimestamp: timestamp
1021
+ }
1022
+ }
1023
+ });
1024
+ } else if (child.tag === 'blocklist') {
1025
+ const blocklists = (0, _index5.getBinaryNodeChildren)(child, 'item');
1026
+ for (const {
1027
+ attrs
1028
+ } of blocklists) {
1029
+ const blocklist = [attrs.jid];
1030
+ const type = attrs.action === 'block' ? 'add' : 'remove';
1031
+ ev.emit('blocklist.update', {
1032
+ blocklist,
1033
+ type
1034
+ });
1035
+ }
1036
+ }
1037
+ break;
1038
+ case 'link_code_companion_reg':
1039
+ const linkCodeCompanionReg = (0, _index5.getBinaryNodeChild)(node, 'link_code_companion_reg');
1040
+ const ref = toRequiredBuffer((0, _index5.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_ref'));
1041
+ const primaryIdentityPublicKey = toRequiredBuffer((0, _index5.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'primary_identity_pub'));
1042
+ const primaryEphemeralPublicKeyWrapped = toRequiredBuffer((0, _index5.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_wrapped_primary_ephemeral_pub'));
1043
+ const codePairingPublicKey = await decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped);
1044
+ const companionSharedKey = _index4.Curve.sharedKey(authState.creds.pairingEphemeralKeyPair.private, codePairingPublicKey);
1045
+ const random = (0, _crypto.randomBytes)(32);
1046
+ const linkCodeSalt = (0, _crypto.randomBytes)(32);
1047
+ const linkCodePairingExpanded = (0, _index4.hkdf)(companionSharedKey, 32, {
1048
+ salt: linkCodeSalt,
1049
+ info: 'link_code_pairing_key_bundle_encryption_key'
1050
+ });
1051
+ const encryptPayload = Buffer.concat([Buffer.from(authState.creds.signedIdentityKey.public), primaryIdentityPublicKey, random]);
1052
+ const encryptIv = (0, _crypto.randomBytes)(12);
1053
+ const encrypted = (0, _index4.aesEncryptGCM)(encryptPayload, linkCodePairingExpanded, encryptIv, Buffer.alloc(0));
1054
+ const encryptedPayload = Buffer.concat([linkCodeSalt, encryptIv, encrypted]);
1055
+ const identitySharedKey = _index4.Curve.sharedKey(authState.creds.signedIdentityKey.private, primaryIdentityPublicKey);
1056
+ const identityPayload = Buffer.concat([companionSharedKey, identitySharedKey, random]);
1057
+ authState.creds.advSecretKey = Buffer.from((0, _index4.hkdf)(identityPayload, 32, {
1058
+ info: 'adv_secret'
1059
+ })).toString('base64');
1060
+ await query({
1061
+ tag: 'iq',
1062
+ attrs: {
1063
+ to: _index5.S_WHATSAPP_NET,
1064
+ type: 'set',
1065
+ id: sock.generateMessageTag(),
1066
+ xmlns: 'md'
1067
+ },
1068
+ content: [{
1069
+ tag: 'link_code_companion_reg',
1070
+ attrs: {
1071
+ jid: authState.creds.me.id,
1072
+ stage: 'companion_finish'
1073
+ },
1074
+ content: [{
1075
+ tag: 'link_code_pairing_wrapped_key_bundle',
1076
+ attrs: {},
1077
+ content: encryptedPayload
1078
+ }, {
1079
+ tag: 'companion_identity_public',
1080
+ attrs: {},
1081
+ content: authState.creds.signedIdentityKey.public
1082
+ }, {
1083
+ tag: 'link_code_pairing_ref',
1084
+ attrs: {},
1085
+ content: ref
1086
+ }]
1087
+ }]
1088
+ });
1089
+ authState.creds.registered = true;
1090
+ ev.emit('creds.update', authState.creds);
1091
+ break;
1092
+ case 'privacy_token':
1093
+ await handlePrivacyTokenNotification(node);
1094
+ break;
1095
+ }
1096
+ if (Object.keys(result).length) {
1097
+ return result;
1098
+ }
1099
+ };
1100
+ /**
1101
+ * In-memory cache of storage JIDs with stored tctokens, seeded from the persisted index.
1102
+ * Used to coalesce writes during a session; pruning always re-reads the persisted index
1103
+ * to cover writes made by other layers (e.g. history sync).
1104
+ */
1105
+ const tcTokenKnownJids = new Set();
1106
+ const tcTokenIndexLoaded = (async () => {
1107
+ try {
1108
+ const jids = await (0, _tcTokenUtils.readTcTokenIndex)(authState.keys);
1109
+ for (const jid of jids) tcTokenKnownJids.add(jid);
1110
+ logger.debug({
1111
+ count: tcTokenKnownJids.size
1112
+ }, 'loaded tctoken index');
1113
+ } catch (err) {
1114
+ logger.warn({
1115
+ err: err?.message
1116
+ }, 'failed to load tctoken index');
1117
+ }
1118
+ })();
1119
+ let tcTokenIndexTimer;
1120
+ async function flushTcTokenIndex() {
1121
+ if (tcTokenIndexTimer) {
1122
+ clearTimeout(tcTokenIndexTimer);
1123
+ tcTokenIndexTimer = undefined;
1124
+ }
1125
+ // Merge with whatever is already persisted so we don't clobber writes from other
1126
+ // paths (history sync, concurrent sessions on the same store).
1127
+ const write = await (0, _tcTokenUtils.buildMergedTcTokenIndexWrite)(authState.keys, tcTokenKnownJids);
1128
+ return authState.keys.set({
1129
+ tctoken: write
1130
+ });
1131
+ }
1132
+ function scheduleTcTokenIndexSave() {
1133
+ if (tcTokenIndexTimer) {
1134
+ clearTimeout(tcTokenIndexTimer);
1135
+ }
1136
+ tcTokenIndexTimer = setTimeout(() => {
1137
+ tcTokenIndexTimer = undefined;
1138
+ flushTcTokenIndex().catch(err => {
1139
+ logger.warn({
1140
+ err: err?.message
1141
+ }, 'failed to save tctoken index');
1142
+ });
1143
+ }, 5000);
1144
+ }
1145
+ function trackTcTokenJid(jid) {
1146
+ if (jid && jid !== _tcTokenUtils.TC_TOKEN_INDEX_KEY && !tcTokenKnownJids.has(jid)) {
1147
+ tcTokenKnownJids.add(jid);
1148
+ scheduleTcTokenIndexSave();
1149
+ }
1150
+ }
1151
+ const handlePrivacyTokenNotification = async node => {
1152
+ const tokensNode = (0, _index5.getBinaryNodeChild)(node, 'tokens');
1153
+ if (!tokensNode) return;
1154
+ const from = (0, _index5.jidNormalizedUser)(node.attrs.from);
1155
+ // WA Web uses: senderLid ?? toLid(from) for the storage key
1156
+ // The sender_lid attribute provides the LID directly when available
1157
+ const senderLid = node.attrs.sender_lid && (0, _index5.isLidUser)((0, _index5.jidNormalizedUser)(node.attrs.sender_lid)) ? (0, _index5.jidNormalizedUser)(node.attrs.sender_lid) : undefined;
1158
+ const fallbackJid = senderLid ?? (await (0, _tcTokenUtils.resolveTcTokenJid)(from, getLIDForPN));
1159
+ logger.debug({
1160
+ from,
1161
+ storageJid: fallbackJid
1162
+ }, 'processing privacy token notification');
1163
+ await (0, _tcTokenUtils.storeTcTokensFromIqResult)({
1164
+ result: node,
1165
+ fallbackJid,
1166
+ keys: authState.keys,
1167
+ getLIDForPN,
1168
+ onNewJidStored: trackTcTokenJid
1169
+ });
1170
+ };
1171
+ async function decipherLinkPublicKey(data) {
1172
+ const buffer = toRequiredBuffer(data);
1173
+ const salt = buffer.slice(0, 32);
1174
+ const secretKey = await (0, _index4.derivePairingCodeKey)(authState.creds.pairingCode, salt);
1175
+ const iv = buffer.slice(32, 48);
1176
+ const payload = buffer.slice(48, 80);
1177
+ return (0, _index4.aesDecryptCTR)(payload, secretKey, iv);
1178
+ }
1179
+ function toRequiredBuffer(data) {
1180
+ if (data === undefined) {
1181
+ throw new _boom.Boom('Invalid buffer', {
1182
+ statusCode: 400
1183
+ });
1184
+ }
1185
+ return data instanceof Buffer ? data : Buffer.from(data);
1186
+ }
1187
+ const willSendMessageAgain = async (id, participant) => {
1188
+ const key = `${id}:${participant}`;
1189
+ const retryCount = (await msgRetryCache.get(key)) || 0;
1190
+ return retryCount < maxMsgRetryCount;
1191
+ };
1192
+ const updateSendMessageAgainCount = async (id, participant) => {
1193
+ const key = `${id}:${participant}`;
1194
+ const newValue = ((await msgRetryCache.get(key)) || 0) + 1;
1195
+ await msgRetryCache.set(key, newValue);
1196
+ };
1197
+ const sendMessagesAgain = async (key, ids, retryNode, receiptNode) => {
1198
+ const remoteJid = key.remoteJid;
1199
+ const participant = key.participant || remoteJid;
1200
+ const retryCount = +retryNode.attrs.count || 1;
1201
+ const msgId = ids[0];
1202
+ // Try to get messages from cache first, then fallback to getMessage
1203
+ const msgs = [];
1204
+ for (const id of ids) {
1205
+ let msg;
1206
+ // Try to get from retry cache first if enabled
1207
+ if (messageRetryManager) {
1208
+ const cachedMsg = messageRetryManager.getRecentMessage(remoteJid, id);
1209
+ if (cachedMsg) {
1210
+ msg = cachedMsg.message;
1211
+ logger.debug({
1212
+ jid: remoteJid,
1213
+ id
1214
+ }, 'found message in retry cache');
1215
+ // Mark retry as successful since we found the message
1216
+ messageRetryManager.markRetrySuccess(id);
1217
+ }
1218
+ }
1219
+ // Fallback to getMessage if not found in cache
1220
+ if (!msg) {
1221
+ msg = await getMessage({
1222
+ ...key,
1223
+ id
1224
+ });
1225
+ if (msg) {
1226
+ logger.debug({
1227
+ jid: remoteJid,
1228
+ id
1229
+ }, 'found message via getMessage');
1230
+ // Also mark as successful if found via getMessage
1231
+ if (messageRetryManager) {
1232
+ messageRetryManager.markRetrySuccess(id);
1233
+ }
1234
+ }
1235
+ }
1236
+ msgs.push(msg);
1237
+ }
1238
+ // if it's the primary jid sending the request
1239
+ // just re-send the message to everyone
1240
+ // prevents the first message decryption failure
1241
+ const sendToAll = !(0, _index5.jidDecode)(participant)?.device;
1242
+ const sessionId = signalRepository.jidToSignalProtocolAddress(participant);
1243
+ let injectedFromBundle = false;
1244
+ const bundle = (0, _index4.extractE2ESessionFromRetryReceipt)(receiptNode);
1245
+ if (bundle) {
1246
+ try {
1247
+ await signalRepository.injectE2ESession({
1248
+ jid: participant,
1249
+ session: bundle
1250
+ });
1251
+ injectedFromBundle = true;
1252
+ logger.debug({
1253
+ participant,
1254
+ retryCount
1255
+ }, 'injected session from retry receipt key bundle');
1256
+ } catch (error) {
1257
+ logger.warn({
1258
+ error,
1259
+ participant
1260
+ }, 'failed to inject session from retry receipt');
1261
+ }
1262
+ }
1263
+ if (!injectedFromBundle) {
1264
+ const receivedRegId = (0, _index5.getBinaryNodeChildUInt)(receiptNode, 'registration', 4);
1265
+ if (typeof receivedRegId === 'number' && Number.isInteger(receivedRegId)) {
1266
+ const info = await signalRepository.getSessionInfo(participant);
1267
+ if (info && info.registrationId !== 0 && info.registrationId !== receivedRegId) {
1268
+ logger.info({
1269
+ participant,
1270
+ stored: info.registrationId,
1271
+ received: receivedRegId
1272
+ }, 'reg id mismatch on retry without bundle, deleting session');
1273
+ await authState.keys.set({
1274
+ session: {
1275
+ [sessionId]: null
1276
+ }
1277
+ });
1278
+ }
1279
+ }
1280
+ }
1281
+ const BASE_KEY_CHECK_RETRY = 2;
1282
+ if (msgId && messageRetryManager) {
1283
+ const info = await signalRepository.getSessionInfo(participant);
1284
+ if (info) {
1285
+ if (retryCount === BASE_KEY_CHECK_RETRY) {
1286
+ messageRetryManager.saveBaseKey(sessionId, msgId, info.baseKey);
1287
+ } else if (retryCount > BASE_KEY_CHECK_RETRY) {
1288
+ if (messageRetryManager.hasSameBaseKey(sessionId, msgId, info.baseKey)) {
1289
+ logger.warn({
1290
+ participant,
1291
+ retryCount
1292
+ }, 'base key collision on retry, forcing fresh session');
1293
+ await authState.keys.set({
1294
+ session: {
1295
+ [sessionId]: null
1296
+ }
1297
+ });
1298
+ }
1299
+ messageRetryManager.deleteBaseKey(sessionId, msgId);
1300
+ }
1301
+ }
1302
+ }
1303
+ let shouldRecreateSession = false;
1304
+ let recreateReason = '';
1305
+ if (enableAutoSessionRecreation && messageRetryManager && retryCount > 1 && !injectedFromBundle) {
1306
+ try {
1307
+ const hasSession = await signalRepository.validateSession(participant);
1308
+ const result = messageRetryManager.shouldRecreateSession(participant, hasSession.exists);
1309
+ shouldRecreateSession = result.recreate;
1310
+ recreateReason = result.reason;
1311
+ if (shouldRecreateSession) {
1312
+ logger.debug({
1313
+ participant,
1314
+ retryCount,
1315
+ reason: recreateReason
1316
+ }, 'recreating session for outgoing retry');
1317
+ await authState.keys.set({
1318
+ session: {
1319
+ [sessionId]: null
1320
+ }
1321
+ });
1322
+ }
1323
+ } catch (error) {
1324
+ logger.warn({
1325
+ error,
1326
+ participant
1327
+ }, 'failed to check session recreation for outgoing retry');
1328
+ }
1329
+ }
1330
+ if (!injectedFromBundle) {
1331
+ await assertSessions([participant], true);
1332
+ }
1333
+ if ((0, _index5.isJidGroup)(remoteJid)) {
1334
+ await authState.keys.set({
1335
+ 'sender-key-memory': {
1336
+ [remoteJid]: null
1337
+ }
1338
+ });
1339
+ }
1340
+ logger.debug({
1341
+ participant,
1342
+ sendToAll,
1343
+ shouldRecreateSession,
1344
+ recreateReason,
1345
+ injectedFromBundle
1346
+ }, 'prepared session for retry resend');
1347
+ for (const [i, msg] of msgs.entries()) {
1348
+ if (!ids[i]) continue;
1349
+ if (msg && (await willSendMessageAgain(ids[i], participant))) {
1350
+ await updateSendMessageAgainCount(ids[i], participant);
1351
+ const msgRelayOpts = {
1352
+ messageId: ids[i]
1353
+ };
1354
+ if (sendToAll) {
1355
+ msgRelayOpts.useUserDevicesCache = false;
1356
+ } else {
1357
+ msgRelayOpts.participant = {
1358
+ jid: participant,
1359
+ count: +retryNode.attrs.count
1360
+ };
1361
+ }
1362
+ await relayMessage(key.remoteJid, msg, msgRelayOpts);
1363
+ } else {
1364
+ logger.debug({
1365
+ jid: key.remoteJid,
1366
+ id: ids[i]
1367
+ }, 'recv retry request, but message not available');
1368
+ }
1369
+ }
1370
+ };
1371
+ const handleReceipt = async node => {
1372
+ const {
1373
+ attrs,
1374
+ content
1375
+ } = node;
1376
+ const isLid = attrs.from.includes('lid');
1377
+ const isNodeFromMe = (0, _index5.areJidsSameUser)(attrs.participant || attrs.from, isLid ? authState.creds.me?.lid : authState.creds.me?.id);
1378
+ const remoteJid = !isNodeFromMe || (0, _index5.isJidGroup)(attrs.from) ? attrs.from : attrs.recipient;
1379
+ const fromMe = !attrs.recipient || (attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe;
1380
+ const key = {
1381
+ remoteJid,
1382
+ id: '',
1383
+ fromMe,
1384
+ participant: attrs.participant
1385
+ };
1386
+ const ids = [attrs.id];
1387
+ if (Array.isArray(content)) {
1388
+ const items = (0, _index5.getBinaryNodeChildren)(content[0], 'item');
1389
+ ids.push(...items.map(i => i.attrs.id));
1390
+ }
1391
+ try {
1392
+ await Promise.all([receiptMutex.mutex(async () => {
1393
+ const status = (0, _index4.getStatusFromReceiptType)(attrs.type);
1394
+ if (typeof status !== 'undefined' && (
1395
+ // basically, we only want to know when a message from us has been delivered to/read by the other person
1396
+ // or another device of ours has read some messages
1397
+ status >= _index.proto.WebMessageInfo.Status.SERVER_ACK || !isNodeFromMe)) {
1398
+ if ((0, _index5.isJidGroup)(remoteJid) || (0, _index5.isJidStatusBroadcast)(remoteJid)) {
1399
+ if (attrs.participant) {
1400
+ const updateKey = status === _index.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';
1401
+ ev.emit('message-receipt.update', ids.map(id => ({
1402
+ key: {
1403
+ ...key,
1404
+ id
1405
+ },
1406
+ receipt: {
1407
+ userJid: (0, _index5.jidNormalizedUser)(attrs.participant),
1408
+ [updateKey]: +attrs.t
1409
+ }
1410
+ })));
1411
+ }
1412
+ } else {
1413
+ ev.emit('messages.update', ids.map(id => ({
1414
+ key: {
1415
+ ...key,
1416
+ id
1417
+ },
1418
+ update: {
1419
+ status,
1420
+ messageTimestamp: (0, _index4.toNumber)(+(attrs.t ?? 0))
1421
+ }
1422
+ })));
1423
+ }
1424
+ }
1425
+ if (attrs.type === 'retry') {
1426
+ // correctly set who is asking for the retry
1427
+ key.participant = key.participant || attrs.from;
1428
+ const retryNode = (0, _index5.getBinaryNodeChild)(node, 'retry');
1429
+ if (ids[0] && key.participant && (await willSendMessageAgain(ids[0], key.participant))) {
1430
+ if (key.fromMe) {
1431
+ try {
1432
+ await updateSendMessageAgainCount(ids[0], key.participant);
1433
+ logger.debug({
1434
+ attrs,
1435
+ key
1436
+ }, 'recv retry request');
1437
+ await sendMessagesAgain(key, ids, retryNode, node);
1438
+ } catch (error) {
1439
+ logger.error({
1440
+ key,
1441
+ ids,
1442
+ trace: error instanceof Error ? error.stack : 'Unknown error'
1443
+ }, 'error in sending message again');
1444
+ }
1445
+ } else {
1446
+ logger.info({
1447
+ attrs,
1448
+ key
1449
+ }, 'recv retry for not fromMe message');
1450
+ }
1451
+ } else {
1452
+ logger.info({
1453
+ attrs,
1454
+ key
1455
+ }, 'will not send message again, as sent too many times');
1456
+ }
1457
+ }
1458
+ })]);
1459
+ } finally {
1460
+ await sendMessageAck(node).catch(ackErr => logger.error({
1461
+ ackErr
1462
+ }, 'failed to ack receipt'));
1463
+ }
1464
+ };
1465
+ const handleNotification = async node => {
1466
+ const remoteJid = node.attrs.from;
1467
+ try {
1468
+ await Promise.all([notificationMutex.mutex(async () => {
1469
+ const msg = await processNotification(node);
1470
+ if (msg) {
1471
+ const fromMe = (0, _index5.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);
1472
+ const {
1473
+ senderAlt: participantAlt,
1474
+ addressingMode
1475
+ } = (0, _index4.extractAddressingContext)(node);
1476
+ msg.key = {
1477
+ remoteJid,
1478
+ fromMe,
1479
+ participant: node.attrs.participant,
1480
+ participantAlt,
1481
+ participantUsername: node.attrs.participant_username,
1482
+ addressingMode,
1483
+ id: node.attrs.id,
1484
+ ...(msg.key || {})
1485
+ };
1486
+ msg.participant ?? (msg.participant = node.attrs.participant);
1487
+ msg.messageTimestamp = +node.attrs.t;
1488
+ const fullMsg = _index.proto.WebMessageInfo.fromObject(msg);
1489
+ await upsertMessage(fullMsg, 'append');
1490
+ }
1491
+ })]);
1492
+ } finally {
1493
+ await sendMessageAck(node).catch(ackErr => logger.error({
1494
+ ackErr
1495
+ }, 'failed to ack notification'));
1496
+ }
1497
+ };
1498
+ const handleMessage = async node => {
1499
+ const encNode = (0, _index5.getBinaryNodeChild)(node, 'enc');
1500
+ // TODO: temporary fix for crashes and issues resulting of failed msmsg decryption
1501
+ if (encNode?.attrs.type === 'msmsg') {
1502
+ logger.debug({
1503
+ key: node.attrs.key
1504
+ }, 'ignored msmsg');
1505
+ await sendMessageAck(node, _index4.NACK_REASONS.MissingMessageSecret);
1506
+ return;
1507
+ }
1508
+ let acked = false;
1509
+ try {
1510
+ const {
1511
+ fullMessage: msg,
1512
+ category,
1513
+ author,
1514
+ decrypt
1515
+ } = (0, _index4.decryptMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, logger);
1516
+ const alt = msg.key.participantAlt || msg.key.remoteJidAlt;
1517
+ // store new mappings we didn't have before
1518
+ if (!!alt) {
1519
+ const altServer = (0, _index5.jidDecode)(alt)?.server;
1520
+ const primaryJid = msg.key.participant || msg.key.remoteJid;
1521
+ if (altServer === 'lid') {
1522
+ if (!(await signalRepository.lidMapping.getPNForLID(alt))) {
1523
+ await signalRepository.lidMapping.storeLIDPNMappings([{
1524
+ lid: alt,
1525
+ pn: primaryJid
1526
+ }]);
1527
+ await signalRepository.migrateSession(primaryJid, alt);
1528
+ }
1529
+ } else {
1530
+ await signalRepository.lidMapping.storeLIDPNMappings([{
1531
+ lid: primaryJid,
1532
+ pn: alt
1533
+ }]);
1534
+ await signalRepository.migrateSession(alt, primaryJid);
1535
+ }
1536
+ }
1537
+ await messageMutex.mutex(async () => {
1538
+ await decrypt();
1539
+ if (msg.key?.remoteJid && msg.key?.id && msg.message && messageRetryManager) {
1540
+ messageRetryManager.addRecentMessage(msg.key.remoteJid, msg.key.id, msg.message);
1541
+ }
1542
+ // message failed to decrypt
1543
+ if (msg.messageStubType === _index.proto.WebMessageInfo.StubType.CIPHERTEXT && msg.category !== 'peer') {
1544
+ if (msg?.messageStubParameters?.[0] === _index4.MISSING_KEYS_ERROR_TEXT) {
1545
+ acked = true;
1546
+ return sendMessageAck(node, _index4.NACK_REASONS.ParsingError);
1547
+ }
1548
+ if (msg.messageStubParameters?.[0] === _index4.NO_MESSAGE_FOUND_ERROR_TEXT) {
1549
+ // Message arrived without encryption (e.g. CTWA ads messages).
1550
+ // Check if this is eligible for placeholder resend (matching WA Web filters).
1551
+ const unavailableNode = (0, _index5.getBinaryNodeChild)(node, 'unavailable');
1552
+ const unavailableType = unavailableNode?.attrs?.type;
1553
+ if (unavailableType === 'bot_unavailable_fanout' || unavailableType === 'hosted_unavailable_fanout' || unavailableType === 'view_once_unavailable_fanout') {
1554
+ logger.debug({
1555
+ msgId: msg.key.id,
1556
+ unavailableType
1557
+ }, 'skipping placeholder resend for excluded unavailable type');
1558
+ acked = true;
1559
+ return sendMessageAck(node);
1560
+ }
1561
+ const messageAge = (0, _index4.unixTimestampSeconds)() - (0, _index4.toNumber)(msg.messageTimestamp);
1562
+ if (messageAge > _index2.PLACEHOLDER_MAX_AGE_SECONDS) {
1563
+ logger.debug({
1564
+ msgId: msg.key.id,
1565
+ messageAge
1566
+ }, 'skipping placeholder resend for old message');
1567
+ acked = true;
1568
+ return sendMessageAck(node);
1569
+ }
1570
+ // Request the real content from the phone via placeholder resend PDO.
1571
+ // Upsert the CIPHERTEXT stub as a placeholder (like WA Web's processPlaceholderMsg),
1572
+ // and store the requestId in stubParameters[1] so users can correlate
1573
+ // with the incoming PDO response event.
1574
+ const cleanKey = {
1575
+ remoteJid: msg.key.remoteJid,
1576
+ fromMe: msg.key.fromMe,
1577
+ id: msg.key.id,
1578
+ participant: msg.key.participant
1579
+ };
1580
+ // Cache the original message metadata so the PDO response handler
1581
+ // can preserve key fields (LID details etc.) that the phone may omit
1582
+ const msgData = {
1583
+ key: msg.key,
1584
+ messageTimestamp: msg.messageTimestamp,
1585
+ pushName: msg.pushName,
1586
+ participant: msg.participant,
1587
+ verifiedBizName: msg.verifiedBizName
1588
+ };
1589
+ requestPlaceholderResend(cleanKey, msgData).then(requestId => {
1590
+ if (requestId && requestId !== 'RESOLVED') {
1591
+ logger.debug({
1592
+ msgId: msg.key.id,
1593
+ requestId
1594
+ }, 'requested placeholder resend for unavailable message');
1595
+ ev.emit('messages.update', [{
1596
+ key: msg.key,
1597
+ update: {
1598
+ messageStubParameters: [_index4.NO_MESSAGE_FOUND_ERROR_TEXT, requestId]
1599
+ }
1600
+ }]);
1601
+ }
1602
+ }).catch(err => {
1603
+ logger.warn({
1604
+ err,
1605
+ msgId: msg.key.id
1606
+ }, 'failed to request placeholder resend for unavailable message');
1607
+ });
1608
+ acked = true;
1609
+ await sendMessageAck(node);
1610
+ // Don't return — fall through to upsertMessage so the stub is emitted
1611
+ } else {
1612
+ // Skip retry for expired status messages (>24h old)
1613
+ if ((0, _index5.isJidStatusBroadcast)(msg.key.remoteJid)) {
1614
+ const messageAge = (0, _index4.unixTimestampSeconds)() - (0, _index4.toNumber)(msg.messageTimestamp);
1615
+ if (messageAge > _index2.STATUS_EXPIRY_SECONDS) {
1616
+ logger.debug({
1617
+ msgId: msg.key.id,
1618
+ messageAge,
1619
+ remoteJid: msg.key.remoteJid
1620
+ }, 'skipping retry for expired status message');
1621
+ acked = true;
1622
+ return sendMessageAck(node);
1623
+ }
1624
+ }
1625
+ logger.debug('[handleMessage] Attempting retry request for failed decryption');
1626
+ // WAWeb only retry-receipts here; server emits PreKeyLow if prekeys run low.
1627
+ await retryMutex.mutex(async () => {
1628
+ try {
1629
+ if (!ws.isOpen) {
1630
+ logger.debug({
1631
+ node
1632
+ }, 'Connection closed, skipping retry');
1633
+ return;
1634
+ }
1635
+ const encNode = (0, _index5.getBinaryNodeChild)(node, 'enc');
1636
+ await sendRetryRequest(node, !encNode);
1637
+ if (retryRequestDelayMs) {
1638
+ await (0, _index4.delay)(retryRequestDelayMs);
1639
+ }
1640
+ } catch (err) {
1641
+ logger.error({
1642
+ err
1643
+ }, 'Failed to send retry');
1644
+ }
1645
+ acked = true;
1646
+ await sendMessageAck(node, _index4.NACK_REASONS.UnhandledError);
1647
+ });
1648
+ }
1649
+ } else {
1650
+ if (messageRetryManager && msg.key.id) {
1651
+ messageRetryManager.cancelPendingPhoneRequest(msg.key.id);
1652
+ }
1653
+ const isNewsletter = (0, _index5.isJidNewsletter)(msg.key.remoteJid);
1654
+ if (!isNewsletter) {
1655
+ // no type in the receipt => message delivered
1656
+ let type = undefined;
1657
+ let participant = msg.key.participant;
1658
+ if (category === 'peer') {
1659
+ // special peer message
1660
+ type = 'peer_msg';
1661
+ } else if (msg.key.fromMe) {
1662
+ // message was sent by us from a different device
1663
+ type = 'sender';
1664
+ // need to specially handle this case
1665
+ if ((0, _index5.isLidUser)(msg.key.remoteJid) || (0, _index5.isLidUser)(msg.key.remoteJidAlt)) {
1666
+ participant = author; // TODO: investigate sending receipts to LIDs and not PNs
1667
+ }
1668
+ } else if (!sendActiveReceipts) {
1669
+ type = 'inactive';
1670
+ }
1671
+ acked = true;
1672
+ await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
1673
+ // send ack for history message
1674
+ const isAnyHistoryMsg = (0, _index4.getHistoryMsg)(msg.message);
1675
+ if (isAnyHistoryMsg) {
1676
+ const jid = (0, _index5.jidNormalizedUser)(msg.key.remoteJid);
1677
+ await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync'); // TODO: investigate
1678
+ }
1679
+ } else {
1680
+ acked = true;
1681
+ await sendMessageAck(node);
1682
+ logger.debug({
1683
+ key: msg.key
1684
+ }, 'processed newsletter message without receipts');
1685
+ }
1686
+ }
1687
+ (0, _index4.cleanMessage)(msg, authState.creds.me.id, authState.creds.me.lid);
1688
+ await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
1689
+ });
1690
+ } catch (error) {
1691
+ logger.error({
1692
+ error,
1693
+ node: (0, _index5.binaryNodeToString)(node)
1694
+ }, 'error in handling message');
1695
+ if (!acked) {
1696
+ await sendMessageAck(node, _index4.NACK_REASONS.UnhandledError).catch(ackErr => logger.error({
1697
+ ackErr
1698
+ }, 'failed to ack message after error'));
1699
+ }
1700
+ }
1701
+ };
1702
+ const handleCall = async node => {
1703
+ try {
1704
+ const {
1705
+ attrs
1706
+ } = node;
1707
+ const [infoChild] = (0, _index5.getAllBinaryNodeChildren)(node);
1708
+ if (!infoChild) {
1709
+ throw new _boom.Boom('Missing call info in call node');
1710
+ }
1711
+ const status = (0, _index4.getCallStatusFromNode)(infoChild);
1712
+ const callId = infoChild.attrs['call-id'];
1713
+ const from = infoChild.attrs.from || infoChild.attrs['call-creator'];
1714
+ const call = {
1715
+ chatId: attrs.from,
1716
+ from,
1717
+ callerPn: infoChild.attrs['caller_pn'],
1718
+ id: callId,
1719
+ date: new Date(+attrs.t * 1000),
1720
+ offline: !!attrs.offline,
1721
+ status
1722
+ };
1723
+ if (status === 'relaylatency') {
1724
+ const latencyValue = infoChild.attrs.latency || infoChild.attrs['latency_ms'] || infoChild.attrs['latency-ms'];
1725
+ const latencyMs = latencyValue ? Number(latencyValue) : undefined;
1726
+ if (Number.isFinite(latencyMs)) {
1727
+ call.latencyMs = latencyMs;
1728
+ }
1729
+ }
1730
+ if (status === 'offer') {
1731
+ call.isVideo = !!(0, _index5.getBinaryNodeChild)(infoChild, 'video');
1732
+ call.isGroup = infoChild.attrs.type === 'group' || !!infoChild.attrs['group-jid'];
1733
+ call.groupJid = infoChild.attrs['group-jid'];
1734
+ await callOfferCache.set(call.id, call);
1735
+ }
1736
+ const existingCall = await callOfferCache.get(call.id);
1737
+ // use existing call info to populate this event
1738
+ if (existingCall) {
1739
+ call.isVideo = existingCall.isVideo;
1740
+ call.isGroup = existingCall.isGroup;
1741
+ call.callerPn = call.callerPn || existingCall.callerPn;
1742
+ }
1743
+ // delete data once call has ended
1744
+ if (status === 'reject' || status === 'accept' || status === 'timeout' || status === 'terminate') {
1745
+ await callOfferCache.del(call.id);
1746
+ }
1747
+ ev.emit('call', [call]);
1748
+ } catch (error) {
1749
+ logger.error({
1750
+ error,
1751
+ node: (0, _index5.binaryNodeToString)(node)
1752
+ }, 'error in handling call');
1753
+ } finally {
1754
+ await sendMessageAck(node).catch(ackErr => logger.error({
1755
+ ackErr
1756
+ }, 'failed to ack call'));
1757
+ }
1758
+ };
1759
+ const handleBadAck = async ({
1760
+ attrs
1761
+ }) => {
1762
+ const key = {
1763
+ remoteJid: attrs.from,
1764
+ fromMe: true,
1765
+ id: attrs.id
1766
+ };
1767
+ // WARNING: REFRAIN FROM ENABLING THIS FOR NOW. IT WILL CAUSE A LOOP
1768
+ // // current hypothesis is that if pash is sent in the ack
1769
+ // // it means -- the message hasn't reached all devices yet
1770
+ // // we'll retry sending the message here
1771
+ // if(attrs.phash) {
1772
+ // logger.info({ attrs }, 'received phash in ack, resending message...')
1773
+ // const msg = await getMessage(key)
1774
+ // if(msg) {
1775
+ // await relayMessage(key.remoteJid!, msg, { messageId: key.id!, useUserDevicesCache: false })
1776
+ // } else {
1777
+ // logger.warn({ attrs }, 'could not send message again, as it was not found')
1778
+ // }
1779
+ // }
1780
+ // error in acknowledgement,
1781
+ // device could not display the message
1782
+ if (attrs.error) {
1783
+ const isReachoutTimelocked = attrs.error === String(_index4.NACK_REASONS.SenderReachoutTimelocked);
1784
+ if (attrs.error === _index4.SERVER_ERROR_CODES.MessageAccountRestriction) {
1785
+ // 463 = 1:1 message missing privacy token (tctoken). Usually means the
1786
+ // account is restricted: WhatsApp blocks starting new chats but preserves
1787
+ // existing ones, since established chats already carry a tctoken.
1788
+ // WA Web prevents this client-side (disables the compose bar).
1789
+ // No retry — retrying counts as another "reach out" and worsens the restriction.
1790
+ logger.warn({
1791
+ msgId: attrs.id,
1792
+ from: attrs.from
1793
+ }, 'error 463: account restricted or missing tctoken for contact');
1794
+ const ackFrom = attrs.from;
1795
+ if (ackFrom && !inFlight463Recoveries.has(ackFrom)) {
1796
+ inFlight463Recoveries.add(ackFrom);
1797
+ void (async () => {
1798
+ try {
1799
+ const getPNForLID = signalRepository.lidMapping.getPNForLID.bind(signalRepository.lidMapping);
1800
+ const tcStorageJid = await (0, _tcTokenUtils.resolveTcTokenJid)(ackFrom, getLIDForPN);
1801
+ const issueJid = await (0, _tcTokenUtils.resolveIssuanceJid)(ackFrom, sock.serverProps.lidTrustedTokenIssueToLid, getLIDForPN, getPNForLID);
1802
+ const result = await issuePrivacyTokens([issueJid], (0, _index4.unixTimestampSeconds)());
1803
+ await (0, _tcTokenUtils.storeTcTokensFromIqResult)({
1804
+ result,
1805
+ fallbackJid: tcStorageJid,
1806
+ keys: authState.keys,
1807
+ getLIDForPN,
1808
+ onNewJidStored: trackTcTokenJid
1809
+ });
1810
+ logger.debug({
1811
+ from: ackFrom
1812
+ }, 'completed 463 token recovery issuance');
1813
+ } catch (err) {
1814
+ logger.debug({
1815
+ from: ackFrom,
1816
+ err: err?.message
1817
+ }, 'failed 463 token recovery issuance');
1818
+ } finally {
1819
+ inFlight463Recoveries.delete(ackFrom);
1820
+ }
1821
+ })();
1822
+ }
1823
+ } else if (attrs.error === _index4.SERVER_ERROR_CODES.SmaxInvalid) {
1824
+ logger.warn({
1825
+ msgId: attrs.id,
1826
+ from: attrs.from
1827
+ }, 'smax-invalid (479): stanza rejected by server — likely stale device session or malformed addressing');
1828
+ } else if (isReachoutTimelocked) {
1829
+ // user is temporarily restricted, fetch current restriction details
1830
+ await fetchAccountReachoutTimelock().catch(err => logger.warn({
1831
+ err
1832
+ }, 'failed to fetch reachout timelock'));
1833
+ logger.warn({
1834
+ attrs
1835
+ }, 'received error in ack');
1836
+ } else {
1837
+ logger.warn({
1838
+ attrs
1839
+ }, 'received error in ack');
1840
+ }
1841
+ ev.emit('messages.update', [{
1842
+ key,
1843
+ update: {
1844
+ status: _index3.WAMessageStatus.ERROR,
1845
+ messageStubParameters: isReachoutTimelocked ? [attrs.error, _index4.ACCOUNT_RESTRICTED_TEXT] : [attrs.error]
1846
+ }
1847
+ }]);
1848
+ }
1849
+ };
1850
+ /// processes a node with the given function
1851
+ /// and adds the task to the existing buffer if we're buffering events
1852
+ const processNodeWithBuffer = async (node, identifier, exec) => {
1853
+ ev.buffer();
1854
+ await execTask();
1855
+ ev.flush();
1856
+ function execTask() {
1857
+ return exec(node, false).catch(err => onUnexpectedError(err, identifier));
1858
+ }
1859
+ };
1860
+ const offlineNodeProcessor = (0, _offlineNodeProcessor.makeOfflineNodeProcessor)(new Map([['message', handleMessage], ['call', handleCall], ['receipt', handleReceipt], ['notification', handleNotification]]), {
1861
+ isWsOpen: () => ws.isOpen,
1862
+ onUnexpectedError,
1863
+ yieldToEventLoop: () => new Promise(resolve => setImmediate(resolve))
1864
+ });
1865
+ const processNode = async (type, node, identifier, exec) => {
1866
+ // Fast path: ack and drop ignored JIDs before entering the buffer/queue
1867
+ const from = node.attrs.from;
1868
+ let ignoreJid = from;
1869
+ if (type === 'receipt' && from) {
1870
+ const attrs = node.attrs;
1871
+ const isLid = attrs.from.includes('lid');
1872
+ const isNodeFromMe = (0, _index5.areJidsSameUser)(attrs.participant || attrs.from, isLid ? authState.creds.me?.lid : authState.creds.me?.id);
1873
+ ignoreJid = !isNodeFromMe || (0, _index5.isJidGroup)(attrs.from) ? attrs.from : attrs.recipient;
1874
+ }
1875
+ if (ignoreJid && ignoreJid !== _index5.S_WHATSAPP_NET && shouldIgnoreJid(ignoreJid)) {
1876
+ await sendMessageAck(node, type === 'message' ? _index4.NACK_REASONS.UnhandledError : undefined);
1877
+ return;
1878
+ }
1879
+ const isOffline = !!node.attrs.offline;
1880
+ if (isOffline) {
1881
+ offlineNodeProcessor.enqueue(type, node);
1882
+ } else {
1883
+ await processNodeWithBuffer(node, identifier, exec);
1884
+ }
1885
+ };
1886
+ // recv a message
1887
+ ws.on('CB:message', async node => {
1888
+ await processNode('message', node, 'processing message', handleMessage);
1889
+ });
1890
+ ws.on('CB:call', async node => {
1891
+ await processNode('call', node, 'handling call', handleCall);
1892
+ });
1893
+ ws.on('CB:receipt', async node => {
1894
+ await processNode('receipt', node, 'handling receipt', handleReceipt);
1895
+ });
1896
+ ws.on('CB:notification', async node => {
1897
+ await processNode('notification', node, 'handling notification', handleNotification);
1898
+ });
1899
+ ws.on('CB:ack,class:message', node => {
1900
+ handleBadAck(node).catch(error => onUnexpectedError(error, 'handling bad ack'));
1901
+ });
1902
+ ev.on('call', async ([call]) => {
1903
+ if (!call) {
1904
+ return;
1905
+ }
1906
+ // missed call + group call notification message generation
1907
+ if (call.status === 'timeout' || call.status === 'offer' && call.isGroup) {
1908
+ const msg = {
1909
+ key: {
1910
+ remoteJid: call.chatId,
1911
+ id: call.id,
1912
+ fromMe: false
1913
+ },
1914
+ messageTimestamp: (0, _index4.unixTimestampSeconds)(call.date)
1915
+ };
1916
+ if (call.status === 'timeout') {
1917
+ if (call.isGroup) {
1918
+ msg.messageStubType = call.isVideo ? _index3.WAMessageStubType.CALL_MISSED_GROUP_VIDEO : _index3.WAMessageStubType.CALL_MISSED_GROUP_VOICE;
1919
+ } else {
1920
+ msg.messageStubType = call.isVideo ? _index3.WAMessageStubType.CALL_MISSED_VIDEO : _index3.WAMessageStubType.CALL_MISSED_VOICE;
1921
+ }
1922
+ } else {
1923
+ msg.message = {
1924
+ call: {
1925
+ callKey: Buffer.from(call.id)
1926
+ }
1927
+ };
1928
+ }
1929
+ const protoMsg = _index.proto.WebMessageInfo.fromObject(msg);
1930
+ await upsertMessage(protoMsg, call.offline ? 'append' : 'notify');
1931
+ }
1932
+ });
1933
+ /** timestamp of last tctoken prune run — throttles to once per 24h */
1934
+ let lastTcTokenPruneTs = 0;
1935
+ /** dedupe in-flight 463 recovery token issuance by target JID */
1936
+ const inFlight463Recoveries = new Set();
1937
+ ev.on('connection.update', ({
1938
+ isOnline,
1939
+ connection
1940
+ }) => {
1941
+ if (typeof isOnline !== 'undefined') {
1942
+ sendActiveReceipts = isOnline;
1943
+ logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`);
1944
+ }
1945
+ // Flush pending tctoken index save on disconnect to avoid writing after close
1946
+ if (connection === 'close' && tcTokenIndexTimer) {
1947
+ clearTimeout(tcTokenIndexTimer);
1948
+ tcTokenIndexTimer = undefined;
1949
+ // Best-effort flush — may fail if store is already closed
1950
+ try {
1951
+ void Promise.resolve(flushTcTokenIndex()).catch(() => {});
1952
+ } catch {
1953
+ /* ignore sync errors */
1954
+ }
1955
+ }
1956
+ // Prune expired tctokens when coming online, at most once per 24 hours
1957
+ // Matches WA Web's CLEAN_TC_TOKENS task
1958
+ // Note: don't gate on tcTokenKnownJids.size — the index may still be loading
1959
+ if (isOnline) {
1960
+ const now = Date.now();
1961
+ const DAY_MS = 24 * 60 * 60 * 1000;
1962
+ if (now - lastTcTokenPruneTs >= DAY_MS) {
1963
+ lastTcTokenPruneTs = now;
1964
+ void pruneExpiredTcTokens();
1965
+ }
1966
+ }
1967
+ });
1968
+ registerSocketEndHandler(() => {
1969
+ if (!config.msgRetryCounterCache && msgRetryCache.close) {
1970
+ msgRetryCache.close();
1971
+ }
1972
+ if (!config.callOfferCache && callOfferCache.close) {
1973
+ callOfferCache.close();
1974
+ }
1975
+ identityAssertDebounce.close();
1976
+ sendActiveReceipts = false;
1977
+ });
1978
+ async function pruneExpiredTcTokens() {
1979
+ try {
1980
+ await tcTokenIndexLoaded;
1981
+ // Union with the persisted index picks up JIDs added by other layers
1982
+ // (history sync) without needing inter-module wiring.
1983
+ const persisted = await (0, _tcTokenUtils.readTcTokenIndex)(authState.keys);
1984
+ const allJids = new Set(tcTokenKnownJids);
1985
+ for (const jid of persisted) allJids.add(jid);
1986
+ if (!allJids.size) return;
1987
+ const jids = [...allJids];
1988
+ const allTokens = await authState.keys.get('tctoken', jids);
1989
+ const writes = {};
1990
+ const survivors = new Set();
1991
+ let mutated = 0;
1992
+ for (const jid of jids) {
1993
+ const entry = allTokens[jid];
1994
+ if (!entry) {
1995
+ // Tracked but nothing in store — drop from index.
1996
+ mutated++;
1997
+ continue;
1998
+ }
1999
+ const hasPeerToken = !!entry.token?.length;
2000
+ const peerTokenExpired = hasPeerToken && (0, _tcTokenUtils.isTcTokenExpired)(entry.timestamp);
2001
+ const hasSenderTs = entry.senderTimestamp !== undefined;
2002
+ const senderTsExpired = hasSenderTs && (0, _tcTokenUtils.isTcTokenExpired)(entry.senderTimestamp);
2003
+ const keepPeerToken = hasPeerToken && !peerTokenExpired;
2004
+ const keepSenderTs = hasSenderTs && !senderTsExpired;
2005
+ if (!keepPeerToken && !keepSenderTs) {
2006
+ writes[jid] = null;
2007
+ mutated++;
2008
+ } else if (peerTokenExpired && keepSenderTs) {
2009
+ writes[jid] = {
2010
+ token: Buffer.alloc(0),
2011
+ senderTimestamp: entry.senderTimestamp
2012
+ };
2013
+ survivors.add(jid);
2014
+ mutated++;
2015
+ } else {
2016
+ survivors.add(jid);
2017
+ }
2018
+ }
2019
+ if (mutated === 0) return;
2020
+ await authState.keys.set({
2021
+ tctoken: {
2022
+ ...writes,
2023
+ [_tcTokenUtils.TC_TOKEN_INDEX_KEY]: {
2024
+ token: Buffer.from(JSON.stringify([...survivors]))
2025
+ }
2026
+ }
2027
+ });
2028
+ tcTokenKnownJids.clear();
2029
+ for (const jid of survivors) tcTokenKnownJids.add(jid);
2030
+ logger.debug({
2031
+ mutated,
2032
+ remaining: survivors.size
2033
+ }, 'pruned expired tctokens');
2034
+ } catch (err) {
2035
+ logger.warn({
2036
+ err: err?.message
2037
+ }, 'failed to prune expired tctokens');
2038
+ }
2039
+ }
2040
+ const sendText = async (jid, text, options = {}, quoted = null) => sendMessage(jid, {
2041
+ text,
2042
+ ...options
2043
+ }, {
2044
+ quoted
2045
+ });
2046
+ const sendImage = async (jid, image, caption, options = {}, quoted = null) => sendMessage(jid, {
2047
+ image,
2048
+ caption,
2049
+ ...options
2050
+ }, {
2051
+ quoted
2052
+ });
2053
+ const sendVideo = async (jid, video, caption, options = {}, quoted = null) => sendMessage(jid, {
2054
+ video,
2055
+ caption,
2056
+ ...options
2057
+ }, {
2058
+ quoted
2059
+ });
2060
+ const sendDocument = async (jid, document, fileName, caption, options = {}, quoted = null) => sendMessage(jid, {
2061
+ document,
2062
+ fileName,
2063
+ caption,
2064
+ ...options
2065
+ }, {
2066
+ quoted
2067
+ });
2068
+ const sendAudio = async (jid, audio, options = {}, quoted = null) => sendMessage(jid, {
2069
+ audio,
2070
+ ...options
2071
+ }, {
2072
+ quoted
2073
+ });
2074
+ const sendLocation = async (jid, name, degreesLongitude, degreesLatitude, url, address, options = {}, quoted = null) => sendMessage(jid, {
2075
+ location: {
2076
+ degreesLongitude,
2077
+ degreesLatitude,
2078
+ name,
2079
+ url,
2080
+ address
2081
+ },
2082
+ ...options
2083
+ }, {
2084
+ quoted
2085
+ });
2086
+ const sendPoll = async (jid, name, pollVote = [], multiSelect = false, options = {}, quoted = null) => {
2087
+ const selectableCount = multiSelect ? pollVote.length : 1;
2088
+ return sendMessage(jid, {
2089
+ poll: {
2090
+ name,
2091
+ values: pollVote,
2092
+ selectableCount
2093
+ },
2094
+ ...options
2095
+ }, {
2096
+ quoted
2097
+ });
2098
+ };
2099
+ const sendQuiz = async (jid, name, pollVote = [], answer, options = {}, quoted = null) => sendMessage(jid, {
2100
+ poll: {
2101
+ name,
2102
+ values: pollVote,
2103
+ selectableCount: 1,
2104
+ type: "QUIZ",
2105
+ answer: {
2106
+ optionName: answer
2107
+ }
2108
+ },
2109
+ ...options
2110
+ }, {
2111
+ quoted
2112
+ });
2113
+ const sendPtv = (jid, ptv, options = {}, quoted = null) => sendMessage(jid, {
2114
+ ptv,
2115
+ ...options
2116
+ }, {
2117
+ quoted
2118
+ });
2119
+ const normalizeMenuButtons = buttons => {
2120
+ if (!Array.isArray(buttons)) return [];
2121
+ return buttons.map((btn, index) => {
2122
+ if (typeof btn === 'string') {
2123
+ return {
2124
+ id: btn,
2125
+ text: btn
2126
+ };
2127
+ }
2128
+ const id = btn.id || btn.buttonId || btn.cmd || `btn_${index}`;
2129
+ const text = btn.text || btn.label || btn.buttonText?.displayText || btn.displayText || String(id);
2130
+ return {
2131
+ id: String(id),
2132
+ text: String(text)
2133
+ };
2134
+ }).filter(b => b.id);
2135
+ };
2136
+ const buildMenuTextFallback = (text, buttons, footer) => {
2137
+ const hint = buttons.map(b => `▸ ${b.text} (${b.id})`).join('\n');
2138
+ return `${String(text || '').trim()}${hint ? `\n\n${hint}` : ''}${footer ? `\n\n_${footer}_` : ''}`.trim();
2139
+ };
2140
+ const MAX_NATIVE_FLOW_BUTTONS = 3;
2141
+ const buildNativeFlowButtons = (normalized, options = {}) => {
2142
+ const telegram = options.telegram || options.url;
2143
+ const flowButtons = [];
2144
+ const slots = MAX_NATIVE_FLOW_BUTTONS;
2145
+ const menuButtons = normalized.slice(0, slots);
2146
+ const reserveUrl = options.includeUrlButton !== false && telegram && menuButtons.length < slots;
2147
+ if (reserveUrl) {
2148
+ flowButtons.push({
2149
+ name: 'cta_url',
2150
+ buttonParamsJson: JSON.stringify({
2151
+ display_text: options.urlLabel || 'Telegram',
2152
+ url: telegram,
2153
+ merchant_url: telegram
2154
+ })
2155
+ });
2156
+ }
2157
+ const quickLimit = slots - flowButtons.length;
2158
+ for (const btn of menuButtons.slice(0, quickLimit)) {
2159
+ flowButtons.push({
2160
+ name: 'quick_reply',
2161
+ buttonParamsJson: JSON.stringify({
2162
+ display_text: btn.text.slice(0, 25),
2163
+ id: btn.id
2164
+ })
2165
+ });
2166
+ }
2167
+ return flowButtons;
2168
+ };
2169
+ const buildInteractiveHeader = async (thumb, headerTitle) => {
2170
+ if (thumb && sock.waUploadToServer) {
2171
+ try {
2172
+ const mediaContent = await (0, _index4.generateWAMessageContent)({
2173
+ image: thumb
2174
+ }, {
2175
+ upload: sock.waUploadToServer
2176
+ });
2177
+ return {
2178
+ title: headerTitle,
2179
+ hasMediaAttachment: true,
2180
+ ...mediaContent
2181
+ };
2182
+ } catch {}
2183
+ }
2184
+ if (thumb) {
2185
+ return {
2186
+ title: headerTitle,
2187
+ hasMediaAttachment: true,
2188
+ imageMessage: {
2189
+ jpegThumbnail: thumb,
2190
+ mimetype: 'image/jpeg'
2191
+ }
2192
+ };
2193
+ }
2194
+ return {
2195
+ title: headerTitle,
2196
+ hasMediaAttachment: false
2197
+ };
2198
+ };
2199
+ const sendButtonMenu = async (jid, text, buttons = [], options = {}) => {
2200
+ const normalized = normalizeMenuButtons(buttons);
2201
+ const footerText = [options.footer || '', options.telegram || options.url ? `${options.urlLabel || 'Telegram'}: ${options.telegram || options.url}` : ''].filter(Boolean).join('\n');
2202
+ const thumb = options.jpegThumbnail || options.thumbnail || options.image || null;
2203
+ const headerTitle = options.headerTitle || options.title || "Manta'X";
2204
+ const mentionJid = options.mentionJid || options.mentionedJid;
2205
+ const quoted = options.quoted || null;
2206
+ const modes = options.tryModes || ['interactive', 'buttons', 'text'];
2207
+ const errors = [];
2208
+ if (modes.includes('interactive')) {
2209
+ try {
2210
+ const flowButtons = buildNativeFlowButtons(normalized, options);
2211
+ const interactive = {
2212
+ body: {
2213
+ text: String(text || '').trim()
2214
+ },
2215
+ footer: footerText ? {
2216
+ text: footerText
2217
+ } : undefined,
2218
+ nativeFlowMessage: {
2219
+ buttons: flowButtons,
2220
+ messageParamsJson: '',
2221
+ messageVersion: 1
2222
+ },
2223
+ header: await buildInteractiveHeader(thumb, headerTitle)
2224
+ };
2225
+ if (mentionJid) {
2226
+ interactive.contextInfo = {
2227
+ mentionedJid: Array.isArray(mentionJid) ? mentionJid : [mentionJid]
2228
+ };
2229
+ }
2230
+ const content = {
2231
+ messageContextInfo: {
2232
+ messageSecret: (0, _crypto.randomBytes)(32)
2233
+ },
2234
+ interactiveMessage: interactive
2235
+ };
2236
+ const msg = await (0, _index4.generateWAMessageFromContent)(jid, content, {
2237
+ userJid: authState.creds.me?.id,
2238
+ quoted
2239
+ });
2240
+ await relayMessage(jid, msg.message, {
2241
+ messageId: msg.key.id
2242
+ });
2243
+ return {
2244
+ mode: 'interactive',
2245
+ message: msg,
2246
+ buttonCount: flowButtons.length
2247
+ };
2248
+ } catch (err) {
2249
+ errors.push(`interactive: ${err?.message || err}`);
2250
+ }
2251
+ }
2252
+ if (modes.includes('buttons')) {
2253
+ try {
2254
+ const legacyButtons = normalized.map(btn => ({
2255
+ buttonId: btn.id,
2256
+ buttonText: {
2257
+ displayText: btn.text.slice(0, 25)
2258
+ },
2259
+ type: 1
2260
+ }));
2261
+ const buttonsMessage = {
2262
+ contentText: String(text || '').trim(),
2263
+ footerText: footerText || undefined,
2264
+ buttons: legacyButtons,
2265
+ contextInfo: mentionJid ? {
2266
+ mentionedJid: Array.isArray(mentionJid) ? mentionJid : [mentionJid]
2267
+ } : undefined
2268
+ };
2269
+ if (thumb) {
2270
+ buttonsMessage.headerType = 6;
2271
+ buttonsMessage.locationMessage = {
2272
+ degreesLatitude: options.degreesLatitude ?? 0,
2273
+ degreesLongitude: options.degreesLongitude ?? 0,
2274
+ jpegThumbnail: thumb,
2275
+ name: headerTitle
2276
+ };
2277
+ } else {
2278
+ buttonsMessage.headerType = 1;
2279
+ }
2280
+ const content = {
2281
+ buttonsMessage
2282
+ };
2283
+ const msg = await (0, _index4.generateWAMessageFromContent)(jid, content, {
2284
+ userJid: authState.creds.me?.id,
2285
+ quoted
2286
+ });
2287
+ await relayMessage(jid, msg.message, {
2288
+ messageId: msg.key.id
2289
+ });
2290
+ return {
2291
+ mode: 'buttons',
2292
+ message: msg
2293
+ };
2294
+ } catch (err) {
2295
+ errors.push(`buttons: ${err?.message || err}`);
2296
+ }
2297
+ }
2298
+ const fallbackText = buildMenuTextFallback(text, normalized, footerText);
2299
+ const msg = await sendText(jid, fallbackText, mentionJid ? {
2300
+ mentionedJid: Array.isArray(mentionJid) ? mentionJid : [mentionJid]
2301
+ } : {}, quoted);
2302
+ return {
2303
+ mode: 'text',
2304
+ message: msg,
2305
+ errors
2306
+ };
2307
+ };
2308
+ const statusMention = async (jid, content) => {
2309
+ const msg = await (0, _index4.generateWAMessageFromContent)(jid, content, {
2310
+ userJid: authState.creds.me.id
2311
+ });
2312
+ await relayMessage("status@broadcast", msg.message, {
2313
+ statusJidList: [jid, authState.creds.me.id],
2314
+ additionalNodes: [{
2315
+ tag: "meta",
2316
+ attrs: {},
2317
+ content: [{
2318
+ tag: "mentioned_users",
2319
+ attrs: {},
2320
+ content: [{
2321
+ tag: "to",
2322
+ attrs: {
2323
+ jid
2324
+ },
2325
+ content: undefined
2326
+ }]
2327
+ }]
2328
+ }]
2329
+ });
2330
+ const mentionMsg = {
2331
+ statusMentionMessage: {
2332
+ message: {
2333
+ protocolMessage: {
2334
+ key: msg.key,
2335
+ type: 25,
2336
+ timestamp: Math.floor(Date.now() / 1000)
2337
+ }
2338
+ }
2339
+ }
2340
+ };
2341
+ const x = (0, _index4.generateWAMessageFromContent)(jid, mentionMsg, {});
2342
+ return relayMessage(jid, x.message, {
2343
+ messageId: x.key.id,
2344
+ additionalNodes: [{
2345
+ tag: "meta",
2346
+ attrs: {
2347
+ is_status_mention: "true"
2348
+ }
2349
+ }]
2350
+ });
2351
+ };
2352
+ return {
2353
+ ...sock,
2354
+ sendMessageAck,
2355
+ sendRetryRequest,
2356
+ rejectCall,
2357
+ fetchMessageHistory,
2358
+ requestPlaceholderResend,
2359
+ messageRetryManager,
2360
+ sendText,
2361
+ sendImage,
2362
+ sendVideo,
2363
+ sendAudio,
2364
+ sendDocument,
2365
+ sendLocation,
2366
+ sendPoll,
2367
+ sendQuiz,
2368
+ sendPtv,
2369
+ sendButtonMenu,
2370
+ statusMention
2371
+ };
2372
+ };
2373
+ //# sourceMappingURL=messages-recv.js.map
2374
+ exports.makeMessagesRecvSocket = makeMessagesRecvSocket;