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
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright 2012-14 Miranda NG project,
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"
int CDb3Mmap::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) CDb3Mmap::GetContactCount(void)
{
mir_cslock lck(m_csDbAccess);
return m_dbHeader.contactCount;
}
STDMETHODIMP_(MCONTACT) CDb3Mmap::FindFirstContact(const char *szProto)
{
mir_cslock lck(m_csDbAccess);
DBCachedContact *cc = m_cache->GetFirstContact();
if (cc == NULL)
return NULL;
if (!szProto || CheckProto(cc, szProto))
return cc->contactID;
return FindNextContact(cc->contactID, szProto);
}
STDMETHODIMP_(MCONTACT) CDb3Mmap::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) CDb3Mmap::DeleteContact(MCONTACT contactID)
{
if (contactID == NULL)
return 1;
mir_cslockfull lck(m_csDbAccess);
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
if (cc == NULL)
return 1;
DBContact *dbc = (DBContact*)DBRead(cc->dwDriverData, sizeof(DBContact), NULL);
if (dbc->signature != DBCONTACT_SIGNATURE)
return 1;
if (cc->dwDriverData == m_dbHeader.ofsUser) {
log0("FATAL: del of user chain attempted.");
return 1;
}
lck.unlock();
log0("del contact");
// call notifier while outside mutex
NotifyEventHooks(hContactDeletedEvent, contactID, 0);
// get back in
lck.lock();
// delete settings chain
DWORD ofsThis = dbc->ofsFirstSettings;
DWORD ofsFirstEvent = dbc->ofsFirstEvent;
while (ofsThis) {
DBContactSettings *dbcs = (DBContactSettings*)DBRead(ofsThis, sizeof(DBContactSettings), NULL);
DWORD ofsNext = dbcs->ofsNext;
DeleteSpace(ofsThis, offsetof(DBContactSettings, blob) + dbcs->cbBlob);
ofsThis = ofsNext;
}
// delete event chain
ofsThis = ofsFirstEvent;
while (ofsThis) {
DBEvent *dbe = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
DWORD ofsNext = dbe->ofsNext;
DeleteSpace(ofsThis, offsetof(DBEvent, blob) + dbe->cbBlob);
ofsThis = ofsNext;
}
// find previous contact in chain and change ofsNext
if (m_dbHeader.ofsFirstContact == cc->dwDriverData) {
m_dbHeader.ofsFirstContact = dbc->ofsNext;
DBWrite(0, &m_dbHeader, sizeof(m_dbHeader));
}
else {
DWORD ofsNext = dbc->ofsNext;
ofsThis = m_dbHeader.ofsFirstContact;
DBContact *dbcPrev = (DBContact*)DBRead(ofsThis, sizeof(DBContact), NULL);
while (dbcPrev->ofsNext != cc->dwDriverData) {
if (dbcPrev->ofsNext == 0) DatabaseCorruption(NULL);
ofsThis = dbcPrev->ofsNext;
dbcPrev = (DBContact*)DBRead(ofsThis, sizeof(DBContact), NULL);
}
dbcPrev->ofsNext = ofsNext;
DBWrite(ofsThis, dbcPrev, sizeof(DBContact));
}
// delete contact
DeleteSpace(cc->dwDriverData, sizeof(DBContact));
// decrement contact count
m_dbHeader.contactCount--;
DBWrite(0, &m_dbHeader, sizeof(m_dbHeader));
DBFlush(0);
// free cache item
m_cache->FreeCachedContact(contactID);
if (contactID == m_hLastCachedContact)
m_hLastCachedContact = NULL;
return 0;
}
STDMETHODIMP_(HANDLE) CDb3Mmap::AddContact()
{
DWORD ofsNew;
log0("add contact");
DBContact dbc = { 0 };
dbc.signature = DBCONTACT_SIGNATURE;
{
mir_cslock lck(m_csDbAccess);
ofsNew = CreateNewSpace(sizeof(DBContact));
dbc.ofsNext = m_dbHeader.ofsFirstContact;
dbc.dwContactID = m_dwMaxContactId++;
m_dbHeader.ofsFirstContact = ofsNew;
m_dbHeader.contactCount++;
DBWrite(ofsNew, &dbc, sizeof(DBContact));
DBWrite(0, &m_dbHeader, sizeof(m_dbHeader));
DBFlush(0);
}
DBCachedContact *cc = m_cache->AddContactToCache(dbc.dwContactID);
cc->dwDriverData = ofsNew;
NotifyEventHooks(hContactAddedEvent, dbc.dwContactID, 0);
return (HANDLE)dbc.dwContactID;
}
STDMETHODIMP_(BOOL) CDb3Mmap::IsDbContact(MCONTACT contactID)
{
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
if (cc == NULL)
return FALSE;
mir_cslock lck(m_csDbAccess);
DBContact *dbc = (DBContact*)DBRead(cc->dwDriverData, sizeof(DBContact), NULL);
if (dbc->signature == DBCONTACT_SIGNATURE) {
m_cache->AddContactToCache(contactID);
return TRUE;
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////
// contacts convertor
#define OLD_CONTACT_SIZE offsetof(DBContact, dwContactID)
void CDb3Mmap::ConvertContacts()
{
DBContact *pPrev = NULL;
m_dbHeader.ofsUser = ReallocSpace(m_dbHeader.ofsUser, OLD_CONTACT_SIZE, sizeof(DBContact));
for (DWORD dwOffset = m_dbHeader.ofsFirstContact; dwOffset != 0;) {
DBContact *pOld = (DBContact*)DBRead(dwOffset, sizeof(DBContact), NULL);
DWORD dwNew = ReallocSpace(dwOffset, OLD_CONTACT_SIZE, sizeof(DBContact));
DBContact *pNew = (DBContact*)DBRead(dwNew, sizeof(DBContact), NULL);
pNew->dwContactID = m_dwMaxContactId++;
if (pPrev == NULL)
m_dbHeader.ofsFirstContact = dwNew;
else
pPrev->ofsNext = dwNew;
pPrev = pNew;
dwOffset = pNew->ofsNext;
}
FlushViewOfFile(m_pDbCache, 0);
}
void CDb3Mmap::FillContacts()
{
for (DWORD dwOffset = m_dbHeader.ofsFirstContact; dwOffset != 0;) {
DBContact *p = (DBContact*)DBRead(dwOffset, sizeof(DBContact), NULL);
if (p->signature != DBCONTACT_SIGNATURE)
break;
DWORD dwContactID;
if (m_dbHeader.version >= DB_095_VERSION) {
dwContactID = p->dwContactID;
if (dwContactID > m_dwMaxContactId)
m_dwMaxContactId = dwContactID + 1;
}
else dwContactID = m_dwMaxContactId++;
DBCachedContact *cc = m_cache->AddContactToCache(dwContactID);
cc->dwDriverData = dwOffset;
CheckProto(cc, "");
DBVARIANT dbv; dbv.type = DBVT_DWORD;
cc->nSubs = (0 != GetContactSetting(dwContactID, META_PROTO, "NumContacts", &dbv)) ? -1 : dbv.dVal;
if (cc->nSubs != -1) {
cc->pSubs = (MCONTACT*)mir_alloc(cc->nSubs*sizeof(MCONTACT));
for (int i = 0; i < cc->nSubs; i++) {
char setting[100];
mir_snprintf(setting, sizeof(setting), "Handle%d", i);
cc->pSubs[i] = (0 != GetContactSetting(dwContactID, META_PROTO, setting, &dbv)) ? NULL : dbv.dVal;
}
}
cc->activeID = (0 != GetContactSetting(dwContactID, META_PROTO, "Default", &dbv)) ? NULL : dbv.dVal;
cc->parentID = (0 != GetContactSetting(dwContactID, META_PROTO, "Handle", &dbv)) ? NULL : dbv.dVal;
// whether we need conversion or not
if (!GetContactSetting(dwContactID, META_PROTO, "MetaID", &dbv)) {
// we don't need it anymore
DeleteContactSetting(dwContactID, META_PROTO, "MetaID");
for (int i = 0; i < cc->nSubs; i++) {
// store contact id instead of the old mc number
DBCONTACTWRITESETTING dbws = { META_PROTO, "ParentMeta" };
dbws.value.type = DBVT_DWORD;
dbws.value.dVal = dwContactID;
WriteContactSetting(cc->pSubs[i], &dbws);
// wipe out old data from subcontacts
DeleteContactSetting(cc->pSubs[i], META_PROTO, "ContactNumber");
DeleteContactSetting(cc->pSubs[i], META_PROTO, "MetaLink");
}
}
dwOffset = p->ofsNext;
}
}
DWORD CDb3Mmap::GetContactOffset(MCONTACT contactID)
{
if (contactID == 0)
return m_dbHeader.ofsUser;
DBCachedContact *cc = m_cache->GetCachedContact(contactID);
return (cc == NULL) ? 0 : cc->dwDriverData;
}
|