@alannxd/baileys 6.0.6 → 6.0.9
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 +1 -1
- package/README.md +341 -286
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +6902 -0
- package/WAProto/fix-imports.js +85 -0
- package/WAProto/index.d.ts +79257 -0
- package/WAProto/index.js +205861 -60565
- package/engine-requirements.js +1 -1
- package/lib/Defaults/index.js +119 -136
- package/lib/Signal/Group/ciphertext-message.js +2 -5
- package/lib/Signal/Group/group-session-builder.js +7 -41
- package/lib/Signal/Group/group_cipher.js +37 -51
- package/lib/Signal/Group/index.js +12 -57
- package/lib/Signal/Group/keyhelper.js +7 -44
- package/lib/Signal/Group/sender-chain-key.js +7 -15
- package/lib/Signal/Group/sender-key-distribution-message.js +8 -11
- package/lib/Signal/Group/sender-key-message.js +9 -12
- package/lib/Signal/Group/sender-key-name.js +2 -5
- package/lib/Signal/Group/sender-key-record.js +9 -21
- package/lib/Signal/Group/sender-key-state.js +27 -42
- package/lib/Signal/Group/sender-message-key.js +4 -7
- package/lib/Signal/libsignal.js +347 -90
- package/lib/Signal/lid-mapping.js +277 -0
- package/lib/Socket/Client/index.js +3 -19
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/business.js +162 -43
- package/lib/Socket/chats.js +627 -427
- package/lib/Socket/communities.js +90 -80
- package/lib/Socket/groups.js +154 -161
- package/lib/Socket/index.js +11 -10
- package/lib/Socket/luxu.js +315 -469
- package/lib/Socket/messages-recv.js +1421 -615
- package/lib/Socket/messages-send.js +1150 -799
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.js +152 -204
- package/lib/Socket/socket.js +544 -313
- package/lib/Store/index.js +10 -10
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +43 -41
- package/lib/Store/make-in-memory-store.js +112 -341
- package/lib/Store/make-ordered-dictionary.js +14 -20
- package/lib/Store/object-repository.js +11 -6
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.js +8 -4
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.js +3 -5
- package/lib/Types/LabelAssociation.js +3 -5
- package/lib/Types/Message.js +11 -9
- package/lib/Types/Mex.js +37 -0
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/State.js +56 -2
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +15 -31
- package/lib/Utils/auth-utils.js +239 -143
- package/lib/Utils/browser-utils.js +48 -0
- package/lib/Utils/business.js +66 -69
- package/lib/Utils/chat-utils.js +396 -253
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/crypto.js +57 -90
- package/lib/Utils/decode-wa-message.js +236 -84
- package/lib/Utils/event-buffer.js +185 -77
- package/lib/Utils/generics.js +189 -209
- package/lib/Utils/history.js +93 -55
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/index.js +23 -33
- package/lib/Utils/link-preview.js +16 -24
- package/lib/Utils/logger.js +3 -7
- package/lib/Utils/lt-hash.js +3 -46
- package/lib/Utils/make-mutex.js +24 -34
- package/lib/Utils/message-composer.js +273 -0
- package/lib/Utils/message-retry-manager.js +265 -0
- package/lib/Utils/messages-media.js +451 -482
- package/lib/Utils/messages.js +795 -369
- package/lib/Utils/noise-handler.js +145 -99
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/process-message.js +459 -150
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/signal.js +120 -72
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/tc-token-utils.js +163 -0
- package/lib/Utils/use-multi-file-auth-state.js +29 -27
- package/lib/Utils/validate-connection.js +73 -99
- package/lib/WABinary/constants.js +1281 -20
- package/lib/WABinary/decode.js +52 -42
- package/lib/WABinary/encode.js +110 -155
- package/lib/WABinary/generic-utils.js +55 -49
- package/lib/WABinary/index.js +6 -21
- package/lib/WABinary/jid-utils.js +76 -40
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +2 -5
- package/lib/WAM/constants.js +19071 -11568
- package/lib/WAM/encode.js +17 -22
- package/lib/WAM/index.js +4 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +33 -13
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -8
- package/lib/WAUSync/Protocols/index.js +6 -20
- package/lib/WAUSync/USyncQuery.js +44 -35
- package/lib/WAUSync/USyncUser.js +10 -5
- package/lib/WAUSync/index.js +4 -19
- package/lib/index.js +13 -36
- package/package.json +85 -51
- package/WAProto/fix-import.js +0 -29
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -53
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
- package/lib/Signal/Group/group-session-builder.d.ts +0 -14
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -11
- package/lib/Signal/Group/keyhelper.d.ts +0 -10
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -13
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -16
- package/lib/Signal/Group/sender-key-message.d.ts +0 -18
- package/lib/Signal/Group/sender-key-name.d.ts +0 -17
- package/lib/Signal/Group/sender-key-record.d.ts +0 -30
- package/lib/Signal/Group/sender-key-state.d.ts +0 -38
- package/lib/Signal/Group/sender-message-key.d.ts +0 -11
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.d.ts +0 -12
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/business.d.ts +0 -171
- package/lib/Socket/chats.d.ts +0 -267
- package/lib/Socket/communities.d.ts +0 -180
- package/lib/Socket/groups.d.ts +0 -115
- package/lib/Socket/index.d.ts +0 -173
- package/lib/Socket/luxu.d.ts +0 -266
- package/lib/Socket/messages-recv.d.ts +0 -161
- package/lib/Socket/messages-send.d.ts +0 -183
- package/lib/Socket/newsletter.d.ts +0 -134
- package/lib/Socket/registration.d.ts +0 -267
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/socket.d.ts +0 -44
- package/lib/Socket/usync.d.ts +0 -36
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -13
- package/lib/Store/make-in-memory-store.d.ts +0 -118
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -110
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -102
- package/lib/Types/Contact.d.ts +0 -19
- package/lib/Types/Events.d.ts +0 -157
- package/lib/Types/GroupMetadata.d.ts +0 -55
- package/lib/Types/Label.d.ts +0 -35
- package/lib/Types/LabelAssociation.d.ts +0 -29
- package/lib/Types/Message.d.ts +0 -273
- package/lib/Types/Newsletter.d.ts +0 -103
- package/lib/Types/Newsletter.js +0 -38
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/Socket.d.ts +0 -111
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/USync.d.ts +0 -25
- package/lib/Types/index.d.ts +0 -57
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/baileys-event-stream.js +0 -63
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -71
- package/lib/Utils/crypto.d.ts +0 -41
- package/lib/Utils/decode-wa-message.d.ts +0 -19
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -15
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -4
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -116
- package/lib/Utils/messages.d.ts +0 -77
- package/lib/Utils/noise-handler.d.ts +0 -21
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -32
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -30
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -17
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -31
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/WAM/BinaryInfo.d.ts +0 -17
- package/lib/WAM/constants.d.ts +0 -38
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -25
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -8
- package/lib/WAUSync/Protocols/index.d.ts +0 -4
- package/lib/WAUSync/USyncQuery.d.ts +0 -28
- package/lib/WAUSync/USyncUser.d.ts +0 -12
- package/lib/index.d.ts +0 -12
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js'
|
|
2
|
+
import { generateMessageIDV2 } from './generics.js'
|
|
3
|
+
|
|
4
|
+
const JS_KEYWORDS = new Set([
|
|
5
|
+
'import', 'export', 'from', 'default', 'as', 'const', 'let', 'var',
|
|
6
|
+
'function', 'class', 'extends', 'new', 'return', 'if', 'else', 'for',
|
|
7
|
+
'while', 'do', 'switch', 'case', 'break', 'continue', 'try', 'catch',
|
|
8
|
+
'finally', 'throw', 'async', 'await', 'yield', 'typeof', 'instanceof',
|
|
9
|
+
'in', 'of', 'delete', 'void', 'true', 'false', 'null', 'undefined',
|
|
10
|
+
'NaN', 'Infinity', 'this', 'super', 'static', 'get', 'set', 'debugger', 'with'
|
|
11
|
+
])
|
|
12
|
+
|
|
13
|
+
const PYTHON_KEYWORDS = new Set([
|
|
14
|
+
'import', 'from', 'as', 'def', 'class', 'return', 'if', 'elif', 'else',
|
|
15
|
+
'for', 'while', 'break', 'continue', 'try', 'except', 'finally', 'raise',
|
|
16
|
+
'with', 'yield', 'lambda', 'pass', 'del', 'global', 'nonlocal', 'assert',
|
|
17
|
+
'True', 'False', 'None', 'and', 'or', 'not', 'in', 'is', 'async', 'await',
|
|
18
|
+
'self', 'print'
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
const LANGUAGE_KEYWORDS = {
|
|
22
|
+
javascript: JS_KEYWORDS,
|
|
23
|
+
typescript: JS_KEYWORDS,
|
|
24
|
+
js: JS_KEYWORDS,
|
|
25
|
+
ts: JS_KEYWORDS,
|
|
26
|
+
python: PYTHON_KEYWORDS,
|
|
27
|
+
py: PYTHON_KEYWORDS
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export var CodeHighlightType
|
|
31
|
+
;(function (CodeHighlightType) {
|
|
32
|
+
CodeHighlightType[(CodeHighlightType['DEFAULT'] = 0)] = 'DEFAULT'
|
|
33
|
+
CodeHighlightType[(CodeHighlightType['KEYWORD'] = 1)] = 'KEYWORD'
|
|
34
|
+
CodeHighlightType[(CodeHighlightType['METHOD'] = 2)] = 'METHOD'
|
|
35
|
+
CodeHighlightType[(CodeHighlightType['STRING'] = 3)] = 'STRING'
|
|
36
|
+
CodeHighlightType[(CodeHighlightType['NUMBER'] = 4)] = 'NUMBER'
|
|
37
|
+
CodeHighlightType[(CodeHighlightType['COMMENT'] = 5)] = 'COMMENT'
|
|
38
|
+
})(CodeHighlightType || (CodeHighlightType = {}))
|
|
39
|
+
|
|
40
|
+
export var RichSubMessageType
|
|
41
|
+
;(function (RichSubMessageType) {
|
|
42
|
+
RichSubMessageType[(RichSubMessageType['UNKNOWN'] = 0)] = 'UNKNOWN'
|
|
43
|
+
RichSubMessageType[(RichSubMessageType['GRID_IMAGE'] = 1)] = 'GRID_IMAGE'
|
|
44
|
+
RichSubMessageType[(RichSubMessageType['TEXT'] = 2)] = 'TEXT'
|
|
45
|
+
RichSubMessageType[(RichSubMessageType['INLINE_IMAGE'] = 3)] = 'INLINE_IMAGE'
|
|
46
|
+
RichSubMessageType[(RichSubMessageType['TABLE'] = 4)] = 'TABLE'
|
|
47
|
+
RichSubMessageType[(RichSubMessageType['CODE'] = 5)] = 'CODE'
|
|
48
|
+
RichSubMessageType[(RichSubMessageType['DYNAMIC'] = 6)] = 'DYNAMIC'
|
|
49
|
+
RichSubMessageType[(RichSubMessageType['MAP'] = 7)] = 'MAP'
|
|
50
|
+
RichSubMessageType[(RichSubMessageType['LATEX'] = 8)] = 'LATEX'
|
|
51
|
+
RichSubMessageType[(RichSubMessageType['CONTENT_ITEMS'] = 9)] = 'CONTENT_ITEMS'
|
|
52
|
+
})(RichSubMessageType || (RichSubMessageType = {}))
|
|
53
|
+
|
|
54
|
+
export const tokenizeCode = (codeStr, language = 'javascript') => {
|
|
55
|
+
const keywords = LANGUAGE_KEYWORDS[language] || JS_KEYWORDS
|
|
56
|
+
const blocks = []
|
|
57
|
+
const lines = codeStr.split('\n')
|
|
58
|
+
for (let li = 0; li < lines.length; li++) {
|
|
59
|
+
const line = lines[li]
|
|
60
|
+
const isLast = li === lines.length - 1
|
|
61
|
+
const nl = isLast? '' : '\n'
|
|
62
|
+
if (!line.trim()) {
|
|
63
|
+
blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: line + nl })
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
if (line.trim().startsWith('//') || line.trim().startsWith('#')) {
|
|
67
|
+
blocks.push({ highlightType: CodeHighlightType.COMMENT, codeContent: line + nl })
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
const regex = /(\/\/.*$|#.*$)|(["'`](?:[^"'`\\]|\\.)*["'`])|(\b\d+(?:\.\d+)?\b)|(\b[a-zA-Z_$][\w$]*\b)|([^\s\w$"'`]+)|(\s+)/g
|
|
71
|
+
let match
|
|
72
|
+
const tokens = []
|
|
73
|
+
while ((match = regex.exec(line))!== null) {
|
|
74
|
+
const val = match[0]
|
|
75
|
+
if (match[1]) {
|
|
76
|
+
tokens.push({ highlightType: CodeHighlightType.COMMENT, codeContent: val })
|
|
77
|
+
} else if (match[2]) {
|
|
78
|
+
tokens.push({ highlightType: CodeHighlightType.STRING, codeContent: val })
|
|
79
|
+
} else if (match[3]) {
|
|
80
|
+
tokens.push({ highlightType: CodeHighlightType.NUMBER, codeContent: val })
|
|
81
|
+
} else if (match[4]) {
|
|
82
|
+
if (keywords.has(val)) {
|
|
83
|
+
tokens.push({ highlightType: CodeHighlightType.KEYWORD, codeContent: val })
|
|
84
|
+
} else {
|
|
85
|
+
const after = line.slice(regex.lastIndex).trimStart()
|
|
86
|
+
if (after.startsWith('(')) {
|
|
87
|
+
tokens.push({ highlightType: CodeHighlightType.METHOD, codeContent: val })
|
|
88
|
+
} else {
|
|
89
|
+
tokens.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: val })
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
tokens.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: val })
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (tokens.length === 0) {
|
|
97
|
+
blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: line + nl })
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
const merged = []
|
|
101
|
+
for (const t of tokens) {
|
|
102
|
+
const prev = merged.length > 0? merged[merged.length - 1] : undefined
|
|
103
|
+
if (prev && prev.highlightType === t.highlightType) {
|
|
104
|
+
prev.codeContent += t.codeContent
|
|
105
|
+
} else {
|
|
106
|
+
merged.push({...t })
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (merged.length > 0) {
|
|
110
|
+
merged[merged.length - 1].codeContent += nl
|
|
111
|
+
}
|
|
112
|
+
blocks.push(...merged)
|
|
113
|
+
}
|
|
114
|
+
return blocks
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const buildRichContextInfo = (quoted, options) => {
|
|
118
|
+
const ctxInfo = {
|
|
119
|
+
forwardingScore: 1,
|
|
120
|
+
isForwarded: true,
|
|
121
|
+
forwardedAiBotMessageInfo: { botJid: options?.botJid? options.botJid : '867051314767696@bot' },
|
|
122
|
+
forwardOrigin: 4,
|
|
123
|
+
...(options?.mentions? { mentionedJid: options.mentions } : {})
|
|
124
|
+
}
|
|
125
|
+
if (quoted?.key) {
|
|
126
|
+
ctxInfo.stanzaId = quoted.key.id
|
|
127
|
+
ctxInfo.participant = quoted.key.participant || quoted.sender || quoted.key.remoteJid
|
|
128
|
+
ctxInfo.quotedMessage = quoted.message
|
|
129
|
+
}
|
|
130
|
+
return ctxInfo
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const buildBotForwardedMessage = (submessages, contextInfo, unifiedResponse) => {
|
|
134
|
+
const richResponse = { messageType: 1, submessages, contextInfo }
|
|
135
|
+
if (unifiedResponse) {
|
|
136
|
+
richResponse.unifiedResponse = unifiedResponse
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
botForwardedMessage: {
|
|
140
|
+
message: { richResponseMessage: richResponse }
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const generateTableContent = (title, headers, rows, quoted, options = {}) => {
|
|
146
|
+
const { footer, headerText } = options
|
|
147
|
+
const tableRows = [{ items: headers, isHeading: true },...rows.map(row => ({ items: row.map(String) }))]
|
|
148
|
+
const submessages = []
|
|
149
|
+
if (headerText) submessages.push({ messageType: 2, messageText: headerText })
|
|
150
|
+
submessages.push({ messageType: 4, tableMetadata: { title, rows: tableRows } })
|
|
151
|
+
if (footer) submessages.push({ messageType: 2, messageText: footer })
|
|
152
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
153
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const generateListContent = (title, items, quoted, options = {}) => {
|
|
157
|
+
const { footer, headerText } = options
|
|
158
|
+
const tableRows = items.map(item => ({
|
|
159
|
+
items: Array.isArray(item)? item.map(String) : [String(item)]
|
|
160
|
+
}))
|
|
161
|
+
const submessages = []
|
|
162
|
+
if (headerText) submessages.push({ messageType: 2, messageText: headerText })
|
|
163
|
+
submessages.push({ messageType: 4, tableMetadata: { title, rows: tableRows } })
|
|
164
|
+
if (footer) submessages.push({ messageType: 2, messageText: footer })
|
|
165
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
166
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export const generateCodeBlockContent = (code, quoted, options = {}) => {
|
|
170
|
+
const { title, footer, language = 'javascript' } = options
|
|
171
|
+
const submessages = []
|
|
172
|
+
if (title) submessages.push({ messageType: 2, messageText: title })
|
|
173
|
+
submessages.push({
|
|
174
|
+
messageType: 5,
|
|
175
|
+
codeMetadata: { codeLanguage: language, codeBlocks: tokenizeCode(code, language) }
|
|
176
|
+
})
|
|
177
|
+
if (footer) submessages.push({ messageType: 2, messageText: footer })
|
|
178
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
179
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export const generateLatexContent = (quoted, options) => {
|
|
183
|
+
const { text, expressions, headerText, footer } = options
|
|
184
|
+
const submessages = []
|
|
185
|
+
if (headerText) submessages.push({ messageType: 2, messageText: headerText })
|
|
186
|
+
const latexExpressions = expressions.map(expr => {
|
|
187
|
+
const entry = {
|
|
188
|
+
latexExpression: expr.latexExpression,
|
|
189
|
+
url: expr.url,
|
|
190
|
+
width: expr.width,
|
|
191
|
+
height: expr.height
|
|
192
|
+
}
|
|
193
|
+
if (expr.fontHeight!== undefined) entry.fontHeight = expr.fontHeight
|
|
194
|
+
if (expr.imageTopPadding!== undefined) entry.imageTopPadding = expr.imageTopPadding
|
|
195
|
+
if (expr.imageLeadingPadding!== undefined) entry.imageLeadingPadding = expr.imageLeadingPadding
|
|
196
|
+
if (expr.imageBottomPadding!== undefined) entry.imageBottomPadding = expr.imageBottomPadding
|
|
197
|
+
if (expr.imageTrailingPadding!== undefined) entry.imageTrailingPadding = expr.imageTrailingPadding
|
|
198
|
+
return entry
|
|
199
|
+
})
|
|
200
|
+
submessages.push({ messageType: 8, latexMetadata: { text: text || '', expressions: latexExpressions } })
|
|
201
|
+
if (footer) submessages.push({ messageType: 2, messageText: footer })
|
|
202
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
203
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export const generateLatexImageContent = async (quoted, options, uploadFn, renderLatexToPng) => {
|
|
207
|
+
const { text, expressions, headerText, footer } = options
|
|
208
|
+
const submessages = []
|
|
209
|
+
if (headerText) submessages.push({ messageType: 2, messageText: headerText })
|
|
210
|
+
const latexExpressions = await Promise.all(
|
|
211
|
+
expressions.map(async expr => {
|
|
212
|
+
const { buffer, width, height } = await renderLatexToPng(expr.latexExpression)
|
|
213
|
+
const uploadResult = await uploadFn(buffer, 'image')
|
|
214
|
+
const imageUrl = uploadResult.url || uploadResult.directPath
|
|
215
|
+
return { latexExpression: expr.latexExpression, url: imageUrl, width, height }
|
|
216
|
+
})
|
|
217
|
+
)
|
|
218
|
+
submessages.push({ messageType: 8, latexMetadata: { text: text || '', expressions: latexExpressions } })
|
|
219
|
+
if (footer) submessages.push({ messageType: 2, messageText: footer })
|
|
220
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
221
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export const generateLatexInlineImageContent = async (quoted, options, uploadFn, renderLatexToPng) => {
|
|
225
|
+
const { text, expressions, headerText, footer } = options
|
|
226
|
+
const submessages = []
|
|
227
|
+
if (headerText) submessages.push({ messageType: 2, messageText: headerText })
|
|
228
|
+
if (text) submessages.push({ messageType: 2, messageText: text })
|
|
229
|
+
for (const expr of expressions) {
|
|
230
|
+
const { buffer, width, height } = await renderLatexToPng(expr.latexExpression)
|
|
231
|
+
const uploadResult = await uploadFn(buffer, 'image')
|
|
232
|
+
const imageUrl = uploadResult.url || uploadResult.directPath
|
|
233
|
+
submessages.push({
|
|
234
|
+
messageType: 3,
|
|
235
|
+
imageMetadata: {
|
|
236
|
+
imageUrl: { imagePreviewUrl: imageUrl, imageHighResUrl: imageUrl },
|
|
237
|
+
imageText: expr.latexExpression,
|
|
238
|
+
alignment: 2
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
if (footer) submessages.push({ messageType: 2, messageText: footer })
|
|
243
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
244
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export const captureUnifiedResponse = msg => {
|
|
248
|
+
const botFwd = msg?.botForwardedMessage?.message
|
|
249
|
+
if (!botFwd) return null
|
|
250
|
+
const rich = botFwd.richResponseMessage
|
|
251
|
+
if (!rich?.unifiedResponse?.data) return null
|
|
252
|
+
return {
|
|
253
|
+
unifiedResponse: { data: rich.unifiedResponse.data },
|
|
254
|
+
submessages: rich.submessages || [],
|
|
255
|
+
contextInfo: rich.contextInfo || {}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export const generateUnifiedResponseContent = (quoted, captured) => {
|
|
260
|
+
const ctxInfo = buildRichContextInfo(quoted)
|
|
261
|
+
return { message: buildBotForwardedMessage(captured.submessages, ctxInfo, captured.unifiedResponse), messageId: generateMessageIDV2() }
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export const generateRichMessageContent = (submessages, quoted, options) => {
|
|
265
|
+
const ctxInfo = buildRichContextInfo(quoted, options)
|
|
266
|
+
return { message: buildBotForwardedMessage(submessages, ctxInfo), messageId: generateMessageIDV2() }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export {
|
|
270
|
+
JS_KEYWORDS,
|
|
271
|
+
PYTHON_KEYWORDS,
|
|
272
|
+
LANGUAGE_KEYWORDS
|
|
273
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
/** Number of sent messages to cache in memory for handling retry receipts */
|
|
3
|
+
const RECENT_MESSAGES_SIZE = 512;
|
|
4
|
+
const MESSAGE_KEY_SEPARATOR = '\u0000';
|
|
5
|
+
/** Timeout for session recreation - 1 hour */
|
|
6
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
7
|
+
const PHONE_REQUEST_DELAY = 3000;
|
|
8
|
+
// Retry reason codes matching WhatsApp Web's Signal error codes.
|
|
9
|
+
export var RetryReason;
|
|
10
|
+
(function (RetryReason) {
|
|
11
|
+
RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
|
|
12
|
+
RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
|
|
13
|
+
RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
|
|
14
|
+
RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
|
|
15
|
+
/** MAC verification failed - most common cause of decryption failures */
|
|
16
|
+
RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
|
|
17
|
+
RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
|
|
18
|
+
RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
|
|
19
|
+
/** Explicit MAC failure - session is definitely out of sync */
|
|
20
|
+
RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
|
|
21
|
+
RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
|
|
22
|
+
RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
|
|
23
|
+
RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
|
|
24
|
+
RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
|
|
25
|
+
RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
|
|
26
|
+
RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
|
|
27
|
+
})(RetryReason || (RetryReason = {}));
|
|
28
|
+
/** Error codes that indicate a MAC failure and require immediate session recreation */
|
|
29
|
+
const MAC_ERROR_CODES = new Set([RetryReason.SignalErrorInvalidMessage, RetryReason.SignalErrorBadMac]);
|
|
30
|
+
export class MessageRetryManager {
|
|
31
|
+
constructor(logger, maxMsgRetryCount) {
|
|
32
|
+
this.logger = logger;
|
|
33
|
+
this.recentMessagesMap = new LRUCache({
|
|
34
|
+
max: RECENT_MESSAGES_SIZE,
|
|
35
|
+
ttl: 5 * 60 * 1000,
|
|
36
|
+
ttlAutopurge: true,
|
|
37
|
+
dispose: (_value, key) => {
|
|
38
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
39
|
+
if (separatorIndex > -1) {
|
|
40
|
+
const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
|
|
41
|
+
this.messageKeyIndex.delete(messageId);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
this.messageKeyIndex = new Map();
|
|
46
|
+
this.sessionRecreateHistory = new LRUCache({
|
|
47
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
48
|
+
ttlAutopurge: true
|
|
49
|
+
});
|
|
50
|
+
this.retryCounters = new LRUCache({
|
|
51
|
+
ttl: 15 * 60 * 1000,
|
|
52
|
+
ttlAutopurge: true,
|
|
53
|
+
updateAgeOnGet: true
|
|
54
|
+
}); // 15 minutes TTL
|
|
55
|
+
this.baseKeys = new LRUCache({
|
|
56
|
+
max: 1024,
|
|
57
|
+
ttl: 15 * 60 * 1000,
|
|
58
|
+
ttlAutopurge: true
|
|
59
|
+
});
|
|
60
|
+
this.pendingPhoneRequests = {};
|
|
61
|
+
this.maxMsgRetryCount = 5;
|
|
62
|
+
this.statistics = {
|
|
63
|
+
totalRetries: 0,
|
|
64
|
+
successfulRetries: 0,
|
|
65
|
+
failedRetries: 0,
|
|
66
|
+
mediaRetries: 0,
|
|
67
|
+
sessionRecreations: 0,
|
|
68
|
+
phoneRequests: 0
|
|
69
|
+
};
|
|
70
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Add a recent message to the cache for retry handling
|
|
74
|
+
*/
|
|
75
|
+
addRecentMessage(to, id, message) {
|
|
76
|
+
const key = { to, id };
|
|
77
|
+
const keyStr = this.keyToString(key);
|
|
78
|
+
// Add new message
|
|
79
|
+
this.recentMessagesMap.set(keyStr, {
|
|
80
|
+
message,
|
|
81
|
+
timestamp: Date.now()
|
|
82
|
+
});
|
|
83
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
84
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Get a recent message from the cache
|
|
88
|
+
*/
|
|
89
|
+
getRecentMessage(to, id) {
|
|
90
|
+
const key = { to, id };
|
|
91
|
+
const keyStr = this.keyToString(key);
|
|
92
|
+
return this.recentMessagesMap.get(keyStr);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if a session should be recreated based on retry count, history, and error code.
|
|
96
|
+
* MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
|
|
97
|
+
*/
|
|
98
|
+
shouldRecreateSession(jid, hasSession, errorCode) {
|
|
99
|
+
// If we don't have a session, always recreate
|
|
100
|
+
if (!hasSession) {
|
|
101
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
102
|
+
this.statistics.sessionRecreations++;
|
|
103
|
+
return {
|
|
104
|
+
reason: "we don't have a Signal session with them",
|
|
105
|
+
recreate: true
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
// IMMEDIATE recreation for MAC errors - session is definitely out of sync
|
|
109
|
+
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
110
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
111
|
+
this.statistics.sessionRecreations++;
|
|
112
|
+
this.logger.warn({ jid, errorCode: RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
|
|
113
|
+
return {
|
|
114
|
+
reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
|
|
115
|
+
recreate: true
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const now = Date.now();
|
|
119
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
120
|
+
// If no previous recreation or it's been more than an hour
|
|
121
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
122
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
123
|
+
this.statistics.sessionRecreations++;
|
|
124
|
+
return {
|
|
125
|
+
reason: 'retry count > 1 and over an hour since last recreation',
|
|
126
|
+
recreate: true
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return { reason: '', recreate: false };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Parse error code from retry receipt's retry node.
|
|
133
|
+
* Returns undefined if no error code is present.
|
|
134
|
+
*/
|
|
135
|
+
parseRetryErrorCode(errorAttr) {
|
|
136
|
+
if (errorAttr === undefined || errorAttr === '') {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const code = parseInt(errorAttr, 10);
|
|
140
|
+
if (Number.isNaN(code)) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
// Validate it's a known RetryReason
|
|
144
|
+
if (code >= RetryReason.UnknownError && code <= RetryReason.StatusRevokeDelay) {
|
|
145
|
+
return code;
|
|
146
|
+
}
|
|
147
|
+
return RetryReason.UnknownError;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if an error code indicates a MAC failure
|
|
151
|
+
*/
|
|
152
|
+
isMacError(errorCode) {
|
|
153
|
+
return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Increment retry counter for a message
|
|
157
|
+
*/
|
|
158
|
+
incrementRetryCount(messageId) {
|
|
159
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
160
|
+
this.statistics.totalRetries++;
|
|
161
|
+
return this.retryCounters.get(messageId);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get retry count for a message
|
|
165
|
+
*/
|
|
166
|
+
getRetryCount(messageId) {
|
|
167
|
+
return this.retryCounters.get(messageId) || 0;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Check if message has exceeded maximum retry attempts
|
|
171
|
+
*/
|
|
172
|
+
hasExceededMaxRetries(messageId) {
|
|
173
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Mark retry as successful
|
|
177
|
+
*/
|
|
178
|
+
markRetrySuccess(messageId) {
|
|
179
|
+
this.statistics.successfulRetries++;
|
|
180
|
+
// Clean up retry counter for successful message
|
|
181
|
+
this.retryCounters.delete(messageId);
|
|
182
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
183
|
+
this.removeRecentMessage(messageId);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Mark retry as failed
|
|
187
|
+
*/
|
|
188
|
+
markRetryFailed(messageId) {
|
|
189
|
+
this.statistics.failedRetries++;
|
|
190
|
+
this.retryCounters.delete(messageId);
|
|
191
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
192
|
+
this.removeRecentMessage(messageId);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Schedule a phone request with delay
|
|
196
|
+
*/
|
|
197
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
198
|
+
// Cancel any existing request for this message
|
|
199
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
200
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
201
|
+
delete this.pendingPhoneRequests[messageId];
|
|
202
|
+
this.statistics.phoneRequests++;
|
|
203
|
+
callback();
|
|
204
|
+
}, delay);
|
|
205
|
+
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Cancel pending phone request
|
|
209
|
+
*/
|
|
210
|
+
cancelPendingPhoneRequest(messageId) {
|
|
211
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
212
|
+
if (timeout) {
|
|
213
|
+
clearTimeout(timeout);
|
|
214
|
+
delete this.pendingPhoneRequests[messageId];
|
|
215
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
clear() {
|
|
219
|
+
this.recentMessagesMap.clear();
|
|
220
|
+
this.messageKeyIndex.clear();
|
|
221
|
+
this.sessionRecreateHistory.clear();
|
|
222
|
+
this.retryCounters.clear();
|
|
223
|
+
this.baseKeys.clear();
|
|
224
|
+
for (const messageId of Object.keys(this.pendingPhoneRequests)) {
|
|
225
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
226
|
+
}
|
|
227
|
+
this.statistics = {
|
|
228
|
+
totalRetries: 0,
|
|
229
|
+
successfulRetries: 0,
|
|
230
|
+
failedRetries: 0,
|
|
231
|
+
mediaRetries: 0,
|
|
232
|
+
sessionRecreations: 0,
|
|
233
|
+
phoneRequests: 0
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
saveBaseKey(addr, msgId, baseKey) {
|
|
237
|
+
this.baseKeys.set(`${addr}:${msgId}`, baseKey);
|
|
238
|
+
}
|
|
239
|
+
hasSameBaseKey(addr, msgId, baseKey) {
|
|
240
|
+
const stored = this.baseKeys.get(`${addr}:${msgId}`);
|
|
241
|
+
if (!stored || stored.length !== baseKey.length) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
for (let i = 0; i < stored.length; i++) {
|
|
245
|
+
if (stored[i] !== baseKey[i])
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
deleteBaseKey(addr, msgId) {
|
|
251
|
+
this.baseKeys.delete(`${addr}:${msgId}`);
|
|
252
|
+
}
|
|
253
|
+
keyToString(key) {
|
|
254
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
255
|
+
}
|
|
256
|
+
removeRecentMessage(messageId) {
|
|
257
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
258
|
+
if (!keyStr) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
this.recentMessagesMap.delete(keyStr);
|
|
262
|
+
this.messageKeyIndex.delete(messageId);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=message-retry-manager.js.map
|