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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (C) 2012-21 Miranda NG team (https://miranda-ng.org)
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdafx.h"
LONG CDb3Mmap::GetEventCount(MCONTACT contactID)
{
mir_cslock lck(m_csDbAccess);
DBContact *dbc = (DBContact*)DBRead(GetContactOffset(contactID), nullptr);
return (dbc->signature != DBCONTACT_SIGNATURE) ? -1 : dbc->eventCount;
}
MEVENT CDb3Mmap::AddEvent(MCONTACT contactID, const DBEVENTINFO *dbei)
{
if (dbei == nullptr) return 0;
if (dbei->timestamp == 0) return 0;
DBEvent dbe;
dbe.signature = DBEVENT_SIGNATURE;
dbe.contactID = contactID; // store native or subcontact's id
MCONTACT contactNotifyID = contactID;
DBCachedContact *ccSub = nullptr;
if (contactID != 0) {
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
if (cc == nullptr)
return 0;
if (cc->IsSub()) {
ccSub = cc;
// set default sub to the event's source
if (!(dbei->flags & DBEF_SENT))
db_mc_setDefault(cc->parentID, contactID, false);
contactID = cc->parentID; // and add an event to a metahistory
if (db_mc_isEnabled())
contactNotifyID = contactID;
}
}
if (NotifyEventHooks(g_hevEventFiltered, contactNotifyID, (LPARAM)dbei))
return 0;
dbe.timestamp = dbei->timestamp;
dbe.flags = dbei->flags;
dbe.wEventType = dbei->eventType;
dbe.cbBlob = dbei->cbBlob;
BYTE *pBlob = dbei->pBlob;
mir_ptr<BYTE> pCryptBlob;
if (m_bEncrypted) {
size_t len;
BYTE *pResult = m_crypto->encodeBuffer(pBlob, dbe.cbBlob, &len);
if (pResult != nullptr) {
pCryptBlob = pBlob = pResult;
dbe.cbBlob = (DWORD)len;
dbe.flags |= DBEF_ENCRYPTED;
}
}
mir_cslockfull lck(m_csDbAccess);
DWORD ofsContact = GetContactOffset(contactID);
DBContact dbc = *(DBContact*)DBRead(ofsContact, nullptr);
if (dbc.signature != DBCONTACT_SIGNATURE)
return 0;
DWORD ofsNew = CreateNewSpace(offsetof(DBEvent, blob) + dbe.cbBlob);
dbe.ofsModuleName = GetModuleNameOfs(dbei->szModule);
// find where to put it - sort by timestamp
if (dbc.eventCount == 0) {
dbe.ofsPrev = dbe.ofsNext = 0;
dbc.ofsFirstEvent = dbc.ofsLastEvent = ofsNew;
}
else {
DBEvent *dbeTest = (DBEvent*)DBRead(dbc.ofsFirstEvent, nullptr);
// Should new event be placed before first event in chain?
if (dbe.timestamp < dbeTest->timestamp) {
dbe.ofsPrev = 0;
dbe.ofsNext = dbc.ofsFirstEvent;
dbc.ofsFirstEvent = ofsNew;
dbeTest = (DBEvent*)DBRead(dbe.ofsNext, nullptr);
dbeTest->ofsPrev = ofsNew;
DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent));
}
else {
// Loop through the chain, starting at the end
DWORD ofsThis = dbc.ofsLastEvent;
dbeTest = (DBEvent*)DBRead(ofsThis, nullptr);
for (;;) {
// If the new event's timesstamp is equal to or greater than the
// current dbevent, it will be inserted after. If not, continue
// with the previous dbevent in chain.
if (dbe.timestamp >= dbeTest->timestamp) {
dbe.ofsPrev = ofsThis;
dbe.ofsNext = dbeTest->ofsNext;
dbeTest->ofsNext = ofsNew;
DBWrite(ofsThis, dbeTest, sizeof(DBEvent));
if (dbe.ofsNext == 0)
dbc.ofsLastEvent = ofsNew;
else {
dbeTest = (DBEvent*)DBRead(dbe.ofsNext, nullptr);
dbeTest->ofsPrev = ofsNew;
DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent));
}
break;
}
ofsThis = dbeTest->ofsPrev;
dbeTest = (DBEvent*)DBRead(ofsThis, nullptr);
}
}
}
dbc.eventCount++;
bool neednotify;
if (!(dbe.flags & (DBEF_READ | DBEF_SENT))) {
if (dbe.timestamp < dbc.tsFirstUnread || dbc.tsFirstUnread == 0) {
dbc.tsFirstUnread = dbe.timestamp;
dbc.ofsFirstUnread = ofsNew;
}
neednotify = true;
}
else if (dbe.flags & DBEF_TEMPORARY) {
neednotify = false;
}
else neednotify = m_safetyMode;
if (ccSub != nullptr) {
DBContact *pSub = (DBContact*)DBRead(ccSub->dwOfsContact, nullptr);
pSub->eventCount++;
}
DBWrite(ofsContact, &dbc, sizeof(DBContact));
DBWrite(ofsNew, &dbe, offsetof(DBEvent, blob));
DBWrite(ofsNew + offsetof(DBEvent, blob), pBlob, dbe.cbBlob);
DBFlush(0);
lck.unlock();
log1("add event @ %08x", ofsNew);
// Notify only in safe mode or on really new events
if (neednotify)
NotifyEventHooks(g_hevEventAdded, contactNotifyID, (LPARAM)ofsNew);
return (MEVENT)ofsNew;
}
BOOL CDb3Mmap::DeleteEvent(MEVENT hDbEvent)
{
mir_cslockfull lck(m_csDbAccess);
DBEvent dbe = *(DBEvent*)DBRead((DWORD)hDbEvent, nullptr);
if (dbe.signature != DBEVENT_SIGNATURE)
return 1;
DBCachedContact *cc;
if (dbe.contactID) {
if ((cc = m_cache->GetCachedContact(dbe.contactID)) == nullptr)
return 2;
if (cc->IsSub())
if ((cc = m_cache->GetCachedContact(cc->parentID)) == nullptr)
return 3;
}
else cc = nullptr;
DWORD ofsContact = (cc) ? cc->dwOfsContact : m_dbHeader.ofsUser;
DBContact dbc = *(DBContact *)DBRead(ofsContact, nullptr);
if (dbc.signature != DBCONTACT_SIGNATURE)
return 1;
lck.unlock();
log1("delete event @ %08x", hContact);
// call notifier while outside mutex
NotifyEventHooks(g_hevEventDeleted, dbe.contactID, (LPARAM)hDbEvent);
// get back in
lck.lock();
dbc = *(DBContact*)DBRead(ofsContact, nullptr);
dbe = *(DBEvent*)DBRead((DWORD)hDbEvent, nullptr);
// check if this was the first unread, if so, recalc the first unread
if (dbc.ofsFirstUnread == (DWORD)hDbEvent) {
for (DBEvent *dbeNext = &dbe;;) {
if (dbeNext->ofsNext == 0) {
dbc.ofsFirstUnread = 0;
dbc.tsFirstUnread = 0;
break;
}
DWORD ofsThis = dbeNext->ofsNext;
dbeNext = (DBEvent*)DBRead(ofsThis, nullptr);
if (!dbeNext->markedRead()) {
dbc.ofsFirstUnread = ofsThis;
dbc.tsFirstUnread = dbeNext->timestamp;
break;
}
}
}
// get previous and next events in chain and change offsets
if (dbe.ofsPrev == 0) {
if (dbe.ofsNext == 0)
dbc.ofsFirstEvent = dbc.ofsLastEvent = 0;
else {
DBEvent *dbeNext = (DBEvent*)DBRead(dbe.ofsNext, nullptr);
dbeNext->ofsPrev = 0;
DBWrite(dbe.ofsNext, dbeNext, sizeof(DBEvent));
dbc.ofsFirstEvent = dbe.ofsNext;
}
}
else {
if (dbe.ofsNext == 0) {
DBEvent *dbePrev = (DBEvent*)DBRead(dbe.ofsPrev, nullptr);
dbePrev->ofsNext = 0;
DBWrite(dbe.ofsPrev, dbePrev, sizeof(DBEvent));
dbc.ofsLastEvent = dbe.ofsPrev;
}
else {
DBEvent *dbePrev = (DBEvent*)DBRead(dbe.ofsPrev, nullptr);
dbePrev->ofsNext = dbe.ofsNext;
DBWrite(dbe.ofsPrev, dbePrev, sizeof(DBEvent));
DBEvent *dbeNext = (DBEvent*)DBRead(dbe.ofsNext, nullptr);
dbeNext->ofsPrev = dbe.ofsPrev;
DBWrite(dbe.ofsNext, dbeNext, sizeof(DBEvent));
}
}
// decrement event count
dbc.eventCount--;
DBWrite(ofsContact, &dbc, sizeof(DBContact));
// delete event
DeleteSpace((DWORD)hDbEvent, offsetof(DBEvent, blob) + dbe.cbBlob);
// also update a sub
if (cc && dbe.contactID != cc->contactID) {
DBContact *pSub = (DBContact*)DBRead(GetContactOffset(dbe.contactID), nullptr);
if (pSub->eventCount > 0)
pSub->eventCount--;
}
DBFlush(0);
return 0;
}
BOOL CDb3Mmap::EditEvent(MCONTACT, MEVENT, const DBEVENTINFO*)
{
return 1;
}
LONG CDb3Mmap::GetBlobSize(MEVENT hDbEvent)
{
mir_cslock lck(m_csDbAccess);
DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, 0);
return (dbe->signature != DBEVENT_SIGNATURE) ? -1 : dbe->cbBlob;
}
BOOL CDb3Mmap::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
{
if (dbei == nullptr) return 1;
if (dbei->cbBlob > 0 && dbei->pBlob == nullptr) {
dbei->cbBlob = 0;
return 1;
}
mir_cslock lck(m_csDbAccess);
DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, 0);
if (dbe->signature != DBEVENT_SIGNATURE)
return 1;
dbei->szModule = GetModuleNameByOfs(dbe->ofsModuleName);
dbei->timestamp = dbe->timestamp;
dbei->flags = dbe->flags;
dbei->eventType = dbe->wEventType;
DWORD cbBlob = dbe->cbBlob;
size_t bytesToCopy = cbBlob;
if (dbei->cbBlob == -1)
dbei->pBlob = (PBYTE)mir_calloc(cbBlob + 2);
else if (dbei->cbBlob < cbBlob)
bytesToCopy = dbei->cbBlob;
dbei->cbBlob = dbe->cbBlob;
if (bytesToCopy && dbei->pBlob) {
BYTE *pSrc;
if (m_dbHeader.version >= DB_095_1_VERSION)
pSrc = DBRead(DWORD(hDbEvent) + offsetof(DBEvent, blob), nullptr);
else
pSrc = DBRead(DWORD(hDbEvent) + offsetof(DBEvent_094, blob), nullptr);
if (dbe->flags & DBEF_ENCRYPTED) {
dbei->flags &= ~DBEF_ENCRYPTED;
size_t len;
BYTE* pBlob = (BYTE*)m_crypto->decodeBuffer(pSrc, dbe->cbBlob, &len);
if (pBlob == nullptr)
return 1;
memcpy(dbei->pBlob, pBlob, bytesToCopy);
if (bytesToCopy > (int)len)
memset(dbei->pBlob + len, 0, bytesToCopy - len);
mir_free(pBlob);
}
else memcpy(dbei->pBlob, pSrc, bytesToCopy);
}
return 0;
}
BOOL CDb3Mmap::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent)
{
DBCachedContact *cc;
if (contactID) {
if ((cc = m_cache->GetCachedContact(contactID)) == nullptr)
return -1;
if (cc->IsSub())
if ((cc = m_cache->GetCachedContact(cc->parentID)) == nullptr)
return -1;
}
else cc = nullptr;
mir_cslockfull lck(m_csDbAccess);
DWORD ofsContact = (cc) ? cc->dwOfsContact : m_dbHeader.ofsUser;
DBContact dbc = *(DBContact*)DBRead(ofsContact, nullptr);
DBEvent *dbe = (DBEvent*)DBRead((DWORD)hDbEvent, nullptr);
if (dbe->signature != DBEVENT_SIGNATURE || dbc.signature != DBCONTACT_SIGNATURE)
return -1;
if (dbe->markedRead())
return dbe->flags;
// log1("mark read @ %08x", hContact);
dbe->flags |= DBEF_READ;
DBWrite((DWORD)hDbEvent, dbe, sizeof(DBEvent));
BOOL ret = dbe->flags;
if (dbc.ofsFirstUnread == (DWORD)hDbEvent) {
for (;;) {
if (dbe->ofsNext == 0) {
dbc.ofsFirstUnread = 0;
dbc.tsFirstUnread = 0;
break;
}
DWORD ofsThis = dbe->ofsNext;
dbe = (DBEvent*)DBRead(ofsThis, nullptr);
if (!dbe->markedRead()) {
dbc.ofsFirstUnread = ofsThis;
dbc.tsFirstUnread = dbe->timestamp;
break;
}
}
}
DBWrite(ofsContact, &dbc, sizeof(DBContact));
DBFlush(0);
lck.unlock();
NotifyEventHooks(g_hevMarkedRead, contactID, (LPARAM)hDbEvent);
return ret;
}
MCONTACT CDb3Mmap::GetEventContact(MEVENT hDbEvent)
{
mir_cslock lck(m_csDbAccess);
DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, INVALID_CONTACT_ID);
return (dbe->signature != DBEVENT_SIGNATURE) ? INVALID_CONTACT_ID : dbe->contactID;
}
MEVENT CDb3Mmap::FindFirstEvent(MCONTACT contactID)
{
DBCachedContact *cc;
DWORD ofsContact = GetContactOffset(contactID, &cc);
mir_cslock lck(m_csDbAccess);
DBContact *dbc = (DBContact*)DBRead(ofsContact, nullptr);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 0;
if (!cc || !cc->IsSub())
return MEVENT(dbc->ofsFirstEvent);
if ((cc = m_cache->GetCachedContact(cc->parentID)) == nullptr)
return 0;
dbc = (DBContact*)DBRead(cc->dwOfsContact, nullptr);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 0;
for (DWORD dwOffset = dbc->ofsFirstEvent; dwOffset != 0;) {
DBEvent *dbe = AdaptEvent(dwOffset, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (dbe->contactID == contactID)
return MEVENT(dwOffset);
dwOffset = dbe->ofsNext;
}
return 0;
}
MEVENT CDb3Mmap::FindFirstUnreadEvent(MCONTACT contactID)
{
DBCachedContact *cc;
DWORD ofsContact = GetContactOffset(contactID, &cc);
mir_cslock lck(m_csDbAccess);
DBContact *dbc = (DBContact*)DBRead(ofsContact, nullptr);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 0;
if (!cc || !cc->IsSub())
return MEVENT(dbc->ofsFirstUnread);
if ((cc = m_cache->GetCachedContact(cc->parentID)) == nullptr)
return 0;
dbc = (DBContact*)DBRead(cc->dwOfsContact, nullptr);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 0;
for (DWORD dwOffset = dbc->ofsFirstUnread; dwOffset != 0;) {
DBEvent *dbe = AdaptEvent(dwOffset, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (dbe->contactID == contactID && !dbe->markedRead())
return MEVENT(dwOffset);
dwOffset = dbe->ofsNext;
}
return 0;
}
MEVENT CDb3Mmap::FindLastEvent(MCONTACT contactID)
{
DBCachedContact *cc;
DWORD ofsContact = GetContactOffset(contactID, &cc);
mir_cslock lck(m_csDbAccess);
DBContact *dbc = (DBContact*)DBRead(ofsContact, nullptr);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 0;
if (!cc || !cc->IsSub())
return MEVENT(dbc->ofsLastEvent);
if ((cc = m_cache->GetCachedContact(cc->parentID)) == nullptr)
return 0;
dbc = (DBContact*)DBRead(cc->dwOfsContact, nullptr);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 0;
for (DWORD dwOffset = dbc->ofsLastEvent; dwOffset != 0;) {
DBEvent *dbe = AdaptEvent(dwOffset, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (dbe->contactID == contactID)
return MEVENT(dwOffset);
dwOffset = dbe->ofsPrev;
}
return 0;
}
MEVENT CDb3Mmap::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent)
{
DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : nullptr;
mir_cslock lck(m_csDbAccess);
DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (!cc || !cc->IsSub())
return MEVENT(dbe->ofsNext);
for (DWORD dwOffset = dbe->ofsNext; dwOffset != 0;) {
dbe = AdaptEvent(dwOffset, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (dbe->contactID == contactID)
return MEVENT(dwOffset);
dwOffset = dbe->ofsNext;
}
return 0;
}
MEVENT CDb3Mmap::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent)
{
DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : nullptr;
mir_cslock lck(m_csDbAccess);
DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (!cc || !cc->IsSub())
return MEVENT(dbe->ofsPrev);
for (DWORD dwOffset = dbe->ofsPrev; dwOffset != 0;) {
dbe = AdaptEvent(dwOffset, contactID);
if (dbe->signature != DBEVENT_SIGNATURE)
return 0;
if (dbe->contactID == contactID)
return MEVENT(dwOffset);
dwOffset = dbe->ofsPrev;
}
return 0;
}
DBEvent* CDb3Mmap::AdaptEvent(DWORD ofs, DWORD dwContactID)
{
if (m_dbHeader.version >= DB_095_1_VERSION)
return (DBEvent*)DBRead(ofs, nullptr);
DBEvent_094 *pOldEvent = (DBEvent_094*)DBRead(ofs, nullptr);
m_tmpEvent.signature = pOldEvent->signature;
m_tmpEvent.contactID = dwContactID;
memcpy(&m_tmpEvent.ofsPrev, &pOldEvent->ofsPrev, sizeof(DBEvent_094) - sizeof(DWORD));
return &m_tmpEvent;
}
/////////////////////////////////////////////////////////////////////////////////////////
// low-level history cleaner
int CDb3Mmap::WipeContactHistory(DBContact *dbc)
{
// drop subContact's history if any
for (DWORD dwOffset = dbc->ofsFirstEvent; dwOffset != 0;) {
DBEvent *pev = (DBEvent*)DBRead(dwOffset, nullptr);
if (pev->signature != DBEVENT_SIGNATURE) // broken chain, don't touch it
return 2;
DWORD dwNext = pev->ofsNext;
DeleteSpace(dwOffset, offsetof(DBEvent, blob) + pev->cbBlob);
dwOffset = dwNext;
}
dbc->eventCount = 0; dbc->ofsFirstEvent = dbc->ofsLastEvent = dbc->ofsFirstUnread = dbc->tsFirstUnread = 0;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
MEVENT CDb3Mmap::GetEventById(LPCSTR, LPCSTR)
{
return 0;
}
|