@ntzckofficial/baileys 0.0.0-stage → 2.1.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 (289) hide show
  1. package/Example/example.ts +235 -0
  2. package/LICENSE +21 -0
  3. package/README.md +43 -2
  4. package/WAProto/GenerateStatics.sh +3 -0
  5. package/WAProto/WAProto.proto +5479 -0
  6. package/WAProto/fix-imports.js +81 -0
  7. package/WAProto/index.d.ts +17302 -0
  8. package/WAProto/index.js +125089 -0
  9. package/index.cjs +29 -0
  10. package/index.d.ts +2 -0
  11. package/package.json +114 -4
  12. package/src/Defaults/index.d.ts +153 -0
  13. package/src/Defaults/index.js +165 -0
  14. package/src/MessageBuilder/bot-signature.d.ts +22 -0
  15. package/src/MessageBuilder/bot-signature.js +123 -0
  16. package/src/MessageBuilder/extras.d.ts +449 -0
  17. package/src/MessageBuilder/extras.js +1357 -0
  18. package/src/MessageBuilder/index.d.ts +210 -0
  19. package/src/MessageBuilder/index.js +3561 -0
  20. package/src/MessageBuilder/metaai.d.ts +438 -0
  21. package/src/MessageBuilder/metaai.js +891 -0
  22. package/src/Signal/Group/ciphertext-message.d.ts +10 -0
  23. package/src/Signal/Group/ciphertext-message.js +12 -0
  24. package/src/Signal/Group/group-session-builder.d.ts +8 -0
  25. package/src/Signal/Group/group-session-builder.js +28 -0
  26. package/src/Signal/Group/group_cipher.d.ts +11 -0
  27. package/src/Signal/Group/group_cipher.js +79 -0
  28. package/src/Signal/Group/index.d.ts +12 -0
  29. package/src/Signal/Group/index.js +12 -0
  30. package/src/Signal/Group/keyhelper.d.ts +7 -0
  31. package/src/Signal/Group/keyhelper.js +18 -0
  32. package/src/Signal/Group/sender-chain-key.d.ts +14 -0
  33. package/src/Signal/Group/sender-chain-key.js +26 -0
  34. package/src/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  35. package/src/Signal/Group/sender-key-distribution-message.js +63 -0
  36. package/src/Signal/Group/sender-key-message.d.ts +19 -0
  37. package/src/Signal/Group/sender-key-message.js +66 -0
  38. package/src/Signal/Group/sender-key-name.d.ts +12 -0
  39. package/src/Signal/Group/sender-key-name.js +48 -0
  40. package/src/Signal/Group/sender-key-record.d.ts +13 -0
  41. package/src/Signal/Group/sender-key-record.js +41 -0
  42. package/src/Signal/Group/sender-key-state.d.ts +17 -0
  43. package/src/Signal/Group/sender-key-state.js +84 -0
  44. package/src/Signal/Group/sender-message-key.d.ts +12 -0
  45. package/src/Signal/Group/sender-message-key.js +26 -0
  46. package/src/Signal/libsignal.d.ts +55 -0
  47. package/src/Signal/libsignal.js +459 -0
  48. package/src/Signal/lid-mapping.d.ts +21 -0
  49. package/src/Signal/lid-mapping.js +275 -0
  50. package/src/Socket/Client/index.d.ts +3 -0
  51. package/src/Socket/Client/index.js +3 -0
  52. package/src/Socket/Client/types.d.ts +6 -0
  53. package/src/Socket/Client/types.js +10 -0
  54. package/src/Socket/Client/websocket.d.ts +13 -0
  55. package/src/Socket/Client/websocket.js +54 -0
  56. package/src/Socket/business.d.ts +432 -0
  57. package/src/Socket/business.js +380 -0
  58. package/src/Socket/chats.d.ts +248 -0
  59. package/src/Socket/chats.js +1485 -0
  60. package/src/Socket/communities.d.ts +599 -0
  61. package/src/Socket/communities.js +422 -0
  62. package/src/Socket/groups.d.ts +299 -0
  63. package/src/Socket/groups.js +340 -0
  64. package/src/Socket/index.d.ts +582 -0
  65. package/src/Socket/index.js +43 -0
  66. package/src/Socket/messages-recv.d.ts +371 -0
  67. package/src/Socket/messages-recv.js +1813 -0
  68. package/src/Socket/messages-send.d.ts +391 -0
  69. package/src/Socket/messages-send.js +1575 -0
  70. package/src/Socket/mex.d.ts +2 -0
  71. package/src/Socket/mex.js +45 -0
  72. package/src/Socket/newsletter.d.ts +469 -0
  73. package/src/Socket/newsletter.js +643 -0
  74. package/src/Socket/socket.d.ts +78 -0
  75. package/src/Socket/socket.js +1088 -0
  76. package/src/Store/index.d.ts +4 -0
  77. package/src/Store/index.js +4 -0
  78. package/src/Store/make-in-memory-store.d.ts +63 -0
  79. package/src/Store/make-in-memory-store.js +398 -0
  80. package/src/Store/make-ordered-dictionary.d.ts +13 -0
  81. package/src/Store/make-ordered-dictionary.js +79 -0
  82. package/src/Store/object-repository.d.ts +11 -0
  83. package/src/Store/object-repository.js +24 -0
  84. package/src/Types/Auth.d.ts +2 -0
  85. package/src/Types/Auth.js +2 -0
  86. package/src/Types/Bussines.d.ts +2 -0
  87. package/src/Types/Bussines.js +2 -0
  88. package/src/Types/Call.d.ts +2 -0
  89. package/src/Types/Call.js +2 -0
  90. package/src/Types/Chat.d.ts +3 -0
  91. package/src/Types/Chat.js +25 -0
  92. package/src/Types/Contact.d.ts +2 -0
  93. package/src/Types/Contact.js +2 -0
  94. package/src/Types/Events.d.ts +2 -0
  95. package/src/Types/Events.js +1 -0
  96. package/src/Types/GroupMetadata.d.ts +6 -0
  97. package/src/Types/GroupMetadata.js +7 -0
  98. package/src/Types/Label.d.ts +3 -0
  99. package/src/Types/Label.js +25 -0
  100. package/src/Types/LabelAssociation.d.ts +3 -0
  101. package/src/Types/LabelAssociation.js +7 -0
  102. package/src/Types/Message.d.ts +14 -0
  103. package/src/Types/Message.js +24 -0
  104. package/src/Types/Mex.d.ts +3 -0
  105. package/src/Types/Mex.js +96 -0
  106. package/src/Types/Product.d.ts +2 -0
  107. package/src/Types/Product.js +2 -0
  108. package/src/Types/Signal.d.ts +2 -0
  109. package/src/Types/Signal.js +1 -0
  110. package/src/Types/Socket.d.ts +2 -0
  111. package/src/Types/Socket.js +1 -0
  112. package/src/Types/State.d.ts +6 -0
  113. package/src/Types/State.js +55 -0
  114. package/src/Types/USync.d.ts +2 -0
  115. package/src/Types/USync.js +1 -0
  116. package/src/Types/index.d.ts +14 -0
  117. package/src/Types/index.js +26 -0
  118. package/src/Utils/auth-store.d.ts +30 -0
  119. package/src/Utils/auth-store.js +77 -0
  120. package/src/Utils/auth-utils.d.ts +59 -0
  121. package/src/Utils/auth-utils.js +287 -0
  122. package/src/Utils/browser-utils.d.ts +10 -0
  123. package/src/Utils/browser-utils.js +29 -0
  124. package/src/Utils/business.d.ts +50 -0
  125. package/src/Utils/business.js +227 -0
  126. package/src/Utils/chat-utils.d.ts +409 -0
  127. package/src/Utils/chat-utils.js +847 -0
  128. package/src/Utils/companion-reg-client-utils.d.ts +5 -0
  129. package/src/Utils/companion-reg-client-utils.js +40 -0
  130. package/src/Utils/crypto.d.ts +40 -0
  131. package/src/Utils/crypto.js +111 -0
  132. package/src/Utils/decode-wa-message.d.ts +100 -0
  133. package/src/Utils/decode-wa-message.js +398 -0
  134. package/src/Utils/event-buffer.d.ts +23 -0
  135. package/src/Utils/event-buffer.js +625 -0
  136. package/src/Utils/generics.d.ts +64 -0
  137. package/src/Utils/generics.js +376 -0
  138. package/src/Utils/group-metadata-cache.d.ts +5 -0
  139. package/src/Utils/group-metadata-cache.js +26 -0
  140. package/src/Utils/history.d.ts +76 -0
  141. package/src/Utils/history.js +132 -0
  142. package/src/Utils/html-app.d.ts +19 -0
  143. package/src/Utils/html-app.js +97 -0
  144. package/src/Utils/html-multiplayer.d.ts +10 -0
  145. package/src/Utils/html-multiplayer.js +95 -0
  146. package/src/Utils/identity-change-handler.d.ts +14 -0
  147. package/src/Utils/identity-change-handler.js +49 -0
  148. package/src/Utils/index.d.ts +45 -0
  149. package/src/Utils/index.js +45 -0
  150. package/src/Utils/link-preview-metadata.d.ts +47 -0
  151. package/src/Utils/link-preview-metadata.js +89 -0
  152. package/src/Utils/link-preview.d.ts +13 -0
  153. package/src/Utils/link-preview.js +80 -0
  154. package/src/Utils/logger.d.ts +3 -0
  155. package/src/Utils/logger.js +3 -0
  156. package/src/Utils/lt-hash.d.ts +7 -0
  157. package/src/Utils/lt-hash.js +4 -0
  158. package/src/Utils/make-mutex.d.ts +7 -0
  159. package/src/Utils/make-mutex.js +33 -0
  160. package/src/Utils/message-retry-manager.d.ts +80 -0
  161. package/src/Utils/message-retry-manager.js +256 -0
  162. package/src/Utils/messages-media.d.ts +137 -0
  163. package/src/Utils/messages-media.js +815 -0
  164. package/src/Utils/messages.d.ts +63 -0
  165. package/src/Utils/messages.js +2153 -0
  166. package/src/Utils/modern-messages.d.ts +150 -0
  167. package/src/Utils/modern-messages.js +496 -0
  168. package/src/Utils/native-flow.d.ts +11 -0
  169. package/src/Utils/native-flow.js +116 -0
  170. package/src/Utils/newsletter-status.d.ts +158 -0
  171. package/src/Utils/newsletter-status.js +651 -0
  172. package/src/Utils/noise-handler.d.ts +21 -0
  173. package/src/Utils/noise-handler.js +201 -0
  174. package/src/Utils/offline-node-processor.d.ts +10 -0
  175. package/src/Utils/offline-node-processor.js +34 -0
  176. package/src/Utils/optional-media.d.ts +3 -0
  177. package/src/Utils/optional-media.js +46 -0
  178. package/src/Utils/payment-messages.d.ts +66 -0
  179. package/src/Utils/payment-messages.js +161 -0
  180. package/src/Utils/pre-key-manager.d.ts +26 -0
  181. package/src/Utils/pre-key-manager.js +96 -0
  182. package/src/Utils/process-message.d.ts +61 -0
  183. package/src/Utils/process-message.js +689 -0
  184. package/src/Utils/reporting-utils.d.ts +13 -0
  185. package/src/Utils/reporting-utils.js +257 -0
  186. package/src/Utils/scheduled-message.d.ts +51 -0
  187. package/src/Utils/scheduled-message.js +113 -0
  188. package/src/Utils/sender-key-memory.d.ts +20 -0
  189. package/src/Utils/sender-key-memory.js +52 -0
  190. package/src/Utils/signal.d.ts +83 -0
  191. package/src/Utils/signal.js +194 -0
  192. package/src/Utils/stanza-ack.d.ts +17 -0
  193. package/src/Utils/stanza-ack.js +31 -0
  194. package/src/Utils/status-stickers.d.ts +127 -0
  195. package/src/Utils/status-stickers.js +261 -0
  196. package/src/Utils/sync-action-utils.d.ts +3 -0
  197. package/src/Utils/sync-action-utils.js +45 -0
  198. package/src/Utils/tc-token-utils.d.ts +30 -0
  199. package/src/Utils/tc-token-utils.js +156 -0
  200. package/src/Utils/ttl-cache.d.ts +29 -0
  201. package/src/Utils/ttl-cache.js +121 -0
  202. package/src/Utils/use-mongo-auth-state.d.ts +22 -0
  203. package/src/Utils/use-mongo-auth-state.js +68 -0
  204. package/src/Utils/use-multi-file-auth-state.d.ts +12 -0
  205. package/src/Utils/use-multi-file-auth-state.js +119 -0
  206. package/src/Utils/use-mysql-auth-state.d.ts +20 -0
  207. package/src/Utils/use-mysql-auth-state.js +80 -0
  208. package/src/Utils/use-postgres-auth-state.d.ts +20 -0
  209. package/src/Utils/use-postgres-auth-state.js +80 -0
  210. package/src/Utils/use-redis-auth-state.d.ts +20 -0
  211. package/src/Utils/use-redis-auth-state.js +90 -0
  212. package/src/Utils/use-single-file-auth-state.d.ts +11 -0
  213. package/src/Utils/use-single-file-auth-state.js +109 -0
  214. package/src/Utils/use-sqlite-auth-state.d.ts +13 -0
  215. package/src/Utils/use-sqlite-auth-state.js +121 -0
  216. package/src/Utils/useNekoDBAuth.d.ts +19 -0
  217. package/src/Utils/useNekoDBAuth.js +111 -0
  218. package/src/Utils/username.d.ts +12 -0
  219. package/src/Utils/username.js +59 -0
  220. package/src/Utils/validate-connection.d.ts +44 -0
  221. package/src/Utils/validate-connection.js +208 -0
  222. package/src/Utils/voice-recognition.d.ts +53 -0
  223. package/src/Utils/voice-recognition.js +219 -0
  224. package/src/Voip/audio-feeder.d.ts +25 -0
  225. package/src/Voip/audio-feeder.js +269 -0
  226. package/src/Voip/index.d.ts +89 -0
  227. package/src/Voip/index.js +937 -0
  228. package/src/Voip/relay-transport.d.ts +6 -0
  229. package/src/Voip/relay-transport.js +825 -0
  230. package/src/Voip/signaling.d.ts +12 -0
  231. package/src/Voip/signaling.js +1100 -0
  232. package/src/Voip/types.d.ts +3 -0
  233. package/src/Voip/types.js +13 -0
  234. package/src/Voip/video-feeder.d.ts +25 -0
  235. package/src/Voip/video-feeder.js +269 -0
  236. package/src/Voip/wasm-engine.d.ts +27 -0
  237. package/src/Voip/wasm-engine.js +1308 -0
  238. package/src/Voip/worker-bootstrap.js +1024 -0
  239. package/src/WABinary/constants.d.ts +23 -0
  240. package/src/WABinary/constants.js +1301 -0
  241. package/src/WABinary/decode.d.ts +10 -0
  242. package/src/WABinary/decode.js +262 -0
  243. package/src/WABinary/encode.d.ts +3 -0
  244. package/src/WABinary/encode.js +220 -0
  245. package/src/WABinary/generic-utils.d.ts +77 -0
  246. package/src/WABinary/generic-utils.js +215 -0
  247. package/src/WABinary/index.d.ts +6 -0
  248. package/src/WABinary/index.js +6 -0
  249. package/src/WABinary/jid-utils.d.ts +31 -0
  250. package/src/WABinary/jid-utils.js +98 -0
  251. package/src/WABinary/types.d.ts +2 -0
  252. package/src/WABinary/types.js +1 -0
  253. package/src/WAM/BinaryInfo.d.ts +8 -0
  254. package/src/WAM/BinaryInfo.js +10 -0
  255. package/src/WAM/constants.d.ts +34927 -0
  256. package/src/WAM/constants.js +22853 -0
  257. package/src/WAM/encode.d.ts +2 -0
  258. package/src/WAM/encode.js +149 -0
  259. package/src/WAM/index.d.ts +4 -0
  260. package/src/WAM/index.js +4 -0
  261. package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
  262. package/src/WAUSync/Protocols/USyncContactProtocol.js +51 -0
  263. package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  264. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +52 -0
  265. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  266. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  267. package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  268. package/src/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  269. package/src/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  270. package/src/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  271. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
  272. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  273. package/src/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
  274. package/src/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  275. package/src/WAUSync/Protocols/index.d.ts +6 -0
  276. package/src/WAUSync/Protocols/index.js +6 -0
  277. package/src/WAUSync/USyncQuery.d.ts +21 -0
  278. package/src/WAUSync/USyncQuery.js +94 -0
  279. package/src/WAUSync/USyncUser.d.ts +17 -0
  280. package/src/WAUSync/USyncUser.js +31 -0
  281. package/src/WAUSync/index.d.ts +4 -0
  282. package/src/WAUSync/index.js +4 -0
  283. package/src/assets/wasm/README.md +76 -0
  284. package/src/assets/wasm/loader.js +5 -0
  285. package/src/assets/wasm/whatsapp.wasm +0 -0
  286. package/src/assets/wasm/worker-modules.js +273 -0
  287. package/src/custom.js +85 -0
  288. package/src/index.d.ts +49 -0
  289. package/src/index.js +55 -0
