summaryrefslogtreecommitdiff
path: root/plugins/Variables/src/parse_miranda.cpp
blob: 43610db3fc89d14964e124851a2b6cd3dbf1867a (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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
    Variables Plugin for Miranda-IM (www.miranda-im.org)
    Copyright 2003-2006 P. Boon

    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"

static wchar_t* parseCodeToStatus(ARGUMENTSINFO *ai)
{
	if (ai->argc != 2)
		return nullptr;

	unsigned int status = ttoi(ai->targv[1]);
	wchar_t *szStatus = pcli->pfnGetStatusModeDescription(status, 0);
	if (szStatus != nullptr)
		return mir_wstrdup(szStatus);

	return nullptr;
}

static int getContactInfoFlags(wchar_t *tszDesc)
{
	int flags = 0;
	for (wchar_t *cur = tszDesc; (cur < (tszDesc + mir_wstrlen(tszDesc))); cur++) {
		if (!wcsnicmp(cur, STR_PROTOID, mir_wstrlen(STR_PROTOID))) {
			flags |= CI_PROTOID;
			cur += mir_wstrlen(STR_PROTOID) - 1;
		}
		else if (!wcsnicmp(cur, STR_NICK, mir_wstrlen(STR_NICK))) {
			flags |= CI_NICK;
			cur += mir_wstrlen(STR_NICK) - 1;
		}
		else if (!wcsnicmp(cur, STR_FIRSTNAME, mir_wstrlen(STR_FIRSTNAME))) {
			flags |= CI_FIRSTNAME;
			cur += mir_wstrlen(STR_FIRSTNAME) - 1;
		}
		else if (!wcsnicmp(cur, STR_LASTNAME, mir_wstrlen(STR_LASTNAME))) {
			flags |= CI_LASTNAME;
			cur += mir_wstrlen(STR_LASTNAME) - 1;
		}
		else if (!wcsnicmp(cur, STR_DISPLAY, mir_wstrlen(STR_DISPLAY))) {
			flags |= CI_LISTNAME;
			cur += mir_wstrlen(STR_DISPLAY) - 1;
		}
		else if (!wcsnicmp(cur, STR_EMAIL, mir_wstrlen(STR_EMAIL))) {
			flags |= CI_EMAIL;
			cur += mir_wstrlen(STR_EMAIL) - 1;
		}
		else if (!wcsnicmp(cur, STR_UNIQUEID, mir_wstrlen(STR_UNIQUEID))) {
			flags |= CI_UNIQUEID;
			cur += mir_wstrlen(STR_UNIQUEID) - 1;
		}
	}
	if (flags == 0) {
		flags = getContactInfoType(tszDesc);
		if (flags != 0)
			flags |= CI_CNFINFO;
	}

	return flags;
}

static wchar_t* parseContact(ARGUMENTSINFO *ai)
{
	if (ai->argc < 3 || ai->argc > 4)
		return nullptr;

	int n = 0;
	if (ai->argc == 4) {
		if (*ai->targv[3] != 'r') // random contact
			n = -1;
		else
			n = ttoi(ai->targv[3]) - 1;
	}

	MCONTACT hContact = getContactFromString(ai->targv[1], getContactInfoFlags(ai->targv[2]), n);
	if (hContact == INVALID_CONTACT_ID)
		return nullptr;

	log_debugA("contact: %x", hContact);
	return encodeContactToString(hContact);
}

static wchar_t* parseContactCount(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return nullptr;

	int count = getContactFromString(ai->targv[1], CI_NEEDCOUNT | getContactInfoFlags(ai->targv[2]));
	return itot(count);
}

static wchar_t* parseContactInfo(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return nullptr;

	MCONTACT hContact = getContactFromString(ai->targv[1], CI_ALLFLAGS);
	if (hContact == INVALID_CONTACT_ID)
		return nullptr;

	BYTE type = getContactInfoType(ai->targv[2]);
	if (type == 0)
		return nullptr;

	return getContactInfoT(type, hContact);
}

static wchar_t* parseDBProfileName(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1)
		return nullptr;

	wchar_t name[MAX_PATH];
	if (Profile_GetNameW(_countof(name), name))
		return nullptr;

	return mir_wstrdup(name);
}

static wchar_t* parseDBProfilePath(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1)
		return nullptr;

	wchar_t path[MAX_PATH];
	Profile_GetPathW(_countof(path), path);
	return mir_wstrdup(path);
}

