@ruthless-imperium/baileys 1.0.1 → 1.0.3

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.
@@ -1,10 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const Defaults_1 = require("../Defaults");
4
- const business_1 = require("./business");
4
+ const { makeCommunitiesSocket } = require("./communities");
5
+ const { makeInteropSocket } = require("./interop");
6
+ const { makePrivacySocket } = require("./privacy");
7
+ const { makeGraphQLSocket } = require("./graphql");
8
+ const { makeMessageBuilderSocket } = require("./message-builder");
9
+ const MB = require("./message-builder-core").default;
5
10
  // export the last socket layer
6
- const makeWASocket = (config) => ((0, business_1.makeBusinessSocket)({
7
- ...Defaults_1.DEFAULT_CONNECTION_CONFIG,
8
- ...config
9
- }));
11
+ const makeWASocket = (config) => {
12
+ const newConfig = {
13
+ ...Defaults_1.DEFAULT_CONNECTION_CONFIG,
14
+ ...config
15
+ };
16
+ const satu = makeCommunitiesSocket(newConfig);
17
+ const dua = makeInteropSocket(satu);
18
+ const tiga = makePrivacySocket(dua);
19
+ const empat = makeGraphQLSocket(tiga);
20
+ const sock = makeMessageBuilderSocket(empat);
21
+ MB.bind(sock);
22
+ sock.AIRich = MB.AIRich;
23
+ sock.Carousel = MB.Carousel;
24
+ sock.Button = MB.Button;
25
+ sock.ButtonV2 = MB.ButtonV2;
26
+ sock.Toolkit = MB.Toolkit;
27
+ sock.MessageBuilder = MB;
28
+ return sock;
29
+ };
10
30
  exports.default = makeWASocket;
