-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
393 lines (332 loc) · 12.3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* hazirlik
*/
"use strict";
const QRCode = require('qrcode-terminal');
const { Client, LocalAuth, MessageAck } = require('whatsapp-web.js');
const assert = require('node:assert');
const process = require('node:process');
let whitelist = [];
let config;
try {
config = require('./config.json');
assert(config.statusgroup !== undefined);
whitelist = [config.statusgroup];
if (config.usergroup !== undefined) {
whitelist.push(config.usergroup);
}
if (config.usergroups !== undefined) {
config.usergroups.forEach((e) => {
assert(typeof e === 'string');
whitelist.push(e);
});
}
} catch (err) {
config = {};
}
console.log("whitelist:", whitelist);
const db = require ('./db.js');
const convert = require ('./converters');
const uuidv4 = require('uuid').v4;
const crypto = require('crypto');
const { TextEncoder } = require('util');
const axios = require('axios');
const { unescape } = require('node:querystring');
/*
* cercop
*/
const HELP = `Komutlar:
- *!ping*: alintili ping
- *!alo*: alintisiz ping
- *!tview* <symbol>:
`;
const log = {
write: (ctx, ...args) => {
ctx = (new Date().toISOString().replace("T", " ").split(".")[0])
+ (ctx + "| ").padStart(10);
console.log(ctx, ...args);
},
fmt: {
is_string: (s) => { return Object.prototype.toString.call(s) === '[object String]' },
timestamp: (t) => {
if (! (t instanceof Date)) {
t = new Date(t);
}
return t.toISOString().replace("T", " ").split(".")[0];
},
preamble: (message, contact, chat) => {
let retval = log.fmt.timestamp(message.timestamp * 1000);
let name = contact.pushname;
if (! log.fmt.is_string(name)) {
name = contact.name;
}
if (! log.fmt.is_string(name)) {
name = contact.number;
}
if (! log.fmt.is_string(name)) {
console.error("Unable to read contact name from:", contact);
name = "??????";
}
retval += " " + ("<" + name.substring(0,12) + ">").padStart(14);
if (chat.isGroup) {
if (chat.name !== undefined) {
retval += " " + ("("+ chat.name.substring(0,10) +")").padEnd(12);
} else {
retval += " " + ("("+ "??????????" +")").padEnd(12);
}
}
return retval;
}
},
command: (...args) => { log.write("command", ...args); },
message: (...args) => { log.write("message", ...args); },
status: (...args) => { log.write("status", ...args); },
voice: (...args) => { log.write("voice", ...args); },
debug: (...args) => { log.write("debug", ...args); },
};
const biara = (f) => {
let delay_ms = (Math.random() * 10000) + 2000;
return new Promise((resolve) => {
setTimeout(() => { resolve(f()); }, delay_ms);
});
};
/*
* komutlar
*/
const command = {
exists: (s) => { return (command[s] !== undefined); },
"!tview": async (message, contact, chat) => {
const args = message.body.split(' ').filter(s => s);
if (args.length < 2) {
let retval = "yanlis: " + message.body;
await biara(() => { client.sendMessage(config.statusgroup, retval); });
return;
}
let retval = '';
for (let i = 1; i < args.length; ++i) {
let symbol = args[i].toUpperCase();
let source = tview.sources[symbol];
if (source === undefined) {
let hata = symbol + " icin kaynak tanimli degil";
log.command(hata);
await biara(() => { client.sendMessage(config.statusgroup, hata); });
return;
}
let data = await tview.get(symbol, source);
log.debug(symbol, "response:", data);
if (i > 1) {
retval+= '\n';
}
retval += symbol + ': ' + data.lp;
}
await biara(() => { message.reply(retval); });
return retval;
},
"!alo": async (message, contact, chat) => {
let retval = 'ne var';
await biara(() => { client.sendMessage(message.from, retval); });
return retval;
},
"!ping": async (message, contact, chat) => {
let retval = 'pong';
await biara(() => { message.reply('pong'); });
return retval;
},
"!nedir": async (message, contact, chat) => {
await biara(() => { message.reply(HELP); });
},
};
/*
* handlers
*/
const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: {
executablePath: '/usr/bin/google-chrome-stable'
}
});
client.on('qr', (qr) => {
log.status("Got QR");
QRCode.generate(qr, {small: true});
});
client.on('ready', async () => {
log.status("Ready");
//await biara(() => { client.sendMessage(config.statusgroup, "hop"); });
});
client.on('message_create', async (message) => {
if (message.isStatus) {
return;
}
if (message.type === 'e2e_notification'){
return;
}
let ClientPushname;
if (client.info.pushname === null && client.info.pushname === undefined) {
ClientPushname = '';
} else {
ClientPushname = client.info.pushname;
}
let chat = await message.getChat();
let contact = await message.getContact();
let preamble = log.fmt.preamble(message, contact, chat)
log.debug(message);
log.message(preamble, message.body);
let rd_uuidv = '{' + uuidv4() + '}';
const folderUUID = '{' + uuidv4() + '}';
console.log("Message UUID: ", rd_uuidv);
console.log("Folder UUID: ", folderUUID);
let irtMUUID = '{00000000-0000-0000-0000-000000000000}';
let quotedMsg_MIME_ID = null;
if (message.hasQuotedMsg) { //if there is a quoted message and it's in database
quotedMsg_MIME_ID = (await message.getQuotedMessage())._data.id._serialized;
if((await db.quotedMessageIsInDb(quotedMsg_MIME_ID)) === 1){
irtMUUID = await db.getMessageIRT(quotedMsg_MIME_ID);
}
}
let header = '[]';
let fromName = '';
let toName = '';
let sender = '';
let recipient = '';
if (chat.isGroup){
if(message.fromMe){
header = convert.hd4Groups(message.from, client.pushname, chat.id._serialized, chat.name, chat.name);
} else {
header = convert.hd4Groups((await message.getContact()).id._serialized, message.author, chat.id._serialized, chat.name, chat.name);
}
} else {
if(message.fromMe){
fromName = ClientPushname;
toName = chat.name;
} else {
fromName = chat.name;
toName = ClientPushname;
}
header = convert.hd4Direct(message.from, fromName, message.to);
}
sender = convert.SenderOrRecipientJSON(message.from, fromName);
recipient = convert.SenderOrRecipientJSON(message.to, toName);
// If message is not empty, do fill body_blob and preview columns in database
let bodyBlob = null;
let preview = null;
if (message.body !== null && message.body !== undefined && message.body !== ''){
const encodedText = new TextEncoder().encode(message.body);
preview = encodedText.slice(0, 256);
const mHash_SHA512 = crypto.createHash('sha512').update(message.body).digest('base64');
const contentF = await db.contentsAll_txn(rd_uuidv, encodedText, 3, 2);
if (contentF === null) {
bodyBlob = convert.bodyBlobB64JSON((Buffer.from(message.body, 'utf-8').toString('base64')));
}
else {
bodyBlob = convert.bodyBlobJSON(contentF.reg, contentF.size, contentF.size, mHash_SHA512);
}
}
let files = '[]';
if (message.hasMedia && message.downloadMedia() !== undefined ) {
const fmimetype = (await message.downloadMedia()).mimetype;
const media_data = (await message.downloadMedia()).data;
const ffilename = (await message.downloadMedia()).fileName;
const fSHA512 = crypto.createHash('sha512').update(media_data).digest('base64');
const contentF = await db.contentsAll_txn(rd_uuidv, media_data, 3, 2);
if (contentF === null) {
files = convert.filesB64JSON(ffilename, fmimetype, media_data);
}
else {
files = convert.filesJSON(ffilename, fmimetype, contentF.reg, contentF.size, contentF.size, fSHA512, contentF.ContentID);
}
}
const MessagesTable = await db.add_message_txn(rd_uuidv, message._data.id._serialized, message.timestamp, sender, recipient, files, irtMUUID, quotedMsg_MIME_ID, header, preview, bodyBlob);
await db.folders_txn(chat.id._serialized, chat.name, chat.isGroup, MessagesTable.id, folderUUID);
await db.mbody_txn(rd_uuidv, message.body);
if (!message.fromMe) {
const ContactID = await db.contacts_txn(contact.id._serialized, contact.name);
const response = await axios.get((await contact.getProfilePicUrl()), { responseType: 'arraybuffer' });
const AvatarData = Buffer.from(response.data, 'binary');
const fSHA512 = crypto.createHash('sha512').update(AvatarData).digest('base64');
let avatarData;
let isContactKnown = false;
if (contact.name !== undefined) {
isContactKnown = true;
}
const contentF = await db.contentsAll_txn(rd_uuidv, AvatarData, ContactID, 4);
if (contentF === null) {
avatarData = "'" + AvatarData.toString('base64') + "'";
} else {
const JSON_str = [[contentF.reg, String(contentF.size), String(contentF.size), fSHA512]];
avatarData = JSON.stringify(JSON_str);
}
let ContactINF = {
WHATSAPP_ID: contact.id._serialized ,
WHATSAPP_PHONE_NUMBER: await contact.getFormattedNumber(),
WHATSAPP_AVATAR: avatarData,
WHATSAPP_NAME: contact.name,
WHATSAPP_SHORTNAME: contact.shortName,
WHATSAPP_PUSHNAME: contact.pushname,
WHATSAPP_BLOCKED: contact.isBlocked,
WHATSAPP_KNOWN: isContactKnown
};
console.log("CONTACT INF:", ContactINF);
console.log("chatname: ", chat.name);
await db.contactattrs_txn(ContactINF, ContactID);
}
});
client.on('message', async (message) => {
let cmdstr = message.body.split(" ")[0];
let handler = command[cmdstr];
if (handler === undefined) {
return;
}
if (! whitelist.includes(message.from)) {
log.debug("Non-whitelisted sender:", message.from);
return;
}
let chat = await message.getChat();
let contact = await message.getContact();
let preamble = log.fmt.preamble(message, contact, chat)
let ret = await handler(message, contact, chat);
let logret = 'undefined';
if (ret !== undefined) {
logret = ret.replaceAll("\n", "\\n");
}
log.command(preamble, message.body, "=>", logret);
});
// example.js'den reject calls kodu
client.on('call', async (call) => {
log.voice('Rejecting call', call);
await call.reject();
await client.sendMessage(call.from, `[${call.fromMe ? 'Outgoing' : 'Incoming'}] Phone call from ${call.from}, type ${call.isGroup ? 'group' : ''} ${call.isVideo ? 'video' : 'audio'} call. ${rejectCalls ? 'This call was automatically rejected by the script.' : ''}`);
});
/*
* main
*/
console.log(process.argv);
if (process.argv.length == 3 && process.argv[2] === 'devel') {
log.status("Booting devel ...");
client.initialize();
}
else if (process.argv.length == 3 && process.argv[2] === 'prod') {
log.status("Booting prod ...");
log.debug = (...args) => {};
client.initialize();
}
else if (process.argv.length > 3 && process.argv[2] === 'cmd') {
let message = {
body: process.argv[3],
from: config.usergroup,
timestamp: (new Date()) / 1000,
reply: (s) => {},
};
let contact = {};
let chat = {};
let preamble = log.fmt.preamble(message, contact, chat)
let cmdstr = message.body.split(" ")[0];
let handler = command[cmdstr];
if (handler === undefined) {
log.command("ERROR: No handler found");
}
else {
handler(message, contact, chat).then((ret) => {
log.command(preamble, message.body, "=>", ret.replaceAll("\n", "\\n"));
});
}
}