static wchar_t* getDBSetting(MCONTACT hContact, char* module, char* setting, wchar_t* defaultValue)
{
	DBVARIANT dbv;
	if (db_get_s(hContact, module, setting, &dbv, 0))
		return defaultValue;

	wchar_t *var = nullptr;
	switch (dbv.type) {
	case DBVT_BYTE:
		var = itot(dbv.bVal);
		break;
	case DBVT_WORD:
		var = itot(dbv.wVal);
		break;
	case DBVT_DWORD:
		var = itot(dbv.dVal);
		break;
	case DBVT_ASCIIZ:
		var = mir_a2u(dbv.pszVal);
		break;
	case DBVT_WCHAR:
		var = mir_wstrdup(dbv.pwszVal);
		break;
	case DBVT_UTF8:
		Utf8Decode(dbv.pszVal, &var);
		break;
	}

	db_free(&dbv);
	return var;
}

static wchar_t* parseDBSetting(ARGUMENTSINFO *ai)
{
	if (ai->argc < 4)
		return nullptr;

	MCONTACT hContact = NULL;
	if (mir_wstrlen(ai->targv[1]) > 0) {
		hContact = getContactFromString(ai->targv[1], CI_ALLFLAGS);
		if (hContact == INVALID_CONTACT_ID)
			return nullptr;
	}

	char *szModule = mir_u2a(ai->targv[2]);
	if (szModule == nullptr)
		return nullptr;

	char *szSetting = mir_u2a(ai->targv[3]);
	if (szSetting == nullptr) {
		mir_free(szModule);
		return nullptr;
	}

	wchar_t *szDefaultValue = ((ai->argc > 4 && mir_wstrlen(ai->targv[4]) > 0) ? mir_wstrdup(ai->targv[4]) : nullptr);
	wchar_t *res = getDBSetting(hContact, szModule, szSetting, szDefaultValue);
	mir_free(szDefaultValue);
	mir_free(szSetting);
	mir_free(szModule);
	return res;
}

static wchar_t* parseLastSeenDate(ARGUMENTSINFO *ai)
{
	if (ai->argc <= 1)
		return nullptr;

	MCONTACT hContact = getContactFromString(ai->targv[1], CI_ALLFLAGS);
	if (hContact == INVALID_CONTACT_ID)
		return nullptr;

	wchar_t *szFormat;
	if (ai->argc == 2 || (ai->argc > 2 && mir_wstrlen(ai->targv[2]) == 0))
		szFormat = nullptr;
	else
		szFormat = ai->targv[2];

	SYSTEMTIME lsTime = { 0 };
	char *szModule = SEEN_MODULE;
	lsTime.wYear = db_get_w(hContact, szModule, "Year", 0);
	if (lsTime.wYear == 0)
		return nullptr;

	lsTime.wMilliseconds = 0;
	lsTime.wSecond = db_get_w(hContact, szModule, "Seconds", 0);
	lsTime.wMinute = db_get_w(hContact, szModule, "Minutes", 0);
	lsTime.wHour = db_get_w(hContact, szModule, "Hours", 0);
	lsTime.wDay = db_get_w(hContact, szModule, "Day", 0);
	lsTime.wDayOfWeek = db_get_w(hContact, szModule, "WeekDay", 0);
	lsTime.wMonth = db_get_w(hContact, szModule, "Month", 0);

	int len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, nullptr, 0);
	wchar_t *res = (wchar_t*)mir_alloc((len + 1)*sizeof(wchar_t));
	if (res == nullptr)
		return nullptr;

	if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, res, len) == 0) {
		mir_free(res);
		return nullptr;
	}

	return res;
}

