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
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (ñ) 2012-16 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 "stdafx.h"
int CDbxMdb::CheckProto(DBCachedContact *cc, const char *proto)
{
if (cc->szProto == NULL) {
char protobuf[MAX_PATH] = { 0 };
DBVARIANT dbv;
dbv.type = DBVT_ASCIIZ;
dbv.pszVal = protobuf;
dbv.cchVal = sizeof(protobuf);
if (GetContactSettingStatic(cc->contactID, "Protocol", "p", &dbv) != 0 || (dbv.type != DBVT_ASCIIZ))
return 0;
cc->szProto = m_cache->GetCachedSetting(NULL, protobuf, 0, (int)strlen(protobuf));
}
return !strcmp(cc->szProto, proto);
}
STDMETHODIMP_(LONG) CDbxMdb::GetContactCount(void)
{
mir_cslock lck(m_csDbAccess);
return m_contactCount;
}
STDMETHODIMP_(LONG) CDbxMdb::GetContactSize(void)
{
return sizeof(DBCachedContact);
}
STDMETHODIMP_(MCONTACT) CDbxMdb::FindFirstContact(const char *szProto)
{
mir_cslock lck(m_csDbAccess);
DBCachedContact *cc = m_cache->GetFirstContact();
if (cc == NULL)
return NULL;
if (cc->contactID == 0)
if ((cc = m_cache->GetNextContact(0)) == NULL)
return NULL;
if (!szProto || CheckProto(cc, szProto))
return cc->contactID;
return FindNextContact(cc->contactID, szProto);
}
STDMETHODIMP_(MCONTACT) CDbxMdb::FindNextContact(MCONTACT contactID, const char *szProto)
{
mir_cslock lck(m_csDbAccess);
while (contactID) {
DBCachedContact *cc = m_cache->GetNextContact(contactID);
if (cc == NULL)
break;
if (!szProto || CheckProto(cc, szProto))
return cc->contactID;
contactID = cc->contactID;
}
return NULL;
}
STDMETHODIMP_(LONG) CDbxMdb::DeleteContact(MCONTACT contactID)
{
if (contactID == 0) // global contact cannot be removed
return 1;
// delete
// call notifier while outside mutex
NotifyEventHooks(hContactDeletedEvent, contactID, 0);
mir_cslockfull lck(m_csDbAccess);
{
LIST<EventItem> events(50);
GatherContactHistory(contactID, events);
while (events.getCount())
{
DeleteEvent(contactID, events[0]->eventId);
delete events[0];
events.remove(0);
}
}
{
MDB_val key, data;
DBSettingKey keyS = { contactID, 0 };
memset(keyS.szSettingName, 0, sizeof(keyS.szSettingName));
txn_ptr txn(m_pMdbEnv);
cursor_ptr cursor(txn, m_dbSettings);
key.mv_size = sizeof(keyS); key.mv_data = &keyS;
if (mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE))
{
do
{
const DBSettingKey *pKey = (const DBSettingKey*)key.mv_data;
if (pKey->dwContactID != contactID)
break;
mdb_cursor_del(cursor, 0);
} while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS);
}
txn.commit();
}
MDB_val key = { sizeof(MCONTACT), &contactID };
for (;; Remap())
{
txn_ptr trnlck(m_pMdbEnv);
MDB_CHECK(mdb_del(trnlck, m_dbContacts, &key, nullptr), 1);
if (trnlck.commit())
break;
}
m_contactCount--;
m_cache->FreeCachedContact(contactID);
if (contactID == m_hLastCachedContact)
m_hLastCachedContact = NULL;
lck.unlock();
return 0;
}
STDMETHODIMP_(MCONTACT) CDbxMdb::AddContact()
{
DWORD dwContactId;
{
mir_cslock lck(m_csDbAccess);
dwContactId = ++m_dwMaxContactId;
DBCachedContact *cc = m_cache->AddContactToCache(dwContactId);
cc->dbc.dwSignature = DBCONTACT_SIGNATURE;
MDB_val key = { sizeof(MCONTACT), &dwContactId };
MDB_val data = { sizeof(cc->dbc), &cc->dbc };
for (;; Remap()) {
txn_ptr trnlck(m_pMdbEnv);
MDB_CHECK(mdb_put(trnlck, m_dbContacts, &key, &data, 0), 0);
if (trnlck.commit())
break;
}
m_contactCount++;
}
NotifyEventHooks(hContactAddedEvent, dwContactId, 0);
return dwContactId;
}
STDMETHODIMP_(BOOL) CDbxMdb::IsDbContact(MCONTACT contactID)
{
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
return (cc != NULL);
}
/////////////////////////////////////////////////////////////////////////////////////////
// metacontacts support
BOOL CDbxMdb::MetaDetouchSub(DBCachedContact *cc, int nSub)
{
CallService(MS_DB_MODULE_DELETE, cc->pSubs[nSub], (LPARAM)META_PROTO);
return 0;
}
BOOL CDbxMdb::MetaSetDefault(DBCachedContact *cc)
{
return db_set_dw(cc->contactID, META_PROTO, "Default", cc->nDefault);
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDbxMdb::GatherContactHistory(MCONTACT hContact, LIST<EventItem> &list)
{
DBEventSortingKey keyVal = { 0, 0, hContact };
MDB_val key = { sizeof(keyVal), &keyVal }, data;
txn_ptr_ro trnlck(m_txn);
cursor_ptr_ro cursor(m_curEventsSort);
if (mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE) == MDB_SUCCESS)
{
do {
const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.mv_data;
if (pKey->dwContactId != hContact)
return;
list.insert(new EventItem(pKey->ts, pKey->dwEventId));
} while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS);
}
}
BOOL CDbxMdb::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
{
LIST<EventItem> list(10000);
GatherContactHistory(ccSub->contactID, list);
for (int i = 0; i < list.getCount(); i++) {
EventItem *EI = list[i];
for (;; Remap())
{
txn_ptr trnlck(m_pMdbEnv);
DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID };
MDB_val key = { sizeof(insVal), &insVal }, data = { 1, "" };
mdb_put(trnlck, m_dbEventsSort, &key, &data, 0);
if (trnlck.commit())
break;
}
ccMeta->dbc.dwEventCount++;
delete EI;
}
MDB_val keyc = { sizeof(MCONTACT), &ccMeta->contactID }, datac = { sizeof(ccMeta->dbc), &ccMeta->dbc };
for (;; Remap())
{
txn_ptr trnlck(m_pMdbEnv);
MDB_CHECK(mdb_put(trnlck, m_dbContacts, &keyc, &datac, 0), 1);
if (trnlck.commit())
break;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
BOOL CDbxMdb::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub)
{
LIST<EventItem> list(10000);
GatherContactHistory(ccSub->contactID, list);
for (int i = 0; i < list.getCount(); i++) {
EventItem *EI = list[i];
for (;; Remap()) {
txn_ptr trnlck(m_pMdbEnv);
DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID };
MDB_val key = { sizeof(insVal), &insVal }, data = { 1, "" };
mdb_del(trnlck, m_dbEventsSort, &key, &data);
if (trnlck.commit())
break;
}
ccMeta->dbc.dwEventCount--;
delete EI;
}
MDB_val keyc = { sizeof(MCONTACT), &ccMeta->contactID }, datac = { sizeof(ccMeta->dbc), &ccMeta->dbc };
for (;; Remap())
{
txn_ptr trnlck(m_pMdbEnv);
MDB_CHECK(mdb_put(trnlck, m_dbContacts, &keyc, &datac, 0), 1);
if (trnlck.commit())
break;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
void DBCachedContact::Advance(DWORD id, DBEvent &dbe)
{
dbc.dwEventCount++;
if (dbe.flags & (DBEF_READ | DBEF_SENT))
return;
if (dbe.timestamp < dbc.tsFirstUnread || dbc.tsFirstUnread == 0) {
dbc.tsFirstUnread = dbe.timestamp;
dbc.dwFirstUnread = id;
}
}
void DBCachedContact::Snapshot()
{
memcpy(&tmp_dbc, &dbc, sizeof(dbc));
}
void DBCachedContact::Revert()
{
memcpy(&dbc, &tmp_dbc, sizeof(dbc));
}
/////////////////////////////////////////////////////////////////////////////////////////
// initial cycle to fill the contacts' cache
void CDbxMdb::FillContacts()
{
LIST<DBCachedContact> arContacts(10);
{
mir_cslock lck(m_csDbAccess);
txn_ptr_ro trnlck(m_txn);
cursor_ptr_ro cursor(m_curContacts);
MDB_val key, data;
while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == 0)
{
const DBContact *dbc = (const DBContact*)data.mv_data;
if (dbc->dwSignature != DBCONTACT_SIGNATURE)
DatabaseCorruption(NULL);
DBCachedContact *cc = m_cache->AddContactToCache(*(DWORD*)key.mv_data);
cc->dbc.dwSignature = DBCONTACT_SIGNATURE;
cc->dbc.dwEventCount = dbc->dwEventCount;
cc->dbc.dwFirstUnread = dbc->dwFirstUnread;
cc->dbc.tsFirstUnread = dbc->tsFirstUnread;
arContacts.insert(cc);
}
}
m_contactCount = 0;
for (int i = 0; i < arContacts.getCount(); i++)
{
DBCachedContact *cc = arContacts[i];
CheckProto(cc, "");
m_contactCount++;
DBVARIANT dbv; dbv.type = DBVT_DWORD;
cc->nSubs = (0 != GetContactSetting(cc->contactID, META_PROTO, "NumContacts", &dbv)) ? -1 : dbv.dVal;
if (cc->nSubs != -1) {
cc->pSubs = (MCONTACT*)mir_alloc(cc->nSubs*sizeof(MCONTACT));
for (int k = 0; k < cc->nSubs; k++) {
char setting[100];
mir_snprintf(setting, _countof(setting), "Handle%d", k);
cc->pSubs[k] = (0 != GetContactSetting(cc->contactID, META_PROTO, setting, &dbv)) ? NULL : dbv.dVal;
}
}
cc->nDefault = (0 != GetContactSetting(cc->contactID, META_PROTO, "Default", &dbv)) ? -1 : dbv.dVal;
cc->parentID = (0 != GetContactSetting(cc->contactID, META_PROTO, "ParentMeta", &dbv)) ? NULL : dbv.dVal;
}
}
|