@vanzxy/baileys 1.4.0 → 1.4.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.
Potentially problematic release.
This version of @vanzxy/baileys might be problematic. Click here for more details.
- package/cjs/WAProto/index.js +106053 -0
- package/cjs/lib/Defaults/index.js +168 -0
- package/cjs/lib/Signal/Group/ciphertext-message.js +16 -0
- package/cjs/lib/Signal/Group/group-session-builder.js +67 -0
- package/cjs/lib/Signal/Group/group_cipher.js +86 -0
- package/cjs/lib/Signal/Group/index.js +58 -0
- package/cjs/lib/Signal/Group/keyhelper.js +56 -0
- package/cjs/lib/Signal/Group/sender-chain-key.js +30 -0
- package/cjs/lib/Signal/Group/sender-key-distribution-message.js +67 -0
- package/cjs/lib/Signal/Group/sender-key-message.js +70 -0
- package/cjs/lib/Signal/Group/sender-key-name.js +52 -0
- package/cjs/lib/Signal/Group/sender-key-record.js +45 -0
- package/cjs/lib/Signal/Group/sender-key-state.js +88 -0
- package/cjs/lib/Signal/Group/sender-message-key.js +30 -0
- package/cjs/lib/Signal/libsignal.js +467 -0
- package/cjs/lib/Signal/lid-mapping.js +281 -0
- package/cjs/lib/Socket/Client/index.js +19 -0
- package/cjs/lib/Socket/Client/types.js +15 -0
- package/cjs/lib/Socket/Client/websocket.js +61 -0
- package/cjs/lib/Socket/business.js +384 -0
- package/cjs/lib/Socket/chats.js +1257 -0
- package/cjs/lib/Socket/communities.js +442 -0
- package/cjs/lib/Socket/dugong.js +753 -0
- package/cjs/lib/Socket/groups.js +360 -0
- package/cjs/lib/Socket/index.js +19 -0
- package/cjs/lib/Socket/messages-recv.js +1790 -0
- package/cjs/lib/Socket/messages-send.js +1390 -0
- package/cjs/lib/Socket/mex.js +46 -0
- package/cjs/lib/Socket/newsletter.js +340 -0
- package/cjs/lib/Socket/socket.js +1007 -0
- package/cjs/lib/Store/index.js +20 -0
- package/cjs/lib/Store/make-in-memory-store.js +438 -0
- package/cjs/lib/Store/make-ordered-dictionary.js +82 -0
- package/cjs/lib/Store/object-repository.js +28 -0
- package/cjs/lib/Types/Auth.js +3 -0
- package/cjs/lib/Types/Bussines.js +3 -0
- package/cjs/lib/Types/Call.js +3 -0
- package/cjs/lib/Types/Chat.js +11 -0
- package/cjs/lib/Types/Contact.js +3 -0
- package/cjs/lib/Types/Events.js +4 -0
- package/cjs/lib/Types/GroupMetadata.js +3 -0
- package/cjs/lib/Types/Label.js +26 -0
- package/cjs/lib/Types/LabelAssociation.js +8 -0
- package/cjs/lib/Types/Message.js +19 -0
- package/cjs/lib/Types/Mex.js +40 -0
- package/cjs/lib/Types/Product.js +3 -0
- package/cjs/lib/Types/RichType.js +24 -0
- package/cjs/lib/Types/Signal.js +4 -0
- package/cjs/lib/Types/Socket.js +4 -0
- package/cjs/lib/Types/State.js +54 -0
- package/cjs/lib/Types/USync.js +4 -0
- package/cjs/lib/Types/index.js +42 -0
- package/cjs/lib/Utils/MessageBuilder.js +2164 -0
- package/cjs/lib/Utils/auth-utils.js +312 -0
- package/cjs/lib/Utils/browser-utils.js +32 -0
- package/cjs/lib/Utils/business.js +245 -0
- package/cjs/lib/Utils/chat-utils.js +937 -0
- package/cjs/lib/Utils/companion-reg-client-utils.js +45 -0
- package/cjs/lib/Utils/crypto.js +196 -0
- package/cjs/lib/Utils/decode-wa-message.js +324 -0
- package/cjs/lib/Utils/event-buffer.js +643 -0
- package/cjs/lib/Utils/generics.js +429 -0
- package/cjs/lib/Utils/history.js +151 -0
- package/cjs/lib/Utils/identity-change-handler.js +56 -0
- package/cjs/lib/Utils/index.js +42 -0
- package/cjs/lib/Utils/link-preview.js +122 -0
- package/cjs/lib/Utils/logger.js +8 -0
- package/cjs/lib/Utils/lt-hash.js +62 -0
- package/cjs/lib/Utils/make-mutex.js +38 -0
- package/cjs/lib/Utils/message-retry-manager.js +267 -0
- package/cjs/lib/Utils/messages-media.js +933 -0
- package/cjs/lib/Utils/messages.js +2175 -0
- package/cjs/lib/Utils/noise-handler.js +205 -0
- package/cjs/lib/Utils/offline-node-processor.js +43 -0
- package/cjs/lib/Utils/pre-key-manager.js +113 -0
- package/cjs/lib/Utils/process-message.js +772 -0
- package/cjs/lib/Utils/reporting-utils.js +263 -0
- package/cjs/lib/Utils/rich-message-utils.js +439 -0
- package/cjs/lib/Utils/signal.js +214 -0
- package/cjs/lib/Utils/stanza-ack.js +41 -0
- package/cjs/lib/Utils/sync-action-utils.js +54 -0
- package/cjs/lib/Utils/tc-token-utils.js +174 -0
- package/cjs/lib/Utils/use-multi-file-auth-state.js +125 -0
- package/cjs/lib/Utils/use-single-file-auth-state.js +121 -0
- package/cjs/lib/Utils/use-sqlite-auth-state.js +182 -0
- package/cjs/lib/Utils/validate-connection.js +210 -0
- package/cjs/lib/WABinary/constants.js +1470 -0
- package/cjs/lib/WABinary/decode.js +301 -0
- package/cjs/lib/WABinary/encode.js +257 -0
- package/cjs/lib/WABinary/generic-utils.js +274 -0
- package/cjs/lib/WABinary/index.js +22 -0
- package/cjs/lib/WABinary/jid-utils.js +114 -0
- package/cjs/lib/WABinary/types.js +37 -0
- package/cjs/lib/WAM/BinaryInfo.js +14 -0
- package/cjs/lib/WAM/constants.js +22856 -0
- package/cjs/lib/WAM/encode.js +154 -0
- package/cjs/lib/WAM/index.js +20 -0
- package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.js +56 -0
- package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.js +67 -0
- package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.js +29 -0
- package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +55 -0
- package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.js +33 -0
- package/cjs/lib/WAUSync/Protocols/index.js +24 -0
- package/cjs/lib/WAUSync/USyncQuery.js +126 -0
- package/cjs/lib/WAUSync/USyncUser.js +35 -0
- package/cjs/lib/WAUSync/index.js +20 -0
- package/cjs/lib/index.js +34 -0
- package/cjs/package.json +1 -0
- package/lib/Socket/messages-send.js +4 -3
- package/lib/Utils/messages.js +4 -1
- package/package.json +16 -5
- package/postinstall-banner.js +3 -2
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;
|
|
4
|
+
exports.decryptPollVote = decryptPollVote;
|
|
5
|
+
exports.decryptEventResponse = decryptEventResponse;
|
|
6
|
+
exports.decryptMessageEdit = decryptMessageEdit;
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const index_js_1 = require("../../WAProto/index.js");
|
|
9
|
+
const index_js_2 = require("../Types/index.js");
|
|
10
|
+
const messages_js_1 = require("../Utils/messages.js");
|
|
11
|
+
const index_js_3 = require("../WABinary/index.js");
|
|
12
|
+
const crypto_js_1 = require("./crypto.js");
|
|
13
|
+
const generics_js_1 = require("./generics.js");
|
|
14
|
+
const history_js_1 = require("./history.js");
|
|
15
|
+
const tc_token_utils_js_1 = require("./tc-token-utils.js");
|
|
16
|
+
const REAL_MSG_STUB_TYPES = new Set([
|
|
17
|
+
index_js_2.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
18
|
+
index_js_2.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
|
|
19
|
+
index_js_2.WAMessageStubType.CALL_MISSED_VIDEO,
|
|
20
|
+
index_js_2.WAMessageStubType.CALL_MISSED_VOICE
|
|
21
|
+
]);
|
|
22
|
+
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([index_js_2.WAMessageStubType.GROUP_PARTICIPANT_ADD]);
|
|
23
|
+
async function storeTcTokensFromHistorySync(chats, signalRepository, keyStore, logger) {
|
|
24
|
+
const getLIDForPN = signalRepository.lidMapping.getLIDForPN.bind(signalRepository.lidMapping);
|
|
25
|
+
const candidates = [];
|
|
26
|
+
for (const chat of chats) {
|
|
27
|
+
const ts = chat.tcTokenTimestamp ? (0, generics_js_1.toNumber)(chat.tcTokenTimestamp) : 0;
|
|
28
|
+
if (chat.tcToken?.length && ts > 0) {
|
|
29
|
+
const jid = (0, index_js_3.jidNormalizedUser)(chat.id);
|
|
30
|
+
const storageJid = await (0, tc_token_utils_js_1.resolveTcTokenJid)(jid, getLIDForPN);
|
|
31
|
+
candidates.push({
|
|
32
|
+
storageJid,
|
|
33
|
+
token: Buffer.from(chat.tcToken),
|
|
34
|
+
ts,
|
|
35
|
+
senderTs: chat.tcTokenSenderTimestamp ? (0, generics_js_1.toNumber)(chat.tcTokenSenderTimestamp) : undefined
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!candidates.length) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const jids = candidates.map(c => c.storageJid);
|
|
43
|
+
const existing = await keyStore.get('tctoken', jids);
|
|
44
|
+
const entries = {};
|
|
45
|
+
for (const c of candidates) {
|
|
46
|
+
const existingEntry = existing[c.storageJid];
|
|
47
|
+
const existingTs = existingEntry?.timestamp ? Number(existingEntry.timestamp) : 0;
|
|
48
|
+
if (existingTs > 0 && existingTs >= c.ts) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
entries[c.storageJid] = {
|
|
52
|
+
...existingEntry,
|
|
53
|
+
token: c.token,
|
|
54
|
+
timestamp: String(c.ts),
|
|
55
|
+
...(c.senderTs !== undefined ? { senderTimestamp: c.senderTs } : {})
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (Object.keys(entries).length) {
|
|
59
|
+
logger?.debug({ count: Object.keys(entries).length }, 'storing tctokens from history sync');
|
|
60
|
+
try {
|
|
61
|
+
// Include updated __index so cross-session pruning picks these JIDs up.
|
|
62
|
+
const indexWrite = await (0, tc_token_utils_js_1.buildMergedTcTokenIndexWrite)(keyStore, Object.keys(entries));
|
|
63
|
+
await keyStore.set({ tctoken: { ...entries, ...indexWrite } });
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
logger?.warn({ err }, 'failed to store tctokens from history sync');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Cleans a received message to further processing */
|
|
71
|
+
const cleanMessage = (message, meId, meLid) => {
|
|
72
|
+
// ensure remoteJid and participant doesn't have device or agent in it
|
|
73
|
+
if ((0, index_js_3.isHostedPnUser)(message.key.remoteJid) || (0, index_js_3.isHostedLidUser)(message.key.remoteJid)) {
|
|
74
|
+
message.key.remoteJid = (0, index_js_3.jidEncode)((0, index_js_3.jidDecode)(message.key?.remoteJid)?.user, (0, index_js_3.isHostedPnUser)(message.key.remoteJid) ? 's.whatsapp.net' : 'lid');
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
message.key.remoteJid = (0, index_js_3.jidNormalizedUser)(message.key.remoteJid);
|
|
78
|
+
}
|
|
79
|
+
if ((0, index_js_3.isHostedPnUser)(message.key.participant) || (0, index_js_3.isHostedLidUser)(message.key.participant)) {
|
|
80
|
+
message.key.participant = (0, index_js_3.jidEncode)((0, index_js_3.jidDecode)(message.key.participant)?.user, (0, index_js_3.isHostedPnUser)(message.key.participant) ? 's.whatsapp.net' : 'lid');
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
message.key.participant = (0, index_js_3.jidNormalizedUser)(message.key.participant);
|
|
84
|
+
}
|
|
85
|
+
const content = (0, messages_js_1.normalizeMessageContent)(message.message);
|
|
86
|
+
// if the message has a reaction, ensure fromMe & remoteJid are from our perspective
|
|
87
|
+
if (content?.reactionMessage) {
|
|
88
|
+
normaliseKey(content.reactionMessage.key);
|
|
89
|
+
}
|
|
90
|
+
if (content?.pollUpdateMessage) {
|
|
91
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
|
|
92
|
+
}
|
|
93
|
+
function normaliseKey(msgKey) {
|
|
94
|
+
// if the reaction is from another user
|
|
95
|
+
// we've to correctly map the key to this user's perspective
|
|
96
|
+
if (!message.key.fromMe) {
|
|
97
|
+
// if the sender believed the message being reacted to is not from them
|
|
98
|
+
// we've to correct the key to be from them, or some other participant
|
|
99
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
100
|
+
? (0, index_js_3.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId) ||
|
|
101
|
+
(0, index_js_3.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meLid)
|
|
102
|
+
: // if the message being reacted to, was from them
|
|
103
|
+
// fromMe automatically becomes false
|
|
104
|
+
false;
|
|
105
|
+
// set the remoteJid to being the same as the chat the message came from
|
|
106
|
+
// TODO: investigate inconsistencies
|
|
107
|
+
msgKey.remoteJid = message.key.remoteJid;
|
|
108
|
+
// set participant of the message
|
|
109
|
+
msgKey.participant = msgKey.participant || message.key.participant;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
exports.cleanMessage = cleanMessage;
|
|
114
|
+
// TODO: target:audit AUDIT THIS FUNCTION AGAIN
|
|
115
|
+
const isRealMessage = (message) => {
|
|
116
|
+
const normalizedContent = (0, messages_js_1.normalizeMessageContent)(message.message);
|
|
117
|
+
const hasSomeContent = !!(0, messages_js_1.getContentType)(normalizedContent);
|
|
118
|
+
return ((!!normalizedContent ||
|
|
119
|
+
REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
|
|
120
|
+
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
|
|
121
|
+
hasSomeContent &&
|
|
122
|
+
!normalizedContent?.protocolMessage &&
|
|
123
|
+
!normalizedContent?.reactionMessage &&
|
|
124
|
+
!normalizedContent?.pollUpdateMessage);
|
|
125
|
+
};
|
|
126
|
+
exports.isRealMessage = isRealMessage;
|
|
127
|
+
const shouldIncrementChatUnread = (message) => !message.key.fromMe && !message.messageStubType;
|
|
128
|
+
exports.shouldIncrementChatUnread = shouldIncrementChatUnread;
|
|
129
|
+
/**
|
|
130
|
+
* Get the ID of the chat from the given key.
|
|
131
|
+
* Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
|
|
132
|
+
*/
|
|
133
|
+
const getChatId = ({ remoteJid, participant, fromMe }) => {
|
|
134
|
+
if (!remoteJid) {
|
|
135
|
+
throw new boom_1.Boom('Cannot derive chat id: message key is missing remoteJid', {
|
|
136
|
+
data: { remoteJid, participant, fromMe }
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if ((0, index_js_3.isJidBroadcast)(remoteJid) && !(0, index_js_3.isJidStatusBroadcast)(remoteJid) && !fromMe) {
|
|
140
|
+
if (!participant) {
|
|
141
|
+
throw new boom_1.Boom('Cannot derive chat id: broadcast message key is missing participant', {
|
|
142
|
+
data: { remoteJid, fromMe }
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return participant;
|
|
146
|
+
}
|
|
147
|
+
return remoteJid;
|
|
148
|
+
};
|
|
149
|
+
exports.getChatId = getChatId;
|
|
150
|
+
/**
|
|
151
|
+
* Decrypt a poll vote
|
|
152
|
+
* @param vote encrypted vote
|
|
153
|
+
* @param ctx additional info about the poll required for decryption
|
|
154
|
+
* @returns list of SHA256 options
|
|
155
|
+
*/
|
|
156
|
+
function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
|
|
157
|
+
const sign = Buffer.concat([
|
|
158
|
+
toBinary(pollMsgId),
|
|
159
|
+
toBinary(pollCreatorJid),
|
|
160
|
+
toBinary(voterJid),
|
|
161
|
+
toBinary('Poll Vote'),
|
|
162
|
+
new Uint8Array([1])
|
|
163
|
+
]);
|
|
164
|
+
const key0 = (0, crypto_js_1.hmacSign)(pollEncKey, new Uint8Array(32), 'sha256');
|
|
165
|
+
const decKey = (0, crypto_js_1.hmacSign)(sign, key0, 'sha256');
|
|
166
|
+
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
|
|
167
|
+
const decrypted = (0, crypto_js_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
|
|
168
|
+
return index_js_1.proto.Message.PollVoteMessage.decode(decrypted);
|
|
169
|
+
function toBinary(txt) {
|
|
170
|
+
return Buffer.from(txt);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Decrypt an event response
|
|
175
|
+
* @param response encrypted event response
|
|
176
|
+
* @param ctx additional info about the event required for decryption
|
|
177
|
+
* @returns event response message
|
|
178
|
+
*/
|
|
179
|
+
function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }) {
|
|
180
|
+
const sign = Buffer.concat([
|
|
181
|
+
toBinary(eventMsgId),
|
|
182
|
+
toBinary(eventCreatorJid),
|
|
183
|
+
toBinary(responderJid),
|
|
184
|
+
toBinary('Event Response'),
|
|
185
|
+
new Uint8Array([1])
|
|
186
|
+
]);
|
|
187
|
+
const key0 = (0, crypto_js_1.hmacSign)(eventEncKey, new Uint8Array(32), 'sha256');
|
|
188
|
+
const decKey = (0, crypto_js_1.hmacSign)(sign, key0, 'sha256');
|
|
189
|
+
const aad = toBinary(`${eventMsgId}\u0000${responderJid}`);
|
|
190
|
+
const decrypted = (0, crypto_js_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
|
|
191
|
+
return index_js_1.proto.Message.EventResponseMessage.decode(decrypted);
|
|
192
|
+
function toBinary(txt) {
|
|
193
|
+
return Buffer.from(txt);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Decrypt a `secretEncryptedMessage` carrying a `MESSAGE_EDIT` payload.
|
|
198
|
+
*
|
|
199
|
+
* WhatsApp started wrapping message edits in an E2EE envelope (May 2026).
|
|
200
|
+
* The new content is encrypted with a key derived from the original
|
|
201
|
+
* message's `messageContextInfo.messageSecret` using HKDF-SHA256
|
|
202
|
+
* + AES-256-GCM, same family as polls and event responses but with
|
|
203
|
+
* different constants (validated against live WhatsApp Android traffic):
|
|
204
|
+
*
|
|
205
|
+
* info = msgId || origSenderJid || editorJid || "Message Edit"
|
|
206
|
+
* aad = (empty) <-- differs from Poll Vote / Event Response
|
|
207
|
+
* key = HKDF-SHA256(salt=zeros, ikm=messageSecret, info, L=32)
|
|
208
|
+
*
|
|
209
|
+
* The decrypted plaintext is a regular `proto.Message` whose
|
|
210
|
+
* `protocolMessage.editedMessage` field holds the new content — same
|
|
211
|
+
* shape as the legacy `protocolMessage.editedMessage` edit path, so
|
|
212
|
+
* consumers can treat the result identically.
|
|
213
|
+
*
|
|
214
|
+
* @param edit encrypted edit payload (encPayload + encIv from SecretEncryptedMessage)
|
|
215
|
+
* @param ctx info about the original message required for decryption
|
|
216
|
+
* @returns decoded outer `Message` whose `protocolMessage.editedMessage`
|
|
217
|
+
* carries the new content (extendedTextMessage / conversation / etc.)
|
|
218
|
+
*/
|
|
219
|
+
function decryptMessageEdit({ encPayload, encIv }, { originalSenderJid, originalMsgId, editEncKey, editorJid }) {
|
|
220
|
+
const sign = Buffer.concat([
|
|
221
|
+
toBinary(originalMsgId),
|
|
222
|
+
toBinary(originalSenderJid),
|
|
223
|
+
toBinary(editorJid),
|
|
224
|
+
toBinary('Message Edit'),
|
|
225
|
+
new Uint8Array([1])
|
|
226
|
+
]);
|
|
227
|
+
const key0 = (0, crypto_js_1.hmacSign)(editEncKey, new Uint8Array(32), 'sha256');
|
|
228
|
+
const decKey = (0, crypto_js_1.hmacSign)(sign, key0, 'sha256');
|
|
229
|
+
// AAD is intentionally empty for MESSAGE_EDIT — unlike Poll Vote and
|
|
230
|
+
// Event Response, WA does not include `${msgId}\0${editor}` in the AAD here.
|
|
231
|
+
const aad = Buffer.alloc(0);
|
|
232
|
+
const decrypted = (0, crypto_js_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
|
|
233
|
+
return index_js_1.proto.Message.decode(decrypted);
|
|
234
|
+
function toBinary(txt) {
|
|
235
|
+
return Buffer.from(txt);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const buildEditUpdate = (args) => {
|
|
239
|
+
const editedInner = decryptMessageEdit({ encPayload: args.encPayload, encIv: args.encIv }, {
|
|
240
|
+
editEncKey: args.editEncKey,
|
|
241
|
+
originalSenderJid: args.originalSenderJid,
|
|
242
|
+
originalMsgId: args.originalMsgId,
|
|
243
|
+
editorJid: args.editorJid
|
|
244
|
+
});
|
|
245
|
+
const editProtocol = editedInner.protocolMessage;
|
|
246
|
+
const innerEdited = editProtocol?.editedMessage;
|
|
247
|
+
if (!innerEdited) {
|
|
248
|
+
args.logger?.warn({ targetKey: args.targetKey }, 'decrypted MESSAGE_EDIT plaintext had no protocolMessage.editedMessage — skipping update');
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
key: { ...args.messageKey, id: args.targetKeyId },
|
|
253
|
+
update: {
|
|
254
|
+
message: {
|
|
255
|
+
editedMessage: {
|
|
256
|
+
message: innerEdited
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
messageTimestamp: editProtocol?.timestampMs
|
|
260
|
+
? Math.floor((0, generics_js_1.toNumber)(editProtocol.timestampMs) / 1000)
|
|
261
|
+
: args.fallbackTimestamp
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, signalRepository, keyStore, logger, options, getMessage }) => {
|
|
266
|
+
const meId = creds.me.id;
|
|
267
|
+
const { accountSettings } = creds;
|
|
268
|
+
const chat = { id: (0, index_js_3.jidNormalizedUser)((0, exports.getChatId)(message.key)) };
|
|
269
|
+
const isRealMsg = (0, exports.isRealMessage)(message);
|
|
270
|
+
if (isRealMsg) {
|
|
271
|
+
chat.messages = [{ message }];
|
|
272
|
+
chat.conversationTimestamp = (0, generics_js_1.toNumber)(message.messageTimestamp);
|
|
273
|
+
// only increment unread count if not CIPHERTEXT and from another person
|
|
274
|
+
if ((0, exports.shouldIncrementChatUnread)(message)) {
|
|
275
|
+
chat.unreadCount = (chat.unreadCount || 0) + 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const content = (0, messages_js_1.normalizeMessageContent)(message.message);
|
|
279
|
+
// unarchive chat if it's a real message, or someone reacted to our message
|
|
280
|
+
// and we've the unarchive chats setting on
|
|
281
|
+
if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
|
|
282
|
+
chat.archived = false;
|
|
283
|
+
chat.readOnly = false;
|
|
284
|
+
}
|
|
285
|
+
const protocolMsg = content?.protocolMessage;
|
|
286
|
+
if (protocolMsg) {
|
|
287
|
+
// Mirror whatsmeow's `handleProtocolMessage` guard, but applied only to
|
|
288
|
+
// the protocol message types that originate from our own device — an
|
|
289
|
+
// attacker could otherwise spoof any of these to manipulate local state.
|
|
290
|
+
//
|
|
291
|
+
// Self-only types (drop if `!fromMe`):
|
|
292
|
+
// - HISTORY_SYNC_NOTIFICATION (our phone driving history sync)
|
|
293
|
+
// - APP_STATE_SYNC_KEY_SHARE (key share between our devices)
|
|
294
|
+
// - LID_MIGRATION_MAPPING_SYNC (server-initiated via our phone)
|
|
295
|
+
// - PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE (response from our phone to our PDO request)
|
|
296
|
+
//
|
|
297
|
+
// Cross-user types (must NOT be dropped — legitimately arrive from others):
|
|
298
|
+
// - REVOKE
|
|
299
|
+
// - MESSAGE_EDIT
|
|
300
|
+
// - EPHEMERAL_SETTING
|
|
301
|
+
// - GROUP_MEMBER_LABEL_CHANGE
|
|
302
|
+
//
|
|
303
|
+
// See https://github.com/tulir/whatsmeow/blob/8d3700152a/message.go#L842-L845
|
|
304
|
+
// for the reference architecture — whatsmeow's `handleProtocolMessage`
|
|
305
|
+
// only contains self-only types because edits are unwrapped from
|
|
306
|
+
// `EditedMessage` BEFORE this dispatch and revokes aren't routed here.
|
|
307
|
+
const SELF_ONLY_TYPES = new Set([
|
|
308
|
+
index_js_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION,
|
|
309
|
+
index_js_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE,
|
|
310
|
+
index_js_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC,
|
|
311
|
+
index_js_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE
|
|
312
|
+
]);
|
|
313
|
+
if (protocolMsg.type !== null &&
|
|
314
|
+
protocolMsg.type !== undefined &&
|
|
315
|
+
SELF_ONLY_TYPES.has(protocolMsg.type) &&
|
|
316
|
+
!message.key.fromMe) {
|
|
317
|
+
logger?.warn({ msgId: message.key.id, type: protocolMsg.type, from: message.key.participant || message.key.remoteJid }, 'dropping spoofed self-only protocolMessage from non-self origin');
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
switch (protocolMsg.type) {
|
|
321
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
|
322
|
+
const histNotification = protocolMsg.historySyncNotification;
|
|
323
|
+
const process = shouldProcessHistoryMsg;
|
|
324
|
+
const isLatest = !creds.processedHistoryMessages?.length;
|
|
325
|
+
logger?.info({
|
|
326
|
+
histNotification,
|
|
327
|
+
process,
|
|
328
|
+
id: message.key.id,
|
|
329
|
+
isLatest
|
|
330
|
+
}, 'got history notification');
|
|
331
|
+
if (process) {
|
|
332
|
+
// TODO: investigate
|
|
333
|
+
if (histNotification.syncType !== index_js_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
334
|
+
ev.emit('creds.update', {
|
|
335
|
+
processedHistoryMessages: [
|
|
336
|
+
...(creds.processedHistoryMessages || []),
|
|
337
|
+
{ key: message.key, messageTimestamp: message.messageTimestamp }
|
|
338
|
+
]
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
const data = await (0, history_js_1.downloadAndProcessHistorySyncNotification)(histNotification, options, logger);
|
|
342
|
+
if (data.lidPnMappings?.length) {
|
|
343
|
+
logger?.debug({ count: data.lidPnMappings.length }, 'processing LID-PN mappings from history sync');
|
|
344
|
+
await signalRepository.lidMapping
|
|
345
|
+
.storeLIDPNMappings(data.lidPnMappings)
|
|
346
|
+
.catch(err => logger?.warn({ err }, 'failed to store LID-PN mappings from history sync'));
|
|
347
|
+
}
|
|
348
|
+
await storeTcTokensFromHistorySync(data.chats, signalRepository, keyStore, logger);
|
|
349
|
+
ev.emit('messaging-history.set', {
|
|
350
|
+
...data,
|
|
351
|
+
isLatest: histNotification.syncType !== index_js_1.proto.HistorySync.HistorySyncType.ON_DEMAND ? isLatest : undefined,
|
|
352
|
+
chunkOrder: histNotification.chunkOrder,
|
|
353
|
+
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
|
358
|
+
const keys = protocolMsg.appStateSyncKeyShare.keys;
|
|
359
|
+
if (keys?.length) {
|
|
360
|
+
let newAppStateSyncKeyId = '';
|
|
361
|
+
await keyStore.transaction(async () => {
|
|
362
|
+
const newKeys = [];
|
|
363
|
+
for (const { keyData, keyId } of keys) {
|
|
364
|
+
const strKeyId = Buffer.from(keyId.keyId).toString('base64');
|
|
365
|
+
newKeys.push(strKeyId);
|
|
366
|
+
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
|
|
367
|
+
newAppStateSyncKeyId = strKeyId;
|
|
368
|
+
}
|
|
369
|
+
logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
|
|
370
|
+
}, meId);
|
|
371
|
+
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
logger?.info({ protocolMsg }, 'recv app state sync with 0 keys');
|
|
375
|
+
}
|
|
376
|
+
break;
|
|
377
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.REVOKE:
|
|
378
|
+
ev.emit('messages.update', [
|
|
379
|
+
{
|
|
380
|
+
key: {
|
|
381
|
+
...message.key,
|
|
382
|
+
id: protocolMsg.key.id
|
|
383
|
+
},
|
|
384
|
+
update: { message: null, messageStubType: index_js_2.WAMessageStubType.REVOKE, key: message.key }
|
|
385
|
+
}
|
|
386
|
+
]);
|
|
387
|
+
break;
|
|
388
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
389
|
+
Object.assign(chat, {
|
|
390
|
+
ephemeralSettingTimestamp: (0, generics_js_1.toNumber)(message.messageTimestamp),
|
|
391
|
+
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
392
|
+
});
|
|
393
|
+
break;
|
|
394
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
395
|
+
const response = protocolMsg.peerDataOperationRequestResponseMessage;
|
|
396
|
+
if (response) {
|
|
397
|
+
// TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
|
|
398
|
+
const peerDataOperationResult = response.peerDataOperationResult || [];
|
|
399
|
+
for (const result of peerDataOperationResult) {
|
|
400
|
+
const retryResponse = result?.placeholderMessageResendResponse;
|
|
401
|
+
//eslint-disable-next-line max-depth
|
|
402
|
+
if (!retryResponse?.webMessageInfoBytes) {
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
//eslint-disable-next-line max-depth
|
|
406
|
+
try {
|
|
407
|
+
const webMessageInfo = index_js_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
|
|
408
|
+
const msgId = webMessageInfo.key?.id;
|
|
409
|
+
// Retrieve cached original message data (preserves LID details,
|
|
410
|
+
// timestamps, etc. that the phone may omit in its PDO response)
|
|
411
|
+
const cachedData = msgId ? await placeholderResendCache?.get(msgId) : undefined;
|
|
412
|
+
//eslint-disable-next-line max-depth
|
|
413
|
+
if (msgId) {
|
|
414
|
+
await placeholderResendCache?.del(msgId);
|
|
415
|
+
}
|
|
416
|
+
let finalMsg;
|
|
417
|
+
//eslint-disable-next-line max-depth
|
|
418
|
+
if (cachedData && typeof cachedData === 'object') {
|
|
419
|
+
// Apply decoded message content onto cached metadata (preserves LID etc.)
|
|
420
|
+
cachedData.message = webMessageInfo.message;
|
|
421
|
+
//eslint-disable-next-line max-depth
|
|
422
|
+
if (webMessageInfo.messageTimestamp) {
|
|
423
|
+
cachedData.messageTimestamp = webMessageInfo.messageTimestamp;
|
|
424
|
+
}
|
|
425
|
+
finalMsg = cachedData;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
finalMsg = webMessageInfo;
|
|
429
|
+
}
|
|
430
|
+
logger?.debug({ msgId, requestId: response.stanzaId }, 'received placeholder resend');
|
|
431
|
+
ev.emit('messages.upsert', {
|
|
432
|
+
messages: [finalMsg],
|
|
433
|
+
type: 'notify',
|
|
434
|
+
requestId: response.stanzaId
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
catch (err) {
|
|
438
|
+
logger?.warn({ err, stanzaId: response.stanzaId }, 'failed to decode placeholder resend response');
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
break;
|
|
443
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
|
|
444
|
+
ev.emit('messages.update', [
|
|
445
|
+
{
|
|
446
|
+
// flip the sender / fromMe properties because they're in the perspective of the sender
|
|
447
|
+
key: { ...message.key, id: protocolMsg.key?.id },
|
|
448
|
+
update: {
|
|
449
|
+
message: {
|
|
450
|
+
editedMessage: {
|
|
451
|
+
message: protocolMsg.editedMessage
|
|
452
|
+
}
|
|
453
|
+
},
|
|
454
|
+
messageTimestamp: protocolMsg.timestampMs
|
|
455
|
+
? Math.floor((0, generics_js_1.toNumber)(protocolMsg.timestampMs) / 1000)
|
|
456
|
+
: message.messageTimestamp
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
]);
|
|
460
|
+
break;
|
|
461
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.GROUP_MEMBER_LABEL_CHANGE:
|
|
462
|
+
const labelAssociationMsg = protocolMsg.memberLabel;
|
|
463
|
+
if (labelAssociationMsg?.label) {
|
|
464
|
+
ev.emit('group.member-tag.update', {
|
|
465
|
+
groupId: chat.id,
|
|
466
|
+
label: labelAssociationMsg.label,
|
|
467
|
+
participant: message.key.participant,
|
|
468
|
+
participantAlt: message.key.participantAlt,
|
|
469
|
+
messageTimestamp: Number(message.messageTimestamp)
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
case index_js_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
|
|
474
|
+
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload;
|
|
475
|
+
const { pnToLidMappings, chatDbMigrationTimestamp } = index_js_1.proto.LIDMigrationMappingSyncPayload.decode(encodedPayload);
|
|
476
|
+
logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp');
|
|
477
|
+
const pairs = [];
|
|
478
|
+
for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
|
|
479
|
+
const lid = latestLid || assignedLid;
|
|
480
|
+
pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` });
|
|
481
|
+
}
|
|
482
|
+
await signalRepository.lidMapping.storeLIDPNMappings(pairs);
|
|
483
|
+
if (pairs.length) {
|
|
484
|
+
for (const { pn, lid } of pairs) {
|
|
485
|
+
await signalRepository.migrateSession(pn, lid);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
else if (content?.reactionMessage) {
|
|
491
|
+
const reaction = {
|
|
492
|
+
...content.reactionMessage,
|
|
493
|
+
key: message.key
|
|
494
|
+
};
|
|
495
|
+
ev.emit('messages.reaction', [
|
|
496
|
+
{
|
|
497
|
+
reaction,
|
|
498
|
+
key: content.reactionMessage?.key
|
|
499
|
+
}
|
|
500
|
+
]);
|
|
501
|
+
}
|
|
502
|
+
else if (content?.encEventResponseMessage) {
|
|
503
|
+
const encEventResponse = content.encEventResponseMessage;
|
|
504
|
+
const creationMsgKey = encEventResponse.eventCreationMessageKey;
|
|
505
|
+
// we need to fetch the event creation message to get the event enc key
|
|
506
|
+
const eventMsg = await getMessage(creationMsgKey);
|
|
507
|
+
if (eventMsg) {
|
|
508
|
+
try {
|
|
509
|
+
const meIdNormalised = (0, index_js_3.jidNormalizedUser)(meId);
|
|
510
|
+
// all jids need to be PN
|
|
511
|
+
const eventCreatorKey = creationMsgKey.participant || creationMsgKey.remoteJid;
|
|
512
|
+
const eventCreatorPn = (0, index_js_3.isLidUser)(eventCreatorKey)
|
|
513
|
+
? await signalRepository.lidMapping.getPNForLID(eventCreatorKey)
|
|
514
|
+
: eventCreatorKey;
|
|
515
|
+
const eventCreatorJid = (0, generics_js_1.getKeyAuthor)({ remoteJid: (0, index_js_3.jidNormalizedUser)(eventCreatorPn), fromMe: meIdNormalised === eventCreatorPn }, meIdNormalised);
|
|
516
|
+
const responderJid = (0, generics_js_1.getKeyAuthor)(message.key, meIdNormalised);
|
|
517
|
+
const eventEncKey = eventMsg?.messageContextInfo?.messageSecret;
|
|
518
|
+
if (!eventEncKey) {
|
|
519
|
+
logger?.warn({ creationMsgKey }, 'event response: missing messageSecret for decryption');
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
const responseMsg = decryptEventResponse(encEventResponse, {
|
|
523
|
+
eventEncKey,
|
|
524
|
+
eventCreatorJid,
|
|
525
|
+
eventMsgId: creationMsgKey.id,
|
|
526
|
+
responderJid
|
|
527
|
+
});
|
|
528
|
+
const eventResponse = {
|
|
529
|
+
eventResponseMessageKey: message.key,
|
|
530
|
+
senderTimestampMs: responseMsg.timestampMs,
|
|
531
|
+
response: responseMsg
|
|
532
|
+
};
|
|
533
|
+
ev.emit('messages.update', [
|
|
534
|
+
{
|
|
535
|
+
key: creationMsgKey,
|
|
536
|
+
update: {
|
|
537
|
+
eventResponses: [eventResponse]
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
]);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
catch (err) {
|
|
544
|
+
logger?.warn({ err, creationMsgKey }, 'failed to decrypt event response');
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
logger?.warn({ creationMsgKey }, 'event creation message not found, cannot decrypt response');
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
else if (content?.secretEncryptedMessage &&
|
|
552
|
+
content.secretEncryptedMessage.secretEncType === index_js_1.proto.Message.SecretEncryptedMessage.SecretEncType.MESSAGE_EDIT) {
|
|
553
|
+
// E2EE message edit envelope (new in 2026-05). Replaces the older
|
|
554
|
+
// `protocolMessage.editedMessage` path for direct text edits.
|
|
555
|
+
// We fetch the target message to obtain its `messageSecret`, derive the
|
|
556
|
+
// decryption key, and surface the decoded inner Message via the same
|
|
557
|
+
// `messages.update` event used by the legacy edit path so consumers do
|
|
558
|
+
// not need a new event type.
|
|
559
|
+
const secEnc = content.secretEncryptedMessage;
|
|
560
|
+
const targetKey = secEnc.targetMessageKey;
|
|
561
|
+
const targetMsg = await getMessage(targetKey);
|
|
562
|
+
if (targetMsg) {
|
|
563
|
+
try {
|
|
564
|
+
const meIdNormalised = (0, index_js_3.jidNormalizedUser)(meId);
|
|
565
|
+
const origSenderRaw = targetKey.participant || targetKey.remoteJid;
|
|
566
|
+
const origSenderPn = (0, index_js_3.isLidUser)(origSenderRaw)
|
|
567
|
+
? await signalRepository.lidMapping.getPNForLID(origSenderRaw)
|
|
568
|
+
: origSenderRaw;
|
|
569
|
+
const originalSenderJid = (0, generics_js_1.getKeyAuthor)({ remoteJid: (0, index_js_3.jidNormalizedUser)(origSenderPn), fromMe: meIdNormalised === origSenderPn }, meIdNormalised);
|
|
570
|
+
const editorJid = (0, generics_js_1.getKeyAuthor)(message.key, meIdNormalised);
|
|
571
|
+
const editEncKey = targetMsg?.messageContextInfo?.messageSecret;
|
|
572
|
+
if (!editEncKey) {
|
|
573
|
+
logger?.warn({ targetKey }, 'message edit: missing messageSecret on original message — cannot decrypt');
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const update = buildEditUpdate({
|
|
577
|
+
editEncKey,
|
|
578
|
+
encPayload: secEnc.encPayload,
|
|
579
|
+
encIv: secEnc.encIv,
|
|
580
|
+
originalSenderJid,
|
|
581
|
+
originalMsgId: targetKey.id,
|
|
582
|
+
editorJid,
|
|
583
|
+
messageKey: message.key,
|
|
584
|
+
targetKeyId: targetKey.id,
|
|
585
|
+
fallbackTimestamp: (0, generics_js_1.toNumber)(message.messageTimestamp),
|
|
586
|
+
logger,
|
|
587
|
+
targetKey
|
|
588
|
+
});
|
|
589
|
+
if (update) {
|
|
590
|
+
ev.emit('messages.update', [update]);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
catch (err) {
|
|
594
|
+
logger?.warn({ err, targetKey }, 'failed to decrypt secretEncryptedMessage MESSAGE_EDIT');
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
logger?.warn({ targetKey }, 'original message not found in store, cannot decrypt secretEncryptedMessage edit');
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (message.messageStubType) {
|
|
602
|
+
const jid = message.key?.remoteJid;
|
|
603
|
+
//let actor = whatsappID (message.participant)
|
|
604
|
+
let participants;
|
|
605
|
+
const emitParticipantsUpdate = (action) => ev.emit('group-participants.update', {
|
|
606
|
+
id: jid,
|
|
607
|
+
author: message.key.participant,
|
|
608
|
+
authorPn: message.key.participantAlt,
|
|
609
|
+
authorUsername: message.key.participantUsername,
|
|
610
|
+
participants,
|
|
611
|
+
action
|
|
612
|
+
});
|
|
613
|
+
const emitGroupUpdate = (update) => {
|
|
614
|
+
ev.emit('groups.update', [
|
|
615
|
+
{
|
|
616
|
+
id: jid,
|
|
617
|
+
...update,
|
|
618
|
+
author: message.key.participant ?? undefined,
|
|
619
|
+
authorPn: message.key.participantAlt,
|
|
620
|
+
authorUsername: message.key.participantUsername
|
|
621
|
+
}
|
|
622
|
+
]);
|
|
623
|
+
};
|
|
624
|
+
const emitGroupRequestJoin = (participant, action, method) => {
|
|
625
|
+
ev.emit('group.join-request', {
|
|
626
|
+
id: jid,
|
|
627
|
+
author: message.key.participant,
|
|
628
|
+
authorPn: message.key.participantAlt,
|
|
629
|
+
authorUsername: message.key.participantUsername,
|
|
630
|
+
participant: participant.lid,
|
|
631
|
+
participantPn: participant.pn,
|
|
632
|
+
action,
|
|
633
|
+
method: method
|
|
634
|
+
});
|
|
635
|
+
};
|
|
636
|
+
const participantsIncludesMe = () => participants.find(jid => (0, index_js_3.areJidsSameUser)(meId, jid.phoneNumber)); // ADD SUPPORT FOR LID
|
|
637
|
+
switch (message.messageStubType) {
|
|
638
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
|
|
639
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
640
|
+
emitParticipantsUpdate('modify');
|
|
641
|
+
break;
|
|
642
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
643
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
644
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
645
|
+
emitParticipantsUpdate('remove');
|
|
646
|
+
// mark the chat read only if you left the group
|
|
647
|
+
if (participantsIncludesMe()) {
|
|
648
|
+
chat.readOnly = true;
|
|
649
|
+
}
|
|
650
|
+
break;
|
|
651
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
652
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
653
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
654
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
655
|
+
if (participantsIncludesMe()) {
|
|
656
|
+
chat.readOnly = false;
|
|
657
|
+
}
|
|
658
|
+
emitParticipantsUpdate('add');
|
|
659
|
+
break;
|
|
660
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
661
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
662
|
+
emitParticipantsUpdate('demote');
|
|
663
|
+
break;
|
|
664
|
+
case index_js_2.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
665
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
666
|
+
emitParticipantsUpdate('promote');
|
|
667
|
+
break;
|
|
668
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
669
|
+
const announceValue = message.messageStubParameters?.[0];
|
|
670
|
+
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
|
|
671
|
+
break;
|
|
672
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
673
|
+
const restrictValue = message.messageStubParameters?.[0];
|
|
674
|
+
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
|
|
675
|
+
break;
|
|
676
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
677
|
+
const name = message.messageStubParameters?.[0];
|
|
678
|
+
chat.name = name;
|
|
679
|
+
emitGroupUpdate({ subject: name });
|
|
680
|
+
break;
|
|
681
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
|
|
682
|
+
const description = message.messageStubParameters?.[0];
|
|
683
|
+
chat.description = description;
|
|
684
|
+
emitGroupUpdate({ desc: description });
|
|
685
|
+
break;
|
|
686
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
687
|
+
const code = message.messageStubParameters?.[0];
|
|
688
|
+
emitGroupUpdate({ inviteCode: code });
|
|
689
|
+
break;
|
|
690
|
+
case index_js_2.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
691
|
+
const memberAddValue = message.messageStubParameters?.[0];
|
|
692
|
+
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
|
|
693
|
+
break;
|
|
694
|
+
case index_js_2.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
695
|
+
const approvalMode = message.messageStubParameters?.[0];
|
|
696
|
+
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
|
|
697
|
+
break;
|
|
698
|
+
case index_js_2.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD: // TODO: Add other events
|
|
699
|
+
const participant = JSON.parse(message.messageStubParameters?.[0]);
|
|
700
|
+
const action = message.messageStubParameters?.[1];
|
|
701
|
+
const method = message.messageStubParameters?.[2];
|
|
702
|
+
emitGroupRequestJoin(participant, action, method);
|
|
703
|
+
break;
|
|
704
|
+
// Vanz@Fix (bug 44): handle previously unhandled stub types
|
|
705
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_ICON:
|
|
706
|
+
emitGroupUpdate({ icon: message.messageStubParameters?.[0] });
|
|
707
|
+
break;
|
|
708
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_NO_FREQUENTLY_FORWARDED:
|
|
709
|
+
emitGroupUpdate({ noFrequentlyForwarded: message.messageStubParameters?.[0] === 'on' });
|
|
710
|
+
break;
|
|
711
|
+
case index_js_2.WAMessageStubType.GROUP_V4_ADD_INVITE_SENT:
|
|
712
|
+
// notification that a v4 invite was sent — no group metadata change, but emit for plugin awareness
|
|
713
|
+
ev.emit('groups.update', [{ id: jid, v4AddInviteSent: true }]);
|
|
714
|
+
break;
|
|
715
|
+
case index_js_2.WAMessageStubType.GROUP_CHANGE_RECENT_HISTORY_SHARING:
|
|
716
|
+
emitGroupUpdate({ recentHistorySharing: message.messageStubParameters?.[0] === 'on' });
|
|
717
|
+
break;
|
|
718
|
+
// Vanz@Fix (bug 45): handle GROUP_CREATE — bot entered a new group via link
|
|
719
|
+
case index_js_2.WAMessageStubType.GROUP_CREATE:
|
|
720
|
+
ev.emit('groups.upsert', [{ id: jid, subject: message.messageStubParameters?.[0] || '' }]);
|
|
721
|
+
break;
|
|
722
|
+
}
|
|
723
|
+
// Vanz@Fix (bug 47): poll update handler was entirely commented out.
|
|
724
|
+
// Re-enabled: decrypts poll votes and emits messages.update with pollUpdates.
|
|
725
|
+
}
|
|
726
|
+
else if (content?.pollUpdateMessage) {
|
|
727
|
+
const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey;
|
|
728
|
+
const pollMsg = await getMessage(creationMsgKey);
|
|
729
|
+
if (pollMsg) {
|
|
730
|
+
const meIdNormalised = (0, index_js_3.jidNormalizedUser)(meId);
|
|
731
|
+
const pollCreatorJid = (0, generics_js_1.getKeyAuthor)(creationMsgKey, meIdNormalised);
|
|
732
|
+
const voterJid = (0, generics_js_1.getKeyAuthor)(message.key, meIdNormalised);
|
|
733
|
+
const pollEncKey = pollMsg.messageContextInfo?.messageSecret;
|
|
734
|
+
if (pollEncKey) {
|
|
735
|
+
try {
|
|
736
|
+
const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
|
|
737
|
+
pollEncKey,
|
|
738
|
+
pollCreatorJid,
|
|
739
|
+
pollMsgId: creationMsgKey.id,
|
|
740
|
+
voterJid,
|
|
741
|
+
});
|
|
742
|
+
ev.emit('messages.update', [{
|
|
743
|
+
key: creationMsgKey,
|
|
744
|
+
update: {
|
|
745
|
+
pollUpdates: [{
|
|
746
|
+
pollUpdateMessageKey: message.key,
|
|
747
|
+
vote: voteMsg,
|
|
748
|
+
senderTimestampMs: typeof content.pollUpdateMessage.senderTimestampMs === 'object'
|
|
749
|
+
? content.pollUpdateMessage.senderTimestampMs.toNumber()
|
|
750
|
+
: Number(content.pollUpdateMessage.senderTimestampMs),
|
|
751
|
+
}]
|
|
752
|
+
}
|
|
753
|
+
}]);
|
|
754
|
+
}
|
|
755
|
+
catch (err) {
|
|
756
|
+
logger?.warn({ err, creationMsgKey }, 'failed to decrypt poll vote');
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
else {
|
|
760
|
+
logger?.warn({ creationMsgKey }, 'poll message has no messageSecret — cannot decrypt vote');
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
else {
|
|
764
|
+
logger?.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update');
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (Object.keys(chat).length > 1) {
|
|
768
|
+
ev.emit('chats.update', [chat]);
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
exports.default = processMessage;
|
|
772
|
+
//# sourceMappingURL=process-message.js.map
|