static wchar_t* parseLastSeenTime(ARGUMENTSINFO *ai)
{
	if (ai->argc <= 1)
		return nullptr;

	MCONTACT hContact = getContactFromString(ai->targv[1], CI_ALLFLAGS);
	if (hContact == INVALID_CONTACT_ID)
		return nullptr;

	wchar_t *szFormat;
	if (ai->argc == 2 || (ai->argc > 2 && mir_wstrlen(ai->targv[2]) == 0))
		szFormat = nullptr;
	else
		szFormat = ai->targv[2];

	SYSTEMTIME lsTime = { 0 };
	char *szModule = SEEN_MODULE;
	lsTime.wYear = db_get_w(hContact, szModule, "Year", 0);
	if (lsTime.wYear == 0)
		return nullptr;

	lsTime.wMilliseconds = 0;
	lsTime.wSecond = db_get_w(hContact, szModule, "Seconds", 0);
	lsTime.wMinute = db_get_w(hContact, szModule, "Minutes", 0);
	lsTime.wHour = db_get_w(hContact, szModule, "Hours", 0);
	lsTime.wDay = db_get_w(hContact, szModule, "Day", 0);
	lsTime.wDayOfWeek = db_get_w(hContact, szModule, "WeekDay", 0);
	lsTime.wMonth = db_get_w(hContact, szModule, "Month", 0);
	lsTime.wYear = db_get_w(hContact, szModule, "Year", 0);

	int len = GetTimeFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, nullptr, 0);
	wchar_t *res = (wchar_t*)mir_alloc((len + 1)*sizeof(wchar_t));
	if (res == nullptr)
		return nullptr;

	if (GetTimeFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, res, len) == 0) {
		mir_free(res);
		return nullptr;
	}

	return res;
}

static wchar_t* parseLastSeenStatus(ARGUMENTSINFO *ai)
{
	if (ai->argc != 2)
		return nullptr;

	MCONTACT hContact = getContactFromString(ai->targv[1], CI_ALLFLAGS);
	if (hContact == INVALID_CONTACT_ID)
		return nullptr;

	char *szModule = SEEN_MODULE;
	int status = db_get_w(hContact, szModule, "Status", 0);
	if (status == 0)
		return nullptr;

	wchar_t *szStatus = pcli->pfnGetStatusModeDescription(status, 0);
	if (szStatus != nullptr)
		return mir_wstrdup(szStatus);

	return nullptr;
}

static wchar_t* parseMirandaPath(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1)
		return nullptr;

	ai->flags |= AIF_DONTPARSE;
	wchar_t path[MAX_PATH];
	if (GetModuleFileName(nullptr, path, _countof(path)) == 0)
		return nullptr;

	return mir_wstrdup(path);
}

static wchar_t* parseMyStatus(ARGUMENTSINFO *ai)
{
	if (ai->argc > 2)
		return nullptr;

	int status;
	if (ai->argc == 1 || mir_wstrlen(ai->targv[1]) == 0)
		status = CallService(MS_CLIST_GETSTATUSMODE, 0, 0);
	else
		status = CallProtoService(_T2A(ai->targv[1]), PS_GETSTATUS, 0, 0);

	wchar_t *szStatus = pcli->pfnGetStatusModeDescription(status, 0);
	return (szStatus != nullptr) ? mir_wstrdup(szStatus) : nullptr;
}

static wchar_t* parseProtoInfo(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return nullptr;

	const char *szRes = nullptr;
	wchar_t *tszRes = nullptr;
	ptrA szProto(mir_u2a(ai->targv[1]));

	if (!mir_wstrcmp(ai->targv[2], _A2W(STR_PINAME)))
		tszRes = Hlp_GetProtocolName(szProto);
	else if (!mir_wstrcmp(ai->targv[2], _A2W(STR_PIUIDTEXT))) {
		szRes = (const char *)CallProtoService(szProto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDTEXT, 0);
		if (INT_PTR(szRes) == CALLSERVICE_NOTFOUND || szRes == nullptr)
			return nullptr;
	}
	else if (!mir_wstrcmp(ai->targv[2], _A2W(STR_PIUIDSETTING))) {
		szRes = Proto_GetUniqueId(szProto);
		if (szRes == nullptr)
			return nullptr;
	}
	else if (!mir_wstrcmp(ai->targv[2], _A2W(STR_PINICK)))
		tszRes = Contact_GetInfo(CNF_DISPLAY, NULL, szProto);

	if (szRes == nullptr && tszRes == nullptr)
		return nullptr;

	if (szRes != nullptr && tszRes == nullptr)
		tszRes = mir_a2u(szRes);

	return tszRes;
}

static wchar_t* parseSpecialContact(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1 || ai->fi->hContact == NULL)
		return nullptr;

	ai->flags |= AIF_DONTPARSE;
	ptrW szUniqueID;
	char *szProto = GetContactProto(ai->fi->hContact);
	if (szProto != nullptr)
		szUniqueID = getContactInfoT(CNF_UNIQUEID, ai->fi->hContact);

	if (szUniqueID == NULL) {
		szProto = PROTOID_HANDLE;
		szUniqueID = (wchar_t*)mir_alloc(40);
		if (szUniqueID == NULL)
			return nullptr;
		mir_snwprintf(szUniqueID, 20, L"%p", ai->fi->hContact);
	}

	if (szUniqueID == NULL)
		return nullptr;

	return CMStringW(FORMAT, L"<%S:%s>", szProto, szUniqueID).Detach();
}

