@baileys-md/baileys 10.1.0 → 11.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.
Files changed (122) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1303 -2
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +4633 -0
  5. package/WAProto/fix-imports.js +29 -0
  6. package/WAProto/index.js +13516 -4182
  7. package/lib/Defaults/baileys-version.js +1 -0
  8. package/lib/Defaults/index.js +51 -72
  9. package/lib/Signal/Group/ciphertext-message.js +12 -0
  10. package/lib/Signal/Group/group-session-builder.js +30 -0
  11. package/lib/Signal/Group/group_cipher.js +94 -0
  12. package/lib/Signal/Group/index.js +12 -0
  13. package/lib/Signal/Group/keyhelper.js +19 -0
  14. package/lib/Signal/Group/queue-job.js +54 -0
  15. package/lib/Signal/Group/sender-chain-key.js +32 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  17. package/lib/Signal/Group/sender-key-message.js +67 -0
  18. package/lib/Signal/Group/sender-key-name.js +48 -0
  19. package/lib/Signal/Group/sender-key-record.js +50 -0
  20. package/lib/Signal/Group/sender-key-state.js +96 -0
  21. package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +4 -16
  22. package/lib/Signal/libsignal.js +41 -61
  23. package/lib/Socket/Client/index.js +3 -19
  24. package/lib/Socket/Client/types.js +11 -0
  25. package/lib/Socket/Client/websocket.js +50 -0
  26. package/lib/Socket/business.js +37 -42
  27. package/lib/Socket/chats.js +194 -187
  28. package/lib/Socket/communities.js +351 -0
  29. package/lib/Socket/groups.js +87 -90
  30. package/lib/Socket/index.js +7 -8
  31. package/lib/Socket/messages-recv.js +360 -335
  32. package/lib/Socket/messages-send.js +156 -279
  33. package/lib/Socket/mex.js +42 -0
  34. package/lib/Socket/newsletter.js +144 -213
  35. package/lib/Socket/socket.js +128 -161
  36. package/lib/Socket/usync.js +19 -26
  37. package/lib/Types/Auth.js +2 -2
  38. package/lib/Types/Call.js +2 -2
  39. package/lib/Types/Chat.js +8 -4
  40. package/lib/Types/Contact.js +2 -2
  41. package/lib/Types/Events.js +2 -2
  42. package/lib/Types/GroupMetadata.js +2 -2
  43. package/lib/Types/Label.js +3 -5
  44. package/lib/Types/LabelAssociation.js +3 -5
  45. package/lib/Types/Message.js +7 -7
  46. package/lib/Types/Newsletter.js +30 -17
  47. package/lib/Types/Product.js +2 -2
  48. package/lib/Types/Signal.js +2 -2
  49. package/lib/Types/Socket.js +3 -2
  50. package/lib/Types/State.js +2 -2
  51. package/lib/Types/USync.js +2 -2
  52. package/lib/Types/index.js +15 -31
  53. package/lib/Utils/auth-utils.js +31 -47
  54. package/lib/Utils/baileys-event-stream.js +15 -22
  55. package/lib/Utils/business.js +66 -69
  56. package/lib/Utils/chat-utils.js +200 -195
  57. package/lib/Utils/crypto.js +70 -85
  58. package/lib/Utils/decode-wa-message.js +47 -51
  59. package/lib/Utils/event-buffer.js +36 -46
  60. package/lib/Utils/generics.js +116 -188
  61. package/lib/Utils/history.js +37 -46
  62. package/lib/Utils/index.js +19 -33
  63. package/lib/Utils/link-preview.js +14 -55
  64. package/lib/Utils/logger.js +3 -7
  65. package/lib/Utils/lt-hash.js +23 -26
  66. package/lib/{Store → Utils}/make-in-memory-store.js +19 -27
  67. package/lib/Utils/make-mutex.js +7 -10
  68. package/lib/{Store → Utils}/make-ordered-dictionary.js +1 -3
  69. package/lib/Utils/messages-media.js +236 -368
  70. package/lib/Utils/messages.js +278 -510
  71. package/lib/Utils/noise-handler.js +22 -31
  72. package/lib/{Store → Utils}/object-repository.js +1 -4
  73. package/lib/Utils/process-message.js +144 -148
  74. package/lib/Utils/signal.js +71 -64
  75. package/lib/Utils/use-multi-file-auth-state.js +112 -84
  76. package/lib/Utils/validate-connection.js +72 -115
  77. package/lib/WABinary/constants.js +1281 -20
  78. package/lib/WABinary/decode.js +15 -52
  79. package/lib/WABinary/encode.js +14 -48
  80. package/lib/WABinary/generic-utils.js +31 -39
  81. package/lib/WABinary/index.js +6 -21
  82. package/lib/WABinary/jid-utils.js +23 -40
  83. package/lib/WABinary/types.js +2 -2
  84. package/lib/WAM/BinaryInfo.js +2 -5
  85. package/lib/WAM/constants.js +2257 -2366
  86. package/lib/WAM/encode.js +17 -21
  87. package/lib/WAM/index.js +4 -19
  88. package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
  89. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
  90. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
  91. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
  92. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
  93. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +3 -6
  94. package/lib/WAUSync/Protocols/index.js +5 -20
  95. package/lib/WAUSync/USyncQuery.js +34 -32
  96. package/lib/WAUSync/USyncUser.js +2 -5
  97. package/lib/WAUSync/index.js +4 -19
  98. package/lib/index.js +11 -33
  99. package/package.json +25 -54
  100. package/WASignalGroup/GroupProtocol.js +0 -1697
  101. package/WASignalGroup/ciphertext_message.js +0 -16
  102. package/WASignalGroup/group_cipher.js +0 -120
  103. package/WASignalGroup/group_session_builder.js +0 -46
  104. package/WASignalGroup/index.js +0 -5
  105. package/WASignalGroup/keyhelper.js +0 -21
  106. package/WASignalGroup/protobufs.js +0 -3
  107. package/WASignalGroup/queue_job.js +0 -69
  108. package/WASignalGroup/sender_chain_key.js +0 -50
  109. package/WASignalGroup/sender_key_distribution_message.js +0 -78
  110. package/WASignalGroup/sender_key_message.js +0 -92
  111. package/WASignalGroup/sender_key_name.js +0 -70
  112. package/WASignalGroup/sender_key_record.js +0 -56
  113. package/WASignalGroup/sender_key_state.js +0 -129
  114. package/lib/Defaults/baileys-version.json +0 -3
  115. package/lib/Defaults/phonenumber-mcc.json +0 -223
  116. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  117. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  118. package/lib/Socket/Client/web-socket-client.js +0 -62
  119. package/lib/Socket/registration.js +0 -166
  120. package/lib/Store/index.js +0 -8
  121. package/lib/Store/make-cache-manager-store.js +0 -83
  122. package/lib/Store/make-mongo-store.js +0 -567
