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
|
{$INCLUDE compilers.inc}
unit mircontacts;
interface
uses
Windows,
m_api;
//----- Contact info -----
function GetContactProto(hContact: TMCONTACT): PAnsiChar; overload;
function GetContactProto(hContact: TMCONTACT; var SubContact: TMCONTACT; var SubProtocol: PAnsiChar): PAnsiChar; overload;
function GetContactDisplayName(hContact: TMCONTACT; Proto: PAnsiChar = nil; Contact: boolean = false): PWideChar;
function GetContactID(hContact: TMCONTACT; Proto: PAnsiChar = nil; Contact: boolean = false): PAnsiChar;
function GetContactCodePage (hContact: TMCONTACT; Proto: PAnsiChar; var UsedDefault: boolean): Cardinal; overload;
function GetContactCodePage (hContact: TMCONTACT; const Proto: PAnsiChar = nil): Cardinal; overload;
function WriteContactCodePage(hContact: TMCONTACT; CodePage: Cardinal; Proto: PAnsiChar = nil): boolean;
function GetContactStatus(hContact:TMCONTACT):integer;
//----- Contact type check -----
function IsChat(hContact:TMCONTACT):bool;
function IsMirandaUser(hContact:TMCONTACT):integer; // >0=Miranda; 0=Not miranda; -1=unknown
// -2 - deleted account, -1 - disabled account, 0 - hidden
// 1 - metacontact, 2 - submetacontact, positive - active
// proto - ASSIGNED buffer
function IsContactActive(hContact:TMCONTACT;Proto:PAnsiChar=nil):integer;
//----- Save / Load contact -----
function LoadContact(group,setting:PAnsiChar):TMCONTACT;
function SaveContact(hContact:TMCONTACT;group,setting:PAnsiChar):integer;
function FindContactHandle(Proto:PAnsiChar;const dbv:TDBVARIANT;is_chat:boolean=false):TMCONTACT;
//----- Another functions -----
function SetCListSelContact(hContact:TMCONTACT):TMCONTACT;
function GetCListSelContact:TMCONTACT;
function WndToContact(wnd:HWND):TMCONTACT; overload;
function WndToContact:TMCONTACT; overload;
procedure ShowContactDialog(hContact:TMCONTACT;DblClk:boolean=true;anystatus:boolean=true);
procedure SendToChat(hContact:TMCONTACT;pszText:PWideChar);
//----- List of contacts (combobox) -----
procedure FillContactList(list:HWND;filter:boolean=true;format:PWideChar=nil);
function FindContact (list:HWND;contact:TMCONTACT):integer;
implementation
uses
messages,
common, syswin, datetime,
dbsettings;
//----- Contact info -----
function GetContactProto(hContact: TMCONTACT): PAnsiChar;
{$IFDEF AllowInline}inline;{$ENDIF}
begin
Result := Proto_GetProtoName(hContact);
end;
function GetContactProto(hContact: TMCONTACT; var SubContact: TMCONTACT; var SubProtocol: PAnsiChar): PAnsiChar;
begin
Result := Proto_GetProtoName(hContact);
if StrCmp(Result, META_PROTO)=0 then
begin
SubContact := CallService(MS_MC_GETMOSTONLINECONTACT, hContact, 0);
SubProtocol := Proto_GetProtoName(SubContact);
end
else
begin
SubContact := hContact;
SubProtocol := Result;
end;
end;
function GetContactDisplayName(hContact: TMCONTACT; Proto: PAnsiChar = nil; Contact: boolean = false): PWideChar;
var
pName, pUnk:PWideChar;
begin
if (hContact = 0) and Contact then
StrDupW(Result, TranslateW('Server'))
else
begin
if Proto = nil then
Proto := GetContactProto(hContact);
pUnk := TranslateW('''(Unknown Contact)''');
if Proto = nil then
StrDupW(Result, pUnk)
else
begin
pName := Contact_GetInfo(CNF_DISPLAY, hContact, Proto);
if pName <> nil then
begin
if StrCmpW(pName, pUnk)=0 then
AnsiToWide(GetContactID(hContact, Proto), Result, CP_ACP)
else
StrDupW(Result, pName);
mir_free(pName);
end
else
AnsiToWide(GetContactID(hContact, Proto), Result);
if (Result = nil) or (Result^ = #0) then
AnsiToWide(Translate(Proto), Result, Langpack_GetDefaultCodePage);
end;
end;
end;
function GetContactID(hContact: TMCONTACT; Proto: PAnsiChar = nil; Contact: boolean = false): PAnsiChar;
var
uid: PAnsiChar;
dbv: TDBVARIANT;
buf: array [0..15] of AnsiChar;
cp: Cardinal;
begin
Result := nil;
if not((hContact = 0) and Contact) then
begin
if Proto = nil then
Proto := GetContactProto(hContact);
uid := PAnsiChar(CallProtoService(Proto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0));
if (uid <> PAnsiChar(CALLSERVICE_NOTFOUND)) and (uid <> nil) then
begin
// db_get_s comparing to DBGetContactSetting don't translate strings
// when uType=0 (DBVT_ASIS)
if db_get_s(hContact, Proto, uid, @dbv, DBVT_ASIS) = 0 then
begin
case dbv._type of
DBVT_BYTE: StrDup(Result, IntToStr(buf,dbv.bVal));
DBVT_WORD: StrDup(Result, IntToStr(buf,dbv.wVal));
DBVT_DWORD: StrDup(Result, IntToStr(buf,dbv.dVal));
DBVT_ASCIIZ: StrDup(Result, dbv.szVal.a);
DBVT_UTF8,
DBVT_WCHAR: begin
cp := Langpack_GetDefaultCodePage;
if dbv._type = DBVT_UTF8 then
UTF8ToAnsi(dbv.szVal.a, Result, cp)
else // dbv._type = DBVT_WCHAR then
WideToAnsi(dbv.szVal.w, Result, cp);
end;
end;
// free variant
db_free(@dbv);
end;
end;
end;
end;
function GetContactCodePage(hContact: TMCONTACT; Proto: PAnsiChar; var UsedDefault: boolean) : Cardinal;
begin
if Proto = nil then
Proto := GetContactProto(hContact);
if Proto = nil then
Result := Langpack_GetDefaultCodePage
else
begin
Result := DBReadWord(hContact, Proto, 'AnsiCodePage', $FFFF);
If Result = $FFFF then
Result := DBReadWord(0, Proto, 'AnsiCodePage', CP_ACP);
end;
UsedDefault := (Result = CP_ACP);
if UsedDefault then
Result := GetACP();
end;
function GetContactCodePage(hContact: TMCONTACT; const Proto: PAnsiChar = nil): Cardinal;
var
def: boolean;
begin
Result := GetContactCodePage(hContact, Proto, def);
end;
function WriteContactCodePage(hContact: TMCONTACT; CodePage: Cardinal; Proto: PAnsiChar = nil): boolean;
begin
Result := false;
if Proto = nil then
Proto := GetContactProto(hContact);
if Proto = nil then
exit;
DBWriteWord(hContact, Proto, 'AnsiCodePage', CodePage);
Result := True;
end;
function GetContactStatus(hContact:TMCONTACT):integer;
var
szProto:PAnsiChar;
begin
szProto:=GetContactProto(hContact);
if szProto=nil then
result:=ID_STATUS_OFFLINE
else
result:=DBReadWord(hContact,szProto,'Status',ID_STATUS_OFFLINE);
end;
//----- Contact type check -----
function IsChat(hContact:TMCONTACT):bool;
begin
result:=DBReadByte(hContact,GetContactProto(hContact),'ChatRoom',0)=1;
end;
function IsMirandaUser(hContact:TMCONTACT):integer; // >0=Miranda; 0=Not miranda; -1=unknown
var
sz:PAnsiChar;
begin
sz:=DBReadString(hContact,GetContactProto(hContact),'MirVer');
if sz<>nil then
begin
result:=int_ptr(StrPos(sz,'Miranda'));
mFreeMem(sz);
end
else
result:=-1;
end;
function IsContactActive(hContact:TMCONTACT;Proto:PAnsiChar=nil):integer;
var
p:PPROTOACCOUNT;
name: array [0..31] of AnsiChar;
begin
if db_get_static(hContact,'Protocol','p',@name,SizeOf(name))=0 then
begin
result:=0;
p:=Proto_GetAccount(@name);
if p=nil then
result:=-2 // deleted
else if (not p^.bIsEnabled) or p^.bDynDisabled then
result:=-1; // disabled
if (result=0) and (DBReadByte(hContact,strCList,'Hidden',0)=0) then
begin
result:=255;
if db_mc_getMeta(hContact)<>0 then
result:=2;
if StrCmp(GetContactProto(hContact),META_PROTO)=0 then
result:=1;
end;
if Proto<>nil then
StrCopy(Proto,@name);
end
else
begin
result:=-2;
if Proto<>nil then
Proto^:=#0;
end;
end;
//----- Save / Load contact -----
const
opt_cproto = 'cproto';
opt_cuid = 'cuid';
opt_ischat = 'ischat';
function FindContactHandle(Proto:PAnsiChar;const dbv:TDBVARIANT;is_chat:boolean=false):TMCONTACT;
var
uid:PAnsiChar;
ldbv:TDBVARIANT;
hContact:TMCONTACT;
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;
db_free(@ldbv);
end;
end;
// added 2011.04.20
if result<>0 then break;
hContact:=db_find_next(hContact);
end;
end;
function LoadContact(group,setting:PAnsiChar):TMCONTACT;
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
db_free(@dbv)
else
mFreeMem(dbv.szVal.W);
end;
function SaveContact(hContact:TMCONTACT;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:=Proto_GetBaseAccountName(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);
db_free(@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;
//----- Another functions -----
function SetCListSelContact(hContact:TMCONTACT):TMCONTACT;
var
wnd:HWND;
begin
wnd:=cli^.hwndContactTree;
result:=hContact;
SendMessage(wnd,CLM_SELECTITEM ,hContact,0);
end;
function GetCListSelContact:TMCONTACT;
begin
result:=SendMessageW(cli^.hwndContactTree,CLM_GETSELECTION,0,0);
end;
function WndToContact(wnd:HWND):TMCONTACT;
var
hContact:TMCONTACT;
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:TMCONTACT; 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;
procedure ShowContactDialog(hContact:TMCONTACT;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,GetContactProto(hContact))<>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
Clist_ContactDoubleClicked(hContact);
// 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;
gce.time :=GetCurrentTimeStamp;
CallServiceSync(MS_GC_EVENT,0,lparam(@gce));
end;
procedure SendToChat(hContact:TMCONTACT;pszText:PWideChar);
var
gci:TGC_INFO;
pszModule:PAnsiChar;
i,cnt:integer;
begin
pszModule:=GetContactProto(hContact);
cnt:=CallService(MS_GC_GETSESSIONCOUNT,0,lparam(pszModule));
i:=0;
gci.pszModule:=pszModule;
while i<cnt do
begin
gci.iItem:=i;
gci.Flags:=GCF_BYINDEX+GCF_HCONTACT+GCF_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;
//----- List of contacts -----
const
defformat = '%name% - %uid% (%account%:%group%)';
procedure FillContactList(list:HWND; filter:boolean=true;format:PWideChar=nil);
var
hContact:TMCONTACT;
buf:array [0..511] of WideChar;
buf1:array [0..63] of WideChar;
p:PWideChar;
uid:PAnsiChar;
ldbv:TDBVARIANT;
acc:PAnsiChar;
lName,
lGroup,
lAccount,
lUID:boolean;
begin
if format=nil then format:=defformat;
SendMessage(list,CB_RESETCONTENT,0,0);
hContact:=db_find_first();
lName :=StrPosW(format,'%name%')<>nil;
lGroup :=StrPosW(format,'%group%')<>nil;
lAccount:=StrPosW(format,'%account%')<>nil;
lUID :=StrPosW(format,'%uid%')<>nil;
while hContact<>0 do
begin
if ((not filter) and ((IsContactActive(hContact)+1)>=0)) or // + disabled (not deleted)
(filter and (IsContactActive(hContact) >=0)) then
begin
StrCopyW(buf,format);
if lName then
StrReplaceW(buf,'%name%', cli^.pfnGetContactDisplayName(hContact,0));
if lGroup then
begin
p:=DBReadUnicode(hContact,strCList,'Group',nil);
StrReplaceW(buf,'%group%',p);
mFreeMem(p);
end;
if lAccount then
begin
acc:=Proto_GetBaseAccountName(hContact);
StrReplaceW(buf,'%account%',FastAnsiToWideBuf(acc,buf1));
end
else
acc:=nil;
if lUID then
begin
if acc=nil then
acc:=Proto_GetBaseAccountName(hContact);
if IsChat(hContact) then
begin
p:=DBReadUnicode(hContact,acc,'ChatRoomID');
StrReplaceW(buf,'%uid%',p);
mFreeMem(p);
end
else
begin
uid:=PAnsiChar(CallProtoService(acc,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0));
if uid<>PAnsiChar(CALLSERVICE_NOTFOUND) then
begin
if DBReadSetting(hContact,acc,uid,@ldbv)=0 then
begin
case ldbv._type of
DBVT_DELETED: p:='[deleted]';
DBVT_BYTE : p:=IntToStr(buf1,ldbv.bVal);
DBVT_WORD : p:=IntToStr(buf1,ldbv.wVal);
DBVT_DWORD : p:=IntToStr(buf1,ldbv.dVal);
DBVT_UTF8 : UTF8ToWide(ldbv.szVal.A,p);
DBVT_ASCIIZ : AnsiToWide(ldbv.szVal.A,p,Langpack_GetDefaultCodePage);
DBVT_WCHAR : p:=ldbv.szVal.W;
DBVT_BLOB : p:='blob';
end;
StrReplaceW(buf,'%uid%',p);
if ldbv._type in [DBVT_UTF8,DBVT_ASCIIZ] then
mFreeMem(p);
db_free(@ldbv);
end;
end;
StrReplaceW(buf,'%uid%',nil);
end;
end;
SendMessage(list,CB_SETITEMDATA,
SendMessageW(list,CB_ADDSTRING,0,tlparam(@buf)),
hContact);
end;
hContact:=db_find_next(hContact);
end;
end;
function FindContact(list:HWND;contact:TMCONTACT):integer;
var
j:integer;
begin
result:=0;
j:=SendMessage(list,CB_GETCOUNT,0,0);
while j>0 do
begin
dec(j);
if TMCONTACT(SendMessage(list,CB_GETITEMDATA,j,0))=contact then
begin
result:=j;
break;
end;
end;
end;
end.
|