@zervonexx/baileys 5.0.0 → 5.0.2

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 (127) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +4124 -4112
  3. package/WAProto/WAProto.json +1 -0
  4. package/WAProto/WAProto.proto +7160 -0
  5. package/WAProto/WAProtoCompile.js +264 -322
  6. package/WAProto/index.js +55 -95
  7. package/lib/Builders/AIRich.js +673 -673
  8. package/lib/Builders/auto.js +49 -49
  9. package/lib/Builders/base.js +144 -144
  10. package/lib/Builders/builders.js +588 -588
  11. package/lib/Builders/index.js +3 -3
  12. package/lib/Defaults/index.js +249 -248
  13. package/lib/Signal/index.js +2 -2
  14. package/lib/Signal/libsignal.js +526 -526
  15. package/lib/Signal/lid-mapping.js +282 -282
  16. package/lib/Socket/Client/index.js +1 -1
  17. package/lib/Socket/Client/types.js +9 -9
  18. package/lib/Socket/Client/websocket.js +83 -83
  19. package/lib/Socket/business.js +151 -151
  20. package/lib/Socket/chats.js +854 -854
  21. package/lib/Socket/communities.js +146 -146
  22. package/lib/Socket/groups.js +293 -293
  23. package/lib/Socket/index.js +17 -14
  24. package/lib/Socket/messages-recv.js +993 -993
  25. package/lib/Socket/messages-send.js +722 -722
  26. package/lib/Socket/mex.js +33 -33
  27. package/lib/Socket/newsletter.js +783 -783
  28. package/lib/Socket/registration.js +154 -154
  29. package/lib/Socket/socket.js +797 -797
  30. package/lib/Socket/spam-report.js +143 -143
  31. package/lib/Socket/username.js +81 -81
  32. package/lib/Socket/zervonex-handler.js +57 -57
  33. package/lib/Store/index.js +6 -6
  34. package/lib/Store/make-cache-manager-store.js +80 -80
  35. package/lib/Store/make-in-memory-store.js +426 -426
  36. package/lib/Store/make-ordered-dictionary.js +81 -81
  37. package/lib/Store/object-repository.js +30 -30
  38. package/lib/Store/reporting-info-store.js +37 -37
  39. package/lib/Types/Auth.js +1 -1
  40. package/lib/Types/Bussines.js +1 -1
  41. package/lib/Types/Call.js +1 -1
  42. package/lib/Types/Chat.js +7 -7
  43. package/lib/Types/Contact.js +1 -1
  44. package/lib/Types/Events.js +1 -1
  45. package/lib/Types/GroupMetadata.js +1 -1
  46. package/lib/Types/Label.js +24 -24
  47. package/lib/Types/LabelAssociation.js +6 -6
  48. package/lib/Types/Message.js +14 -14
  49. package/lib/Types/Newsletter.js +99 -99
  50. package/lib/Types/Product.js +1 -1
  51. package/lib/Types/Signal.js +1 -1
  52. package/lib/Types/Socket.js +14 -14
  53. package/lib/Types/State.js +26 -26
  54. package/lib/Types/USync.js +1 -1
  55. package/lib/Types/index.js +30 -30
  56. package/lib/Utils/auth-utils.js +159 -159
  57. package/lib/Utils/baileys-event-stream.js +32 -32
  58. package/lib/Utils/bot-capabilities.js +40 -40
  59. package/lib/Utils/bot-planning-replay.js +43 -43
  60. package/lib/Utils/browser-utils.js +29 -29
  61. package/lib/Utils/business.js +74 -74
  62. package/lib/Utils/chat-utils.js +316 -316
  63. package/lib/Utils/companion-reg-client-utils.js +19 -19
  64. package/lib/Utils/consumer-application.js +37 -37
  65. package/lib/Utils/crypto.js +52 -52
  66. package/lib/Utils/cstoken.js +14 -14
  67. package/lib/Utils/decode-wa-message.js +199 -199
  68. package/lib/Utils/event-buffer.js +272 -272
  69. package/lib/Utils/extras.js +186 -186
  70. package/lib/Utils/generics.js +195 -194
  71. package/lib/Utils/history.js +50 -50
  72. package/lib/Utils/identity-change-handler.js +18 -18
  73. package/lib/Utils/index.js +40 -40
  74. package/lib/Utils/link-preview.js +50 -50
  75. package/lib/Utils/logger.js +3 -2
  76. package/lib/Utils/lt-hash.js +3 -3
  77. package/lib/Utils/make-mutex.js +15 -15
  78. package/lib/Utils/message-composer.js +61 -61
  79. package/lib/Utils/message-retry-manager.js +47 -47
  80. package/lib/Utils/messages-media.js +501 -501
  81. package/lib/Utils/messages.js +944 -942
  82. package/lib/Utils/meta-ai-msmsg.js +121 -121
  83. package/lib/Utils/meta-compositing.js +74 -74
  84. package/lib/Utils/modern-messages.js +234 -234
  85. package/lib/Utils/native-flow.js +80 -80
  86. package/lib/Utils/noise-handler.js +160 -160
  87. package/lib/Utils/pre-key-manager.js +75 -75
  88. package/lib/Utils/process-message.js +400 -400
  89. package/lib/Utils/reporting-utils.js +170 -170
  90. package/lib/Utils/signal.js +166 -166
  91. package/lib/Utils/spam-report-utils.js +165 -165
  92. package/lib/Utils/sync-action-utils.js +33 -33
  93. package/lib/Utils/tc-token-utils.js +153 -153
  94. package/lib/Utils/use-keyv-auth-state.js +199 -199
  95. package/lib/Utils/use-multi-file-auth-state.js +83 -83
  96. package/lib/Utils/validate-connection.js +171 -171
  97. package/lib/WABinary/constants.js +1320 -1320
  98. package/lib/WABinary/decode.js +151 -151
  99. package/lib/WABinary/encode.js +139 -139
  100. package/lib/WABinary/generic-utils.js +202 -202
  101. package/lib/WABinary/index.js +6 -6
  102. package/lib/WABinary/jid-utils.js +235 -235
  103. package/lib/WABinary/types.js +4 -4
  104. package/lib/WAM/BinaryInfo.js +9 -9
  105. package/lib/WAM/constants.js +22852 -22852
  106. package/lib/WAM/encode.js +149 -149
  107. package/lib/WAM/index.js +3 -3
  108. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
  109. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
  110. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  111. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
  112. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
  113. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
  114. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
  115. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
  116. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
  117. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
  118. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
  119. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
  120. package/lib/WAUSync/Protocols/index.js +12 -12
  121. package/lib/WAUSync/USyncQuery.js +88 -88
  122. package/lib/WAUSync/USyncUser.js +20 -20
  123. package/lib/WAUSync/index.js +3 -3
  124. package/lib/index.js +66 -66
  125. package/package.json +187 -192
  126. package/WAProto/fetcher.js +0 -68
  127. package/WAProto/parser.js +0 -334
