@blckrose/baileys 1.0.0 → 1.0.5

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,4 +1,8 @@
1
1
  import { proto } from '../../WAProto/index.js';
2
+ import { readFileSync } from 'node:fs';
3
+ const phoneNumberMcc = JSON.parse(
4
+ readFileSync(new URL('./phonenumber-mcc.json', import.meta.url))
5
+ );
2
6
  import { makeLibSignalRepository } from '../Signal/libsignal.js';
3
7
  import { Browsers } from '../Utils/browser-utils.js';
4
8
  import logger from '../Utils/logger.js';
@@ -125,4 +129,5 @@ export const TimeMs = {
125
129
  Day: 24 * 60 * 60 * 1000,
126
130
  Week: 7 * 24 * 60 * 60 * 1000
127
131
  };
132
+ export const PHONENUMBER_MCC = phoneNumberMcc;
128
133
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,223 @@
1
+ {
2
+ "93": 412,
3
+ "355": 276,
4
+ "213": 603,
5
+ "1-684": 544,
6
+ "376": 213,
7
+ "244": 631,
8
+ "1-264": 365,
9
+ "1-268": 344,
10
+ "54": 722,
11
+ "374": 283,
12
+ "297": 363,
13
+ "61": 505,
14
+ "43": 232,
15
+ "994": 400,
16
+ "1-242": 364,
17
+ "973": 426,
18
+ "880": 470,
19
+ "1-246": 342,
20
+ "375": 257,
21
+ "32": 206,
22
+ "501": 702,
23
+ "229": 616,
24
+ "1-441": 350,
25
+ "975": 402,
26
+ "591": 736,
27
+ "387": 218,
28
+ "267": 652,
29
+ "55": 724,
30
+ "1-284": 348,
31
+ "673": 528,
32
+ "359": 284,
33
+ "226": 613,
34
+ "257": 642,
35
+ "855": 456,
36
+ "237": 624,
37
+ "238": 625,
38
+ "1-345": 346,
39
+ "236": 623,
40
+ "235": 622,
41
+ "56": 730,
42
+ "86": 454,
43
+ "57": 732,
44
+ "269": 654,
45
+ "682": 548,
46
+ "506": 712,
47
+ "385": 219,
48
+ "53": 368,
49
+ "357": 280,
50
+ "420": 230,
51
+ "243": 630,
52
+ "45": 238,
53
+ "253": 638,
54
+ "1-767": 366,
55
+ "1-809": 370,
56
+ "1-849": 370,
57
+ "1-829": 370,
58
+ "593": 740,
59
+ "20": 602,
60
+ "503": 706,
61
+ "240": 627,
62
+ "291": 657,
63
+ "372": 248,
64
+ "251": 636,
65
+ "500": 750,
66
+ "298": 288,
67
+ "679": 542,
68
+ "358": 244,
69
+ "33": 208,
70
+ "689": 547,
71
+ "241": 628,
72
+ "220": 607,
73
+ "995": 282,
74
+ "49": 262,
75
+ "233": 620,
76
+ "350": 266,
77
+ "30": 202,
78
+ "299": 290,
79
+ "1-473": 352,
80
+ "1-671": 535,
81
+ "502": 704,
82
+ "224": 537,
83
+ "592": 738,
84
+ "509": 372,
85
+ "504": 708,
86
+ "852": 454,
87
+ "36": 216,
88
+ "354": 274,
89
+ "91": 404,
90
+ "62": 510,
91
+ "98": 432,
92
+ "964": 418,
93
+ "353": 234,
94
+ "972": 425,
95
+ "39": 222,
96
+ "225": 612,
97
+ "1-876": 338,
98
+ "81": 440,
99
+ "962": 416,
100
+ "254": 639,
101
+ "686": 545,
102
+ "383": 221,
103
+ "965": 419,
104
+ "371": 247,
105
+ "961": 415,
106
+ "266": 651,
107
+ "231": 618,
108
+ "218": 606,
109
+ "423": 295,
110
+ "370": 246,
111
+ "352": 270,
112
+ "389": 294,
113
+ "261": 646,
114
+ "265": 650,
115
+ "60": 502,
116
+ "960": 472,
117
+ "223": 610,
118
+ "356": 278,
119
+ "692": 551,
120
+ "222": 609,
121
+ "230": 617,
122
+ "52": 334,
123
+ "691": 550,
124
+ "373": 259,
125
+ "377": 212,
126
+ "976": 428,
127
+ "382": 297,
128
+ "1-664": 354,
129
+ "212": 604,
130
+ "258": 643,
131
+ "95": 414,
132
+ "264": 649,
133
+ "674": 536,
134
+ "977": 429,
135
+ "31": 204,
136
+ "687": 546,
137
+ "64": 530,
138
+ "505": 710,
139
+ "227": 614,
140
+ "234": 621,
141
+ "683": 555,
142
+ "1-670": 534,
143
+ "47": 242,
144
+ "968": 226,
145
+ "92": 410,
146
+ "680": 552,
147
+ "970": 423,
148
+ "507": 714,
149
+ "675": 537,
150
+ "595": 744,
151
+ "51": 716,
152
+ "63": 515,
153
+ "48": 260,
154
+ "351": 268,
155
+ "1-787, 1-939": 330,
156
+ "974": 427,
157
+ "242": 630,
158
+ "40": 226,
159
+ "7": 250,
160
+ "250": 635,
161
+ "290": 658,
162
+ "1-869": 356,
163
+ "1-758": 358,
164
+ "508": 308,
165
+ "1-784": 360,
166
+ "685": 544,
167
+ "378": 292,
168
+ "239": 626,
169
+ "966": 420,
170
+ "221": 608,
171
+ "381": 220,
172
+ "248": 633,
173
+ "232": 619,
174
+ "65": 525,
175
+ "386": 293,
176
+ "677": 540,
177
+ "27": 655,
178
+ "211": 659,
179
+ "34": 214,
180
+ "94": 413,
181
+ "249": 634,
182
+ "597": 746,
183
+ "268": 653,
184
+ "46": 240,
185
+ "41": 228,
186
+ "963": 417,
187
+ "886": 466,
188
+ "992": 436,
189
+ "255": 640,
190
+ "66": 520,
191
+ "228": 615,
192
+ "690": 554,
193
+ "676": 539,
194
+ "1-868": 374,
195
+ "216": 605,
196
+ "90": 286,
197
+ "993": 438,
198
+ "1-649": 376,
199
+ "688": 553,
200
+ "1-340": 332,
201
+ "256": 641,
202
+ "380": 255,
203
+ "971": 424,
204
+ "44": 234,
205
+ "1": 310,
206
+ "598": 748,
207
+ "998": 434,
208
+ "678": 541,
209
+ "379": 225,
210
+ "58": 734,
211
+ "681": 543,
212
+ "967": 421,
213
+ "260": 645,
214
+ "263": 648,
215
+ "670": 514,
216
+ "245": 632,
217
+ "856": 457,
218
+ "599": 362,
219
+ "850": 467,
220
+ "262": 647,
221
+ "82": 450,
222
+ "84": 452
223
+ }
@@ -0,0 +1,361 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import { WAMessageStubType } from '../Types/index.js';
3
+ import { generateMessageID, unixTimestampSeconds } from '../Utils/index.js';
4
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
5
+ import { makeBusinessSocket } from './business.js';
6
+
7
+ export const makeCommunitiesSocket = (config) => {
8
+ const sock = makeBusinessSocket(config);
9
+ const { authState, ev, query, groupMetadata, upsertMessage } = sock;
10
+
11
+ const communityQuery = async (jid, type, content) => (query({
12
+ tag: 'iq',
13
+ attrs: {
14
+ type,
15
+ xmlns: 'w:g2',
16
+ to: jid,
17
+ },
18
+ content
19
+ }));
20
+
21
+ const communityMetadata = async (jid) => {
22
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
23
+ return extractCommunityMetadata(result);
24
+ };
25
+
26
+ const communityFetchAllParticipating = async () => {
27
+ const result = await query({
28
+ tag: 'iq',
29
+ attrs: {
30
+ to: '@g.us',
31
+ xmlns: 'w:g2',
32
+ type: 'get',
33
+ },
34
+ content: [
35
+ {
36
+ tag: 'participating',
37
+ attrs: {},
38
+ content: [
39
+ { tag: 'participants', attrs: {} },
40
+ { tag: 'description', attrs: {} }
41
+ ]
42
+ }
43
+ ]
44
+ });
45
+
46
+ const data = {};
47
+ const communitiesChild = getBinaryNodeChild(result, 'communities');
48
+
49
+ if (communitiesChild) {
50
+ const communities = getBinaryNodeChildren(communitiesChild, 'community');
51
+ for (const communityNode of communities) {
52
+ const meta = extractCommunityMetadata({
53
+ tag: 'result',
54
+ attrs: {},
55
+ content: [communityNode]
56
+ });
57
+ data[meta.id] = meta;
58
+ }
59
+ }
60
+
61
+ sock.ev.emit('groups.update', Object.values(data));
62
+ return data;
63
+ };
64
+
65
+ sock.ws.on('CB:ib,,dirty', async (node) => {
66
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
67
+
68
+ if (attrs.type !== 'communities') {
69
+ return;
70
+ }
71
+
72
+ await communityFetchAllParticipating();
73
+ await sock.cleanDirtyBits('groups');
74
+ });
75
+
76
+ return {
77
+ ...sock,
78
+ communityQuery,
79
+ communityMetadata,
80
+ communityCreate: async (subject, body) => {
81
+ const descriptionId = generateMessageID().substring(0, 12);
82
+
83
+ const result = await communityQuery('@g.us', 'set', [
84
+ {
85
+ tag: 'create',
86
+ attrs: { subject },
87
+ content: [{
88
+ tag: 'description',
89
+ attrs: {
90
+ id: descriptionId
91
+ },
92
+ content: [{
93
+ tag: 'body',
94
+ attrs: {},
95
+ content: Buffer.from(body || '', 'utf-8')
96
+ }]
97
+ },
98
+ {
99
+ tag: 'parent',
100
+ attrs: {
101
+ default_membership_approval_mode: 'request_required'
102
+ }
103
+ },
104
+ {
105
+ tag: 'allow_non_admin_sub_group_creation',
106
+ attrs: {}
107
+ },
108
+ {
109
+ tag: 'create_general_chat',
110
+ attrs: {}
111
+ }]
112
+ }
113
+ ]);
114
+
115
+ return extractCommunityMetadata(result);
116
+ },
117
+ communityLeave: async (id) => {
118
+ await communityQuery('@g.us', 'set', [
119
+ {
120
+ tag: 'leave',
121
+ attrs: {},
122
+ content: [
123
+ { tag: 'community', attrs: { id } }
124
+ ]
125
+ }
126
+ ]);
127
+ },
128
+ communityUpdateSubject: async (jid, subject) => {
129
+ await communityQuery(jid, 'set', [
130
+ {
131
+ tag: 'subject',
132
+ attrs: {},
133
+ content: Buffer.from(subject, 'utf-8')
134
+ }
135
+ ]);
136
+ },
137
+ communityRequestParticipantsList: async (jid) => {
138
+ const result = await communityQuery(jid, 'get', [
139
+ {
140
+ tag: 'membership_approval_requests',
141
+ attrs: {}
142
+ }
143
+ ]);
144
+
145
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
146
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
147
+
148
+ return participants.map(v => v.attrs);
149
+ },
150
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
151
+ const result = await communityQuery(jid, 'set', [{
152
+ tag: 'membership_requests_action',
153
+ attrs: {},
154
+ content: [
155
+ {
156
+ tag: action,
157
+ attrs: {},
158
+ content: participants.map(jid => ({
159
+ tag: 'participant',
160
+ attrs: { jid }
161
+ }))
162
+ }
163
+ ]
164
+ }]);
165
+
166
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
167
+ const nodeAction = getBinaryNodeChild(node, action);
168
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
169
+
170
+ return participantsAffected.map(p => {
171
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
172
+ });
173
+ },
174
+ communityParticipantsUpdate: async (jid, participants, action) => {
175
+ const result = await communityQuery(jid, 'set', [
176
+ {
177
+ tag: action,
178
+ attrs: {},
179
+ content: participants.map(jid => ({
180
+ tag: 'participant',
181
+ attrs: { jid }
182
+ }))
183
+ }
184
+ ]);
185
+ const node = getBinaryNodeChild(result, action);
186
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
187
+
188
+ return participantsAffected.map(p => {
189
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
190
+ });
191
+ },
192
+ communityUpdateDescription: async (jid, description) => {
193
+ const metadata = await communityMetadata(jid);
194
+ const prev = metadata.descId ? metadata.descId : null;
195
+
196
+ await communityQuery(jid, 'set', [
197
+ {
198
+ tag: 'description',
199
+ attrs: {
200
+ ...(description ? { id: generateMessageID() } : { delete: 'true' }),
201
+ ...(prev ? { prev } : {})
202
+ },
203
+ content: description ? [
204
+ { tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
205
+ ] : undefined
206
+ }
207
+ ]);
208
+ },
209
+ communityInviteCode: async (jid) => {
210
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
211
+ const inviteNode = getBinaryNodeChild(result, 'invite');
212
+
213
+ return inviteNode?.attrs?.code;
214
+ },
215
+ communityRevokeInvite: async (jid) => {
216
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
217
+ const inviteNode = getBinaryNodeChild(result, 'invite');
218
+
219
+ return inviteNode?.attrs?.code;
220
+ },
221
+ communityAcceptInvite: async (code) => {
222
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
223
+ const result = getBinaryNodeChild(results, 'community');
224
+
225
+ return result?.attrs?.jid;
226
+ },
227
+ /**
228
+ * revoke a v4 invite for someone
229
+ * @param communityJid community jid
230
+ * @param invitedJid jid of person you invited
231
+ * @returns true if successful
232
+ */
233
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
234
+ const result = await communityQuery(communityJid, 'set', [{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }]);
235
+
236
+ return !!result;
237
+ },
238
+ /**
239
+ * accept a GroupInviteMessage
240
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
241
+ * @param inviteMessage the message to accept
242
+ */
243
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
244
+ key = typeof key === 'string' ? { remoteJid: key } : key;
245
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [{
246
+ tag: 'accept',
247
+ attrs: {
248
+ code: inviteMessage.inviteCode,
249
+ expiration: inviteMessage.inviteExpiration.toString(),
250
+ admin: key.remoteJid
251
+ }
252
+ }]);
253
+
254
+ // if we have the full message key
255
+ // update the invite message to be expired
256
+ if (key.id) {
257
+ // create new invite message that is expired
258
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
259
+ inviteMessage.inviteExpiration = 0;
260
+ inviteMessage.inviteCode = '';
261
+ ev.emit('messages.update', [
262
+ {
263
+ key,
264
+ update: {
265
+ message: {
266
+ groupInviteMessage: inviteMessage
267
+ }
268
+ }
269
+ }
270
+ ]);
271
+ }
272
+
273
+ // generate the group add message
274
+ await upsertMessage({
275
+ key: {
276
+ remoteJid: inviteMessage.groupJid,
277
+ id: generateMessageID(authState.creds.me?.id),
278
+ fromMe: false,
279
+ participant: key.remoteJid,
280
+ },
281
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
282
+ messageStubParameters: [
283
+ authState.creds.me.id
284
+ ],
285
+ participant: key.remoteJid,
286
+ messageTimestamp: unixTimestampSeconds()
287
+ }, 'notify');
288
+
289
+ return results.attrs.from;
290
+ }),
291
+ communityGetInviteInfo: async (code) => {
292
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
293
+
294
+ return extractCommunityMetadata(results);
295
+ },
296
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
297
+ const content = ephemeralExpiration ?
298
+ { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
299
+ { tag: 'not_ephemeral', attrs: {} };
300
+ await communityQuery(jid, 'set', [content]);
301
+ },
302
+ communitySettingUpdate: async (jid, setting) => {
303
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
304
+ },
305
+ communityMemberAddMode: async (jid, mode) => {
306
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
307
+ },
308
+ communityJoinApprovalMode: async (jid, mode) => {
309
+ await communityQuery(jid, 'set', [{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }]);
310
+ },
311
+ communityFetchAllParticipating
312
+ };
313
+ };
314
+
315
+ export const extractCommunityMetadata = (result) => {
316
+ const community = getBinaryNodeChild(result, 'group');
317
+ const descChild = getBinaryNodeChild(community, 'description');
318
+
319
+ let desc;
320
+ let descId;
321
+
322
+ if (descChild) {
323
+ desc = getBinaryNodeChildString(descChild, 'body');
324
+ descId = descChild.attrs.id;
325
+ }
326
+
327
+ const mode = community.attrs.addressing_mode;
328
+ const communityId = community.attrs.id.includes('@') ? community.attrs.id : jidEncode(community.attrs.id, 'g.us');
329
+ const eph = getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
330
+ const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
331
+
332
+ const metadata = {
333
+ id: communityId,
334
+ addressingMode: mode,
335
+ subject: community.attrs.subject,
336
+ subjectOwner: mode === 'lid' ? community.attrs.s_o_pn : community.attrs.s_o,
337
+ subjectTime: +community.attrs.s_t,
338
+ size: community.attrs?.size ? +community.attrs.size : getBinaryNodeChildren(community, 'participant').length,
339
+ creation: +community.attrs.creation,
340
+ owner: community.attrs.creator ? jidNormalizedUser(mode === 'lid' ? community.attrs.creator_pn : community.attrs.creator) : undefined,
341
+ desc,
342
+ descId,
343
+ linkedParent: getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
344
+ restrict: !!getBinaryNodeChild(community, 'locked'),
345
+ announce: !!getBinaryNodeChild(community, 'announcement'),
346
+ isCommunity: !!getBinaryNodeChild(community, 'parent'),
347
+ isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_group'),
348
+ joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
349
+ memberAddMode,
350
+ participants: getBinaryNodeChildren(community, 'participant').map(({ attrs }) => {
351
+ return {
352
+ id: mode === 'lid' ? community.phone_number : attrs.jid,
353
+ lid: mode === 'lid' ? community.jid : attrs.lid,
354
+ admin: (attrs.type || null),
355
+ };
356
+ }),
357
+ ephemeralDuration: eph ? +eph : undefined,
358
+ };
359
+
360
+ return metadata;
361
+ };
@@ -994,6 +994,17 @@ export const makeMessagesRecvSocket = (config) => {
994
994
  return null;
995
995
  };
