Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/messages-recv.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.makeMessagesRecvSocket = void 0;
4
const WAProto_1 = require("../../WAProto");
5
const Defaults_1 = require("../Defaults");
6
const Types_1 = require("../Types");
7
const Utils_1 = require("../Utils");
8
const make_mutex_1 = require("../Utils/make-mutex");
9
const process_message_1 = require("../Utils/process-message");
10
const WABinary_1 = require("../WABinary");
11
const groups_1 = require("./groups");
12
const messages_send_1 = require("./messages-send");
13
const makeMessagesRecvSocket = (config) => {
14
const { logger, retryRequestDelayMs, getMessage } = config;
15
const sock = (0, messages_send_1.makeMessagesSocket)(config);
16
const { ev, authState, ws, processingMutex, upsertMessage, resyncAppState, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, } = sock;
17
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
18
const retryMutex = (0, make_mutex_1.makeMutex)();
19
const msgRetryMap = config.msgRetryCounterMap || {};
20
const callOfferData = {};
21
let sendActiveReceipts = false;
22
const sendMessageAck = async ({ tag, attrs }) => {
23
const stanza = {
24
tag: 'ack',
25
attrs: {
26
id: attrs.id,
27
to: attrs.from,
28
class: tag,
29
}
30
};
31
if (!!attrs.participant) {
32
stanza.attrs.participant = attrs.participant;
33
}
34
if (!!attrs.recipient) {
35
stanza.attrs.recipient = attrs.recipient;
36
}
37
if (tag !== 'message' && attrs.type) {
38
stanza.attrs.type = attrs.type;
39
}
40
logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');
41
await sendNode(stanza);
42
};
43
const sendRetryRequest = async (node, forceIncludeKeys = false) => {
44
const msgId = node.attrs.id;
45
let retryCount = msgRetryMap[msgId] || 0;
46
if (retryCount >= 5) {
47
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');
48
delete msgRetryMap[msgId];
49
return;
50
}
51
retryCount += 1;
52
msgRetryMap[msgId] = retryCount;
53
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;
54
const deviceIdentity = (0, Utils_1.encodeSignedDeviceIdentity)(account, true);
55
await authState.keys.transaction(async () => {
56
const receipt = {
57
tag: 'receipt',
58
attrs: {
59
id: msgId,
60
type: 'retry',
61
to: node.attrs.from
62
},
63
content: [
64
{
65
tag: 'retry',
66
attrs: {
67
count: retryCount.toString(),
68
id: node.attrs.id,
69
t: node.attrs.t,
70
v: '1'
71
}
72
},
73
{
74
tag: 'registration',
75
attrs: {},
76
content: (0, Utils_1.encodeBigEndian)(authState.creds.registrationId)
77
}
78
]
79
};
80
if (node.attrs.recipient) {
81
receipt.attrs.recipient = node.attrs.recipient;
82
}
83
if (node.attrs.participant) {
84
receipt.attrs.participant = node.attrs.participant;
85
}
86
if (retryCount > 1 || forceIncludeKeys) {
87
const { update, preKeys } = await (0, Utils_1.getNextPreKeys)(authState, 1);
88
const [keyId] = Object.keys(preKeys);
89
const key = preKeys[+keyId];
90
const content = receipt.content;
91
content.push({
92
tag: 'keys',
93
attrs: {},
94
content: [
95
{ tag: 'type', attrs: {}, content: Buffer.from(Defaults_1.KEY_BUNDLE_TYPE) },
96
{ tag: 'identity', attrs: {}, content: identityKey.public },
97
(0, Utils_1.xmppPreKey)(key, +keyId),
98
(0, Utils_1.xmppSignedPreKey)(signedPreKey),
99
{ tag: 'device-identity', attrs: {}, content: deviceIdentity }
100
]
101
});
102
ev.emit('creds.update', update);
103
}
104
await sendNode(receipt);
105
logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt');
106
});
107
};
108
const handleEncryptNotification = async (node) => {
109
const from = node.attrs.from;
110
if (from === WABinary_1.S_WHATSAPP_NET) {
111
const countChild = (0, WABinary_1.getBinaryNodeChild)(node, 'count');
112
const count = +countChild.attrs.value;
113
const shouldUploadMorePreKeys = count < Defaults_1.MIN_PREKEY_COUNT;
114
logger.debug({ count, shouldUploadMorePreKeys }, 'recv pre-key count');
115
if (shouldUploadMorePreKeys) {
116
await uploadPreKeys();
117
}
118
}
119
else {
120
const identityNode = (0, WABinary_1.getBinaryNodeChild)(node, 'identity');
121
if (identityNode) {
122
logger.info({ jid: from }, 'identity changed');
123
// not handling right now
124
// signal will override new identity anyway
125
}
126
else {
127
logger.info({ node }, 'unknown encrypt notification');
128
}
129
}
130
};
131
const processNotification = async (node) => {
132
const result = {};
133
const [child] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
134
const nodeType = node.attrs.type;
135
if (nodeType === 'w:gp2') {
136
switch (child === null || child === void 0 ? void 0 : child.tag) {
137
case 'create':
138
const metadata = (0, groups_1.extractGroupMetadata)(child);
139
result.messageStubType = Types_1.WAMessageStubType.GROUP_CREATE;
140
result.messageStubParameters = [metadata.subject];
141
result.key = { participant: metadata.owner };
142
ev.emit('chats.upsert', [{
143
id: metadata.id,
144
name: metadata.subject,
145
conversationTimestamp: metadata.creation,
146
}]);
147
ev.emit('groups.upsert', [metadata]);
148
break;
149
case 'ephemeral':
150
case 'not_ephemeral':
151
result.message = {
152
protocolMessage: {
153
type: WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
154
ephemeralExpiration: +(child.attrs.expiration || 0)
155
}
156
};
157
break;
158
case 'promote':
159
case 'demote':
160
case 'remove':
161
case 'add':
162
case 'leave':
163
const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;
164
result.messageStubType = Types_1.WAMessageStubType[stubType];
165
const participants = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
166
if (participants.length === 1 &&
167
// if recv. "remove" message and sender removed themselves
168
// mark as left
169
(0, WABinary_1.areJidsSameUser)(participants[0], node.attrs.participant) &&
170
child.tag === 'remove') {
171
result.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;
172
}
173
result.messageStubParameters = participants;
174
break;
175
case 'subject':
176
result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;
177
result.messageStubParameters = [child.attrs.subject];
178
break;
179
case 'announcement':
180
case 'not_announcement':
181
result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;
182
result.messageStubParameters = [(child.tag === 'announcement') ? 'on' : 'off'];
183
break;
184
case 'locked':
185
case 'unlocked':
186
result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT;
187
result.messageStubParameters = [(child.tag === 'locked') ? 'on' : 'off'];
188
break;
189
}
190
}
191
else if (nodeType === 'mediaretry') {
192
const event = (0, Utils_1.decodeMediaRetryNode)(node);
193
ev.emit('messages.media-update', [event]);
194
}
195
else if (nodeType === 'encrypt') {
196
await handleEncryptNotification(node);
197
}
198
else if (nodeType === 'devices') {
199
const devices = (0, WABinary_1.getBinaryNodeChildren)(child, 'device');
200
if ((0, WABinary_1.areJidsSameUser)(child.attrs.jid, authState.creds.me.id)) {
201
const deviceJids = devices.map(d => d.attrs.jid);
202
logger.info({ deviceJids }, 'got my own devices');
203
}
204
}
205
else if (nodeType === 'server_sync') {
206
const update = (0, WABinary_1.getBinaryNodeChild)(node, 'collection');
207
if (update) {
208
const name = update.attrs.name;
209
await resyncAppState([name], undefined);
210
}
211
}
212
if (Object.keys(result).length) {
213
return result;
214
}
215
};
216
const willSendMessageAgain = (id, participant) => {
217
const key = `${id}:${participant}`;
218
const retryCount = msgRetryMap[key] || 0;
219
return retryCount < 5;
220
};
221
const updateSendMessageAgainCount = (id, participant) => {
222
const key = `${id}:${participant}`;
223
msgRetryMap[key] = (msgRetryMap[key] || 0) + 1;
224
};
225
const sendMessagesAgain = async (key, ids, retryNode) => {
226
var _a;
227
const msgs = await Promise.all(ids.map(id => getMessage({ ...key, id })));
228
const remoteJid = key.remoteJid;
229
const participant = key.participant || remoteJid;
230
// if it's the primary jid sending the request
231
// just re-send the message to everyone
232
// prevents the first message decryption failure
233
const sendToAll = !((_a = (0, WABinary_1.jidDecode)(participant)) === null || _a === void 0 ? void 0 : _a.device);
234
await assertSessions([participant], true);
235
if ((0, WABinary_1.isJidGroup)(remoteJid)) {
236
await authState.keys.set({ 'sender-key-memory': { [remoteJid]: null } });
237
}
238
logger.debug({ participant, sendToAll }, 'forced new session for retry recp');
239
for (let i = 0; i < msgs.length; i++) {
240
const msg = msgs[i];
241
if (msg) {
242
updateSendMessageAgainCount(ids[i], participant);
243
const msgRelayOpts = { messageId: ids[i] };
244
if (sendToAll) {
245
msgRelayOpts.useUserDevicesCache = false;
246
}
247
else {
248
msgRelayOpts.participant = {
249
jid: participant,
250
count: +retryNode.attrs.count
251
};
252
}
253
await relayMessage(key.remoteJid, msg, msgRelayOpts);
254
}
255
else {
256
logger.debug({ jid: key.remoteJid, id: ids[i] }, 'recv retry request, but message not available');
257
}
258
}
259
};
260
const handleReceipt = async (node) => {
261
var _a;
262
const { attrs, content } = node;
263
const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(attrs.participant || attrs.from, (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);
264
const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? attrs.from : attrs.recipient;
265
const fromMe = !attrs.recipient || (attrs.type === 'retry' && isNodeFromMe);
266
const ids = [attrs.id];
267
if (Array.isArray(content)) {
268
const items = (0, WABinary_1.getBinaryNodeChildren)(content[0], 'item');
269
ids.push(...items.map(i => i.attrs.id));
270
}
271
const key = {
272
remoteJid,
273
id: '',
274
fromMe,
275
participant: attrs.participant
276
};
277
await Promise.all([
278
processingMutex.mutex(async () => {
279
const status = (0, Utils_1.getStatusFromReceiptType)(attrs.type);
280
if (typeof status !== 'undefined' &&
281
(
282
// basically, we only want to know when a message from us has been delivered to/read by the other person
283
// or another device of ours has read some messages
284
status > WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ||
285
!isNodeFromMe)) {
286
if ((0, WABinary_1.isJidGroup)(remoteJid)) {
287
if (attrs.participant) {
288
const updateKey = status === WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';
289
ev.emit('message-receipt.update', ids.map(id => ({
290
key: { ...key, id },
291
receipt: {
292
userJid: (0, WABinary_1.jidNormalizedUser)(attrs.participant),
293
[updateKey]: +attrs.t
294
}
295
})));
296
}
297
}
298
else {
299
ev.emit('messages.update', ids.map(id => ({
300
key: { ...key, id },
301
update: { status }
302
})));
303
}
304
}
305
if (attrs.type === 'retry') {
306
// correctly set who is asking for the retry
307
key.participant = key.participant || attrs.from;
308
const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');
309
if (willSendMessageAgain(ids[0], key.participant)) {
310
if (key.fromMe) {
311
try {
312
logger.debug({ attrs, key }, 'recv retry request');
313
await sendMessagesAgain(key, ids, retryNode);
314
}
315
catch (error) {
316
logger.error({ key, ids, trace: error.stack }, 'error in sending message again');
317
}
318
}
319
else {
320
logger.info({ attrs, key }, 'recv retry for not fromMe message');
321
}
322
}
323
else {
324
logger.info({ attrs, key }, 'will not send message again, as sent too many times');
325
}
326
}
327
}),
328
sendMessageAck(node)
329
]);
330
};
331
const handleNotification = async (node) => {
332
const remoteJid = node.attrs.from;
333
await Promise.all([
334
processingMutex.mutex(async () => {
335
const msg = await processNotification(node);
336
if (msg) {
337
const fromMe = (0, WABinary_1.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);
338
msg.key = {
339
remoteJid,
340
fromMe,
341
participant: node.attrs.participant,
342
id: node.attrs.id,
343
...(msg.key || {})
344
};
345
msg.participant = node.attrs.participant;
346
msg.messageTimestamp = +node.attrs.t;
347
const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
348
await upsertMessage(fullMsg, 'append');
349
}
350
}),
351
sendMessageAck(node)
352
]);
353
};
354
const handleMessage = async (node) => {
355
const { fullMessage: msg, category, author, decryptionTask } = (0, Utils_1.decodeMessageStanza)(node, authState);
356
await Promise.all([
357
processingMutex.mutex(async () => {
358
await decryptionTask;
359
// message failed to decrypt
360
if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {
361
logger.error({ key: msg.key, params: msg.messageStubParameters }, 'failure in decrypting message');
362
retryMutex.mutex(async () => {
363
if (ws.readyState === ws.OPEN) {
364
const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
365
await sendRetryRequest(node, !encNode);
366
if (retryRequestDelayMs) {
367
await (0, Utils_1.delay)(retryRequestDelayMs);
368
}
369
}
370
else {
371
logger.debug({ node }, 'connection closed, ignoring retry req');
372
}
373
});
374
}
375
else {
376
// no type in the receipt => message delivered
377
let type = undefined;
378
let participant = msg.key.participant;
379
if (category === 'peer') { // special peer message
380
type = 'peer_msg';
381
}
382
else if (msg.key.fromMe) { // message was sent by us from a different device
383
type = 'sender';
384
// need to specially handle this case
385
if ((0, WABinary_1.isJidUser)(msg.key.remoteJid)) {
386
participant = author;
387
}
388
}
389
else if (!sendActiveReceipts) {
390
type = 'inactive';
391
}
392
await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
393
// send ack for history message
394
const isAnyHistoryMsg = (0, Utils_1.isHistoryMsg)(msg.message);
395
if (isAnyHistoryMsg) {
396
const jid = (0, WABinary_1.jidNormalizedUser)(msg.key.remoteJid);
397
await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');
398
}
399
}
400
(0, process_message_1.cleanMessage)(msg, authState.creds.me.id);
401
await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
402
}),
403
sendMessageAck(node)
404
]);
405
};
406
const handleCall = async (node) => {
407
const { attrs } = node;
408
const [infoChild] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
409
const callId = infoChild.attrs['call-id'];
410
const from = infoChild.attrs.from || infoChild.attrs['call-creator'];
411
const status = (0, Utils_1.getCallStatusFromNode)(infoChild);
412
const call = {
413
chatId: attrs.from,
414
from,
415
id: callId,
416
date: new Date(+attrs.t * 1000),
417
offline: !!attrs.offline,
418
status,
419
};
420
if (status === 'offer') {
421
call.isVideo = !!(0, WABinary_1.getBinaryNodeChild)(infoChild, 'video');
422
call.isGroup = infoChild.attrs.type === 'group';
423
callOfferData[call.id] = call;
424
}
425
// use existing call info to populate this event
426
if (callOfferData[call.id]) {
427
call.isVideo = callOfferData[call.id].isVideo;
428
call.isGroup = callOfferData[call.id].isGroup;
429
}
430
// delete data once call has ended
431
if (status === 'reject' || status === 'accept' || status === 'timeout') {
432
delete callOfferData[call.id];
433
}
434
ev.emit('call', [call]);
435
await sendMessageAck(node);
436
};
437
const handleBadAck = async ({ attrs }) => {
438
// current hypothesis is that if pash is sent in the ack
439
// it means -- the message hasn't reached all devices yet
440
// we'll retry sending the message here
441
if (attrs.phash) {
442
logger.info({ attrs }, 'received phash in ack, resending message...');
443
const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id };
444
const msg = await getMessage(key);
445
if (msg) {
446
await relayMessage(key.remoteJid, msg, { messageId: key.id, useUserDevicesCache: false });
447
}
448
else {
449
logger.warn({ attrs }, 'could not send message again, as it was not found');
450
}
451
}
452
};
453
const flushBufferIfLastOfflineNode = (node, identifier, exec) => {
454
const task = exec(node)
455
.catch(err => onUnexpectedError(err, identifier));
456
const offline = node.attrs.offline;
457
if (offline) {
458
ev.processInBuffer(task);
459
}
460
};
461
// called when all offline notifs are handled
462
ws.on('CB:ib,,offline', async (node) => {
463
const child = (0, WABinary_1.getBinaryNodeChild)(node, 'offline');
464
const offlineNotifs = +((child === null || child === void 0 ? void 0 : child.attrs.count) || 0);
465
logger.info(`handled ${offlineNotifs} offline messages/notifications`);
466
await ev.flush();
467
ev.emit('connection.update', { receivedPendingNotifications: true });
468
});
469
// recv a message
470
ws.on('CB:message', (node) => {
471
flushBufferIfLastOfflineNode(node, 'processing message', handleMessage);
472
});
473
ws.on('CB:call', async (node) => {
474
flushBufferIfLastOfflineNode(node, 'handling call', handleCall);
475
});
476
ws.on('CB:receipt', node => {
477
flushBufferIfLastOfflineNode(node, 'handling receipt', handleReceipt);
478
});
479
ws.on('CB:notification', async (node) => {
480
flushBufferIfLastOfflineNode(node, 'handling notification', handleNotification);
481
});
482
ws.on('CB:ack,class:message', (node) => {
483
handleBadAck(node)
484
.catch(error => onUnexpectedError(error, 'handling bad ack'));
485
});
486
ev.on('call', ([call]) => {
487
// missed call + group call notification message generation
488
if (call.status === 'timeout' || (call.status === 'offer' && call.isGroup)) {
489
const msg = {
490
key: {
491
remoteJid: call.chatId,
492
id: call.id,
493
fromMe: false
494
},
495
messageTimestamp: (0, Utils_1.unixTimestampSeconds)(call.date),
496
};
497
if (call.status === 'timeout') {
498
if (call.isGroup) {
499
msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE;
500
}
501
else {
502
msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_VOICE;
503
}
504
}
505
else {
506
msg.message = { call: { callKey: Buffer.from(call.id) } };
507
}
508
const protoMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
509
upsertMessage(protoMsg, call.offline ? 'append' : 'notify');
510
}
511
});
512
ev.on('connection.update', ({ isOnline }) => {
513
if (typeof isOnline !== 'undefined') {
514
sendActiveReceipts = isOnline;
515
logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`);
516
}
517
});
518
return {
519
...sock,
520
sendMessageAck,
521
sendRetryRequest
522
};
523
};
524
exports.makeMessagesRecvSocket = makeMessagesRecvSocket;
525
526