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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
|
{$Include compilers.inc}
unit mirutils;
interface
uses windows,m_api;
// for miranda services
const
rtInt = 1;
rtWide = 2;
rtAnsi = 3;
rtUTF8 = 4;
// icons
function SetButtonIcon(btn:HWND;name:PAnsiChar):HICON;
function RegisterSingleIcon(resname,ilname,descr,group:PAnsiChar):int;
// others
function ConvertFileName(src:pAnsiChar;hContact:THANDLE=0):pAnsiChar; overload;
function ConvertFileName(src:pWideChar;hContact:THANDLE=0):pWideChar; overload;
function ConvertFileName(src:pAnsiChar;dst:pAnsiChar;hContact:THANDLE=0):pAnsiChar; overload;
function ConvertFileName(src:pWideChar;dst:pWideChar;hContact:THANDLE=0):pWideChar; overload;
procedure ShowPopupW(text:pWideChar;title:pWideChar=nil);
function GetAddonFileName(prefix,altname,path:PAnsiChar;ext:PAnsiChar):PAnsiChar;
function TranslateA2W(sz:PAnsiChar):PWideChar;
function MirandaCP:integer;
function isVarsInstalled:bool;
function ParseVarString(astr:pAnsiChar;aContact:THANDLE=0;extra:pAnsiChar=nil):pAnsiChar; overload;
function ParseVarString(astr:pWideChar;aContact:THANDLE=0;extra:pWideChar=nil):pWideChar; overload;
function ShowVarHelp(dlg:HWND;id:integer=0):integer;
function IsChat(hContact:THANDLE):bool;
procedure SendToChat(hContact:THANDLE;pszText:PWideChar);
function LoadContact(group,setting:PAnsiChar):THANDLE;
function SaveContact(hContact:THANDLE;group,setting:PAnsiChar):integer;
function SetCListSelContact(hContact:THANDLE):THANDLE;
function GetCListSelContact:THANDLE; {$IFDEF DELPHI_10_UP}inline;{$ENDIF}
function GetContactProtoAcc(hContact:THANDLE):PAnsiChar;
function IsMirandaUser(hContact:THANDLE):integer; // >0=Miranda; 0=Not miranda; -1=unknown
procedure ShowContactDialog(hContact:THANDLE;DblClk:boolean=true;anystatus:boolean=true);
function FindContactHandle(proto:pAnsiChar;const dbv:TDBVARIANT;is_chat:boolean=false):THANDLE;
function WndToContact(wnd:hwnd):THANDLE; overload;
function WndToContact:THANDLE; overload;
function GetContactStatus(hContact:THANDLE):integer;
// -2 - deleted account, -1 - disabled account, 0 - hidden
// 1 - metacontact, 2 - submetacontact, positive - active
// proto - ASSIGNED buffer
function IsContactActive(hContact:THANDLE;proto:pAnsiChar=nil):integer;
function CreateGroupW(name:pWideChar;hContact:THANDLE):integer;
function CreateGroup (name:pAnsiChar;hContact:THANDLE):integer;
function MakeGroupMenu(idxfrom:integer=100):HMENU;
function GetNewGroupName(parent:HWND):pWideChar;
const
MAX_REDIRECT_RECURSE = 4;
function SendRequest(url:PAnsiChar;rtype:int;args:pAnsiChar=nil;hNetLib:THANDLE=0):pAnsiChar;
function GetFile(url:PAnsiChar;save_file:PAnsiChar;
hNetLib:THANDLE=0;recurse_count:integer=0):bool; overload;
// next is just wrapper
function GetFile(url:PWideChar;save_file:PWideChar;
hNetLib:THANDLE=0;recurse_count:integer=0):bool; overload;
function GetProxy(hNetLib:THANDLE):PAnsiChar;
function LoadImageURL(url:pAnsiChar;size:integer=0):HBITMAP;
implementation
uses
Messages,
dbsettings,freeimage,
common,io,syswin;
const
clGroup = 'Group';
// Save / Load contact
const
opt_cproto = 'cproto';
opt_cuid = 'cuid';
opt_ischat = 'ischat';
function SetButtonIcon(btn:HWND;name:PAnsiChar):HICON;
begin
result:=CallService(MS_SKIN2_GETICON,0,LPARAM(name));
SendMessage(btn,BM_SETIMAGE,IMAGE_ICON,result);
end;
function ConvertFileName(src:pWideChar;dst:pWideChar;hContact:THANDLE=0):pWideChar; overload;
var
pc:pWideChar;
begin
result:=dst;
dst^:=#0;
if (src<>nil) and (src^<>#0) then
begin
if isVarsInstalled then
begin
pc:=ParseVarString(src,hContact);
src:=pc;
end
else
pc:=nil;
CallService(MS_UTILS_PATHTOABSOLUTEW,wparam(src),lparam(dst));
mFreeMem(pc);
end;
end;
function ConvertFileName(src:pWideChar;hContact:THANDLE=0):pWideChar; overload;
var
buf1:array [0..511] of WideChar;
begin
if (src<>nil) and (src^<>#0) then
StrDupW(result,ConvertFileName(src,buf1,hContact))
else
result:=nil;
end;
function ConvertFileName(src:pAnsiChar;dst:pAnsiChar;hContact:THANDLE=0):pAnsiChar; overload;
var
pc:pAnsiChar;
begin
result:=dst;
dst^:=#0;
if (src<>nil) and (src^<>#0) then
begin
if isVarsInstalled then
begin
pc:=ParseVarString(src,hContact);
src:=pc;
end
else
pc:=nil;
CallService(MS_UTILS_PATHTOABSOLUTE,wparam(src),lparam(dst));
mFreeMem(pc);
end;
end;
function ConvertFileName(src:pAnsiChar;hContact:THANDLE=0):pAnsiChar; overload;
var
buf1:array [0..511] of AnsiChar;
begin
if (src<>nil) and (src^<>#0) then
StrDup(result,ConvertFileName(src,buf1,hContact))
else
result:=nil;
end;
const
MirCP:integer=-1;
function MirandaCP:integer;
begin
if MirCP<0 then
MirCP:=CallService(MS_LANGPACK_GETCODEPAGE,0,0);
result:=MirCP;
end;
function IsChat(hContact:THANDLE):bool;
begin
result:=DBReadByte(hContact,
PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0)),
'ChatRoom',0)=1;
end;
function isVarsInstalled:bool;
begin
result:=ServiceExists(MS_VARS_FORMATSTRING)<>0;
end;
function ParseVarString(astr:pAnsiChar;aContact:THANDLE=0;extra:pAnsiChar=nil):pAnsiChar;
var
tfi:TFORMATINFO;
tmp,pc:pAnsiChar;
dat:TREPLACEVARSDATA;
begin
if ServiceExists(MS_UTILS_REPLACEVARS)<>0 then
begin
FillChar(dat,SizeOf(TREPLACEVARSDATA),0);
dat.cbSize :=SizeOf(TREPLACEVARSDATA);
pc:=pAnsiChar(CallService(MS_UTILS_REPLACEVARS,wparam(astr),lparam(@dat)));
astr:=pc;
end
else
pc:=nil;
if isVarsInstalled then
begin
FillChar(tfi,SizeOf(tfi),0);
with tfi do
begin
cbSize :=SizeOf(TFORMATINFO);
szFormat.a :=astr;
szExtraText.a:=extra;
hContact :=aContact;
end;
tmp:=pointer(CallService(MS_VARS_FORMATSTRING,wparam(@tfi),0));
StrDup(result,tmp);
mir_free(tmp);
end
else
begin
StrDup(result,astr);
end;
mir_free(pc);
end;
function ParseVarString(astr:pWideChar;aContact:THANDLE=0;extra:pWideChar=nil):pWideChar;
var
tfi:TFORMATINFO;
tmp,pc:pWideChar;
dat:TREPLACEVARSDATA;
begin
if ServiceExists(MS_UTILS_REPLACEVARS)<>0 then
begin
FillChar(dat,SizeOf(TREPLACEVARSDATA),0);
dat.cbSize :=SizeOf(TREPLACEVARSDATA);
dat.dwflags:=RVF_UNICODE;
pc:=pWideChar(CallService(MS_UTILS_REPLACEVARS,wparam(astr),lparam(@dat)));
astr:=pc;
end
else
pc:=nil;
if isVarsInstalled then
begin
FillChar(tfi,SizeOf(tfi),0);
with tfi do
begin
cbSize :=SizeOf(TFORMATINFO);
flags :=FIF_UNICODE;
szFormat.w :=astr;
szExtraText.w:=extra;
hContact :=aContact;
end;
tmp:=pointer(CallService(MS_VARS_FORMATSTRING,wparam(@tfi),0));
StrDupW(result,tmp);
mir_free(tmp);
end
else
begin
StrDupW(result,astr);
end;
mir_free(pc); // forced!
// mFreeMem(pc);
end;
function ShowVarHelp(dlg:HWND;id:integer=0):integer;
var
vhi:TVARHELPINFO;
begin
FillChar(vhi,SizeOf(vhi),0);
with vhi do
begin
cbSize:=SizeOf(vhi);
if id=0 then
flags:=VHF_NOINPUTDLG
else
begin
flags :=VHF_FULLDLG or VHF_SETLASTSUBJECT;
hwndCtrl:=GetDlgItem(dlg,id);
end;
end;
result:=CallService(MS_VARS_SHOWHELPEX,dlg,lparam(@vhi));
end;
procedure ShowPopupW(text:pWideChar;title:pWideChar=nil);
var
ppdu:TPOPUPDATAW;
begin
if ServiceExists(MS_POPUP_ADDPOPUPW)=0 then
exit;
FillChar(ppdu,SizeOf(TPOPUPDATAW),0);
if CallService(MS_POPUP_ISSECONDLINESHOWN,0,0)<>0 then
begin
StrCopyW(ppdu.lpwzText,text,MAX_SECONDLINE-1);
if title<>nil then
StrCopyW(ppdu.lpwzContactName,title,MAX_CONTACTNAME-1)
else
ppdu.lpwzContactName[0]:=' ';
end
else
begin
StrCopyW(ppdu.lpwzContactName,text,MAX_CONTACTNAME-1);
ppdu.lpwzText[0]:=' ';
end;
CallService(MS_POPUP_ADDPOPUPW,wparam(@ppdu),APF_NO_HISTORY);
end;
function TranslateA2W(sz:PAnsiChar):PWideChar;
var
tmp:pWideChar;
begin
mGetMem(tmp,(StrLen(sz)+1)*SizeOf(WideChar));
Result:=PWideChar(CallService(MS_LANGPACK_TRANSLATESTRING,LANG_UNICODE,
lParam(FastAnsiToWideBuf(sz,tmp))));
if Result<>tmp then
begin
StrDupW(Result,Result);
mFreeMem(tmp);
end;
end;
function GetContactProtoAcc(hContact:THANDLE):PAnsiChar;
begin
if ServiceExists(MS_PROTO_GETCONTACTBASEACCOUNT)<>0 then
result:=PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEACCOUNT,hContact,0))
else
result:=PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0));
end;
function IsMirandaUser(hContact:THANDLE):integer; // >0=Miranda; 0=Not miranda; -1=unknown
var
sz:PAnsiChar;
begin
sz:=PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0));
sz:=DBReadString(hContact,sz,'MirVer');
if sz<>nil then
begin
result:=int_ptr(StrPos(sz,'Miranda'));
mFreeMem(sz);
end
else
result:=-1;
end;
function SetCListSelContact(hContact:THANDLE):THANDLE;
var
wnd:HWND;
begin
wnd:=CallService(MS_CLUI_GETHWNDTREE,0,0);
result:=hContact;
// hContact:=SendMessage(wnd,CLM_FINDCONTACT ,hContact,0);
SendMessage(wnd,CLM_SELECTITEM ,hContact,0);
// SendMessage(wnd,CLM_ENSUREVISIBLE,hContact,0);
end;
function GetCListSelContact:THANDLE;
begin
result:=SendMessageW(CallService(MS_CLUI_GETHWNDTREE,0,0),CLM_GETSELECTION,0,0);
end;
function LoadContact(group,setting:PAnsiChar):THANDLE;
var
p,proto:pAnsiChar;
section:array [0..63] of AnsiChar;
dbv:TDBVARIANT;
is_chat:boolean;
begin
p:=StrCopyE(section,setting);
StrCopy(p,opt_cproto); proto :=DBReadString(0,group,section);
StrCopy(p,opt_ischat); is_chat:=DBReadByte (0,group,section,0)<>0;
StrCopy(p,opt_cuid );
if is_chat then
dbv.szVal.W:=DBReadUnicode(0,group,section,@dbv)
else
DBReadSetting(0,group,section,@dbv);
result:=FindContactHandle(proto,dbv,is_chat);
mFreeMem(proto);
if not is_chat then
DBFreeVariant(@dbv)
else
mFreeMem(dbv.szVal.W);
end;
function SaveContact(hContact:THANDLE;group,setting:PAnsiChar):integer;
var
p,proto,uid:pAnsiChar;
cws:TDBVARIANT;
section:array [0..63] of AnsiChar;
pw:pWideChar;
is_chat:boolean;
begin
result:=0;
proto:=GetContactProtoAcc(hContact);
if proto<>nil then
begin
p:=StrCopyE(section,setting);
is_chat:=IsChat(hContact);
if is_chat then
begin
pw:=DBReadUnicode(hContact,proto,'ChatRoomID');
StrCopy(p,opt_cuid); DBWriteUnicode(0,group,section,pw);
mFreeMem(pw);
result:=1;
end
else
begin
uid:=pAnsiChar(CallProtoService(proto,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0));
if uid<>pAnsiChar(CALLSERVICE_NOTFOUND) then
begin
if DBReadSetting(hContact,proto,uid,@cws)=0 then
begin
StrCopy(p,opt_cuid); DBWriteSetting(0,group,section,@cws);
DBFreeVariant(@cws);
result:=1;
end;
end;
end;
if result<>0 then
begin
StrCopy(p,opt_cproto); DBWriteString(0,group,section,proto);
StrCopy(p,opt_ischat); DBWriteByte (0,group,section,ord(is_chat));
end;
end;
end;
function WndToContact(wnd:hwnd):THANDLE; overload;
var
hContact:THANDLE;
mwid:TMessageWindowInputData;
mwod:TMessageWindowOutputData;
begin
wnd:=GetParent(wnd); //!!
hContact:=db_find_first();
with mwid do
begin
cbSize:=SizeOf(mwid);
uFlags:=MSG_WINDOW_UFLAG_MSG_BOTH;
end;
mwod.cbSize:=SizeOf(mwod);
while hContact<>0 do
begin
mwid.hContact:=hContact;
if CallService(MS_MSG_GETWINDOWDATA,wparam(@mwid),lparam(@mwod))=0 then
begin
if {((mwod.uState and MSG_WINDOW_STATE_FOCUS)<>0) and} (mwod.hwndWindow=wnd) then
begin
result:=mwid.hContact;
exit;
end
end;
hContact:=db_find_next(hContact);
end;
result:=0;
end;
function WndToContact:THANDLE; overload;
var
wnd:HWND;
begin
wnd:=GetFocus;
if wnd=0 then
wnd:=WaitFocusedWndChild(GetForegroundWindow);
if wnd<>0 then
result:=WndToContact(wnd)
else
result:=0;
if result=0 then
result:=GetCListSelContact;
end;
function GetContactStatus(hContact:THANDLE):integer;
var
szProto:PAnsiChar;
begin
szProto:=PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0));
if szProto=NIL then
result:=ID_STATUS_OFFLINE
else
result:=DBReadWord(hContact,szProto,'Status',ID_STATUS_OFFLINE);
end;
function CheckPath(filename,profilepath,path:PAnsiChar):PAnsiChar;
var
buf:array [0..511] of AnsiChar;
f:THANDLE;
p:PAnsiChar;
begin
result:=nil;
if profilepath<>nil then
StrCopy(buf,profilepath)
else
buf[0]:=#0;
StrCat(buf,filename);
f:=Reset(buf);
if f=THANDLE(INVALID_HANDLE_VALUE) then
begin
if path<>nil then
begin
CallService(MS_UTILS_PATHTOABSOLUTE,wparam(path),lparam(@buf));
p:=StrEnd(buf);
if p^<>'\' then
begin
p^:='\';
inc(p);
p^:=#0;
end;
end
else if profilepath=nil then
exit
else
buf[0]:=#0;
StrCat(buf,filename); //path\prefix+name
f:=Reset(buf);
end;
if f<>THANDLE(INVALID_HANDLE_VALUE) then
begin
CloseHandle(f);
StrDup(result,buf);
end;
end;
function GetAddonFileName(prefix,altname,path:PAnsiChar;ext:PAnsiChar):PAnsiChar;
var
profilepath:array [0..511] of AnsiChar;
altfilename,filename:array [0..127] of AnsiChar;
p:PAnsiChar;
begin
CallService(MS_DB_GETPROFILEPATH,300,lparam(@profilepath));
p:=StrEnd(profilepath);
p^:='\'; inc(p);
p^:=#0;
filename[0]:=#0;
altfilename[0]:=#0;
if prefix<>nil then
begin
StrCopy(filename,prefix);
p:=StrEnd(filename);
CallService(MS_DB_GETPROFILENAME,SizeOf(filename)-integer(p-pAnsiChar(@filename)),lparam(p));
ChangeExt(filename,ext);
result:=CheckPath(filename,profilepath,path);
end
else
result:=nil;
if (result=nil) and (altname<>nil) then
begin
StrCopy(altfilename,altname);
ChangeExt(altfilename,ext);
result:=CheckPath(altfilename,profilepath,path);
end;
if result=nil then
begin
if filename[0]<>#0 then
StrCat(profilepath,filename)
else
StrCat(profilepath,altfilename);
StrDup(result,profilepath);
end;
end;
procedure ShowContactDialog(hContact:THANDLE;DblClk:boolean=true;anystatus:boolean=true);
var
pc:array [0..127] of AnsiChar;
begin
{
CallService(MS_CLIST_CONTACTDOUBLECLICKED,hContact,0);
}
if (hContact<>0) and (CallService(MS_DB_CONTACT_IS,hContact,0)<>0) then
begin
if StrCopy(pc,PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0)))<>nil then
if DblClk or (DBReadByte(hContact,pc,'ChatRoom',0)=1) then // chat room
begin
if not anystatus then
begin
StrCat(pc,PS_GETSTATUS);
anystatus:=(CallService(pc,0,0)<>ID_STATUS_OFFLINE);
end;
if anystatus then
begin
CallService(MS_CLIST_CONTACTDOUBLECLICKED,hContact,0); //??
// if chat exist, open chat
// else create new session
end;
end
else
begin
if ServiceExists(MS_MSG_CONVERS)<>0 then // Convers compat.
CallService(MS_MSG_CONVERS,hContact,0)
else
CallService(MS_MSG_SENDMESSAGE,hContact,0)
end;
end;
end;
procedure SendChatText(pszID:pointer;pszModule:PAnsiChar;pszText:pointer);
var
gcd:TGCDEST;
gce:TGCEVENT;
begin
gcd.pszModule:=pszModule;
gcd.iType :=GC_EVENT_SENDMESSAGE;
gcd.szID.w :=pszID;
FillChar(gce,SizeOf(TGCEVENT),0);
gce.cbSize :=SizeOf(TGCEVENT);
gce.pDest :=@gcd;
gce.bIsMe :=true;
gce.szText.w:=pszText;
gce.dwFlags :=GCEF_ADDTOLOG+GC_UNICODE;
gce.time :=GetCurrentTime;
CallServiceSync(MS_GC_EVENT,0,lparam(@gce));
end;
procedure SendToChat(hContact:THANDLE;pszText:PWideChar);
var
gci:TGC_INFO;
pszModule:PAnsiChar;
i,cnt:integer;
begin
pszModule:=PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0));
cnt:=CallService(MS_GC_GETSESSIONCOUNT,0,lparam(pszModule));
i:=0;
gci.pszModule:=pszModule;
while i<cnt do
begin
gci.iItem:=i;
gci.Flags:=GCI_BYINDEX+GCI_HCONTACT+GCI_ID;
CallService(MS_GC_GETINFO,0,lparam(@gci));
if gci.hContact=hContact then
begin
SendChatText(gci.pszID.w,pszModule,pszText);
break;
end;
inc(i);
end;
end;
function FindContactHandle(proto:pAnsiChar;const dbv:TDBVARIANT;is_chat:boolean=false):THANDLE;
var
uid:pAnsiChar;
ldbv:TDBVARIANT;
hContact:THANDLE;
pw:pWideChar;
begin
result:=0;
uid:=nil;
if not is_chat then
begin
uid:=pAnsiChar(CallProtoService(proto,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0));
if uid=pAnsiChar(CALLSERVICE_NOTFOUND) then exit;
end;
hContact:=db_find_first();
while hContact<>0 do
begin
if is_chat then
begin
if IsChat(hContact) then
begin
pw:=DBReadUnicode(hContact,proto,'ChatRoomID');
if StrCmpW(pw,dbv.szVal.W)=0 then result:=hContact;
mFreeMem(pw);
end
end
else
begin
if DBReadSetting(hContact,proto,uid,@ldbv)=0 then
begin
if dbv._type=ldbv._type then
begin
case dbv._type of
// DBVT_DELETED: ;
DBVT_BYTE : if dbv.bVal=ldbv.bVal then result:=hContact;
DBVT_WORD : if dbv.wVal=ldbv.wVal then result:=hContact;
DBVT_DWORD : if dbv.dVal=ldbv.dVal then result:=hContact;
DBVT_UTF8,
DBVT_ASCIIZ : if StrCmp (dbv.szVal.A,ldbv.szVal.A)=0 then result:=hContact;
DBVT_WCHAR : if StrCmpW(dbv.szVal.W,ldbv.szVal.W)=0 then result:=hContact;
DBVT_BLOB : begin
if dbv.cpbVal = ldbv.cpbVal then
begin
if CompareMem(dbv.pbVal,ldbv.pbVal,dbv.cpbVal) then
result:=hContact;
end;
end;
end;
end;
DBFreeVariant(@ldbv);
end;
end;
// added 2011.04.20
if result<>0 then break;
hContact:=db_find_next(hContact);
end;
end;
function IsContactActive(hContact:THANDLE;proto:pAnsiChar=nil):integer;
var
p:PPROTOACCOUNT;
dbv :TDBVARIANT;
dbcgs:TDBCONTACTGETSETTING;
name: array [0..31] of AnsiChar;
begin
dbv._type :=DBVT_ASCIIZ;
dbv.szVal.a:=@name;
dbv.cchVal :=SizeOf(name);
dbcgs.pValue :=@dbv;
dbcgs.szModule :='Protocol';
dbcgs.szSetting:='p';
if CallService(MS_DB_CONTACT_GETSETTINGSTATIC,hContact,lparam(@dbcgs))=0 then
begin
result:=0;
if ServiceExists(MS_PROTO_GETACCOUNT)<>0 then
begin
p:=PPROTOACCOUNT(CallService(MS_PROTO_GETACCOUNT,0,lparam(dbv.szVal.a)));
if p=nil then
result:=-2 // deleted
else if (p^.bIsEnabled=0) or p^.bDynDisabled then
result:=-1; // disabled
end
else
begin
if CallService(MS_PROTO_ISPROTOCOLLOADED,0,lparam(dbv.szVal.a))=0 then
result:=-1;
end;
if (result=0) and (DBReadByte(hContact,strCList,'Hidden',0)=0) then
begin
result:=255;
if ServiceExists(MS_MC_GETMETACONTACT)<>0 then
begin
if CallService(MS_MC_GETMETACONTACT,hContact,0)<>0 then
result:=2;
if StrCmp(
PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO,hContact,0)),
PAnsiChar(CallService(MS_MC_GETPROTOCOLNAME,0,0)))=0 then
result:=1;
end;
end;
if proto<>nil then
StrCopy(proto,dbv.szVal.a);
end
else
begin
result:=-2;
if proto<>nil then
proto^:=#0;
end;
end;
// Import plugin function adaptation
function CreateGroupW(name:pWideChar;hContact:THANDLE):integer;
var
groupId, res:integer;
groupIdStr:array [0..10] of AnsiChar;
grbuf:array [0..127] of WideChar;
p, pw:pWideChar;
begin
if (name=nil) or (name^=#0) then
begin
result:=0;
exit;
end;
StrCopyW(@grbuf[1],name);
grbuf[0]:=WideChar(1 or GROUPF_EXPANDED);
// Check for duplicate & find unused id
groupId:=0;
repeat
pw:=DBReadUnicode(0,'CListGroups',IntToStr(groupIdStr,groupId));
if pw=nil then
break;
res:=StrCmpW(pw+1,@grbuf[1]);
mFreeMem(pw);
if res=0 then
begin
if hContact<>0 then
DBWriteUnicode(hContact,strCList,clGroup,@grbuf[1]);
result:=0;
exit;
end;
inc(groupId);
until false;
DBWriteUnicode(0,'CListGroups',groupIdStr,grbuf);
if hContact<>0 then
DBWriteUnicode(hContact,strCList,clGroup,@grbuf[1]);
p:=StrRScanW(grbuf,'\');
if p<>nil then
begin
p^:=#0;
CreateGroupW(grbuf+1,0);
end;
result:=1;
end;
function CreateGroup(name:pAnsiChar;hContact:THANDLE):integer;
var
groupId, res:integer;
groupIdStr:array [0..10] of AnsiChar;
grbuf:array [0..127] of AnsiChar;
p, pa:pAnsiChar;
begin
if (name=nil) or (name^=#0) then
begin
result:=0;
exit;
end;
StrCopy(@grbuf[1],name);
grbuf[0]:=CHAR(1 or GROUPF_EXPANDED);
// Check for duplicate & find unused id
groupId:=0;
repeat
pa:=DBReadString(0,'CListGroups',IntToStr(groupIdStr,groupId));
if pa=nil then
break;
res:=StrCmp(pa+1,@grbuf[1]);
mFreeMem(pa);
if res=0 then
begin
if hContact<>0 then
DBWriteString(hContact,strCList,clGroup,@grbuf[1]);
result:=0;
exit;
end;
inc(groupId);
until false;
DBWriteString(0,'CListGroups',groupIdStr,grbuf);
if hContact<>0 then
DBWriteString(hContact,strCList,clGroup,@grbuf[1]);
p:=StrRScan(grbuf,'\');
if p<>nil then
begin
p^:=#0;
CreateGroup(grbuf+1,0);
end;
result:=1;
end;
function MyStrSort(para1:pointer; para2:pointer):int; cdecl;
begin
result:=StrCmpW(pWideChar(para1),pWideChar(para2));
end;
function MakeGroupMenu(idxfrom:integer=100):HMENU;
var
sl:TSortedList;
i:integer;
b:array [0..15] of AnsiChar;
p:pWideChar;
begin
result:=CreatePopupMenu;
i:=0;
AppendMenuW(result,MF_STRING,idxfrom,TranslateW('<Root Group>'));
AppendMenuW(result,MF_SEPARATOR,0,nil);
FillChar(sl,SizeOf(sl),0);
sl.increment:=16;
sl.sortFunc:=@MyStrSort;
repeat
p:=DBReadUnicode(0,'CListGroups',IntToStr(b,i),nil);
if p=nil then break;
List_InsertPtr(@sl,p+1);
inc(i);
until false;
inc(idxfrom);
for i:=0 to sl.realCount-1 do
begin
AppendMenuW(result,MF_STRING,idxfrom+i,pWideChar(sl.Items[i]));
p:=pWideChar(sl.Items[i])-1;
mFreeMem(p);
end;
List_Destroy(@sl);
end;
function GetNewGroupName(parent:HWND):pWideChar;
var
mmenu:HMENU;
i:integer;
buf:array [0..63] of WideChar;
pt:TPoint;
begin
result:=nil;
mmenu:=MakeGroupMenu(100);
GetCursorPos(pt);
i:=integer(TrackPopupMenu(mmenu,TPM_RETURNCMD+TPM_NONOTIFY,pt.x,pt.y,0,parent,nil));
if i>100 then // no root or cancel
begin
GetMenuStringW(mmenu,i,buf,HIGH(buf)+1,MF_BYCOMMAND);
StrDupW(result,buf);
end;
DestroyMenu(mmenu);
end;
function SendRequest(url:PAnsiChar;rtype:int;args:pAnsiChar=nil;hNetLib:THANDLE=0):pAnsiChar;
var
nlu:TNETLIBUSER;
req :TNETLIBHTTPREQUEST;
resp:PNETLIBHTTPREQUEST;
hTmpNetLib:THANDLE;
nlh:array [0..1] of TNETLIBHTTPHEADER;
buf:array [0..31] of AnsiChar;
begin
result:=nil;
FillChar(req,SizeOf(req),0);
req.cbSize :=NETLIBHTTPREQUEST_V1_SIZE;//SizeOf(req);
req.requestType:=rtype;
req.szUrl :=url;
req.flags :=NLHRF_NODUMP or NLHRF_HTTP11;
if args<>nil then
begin
nlh[0].szName :='Content-Type';
nlh[0].szValue:='application/x-www-form-urlencoded';
nlh[1].szName :='Content-Length';
nlh[1].szValue:=IntToStr(buf,StrLen(args));
req.headers :=@nlh;
req.headersCount:=2;
req.pData :=args;
req.dataLength :=StrLen(args);
end;
if hNetLib=0 then
begin
FillChar(nlu,SizeOf(nlu),0);
nlu.cbSize :=SizeOf(nlu);
nlu.flags :=NUF_HTTPCONNS or NUF_NOHTTPSOPTION or NUF_OUTGOING or NUF_NOOPTIONS;
nlu.szSettingsModule:='dummy';
hTmpNetLib:=CallService(MS_NETLIB_REGISTERUSER,0,lparam(@nlu));
end
else
hTmpNetLib:=hNetLib;
resp:=pointer(CallService(MS_NETLIB_HTTPTRANSACTION,hTmpNetLib,lparam(@req)));
if resp<>nil then
begin
if resp^.resultCode=200 then
begin
StrDup(result,resp.pData,resp.dataLength);
end
else
begin
result:=pAnsiChar(int_ptr(resp^.resultCode and $0FFF));
end;
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,lparam(resp));
end;
if (hNetLib=0) and (nlu.cbSize<>0) then
CallService(MS_NETLIB_CLOSEHANDLE,hTmpNetLib,0);
end;
(*
static int __inline NLog(AnsiChar *msg) {
return CallService(MS_NETLIB_LOG, (WPARAM)hNetlibUser, (LPARAM)msg);
}
*)
function GetFile(url:PAnsiChar;save_file:PAnsiChar;
hNetLib:THANDLE=0;recurse_count:integer=0):bool;
var
nlu:TNETLIBUSER;
req :TNETLIBHTTPREQUEST;
resp:PNETLIBHTTPREQUEST;
hSaveFile:THANDLE;
i:integer;
begin
result:=false;
if recurse_count>MAX_REDIRECT_RECURSE then
exit;
if (url=nil) or (url^=#0) or (save_file=nil) or (save_file^=#0) then
exit;
FillChar(req,SizeOf(req),0);
req.cbSize :=NETLIBHTTPREQUEST_V1_SIZE;//SizeOf(req);
req.requestType:=REQUEST_GET;
req.szUrl :=url;
req.flags :=NLHRF_NODUMP;
FillChar(nlu,SizeOf(nlu),0);
if hNetLib=0 then
begin
nlu.cbSize :=SizeOf(nlu);
nlu.flags :=NUF_HTTPCONNS or NUF_NOHTTPSOPTION or NUF_OUTGOING or NUF_NOOPTIONS;
nlu.szSettingsModule:='dummy';
hNetLib:=CallService(MS_NETLIB_REGISTERUSER,0,lparam(@nlu));
end;
resp:=pointer(CallService(MS_NETLIB_HTTPTRANSACTION,hNetLib,lparam(@req)));
if resp<>nil then
begin
if resp^.resultCode=200 then
begin
hSaveFile:=Rewrite(save_file);
if hSaveFile<>THANDLE(INVALID_HANDLE_VALUE) then
begin
BlockWrite(hSaveFile,resp^.pData^,resp^.dataLength);
CloseHandle(hSaveFile);
result:=true;
end
end
else if (resp.resultCode>=300) and (resp.resultCode<400) then
begin
// get new location
for i:=0 to resp^.headersCount-1 do
begin
//MessageBox(0,resp^.headers[i].szValue, resp^.headers[i].szName,MB_OK);
if StrCmp(resp^.headers^[i].szName,'Location')=0 then
begin
result:=GetFile(resp^.headers^[i].szValue,save_file,hNetLib,recurse_count+1);
break;
end
end;
end
else
begin
{
_stprintf(buff, TranslateT("Failed to download \"%s\" - Invalid response, code %d"), plugin_name, resp->resultCode);
ShowError(buff);
AnsiChar *ts = GetAString(buff);
NLog(ts);
}
end;
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,lparam(resp));
if nlu.cbSize<>0 then
CallService(MS_NETLIB_CLOSEHANDLE,hNetLib,0);
end;
end;
function GetFile(url:PWideChar;save_file:PWideChar;
hNetLib:THANDLE=0;recurse_count:integer=0):bool;
var
aurl,asave:array [0..MAX_PATH-1] of AnsiChar;
begin
FastWideToAnsiBuf(url,aurl);
FastWideToAnsiBuf(save_file,asave);
result:=GetFile(aurl,asave,hNetLib,0);
end;
function GetProxy(hNetLib:THANDLE):PAnsiChar;
var
nlus:TNETLIBUSERSETTINGS;
pc:PAnsiChar;
proxy:array [0..127] of AnsiChar;
begin
result:=nil;
nlus.cbSize:=SizeOf(nlus);
if CallService(MS_NETLIB_GETUSERSETTINGS,hNetLib,lparam(@nlus))<>0 then
begin
if nlus.useProxy<>0 then
begin
if nlus.proxyType<>PROXYTYPE_IE then
begin
pc:=@proxy;
if nlus.szProxyServer<>nil then
begin
if nlus.useProxyAuth<>0 then
begin
if nlus.szProxyAuthUser<>nil then
begin
pc:=StrCopyE(proxy,nlus.szProxyAuthUser);
if nlus.szProxyAuthPassword<>nil then
begin
pc^:=':'; inc(pc);
pc:=StrCopyE(pc,nlus.szProxyAuthPassword);
end;
pc^:='@';
inc(pc);
end;
end;
pc:=StrCopyE(pc,nlus.szProxyServer);
if nlus.wProxyPort<>0 then
begin
pc^:=':'; inc(pc);
IntToStr(pc,nlus.wProxyPort);
end;
end;
StrDup(result,proxy);
end
else // use IE proxy
begin
mGetMem(result,1);
result^:=#0;
end;
end;
end;
end;
function LoadImageURL(url:pAnsiChar;size:integer=0):HBITMAP;
var
nlu:TNETLIBUSER;
req :TNETLIBHTTPREQUEST;
resp:PNETLIBHTTPREQUEST;
hNetLib:THANDLE;
im:TIMGSRVC_MEMIO;
begin
result:=0;
if (url=nil) or (url^=#0) then
exit;
FillChar(req,SizeOf(req),0);
req.cbSize :=NETLIBHTTPREQUEST_V1_SIZE;//SizeOf(req);
req.requestType:=REQUEST_GET;
req.szUrl :=url;
req.flags :=NLHRF_NODUMP;
FillChar(nlu,SizeOf(nlu),0);
nlu.cbSize :=SizeOf(nlu);
nlu.flags :=NUF_HTTPCONNS or NUF_NOHTTPSOPTION or NUF_OUTGOING or NUF_NOOPTIONS;
nlu.szSettingsModule:='dummy';
hNetLib:=CallService(MS_NETLIB_REGISTERUSER,0,lparam(@nlu));
resp:=pointer(CallService(MS_NETLIB_HTTPTRANSACTION,hNetLib,lparam(@req)));
if resp<>nil then
begin
if resp^.resultCode=200 then
begin
im.iLen :=resp.dataLength;
im.pBuf :=resp.pData;
im.flags:=size shl 16;
im.fif :=FIF_JPEG;
result :=CallService(MS_IMG_LOADFROMMEM,wparam(@im),0);
// if result<>0 then
// DeleteObject(SendMessage(wnd,STM_SETIMAGE,IMAGE_BITMAP,result)); //!!
end;
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,lparam(resp));
end;
CallService(MS_NETLIB_CLOSEHANDLE,hNetLib,0);
end;
function RegisterSingleIcon(resname,ilname,descr,group:pAnsiChar):int;
var
sid:TSKINICONDESC;
begin
FillChar(sid,SizeOf(TSKINICONDESC),0);
sid.cbSize :=SizeOf(TSKINICONDESC);
sid.cx :=16;
sid.cy :=16;
sid.flags :=0;
sid.szSection.a:=group;
sid.hDefaultIcon :=LoadImageA(hInstance,resname,IMAGE_ICON,16,16,0);
sid.pszName :=ilname;
sid.szDescription.a:=descr;
result:=Skin_AddIcon(@sid);
DestroyIcon(sid.hDefaultIcon);
end;
end.
|