@codexverified/baileys 2.7.5

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 (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +233 -0
  3. package/WAProto/V +1 -0
  4. package/WAProto/WAProto.proto +8051 -0
  5. package/WAProto/index.js +289292 -0
  6. package/engine-requirements.js +13 -0
  7. package/lib/Defaults/index.js +158 -0
  8. package/lib/Signal/Group/ciphertext-message.js +11 -0
  9. package/lib/Signal/Group/group-session-builder.js +29 -0
  10. package/lib/Signal/Group/group_cipher.js +81 -0
  11. package/lib/Signal/Group/index.js +11 -0
  12. package/lib/Signal/Group/keyhelper.js +17 -0
  13. package/lib/Signal/Group/sender-chain-key.js +25 -0
  14. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  15. package/lib/Signal/Group/sender-key-message.js +65 -0
  16. package/lib/Signal/Group/sender-key-name.js +47 -0
  17. package/lib/Signal/Group/sender-key-record.js +40 -0
  18. package/lib/Signal/Group/sender-key-state.js +83 -0
  19. package/lib/Signal/Group/sender-message-key.js +25 -0
  20. package/lib/Signal/libsignal.js +428 -0
  21. package/lib/Signal/lid-mapping.js +280 -0
  22. package/lib/Socket/Client/index.js +2 -0
  23. package/lib/Socket/Client/types.js +10 -0
  24. package/lib/Socket/Client/websocket.js +53 -0
  25. package/lib/Socket/aigroups.js +315 -0
  26. package/lib/Socket/business.js +407 -0
  27. package/lib/Socket/chats.js +1831 -0
  28. package/lib/Socket/communities.js +431 -0
  29. package/lib/Socket/graphql.js +899 -0
  30. package/lib/Socket/groups.js +549 -0
  31. package/lib/Socket/index.js +79 -0
  32. package/lib/Socket/interactive-handler.js +600 -0
  33. package/lib/Socket/interop.js +563 -0
  34. package/lib/Socket/managed-account.js +208 -0
  35. package/lib/Socket/messages-recv.js +2144 -0
  36. package/lib/Socket/messages-send.js +2030 -0
  37. package/lib/Socket/mex.js +41 -0
  38. package/lib/Socket/newsletter.js +226 -0
  39. package/lib/Socket/privacy.js +446 -0
  40. package/lib/Socket/registration.js +428 -0
  41. package/lib/Socket/socket.js +1017 -0
  42. package/lib/Socket/username.js +229 -0
  43. package/lib/Store/index.js +4 -0
  44. package/lib/Store/make-cache-manager-store.js +89 -0
  45. package/lib/Store/make-in-memory-store.js +420 -0
  46. package/lib/Store/make-ordered-dictionary.js +78 -0
  47. package/lib/Store/object-repository.js +23 -0
  48. package/lib/Types/Auth.js +1 -0
  49. package/lib/Types/Bussines.js +1 -0
  50. package/lib/Types/Call.js +1 -0
  51. package/lib/Types/Chat.js +7 -0
  52. package/lib/Types/Contact.js +1 -0
  53. package/lib/Types/Events.js +1 -0
  54. package/lib/Types/GroupMetadata.js +1 -0
  55. package/lib/Types/Label.js +24 -0
  56. package/lib/Types/LabelAssociation.js +6 -0
  57. package/lib/Types/Message.js +17 -0
  58. package/lib/Types/Mex.js +39 -0
  59. package/lib/Types/Product.js +1 -0
  60. package/lib/Types/RichType.js +22 -0
  61. package/lib/Types/Signal.js +1 -0
  62. package/lib/Types/Socket.js +2 -0
  63. package/lib/Types/State.js +48 -0
  64. package/lib/Types/USync.js +1 -0
  65. package/lib/Types/index.js +25 -0
  66. package/lib/Utils/auth-utils.js +301 -0
  67. package/lib/Utils/bot-planning-replay.js +169 -0
  68. package/lib/Utils/browser-utils.js +28 -0
  69. package/lib/Utils/business.js +230 -0
  70. package/lib/Utils/chat-utils.js +885 -0
  71. package/lib/Utils/companion-reg-client-utils.js +32 -0
  72. package/lib/Utils/consumer-application.js +102 -0
  73. package/lib/Utils/crypto.js +117 -0
  74. package/lib/Utils/decode-wa-message.js +484 -0
  75. package/lib/Utils/event-buffer.js +589 -0
  76. package/lib/Utils/generics.js +388 -0
  77. package/lib/Utils/group-history.js +56 -0
  78. package/lib/Utils/history.js +130 -0
  79. package/lib/Utils/identity-change-handler.js +48 -0
  80. package/lib/Utils/index.js +34 -0
  81. package/lib/Utils/jid-display-normalization.js +209 -0
  82. package/lib/Utils/link-preview.js +84 -0
  83. package/lib/Utils/logger.js +2 -0
  84. package/lib/Utils/lt-hash.js +7 -0
  85. package/lib/Utils/make-mutex.js +32 -0
  86. package/lib/Utils/message-composer.js +467 -0
  87. package/lib/Utils/message-inspect.js +67 -0
  88. package/lib/Utils/message-retry-manager.js +241 -0
  89. package/lib/Utils/messages-media.js +1006 -0
  90. package/lib/Utils/messages.js +1851 -0
  91. package/lib/Utils/meta-ai-msmsg.js +130 -0
  92. package/lib/Utils/meta-compositing.js +194 -0
  93. package/lib/Utils/noise-handler.js +200 -0
  94. package/lib/Utils/offline-node-processor.js +39 -0
  95. package/lib/Utils/pre-key-manager.js +105 -0
  96. package/lib/Utils/process-message.js +527 -0
  97. package/lib/Utils/reporting-utils.js +257 -0
  98. package/lib/Utils/rich-message-utils.js +439 -0
  99. package/lib/Utils/signal.js +200 -0
  100. package/lib/Utils/stanza-ack.js +37 -0
  101. package/lib/Utils/sync-action-utils.js +47 -0
  102. package/lib/Utils/tc-token-utils.js +162 -0
  103. package/lib/Utils/use-multi-file-auth-state.js +120 -0
  104. package/lib/Utils/use-single-file-auth-state.js +96 -0
  105. package/lib/Utils/use-sqlite-auth-state.js +108 -0
  106. package/lib/Utils/validate-connection.js +206 -0
  107. package/lib/Utils/voip-rekey.js +20 -0
  108. package/lib/Utils/welcome-flow.js +217 -0
  109. package/lib/WABinary/constants.js +1463 -0
  110. package/lib/WABinary/decode.js +261 -0
  111. package/lib/WABinary/encode.js +219 -0
  112. package/lib/WABinary/generic-utils.js +227 -0
  113. package/lib/WABinary/index.js +5 -0
  114. package/lib/WABinary/jid-utils.js +95 -0
  115. package/lib/WABinary/types.js +1 -0
  116. package/lib/WAM/BinaryInfo.js +9 -0
  117. package/lib/WAM/constants.js +22852 -0
  118. package/lib/WAM/encode.js +149 -0
  119. package/lib/WAM/index.js +3 -0
  120. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +96 -0
  121. package/lib/WAUSync/Protocols/USyncContactProtocol.js +56 -0
  122. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  123. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  124. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +70 -0
  125. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +28 -0
  126. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +25 -0
  127. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  128. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +34 -0
  129. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  130. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  131. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  132. package/lib/WAUSync/Protocols/index.js +12 -0
  133. package/lib/WAUSync/USyncQuery.js +201 -0
  134. package/lib/WAUSync/USyncUser.js +54 -0
  135. package/lib/WAUSync/index.js +3 -0
  136. package/lib/antiban.cjs +4726 -0
  137. package/lib/index.d.ts +286 -0
  138. package/lib/index.js +18 -0
  139. package/package.json +121 -0
@@ -0,0 +1,209 @@
1
+ import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser, jidDecode } from '../WABinary/index.js';
2
+
3
+ const createLidPnDebug = (logger) => {
4
+ const enabled = process.env.LID_PN_DEBUG === '1' || process.env.LID_PN_DEBUG === 'true';
5
+ return (phase, payload) => {
6
+ if (!enabled) {
7
+ return;
8
+ }
9
+ logger?.debug({ ...payload, phase }, 'lid->pn normalization trace');
10
+ };
11
+ };
12
+
13
+ const fallbackPnFromLidJid = (jid) => {
14
+ const decoded = jidDecode(jid);
15
+ const rawUser = decoded?.user || (typeof jid === 'string' ? jid.split('@')[0] : '');
16
+ const user = rawUser.split(':')[0];
17
+ if (!user) {
18
+ return jid;
19
+ }
20
+ return `${user}@s.whatsapp.net`;
21
+ };
22
+
23
+ const toDisplayPnJid = (jid) => {
24
+ if (!jid || typeof jid !== 'string') {
25
+ return jid;
26
+ }
27
+ const decoded = jidDecode(jid);
28
+ if (!decoded?.user) {
29
+ return jid;
30
+ }
31
+ if (!isPnUser(jid) && !isHostedPnUser(jid)) {
32
+ return jid;
33
+ }
34
+ const user = decoded.user.split(':')[0];
35
+ if (!user) {
36
+ return jid;
37
+ }
38
+ const server = decoded.server === 'hosted' ? 'hosted' : 's.whatsapp.net';
39
+ return `${user}@${server}`;
40
+ };
41
+
42
+ const buildLidPnHints = (key) => {
43
+ const hints = new Map();
44
+ if (!key) {
45
+ return hints;
46
+ }
47
+ const addHint = (lid, pn) => {
48
+ if (!isLidUser(lid) && !isHostedLidUser(lid)) {
49
+ return;
50
+ }
51
+ if (!isPnUser(pn) && !isHostedPnUser(pn)) {
52
+ return;
53
+ }
54
+ hints.set(lid, pn);
55
+ const lidDecoded = jidDecode(lid);
56
+ const pnDecoded = jidDecode(pn);
57
+ if (lidDecoded?.user && pnDecoded?.user) {
58
+ hints.set(`${lidDecoded.user}@lid`, `${pnDecoded.user}@s.whatsapp.net`);
59
+ }
60
+ };
61
+ addHint(key.participant, key.participantAlt);
62
+ addHint(key.remoteJid, key.remoteJidAlt);
63
+ addHint(key.participantAlt, key.participant);
64
+ addHint(key.remoteJidAlt, key.remoteJid);
65
+ return hints;
66
+ };
67
+
68
+ const mergeGroupDataHints = (hints, groupData, debug) => {
69
+ if (!groupData?.participants?.length) {
70
+ return;
71
+ }
72
+ debug?.('groupData-size', { participants: groupData.participants.length });
73
+ for (const participant of groupData.participants) {
74
+ const lidCandidate = participant?.lid;
75
+ const idCandidate = participant?.id;
76
+ const pnCandidate = participant?.phoneNumber;
77
+ const lid =
78
+ ((isLidUser(lidCandidate) || isHostedLidUser(lidCandidate)
79
+ ? lidCandidate
80
+ : undefined)) ||
81
+ ((isLidUser(idCandidate) || isHostedLidUser(idCandidate) ? idCandidate : undefined));
82
+ const pn =
83
+ ((isPnUser(pnCandidate) || isHostedPnUser(pnCandidate)
84
+ ? pnCandidate
85
+ : undefined)) ||
86
+ ((isPnUser(idCandidate) || isHostedPnUser(idCandidate) ? idCandidate : undefined));
87
+ if (!lid || !pn) {
88
+ continue;
89
+ }
90
+ if (
91
+ (isLidUser(lid) || isHostedLidUser(lid)) &&
92
+ (isPnUser(pn) || isHostedPnUser(pn))
93
+ ) {
94
+ const displayPn = toDisplayPnJid(pn);
95
+ hints.set(lid, displayPn);
96
+ const lidDecoded = jidDecode(lid);
97
+ if (lidDecoded?.user) {
98
+ hints.set(`${lidDecoded.user}@lid`, displayPn);
99
+ }
100
+ debug?.('group-hint', { lid, pn: displayPn });
101
+ }
102
+ }
103
+ };
104
+
105
+ const normalizeToPnJid = async (jid, hints, signalRepository, debug) => {
106
+ if (!jid || typeof jid !== 'string') {
107
+ return jid;
108
+ }
109
+ if (!isLidUser(jid) && !isHostedLidUser(jid)) {
110
+ return jid;
111
+ }
112
+ debug?.('input', { lid: jid });
113
+ const hinted = hints.get(jid);
114
+ if (hinted) {
115
+ const normalized = toDisplayPnJid(hinted);
116
+ debug?.('hint-exact', { lid: jid, hinted, normalized });
117
+ return normalized;
118
+ }
119
+ const decoded = jidDecode(jid);
120
+ if (decoded?.user) {
121
+ const userHint = hints.get(`${decoded.user}@lid`);
122
+ if (userHint) {
123
+ const normalized = toDisplayPnJid(userHint);
124
+ debug?.('hint-user', { lid: jid, userHint, normalized });
125
+ return normalized;
126
+ }
127
+ }
128
+ const mapped = await signalRepository?.lidMapping?.getPNForLID?.(jid);
129
+ if (mapped) {
130
+ const normalized = toDisplayPnJid(mapped);
131
+ debug?.('mapping', { lid: jid, mapped, normalized });
132
+ return normalized;
133
+ }
134
+ const fallback = fallbackPnFromLidJid(jid);
135
+ debug?.('fallback', { lid: jid, fallback });
136
+ return fallback;
137
+ };
138
+
139
+ const normalizeMentionedJidsToPn = async (node, hints, signalRepository, debug) => {
140
+ if (!node || typeof node !== 'object') {
141
+ return;
142
+ }
143
+ if (Array.isArray(node)) {
144
+ for (const item of node) {
145
+ await normalizeMentionedJidsToPn(item, hints, signalRepository, debug);
146
+ }
147
+ return;
148
+ }
149
+ if (Array.isArray(node.mentionedJid)) {
150
+ const before = [...node.mentionedJid];
151
+ node.mentionedJid = await Promise.all(
152
+ node.mentionedJid.map(jid => normalizeToPnJid(jid, hints, signalRepository, debug))
153
+ );
154
+ debug?.('mentions', { before, after: node.mentionedJid });
155
+ }
156
+ if (typeof node.participant === 'string') {
157
+ const before = node.participant;
158
+ node.participant = await normalizeToPnJid(node.participant, hints, signalRepository, debug);
159
+ if (before !== node.participant) {
160
+ debug?.('participant-field', { before, after: node.participant });
161
+ }
162
+ }
163
+ if (typeof node.remoteJid === 'string') {
164
+ const before = node.remoteJid;
165
+ node.remoteJid = await normalizeToPnJid(node.remoteJid, hints, signalRepository, debug);
166
+ if (before !== node.remoteJid) {
167
+ debug?.('remoteJid-field', { before, after: node.remoteJid });
168
+ }
169
+ }
170
+ for (const value of Object.values(node)) {
171
+ if (value && typeof value === 'object') {
172
+ await normalizeMentionedJidsToPn(value, hints, signalRepository, debug);
173
+ }
174
+ }
175
+ };
176
+
177
+ export const normalizeMentionedJidsForSend = async (mentions, groupData, signalRepository, logger) => {
178
+ if (!Array.isArray(mentions)) {
179
+ return mentions;
180
+ }
181
+ const debug = createLidPnDebug(logger);
182
+ const hints = new Map();
183
+ mergeGroupDataHints(hints, groupData, debug);
184
+ const normalized = await Promise.all(mentions.map(jid => normalizeToPnJid(jid, hints, signalRepository, debug)));
185
+ debug('send-mentions-result', { before: mentions, after: normalized });
186
+ return normalized;
187
+ };
188
+
189
+ export const normalizeMessageForDisplayJids = async (messageInfo, signalRepository, logger, groupData) => {
190
+ if (!messageInfo?.key) {
191
+ return messageInfo;
192
+ }
193
+ const debug = createLidPnDebug(logger);
194
+ const hints = buildLidPnHints(messageInfo.key);
195
+ mergeGroupDataHints(hints, groupData, debug);
196
+ const beforeKey = { ...messageInfo.key };
197
+ messageInfo.key.participant = await normalizeToPnJid(messageInfo.key.participant, hints, signalRepository, debug);
198
+ messageInfo.key.participantAlt = await normalizeToPnJid(
199
+ messageInfo.key.participantAlt,
200
+ hints,
201
+ signalRepository,
202
+ debug
203
+ );
204
+ messageInfo.key.remoteJid = await normalizeToPnJid(messageInfo.key.remoteJid, hints, signalRepository, debug);
205
+ messageInfo.key.remoteJidAlt = await normalizeToPnJid(messageInfo.key.remoteJidAlt, hints, signalRepository, debug);
206
+ await normalizeMentionedJidsToPn(messageInfo.message, hints, signalRepository, debug);
207
+ debug('display-key-result', { before: beforeKey, after: messageInfo.key });
208
+ return messageInfo;
209
+ };
@@ -0,0 +1,84 @@
1
+ import { prepareWAMessageMedia } from './messages.js';
2
+ import { extractImageThumb, getHttpStream } from './messages-media.js';
3
+ const THUMBNAIL_WIDTH_PX = 192;
4
+ /** Fetches an image and generates a thumbnail for it */
5
+ const getCompressedJpegThumbnail = async (url, { thumbnailWidth, fetchOpts }) => {
6
+ const stream = await getHttpStream(url, fetchOpts);
7
+ const result = await extractImageThumb(stream, thumbnailWidth);
8
+ return result;
9
+ };
10
+ /**
11
+ * Given a piece of text, checks for any URL present, generates link preview for the same and returns it
12
+ * Return undefined if the fetch failed or no URL was found
13
+ * @param text first matched URL in text
14
+ * @returns the URL info required to generate link preview
15
+ */
16
+ export const getUrlInfo = async (text, opts = {
17
+ thumbnailWidth: THUMBNAIL_WIDTH_PX,
18
+ fetchOpts: { timeout: 3000 }
19
+ }) => {
20
+ try {
21
+ // retries
22
+ const retries = 0;
23
+ const maxRetry = 5;
24
+ const { getLinkPreview } = await import('link-preview-js');
25
+ let previewLink = text;
26
+ if (!text.startsWith('https://') && !text.startsWith('http://')) {
27
+ previewLink = 'https://' + previewLink;
28
+ }
29
+ const info = await getLinkPreview(previewLink, {
30
+ ...opts.fetchOpts,
31
+ followRedirects: 'follow',
32
+ handleRedirects: (baseURL, forwardedURL) => {
33
+ const urlObj = new URL(baseURL);
34
+ const forwardedURLObj = new URL(forwardedURL);
35
+ if (retries >= maxRetry) {
36
+ return false;
37
+ }
38
+ if (forwardedURLObj.hostname === urlObj.hostname ||
39
+ forwardedURLObj.hostname === 'www.' + urlObj.hostname ||
40
+ 'www.' + forwardedURLObj.hostname === urlObj.hostname) {
41
+ retries + 1;
42
+ return true;
43
+ }
44
+ else {
45
+ return false;
46
+ }
47
+ },
48
+ headers: opts.fetchOpts?.headers
49
+ });
50
+ if (info && 'title' in info && info.title) {
51
+ const [image] = info.images;
52
+ const urlInfo = {
53
+ 'canonical-url': info.url,
54
+ 'matched-text': text,
55
+ title: info.title,
56
+ description: info.description,
57
+ originalThumbnailUrl: image
58
+ };
59
+ if (opts.uploadImage) {
60
+ const { imageMessage } = await prepareWAMessageMedia({ image: { url: image } }, {
61
+ upload: opts.uploadImage,
62
+ mediaTypeOverride: 'thumbnail-link',
63
+ options: opts.fetchOpts
64
+ });
65
+ urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined;
66
+ urlInfo.highQualityThumbnail = imageMessage || undefined;
67
+ }
68
+ else {
69
+ try {
70
+ urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined;
71
+ }
72
+ catch (error) {
73
+ opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail');
74
+ }
75
+ }
76
+ return urlInfo;
77
+ }
78
+ }
79
+ catch (error) {
80
+ if (!error.message.includes('receive a valid')) {
81
+ throw error;
82
+ }
83
+ }
84
+ };
@@ -0,0 +1,2 @@
1
+ import P from 'pino';
2
+ export default P({ timestamp: () => `,"time":"${new Date().toJSON()}"` });
@@ -0,0 +1,7 @@
1
+ import { LTHashAntiTampering } from 'whatsapp-rust-bridge';
2
+ /**
3
+ * LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
4
+ * over a series of mutations. You can add/remove mutations and it'll return a hash equal to
5
+ * if the same series of mutations was made sequentially.
6
+ */
7
+ export const LT_HASH_ANTI_TAMPERING = new LTHashAntiTampering();
@@ -0,0 +1,32 @@
1
+ import { Mutex as AsyncMutex } from 'async-mutex';
2
+ export const makeMutex = () => {
3
+ const mutex = new AsyncMutex();
4
+ return {
5
+ mutex(code) {
6
+ return mutex.runExclusive(code);
7
+ }
8
+ };
9
+ };
10
+ export const makeKeyedMutex = () => {
11
+ const map = new Map();
12
+ return {
13
+ async mutex(key, task) {
14
+ let entry = map.get(key);
15
+ if (!entry) {
16
+ entry = { mutex: new AsyncMutex(), refCount: 0 };
17
+ map.set(key, entry);
18
+ }
19
+ entry.refCount++;
20
+ try {
21
+ return await entry.mutex.runExclusive(task);
22
+ }
23
+ finally {
24
+ entry.refCount--;
25
+ // only delete it if this is still the current entry
26
+ if (entry.refCount === 0 && map.get(key) === entry) {
27
+ map.delete(key);
28
+ }
29
+ }
30
+ }
31
+ };
32
+ };