@rosyn/whats 0.1.1 → 0.1.4
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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/dist/compat/extensions.d.ts +7 -0
- package/dist/compat/extensions.d.ts.map +1 -1
- package/dist/compat/extensions.js +51 -3
- package/dist/compat/extensions.js.map +1 -1
- package/dist/compat/index.d.ts +1 -1
- package/dist/compat/index.d.ts.map +1 -1
- package/dist/compat/index.js +311 -149
- package/dist/compat/index.js.map +1 -1
- package/dist/media/index.d.ts +1 -1
- package/dist/media/index.d.ts.map +1 -1
- package/dist/media/index.js +12 -3
- package/dist/media/index.js.map +1 -1
- package/dist/proto/index.d.ts +131 -0
- package/dist/proto/index.d.ts.map +1 -1
- package/dist/proto/index.js +467 -6
- package/dist/proto/index.js.map +1 -1
- package/dist/protocol/jid.d.ts.map +1 -1
- package/dist/protocol/jid.js +9 -1
- package/dist/protocol/jid.js.map +1 -1
- package/dist/protocol/wabin.d.ts.map +1 -1
- package/dist/protocol/wabin.js +61 -9
- package/dist/protocol/wabin.js.map +1 -1
- package/dist/signal/protocol.d.ts +1 -0
- package/dist/signal/protocol.d.ts.map +1 -1
- package/dist/signal/protocol.js +105 -11
- package/dist/signal/protocol.js.map +1 -1
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js.map +1 -1
- package/native/rosyn_whats_engine.node +0 -0
- package/package.json +1 -1
package/dist/compat/index.js
CHANGED
|
@@ -50,8 +50,13 @@ export async function fetchLatestBaileysVersion() {
|
|
|
50
50
|
function reviveBuffers(val) {
|
|
51
51
|
if (!val || typeof val !== "object")
|
|
52
52
|
return val;
|
|
53
|
-
if (val
|
|
54
|
-
return Buffer.from(val
|
|
53
|
+
if (val instanceof Uint8Array)
|
|
54
|
+
return Buffer.from(val);
|
|
55
|
+
if (val.type === "Buffer") {
|
|
56
|
+
if (Array.isArray(val.data))
|
|
57
|
+
return Buffer.from(val.data);
|
|
58
|
+
if (typeof val.data === "string")
|
|
59
|
+
return Buffer.from(val.data, "base64");
|
|
55
60
|
}
|
|
56
61
|
if (Array.isArray(val))
|
|
57
62
|
return val.map(reviveBuffers);
|
|
@@ -107,9 +112,17 @@ export async function useMultiFileAuthState(dir) {
|
|
|
107
112
|
flushTimer = setTimeout(() => {
|
|
108
113
|
flushTimer = undefined;
|
|
109
114
|
void client.flushSession().catch(() => { });
|
|
110
|
-
},
|
|
115
|
+
}, 5000);
|
|
111
116
|
flushTimer.unref();
|
|
112
117
|
};
|
|
118
|
+
const onExitFlush = () => {
|
|
119
|
+
if (flushTimer) {
|
|
120
|
+
clearTimeout(flushTimer);
|
|
121
|
+
flushTimer = undefined;
|
|
122
|
+
}
|
|
123
|
+
void client.flushSession().catch(() => { });
|
|
124
|
+
};
|
|
125
|
+
process.once("beforeExit", onExitFlush);
|
|
113
126
|
return {
|
|
114
127
|
state: {
|
|
115
128
|
creds,
|
|
@@ -130,15 +143,29 @@ export async function useMultiFileAuthState(dir) {
|
|
|
130
143
|
return out;
|
|
131
144
|
},
|
|
132
145
|
set: async (data) => {
|
|
146
|
+
let changed = false;
|
|
133
147
|
for (const [type, entries] of Object.entries(data)) {
|
|
134
148
|
for (const [id, val] of Object.entries(entries)) {
|
|
135
|
-
|
|
149
|
+
const recKey = `key:${type}:${id}`;
|
|
150
|
+
if (val === null || val === undefined) {
|
|
151
|
+
const prev = client.getAuthRecord(recKey);
|
|
152
|
+
if (prev && prev.length > 0) {
|
|
153
|
+
client.putAuthRecord(recKey, Buffer.alloc(0));
|
|
154
|
+
changed = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
136
158
|
const payload = val instanceof Uint8Array ? Buffer.from(val) : packAuthJson(val);
|
|
137
|
-
client.
|
|
159
|
+
const prev = client.getAuthRecord(recKey);
|
|
160
|
+
if (!prev || Buffer.compare(prev, payload) !== 0) {
|
|
161
|
+
client.putAuthRecord(recKey, payload);
|
|
162
|
+
changed = true;
|
|
163
|
+
}
|
|
138
164
|
}
|
|
139
165
|
}
|
|
140
166
|
}
|
|
141
|
-
|
|
167
|
+
if (changed)
|
|
168
|
+
scheduleFlush();
|
|
142
169
|
},
|
|
143
170
|
},
|
|
144
171
|
},
|
|
@@ -201,7 +228,12 @@ export function makeWASocket(config = {}) {
|
|
|
201
228
|
return memKeys.get(`${type}:${id}`);
|
|
202
229
|
};
|
|
203
230
|
const setKey = async (type, id, val) => {
|
|
204
|
-
|
|
231
|
+
if (val === null || val === undefined) {
|
|
232
|
+
memKeys.delete(`${type}:${id}`);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
memKeys.set(`${type}:${id}`, val);
|
|
236
|
+
}
|
|
205
237
|
if (authKeys?.set) {
|
|
206
238
|
await authKeys.set({ [type]: { [id]: val } });
|
|
207
239
|
}
|
|
@@ -276,7 +308,76 @@ export function makeWASocket(config = {}) {
|
|
|
276
308
|
},
|
|
277
309
|
}));
|
|
278
310
|
const pendingQueries = new Map();
|
|
311
|
+
const deviceCache = new Map();
|
|
279
312
|
const pendingMediaRetries = new Map();
|
|
313
|
+
const ensureSessions = async (devJids) => {
|
|
314
|
+
const checks = await Promise.all(devJids.map(async (dj) => ((await signalMgr.hasSession(jidToSignalAddress(dj))) ? null : dj)));
|
|
315
|
+
const missing = checks.filter((x) => x !== null);
|
|
316
|
+
if (missing.length === 0)
|
|
317
|
+
return;
|
|
318
|
+
const batches = [];
|
|
319
|
+
for (let i = 0; i < missing.length; i += 50)
|
|
320
|
+
batches.push(missing.slice(i, i + 50));
|
|
321
|
+
await Promise.all(batches.map(async (batch) => {
|
|
322
|
+
try {
|
|
323
|
+
const res = await sendQuery({
|
|
324
|
+
tag: "iq",
|
|
325
|
+
attrs: { to: "s.whatsapp.net", type: "get", id: nextTag(), xmlns: "encrypt" },
|
|
326
|
+
content: [
|
|
327
|
+
{
|
|
328
|
+
tag: "key",
|
|
329
|
+
attrs: {},
|
|
330
|
+
content: batch.map((dj) => ({ tag: "user", attrs: { jid: dj } })),
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
});
|
|
334
|
+
const list = Array.isArray(res.content) ? res.content.find((c) => c.tag === "list") : undefined;
|
|
335
|
+
if (!Array.isArray(list?.content))
|
|
336
|
+
return;
|
|
337
|
+
for (const u of list.content) {
|
|
338
|
+
if (u.tag !== "user" || !u.attrs?.jid || !Array.isArray(u.content))
|
|
339
|
+
continue;
|
|
340
|
+
const regNode = u.content.find((c) => c.tag === "registration");
|
|
341
|
+
const identNode = u.content.find((c) => c.tag === "identity");
|
|
342
|
+
const skeyNode = u.content.find((c) => c.tag === "skey");
|
|
343
|
+
const keyNode = u.content.find((c) => c.tag === "key");
|
|
344
|
+
if (!(regNode?.content instanceof Uint8Array) || !(identNode?.content instanceof Uint8Array) || !Array.isArray(skeyNode?.content)) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const skeyIdBuf = skeyNode.content.find((c) => c.tag === "id")?.content;
|
|
348
|
+
const skeyValBuf = skeyNode.content.find((c) => c.tag === "value")?.content;
|
|
349
|
+
if (!(skeyIdBuf instanceof Uint8Array) || !(skeyValBuf instanceof Uint8Array))
|
|
350
|
+
continue;
|
|
351
|
+
const regId = Buffer.from(regNode.content).readUInt32BE(0);
|
|
352
|
+
const skeyId = Buffer.from(skeyIdBuf).readUIntBE(0, skeyIdBuf.length);
|
|
353
|
+
let preKey;
|
|
354
|
+
if (Array.isArray(keyNode?.content)) {
|
|
355
|
+
const pkIdBuf = keyNode.content.find((c) => c.tag === "id")?.content;
|
|
356
|
+
const pkValBuf = keyNode.content.find((c) => c.tag === "value")?.content;
|
|
357
|
+
if (pkIdBuf instanceof Uint8Array && pkValBuf instanceof Uint8Array) {
|
|
358
|
+
preKey = {
|
|
359
|
+
keyId: Buffer.from(pkIdBuf).readUIntBE(0, pkIdBuf.length),
|
|
360
|
+
publicKey: pkValBuf,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
await signalMgr.initOutgoingSession(jidToSignalAddress(u.attrs.jid), {
|
|
365
|
+
registrationId: regId,
|
|
366
|
+
identityKey: identNode.content,
|
|
367
|
+
signedPreKey: { keyId: skeyId, publicKey: skeyValBuf },
|
|
368
|
+
preKey,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
catch {
|
|
373
|
+
}
|
|
374
|
+
}));
|
|
375
|
+
};
|
|
376
|
+
const computePhash = (devs) => {
|
|
377
|
+
const sorted = [...devs].sort();
|
|
378
|
+
const hash = createHash("sha256").update(sorted.join("")).digest("base64");
|
|
379
|
+
return "2:" + hash.slice(0, 6);
|
|
380
|
+
};
|
|
280
381
|
const lidToPnMap = new Map();
|
|
281
382
|
const rememberLidPn = (lid, pn) => {
|
|
282
383
|
if (lid && pn && lid.endsWith("@lid") && pn.endsWith("@s.whatsapp.net")) {
|
|
@@ -507,8 +608,17 @@ export function makeWASocket(config = {}) {
|
|
|
507
608
|
admin: p.attrs.type || null,
|
|
508
609
|
};
|
|
509
610
|
});
|
|
510
|
-
if (action === "
|
|
511
|
-
senderKeyMemory.
|
|
611
|
+
if (action === "remove") {
|
|
612
|
+
const mem = senderKeyMemory.get(groupId);
|
|
613
|
+
if (mem) {
|
|
614
|
+
for (const p of participants) {
|
|
615
|
+
for (const dev of Array.from(mem)) {
|
|
616
|
+
const u = dev.split(":")[0]?.split("@")[0];
|
|
617
|
+
if (u && (p.jid?.startsWith(u) || p.lid?.startsWith(u)))
|
|
618
|
+
mem.delete(dev);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
512
622
|
}
|
|
513
623
|
groupCache.delete(groupId);
|
|
514
624
|
if (participants.length > 0) {
|
|
@@ -750,7 +860,6 @@ export function makeWASocket(config = {}) {
|
|
|
750
860
|
rAck.participant = node.attrs.participant;
|
|
751
861
|
transport.sendEncrypted(encodeWABinaryNode({ tag: "ack", attrs: rAck }));
|
|
752
862
|
if (node.attrs.type === "retry") {
|
|
753
|
-
console.warn(`[rosyn/whats] WA retry receipt:`, JSON.stringify(node));
|
|
754
863
|
const retryPid = node.attrs.participant || node.attrs.from;
|
|
755
864
|
const retryNode = Array.isArray(node.content)
|
|
756
865
|
? node.content.find((c) => c.tag === "retry")
|
|
@@ -808,7 +917,8 @@ export function makeWASocket(config = {}) {
|
|
|
808
917
|
meta.addressingMode ||
|
|
809
918
|
((meta.participants ?? []).some((p) => p.id.endsWith("@lid")) ? "lid" : "pn");
|
|
810
919
|
const groupSender = retryAddrMode === "lid" && meLid ? meLid : meJid;
|
|
811
|
-
const
|
|
920
|
+
const groupKey = `${cachedMsg.jid}:${jidToSignalAddress(groupSender)}`;
|
|
921
|
+
const { distributionBytes } = await signalMgr.getOrCreateGroupDistribution(groupKey);
|
|
812
922
|
payloadToResend = {
|
|
813
923
|
...cachedMsg.message,
|
|
814
924
|
senderKeyDistributionMessage: {
|
|
@@ -828,6 +938,11 @@ export function makeWASocket(config = {}) {
|
|
|
828
938
|
},
|
|
829
939
|
};
|
|
830
940
|
}
|
|
941
|
+
if (cachedMsg.jid.endsWith("@g.us") && !jidDecode(retryPid)?.device) {
|
|
942
|
+
senderKeyMemory.delete(cachedMsg.jid);
|
|
943
|
+
await setKey("sender-key-memory-v3", cachedMsg.jid, []);
|
|
944
|
+
}
|
|
945
|
+
await ensureSessions([retryPid]);
|
|
831
946
|
const paddedRetry = writeRandomPadMax16(proto.Message.encode(payloadToResend));
|
|
832
947
|
const enc = await signalMgr.encryptMessage(jidToSignalAddress(retryPid), paddedRetry);
|
|
833
948
|
const getMediaStanzaMeta = (m) => {
|
|
@@ -851,6 +966,10 @@ export function makeWASocket(config = {}) {
|
|
|
851
966
|
};
|
|
852
967
|
const { stanzaType: retryStanzaType, mediatype: retryMediatype } = getMediaStanzaMeta(cachedMsg.message);
|
|
853
968
|
const isLegacyBtnRetry = Boolean(cachedMsg.message?.buttonsMessage);
|
|
969
|
+
if (cachedMsg.jid.endsWith("@g.us") && !jidDecode(retryPid)?.device) {
|
|
970
|
+
senderKeyMemory.delete(cachedMsg.jid);
|
|
971
|
+
await setKey("sender-key-memory-v3", cachedMsg.jid, []);
|
|
972
|
+
}
|
|
854
973
|
const retryContent = [
|
|
855
974
|
{
|
|
856
975
|
tag: "enc",
|
|
@@ -910,6 +1029,7 @@ export function makeWASocket(config = {}) {
|
|
|
910
1029
|
id: node.attrs.id,
|
|
911
1030
|
to: cachedMsg.jid,
|
|
912
1031
|
...(cachedMsg.jid.endsWith("@g.us") ? { participant: retryPid } : {}),
|
|
1032
|
+
...(retryAddrMode ? { addressing_mode: retryAddrMode } : {}),
|
|
913
1033
|
type: retryStanzaType,
|
|
914
1034
|
},
|
|
915
1035
|
content: retryContent,
|
|
@@ -920,8 +1040,9 @@ export function makeWASocket(config = {}) {
|
|
|
920
1040
|
return;
|
|
921
1041
|
}
|
|
922
1042
|
if (node.tag === "ack") {
|
|
923
|
-
if (node.attrs.error
|
|
924
|
-
|
|
1043
|
+
if (node.attrs.error && node.attrs.from?.endsWith("@g.us") && !node.attrs.participant) {
|
|
1044
|
+
senderKeyMemory.delete(node.attrs.from);
|
|
1045
|
+
void setKey("sender-key-memory-v3", node.attrs.from, []).catch(() => { });
|
|
925
1046
|
}
|
|
926
1047
|
return;
|
|
927
1048
|
}
|
|
@@ -981,19 +1102,14 @@ export function makeWASocket(config = {}) {
|
|
|
981
1102
|
}
|
|
982
1103
|
let decoded = proto.Message.decode(plainBuf);
|
|
983
1104
|
decoded = decoded.deviceSentMessage?.message ?? decoded;
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
1105
|
+
const skdm = decoded.senderKeyDistributionMessage ?? decoded.fastRatchetKeySenderKeyDistributionMessage;
|
|
1106
|
+
if (skdm?.axolotlSenderKeyDistributionMessage) {
|
|
1107
|
+
const gId = skdm.groupId ?? chatId;
|
|
1108
|
+
await signalMgr.processSenderKeyDistribution(`${gId}:${jidToSignalAddress(author)}`, skdm.axolotlSenderKeyDistributionMessage);
|
|
987
1109
|
}
|
|
988
|
-
const hasDisplayContent =
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
decoded.interactiveResponseMessage?.nativeFlowResponseMessage ||
|
|
992
|
-
decoded.ephemeralMessage?.message ||
|
|
993
|
-
decoded.viewOnceMessage?.message ||
|
|
994
|
-
decoded.viewOnceMessageV2?.message ||
|
|
995
|
-
decoded.documentWithCaptionMessage?.message ||
|
|
996
|
-
decoded.editedMessage?.message);
|
|
1110
|
+
const hasDisplayContent = Object.keys(decoded).some((k) => k !== "senderKeyDistributionMessage" &&
|
|
1111
|
+
k !== "fastRatchetKeySenderKeyDistributionMessage" &&
|
|
1112
|
+
k !== "messageContextInfo");
|
|
997
1113
|
if (hasDisplayContent) {
|
|
998
1114
|
if (isGroup &&
|
|
999
1115
|
participant?.endsWith("@lid") &&
|
|
@@ -1325,10 +1441,10 @@ export function makeWASocket(config = {}) {
|
|
|
1325
1441
|
? b.buttonParamsJson
|
|
1326
1442
|
: JSON.stringify(b.buttonParamsJson ?? {}),
|
|
1327
1443
|
})),
|
|
1328
|
-
messageParamsJson:
|
|
1329
|
-
? typeof
|
|
1330
|
-
?
|
|
1331
|
-
: JSON.stringify(
|
|
1444
|
+
messageParamsJson: card.messageParams
|
|
1445
|
+
? typeof card.messageParams === "string"
|
|
1446
|
+
? card.messageParams
|
|
1447
|
+
: JSON.stringify(card.messageParams)
|
|
1332
1448
|
: "{}",
|
|
1333
1449
|
messageVersion: 1,
|
|
1334
1450
|
},
|
|
@@ -1662,114 +1778,92 @@ export function makeWASocket(config = {}) {
|
|
|
1662
1778
|
const text = typeof waMsg.conversation === "string"
|
|
1663
1779
|
? waMsg.conversation
|
|
1664
1780
|
: waMsg.interactiveMessage?.body?.text ?? waMsg.buttonsMessage?.contentText ?? "";
|
|
1781
|
+
if (!waMsg.protocolMessage && !waMsg.reactionMessage && !waMsg.messageContextInfo && !jid.endsWith("@newsletter")) {
|
|
1782
|
+
waMsg.messageContextInfo = {
|
|
1783
|
+
deviceListMetadata: {},
|
|
1784
|
+
deviceListMetadataVersion: 2,
|
|
1785
|
+
...(waMsg.pollCreationMessage || waMsg.pollCreationMessageV3 ? { messageSecret: randomBytes(32) } : {}),
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1665
1788
|
const padded = writeRandomPadMax16(proto.Message.encode(waMsg));
|
|
1666
1789
|
const meJid = authCreds.me?.id ?? sock.user?.id ?? "0@s.whatsapp.net";
|
|
1667
1790
|
const msgId = generateMessageIDV2(meJid);
|
|
1668
1791
|
sentMessages.set(msgId, { jid, message: waMsg });
|
|
1669
1792
|
setTimeout(() => sentMessages.delete(msgId), 5 * 60_000).unref();
|
|
1670
1793
|
const getDevices = async (userJids) => {
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
const
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1794
|
+
const now = Date.now();
|
|
1795
|
+
const cachedOut = [];
|
|
1796
|
+
const uncached = [];
|
|
1797
|
+
for (const u of userJids) {
|
|
1798
|
+
const key = jidNormalizedUser(u);
|
|
1799
|
+
const hit = deviceCache.get(key);
|
|
1800
|
+
if (hit && now - hit.ts < 5 * 60_000) {
|
|
1801
|
+
cachedOut.push(...hit.devices);
|
|
1802
|
+
}
|
|
1803
|
+
else {
|
|
1804
|
+
uncached.push(key);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
if (uncached.length === 0 && cachedOut.length > 0)
|
|
1808
|
+
return cachedOut;
|
|
1809
|
+
const batches = [];
|
|
1810
|
+
for (let i = 0; i < uncached.length; i += 50)
|
|
1811
|
+
batches.push(uncached.slice(i, i + 50));
|
|
1812
|
+
const batchResults = await Promise.all(batches.map(async (batch) => {
|
|
1813
|
+
const batchOut = [];
|
|
1814
|
+
try {
|
|
1815
|
+
const res = await sendQuery({
|
|
1816
|
+
tag: "iq",
|
|
1817
|
+
attrs: { to: "s.whatsapp.net", type: "get", id: nextTag(), xmlns: "usync" },
|
|
1818
|
+
content: [
|
|
1819
|
+
{
|
|
1820
|
+
tag: "usync",
|
|
1821
|
+
attrs: { sid: nextTag(), mode: "query", last: "true", index: "0", context: "message" },
|
|
1822
|
+
content: [
|
|
1823
|
+
{ tag: "query", attrs: {}, content: [{ tag: "devices", attrs: { version: "2" } }] },
|
|
1824
|
+
{
|
|
1825
|
+
tag: "list",
|
|
1826
|
+
attrs: {},
|
|
1827
|
+
content: batch.map((u) => ({ tag: "user", attrs: { jid: u } })),
|
|
1828
|
+
},
|
|
1829
|
+
],
|
|
1830
|
+
},
|
|
1831
|
+
],
|
|
1832
|
+
});
|
|
1833
|
+
const usync = Array.isArray(res.content) ? res.content.find((c) => c.tag === "usync") : undefined;
|
|
1834
|
+
const list = Array.isArray(usync?.content) ? usync.content.find((c) => c.tag === "list") : undefined;
|
|
1835
|
+
if (Array.isArray(list?.content)) {
|
|
1836
|
+
for (const u of list.content) {
|
|
1837
|
+
const uJid = u.attrs?.jid;
|
|
1838
|
+
const devs = Array.isArray(u.content) ? u.content.find((c) => c.tag === "devices") : undefined;
|
|
1839
|
+
const devList = Array.isArray(devs?.content) ? devs.content.find((c) => c.tag === "device-list") : undefined;
|
|
1840
|
+
if (uJid && Array.isArray(devList?.content)) {
|
|
1841
|
+
const dec = jidDecode(uJid);
|
|
1842
|
+
const userDevs = [];
|
|
1843
|
+
for (const d of devList.content) {
|
|
1844
|
+
if (d.tag === "device" && d.attrs?.id !== undefined && dec) {
|
|
1845
|
+
if (d.attrs.is_hosted === "true")
|
|
1846
|
+
continue;
|
|
1847
|
+
const devId = Number(d.attrs.id);
|
|
1848
|
+
if (devId !== 0 && !d.attrs["key-index"])
|
|
1849
|
+
continue;
|
|
1850
|
+
userDevs.push(devId === 0 ? `${dec.user}@${dec.server}` : `${dec.user}:${devId}@${dec.server}`);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
if (userDevs.length > 0) {
|
|
1854
|
+
deviceCache.set(jidNormalizedUser(uJid), { devices: userDevs, ts: now });
|
|
1855
|
+
batchOut.push(...userDevs);
|
|
1704
1856
|
}
|
|
1705
1857
|
}
|
|
1706
1858
|
}
|
|
1707
1859
|
}
|
|
1708
1860
|
}
|
|
1709
|
-
|
|
1710
|
-
return out;
|
|
1711
|
-
}
|
|
1712
|
-
catch {
|
|
1713
|
-
}
|
|
1714
|
-
return userJids.map((u) => jidNormalizedUser(u));
|
|
1715
|
-
};
|
|
1716
|
-
const ensureSessions = async (devJids) => {
|
|
1717
|
-
const missing = [];
|
|
1718
|
-
for (const dj of devJids) {
|
|
1719
|
-
if (!(await signalMgr.hasSession(jidToSignalAddress(dj)))) {
|
|
1720
|
-
missing.push(dj);
|
|
1721
|
-
}
|
|
1722
|
-
}
|
|
1723
|
-
if (missing.length === 0)
|
|
1724
|
-
return;
|
|
1725
|
-
const res = await sendQuery({
|
|
1726
|
-
tag: "iq",
|
|
1727
|
-
attrs: { to: "s.whatsapp.net", type: "get", id: nextTag(), xmlns: "encrypt" },
|
|
1728
|
-
content: [
|
|
1729
|
-
{
|
|
1730
|
-
tag: "key",
|
|
1731
|
-
attrs: {},
|
|
1732
|
-
content: missing.map((dj) => ({ tag: "user", attrs: { jid: dj } })),
|
|
1733
|
-
},
|
|
1734
|
-
],
|
|
1735
|
-
});
|
|
1736
|
-
const list = Array.isArray(res.content) ? res.content.find((c) => c.tag === "list") : undefined;
|
|
1737
|
-
if (!Array.isArray(list?.content))
|
|
1738
|
-
return;
|
|
1739
|
-
for (const u of list.content) {
|
|
1740
|
-
if (u.tag !== "user" || !u.attrs?.jid || !Array.isArray(u.content))
|
|
1741
|
-
continue;
|
|
1742
|
-
const regNode = u.content.find((c) => c.tag === "registration");
|
|
1743
|
-
const identNode = u.content.find((c) => c.tag === "identity");
|
|
1744
|
-
const skeyNode = u.content.find((c) => c.tag === "skey");
|
|
1745
|
-
const keyNode = u.content.find((c) => c.tag === "key");
|
|
1746
|
-
if (!(regNode?.content instanceof Uint8Array) || !(identNode?.content instanceof Uint8Array) || !Array.isArray(skeyNode?.content)) {
|
|
1747
|
-
continue;
|
|
1748
|
-
}
|
|
1749
|
-
const skeyIdBuf = skeyNode.content.find((c) => c.tag === "id")?.content;
|
|
1750
|
-
const skeyValBuf = skeyNode.content.find((c) => c.tag === "value")?.content;
|
|
1751
|
-
if (!(skeyIdBuf instanceof Uint8Array) || !(skeyValBuf instanceof Uint8Array))
|
|
1752
|
-
continue;
|
|
1753
|
-
const regId = Buffer.from(regNode.content).readUInt32BE(0);
|
|
1754
|
-
const skeyId = Buffer.from(skeyIdBuf).readUIntBE(0, skeyIdBuf.length);
|
|
1755
|
-
let preKey;
|
|
1756
|
-
if (Array.isArray(keyNode?.content)) {
|
|
1757
|
-
const pkIdBuf = keyNode.content.find((c) => c.tag === "id")?.content;
|
|
1758
|
-
const pkValBuf = keyNode.content.find((c) => c.tag === "value")?.content;
|
|
1759
|
-
if (pkIdBuf instanceof Uint8Array && pkValBuf instanceof Uint8Array) {
|
|
1760
|
-
preKey = {
|
|
1761
|
-
keyId: Buffer.from(pkIdBuf).readUIntBE(0, pkIdBuf.length),
|
|
1762
|
-
publicKey: pkValBuf,
|
|
1763
|
-
};
|
|
1764
|
-
}
|
|
1861
|
+
catch {
|
|
1765
1862
|
}
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
preKey,
|
|
1771
|
-
});
|
|
1772
|
-
}
|
|
1863
|
+
return batchOut.length > 0 ? batchOut : batch;
|
|
1864
|
+
}));
|
|
1865
|
+
const out = [...cachedOut, ...batchResults.flat()];
|
|
1866
|
+
return out.length > 0 ? out : userJids.map((u) => jidNormalizedUser(u));
|
|
1773
1867
|
};
|
|
1774
1868
|
const buildDeviceIdentityNode = () => {
|
|
1775
1869
|
if (!authCreds.account)
|
|
@@ -1783,25 +1877,33 @@ export function makeWASocket(config = {}) {
|
|
|
1783
1877
|
});
|
|
1784
1878
|
return [{ tag: "device-identity", attrs: {}, content: enc }];
|
|
1785
1879
|
};
|
|
1786
|
-
const computePhash = (devs) => {
|
|
1787
|
-
const sorted = [...devs].sort();
|
|
1788
|
-
const hash = createHash("sha256").update(sorted.join("")).digest("base64");
|
|
1789
|
-
return "2:" + hash.slice(0, 6);
|
|
1790
|
-
};
|
|
1791
1880
|
if (jid.endsWith("@newsletter")) {
|
|
1881
|
+
const isNlDelete = Boolean(content?.delete && typeof content.delete === "object");
|
|
1882
|
+
const isNlEdit = Boolean(content?.edit && typeof content.edit === "object");
|
|
1883
|
+
const nlMsgId = isNlDelete
|
|
1884
|
+
? String(content.delete.id ?? msgId)
|
|
1885
|
+
: isNlEdit
|
|
1886
|
+
? String(content.edit.id ?? msgId)
|
|
1887
|
+
: msgId;
|
|
1888
|
+
const nlEditAttr = isNlDelete ? "8" : isNlEdit ? "3" : deleteEditAttr;
|
|
1889
|
+
const nlPlaintext = isNlDelete
|
|
1890
|
+
? Buffer.alloc(0)
|
|
1891
|
+
: isNlEdit && waMsg.protocolMessage?.editedMessage
|
|
1892
|
+
? proto.Message.encode(waMsg.protocolMessage.editedMessage)
|
|
1893
|
+
: proto.Message.encode(waMsg);
|
|
1792
1894
|
transport.sendEncrypted(encodeWABinaryNode({
|
|
1793
1895
|
tag: "message",
|
|
1794
1896
|
attrs: {
|
|
1795
|
-
id:
|
|
1897
|
+
id: nlMsgId,
|
|
1796
1898
|
to: jid,
|
|
1797
1899
|
type: stanzaType,
|
|
1798
|
-
...(
|
|
1900
|
+
...(nlEditAttr ? { edit: nlEditAttr } : {}),
|
|
1799
1901
|
},
|
|
1800
1902
|
content: [
|
|
1801
1903
|
{
|
|
1802
1904
|
tag: "plaintext",
|
|
1803
|
-
attrs: encMediatype ? { mediatype: encMediatype } : {},
|
|
1804
|
-
content:
|
|
1905
|
+
attrs: !isNlDelete && encMediatype ? { mediatype: encMediatype } : {},
|
|
1906
|
+
content: nlPlaintext,
|
|
1805
1907
|
},
|
|
1806
1908
|
],
|
|
1807
1909
|
}));
|
|
@@ -1813,7 +1915,16 @@ export function makeWASocket(config = {}) {
|
|
|
1813
1915
|
.filter(Boolean);
|
|
1814
1916
|
const meLid = authCreds.me?.lid ?? sock.user?.lid;
|
|
1815
1917
|
const rawDevices = await getDevices(participants.length ? participants : [meJid]);
|
|
1816
|
-
const devices = rawDevices.filter((d) =>
|
|
1918
|
+
const devices = rawDevices.filter((d) => {
|
|
1919
|
+
if (d === meJid || (meLid && d === meLid))
|
|
1920
|
+
return false;
|
|
1921
|
+
const dec = jidDecode(d);
|
|
1922
|
+
if (!dec || dec.device === 99)
|
|
1923
|
+
return false;
|
|
1924
|
+
if (d.endsWith("@hosted") || d.endsWith("@hosted.lid"))
|
|
1925
|
+
return false;
|
|
1926
|
+
return true;
|
|
1927
|
+
});
|
|
1817
1928
|
const addrMode = meta.addressingMode || (participants.some((p) => p.endsWith("@lid")) ? "lid" : "pn");
|
|
1818
1929
|
const groupSender = addrMode === "lid" && meLid ? meLid : meJid;
|
|
1819
1930
|
const groupKey = `${jid}:${jidToSignalAddress(groupSender)}`;
|
|
@@ -1821,7 +1932,7 @@ export function makeWASocket(config = {}) {
|
|
|
1821
1932
|
const skmsgBytes = await signalMgr.encryptGroupMessage(groupKey, padded);
|
|
1822
1933
|
let sentTo = senderKeyMemory.get(jid);
|
|
1823
1934
|
if (!sentTo) {
|
|
1824
|
-
const savedMem = (await getKey("sender-key-memory", jid));
|
|
1935
|
+
const savedMem = (await getKey("sender-key-memory-v3", jid));
|
|
1825
1936
|
sentTo = new Set(Array.isArray(savedMem) ? savedMem : []);
|
|
1826
1937
|
senderKeyMemory.set(jid, sentTo);
|
|
1827
1938
|
}
|
|
@@ -1851,7 +1962,7 @@ export function makeWASocket(config = {}) {
|
|
|
1851
1962
|
catch {
|
|
1852
1963
|
}
|
|
1853
1964
|
}
|
|
1854
|
-
await setKey("sender-key-memory", jid, Array.from(sentTo));
|
|
1965
|
+
await setKey("sender-key-memory-v3", jid, Array.from(sentTo));
|
|
1855
1966
|
}
|
|
1856
1967
|
transport.sendEncrypted(encodeWABinaryNode({
|
|
1857
1968
|
tag: "message",
|
|
@@ -1860,15 +1971,18 @@ export function makeWASocket(config = {}) {
|
|
|
1860
1971
|
to: jid,
|
|
1861
1972
|
type: stanzaType,
|
|
1862
1973
|
addressing_mode: addrMode,
|
|
1974
|
+
...(meta.ephemeralDuration && meta.ephemeralDuration > 0
|
|
1975
|
+
? { expiration: String(meta.ephemeralDuration) }
|
|
1976
|
+
: {}),
|
|
1863
1977
|
...(deleteEditAttr ? { edit: deleteEditAttr } : {}),
|
|
1864
1978
|
},
|
|
1865
1979
|
content: [
|
|
1866
|
-
...(participantNodes.length ? [{ tag: "participants", attrs: {}, content: participantNodes }] : []),
|
|
1867
1980
|
{
|
|
1868
1981
|
tag: "enc",
|
|
1869
1982
|
attrs: { v: "2", type: "skmsg", ...(encMediatype ? { mediatype: encMediatype } : {}) },
|
|
1870
1983
|
content: skmsgBytes,
|
|
1871
1984
|
},
|
|
1985
|
+
...(participantNodes.length ? [{ tag: "participants", attrs: {}, content: participantNodes }] : []),
|
|
1872
1986
|
...(hasPkmsg ? buildDeviceIdentityNode() : []),
|
|
1873
1987
|
...buildButtonAddonNodes(),
|
|
1874
1988
|
],
|
|
@@ -2084,9 +2198,13 @@ export function makeWASocket(config = {}) {
|
|
|
2084
2198
|
const parseGroupNode = (grp) => {
|
|
2085
2199
|
const subject = grp.attrs?.subject ?? "";
|
|
2086
2200
|
const addressingMode = grp.attrs?.addressing_mode;
|
|
2201
|
+
let ephemeralDuration;
|
|
2087
2202
|
const participants = [];
|
|
2088
2203
|
if (Array.isArray(grp.content)) {
|
|
2089
2204
|
for (const c of grp.content) {
|
|
2205
|
+
if (c.tag === "ephemeral" && c.attrs?.expiration) {
|
|
2206
|
+
ephemeralDuration = Number(c.attrs.expiration) || undefined;
|
|
2207
|
+
}
|
|
2090
2208
|
if (c.tag === "participant" && c.attrs?.jid) {
|
|
2091
2209
|
rememberLidPn(c.attrs.jid, c.attrs.phone_number);
|
|
2092
2210
|
const pnId = c.attrs.phone_number ? jidNormalizedUser(c.attrs.phone_number) : resolvePnJid(c.attrs.jid);
|
|
@@ -2100,7 +2218,7 @@ export function makeWASocket(config = {}) {
|
|
|
2100
2218
|
}
|
|
2101
2219
|
}
|
|
2102
2220
|
}
|
|
2103
|
-
return { subject, addressingMode, participants };
|
|
2221
|
+
return { subject, addressingMode, ...(ephemeralDuration ? { ephemeralDuration } : {}), participants };
|
|
2104
2222
|
};
|
|
2105
2223
|
try {
|
|
2106
2224
|
const res = await sendQuery({
|
|
@@ -2424,27 +2542,71 @@ export function makeWASocket(config = {}) {
|
|
|
2424
2542
|
export function normalizeMessageContent(content) {
|
|
2425
2543
|
if (!content || typeof content !== "object")
|
|
2426
2544
|
return content;
|
|
2427
|
-
|
|
2545
|
+
const wrapperKeys = [
|
|
2428
2546
|
"ephemeralMessage",
|
|
2429
2547
|
"viewOnceMessage",
|
|
2430
2548
|
"viewOnceMessageV2",
|
|
2431
2549
|
"viewOnceMessageV2Extension",
|
|
2432
2550
|
"documentWithCaptionMessage",
|
|
2433
2551
|
"editedMessage",
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2552
|
+
"lottieStickerMessage",
|
|
2553
|
+
"groupMentionedMessage",
|
|
2554
|
+
"botInvokeMessage",
|
|
2555
|
+
"associatedChildMessage",
|
|
2556
|
+
"groupStatusMessage",
|
|
2557
|
+
"groupStatusMessageV2",
|
|
2558
|
+
"groupStatusMentionMessage",
|
|
2559
|
+
"eventCoverImage",
|
|
2560
|
+
"statusMentionMessage",
|
|
2561
|
+
"pollCreationOptionImageMessage",
|
|
2562
|
+
"pollCreationMessageV4",
|
|
2563
|
+
"botForwardedMessage",
|
|
2564
|
+
];
|
|
2565
|
+
let cur = content;
|
|
2566
|
+
for (let depth = 0; depth < 5; depth++) {
|
|
2567
|
+
let unwrapped = false;
|
|
2568
|
+
for (const key of wrapperKeys) {
|
|
2569
|
+
if (cur?.[key]?.message) {
|
|
2570
|
+
cur = cur[key].message;
|
|
2571
|
+
unwrapped = true;
|
|
2572
|
+
break;
|
|
2573
|
+
}
|
|
2437
2574
|
}
|
|
2575
|
+
if (!unwrapped)
|
|
2576
|
+
break;
|
|
2438
2577
|
}
|
|
2439
|
-
return
|
|
2578
|
+
return cur;
|
|
2440
2579
|
}
|
|
2441
2580
|
export function extractMessageContent(content) {
|
|
2442
|
-
|
|
2581
|
+
const extractFromTemplate = (msg) => {
|
|
2582
|
+
if (msg?.imageMessage)
|
|
2583
|
+
return { imageMessage: msg.imageMessage };
|
|
2584
|
+
if (msg?.documentMessage)
|
|
2585
|
+
return { documentMessage: msg.documentMessage };
|
|
2586
|
+
if (msg?.videoMessage)
|
|
2587
|
+
return { videoMessage: msg.videoMessage };
|
|
2588
|
+
if (msg?.locationMessage)
|
|
2589
|
+
return { locationMessage: msg.locationMessage };
|
|
2590
|
+
return { conversation: msg?.contentText ?? msg?.hydratedContentText ?? "" };
|
|
2591
|
+
};
|
|
2592
|
+
const norm = normalizeMessageContent(content);
|
|
2593
|
+
if (norm?.buttonsMessage)
|
|
2594
|
+
return extractFromTemplate(norm.buttonsMessage);
|
|
2595
|
+
if (norm?.templateMessage?.hydratedFourRowTemplate)
|
|
2596
|
+
return extractFromTemplate(norm.templateMessage.hydratedFourRowTemplate);
|
|
2597
|
+
if (norm?.templateMessage?.hydratedTemplate)
|
|
2598
|
+
return extractFromTemplate(norm.templateMessage.hydratedTemplate);
|
|
2599
|
+
if (norm?.templateMessage?.fourRowTemplate)
|
|
2600
|
+
return extractFromTemplate(norm.templateMessage.fourRowTemplate);
|
|
2601
|
+
return norm;
|
|
2443
2602
|
}
|
|
2444
|
-
export async function downloadMediaMessage(msg, type = "buffer") {
|
|
2445
|
-
const inner =
|
|
2446
|
-
const mediaKey = Object.keys(inner || {}).find((k) =>
|
|
2447
|
-
|
|
2603
|
+
export async function downloadMediaMessage(msg, type = "buffer", _options, _ctx) {
|
|
2604
|
+
const inner = extractMessageContent(msg?.message ?? msg);
|
|
2605
|
+
const mediaKey = Object.keys(inner || {}).find((k) => ["imageMessage", "videoMessage", "ptvMessage", "audioMessage", "documentMessage", "stickerMessage", "stickerPackMessage"].includes(k)) ??
|
|
2606
|
+
Object.keys(inner || {}).find((k) => k.endsWith("Message")) ??
|
|
2607
|
+
"imageMessage";
|
|
2608
|
+
const rawType = mediaKey.replace("Message", "");
|
|
2609
|
+
const mediaType = (rawType === "stickerPack" ? "sticker-pack" : rawType);
|
|
2448
2610
|
const stream = await downloadContentFromMessage(inner[mediaKey], mediaType);
|
|
2449
2611
|
return type === "stream" ? stream : await toBuffer(stream);
|
|
2450
2612
|
}
|