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
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (ñ) 2012-15 Miranda NG project (http://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 "commonheaders.h"
STDMETHODIMP_(LONG) CDbxKyoto::GetEventCount(MCONTACT contactID)
{
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
return (cc == NULL) ? 0 : cc->dbc.dwEventCount;
}
STDMETHODIMP_(MEVENT) CDbxKyoto::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei)
{
if (dbei == NULL || dbei->cbSize != sizeof(DBEVENTINFO)) return 0;
if (dbei->timestamp == 0) return 0;
DBEvent dbe;
dbe.dwSignature = DBEVENT_SIGNATURE;
dbe.contactID = contactID; // store native or subcontact's id
dbe.ofsModuleName = GetModuleNameOfs(dbei->szModule);
dbe.timestamp = dbei->timestamp;
dbe.flags = dbei->flags;
dbe.wEventType = dbei->eventType;
dbe.cbBlob = dbei->cbBlob;
BYTE *pBlob = dbei->pBlob;
MCONTACT contactNotifyID = contactID;
DBCachedContact *cc, *ccSub = NULL;
if ((cc = m_cache->GetCachedContact(contactID)) == NULL)
return 0;
if (cc->IsSub()) {
ccSub = cc;
if ((cc = m_cache->GetCachedContact(cc->parentID)) == NULL)
return 0;
// set default sub to the event's source
if (!(dbei->flags & DBEF_SENT))
db_mc_setDefault(cc->contactID, contactID, false);
contactID = cc->contactID; // and add an event to a metahistory
if (db_mc_isEnabled())
contactNotifyID = contactID;
}
if (m_safetyMode)
if (NotifyEventHooks(hEventFilterAddedEvent, contactNotifyID, (LPARAM)dbei))
return NULL;
mir_ptr<BYTE> pCryptBlob;
if (m_bEncrypted) {
size_t len;
BYTE *pResult = m_crypto->encodeBuffer(pBlob, dbe.cbBlob, &len);
if (pResult != NULL) {
pCryptBlob = pBlob = pResult;
dbe.cbBlob = (DWORD)len;
dbe.flags |= DBEF_ENCRYPTED;
}
}
DWORD dwEventId = ++m_dwMaxEventId;
BYTE *pDest = (BYTE*)_alloca(sizeof(DBEvent) + dbe.cbBlob);
memcpy(pDest, &dbe, sizeof(DBEvent));
memcpy(pDest + sizeof(DBEvent), pBlob, dbe.cbBlob);
m_dbEvents.set((LPCSTR)&dwEventId, sizeof(int), (LPCSTR)pDest, sizeof(DBEvent) + dbe.cbBlob);
// add a sorting key
DBEventSortingKey key2 = { contactID, dbe.timestamp, dwEventId };
m_dbEventsSort.set((LPCSTR)&key2, sizeof(key2), "", 1);
cc->Advance(dwEventId, dbe);
m_dbContacts.set((LPCSTR)&contactID, sizeof(int), (LPCSTR)&cc->dbc, sizeof(DBContact));
// insert an event into a sub's history too
if (ccSub != NULL) {
key2.dwContactId = ccSub->contactID;
m_dbEventsSort.set((LPCSTR)&key2, sizeof(key2), "", 1);
ccSub->Advance(dwEventId, dbe);
m_dbContacts.set((LPCSTR)&ccSub->contactID, sizeof(int), (LPCSTR)&ccSub->dbc, sizeof(DBContact));
}
// Notify only in safe mode or on really new events
if (m_safetyMode)
NotifyEventHooks(hEventAddedEvent, contactNotifyID, dwEventId);
return dwEventId;
}
STDMETHODIMP_(BOOL) CDbxKyoto::DeleteEvent(MCONTACT contactID, MEVENT hDbEvent)
{
if (hDbEvent == 0) return INVALID_CONTACT_ID;
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
if (cc == NULL || cc->dbc.dwEventCount == 0)
return 1;
DBEvent dbe;
if (-1 == m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), (LPSTR)&dbe, sizeof(dbe)))
return 1;
DWORD dwSavedContact = dbe.contactID;
DBEventSortingKey key2 = { contactID, dbe.timestamp, hDbEvent };
m_dbEvents.remove((LPCSTR)&hDbEvent, sizeof(MEVENT));
// remove a sorting key
m_dbEventsSort.remove((LPCSTR)&key2, sizeof(key2));
// remove a sub's history entry too
if (contactID != dwSavedContact) {
key2.dwContactId = dwSavedContact;
m_dbEventsSort.remove((LPCSTR)&key2, sizeof(key2));
}
// update a contact
cc->dbc.dwEventCount--;
if (cc->dbc.dwFirstUnread == hDbEvent)
FindNextUnread(cc, key2);
// call notifier while outside mutex
NotifyEventHooks(hEventDeletedEvent, contactID, hDbEvent);
return 0;
}
STDMETHODIMP_(LONG) CDbxKyoto::GetBlobSize(MEVENT hDbEvent)
{
DBEvent dbe;
if (-1 == m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), (LPSTR)&dbe, sizeof(dbe)))
return -1;
return (dbe.dwSignature == DBEVENT_SIGNATURE) ? dbe.cbBlob : 0;
}
STDMETHODIMP_(BOOL) CDbxKyoto::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei)
{
if (hDbEvent == 0 || dbei == NULL || dbei->cbSize != sizeof(DBEVENTINFO)) return 1;
if (dbei->cbBlob > 0 && dbei->pBlob == NULL) {
dbei->cbBlob = 0;
return 1;
}
char rec[65536];
if (-1 == m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), rec, sizeof(rec)))
return 1;
DBEvent *dbe = (DBEvent*)rec;
if (dbe->dwSignature != DBEVENT_SIGNATURE)
return 1;
dbei->szModule = GetModuleNameByOfs(dbe->ofsModuleName);
dbei->timestamp = dbe->timestamp;
dbei->flags = dbe->flags;
dbei->eventType = dbe->wEventType;
int bytesToCopy = (dbei->cbBlob < dbe->cbBlob) ? dbei->cbBlob : dbe->cbBlob;
dbei->cbBlob = dbe->cbBlob;
if (bytesToCopy && dbei->pBlob) {
BYTE *pSrc = (BYTE*)rec + sizeof(DBEvent);
if (dbe->flags & DBEF_ENCRYPTED) {
dbei->flags &= ~DBEF_ENCRYPTED;
size_t len;
BYTE* pBlob = (BYTE*)m_crypto->decodeBuffer(pSrc, dbe->cbBlob, &len);
if (pBlob == NULL)
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;
}
void CDbxKyoto::FindNextUnread(DBCachedContact *cc, DBEventSortingKey &key2)
{
key2.dwEventId++;
cursor_ptr cursor(m_dbEventsSort);
cursor->jump((LPCSTR)&key2, sizeof(key2));
while(cursor->step()) {
size_t size;
const char *pRec;
delete[] cursor->get(&size, &pRec, &size);
DBEvent *dbe = (DBEvent*)pRec;
if (!dbe->markedRead()) {
cc->dbc.dwFirstUnread = key2.dwEventId;
cc->dbc.tsFirstUnread = key2.ts;
return;
}
}
cc->dbc.dwFirstUnread = cc->dbc.tsFirstUnread = 0;
}
STDMETHODIMP_(BOOL) CDbxKyoto::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent)
{
if (hDbEvent == 0) return -1;
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
if (cc == NULL)
return -1;
char rec[65536];
int32_t recLen = m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), rec, sizeof(rec));
if (recLen == -1)
return -1;
DBEvent *dbe = (DBEvent*)rec;
if (dbe->dwSignature != DBEVENT_SIGNATURE)
return -1;
if (dbe->markedRead())
return dbe->flags;
DBEventSortingKey key2 = { contactID, dbe->timestamp, hDbEvent };
dbe->flags |= DBEF_READ;
m_dbEvents.set((LPCSTR)&hDbEvent, sizeof(MEVENT), rec, recLen);
FindNextUnread(cc, key2);
m_dbContacts.set((LPCSTR)&contactID, sizeof(int), (LPCSTR)&cc->dbc, sizeof(cc->dbc));
NotifyEventHooks(hEventMarkedRead, contactID, (LPARAM)hDbEvent);
return dbe->flags;
}
STDMETHODIMP_(MCONTACT) CDbxKyoto::GetEventContact(MEVENT hDbEvent)
{
if (hDbEvent == 0) return INVALID_CONTACT_ID;
char rec[65536];
if (-1 == m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), rec, sizeof(rec)))
return 1;
DBEvent *dbe = (DBEvent*)rec;
return (dbe->dwSignature == DBEVENT_SIGNATURE) ? dbe->contactID : INVALID_CONTACT_ID;
}
STDMETHODIMP_(MEVENT) CDbxKyoto::FindFirstEvent(MCONTACT contactID)
{
DBEventSortingKey keyVal = { contactID, 0, 0 };
cursor_ptr cursor(m_dbEventsSort);
cursor->jump((LPCSTR)&keyVal, sizeof(keyVal));
size_t size;
DBEventSortingKey *pKey = (DBEventSortingKey*)cursor->get_key(&size);
if (pKey == NULL)
return m_evLast = 0;
m_tsLast = pKey->ts;
m_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
delete[] pKey;
return m_evLast;
}
STDMETHODIMP_(MEVENT) CDbxKyoto::FindFirstUnreadEvent(MCONTACT contactID)
{
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
return (cc == NULL) ? 0 : cc->dbc.dwFirstUnread;
}
STDMETHODIMP_(MEVENT) CDbxKyoto::FindLastEvent(MCONTACT contactID)
{
DBEventSortingKey keyVal = { contactID, 0xFFFFFFFF, 0xFFFFFFFF };
cursor_ptr cursor(m_dbEventsSort);
cursor->jump_back((LPCSTR)&keyVal, sizeof(keyVal));
size_t size;
DBEventSortingKey *pKey = (DBEventSortingKey*)cursor->get_key(&size);
if (pKey == NULL)
return m_evLast = 0;
m_tsLast = pKey->ts;
m_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
delete[] pKey;
return m_evLast;
}
STDMETHODIMP_(MEVENT) CDbxKyoto::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent)
{
if (hDbEvent == 0) return m_evLast = 0;
DWORD ts;
if (m_evLast != hDbEvent) {
DBEvent dbe;
if (-1 == m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), (LPSTR)&dbe, sizeof(dbe)))
return 0;
m_tsLast = ts = dbe.timestamp;
}
else ts = m_tsLast;
DBEventSortingKey keyVal = { contactID, ts, hDbEvent+1 };
cursor_ptr cursor(m_dbEventsSort);
cursor->jump((LPCSTR)&keyVal, sizeof(keyVal));
cursor->step();
size_t size;
DBEventSortingKey *pKey = (DBEventSortingKey*)cursor->get_key(&size);
if (pKey == NULL)
return m_evLast = 0;
m_tsLast = pKey->ts;
m_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
delete[] pKey;
return m_evLast;
}
STDMETHODIMP_(MEVENT) CDbxKyoto::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent)
{
if (hDbEvent == 0) return m_evLast = 0;
DWORD ts;
if (m_evLast != hDbEvent) {
DBEvent dbe;
if (-1 == m_dbEvents.get((LPCSTR)&hDbEvent, sizeof(MEVENT), (LPSTR)&dbe, sizeof(dbe)))
return 0;
m_tsLast = ts = dbe.timestamp;
}
else ts = m_tsLast;
DBEventSortingKey keyVal = { contactID, ts, hDbEvent-1 };
cursor_ptr cursor(m_dbEventsSort);
cursor->jump_back((LPCSTR)&keyVal, sizeof(keyVal));
cursor->step_back();
size_t size;
DBEventSortingKey *pKey = (DBEventSortingKey*)cursor->get_key(&size);
if (pKey == NULL)
return m_evLast = 0;
m_tsLast = pKey->ts;
m_evLast = (pKey->dwContactId == contactID) ? pKey->dwEventId : 0;
delete[] pKey;
return m_evLast;
}
/////////////////////////////////////////////////////////////////////////////////////////
// low-level history cleaner
int CDbxKyoto::WipeContactHistory(DBContact*)
{
// drop subContact's history if any
return 0;
}
|