@@ -0,0 +1,468 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const __def = (m) => (m && m.__esModule) ? m.default : m;
4
+ const __exportStar = (m, e) => { for (const k in m) if (k !== "default" && !Object.prototype.hasOwnProperty.call(e, k)) e[k] = m[k]; };
5
+ const { getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } = require("../WABinary/index.js");
6
+ const { executeWMexQuery } = require("./mex.js");
7
+
8
+ /**
9
+ * w:mex query IDs for interop group and privacy operations.
10
+ *
11
+ * Source: whatsapp-android-mex_client_persist_ids.json (WA 2.26.26.4 APK assets)
12
+ */
13
+ const INTEROP_MEX_QUERY_IDS = {
14
+ CREATE_GROUP: '25726817620301611', // GroupsCreateInteropGroup
15
+ LEAVE_GROUP: '25346167795013271', // LeaveInteropGroup
16
+ ADD_PARTICIPANTS: '25732168276369451', // AddParticipantsToInteropGroup
17
+ QUERY_GROUP_INFO: '32734144032867938', // QueryInteropGroupInfo
18
+ PRIVACY_SETTINGS_QUERY: '24849123668112654', // InteropPrivacySettingsQuery
19
+ PRIVACY_SETTINGS_UPDATE: '25421856497452763', // InteropPrivacySettingsUpdate
20
+ PRIVACY_SETTINGS_WITH_CONTACT_LIST: '24913399124998598' // InteropPrivacySettingWithContactListUpdate
21
+ }
22
+
23
+ /**
24
+ * Known integrator IDs (assigned by WhatsApp).
25
+ * BirdyChat → identifier_type="email"
26
+ * Haiket → identifier_type="pn"
27
+ */
28
+ const INTEGRATOR_BIRDYCHAT = 12
29
+ const INTEGRATOR_HAIKET = 13
30
+
31
+ /**
32
+ * TOS trackable results sent by WA on first interop opt-in.
33
+ * 105 = TOS shown, 160 = TOS accepted.
34
+ */
35
+ const TOS_TRACKABLE_ID = '20240306'
36
+ const TOS_RESULT_SHOWN = '105'
37
+ const TOS_RESULT_ACCEPTED = '160'
38
+
39
+ /** Maximum users per batch lookup (enforced server-side too). */
40
+ const INTEROP_BATCH_MAX = 256
41
+
42
+ const makeInteropSocket = sock => {
43
+ const { query, generateMessageTag, logger, signalRepository } = sock
44
+
45
+ const mexQuery = (variables, queryId, dataPath) =>
46
+ executeWMexQuery(variables, queryId, dataPath, query, generateMessageTag)
47
+
48
+ /**
49
+ * Fetch all available interop integrators from the server.
50
+ */
51
+ const fetchIntegrators = async () => {
52
+ const result = await query({
53
+ tag: 'iq',
54
+ attrs: {
55
+ type: 'get',
56
+ xmlns: 'w:interop',
57
+ to: S_WHATSAPP_NET
58
+ },
59
+ content: [{ tag: 'integrator', attrs: { fetch: 'all' } }]
60
+ })
61
+ const listNode = getBinaryNodeChild(result, 'integrator_list')
62
+ if (!listNode) return []
63
+ const globalOptedIn = listNode.attrs?.opted_in === 'true'
64
+ return getBinaryNodeChildren(listNode, 'integrator').map(node => {
65
+ const featuresNode = getBinaryNodeChild(node, 'features')
66
+ return {
67
+ id: parseInt(node.attrs.id, 10),
68
+ name: node.attrs.name,
69
+ // "active" | "onboarding" | "removed"
70
+ status: node.attrs.status,
71
+ icon: node.attrs.icon,
72
+ // "email" | "pn" | "username"
73
+ identifierType: node.attrs.identifier_type,
74
+ // Whether we are already opted-in to this integrator
75
+ optedIn: node.attrs.opted_in === 'true' || globalOptedIn,
76
+ features: {
77
+ groupMessaging: featuresNode?.attrs?.group_messaging === 'true'
78
+ }
79
+ }
80
+ })
81
+ }
82
+
83
+ /** Send a single TOS trackable item (shown or accepted). */
84
+ const sendTOSTrackable = async (id, result) => {
85
+ await query({
86
+ tag: 'iq',
87
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'tos' },
88
+ content: [{ tag: 'trackable', attrs: { id, result } }]
89
+ })
90
+ }
91
+
92
+ /**
93
+ * Accept TOS for interop.
94
+ * Sends two trackable items: shown (105) then accepted (160).
95
+ */
96
+ const acceptInteropTOS = async () => {
97
+ await sendTOSTrackable(TOS_TRACKABLE_ID, TOS_RESULT_SHOWN)
98
+ await sendTOSTrackable(TOS_TRACKABLE_ID, TOS_RESULT_ACCEPTED)
99
+ }
100
+
101
+ /**
102
+ * Opt in to a list of integrators (by numeric ID).
103
+ */
104
+ const optInIntegrators = async (integratorIds = [INTEGRATOR_BIRDYCHAT, INTEGRATOR_HAIKET]) => {
105
+ await query({
106
+ tag: 'iq',
107
+ attrs: { type: 'set', xmlns: 'w:interop', to: S_WHATSAPP_NET },
108
+ content: [
109
+ {
110
+ tag: 'opt_integrators',
111
+ attrs: {},
112
+ content: [
113
+ {
114
+ tag: 'integrator_list',
115
+ attrs: {},
116
+ content: integratorIds.map(id => ({
117
+ tag: 'integrator',
118
+ attrs: { id: id.toString() }
119
+ }))
120
+ }
121
+ ]
122
+ }
123
+ ]
124
+ })
125
+ }
126
+
127
+ /**
128
+ * Opt out of a list of integrators (by numeric ID).
129
+ */
130
+ const optOutIntegrators = async (integratorIds = [INTEGRATOR_BIRDYCHAT, INTEGRATOR_HAIKET]) => {
131
+ await query({
132
+ tag: 'iq',
133
+ attrs: { type: 'set', xmlns: 'w:interop', to: S_WHATSAPP_NET },
134
+ content: [
135
+ {
136
+ tag: 'opt_out_integrators',
137
+ attrs: {},
138
+ content: [
139
+ {
140
+ tag: 'integrator_list',
141
+ attrs: {},
142
+ content: integratorIds.map(id => ({
143
+ tag: 'integrator',
144
+ attrs: { id: id.toString() }
145
+ }))
146
+ }
147
+ ]
148
+ }
149
+ ]
150
+ })
151
+ }
152
+
153
+ /**
154
+ * Resolve one or more interop users by external ID in a single IQ.
155
+ */
156
+ const resolveInteropUsers = async users => {
157
+ if (!users || users.length === 0) return []
158
+ if (users.length > INTEROP_BATCH_MAX) {
159
+ throw new Error(`resolveInteropUsers: max ${INTEROP_BATCH_MAX} users per request`)
160
+ }
161
+ const result = await query({
162
+ tag: 'iq',
163
+ attrs: { type: 'get', xmlns: 'w:interop', to: S_WHATSAPP_NET },
164
+ content: [
165
+ {
166
+ tag: 'users',
167
+ attrs: {},
168
+ content: users.map(({ externalId, integratorId }) => ({
169
+ tag: 'user',
170
+ attrs: {
171
+ external_id: externalId,
172
+ integrator_id: integratorId.toString()
173
+ }
174
+ }))
175
+ }
176
+ ]
177
+ })
178
+ const usersNode = getBinaryNodeChild(result, 'users')
179
+ if (!usersNode) return []
180
+ return getBinaryNodeChildren(usersNode, 'user').map(userNode => {
181
+ const errorNode = getBinaryNodeChild(userNode, 'error')
182
+ if (errorNode) {
183
+ return {
184
+ externalId: userNode.attrs.external_id,
185
+ integratorId: parseInt(userNode.attrs.integrator_id, 10),
186
+ error: {
187
+ code: parseInt(errorNode.attrs.code, 10),
188
+ text: errorNode.attrs.text
189
+ }
190
+ }
191
+ }
192
+ return {
193
+ jid: userNode.attrs.jid,
194
+ externalId: userNode.attrs.external_id,
195
+ normalizedExternalId: userNode.attrs.normalized_external_id,
196
+ integratorId: parseInt(userNode.attrs.integrator_id, 10)
197
+ }
198
+ })
199
+ }
200
+
201
+ /**
202
+ * Convenience wrapper: resolve a single interop user.
203
+ */
204
+ const resolveInteropUser = async (externalId, integratorId) => {
205
+ const results = await resolveInteropUsers([{ externalId, integratorId }])
206
+ return results[0]?? null
207
+ }
208
+
209
+ /**
210
+ * Get reachability settings for interop contacts.
211
+ */
212
+ const getReachabilitySettings = async () => {
213
+ const result = await query({
214
+ tag: 'iq',
215
+ attrs: { type: 'get', xmlns: 'w:interop', to: S_WHATSAPP_NET },
216
+ content: [{ tag: 'reachability_settings', attrs: {} }]
217
+ })
218
+ const settingsNode = getBinaryNodeChild(result, 'reachability_settings')
219
+ if (!settingsNode) return null
220
+ return {
221
+ enabled: settingsNode.attrs?.enabled,
222
+ users: getBinaryNodeChildren(settingsNode, 'user').map(n => ({
223
+ externalId: n.attrs.external_id,
224
+ integratorId: parseInt(n.attrs.integrator_id, 10),
225
+ jid: n.attrs.jid
226
+ }))
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Set reachability settings (subscribe to presence) for interop contacts.
232
+ */
233
+ const setReachabilitySettings = async (users, enabled = 'true') => {
234
+ await query({
235
+ tag: 'iq',
236
+ attrs: { type: 'set', xmlns: 'w:interop', to: S_WHATSAPP_NET },
237
+ content: [
238
+ {
239
+ tag: 'reachability_settings',
240
+ attrs: { enabled },
241
+ content: users.map(({ externalId, integratorId }) => ({
242
+ tag: 'user',
243
+ attrs: {
244
+ external_id: externalId,
245
+ integrator_id: integratorId.toString()
246
+ }
247
+ }))
248
+ }
249
+ ]
250
+ })
251
+ }
252
+
253
+ /**
254
+ * Block or unblock an interop user via the w:interop blocklist.
255
+ */
256
+ const updateInteropBlockStatus = async (jid, action) => {
257
+ await query({
258
+ tag: 'iq',
259
+ attrs: { type: 'set', xmlns: 'w:interop', to: S_WHATSAPP_NET },
260
+ content: [
261
+ {
262
+ tag: 'blocklist',
263
+ attrs: {},
264
+ content: [{ tag: 'item', attrs: { action, jid } }]
265
+ }
266
+ ]
267
+ })
268
+ }
269
+
270
+ const blockInteropUser = jid => updateInteropBlockStatus(jid, 'block')
271
+ const unblockInteropUser = jid => updateInteropBlockStatus(jid, 'unblock')
272
+
273
+ /**
274
+ * Report an interop contact as spam.
275
+ */
276
+ const reportInteropSpam = async (jid, spamFlow = 'account_info_block') => {
277
+ await query({
278
+ tag: 'iq',
279
+ attrs: { type: 'set', xmlns: 'spam', to: S_WHATSAPP_NET },
280
+ content: [{ tag: 'spam_list', attrs: { jid, spam_flow: spamFlow } }]
281
+ })
282
+ }
283
+
284
+ /**
285
+ * Mark an interop JID as trusted_contact in privacy tokens.
286
+ */
287
+ const trustInteropContact = async jid => {
288
+ const t = Math.floor(Date.now() / 1000).toString()
289
+ await query({
290
+ tag: 'iq',
291
+ attrs: { to: S_WHATSAPP_NET, xmlns: 'privacy', type: 'set' },
292
+ content: [
293
+ {
294
+ tag: 'tokens',
295
+ attrs: {},
296
+ content: [{ tag: 'token', attrs: { jid, type: 'trusted_contact', t } }]
297
+ }
298
+ ]
299
+ })
300
+ }
301
+
302
+ /**
303
+ * Full interop initialization sequence matching WA Web.
304
+ */
305
+ const initInterop = async () => {
306
+ let integrators
307
+ try {
308
+ integrators = await fetchIntegrators()
309
+ } catch (err) {
310
+ logger.warn({ err }, 'interop: failed to fetch integrators')
311
+ return []
312
+ }
313
+ const toOptIn = integrators.filter(i => i.status === 'active' || i.status === 'onboarding')
314
+ if (toOptIn.length === 0) return integrators
315
+ try {
316
+ await acceptInteropTOS()
317
+ } catch (err) {
318
+ logger.warn({ err }, 'interop: failed to accept TOS')
319
+ }
320
+ try {
321
+ await optInIntegrators(toOptIn.map(i => i.id))
322
+ } catch (err) {
323
+ logger.warn({ err }, 'interop: failed to opt-in integrators')
324
+ }
325
+ logger.info({ integrators: toOptIn.map(i => i.name) }, 'interop: initialized')
326
+ return integrators
327
+ }
328
+
329
+ /**
330
+ * Reset the Signal session with an interop contact and force a fresh pkmsg handshake.
331
+ */
332
+ const resetInteropSession = async jid => {
333
+ await signalRepository.deleteSession([jid])
334
+ logger.info({ jid }, '[interop] session reset — next send will use pkmsg')
335
+ }
336
+
337
+ /**
338
+ * Create an interop group via MEX.
339
+ */
340
+ const createInteropGroup = async participants => {
341
+ const result = await mexQuery(
342
+ { input: { participants: participants.map(jid => ({ jid })) } },
343
+ INTEROP_MEX_QUERY_IDS.CREATE_GROUP,
344
+ 'xwa2_interop_group_create'
345
+ )
346
+ logger.info({ participants }, '[interop] group created via MEX')
347
+ return result
348
+ }
349
+
350
+ /**
351
+ * Leave one or more interop groups via MEX.
352
+ */
353
+ const leaveInteropGroup = async jids => {
354
+ const ids = Array.isArray(jids)? jids : [jids]
355
+ const result = await mexQuery(
356
+ { input: { groups_to_leave: ids.map(jid => ({ gid: jid.split('@')[0] })) } },
357
+ INTEROP_MEX_QUERY_IDS.LEAVE_GROUP,
358
+ 'xwa2_interop_group_leave'
359
+ )
360
+ logger.info({ jids: ids }, '[interop] left group(s) via MEX')
361
+ return result
362
+ }
363
+
364
+ /**
365
+ * Add participants to an existing interop group via MEX.
366
+ */
367
+ const addParticipantsToInteropGroup = async (groupJid, participants) => {
368
+ const gid = groupJid.split('@')[0]
369
+ return mexQuery(
370
+ { input: { gid, participants: participants.map(jid => ({ jid })) } },
371
+ INTEROP_MEX_QUERY_IDS.ADD_PARTICIPANTS,
372
+ 'xwa2_interop_add_participants_to_group'
373
+ )
374
+ }
375
+
376
+ /**
377
+ * Query info about an interop group via MEX.
378
+ */
379
+ const queryInteropGroupInfo = async groupJid => {
380
+ const gid = groupJid.split('@')[0]
381
+ return mexQuery({ group_input: { gid } }, INTEROP_MEX_QUERY_IDS.QUERY_GROUP_INFO, 'xwa2_interop_group_query_by_id')
382
+ }
383
+
384
+ /**
385
+ * Update an interop privacy setting via MEX.
386
+ */
387
+ const updateInteropPrivacySetting = async (feature, setting) => {
388
+ return mexQuery(
389
+ { feature, setting },
390
+ INTEROP_MEX_QUERY_IDS.PRIVACY_SETTINGS_UPDATE,
391
+ 'xwa2_interop_privacy_setting_update'
392
+ )
393
+ }
394
+
395
+ /**
396
+ * Update an interop privacy setting with an explicit contact list via MEX.
397
+ */
398
+ const updateInteropPrivacySettingWithContactList = async (
399
+ feature,
400
+ setting,
401
+ contacts,
402
+ contactListType,
403
+ dhash = 'none'
404
+ ) => {
405
+ return mexQuery(
406
+ { feature, setting, contacts, contact_list_type: contactListType, dhash },
407
+ INTEROP_MEX_QUERY_IDS.PRIVACY_SETTINGS_WITH_CONTACT_LIST,
408
+ 'xwa2_interop_privacy_setting_with_contact_list_update'
409
+ )
410
+ }
411
+
412
+ /**
413
+ * Check whether an interop user allows being added to groups (GROUPADD privacy).
414
+ */
415
+ const getInteropGroupAddPrivacy = async (jid, integratorId) => {
416
+ const result = await query({
417
+ tag: 'iq',
418
+ attrs: { type: 'get', xmlns: 'w:interop', to: S_WHATSAPP_NET },
419
+ content: [
420
+ {
421
+ tag: 'privacy',
422
+ attrs: { feature: 'GROUPADD' },
423
+ content: [
424
+ {
425
+ tag: 'user',
426
+ attrs: {
427
+ jid,
428
+ integrator_id: integratorId.toString()
429
+ }
430
+ }
431
+ ]
432
+ }
433
+ ]
434
+ })
435
+ const privacyNode = getBinaryNodeChild(result, 'privacy')
436
+ const userNode = getBinaryNodeChild(privacyNode, 'user')
437
+ return userNode?.attrs?.result === 'allowed'
438
+ }
439
+
440
+ return {
441
+ ...sock,
442
+ fetchIntegrators,
443
+ acceptInteropTOS,
444
+ optInIntegrators,
445
+ optOutIntegrators,
446
+ resolveInteropUser,
447
+ resolveInteropUsers,
448
+ getReachabilitySettings,
449
+ setReachabilitySettings,
450
+ blockInteropUser,
451
+ unblockInteropUser,
452
+ reportInteropSpam,
453
+ trustInteropContact,
454
+ initInterop,
455
+ resetInteropSession,
456
+ createInteropGroup,
457
+ leaveInteropGroup,
458
+ addParticipantsToInteropGroup,
459
+ queryInteropGroupInfo,
460
+ updateInteropPrivacySetting,
461
+ updateInteropPrivacySettingWithContactList,
462
+ getInteropGroupAddPrivacy,
463
+ INTEGRATOR_BIRDYCHAT,
464
+ INTEGRATOR_HAIKET,
465
+ INTEROP_MEX_QUERY_IDS
466
+ }
467
+ }
468
+ exports.makeInteropSocket = makeInteropSocket;