@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.
- package/LICENSE +22 -22
- package/README.md +4124 -4112
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +7160 -0
- package/WAProto/WAProtoCompile.js +264 -322
- package/WAProto/index.js +55 -95
- package/lib/Builders/AIRich.js +673 -673
- package/lib/Builders/auto.js +49 -49
- package/lib/Builders/base.js +144 -144
- package/lib/Builders/builders.js +588 -588
- package/lib/Builders/index.js +3 -3
- package/lib/Defaults/index.js +249 -248
- package/lib/Signal/index.js +2 -2
- package/lib/Signal/libsignal.js +526 -526
- package/lib/Signal/lid-mapping.js +282 -282
- package/lib/Socket/Client/index.js +1 -1
- package/lib/Socket/Client/types.js +9 -9
- package/lib/Socket/Client/websocket.js +83 -83
- package/lib/Socket/business.js +151 -151
- package/lib/Socket/chats.js +854 -854
- package/lib/Socket/communities.js +146 -146
- package/lib/Socket/groups.js +293 -293
- package/lib/Socket/index.js +17 -14
- package/lib/Socket/messages-recv.js +993 -993
- package/lib/Socket/messages-send.js +722 -722
- package/lib/Socket/mex.js +33 -33
- package/lib/Socket/newsletter.js +783 -783
- package/lib/Socket/registration.js +154 -154
- package/lib/Socket/socket.js +797 -797
- package/lib/Socket/spam-report.js +143 -143
- package/lib/Socket/username.js +81 -81
- package/lib/Socket/zervonex-handler.js +57 -57
- package/lib/Store/index.js +6 -6
- package/lib/Store/make-cache-manager-store.js +80 -80
- package/lib/Store/make-in-memory-store.js +426 -426
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Store/reporting-info-store.js +37 -37
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +14 -14
- package/lib/Types/Newsletter.js +99 -99
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +14 -14
- package/lib/Types/State.js +26 -26
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +30 -30
- package/lib/Utils/auth-utils.js +159 -159
- package/lib/Utils/baileys-event-stream.js +32 -32
- package/lib/Utils/bot-capabilities.js +40 -40
- package/lib/Utils/bot-planning-replay.js +43 -43
- package/lib/Utils/browser-utils.js +29 -29
- package/lib/Utils/business.js +74 -74
- package/lib/Utils/chat-utils.js +316 -316
- package/lib/Utils/companion-reg-client-utils.js +19 -19
- package/lib/Utils/consumer-application.js +37 -37
- package/lib/Utils/crypto.js +52 -52
- package/lib/Utils/cstoken.js +14 -14
- package/lib/Utils/decode-wa-message.js +199 -199
- package/lib/Utils/event-buffer.js +272 -272
- package/lib/Utils/extras.js +186 -186
- package/lib/Utils/generics.js +195 -194
- package/lib/Utils/history.js +50 -50
- package/lib/Utils/identity-change-handler.js +18 -18
- package/lib/Utils/index.js +40 -40
- package/lib/Utils/link-preview.js +50 -50
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +3 -3
- package/lib/Utils/make-mutex.js +15 -15
- package/lib/Utils/message-composer.js +61 -61
- package/lib/Utils/message-retry-manager.js +47 -47
- package/lib/Utils/messages-media.js +501 -501
- package/lib/Utils/messages.js +944 -942
- package/lib/Utils/meta-ai-msmsg.js +121 -121
- package/lib/Utils/meta-compositing.js +74 -74
- package/lib/Utils/modern-messages.js +234 -234
- package/lib/Utils/native-flow.js +80 -80
- package/lib/Utils/noise-handler.js +160 -160
- package/lib/Utils/pre-key-manager.js +75 -75
- package/lib/Utils/process-message.js +400 -400
- package/lib/Utils/reporting-utils.js +170 -170
- package/lib/Utils/signal.js +166 -166
- package/lib/Utils/spam-report-utils.js +165 -165
- package/lib/Utils/sync-action-utils.js +33 -33
- package/lib/Utils/tc-token-utils.js +153 -153
- package/lib/Utils/use-keyv-auth-state.js +199 -199
- package/lib/Utils/use-multi-file-auth-state.js +83 -83
- package/lib/Utils/validate-connection.js +171 -171
- package/lib/WABinary/constants.js +1320 -1320
- package/lib/WABinary/decode.js +151 -151
- package/lib/WABinary/encode.js +139 -139
- package/lib/WABinary/generic-utils.js +202 -202
- package/lib/WABinary/index.js +6 -6
- package/lib/WABinary/jid-utils.js +235 -235
- package/lib/WABinary/types.js +4 -4
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
- package/lib/WAUSync/Protocols/index.js +12 -12
- package/lib/WAUSync/USyncQuery.js +88 -88
- package/lib/WAUSync/USyncUser.js +20 -20
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +66 -66
- package/package.json +187 -192
- package/WAProto/fetcher.js +0 -68
- 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
|
+
}
|