@@ -0,0 +1,351 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import { WAMessageStubType } from '../Types/index.js';
3
+ import { generateMessageID, generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
4
+ import logger from '../Utils/logger.js';
5
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
6
+ import { makeBusinessSocket } from './business.js';
7
+ export const makeCommunitiesSocket = (config) => {
8
+ const sock = makeBusinessSocket(config);
9
+ const { authState, ev, query, upsertMessage } = sock;
10
+ const communityQuery = async (jid, type, content) => query({
11
+ tag: 'iq',
12
+ attrs: {
13
+ type,
14
+ xmlns: 'w:g2',
15
+ to: jid
16
+ },
17
+ content
18
+ });
19
+ const communityMetadata = async (jid) => {
20
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
21
+ return extractCommunityMetadata(result);
22
+ };
23
+ const communityFetchAllParticipating = async () => {
24
+ const result = await query({
25
+ tag: 'iq',
26
+ attrs: {
27
+ to: '@g.us',
28
+ xmlns: 'w:g2',
29
+ type: 'get'
30
+ },
31
+ content: [
32
+ {
33
+ tag: 'participating',
34
+ attrs: {},
35
+ content: [
36
+ { tag: 'participants', attrs: {} },
37
+ { tag: 'description', attrs: {} }
38
+ ]
39
+ }
40
+ ]
41
+ });
42
+ const data = {};
43
+ const communitiesChild = getBinaryNodeChild(result, 'communities');
44
+ if (communitiesChild) {
45
+ const communities = getBinaryNodeChildren(communitiesChild, 'community');
46
+ for (const communityNode of communities) {
47
+ const meta = extractCommunityMetadata({
48
+ tag: 'result',
49
+ attrs: {},
50
+ content: [communityNode]
51
+ });
52
+ data[meta.id] = meta;
53
+ }
54
+ }
55
+ sock.ev.emit('groups.update', Object.values(data));
56
+ return data;
57
+ };
58
+ async function parseGroupResult(node) {
59
+ logger.info({ node }, 'parseGroupResult');
60
+ const groupNode = getBinaryNodeChild(node, 'group');
61
+ if (groupNode) {
62
+ try {
63
+ logger.info({ groupNode }, 'groupNode');
64
+ const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
65
+ return metadata ? metadata : Optional.empty();
66
+ }
67
+ catch (error) {
68
+ console.error('Error parsing group metadata:', error);
69
+ return Optional.empty();
70
+ }
71
+ }
72
+ return Optional.empty();
73
+ }
74
+ const Optional = {
75
+ empty: () => null,
76
+ of: (value) => (value !== null ? { value } : null)
77
+ };
78
+ sock.ws.on('CB:ib,,dirty', async (node) => {
79
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
80
+ if (attrs.type !== 'communities') {
81
+ return;
82
+ }
83
+ await communityFetchAllParticipating();
84
+ await sock.cleanDirtyBits('groups');
85
+ });
86
+ return {
87
+ ...sock,
88
+ communityMetadata,
89
+ communityCreate: async (subject, body) => {
90
+ const descriptionId = generateMessageID().substring(0, 12);
91
+ const result = await communityQuery('@g.us', 'set', [
92
+ {
93
+ tag: 'create',
94
+ attrs: { subject },
95
+ content: [
96
+ {
97
+ tag: 'description',
98
+ attrs: { id: descriptionId },
99
+ content: [
100
+ {
101
+ tag: 'body',
102
+ attrs: {},
103
+ content: Buffer.from(body || '', 'utf-8')
104
+ }
105
+ ]
106
+ },
107
+ {
108
+ tag: 'parent',
109
+ attrs: { default_membership_approval_mode: 'request_required' }
110
+ },
111
+ {
112
+ tag: 'allow_non_admin_sub_group_creation',
113
+ attrs: {}
114
+ },
115
+ {
116
+ tag: 'create_general_chat',
117
+ attrs: {}
118
+ }
119
+ ]
120
+ }
121
+ ]);
122
+ return await parseGroupResult(result);
123
+ },
124
+ communityLeave: async (id) => {
125
+ await communityQuery('@g.us', 'set', [
126
+ {
127
+ tag: 'leave',
128
+ attrs: {},
129
+ content: [{ tag: 'community', attrs: { id } }]
130
+ }
131
+ ]);
132
+ },
133
+ communityUpdateSubject: async (jid, subject) => {
134
+ await communityQuery(jid, 'set', [
135
+ {
136
+ tag: 'subject',
137
+ attrs: {},
138
+ content: Buffer.from(subject, 'utf-8')
139
+ }
140
+ ]);
141
+ },
142
+ communityRequestParticipantsList: async (jid) => {
143
+ const result = await communityQuery(jid, 'get', [
144
+ {
145
+ tag: 'membership_approval_requests',
146
+ attrs: {}
147
+ }
148
+ ]);
149
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
150
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
151
+ return participants.map(v => v.attrs);
152
+ },
153
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
154
+ const result = await communityQuery(jid, 'set', [
155
+ {
156
+ tag: 'membership_requests_action',
157
+ attrs: {},
158
+ content: [
159
+ {
160
+ tag: action,
161
+ attrs: {},
162
+ content: participants.map(jid => ({
163
+ tag: 'participant',
164
+ attrs: { jid }
165
+ }))
166
+ }
167
+ ]
168
+ }
169
+ ]);
170
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
171
+ const nodeAction = getBinaryNodeChild(node, action);
172
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
173
+ return participantsAffected.map(p => {
174
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
175
+ });
176
+ },
177
+ communityParticipantsUpdate: async (jid, participants, action) => {
178
+ const result = await communityQuery(jid, 'set', [
179
+ {
180
+ tag: action,
181
+ attrs: {},
182
+ content: participants.map(jid => ({
183
+ tag: 'participant',
184
+ attrs: { jid }
185
+ }))
186
+ }
187
+ ]);
188
+ const node = getBinaryNodeChild(result, action);
189
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
190
+ return participantsAffected.map(p => {
191
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
192
+ });
193
+ },
194
+ communityUpdateDescription: async (jid, description) => {
195
+ const metadata = await communityMetadata(jid);
196
+ const prev = metadata.descId ?? null;
197
+ await communityQuery(jid, 'set', [
198
+ {
199
+ tag: 'description',
200
+ attrs: {
201
+ ...(description ? { id: generateMessageID() } : { delete: 'true' }),
202
+ ...(prev ? { prev } : {})
203
+ },
204
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
205
+ }
206
+ ]);
207
+ },
208
+ communityInviteCode: async (jid) => {
209
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
210
+ const inviteNode = getBinaryNodeChild(result, 'invite');
211
+ return inviteNode?.attrs.code;
212
+ },
213
+ communityRevokeInvite: async (jid) => {
214
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
215
+ const inviteNode = getBinaryNodeChild(result, 'invite');
216
+ return inviteNode?.attrs.code;
217
+ },
218
+ communityAcceptInvite: async (code) => {
219
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
220
+ const result = getBinaryNodeChild(results, 'community');
221
+ return result?.attrs.jid;
222
+ },
223
+ /**
224
+ * revoke a v4 invite for someone
225
+ * @param communityJid community jid
226
+ * @param invitedJid jid of person you invited
227
+ * @returns true if successful
228
+ */
229
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
230
+ const result = await communityQuery(communityJid, 'set', [
231
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
232
+ ]);
233
+ return !!result;
234
+ },
235
+ /**
236
+ * accept a CommunityInviteMessage
237
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
238
+ * @param inviteMessage the message to accept
239
+ */
240
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
241
+ key = typeof key === 'string' ? { remoteJid: key } : key;
242
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
243
+ {
244
+ tag: 'accept',
245
+ attrs: {
246
+ code: inviteMessage.inviteCode,
247
+ expiration: inviteMessage.inviteExpiration.toString(),
248
+ admin: key.remoteJid
249
+ }
250
+ }
251
+ ]);
252
+ // if we have the full message key
253
+ // update the invite message to be expired
254
+ if (key.id) {
255
+ // create new invite message that is expired
256
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
257
+ inviteMessage.inviteExpiration = 0;
258
+ inviteMessage.inviteCode = '';
259
+ ev.emit('messages.update', [
260
+ {
261
+ key,
262
+ update: {
263
+ message: {
264
+ groupInviteMessage: inviteMessage
265
+ }
266
+ }
267
+ }
268
+ ]);
269
+ }
270
+ // generate the community add message
271
+ await upsertMessage({
272
+ key: {
273
+ remoteJid: inviteMessage.groupJid,
274
+ id: generateMessageIDV2(sock.user?.id),
275
+ fromMe: false,
276
+ participant: key.remoteJid
277
+ },
278
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
279
+ messageStubParameters: [authState.creds.me.id],
280
+ participant: key.remoteJid,
281
+ messageTimestamp: unixTimestampSeconds()
282
+ }, 'notify');
283
+ return results.attrs.from;
284
+ }),
285
+ communityGetInviteInfo: async (code) => {
286
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
287
+ return extractCommunityMetadata(results);
288
+ },
289
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
290
+ const content = ephemeralExpiration
291
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
292
+ : { tag: 'not_ephemeral', attrs: {} };
293
+ await communityQuery(jid, 'set', [content]);
294
+ },
295
+ communitySettingUpdate: async (jid, setting) => {
296
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
297
+ },
298
+ communityMemberAddMode: async (jid, mode) => {
299
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
300
+ },
301
+ communityJoinApprovalMode: async (jid, mode) => {
302
+ await communityQuery(jid, 'set', [
303
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
304
+ ]);
305
+ },
306
+ communityFetchAllParticipating
307
+ };
308
+ };
309
+ export const extractCommunityMetadata = (result) => {
310
+ const community = getBinaryNodeChild(result, 'community');
311
+ const descChild = getBinaryNodeChild(community, 'description');
312
+ let desc;
313
+ let descId;
314
+ if (descChild) {
315
+ desc = getBinaryNodeChildString(descChild, 'body');
316
+ descId = descChild.attrs.id;
317
+ }
318
+ const communityId = community.attrs.id?.includes('@')
319
+ ? community.attrs.id
320
+ : jidEncode(community.attrs.id || '', 'g.us');
321
+ const eph = getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
322
+ const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
323
+ const metadata = {
324
+ id: communityId,
325
+ subject: community.attrs.subject || '',
326
+ subjectOwner: community.attrs.s_o,
327
+ subjectTime: Number(community.attrs.s_t || 0),
328
+ size: getBinaryNodeChildren(community, 'participant').length,
329
+ creation: Number(community.attrs.creation || 0),
330
+ owner: community.attrs.creator ? jidNormalizedUser(community.attrs.creator) : undefined,
331
+ desc,
332
+ descId,
333
+ linkedParent: getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
334
+ restrict: !!getBinaryNodeChild(community, 'locked'),
335
+ announce: !!getBinaryNodeChild(community, 'announcement'),
336
+ isCommunity: !!getBinaryNodeChild(community, 'parent'),
337
+ isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_community'),
338
+ joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
339
+ memberAddMode,
340
+ participants: getBinaryNodeChildren(community, 'participant').map(({ attrs }) => {
341
+ return {
342
+ id: attrs.jid,
343
+ admin: (attrs.type || null)
344
+ };
345
+ }),
346
+ ephemeralDuration: eph ? +eph : undefined,
347
+ addressingMode: getBinaryNodeChildString(community, 'addressing_mode')
348
+ };
349
+ return metadata;
350
+ };
351
+ //# sourceMappingURL=communities.js.map