summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/ex_import/classExImContactBase.cpp
blob: ffbce296e98e9348acd7423f3b0e3f1560d15731 (plain)
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
552
553
554
555
556
557
558
559
560
561
562
563
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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"

HANDLE CListFindGroup(LPCSTR pszGroup)
{
	CHAR buf[4];
	BYTE i;
	DBVARIANT dbv;

	for (i = 0; i < 255; i++) {
		_itoa(i, buf, 10);
		if (DB::Setting::GetUString(NULL, "CListGroups", buf, &dbv))
			break;
		if (dbv.pszVal && pszGroup && !_stricmp(dbv.pszVal + 1, pszGroup)) {
			db_free(&dbv);
			return (HANDLE)(INT_PTR)(i+1);
		}
		db_free(&dbv);
	}
	return INVALID_HANDLE_VALUE;
}

/**
 * name:	CExImContactBase
 * class:	CExImContactBase
 * desc:	default constructor
 * param:	none
 * return:	nothing
 **/
CExImContactBase::CExImContactBase()
{
	_pszNick		= NULL;
	_pszDisp		= NULL;
	_pszGroup		= NULL;
	_pszProto		= NULL;
	_pszProtoOld	= NULL;
	_pszAMPro		= NULL;
	_pszUIDKey		= NULL;
	_dbvUIDHash		= NULL;
	ZeroMemory(&_dbvUID, sizeof(DBVARIANT));
	_hContact		= INVALID_HANDLE_VALUE;
	_isNewContact	= FALSE;
}

/**
 * name:	~CExImContactBase
 * class:	CExImContactBase
 * desc:	default denstructor
 * param:	none
 * return:	nothing
 **/
CExImContactBase::~CExImContactBase()
{
	MIR_FREE(_pszNick);
	MIR_FREE(_pszDisp);
	MIR_FREE(_pszGroup);
	MIR_FREE(_pszProtoOld);
	MIR_FREE(_pszProto);
	MIR_FREE(_pszAMPro);
	MIR_FREE(_pszUIDKey);
	db_free(&_dbvUID);
}

/**
 * name:	fromDB
 * class:	CExImContactBase
 * desc:	get contact information from database
 * param:	hContact	- handle to contact whose information to read
 * return:	TRUE if successful or FALSE otherwise
 **/
BYTE CExImContactBase::fromDB(HANDLE hContact)
{
	BYTE		ret			= FALSE;
	BYTE		isChatRoom	= FALSE;
	LPSTR		pszProto;
	LPCSTR		uidSetting;
	DBVARIANT	dbv;
	
	_hContact				= hContact;
	_dbvUIDHash				= 0;
	MIR_FREE(_pszProtoOld);
	MIR_FREE(_pszProto);
	MIR_FREE(_pszAMPro);
	MIR_FREE(_pszNick);
	MIR_FREE(_pszDisp);
	MIR_FREE(_pszGroup);
	MIR_FREE(_pszUIDKey);
	db_free(&_dbvUID);
	ZeroMemory(&_dbvUID, sizeof(DBVARIANT));

	// OWNER
	if (!_hContact) return TRUE;
	
	// Proto
	if (!(pszProto = DB::Contact::Proto(_hContact))) return FALSE;
	_pszProto = mir_strdup(pszProto);

	// AM_BaseProto
	if (!DB::Setting::GetUString(NULL, pszProto, "AM_BaseProto", &dbv )) {
		_pszAMPro = mir_strdup(dbv.pszVal);
		db_free(&dbv);
	}

	// unique id (for ChatRoom)
	if (isChatRoom = db_get_b(_hContact, pszProto, "ChatRoom", 0)) {
		uidSetting = "ChatRoomID";
		_pszUIDKey = mir_strdup(uidSetting);
		if (!DB::Setting::GetAsIs(_hContact, pszProto, uidSetting, &_dbvUID)) {
			ret = TRUE;
		}
	}
	// unique id (normal)
	else {
		uidSetting = (LPCSTR)CallProtoService(pszProto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);
		// valid
		if (uidSetting != NULL && (INT_PTR)uidSetting != CALLSERVICE_NOTFOUND) {
			_pszUIDKey = mir_strdup(uidSetting);
			if (!DB::Setting::GetAsIs(_hContact, pszProto, uidSetting, &_dbvUID)) {
				ret = TRUE;
			}
		}
		// fails because the protocol is no longer installed
		else {
			// assert(ret == TRUE);
			ret = TRUE;
		}
	}
	
	// nickname
	if (!DB::Setting::GetUString(_hContact, pszProto, SET_CONTACT_NICK, &dbv)) {
		_pszNick = mir_strdup(dbv.pszVal);
		db_free(&dbv);
	}

	if (_hContact && ret) {
	// Clist Group
		if (!DB::Setting::GetUString(_hContact, MOD_CLIST, "Group", &dbv)) {
			_pszGroup = mir_strdup(dbv.pszVal);
			db_free(&dbv);
		}
	// Clist DisplayName
		if (!DB::Setting::GetUString(_hContact, MOD_CLIST, SET_CONTACT_MYHANDLE, &dbv)) {
			_pszDisp = mir_strdup(dbv.pszVal);
			db_free(&dbv);
		}
	}
	return ret;
}

