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
|
{$Include compilers.inc}
unit mirutils;
interface
uses windows,m_api;
// icons
function SetButtonIcon(btn:HWND;name:PAnsiChar):HICON;
function RegisterSingleIcon(resname,ilname,descr,group:PAnsiChar):int;
// others
function ConvertFileName(src:PAnsiChar;hContact:TMCONTACT=0):PAnsiChar; overload;
function ConvertFileName(src:PWideChar;hContact:TMCONTACT=0):PWideChar; overload;
function ConvertFileName(src:PAnsiChar;dst:PAnsiChar;hContact:TMCONTACT=0):PAnsiChar; overload;
function ConvertFileName(src:PWideChar;dst:PWideChar;hContact:TMCONTACT=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:TMCONTACT=0;extra:PAnsiChar=nil):PAnsiChar; overload;
function ParseVarString(astr:PWideChar;aContact:TMCONTACT=0;extra:PWideChar=nil):PWideChar; overload;
function ShowVarHelp(dlg:HWND;id:integer=0):integer;
function CreateGroupW(name:PWideChar;hContact:TMCONTACT):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;
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:=IcoLib_GetIcon(name,0);
SendMessage(btn,BM_SETIMAGE,IMAGE_ICON,result);
end;
function ConvertFileName(src:PWideChar;dst:PWideChar;hContact:TMCONTACT=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;
PathToAbsoluteW(src,dst);
mFreeMem(pc);
end;
end;
function ConvertFileName(src:PWideChar;hContact:TMCONTACT=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:TMCONTACT=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;
PathToAbsolute(src,dst);
mFreeMem(pc);
end;
end;
function ConvertFileName(src:PAnsiChar;hContact:TMCONTACT=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:=Langpack_GetDefaultCodePage;
result:=MirCP;
end;
function isVarsInstalled:bool;
{$IFDEF AllowInline}inline;{$ENDIF}
begin
result:=ServiceExists(MS_VARS_FORMATSTRING)<>0;
end;
function ParseVarString(astr:PAnsiChar;aContact:TMCONTACT=0;extra:PAnsiChar=nil):PAnsiChar;
var
tfi:TFORMATINFO;
tmp,pc:PAnsiChar;
begin
if (astr=nil) or (astr^=#0) then exit;
pc:=Utils_ReplaceVars(astr);
astr:=pc;
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:TMCONTACT=0;extra:PWideChar=nil):PWideChar;
var
tfi:TFORMATINFO;
tmp,pc:PWideChar;
begin
if (astr=nil) or (astr^=#0) then exit;
pc:=Utils_ReplaceVarsW(astr);
astr:=pc;
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!
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);
StrCopyW(ppdu.lpwzText,text,MAX_SECONDLINE-1);
if title<>nil then
StrCopyW(ppdu.lpwzContactName,title,MAX_CONTACTNAME-1)
else
ppdu.lpwzContactName[0]:=' ';
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:=TranslateW(FastAnsiToWideBuf(sz,tmp));
if Result<>tmp then
begin
StrDupW(Result,Result);
mFreeMem(tmp);
end;
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
PathToAbsolute(path,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
Profile_GetPathA(300,@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);
Profile_GetNameA(Sizeof(filename)-integer(p-PAnsiChar(@filename)),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;
// Import plugin function adaptation
function CreateGroupW(name:PWideChar;hContact:TMCONTACT):integer;
begin
if (name=nil) or (name^=#0) then
begin
result:=0;
exit;
end;
Clist_GroupCreate(0,name);
if hContact<>0 then
DBWriteUnicode(hContact,strCList,clGroup,name);
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.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.
|