@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,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Socket/aigroups.js
|
|
5
|
+
* About: Aigroups module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: META_AI_BOT_JID, isMetaAIBotResponse, collectMetaAIBotParticipantJids, makeAIGroupsSocket, extractAIGroupMetadata
|
|
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
|
+
import { WAMessageAddressingMode } from '../Types/index.js';
|
|
14
|
+
import { generateMessageIDV2, normalizeMessageContent } from '../Utils/index.js';
|
|
15
|
+
import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isJidGroup, isJidMetaAI, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
|
|
16
|
+
import { makeGroupsSocket } from './groups.js';
|
|
17
|
+
|
|
18
|
+
/** Default Meta AI bot JID used inside AI groups. */
|
|
19
|
+
export const META_AI_BOT_JID = '867051314767696@bot';
|
|
20
|
+
|
|
21
|
+
// `@bot` JIDs carry their device as a trailing suffix (`bot:1`), which
|
|
22
|
+
// jidNormalizedUser does not strip — normalize it before comparing.
|
|
23
|
+
const stripDeviceSuffix = (jid) => String(jid || '').replace(/:\d+$/, '');
|
|
24
|
+
const sameBotJid = (a, b) => {
|
|
25
|
+
if (!a || !b) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return jidNormalizedUser(stripDeviceSuffix(a)) === jidNormalizedUser(stripDeviceSuffix(b));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Pull the reply stanzaId out of a message: normalizeMessageContent returns the
|
|
32
|
+
// type-keyed wrapper (`{ extendedTextMessage: { ... } }`), so unwrap one level.
|
|
33
|
+
const extractReplyStanzaId = (message) => {
|
|
34
|
+
const content = message ? normalizeMessageContent(message) : undefined;
|
|
35
|
+
const inner = content && typeof content === 'object'
|
|
36
|
+
? content[Object.keys(content)[0]]
|
|
37
|
+
: undefined;
|
|
38
|
+
return inner?.contextInfo?.stanzaId || content?.contextInfo?.stanzaId || message?.messageContextInfo?.stanzaId;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Pure matcher: is `message` a Meta AI bot response in `chatJid`?
|
|
43
|
+
*
|
|
44
|
+
* A message is treated as a bot response when ALL of these hold:
|
|
45
|
+
* 1. it is not from me, and its remoteJid is the chat we prompted in;
|
|
46
|
+
* 2. the sender is a known bot JID (`@bot` suffix or one of `botJids`); OR
|
|
47
|
+
* its reply context (`contextInfo.stanzaId`) references the prompt we sent.
|
|
48
|
+
*
|
|
49
|
+
* The `botJids` fallback lets callers pass the bot's LID participant JID
|
|
50
|
+
* (resolved from group metadata) so LID-addressed AI groups match too.
|
|
51
|
+
*/
|
|
52
|
+
export const isMetaAIBotResponse = (message, chatJid, promptId, botJids = []) => {
|
|
53
|
+
if (!message || !message.key || message.key.fromMe) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (message.key.remoteJid !== chatJid) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
const sender = stripDeviceSuffix(message.key.participant || message.key.remoteJid);
|
|
60
|
+
if (isJidMetaAI(sender)) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
for (const botJid of botJids) {
|
|
64
|
+
if (sameBotJid(botJid, sender)) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (promptId && extractReplyStanzaId(message.message) === promptId) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// Resolve the bot's real participant JID(s) inside an AI group so the response
|
|
75
|
+
// matcher can recognize LID-addressed bot messages. Falls back to the canonical
|
|
76
|
+
// `@bot` JID when metadata is unavailable.
|
|
77
|
+
export const collectMetaAIBotParticipantJids = async (sock, jid, botUser) => {
|
|
78
|
+
const candidates = new Set([botUser]);
|
|
79
|
+
if (!isJidGroup(jid)) {
|
|
80
|
+
return candidates;
|
|
81
|
+
}
|
|
82
|
+
const botNumber = jidNormalizedUser(botUser).split('@')[0];
|
|
83
|
+
try {
|
|
84
|
+
const meta = await sock.groupMetadata(jid);
|
|
85
|
+
for (const participant of meta.participants || []) {
|
|
86
|
+
const pn = participant.phoneNumber
|
|
87
|
+
? jidNormalizedUser(participant.phoneNumber).split('@')[0]
|
|
88
|
+
: undefined;
|
|
89
|
+
const idNumber = jidNormalizedUser(participant.id).split('@')[0];
|
|
90
|
+
if (pn === botNumber || idNumber === botNumber || isJidMetaAI(participant.id)) {
|
|
91
|
+
candidates.add(participant.id);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// metadata unavailable — the canonical @bot JID is already in the set
|
|
97
|
+
}
|
|
98
|
+
return candidates;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const makeAIGroupsSocket = (config) => {
|
|
102
|
+
const sock = makeGroupsSocket(config);
|
|
103
|
+
const { ev, query } = sock;
|
|
104
|
+
|
|
105
|
+
/** Query helper for AI groups (w:gp2 namespace) */
|
|
106
|
+
const aiGroupQuery = async (jid, type, content) => query({
|
|
107
|
+
tag: 'iq',
|
|
108
|
+
attrs: {
|
|
109
|
+
type,
|
|
110
|
+
xmlns: 'w:g2',
|
|
111
|
+
to: jid
|
|
112
|
+
},
|
|
113
|
+
content
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const aiGroupMetadata = async (jid) => {
|
|
117
|
+
const result = await aiGroupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
|
|
118
|
+
return extractAIGroupMetadata(result);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Handle incoming w:gp2 notifications (create, promote, remove, add)
|
|
122
|
+
sock.ws.on('CB:notification,w:gp2', async (node) => {
|
|
123
|
+
const { attrs, content } = node;
|
|
124
|
+
if (!Array.isArray(content) || content.length === 0) return;
|
|
125
|
+
|
|
126
|
+
const inner = content[0];
|
|
127
|
+
const tag = inner.tag;
|
|
128
|
+
const groupId = typeof attrs.from === 'string'
|
|
129
|
+
? attrs.from
|
|
130
|
+
: attrs.from?.$1?.user
|
|
131
|
+
? jidEncode(attrs.from.$1.user, 'g.us')
|
|
132
|
+
: undefined;
|
|
133
|
+
|
|
134
|
+
if (!groupId) return;
|
|
135
|
+
|
|
136
|
+
if (tag === 'create') {
|
|
137
|
+
// New AI group created — emit groups.upsert
|
|
138
|
+
try {
|
|
139
|
+
const meta = await aiGroupMetadata(groupId);
|
|
140
|
+
ev.emit('groups.upsert', [meta]);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// metadata fetch may fail; emit minimal info
|
|
144
|
+
ev.emit('groups.upsert', [{ id: groupId }]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (tag === 'promote' || tag === 'demote' || tag === 'remove' || tag === 'add') {
|
|
148
|
+
const participants = getBinaryNodeChildren(inner, 'participant')
|
|
149
|
+
.map(p => {
|
|
150
|
+
const jid = p.attrs.jid;
|
|
151
|
+
if (typeof jid === 'string') return jid;
|
|
152
|
+
if (jid?.$1) {
|
|
153
|
+
return jidEncode(jid.$1.user, jid.$1.server || 's.whatsapp.net');
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
})
|
|
157
|
+
.filter(Boolean);
|
|
158
|
+
|
|
159
|
+
ev.emit('group-participants.update', {
|
|
160
|
+
id: groupId,
|
|
161
|
+
participants,
|
|
162
|
+
action: tag
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
else if (tag === 'subject') {
|
|
166
|
+
ev.emit('groups.update', [{ id: groupId, subject: inner.attrs?.subject }]);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
await sock.sendMessageAck(node);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
...sock,
|
|
174
|
+
aiGroupMetadata,
|
|
175
|
+
aiGroupCreate: async (subject, participants = [], options = {}) => {
|
|
176
|
+
if (!Array.isArray(participants)) participants = [];
|
|
177
|
+
const key = generateMessageIDV2();
|
|
178
|
+
const {
|
|
179
|
+
ephemeralExpiration = 86400,
|
|
180
|
+
memberAddMode = 'all_member_add',
|
|
181
|
+
memberShareGroupHistoryMode = 'all_member_share',
|
|
182
|
+
memberLinkMode = 'all_member_link'
|
|
183
|
+
} = options;
|
|
184
|
+
const result = await aiGroupQuery('@g.us', 'set', [
|
|
185
|
+
{
|
|
186
|
+
tag: 'create',
|
|
187
|
+
attrs: {
|
|
188
|
+
subject,
|
|
189
|
+
key
|
|
190
|
+
},
|
|
191
|
+
content: participants.map(jid => ({
|
|
192
|
+
tag: 'participant',
|
|
193
|
+
attrs: { jid }
|
|
194
|
+
}))
|
|
195
|
+
}
|
|
196
|
+
]);
|
|
197
|
+
return extractAIGroupMetadata(result);
|
|
198
|
+
},
|
|
199
|
+
aiGroupAddBot: async (jid, botUser = '867051314767696') => {
|
|
200
|
+
const result = await aiGroupQuery(jid, 'set', [
|
|
201
|
+
{
|
|
202
|
+
tag: 'add',
|
|
203
|
+
attrs: {},
|
|
204
|
+
content: [
|
|
205
|
+
{
|
|
206
|
+
tag: 'participant',
|
|
207
|
+
attrs: { jid: `${botUser}@bot` }
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
]);
|
|
212
|
+
const node = getBinaryNodeChild(result, 'add');
|
|
213
|
+
const participantsAffected = getBinaryNodeChildren(node, 'participant');
|
|
214
|
+
return participantsAffected.map(p => ({
|
|
215
|
+
status: p.attrs.error || '200',
|
|
216
|
+
jid: p.attrs.jid
|
|
217
|
+
}));
|
|
218
|
+
},
|
|
219
|
+
aiGroupLeave: async (id) => {
|
|
220
|
+
await aiGroupQuery('@g.us', 'set', [
|
|
221
|
+
{
|
|
222
|
+
tag: 'leave',
|
|
223
|
+
attrs: {},
|
|
224
|
+
content: [{ tag: 'group', attrs: { id } }]
|
|
225
|
+
}
|
|
226
|
+
]);
|
|
227
|
+
},
|
|
228
|
+
aiGroupParticipantsUpdate: async (jid, participants, action) => {
|
|
229
|
+
const result = await aiGroupQuery(jid, 'set', [
|
|
230
|
+
{
|
|
231
|
+
tag: action,
|
|
232
|
+
attrs: {},
|
|
233
|
+
content: participants.map(jid => ({
|
|
234
|
+
tag: 'participant',
|
|
235
|
+
attrs: { jid }
|
|
236
|
+
}))
|
|
237
|
+
}
|
|
238
|
+
]);
|
|
239
|
+
const node = getBinaryNodeChild(result, action);
|
|
240
|
+
const participantsAffected = getBinaryNodeChildren(node, 'participant');
|
|
241
|
+
return participantsAffected.map(p => ({
|
|
242
|
+
status: p.attrs.error || '200',
|
|
243
|
+
jid: p.attrs.jid,
|
|
244
|
+
content: p
|
|
245
|
+
}));
|
|
246
|
+
},
|
|
247
|
+
aiGroupUpdateSubject: async (jid, subject) => {
|
|
248
|
+
await aiGroupQuery(jid, 'set', [
|
|
249
|
+
{
|
|
250
|
+
tag: 'subject',
|
|
251
|
+
attrs: {},
|
|
252
|
+
content: Buffer.from(subject, 'utf-8')
|
|
253
|
+
}
|
|
254
|
+
]);
|
|
255
|
+
},
|
|
256
|
+
aiGroupInviteCode: async (jid) => {
|
|
257
|
+
const result = await aiGroupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
|
|
258
|
+
const inviteNode = getBinaryNodeChild(result, 'invite');
|
|
259
|
+
return inviteNode?.attrs.code;
|
|
260
|
+
},
|
|
261
|
+
aiGroupRevokeInvite: async (jid) => {
|
|
262
|
+
const result = await aiGroupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
|
|
263
|
+
const inviteNode = getBinaryNodeChild(result, 'invite');
|
|
264
|
+
return inviteNode?.attrs.code;
|
|
265
|
+
},
|
|
266
|
+
aiGroupAcceptInvite: async (code) => {
|
|
267
|
+
const results = await aiGroupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
|
|
268
|
+
const result = getBinaryNodeChild(results, 'group');
|
|
269
|
+
return result?.attrs.jid;
|
|
270
|
+
},
|
|
271
|
+
aiGroupSettingUpdate: async (jid, setting) => {
|
|
272
|
+
await aiGroupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
|
|
273
|
+
},
|
|
274
|
+
aiGroupToggleEphemeral: async (jid, ephemeralExpiration) => {
|
|
275
|
+
const content = ephemeralExpiration
|
|
276
|
+
? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
|
|
277
|
+
: { tag: 'not_ephemeral', attrs: {} };
|
|
278
|
+
await aiGroupQuery(jid, 'set', [content]);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
export const extractAIGroupMetadata = (result) => {
|
|
284
|
+
// IQ response nests group inside create node: result → create → group
|
|
285
|
+
const createNode = getBinaryNodeChild(result, 'create');
|
|
286
|
+
const group =
|
|
287
|
+
getBinaryNodeChild(createNode || result, 'group') ||
|
|
288
|
+
getBinaryNodeChild(result, 'group');
|
|
289
|
+
const descChild = getBinaryNodeChild(group, 'description');
|
|
290
|
+
let desc, descId, descOwner, descOwnerPn, descTime;
|
|
291
|
+
if (descChild) {
|
|
292
|
+
desc = getBinaryNodeChildString(descChild, 'body');
|
|
293
|
+
descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
|
|
294
|
+
descOwnerPn = descChild.attrs.participant_pn
|
|
295
|
+
? jidNormalizedUser(descChild.attrs.participant_pn)
|
|
296
|
+
: undefined;
|
|
297
|
+
descTime = +descChild.attrs.t;
|
|
298
|
+
descId = descChild.attrs.id;
|
|
299
|
+
}
|
|
300
|
+
const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
|
|
301
|
+
const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
|
|
302
|
+
const metadata = {
|
|
303
|
+
id: groupId,
|
|
304
|
+
subject: group.attrs.subject,
|
|
305
|
+
subjectTime: +group.attrs.s_t,
|
|
306
|
+
creation: +group.attrs.creation,
|
|
307
|
+
owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
|
|
308
|
+
ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
|
|
309
|
+
owner_country_code: group.attrs.creator_country_code,
|
|
310
|
+
size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
|
|
311
|
+
desc,
|
|
312
|
+
descId,
|
|
313
|
+
descOwner,
|
|
314
|
+
descOwnerPn,
|
|
315
|
+
descTime,
|
|
316
|
+
isAIGroup: true,
|
|
317
|
+
addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
|
|
318
|
+
participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => ({
|
|
319
|
+
id: attrs.jid,
|
|
320
|
+
phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
|
|
321
|
+
lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
|
|
322
|
+
admin: attrs.type || null
|
|
323
|
+
})),
|
|
324
|
+
ephemeralDuration: eph ? +eph : undefined
|
|
325
|
+
};
|
|
326
|
+
return metadata;
|
|
327
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Socket/ban-checker.js
|
|
5
|
+
* About: Ban checker module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: checkStatusWA, getBanDetails, normalizePhone
|
|
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
|
+
import { randomBytes, randomUUID } from 'crypto';
|
|
14
|
+
import { initAuthCreds } from '../Utils/auth-utils.js';
|
|
15
|
+
import { Curve, md5, signedKeyPair } from '../Utils/crypto.js';
|
|
16
|
+
import {
|
|
17
|
+
GRAPHQL_ACCESS_TOKEN,
|
|
18
|
+
GRAPHQL_BAN_STATUS_DOC_ID,
|
|
19
|
+
GRAPHQL_ENDPOINT,
|
|
20
|
+
MOBILE_COUNTRY_CODE,
|
|
21
|
+
MOBILE_REGISTRATION_ENDPOINT,
|
|
22
|
+
MOBILE_TOKEN,
|
|
23
|
+
MOBILE_USERAGENT
|
|
24
|
+
} from '../Defaults/ban-checker.js';
|
|
25
|
+
|
|
26
|
+
const COUNTRY_CODES = [
|
|
27
|
+
'1', '20', '27', '30', '31', '32', '33', '34', '36', '39', '40', '41', '43', '44', '45', '46', '47', '48', '49',
|
|
28
|
+
'51', '52', '53', '54', '55', '56', '57', '58', '60', '61', '62', '63', '64', '65', '66', '81', '82', '84', '86',
|
|
29
|
+
'90', '91', '92', '93', '94', '95', '98', '211', '212', '213', '216', '218', '220', '221', '222', '223', '224', '225',
|
|
30
|
+
'226', '227', '228', '229', '230', '231', '232', '233', '234', '235', '236', '237', '238', '239', '240', '241', '242',
|
|
31
|
+
'243', '244', '245', '246', '248', '249', '250', '251', '252', '253', '254', '255', '256', '257', '258', '260', '261',
|
|
32
|
+
'262', '263', '264', '265', '266', '267', '268', '269', '290', '291', '297', '298', '299', '350', '351', '352', '353',
|
|
33
|
+
'354', '355', '356', '357', '358', '359', '370', '371', '372', '373', '374', '375', '376', '377', '378', '380', '381',
|
|
34
|
+
'382', '383', '385', '386', '387', '389', '420', '421', '423', '500', '501', '502', '503', '504', '505', '506', '507',
|
|
35
|
+
'508', '509', '590', '591', '592', '593', '594', '595', '596', '597', '598', '599', '670', '672', '673', '674', '675',
|
|
36
|
+
'676', '677', '678', '679', '680', '681', '682', '683', '685', '686', '687', '688', '689', '690', '691', '692', '850',
|
|
37
|
+
'852', '853', '855', '856', '880', '886', '960', '961', '962', '963', '964', '965', '966', '967', '968', '970', '971',
|
|
38
|
+
'972', '973', '974', '975', '976', '977', '992', '993', '994', '995', '996', '998'
|
|
39
|
+
].sort((a, b) => b.length - a.length);
|
|
40
|
+
|
|
41
|
+
const PLACEHOLDER = /^REPLACE_WITH_/;
|
|
42
|
+
|
|
43
|
+
const cleanNumber = (value) => String(value || '').replace(/[\s().\-/]/g, '').replace(/^00/, '');
|
|
44
|
+
|
|
45
|
+
const normalizePhone = (value, options = {}) => {
|
|
46
|
+
const raw = cleanNumber(typeof value === 'object' ? value.number : value).replace(/^\+/, '');
|
|
47
|
+
if (!/^\d{6,15}$/.test(raw)) throw new TypeError('number must be an international phone number');
|
|
48
|
+
const requestedCountry = String(options.countryCode || MOBILE_COUNTRY_CODE || '').replace(/\D/g, '');
|
|
49
|
+
const countryCode = requestedCountry || COUNTRY_CODES.find((code) => raw.startsWith(code));
|
|
50
|
+
if (!countryCode || !raw.startsWith(countryCode)) throw new TypeError('number must include a recognized country calling code');
|
|
51
|
+
const nationalNumber = raw.slice(countryCode.length);
|
|
52
|
+
if (!nationalNumber) throw new TypeError('number is missing its national part');
|
|
53
|
+
return { countryCode, nationalNumber, e164: `+${raw}` };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const urlEncode = (value) => encodeURIComponent(String(value)).replace(/%25([0-9a-f]{2})/gi, '%$1');
|
|
57
|
+
const urlHex = (buffer) => [...buffer].map((byte) => `%${byte.toString(16).padStart(2, '0')}`).join('');
|
|
58
|
+
|
|
59
|
+
const makeRegistrationParams = (countryCode, nationalNumber) => {
|
|
60
|
+
const creds = initAuthCreds();
|
|
61
|
+
const eRegId = Buffer.alloc(4);
|
|
62
|
+
eRegId.writeInt32BE(creds.registrationId);
|
|
63
|
+
const eSkeyId = Buffer.alloc(3);
|
|
64
|
+
eSkeyId.writeInt16BE(creds.signedPreKey.keyId);
|
|
65
|
+
return {
|
|
66
|
+
cc: countryCode,
|
|
67
|
+
in: nationalNumber,
|
|
68
|
+
Rc: '0', lg: 'en', lc: 'GB', mistyped: '6',
|
|
69
|
+
authkey: Buffer.from(creds.noiseKey.public).toString('base64url'),
|
|
70
|
+
e_regid: eRegId.toString('base64url'),
|
|
71
|
+
e_keytype: 'BQ',
|
|
72
|
+
e_ident: Buffer.from(creds.signedIdentityKey.public).toString('base64url'),
|
|
73
|
+
e_skey_id: eSkeyId.toString('base64url'),
|
|
74
|
+
e_skey_val: Buffer.from(creds.signedPreKey.keyPair.public).toString('base64url'),
|
|
75
|
+
e_skey_sig: Buffer.from(creds.signedPreKey.signature).toString('base64url'),
|
|
76
|
+
fdid: randomUUID(),
|
|
77
|
+
network_ratio_type: '1', expid: randomUUID(), simnum: '1', hasinrc: '1',
|
|
78
|
+
pid: String(Math.floor(Math.random() * 1000)),
|
|
79
|
+
id: urlHex(randomBytes(16)),
|
|
80
|
+
backup_token: urlHex(randomBytes(32)),
|
|
81
|
+
token: Buffer.from(md5(Buffer.concat([MOBILE_TOKEN, Buffer.from(nationalNumber)]))).toString('hex')
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const requestJson = async (url, options = {}) => {
|
|
86
|
+
const response = await fetch(url, { ...options, headers: { 'User-Agent': MOBILE_USERAGENT, ...(options.headers || {}) } });
|
|
87
|
+
let body = null;
|
|
88
|
+
try { body = await response.json(); } catch { body = {}; }
|
|
89
|
+
return { response, body };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const getBanDetails = async (appealToken) => {
|
|
93
|
+
if (!appealToken || PLACEHOLDER.test(GRAPHQL_ACCESS_TOKEN) || PLACEHOLDER.test(GRAPHQL_BAN_STATUS_DOC_ID)) return null;
|
|
94
|
+
const { response, body } = await requestJson(GRAPHQL_ENDPOINT, {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
headers: { 'Content-Type': 'application/json' },
|
|
97
|
+
body: JSON.stringify({
|
|
98
|
+
variables: JSON.stringify({ app_id: 'dev.app.id', request_token: appealToken }),
|
|
99
|
+
access_token: GRAPHQL_ACCESS_TOKEN,
|
|
100
|
+
doc_id: GRAPHQL_BAN_STATUS_DOC_ID,
|
|
101
|
+
lang: 'en_GB'
|
|
102
|
+
})
|
|
103
|
+
});
|
|
104
|
+
if (!response.ok) return null;
|
|
105
|
+
return body?.data?.whatsapp_support_ban_appeal_status || null;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const result = (number, status, isBanned = false, isNeedOfficialWa = false, banInfo = null) => ({ number, status, isBanned, isNeedOfficialWa, banInfo });
|
|
109
|
+
|
|
110
|
+
const getDiagnostic = (response, body) => {
|
|
111
|
+
const bodyObject = body && typeof body === 'object' && !Array.isArray(body) ? body : {};
|
|
112
|
+
const nestedData = bodyObject.data && typeof bodyObject.data === 'object' && !Array.isArray(bodyObject.data)
|
|
113
|
+
? bodyObject.data
|
|
114
|
+
: {};
|
|
115
|
+
const keys = Object.keys(bodyObject).sort();
|
|
116
|
+
return {
|
|
117
|
+
httpStatus: Number.isInteger(response?.status) ? response.status : null,
|
|
118
|
+
ok: response?.ok === true,
|
|
119
|
+
contentType: response?.headers?.get?.('content-type') || null,
|
|
120
|
+
bodyKeys: keys,
|
|
121
|
+
dataKeys: Object.keys(nestedData).sort(),
|
|
122
|
+
signals: {
|
|
123
|
+
hasReason: Object.prototype.hasOwnProperty.call(bodyObject, 'reason'),
|
|
124
|
+
hasError: Object.prototype.hasOwnProperty.call(bodyObject, 'error'),
|
|
125
|
+
hasStatus: Object.prototype.hasOwnProperty.call(bodyObject, 'status'),
|
|
126
|
+
hasAppealToken: Boolean(bodyObject.appeal_token || bodyObject.appealToken || nestedData.appeal_token),
|
|
127
|
+
hasBanFields: Boolean(bodyObject.ban_type || bodyObject.banType || bodyObject.violation_type || bodyObject.violationType)
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const withDiagnostic = (value, diagnostic) => diagnostic ? { ...value, diagnostics: diagnostic } : value;
|
|
133
|
+
|
|
134
|
+
export const checkStatusWA = async (number, options = {}) => {
|
|
135
|
+
const normalized = normalizePhone(number, options);
|
|
136
|
+
const params = makeRegistrationParams(normalized.countryCode, normalized.nationalNumber);
|
|
137
|
+
const query = Object.entries(params).map(([key, value]) => `${key}=${urlEncode(value)}`).join('&');
|
|
138
|
+
const { response, body } = await requestJson(`${MOBILE_REGISTRATION_ENDPOINT}/exist?${query}`);
|
|
139
|
+
const diagnostic = options.diagnostic === true ? getDiagnostic(response, body) : null;
|
|
140
|
+
const reason = String(body?.reason || body?.error || body?.status || '').toLowerCase();
|
|
141
|
+
const appealToken = body?.appeal_token || body?.appealToken || body?.data?.appeal_token;
|
|
142
|
+
|
|
143
|
+
if (response.status === 429 || /rate.?limit|too many/.test(reason)) return withDiagnostic(result(normalized.e164, 'rate_limited'), diagnostic);
|
|
144
|
+
if (/blocked|official/.test(reason) && !appealToken) return withDiagnostic(result(normalized.e164, 'blocked', false, true), diagnostic);
|
|
145
|
+
if (appealToken || /banned|ban|temporar|perman/.test(reason)) {
|
|
146
|
+
const details = await getBanDetails(appealToken);
|
|
147
|
+
const banType = details?.ban_type || details?.banType || body?.ban_type || body?.banType || (reason.includes('perman') ? 'permanent' : 'temporary');
|
|
148
|
+
return withDiagnostic(result(normalized.e164, 'banned', true, false, {
|
|
149
|
+
banType,
|
|
150
|
+
violationType: body?.violation_type || body?.violationType || null,
|
|
151
|
+
violationReason: body?.violation_reason || body?.violationReason || null,
|
|
152
|
+
canAppeal: banType !== 'permanent',
|
|
153
|
+
appealToken: appealToken || null,
|
|
154
|
+
banTime: details?.ban_time || details?.banTime || null,
|
|
155
|
+
banDate: details?.ban_date || details?.banDate || null,
|
|
156
|
+
appealStatus: details?.appeal_status || details?.appealStatus || null,
|
|
157
|
+
appealCreatedAt: details?.appeal_created_at || details?.appealCreatedAt || null
|
|
158
|
+
}), diagnostic);
|
|
159
|
+
}
|
|
160
|
+
if (response.ok && (!body?.reason || /^(ok|sent|active|registered)$/.test(reason))) return withDiagnostic(result(normalized.e164, 'active'), diagnostic);
|
|
161
|
+
if (/not.?registered|does.?not.?exist/.test(reason)) return withDiagnostic(result(normalized.e164, 'not_registered'), diagnostic);
|
|
162
|
+
return withDiagnostic(result(normalized.e164, 'unknown'), diagnostic);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export { getBanDetails, normalizePhone };
|