@mrlegendbot/baileys 1.2.4 → 1.2.6
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/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/fix-imports.js +85 -0
- package/WAProto/index.d.ts +14017 -0
- package/WAProto/index.js +97691 -2
- package/lib/Defaults/index.d.ts +75 -0
- package/lib/Defaults/index.js +92 -89
- package/lib/Menu/index.d.ts +42 -0
- package/lib/Menu/index.js +283 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/ciphertext-message.js +10 -9
- package/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/lib/Signal/Group/group-session-builder.js +22 -21
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.js +69 -75
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/index.js +1 -0
- package/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/lib/Signal/Group/keyhelper.js +10 -9
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-chain-key.js +22 -21
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +60 -51
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-message.js +62 -56
- package/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/lib/Signal/Group/sender-key-name.js +40 -38
- package/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/lib/Signal/Group/sender-key-record.js +34 -33
- package/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/lib/Signal/Group/sender-key-state.js +76 -72
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/Group/sender-message-key.js +23 -22
- package/lib/Signal/libsignal.d.ts +5 -0
- package/lib/Signal/libsignal.js +405 -506
- package/lib/Signal/lid-mapping.d.ts +23 -0
- package/lib/Signal/lid-mapping.js +258 -276
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +1 -0
- package/lib/Socket/Client/types.d.ts +16 -0
- package/lib/Socket/Client/types.js +7 -6
- package/lib/Socket/Client/websocket.d.ts +13 -0
- package/lib/Socket/Client/websocket.js +49 -48
- package/lib/Socket/business.d.ts +217 -0
- package/lib/Socket/business.js +369 -349
- package/lib/Socket/chats.d.ts +125 -0
- package/lib/Socket/chats.js +1148 -1172
- package/lib/Socket/communities.d.ts +273 -0
- package/lib/Socket/communities.js +413 -473
- package/lib/Socket/groups.d.ts +161 -0
- package/lib/Socket/groups.js +331 -387
- package/lib/Socket/index.d.ts +267 -0
- package/lib/Socket/index.js +19 -6
- package/lib/Socket/messages-recv.d.ts +203 -0
- package/lib/Socket/messages-recv.js +1719 -1782
- package/lib/Socket/messages-send.d.ts +199 -0
- package/lib/Socket/messages-send.js +2451 -1504
- package/lib/Socket/mex.d.ts +3 -0
- package/lib/Socket/mex.js +35 -40
- package/lib/Socket/newsletter.d.ts +170 -0
- package/lib/Socket/newsletter.js +191 -239
- package/lib/Socket/socket.d.ts +59 -0
- package/lib/Socket/socket.js +923 -992
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.js +8 -1
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.js +2 -1
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +47 -0
- package/lib/Types/Label.js +22 -20
- package/lib/Types/LabelAssociation.d.ts +30 -0
- package/lib/Types/LabelAssociation.js +4 -2
- package/lib/Types/Message.d.ts +320 -0
- package/lib/Types/Message.js +5 -3
- package/lib/Types/Mex.d.ts +141 -0
- package/lib/Types/Mex.js +31 -26
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.js +2 -1
- package/lib/Types/Socket.d.ts +136 -0
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/State.d.ts +97 -0
- package/lib/Types/State.js +52 -4
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.d.ts +66 -0
- package/lib/Types/index.js +11 -10
- package/lib/Utils/auth-utils.d.ts +24 -0
- package/lib/Utils/auth-utils.js +277 -240
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.js +23 -20
- package/lib/Utils/business.d.ts +23 -0
- package/lib/Utils/business.js +204 -200
- package/lib/Utils/chat-utils.d.ts +100 -0
- package/lib/Utils/chat-utils.js +810 -836
- package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
- package/lib/Utils/companion-reg-client-utils.js +25 -23
- package/lib/Utils/crypto.d.ts +37 -0
- package/lib/Utils/crypto.js +86 -70
- package/lib/Utils/decode-wa-message.d.ts +66 -0
- package/lib/Utils/decode-wa-message.js +284 -277
- package/lib/Utils/event-buffer.d.ts +36 -0
- package/lib/Utils/event-buffer.js +595 -592
- package/lib/Utils/generics.d.ts +92 -0
- package/lib/Utils/generics.js +335 -312
- package/lib/Utils/history.d.ts +24 -0
- package/lib/Utils/history.js +115 -123
- package/lib/Utils/identity-change-handler.d.ts +44 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/index.d.ts +22 -0
- package/lib/Utils/index.js +2 -1
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +73 -74
- package/lib/Utils/logger.d.ts +12 -0
- package/lib/Utils/logger.js +2 -3
- package/lib/Utils/lt-hash.d.ts +8 -0
- package/lib/Utils/lt-hash.js +6 -0
- package/lib/Utils/make-mutex.d.ts +9 -0
- package/lib/Utils/make-mutex.js +27 -27
- package/lib/Utils/message-retry-manager.d.ts +115 -0
- package/lib/Utils/message-retry-manager.js +254 -213
- package/lib/Utils/messages-media.d.ts +133 -0
- package/lib/Utils/messages-media.js +715 -741
- package/lib/Utils/messages.d.ts +91 -0
- package/lib/Utils/messages.js +827 -791
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +187 -215
- package/lib/Utils/offline-node-processor.d.ts +17 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/pre-key-manager.d.ts +28 -0
- package/lib/Utils/pre-key-manager.js +98 -69
- package/lib/Utils/process-message.d.ts +60 -0
- package/lib/Utils/process-message.js +586 -617
- package/lib/Utils/reporting-utils.d.ts +11 -0
- package/lib/Utils/reporting-utils.js +234 -553
- package/lib/Utils/signal.d.ts +47 -0
- package/lib/Utils/signal.js +170 -217
- package/lib/Utils/stanza-ack.d.ts +11 -0
- package/lib/Utils/stanza-ack.js +35 -27
- package/lib/Utils/sync-action-utils.d.ts +19 -0
- package/lib/Utils/sync-action-utils.js +41 -37
- package/lib/Utils/tc-token-utils.d.ts +37 -0
- package/lib/Utils/tc-token-utils.js +141 -120
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.js +105 -87
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.js +188 -190
- package/lib/WABinary/constants.d.ts +28 -0
- package/lib/WABinary/constants.js +1294 -28
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.js +249 -245
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.js +206 -201
- package/lib/WABinary/generic-utils.d.ts +15 -0
- package/lib/WABinary/generic-utils.js +89 -86
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.js +1 -0
- package/lib/WABinary/jid-utils.d.ts +48 -0
- package/lib/WABinary/jid-utils.js +77 -68
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.d.ts +9 -0
- package/lib/WAM/BinaryInfo.js +8 -7
- package/lib/WAM/constants.d.ts +40 -0
- package/lib/WAM/constants.js +22853 -19892
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.js +132 -121
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAM/index.js +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +43 -46
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -47
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -22
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -30
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +20 -19
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +44 -53
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
- package/lib/WAUSync/Protocols/index.d.ts +6 -0
- package/lib/WAUSync/Protocols/index.js +1 -0
- package/lib/WAUSync/USyncQuery.d.ts +30 -0
- package/lib/WAUSync/USyncQuery.js +88 -79
- package/lib/WAUSync/USyncUser.d.ts +17 -0
- package/lib/WAUSync/USyncUser.js +29 -28
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.js +1 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +2 -2
- package/package.json +86 -22
- package/README.upstream.md +0 -1312
- package/WAProto/WAProto.json +0 -1
- package/WAProto/WAProto.proto +0 -6275
- package/WAProto/compiler.js +0 -550
- package/lib/Store/index.js +0 -8
- package/lib/Store/keyed-db.js +0 -108
- package/lib/Store/make-cache-manager-store.js +0 -70
- package/lib/Store/make-in-memory-store.js +0 -417
- package/lib/Store/make-ordered-dictionary.js +0 -69
- package/lib/Store/object-repository.js +0 -27
- package/lib/Utils/use-sqlite-auth-state.js +0 -162
|
@@ -1,310 +1,317 @@
|
|
|
1
1
|
import { Boom } from '@hapi/boom';
|
|
2
|
-
import { proto } from '../../WAProto/
|
|
3
|
-
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidGroup, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidGroup, isJidMetaAI, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser
|
|
4
|
+
// transferDevice
|
|
5
|
+
} from '../WABinary/index.js';
|
|
4
6
|
import { unpadRandomMax16 } from './generics.js';
|
|
5
7
|
export const getDecryptionJid = async (sender, repository) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
if (isLidUser(sender) || isHostedLidUser(sender)) {
|
|
9
|
+
return sender;
|
|
10
|
+
}
|
|
11
|
+
const mapped = await repository.lidMapping.getLIDForPN(sender);
|
|
12
|
+
return mapped || sender;
|
|
11
13
|
};
|
|
12
14
|
const storeMappingFromEnvelope = async (stanza, sender, repository, decryptionJid, logger) => {
|
|
13
|
-
|
|
14
|
-
senderAlt
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
sender,
|
|
25
|
-
senderAlt
|
|
26
|
-
}, 'Stored LID mapping from envelope');
|
|
27
|
-
} catch (error) {
|
|
28
|
-
logger.warn({
|
|
29
|
-
sender,
|
|
30
|
-
senderAlt,
|
|
31
|
-
error
|
|
32
|
-
}, 'Failed to store LID mapping');
|
|
15
|
+
// TODO: Handle hosted IDs
|
|
16
|
+
const { senderAlt } = extractAddressingContext(stanza);
|
|
17
|
+
if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
|
|
18
|
+
try {
|
|
19
|
+
await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }]);
|
|
20
|
+
await repository.migrateSession(sender, senderAlt);
|
|
21
|
+
logger.debug({ sender, senderAlt }, 'Stored LID mapping from envelope');
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
logger.warn({ sender, senderAlt, error }, 'Failed to store LID mapping');
|
|
25
|
+
}
|
|
33
26
|
}
|
|
34
|
-
}
|
|
35
27
|
};
|
|
36
28
|
export const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
|
|
37
29
|
export const MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
|
|
38
30
|
export const ACCOUNT_RESTRICTED_TEXT = 'Your account has been restricted';
|
|
31
|
+
// Retry configuration for failed decryption
|
|
39
32
|
export const DECRYPTION_RETRY_CONFIG = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
33
|
+
maxRetries: 3,
|
|
34
|
+
baseDelayMs: 100,
|
|
35
|
+
sessionRecordErrors: ['No session record', 'SessionError: No session record']
|
|
43
36
|
};
|
|
37
|
+
/** NACK reason codes we send to the server (client → server) */
|
|
44
38
|
export const NACK_REASONS = {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
39
|
+
SenderReachoutTimelocked: 463,
|
|
40
|
+
ParsingError: 487,
|
|
41
|
+
UnrecognizedStanza: 488,
|
|
42
|
+
UnrecognizedStanzaClass: 489,
|
|
43
|
+
UnrecognizedStanzaType: 490,
|
|
44
|
+
InvalidProtobuf: 491,
|
|
45
|
+
InvalidHostedCompanionStanza: 493,
|
|
46
|
+
MissingMessageSecret: 495,
|
|
47
|
+
SignalErrorOldCounter: 496,
|
|
48
|
+
MessageDeletedOnPeer: 499,
|
|
49
|
+
UnhandledError: 500,
|
|
50
|
+
UnsupportedAdminRevoke: 550,
|
|
51
|
+
UnsupportedLIDGroup: 551,
|
|
52
|
+
DBOperationFailed: 552
|
|
59
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Server-side error codes returned in ack stanzas (server → client) that we
|
|
56
|
+
* currently have dedicated handlers for. Extend as more handlers are added.
|
|
57
|
+
* Distinct from the client-side NackReason enum (WAWebCreateNackFromStanza).
|
|
58
|
+
*/
|
|
60
59
|
export const SERVER_ERROR_CODES = {
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
/**
|
|
61
|
+
* 1:1 message missing privacy token (tctoken). Usually means the account is
|
|
62
|
+
* restricted: WhatsApp blocks starting new chats but preserves existing ones,
|
|
63
|
+
* since established chats already carry a tctoken.
|
|
64
|
+
*/
|
|
65
|
+
MessageAccountRestriction: '463',
|
|
66
|
+
/** Stanza validation failure (SMAX_INVALID) — likely stale device session */
|
|
67
|
+
SmaxInvalid: '479'
|
|
63
68
|
};
|
|
64
|
-
export const extractAddressingContext = stanza => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
69
|
+
export const extractAddressingContext = (stanza) => {
|
|
70
|
+
let senderAlt;
|
|
71
|
+
let recipientAlt;
|
|
72
|
+
const sender = stanza.attrs.participant || stanza.attrs.from;
|
|
73
|
+
const addressingMode = stanza.attrs.addressing_mode || (sender?.endsWith('lid') ? 'lid' : 'pn');
|
|
74
|
+
if (addressingMode === 'lid') {
|
|
75
|
+
// Message is LID-addressed: sender is LID, extract corresponding PN
|
|
76
|
+
// without device data
|
|
77
|
+
senderAlt = stanza.attrs.participant_pn || stanza.attrs.sender_pn || stanza.attrs.peer_recipient_pn;
|
|
78
|
+
recipientAlt = stanza.attrs.recipient_pn;
|
|
79
|
+
// with device data
|
|
80
|
+
//if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
// Message is PN-addressed: sender is PN, extract corresponding LID
|
|
84
|
+
// without device data
|
|
85
|
+
senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid;
|
|
86
|
+
recipientAlt = stanza.attrs.recipient_lid;
|
|
87
|
+
//with device data
|
|
88
|
+
//if (sender && senderAlt) senderAlt = transferDevice(sender, senderAlt)
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
addressingMode,
|
|
92
|
+
senderAlt,
|
|
93
|
+
recipientAlt
|
|
94
|
+
};
|
|
81
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
* Decode the received node as a message.
|
|
98
|
+
* @note this will only parse the message, not decrypt it
|
|
99
|
+
*/
|
|
82
100
|
export function decodeMessageNode(stanza, meId, meLid) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
data: stanza
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
if (!from) {
|
|
97
|
-
throw new Boom('Invalid message stanza: missing from attribute', {
|
|
98
|
-
data: stanza
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
const addressingContext = extractAddressingContext(stanza);
|
|
102
|
-
const isMe = jid => areJidsSameUser(jid, meId);
|
|
103
|
-
const isMeLid = jid => areJidsSameUser(jid, meLid);
|
|
104
|
-
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
105
|
-
if (recipient) {
|
|
106
|
-
if (!isMe(from) && !isMeLid(from)) {
|
|
107
|
-
throw new Boom('receipient present, but msg not from me', {
|
|
108
|
-
data: stanza
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
if (isMe(from) || isMeLid(from)) {
|
|
112
|
-
fromMe = true;
|
|
113
|
-
}
|
|
114
|
-
chatId = recipient;
|
|
115
|
-
} else {
|
|
116
|
-
if (isMe(from) || isMeLid(from)) {
|
|
117
|
-
fromMe = true;
|
|
118
|
-
}
|
|
119
|
-
chatId = from;
|
|
101
|
+
let msgType;
|
|
102
|
+
let chatId;
|
|
103
|
+
let author;
|
|
104
|
+
let fromMe = false;
|
|
105
|
+
const msgId = stanza.attrs.id;
|
|
106
|
+
const from = stanza.attrs.from;
|
|
107
|
+
const participant = stanza.attrs.participant;
|
|
108
|
+
const recipient = stanza.attrs.recipient;
|
|
109
|
+
if (!msgId) {
|
|
110
|
+
throw new Boom('Invalid message stanza: missing id attribute', { data: stanza });
|
|
120
111
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
} else if (isJidGroup(from)) {
|
|
124
|
-
if (!participant) {
|
|
125
|
-
throw new Boom('No participant in group message');
|
|
112
|
+
if (!from) {
|
|
113
|
+
throw new Boom('Invalid message stanza: missing from attribute', { data: stanza });
|
|
126
114
|
}
|
|
127
|
-
|
|
128
|
-
|
|
115
|
+
const addressingContext = extractAddressingContext(stanza);
|
|
116
|
+
const isMe = (jid) => areJidsSameUser(jid, meId);
|
|
117
|
+
const isMeLid = (jid) => areJidsSameUser(jid, meLid);
|
|
118
|
+
if (isPnUser(from) || isLidUser(from) || isHostedLidUser(from) || isHostedPnUser(from)) {
|
|
119
|
+
if (recipient && !isJidMetaAI(recipient)) {
|
|
120
|
+
if (!isMe(from) && !isMeLid(from)) {
|
|
121
|
+
throw new Boom('receipient present, but msg not from me', { data: stanza });
|
|
122
|
+
}
|
|
123
|
+
if (isMe(from) || isMeLid(from)) {
|
|
124
|
+
fromMe = true;
|
|
125
|
+
}
|
|
126
|
+
chatId = recipient;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
// Peer-routed self stanzas (history sync, app-state sync, etc.) arrive
|
|
130
|
+
// with `from` set to our own device but no `recipient` attribute —
|
|
131
|
+
// still mark as fromMe so self-only protocolMessage handlers run.
|
|
132
|
+
if (isMe(from) || isMeLid(from)) {
|
|
133
|
+
fromMe = true;
|
|
134
|
+
}
|
|
135
|
+
chatId = from;
|
|
136
|
+
}
|
|
137
|
+
msgType = 'chat';
|
|
138
|
+
author = from;
|
|
129
139
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
else if (isJidGroup(from)) {
|
|
141
|
+
if (!participant) {
|
|
142
|
+
throw new Boom('No participant in group message');
|
|
143
|
+
}
|
|
144
|
+
if (isMe(participant) || isMeLid(participant)) {
|
|
145
|
+
fromMe = true;
|
|
146
|
+
}
|
|
147
|
+
msgType = 'group';
|
|
148
|
+
author = participant;
|
|
149
|
+
chatId = from;
|
|
136
150
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
151
|
+
else if (isJidBroadcast(from)) {
|
|
152
|
+
if (!participant) {
|
|
153
|
+
throw new Boom('No participant in group message');
|
|
154
|
+
}
|
|
155
|
+
const isParticipantMe = isMe(participant);
|
|
156
|
+
if (isJidStatusBroadcast(from)) {
|
|
157
|
+
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast';
|
|
161
|
+
}
|
|
162
|
+
fromMe = isParticipantMe;
|
|
163
|
+
chatId = from;
|
|
164
|
+
author = participant;
|
|
142
165
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (stanza.attrs.is_sender) {
|
|
151
|
-
fromMe = true;
|
|
166
|
+
else if (isJidNewsletter(from)) {
|
|
167
|
+
msgType = 'newsletter';
|
|
168
|
+
chatId = from;
|
|
169
|
+
author = from;
|
|
170
|
+
if (isMe(from) || isMeLid(from)) {
|
|
171
|
+
fromMe = true;
|
|
172
|
+
}
|
|
152
173
|
}
|
|
153
|
-
|
|
154
|
-
|
|
174
|
+
else {
|
|
175
|
+
throw new Boom('Unknown message type', { data: stanza });
|
|
155
176
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
fullMessage,
|
|
188
|
-
author,
|
|
189
|
-
sender: msgType === 'chat' ? author : chatId
|
|
190
|
-
};
|
|
177
|
+
const pushname = stanza?.attrs?.notify;
|
|
178
|
+
const key = {
|
|
179
|
+
remoteJid: chatId,
|
|
180
|
+
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
181
|
+
remoteJidUsername: !isJidGroup(chatId)
|
|
182
|
+
? stanza.attrs.peer_recipient_username || stanza.attrs.recipient_username
|
|
183
|
+
: undefined,
|
|
184
|
+
fromMe,
|
|
185
|
+
id: msgId,
|
|
186
|
+
participant,
|
|
187
|
+
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
|
|
188
|
+
participantUsername: stanza.attrs.participant ? stanza.attrs.participant_username : undefined,
|
|
189
|
+
addressingMode: addressingContext.addressingMode,
|
|
190
|
+
...(msgType === 'newsletter' && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
|
|
191
|
+
};
|
|
192
|
+
const fullMessage = {
|
|
193
|
+
key,
|
|
194
|
+
category: stanza.attrs.category,
|
|
195
|
+
messageTimestamp: +stanza.attrs.t,
|
|
196
|
+
pushName: pushname,
|
|
197
|
+
broadcast: isJidBroadcast(from)
|
|
198
|
+
};
|
|
199
|
+
if (key.fromMe) {
|
|
200
|
+
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK;
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
fullMessage,
|
|
204
|
+
author,
|
|
205
|
+
sender: msgType === 'chat' ? author : chatId
|
|
206
|
+
};
|
|
191
207
|
}
|
|
192
208
|
export const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
209
|
+
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
|
|
210
|
+
return {
|
|
211
|
+
fullMessage,
|
|
212
|
+
category: stanza.attrs.category,
|
|
213
|
+
author,
|
|
214
|
+
async decrypt() {
|
|
215
|
+
let decryptables = 0;
|
|
216
|
+
if (Array.isArray(stanza.content)) {
|
|
217
|
+
for (const { tag, attrs, content } of stanza.content) {
|
|
218
|
+
if (tag === 'verified_name' && content instanceof Uint8Array) {
|
|
219
|
+
const cert = proto.VerifiedNameCertificate.decode(content);
|
|
220
|
+
const details = proto.VerifiedNameCertificate.Details.decode(cert.details);
|
|
221
|
+
fullMessage.verifiedBizName = details.verifiedName;
|
|
222
|
+
}
|
|
223
|
+
if (tag === 'unavailable' && attrs.type === 'view_once') {
|
|
224
|
+
fullMessage.key.isViewOnce = true; // TODO: remove from here and add a STUB TYPE
|
|
225
|
+
}
|
|
226
|
+
if (attrs.count && tag === 'enc') {
|
|
227
|
+
fullMessage.retryCount = Number(attrs.count);
|
|
228
|
+
}
|
|
229
|
+
if (tag !== 'enc' && tag !== 'plaintext') {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (!(content instanceof Uint8Array)) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
decryptables += 1;
|
|
236
|
+
let msgBuffer;
|
|
237
|
+
const decryptionJid = await getDecryptionJid(author, repository);
|
|
238
|
+
if (tag !== 'plaintext') {
|
|
239
|
+
// TODO: Handle hosted devices
|
|
240
|
+
await storeMappingFromEnvelope(stanza, author, repository, decryptionJid, logger);
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
|
|
244
|
+
switch (e2eType) {
|
|
245
|
+
case 'skmsg':
|
|
246
|
+
msgBuffer = await repository.decryptGroupMessage({
|
|
247
|
+
group: sender,
|
|
248
|
+
authorJid: author,
|
|
249
|
+
msg: content
|
|
250
|
+
});
|
|
251
|
+
break;
|
|
252
|
+
case 'pkmsg':
|
|
253
|
+
case 'msg':
|
|
254
|
+
msgBuffer = await repository.decryptMessage({
|
|
255
|
+
jid: decryptionJid,
|
|
256
|
+
type: e2eType,
|
|
257
|
+
ciphertext: content
|
|
258
|
+
});
|
|
259
|
+
break;
|
|
260
|
+
case 'plaintext':
|
|
261
|
+
msgBuffer = content;
|
|
262
|
+
break;
|
|
263
|
+
default:
|
|
264
|
+
throw new Error(`Unknown e2e type: ${e2eType}`);
|
|
265
|
+
}
|
|
266
|
+
let msg = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer);
|
|
267
|
+
msg = msg.deviceSentMessage?.message || msg;
|
|
268
|
+
if (msg.senderKeyDistributionMessage) {
|
|
269
|
+
//eslint-disable-next-line max-depth
|
|
270
|
+
try {
|
|
271
|
+
await repository.processSenderKeyDistributionMessage({
|
|
272
|
+
authorJid: author,
|
|
273
|
+
item: msg.senderKeyDistributionMessage
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
catch (err) {
|
|
277
|
+
logger.error({ key: fullMessage.key, err }, 'failed to process sender key distribution message');
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (fullMessage.message) {
|
|
281
|
+
Object.assign(fullMessage.message, msg);
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
fullMessage.message = msg;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
const errorContext = {
|
|
289
|
+
key: fullMessage.key,
|
|
290
|
+
err,
|
|
291
|
+
messageType: tag === 'plaintext' ? 'plaintext' : attrs.type,
|
|
292
|
+
sender,
|
|
293
|
+
author,
|
|
294
|
+
isSessionRecordError: isSessionRecordError(err)
|
|
295
|
+
};
|
|
296
|
+
logger.error(errorContext, 'failed to decrypt message');
|
|
297
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
298
|
+
fullMessage.messageStubParameters = [err.message.toString()];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
266
301
|
}
|
|
267
|
-
if
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
item: msg.senderKeyDistributionMessage
|
|
272
|
-
});
|
|
273
|
-
} catch (err) {
|
|
274
|
-
logger.error({
|
|
275
|
-
key: fullMessage.key,
|
|
276
|
-
err
|
|
277
|
-
}, 'failed to process sender key distribution message');
|
|
278
|
-
}
|
|
302
|
+
// if nothing was found to decrypt
|
|
303
|
+
if (!decryptables && !fullMessage.key?.isViewOnce) {
|
|
304
|
+
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
305
|
+
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
279
306
|
}
|
|
280
|
-
if (fullMessage.message) {
|
|
281
|
-
Object.assign(fullMessage.message, msg);
|
|
282
|
-
} else {
|
|
283
|
-
fullMessage.message = msg;
|
|
284
|
-
}
|
|
285
|
-
} catch (err) {
|
|
286
|
-
const errorContext = {
|
|
287
|
-
key: fullMessage.key,
|
|
288
|
-
err,
|
|
289
|
-
messageType: tag === 'plaintext' ? 'plaintext' : attrs.type,
|
|
290
|
-
sender,
|
|
291
|
-
author,
|
|
292
|
-
isSessionRecordError: isSessionRecordError(err)
|
|
293
|
-
};
|
|
294
|
-
logger.error(errorContext, 'failed to decrypt message');
|
|
295
|
-
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
296
|
-
fullMessage.messageStubParameters = [err.message.toString()];
|
|
297
|
-
}
|
|
298
307
|
}
|
|
299
|
-
|
|
300
|
-
if (!decryptables && !fullMessage.key?.isViewOnce) {
|
|
301
|
-
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT;
|
|
302
|
-
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
};
|
|
308
|
+
};
|
|
306
309
|
};
|
|
310
|
+
/**
|
|
311
|
+
* Utility function to check if an error is related to missing session record
|
|
312
|
+
*/
|
|
307
313
|
function isSessionRecordError(error) {
|
|
308
|
-
|
|
309
|
-
|
|
314
|
+
const errorMessage = error?.message || error?.toString() || '';
|
|
315
|
+
return DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(errorPattern => errorMessage.includes(errorPattern));
|
|
310
316
|
}
|
|
317
|
+
//# sourceMappingURL=decode-wa-message.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { BaileysEventEmitter, BaileysEventMap } from '../Types/index.js';
|
|
2
|
+
import type { ILogger } from './logger.js';
|
|
3
|
+
/**
|
|
4
|
+
* A map that contains a list of all events that have been triggered
|
|
5
|
+
*
|
|
6
|
+
* Note, this can contain different type of events
|
|
7
|
+
* this can make processing events extremely efficient -- since everything
|
|
8
|
+
* can be done in a single transaction
|
|
9
|
+
*/
|
|
10
|
+
type BaileysEventData = Partial<BaileysEventMap>;
|
|
11
|
+
type BaileysBufferableEventEmitter = BaileysEventEmitter & {
|
|
12
|
+
/** Use to process events in a batch */
|
|
13
|
+
process(handler: (events: BaileysEventData) => void | Promise<void>): () => void;
|
|
14
|
+
/**
|
|
15
|
+
* starts buffering events, call flush() to release them
|
|
16
|
+
* */
|
|
17
|
+
buffer(): void;
|
|
18
|
+
/** buffers all events till the promise completes */
|
|
19
|
+
createBufferedFunction<A extends any[], T>(work: (...args: A) => Promise<T>): (...args: A) => Promise<T>;
|
|
20
|
+
/**
|
|
21
|
+
* flushes all buffered events
|
|
22
|
+
* @returns returns true if the flush actually happened, otherwise false
|
|
23
|
+
*/
|
|
24
|
+
flush(): boolean;
|
|
25
|
+
/** is there an ongoing buffer */
|
|
26
|
+
isBuffering(): boolean;
|
|
27
|
+
/** destroy the event buffer, clearing all resources */
|
|
28
|
+
destroy(): void;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The event buffer logically consolidates different events into a single event
|
|
32
|
+
* making the data processing more efficient.
|
|
33
|
+
*/
|
|
34
|
+
export declare const makeEventBuffer: (logger: ILogger) => BaileysBufferableEventEmitter;
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=event-buffer.d.ts.map
|