summaryrefslogtreecommitdiff
path: root/plugins/MirandaNGHistoryToDB/Contacts.pas
blob: 6ecb7f7eecdca866cc6126fc44212574392daaf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{ ################################################################################ }
{ #                                                                              # }
{ #  MirandaNG HistoryToDB Plugin v2.5                                           # }
{ #                                                                              # }
{ #  License: GPLv3                                                              # }
{ #                                                                              # }
{ #  Author: Grigorev Michael (icq: 161867489, email: sleuthhound@gmail.com)     # }
{ #                                                                              # }
{ ################################################################################ }

{ ################################################################################ }
{ #                                                                              # }
{ # History++ plugin for Miranda IM: the free IM client for Microsoft* Windows*  # }
{ #                                                                              # }
{ # Copyright (C) 2006-2009 theMIROn, 2003-2006 Art Fedorov.                     # }
{ # History+ parts (C) 2001 Christian Kastner                                    # }
{ #                                                                              # }
{ # 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    # }
{ #                                                                              # }
{ ################################################################################ }

unit Contacts;

interface

uses
  Windows, SysUtils, Forms, Classes, Global;

function GetContactDisplayName(hContact: THandle; Proto: AnsiString = ''; Contact: Boolean = False): String;
function GetContactProto(hContact: THandle): AnsiString; overload;
function GetContactProto(hContact: THandle; var SubContact: THandle; var SubProtocol: AnsiString): AnsiString; overload;
function GetContactID(hContact: THandle; Proto: AnsiString = ''; Contact: boolean = false): AnsiString;
function TranslateAnsiW(const S: AnsiString): WideString;
function GetMyContactDisplayName(Proto: AnsiString): String;
function GetMyContactID(Proto: AnsiString): String;

implementation

uses m_api;

function GetContactProto(hContact: THandle): AnsiString;
begin
  Result := PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO, hContact, 0));
end;

function GetContactProto(hContact: THandle; var SubContact: THandle; var SubProtocol: AnsiString): AnsiString;
begin
  Result := PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO, hContact, 0));
  if MetaContactsEnabled and (Result = MetaContactsProto) then
  begin
    SubContact := CallService(MS_MC_GETMOSTONLINECONTACT, hContact, 0);
    SubProtocol := PAnsiChar(CallService(MS_PROTO_GETCONTACTBASEPROTO, SubContact, 0));
  end
  else
  begin
    SubContact := hContact;
    SubProtocol := Result;
  end;
end;

function GetContactDisplayName(hContact: THandle; Proto: AnsiString = ''; Contact: Boolean = False): String;
var
  ci: TContactInfo;
  RetPWideChar, UW: PChar;
begin
  if (hContact = 0) and Contact then
    Result := TranslateW('Server')
  else
  begin
    if Proto = '' then
      Proto := GetContactProto(hContact);
    if Proto = '' then
      Result := TranslateW('Unknown Contact')
    else
    begin
      ci.cbSize := SizeOf(ci);
      ci.hContact := hContact;
      ci.szProto := PAnsiChar(Proto);
      ci.dwFlag := CNF_DISPLAY + CNF_UNICODE;
      if CallService(MS_CONTACT_GETCONTACTINFO, 0, LPARAM(@ci)) = 0 then
      begin
        RetPWideChar := ci.retval.szVal.w;
        UW := TranslateW('Unknown Contact');
        if WideCompareText(RetPWideChar, UW) = 0 then
          Result := AnsiToWideString(GetContactID(hContact, Proto), CP_ACP)
        else
          Result := RetPWideChar;
      end
      else
        Result := String(GetContactID(hContact, Proto));
      if Result = '' then
        Result := TranslateAnsiW(Proto);
    end;
  end;
end;

function GetContactID(hContact: THandle; Proto: AnsiString = ''; Contact: Boolean = False): AnsiString;
var
  uid: PAnsiChar;
  dbv: TDBVARIANT;
  tmp: String;
begin
  Result := '';
  if not((hContact = 0) and Contact) then
  begin
    if Proto = '' then
      Proto := GetContactProto(hContact);
    uid := PAnsiChar(CallProtoService(PAnsiChar(Proto), PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0));
    if (Cardinal(uid) <> CALLSERVICE_NOTFOUND) and (uid <> nil) then
    begin
      try
        if db_get(hContact, PAnsiChar(Proto), uid, @dbv) = 0 then
        begin
          case dbv._type of
            DBVT_BYTE:
              Result := AnsiString(intToStr(dbv.bVal));
            DBVT_WORD:
              Result := AnsiString(intToStr(dbv.wVal));
            DBVT_DWORD:
              Result := AnsiString(intToStr(dbv.dVal));
            DBVT_ASCIIZ:
              Result := AnsiString(dbv.szVal.a);
            DBVT_UTF8:
              begin
                tmp := AnsiToWideString(dbv.szVal.a, CP_UTF8);
                Result := WideToAnsiString(tmp, hppCodepage);
              end;
            DBVT_WCHAR:
              Result := WideToAnsiString(dbv.szVal.w, hppCodepage);
          end;
          DBFreeVariant(@dbv);
        end;
      except
      end;
    end;
  end;
end;

function TranslateAnsiW(const S: AnsiString): WideString;
begin
  Result := AnsiToWideString(Translate(PAnsiChar(S)),hppCodepage);
end;

function GetMyContactDisplayName(Proto: AnsiString): String;
var
  ci: TContactInfo;
  RetPWideChar, UW: PChar;
begin
  ci.cbSize := SizeOf(ci);
  ci.hContact := 0;
  ci.szProto := PAnsiChar(Proto);
  ci.dwFlag := CNF_DISPLAY + CNF_UNICODE;
  if CallService(MS_CONTACT_GETCONTACTINFO, 0, LPARAM(@ci)) = 0 then
  begin
    RetPWideChar := ci.retval.szVal.w;
    UW := TranslateW('Unknown Contact');
    if WideCompareText(RetPWideChar, UW) = 0 then
      Result := TranslateW('Unknown Contact')
    else
      Result := RetPWideChar;
  end
  else
      Result := TranslateW('Unknown Contact');
end;

function GetMyContactID(Proto: AnsiString): String;
var
  ci: TContactInfo;
  RetPWideChar, UW: PChar;
  TmpContactID: AnsiString;
begin
  ci.cbSize := SizeOf(ci);
  ci.hContact := 0;
  ci.szProto := PAnsiChar(Proto);
  ci.dwFlag := CNF_DISPLAY + CNF_UNICODE;
  if CallService(MS_CONTACT_GETCONTACTINFO, 0, LPARAM(@ci)) = 0 then
  begin
    RetPWideChar := ci.retval.szVal.w;
    UW := TranslateW('Unknown Contact');
    if WideCompareText(RetPWideChar, UW) = 0 then
      Result := TranslateW('Unknown Contact')
    else
    begin
      TmpContactID := GetContactID(ci.hContact, Proto);
      if TmpContactID <> '' then
        Result := TmpContactID
      else
        Result := TranslateW('Unknown Contact');
    end;
  end
  else
      Result := TranslateW('Unknown Contact');
end;

end.