/**
 * name:	fromIni
 * class:	CExImContactBase
 * desc:	get contact information from a row of a ini file
 * param:	row	- the rows data
 * return:	TRUE if successful or FALSE otherwise
 **/
BYTE CExImContactBase::fromIni(LPSTR& row)
{
	LPSTR p1, p2 = NULL;
	LPSTR pszUIDValue, pszUIDSetting, pszProto = NULL;
	LPSTR pszBuf = &row[0];
	size_t cchBuf = strlen(row);

	MIR_FREE(_pszProtoOld);
	MIR_FREE(_pszProto);
	MIR_FREE(_pszAMPro);
	MIR_FREE(_pszNick);
	MIR_FREE(_pszDisp);
	MIR_FREE(_pszGroup);
	MIR_FREE(_pszUIDKey);
	db_free(&_dbvUID);
	ZeroMemory(&_dbvUID, sizeof(DBVARIANT));
	_dbvUIDHash = 0;

	// read uid value
	if (cchBuf > 10 && (p1 = mir_strrchr(pszBuf, '*{')) && (p2 = mir_strchr(p1, '}*')) && p1 + 2 < p2) {
		pszUIDValue = p1 + 1;
		*p1 = *(p2 - 1) = 0;

		// insulate the uid setting from buffer pointer
		if (cchBuf > 0 && (p1 = mir_strrchr(pszBuf, '*<')) && (p2 = mir_strchr(p1, '>*')) && p1 + 2 < p2) {
			pszUIDSetting = p1 + 1;
			*p1 = *(p2 - 1) = 0;

			// insulate the protocol name from buffer pointer
			if (cchBuf > 0 && (p1 = mir_strrchr(pszBuf, '*(')) && (p2 = mir_strchr(p1, ')*')) && p1 + 2 < p2) {
				pszProto = p1 + 1;
				*(--p1) = *(p2 - 1) = 0;

				// DBVT_DWORD
				if (strspn(pszUIDValue, "0123456789") == mir_strlen(pszUIDValue)) {
					_dbvUID.dVal = _atoi64(pszUIDValue);
					_dbvUID.type = DBVT_DWORD;
				}
				else {
				// DBVT_UTF8
					_dbvUID.pszVal = mir_strdup(pszUIDValue);
					_dbvUID.type = DBVT_UTF8;
				}
				_pszUIDKey = mir_strdup(pszUIDSetting);
				_pszProto = mir_strdup(pszProto);
			} //end insulate the protocol name from buffer pointer
		} //end insulate the uid setting from buffer pointer
	} //end read uid value

	// create valid nickname
	_pszNick = mir_strdup(pszBuf);
	size_t i = strlen(_pszNick)-1;
	while (i > 0 && (_pszNick[i] == ' ' || _pszNick[i] == '\t')) {
		_pszNick[i] = 0;
		i--;
	}
	// finally try to find contact in contact list
	findHandle();
	return FALSE;
}

