@x4r1s3/baileys 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +1262 -2
- package/SECURITY.md +47 -0
- package/WAProto/V +1 -0
- package/WAProto/WAProto.proto +8892 -0
- package/WAProto/index.d.ts +104128 -0
- package/WAProto/index.js +334920 -0
- package/assets/rolling-circle.svg +133 -0
- package/assets/x4r1s3-banner.svg +32 -0
- package/engine-requirements.js +27 -0
- package/lib/Builders/A2UI.js +245 -0
- package/lib/Builders/AIRich.js +179 -0
- package/lib/Builders/Button.js +108 -0
- package/lib/Builders/CFC.js +284 -0
- package/lib/Builders/Carousel.js +81 -0
- package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
- package/lib/Call/audio-feeder.mjs +179 -0
- package/lib/Call/audio-recorder.mjs +250 -0
- package/lib/Call/fetch-wasm-resources.mjs +104 -0
- package/lib/Call/index.mjs +680 -0
- package/lib/Call/relay-transport.mjs +563 -0
- package/lib/Call/signaling.mjs +641 -0
- package/lib/Call/types.mjs +17 -0
- package/lib/Call/video-feeder.mjs +94 -0
- package/lib/Call/wasm-engine.mjs +1372 -0
- package/lib/Call/worker-bootstrap.mjs +1042 -0
- package/lib/Defaults/ban-checker.js +56 -0
- package/lib/Defaults/index.js +170 -0
- package/lib/Signal/Group/ciphertext-message.js +23 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +93 -0
- package/lib/Signal/Group/index.js +23 -0
- package/lib/Signal/Group/keyhelper.js +29 -0
- package/lib/Signal/Group/sender-chain-key.js +37 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
- package/lib/Signal/Group/sender-key-message.js +77 -0
- package/lib/Signal/Group/sender-key-name.js +59 -0
- package/lib/Signal/Group/sender-key-record.js +52 -0
- package/lib/Signal/Group/sender-key-state.js +95 -0
- package/lib/Signal/Group/sender-message-key.js +37 -0
- package/lib/Signal/libsignal.js +440 -0
- package/lib/Signal/lid-mapping.js +292 -0
- package/lib/Socket/Client/index.js +14 -0
- package/lib/Socket/Client/types.js +22 -0
- package/lib/Socket/Client/websocket.js +65 -0
- package/lib/Socket/aigroups.js +327 -0
- package/lib/Socket/ban-checker.js +165 -0
- package/lib/Socket/business.js +419 -0
- package/lib/Socket/chats.js +1921 -0
- package/lib/Socket/communities.js +443 -0
- package/lib/Socket/graphql.js +911 -0
- package/lib/Socket/groups.js +561 -0
- package/lib/Socket/index.js +49 -0
- package/lib/Socket/interactive-handler.js +612 -0
- package/lib/Socket/interop.js +575 -0
- package/lib/Socket/managed-account.js +220 -0
- package/lib/Socket/messages-recv.js +2191 -0
- package/lib/Socket/messages-send.js +4104 -0
- package/lib/Socket/mex.js +53 -0
- package/lib/Socket/newsletter.js +291 -0
- package/lib/Socket/privacy.js +458 -0
- package/lib/Socket/registration.js +440 -0
- package/lib/Socket/socket.js +1029 -0
- package/lib/Socket/status-actions.js +287 -0
- package/lib/Socket/username.js +241 -0
- package/lib/Store/index.js +16 -0
- package/lib/Store/make-cache-manager-store.js +101 -0
- package/lib/Store/make-in-memory-store.js +432 -0
- package/lib/Store/make-ordered-dictionary.js +90 -0
- package/lib/Store/object-repository.js +35 -0
- package/lib/Types/Auth.js +13 -0
- package/lib/Types/Bussines.js +13 -0
- package/lib/Types/Call.js +13 -0
- package/lib/Types/Chat.js +19 -0
- package/lib/Types/Contact.js +13 -0
- package/lib/Types/Events.js +13 -0
- package/lib/Types/GroupMetadata.js +13 -0
- package/lib/Types/Label.js +36 -0
- package/lib/Types/LabelAssociation.js +18 -0
- package/lib/Types/Message.js +30 -0
- package/lib/Types/Mex.js +53 -0
- package/lib/Types/Product.js +13 -0
- package/lib/Types/RichType.js +34 -0
- package/lib/Types/Signal.js +13 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +60 -0
- package/lib/Types/USync.js +13 -0
- package/lib/Types/index.js +37 -0
- package/lib/Utils/auth-utils.js +313 -0
- package/lib/Utils/ban-checker.js +48 -0
- package/lib/Utils/bot-capabilities.js +96 -0
- package/lib/Utils/bot-planning-replay.js +177 -0
- package/lib/Utils/browser-utils.js +40 -0
- package/lib/Utils/business.js +242 -0
- package/lib/Utils/c2582.js +147 -0
- package/lib/Utils/chat-utils.js +905 -0
- package/lib/Utils/companion-reg-client-utils.js +44 -0
- package/lib/Utils/consumer-application.js +114 -0
- package/lib/Utils/crypto.js +129 -0
- package/lib/Utils/decode-wa-message.js +496 -0
- package/lib/Utils/direct-chat-theme.js +164 -0
- package/lib/Utils/event-buffer.js +601 -0
- package/lib/Utils/experimental-proto.js +206 -0
- package/lib/Utils/games/miniapp.js +17 -0
- package/lib/Utils/games/slot-machine.js +270 -0
- package/lib/Utils/games/website-preview.js +306 -0
- package/lib/Utils/generics.js +402 -0
- package/lib/Utils/group-history.js +68 -0
- package/lib/Utils/group-wallpaper.js +168 -0
- package/lib/Utils/history.js +162 -0
- package/lib/Utils/identity-change-handler.js +60 -0
- package/lib/Utils/index.js +61 -0
- package/lib/Utils/jid-display-normalization.js +221 -0
- package/lib/Utils/link-preview.js +96 -0
- package/lib/Utils/logger.js +14 -0
- package/lib/Utils/lt-hash.js +19 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/message-composer.js +709 -0
- package/lib/Utils/message-inspect.js +79 -0
- package/lib/Utils/message-retry-manager.js +253 -0
- package/lib/Utils/message-security.js +167 -0
- package/lib/Utils/messages-media.js +1027 -0
- package/lib/Utils/messages.js +2467 -0
- package/lib/Utils/meta-ai-msmsg.js +215 -0
- package/lib/Utils/meta-compositing.js +239 -0
- package/lib/Utils/native-flow.js +158 -0
- package/lib/Utils/newsletter-live-update-utils.js +74 -0
- package/lib/Utils/newsletter-status-utils.js +152 -0
- package/lib/Utils/noise-handler.js +212 -0
- package/lib/Utils/offline-node-processor.js +51 -0
- package/lib/Utils/poll-manager.js +206 -0
- package/lib/Utils/portable-compat.js +12 -0
- package/lib/Utils/pre-key-manager.js +117 -0
- package/lib/Utils/process-message.js +610 -0
- package/lib/Utils/reporting-utils.js +269 -0
- package/lib/Utils/rich-message-utils.js +2841 -0
- package/lib/Utils/scheduled-message.js +107 -0
- package/lib/Utils/signal.js +212 -0
- package/lib/Utils/source-grep.js +57 -0
- package/lib/Utils/stanza-ack.js +49 -0
- package/lib/Utils/sticker-annotation.js +87 -0
- package/lib/Utils/sync-action-utils.js +59 -0
- package/lib/Utils/tc-token-utils.js +174 -0
- package/lib/Utils/use-multi-file-auth-state.js +132 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +220 -0
- package/lib/Utils/view-once-utils.js +218 -0
- package/lib/Utils/voip-rekey.js +32 -0
- package/lib/Utils/website-preview.js +27 -0
- package/lib/Utils/welcome-flow.js +228 -0
- package/lib/Utils/x4r1s3.js +144 -0
- package/lib/WABinary/constants.js +1475 -0
- package/lib/WABinary/decode.js +273 -0
- package/lib/WABinary/encode.js +231 -0
- package/lib/WABinary/generic-utils.js +242 -0
- package/lib/WABinary/index.js +17 -0
- package/lib/WABinary/jid-utils.js +107 -0
- package/lib/WABinary/types.js +13 -0
- package/lib/WAM/BinaryInfo.js +21 -0
- package/lib/WAM/constants.js +22864 -0
- package/lib/WAM/encode.js +161 -0
- package/lib/WAM/index.js +15 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
- package/lib/WAUSync/Protocols/index.js +24 -0
- package/lib/WAUSync/USyncQuery.js +213 -0
- package/lib/WAUSync/USyncUser.js +66 -0
- package/lib/WAUSync/index.js +15 -0
- package/lib/antiban.cjs +4726 -0
- package/lib/index.d.ts +1285 -0
- package/lib/index.js +30 -0
- package/package.json +125 -4
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Utils/welcome-flow.js
|
|
5
|
+
* About: Welcome flow module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: createWelcomeFlow
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Lia@Changes [WIP]
|
|
15
|
+
* Welcome Flow — Meta Business-style startup template.
|
|
16
|
+
*
|
|
17
|
+
* Fires automatically on first message from any new contact.
|
|
18
|
+
* Sends a fully configurable greeting with interactive FAQ buttons,
|
|
19
|
+
* exactly like Meta Business account welcome templates.
|
|
20
|
+
*
|
|
21
|
+
* Tracks seen JIDs in memory + optional persistent JSON file so the
|
|
22
|
+
* greeting fires ONCE per contact — never on repeat messages.
|
|
23
|
+
*
|
|
24
|
+
* Usage:
|
|
25
|
+
* const welcome = createWelcomeFlow(sock, config)
|
|
26
|
+
* welcome.listen() // start listening
|
|
27
|
+
* welcome.stop() // stop listening
|
|
28
|
+
* welcome.reset(jid) // force re-greet a contact
|
|
29
|
+
* welcome.resetAll() // clear all seen contacts
|
|
30
|
+
*
|
|
31
|
+
* If you use or copy this code, please credit @X4R1S3/bailey.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
|
35
|
+
import { isJidGroup, isJidNewsletter, isJidBroadcast, jidNormalizedUser } from '../WABinary/index.js';
|
|
36
|
+
import { delay } from './generics.js';
|
|
37
|
+
|
|
38
|
+
// ─── Defaults ────────────────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
const DEFAULT_CONFIG = {
|
|
41
|
+
greeting: '👋 Welcome! How can I help you today?',
|
|
42
|
+
footer: 'Powered by @X4R1S3/bailey',
|
|
43
|
+
buttonText: '📋 Choose an option',
|
|
44
|
+
faqs: [
|
|
45
|
+
{ id: 'faq_1', title: '📦 Track my order', description: 'Check order status' },
|
|
46
|
+
{ id: 'faq_2', title: '💳 Billing & payments', description: 'Payment issues & invoices' },
|
|
47
|
+
{ id: 'faq_3', title: '🛠️ Technical support', description: 'Get help with a problem' },
|
|
48
|
+
{ id: 'faq_4', title: '📞 Talk to a human', description: 'Connect with support staff' },
|
|
49
|
+
],
|
|
50
|
+
sectionTitle: 'How can we help?',
|
|
51
|
+
typingDelayMs: 1200, // simulate typing before greeting appears
|
|
52
|
+
persistPath: null, // set to a file path to persist seen JIDs across restarts
|
|
53
|
+
ignoreGroups: true, // don't fire in group chats
|
|
54
|
+
ignoreNewsletter: true, // don't fire for newsletter messages
|
|
55
|
+
ignoreBroadcast: true, // don't fire for broadcast messages
|
|
56
|
+
onGreet: null, // async (jid, message) => {} — called after greeting sent
|
|
57
|
+
onFaqReply: null, // async (jid, faqId, message) => {} — called when user picks a FAQ
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// ─── Persistent seen-JID store ───────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
const createSeenStore = (persistPath) => {
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
|
|
65
|
+
// Load from file if path given and file exists
|
|
66
|
+
if (persistPath && existsSync(persistPath)) {
|
|
67
|
+
try {
|
|
68
|
+
const data = JSON.parse(readFileSync(persistPath, 'utf8'));
|
|
69
|
+
if (Array.isArray(data)) data.forEach(jid => seen.add(jid));
|
|
70
|
+
} catch (_) {}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const save = () => {
|
|
74
|
+
if (!persistPath) return;
|
|
75
|
+
try {
|
|
76
|
+
writeFileSync(persistPath, JSON.stringify([...seen]), 'utf8');
|
|
77
|
+
} catch (_) {}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
has: (jid) => seen.has(jid),
|
|
82
|
+
add: (jid) => { seen.add(jid); save(); },
|
|
83
|
+
delete: (jid) => { seen.delete(jid); save(); },
|
|
84
|
+
clear: () => { seen.clear(); save(); }
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// ─── Build the welcome interactive list message ───────────────────────────────
|
|
89
|
+
|
|
90
|
+
const buildWelcomeMessage = (config, quotedMsg = null) => {
|
|
91
|
+
const { greeting, footer, buttonText, faqs, sectionTitle } = config;
|
|
92
|
+
|
|
93
|
+
const content = {
|
|
94
|
+
text: greeting,
|
|
95
|
+
footer,
|
|
96
|
+
buttonText,
|
|
97
|
+
sections: [
|
|
98
|
+
{
|
|
99
|
+
title: sectionTitle,
|
|
100
|
+
rows: faqs.map(faq => ({
|
|
101
|
+
title: faq.title,
|
|
102
|
+
description: faq.description || '',
|
|
103
|
+
rowId: faq.id
|
|
104
|
+
}))
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
if (quotedMsg) {
|
|
110
|
+
content.quoted = quotedMsg;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return content;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// ─── Main factory ─────────────────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* createWelcomeFlow — attach a Meta Business-style welcome template to a socket.
|
|
120
|
+
*
|
|
121
|
+
* @param {object} sock - Baileys socket
|
|
122
|
+
* @param {object} config - Partial config — merges with DEFAULT_CONFIG
|
|
123
|
+
* @returns {{ listen, stop, reset, resetAll }}
|
|
124
|
+
*/
|
|
125
|
+
export const createWelcomeFlow = (sock, config = {}) => {
|
|
126
|
+
const cfg = { ...DEFAULT_CONFIG, ...config };
|
|
127
|
+
const seen = createSeenStore(cfg.persistPath);
|
|
128
|
+
|
|
129
|
+
// ── FAQ reply handler — called when user selects a list option ────────────
|
|
130
|
+
const handleFaqReply = async (msg) => {
|
|
131
|
+
if (typeof cfg.onFaqReply !== 'function') return;
|
|
132
|
+
const reply = msg.message?.listResponseMessage;
|
|
133
|
+
if (!reply) return;
|
|
134
|
+
const faqId = reply.singleSelectReply?.selectedRowId;
|
|
135
|
+
const jid = jidNormalizedUser(msg.key.remoteJid);
|
|
136
|
+
if (faqId) {
|
|
137
|
+
await cfg.onFaqReply(jid, faqId, msg);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// ── First-message handler ─────────────────────────────────────────────────
|
|
142
|
+
const handleMessage = async ({ messages, type }) => {
|
|
143
|
+
if (type !== 'notify') return;
|
|
144
|
+
|
|
145
|
+
for (const msg of messages) {
|
|
146
|
+
try {
|
|
147
|
+
const jid = msg.key?.remoteJid;
|
|
148
|
+
if (!jid || msg.key?.fromMe) continue;
|
|
149
|
+
|
|
150
|
+
// Skip ignored JID types
|
|
151
|
+
if (cfg.ignoreGroups && isJidGroup(jid)) continue;
|
|
152
|
+
if (cfg.ignoreNewsletter && isJidNewsletter(jid)) continue;
|
|
153
|
+
if (cfg.ignoreBroadcast && isJidBroadcast(jid)) continue;
|
|
154
|
+
|
|
155
|
+
const normalizedJid = jidNormalizedUser(jid);
|
|
156
|
+
|
|
157
|
+
// Check if it's a FAQ reply first
|
|
158
|
+
if (msg.message?.listResponseMessage) {
|
|
159
|
+
await handleFaqReply(msg);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Already greeted this contact — skip
|
|
164
|
+
if (seen.has(normalizedJid)) continue;
|
|
165
|
+
|
|
166
|
+
// Mark as seen immediately to prevent race conditions
|
|
167
|
+
seen.add(normalizedJid);
|
|
168
|
+
|
|
169
|
+
// Simulate typing before the greeting
|
|
170
|
+
if (cfg.typingDelayMs > 0) {
|
|
171
|
+
await sock.sendPresenceUpdate('composing', jid);
|
|
172
|
+
await delay(cfg.typingDelayMs);
|
|
173
|
+
await sock.sendPresenceUpdate('paused', jid);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Send the configured welcome message.
|
|
177
|
+
await sock.sendMessage(jid, buildWelcomeMessage(cfg, msg));
|
|
178
|
+
|
|
179
|
+
// Fire onGreet callback if provided
|
|
180
|
+
if (typeof cfg.onGreet === 'function') {
|
|
181
|
+
await cfg.onGreet(normalizedJid, msg).catch(() => {});
|
|
182
|
+
}
|
|
183
|
+
} catch (_) {}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// ── Public API ────────────────────────────────────────────────────────────
|
|
188
|
+
return {
|
|
189
|
+
/**
|
|
190
|
+
* Start listening for new contacts.
|
|
191
|
+
*/
|
|
192
|
+
listen() {
|
|
193
|
+
sock.ev.on('messages.upsert', handleMessage);
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Stop listening.
|
|
198
|
+
*/
|
|
199
|
+
stop() {
|
|
200
|
+
sock.ev.off('messages.upsert', handleMessage);
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Force re-greet a specific contact next time they message.
|
|
205
|
+
* @param {string} jid
|
|
206
|
+
*/
|
|
207
|
+
reset(jid) {
|
|
208
|
+
seen.delete(jidNormalizedUser(jid));
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Clear all seen contacts — everyone gets re-greeted.
|
|
213
|
+
*/
|
|
214
|
+
resetAll() {
|
|
215
|
+
seen.clear();
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Check if a contact has already been greeted.
|
|
220
|
+
* @param {string} jid
|
|
221
|
+
* @returns {boolean}
|
|
222
|
+
*/
|
|
223
|
+
hasGreeted(jid) {
|
|
224
|
+
return seen.has(jidNormalizedUser(jid));
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import { jidDecode } from '../WABinary/jid-utils.js';
|
|
3
|
+
|
|
4
|
+
const supportedDestinationServers = new Set([
|
|
5
|
+
'c.us',
|
|
6
|
+
's.whatsapp.net',
|
|
7
|
+
'lid',
|
|
8
|
+
'hosted',
|
|
9
|
+
'hosted.lid',
|
|
10
|
+
'g.us'
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
const getTargetId = target => {
|
|
14
|
+
const targetId = typeof target === 'string'
|
|
15
|
+
? target
|
|
16
|
+
: (target?.id || target?.stanzaId);
|
|
17
|
+
|
|
18
|
+
if (targetId === undefined || targetId === null || targetId === '') {
|
|
19
|
+
throw new Error('Missing message id to vanish');
|
|
20
|
+
}
|
|
21
|
+
if (typeof targetId !== 'string' || targetId.length > 256 || targetId.trim() !== targetId || /[\s\u0000-\u001f\u007f]/u.test(targetId)) {
|
|
22
|
+
throw new Error('Invalid target message id');
|
|
23
|
+
}
|
|
24
|
+
return targetId;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const isValidDestinationJid = jid => {
|
|
28
|
+
if (typeof jid !== 'string' || jid.length > 256 || jid.trim() !== jid || /[\s\u0000-\u001f\u007f]/u.test(jid)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const decoded = jidDecode(jid);
|
|
33
|
+
return Boolean(
|
|
34
|
+
decoded?.user &&
|
|
35
|
+
/^[A-Za-z0-9][A-Za-z0-9._:-]*$/u.test(jid.slice(0, jid.indexOf('@'))) &&
|
|
36
|
+
supportedDestinationServers.has(decoded.server)
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const isMessageId = value => typeof value === 'string' && value.length > 0 && value.length <= 256 && value.trim() === value && !/[\s\u0000-\u001f\u007f]/u.test(value);
|
|
41
|
+
|
|
42
|
+
const groupStatusMessage = () => ({
|
|
43
|
+
groupStatusMessageV2: {
|
|
44
|
+
message: {
|
|
45
|
+
extendedTextMessage: {
|
|
46
|
+
text: '',
|
|
47
|
+
contextInfo: {
|
|
48
|
+
isGroupStatus: true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Relay the experimental X4R1S3 protocol sequence for a target message.
|
|
57
|
+
* The connection's network methods are called directly, making the helper
|
|
58
|
+
* straightforward to exercise with mocks and safe to use with a socket.
|
|
59
|
+
*/
|
|
60
|
+
export const X4R1S3 = async (conn, jid, target) => {
|
|
61
|
+
if (!isValidDestinationJid(jid)) {
|
|
62
|
+
throw new Error('Invalid destination JID for X4R1S3');
|
|
63
|
+
}
|
|
64
|
+
const targetId = getTargetId(target);
|
|
65
|
+
if (!conn || typeof conn.relayMessage !== 'function' || typeof conn.sendMessage !== 'function') {
|
|
66
|
+
throw new TypeError('X4R1S3 requires a connection with relayMessage and sendMessage methods');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const temporaryIds = [];
|
|
70
|
+
let relayError;
|
|
71
|
+
let relaysSucceeded = false;
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const tempId = await conn.relayMessage(jid, groupStatusMessage(), {});
|
|
75
|
+
if (!isMessageId(tempId)) {
|
|
76
|
+
throw new Error('X4R1S3 first relay did not return a valid temporary message id');
|
|
77
|
+
}
|
|
78
|
+
temporaryIds.push(tempId);
|
|
79
|
+
|
|
80
|
+
const tempId2 = await conn.relayMessage(jid, {
|
|
81
|
+
protocolMessage: {
|
|
82
|
+
key: {
|
|
83
|
+
jid,
|
|
84
|
+
fromMe: true,
|
|
85
|
+
id: tempId
|
|
86
|
+
},
|
|
87
|
+
type: proto.Message.ProtocolMessage.Type.MESSAGE_EDIT,
|
|
88
|
+
editedMessage: groupStatusMessage()
|
|
89
|
+
}
|
|
90
|
+
}, {
|
|
91
|
+
messageId: targetId
|
|
92
|
+
});
|
|
93
|
+
if (!isMessageId(tempId2)) {
|
|
94
|
+
throw new Error('X4R1S3 second relay did not return a valid temporary message id');
|
|
95
|
+
}
|
|
96
|
+
temporaryIds.push(tempId2);
|
|
97
|
+
relaysSucceeded = true;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
relayError = error;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (relaysSucceeded) {
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const cleanupResults = await Promise.allSettled(temporaryIds.map(id => Promise.resolve().then(() => conn.sendMessage(jid, {
|
|
108
|
+
delete: {
|
|
109
|
+
remoteJid: jid,
|
|
110
|
+
id,
|
|
111
|
+
fromMe: true
|
|
112
|
+
}
|
|
113
|
+
}))));
|
|
114
|
+
const cleanupErrors = cleanupResults
|
|
115
|
+
.filter(result => result.status === 'rejected')
|
|
116
|
+
.map(result => result.reason);
|
|
117
|
+
|
|
118
|
+
if (relayError) {
|
|
119
|
+
if (cleanupErrors.length > 0) {
|
|
120
|
+
throw new AggregateError(
|
|
121
|
+
[relayError, ...cleanupErrors],
|
|
122
|
+
'X4R1S3 relay failed and temporary-message cleanup also failed',
|
|
123
|
+
{ cause: relayError }
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
throw relayError;
|
|
127
|
+
}
|
|
128
|
+
if (cleanupErrors.length > 0) {
|
|
129
|
+
throw new AggregateError(
|
|
130
|
+
cleanupErrors,
|
|
131
|
+
'X4R1S3 protocol relays succeeded but temporary-message cleanup failed'
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
key: {
|
|
137
|
+
remoteJid: jid,
|
|
138
|
+
id: targetId
|
|
139
|
+
},
|
|
140
|
+
status: 'vanished'
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export default X4R1S3;
|