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
|
/*
Variables Plugin for Miranda-IM (www.miranda-im.org)
Copyright 2003-2006 P. Boon
This program is mir_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 "variables.h"
struct _tagType
{
int cnfCode;
TCHAR* str;
}
static builtinCnfs[] =
{
{ CNF_FIRSTNAME, _T(STR_FIRSTNAME) },
{ CNF_LASTNAME, _T(STR_LASTNAME) },
{ CNF_NICK, _T(STR_NICK) },
{ CNF_CUSTOMNICK, _T(STR_CUSTOMNICK) },
{ CNF_EMAIL, _T(STR_EMAIL) },
{ CNF_CITY, _T(STR_CITY) },
{ CNF_STATE, _T(STR_STATE) },
{ CNF_COUNTRY, _T(STR_COUNTRY) },
{ CNF_PHONE, _T(STR_PHONE) },
{ CNF_HOMEPAGE, _T(STR_HOMEPAGE) },
{ CNF_ABOUT, _T(STR_ABOUT) },
{ CNF_GENDER, _T(STR_GENDER) },
{ CNF_AGE, _T(STR_AGE) },
{ CNF_FIRSTLAST, _T(STR_FIRSTLAST) },
{ CNF_UNIQUEID, _T(STR_UNIQUEID) },
{ CNF_DISPLAY, _T(STR_DISPLAY) },
{ CNF_FAX, _T(STR_FAX) },
{ CNF_CELLULAR, _T(STR_CELLULAR) },
{ CNF_TIMEZONE, _T(STR_TIMEZONE) },
{ CNF_MYNOTES, _T(STR_MYNOTES) },
{ CNF_BIRTHDAY, _T(STR_BIRTHDAY) },
{ CNF_BIRTHMONTH, _T(STR_BIRTHMONTH) },
{ CNF_BIRTHYEAR, _T(STR_BIRTHYEAR) },
{ CNF_STREET, _T(STR_STREET) },
{ CNF_ZIP, _T(STR_ZIP) },
{ CNF_LANGUAGE1, _T(STR_LANGUAGE1) },
{ CNF_LANGUAGE2, _T(STR_LANGUAGE2) },
{ CNF_LANGUAGE3, _T(STR_LANGUAGE3) },
{ CNF_CONAME, _T(STR_CONAME) },
{ CNF_CODEPT, _T(STR_CODEPT) },
{ CNF_COPOSITION, _T(STR_COPOSITION) },
{ CNF_COSTREET, _T(STR_COSTREET) },
{ CNF_COCITY, _T(STR_COCITY) },
{ CNF_COSTATE, _T(STR_COSTATE) },
{ CNF_COZIP, _T(STR_COZIP) },
{ CNF_COCOUNTRY, _T(STR_COCOUNTRY) },
{ CNF_COHOMEPAGE, _T(STR_COHOMEPAGE) },
{ CCNF_ACCOUNT, _T(STR_ACCOUNT) },
{ CCNF_PROTOCOL, _T(STR_PROTOCOL) },
{ CCNF_STATUS, _T(STR_STATUS) },
{ CCNF_INTERNALIP, _T(STR_INTERNALIP) },
{ CCNF_EXTERNALIP, _T(STR_EXTERNALIP) },
{ CCNF_GROUP, _T(STR_GROUP) },
{ CCNF_PROTOID, _T(STR_PROTOID) }
};
typedef struct {
TCHAR* tszContact;
HANDLE hContact;
DWORD flags;
} CONTACTCE; /* contact cache entry */
/* cache for 'getcontactfromstring' service */
static CONTACTCE *cce = NULL;
static int cacheSize = 0;
static CRITICAL_SECTION csContactCache;
static HANDLE hContactSettingChangedHook;
static HANDLE hGetContactFromStringService;
/*
converts a string into a CNF_ type
*/
BYTE getContactInfoType(TCHAR* type)
{
if (type == NULL || _tcslen(type) == 0 )
return 0;
for (int i=0; i < SIZEOF(builtinCnfs); i++ )
if (!_tcscmp( builtinCnfs[i].str, type ))
return builtinCnfs[i].cnfCode;
return 0;
}
/*
returns info about a contact as a string
*/
TCHAR* getContactInfoT(BYTE type, HANDLE hContact)
{
/* returns dynamic allocated buffer with info, or NULL if failed */
TCHAR *res = NULL;
char protoname[128], szVal[16];
PROTOACCOUNT *pa;
if (hContact == NULL)
return NULL;
char *szProto = GetContactProto(hContact);
if (szProto == NULL)
return NULL;
switch (type) {
case CCNF_PROTOID:
return mir_a2t(szProto);
case CCNF_ACCOUNT:
pa = ProtoGetAccount(szProto);
return pa ? mir_tstrdup(pa->tszAccountName) : NULL;
case CCNF_PROTOCOL:
if (CallProtoService(szProto, PS_GETNAME, (WPARAM)sizeof(protoname), (LPARAM)protoname))
return NULL;
return mir_a2t(protoname);
case CCNF_STATUS:
return mir_tstrdup((TCHAR*)CallService(
MS_CLIST_GETSTATUSMODEDESCRIPTION,
db_get_w(hContact, szProto, "Status", ID_STATUS_OFFLINE),
GSMDF_UNICODE));
case CCNF_INTERNALIP:
case CCNF_EXTERNALIP:
{
DWORD ip = db_get_dw(hContact, szProto, (type == CCNF_INTERNALIP) ? "RealIP" : "IP", 0);
if (ip == 0)
return NULL;
struct in_addr in;
in.s_addr = htonl(ip);
return mir_a2t( inet_ntoa(in));
}
case CCNF_GROUP:
if ((res = db_get_tsa(hContact, "CList", "Group")) != NULL)
return res;
break;
case CNF_UNIQUEID:
//UID for ChatRoom
if (db_get_b(hContact, szProto, "ChatRoom", 0) == 1)
if ((res = db_get_tsa(hContact, szProto, "ChatRoomID")) != NULL)
return res;
//UID for other contact
break;
}
CONTACTINFO ci = { sizeof(ci) };
ci.hContact = hContact;
ci.dwFlag = type | CNF_UNICODE;
CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)&ci);
memset(szVal, '\0', sizeof(szVal));
switch(ci.type) {
case CNFT_BYTE:
if (type == CNF_GENDER) {
szVal[0] = (char)ci.bVal; szVal[1] = 0;
return mir_a2t(szVal);
}
return itot(ci.bVal);
case CNFT_WORD:
return itot(ci.wVal);
case CNFT_DWORD:
return itot(ci.dVal);
case CNFT_ASCIIZ:
return ci.pszVal;
}
return NULL;
}
/*
MS_VARS_GETCONTACTFROMSTRING
*/
int getContactFromString(CONTACTSINFO *ci)
{
/* service to retrieve a contact's HANDLE from a given string */
if (ci == NULL)
return -1;
TCHAR *tszContact;
if (ci->flags & CI_UNICODE)
tszContact = NEWTSTR_ALLOCA(ci->tszContact);
else {
WCHAR* tmp = mir_a2t(ci->szContact);
tszContact = NEWTSTR_ALLOCA(tmp);
mir_free(tmp);
}
if ((tszContact == NULL) || (_tcslen(tszContact) == 0))
return -1;
ci->hContacts = NULL;
int count = 0;
/* search the cache */
{
mir_cslock lck(csContactCache);
for (int i=0; i < cacheSize; i++) {
if ((!_tcscmp(cce[i].tszContact, tszContact)) && (ci->flags == cce[i].flags)) {
/* found in cache */
ci->hContacts = (HANDLE*)mir_alloc(sizeof(HANDLE));
if (ci->hContacts == NULL)
return -1;
ci->hContacts[0] = cce[i].hContact;
return 1;
}
}
}
/* contact was not in cache, do a search */
for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
char *szProto = GetContactProto(hContact);
if (szProto == NULL)
continue;
// <proto:id> (exact)
TCHAR *szFind = NULL;
BOOL bMatch = FALSE;
if (ci->flags & CI_PROTOID) {
TCHAR *cInfo = getContactInfoT(CNF_UNIQUEID, hContact);
if (cInfo == NULL) {
// <HANDLE:hContact>
size_t size = _tcslen(_T(PROTOID_HANDLE)) + 36;
szFind = (TCHAR *)mir_alloc(size * sizeof(TCHAR));
if (szFind != NULL) {
mir_sntprintf(szFind, size, _T("<%s:%p>"), _T(PROTOID_HANDLE), hContact);
if (!_tcsncmp(tszContact, szFind, _tcslen(tszContact)))
bMatch = TRUE;
mir_free(szFind);
}
}
else {
size_t size = _tcslen(cInfo) + strlen(szProto) + 4;
szFind = (TCHAR *)mir_alloc(size * sizeof(TCHAR));
if (szFind != NULL) {
mir_sntprintf(szFind, size, _T("<%S:%s>"), szProto, cInfo);
mir_free(cInfo);
if (!_tcsncmp(tszContact, szFind, _tcslen(tszContact)))
bMatch = TRUE;
mir_free(szFind);
}
}
}
// id (exact)
if ((ci->flags & CI_UNIQUEID) && (!bMatch)) {
szFind = getContactInfoT(CNF_UNIQUEID, hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
// nick (not exact)
if ((ci->flags & CI_NICK) && (!bMatch)) {
szFind = getContactInfoT(CNF_NICK, hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
// list name (not exact)
if ((ci->flags & CI_LISTNAME) && (!bMatch)) {
szFind = getContactInfoT(CNF_DISPLAY, hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
// firstname (exact)
if ((ci->flags & CI_FIRSTNAME) && (!bMatch)) {
szFind = getContactInfoT(CNF_FIRSTNAME, hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
// lastname (exact)
if ((ci->flags & CI_LASTNAME) && (!bMatch)) {
szFind = getContactInfoT(CNF_LASTNAME, hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
// email (exact)
if ((ci->flags & CI_EMAIL) && (!bMatch)) {
szFind = getContactInfoT(CNF_EMAIL, hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
// CNF_ (exact)
if ((ci->flags & CI_CNFINFO) && (!bMatch)) {
szFind = getContactInfoT((BYTE)(ci->flags&~(CI_CNFINFO|CI_TCHAR)), hContact);
if (szFind != NULL) {
if (!_tcscmp(tszContact, szFind))
bMatch = TRUE;
mir_free(szFind);
}
}
if (bMatch) {
ci->hContacts = (HANDLE*)mir_realloc(ci->hContacts, (count+1)*sizeof(HANDLE));
if (ci->hContacts == NULL)
return -1;
ci->hContacts[count] = hContact;
count += 1;
}
}
if (count == 1) { /* cache the found result */
mir_cslock lck(csContactCache);
cce = (CONTACTCE*)mir_realloc(cce, (cacheSize+1)*sizeof(CONTACTCE));
if (cce != NULL) {
cce[cacheSize].hContact = ci->hContacts[0];
cce[cacheSize].flags = ci->flags;
cce[cacheSize].tszContact = mir_tstrdup(tszContact);
if (cce[cacheSize].tszContact != NULL)
cacheSize += 1;
}
}
return count;
}
/* keep cache consistent */
static int contactSettingChanged(WPARAM wParam, LPARAM lParam)
{
DBCONTACTWRITESETTING *dbw = (DBCONTACTWRITESETTING*)lParam;
HANDLE hContact = (HANDLE) wParam;
mir_cslock lck(csContactCache);
for (int i=0; i < cacheSize; i++) {
if (hContact != cce[i].hContact && (cce[i].flags & CI_CNFINFO) == 0 )
continue;
char *szProto = GetContactProto(hContact);
if (szProto == NULL)
continue;
char *uid = (char*)CallProtoService(szProto,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0);
if (((!strcmp(dbw->szSetting, "Nick")) && (cce[i].flags & CI_NICK)) ||
((!strcmp(dbw->szSetting, "FirstName")) && (cce[i].flags & CI_FIRSTNAME)) ||
((!strcmp(dbw->szSetting, "LastName")) && (cce[i].flags & CI_LASTNAME)) ||
((!strcmp(dbw->szSetting, "e-mail")) && (cce[i].flags & CI_EMAIL)) ||
((!strcmp(dbw->szSetting, "MyHandle")) && (cce[i].flags & CI_LISTNAME)) ||
(cce[i].flags & CI_CNFINFO) != 0 || // lazy; always invalidate CNF info cache entries
(( ((INT_PTR)uid != CALLSERVICE_NOTFOUND) && (uid != NULL)) && (!strcmp(dbw->szSetting, uid)) && (cce[i].flags & CI_UNIQUEID)))
{
/* remove from cache */
mir_free(cce[i].tszContact);
if (cacheSize > 1) {
MoveMemory(&cce[i], &cce[cacheSize-1], sizeof(CONTACTCE));
cce = ( CONTACTCE* )mir_realloc(cce, (cacheSize-1)*sizeof(CONTACTCE));
cacheSize -= 1;
}
else {
mir_free(cce);
cce = NULL;
cacheSize = 0;
}
break;
}
}
return 0;
}
static INT_PTR getContactFromStringSvc( WPARAM wParam, LPARAM lParam)
{
return getContactFromString(( CONTACTSINFO* )wParam );
}
int initContactModule()
{
InitializeCriticalSection(&csContactCache);
hContactSettingChangedHook = HookEvent(ME_DB_CONTACT_SETTINGCHANGED, contactSettingChanged);
hGetContactFromStringService = CreateServiceFunction(MS_VARS_GETCONTACTFROMSTRING, getContactFromStringSvc);
return 0;
}
int deinitContactModule()
{
DestroyServiceFunction(hGetContactFromStringService);
UnhookEvent(hContactSettingChangedHook);
DeleteCriticalSection(&csContactCache);
return 0;
}
// returns a string in the form <PROTOID:UNIQUEID>, cannot be _HANDLE_!
// result must be freed
TCHAR *encodeContactToString(HANDLE hContact)
{
char *szProto = GetContactProto(hContact);
TCHAR *tszUniqueId = getContactInfoT(CNF_UNIQUEID, hContact);
if (szProto == NULL || tszUniqueId == NULL)
return NULL;
size_t size = _tcslen(tszUniqueId) + strlen(szProto) + 4;
TCHAR *tszResult = (TCHAR *)mir_calloc(size * sizeof(TCHAR));
if (tszResult)
mir_sntprintf(tszResult, size, _T("<%S:%s>"), szProto, tszUniqueId);
return tszResult;
}
// returns a contact from a string in the form <PROTOID:UNIQUEID>
// returns INVALID_HANDLE_VALUE in case of an error.
HANDLE *decodeContactFromString(TCHAR *tszContact)
{
HANDLE hContact = INVALID_HANDLE_VALUE;
CONTACTSINFO ci = { sizeof(ci) };
ci.tszContact = tszContact;
ci.flags = CI_PROTOID|CI_TCHAR;
int count = getContactFromString( &ci );
if (count != 1) {
if (ci.hContacts != NULL)
mir_free(ci.hContacts);
return (HANDLE*)hContact;
}
if (ci.hContacts != NULL) {
hContact = ci.hContacts[0];
mir_free(ci.hContacts);
}
return (HANDLE*)hContact;
}
|