/**
 * name:	toDB
 * class:	CExImContactBase
 * desc:	searches the database for a contact representing the one
 *			identified by this class or creates a new one if it was not found
 * param:	hMetaContact - a meta contact to add this contact to
 * return:	handle of the contact if successful
 **/
HANDLE CExImContactBase::toDB()
{
	// create new contact if none exists
	if (_hContact == INVALID_HANDLE_VALUE && _pszProto && _pszUIDKey && _dbvUID.type != DBVT_DELETED) {
		PROTOACCOUNT* pszAccount = 0;
		if (NULL == (pszAccount = ProtoGetAccount( _pszProto ))) {
			//account does not exist
			_hContact = INVALID_HANDLE_VALUE;
			return INVALID_HANDLE_VALUE;
		}
		if (!IsAccountEnabled(pszAccount)) {
			;
		}
		// create new contact
		_hContact = DB::Contact::Add();
		if (!_hContact) {
			_hContact = INVALID_HANDLE_VALUE;
			return INVALID_HANDLE_VALUE;
		}
		// Add the protocol to the new contact
		if (CallService(MS_PROTO_ADDTOCONTACT, (WPARAM)_hContact, (LPARAM)_pszProto)) {
			DB::Contact::Delete(_hContact);
			_hContact = INVALID_HANDLE_VALUE;
			return INVALID_HANDLE_VALUE;
		}
		// write uid to protocol module
		if (db_set(_hContact, _pszProto, _pszUIDKey, &_dbvUID)) {
			DB::Contact::Delete(_hContact);
			_hContact = INVALID_HANDLE_VALUE;
			return INVALID_HANDLE_VALUE;
		}
		// write nick and display name
		if (_pszNick) db_set_utf(_hContact, _pszProto, SET_CONTACT_NICK, _pszNick);
		if (_pszDisp) db_set_utf(_hContact, MOD_CLIST, SET_CONTACT_MYHANDLE, _pszDisp);

		// add group
		if (_pszGroup) {
			db_set_utf(_hContact, MOD_CLIST, "Group", _pszGroup);
			if (CListFindGroup(_pszGroup) == INVALID_HANDLE_VALUE) {
				WPARAM hGroup = (WPARAM)CallService(MS_CLIST_GROUPCREATE, 0, 0);
				if (hGroup) {
					// renaming twice is stupid but the only way to avoid error dialog telling shit like
					// a group with that name does exist
					CallService(MS_CLIST_GROUPRENAME, (WPARAM)hGroup, (LPARAM)_pszGroup);
				}
			}
		}
	}
	return _hContact;
}

/**
 * name:	toIni
 * class:	CExImContactBase
 * desc:	writes the line to an opened ini file which identifies the contact
 *			whose information are stored in this class
 * param:	file	- pointer to the opened file
 * return:	nothing
 **/
void CExImContactBase::toIni(FILE* file, int modCount)
{
	// getting dbeditor++ NickFromHContact(hContact)
	static char name[512] = "";
	char* ret = 0;

	if (_hContact){
		int loaded = _pszUIDKey ? 1 : 0;
		if (_pszProto == NULL || !loaded) {
			if (_pszProto){
				if (_pszNick)
					mir_snprintf(name, sizeof(name),"%s (%s)", _pszNick, _pszProto);
				else
					mir_snprintf(name, sizeof(name),"(UNKNOWN) (%s)", _pszProto);
			}
			else
				mir_snprintf(name, sizeof(name),"(UNKNOWN)");
		}
		else {
			// Proto loadet - GetContactName(hContact,pszProto,0)
			LPSTR pszCI	= NULL;
			CONTACTINFO ci;
			ZeroMemory(&ci, sizeof(ci));

			ci.cbSize		= sizeof(ci);
			ci.hContact		= _hContact;
			ci.szProto		= _pszProto;
			ci.dwFlag		= CNF_DISPLAY;

			if (!GetContactInfo(NULL, (LPARAM) &ci)) {
				// CNF_DISPLAY always returns a string type
				pszCI = (LPSTR)ci.pszVal;
			}
			LPSTR pszUID = uid2String(FALSE);
			if (_pszUIDKey && pszUID)
				mir_snprintf(name, sizeof(name), "%s *(%s)*<%s>*{%s}*", pszCI, _pszProto, _pszUIDKey, pszUID);
			else 
				mir_snprintf(name, sizeof(name), "%s (%s)", pszCI, _pszProto);

			mir_free(pszCI);
			mir_free(pszUID);
		} // end else (Proto loadet)

		// it is not the best solution (but still works if only basic modules export) - need rework
		if (modCount > 3)
			fprintf(file, "CONTACT: %s\n", name);
		else
			fprintf(file, "FROM CONTACT: %s\n", name);

	} // end *if (_hContact)
	else {
		fprintf(file, "SETTINGS:\n");
	}
}

