@sgintokic/baileys 0.0.2

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.

Potentially problematic release.


This version of @sgintokic/baileys might be problematic. Click here for more details.

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