static BOOL isValidDbEvent(DBEVENTINFO *dbe, int flags)
{
	BOOL bEventType, bEventFlags;

	bEventType = ((dbe->eventType == EVENTTYPE_MESSAGE) && (flags & DBE_MESSAGE)) ||
		((dbe->eventType == EVENTTYPE_URL) && (flags & DBE_URL)) ||
		((dbe->eventType == EVENTTYPE_CONTACTS) && (flags & DBE_CONTACTS)) ||
		((dbe->eventType == EVENTTYPE_ADDED) && (flags & DBE_ADDED)) ||
		((dbe->eventType == EVENTTYPE_AUTHREQUEST) && (flags & DBE_AUTHREQUEST)) ||
		((dbe->eventType == EVENTTYPE_FILE) && (flags & DBE_FILE)) ||
		((flags & DBE_OTHER));
	bEventFlags = (dbe->flags&DBEF_SENT) ? (flags&DBE_SENT) : (flags&DBE_RCVD);
	bEventFlags = (bEventFlags && ((dbe->flags&DBEF_READ) ? (flags&DBE_READ) : (flags&DBE_UNREAD)));

	return (bEventType && bEventFlags);
}

static MEVENT findDbEvent(MCONTACT hContact, MEVENT hDbEvent, int flags)
{
	BOOL bEventOk;

	do {
		if (hContact != NULL) {
			if ((flags & DBE_FIRST) && (flags & DBE_UNREAD)) {
				hDbEvent = db_event_firstUnread(hContact);
				if (hDbEvent == NULL && (flags & DBE_READ))
					hDbEvent = db_event_first(hContact);
			}
			else if (flags & DBE_FIRST)
				hDbEvent = db_event_first(hContact);
			else if (flags & DBE_LAST)
				hDbEvent = db_event_last(hContact);
			else if (flags & DBE_NEXT)
				hDbEvent = db_event_next(hContact, hDbEvent);
			else if (flags & DBE_PREV)
				hDbEvent = db_event_prev(hContact, hDbEvent);
		}
		else {
			MEVENT hMatchEvent = NULL, hSearchEvent = NULL;
			DWORD matchTimestamp = 0, priorTimestamp = 0;

			DBEVENTINFO dbe = {};
			if (flags & DBE_FIRST) {
				for (auto &hSearchContact : Contacts()) {
					hSearchEvent = findDbEvent(hSearchContact, NULL, flags);
					dbe.cbBlob = 0;
					if (!db_event_get(hSearchEvent, &dbe)) {
						if ((dbe.timestamp < matchTimestamp) || (matchTimestamp == 0)) {
							hMatchEvent = hSearchEvent;
							matchTimestamp = dbe.timestamp;
						}
					}
				}
				hDbEvent = hMatchEvent;
			}
			else if (flags & DBE_LAST) {
				for (auto &hSearchContact : Contacts()) {
					hSearchEvent = findDbEvent(hSearchContact, NULL, flags);
					dbe.cbBlob = 0;
					if (!db_event_get(hSearchEvent, &dbe)) {
						if ((dbe.timestamp > matchTimestamp) || (matchTimestamp == 0)) {
							hMatchEvent = hSearchEvent;
							matchTimestamp = dbe.timestamp;
						}
					}
				}
				hDbEvent = hMatchEvent;
			}
			else if (flags & DBE_NEXT) {
				dbe.cbBlob = 0;
				if (!db_event_get(hDbEvent, &dbe)) {
					priorTimestamp = dbe.timestamp;
					for (auto &hSearchContact : Contacts()) {
						hSearchEvent = findDbEvent(hSearchContact, hDbEvent, flags);
						dbe.cbBlob = 0;
						if (!db_event_get(hSearchEvent, &dbe)) {
							if (((dbe.timestamp < matchTimestamp) || (matchTimestamp == 0)) && (dbe.timestamp > priorTimestamp)) {
								hMatchEvent = hSearchEvent;
								matchTimestamp = dbe.timestamp;
							}
						}
					}
					hDbEvent = hMatchEvent;
				}
			}
			else if (flags & DBE_PREV) {
				if (!db_event_get(hDbEvent, &dbe)) {
					priorTimestamp = dbe.timestamp;
					for (auto &hSearchContact : Contacts()) {
						hSearchEvent = findDbEvent(hSearchContact, hDbEvent, flags);
						dbe.cbBlob = 0;
						if (!db_event_get(hSearchEvent, &dbe)) {
							if (((dbe.timestamp > matchTimestamp) || (matchTimestamp == 0)) && (dbe.timestamp < priorTimestamp)) {
								hMatchEvent = hSearchEvent;
								matchTimestamp = dbe.timestamp;
							}
						}
					}
					hDbEvent = hMatchEvent;
				}
			}
		}

		DBEVENTINFO dbe = {};
		if (db_event_get(hDbEvent, &dbe))
			bEventOk = FALSE;
		else
			bEventOk = isValidDbEvent(&dbe, flags);
		if (!bEventOk) {
			if (flags&DBE_FIRST) {
				flags |= DBE_NEXT;
				flags &= ~DBE_FIRST;
			}
			else if (flags&DBE_LAST) {
				flags |= DBE_PREV;
				flags &= ~DBE_LAST;
			}
		}
	}
	while ((!bEventOk) && (hDbEvent != NULL));

	return hDbEvent;
}

