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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
|
/*
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 "variables.h"
#include "parse_miranda.h"
#include "contact.h"
static TCHAR *parseCodeToStatus(ARGUMENTSINFO *ai)
{
TCHAR *szStatus;
unsigned int status;
if (ai->argc != 2)
return NULL;
status = ttoi(ai->targv[1]);
szStatus = (TCHAR *)CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, (WPARAM)status, GSMDF_TCHAR);
if (szStatus != NULL)
return _tcsdup(szStatus);
return NULL;
}
static int getContactInfoFlags(TCHAR *tszDesc)
{
int flags;
TCHAR *cur;
flags = 0;
for (cur=tszDesc;(cur < (tszDesc+_tcslen(tszDesc)));cur++) {
if (!_tcsnicmp(cur, _T(STR_PROTOID), _tcslen(_T(STR_PROTOID)))) {
flags|=CI_PROTOID;
cur += _tcslen(_T(STR_PROTOID)) - 1;
}
else if (!_tcsnicmp(cur, _T(STR_NICK), _tcslen(_T(STR_NICK)))) {
flags|=CI_NICK;
cur += _tcslen(_T(STR_NICK)) - 1;
}
else if (!_tcsnicmp(cur, _T(STR_FIRSTNAME), _tcslen(_T(STR_FIRSTNAME)))) {
flags|=CI_FIRSTNAME;
cur += _tcslen(_T(STR_FIRSTNAME)) - 1;
}
else if (!_tcsnicmp(cur, _T(STR_LASTNAME), _tcslen(_T(STR_LASTNAME)))) {
flags|=CI_LASTNAME;
cur += _tcslen(_T(STR_LASTNAME)) - 1;
}
else if (!_tcsnicmp(cur, _T(STR_DISPLAY), _tcslen(_T(STR_DISPLAY)))) {
flags|=CI_LISTNAME;
cur += _tcslen(_T(STR_DISPLAY)) - 1;
}
else if (!_tcsnicmp(cur, _T(STR_EMAIL), _tcslen(_T(STR_EMAIL)))) {
flags|=CI_EMAIL;
cur += _tcslen(_T(STR_EMAIL)) - 1;
}
else if (!_tcsnicmp(cur, _T(STR_UNIQUEID), _tcslen(_T(STR_UNIQUEID)))) {
flags|=CI_UNIQUEID;
cur += _tcslen(_T(STR_UNIQUEID)) - 1;
}
}
if (flags == 0) {
flags = getContactInfoType(tszDesc);
if (flags != 0)
flags |= CI_CNFINFO;
}
flags |= CI_TCHAR;
return flags;
}
static TCHAR *parseContact(ARGUMENTSINFO *ai)
{
CONTACTSINFO ci;
int count, n;
HANDLE hContact;
if ( ai->argc < 3 || ai->argc > 4 )
return NULL;
n = 0;
if ( ai->argc == 4 && *ai->targv[3] != _T('r'))
n = ttoi(ai->targv[3]) - 1;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = getContactInfoFlags(ai->targv[2]);
count = getContactFromString( &ci );
if ( count == 0 || ci.hContacts == NULL )
return NULL;
if ( ai->argc == 4 && *ai->targv[3] == _T('r'))
n = rand() % count;
if ( count != 1 && ai->argc != 4 ) {
if ( ci.hContacts != NULL )
free(ci.hContacts);
return NULL;
}
hContact = ci.hContacts[n];
log_debugA("contact: %x", hContact);
free(ci.hContacts);
return encodeContactToString(hContact);
}
static TCHAR *parseContactCount(ARGUMENTSINFO *ai)
{
CONTACTSINFO ci;
int count;
if (ai->argc != 3)
return NULL;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = getContactInfoFlags(ai->targv[2]);
count = getContactFromString( &ci );
if ( count != 0 && ci.hContacts != NULL )
free(ci.hContacts);
return itot(count);
}
static TCHAR *parseContactInfo(ARGUMENTSINFO *ai)
{
HANDLE hContact;
CONTACTSINFO ci;
int count;
BYTE type;
if (ai->argc != 3)
return NULL;
hContact = NULL;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = 0xFFFFFFFF ^ (CI_TCHAR == 0 ? CI_UNICODE : 0);
count = getContactFromString( &ci );
if ( count == 1 && ci.hContacts != NULL ) {
hContact = ci.hContacts[0];
free(ci.hContacts);
}
else {
if (ci.hContacts != NULL)
free(ci.hContacts);
return NULL;
}
type = getContactInfoType(ai->targv[2]);
if (type == 0)
return NULL;
return getContactInfoT(type, hContact, 1);
}
static TCHAR *parseDBProfileName(ARGUMENTSINFO *ai)
{
char name[MAX_PATH];
if (ai->argc != 1)
return NULL;
if (CallService(MS_DB_GETPROFILENAME, SIZEOF(name), (LPARAM)name))
return NULL;
#ifdef UNICODE
return a2u(name);
#else
return _strdup(name);
#endif
}
static TCHAR *parseDBProfilePath(ARGUMENTSINFO *ai) {
char path[MAX_PATH];
if (ai->argc != 1)
return NULL;
if (CallService(MS_DB_GETPROFILEPATH, SIZEOF(path), (LPARAM)path))
return NULL;
#ifdef UNICODE
return a2u(path);
#else
return _strdup(path);
#endif
}
static TCHAR* getDBSetting(HANDLE hContact, char* module, char* setting, TCHAR* defaultValue) {
DBVARIANT dbv;
TCHAR* var = NULL;
if (DBGetContactSettingW(hContact, module, setting, &dbv))
return defaultValue;
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:
#ifdef UNICODE
var = a2u(dbv.pszVal);
#else
var = _strdup(dbv.pszVal);
#endif
break;
case DBVT_WCHAR:
#ifdef UNICODE
var = _wcsdup(dbv.pwszVal);
#else
var = u2a(dbv.pwszVal);
#endif
break;
case DBVT_UTF8:
#ifdef UNICODE
Utf8Decode(dbv.pszVal, &var);
#else
var = _strdup(dbv.pszVal);
Utf8Decode(var, NULL);
#endif
break;
}
DBFreeVariant(&dbv);
return var;
}
static TCHAR *parseDBSetting(ARGUMENTSINFO *ai)
{
CONTACTSINFO ci;
HANDLE hContact;
int count;
char *szModule, *szSetting;
TCHAR *res, *szDefaultValue;
if (ai->argc < 4)
return NULL;
res = NULL;
hContact = NULL;
szDefaultValue = NULL;
if (_tcslen(ai->targv[1]) > 0) {
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = 0xFFFFFFFF^(CI_TCHAR==0?CI_UNICODE:0);
count = getContactFromString( &ci );
if ( count == 1 && ci.hContacts != NULL ) {
hContact = ci.hContacts[0];
free(ci.hContacts);
}
else {
if (ci.hContacts != NULL)
free(ci.hContacts);
return NULL;
}
}
#ifdef UNICODE
szModule = u2a(ai->targv[2]);
szSetting = u2a(ai->targv[3]);
#else
szModule = _strdup(ai->argv[2]);
szSetting = _strdup(ai->argv[3]);
#endif
if ( ai->argc > 4 && _tcslen(ai->targv[4]) > 0 )
szDefaultValue = _tcsdup(ai->targv[4]);
if ( szModule != NULL && szSetting != NULL ) {
res = getDBSetting(hContact, szModule, szSetting, szDefaultValue);
free(szModule);
free(szSetting);
}
return res;
}
static TCHAR *parseLastSeenDate(ARGUMENTSINFO *ai)
{
HANDLE hContact;
CONTACTSINFO ci;
int count, len;
SYSTEMTIME lsTime;
TCHAR *szFormat, *res;
char *szModule;
if (ai->argc <= 1)
return NULL;
hContact = NULL;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = 0xFFFFFFFF^(CI_TCHAR==0?CI_UNICODE:0);
count = getContactFromString( &ci );
if ( count == 1 && ci.hContacts != NULL ) {
hContact = ci.hContacts[0];
free(ci.hContacts);
}
else {
if (ci.hContacts != NULL)
free(ci.hContacts);
return NULL;
}
if ( ai->argc == 2 || (ai->argc > 2 && _tcslen(ai->targv[2]) == 0))
szFormat = NULL;
else
szFormat = ai->targv[2];
ZeroMemory(&lsTime, sizeof(lsTime));
szModule = CEX_MODULE;
lsTime.wYear = DBGetContactSettingWord(hContact, szModule, "Year", 0);
if (lsTime.wYear == 0)
szModule = SEEN_MODULE;
lsTime.wYear = DBGetContactSettingWord(hContact, szModule, "Year", 0);
if (lsTime.wYear == 0)
return NULL;
lsTime.wMilliseconds = 0;
lsTime.wSecond = DBGetContactSettingWord(hContact, szModule, "Seconds", 0);
lsTime.wMinute = DBGetContactSettingWord(hContact, szModule, "Minutes", 0);
lsTime.wHour = DBGetContactSettingWord(hContact, szModule, "Hours", 0);
lsTime.wDay = DBGetContactSettingWord(hContact, szModule, "Day", 0);
lsTime.wDayOfWeek = DBGetContactSettingWord(hContact, szModule, "WeekDay", 0);
lsTime.wMonth = DBGetContactSettingWord(hContact, szModule, "Month", 0);
len = GetDateFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, NULL, 0);
res = ( TCHAR* )malloc((len+1)*sizeof(TCHAR));
if (res == NULL)
return NULL;
if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, res, len) == 0) {
free(res);
return NULL;
}
return res;
}
static TCHAR *parseLastSeenTime(ARGUMENTSINFO *ai)
{
HANDLE hContact;
CONTACTSINFO ci;
int count, len;
SYSTEMTIME lsTime;
TCHAR *szFormat, *res;
char *szModule;
if (ai->argc <= 1)
return NULL;
hContact = NULL;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = 0xFFFFFFFF^(CI_TCHAR==0?CI_UNICODE:0);
count = getContactFromString( &ci );
if ( count == 1 && ci.hContacts != NULL ) {
hContact = ci.hContacts[0];
free(ci.hContacts);
}
else {
if (ci.hContacts != NULL)
free(ci.hContacts);
return NULL;
}
if ( ai->argc == 2 || (ai->argc > 2 && _tcslen(ai->targv[2]) == 0))
szFormat = NULL;
else
szFormat = ai->targv[2];
ZeroMemory(&lsTime, sizeof(lsTime));
szModule = CEX_MODULE;
lsTime.wYear = DBGetContactSettingWord(hContact, szModule, "Year", 0);
if (lsTime.wYear == 0)
szModule = SEEN_MODULE;
lsTime.wYear = DBGetContactSettingWord(hContact, szModule, "Year", 0);
if (lsTime.wYear == 0)
return NULL;
lsTime.wMilliseconds = 0;
lsTime.wSecond = DBGetContactSettingWord(hContact, szModule, "Seconds", 0);
lsTime.wMinute = DBGetContactSettingWord(hContact, szModule, "Minutes", 0);
lsTime.wHour = DBGetContactSettingWord(hContact, szModule, "Hours", 0);
lsTime.wDay = DBGetContactSettingWord(hContact, szModule, "Day", 0);
lsTime.wDayOfWeek = DBGetContactSettingWord(hContact, szModule, "WeekDay", 0);
lsTime.wMonth = DBGetContactSettingWord(hContact, szModule, "Month", 0);
lsTime.wYear = DBGetContactSettingWord(hContact, szModule, "Year", 0);
len = GetTimeFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, NULL, 0);
res = ( TCHAR* )malloc((len+1)*sizeof(TCHAR));
if (res == NULL)
return NULL;
if (GetTimeFormat(LOCALE_USER_DEFAULT, 0, &lsTime, szFormat, res, len) == 0) {
free(res);
return NULL;
}
return res;
}
static TCHAR *parseLastSeenStatus(ARGUMENTSINFO *ai) {
HANDLE hContact;
CONTACTSINFO ci;
int count, status;
char *szModule;
TCHAR *szStatus;
if (ai->argc != 2)
return NULL;
hContact = NULL;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = 0xFFFFFFFF^(CI_TCHAR==0?CI_UNICODE:0);
count = getContactFromString( &ci );
if ( (count == 1) && (ci.hContacts != NULL) ) {
hContact = ci.hContacts[0];
free(ci.hContacts);
}
else {
if (ci.hContacts != NULL)
free(ci.hContacts);
return NULL;
}
szModule = CEX_MODULE;
status = DBGetContactSettingWord(hContact, szModule, "Status", 0);
if (status == 0)
szModule = SEEN_MODULE;
status = DBGetContactSettingWord(hContact, szModule, "Status", 0);
if (status == 0)
return NULL;
szStatus = (TCHAR *)CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, (WPARAM)status, GSMDF_UNICODE);
if (szStatus != NULL)
return _tcsdup(szStatus);
return NULL;
}
static TCHAR *parseMirandaPath(ARGUMENTSINFO *ai) {
TCHAR path[MAX_PATH];
if (ai->argc != 1)
return NULL;
ai->flags |= AIF_DONTPARSE;
if (GetModuleFileName(NULL, path, SIZEOF(path)) == 0)
return NULL;
return _tcsdup(path);
}
static TCHAR *parseMyStatus(ARGUMENTSINFO *ai) {
int status;
char *szProto;
TCHAR *szStatus;
if (ai->argc > 2)
return NULL;
if ( ai->argc == 1 || _tcslen(ai->targv[1]) == 0 )
status = CallService(MS_CLIST_GETSTATUSMODE, 0, 0);
else {
#ifdef UNICODE
szProto = u2a(ai->targv[1]);
#else
szProto = _strdup(ai->targv[1]);
#endif
status = CallProtoService(szProto, PS_GETSTATUS, 0, 0);
free(szProto);
}
szStatus = (TCHAR *)CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, (WPARAM)status, GSMDF_UNICODE);
if (szStatus != NULL)
return _tcsdup(szStatus);
return NULL;
}
static TCHAR *parseProtoInfo(ARGUMENTSINFO *ai)
{
TCHAR *tszRes;
char *szProto, *szRes;
if (ai->argc != 3)
return NULL;
szRes = NULL;
tszRes = NULL;
#ifdef UNICODE
szProto = u2a(ai->targv[1]);
#else
szProto = _strdup(ai->targv[1]);
#endif
if (!_tcscmp(ai->targv[2], _T(STR_PINAME)))
tszRes = Hlp_GetProtocolName(szProto);
else if (!_tcscmp(ai->targv[2], _T(STR_PIUIDTEXT))) {
char *szText;
if (!ProtoServiceExists(szProto, PS_GETCAPS))
return NULL;
szText = (char *)CallProtoService(szProto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDTEXT, 0);
if (szText != NULL)
szRes = _strdup(szText);
}
else if (!_tcscmp(ai->targv[2], _T(STR_PIUIDSETTING))) {
char *szText;
if (!ProtoServiceExists(szProto, PS_GETCAPS))
return NULL;
szText = (char *)CallProtoService(szProto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
if (szText != NULL)
szRes = _strdup(szText);
}
free(szProto);
if ( szRes == NULL && tszRes == NULL )
return NULL;
if ( szRes != NULL && tszRes == NULL ) {
#ifdef UNICODE
tszRes = a2u(szRes);
free(szRes);
#else
tszRes = szRes;
#endif
}
else if ( szRes != NULL && tszRes != NULL )
free(szRes);
return tszRes;
}
static TCHAR *parseSpecialContact(ARGUMENTSINFO *ai)
{
char *szProto;
TCHAR *szUniqueID, *res, *tszProto;
if ( ai->argc != 1 || ai->fi->hContact == NULL )
return NULL;
ai->flags |= AIF_DONTPARSE;
res = NULL;
szUniqueID = NULL;
szProto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ai->fi->hContact, 0);
if (szProto != NULL)
szUniqueID = getContactInfoT(CNF_UNIQUEID, ai->fi->hContact, 1);
if (szUniqueID == NULL) {
szProto = PROTOID_HANDLE;
szUniqueID = itot((int)ai->fi->hContact);
if ( szProto == NULL || szUniqueID == NULL )
return NULL;
}
res = ( TCHAR* )malloc((strlen(szProto) + _tcslen(szUniqueID) + 4)*sizeof(TCHAR));
if (res == NULL) {
free(szUniqueID);
return NULL;
}
#ifdef UNICODE
tszProto = a2u(szProto);
#else
tszProto = _strdup(szProto);
#endif
if ( tszProto != NULL && szUniqueID != NULL ) {
wsprintf(res, _T("<%s:%s>"), tszProto, szUniqueID);
free(szUniqueID);
free(tszProto);
}
return res;
}
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)) ||
((dbe->eventType == EVENTTYPE_STATUSCHANGE) && (flags&DBE_STATUSCHANGE)) ||
((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 HANDLE findDbEvent(HANDLE hContact, HANDLE hDbEvent, int flags)
{
DBEVENTINFO dbe;
BOOL bEventOk;
do {
ZeroMemory(&dbe, sizeof(DBEVENTINFO));
dbe.cbSize = sizeof(DBEVENTINFO);
dbe.cbBlob = 0;
dbe.pBlob = NULL;
if (hContact != NULL) {
if ( (flags & DBE_FIRST) && (flags & DBE_UNREAD) ) {
hDbEvent = (HANDLE)CallService(MS_DB_EVENT_FINDFIRSTUNREAD, (WPARAM)hContact, 0);
if ( hDbEvent == NULL && (flags & DBE_READ) )
hDbEvent = (HANDLE)CallService(MS_DB_EVENT_FINDFIRST, (WPARAM)hContact, 0);
}
else if (flags & DBE_FIRST)
hDbEvent = (HANDLE)CallService(MS_DB_EVENT_FINDFIRST, (WPARAM)hContact, 0);
else if (flags & DBE_LAST)
hDbEvent = (HANDLE)CallService(MS_DB_EVENT_FINDLAST, (WPARAM)hContact, 0);
else if (flags & DBE_NEXT)
hDbEvent = (HANDLE)CallService(MS_DB_EVENT_FINDNEXT, (WPARAM)hDbEvent, 0);
else if (flags & DBE_PREV)
hDbEvent = (HANDLE)CallService(MS_DB_EVENT_FINDPREV, (WPARAM)hDbEvent, 0);
}
else {
HANDLE hMatchEvent, hSearchEvent, hSearchContact;
DWORD matchTimestamp, priorTimestamp;
hMatchEvent = hSearchEvent = hSearchContact = NULL;
matchTimestamp = priorTimestamp = 0;
if (flags & DBE_FIRST) {
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
do {
hSearchEvent = findDbEvent(hSearchContact, NULL, flags);
dbe.cbBlob = 0;
if (!CallService(MS_DB_EVENT_GET, (WPARAM)hSearchEvent, (LPARAM)&dbe)) {
if ((dbe.timestamp < matchTimestamp) || (matchTimestamp == 0)) {
hMatchEvent = hSearchEvent;
matchTimestamp = dbe.timestamp;
}
}
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM) hSearchContact, 0);
} while (hSearchContact);
hDbEvent = hMatchEvent;
}
else if (flags&DBE_LAST) {
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
do {
hSearchEvent = findDbEvent(hSearchContact, NULL, flags);
dbe.cbBlob = 0;
if (!CallService(MS_DB_EVENT_GET, (WPARAM)hSearchEvent, (LPARAM)&dbe)) {
if ((dbe.timestamp > matchTimestamp) || (matchTimestamp == 0)) {
hMatchEvent = hSearchEvent;
matchTimestamp = dbe.timestamp;
}
}
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM) hSearchContact, 0);
} while (hSearchContact);
hDbEvent = hMatchEvent;
}
else if (flags&DBE_NEXT) {
dbe.cbBlob = 0;
if (!CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&dbe)) {
priorTimestamp = dbe.timestamp;
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
do {
hSearchEvent = findDbEvent(hSearchContact, hDbEvent, flags);
dbe.cbBlob = 0;
if (!CallService(MS_DB_EVENT_GET, (WPARAM)hSearchEvent, (LPARAM)&dbe)) {
if (((dbe.timestamp < matchTimestamp) || (matchTimestamp == 0) ) && (dbe.timestamp > priorTimestamp)) {
hMatchEvent = hSearchEvent;
matchTimestamp = dbe.timestamp;
}
}
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hSearchContact, 0);
} while (hSearchContact);
hDbEvent = hMatchEvent;
}
}
else if (flags&DBE_PREV) {
if (!CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&dbe)) {
priorTimestamp = dbe.timestamp;
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
do {
hSearchEvent = findDbEvent(hSearchContact, hDbEvent, flags);
dbe.cbBlob = 0;
if (!CallService(MS_DB_EVENT_GET, (WPARAM)hSearchEvent, (LPARAM)&dbe)) {
if ( ((dbe.timestamp > matchTimestamp) || (matchTimestamp == 0)) && (dbe.timestamp < priorTimestamp) ) {
hMatchEvent = hSearchEvent;
matchTimestamp = dbe.timestamp;
}
}
hSearchContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hSearchContact, 0);
} while (hSearchContact);
hDbEvent = hMatchEvent;
}
}
}
dbe.cbBlob = 0;
if (CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&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;
}
// modified from history.c
static TCHAR *GetMessageDescription(DBEVENTINFO *dbei)
{
TCHAR *tszRes;
if ( ServiceExists( MS_DB_EVENT_GETTEXT )) {
// Miranda 0.7
TCHAR *buf = DbGetEventTextT(dbei, CP_ACP);
tszRes = _tcsdup(buf);
mir_free(buf);
}
else {
char *pszSrc = ( char* )dbei->pBlob;
size_t len = strlen(( char* )dbei->pBlob )+1;
#if defined( _UNICODE )
if ( dbei->cbBlob > len ) {
int len2 = dbei->cbBlob - len;
tszRes = ( TCHAR* )calloc(len2, 1);
memcpy( tszRes, &dbei->pBlob[ len ], len2 );
}
else {
char *szRes = ( char* )calloc(len, sizeof(char));
strncpy( szRes, ( const char* )pszSrc, len );
tszRes = a2u(szRes);
free(szRes);
}
#else
tszRes = ( char* )calloc(len, sizeof(char));
strncpy( tszRes, ( const char* )pszSrc, len );
#endif
}
return tszRes;
}
// end: from history.c
// ?message(%subject%,last|first,sent|recv,read|unread)
static TCHAR *parseDbEvent(ARGUMENTSINFO *ai)
{
int flags, count;
HANDLE hContact;
HANDLE hDbEvent;
DBEVENTINFO dbe;
CONTACTSINFO ci;
TCHAR *res;
if (ai->argc != 5)
return NULL;
flags = DBE_MESSAGE;
switch (*ai->targv[2]) {
case _T('f'):
flags |= DBE_FIRST;
break;
default:
flags |= DBE_LAST;
break;
}
switch (*ai->targv[3]) {
case _T('s'):
flags |= DBE_SENT;
break;
case _T('r'):
flags |= DBE_RCVD;
break;
default:
flags |= DBE_RCVD|DBE_SENT;
break;
}
switch (*ai->targv[4]) {
case _T('r'):
flags |= DBE_READ;
break;
case _T('u'):
flags |= DBE_UNREAD;
break;
default:
flags |= DBE_READ|DBE_UNREAD;
break;
}
hContact = NULL;
ZeroMemory(&ci, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.tszContact = ai->targv[1];
ci.flags = 0xFFFFFFFF^(CI_TCHAR==0?CI_UNICODE:0);
count = getContactFromString( &ci );
if ( (count == 1) && (ci.hContacts != NULL) ) {
hContact = ci.hContacts[0];
free(ci.hContacts);
}
else if (ci.hContacts != NULL)
free(ci.hContacts);
hDbEvent = findDbEvent(hContact, NULL, flags);
if (hDbEvent == NULL)
return NULL;
ZeroMemory(&dbe, sizeof(DBEVENTINFO));
dbe.cbSize = sizeof(DBEVENTINFO);
dbe.cbBlob = CallService(MS_DB_EVENT_GETBLOBSIZE, (WPARAM)hDbEvent, 0);
dbe.pBlob = ( PBYTE )calloc(dbe.cbBlob, 1);
if (CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&dbe)) {
free(dbe.pBlob);
return NULL;
}
res = GetMessageDescription(&dbe);
free(dbe.pBlob);
return res;
}
static TCHAR *parseTranslate(ARGUMENTSINFO *ai)
{
if (ai->argc != 2)
return NULL;
TCHAR* res = TranslateTS(ai->targv[1]);
return (res == NULL) ? NULL : _tcsdup(res);
}
static TCHAR *parseVersionString(ARGUMENTSINFO *ai) {
char versionString[128];
if (ai->argc != 1)
return NULL;
ai->flags |= AIF_DONTPARSE;
if (CallService(MS_SYSTEM_GETVERSIONTEXT, (WPARAM)sizeof(versionString), (LPARAM)versionString))
return NULL;
#ifdef UNICODE
return a2u(versionString);
#else
return _strdup(versionString);
#endif
}
static TCHAR *parseContactNameString(ARGUMENTSINFO *ai)
{
TCHAR *ret;
if (ai->argc != 1 || ai->fi->hContact == NULL)
return NULL;
ai->flags |= AIF_DONTPARSE;
ret = (TCHAR *) CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM) ai->fi->hContact, GCDNF_TCHAR);
if (ret == NULL)
return NULL;
return _tcsdup(ret);
}
static TCHAR *parseMirDateString(ARGUMENTSINFO *ai)
{
TCHAR ret[128];
DBTIMETOSTRINGT tst = {0};
if (ai->argc != 1)
return NULL;
ai->flags |= AIF_DONTPARSE;
tst.szFormat = _T("d s");
tst.szDest = ret;
tst.cbDest = 128;
if (CallService(MS_DB_TIME_TIMESTAMPTOSTRINGT, (WPARAM) time(NULL), (LPARAM) &tst))
return NULL;
return _tcsdup(ret);
}
static TCHAR *parseMirandaCoreVar(ARGUMENTSINFO *ai) {
TCHAR path[MAX_PATH], corevar[MAX_PATH];
TCHAR* tmpPath;
if (ai->argc != 1)
return NULL;
ai->flags |= AIF_DONTPARSE;
mir_sntprintf(corevar, MAX_PATH,_T("%%%s%%"), ai->targv[0]);
tmpPath = Utils_ReplaceVarsT(corevar);
mir_sntprintf(path, sizeof(path)-1, _T("%s"), tmpPath);
mir_free(tmpPath);
return _tcsdup(path);
}
static TCHAR *parseMirSrvExists(ARGUMENTSINFO *ai)
{
if ( ai->argc != 2 )
return NULL;
char* serviceName;
#ifdef _UNICODE
serviceName = u2a( ai->targv[1] );
#else
serviceName = ai->targv[1];
#endif
if ( !ServiceExists( serviceName ))
ai->flags |= AIF_FALSE;
#ifdef _UNICODE
free( serviceName );
#endif
return _tcsdup(_T(""));
}
int registerMirandaTokens() {
if (ServiceExists(MS_UTILS_REPLACEVARS)) {
// global vars
registerIntToken(_T("miranda_path"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core Global\tpath to root miranda folder");
registerIntToken(_T("miranda_profile"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core Global\tpath to current miranda profile");
registerIntToken(_T("miranda_profilename"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core Global\tname of current miranda profile (filename, without extension)");
registerIntToken(_T("miranda_userdata"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core Global\twill return parsed string %miranda_profile%\\Profiles\\%miranda_profilename%");
registerIntToken(_T("miranda_avatarcache"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core Global\twill return parsed string %miranda_profile%\\Profiles\\%miranda_profilename%\\AvatarCache");
registerIntToken(_T("miranda_logpath"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core Global\twill return parsed string %miranda_profile%\\Profiles\\%miranda_profilename%\\Logs");
// OS vars
registerIntToken(_T("appdata"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core OS\tsame as environment variable %APPDATA% for currently logged-on Windows user");
registerIntToken(_T("username"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core OS\tusername for currently logged-on Windows user");
registerIntToken(_T("mydocuments"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core OS\t\"My Documents\" folder for currently logged-on Windows user");
registerIntToken(_T("desktop"), parseMirandaCoreVar , TRF_FIELD, "Miranda Core OS\t\"Desktop\" folder for currently logged-on Windows user");
}
registerIntToken(_T(CODETOSTATUS), parseCodeToStatus, TRF_FUNCTION, "Miranda Related\t(x)\ttranslates status code x into a status description");
registerIntToken(_T(CONTACT), parseContact, TRF_FUNCTION, "Miranda Related\t(x,y,z)\tzth contact with property y described by x, example: (unregistered,nick) (z is optional)");
registerIntToken(_T(CONTACTCOUNT), parseContactCount, TRF_FUNCTION, "Miranda Related\t(x,y)\tnumber of contacts with property y described by x, example: (unregistered,nick)");
registerIntToken(_T(CONTACTINFO), parseContactInfo, TRF_FUNCTION, "Miranda Related\t(x,y)\tinfo property y of contact x");
registerIntToken(_T(DBPROFILENAME), parseDBProfileName, TRF_FIELD, "Miranda Related\tdb profile name");
registerIntToken(_T(DBPROFILEPATH), parseDBProfilePath, TRF_FIELD, "Miranda Related\tdb profile path");
registerIntToken(_T(DBSETTING), parseDBSetting, TRF_FUNCTION, "Miranda Related\t(x,y,z,w)\tdb setting z of module y of contact x and return w if z isn't exist (w is optional)");
registerIntToken(_T(DBEVENT), parseDbEvent, TRF_FUNCTION, "Miranda Related\t(x,y,z,w)\tget event for contact x (optional), according to y,z,w, see documentation");
registerIntToken(_T(LSTIME), parseLastSeenTime, TRF_FUNCTION, "Miranda Related\t(x,y)\tget last seen time of contact x in format y (y is optional)");
registerIntToken(_T(LSDATE), parseLastSeenDate, TRF_FUNCTION, "Miranda Related\t(x,y)\tget last seen date of contact x in format y (y is optional)");
registerIntToken(_T(LSSTATUS), parseLastSeenStatus, TRF_FUNCTION, "Miranda Related\t(x)\tget last seen status of contact x");
registerIntToken(_T(MIRANDAPATH), parseMirandaPath, TRF_FIELD, "Miranda Related\tpath to the Miranda-IM executable");
registerIntToken(_T(MYSTATUS), parseMyStatus, TRF_FUNCTION, "Miranda Related\t(x)\tcurrent status description of protocol x (without x, the global status is retrieved)");
registerIntToken(_T(PROTOINFO), parseProtoInfo, TRF_FUNCTION, "Miranda Related\t(x,y)\tinfo property y of protocol id x");
registerIntToken(_T(SUBJECT), parseSpecialContact, TRF_FIELD, "Miranda Related\tretrieves the subject, depending on situation");
registerIntToken(_T(TRANSLATE), parseTranslate, TRF_FUNCTION, "Miranda Related\t(x)\ttranslates x");
registerIntToken(_T(VERSIONSTRING), parseVersionString, TRF_FIELD, "Miranda Related\tget the version of Miranda");
registerIntToken(_T(CONTACT_NAME), parseContactNameString, TRF_FIELD, "Miranda Related\tget the contact display name");
registerIntToken(_T(MIR_DATE), parseMirDateString, TRF_FIELD, "Miranda Related\tget the date and time (using Miranda format)");
registerIntToken(_T(SRVEXISTS), parseMirSrvExists, TRF_FUNCTION, "Miranda Related\t(x)\tTRUE if service function is exists");
return 0;
}
|