@hansaka02/baileys 7.3.4 → 7.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +203 -247
  2. package/lib/Defaults/baileys-version.json +2 -2
  3. package/lib/Defaults/connection.js +1 -1
  4. package/lib/Defaults/constants.js +13 -1
  5. package/lib/Defaults/history.js +3 -1
  6. package/lib/Signal/Group/sender-chain-key.js +1 -14
  7. package/lib/Signal/Group/sender-key-distribution-message.js +2 -2
  8. package/lib/Signal/Group/sender-key-record.js +2 -11
  9. package/lib/Signal/Group/sender-key-state.js +11 -57
  10. package/lib/Signal/libsignal.js +200 -116
  11. package/lib/Signal/lid-mapping.js +121 -68
  12. package/lib/Socket/Client/websocket.js +9 -2
  13. package/lib/Socket/business.js +5 -1
  14. package/lib/Socket/chats.js +180 -89
  15. package/lib/Socket/community.js +169 -41
  16. package/lib/Socket/groups.js +25 -21
  17. package/lib/Socket/messages-recv.js +458 -333
  18. package/lib/Socket/messages-send.js +517 -572
  19. package/lib/Socket/mex.js +61 -0
  20. package/lib/Socket/newsletter.js +159 -252
  21. package/lib/Socket/socket.js +283 -100
  22. package/lib/Types/Newsletter.js +32 -25
  23. package/lib/Utils/auth-utils.js +189 -354
  24. package/lib/Utils/browser-utils.js +43 -0
  25. package/lib/Utils/chat-utils.js +166 -41
  26. package/lib/Utils/decode-wa-message.js +77 -35
  27. package/lib/Utils/event-buffer.js +80 -24
  28. package/lib/Utils/generics.js +28 -128
  29. package/lib/Utils/history.js +10 -8
  30. package/lib/Utils/index.js +1 -1
  31. package/lib/Utils/link-preview.js +17 -32
  32. package/lib/Utils/lt-hash.js +28 -22
  33. package/lib/Utils/make-mutex.js +26 -28
  34. package/lib/Utils/message-retry-manager.js +51 -3
  35. package/lib/Utils/messages-media.js +343 -151
  36. package/lib/Utils/messages.js +806 -792
  37. package/lib/Utils/noise-handler.js +33 -2
  38. package/lib/Utils/pre-key-manager.js +126 -0
  39. package/lib/Utils/process-message.js +115 -55
  40. package/lib/Utils/signal.js +45 -18
  41. package/lib/Utils/validate-connection.js +52 -29
  42. package/lib/WABinary/constants.js +1268 -1268
  43. package/lib/WABinary/decode.js +58 -4
  44. package/lib/WABinary/encode.js +54 -7
  45. package/lib/WABinary/jid-utils.js +58 -11
  46. package/lib/WAM/constants.js +19064 -11563
  47. package/lib/WAM/encode.js +57 -8
  48. package/lib/WAUSync/USyncQuery.js +35 -19
  49. package/package.json +9 -8
  50. package/lib/Socket/usync.js +0 -83
@@ -175,7 +175,7 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
175
175
  // if it's a syncdmutation, get the operation property
176
176
  // otherwise, if it's only a record -- it'll be a SET mutation
177
177
  const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET
178
- const record = ('record' in msgMutation && !!msgMutation.record) ? msgMutation.record : msgMutation
178
+ const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation
179
179
  const key = await getKey(record.keyId.id)
180
180
  const content = Buffer.from(record.value.blob)
181
181
  const encContent = content.slice(0, -32)
@@ -320,39 +320,52 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
320
320
  ...initial,
321
321
  indexValueMap: { ...initial.indexValueMap }
322
322
  }
323
+
323
324
  const mutationMap = {}