// ?message(%subject%,last|first,sent|recv,read|unread)
static wchar_t* parseDbEvent(ARGUMENTSINFO *ai)
{
	if (ai->argc != 5)
		return nullptr;

	int flags = DBE_MESSAGE;
	switch (*ai->targv[2]) {
	case 'f':
		flags |= DBE_FIRST;
		break;
	default:
		flags |= DBE_LAST;
		break;
	}
	switch (*ai->targv[3]) {
	case 's':
		flags |= DBE_SENT;
		break;
	case 'r':
		flags |= DBE_RCVD;
		break;
	default:
		flags |= DBE_RCVD | DBE_SENT;
		break;
	}
	switch (*ai->targv[4]) {
	case 'r':
		flags |= DBE_READ;
		break;
	case 'u':
		flags |= DBE_UNREAD;
		break;
	default:
		flags |= DBE_READ | DBE_UNREAD;
		break;
	}

	MCONTACT hContact = getContactFromString(ai->targv[1], CI_ALLFLAGS);
	if (hContact == INVALID_CONTACT_ID)
		return nullptr;

	MEVENT hDbEvent = findDbEvent(hContact, NULL, flags);
	if (hDbEvent == NULL)
		return nullptr;

	DBEVENTINFO dbe = {};
	dbe.cbBlob = db_event_getBlobSize(hDbEvent);
	dbe.pBlob = (PBYTE)mir_calloc(dbe.cbBlob);
	if (db_event_get(hDbEvent, &dbe)) {
		mir_free(dbe.pBlob);
		return nullptr;
	}

	wchar_t *res = DbEvent_GetTextW(&dbe, CP_ACP);
	mir_free(dbe.pBlob);
	return res;
}

static wchar_t* parseTranslate(ARGUMENTSINFO *ai)
{
	if (ai->argc != 2)
		return nullptr;

	wchar_t *res = TranslateW(ai->targv[1]);
	return (res == nullptr) ? nullptr : mir_wstrdup(res);
}

static wchar_t* parseVersionString(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1)
		return nullptr;

	ai->flags |= AIF_DONTPARSE;
	char versionString[128];
	Miranda_GetVersionText(versionString, sizeof(versionString));
	return mir_a2u(versionString);
}

static wchar_t *parseContactNameString(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1 || ai->fi->hContact == NULL)
		return nullptr;

	ai->flags |= AIF_DONTPARSE;
	wchar_t *ret = (wchar_t*)pcli->pfnGetContactDisplayName(ai->fi->hContact, 0);
	return (ret == nullptr) ? nullptr : mir_wstrdup(ret);
}

static wchar_t *parseMirDateString(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1)
		return nullptr;

	ai->flags |= AIF_DONTPARSE;

	wchar_t ret[128];
	return mir_wstrdup(TimeZone_ToStringT(time(nullptr), L"d s", ret, _countof(ret)));
}

static wchar_t *parseMirandaCoreVar(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1)
		return nullptr;

	ai->flags |= AIF_DONTPARSE;

	wchar_t corevar[MAX_PATH];
	mir_snwprintf(corevar, L"%%%s%%", ai->targv[0]);
	return Utils_ReplaceVarsW(corevar);
}