996
996
  const contextInfo = getContextInfo(msgContent);
997
+ // resolve contextInfo.participant (sender of quoted message) jika masih LID
998
+ if (contextInfo?.participant?.endsWith('@lid')) {
999
+ try {
1000
+ const pn = await lidMapping.getPNForLID(contextInfo.participant);
1001
+ if (pn) {
1002
+ logger.debug({ lid: contextInfo.participant, pn }, 'resolved contextInfo.participant LID → PN');
1003
+ contextInfo.participant = pn;
1004
+ }
1005
+ }
1006
+ catch { }
1007
+ }
997
1008
  if (!contextInfo?.mentionedJid?.length)
998
1009
  return;
999
1010
  const hasLid = contextInfo.mentionedJid.some((j) => j?.endsWith('@lid'));
@@ -178,4 +178,27 @@ export const makeNewsletterSocket = (config) => {
178
178
  }
179
179
  };
180
180
  };
181
+ export const extractNewsletterMetadata = (node, isCreate) => {
182
+ const result = getBinaryNodeChild(node, 'result')?.content?.toString()
183
+ const metadataPath = JSON.parse(result).data[isCreate ? XWAPaths.CREATE : XWAPaths.NEWSLETTER]
184
+
185
+ const metadata = {
186
+ id: metadataPath?.id,
187
+ state: metadataPath?.state?.type,
188
+ creation_time: +metadataPath?.thread_metadata?.creation_time,
189
+ name: metadataPath?.thread_metadata?.name?.text,
190
+ nameTime: +metadataPath?.thread_metadata?.name?.update_time,
191
+ description: metadataPath?.thread_metadata?.description?.text,
192
+ descriptionTime: +metadataPath?.thread_metadata?.description?.update_time,
193
+ invite: metadataPath?.thread_metadata?.invite,
194
+ handle: metadataPath?.thread_metadata?.handle,
195
+ picture: getUrlFromDirectPath(metadataPath?.thread_metadata?.picture?.direct_path || ''),
196
+ preview: getUrlFromDirectPath(metadataPath?.thread_metadata?.preview?.direct_path || ''),
197
+ reaction_codes: metadataPath?.thread_metadata?.settings?.reaction_codes?.value,
198
+ subscribers: +metadataPath?.thread_metadata?.subscribers_count,
199
+ verification: metadataPath?.thread_metadata?.verification,
200
+ viewer_metadata: metadataPath?.viewer_metadata
201
+ }
202
+ return metadata;
203
+ };
181
204
  //# sourceMappingURL=newsletter.js.map