mid-baileys 0.7.28 → 0.7.30

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.
@@ -6,7 +6,17 @@ const boom_1 = require("@hapi/boom");
6
6
  const WAProto_1 = require("../../WAProto");
7
7
  const WABinary_1 = require("../WABinary");
8
8
  const generics_1 = require("./generics");
9
-
9
+ const unpadRandomMax16 = (e) => {
10
+ const t = new Uint8Array(e);
11
+ if (0 === t.length) {
12
+ throw new Error('unpadPkcs7 given empty bytes');
13
+ }
14
+ var r = t[t.length - 1];
15
+ if (r > t.length) {
16
+ throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
17
+ }
18
+ return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
19
+ };
10
20
  exports.NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
11
21
  exports.MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
12
22
  exports.NACK_REASONS = {
@@ -36,7 +46,9 @@ const extractAddressingContext = stanza => {
36
46
  senderAlt = stanza.attrs.participant_lid || stanza.attrs.sender_lid || stanza.attrs.peer_recipient_lid;
37
47
  recipientAlt = stanza.attrs.recipient_lid;
38
48
  }
49
+ if(sender !== senderAlt)
39
50
  return {
51
+ sender: sender !== senderAlt ? sender : sender,
40
52
  addressingMode,
41
53
  senderAlt,
42
54
  recipientAlt
@@ -56,10 +68,10 @@ function decodeMessageNode(stanza, meId, meLid) {
56
68
  const isMeLid = (jid) => (0, WABinary_1.areJidsSameUser)(jid, meLid);
57
69
 
58
70
  const msgId = stanza.attrs.id;
59
- const senderPn = stanza.attrs?.sender_pn;
60
- const senderLid = stanza.attrs?.sender_lid;
71
+ const senderPn = stanza.attrs?.sender_pn ? stanza.attrs.sender_pn : (stanza.attrs?.participant && (0, WABinary_1.isJidUser)(stanza.attrs.participant) ? stanza.attrs.participant : null);
72
+ const senderLid = stanza.attrs?.sender_lid ? stanza.attrs.sender_lid : (stanza.attrs?.participant && (0, WABinary_1.isLidUser)(stanza.attrs.participant) ? stanza.attrs.participant : null);
61
73
  const participant = stanza.attrs.participant;
62
- const addressingContext = extractAddressingContext(stanza);
74
+ const adCon = extractAddressingContext(stanza);
63
75
 
64
76
  const normalizePrivateChatJid = (jid) => {
65
77
  if ((0, WABinary_1.isJidGroup)(jid)) return jid;
@@ -127,13 +139,14 @@ function decodeMessageNode(stanza, meId, meLid) {
127
139
  const key = Object.fromEntries(
128
140
  Object.entries({
129
141
  remoteJid: chatId,
130
- remoteJidAlt: !(0, WABinary_1.isJidGroup)(chatId) ? (fromMe ? addressingContext.recipientAlt : addressingContext.senderAlt) : (chatId !== addressingContext.recipientAlt && !fromMe ? addressingContext.recipientAlt : null),
142
+ remoteJidAlt: !(0, WABinary_1.isJidGroup)(chatId) ? (fromMe ? adCon.recipientAlt : adCon.senderAlt) : (chatId !== adCon.recipientAlt && !fromMe ? adCon.recipientAlt : null),
131
143
  fromMe,
132
144
  id: msgId,
133
145
  senderPn,
134
146
  senderLid: senderLid || ((0, WABinary_1.isLidUser)(from) ? from : null),
135
147
  participant: (0, WABinary_1.isJidGroup)(chatId) ? participant : undefined,
136
148
  participantAlt: (0, WABinary_1.isJidGroup)(chatId) ? participantAlt : undefined,
149
+ addressingMode: adCon.addressingMode,
137
150
  'server_id': stanza.attrs?.server_id
138
151
  }).filter(([_, v]) => v != null)
139
152
  );
@@ -145,8 +158,7 @@ function decodeMessageNode(stanza, meId, meLid) {
145
158
  pushName: pushname,
146
159
  broadcast: WABinary_1.isJidBroadcast(from),
147
160
  newsletter: WABinary_1.isJidNewsletter(from),
148
- additionalAttributes: stanza.attrs,
149
- mode: addressingContext.addressingMode
161
+ additionalAttributes: stanza.attrs
150
162
  }
151
163
  if (msgType === 'newsletter') {
152
164
  fullMessage.newsletterServerId = +stanza.attrs?.server_id;
@@ -168,58 +180,29 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
168
180
  fullMessage,
169
181
  category: stanza.attrs.category,
170
182
  author,
171
- async decrypt() {
172
- const normalizeDecryptJid = (jid = '') => {
173
- const value = String(jid || '').trim();
174
- if (!value || !value.includes('@')) {
175
- return '';
176
- }
177
- return (0, WABinary_1.jidNormalizedUser)(value);
178
- };
179
-
183
+ async decrypt() {
180
184
  let decryptables = 0;
181
185
  if (Array.isArray(stanza.content)) {
182
186
  for (const { tag, attrs, content } of stanza.content) {
183
- // دمج ميزات الـ Meta والـ Verified Business والـ Bot
184
187
  if (tag === 'verified_name' && content instanceof Uint8Array) {
185
188
  const cert = WAProto_1.proto.VerifiedNameCertificate.decode(content);
186
189
  const details = WAProto_1.proto.VerifiedNameCertificate.Details.decode(cert.details);
187
190
  fullMessage.verifiedBizName = details.verifiedName;
188
191
  }
189
- if (tag === 'multicast' && content instanceof Uint8Array) {
190
- fullMessage.multicast = true;
191
- }
192
- if (tag === 'meta' && content instanceof Uint8Array) {
193
- fullMessage.metaInfo = { targetID: attrs.target_id };
194
- if (attrs.target_sender_jid) {
195
- fullMessage.metaInfo.targetSender = (0, WABinary_1.jidNormalizedUser)(attrs.target_sender_jid);
196
- }
197
- }
198
- if (tag === 'bot' && content instanceof Uint8Array) {
199
- if (attrs.edit) {
200
- fullMessage.botInfo = {
201
- editType: attrs.edit,
202
- editTargetID: attrs.edit_target_id,
203
- editSenderTimestampMS: attrs.sender_timestamp_ms
204
- };
205
- }
206
- }
207
- // كشف الـ ViewOnce الجديد من الكود الثاني
208
192
  if (tag === 'unavailable' && attrs.type === 'view_once') {
209
- fullMessage.key.isViewOnce = true;
210
- fullMessage.additionalAttributes.viewonce = true;
193
+ fullMessage.key.isViewOnce = true; // TODO: remove from here and add a STUB TYPE
194
+ }
195
+ if (attrs.count && tag === 'enc') {
196
+ fullMessage.retryCount = Number(attrs.count);
211
197
  }
212
-
213
198
  if (tag !== 'enc' && tag !== 'plaintext') {
214
199
  continue;
215
200
  }
216
201
  if (!(content instanceof Uint8Array)) {
217
202
  continue;
218
203
  }
219
-
220
204
  decryptables += 1;
221
205
  let msgBuffer;
222
-
223
206
  try {
224
207
  const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
225
208
  switch (e2eType) {
@@ -232,55 +215,22 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
232
215
  break;
233
216
  case 'pkmsg':
234
217
  case 'msg':
235
- {
236
- // دمج الأمان العالي: تجريب فك التشفير بالمعرفات المتعددة (الـ PN والـ LID معاً)
237
- const primaryUser = (0, WABinary_1.isJidUser)(sender) ? sender : author;
238
- const pnCandidate = normalizeDecryptJid(
239
- stanza.attrs.sender_pn ||
240
- stanza.attrs.peer_recipient_pn ||
241
- stanza.attrs.participant_pn ||
242
- fullMessage.key?.senderPn ||
243
- ''
244
- );
245
- const decryptCandidates = Array.from(new Set([
246
- normalizeDecryptJid(primaryUser),
247
- pnCandidate
248
- ].filter(Boolean)));
249
-
250
- let lastDecryptError = null;
251
-
252
- for (const candidate of decryptCandidates) {
253
- try {
254
- msgBuffer = await repository.decryptMessage({
255
- jid: candidate,
256
- type: e2eType,
257
- ciphertext: content
258
- });
259
- lastDecryptError = null;
260
- break; // نجح فك التشفير! اخرج من الـ Loop
261
- }
262
- catch (error) {
263
- lastDecryptError = error;
264
- }
265
- }
266
-
267
- if (lastDecryptError) {
268
- throw lastDecryptError;
269
- }
270
- }
218
+ msgBuffer = await repository.decryptMessage({
219
+ jid: author,
220
+ type: e2eType,
221
+ ciphertext: content
222
+ });
271
223
  break;
272
224
  case 'plaintext':
273
- case undefined:
274
225
  msgBuffer = content;
275
226
  break;
276
227
  default:
277
228
  throw new Error(`Unknown e2e type: ${e2eType}`);
278
229
  }
279
-
280
- let msg = WAProto_1.proto.Message.decode(e2eType !== 'plaintext' ? (0, generics_1.unpadRandomMax16)(msgBuffer) : msgBuffer);
230
+ let msg = WAProto_1.proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer);
281
231
  msg = msg.deviceSentMessage?.message || msg;
282
-
283
232
  if (msg.senderKeyDistributionMessage) {
233
+ //eslint-disable-next-line max-depth
284
234
  try {
285
235
  await repository.processSenderKeyDistributionMessage({
286
236
  authorJid: author,
@@ -288,28 +238,41 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
288
238
  });
289
239
  }
290
240
  catch (err) {
291
- logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
241
+ logger.error({ key: fullMessage.key, err }, 'failed to process sender key distribution message');
292
242
  }
293
243
  }
294
-
295
244
  if (fullMessage.message) {
296
245
  Object.assign(fullMessage.message, msg);
297
- } else {
246
+ }
247
+ else {
298
248
  fullMessage.message = msg;
299
249
  }
300
250
  }
301
251
  catch (err) {
302
- logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
252
+ const errorContext = {
253
+ key: fullMessage.key,
254
+ err,
255
+ messageType: tag === 'plaintext' ? 'plaintext' : attrs.type,
256
+ sender,
257
+ author,
258
+ isSessionRecordError: isSessionRecordError(err)
259
+ };
260
+ logger.error(errorContext, 'failed to decrypt message');
303
261
  fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
304
- fullMessage.messageStubParameters = [err.message];
262
+ fullMessage.messageStubParameters = [err.message.toString()];
305
263
  }
306
264
  }
307
265
  }
308
- if (!decryptables) {
266
+ // if nothing was found to decrypt
267
+ if (!decryptables && !fullMessage.key?.isViewOnce) {
309
268
  fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
310
269
  fullMessage.messageStubParameters = [(0, exports.NO_MESSAGE_FOUND_ERROR_TEXT) || "No message found"];
311
270
  }
312
271
  }
313
272
  };
314
273
  };
274
+ function isSessionRecordError(error) {
275
+ const errorMessage = error?.message || error?.toString() || '';
276
+ return ['No session record', 'SessionError: No session record'].some(errorPattern => errorMessage.includes(errorPattern));
277
+ }
315
278
  exports.decryptMessageNode = decryptMessageNode;
@@ -163,9 +163,8 @@ exports.toNumber = toNumber;
163
163
  /** unix timestamp of a date in seconds - Safe Version */
164
164
  const unixTimestampSeconds = (date = new Date()) => {
165
165
  const d = (date instanceof Date) ? date : new Date(date);
166
- if (isNaN(d.getTime())) {
167
- return Math.floor(Date.now() / 1000);
168
- }
166
+ const targetDate = isNaN(d.getTime()) ? new Date() : d;
167
+ return Math.floor(targetDate.getTime() / 1000);
169
168
  };
170
169
  exports.unixTimestampSeconds = unixTimestampSeconds;
171
170
  const debouncedTimeout = (intervalMs = 1000, task) => {