@nexustechpro/baileys 1.0.1

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.
Files changed (101) hide show
  1. package/README.md +1573 -0
  2. package/WAProto/fix-imports.js +29 -0
  3. package/WAProto/index.js +169659 -0
  4. package/lib/Defaults/index.js +115 -0
  5. package/lib/Signal/Group/ciphertext-message.js +12 -0
  6. package/lib/Signal/Group/group-session-builder.js +30 -0
  7. package/lib/Signal/Group/group_cipher.js +82 -0
  8. package/lib/Signal/Group/index.js +12 -0
  9. package/lib/Signal/Group/keyhelper.js +18 -0
  10. package/lib/Signal/Group/sender-chain-key.js +26 -0
  11. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  12. package/lib/Signal/Group/sender-key-message.js +66 -0
  13. package/lib/Signal/Group/sender-key-name.js +48 -0
  14. package/lib/Signal/Group/sender-key-record.js +41 -0
  15. package/lib/Signal/Group/sender-key-state.js +84 -0
  16. package/lib/Signal/Group/sender-message-key.js +26 -0
  17. package/lib/Signal/libsignal.js +342 -0
  18. package/lib/Signal/lid-mapping.js +171 -0
  19. package/lib/Socket/Client/index.js +3 -0
  20. package/lib/Socket/Client/types.js +11 -0
  21. package/lib/Socket/Client/websocket.js +91 -0
  22. package/lib/Socket/business.js +376 -0
  23. package/lib/Socket/chats.js +963 -0
  24. package/lib/Socket/communities.js +431 -0
  25. package/lib/Socket/groups.js +328 -0
  26. package/lib/Socket/index.js +19 -0
  27. package/lib/Socket/messages-recv.js +1240 -0
  28. package/lib/Socket/messages-send.js +1370 -0
  29. package/lib/Socket/mex.js +42 -0
  30. package/lib/Socket/newsletter.js +202 -0
  31. package/lib/Socket/nexus-handler.js +667 -0
  32. package/lib/Socket/socket.js +871 -0
  33. package/lib/Store/index.js +4 -0
  34. package/lib/Store/make-cache-manager-store.js +81 -0
  35. package/lib/Store/make-in-memory-store.js +416 -0
  36. package/lib/Store/make-ordered-dictionary.js +82 -0
  37. package/lib/Store/object-repository.js +31 -0
  38. package/lib/Types/Auth.js +2 -0
  39. package/lib/Types/Bussines.js +2 -0
  40. package/lib/Types/Call.js +2 -0
  41. package/lib/Types/Chat.js +8 -0
  42. package/lib/Types/Contact.js +2 -0
  43. package/lib/Types/Events.js +2 -0
  44. package/lib/Types/GroupMetadata.js +2 -0
  45. package/lib/Types/Label.js +25 -0
  46. package/lib/Types/LabelAssociation.js +7 -0
  47. package/lib/Types/Message.js +11 -0
  48. package/lib/Types/Newsletter.js +31 -0
  49. package/lib/Types/Product.js +2 -0
  50. package/lib/Types/Signal.js +2 -0
  51. package/lib/Types/Socket.js +3 -0
  52. package/lib/Types/State.js +13 -0
  53. package/lib/Types/USync.js +2 -0
  54. package/lib/Types/index.js +26 -0
  55. package/lib/Utils/auth-utils.js +257 -0
  56. package/lib/Utils/baileys-event-stream.js +56 -0
  57. package/lib/Utils/browser-utils.js +28 -0
  58. package/lib/Utils/business.js +231 -0
  59. package/lib/Utils/chat-utils.js +763 -0
  60. package/lib/Utils/crypto.js +142 -0
  61. package/lib/Utils/decode-wa-message.js +279 -0
  62. package/lib/Utils/event-buffer.js +548 -0
  63. package/lib/Utils/generics.js +381 -0
  64. package/lib/Utils/history.js +84 -0
  65. package/lib/Utils/index.js +20 -0
  66. package/lib/Utils/link-preview.js +85 -0
  67. package/lib/Utils/logger.js +3 -0
  68. package/lib/Utils/lt-hash.js +48 -0
  69. package/lib/Utils/make-mutex.js +40 -0
  70. package/lib/Utils/message-retry-manager.js +149 -0
  71. package/lib/Utils/messages-media.js +685 -0
  72. package/lib/Utils/messages.js +820 -0
  73. package/lib/Utils/noise-handler.js +147 -0
  74. package/lib/Utils/pre-key-manager.js +106 -0
  75. package/lib/Utils/process-message.js +413 -0
  76. package/lib/Utils/signal.js +159 -0
  77. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  78. package/lib/Utils/validate-connection.js +195 -0
  79. package/lib/WABinary/constants.js +1301 -0
  80. package/lib/WABinary/decode.js +238 -0
  81. package/lib/WABinary/encode.js +216 -0
  82. package/lib/WABinary/generic-utils.js +111 -0
  83. package/lib/WABinary/index.js +6 -0
  84. package/lib/WABinary/jid-utils.js +96 -0
  85. package/lib/WABinary/types.js +2 -0
  86. package/lib/WAM/BinaryInfo.js +10 -0
  87. package/lib/WAM/constants.js +22853 -0
  88. package/lib/WAM/encode.js +150 -0
  89. package/lib/WAM/index.js +4 -0
  90. package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
  91. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  92. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  93. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  94. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  95. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  96. package/lib/WAUSync/Protocols/index.js +5 -0
  97. package/lib/WAUSync/USyncQuery.js +94 -0
  98. package/lib/WAUSync/USyncUser.js +23 -0
  99. package/lib/WAUSync/index.js +4 -0
  100. package/lib/index.js +24 -0
  101. package/package.json +113 -0