325
+
324
326
  for (const syncd of syncds) {
325
327
  const { version, keyId, snapshotMac } = syncd
328
+
326
329
  if (syncd.externalMutations) {
327
330
  logger?.trace({ name, version }, 'downloading external patch')
331
+
328
332
  const ref = await downloadExternalPatch(syncd.externalMutations, options)
333
+
329
334
  logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch')
330
- syncd.mutations?.push(...ref.mutations)
335
+ syncd.mutations?.push(...ref.mutations);
331
336
  }
337
+
332
338
  const patchVersion = toNumber(version.version)
339
+
333
340
  newState.version = patchVersion
341
+
334
342
  const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber
335
343
  const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
336
344
  ? mutation => {
337
- var _a
338
345
  const index = mutation.syncAction.index?.toString()
339
346
  mutationMap[index] = mutation
340
347
  }
341
- : (() => { }), true)
348
+ : () => { }, true)
349
+
342
350
  newState.hash = decodeResult.hash
343
351
  newState.indexValueMap = decodeResult.indexValueMap
352
+
344
353
  if (validateMacs) {
345
354
  const base64Key = Buffer.from(keyId.id).toString('base64')
346
355
  const keyEnc = await getAppStateSyncKey(base64Key)
356
+
347
357
  if (!keyEnc) {
348
358
  throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
349
359
  }
360
+
350
361
  const result = await mutationKeys(keyEnc.keyData)
351
362
  const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
363
+
352
364
  if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
353
365
  throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
354
366
  }
355
367
  }
368
+
356
369
  // clear memory used up by the mutations
357
370
  syncd.mutations = []
358
371
  }
@@ -363,33 +376,44 @@ const chatModificationToAppPatch = (mod, jid) => {
363
376
  const OP = proto.SyncdMutation.SyncdOperation
364
377
  const getMessageRange = (lastMessages) => {
365
378
  let messageRange
379
+
366
380
  if (Array.isArray(lastMessages)) {
367
381
  const lastMsg = lastMessages[lastMessages.length - 1]
368
382
  messageRange = {
369
383
  lastMessageTimestamp: lastMsg?.messageTimestamp,
370
- messages: lastMessages?.length ? lastMessages.map(m => {
371
- if (!((m.key?.id) || (m.key?.remoteJid))) {
372
- throw new Boom('Incomplete key', { statusCode: 400, data: m })
373
- }
374
- if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
375
- throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
376
- }
377
- if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
378
- throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
379
- }
380
- if (m.key.participant) {
381
- m.key.participant = jidNormalizedUser(m.key.participant)
382
- }
383
- return m
384
- }) : undefined
384
+ messages: lastMessages?.length
385
+ ? lastMessages.map(m => {
386
+ if (!m.key?.id || !m.key?.remoteJid) {
387
+ throw new Boom('Incomplete key', { statusCode: 400, data: m })
388
+ }
389
+
390
+ if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
391
+ throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
392
+ }
393
+
394
+ if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
395
+ throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
396
+ }
397
+
398
+ if (m.key.participant) {
399
+ m.key.participant = jidNormalizedUser(m.key.participant)
400
+ }
401
+
402
+ return m
403
+ })
404
+ : undefined
385
405
  }
386
406
  }
407
+
387
408
  else {
388
409
  messageRange = lastMessages
389
410
  }
411
+
390
412
  return messageRange
391
413
  }
414
+
392
415
  let patch