BYTE CExImContactBase::compareUID(DBVARIANT *dbv)
{
	DWORD hash = 0;
	switch (dbv->type) {
		case DBVT_BYTE:
			if (dbv->bVal == _dbvUID.bVal) {
				_dbvUID.type = dbv->type;
				return TRUE;
			}
			break;
		case DBVT_WORD:
			if (dbv->wVal == _dbvUID.wVal) {
				_dbvUID.type = dbv->type;
				return TRUE;
			}
			break;
		case DBVT_DWORD:
			if (dbv->dVal == _dbvUID.dVal) {
				_dbvUID.type = dbv->type;
				return TRUE;
			}
			break;
		case DBVT_ASCIIZ:
			hash = hashSetting_M2(dbv->pszVal);
		case DBVT_WCHAR:
			if (!hash) hash = hashSettingW_M2((const char *)dbv->pwszVal);
		case DBVT_UTF8:
			if (!hash) {
				LPWSTR tmp = mir_utf8decodeW(dbv->pszVal);
				hash = hashSettingW_M2((const char *)tmp);
				mir_free(tmp);
			}
			if (hash == _dbvUIDHash) return TRUE;
			break;
		case DBVT_BLOB:		//'n' cpbVal and pbVal are valid
			if (_dbvUID.type == dbv->type && 
				_dbvUID.cpbVal == dbv->cpbVal &&
				memcmp(_dbvUID.pbVal, dbv->pbVal, dbv->cpbVal) == 0) {
				return TRUE;
			}
			break;
		default:
			return FALSE;
	}
	return FALSE;
}

LPSTR CExImContactBase::uid2String(BYTE bPrependType)
{
	CHAR szUID[MAX_PATH];
	LPSTR ptr = szUID;

	switch (_dbvUID.type) {
		case DBVT_BYTE:		//'b' bVal and cVal are valid
			if (bPrependType) *ptr++ = 'b';
			_itoa(_dbvUID.bVal, ptr, 10);
			break;
		case DBVT_WORD:		//'w' wVal and sVal are valid
			if (bPrependType) *ptr++ = 'w';
			_itoa(_dbvUID.wVal, ptr, 10);
			break;
		case DBVT_DWORD:	//'d' dVal and lVal are valid
			if (bPrependType) *ptr++ = 'd';
			_itoa(_dbvUID.dVal, ptr, 10);
			break;
		case DBVT_WCHAR:	//'u' pwszVal is valid
		{
			LPSTR r = mir_utf8encodeW(_dbvUID.pwszVal);
			if (bPrependType) {
				LPSTR t = (LPSTR)mir_alloc(strlen(r)+2);
				t[0] = 'u';
				strcpy(t+1, r);
				mir_free(r);
				r = t;
			}
			return r;
		}
		case DBVT_UTF8:		//'u' pszVal is valid
			{
				if (bPrependType) *ptr++ = 'u';
				mir_strncpy(ptr, _dbvUID.pszVal, SIZEOF(szUID)-1);
			}
			break;
		case DBVT_ASCIIZ: {
			LPSTR r = mir_utf8encode(_dbvUID.pszVal);
			if (bPrependType) {
				LPSTR t = (LPSTR)mir_alloc(strlen(r)+2);
				t[0] = 's';
				strcpy(t+1, r);
				mir_free(r);
				r = t;
			}
			return r;
		}
		case DBVT_BLOB:		//'n' cpbVal and pbVal are valid
		{
			if (bPrependType) {		//True = XML
				INT_PTR baselen = Base64EncodeGetRequiredLength(_dbvUID.cpbVal, BASE64_FLAG_NOCRLF);
				LPSTR t = (LPSTR)mir_alloc(baselen + 5 + bPrependType);
				assert(t != NULL);
				if (Base64Encode(_dbvUID.pbVal, _dbvUID.cpbVal, t + bPrependType, &baselen, BASE64_FLAG_NOCRLF)) {
					if (baselen){
						t[baselen + bPrependType] = 0;
						if (bPrependType) t[0] = 'n';
						return t;
					}
				}
				mir_free(t);
				return NULL;
			}
			else {		//FALSE = INI
				WORD j;
				INT_PTR baselen = (_dbvUID.cpbVal * 3);
				LPSTR t = (LPSTR)mir_alloc(3 + baselen);
				ZeroMemory(t, (3 + baselen));
				for (j = 0; j < _dbvUID.cpbVal; j++) {
					mir_snprintf((t + bPrependType + (j * 3)), 4,"%02X ", (BYTE)_dbvUID.pbVal[j]);
				}
				if (t){
					if (bPrependType) t[0] = 'n';
					return t;
				}
				else {
					mir_free(t);
					return NULL;
				}
			}
			break;
		}
		default:
			return NULL;
	}
	return mir_strdup(szUID);
}

