@rosyn/whats 0.1.2 → 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 +312 -146
- 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) {
|
|
@@ -807,7 +917,8 @@ export function makeWASocket(config = {}) {
|
|
|
807
917
|
meta.addressingMode ||
|
|
808
918
|
((meta.participants ?? []).some((p) => p.id.endsWith("@lid")) ? "lid" : "pn");
|
|
809
919
|
const groupSender = retryAddrMode === "lid" && meLid ? meLid : meJid;
|
|
810
|
-
const
|
|
920
|
+
const groupKey = `${cachedMsg.jid}:${jidToSignalAddress(groupSender)}`;
|
|
921
|
+
const { distributionBytes } = await signalMgr.getOrCreateGroupDistribution(groupKey);
|
|
811
922
|
payloadToResend = {
|
|
812
923
|
...cachedMsg.message,
|
|
813
924
|
senderKeyDistributionMessage: {
|
|
@@ -827,6 +938,11 @@ export function makeWASocket(config = {}) {
|
|
|
827
938
|
},
|
|
828
939
|
};
|
|
829
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]);
|
|
830
946
|
const paddedRetry = writeRandomPadMax16(proto.Message.encode(payloadToResend));
|
|
831
947
|
const enc = await signalMgr.encryptMessage(jidToSignalAddress(retryPid), paddedRetry);
|
|
832
948
|
const getMediaStanzaMeta = (m) => {
|
|
@@ -850,6 +966,10 @@ export function makeWASocket(config = {}) {
|
|
|
850
966
|
};
|
|
851
967
|
const { stanzaType: retryStanzaType, mediatype: retryMediatype } = getMediaStanzaMeta(cachedMsg.message);
|
|
852
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
|
+
}
|
|
853
973
|
const retryContent = [
|
|
854
974
|
{
|
|
855
975
|
tag: "enc",
|
|
@@ -909,6 +1029,7 @@ export function makeWASocket(config = {}) {
|
|
|
909
1029
|
id: node.attrs.id,
|
|
910
1030
|
to: cachedMsg.jid,
|
|
911
1031
|
...(cachedMsg.jid.endsWith("@g.us") ? { participant: retryPid } : {}),
|
|
1032
|
+
...(retryAddrMode ? { addressing_mode: retryAddrMode } : {}),
|
|
912
1033
|
type: retryStanzaType,
|
|
913
1034
|
},
|
|
914
1035
|
content: retryContent,
|
|
@@ -919,6 +1040,10 @@ export function makeWASocket(config = {}) {
|
|
|
919
1040
|
return;
|
|
920
1041
|
}
|
|
921
1042
|
if (node.tag === "ack") {
|
|
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(() => { });
|
|
1046
|
+
}
|
|
922
1047
|
return;
|
|
923
1048
|
}
|
|
924
1049
|
if (node.tag === "iq" && node.attrs.type === "get" && node.attrs.xmlns === "w:p") {
|
|
@@ -977,19 +1102,14 @@ export function makeWASocket(config = {}) {
|
|
|
977
1102
|
}
|
|
978
1103
|
let decoded = proto.Message.decode(plainBuf);
|
|
979
1104
|
decoded = decoded.deviceSentMessage?.message ?? decoded;
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
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);
|
|
983
1109
|
}
|
|
984
|
-
const hasDisplayContent =
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
decoded.interactiveResponseMessage?.nativeFlowResponseMessage ||
|
|
988
|
-
decoded.ephemeralMessage?.message ||
|
|
989
|
-
decoded.viewOnceMessage?.message ||
|
|
990
|
-
decoded.viewOnceMessageV2?.message ||
|
|
991
|
-
decoded.documentWithCaptionMessage?.message ||
|
|
992
|
-
decoded.editedMessage?.message);
|
|
1110
|
+
const hasDisplayContent = Object.keys(decoded).some((k) => k !== "senderKeyDistributionMessage" &&
|
|
1111
|
+
k !== "fastRatchetKeySenderKeyDistributionMessage" &&
|
|
1112
|
+
k !== "messageContextInfo");
|
|
993
1113
|
if (hasDisplayContent) {
|
|
994
1114
|
if (isGroup &&
|
|
995
1115
|
participant?.endsWith("@lid") &&
|
|
@@ -1321,10 +1441,10 @@ export function makeWASocket(config = {}) {
|
|
|
1321
1441
|
? b.buttonParamsJson
|
|
1322
1442
|
: JSON.stringify(b.buttonParamsJson ?? {}),
|
|
1323
1443
|
})),
|
|
1324
|
-
messageParamsJson:
|
|
1325
|
-
? typeof
|
|
1326
|
-
?
|
|
1327
|
-
: JSON.stringify(
|
|
1444
|
+
messageParamsJson: card.messageParams
|
|
1445
|
+
? typeof card.messageParams === "string"
|
|
1446
|
+
? card.messageParams
|
|
1447
|
+
: JSON.stringify(card.messageParams)
|
|
1328
1448
|
: "{}",
|
|
1329
1449
|
messageVersion: 1,
|
|
1330
1450
|
},
|
|
@@ -1658,114 +1778,92 @@ export function makeWASocket(config = {}) {
|
|
|
1658
1778
|
const text = typeof waMsg.conversation === "string"
|
|
1659
1779
|
? waMsg.conversation
|
|
1660
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
|
+
}
|
|
1661
1788
|
const padded = writeRandomPadMax16(proto.Message.encode(waMsg));
|
|
1662
1789
|
const meJid = authCreds.me?.id ?? sock.user?.id ?? "0@s.whatsapp.net";
|
|
1663
1790
|
const msgId = generateMessageIDV2(meJid);
|
|
1664
1791
|
sentMessages.set(msgId, { jid, message: waMsg });
|
|
1665
1792
|
setTimeout(() => sentMessages.delete(msgId), 5 * 60_000).unref();
|
|
1666
1793
|
const getDevices = async (userJids) => {
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
const
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
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);
|
|
1700
1856
|
}
|
|
1701
1857
|
}
|
|
1702
1858
|
}
|
|
1703
1859
|
}
|
|
1704
1860
|
}
|
|
1705
|
-
|
|
1706
|
-
return out;
|
|
1707
|
-
}
|
|
1708
|
-
catch {
|
|
1709
|
-
}
|
|
1710
|
-
return userJids.map((u) => jidNormalizedUser(u));
|
|
1711
|
-
};
|
|
1712
|
-
const ensureSessions = async (devJids) => {
|
|
1713
|
-
const missing = [];
|
|
1714
|
-
for (const dj of devJids) {
|
|
1715
|
-
if (!(await signalMgr.hasSession(jidToSignalAddress(dj)))) {
|
|
1716
|
-
missing.push(dj);
|
|
1861
|
+
catch {
|
|
1717
1862
|
}
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
tag: "iq",
|
|
1723
|
-
attrs: { to: "s.whatsapp.net", type: "get", id: nextTag(), xmlns: "encrypt" },
|
|
1724
|
-
content: [
|
|
1725
|
-
{
|
|
1726
|
-
tag: "key",
|
|
1727
|
-
attrs: {},
|
|
1728
|
-
content: missing.map((dj) => ({ tag: "user", attrs: { jid: dj } })),
|
|
1729
|
-
},
|
|
1730
|
-
],
|
|
1731
|
-
});
|
|
1732
|
-
const list = Array.isArray(res.content) ? res.content.find((c) => c.tag === "list") : undefined;
|
|
1733
|
-
if (!Array.isArray(list?.content))
|
|
1734
|
-
return;
|
|
1735
|
-
for (const u of list.content) {
|
|
1736
|
-
if (u.tag !== "user" || !u.attrs?.jid || !Array.isArray(u.content))
|
|
1737
|
-
continue;
|
|
1738
|
-
const regNode = u.content.find((c) => c.tag === "registration");
|
|
1739
|
-
const identNode = u.content.find((c) => c.tag === "identity");
|
|
1740
|
-
const skeyNode = u.content.find((c) => c.tag === "skey");
|
|
1741
|
-
const keyNode = u.content.find((c) => c.tag === "key");
|
|
1742
|
-
if (!(regNode?.content instanceof Uint8Array) || !(identNode?.content instanceof Uint8Array) || !Array.isArray(skeyNode?.content)) {
|
|
1743
|
-
continue;
|
|
1744
|
-
}
|
|
1745
|
-
const skeyIdBuf = skeyNode.content.find((c) => c.tag === "id")?.content;
|
|
1746
|
-
const skeyValBuf = skeyNode.content.find((c) => c.tag === "value")?.content;
|
|
1747
|
-
if (!(skeyIdBuf instanceof Uint8Array) || !(skeyValBuf instanceof Uint8Array))
|
|
1748
|
-
continue;
|
|
1749
|
-
const regId = Buffer.from(regNode.content).readUInt32BE(0);
|
|
1750
|
-
const skeyId = Buffer.from(skeyIdBuf).readUIntBE(0, skeyIdBuf.length);
|
|
1751
|
-
let preKey;
|
|
1752
|
-
if (Array.isArray(keyNode?.content)) {
|
|
1753
|
-
const pkIdBuf = keyNode.content.find((c) => c.tag === "id")?.content;
|
|
1754
|
-
const pkValBuf = keyNode.content.find((c) => c.tag === "value")?.content;
|
|
1755
|
-
if (pkIdBuf instanceof Uint8Array && pkValBuf instanceof Uint8Array) {
|
|
1756
|
-
preKey = {
|
|
1757
|
-
keyId: Buffer.from(pkIdBuf).readUIntBE(0, pkIdBuf.length),
|
|
1758
|
-
publicKey: pkValBuf,
|
|
1759
|
-
};
|
|
1760
|
-
}
|
|
1761
|
-
}
|
|
1762
|
-
await signalMgr.initOutgoingSession(jidToSignalAddress(u.attrs.jid), {
|
|
1763
|
-
registrationId: regId,
|
|
1764
|
-
identityKey: identNode.content,
|
|
1765
|
-
signedPreKey: { keyId: skeyId, publicKey: skeyValBuf },
|
|
1766
|
-
preKey,
|
|
1767
|
-
});
|
|
1768
|
-
}
|
|
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));
|
|
1769
1867
|
};
|
|
1770
1868
|
const buildDeviceIdentityNode = () => {
|
|
1771
1869
|
if (!authCreds.account)
|
|
@@ -1779,25 +1877,33 @@ export function makeWASocket(config = {}) {
|
|
|
1779
1877
|
});
|
|
1780
1878
|
return [{ tag: "device-identity", attrs: {}, content: enc }];
|
|
1781
1879
|
};
|
|
1782
|
-
const computePhash = (devs) => {
|
|
1783
|
-
const sorted = [...devs].sort();
|
|
1784
|
-
const hash = createHash("sha256").update(sorted.join("")).digest("base64");
|
|
1785
|
-
return "2:" + hash.slice(0, 6);
|
|
1786
|
-
};
|
|
1787
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);
|
|
1788
1894
|
transport.sendEncrypted(encodeWABinaryNode({
|
|
1789
1895
|
tag: "message",
|
|
1790
1896
|
attrs: {
|
|
1791
|
-
id:
|
|
1897
|
+
id: nlMsgId,
|
|
1792
1898
|
to: jid,
|
|
1793
1899
|
type: stanzaType,
|
|
1794
|
-
...(
|
|
1900
|
+
...(nlEditAttr ? { edit: nlEditAttr } : {}),
|
|
1795
1901
|
},
|
|
1796
1902
|
content: [
|
|
1797
1903
|
{
|
|
1798
1904
|
tag: "plaintext",
|
|
1799
|
-
attrs: encMediatype ? { mediatype: encMediatype } : {},
|
|
1800
|
-
content:
|
|
1905
|
+
attrs: !isNlDelete && encMediatype ? { mediatype: encMediatype } : {},
|
|
1906
|
+
content: nlPlaintext,
|
|
1801
1907
|
},
|
|
1802
1908
|
],
|
|
1803
1909
|
}));
|
|
@@ -1809,7 +1915,16 @@ export function makeWASocket(config = {}) {
|
|
|
1809
1915
|
.filter(Boolean);
|
|
1810
1916
|
const meLid = authCreds.me?.lid ?? sock.user?.lid;
|
|
1811
1917
|
const rawDevices = await getDevices(participants.length ? participants : [meJid]);
|
|
1812
|
-
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
|
+
});
|
|
1813
1928
|
const addrMode = meta.addressingMode || (participants.some((p) => p.endsWith("@lid")) ? "lid" : "pn");
|
|
1814
1929
|
const groupSender = addrMode === "lid" && meLid ? meLid : meJid;
|
|
1815
1930
|
const groupKey = `${jid}:${jidToSignalAddress(groupSender)}`;
|
|
@@ -1817,7 +1932,7 @@ export function makeWASocket(config = {}) {
|
|
|
1817
1932
|
const skmsgBytes = await signalMgr.encryptGroupMessage(groupKey, padded);
|
|
1818
1933
|
let sentTo = senderKeyMemory.get(jid);
|
|
1819
1934
|
if (!sentTo) {
|
|
1820
|
-
const savedMem = (await getKey("sender-key-memory", jid));
|
|
1935
|
+
const savedMem = (await getKey("sender-key-memory-v3", jid));
|
|
1821
1936
|
sentTo = new Set(Array.isArray(savedMem) ? savedMem : []);
|
|
1822
1937
|
senderKeyMemory.set(jid, sentTo);
|
|
1823
1938
|
}
|
|
@@ -1847,7 +1962,7 @@ export function makeWASocket(config = {}) {
|
|
|
1847
1962
|
catch {
|
|
1848
1963
|
}
|
|
1849
1964
|
}
|
|
1850
|
-
await setKey("sender-key-memory", jid, Array.from(sentTo));
|
|
1965
|
+
await setKey("sender-key-memory-v3", jid, Array.from(sentTo));
|
|
1851
1966
|
}
|
|
1852
1967
|
transport.sendEncrypted(encodeWABinaryNode({
|
|
1853
1968
|
tag: "message",
|
|
@@ -1856,15 +1971,18 @@ export function makeWASocket(config = {}) {
|
|
|
1856
1971
|
to: jid,
|
|
1857
1972
|
type: stanzaType,
|
|
1858
1973
|
addressing_mode: addrMode,
|
|
1974
|
+
...(meta.ephemeralDuration && meta.ephemeralDuration > 0
|
|
1975
|
+
? { expiration: String(meta.ephemeralDuration) }
|
|
1976
|
+
: {}),
|
|
1859
1977
|
...(deleteEditAttr ? { edit: deleteEditAttr } : {}),
|
|
1860
1978
|
},
|
|
1861
1979
|
content: [
|
|
1862
|
-
...(participantNodes.length ? [{ tag: "participants", attrs: {}, content: participantNodes }] : []),
|
|
1863
1980
|
{
|
|
1864
1981
|
tag: "enc",
|
|
1865
1982
|
attrs: { v: "2", type: "skmsg", ...(encMediatype ? { mediatype: encMediatype } : {}) },
|
|
1866
1983
|
content: skmsgBytes,
|
|
1867
1984
|
},
|
|
1985
|
+
...(participantNodes.length ? [{ tag: "participants", attrs: {}, content: participantNodes }] : []),
|
|
1868
1986
|
...(hasPkmsg ? buildDeviceIdentityNode() : []),
|
|
1869
1987
|
...buildButtonAddonNodes(),
|
|
1870
1988
|
],
|
|
@@ -2080,9 +2198,13 @@ export function makeWASocket(config = {}) {
|
|
|
2080
2198
|
const parseGroupNode = (grp) => {
|
|
2081
2199
|
const subject = grp.attrs?.subject ?? "";
|
|
2082
2200
|
const addressingMode = grp.attrs?.addressing_mode;
|
|
2201
|
+
let ephemeralDuration;
|
|
2083
2202
|
const participants = [];
|
|
2084
2203
|
if (Array.isArray(grp.content)) {
|
|
2085
2204
|
for (const c of grp.content) {
|
|
2205
|
+
if (c.tag === "ephemeral" && c.attrs?.expiration) {
|
|
2206
|
+
ephemeralDuration = Number(c.attrs.expiration) || undefined;
|
|
2207
|
+
}
|
|
2086
2208
|
if (c.tag === "participant" && c.attrs?.jid) {
|
|
2087
2209
|
rememberLidPn(c.attrs.jid, c.attrs.phone_number);
|
|
2088
2210
|
const pnId = c.attrs.phone_number ? jidNormalizedUser(c.attrs.phone_number) : resolvePnJid(c.attrs.jid);
|
|
@@ -2096,7 +2218,7 @@ export function makeWASocket(config = {}) {
|
|
|
2096
2218
|
}
|
|
2097
2219
|
}
|
|
2098
2220
|
}
|
|
2099
|
-
return { subject, addressingMode, participants };
|
|
2221
|
+
return { subject, addressingMode, ...(ephemeralDuration ? { ephemeralDuration } : {}), participants };
|
|
2100
2222
|
};
|
|
2101
2223
|
try {
|
|
2102
2224
|
const res = await sendQuery({
|
|
@@ -2420,27 +2542,71 @@ export function makeWASocket(config = {}) {
|
|
|
2420
2542
|
export function normalizeMessageContent(content) {
|
|
2421
2543
|
if (!content || typeof content !== "object")
|
|
2422
2544
|
return content;
|
|
2423
|
-
|
|
2545
|
+
const wrapperKeys = [
|
|
2424
2546
|
"ephemeralMessage",
|
|
2425
2547
|
"viewOnceMessage",
|
|
2426
2548
|
"viewOnceMessageV2",
|
|
2427
2549
|
"viewOnceMessageV2Extension",
|
|
2428
2550
|
"documentWithCaptionMessage",
|
|
2429
2551
|
"editedMessage",
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
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
|
+
}
|
|
2433
2574
|
}
|
|
2575
|
+
if (!unwrapped)
|
|
2576
|
+
break;
|
|
2434
2577
|
}
|
|
2435
|
-
return
|
|
2578
|
+
return cur;
|
|
2436
2579
|
}
|
|
2437
2580
|
export function extractMessageContent(content) {
|
|
2438
|
-
|
|
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;
|
|
2439
2602
|
}
|
|
2440
|
-
export async function downloadMediaMessage(msg, type = "buffer") {
|
|
2441
|
-
const inner =
|
|
2442
|
-
const mediaKey = Object.keys(inner || {}).find((k) =>
|
|
2443
|
-
|
|
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);
|
|
2444
2610
|
const stream = await downloadContentFromMessage(inner[mediaKey], mediaType);
|
|
2445
2611
|
return type === "stream" ? stream : await toBuffer(stream);
|
|
2446
2612
|
}
|