-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
389 lines (355 loc) · 13 KB
/
db.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
const sqlite3 = require('sqlite3').verbose();
const crypto = require('crypto');
const convert = require('./converters');
const path = require('path');
const fs = require('fs');
function openDatabase(str) {
return new Promise((resolve, reject) => {
const db = new sqlite3.Database('/home/kene/data/profiles/[email protected]/' + str, sqlite3.OPEN_READWRITE, (err) => {
if (err) {
reject(err);
} else {
console.log("DB connection successful");
resolve(db);
}
});
});
}
function closeDatabase(db) {
return new Promise((resolve, reject) => {
db.close((err) => {
if (err) {
reject(err);
} else {
console.log("DB closed");
resolve();
}
});
});
}
async function add_message_txn (uuid, mimeID, timestamp, sender, recipient, files, irtMUUID, mimeQuoted, header, preview, bodyBlob) {
try {
const date = new Date(timestamp*1000);
let body_type = [['body-enc', 'UTF-8']];
body_type = JSON.stringify(body_type);
const db_main = await openDatabase('main.db');
//messages tablosuna ekleme islemi
const rows2 = await new Promise((resolve, reject) => {
db_main.get("INSERT INTO messages (uuid, local_state, read, mime_id, wdate, last_update, tzoffset, mimesize, body_type, sender, recipients, files, size, in_reply_to, mime_irt, headers, preview, body_blob) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) RETURNING *;",
[uuid, '[{}]', 0, mimeID, date.toISOString(), date.toISOString(), (date.getTimezoneOffset()*60), 0, body_type, sender, recipient, files, 0, irtMUUID, mimeQuoted, header, preview, bodyBlob], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
}
)});
await closeDatabase(db_main);
return rows2;
} catch (err) {
console.error(err.message);
}
}
async function folders_txn (ChatID, ChatName, isGroup, mID, uuid) {
const db_main = await openDatabase('main.db');
if (isGroup) {
console.log("This is a Group Chat.");
const row = await new Promise((resolve, reject) => {
db_main.get("SELECT CASE WHEN EXISTS (SELECT 1 FROM folderattrs WHERE val = ?) THEN 1 ELSE 0 END AS folder_exists;", [ChatID], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
if (row.folder_exists === 1){
console.log("Folder exists.");
const folderattrs = await new Promise((resolve, reject) => {
db_main.get("SELECT fid, key FROM folderattrs WHERE val = ?", [ChatID], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
await db_main.run("UPDATE folderattrs SET key = ? WHERE fid = ?", [ChatName, folderattrs.fid]);
await db_main.run("UPDATE folders SET name = ? WHERE id = ?", ['[email protected]:apps/Chat/' + ChatName, folderattrs.fid]);
await db_main.run("INSERT INTO msgfolders (mid, fid) VALUES (?,?)", [mID, folderattrs.fid]);
}
else {
console.log("Folder does not exist.");
const folders = await new Promise((resolve, reject) => {
db_main.get("INSERT INTO folders (name, uuid) VALUES (?,?) RETURNING *;", ['[email protected]:apps/Chat/' + ChatName, uuid], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
console.log("folders: ", folders);
console.log("folders id: ", folders.id);
await db_main.run("INSERT INTO folderattrs (fid,key,val) VALUES (?,?,?)", [folders.id, ChatName, ChatID]);
await db_main.run("INSERT INTO msgfolders (mid, fid) VALUES (?,?)", [mID, folders.id]);
console.log("NEW FOLDER CREATED");
}
}
else {
db_main.run("INSERT INTO msgfolders (mid, fid) VALUES (?,?)", [mID, 8]);
}
await closeDatabase(db_main);
}
async function mbody_txn(uuid, messageBody) {
const db_mbody = await openDatabase('mbody.db');
db_mbody.run("INSERT INTO messages (uuid, data, type, enc) VALUES (?, ?, ?, ?)", [uuid, messageBody, 2, 'UTF-8']);
await closeDatabase(db_mbody);
}
async function getMessageIRT(mimeID) {
const db_main = await openDatabase('main.db');
const row = await new Promise ((resolve, reject) => {
db_main.get("SELECT uuid FROM messages WHERE mime_id = ?;", [mimeID], (err, row) => {
console.log("2.2", err, row);
if (err) {
reject(err);
} else {
resolve(row);
}
});
});
await closeDatabase(db_main);
return row.uuid;
}
async function quotedMessageIsInDb (mime_id){
const db_main = await openDatabase('main.db');
const row = await new Promise((resolve, reject) => {
db_main.get("SELECT CASE WHEN EXISTS (SELECT 1 FROM messages WHERE mime_id = ?) THEN 1 ELSE 0 END AS mimeID", [mime_id], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
if (row.mimeID === 0){
return null;
}
await closeDatabase(db_main);
return row.mimeID;
}
async function doesExistInContents (msgData) {
const db_contents = await openDatabase('blob1/contents.db');
const row = await new Promise((resolve, reject) => {
db_contents.get("SELECT CASE WHEN EXISTS (SELECT 1 FROM blobs WHERE sha512 = ?) THEN 1 ELSE 0 END AS sha512;", [msgData], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
await closeDatabase(db_contents);
return row.sha512;
}
async function createContent_txn(uuid, data, type, csha256, partype, parsubid, blob_id, size, csize, sha512) {
const db_contents = await openDatabase('blob1/contents.db');
const row = await new Promise((resolve, reject) => {
db_contents.get(
"INSERT INTO blob_data (compression, type, data, csha256) VALUES (?,?,?,?) RETURNING id",
[0, type, data, csha256],
(err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
await db_contents.run(
"INSERT INTO blobs (partype, parid, parsubid, blob_id, size, csize, sha512, data_id) VALUES (?,?,?,?,?,?,?,?)",
[partype, uuid, parsubid, blob_id, size, csize, sha512, row.id]);
await closeDatabase(db_contents);
return row.id;
}
async function UpdateContents (uuid, hash) {
const db_contents = await openDatabase('blob1/contents.db');
const row = await new Promise((resolve, reject) => {
db_contents.get("SELECT * FROM blobs WHERE sha512 = ?;", [hash], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
db_contents.run("INSERT INTO blobs (partype, parid, parsubid, blob_id, size, csize, sha512, data_id) VALUES (?,?,?,?,?,?,?,?) RETURNING *", [row.partype, uuid, row.parid, row.blob_id, row.size, row.csize, row.sha512, row.data_id]);
await closeDatabase(db_contents);
return row;
}
async function getContentID (sha512) {
const db_contents = await openDatabase('blob1/contents.db');
const row = await new Promise((resolve, reject) => {
db_contents.get("SELECT data_id FROM blobs WHERE sha512 = ?", [sha512], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
await closeDatabase(db_contents);
return row.data_id;
}
async function contentsAll_txn (uuid, RawData, cid, partype) {
const hash_SHA512 = crypto.createHash('sha512').update(RawData).digest();
const hash_SHA256 = crypto.createHash('sha256').update(RawData).digest();
const regex = convert.createRegex();
let type;
let data;
const db_contents = await openDatabase('blob1/contents.db');
const sizeInBytes = new TextEncoder().encode(RawData).byteLength;
if (sizeInBytes <= 512) {
await closeDatabase(db_contents);
return null;
}
if ((await doesExistInContents(hash_SHA512)) === 1) {
console.log("BLOB EXISTS!");
const row = await UpdateContents(uuid, hash_SHA512);
await closeDatabase(db_contents);
return {size: sizeInBytes, sha512: hash_SHA512, ContentID: row.data_id, reg: regex};
}
else {
console.log("BLOB DOES NOT EXIST!");
if (sizeInBytes > 16384) {
type = 2;
const fileData = Buffer.from(RawData, 'base64');
console.log("fileData: ", fileData);
let filePATH = (await convert.insertCharacterAtIndex(regex)) + '.0';
const fileName = filePATH.slice(12);
filePATH = filePATH.slice(0,12);
const finalPath = path.join(filePATH, fileName);
const directory = '/home/kene/data/profiles/[email protected]/blob1/';
const dbPATH = 'blob1/' + finalPath;
data = dbPATH;
console.log("dbPATH: ", dbPATH);
fs.mkdirSync(path.dirname(directory + finalPath), { recursive: true }, (err) => {
if (err) {
console.error(err);
} else {
console.log('Directory created successfully!');
}
});
fs.writeFile(directory + finalPath, fileData, (err) => {
if (err) {
console.error(err);
} else {
console.log('File written successfully!');
}
});
}
else {
type = 1;
data = RawData;
}
const contentID = await createContent_txn(uuid, data, type, hash_SHA256,
partype, cid, regex, sizeInBytes, sizeInBytes, hash_SHA512);
await closeDatabase(db_contents);
return {size: sizeInBytes, sha512: hash_SHA512, contentID: contentID, reg: regex};
}
}
async function contactattrs_txn (contact, cid) {
const db_main = await openDatabase('main.db');
db_main.serialize(function() {
db_main.run("DELETE FROM contactattrs WHERE cid = ?", [cid]);
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --1", [cid, 'whatsapp-id', contact.WHATSAPP_ID]);
if ( contact.WHATSAPP_PHONE_NUMBER !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --2", [cid, 'whatsapp-phone-number', contact.WHATSAPP_PHONE_NUMBER]);
}
if ( contact.WHATSAPP_AVATAR !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --3", [cid, 'whatsapp-avatar', contact.WHATSAPP_AVATAR]);
}
if ( contact.WHATSAPP_NAME !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --4", [cid, 'whatsapp-name', contact.WHATSAPP_NAME]);
}
if ( contact.WHATSAPP_SHORTNAME !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --5", [cid, 'whatsapp-short-name', contact.WHATSAPP_SHORTNAME]);
}
if ( contact.WHATSAPP_PUSHNAME !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --6", [cid, 'whatsapp-push-name', contact.WHATSAPP_PUSHNAME]);
}
if ( contact.WHATSAPP_BLOCKED !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --7", [cid, 'whatsapp-blocked', contact.WHATSAPP_BLOCKED]);
}
if ( contact.WHATSAPP_KNOWN !== undefined) {
db_main.run("INSERT INTO contactattrs (cid, key, val) VALUES (?,?,?) --8", [cid, 'whatsapp-known', contact.WHATSAPP_KNOWN]);
}
});
await closeDatabase(db_main);
}
async function contacts_txn (wpID, wpName) {
console.log("Contacts txn begins...");
if (wpName === undefined) {
wpName = null;
}
const db_main = await openDatabase('main.db');
const contactRow = await new Promise((resolve, reject) => {
db_main.get("SELECT cid FROM contactattrs WHERE key='whatsapp-id' and val=?", [wpID], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
let retval;
if (contactRow !== undefined && contactRow.cid !== undefined) {
console.log("Contact is in database.")
retval = contactRow.cid;
await db_main.run("UPDATE contacts SET name = ? WHERE id = ?", [wpName, retval]);
console.log("Contact updated.");
console.log("Contact ID: ", retval);
await closeDatabase(db_main);
return retval;
}
else {
console.log("Contact is not in database.")
const row = await new Promise((resolve, reject) => {
db_main.get("INSERT INTO contacts (name, iscoll) VALUES (?,?) RETURNING id;", [wpName, true], (err, row) => {
if (err) {
reject (err);
}
else {
resolve(row);
}
})
});
retval = row.id
console.log("Contact inserted.");
console.log("Contact ID: ", retval);
await closeDatabase(db_main);
return retval;
}
}
module.exports.getMessageIRT = getMessageIRT;
module.exports.add_message_txn = add_message_txn;
module.exports.quotedMessageIsInDb = quotedMessageIsInDb;
module.exports.doesExistInContents = doesExistInContents;
module.exports.createContent_txn = createContent_txn;
module.exports.UpdateContents = UpdateContents;
module.exports.getContentID = getContentID;
module.exports.contactattrs_txn = contactattrs_txn;
module.exports.contentsAll_txn = contentsAll_txn;
module.exports.contacts_txn = contacts_txn;
module.exports.mbody_txn = mbody_txn;
module.exports.folders_txn = folders_txn;