@@ -0,0 +1,398 @@
1
+ /* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
2
+ import { Boom } from '@hapi/boom';
3
+ import { proto } from '../../WAProto/index.js';
4
+ import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidGroup, isJidMetaAI, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser
5
+
6
+ } from '../WABinary/index.js';
7
+ import { unpadRandomMax16 } from './generics.js';
8
+ const findChildByTag = (node, tag) => {
9
+ if (!node || !Array.isArray(node.content)) {
10
+ return undefined;
11
+ }
12
+ return node.content.find(child => child?.tag === tag);
13
+ };
14
+ const readIntAttr = (attrs, key) => {
15
+ const raw = attrs?.[key];
16
+ if (raw === undefined || raw === null || raw === '') {
17
+ return undefined;
18
+ }
19
+ const parsed = Number.parseInt(String(raw), 10);
20
+ return Number.isFinite(parsed) ? parsed : undefined;
21
+ };
22
+ export const extractNewsletterMessageMeta = (stanza) => {
23
+ const meta = findChildByTag(stanza, 'meta');
24
+ if (!meta) {
25
+ return undefined;
26
+ }
27
+ const result = {};
28
+ const adminProfile = findChildByTag(meta, 'admin_profile');
29
+ if (adminProfile) {
30
+ const name = findChildByTag(adminProfile, 'name');
31
+ const picture = findChildByTag(adminProfile, 'picture');
32
+ const content = name?.content;
33
+ result.adminProfile = {
34
+ id: adminProfile.attrs?.id,
35
+ name: typeof content === 'string'
36
+ ? content
37
+ : content instanceof Uint8Array
38
+ ? Buffer.from(content).toString('utf-8')
39
+ : undefined,
40
+ pictureId: picture?.attrs?.id,
41
+ pictureDirectPath: picture?.attrs?.direct_path
42
+ };
43
+ }
44
+ if (findChildByTag(meta, 'paid_partnership')) {
45
+ result.paidPartnership = true;
46
+ }
47
+ if (findChildByTag(meta, 'ai_content')) {
48
+ result.aiContent = true;
49
+ }
50
+ const editTimestamp = readIntAttr(meta.attrs, 'msg_edit_t');
51
+ if (editTimestamp !== undefined) {
52
+ result.editTimestamp = editTimestamp;
53
+ }
54
+ const originalTimestamp = readIntAttr(meta.attrs, 'original_msg_t');
55
+ if (originalTimestamp !== undefined) {
56
+ result.originalTimestamp = originalTimestamp;
57
+ }
58
+ return Object.keys(result).length ? result : undefined;
59
+ };
60
+ export const getDecryptionJid = async (sender, repository) => {
61
+ if (isLidUser(sender) || isHostedLidUser(sender)) {
62
+ return sender;
63
+ }
64
+ const mapped = await repository.lidMapping.getLIDForPN(sender);
65
+ return mapped || sender;
66
+ };
67
+ const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger) => {
68
+
69
+ const { senderAlt } = extractAddressingContext(stanza);
70
+ if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
71
+ try {
72
+ await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }]);
73
+ await repository.migrateSession(sender, senderAlt);
74
+ logger.debug({ sender, senderAlt }, 'Stored LID mapping from envelope');
75
+ }
76
+ catch (error) {
77
+ logger.warn({ sender, senderAlt, error }, 'Failed to store LID mapping');
78
+ }
79
+ }
80
+ };
81
+ export const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
82
+ export const MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
83
+ export const ACCOUNT_RESTRICTED_TEXT = 'Your account has been restricted';
84
+
85
+ export const DECRYPTION_RETRY_CONFIG = {
86
+ maxRetries: 3,
87
+ baseDelayMs: 100,
88
+ sessionRecordErrors: [
89
+ 'No session record',
90
+ 'SessionError: No session record',
91
+ 'Bad MAC',
92
+ 'Invalid MAC',
93
+ 'No matching sessions',
94
+ 'No open session',
95
+ 'No sessions available',
96
+ 'Chain closed',
97
+ 'Over 2000 messages into the future',
98
+ 'Message counter',
99
+ 'UntrustedIdentityKeyError'
100
+ ]
101
+ };
102
+
103
+ export const NACK_REASONS = {
104
+ StaleGroupAddressingMode: 421,
105
+ SenderReachoutTimelocked: 463,
106
+ NewChatMessagesCapped: 475,
107
+ ParsingError: 487,
108
+ UnrecognizedStanza: 488,
109
+ UnrecognizedStanzaClass: 489,
110
+ UnrecognizedStanzaType: 490,
111
+ InvalidProtobuf: 491,
112
+ InvalidHostedCompanionStanza: 493,
113
+ MissingMessageSecret: 495,
114
+ SignalErrorOldCounter: 496,
115
+ MessageDeletedOnPeer: 499,
116
+ UnhandledError: 500,
117
+ UnsupportedAdminRevoke: 550,
118
+ UnsupportedLIDGroup: 551,
119
+ DBOperationFailed: 552
120
+ };
121
+
122
+ export const SERVER_ERROR_CODES = {
123
+
124
+ MessageAccountRestriction: '463',
125
+
126
+ SmaxInvalid: '479',
127
+
128
+ NewChatMessagesCapped: '475'
129
+ };
130
+
131
+ export const RETRYABLE_ACK_CODES = new Set(['408', '429', '500', '503']);
132
+ export const extractAddressingContext = (stanza) => {
133
+ let senderAlt;
134
+ let recipientAlt;
135
+ const sender = stanza.attrs.participant || stanza.attrs.from;
136
+ const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith('lid') ? 'lid' : 'pn');
137
+ if (addressingMode === 'lid') {
138
+
139
+ senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn;
140
+ recipientAlt = stanza.attrs.recipient_pn;
141
+
142
+ }
143
+ else {
144
+
145
+ senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid;
146
+ recipientAlt = stanza.attrs.recipient_lid;
147
+
148
+ }
149
+ return {
150
+ addressingMode,
151
+ senderAlt,
152
+ recipientAlt
153
+ };
154
+ };
155
+
156
+ export function decodeMessageNode(stanza, meId, meLid) {
157
+ let msgType;
158
+ let chatId;
159
+ let author;
160
+ let fromMe = false;
161
+ const msgId = stanza.attrs.id;
162
+ const from = stanza.attrs.from;
163
+ const participant = stanza.attrs.participant;
164
+ const recipient = stanza.attrs.recipient;
165
+ if (!msgId) {
166
+ throw new Boom('Invalid message stanza: missing id attribute', { data: stanza });
167
+ }
168
+ if (!from) {
169
+ throw new Boom('Invalid message stanza: missing from attribute', { data: stanza });
170
+ }
171
+ const addressingContext = extractAddressingContext(stanza);
172
+ const isMe = (jid) => areJidsSameUser(jid, meId);
173
+ const isMeLid = (jid) => areJidsSameUser(jid, meLid);
174
+ if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
175
+ if (recipient && !isJidMetaAI(recipient)) {
176
+ if (!isMe(from) && !isMeLid(from)) {
177
+ throw new Boom('receipient present, but msg not from me', { data: stanza });
178
+ }
179
+ if (isMe(from) || isMeLid(from)) {
180
+ fromMe = true;
181
+ }
182
+ chatId = recipient;
183
+ }
184
+ else {
185
+
186
+ if (isMe(from) || isMeLid(from)) {
187
+ fromMe = true;
188
+ }
189
+ chatId = from;
190
+ }
191
+ msgType = 'chat';
192
+ author = from;
193
+ }
194
+ else if (isJidGroup(from)) {
195
+ if (!participant) {
196
+ throw new Boom('No participant in group message');
197
+ }
198
+ if (isMe(participant) || isMeLid(participant)) {
199
+ fromMe = true;
200
+ }
201
+ msgType = 'group';
202
+ author = participant;
203
+ chatId = from;
204
+ }
205
+ else if (isJidBroadcast(from)) {
206
+ if (!participant) {
207
+ throw new Boom('No participant in group message');
208
+ }
209
+ const isParticipantMe = isMe(participant);
210
+ if (isJidStatusBroadcast(from)) {
211
+ msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';
212
+ }
213
+ else {
214
+ msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast';
215
+ }
216
+ fromMe = isParticipantMe;
217
+ chatId = from;
218
+ author = participant;
219
+ }
220
+ else if (isJidNewsletter(from)) {
221
+ msgType = 'newsletter';
222
+ chatId = from;
223
+ author = from;
224
+ if (isMe(from) || isMeLid(from)) {
225
+ fromMe = true;
226
+ }
227
+ }
228
+ else {
229
+ throw new Boom('Unknown message type', { data: stanza });
230
+ }
231
+ const pushname = stanza?.attrs?.notify;
232
+ const key = {
233
+ remoteJid: chatId,
234
+ remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
235
+ remoteJidUsername: !isJidGroup(chatId)
236
+ ? stanza.attrs.peer_recipient_username || stanza.attrs.recipient_username
237
+ : undefined,
238
+ fromMe,
239
+ id: msgId,
240
+ participant,
241
+ participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
242
+ participantUsername: stanza.attrs.participant ? stanza.attrs.participant_username : undefined,
243
+ addressingMode: addressingContext.addressingMode,
244
+ ...(msgType === 'newsletter' && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
245
+ };
246
+ const fullMessage = {
247
+ key,
248
+ category: stanza.attrs.category,
249
+ messageTimestamp: +stanza.attrs.t,
250
+ pushName: pushname,
251
+ broadcast: isJidBroadcast(from)
252
+ };
253
+ if (msgType === 'newsletter') {
254
+ const meta = extractNewsletterMessageMeta(stanza);
255
+ if (meta) {
256
+ fullMessage.newsletterMeta = meta;
257
+ if (meta.adminProfile?.name) {
258
+ fullMessage.pushName = fullMessage.pushName || meta.adminProfile.name;
259
+ }
260
+ }
261
+ if (stanza.attrs.is_sender === 'true') {
262
+ fullMessage.key.fromMe = true;
263
+ fullMessage.key.isNewsletterSender = true;
264
+ }
265
+ }
266
+ if (key.fromMe) {
267
+ fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
268
+ }
269
+ return {
270
+ fullMessage,
271
+ author,
272
+ sender: msgType === 'chat' ? author : chatId
273
+ };
274
+ }
275
+ export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
276
+ const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
277
+ return {
278
+ fullMessage,
279
+ category: stanza.attrs.category,
280
+ author,
281
+ async decrypt() {
282
+ let decryptables = 0;
283
+ if (Array.isArray(stanza.content)) {
284
+ for (const { tag, attrs, content } of stanza.content) {
285
+ if (tag === 'verified_name' && content instanceof Uint8Array) {
286
+ try {
287
+ const cert = proto.VerifiedNameCertificate.decode(content);
288
+ const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
289
+ fullMessage.verifiedBizName = details.verifiedName;
290
+ }
291
+ catch (err) {
292
+ logger?.warn?.({ err, key: fullMessage.key }, 'could not read the verified business name, leaving it unset');
293
+ }
294
+ }
295
+ if (tag === 'unavailable' && attrs.type === 'view_once') {
296
+ fullMessage.key.isViewOnce = true;
297
+ }
298
+ if (attrs.count && tag === 'enc') {
299
+ fullMessage.retryCount = Number(attrs.count);
300
+ }
301
+ if (tag !== 'enc' && tag !== 'plaintext') {
302
+ continue;
303
+ }
304
+ if (!(content instanceof Uint8Array)) {
305
+ continue;
306
+ }
307
+ decryptables += 1;
308
+ let msgBuffer;
309
+ const decryptionJid = await getDecryptionJid(author, repository);
310
+ if (tag !== 'plaintext') {
311
+
312
+ await storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger);
313
+ }
314
+ try {
315
+ const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
316
+ switch (e2eType) {
317
+ case 'skmsg':
318
+ msgBuffer = await repository.decryptGroupMessage({
319
+ group: sender,
320
+ authorJid: author,
321
+ msg: content
322
+ });
323
+ break;
324
+ case 'pkmsg':
325
+ case 'msg':
326
+ msgBuffer = await repository.decryptMessage({
327
+ jid: decryptionJid,
328
+ type: e2eType,
329
+ ciphertext: content
330
+ });
331
+ break;
332
+ case 'plaintext':
333
+ msgBuffer = content;
334
+ break;
335
+ default:
336
+ throw new Error(`Unknown e2e type: ${e2eType}`);
337
+ }
338
+ let msg = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer);
339
+ msg = msg.deviceSentMessage?.message || msg;
340
+ if (msg.senderKeyDistributionMessage) {
341
+
342
+ try {
343
+ await repository.processSenderKeyDistributionMessage({
344
+ authorJid: author,
345
+ item: msg.senderKeyDistributionMessage
346
+ });
347
+ }
348
+ catch (err) {
349
+ logger.error({ key: fullMessage.key, err }, 'failed to process sender key distribution message');
350
+ }
351
+ }
352
+ if (fullMessage.message) {
353
+ Object.assign(fullMessage.message, msg);
354
+ }
355
+ else {
356
+ fullMessage.message = msg;
357
+ }
358
+ }
359
+ catch (err) {
360
+ const isSessionErr = isSessionRecordError(err);
361
+ const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
362
+ if (isSessionErr && e2eType !== 'skmsg') {
363
+ try {
364
+ const targetsToDelete = new Set([decryptionJid, author].filter(Boolean));
365
+ await repository.deleteSession(Array.from(targetsToDelete));
366
+ logger?.info?.({ targets: Array.from(targetsToDelete), err: err?.message }, 'cleared corrupted session on decryption error');
367
+ }
368
+ catch (delErr) {
369
+ logger?.warn?.({ delErr }, 'failed to delete corrupted session on decryption error');
370
+ }
371
+ }
372
+ const errorContext = {
373
+ key: fullMessage.key,
374
+ err,
375
+ messageType: e2eType,
376
+ sender,
377
+ author,
378
+ isSessionRecordError: isSessionErr
379
+ };
380
+ logger.error(errorContext, 'failed to decrypt message');
381
+ fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
382
+ fullMessage.messageStubParameters = [err.message.toString()];
383
+ }
384
+ }
385
+ }
386
+
387
+ if (!decryptables && !fullMessage.key?.isViewOnce) {
388
+ fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
389
+ fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
390
+ }
391
+ }
392
+ };
393
+ };
394
+
395
+ function isSessionRecordError(error) {
396
+ const errorMessage = error?.message || error?.toString() || '';
397
+ return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(errorPattern => errorMessage.includes(errorPattern));
398
+ }
@@ -0,0 +1,23 @@
1
+ /* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
2
+ export const DEFAULT_MAX_EVENT_LISTENERS: number;
3
+ export function makeEventBuffer(logger: any, maxListeners?: number): {
4
+ process(handler: any): () => void;
5
+ emit(event: any, evData: any): any;
6
+ isBuffering(): boolean;
7
+ buffer: () => void;
8
+ flush: () => boolean;
9
+ createBufferedFunction(work: any): (...args: any[]) => Promise<any>;
10
+ on: (...args: any[]) => any;
11
+ off: (...args: any[]) => any;
12
+ removeAllListeners: (...args: any[]) => any;
13
+ once: (...args: any[]) => any;
14
+ addListener: (...args: any[]) => any;
15
+ removeListener: (...args: any[]) => any;
16
+ listeners: (...args: any[]) => Function[];
17
+ rawListeners: (...args: any[]) => Function[];
18
+ eventNames: () => (string | symbol)[];
19
+ setMaxListeners: (count: number) => any;
20
+ getMaxListeners: () => number;
21
+ listenerCount: (...args: any[]) => number;
22
+ destroy(): void;
23
+ };