@itsliaaa/baileys 0.1.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.
- package/LICENSE +22 -0
- package/README.md +1078 -0
- package/WAProto/index.js +100441 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +144 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.js +402 -0
- package/lib/Signal/lid-mapping.js +270 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +378 -0
- package/lib/Socket/chats.js +1048 -0
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +328 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1442 -0
- package/lib/Socket/messages-send.js +1153 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +227 -0
- package/lib/Socket/socket.js +936 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +421 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +12 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +25 -0
- package/lib/Utils/auth-utils.js +289 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +230 -0
- package/lib/Utils/chat-utils.js +811 -0
- package/lib/Utils/crypto.js +117 -0
- package/lib/Utils/decode-wa-message.js +282 -0
- package/lib/Utils/event-buffer.js +573 -0
- package/lib/Utils/generics.js +385 -0
- package/lib/Utils/history.js +130 -0
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +19 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +7 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/message-retry-manager.js +224 -0
- package/lib/Utils/messages-media.js +789 -0
- package/lib/Utils/messages.js +1832 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +527 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/signal.js +158 -0
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +206 -0
- package/lib/WABinary/constants.js +1300 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +197 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +93 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +72 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
3
|
+
const wMexQuery = (variables, queryId, query, generateMessageTag) => {
|
|
4
|
+
return query({
|
|
5
|
+
tag: 'iq',
|
|
6
|
+
attrs: {
|
|
7
|
+
id: generateMessageTag(),
|
|
8
|
+
type: 'get',
|
|
9
|
+
to: S_WHATSAPP_NET,
|
|
10
|
+
xmlns: 'w:mex'
|
|
11
|
+
},
|
|
12
|
+
content: [
|
|
13
|
+
{
|
|
14
|
+
tag: 'query',
|
|
15
|
+
attrs: { query_id: queryId },
|
|
16
|
+
content: Buffer.from(JSON.stringify({ variables }), 'utf-8')
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
export const executeWMexQuery = async (variables, queryId, dataPath, query, generateMessageTag) => {
|
|
22
|
+
const result = await wMexQuery(variables, queryId, query, generateMessageTag);
|
|
23
|
+
const child = getBinaryNodeChild(result, 'result');
|
|
24
|
+
if (child?.content) {
|
|
25
|
+
const data = JSON.parse(child.content.toString());
|
|
26
|
+
if (data.errors && data.errors.length > 0) {
|
|
27
|
+
const errorMessages = data.errors.map((err) => err.message || 'Unknown error').join(', ');
|
|
28
|
+
const firstError = data.errors[0];
|
|
29
|
+
const errorCode = firstError.extensions?.error_code || 400;
|
|
30
|
+
throw new Boom(`GraphQL server error: ${errorMessages}`, { statusCode: errorCode, data: firstError });
|
|
31
|
+
}
|
|
32
|
+
const response = dataPath ? data?.data?.[dataPath] : data?.data;
|
|
33
|
+
if (typeof response !== 'undefined') {
|
|
34
|
+
return response;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const action = (dataPath || '').startsWith('xwa2_')
|
|
38
|
+
? dataPath.substring(5).replace(/_/g, ' ')
|
|
39
|
+
: dataPath?.replace(/_/g, ' ');
|
|
40
|
+
throw new Boom(`Failed to ${action}, unexpected response structure.`, { statusCode: 400, data: result });
|
|
41
|
+
};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import { QueryIds, XWAPaths } from '../Types/index.js';
|
|
3
|
+
import { generateProfilePicture } from '../Utils/messages-media.js';
|
|
4
|
+
import { getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
5
|
+
import { makeGroupsSocket } from './groups.js';
|
|
6
|
+
import { executeWMexQuery as genericExecuteWMexQuery } from './mex.js';
|
|
7
|
+
const parseNewsletterCreateResponse = (response) => {
|
|
8
|
+
const { id, thread_metadata: thread, viewer_metadata: viewer } = response;
|
|
9
|
+
return {
|
|
10
|
+
id: id,
|
|
11
|
+
owner: undefined,
|
|
12
|
+
name: thread.name.text,
|
|
13
|
+
creation_time: parseInt(thread.creation_time, 10),
|
|
14
|
+
description: thread.description.text,
|
|
15
|
+
invite: thread.invite,
|
|
16
|
+
subscribers: parseInt(thread.subscribers_count, 10),
|
|
17
|
+
verification: thread.verification,
|
|
18
|
+
picture: {
|
|
19
|
+
id: thread.picture?.id,
|
|
20
|
+
directPath: thread.picture?.direct_path
|
|
21
|
+
},
|
|
22
|
+
mute_state: viewer.mute
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const parseNewsletterMetadata = (result) => {
|
|
26
|
+
if (typeof result !== 'object' || result === null) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
if ('id' in result && typeof result.id === 'string') {
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
if ('result' in result && typeof result.result === 'object' && result.result !== null && 'id' in result.result) {
|
|
33
|
+
return result.result;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
};
|
|
37
|
+
export const makeNewsletterSocket = (config) => {
|
|
38
|
+
const sock = makeGroupsSocket(config);
|
|
39
|
+
const { query, generateMessageTag } = sock;
|
|
40
|
+
const { logger } = config;
|
|
41
|
+
const executeWMexQuery = (variables, queryId, dataPath) => {
|
|
42
|
+
return genericExecuteWMexQuery(variables, queryId, dataPath, query, generateMessageTag);
|
|
43
|
+
};
|
|
44
|
+
const newsletterUpdate = async (jid, updates) => {
|
|
45
|
+
const variables = {
|
|
46
|
+
newsletter_id: jid,
|
|
47
|
+
updates: {
|
|
48
|
+
...updates,
|
|
49
|
+
settings: null
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return executeWMexQuery(variables, QueryIds.UPDATE_METADATA, 'xwa2_newsletter_update');
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
...sock,
|
|
56
|
+
executeWMexQuery,
|
|
57
|
+
newsletterCreate: async (name, description) => {
|
|
58
|
+
const variables = {
|
|
59
|
+
input: {
|
|
60
|
+
name,
|
|
61
|
+
description: description ?? null
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const rawResponse = await executeWMexQuery(variables, QueryIds.CREATE, XWAPaths.xwa2_newsletter_create);
|
|
65
|
+
return parseNewsletterCreateResponse(rawResponse);
|
|
66
|
+
},
|
|
67
|
+
newsletterUpdate,
|
|
68
|
+
newsletterSubscribers: async (jid) => {
|
|
69
|
+
return executeWMexQuery({ newsletter_id: jid }, QueryIds.SUBSCRIBERS, XWAPaths.xwa2_newsletter_subscribers);
|
|
70
|
+
},
|
|
71
|
+
// Lia@Changes 29-01-26 --- Add newsletterSubscribed to fetch all subscribed newsletters (similar to groupFetchAllParticipating ( ╹▽╹ ))
|
|
72
|
+
newsletterSubscribed: async () => {
|
|
73
|
+
return executeWMexQuery({}, QueryIds.SUBSCRIBED, XWAPaths.xwa2_newsletter_subscribed);
|
|
74
|
+
},
|
|
75
|
+
newsletterMetadata: async (type, key) => {
|
|
76
|
+
const variables = {
|
|
77
|
+
fetch_creation_time: true,
|
|
78
|
+
fetch_full_image: true,
|
|
79
|
+
fetch_viewer_metadata: true,
|
|
80
|
+
input: {
|
|
81
|
+
key,
|
|
82
|
+
type: type.toUpperCase()
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const result = await executeWMexQuery(variables, QueryIds.METADATA, XWAPaths.xwa2_newsletter_metadata);
|
|
86
|
+
return parseNewsletterMetadata(result);
|
|
87
|
+
},
|
|
88
|
+
newsletterFollow: (jid) => {
|
|
89
|
+
return executeWMexQuery({ newsletter_id: jid }, QueryIds.FOLLOW, XWAPaths.xwa2_newsletter_follow);
|
|
90
|
+
},
|
|
91
|
+
newsletterUnfollow: (jid) => {
|
|
92
|
+
return executeWMexQuery({ newsletter_id: jid }, QueryIds.UNFOLLOW, XWAPaths.xwa2_newsletter_unfollow);
|
|
93
|
+
},
|
|
94
|
+
newsletterMute: (jid) => {
|
|
95
|
+
return executeWMexQuery({ newsletter_id: jid }, QueryIds.MUTE, XWAPaths.xwa2_newsletter_mute_v2);
|
|
96
|
+
},
|
|
97
|
+
newsletterUnmute: (jid) => {
|
|
98
|
+
return executeWMexQuery({ newsletter_id: jid }, QueryIds.UNMUTE, XWAPaths.xwa2_newsletter_unmute_v2);
|
|
99
|
+
},
|
|
100
|
+
newsletterUpdateName: async (jid, name) => {
|
|
101
|
+
return await newsletterUpdate(jid, { name });
|
|
102
|
+
},
|
|
103
|
+
newsletterUpdateDescription: async (jid, description) => {
|
|
104
|
+
return await newsletterUpdate(jid, { description });
|
|
105
|
+
},
|
|
106
|
+
newsletterUpdatePicture: async (jid, content) => {
|
|
107
|
+
const { img } = await generateProfilePicture(content);
|
|
108
|
+
return await newsletterUpdate(jid, { picture: img.toString('base64') });
|
|
109
|
+
},
|
|
110
|
+
newsletterRemovePicture: async (jid) => {
|
|
111
|
+
return await newsletterUpdate(jid, { picture: '' });
|
|
112
|
+
},
|
|
113
|
+
newsletterReactMessage: async (jid, serverId, reaction) => {
|
|
114
|
+
await query({
|
|
115
|
+
tag: 'message',
|
|
116
|
+
attrs: {
|
|
117
|
+
to: jid,
|
|
118
|
+
...(reaction ? {} : { edit: '7' }),
|
|
119
|
+
type: 'reaction',
|
|
120
|
+
server_id: serverId,
|
|
121
|
+
id: generateMessageTag()
|
|
122
|
+
},
|
|
123
|
+
content: [
|
|
124
|
+
{
|
|
125
|
+
tag: 'reaction',
|
|
126
|
+
attrs: reaction ? { code: reaction } : {}
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
newsletterFetchMessages: async (type, key, count, after, before) => {
|
|
132
|
+
// WA Web: GetNewsletterMessages endpoint
|
|
133
|
+
// Request: iq(to=s.whatsapp.net) -> messages(count, type, jid|key, [after|before])
|
|
134
|
+
const messagesAttrs = {
|
|
135
|
+
count: count.toString(),
|
|
136
|
+
type,
|
|
137
|
+
[type === 'jid' ? 'jid' : 'key']: key
|
|
138
|
+
};
|
|
139
|
+
if (after) {
|
|
140
|
+
messagesAttrs.after = after.toString();
|
|
141
|
+
}
|
|
142
|
+
if (before) {
|
|
143
|
+
messagesAttrs.before = before.toString();
|
|
144
|
+
}
|
|
145
|
+
const result = await query({
|
|
146
|
+
tag: 'iq',
|
|
147
|
+
attrs: {
|
|
148
|
+
id: generateMessageTag(),
|
|
149
|
+
type: 'get',
|
|
150
|
+
xmlns: 'newsletter',
|
|
151
|
+
to: S_WHATSAPP_NET
|
|
152
|
+
},
|
|
153
|
+
content: [
|
|
154
|
+
{
|
|
155
|
+
tag: 'messages',
|
|
156
|
+
attrs: messagesAttrs
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
});
|
|
160
|
+
// Response: iq -> messages(jid="newsletter_jid") -> message[]
|
|
161
|
+
const messagesNode = getBinaryNodeChild(result, 'messages');
|
|
162
|
+
if (!messagesNode) {
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
// The response messages node carries the newsletter JID
|
|
166
|
+
const newsletterJid = messagesNode.attrs.jid || (type === 'jid' ? key : undefined);
|
|
167
|
+
const messages = [];
|
|
168
|
+
// WA Web: mapChildrenWithTag(messages, "message", 0, 300, ...)
|
|
169
|
+
for (const child of getBinaryNodeChildren(messagesNode, 'message')) {
|
|
170
|
+
const plaintextNode = getBinaryNodeChild(child, 'plaintext');
|
|
171
|
+
if (!plaintextNode?.content) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const contentBuf =
|
|
176
|
+
typeof plaintextNode.content === 'string'
|
|
177
|
+
? Buffer.from(plaintextNode.content, 'binary')
|
|
178
|
+
: Buffer.from(plaintextNode.content);
|
|
179
|
+
const messageProto = proto.Message.decode(contentBuf).toJSON();
|
|
180
|
+
const fullMessage = proto.WebMessageInfo.fromObject({
|
|
181
|
+
key: {
|
|
182
|
+
remoteJid: newsletterJid,
|
|
183
|
+
id: child.attrs.id || child.attrs.server_id,
|
|
184
|
+
server_id: child.attrs.server_id,
|
|
185
|
+
fromMe: false
|
|
186
|
+
},
|
|
187
|
+
message: messageProto,
|
|
188
|
+
messageTimestamp: child.attrs.t ? +child.attrs.t : undefined
|
|
189
|
+
}).toJSON();
|
|
190
|
+
messages.push(fullMessage);
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
logger.error({ error }, 'Failed to decode newsletter message');
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return messages;
|
|
197
|
+
},
|
|
198
|
+
subscribeNewsletterUpdates: async (jid) => {
|
|
199
|
+
const result = await query({
|
|
200
|
+
tag: 'iq',
|
|
201
|
+
attrs: {
|
|
202
|
+
id: generateMessageTag(),
|
|
203
|
+
type: 'set',
|
|
204
|
+
xmlns: 'newsletter',
|
|
205
|
+
to: jid
|
|
206
|
+
},
|
|
207
|
+
content: [{ tag: 'live_updates', attrs: {}, content: [] }]
|
|
208
|
+
});
|
|
209
|
+
const liveUpdatesNode = getBinaryNodeChild(result, 'live_updates');
|
|
210
|
+
const duration = liveUpdatesNode?.attrs?.duration;
|
|
211
|
+
return duration ? { duration: duration } : null;
|
|
212
|
+
},
|
|
213
|
+
newsletterAdminCount: async (jid) => {
|
|
214
|
+
const response = await executeWMexQuery({ newsletter_id: jid }, QueryIds.ADMIN_COUNT, XWAPaths.xwa2_newsletter_admin_count);
|
|
215
|
+
return response.admin_count;
|
|
216
|
+
},
|
|
217
|
+
newsletterChangeOwner: async (jid, newOwnerJid) => {
|
|
218
|
+
await executeWMexQuery({ newsletter_id: jid, user_id: newOwnerJid }, QueryIds.CHANGE_OWNER, XWAPaths.xwa2_newsletter_change_owner);
|
|
219
|
+
},
|
|
220
|
+
newsletterDemote: async (jid, userJid) => {
|
|
221
|
+
await executeWMexQuery({ newsletter_id: jid, user_id: userJid }, QueryIds.DEMOTE, XWAPaths.xwa2_newsletter_demote);
|
|
222
|
+
},
|
|
223
|
+
newsletterDelete: async (jid) => {
|
|
224
|
+
await executeWMexQuery({ newsletter_id: jid }, QueryIds.DELETE, XWAPaths.xwa2_newsletter_delete_v2);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
};
|