@@ -1,166 +1,166 @@
1
- import { getBinaryNodeChild, getBinaryNodeChildString, isLidUser, isHostedLidUser, isJidGroup, S_WHATSAPP_NET } from '../WABinary/index.js'
2
- import { extractMessageContent } from './messages.js'
3
-
4
- // ─── Constants ────────────────────────────────────────────────────────────────
5
-
6
- // Controls how WA routes the report internally — pass via options.spamFlow to override auto-selection.
7
- // ACCOUNT_INFO_REPORT → tag-based 1:1 contact report (default)
8
- // ACCOUNT_INFO_BLOCK → tag-based contact report signalling intent to block
9
- // GROUP_INFO_REPORT → tag-based group report (auto-selected for group JIDs)
10
- // CONTACT_INFO_REPORT → legacy lightweight report, no reporting_tag needed
11
- // ONE_TO_ONE_SPAM_BANNER_REPORT → from the in-chat spam banner
12
- // MESSAGE_MENU → from long-press message context menu
13
- // OVERFLOW_MENU_REPORT → from the chat overflow (⋮) menu
14
- export const SPAM_FLOWS = {
15
- ACCOUNT_INFO_REPORT: 'account_info_report',
16
- ACCOUNT_INFO_BLOCK: 'account_info_block',
17
- GROUP_INFO_REPORT: 'group_info_report',
18
- CONTACT_INFO_REPORT: 'contact_info_report',
19
- ONE_TO_ONE_SPAM_BANNER_REPORT: '1_1_spam_banner_report',
20
- MESSAGE_MENU: 'message_menu',
21
- OVERFLOW_MENU_REPORT: 'overflow_menu_report',
22
- }
23
-
24
- // Numeric reason codes for the w:report IQ endpoint — used by reportUser() and reportMessage().
25
- export const REPORT_REASON_CODES = { spam: 1, harassment: 2, impersonation: 3, scam: 4, other: 5 }
26
-
27
- // ─── Internal helpers ─────────────────────────────────────────────────────────
28
-
29
- const toBuffer = data => {
30
- if (!data) return null
31
- if (Buffer.isBuffer(data)) return data
32
- if (data instanceof Uint8Array) return Buffer.from(data)
33
- if (typeof data === 'string') return Buffer.from(data, 'base64')
34
- return null
35
- }
36
-
37
- // ─── JID resolution ───────────────────────────────────────────────────────────
38
-
39
- // Resolves a PN JID to its LID equivalent for tag-based spam IQs.
40
- // Groups and already-LID/hosted-LID JIDs pass through unchanged.
41
- export const resolveReportJid = async (jid, lidMapping) => {
42
- if (!jid) throw new Error('resolveReportJid: jid required')
43
- if (isJidGroup(jid) || isLidUser(jid) || isHostedLidUser(jid)) return jid
44
- const lid = lidMapping?.getLIDForPN ? await lidMapping.getLIDForPN(jid) : null
45
- return lid ?? jid
46
- }
47
-
48
- // Auto-detects JID type and returns the resolved JID + correct spam_flow for reportSpam().
49
- // Returns { reportJid, spamFlow, isGroup }.
50
- export const resolveReportContext = async (jid, lidMapping) => {
51
- if (!jid) throw new Error('resolveReportContext: jid required')
52
- if (isJidGroup(jid)) return { reportJid: jid, spamFlow: SPAM_FLOWS.GROUP_INFO_REPORT, isGroup: true }
53
- return { reportJid: await resolveReportJid(jid, lidMapping), spamFlow: SPAM_FLOWS.ACCOUNT_INFO_REPORT, isGroup: false }
54
- }
55
-
56
- // ─── Tag / text extraction ────────────────────────────────────────────────────
57
-
58
- // Normalizes a raw reporting_tag to a Buffer — returns null if invalid or empty.
59
- export const normalizeReportingTag = tag => {
60
- const buf = toBuffer(tag)
61
- return buf?.length ? buf : null
62
- }
63
-
64
- // Extracts plain text from a WAMessage across common message types.
65
- export const extractMessageText = msg => {
66
- if (!msg?.message) return ''
67
- const c = extractMessageContent(msg.message)
68
- if (!c) return ''
69
- return c.conversation ?? c.extendedTextMessage?.text ?? c.imageMessage?.caption ?? c.videoMessage?.caption ?? ''
70
- }
71
-
72
- // Extracts { stanzaId, timestamp, reportingTag, reportingToken, tokenVersion, validationPolicy }
73
- // from a raw incoming binary stanza. Call inside messages.upsert and persist via reportingInfoStore
74
- // so reportSpam() can use the tags later.
75
- export const extractReportingInfoFromStanza = stanza => {
76
- const reporting = getBinaryNodeChild(stanza, 'reporting')
77
- if (!reporting) return null
78
- const tokenNode = getBinaryNodeChild(reporting, 'reporting_token')
79
- const tagNode = getBinaryNodeChild(reporting, 'reporting_tag') ||
80
- getBinaryNodeChild(getBinaryNodeChild(reporting, 'reporting_validation'), 'reporting_tag')
81
- if (!tagNode?.content) return null
82
- const raw = tagNode.content
83
- const reportingTag = toBuffer(raw instanceof Uint8Array || Buffer.isBuffer(raw) ? raw : typeof raw === 'string' ? raw : null)
84
- if (!reportingTag?.length) return null
85
- const stanzaId = tagNode.attrs?.id || stanza.attrs?.id
86
- if (!stanzaId) return null
87
- const tsRaw = tagNode.attrs?.ts_s || stanza.attrs?.t
88
- return {
89
- stanzaId,
90
- timestamp: tsRaw ? Number(tsRaw) : 0,
91
- reportingTag,
92
- reportingToken: tokenNode?.content ? toBuffer(tokenNode.content) : null,
93
- tokenVersion: tokenNode?.attrs?.v ? parseInt(tokenNode.attrs.v) : null,
94
- validationPolicy: reporting.attrs?.validation_policy || null,
95
- }
96
- }
97
-
98
- // ─── Node builders ────────────────────────────────────────────────────────────
99
-
100
- // Builds <message> child nodes for a spam_list IQ.
101
- // Skips messages missing a reportingTag (warn) — throws only if all are filtered (error).
102
- export const buildSpamReportMessageNodes = (messages, logger) => {
103
- const ts = () => Math.floor(Date.now() / 1000)
104
- const nodes = messages.slice(0, 5).reduce((acc, m) => {
105
- const reportingTag = normalizeReportingTag(m.reportingTag)
106
- if (!reportingTag) {
107
- logger?.warn?.({ stanzaId: m.stanzaId }, 'buildSpamReportMessageNodes: skipping — missing reportingTag')
108
- return acc
109
- }
110
- const t = String(m.timestamp || ts())
111
- acc.push({
112
- tag: 'message',
113
- attrs: { t, id: m.stanzaId, reported_push_name: m.pushName || '', type: m.messageType || 'text', from: m.fromJid },
114
- content: [
115
- { tag: 'raw', attrs: { local_message_type: '0', v: '2' }, content: m.text != null ? String(m.text) : '' },
116
- { tag: 'reporting', attrs: {}, content: [{ tag: 'reporting_validation', attrs: {}, content: [{ tag: 'reporting_tag', attrs: { id: m.stanzaId, ts_s: t }, content: reportingTag }] }] },
117
- ],
118
- })
119
- return acc
120
- }, [])
121
- if (!nodes.length) {
122
- logger?.error?.({}, 'buildSpamReportMessageNodes: all messages filtered — none had a valid reportingTag')
123
- throw new Error('reportSpam: no valid messages with reportingTag after filtering')
124
- }
125
- return nodes
126
- }
127
-
128
- // Builds the full xmlns="spam" IQ stanza for tag-based reporting — used by reportSpam().
129
- export const buildSpamReportIq = (jid, messages, spamFlow = SPAM_FLOWS.ACCOUNT_INFO_REPORT, logger) => {
130
- if (!messages?.length) {
131
- logger?.error?.({ jid, spamFlow }, 'buildSpamReportIq: called with empty messages array')
132
- throw new Error('reportSpam: at least one message with reporting_tag required')
133
- }
134
- return {
135
- tag: 'iq',
136
- attrs: { type: 'set', xmlns: 'spam', to: S_WHATSAPP_NET },
137
- content: [{ tag: 'spam_list', attrs: { jid, spam_flow: spamFlow }, content: buildSpamReportMessageNodes(messages, logger) }],
138
- }
139
- }
140
-
141
- // Builds the simpler xmlns="spam" <report> node — used by reportContact() and reportGroup().
142
- // Does NOT require reporting_tags, only message key ids or bare id strings.
143
- export const buildSimpleReportNode = (jid, messageKeys) => ({
144
- tag: 'report',
145
- attrs: { jid, type: 'spam' },
146
- content: (messageKeys || []).map(key => ({ tag: 'message', attrs: { id: key.id || key, ...(key.fromMe ? {} : { from: jid }) } })),
147
- })
148
-
149
- // ─── Response parser ──────────────────────────────────────────────────────────
150
-
151
- // Parses the IQ result node from any spam report query.
152
- // Returns { success: true, reportId? } on success or { success: false, error } on failure.
153
- export const parseSpamReportResponse = result => {
154
- if (!result) return { success: false, error: 'empty response' }
155
- if (result.attrs?.type === 'error') {
156
- const errNode = getBinaryNodeChild(result, 'error')
157
- const code = errNode?.attrs?.code
158
- const text = errNode?.content?.toString?.() || errNode?.attrs?.text
159
- return { success: false, error: `spam IQ error${code ? ` ${code}` : ''}${text ? `: ${text}` : ''}` }
160
- }
161
- const reportNode = getBinaryNodeChild(result, 'report')
162
- const reportId = reportNode?.attrs?.id || getBinaryNodeChildString(reportNode, 'id')
163
- if (reportId) return { success: true, reportId: String(reportId) }
164
- if (result.attrs?.type === 'result') return { success: true }
165
- return { success: false, error: 'unexpected spam IQ response' }
1
+ import { getBinaryNodeChild, getBinaryNodeChildString, isLidUser, isHostedLidUser, isJidGroup, S_WHATSAPP_NET } from '../WABinary/index.js'
2
+ import { extractMessageContent } from './messages.js'
3
+
4
+ // ─── Constants ────────────────────────────────────────────────────────────────
5
+
6
+ // Controls how WA routes the report internally — pass via options.spamFlow to override auto-selection.
7
+ // ACCOUNT_INFO_REPORT → tag-based 1:1 contact report (default)
8
+ // ACCOUNT_INFO_BLOCK → tag-based contact report signalling intent to block
9
+ // GROUP_INFO_REPORT → tag-based group report (auto-selected for group JIDs)
10
+ // CONTACT_INFO_REPORT → legacy lightweight report, no reporting_tag needed
11
+ // ONE_TO_ONE_SPAM_BANNER_REPORT → from the in-chat spam banner
12
+ // MESSAGE_MENU → from long-press message context menu
13
+ // OVERFLOW_MENU_REPORT → from the chat overflow (⋮) menu
14
+ export const SPAM_FLOWS = {
15
+ ACCOUNT_INFO_REPORT: 'account_info_report',
16
+ ACCOUNT_INFO_BLOCK: 'account_info_block',
17
+ GROUP_INFO_REPORT: 'group_info_report',
18
+ CONTACT_INFO_REPORT: 'contact_info_report',
19
+ ONE_TO_ONE_SPAM_BANNER_REPORT: '1_1_spam_banner_report',
20
+ MESSAGE_MENU: 'message_menu',
21
+ OVERFLOW_MENU_REPORT: 'overflow_menu_report',
22
+ }
23
+
24
+ // Numeric reason codes for the w:report IQ endpoint — used by reportUser() and reportMessage().
25
+ export const REPORT_REASON_CODES = { spam: 1, harassment: 2, impersonation: 3, scam: 4, other: 5 }
26
+
27
+ // ─── Internal helpers ─────────────────────────────────────────────────────────
28
+
29
+ const toBuffer = data => {
30
+ if (!data) return null
31
+ if (Buffer.isBuffer(data)) return data
32
+ if (data instanceof Uint8Array) return Buffer.from(data)
33
+ if (typeof data === 'string') return Buffer.from(data, 'base64')
34
+ return null
35
+ }
36
+
37
+ // ─── JID resolution ───────────────────────────────────────────────────────────
38
+
39
+ // Resolves a PN JID to its LID equivalent for tag-based spam IQs.
40
+ // Groups and already-LID/hosted-LID JIDs pass through unchanged.
41
+ export const resolveReportJid = async (jid, lidMapping) => {
42
+ if (!jid) throw new Error('resolveReportJid: jid required')
43
+ if (isJidGroup(jid) || isLidUser(jid) || isHostedLidUser(jid)) return jid
44
+ const lid = lidMapping?.getLIDForPN ? await lidMapping.getLIDForPN(jid) : null
45
+ return lid ?? jid
46
+ }
47
+
48
+ // Auto-detects JID type and returns the resolved JID + correct spam_flow for reportSpam().
49
+ // Returns { reportJid, spamFlow, isGroup }.
50
+ export const resolveReportContext = async (jid, lidMapping) => {
51
+ if (!jid) throw new Error('resolveReportContext: jid required')
52
+ if (isJidGroup(jid)) return { reportJid: jid, spamFlow: SPAM_FLOWS.GROUP_INFO_REPORT, isGroup: true }
53
+ return { reportJid: await resolveReportJid(jid, lidMapping), spamFlow: SPAM_FLOWS.ACCOUNT_INFO_REPORT, isGroup: false }
54
+ }
55
+
56
+ // ─── Tag / text extraction ────────────────────────────────────────────────────
57
+
58
+ // Normalizes a raw reporting_tag to a Buffer — returns null if invalid or empty.
59
+ export const normalizeReportingTag = tag => {
60
+ const buf = toBuffer(tag)
61
+ return buf?.length ? buf : null
62
+ }
63
+
64
+ // Extracts plain text from a WAMessage across common message types.
65
+ export const extractMessageText = msg => {
66
+ if (!msg?.message) return ''
67
+ const c = extractMessageContent(msg.message)
68
+ if (!c) return ''
69
+ return c.conversation ?? c.extendedTextMessage?.text ?? c.imageMessage?.caption ?? c.videoMessage?.caption ?? ''
70
+ }
71
+
72
+ // Extracts { stanzaId, timestamp, reportingTag, reportingToken, tokenVersion, validationPolicy }
73
+ // from a raw incoming binary stanza. Call inside messages.upsert and persist via reportingInfoStore
74
+ // so reportSpam() can use the tags later.
75
+ export const extractReportingInfoFromStanza = stanza => {
76
+ const reporting = getBinaryNodeChild(stanza, 'reporting')
77
+ if (!reporting) return null
78
+ const tokenNode = getBinaryNodeChild(reporting, 'reporting_token')
79
+ const tagNode = getBinaryNodeChild(reporting, 'reporting_tag') ||
80
+ getBinaryNodeChild(getBinaryNodeChild(reporting, 'reporting_validation'), 'reporting_tag')
81
+ if (!tagNode?.content) return null
82
+ const raw = tagNode.content
83
+ const reportingTag = toBuffer(raw instanceof Uint8Array || Buffer.isBuffer(raw) ? raw : typeof raw === 'string' ? raw : null)
84
+ if (!reportingTag?.length) return null
85
+ const stanzaId = tagNode.attrs?.id || stanza.attrs?.id
86
+ if (!stanzaId) return null
87
+ const tsRaw = tagNode.attrs?.ts_s || stanza.attrs?.t
88
+ return {
89
+ stanzaId,
90
+ timestamp: tsRaw ? Number(tsRaw) : 0,
91
+ reportingTag,
92
+ reportingToken: tokenNode?.content ? toBuffer(tokenNode.content) : null,
93
+ tokenVersion: tokenNode?.attrs?.v ? parseInt(tokenNode.attrs.v) : null,
94
+ validationPolicy: reporting.attrs?.validation_policy || null,
95
+ }
96
+ }
97
+
98
+ // ─── Node builders ────────────────────────────────────────────────────────────
99
+
100
+ // Builds <message> child nodes for a spam_list IQ.
101
+ // Skips messages missing a reportingTag (warn) — throws only if all are filtered (error).
102
+ export const buildSpamReportMessageNodes = (messages, logger) => {
103
+ const ts = () => Math.floor(Date.now() / 1000)
104
+ const nodes = messages.slice(0, 5).reduce((acc, m) => {
105
+ const reportingTag = normalizeReportingTag(m.reportingTag)
106
+ if (!reportingTag) {
107
+ logger?.warn?.({ stanzaId: m.stanzaId }, 'buildSpamReportMessageNodes: skipping — missing reportingTag')
108
+ return acc
109
+ }
110
+ const t = String(m.timestamp || ts())
111
+ acc.push({
112
+ tag: 'message',
113
+ attrs: { t, id: m.stanzaId, reported_push_name: m.pushName || '', type: m.messageType || 'text', from: m.fromJid },
114
+ content: [
115
+ { tag: 'raw', attrs: { local_message_type: '0', v: '2' }, content: m.text != null ? String(m.text) : '' },
116
+ { tag: 'reporting', attrs: {}, content: [{ tag: 'reporting_validation', attrs: {}, content: [{ tag: 'reporting_tag', attrs: { id: m.stanzaId, ts_s: t }, content: reportingTag }] }] },
117
+ ],
118
+ })
119
+ return acc
120
+ }, [])
121
+ if (!nodes.length) {
122
+ logger?.error?.({}, 'buildSpamReportMessageNodes: all messages filtered — none had a valid reportingTag')
123
+ throw new Error('reportSpam: no valid messages with reportingTag after filtering')
124
+ }
125
+ return nodes
126
+ }
127
+
128
+ // Builds the full xmlns="spam" IQ stanza for tag-based reporting — used by reportSpam().
129
+ export const buildSpamReportIq = (jid, messages, spamFlow = SPAM_FLOWS.ACCOUNT_INFO_REPORT, logger) => {
130
+ if (!messages?.length) {
131
+ logger?.error?.({ jid, spamFlow }, 'buildSpamReportIq: called with empty messages array')
132
+ throw new Error('reportSpam: at least one message with reporting_tag required')
133
+ }
134
+ return {
135
+ tag: 'iq',
136
+ attrs: { type: 'set', xmlns: 'spam', to: S_WHATSAPP_NET },
137
+ content: [{ tag: 'spam_list', attrs: { jid, spam_flow: spamFlow }, content: buildSpamReportMessageNodes(messages, logger) }],
138
+ }
139
+ }
140
+
141
+ // Builds the simpler xmlns="spam" <report> node — used by reportContact() and reportGroup().
142
+ // Does NOT require reporting_tags, only message key ids or bare id strings.
143
+ export const buildSimpleReportNode = (jid, messageKeys) => ({
144
+ tag: 'report',
145
+ attrs: { jid, type: 'spam' },
146
+ content: (messageKeys || []).map(key => ({ tag: 'message', attrs: { id: key.id || key, ...(key.fromMe ? {} : { from: jid }) } })),
147
+ })
148
+
149
+ // ─── Response parser ──────────────────────────────────────────────────────────
150
+
151
+ // Parses the IQ result node from any spam report query.
152
+ // Returns { success: true, reportId? } on success or { success: false, error } on failure.
153
+ export const parseSpamReportResponse = result => {
154
+ if (!result) return { success: false, error: 'empty response' }
155
+ if (result.attrs?.type === 'error') {
156
+ const errNode = getBinaryNodeChild(result, 'error')
157
+ const code = errNode?.attrs?.code
158
+ const text = errNode?.content?.toString?.() || errNode?.attrs?.text
159
+ return { success: false, error: `spam IQ error${code ? ` ${code}` : ''}${text ? `: ${text}` : ''}` }
160
+ }
161
+ const reportNode = getBinaryNodeChild(result, 'report')
162
+ const reportId = reportNode?.attrs?.id || getBinaryNodeChildString(reportNode, 'id')
163
+ if (reportId) return { success: true, reportId: String(reportId) }
164
+ if (result.attrs?.type === 'result') return { success: true }
165
+ return { success: false, error: 'unexpected spam IQ response' }
166
166
  }
@@ -1,33 +1,33 @@
1
- import { isLidUser, isPnUser } from '../WABinary/index.js'
2
-
3
- export const processContactAction = (action, id, logger) => {
4
- const results = []
5
- if (!id) {
6
- logger?.warn({ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid }, 'contactAction sync: missing id in index')
7
- return results
8
- }
9
- const lidJid = action.lidJid
10
- const idIsPn = isPnUser(id)
11
- const phoneNumber = idIsPn ? id : action.pnJid || undefined
12
- results.push({
13
- event: 'contacts.upsert',
14
- data: [{
15
- id,
16
- name: action.fullName || action.firstName || action.username || undefined,
17
- username: action.username || undefined,
18
- lid: lidJid || undefined,
19
- phoneNumber
20
- }]
21
- })
22
- if (lidJid && isLidUser(lidJid) && idIsPn) {
23
- results.push({ event: 'lid-mapping.update', data: { lid: lidJid, pn: id } })
24
- }
25
- return results
26
- }
27
-
28
- export const emitSyncActionResults = (ev, results) => {
29
- for (const result of results) {
30
- if (result.event === 'contacts.upsert') ev.emit('contacts.upsert', result.data)
31
- else ev.emit('lid-mapping.update', result.data)
32
- }
33
- }
1
+ import { isLidUser, isPnUser } from '../WABinary/index.js'
2
+
3
+ export const processContactAction = (action, id, logger) => {
4
+ const results = []
5
+ if (!id) {
6
+ logger?.warn({ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid }, 'contactAction sync: missing id in index')
7
+ return results
8
+ }
9
+ const lidJid = action.lidJid
10
+ const idIsPn = isPnUser(id)
11
+ const phoneNumber = idIsPn ? id : action.pnJid || undefined
12
+ results.push({
13
+ event: 'contacts.upsert',
14
+ data: [{
15
+ id,
16
+ name: action.fullName || action.firstName || action.username || undefined,
17
+ username: action.username || undefined,
18
+ lid: lidJid || undefined,
19
+ phoneNumber
20
+ }]
21
+ })
22
+ if (lidJid && isLidUser(lidJid) && idIsPn) {
23
+ results.push({ event: 'lid-mapping.update', data: { lid: lidJid, pn: id } })
24
+ }
25
+ return results
26
+ }
27
+
28
+ export const emitSyncActionResults = (ev, results) => {
29
+ for (const result of results) {
30
+ if (result.event === 'contacts.upsert') ev.emit('contacts.upsert', result.data)
31
+ else ev.emit('lid-mapping.update', result.data)
32
+ }
33
+ }