@realvare/based 2.7.61 → 2.7.70
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/README.MD +25 -114
- package/WAProto/WAProto.proto +1073 -244
- package/WAProto/index.d.ts +16282 -8183
- package/WAProto/index.js +76605 -50628
- package/engine-requirements.js +10 -10
- package/lib/Defaults/baileys-version.json +1 -1
- package/lib/Defaults/index.d.ts +1 -1
- package/lib/Defaults/index.js +5 -5
- package/lib/Socket/chats.js +53 -16
- package/lib/Socket/groups.js +53 -9
- package/lib/Socket/messages-recv.js +1298 -1237
- package/lib/Socket/messages-send.js +350 -456
- package/lib/Socket/socket.js +1 -1
- package/lib/Socket/usync.js +57 -4
- package/lib/Store/make-in-memory-store.js +28 -15
- package/lib/Types/Message.d.ts +316 -6
- package/lib/Types/Message.js +1 -1
- package/lib/Utils/cache-manager.d.ts +16 -0
- package/lib/Utils/cache-manager.js +20 -4
- package/lib/Utils/chat-utils.js +17 -13
- package/lib/Utils/decode-wa-message.js +1 -11
- package/lib/Utils/event-buffer.js +103 -2
- package/lib/Utils/generics.js +4 -5
- package/lib/Utils/index.d.ts +5 -0
- package/lib/Utils/index.js +3 -0
- package/lib/Utils/jid-validation.d.ts +2 -0
- package/lib/Utils/jid-validation.js +36 -5
- package/lib/Utils/link-preview.js +38 -28
- package/lib/Utils/messages-media.js +21 -52
- package/lib/Utils/messages.js +540 -23
- package/lib/Utils/performance-config.d.ts +2 -0
- package/lib/Utils/performance-config.js +16 -7
- package/lib/Utils/process-message.js +124 -12
- package/lib/Utils/rate-limiter.js +15 -20
- package/lib/WABinary/jid-utils.js +257 -5
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +75 -5
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -6
- package/lib/WAUSync/USyncQuery.js +64 -6
- package/lib/index.d.ts +1 -0
- package/lib/index.js +5 -2
- package/package.json +7 -13
- package/WAProto/index.ts.ts +0 -53473
|
@@ -1,1238 +1,1299 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.makeMessagesRecvSocket = void 0;
|
|
7
|
-
const boom_1 = require("@hapi/boom");
|
|
8
|
-
const crypto_1 = require("crypto");
|
|
9
|
-
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
10
|
-
const WAProto_1 = require("../../WAProto");
|
|
11
|
-
const Defaults_1 = require("../Defaults");
|
|
12
|
-
const Types_1 = require("../Types");
|
|
13
|
-
const Utils_1 = require("../Utils");
|
|
14
|
-
const performance_config_1 = require("../Utils/performance-config");
|
|
15
|
-
const make_mutex_1 = require("../Utils/make-mutex");
|
|
16
|
-
const WABinary_1 = require("../WABinary");
|
|
17
|
-
const groups_1 = require("./groups");
|
|
18
|
-
const messages_send_1 = require("./messages-send");
|
|
19
|
-
const makeMessagesRecvSocket = (config) => {
|
|
20
|
-
const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid } = config;
|
|
21
|
-
const sock = (0, messages_send_1.makeMessagesSocket)(config);
|
|
22
|
-
const { ev, authState, ws, processingMutex, signalRepository, query, upsertMessage, resyncAppState, groupMetadata, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, createParticipantNodes, getUSyncDevices, sendPeerDataOperationMessage, } = sock;
|
|
23
|
-
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
|
24
|
-
const retryMutex = (0, make_mutex_1.makeMutex)();
|
|
25
|
-
const msgRetryCache = config.msgRetryCounterCache || new node_cache_1.default({
|
|
26
|
-
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
27
|
-
useClones: false
|
|
28
|
-
});
|
|
29
|
-
const callOfferCache = config.callOfferCache || new node_cache_1.default({
|
|
30
|
-
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.CALL_OFFER,
|
|
31
|
-
useClones: false
|
|
32
|
-
});
|
|
33
|
-
const placeholderResendCache = config.placeholderResendCache || new node_cache_1.default({
|
|
34
|
-
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
35
|
-
useClones: false
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
offerContent
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
await
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
case '
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
case '
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
case '
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
const
|
|
362
|
-
if (
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
msg.messageStubParameters = [
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
msg.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
msg.
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
msg.
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
if (
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
if (
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
if (
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
});
|
|
628
|
-
authState.creds.
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
const
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
await
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
if ((0, WABinary_1.
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
};
|
|
1057
|
-
const
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
if (
|
|
1063
|
-
logger.
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
if (
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.makeMessagesRecvSocket = void 0;
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
10
|
+
const WAProto_1 = require("../../WAProto");
|
|
11
|
+
const Defaults_1 = require("../Defaults");
|
|
12
|
+
const Types_1 = require("../Types");
|
|
13
|
+
const Utils_1 = require("../Utils");
|
|
14
|
+
const performance_config_1 = require("../Utils/performance-config");
|
|
15
|
+
const make_mutex_1 = require("../Utils/make-mutex");
|
|
16
|
+
const WABinary_1 = require("../WABinary");
|
|
17
|
+
const groups_1 = require("./groups");
|
|
18
|
+
const messages_send_1 = require("./messages-send");
|
|
19
|
+
const makeMessagesRecvSocket = (config) => {
|
|
20
|
+
const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid } = config;
|
|
21
|
+
const sock = (0, messages_send_1.makeMessagesSocket)(config);
|
|
22
|
+
const { ev, authState, ws, processingMutex, signalRepository, query, upsertMessage, resyncAppState, groupMetadata, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, createParticipantNodes, getUSyncDevices, sendPeerDataOperationMessage, } = sock;
|
|
23
|
+
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
|
24
|
+
const retryMutex = (0, make_mutex_1.makeMutex)();
|
|
25
|
+
const msgRetryCache = config.msgRetryCounterCache || new node_cache_1.default({
|
|
26
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
27
|
+
useClones: false
|
|
28
|
+
});
|
|
29
|
+
const callOfferCache = config.callOfferCache || new node_cache_1.default({
|
|
30
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.CALL_OFFER,
|
|
31
|
+
useClones: false
|
|
32
|
+
});
|
|
33
|
+
const placeholderResendCache = config.placeholderResendCache || new node_cache_1.default({
|
|
34
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
35
|
+
useClones: false
|
|
36
|
+
});
|
|
37
|
+
const sentMessageCache = new Map();
|
|
38
|
+
const sentMessageCacheMaxSize = 256;
|
|
39
|
+
const getSentMessageCacheKey = (remoteJid, id) => `${remoteJid}:${id}`;
|
|
40
|
+
const getCachedSentMessage = (remoteJid, id) => {
|
|
41
|
+
const cacheKey = getSentMessageCacheKey(remoteJid, id);
|
|
42
|
+
const existing = sentMessageCache.get(cacheKey);
|
|
43
|
+
if (!existing) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
sentMessageCache.delete(cacheKey);
|
|
47
|
+
sentMessageCache.set(cacheKey, existing);
|
|
48
|
+
return existing;
|
|
49
|
+
};
|
|
50
|
+
const cacheSentMessage = (remoteJid, id, message) => {
|
|
51
|
+
const cacheKey = getSentMessageCacheKey(remoteJid, id);
|
|
52
|
+
if (sentMessageCache.has(cacheKey)) {
|
|
53
|
+
sentMessageCache.delete(cacheKey);
|
|
54
|
+
}
|
|
55
|
+
sentMessageCache.set(cacheKey, message);
|
|
56
|
+
while (sentMessageCache.size > sentMessageCacheMaxSize) {
|
|
57
|
+
const oldestKey = sentMessageCache.keys().next().value;
|
|
58
|
+
if (!oldestKey) {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
sentMessageCache.delete(oldestKey);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
let sendActiveReceipts = false;
|
|
65
|
+
const resolveJid = (jid) => {
|
|
66
|
+
if (typeof jid === 'string' && jid.endsWith('@lid')) {
|
|
67
|
+
return (0, WABinary_1.lidToJid)(jid);
|
|
68
|
+
}
|
|
69
|
+
return jid;
|
|
70
|
+
};
|
|
71
|
+
const sendMessageAck = async ({ tag, attrs, content }, errorCode) => {
|
|
72
|
+
const stanza = {
|
|
73
|
+
tag: 'ack',
|
|
74
|
+
attrs: {
|
|
75
|
+
id: attrs.id,
|
|
76
|
+
to: attrs.from,
|
|
77
|
+
class: tag
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
if (!!errorCode) {
|
|
81
|
+
stanza.attrs.error = errorCode.toString();
|
|
82
|
+
}
|
|
83
|
+
if (!!attrs.participant) {
|
|
84
|
+
stanza.attrs.participant = attrs.participant;
|
|
85
|
+
}
|
|
86
|
+
if (!!attrs.recipient) {
|
|
87
|
+
stanza.attrs.recipient = attrs.recipient;
|
|
88
|
+
}
|
|
89
|
+
if (!!attrs.type && (tag !== 'message' || (0, WABinary_1.getBinaryNodeChild)({ tag, attrs, content }, 'unavailable') || errorCode !== 0)) {
|
|
90
|
+
stanza.attrs.type = attrs.type;
|
|
91
|
+
}
|
|
92
|
+
if (tag === 'message' && (0, WABinary_1.getBinaryNodeChild)({ tag, attrs, content }, 'unavailable')) {
|
|
93
|
+
stanza.attrs.from = authState.creds.me.id;
|
|
94
|
+
}
|
|
95
|
+
logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');
|
|
96
|
+
await sendNode(stanza);
|
|
97
|
+
};
|
|
98
|
+
// Add withAck wrapper for guaranteed acknowledgments so less ban tag ig
|
|
99
|
+
const withAck = (processFn) => async (node) => {
|
|
100
|
+
try {
|
|
101
|
+
await processFn(node);
|
|
102
|
+
} finally {
|
|
103
|
+
// Always send ack even on failure to allow potential retry (not sure bout this tho)
|
|
104
|
+
await sendMessageAck(node);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const offerCall = async (toJid, isVideo = false) => {
|
|
108
|
+
toJid = resolveJid(toJid);
|
|
109
|
+
const callId = (0, crypto_1.randomBytes)(16).toString('hex').toUpperCase().substring(0, 64);
|
|
110
|
+
const offerContent = [];
|
|
111
|
+
offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '16000' }, content: undefined });
|
|
112
|
+
offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '8000' }, content: undefined });
|
|
113
|
+
if (isVideo) {
|
|
114
|
+
offerContent.push({
|
|
115
|
+
tag: 'video',
|
|
116
|
+
attrs: {
|
|
117
|
+
orientation: '0',
|
|
118
|
+
'screen_width': '1920',
|
|
119
|
+
'screen_height': '1080',
|
|
120
|
+
'device_orientation': '0',
|
|
121
|
+
enc: 'vp8',
|
|
122
|
+
dec: 'vp8',
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
offerContent.push({ tag: 'net', attrs: { medium: '3' }, content: undefined });
|
|
127
|
+
offerContent.push({ tag: 'capability', attrs: { ver: '1' }, content: new Uint8Array([1, 4, 255, 131, 207, 4]) });
|
|
128
|
+
offerContent.push({ tag: 'encopt', attrs: { keygen: '2' }, content: undefined });
|
|
129
|
+
const encKey = (0, crypto_1.randomBytes)(32);
|
|
130
|
+
const devices = (await getUSyncDevices([toJid], true, false)).map(({ user, device }) => (0, WABinary_1.jidEncode)(user, 's.whatsapp.net', device));
|
|
131
|
+
await assertSessions(devices, true);
|
|
132
|
+
const { nodes: destinations, shouldIncludeDeviceIdentity } = await createParticipantNodes(devices, {
|
|
133
|
+
call: {
|
|
134
|
+
callKey: encKey
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
offerContent.push({ tag: 'destination', attrs: {}, content: destinations });
|
|
138
|
+
if (shouldIncludeDeviceIdentity) {
|
|
139
|
+
offerContent.push({
|
|
140
|
+
tag: 'device-identity',
|
|
141
|
+
attrs: {},
|
|
142
|
+
content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const stanza = ({
|
|
146
|
+
tag: 'call',
|
|
147
|
+
attrs: {
|
|
148
|
+
to: toJid,
|
|
149
|
+
},
|
|
150
|
+
content: [{
|
|
151
|
+
tag: 'offer',
|
|
152
|
+
attrs: {
|
|
153
|
+
'call-id': callId,
|
|
154
|
+
'call-creator': authState.creds.me.id,
|
|
155
|
+
},
|
|
156
|
+
content: offerContent,
|
|
157
|
+
}],
|
|
158
|
+
});
|
|
159
|
+
await query(stanza);
|
|
160
|
+
return {
|
|
161
|
+
callId,
|
|
162
|
+
toJid,
|
|
163
|
+
isVideo,
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
const rejectCall = async (callId, callFrom) => {
|
|
167
|
+
callFrom = resolveJid(callFrom);
|
|
168
|
+
const stanza = ({
|
|
169
|
+
tag: 'call',
|
|
170
|
+
attrs: {
|
|
171
|
+
from: authState.creds.me.id,
|
|
172
|
+
to: callFrom,
|
|
173
|
+
},
|
|
174
|
+
content: [{
|
|
175
|
+
tag: 'reject',
|
|
176
|
+
attrs: {
|
|
177
|
+
'call-id': callId,
|
|
178
|
+
'call-creator': callFrom,
|
|
179
|
+
count: '0',
|
|
180
|
+
},
|
|
181
|
+
content: undefined,
|
|
182
|
+
}],
|
|
183
|
+
});
|
|
184
|
+
await query(stanza);
|
|
185
|
+
};
|
|
186
|
+
const sendRetryRequest = async (node, forceIncludeKeys = false) => {
|
|
187
|
+
const { fullMessage } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '');
|
|
188
|
+
const { key: msgKey } = fullMessage;
|
|
189
|
+
const msgId = msgKey.id;
|
|
190
|
+
const key = `${msgId}:${msgKey === null || msgKey === void 0 ? void 0 : msgKey.participant}`;
|
|
191
|
+
let retryCount = msgRetryCache.get(key) || 0;
|
|
192
|
+
if (retryCount >= maxMsgRetryCount) {
|
|
193
|
+
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');
|
|
194
|
+
msgRetryCache.del(key);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
retryCount += 1;
|
|
198
|
+
msgRetryCache.set(key, retryCount);
|
|
199
|
+
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;
|
|
200
|
+
if (retryCount === 1) {
|
|
201
|
+
const msgId = await requestPlaceholderResend(msgKey);
|
|
202
|
+
logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId}`);
|
|
203
|
+
}
|
|
204
|
+
const deviceIdentity = (0, Utils_1.encodeSignedDeviceIdentity)(account, true);
|
|
205
|
+
await authState.keys.transaction(async () => {
|
|
206
|
+
const receipt = {
|
|
207
|
+
tag: 'receipt',
|
|
208
|
+
attrs: {
|
|
209
|
+
id: msgId,
|
|
210
|
+
type: 'retry',
|
|
211
|
+
to: node.attrs.from
|
|
212
|
+
},
|
|
213
|
+
content: [
|
|
214
|
+
{
|
|
215
|
+
tag: 'retry',
|
|
216
|
+
attrs: {
|
|
217
|
+
count: retryCount.toString(),
|
|
218
|
+
id: node.attrs.id,
|
|
219
|
+
t: node.attrs.t,
|
|
220
|
+
v: '1'
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
tag: 'registration',
|
|
225
|
+
attrs: {},
|
|
226
|
+
content: (0, Utils_1.encodeBigEndian)(authState.creds.registrationId)
|
|
227
|
+
}
|
|
228
|
+
]
|
|
229
|
+
};
|
|
230
|
+
if (node.attrs.recipient) {
|
|
231
|
+
receipt.attrs.recipient = node.attrs.recipient;
|
|
232
|
+
}
|
|
233
|
+
if (node.attrs.participant) {
|
|
234
|
+
receipt.attrs.participant = node.attrs.participant;
|
|
235
|
+
}
|
|
236
|
+
if (retryCount > 1 || forceIncludeKeys) {
|
|
237
|
+
const { update, preKeys } = await (0, Utils_1.getNextPreKeys)(authState, 1);
|
|
238
|
+
const [keyId] = Object.keys(preKeys);
|
|
239
|
+
const key = preKeys[+keyId];
|
|
240
|
+
const content = receipt.content;
|
|
241
|
+
content.push({
|
|
242
|
+
tag: 'keys',
|
|
243
|
+
attrs: {},
|
|
244
|
+
content: [
|
|
245
|
+
{ tag: 'type', attrs: {}, content: Buffer.from(Defaults_1.KEY_BUNDLE_TYPE) },
|
|
246
|
+
{ tag: 'identity', attrs: {}, content: identityKey.public },
|
|
247
|
+
(0, Utils_1.xmppPreKey)(key, +keyId),
|
|
248
|
+
(0, Utils_1.xmppSignedPreKey)(signedPreKey),
|
|
249
|
+
{ tag: 'device-identity', attrs: {}, content: deviceIdentity }
|
|
250
|
+
]
|
|
251
|
+
});
|
|
252
|
+
ev.emit('creds.update', update);
|
|
253
|
+
}
|
|
254
|
+
await sendNode(receipt);
|
|
255
|
+
logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt');
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
const handleEncryptNotification = async (node) => {
|
|
259
|
+
const from = node.attrs.from;
|
|
260
|
+
if (from === WABinary_1.S_WHATSAPP_NET) {
|
|
261
|
+
const countChild = (0, WABinary_1.getBinaryNodeChild)(node, 'count');
|
|
262
|
+
const count = +countChild.attrs.value;
|
|
263
|
+
const shouldUploadMorePreKeys = count < Defaults_1.MIN_PREKEY_COUNT;
|
|
264
|
+
logger.debug({ count, shouldUploadMorePreKeys }, 'recv pre-key count');
|
|
265
|
+
if (shouldUploadMorePreKeys) {
|
|
266
|
+
await uploadPreKeys();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
const identityNode = (0, WABinary_1.getBinaryNodeChild)(node, 'identity');
|
|
271
|
+
if (identityNode) {
|
|
272
|
+
logger.info({ jid: from }, 'identity changed');
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
logger.info({ node }, 'unknown encrypt notification');
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
const toLidIfNecessary = (jid) => {
|
|
280
|
+
if (typeof jid !== 'string')
|
|
281
|
+
return jid;
|
|
282
|
+
const [user, server] = jid.split('@');
|
|
283
|
+
if (server === 's.whatsapp.net' && /^[0-9]+$/.test(user) && user.length >= 12) {
|
|
284
|
+
return `${user}@lid`;
|
|
285
|
+
}
|
|
286
|
+
return jid;
|
|
287
|
+
};
|
|
288
|
+
// Helper for LID resolution in group context by samakavare
|
|
289
|
+
const resolveLidFromGroupContext = async (lid, groupJid) => {
|
|
290
|
+
if (!(0, WABinary_1.isLid)(lid) || !(0, WABinary_1.isJidGroup)(groupJid)) {
|
|
291
|
+
return lid;
|
|
292
|
+
}
|
|
293
|
+
const metadata = await groupMetadata(groupJid);
|
|
294
|
+
const found = metadata.participants.find(p => p.id === lid);
|
|
295
|
+
let jid = found?.jid;
|
|
296
|
+
if (!jid) {
|
|
297
|
+
jid = config.lidCache?.get(lid);
|
|
298
|
+
}
|
|
299
|
+
return jid || (0, WABinary_1.lidToJid)(lid); // Fallback to naive if not found
|
|
300
|
+
};
|
|
301
|
+
const handleGroupNotification = async (participant, participantPn, child, groupJid, msg) => {
|
|
302
|
+
var _a, _b, _c, _d;
|
|
303
|
+
const childTag = child === null || child === void 0 ? void 0 : child.tag;
|
|
304
|
+
if (participantPn && participant && (0, WABinary_1.isLid)(participant) && config.lidCache?.set) {
|
|
305
|
+
// NOTE: in most if not every w:gp2 stubs participant_pn refer to the actor (admin) not the target
|
|
306
|
+
config.lidCache.set(participant, participantPn);
|
|
307
|
+
}
|
|
308
|
+
const participantJid = (((_b = (_a = (0, WABinary_1.getBinaryNodeChild)(child, 'participant')) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.jid) || participant);
|
|
309
|
+
if (participantPn && participantJid && (0, WABinary_1.isLid)(participantJid) && config.lidCache?.set &&
|
|
310
|
+
(childTag === 'created_membership_requests' || childTag === 'revoked_membership_requests')) {
|
|
311
|
+
// For membership requests, participant_pn refers to the requester (target), not the actor
|
|
312
|
+
config.lidCache.set(participantJid, participantPn);
|
|
313
|
+
}
|
|
314
|
+
switch (child === null || child === void 0 ? void 0 : child.tag) {
|
|
315
|
+
case 'create':
|
|
316
|
+
let metadata = (0, groups_1.extractGroupMetadata)(child);
|
|
317
|
+
const fullMetadata = await groupMetadata(groupJid);
|
|
318
|
+
if (metadata.owner && metadata.owner.endsWith('@lid')) {
|
|
319
|
+
const found = fullMetadata.participants.find(p => p.id === metadata.owner);
|
|
320
|
+
metadata.owner = found?.jid || (0, WABinary_1.lidToJid)(metadata.owner);
|
|
321
|
+
}
|
|
322
|
+
let resolvedAuthor = participant;
|
|
323
|
+
if (participant.endsWith('@lid')) {
|
|
324
|
+
const found = fullMetadata.participants.find(p => p.id === participant);
|
|
325
|
+
resolvedAuthor = found?.jid || (0, WABinary_1.lidToJid)(participant);
|
|
326
|
+
}
|
|
327
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CREATE;
|
|
328
|
+
msg.messageStubParameters = [metadata.subject];
|
|
329
|
+
msg.key = { participant: metadata.owner };
|
|
330
|
+
ev.emit('chats.upsert', [{
|
|
331
|
+
id: metadata.id,
|
|
332
|
+
name: metadata.subject,
|
|
333
|
+
conversationTimestamp: metadata.creation,
|
|
334
|
+
}]);
|
|
335
|
+
ev.emit('groups.upsert', [{
|
|
336
|
+
...metadata,
|
|
337
|
+
author: resolvedAuthor
|
|
338
|
+
}]);
|
|
339
|
+
break;
|
|
340
|
+
case 'ephemeral':
|
|
341
|
+
case 'not_ephemeral':
|
|
342
|
+
msg.message = {
|
|
343
|
+
protocolMessage: {
|
|
344
|
+
type: WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
345
|
+
ephemeralExpiration: +(child.attrs.expiration || 0)
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
break;
|
|
349
|
+
case 'modify':
|
|
350
|
+
const oldNumber = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
|
|
351
|
+
msg.messageStubParameters = oldNumber || [];
|
|
352
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER;
|
|
353
|
+
break;
|
|
354
|
+
case 'promote':
|
|
355
|
+
case 'demote':
|
|
356
|
+
case 'remove':
|
|
357
|
+
case 'add':
|
|
358
|
+
case 'leave':
|
|
359
|
+
const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;
|
|
360
|
+
msg.messageStubType = Types_1.WAMessageStubType[stubType];
|
|
361
|
+
const participants = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
|
|
362
|
+
if (participants.length === 1 &&
|
|
363
|
+
(0, WABinary_1.areJidsSameUser)(participants[0], participant) &&
|
|
364
|
+
child.tag === 'remove') {
|
|
365
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;
|
|
366
|
+
}
|
|
367
|
+
if ((child.tag === 'leave' || msg.messageStubType === Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE)
|
|
368
|
+
&& participants.length === 1
|
|
369
|
+
&& participantPn
|
|
370
|
+
&& typeof participantPn === 'string') {
|
|
371
|
+
msg.messageStubParameters = [participantPn];
|
|
372
|
+
if (participant && (0, WABinary_1.isLid)(participant)) {
|
|
373
|
+
participant = participantPn;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
msg.messageStubParameters = participants;
|
|
378
|
+
}
|
|
379
|
+
break;
|
|
380
|
+
case 'subject':
|
|
381
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;
|
|
382
|
+
msg.messageStubParameters = [child.attrs.subject];
|
|
383
|
+
break;
|
|
384
|
+
case 'description':
|
|
385
|
+
const description = (_d = (_c = (0, WABinary_1.getBinaryNodeChild)(child, 'body')) === null || _c === void 0 ? void 0 : _c.content) === null || _d === void 0 ? void 0 : _d.toString();
|
|
386
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION;
|
|
387
|
+
msg.messageStubParameters = description ? [description] : undefined;
|
|
388
|
+
break;
|
|
389
|
+
case 'announcement':
|
|
390
|
+
case 'not_announcement':
|
|
391
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;
|
|
392
|
+
msg.messageStubParameters = [(child.tag === 'announcement') ? 'on' : 'off'];
|
|
393
|
+
break;
|
|
394
|
+
case 'locked':
|
|
395
|
+
case 'unlocked':
|
|
396
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT;
|
|
397
|
+
msg.messageStubParameters = [(child.tag === 'locked') ? 'on' : 'off'];
|
|
398
|
+
break;
|
|
399
|
+
case 'invite':
|
|
400
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK;
|
|
401
|
+
msg.messageStubParameters = [child.attrs.code];
|
|
402
|
+
break;
|
|
403
|
+
case 'member_add_mode':
|
|
404
|
+
const addMode = child.content;
|
|
405
|
+
if (addMode) {
|
|
406
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE;
|
|
407
|
+
msg.messageStubParameters = [addMode.toString()];
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
case 'membership_approval_mode':
|
|
411
|
+
const approvalMode = (0, WABinary_1.getBinaryNodeChild)(child, 'group_join');
|
|
412
|
+
if (approvalMode) {
|
|
413
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE;
|
|
414
|
+
msg.messageStubParameters = [approvalMode.attrs.state];
|
|
415
|
+
}
|
|
416
|
+
break;
|
|
417
|
+
case 'created_membership_requests':
|
|
418
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
|
|
419
|
+
msg.messageStubParameters = [participantPn || participantJid, 'created', child.attrs.request_method];
|
|
420
|
+
break;
|
|
421
|
+
case 'revoked_membership_requests':
|
|
422
|
+
const isDenied = (0, WABinary_1.areJidsSameUser)(participantJid, participant);
|
|
423
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
|
|
424
|
+
msg.messageStubParameters = [participantPn || participantJid, isDenied ? 'revoked' : 'rejected'];
|
|
425
|
+
break;
|
|
426
|
+
default:
|
|
427
|
+
// console.log("BAILEYS-DEBUG:", JSON.stringify({ ...child, content: Buffer.isBuffer(child.content) ? child.content.toString() : child.content, participant }, null, 2))
|
|
428
|
+
}
|
|
429
|
+
if (msg.messageStubParameters) {
|
|
430
|
+
msg.messageStubParameters = msg.messageStubParameters.map(toLidIfNecessary);
|
|
431
|
+
}
|
|
432
|
+
participant = toLidIfNecessary(participant);
|
|
433
|
+
if (msg.key?.participant) {
|
|
434
|
+
msg.key.participant = toLidIfNecessary(msg.key.participant);
|
|
435
|
+
}
|
|
436
|
+
const needsResolving = (msg.messageStubParameters && msg.messageStubParameters.some(p => (0, WABinary_1.isLid)(p))) ||
|
|
437
|
+
(participant && (0, WABinary_1.isLid)(participant)) ||
|
|
438
|
+
(msg.key?.participant && (0, WABinary_1.isLid)(msg.key.participant));
|
|
439
|
+
if (needsResolving) {
|
|
440
|
+
const metadata = await groupMetadata(groupJid);
|
|
441
|
+
const { lidCache } = config;
|
|
442
|
+
const resolveLid = (lid) => {
|
|
443
|
+
const found = metadata.participants.find(p => p.id === lid);
|
|
444
|
+
let jid = found === null || found === void 0 ? void 0 : found.jid;
|
|
445
|
+
if (!jid) {
|
|
446
|
+
jid = lidCache === null || lidCache === void 0 ? void 0 : lidCache.get(lid);
|
|
447
|
+
}
|
|
448
|
+
return jid || (0, WABinary_1.lidToJid)(lid);
|
|
449
|
+
};
|
|
450
|
+
if (msg.messageStubParameters) {
|
|
451
|
+
msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' && (0, WABinary_1.isLid)(param) ? await resolveLidFromGroupContext(param, groupJid) : param)));
|
|
452
|
+
}
|
|
453
|
+
if ((0, WABinary_1.isLid)(participant)) {
|
|
454
|
+
msg.participant = await resolveLidFromGroupContext(participant, groupJid);
|
|
455
|
+
}
|
|
456
|
+
else if (participant) {
|
|
457
|
+
//If it's a JID, treat it as a JID. Do not convert back to LID. *smh brah
|
|
458
|
+
msg.participant = participant;
|
|
459
|
+
}
|
|
460
|
+
if (msg.key && (0, WABinary_1.isLid)(msg.key.participant)) {
|
|
461
|
+
msg.key.participant = await resolveLidFromGroupContext(msg.key.participant, groupJid);
|
|
462
|
+
}
|
|
463
|
+
else if (msg.key && msg.key.participant) {
|
|
464
|
+
// If it's a JID, treat it as a JID. Do not convert back to LID. *smh brah pt2
|
|
465
|
+
msg.key.participant = msg.key.participant;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
const handleNewsletterNotification = (id, node) => {
|
|
470
|
+
const messages = (0, WABinary_1.getBinaryNodeChild)(node, 'messages');
|
|
471
|
+
const message = (0, WABinary_1.getBinaryNodeChild)(messages, 'message');
|
|
472
|
+
const serverId = message.attrs.server_id;
|
|
473
|
+
const reactionsList = (0, WABinary_1.getBinaryNodeChild)(message, 'reactions');
|
|
474
|
+
const viewsList = (0, WABinary_1.getBinaryNodeChildren)(message, 'views_count');
|
|
475
|
+
if (reactionsList) {
|
|
476
|
+
const reactions = (0, WABinary_1.getBinaryNodeChildren)(reactionsList, 'reaction');
|
|
477
|
+
if (reactions.length === 0) {
|
|
478
|
+
ev.emit('newsletter.reaction', { id, 'server_id': serverId, reaction: { removed: true } });
|
|
479
|
+
}
|
|
480
|
+
reactions.forEach(item => {
|
|
481
|
+
var _a, _b;
|
|
482
|
+
ev.emit('newsletter.reaction', { id, 'server_id': serverId, reaction: { code: (_a = item.attrs) === null || _a === void 0 ? void 0 : _a.code, count: +((_b = item.attrs) === null || _b === void 0 ? void 0 : _b.count) } });
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
if (viewsList.length) {
|
|
486
|
+
viewsList.forEach(item => {
|
|
487
|
+
ev.emit('newsletter.view', { id, 'server_id': serverId, count: +item.attrs.count });
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
const handleMexNewsletterNotification = (id, node) => {
|
|
492
|
+
var _a;
|
|
493
|
+
const operation = node === null || node === void 0 ? void 0 : node.attrs.op_name;
|
|
494
|
+
const content = JSON.parse((_a = node === null || node === void 0 ? void 0 : node.content) === null || _a === void 0 ? void 0 : _a.toString());
|
|
495
|
+
let contentPath;
|
|
496
|
+
if (operation === Types_1.MexOperations.PROMOTE || operation === Types_1.MexOperations.DEMOTE) {
|
|
497
|
+
let action;
|
|
498
|
+
if (operation === Types_1.MexOperations.PROMOTE) {
|
|
499
|
+
action = 'promote';
|
|
500
|
+
contentPath = content.data[Types_1.XWAPaths.PROMOTE];
|
|
501
|
+
}
|
|
502
|
+
if (operation === Types_1.MexOperations.DEMOTE) {
|
|
503
|
+
action = 'demote';
|
|
504
|
+
contentPath = content.data[Types_1.XWAPaths.DEMOTE];
|
|
505
|
+
}
|
|
506
|
+
const author = resolveJid(contentPath.actor.pn);
|
|
507
|
+
const user = resolveJid(contentPath.user.pn);
|
|
508
|
+
ev.emit('newsletter-participants.update', { id, author, user, new_role: contentPath.user_new_role, action });
|
|
509
|
+
}
|
|
510
|
+
if (operation === Types_1.MexOperations.UPDATE) {
|
|
511
|
+
contentPath = content.data[Types_1.XWAPaths.METADATA_UPDATE];
|
|
512
|
+
ev.emit('newsletter-settings.update', { id, update: contentPath.thread_metadata.settings });
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
const processNotification = async (node) => {
|
|
516
|
+
var _a, _b;
|
|
517
|
+
const result = {};
|
|
518
|
+
const [child] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
|
|
519
|
+
const nodeType = node.attrs.type;
|
|
520
|
+
const from = resolveJid((0, WABinary_1.jidNormalizedUser)(node.attrs.from));
|
|
521
|
+
switch (nodeType) {
|
|
522
|
+
case 'privacy_token':
|
|
523
|
+
const tokenList = (0, WABinary_1.getBinaryNodeChildren)(child, 'token');
|
|
524
|
+
for (const { attrs, content } of tokenList) {
|
|
525
|
+
const jid = resolveJid(attrs.jid);
|
|
526
|
+
ev.emit('chats.update', [
|
|
527
|
+
{
|
|
528
|
+
id: jid,
|
|
529
|
+
tcToken: content
|
|
530
|
+
}
|
|
531
|
+
]);
|
|
532
|
+
logger.debug({ jid }, 'got privacy token update');
|
|
533
|
+
}
|
|
534
|
+
break;
|
|
535
|
+
case 'newsletter':
|
|
536
|
+
handleNewsletterNotification(node.attrs.from, child);
|
|
537
|
+
break;
|
|
538
|
+
case 'mex':
|
|
539
|
+
handleMexNewsletterNotification(node.attrs.from, child);
|
|
540
|
+
break;
|
|
541
|
+
case 'w:gp2':
|
|
542
|
+
await handleGroupNotification(node.attrs.participant, node.attrs.participant_pn, child, from, result);
|
|
543
|
+
break;
|
|
544
|
+
case 'mediaretry':
|
|
545
|
+
const event = (0, Utils_1.decodeMediaRetryNode)(node);
|
|
546
|
+
ev.emit('messages.media-update', [event]);
|
|
547
|
+
break;
|
|
548
|
+
case 'encrypt':
|
|
549
|
+
await handleEncryptNotification(node);
|
|
550
|
+
break;
|
|
551
|
+
case 'devices':
|
|
552
|
+
const devices = (0, WABinary_1.getBinaryNodeChildren)(child, 'device');
|
|
553
|
+
if ((0, WABinary_1.areJidsSameUser)(child.attrs.jid, authState.creds.me.id)) {
|
|
554
|
+
const deviceJids = devices.map(d => resolveJid(d.attrs.jid));
|
|
555
|
+
logger.info({ deviceJids }, 'got my own devices');
|
|
556
|
+
}
|
|
557
|
+
break;
|
|
558
|
+
case 'server_sync':
|
|
559
|
+
const update = (0, WABinary_1.getBinaryNodeChild)(node, 'collection');
|
|
560
|
+
if (update) {
|
|
561
|
+
const name = update.attrs.name;
|
|
562
|
+
await resyncAppState([name], false);
|
|
563
|
+
}
|
|
564
|
+
break;
|
|
565
|
+
case 'picture':
|
|
566
|
+
const setPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'set');
|
|
567
|
+
const delPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'delete');
|
|
568
|
+
ev.emit('contacts.update', [{
|
|
569
|
+
id: resolveJid(from) || ((_b = (_a = (setPicture || delPicture)) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.hash) || '',
|
|
570
|
+
imgUrl: setPicture ? 'changed' : 'removed'
|
|
571
|
+
}]);
|
|
572
|
+
if ((0, WABinary_1.isJidGroup)(from)) {
|
|
573
|
+
const node = setPicture || delPicture;
|
|
574
|
+
result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ICON;
|
|
575
|
+
if (setPicture) {
|
|
576
|
+
result.messageStubParameters = [setPicture.attrs.id];
|
|
577
|
+
}
|
|
578
|
+
result.participant = node === null || node === void 0 ? void 0 : node.attrs.author;
|
|
579
|
+
result.key = {
|
|
580
|
+
...result.key || {},
|
|
581
|
+
participant: setPicture === null || setPicture === void 0 ? void 0 : setPicture.attrs.author
|
|
582
|
+
};
|
|
583
|
+
if (result.participant && (0, WABinary_1.isLid)(result.participant)) {
|
|
584
|
+
result.participant = await resolveLidFromGroupContext(result.participant, from);
|
|
585
|
+
}
|
|
586
|
+
if (result.key?.participant && (0, WABinary_1.isLid)(result.key.participant)) {
|
|
587
|
+
result.key.participant = await resolveLidFromGroupContext(result.key.participant, from);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
break;
|
|
591
|
+
case 'account_sync':
|
|
592
|
+
if (child.tag === 'disappearing_mode') {
|
|
593
|
+
const newDuration = +child.attrs.duration;
|
|
594
|
+
const timestamp = +child.attrs.t;
|
|
595
|
+
logger.info({ newDuration }, 'updated account disappearing mode');
|
|
596
|
+
ev.emit('creds.update', {
|
|
597
|
+
accountSettings: {
|
|
598
|
+
...authState.creds.accountSettings,
|
|
599
|
+
defaultDisappearingMode: {
|
|
600
|
+
ephemeralExpiration: newDuration,
|
|
601
|
+
ephemeralSettingTimestamp: timestamp,
|
|
602
|
+
},
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
else if (child.tag === 'blocklist') {
|
|
607
|
+
const blocklists = (0, WABinary_1.getBinaryNodeChildren)(child, 'item');
|
|
608
|
+
for (const { attrs } of blocklists) {
|
|
609
|
+
const blocklist = [resolveJid(attrs.jid)];
|
|
610
|
+
const type = (attrs.action === 'block') ? 'add' : 'remove';
|
|
611
|
+
ev.emit('blocklist.update', { blocklist, type });
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
break;
|
|
615
|
+
case 'link_code_companion_reg':
|
|
616
|
+
const linkCodeCompanionReg = (0, WABinary_1.getBinaryNodeChild)(node, 'link_code_companion_reg');
|
|
617
|
+
const ref = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_ref'));
|
|
618
|
+
const primaryIdentityPublicKey = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'primary_identity_pub'));
|
|
619
|
+
const primaryEphemeralPublicKeyWrapped = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_wrapped_primary_ephemeral_pub'));
|
|
620
|
+
const codePairingPublicKey = await decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped);
|
|
621
|
+
const companionSharedKey = Utils_1.Curve.sharedKey(authState.creds.pairingEphemeralKeyPair.private, codePairingPublicKey);
|
|
622
|
+
const random = (0, crypto_1.randomBytes)(32);
|
|
623
|
+
const linkCodeSalt = (0, crypto_1.randomBytes)(32);
|
|
624
|
+
const linkCodePairingExpanded = await (0, Utils_1.hkdf)(companionSharedKey, 32, {
|
|
625
|
+
salt: linkCodeSalt,
|
|
626
|
+
info: 'link_code_pairing_key_bundle_encryption_key'
|
|
627
|
+
});
|
|
628
|
+
const encryptPayload = Buffer.concat([Buffer.from(authState.creds.signedIdentityKey.public), primaryIdentityPublicKey, random]);
|
|
629
|
+
const encryptIv = (0, crypto_1.randomBytes)(12);
|
|
630
|
+
const encrypted = (0, Utils_1.aesEncryptGCM)(encryptPayload, linkCodePairingExpanded, encryptIv, Buffer.alloc(0));
|
|
631
|
+
const encryptedPayload = Buffer.concat([linkCodeSalt, encryptIv, encrypted]);
|
|
632
|
+
const identitySharedKey = Utils_1.Curve.sharedKey(authState.creds.signedIdentityKey.private, primaryIdentityPublicKey);
|
|
633
|
+
const identityPayload = Buffer.concat([companionSharedKey, identitySharedKey, random]);
|
|
634
|
+
authState.creds.advSecretKey = (await (0, Utils_1.hkdf)(identityPayload, 32, { info: 'adv_secret' })).toString('base64');
|
|
635
|
+
await query({
|
|
636
|
+
tag: 'iq',
|
|
637
|
+
attrs: {
|
|
638
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
639
|
+
type: 'set',
|
|
640
|
+
id: sock.generateMessageTag(),
|
|
641
|
+
xmlns: 'md'
|
|
642
|
+
},
|
|
643
|
+
content: [
|
|
644
|
+
{
|
|
645
|
+
tag: 'link_code_companion_reg',
|
|
646
|
+
attrs: {
|
|
647
|
+
jid: authState.creds.me.id,
|
|
648
|
+
stage: 'companion_finish',
|
|
649
|
+
},
|
|
650
|
+
content: [
|
|
651
|
+
{
|
|
652
|
+
tag: 'link_code_pairing_wrapped_key_bundle',
|
|
653
|
+
attrs: {},
|
|
654
|
+
content: encryptedPayload
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
tag: 'companion_identity_public',
|
|
658
|
+
attrs: {},
|
|
659
|
+
content: authState.creds.signedIdentityKey.public
|
|
660
|
+
},
|
|
661
|
+
{
|
|
662
|
+
tag: 'link_code_pairing_ref',
|
|
663
|
+
attrs: {},
|
|
664
|
+
content: ref
|
|
665
|
+
}
|
|
666
|
+
]
|
|
667
|
+
}
|
|
668
|
+
]
|
|
669
|
+
});
|
|
670
|
+
authState.creds.registered = true;
|
|
671
|
+
ev.emit('creds.update', authState.creds);
|
|
672
|
+
}
|
|
673
|
+
if (Object.keys(result).length) {
|
|
674
|
+
return result;
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
async function decipherLinkPublicKey(data) {
|
|
678
|
+
const buffer = toRequiredBuffer(data);
|
|
679
|
+
const salt = buffer.slice(0, 32);
|
|
680
|
+
const secretKey = await (0, Utils_1.derivePairingCodeKey)(authState.creds.pairingCode, salt);
|
|
681
|
+
const iv = buffer.slice(32, 48);
|
|
682
|
+
const payload = buffer.slice(48, 80);
|
|
683
|
+
return (0, Utils_1.aesDecryptCTR)(payload, secretKey, iv);
|
|
684
|
+
}
|
|
685
|
+
function toRequiredBuffer(data) {
|
|
686
|
+
if (data === undefined) {
|
|
687
|
+
throw new boom_1.Boom('Invalid buffer', { statusCode: 400 });
|
|
688
|
+
}
|
|
689
|
+
return data instanceof Buffer ? data : Buffer.from(data);
|
|
690
|
+
}
|
|
691
|
+
const willSendMessageAgain = (id, participant) => {
|
|
692
|
+
const key = `${id}:${participant}`;
|
|
693
|
+
const retryCount = msgRetryCache.get(key) || 0;
|
|
694
|
+
return retryCount < maxMsgRetryCount;
|
|
695
|
+
};
|
|
696
|
+
const updateSendMessageAgainCount = (id, participant) => {
|
|
697
|
+
const key = `${id}:${participant}`;
|
|
698
|
+
const newValue = (msgRetryCache.get(key) || 0) + 1;
|
|
699
|
+
msgRetryCache.set(key, newValue);
|
|
700
|
+
};
|
|
701
|
+
const sendMessagesAgain = async (key, ids, retryNode) => {
|
|
702
|
+
var _a;
|
|
703
|
+
// implement a cache to store the last 256 sent messages (copy whatsmeow) | (maybe should lower it)
|
|
704
|
+
const msgs = await Promise.all(ids.map(async (id) => {
|
|
705
|
+
const msg = await getMessage({ ...key, id });
|
|
706
|
+
return msg || getCachedSentMessage(key.remoteJid, id);
|
|
707
|
+
}));
|
|
708
|
+
const remoteJid = key.remoteJid;
|
|
709
|
+
const participant = key.participant || remoteJid;
|
|
710
|
+
const sendToAll = !((_a = (0, WABinary_1.jidDecode)(participant)) === null || _a === void 0 ? void 0 : _a.device);
|
|
711
|
+
await assertSessions([participant], true);
|
|
712
|
+
if ((0, WABinary_1.isJidGroup)(remoteJid)) {
|
|
713
|
+
await authState.keys.set({ 'sender-key-memory': { [remoteJid]: null } });
|
|
714
|
+
}
|
|
715
|
+
logger.debug({ participant, sendToAll }, 'forced new session for retry recp');
|
|
716
|
+
for (const [i, msg] of msgs.entries()) {
|
|
717
|
+
if (msg) {
|
|
718
|
+
updateSendMessageAgainCount(ids[i], participant);
|
|
719
|
+
const msgRelayOpts = { messageId: ids[i] };
|
|
720
|
+
if (sendToAll) {
|
|
721
|
+
msgRelayOpts.useUserDevicesCache = false;
|
|
722
|
+
}
|
|
723
|
+
else {
|
|
724
|
+
msgRelayOpts.participant = {
|
|
725
|
+
jid: participant,
|
|
726
|
+
count: +retryNode.attrs.count
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
await relayMessage(key.remoteJid, msg, msgRelayOpts);
|
|
730
|
+
}
|
|
731
|
+
else {
|
|
732
|
+
logger.debug({ jid: key.remoteJid, id: ids[i] }, 'recv retry request, but message not available');
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
const handleReceipt = async (node) => {
|
|
737
|
+
var _a, _b;
|
|
738
|
+
const { attrs, content } = node;
|
|
739
|
+
let participant = attrs.participant;
|
|
740
|
+
if (participant && (0, WABinary_1.isLid)(participant) && (0, WABinary_1.isJidGroup)(attrs.from)) {
|
|
741
|
+
const metadata = await groupMetadata(attrs.from);
|
|
742
|
+
const found = metadata.participants.find(p => p.id === participant);
|
|
743
|
+
const jid = found === null || found === void 0 ? void 0 : found.jid;
|
|
744
|
+
if (jid && !(0, WABinary_1.isLid)(jid)) {
|
|
745
|
+
participant = jid;
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
const cached = config.lidCache.get(participant);
|
|
749
|
+
if (cached) {
|
|
750
|
+
participant = cached;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
const isLid = attrs.from.includes('lid');
|
|
755
|
+
const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(resolveJid(participant) || resolveJid(attrs.from), isLid ? (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.lid : (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.id);
|
|
756
|
+
const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? resolveJid(attrs.from) : attrs.recipient;
|
|
757
|
+
const fromMe = !attrs.recipient || ((attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe);
|
|
758
|
+
const key = {
|
|
759
|
+
remoteJid,
|
|
760
|
+
id: '',
|
|
761
|
+
fromMe,
|
|
762
|
+
participant: resolveJid(participant)
|
|
763
|
+
};
|
|
764
|
+
if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
|
|
765
|
+
logger.debug({ remoteJid }, 'ignoring receipt from jid');
|
|
766
|
+
await sendMessageAck(node);
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
const ids = [attrs.id];
|
|
770
|
+
if (Array.isArray(content)) {
|
|
771
|
+
const items = (0, WABinary_1.getBinaryNodeChildren)(content[0], 'item');
|
|
772
|
+
ids.push(...items.map(i => i.attrs.id));
|
|
773
|
+
}
|
|
774
|
+
try {
|
|
775
|
+
await Promise.all([
|
|
776
|
+
processingMutex.mutex(async () => {
|
|
777
|
+
const status = (0, Utils_1.getStatusFromReceiptType)(attrs.type);
|
|
778
|
+
if (typeof status !== 'undefined' &&
|
|
779
|
+
(
|
|
780
|
+
status >= WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK ||
|
|
781
|
+
!isNodeFromMe)) {
|
|
782
|
+
if ((0, WABinary_1.isJidGroup)(remoteJid) || (0, WABinary_1.isJidStatusBroadcast)(remoteJid)) {
|
|
783
|
+
if (attrs.participant) {
|
|
784
|
+
const updateKey = status === WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';
|
|
785
|
+
ev.emit('message-receipt.update', ids.map(id => ({
|
|
786
|
+
key: { ...key, id },
|
|
787
|
+
receipt: {
|
|
788
|
+
userJid: (0, WABinary_1.jidNormalizedUser)(resolveJid(attrs.participant)),
|
|
789
|
+
[updateKey]: +attrs.t
|
|
790
|
+
}
|
|
791
|
+
})));
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
else {
|
|
795
|
+
ids.forEach(id => {
|
|
796
|
+
const statusName = Object.keys(WAProto_1.proto.WebMessageInfo.Status)[status] || `UNKNOWN_${status}`;
|
|
797
|
+
logger.debug({ remoteJid, id, status: statusName }, 'ACK status update');
|
|
798
|
+
});
|
|
799
|
+
ev.emit('messages.update', ids.map(id => ({
|
|
800
|
+
key: { ...key, id },
|
|
801
|
+
update: { status }
|
|
802
|
+
})));
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
if (status === WAProto_1.proto.WebMessageInfo.Status.ERROR) {
|
|
806
|
+
ev.emit('messages.update', ids.map(id => ({
|
|
807
|
+
key: { ...key, id },
|
|
808
|
+
update: { status: WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK }
|
|
809
|
+
})));
|
|
810
|
+
}
|
|
811
|
+
if (attrs.type === 'retry') {
|
|
812
|
+
key.participant = key.participant || attrs.from;
|
|
813
|
+
const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');
|
|
814
|
+
if (willSendMessageAgain(ids[0], key.participant)) {
|
|
815
|
+
if (key.fromMe) {
|
|
816
|
+
try {
|
|
817
|
+
logger.debug({ attrs, key }, 'recv retry request');
|
|
818
|
+
await sendMessagesAgain(key, ids, retryNode);
|
|
819
|
+
}
|
|
820
|
+
catch (error) {
|
|
821
|
+
logger.error({ key, ids, trace: error.stack }, 'error in sending message again');
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
logger.info({ attrs, key }, 'recv retry for not fromMe message');
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
else {
|
|
829
|
+
logger.info({ attrs, key }, 'will not send message again, as sent too many times');
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
})
|
|
833
|
+
]);
|
|
834
|
+
}
|
|
835
|
+
finally {
|
|
836
|
+
await sendMessageAck(node);
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
const handleNotification = async (node) => {
|
|
840
|
+
const remoteJid = resolveJid(node.attrs.from);
|
|
841
|
+
if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
|
|
842
|
+
logger.debug({ remoteJid, id: node.attrs.id }, 'ignored notification');
|
|
843
|
+
await sendMessageAck(node);
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
try {
|
|
847
|
+
await Promise.all([
|
|
848
|
+
processingMutex.mutex(async () => {
|
|
849
|
+
var _a;
|
|
850
|
+
const msg = await processNotification(node);
|
|
851
|
+
if (msg) {
|
|
852
|
+
const participant = msg.participant || resolveJid(node.attrs.participant);
|
|
853
|
+
const fromMe = (0, WABinary_1.areJidsSameUser)(participant || remoteJid, authState.creds.me.id);
|
|
854
|
+
const key = msg.key || {};
|
|
855
|
+
key.remoteJid = remoteJid;
|
|
856
|
+
key.fromMe = fromMe;
|
|
857
|
+
key.id = node.attrs.id;
|
|
858
|
+
key.participant = key.participant || participant;
|
|
859
|
+
msg.key = key;
|
|
860
|
+
msg.participant = participant;
|
|
861
|
+
msg.messageTimestamp = +node.attrs.t;
|
|
862
|
+
const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
|
|
863
|
+
await upsertMessage(fullMsg, 'append');
|
|
864
|
+
}
|
|
865
|
+
})
|
|
866
|
+
]);
|
|
867
|
+
}
|
|
868
|
+
finally {
|
|
869
|
+
await sendMessageAck(node);
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
const handleMessage = withAck(async (node) => {
|
|
873
|
+
var _a, _b, _c;
|
|
874
|
+
if (shouldIgnoreJid(node.attrs.from) && node.attrs.from !== '@s.whatsapp.net') {
|
|
875
|
+
logger.debug({ key: node.attrs.key }, 'ignored message');
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
|
|
879
|
+
// temporary fix for crashes and issues resulting of failed msmsg decryption
|
|
880
|
+
if (encNode && encNode.attrs.type === 'msmsg') {
|
|
881
|
+
logger.debug({ key: node.attrs.key }, 'recv msmsg, requesting retry');
|
|
882
|
+
retryMutex.mutex(async () => {
|
|
883
|
+
if (ws.isOpen) {
|
|
884
|
+
if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable')) {
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
await sendRetryRequest(node, false);
|
|
888
|
+
if (retryRequestDelayMs) {
|
|
889
|
+
await (0, Utils_1.delay)(retryRequestDelayMs);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
else {
|
|
893
|
+
logger.debug({ node }, 'connection closed, ignoring retry req');
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
let response;
|
|
899
|
+
if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable') && !encNode) {
|
|
900
|
+
const { key } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '').fullMessage;
|
|
901
|
+
response = await requestPlaceholderResend(key);
|
|
902
|
+
if (response === 'RESOLVED') {
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
logger.debug('received unavailable message, acked and requested resend from phone');
|
|
906
|
+
}
|
|
907
|
+
else {
|
|
908
|
+
if (placeholderResendCache.get(node.attrs.id)) {
|
|
909
|
+
placeholderResendCache.del(node.attrs.id);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
const { fullMessage: msg, category, author, decrypt } = (0, Utils_1.decryptMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, logger);
|
|
913
|
+
if (response && ((_a = msg === null || msg === void 0 ? void 0 : msg.messageStubParameters) === null || _a === void 0 ? void 0 : _a[0]) === Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT) {
|
|
914
|
+
msg.messageStubParameters = [Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT, response];
|
|
915
|
+
}
|
|
916
|
+
if (((_c = (_b = msg.message) === null || _b === void 0 ? void 0 : _b.protocolMessage) === null || _c === void 0 ? void 0 : _c.type) === WAProto_1.proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER && node.attrs.sender_pn) {
|
|
917
|
+
ev.emit('chats.phoneNumberShare', { lid: resolveJid(node.attrs.from), jid: resolveJid(node.attrs.sender_pn) });
|
|
918
|
+
}
|
|
919
|
+
try {
|
|
920
|
+
await Promise.all([
|
|
921
|
+
processingMutex.mutex(async () => {
|
|
922
|
+
var _a, _b, _c, _d, _e, _f;
|
|
923
|
+
await decrypt();
|
|
924
|
+
// if the message is from a group, and contains mentions, resolve the LIDs to JIDs by samakavare 🎐
|
|
925
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
926
|
+
const contextInfo = msg.message?.extendedTextMessage?.contextInfo;
|
|
927
|
+
if (contextInfo) {
|
|
928
|
+
const participant = contextInfo.participant;
|
|
929
|
+
const mentionedJid = contextInfo.mentionedJid;
|
|
930
|
+
const hasLidInParticipant = participant && participant.endsWith('@lid');
|
|
931
|
+
const hasLidInMention = mentionedJid && mentionedJid.some(j => j.endsWith('@lid'));
|
|
932
|
+
if (hasLidInParticipant || hasLidInMention) {
|
|
933
|
+
const metadata = await groupMetadata(msg.key.remoteJid);
|
|
934
|
+
if (hasLidInParticipant) {
|
|
935
|
+
const found = metadata.participants.find(p => p.id === participant);
|
|
936
|
+
contextInfo.participant = (found?.jid) || participant;
|
|
937
|
+
}
|
|
938
|
+
if (hasLidInMention) {
|
|
939
|
+
contextInfo.mentionedJid = await Promise.all(mentionedJid.map(async (jid) => {
|
|
940
|
+
if (jid.endsWith('@lid')) {
|
|
941
|
+
const found = metadata.participants.find(p => p.id === jid);
|
|
942
|
+
return (found?.jid) || jid;
|
|
943
|
+
}
|
|
944
|
+
return jid;
|
|
945
|
+
}));
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
// message failed to decrypt
|
|
951
|
+
if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {
|
|
952
|
+
if (((_a = msg === null || msg === void 0 ? void 0 : msg.messageStubParameters) === null || _a === void 0 ? void 0 : _a[0]) === Utils_1.MISSING_KEYS_ERROR_TEXT) {
|
|
953
|
+
return sendMessageAck(node, Utils_1.NACK_REASONS.ParsingError);
|
|
954
|
+
}
|
|
955
|
+
retryMutex.mutex(async () => {
|
|
956
|
+
if (ws.isOpen) {
|
|
957
|
+
if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable')) {
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
|
|
961
|
+
await sendRetryRequest(node, !encNode);
|
|
962
|
+
if (retryRequestDelayMs) {
|
|
963
|
+
await (0, Utils_1.delay)(retryRequestDelayMs);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
else {
|
|
967
|
+
logger.debug({ node }, 'connection closed, ignoring retry req');
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
else {
|
|
972
|
+
let type = undefined;
|
|
973
|
+
if ((_b = msg.key.participant) === null || _b === void 0 ? void 0 : _b.endsWith('@lid')) {
|
|
974
|
+
msg.key.participant = node.attrs.participant_pn || authState.creds.me.id;
|
|
975
|
+
}
|
|
976
|
+
if (!(0, WABinary_1.isJidGroup)(msg.key.remoteJid) && (0, WABinary_1.isLidUser)(msg.key.remoteJid)) {
|
|
977
|
+
msg.key.remoteJid = node.attrs.sender_pn || node.attrs.peer_recipient_pn;
|
|
978
|
+
}
|
|
979
|
+
let participant = msg.key.participant;
|
|
980
|
+
if (category === 'peer') {
|
|
981
|
+
type = 'peer_msg';
|
|
982
|
+
}
|
|
983
|
+
else if (msg.key.fromMe) {
|
|
984
|
+
type = 'sender';
|
|
985
|
+
if ((0, WABinary_1.isJidUser)(msg.key.remoteJid)) {
|
|
986
|
+
participant = author;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
else if (!sendActiveReceipts) {
|
|
990
|
+
type = 'inactive';
|
|
991
|
+
}
|
|
992
|
+
await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
|
|
993
|
+
const isAnyHistoryMsg = (0, Utils_1.getHistoryMsg)(msg.message);
|
|
994
|
+
if (isAnyHistoryMsg) {
|
|
995
|
+
const jid = (0, WABinary_1.jidNormalizedUser)(msg.key.remoteJid);
|
|
996
|
+
await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
if (msg.messageStubType) {
|
|
1000
|
+
const hasLidParam = msg.messageStubParameters && msg.messageStubParameters.some(p => typeof p === 'string' && (0, WABinary_1.isLid)(p));
|
|
1001
|
+
if (hasLidParam) {
|
|
1002
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
1003
|
+
const groupJid = msg.key.remoteJid;
|
|
1004
|
+
msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' && (0, WABinary_1.isLid)(param))
|
|
1005
|
+
? await resolveLidFromGroupContext(param, groupJid)
|
|
1006
|
+
: param));
|
|
1007
|
+
}
|
|
1008
|
+
else {
|
|
1009
|
+
msg.messageStubParameters = msg.messageStubParameters.map(param => (typeof param === 'string' && (0, WABinary_1.isLid)(param))
|
|
1010
|
+
? (0, WABinary_1.lidToJid)(param)
|
|
1011
|
+
: param);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
if (msg.key?.participant && typeof msg.key.participant === 'string' && (0, WABinary_1.isLid)(msg.key.participant)) {
|
|
1015
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
1016
|
+
msg.key.participant = node.attrs.participant_pn || await resolveLidFromGroupContext(msg.key.participant, msg.key.remoteJid);
|
|
1017
|
+
}
|
|
1018
|
+
else {
|
|
1019
|
+
msg.key.participant = node.attrs.participant_pn || (0, WABinary_1.lidToJid)(msg.key.participant);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
if (msg.participant && typeof msg.participant === 'string' && (0, WABinary_1.isLid)(msg.participant)) {
|
|
1023
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
1024
|
+
msg.participant = node.attrs.participant_pn || await resolveLidFromGroupContext(msg.participant, msg.key.remoteJid);
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
msg.participant = node.attrs.participant_pn || (0, WABinary_1.lidToJid)(msg.participant);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
(0, Utils_1.cleanMessage)(msg, authState.creds.me.id);
|
|
1032
|
+
await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
|
|
1033
|
+
})
|
|
1034
|
+
]);
|
|
1035
|
+
}
|
|
1036
|
+
catch (error) {
|
|
1037
|
+
logger.error({ error, node }, 'error in handling message');
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
const fetchMessageHistory = async (count, oldestMsgKey, oldestMsgTimestamp) => {
|
|
1041
|
+
var _a;
|
|
1042
|
+
if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
1043
|
+
throw new boom_1.Boom('Not authenticated');
|
|
1044
|
+
}
|
|
1045
|
+
const pdoMessage = {
|
|
1046
|
+
historySyncOnDemandRequest: {
|
|
1047
|
+
chatJid: oldestMsgKey.remoteJid,
|
|
1048
|
+
oldestMsgFromMe: oldestMsgKey.fromMe,
|
|
1049
|
+
oldestMsgId: oldestMsgKey.id,
|
|
1050
|
+
oldestMsgTimestampMs: oldestMsgTimestamp,
|
|
1051
|
+
onDemandMsgCount: count
|
|
1052
|
+
},
|
|
1053
|
+
peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.HISTORY_SYNC_ON_DEMAND
|
|
1054
|
+
};
|
|
1055
|
+
return sendPeerDataOperationMessage(pdoMessage);
|
|
1056
|
+
};
|
|
1057
|
+
const requestPlaceholderResend = async (messageKey) => {
|
|
1058
|
+
var _a;
|
|
1059
|
+
if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
1060
|
+
throw new boom_1.Boom('Not authenticated');
|
|
1061
|
+
}
|
|
1062
|
+
if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
|
|
1063
|
+
logger.debug({ messageKey }, 'already requested resend');
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
else {
|
|
1067
|
+
placeholderResendCache.set(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id, true);
|
|
1068
|
+
}
|
|
1069
|
+
await (0, Utils_1.delay)(5000);
|
|
1070
|
+
if (!placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
|
|
1071
|
+
logger.debug({ messageKey }, 'message received while resend requested');
|
|
1072
|
+
return 'RESOLVED';
|
|
1073
|
+
}
|
|
1074
|
+
const pdoMessage = {
|
|
1075
|
+
placeholderMessageResendRequest: [{
|
|
1076
|
+
messageKey
|
|
1077
|
+
}],
|
|
1078
|
+
peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.PLACEHOLDER_MESSAGE_RESEND
|
|
1079
|
+
};
|
|
1080
|
+
setTimeout(() => {
|
|
1081
|
+
if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
|
|
1082
|
+
logger.debug({ messageKey }, 'PDO message without response after 15 seconds. Phone possibly offline');
|
|
1083
|
+
placeholderResendCache.del(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id);
|
|
1084
|
+
}
|
|
1085
|
+
}, 15000);
|
|
1086
|
+
return sendPeerDataOperationMessage(pdoMessage);
|
|
1087
|
+
};
|
|
1088
|
+
const handleCall = async (node) => {
|
|
1089
|
+
const { attrs } = node;
|
|
1090
|
+
const [infoChild] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
|
|
1091
|
+
const callId = infoChild.attrs['call-id'];
|
|
1092
|
+
const status = (0, Utils_1.getCallStatusFromNode)(infoChild);
|
|
1093
|
+
const contextGroupJid = (0, WABinary_1.isJidGroup)(attrs.from) ? attrs.from : undefined;
|
|
1094
|
+
const resolvedCallCreator = await resolveLidFromGroupContext(infoChild.attrs.from || infoChild.attrs['call-creator'], contextGroupJid);
|
|
1095
|
+
const resolvedChatId = await resolveLidFromGroupContext(attrs.from, contextGroupJid);
|
|
1096
|
+
const call = {
|
|
1097
|
+
chatId: resolvedChatId,
|
|
1098
|
+
from: resolvedCallCreator,
|
|
1099
|
+
id: callId,
|
|
1100
|
+
date: new Date(+attrs.t * 1000),
|
|
1101
|
+
offline: !!attrs.offline,
|
|
1102
|
+
status,
|
|
1103
|
+
};
|
|
1104
|
+
if (status === 'offer') {
|
|
1105
|
+
call.isVideo = !!(0, WABinary_1.getBinaryNodeChild)(infoChild, 'video');
|
|
1106
|
+
call.isGroup = infoChild.attrs.type === 'group' || !!infoChild.attrs['group-jid'];
|
|
1107
|
+
if (infoChild.attrs['group-jid']) {
|
|
1108
|
+
call.groupJid = await resolveLidFromGroupContext(infoChild.attrs['group-jid'], infoChild.attrs['group-jid']);
|
|
1109
|
+
}
|
|
1110
|
+
callOfferCache.set(call.id, call);
|
|
1111
|
+
}
|
|
1112
|
+
const existingCall = callOfferCache.get(call.id);
|
|
1113
|
+
if (existingCall) {
|
|
1114
|
+
call.isVideo = existingCall.isVideo;
|
|
1115
|
+
call.isGroup = existingCall.isGroup;
|
|
1116
|
+
}
|
|
1117
|
+
if (status === 'reject' || status === 'accept' || status === 'timeout' || status === 'terminate') {
|
|
1118
|
+
callOfferCache.del(call.id);
|
|
1119
|
+
}
|
|
1120
|
+
ev.emit('call', [call]);
|
|
1121
|
+
await sendMessageAck(node);
|
|
1122
|
+
};
|
|
1123
|
+
const handleBadAck = async ({ attrs }) => {
|
|
1124
|
+
const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id, 'server_id': attrs === null || attrs === void 0 ? void 0 : attrs.server_id };
|
|
1125
|
+
// current hypothesis is that if pash is sent in the ack
|
|
1126
|
+
// it means -- the message hasn't reached all devices yet
|
|
1127
|
+
// we'll retry sending the message here
|
|
1128
|
+
if (attrs.phash && attrs.class === 'message') {
|
|
1129
|
+
logger.info({ attrs }, 'received phash in ack, resending message...');
|
|
1130
|
+
const cacheKey = `${key.remoteJid}:${key.id}`;
|
|
1131
|
+
const retryCount = msgRetryCache.get(cacheKey) || 0;
|
|
1132
|
+
if (retryCount >= maxMsgRetryCount) {
|
|
1133
|
+
logger.warn({ attrs }, 'reached max retry count, not sending message again');
|
|
1134
|
+
msgRetryCache.del(cacheKey);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
const msg = await getMessage(key);
|
|
1138
|
+
if (msg) {
|
|
1139
|
+
await relayMessage(key.remoteJid, msg, { messageId: key.id, useUserDevicesCache: false });
|
|
1140
|
+
msgRetryCache.set(cacheKey, retryCount + 1);
|
|
1141
|
+
}
|
|
1142
|
+
else {
|
|
1143
|
+
logger.warn({ attrs }, 'could not send message again, as it was not found');
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
if (attrs.error) {
|
|
1147
|
+
logger.warn({ attrs }, 'received error in ack');
|
|
1148
|
+
ev.emit('messages.update', [
|
|
1149
|
+
{
|
|
1150
|
+
key,
|
|
1151
|
+
update: {
|
|
1152
|
+
status: Types_1.WAMessageStatus.ERROR,
|
|
1153
|
+
messageStubParameters: [
|
|
1154
|
+
attrs.error
|
|
1155
|
+
]
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
]);
|
|
1159
|
+
}
|
|
1160
|
+
};
|
|
1161
|
+
const processNodeWithBuffer = async (node, identifier, exec) => {
|
|
1162
|
+
ev.buffer();
|
|
1163
|
+
await execTask();
|
|
1164
|
+
ev.flush();
|
|
1165
|
+
function execTask() {
|
|
1166
|
+
return exec(node, false)
|
|
1167
|
+
.catch(err => onUnexpectedError(err, identifier));
|
|
1168
|
+
}
|
|
1169
|
+
};
|
|
1170
|
+
const makeOfflineNodeProcessor = () => {
|
|
1171
|
+
const nodeProcessorMap = new Map([
|
|
1172
|
+
['message', handleMessage],
|
|
1173
|
+
['call', handleCall],
|
|
1174
|
+
['receipt', handleReceipt],
|
|
1175
|
+
['notification', handleNotification]
|
|
1176
|
+
]);
|
|
1177
|
+
const nodes = [];
|
|
1178
|
+
let isProcessing = false;
|
|
1179
|
+
const enqueue = (type, node) => {
|
|
1180
|
+
nodes.push({ type, node });
|
|
1181
|
+
if (isProcessing) {
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
isProcessing = true;
|
|
1185
|
+
const promise = async () => {
|
|
1186
|
+
while (nodes.length && ws.isOpen) {
|
|
1187
|
+
const { type, node } = nodes.shift();
|
|
1188
|
+
const nodeProcessor = nodeProcessorMap.get(type);
|
|
1189
|
+
if (!nodeProcessor) {
|
|
1190
|
+
onUnexpectedError(new Error(`unknown offline node type: ${type}`), 'processing offline node');
|
|
1191
|
+
continue;
|
|
1192
|
+
}
|
|
1193
|
+
await nodeProcessor(node);
|
|
1194
|
+
}
|
|
1195
|
+
isProcessing = false;
|
|
1196
|
+
};
|
|
1197
|
+
promise().catch(error => onUnexpectedError(error, 'processing offline nodes'));
|
|
1198
|
+
};
|
|
1199
|
+
return { enqueue };
|
|
1200
|
+
};
|
|
1201
|
+
const offlineNodeProcessor = makeOfflineNodeProcessor();
|
|
1202
|
+
const processNode = (type, node, identifier, exec) => {
|
|
1203
|
+
const isOffline = !!node.attrs.offline;
|
|
1204
|
+
if (isOffline) {
|
|
1205
|
+
offlineNodeProcessor.enqueue(type, node);
|
|
1206
|
+
}
|
|
1207
|
+
else {
|
|
1208
|
+
processNodeWithBuffer(node, identifier, exec);
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
ws.on('CB:message', (node) => {
|
|
1212
|
+
processNode('message', node, 'processing message', handleMessage);
|
|
1213
|
+
});
|
|
1214
|
+
ws.on('CB:call', async (node) => {
|
|
1215
|
+
processNode('call', node, 'handling call', handleCall);
|
|
1216
|
+
});
|
|
1217
|
+
ws.on('CB:receipt', node => {
|
|
1218
|
+
processNode('receipt', node, 'handling receipt', handleReceipt);
|
|
1219
|
+
});
|
|
1220
|
+
ws.on('CB:notification', async (node) => {
|
|
1221
|
+
processNode('notification', node, 'handling notification', handleNotification);
|
|
1222
|
+
});
|
|
1223
|
+
ws.on('CB:ack,class:message', (node) => {
|
|
1224
|
+
handleBadAck(node)
|
|
1225
|
+
.catch(error => onUnexpectedError(error, 'handling bad ack'));
|
|
1226
|
+
});
|
|
1227
|
+
ev.on('call', ([call]) => {
|
|
1228
|
+
if (call.status === 'timeout' || (call.status === 'offer' && call.isGroup)) {
|
|
1229
|
+
const msg = {
|
|
1230
|
+
key: {
|
|
1231
|
+
remoteJid: call.chatId,
|
|
1232
|
+
id: call.id,
|
|
1233
|
+
fromMe: false
|
|
1234
|
+
},
|
|
1235
|
+
messageTimestamp: (0, Utils_1.unixTimestampSeconds)(call.date),
|
|
1236
|
+
};
|
|
1237
|
+
if (call.status === 'timeout') {
|
|
1238
|
+
if (call.isGroup) {
|
|
1239
|
+
msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE;
|
|
1240
|
+
}
|
|
1241
|
+
else {
|
|
1242
|
+
msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_VOICE;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
else {
|
|
1246
|
+
msg.message = { call: { callKey: Buffer.from(call.id) } };
|
|
1247
|
+
}
|
|
1248
|
+
const protoMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
|
|
1249
|
+
upsertMessage(protoMsg, call.offline ? 'append' : 'notify');
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
1252
|
+
ev.on('connection.update', (update) => {
|
|
1253
|
+
const { connection, lastDisconnect } = update;
|
|
1254
|
+
if (connection === 'close') {
|
|
1255
|
+
const statusCode = lastDisconnect?.error?.output?.statusCode;
|
|
1256
|
+
const shouldReconnect = statusCode !== Types_1.DisconnectReason.loggedOut;
|
|
1257
|
+
if (shouldReconnect) {
|
|
1258
|
+
logger.info('Connection closed, will handle reconnection automatically');
|
|
1259
|
+
} else {
|
|
1260
|
+
logger.warn('Logged out, manual re-authentication required');
|
|
1261
|
+
}
|
|
1262
|
+
} else if (connection === 'open') {
|
|
1263
|
+
sendActiveReceipts = true;
|
|
1264
|
+
}
|
|
1265
|
+
if (typeof update.isOnline !== 'undefined' && update.isOnline) {
|
|
1266
|
+
sendActiveReceipts = true;
|
|
1267
|
+
logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`);
|
|
1268
|
+
}
|
|
1269
|
+
});
|
|
1270
|
+
ev.on('messages.update', (updates) => {
|
|
1271
|
+
const config = (0, performance_config_1.getPerformanceConfig)();
|
|
1272
|
+
updates.forEach(update => {
|
|
1273
|
+
if (update.update.status === WAProto_1.proto.WebMessageInfo.Status.PENDING &&
|
|
1274
|
+
Date.now() - (update.update.timestamp || 0) > 30000) {
|
|
1275
|
+
logger.debug({ key: update.key }, 'retrying stuck pending message with anti-ban delay');
|
|
1276
|
+
setTimeout(async () => {
|
|
1277
|
+
try {
|
|
1278
|
+
const msg = await getMessage(update.key);
|
|
1279
|
+
if (msg) {
|
|
1280
|
+
await relayMessage(update.key.remoteJid, msg, { messageId: update.key.id });
|
|
1281
|
+
}
|
|
1282
|
+
} catch (err) {
|
|
1283
|
+
logger.error({ err, key: update.key }, 'failed to retry stuck message');
|
|
1284
|
+
}
|
|
1285
|
+
}, config.security?.messageDelay?.min || 1000);
|
|
1286
|
+
}
|
|
1287
|
+
});
|
|
1288
|
+
});
|
|
1289
|
+
return {
|
|
1290
|
+
...sock,
|
|
1291
|
+
sendMessageAck,
|
|
1292
|
+
sendRetryRequest,
|
|
1293
|
+
rejectCall,
|
|
1294
|
+
offerCall,
|
|
1295
|
+
fetchMessageHistory,
|
|
1296
|
+
requestPlaceholderResend,
|
|
1297
|
+
};
|
|
1298
|
+
};
|
|
1238
1299
|
exports.makeMessagesRecvSocket = makeMessagesRecvSocket;
|