-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvk_avatars.pas
438 lines (394 loc) · 16.6 KB
/
vk_avatars.pas
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
(*
VKontakte plugin for Miranda IM: the free IM client for Microsoft Windows
Copyright (c) 2008-2009 Andrey Lukyanov
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
*)
{-----------------------------------------------------------------------------
vk_avatars.pas
[ Description ]
Module to support Avatars for VKontakte plugin
[ Known Issues ]
See the code
Contributors: LA
-----------------------------------------------------------------------------}
unit vk_avatars;
interface
procedure AvatarsInit();
procedure AvatarsDestroy();
procedure vk_AvatarGetAndSave(ID: integer; AvatarURL: string);
procedure vk_AvatarsGet();
implementation
uses
m_globaldefs,
m_api,
vk_global, // module with global variables and constant used
vk_common, // module with common functions
vk_http, // module to connect with the site
htmlparse, // module to simplify html parsing
uLkJSON,
Windows,
SysUtils,
Classes;
var
vk_hAvatarInfoGet, vk_hAvatarCapsGet, vk_hAvatarMyGet, vk_hAvatarMySet: THandle;
// =============================================================================
// procedure to verify whether update of avatar is required,
// and, if required, to get updated avatar
// -----------------------------------------------------------------------------
procedure vk_AvatarGetAndSave(ID: integer; AvatarURL: string);
var
AvatarFileName: string;
hContact: THandle;
pai: TPROTO_AVATAR_INFORMATION;
bAvatarDownloaded: boolean;
bAvatarChanged: boolean;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarGetAndSave) Getting avatar for id ' + IntToStr(ID)));
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarGetAndSave) ... avatar url: ' + AvatarURL));
hContact := GetContactByID(ID);
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarGetAndSave) ... verifying whether avatar is changed or file is deleted'));
bAvatarChanged := False;
if ID = 0 then // user's avatar
begin
if (DBReadString(hContact, piShortName, 'AvatarURL', nil) <> AvatarURL) or
(not FileExists(DBReadString(hContact, piShortName, 'AvatarFile', nil))) then
bAvatarChanged := True;
end else // contacts' avatars
begin
if (DBReadString(hContact, 'ContactPhoto', 'AvatarURL', nil) <> AvatarURL) or
(not FileExists(DBReadString(hContact, 'ContactPhoto', 'File', nil))) then
bAvatarChanged := True;
end;
if bAvatarChanged then
begin
AvatarFileName := FolderAvatars + '\' + IntToStr(ID) + '.jpg';
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarGetAndSave) ... downloading avatar'));
bAvatarDownloaded := False;
if HTTP_NL_GetPicture(AvatarURL, AvatarFileName) then
begin
// write Avatar URL to DB
if ID = 0 then
DBWriteContactSettingString(hContact, piShortName, 'AvatarURL', PChar(AvatarURL))
else
DBWriteContactSettingString(hContact, 'ContactPhoto', 'AvatarURL', PChar(AvatarURL));
bAvatarDownloaded := True;
end;
if bAvatarDownloaded then // downloaded successfully
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarGetAndSave) ... avatar downloaded successfully and saved to ' + string(AvatarFileName)));
// write Avatar File Name to DB
// DBWriteContactSettingString(hContact, 'ContactPhoto', 'File', PChar(AvatarFileName));
// DBWriteContactSettingString(hContact, 'ContactPhoto', 'RFile', PChar(piShortName + '\' + ID + '.jpg'));
if ID = 0 then
DBWriteContactSettingString(hContact, piShortName, 'AvatarFile', PChar(AvatarFileName));
FillChar(pai, sizeof(pai), 0);
pai.cbSize := sizeof(pai);
pai.hContact := hContact;
pai.format := PA_FORMAT_JPEG; // vkontakte supports the following formats: JPG, GIF, PNG, TIF and BMP
SetLength(AvatarFileName, (MAX_PATH) - 1);
// BUG: if user chosen too big picture, Miranda resizes it automatically
// in result original filename is changed and next function doesn't work
Move(AvatarFileName[1], pai.filename[0], Length(AvatarFileName));
// inform Miranda about our readiness
ProtoBroadcastAck(piShortName, hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, THandle(@pai), 0);
end;
end;
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarGetAndSave) ... finished getting of avatar'));
end;
// =============================================================================
// procedure to get avatars for all contacts
// -----------------------------------------------------------------------------
procedure vk_AvatarsGet();
var
HTML: string;
ID, AvatarURL: string;
intID: integer;
begin
HTML := HTTP_NL_Post(vk_url + vk_url_feed_friends, '', 'multipart/form-data', '');
HTML := TextBetween(HTML, 'friends'':[', ']]');
if Trim(HTML) <> '' then
begin
HTML := HTML + ']';
while Pos('[', HTML) > 0 do
begin
// [1234567,"Name Surname","http:\/\/cs123.vkontakte.ru\/u1234567\/b_d919d26a.jpg",9,"","Åâãåíèè",0,1,0,"05"]
ID := TextBetween(HTML, '[', ',');
AvatarURL := TextBetween(HTML, '","', '"');
AvatarURL := StringReplace(AvatarURL, '\/', '/', [rfReplaceAll]);
Delete(HTML, 1, Pos(']', HTML));
if (TryStrToInt(ID, intID)) and (AvatarURL <> 'images/question_b.gif') and (Trim(AvatarURL) <> '') then
vk_AvatarGetAndSave(intID, AvatarURL); // update avatar for each contact
end;
end;
end;
// =============================================================================
// procedure to setup our avatar
// -----------------------------------------------------------------------------
procedure vk_AvatarMySetup(AvatarFileName: WideString);
var
sHTML: string;
HTML: string;
URLUpload: string;
AvatarFile: TFileStream;
szData: string;
DelPhoto, DSubm, DHash: string;
Boundary, FileHeader, FileTrailer, szDataFinal: string;
jsoFeed, jsoFeedPhoto: TlkJSONobject;
sServer, sPhoto, sHash,
sPhotoHash: string;
begin
if AvatarFileName <> '' then
begin
// now we should upload our picture to the server
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) ... getting server details to upload an avatar'));
sHTML := HTTP_NL_Get(GenerateApiUrl(vk_url_api_photos_getProfileUploadServer));
if Pos('upload_url', sHTML) > 0 then
begin
jsoFeed := TlkJSON.ParseText(sHTML) as TlkJSONobject;
try
URLUpload := jsoFeed.Field['response'].Field['upload_url'].Value;
if URLUpload <> '' then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) ... uploading our avatar to the server'));
AvatarFile := TFileStream.Create(AvatarFileName, fmOpenRead);
SetLength(szData, AvatarFile.Size);
AvatarFile.Read(szData[1], AvatarFile.Size);
Boundary := '-----------------------------30742771025321';
FileHeader :=
'--' + Boundary +
#10 +
'Content-Disposition: form-data; name="subm"' +
#10 + #10 +
'1' +
#10 +
'--' + Boundary +
#10 +
'Content-Disposition: form-data; name="photo"; filename="310927.jpg"' +
#10 +
'Content-Type: image/jpeg' +
#10 + #10;
FileTrailer := #10 + '--' + Boundary + '--';
szDataFinal := FileHeader + szData + FileTrailer;
sHTML := HTTP_NL_PostPicture(URLUpload, szDataFinal, Boundary);
if Pos('photo', sHTML) > 0 then
begin
jsoFeedPhoto := TlkJSON.ParseText(sHTML) as TlkJSONobject;
try
sServer := jsoFeedPhoto.Field['server'].Value;
sPhoto := jsoFeedPhoto.Field['photo'].Value;
sHash := jsoFeedPhoto.Field['hash'].Value;
if (sServer <> '') and (sPhoto <> '') and (sHash <> '') then
begin
sHTML := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_photos_saveProfilePhoto, [sServer, sPhoto, sHash])));
sPhotoHash := TextBetween(sHTML, '"photo_hash":"', '"');
if sHTML <> '' then
begin
// html parsing is below since VK API doesn't support normal avatar upload process without IE usage
sPhoto := UrlEncode(sPhoto);
sHTML := HTTP_NL_Get(Format(vk_url + vk_url_photo_load_profile, [vk_api_appid, sPhotoHash]));
if sHTML <> '' then
begin
sHash := TextBetween(sHTML, 'hash: ''', '''');
sHTML := HTTP_NL_Get(Format(vk_url + vk_url_photo_save_profile, [vk_api_appid, sServer, sPhoto, sHash]));
DBWriteContactSettingUnicode(0, piShortName, 'AvatarFile', PWideChar(AvatarFileName));
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) ... avatar defined successfully'));
end;
end;
end else
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) ... failed to get server, photo or hash details'));
finally
jsoFeedPhoto.Free;
end;
end else
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) ... failed to upload an avatar'));
AvatarFile.Free;
end else
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) ... failed to get server to upload an avatar'));
finally
jsoFeed.Free;
end;
end;
end
else // delete existing avatar
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySetup) Deleting our avatar... '));
HTML := HTTP_NL_Get(vk_url + vk_url_photo_my);
DelPhoto := TextBetween(HTML, 'delPhoto', '</form>');
DSubm := TextBetween(DelPhoto, 'id="subm" value="', '"');
DHash := TextBetween(DelPhoto, 'id="hash" value="', '"');
HTML := HTTP_NL_Get(Format(vk_url + vk_url_photo_my_delete, [DSubm, DHash]));
DBDeleteContactSetting(0, piShortName, 'AvatarFile');
DBDeleteContactSetting(0, piShortName, 'AvatarURL');
end;
end;
// =============================================================================
// function to inform Miranda about avatar
// -----------------------------------------------------------------------------
function AvatarInfoGet(wParam: wParam; lParam: lParam): integer; cdecl;
var
pai: TPROTO_AVATAR_INFORMATION;
AvatarFileName: string;
begin
if DBGetContactSettingByte(0, piShortName, opt_UserAvatarsSupport, 0) = 0 then // don't support avatars
begin
Result := GAIR_NOAVATAR;
Exit;
end;
FillChar(pai, SizeOf(pai), 0);
pai.cbSize := SizeOf(pai);
pai := PPROTO_AVATAR_INFORMATION(lParam)^;
if pai.hContact = 0 then
AvatarFileName := DBReadString(pai.hContact, piShortName, 'AvatarFile', nil)
else
AvatarFileName := DBReadString(pai.hContact, 'ContactPhoto', 'File', nil);
if Trim(AvatarFileName) = '' then
begin
Result := GAIR_NOAVATAR;
Exit;
end;
pai.format := PA_FORMAT_JPEG;
StrCopy(pai.filename, PChar(AvatarFileName));
Result := GAIR_SUCCESS;
end;
// =============================================================================
// function to inform Miranda about avatars support functionality
// -----------------------------------------------------------------------------
function AvatarCapsGet(wParam: wParam; lParam: lParam): integer; cdecl;
var
size: PPoint;
begin
case wParam of
AF_MAXSIZE: // avatar image max size
begin
size := PPoint(lParam);
size.X := 200;
size.Y := 200;
Result := 0;
end;
AF_PROPORTION:
begin
Result := PIP_NONE; // keep original proportion of avatar
end;
AF_FORMATSUPPORTED:
begin
if lParam = PA_FORMAT_JPEG then // we support JPEG only
Result := 1
else
Result := 0;
end;
AF_ENABLED:
begin
// if AvatarsEnabled in settings then
Result := 1;
// else
// Result := 0;
end;
AF_DONTNEEDDELAYS:
begin
Result := 1; // this protocol doesn't need delays for fetching contact avatars
end;
6: // AF_MAXFILESIZE
begin
Result := 5242880; // max avatar size = 5 Mb
end;
7: // AF_DELAYAFTERFAIL
begin
Result := 1 * 60 * 60 * 1000; // do not request avatar again if server gave an error for one hour
end;
else
Result := 0;
end;
end;
// =============================================================================
// function to inform Miranda about our avatar
// -----------------------------------------------------------------------------
function AvatarMyGet(wParam: wParam; lParam: lParam): integer; cdecl;
var
AvatarFileName: PChar;
begin
if DBGetContactSettingByte(0, piShortName, opt_UserAvatarsSupport, 0) = 0 then // don't support avatars
begin
Result := -2;
Exit;
end;
if wParam = 0 then
begin
Result := -3;
Exit;
end;
AvatarFileName := DBReadString(0, piShortName, 'AvatarFile', '');
if AvatarFileName = '' then
begin
Result := -1;
Exit;
end;
StrLCopy(PChar(wParam), AvatarFileName, integer(lParam));
Result := 0;
end;
// =============================================================================
// function to set up our avatar
// -----------------------------------------------------------------------------
function AvatarMySet(wParam: wParam; lParam: lParam): integer; cdecl;
var AvatarFileNameNew,
AvatarMyFileName: WideString;
CopyResult: longbool;
res: longword;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySet) Setting up our avatar... '));
AvatarMyFileName := PChar(lParam);
if AvatarMyFileName = '' then // delete avatar
begin
CloseHandle(BeginThread(nil, 0, @vk_AvatarMySetup, nil, 0, res));
end else
begin // set up new avatar
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySet) ... avatar filename: ' + string(AvatarMyFileName)));
// copy file to our avatars storage
AvatarFileNameNew := IncludeTrailingPathDelimiter(FolderAvatars) + ExtractFileName(AvatarMyFileName);
CopyResult := CopyFileW(PWideChar(AvatarMyFileName), PWideChar(AvatarFileNameNew), False); // overwrites existing file
if CopyResult then
begin
AvatarMyFileName := AvatarFileNameNew;
CloseHandle(BeginThread(nil, 0, @vk_AvatarMySetup, Pointer(AvatarMyFileName), 0, res));
// ThrIDAvatarMySet := TThreadAvatarMySet.Create(False); // vk_AvatarMySetup(PChar(lParam));
end
else
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySet) ... not possible to create file: ' + string(AvatarFileNameNew)));
Netlib_Log(vk_hNetlibUser, PChar('(vk_AvatarMySet) ... setting up of our avatar failed'));
end;
end;
Result := 0;
end;
// =============================================================================
// function to initiate avatars support
// -----------------------------------------------------------------------------
procedure AvatarsInit();
begin
// avatar related services
vk_hAvatarInfoGet := CreateProtoServiceFunction(piShortName, PS_GETAVATARINFO, @AvatarInfoGet);
vk_hAvatarCapsGet := CreateProtoServiceFunction(piShortName, PS_GETAVATARCAPS, @AvatarCapsGet);
vk_hAvatarMyGet := CreateProtoServiceFunction(piShortName, PS_GETMYAVATAR, @AvatarMyGet);
vk_hAvatarMySet := CreateProtoServiceFunction(piShortName, PS_SETMYAVATAR, @AvatarMySet);
end;
// =============================================================================
// function to destroy avatars support
// -----------------------------------------------------------------------------
procedure AvatarsDestroy();
begin
pluginLink^.DestroyServiceFunction(vk_hAvatarInfoGet);
pluginLink^.DestroyServiceFunction(vk_hAvatarCapsGet);
pluginLink^.DestroyServiceFunction(vk_hAvatarMyGet);
pluginLink^.DestroyServiceFunction(vk_hAvatarMySet);
end;
begin
end.