BYTE CExImContactBase::isMeta() const
{
	return DB::Module::IsMeta(_pszProto);
}

BYTE CExImContactBase::isHandle(HANDLE hContact)
{
	LPCSTR pszProto;
	DBVARIANT dbv;
	BYTE isEqual = FALSE;

	// owner contact ?
	if (!_pszProto) return hContact == NULL;

	// compare protocols
	pszProto = DB::Contact::Proto(hContact);
	if (pszProto == NULL || (INT_PTR)pszProto == CALLSERVICE_NOTFOUND || lstrcmpA(pszProto, _pszProto))
		return FALSE;

	// compare uids
	if (_pszUIDKey) {
		// get uid
		if (DB::Setting::GetAsIs(hContact, pszProto,_pszUIDKey, &dbv))
			return FALSE;

		isEqual = compareUID (&dbv);
		db_free(&dbv);
	}
	// compare nicknames if no UID
	else if (!DB::Setting::GetUString(hContact, _pszProto, SET_CONTACT_NICK, &dbv)) {
		if (dbv.type == DBVT_UTF8 && dbv.pszVal && !mir_stricmp(dbv.pszVal,_pszNick)) {
			LPTSTR ptszNick = mir_utf8decodeT(_pszNick);
			LPTSTR ptszProto = mir_a2t(_pszProto);
			int ans = MsgBox(NULL, MB_ICONQUESTION|MB_YESNO, LPGENT("Question"), LPGENT("contact identificaion"),
				LPGENT("The contact %s(%s) has no unique id in the vCard,\nbut there is a contact in your clist with the same nick and protocol.\nDo you wish to use this contact?"),
				ptszNick, ptszProto);
			MIR_FREE(ptszNick);
			MIR_FREE(ptszProto);
			isEqual = ans == IDYES;
		}
		db_free(&dbv);
	}
	return isEqual;
}

/**
 * name:	handle
 * class:	CExImContactBase
 * desc:	return the handle of the contact
 *			whose information are stored in this class
 * param:	none
 * return:	handle if successful, INVALID_HANDLE_VALUE otherwise
 **/
HANDLE CExImContactBase::findHandle()
{
	HANDLE hContact;

	for (hContact = db_find_first(); hContact != NULL; hContact  = db_find_next(hContact))
	{
		if (isHandle(hContact)) {
			_hContact = hContact;
			_isNewContact = FALSE;
			return hContact;
		}
	}
	_isNewContact = TRUE;
	_hContact = INVALID_HANDLE_VALUE;
	return INVALID_HANDLE_VALUE;
}