@@ -0,0 +1,328 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import { WAMessageAddressingMode, WAMessageStubType } from '../Types/index.js';
3
+ import { generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
4
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
5
+ import { makeChatsSocket } from './chats.js';
6
+ export const makeGroupsSocket = (config) => {
7
+ const sock = makeChatsSocket(config);
8
+ const { authState, ev, query, upsertMessage } = sock;
9
+ const groupQuery = async (jid, type, content) => query({
10
+ tag: 'iq',
11
+ attrs: {
12
+ type,
13
+ xmlns: 'w:g2',
14
+ to: jid
15
+ },
16
+ content
17
+ });
18
+ const groupMetadata = async (jid) => {
19
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
20
+ return extractGroupMetadata(result);
21
+ };
22
+ const groupFetchAllParticipating = async () => {
23
+ const result = await query({
24
+ tag: 'iq',
25
+ attrs: {
26
+ to: '@g.us',
27
+ xmlns: 'w:g2',
28
+ type: 'get'
29
+ },
30
+ content: [
31
+ {
32
+ tag: 'participating',
33
+ attrs: {},
34
+ content: [
35
+ { tag: 'participants', attrs: {} },
36
+ { tag: 'description', attrs: {} }
37
+ ]
38
+ }
39
+ ]
40
+ });
41
+ const data = {};
42
+ const groupsChild = getBinaryNodeChild(result, 'groups');
43
+ if (groupsChild) {
44
+ const groups = getBinaryNodeChildren(groupsChild, 'group');
45
+ for (const groupNode of groups) {
46
+ const meta = extractGroupMetadata({
47
+ tag: 'result',
48
+ attrs: {},
49
+ content: [groupNode]
50
+ });
51
+ data[meta.id] = meta;
52
+ }
53
+ }
54
+ // TODO: properly parse LID / PN DATA
55
+ sock.ev.emit('groups.update', Object.values(data));
56
+ return data;
57
+ };
58
+ sock.ws.on('CB:ib,,dirty', async (node) => {
59
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
60
+ if (attrs.type !== 'groups') {
61
+ return;
62
+ }
63
+ await groupFetchAllParticipating();
64
+ await sock.cleanDirtyBits('groups');
65
+ });
66
+ return {
67
+ ...sock,
68
+ groupMetadata,
69
+ groupCreate: async (subject, participants) => {
70
+ const key = generateMessageIDV2();
71
+ const result = await groupQuery('@g.us', 'set', [
72
+ {
73
+ tag: 'create',
74
+ attrs: {
75
+ subject,
76
+ key
77
+ },
78
+ content: participants.map(jid => ({
79
+ tag: 'participant',
80
+ attrs: { jid }
81
+ }))
82
+ }
83
+ ]);
84
+ return extractGroupMetadata(result);
85
+ },
86
+ groupLeave: async (id) => {
87
+ await groupQuery('@g.us', 'set', [
88
+ {
89
+ tag: 'leave',
90
+ attrs: {},
91
+ content: [{ tag: 'group', attrs: { id } }]
92
+ }
93
+ ]);
94
+ },
95
+ groupUpdateSubject: async (jid, subject) => {
96
+ await groupQuery(jid, 'set', [
97
+ {
98
+ tag: 'subject',
99
+ attrs: {},
100
+ content: Buffer.from(subject, 'utf-8')
101
+ }
102
+ ]);
103
+ },
104
+ groupRequestParticipantsList: async (jid) => {
105
+ const result = await groupQuery(jid, 'get', [
106
+ {
107
+ tag: 'membership_approval_requests',
108
+ attrs: {}
109
+ }
110
+ ]);
111
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
112
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
113
+ return participants.map(v => v.attrs);
114
+ },
115
+ groupRequestParticipantsUpdate: async (jid, participants, action) => {
116
+ const result = await groupQuery(jid, 'set', [
117
+ {
118
+ tag: 'membership_requests_action',
119
+ attrs: {},
120
+ content: [
121
+ {
122
+ tag: action,
123
+ attrs: {},
124
+ content: participants.map(jid => ({
125
+ tag: 'participant',
126
+ attrs: { jid }
127
+ }))
128
+ }
129
+ ]
130
+ }
131
+ ]);
132
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
133
+ const nodeAction = getBinaryNodeChild(node, action);
134
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
135
+ return participantsAffected.map(p => {
136
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
137
+ });
138
+ },
139
+ groupParticipantsUpdate: async (jid, participants, action) => {
140
+ const result = await groupQuery(jid, 'set', [
141
+ {
142
+ tag: action,
143
+ attrs: {},
144
+ content: participants.map(jid => ({
145
+ tag: 'participant',
146
+ attrs: { jid }
147
+ }))
148
+ }
149
+ ]);
150
+ const node = getBinaryNodeChild(result, action);
151
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
152
+ return participantsAffected.map(p => {
153
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
154
+ });
155
+ },
156
+ groupUpdateDescription: async (jid, description) => {
157
+ const metadata = await groupMetadata(jid);
158
+ const prev = metadata.descId ?? null;
159
+ await groupQuery(jid, 'set', [
160
+ {
161
+ tag: 'description',
162
+ attrs: {
163
+ ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
164
+ ...(prev ? { prev } : {})
165
+ },
166
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
167
+ }
168
+ ]);
169
+ },
170
+ groupInviteCode: async (jid) => {
171
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
172
+ const inviteNode = getBinaryNodeChild(result, 'invite');
173
+ return inviteNode?.attrs.code;
174
+ },
175
+ groupRevokeInvite: async (jid) => {
176
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
177
+ const inviteNode = getBinaryNodeChild(result, 'invite');
178
+ return inviteNode?.attrs.code;
179
+ },
180
+ groupAcceptInvite: async (code) => {
181
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
182
+ const result = getBinaryNodeChild(results, 'group');
183
+ return result?.attrs.jid;
184
+ },
185
+ /**
186
+ * revoke a v4 invite for someone
187
+ * @param groupJid group jid
188
+ * @param invitedJid jid of person you invited
189
+ * @returns true if successful
190
+ */
191
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
192
+ const result = await groupQuery(groupJid, 'set', [
193
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
194
+ ]);
195
+ return !!result;
196
+ },
197
+ /**
198
+ * accept a GroupInviteMessage
199
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
200
+ * @param inviteMessage the message to accept
201
+ */
202
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
203
+ key = typeof key === 'string' ? { remoteJid: key } : key;
204
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
205
+ {
206
+ tag: 'accept',
207
+ attrs: {
208
+ code: inviteMessage.inviteCode,
209
+ expiration: inviteMessage.inviteExpiration.toString(),
210
+ admin: key.remoteJid
211
+ }
212
+ }
213
+ ]);
214
+ // if we have the full message key
215
+ // update the invite message to be expired
216
+ if (key.id) {
217
+ // create new invite message that is expired
218
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
219
+ inviteMessage.inviteExpiration = 0;
220
+ inviteMessage.inviteCode = '';
221
+ ev.emit('messages.update', [
222
+ {
223
+ key,
224
+ update: {
225
+ message: {
226
+ groupInviteMessage: inviteMessage
227
+ }
228
+ }
229
+ }
230
+ ]);
231
+ }
232
+ // generate the group add message
233
+ await upsertMessage({
234
+ key: {
235
+ remoteJid: inviteMessage.groupJid,
236
+ id: generateMessageIDV2(sock.user?.id),
237
+ fromMe: false,
238
+ participant: key.remoteJid
239
+ },
240
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
241
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
242
+ participant: key.remoteJid,
243
+ messageTimestamp: unixTimestampSeconds()
244
+ }, 'notify');
245
+ return results.attrs.from;
246
+ }),
247
+ groupGetInviteInfo: async (code) => {
248
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
249
+ return extractGroupMetadata(results);
250
+ },
251
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
252
+ const content = ephemeralExpiration
253
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
254
+ : { tag: 'not_ephemeral', attrs: {} };
255
+ await groupQuery(jid, 'set', [content]);
256
+ },
257
+ groupSettingUpdate: async (jid, setting) => {
258
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
259
+ },
260
+ groupMemberAddMode: async (jid, mode) => {
261
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
262
+ },
263
+ groupJoinApprovalMode: async (jid, mode) => {
264
+ await groupQuery(jid, 'set', [
265
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
266
+ ]);
267
+ },
268
+ groupFetchAllParticipating
269
+ };
270
+ };
271
+ export const extractGroupMetadata = (result) => {
272
+ const group = getBinaryNodeChild(result, 'group');
273
+ const descChild = getBinaryNodeChild(group, 'description');
274
+ let desc;
275
+ let descId;
276
+ let descOwner;
277
+ let descOwnerPn;
278
+ let descTime;
279
+ if (descChild) {
280
+ desc = getBinaryNodeChildString(descChild, 'body');
281
+ descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
282
+ descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
283
+ descTime = +descChild.attrs.t;
284
+ descId = descChild.attrs.id;
285
+ }
286
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
287
+ const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
288
+ const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
289
+ const metadata = {
290
+ id: groupId,
291
+ notify: group.attrs.notify,
292
+ addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
293
+ subject: group.attrs.subject,
294
+ subjectOwner: group.attrs.s_o,
295
+ subjectOwnerPn: group.attrs.s_o_pn,
296
+ subjectTime: +group.attrs.s_t,
297
+ size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
298
+ creation: +group.attrs.creation,
299
+ owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
300
+ ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
301
+ owner_country_code: group.attrs.creator_country_code,
302
+ desc,
303
+ descId,
304
+ descOwner,
305
+ descOwnerPn,
306
+ descTime,
307
+ linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
308
+ restrict: !!getBinaryNodeChild(group, 'locked'),
309
+ announce: !!getBinaryNodeChild(group, 'announcement'),
310
+ isCommunity: !!getBinaryNodeChild(group, 'parent'),
311
+ isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
312
+ joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
313
+ memberAddMode,
314
+ participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
315
+ // Store both ID and phone number for compatibility
316
+ return {
317
+ id: attrs.jid,
318
+ jid: attrs.phone_number || attrs.jid, // Rexx-style jid field for backwards compatibility
319
+ phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
320
+ lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
321
+ admin: (attrs.type || null)
322
+ };
323
+ }),
324
+ ephemeralDuration: eph ? +eph : undefined
325
+ };
326
+ return metadata;
327
+ };
328
+ //# sourceMappingURL=groups.js.map
@@ -0,0 +1,19 @@
1
+ import { DEFAULT_CONNECTION_CONFIG } from '../Defaults/index.js';
2
+ import { makeCommunitiesSocket } from './communities.js';
3
+ import NexusHandler from './nexus-handler.js';
4
+ // export the last socket layer
5
+ const makeWASocket = (config) => {
6
+ const newConfig = {
7
+ ...DEFAULT_CONNECTION_CONFIG,
8
+ ...config
9
+ };
10
+ // If the user hasn't provided their own history sync function,
11
+ // let's create a default one that respects the syncFullHistory flag.
12
+ if (config.shouldSyncHistoryMessage === undefined) {
13
+ newConfig.shouldSyncHistoryMessage = () => !!newConfig.syncFullHistory;
14
+ }
15
+ return makeCommunitiesSocket(newConfig);
16
+ };
17
+ export { NexusHandler };
18
+ export default makeWASocket;
19
+ //# sourceMappingURL=index.js.map