416
+
393
417
  if ('mute' in mod) {
394
418
  patch = {
395
419
  syncAction: {
@@ -404,6 +428,7 @@ const chatModificationToAppPatch = (mod, jid) => {
404
428
  operation: OP.SET
405
429
  }
406
430
  }
431
+
407
432
  else if ('archive' in mod) {
408
433
  patch = {
409
434
  syncAction: {
@@ -418,6 +443,7 @@ const chatModificationToAppPatch = (mod, jid) => {
418
443
  operation: OP.SET
419
444
  }
420
445
  }
446
+
421
447
  else if ('markRead' in mod) {
422
448
  patch = {
423
449
  syncAction: {
@@ -432,6 +458,7 @@ const chatModificationToAppPatch = (mod, jid) => {
432
458
  operation: OP.SET
433
459
  }
434
460
  }
461
+
435
462
  else if ('deleteForMe' in mod) {
436
463
  const { timestamp, key, deleteMedia } = mod.deleteForMe
437
464
  patch = {
@@ -447,6 +474,7 @@ const chatModificationToAppPatch = (mod, jid) => {
447
474
  operation: OP.SET
448
475
  }
449
476
  }
477
+
450
478
  else if ('clear' in mod) {
451
479
  patch = {
452
480
  syncAction: {
@@ -460,6 +488,7 @@ const chatModificationToAppPatch = (mod, jid) => {
460
488
  operation: OP.SET
461
489
  }
462
490
  }
491
+
463
492
  else if ('pin' in mod) {
464
493
  patch = {
465
494
  syncAction: {
@@ -473,17 +502,19 @@ const chatModificationToAppPatch = (mod, jid) => {
473
502
  operation: OP.SET
474
503
  }
475
504
  }
505
+
476
506
  else if ('contact' in mod) {
477
- patch = {
478
- syncAction: {
479
- contactAction: mod?.contact || {}
480
- },
481
- index: ['contact', jid],
482
- type: 'critical_unblock_low',
507
+ patch = {
508
+ syncAction: {
509
+ contactAction: mod.contact || {}
510
+ },
511
+ index: ['contact', jid],
512
+ type: 'critical_unblock_low',
483
513
  apiVersion: 2,
484
514
  operation: mod.contact ? OP.SET : OP.REMOVE
485
515
  }
486
516
  }
517
+
487
518
  else if ('disableLinkPreviews' in mod) {
488
519
  patch = {
489
520
  syncAction: {
@@ -495,6 +526,7 @@ const chatModificationToAppPatch = (mod, jid) => {
495
526
  operation: OP.SET
496
527
  }
497
528
  }
529
+
498
530
  else if ('star' in mod) {
499
531
  const key = mod.star.messages[0]
500
532
  patch = {
@@ -509,6 +541,7 @@ const chatModificationToAppPatch = (mod, jid) => {
509
541
  operation: OP.SET
510
542
  }
511
543
  }
544
+
512
545
  else if ('delete' in mod) {
513
546
  patch = {
514
547
  syncAction: {
@@ -522,6 +555,7 @@ const chatModificationToAppPatch = (mod, jid) => {
522
555
  operation: OP.SET
523
556
  }
524
557
  }
558
+
525
559
  else if ('pushNameSetting' in mod) {
526
560
  patch = {
527
561
  syncAction: {
@@ -535,6 +569,7 @@ const chatModificationToAppPatch = (mod, jid) => {
535
569
  operation: OP.SET
536
570
  }
537
571
  }
572
+
538
573
  else if ('quickReply' in mod) {
539
574
  patch = {
540
575
  syncAction: {
@@ -552,6 +587,7 @@ const chatModificationToAppPatch = (mod, jid) => {
552
587
  operation: OP.SET
553
588
  }
554
589
  }
590
+
555
591
  else if ('addLabel' in mod) {
556
592
  patch = {
557
593
  syncAction: {
@@ -568,6 +604,7 @@ const chatModificationToAppPatch = (mod, jid) => {
568
604
  operation: OP.SET
569
605
  }
570
606
  }
607
+
571
608
  else if ('addChatLabel' in mod) {
572
609
  patch = {
573
610
  syncAction: {
@@ -581,11 +618,12 @@ const chatModificationToAppPatch = (mod, jid) => {
581
618
  operation: OP.SET
582
619
  }
583
620
  }
621
+
584
622
  else if ('removeChatLabel' in mod) {
585
623
  patch = {
586
624
  syncAction: {
587
625
  labelAssociationAction: {
588
- labeled: false,
626
+ labeled: false
589
627
  }
590
628
  },
591
629
  index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
@@ -594,6 +632,7 @@ const chatModificationToAppPatch = (mod, jid) => {
594
632
  operation: OP.SET
595
633
  }
596
634
  }
635
+
597
636
  else if ('addMessageLabel' in mod) {
598
637
  patch = {
599
638
  syncAction: {
@@ -614,6 +653,7 @@ const chatModificationToAppPatch = (mod, jid) => {
614
653
  operation: OP.SET
615
654
  }
616
655
  }
656
+
617
657
  else if ('removeMessageLabel' in mod) {
618
658
  patch = {
619
659
  syncAction: {
@@ -634,18 +674,24 @@ const chatModificationToAppPatch = (mod, jid) => {
634
674
  operation: OP.SET
635
675
  }
636
676
  }
677
+
637
678
  else {
638
679
  throw new Boom('not supported')
639
680
  }
681
+
640
682
  patch.syncAction.timestamp = Date.now()
683
+
641
684
  return patch
642
685
  }
643
686
 
644
687
  const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
645
688
  const isInitialSync = !!initialSyncOpts
646
689
  const accountSettings = initialSyncOpts?.accountSettings
690
+
647
691
  logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action')
692
+
648
693
  const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction
694
+
649
695
  if (action?.muteAction) {
650
696
  ev.emit('chats.update', [
651
697
  {
@@ -657,6 +703,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
657
703
  }
658
704
  ])
659
705
  }
706
+
660
707
  else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
661
708
  // okay so we've to do some annoying computation here
662
709
  // when we're initially syncing the app state
@@ -664,27 +711,28 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
664
711
  // 1. if the account unarchiveChats setting is true
665
712
  // a. if the chat is archived, and no further messages have been received -- simple, keep archived
666
713
  // b. if the chat was archived, and the user received messages from the other person afterwards
667
- // then the chat should be marked unarchved --
668
- // we compare the timestamp of latest message from the other person to determine this
714
+ // then the chat should be marked unarchved --
715
+ // we compare the timestamp of latest message from the other person to determine this
669
716
  // 2. if the account unarchiveChats setting is false -- then it doesn't matter,
670
- // it'll always take an app state action to mark in unarchived -- which we'll get anyway
717
+ // it'll always take an app state action to mark in unarchived -- which we'll get anyway
671
718
  const archiveAction = action?.archiveChatAction
672
- const isArchived = archiveAction
673
- ? archiveAction.archived
674
- : type === 'archive'
719
+ const isArchived = archiveAction ? archiveAction.archived : type === 'archive'
675
720
  // // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
676
721
  // // this only applies for the initial sync
677
722
  // if(isInitialSync && !isArchived) {
678
- // isArchived = false
723
+ // isArchived = false
679
724
  // }
680
725
  const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange
681
726
  // logger?.debug({ chat: id, syncAction }, 'message range archive')
682
- ev.emit('chats.update', [{
727
+ ev.emit('chats.update', [
728
+ {
683
729
  id,
684
730
  archived: isArchived,
685
731
  conditional: getChatUpdateConditional(id, msgRange)
686
- }])
732
+ }
733
+ ])
687
734
  }
735
+
688
736
  else if (action?.markChatAsReadAction) {
689
737
  const markReadAction = action.markChatAsReadAction
690
738
  // basically we don't need to fire an "read" update if the chat is being marked as read
@@ -697,6 +745,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
697
745
  conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
698
746
  }])
699
747
  }
748
+
700
749
  else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
701
750
  ev.emit('messages.delete', {
702
751
  keys: [
@@ -708,20 +757,25 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
708
757
  ]
709
758
  })
710
759
  }
760
+
711
761
  else if (action?.contactAction) {
712
- ev.emit('contacts.upsert', [{
713
- id,
714
- name: action.contactAction.fullName,
715
- lid: action.contactAction.lidJid || undefined,
716
- phoneNumber: action.contactAction.pnJid || undefined
717
- }])
762
+ ev.emit('contacts.upsert', [
763
+ {
764
+ id: id,
765
+ name: action.contactAction.fullName,
766
+ lid: action.contactAction.lidJid || undefined,
767
+ phoneNumber: action.contactAction.pnJid || undefined
768
+ }
769
+ ])
718
770
  }
771
+
719
772
  else if (action?.pushNameSetting) {
720
773
  const name = action?.pushNameSetting?.name
721
774
  if (name && me?.name !== name) {
722
775
  ev.emit('creds.update', { me: { ...me, name } })
723
776
  }
724
777
  }
778
+
725
779
  else if (action?.pinAction) {
726
780
  ev.emit('chats.update', [{
727
781
  id,
@@ -729,14 +783,18 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
729
783
  conditional: getChatUpdateConditional(id, undefined)
730
784
  }])
731
785
  }
786
+
732
787
  else if (action?.unarchiveChatsSetting) {
733
788
  const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats
789
+
734
790
  ev.emit('creds.update', { accountSettings: { unarchiveChats } })
735
791
  logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`)
792
+
736
793
  if (accountSettings) {
737
794
  accountSettings.unarchiveChats = unarchiveChats
738
795
  }
739
796
  }
797
+
740
798
  else if (action?.starAction || type === 'star') {
741
799
  let starred = action?.starAction?.starred
742
800
  if (typeof starred !== 'boolean') {
@@ -749,11 +807,13 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
749
807
  }
750
808
  ])
751
809
  }
810
+
752
811
  else if (action?.deleteChatAction || type === 'deleteChat') {
753
812
  if (!isInitialSync) {
754
813
  ev.emit('chats.delete', [id])
755
814
  }
756
815
  }
816
+
757
817
  else if (action?.labelEditAction) {
758
818
  const { name, color, deleted, predefinedId } = action.labelEditAction
759
819
  ev.emit('labels.edit', {
@@ -764,6 +824,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
764
824
  predefinedId: predefinedId ? String(predefinedId) : undefined
765
825
  })
766
826
  }
827
+
767
828
  else if (action?.labelAssociationAction) {
768
829
  ev.emit('labels.association', {
769
830
  type: action.labelAssociationAction.labeled
@@ -783,9 +844,72 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
783
844
  }
784
845
  })
785
846
  }
847
+
848
+ else if (action?.localeSetting?.locale) {
849
+ ev.emit('settings.update', { setting: 'locale', value: action.localeSetting.locale })
850
+ }
851
+
852
+ else if (action?.timeFormatAction) {
853
+ ev.emit('settings.update', { setting: 'timeFormat', value: action.timeFormatAction })
854
+ }
855
+
856
+ else if (action?.pnForLidChatAction) {
857
+ if (action.pnForLidChatAction.pnJid) {
858
+ ev.emit('lid-mapping.update', { lid: id, pn: action.pnForLidChatAction.pnJid })
859
+ }
860
+ }
861
+
862
+ else if (action?.privacySettingRelayAllCalls) {
863
+ ev.emit('settings.update', {
864
+ setting: 'privacySettingRelayAllCalls',
865
+ value: action.privacySettingRelayAllCalls
866
+ })
867
+ }
868
+
869
+ else if (action?.statusPrivacy) {
870
+ ev.emit('settings.update', { setting: 'statusPrivacy', value: action.statusPrivacy })
871
+ }
872
+
873
+ else if (action?.lockChatAction) {
874
+ ev.emit('chats.lock', { id: id, locked: !!action.lockChatAction.locked })
875
+ }
876
+
877
+ else if (action?.privacySettingDisableLinkPreviewsAction) {
878
+ ev.emit('settings.update', {
879
+ setting: 'disableLinkPreviews',
880
+ value: action.privacySettingDisableLinkPreviewsAction
881
+ })
882
+ }
883
+
884
+ else if (action?.notificationActivitySettingAction?.notificationActivitySetting) {
885
+ ev.emit('settings.update', {
886
+ setting: 'notificationActivitySetting',
887
+ value: action.notificationActivitySettingAction.notificationActivitySetting
888
+ })
889
+ }
890
+
891
+ else if (action?.lidContactAction) {
892
+ ev.emit('contacts.upsert', [
893
+ {
894
+ id: id,
895
+ name: action.lidContactAction.fullName,
896
+ lid: id,
897
+ phoneNumber: undefined
898
+ }
899
+ ])
900
+ }
901
+
902
+ else if (action?.privacySettingChannelsPersonalisedRecommendationAction) {
903
+ ev.emit('settings.update', {
904
+ setting: 'channelsPersonalisedRecommendation',
905
+ value: action.privacySettingChannelsPersonalisedRecommendationAction
906
+ })
907
+ }
908
+
786
909
  else {
787
910
  logger?.debug({ syncAction, id }, 'unprocessable update')
788
911
  }
912
+
789
913
  function getChatUpdateConditional(id, msgRange) {
790
914
  return isInitialSync
791
915
  ? (data) => {
@@ -796,6 +920,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
796
920
  }
797
921
  : undefined
798
922
  }
923
+
799
924
  function isValidPatchBasedOnMessageRange(chat, msgRange) {
800
925
  const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0)
801
926
  const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0)