static wchar_t *parseMirSrvExists(ARGUMENTSINFO *ai)
{
	if (ai->argc != 2)
		return nullptr;

	if (!ServiceExists(_T2A(ai->targv[1])))
		ai->flags |= AIF_FALSE;

	return mir_wstrdup(L"");
}

void registerMirandaTokens()
{
	// global vars
	registerIntToken(L"miranda_path", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core Global") "\t" LPGEN("path to Miranda root folder"));
	registerIntToken(L"miranda_profilesdir", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core Global") "\t" LPGEN("path to folder containing Miranda profiles"));
	registerIntToken(L"miranda_profilename", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core Global") "\t" LPGEN("name of current Miranda profile (filename, without extension)"));
	registerIntToken(L"miranda_userdata", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core Global") "\t" LPGEN("will return parsed string %miranda_profilesdir%\\%miranda_profilename%"));
	registerIntToken(L"miranda_avatarcache", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core Global") "\t" LPGEN("will return parsed string %miranda_profilesdir%\\%miranda_profilename%\\AvatarCache"));
	registerIntToken(L"miranda_logpath", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core Global") "\t" LPGEN("will return parsed string %miranda_profilesdir%\\%miranda_profilename%\\Logs"));

	// OS vars
	registerIntToken(L"appdata", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core OS") "\t" LPGEN("same as environment variable %APPDATA% for currently logged-on Windows user"));
	registerIntToken(L"username", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core OS") "\t" LPGEN("username for currently logged-on Windows user"));
	registerIntToken(L"mydocuments", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core OS") "\t" LPGEN("\"My Documents\" folder for currently logged-on Windows user"));
	registerIntToken(L"desktop", parseMirandaCoreVar, TRF_FIELD, LPGEN("Miranda Core OS") "\t" LPGEN("\"Desktop\" folder for currently logged-on Windows user"));

	registerIntToken(CODETOSTATUS, parseCodeToStatus, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x)\t" LPGEN("translates status code x into a status description"));
	registerIntToken(CONTACT, parseContact, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y,z)\t" LPGEN("zth contact with property y described by x, example: (unregistered,nick) (z is optional)"));
	registerIntToken(CONTACTCOUNT, parseContactCount, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y)\t" LPGEN("number of contacts with property y described by x, example: (unregistered,nick)"));
	registerIntToken(MIR_CONTACTINFO, parseContactInfo, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y)\t" LPGEN("info property y of contact x"));
	registerIntToken(DBPROFILENAME, parseDBProfileName, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("database profile name"));
	registerIntToken(DBPROFILEPATH, parseDBProfilePath, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("database profile path"));
	registerIntToken(DBSETTING, parseDBSetting, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y,z,w)\t" LPGEN("database setting z of module y of contact x and return w if z doesn't exist (w is optional)"));
	registerIntToken(DBEVENT, parseDbEvent, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y,z,w)\t" LPGEN("get event for contact x (optional), according to y,z,w, see documentation"));
	registerIntToken(LSTIME, parseLastSeenTime, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y)\t" LPGEN("get last seen time of contact x in format y (y is optional)"));
	registerIntToken(LSDATE, parseLastSeenDate, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y)\t" LPGEN("get last seen date of contact x in format y (y is optional)"));
	registerIntToken(LSSTATUS, parseLastSeenStatus, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x)\t" LPGEN("get last seen status of contact x"));
	registerIntToken(MIRANDAPATH, parseMirandaPath, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("path to the Miranda NG executable"));
	registerIntToken(MYSTATUS, parseMyStatus, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x)\t" LPGEN("current status description of protocol x (without x, the global status is retrieved)"));
	registerIntToken(PROTOINFO, parseProtoInfo, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x,y)\t" LPGEN("info property y of protocol ID x"));
	registerIntToken(SUBJECT, parseSpecialContact, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("retrieves the subject, depending on situation"));
	registerIntToken(TRANSLATE, parseTranslate, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x)\t" LPGEN("translates x"));
	registerIntToken(VERSIONSTRING, parseVersionString, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("get the version of Miranda"));
	registerIntToken(CONTACT_NAME, parseContactNameString, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("get the contact display name"));
	registerIntToken(MIR_DATE, parseMirDateString, TRF_FIELD, LPGEN("Miranda Related") "\t" LPGEN("get the date and time (using Miranda format)"));
	registerIntToken(SRVEXISTS, parseMirSrvExists, TRF_FUNCTION, LPGEN("Miranda Related") "\t(x)\t" LPGEN("TRUE if service function exists"));
}