-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk_msgs.pas
1518 lines (1360 loc) · 70.3 KB
/
vk_msgs.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
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
(*
VKontakte plugin for Miranda IM: the free IM client for Microsoft Windows
Copyright (c) 2008-2010 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_msgs.pas
[ Description ]
Module to send and receive messages
[ Known Issues ]
None
Contributors: LA
-----------------------------------------------------------------------------}
unit vk_msgs;
interface
procedure MsgsInit();
procedure MsgsDestroy();
procedure vk_GetMsgsSynch();
procedure vk_GetMsgsFriendsEtc();
procedure vk_GetGroupsNews();
procedure vk_GetCommentsNews();
function vk_ReceiveMessage(FromID: THandle; MsgText: WideString; iMsgDate: integer;
iAddlFlags: integer = 0): boolean;
implementation
uses
m_globaldefs,
m_api,
vk_global, // module with global variables and constant used
vk_http, // module to connect with the site
vk_common, // module with common functions
vk_wall, // module to work with the wall
vk_captcha, // module to process captcha
htmlparse, // module to simplify html parsing
vk_core, // module with core functions
uLkJSON, // module to parse data from feed2.php (in JSON format)
Windows,
SysUtils;
{$include api/m_folders.inc}
const
msg_status_captcha_required = 'Security code (captcha) input is required for further processing...';
msg_status_captcha_input = 'Please input the captcha in the separate window';
msg_status_failed = 'Message sending failed (incorrect code?)';
msg_status_captcha_failed = 'Message sending failed. Unable to get the captcha';
PREF_CREATESENT = 16; // one more option for PSR_MESSAGE defined in m_protosvc.inc
type // type to keep message
TMessage = record
bOutgoing: boolean;
bReadState: boolean;
iMsgID: integer;
iMsgDate: integer; // unix format
iMsgSender: integer;
sMsgText: WideString;
end;
type
TMessages = array of TMessage;
type // type to keep news
TNewsRecord = record
NTime: integer;
ID: integer;
NType: string; // add_photo = photo
// movie = video
// add_item = Çàìåòêè
// q = Âîïðîñû
// post = Òåìû
// plus = Äðóçüÿ
// person = Ñòàòóñ
// group = Ãðóïïû
// event = Âñòðå÷è
// audio = Àóäèî
// record = Ëè÷íûå äàííûå
NText: WideString;
end;
type
TNewsRecords = array of TNewsRecord;
type
TNewsRecords2 = array of TNewsRecord;
type
VK_PCCSDATA = ^VK_TCCSDATA;
VK_TCCSDATA = record
ccsData: TCCSDATA;
trx_id: integer;
end;
var
vk_hProtoMessageSend, vk_hProtoMessageSendW, vk_hProtoMessageReceive: THandle;
CaptchaId, CaptchaUrl, CaptchaValue: string;
function MessageSendThread(ccs: VK_PCCSDATA): longword; forward;
// =============================================================================
// function to send message
// (it is called from separate thread, so minimum number of WRITE global variables is used)
// -----------------------------------------------------------------------------
function vk_SendMessage(ToID: integer; Text: WideString; sCaptcha: string = ''): integer;
// 0 - successful
// 1 - not successful (unknown reason)
// 2 - msgs sent two often
var
HTML: string; // html content of the page received
sText: string;
jsoFeed: TlkJSONobject;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_SendMessage) Sending new message to id ' + IntToStr(ToID) + ', message text: ' + ansistring(Text)));
Result := 1; // Unknown error occured
if ToID > 0 then
begin
sText := UTF8Encode(Text);
sText := URLEncode(sText);
HTML := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_messages_send, [ToID, sText]) + sCaptcha));
Netlib_Log(vk_hNetlibUser, PChar('(vk_SendMessage) ... sending of the message to contact ' + IntToStr(ToID) + ' done. Checking result...'));
if Trim(HTML) <> '' then
begin
if Pos('error', HTML) > 0 then
Result := GetJSONError(HTML)
else
Result := GetJSONResponse(HTML);
Netlib_Log(vk_hNetlibUser, PChar('(vk_SendMessage) ... sending of the message to contact ' + IntToStr(ToID) + ' finished with result: ' + IntToStr(Result)));
case Result of
0..9, 100: ; // error occured
14: // captcha needed
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_SendMessage) ... captcha input is required, getting it...'));
jsoFeed := TlkJSON.ParseText(HTML) as TlkJSONobject;
try
CaptchaId := jsoFeed.Field['error'].Field['captcha_sid'].Value;
CaptchaUrl := jsoFeed.Field['error'].Field['captcha_img'].Value;
CaptchaValue := ProcessCaptcha(CaptchaId, CaptchaUrl);
if CaptchaValue = 'captcha_download_failed' then // error - can't download captcha image
Netlib_Log(vk_hNetlibUser, PChar('(vk_SendMessage) ... unable to download captcha'))
else // ok
begin
Result := vk_SendMessage(ToID, Text, '^' + Format(vk_url_api_captcha_addition, [CaptchaId, CaptchaValue]));
end;
finally
jsoFeed.Free;
end;
end;
else // successful
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_SendMessage) ... message sent successfully'));
end;
end;
end;
end;
end;
// =============================================================================
// function to add received/sent message into Miranda's DB
// bChainType = 0 - received (default value)
// bChainType = 1 - sent
// -----------------------------------------------------------------------------
function vk_ReceiveMessage(FromID: THandle; MsgText: WideString; iMsgDate: integer;
iAddlFlags: integer = 0): boolean;
var
ccs_chain: TCCSDATA;
pre: TPROTORECVEVENT; // varibable required to add message to Miranda
begin
Result := False;
FillChar(pre, SizeOf(pre), 0);
pre.flags := PREF_UTF + iAddlFlags;
pre.szMessage := PChar(UTF8Encode(MsgText)); // encode msg to utf8
pre.timestamp := iMsgDate;
pre.lParam := 0;
// now we need to initiate incoming message event
// we can add message without this event (with usage of MS_DB_EVENT_ADD directly),
// but in this case some plugins will not able to filter message (for ex.,
// to ignore them)
FillChar(ccs_chain, SizeOf(ccs_chain), 0);
ccs_chain.szProtoService := PSR_MESSAGE; // so, ProtoMessageReceive will be called,
ccs_chain.hContact := FromID; // if filtering is passed
ccs_chain.wParam := 0;
ccs_chain.flags := PREF_UTF + iAddlFlags; // it is utf8 message
ccs_chain.lParam := Windows.lParam(@pre);
if PluginLink^.CallService(MS_PROTO_CHAINRECV, 0, Windows.lParam(@ccs_chain)) = 0 then // successful?
Result := True;
end;
// =============================================================================
// function to add received message into Miranda's DB
// but with additional check to verify that message not here yet
// -----------------------------------------------------------------------------
function vk_ReceiveMessageCheckUnique(hFromID: THandle; MsgText: WideString; iMsgDate: DWord; bSent: boolean = false): boolean;
var
dbei: TDBEVENTINFO;
hEvent: THandle;
iFlags: integer;
wsDBMsgText: widestring;
begin
Result := False;
// look for presence of the same message in db
// if here - no need to add the same again
hEvent := pluginLink^.CallService(MS_DB_EVENT_FINDLAST, hFromID, 0);
while hEvent <> 0 do
begin
FillChar(dbei, SizeOf(dbei), 0);
dbei.cbSize := SizeOf(dbei);
dbei.cbBlob := PluginLink^.CallService(MS_DB_EVENT_GETBLOBSIZE, hEvent, 0);
dbei.pBlob := AllocMem(dbei.cbBlob);
PluginLink^.CallService(MS_DB_EVENT_GET, hEvent, Windows.lParam(@dbei));
if (dbei.szModule = piShortName) and
(dbei.eventType = EVENTTYPE_MESSAGE) then
begin // some message of our protocol found
if dbei.timestamp < iMsgDate then // no need to further check
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AddMessageToDBUnique) ... have not found such message in the database'));
break;
end;
wsDBMsgText := WideString(PChar(dbei.pBlob)); // text of the message in database
if (iMsgDate = dbei.timestamp) or
((iMsgDate in [dbei.timestamp-90 .. dbei.timestamp+90]) and
(wsDBMsgText = MsgText)) then // duplicate request
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_AddMessageToDBUnique) ... message found in the database, skipped'));
FreeMem(dbei.pBlob);
exit;
end;
end;
hEvent := pluginLink^.CallService(MS_DB_EVENT_FINDPREV, hEvent, 0);
FreeMem(dbei.pBlob);
end;
iFlags := PREF_CREATEREAD;
if bSent = true then
iFlags := iFlags + PREF_CREATESENT;
Result := vk_ReceiveMessage(hFromID, MsgText, iMsgDate, iFlags);
end;
// =============================================================================
// procedure to parse all messages received
// valid messages are added to result TMessages array
// -----------------------------------------------------------------------------
function vk_ParseMsgs(sHTML: string): TMessages;
var
jsoFeed,
jsoFeedAttachment: TlkJSONobject;
iMsgsCount,
i, ii, temppos: integer;
bOutgoing: boolean;
bReadState: boolean;
iMsgID: integer;
iMsgDate: integer; // unix format
iMsgSender: integer;
sMsgText,
sMsgTitle,
sMsgAttachment: WideString;
sHTMLAttachment: string;
iLevel: integer;
begin
if Pos('response', sHTML) > 0 then
begin
jsoFeed := TlkJSON.ParseText(sHTML) as TlkJSONobject;
try
iMsgsCount := jsoFeed.Field['response'].Count;
for i := iMsgsCount - 1 downto 1 do
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_ParseMsgs) ... processing message ' + IntToStr(i)));
bReadState := jsoFeed.Field['response'].Child[i].Field['read_state'].Value;
bOutgoing := jsoFeed.Field['response'].Child[i].Field['out'].Value;
iMsgID := jsoFeed.Field['response'].Child[i].Field['mid'].Value;
iMsgDate := jsoFeed.Field['response'].Child[i].Field['date'].Value;
// iMsgDate := LocalUnixTimeToGMT(iMsgDate);
iMsgSender := jsoFeed.Field['response'].Child[i].Field['uid'].Value;
Netlib_Log(vk_hNetlibUser, PChar('(vk_ParseMsgs) ... message ' + IntToStr(i) + '(' + IntToStr(iMsgID) + '), from id: ' + IntToStr(iMsgSender) + ', sent on ' + IntToStr(iMsgDate)));
sMsgText := jsoFeed.Field['response'].Child[i].Field['body'].Value;
sMsgTitle := jsoFeed.Field['response'].Child[i].Field['title'].Value;
Netlib_Log(vk_hNetlibUser, PChar('(vk_ParseMsgs) ... message ' + IntToStr(i) + '(' + IntToStr(iMsgID) + '), title: ' + string(sMsgTitle) + ', text: ' + string(sMsgText)));
iLevel := 2;
if Pos('attachments', GenerateReadableText(jsoFeed.Field['response'].Child[i], iLevel)) > 0 then
begin
if Trim(sMsgText) <> '' then
sMsgText := sMsgText + Chr(13) + Chr(10) + Chr(13) + Chr(10);
// only 1 attachment can be now
// for ia := 0 to FeedMRoot.Field['response'].Child[i].Field['attachments'].Count-1 do
sMsgAttachment := jsoFeed.Field['response'].Child[i].Field['attachments'].Child[0].Value;
if Pos('audio', sMsgAttachment) > 0 then // audio is attached to the message
begin
sMsgAttachment := TextBetween(sMsgAttachment, '[[audio', ']]');
sHTMLAttachment := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_audio_getbyid, [sMsgAttachment])));
if Pos('response', sHTMLAttachment) > 0 then
begin
jsoFeedAttachment := TlkJSON.ParseText(sHTMLAttachment) as TlkJSONobject;
try
sMsgText := sMsgText +
TranslateW('audio') + ': ' +
jsoFeedAttachment.Field['response'].Child[0].Field['artist'].Value + ' - ' +
jsoFeedAttachment.Field['response'].Child[0].Field['title'].Value + Chr(13) + Chr(10) +
jsoFeedAttachment.Field['response'].Child[0].Field['url'].Value;
finally
jsoFeedAttachment.Free;
end;
end;
end
else
if Pos('photo', sMsgAttachment) > 0 then // photo is attached to the message
begin
sMsgAttachment := TextBetween(sMsgAttachment, '[[photo', ']]');
sHTMLAttachment := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_photos_getbyid, [sMsgAttachment])));
if Pos('response', sHTMLAttachment) > 0 then
begin
jsoFeedAttachment := TlkJSON.ParseText(sHTMLAttachment) as TlkJSONobject;
try
sMsgText := sMsgText +
TranslateW('photo') + ': ' + Chr(13) + Chr(10) +
jsoFeedAttachment.Field['response'].Child[0].Field['src'].Value;
finally
jsoFeedAttachment.Free;
end;
end;
end
else
if Pos('video', sMsgAttachment) > 0 then // video is attached to the message
begin
sMsgAttachment := TextBetween(sMsgAttachment, '[[video', ']]');
sHTMLAttachment := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_video_get, [sMsgAttachment])));
if Pos('response', sHTMLAttachment) > 0 then
begin
jsoFeedAttachment := TlkJSON.ParseText(sHTMLAttachment) as TlkJSONobject;
try
sMsgText := sMsgText +
TranslateW('video') + ': ' +
jsoFeedAttachment.Field['response'].Child[1].Field['title'].Value + Chr(13) + Chr(10) +
jsoFeedAttachment.Field['response'].Child[1].Field['description'].Value + Chr(13) + Chr(10) +
vk_url + '/video' +
sMsgAttachment;
finally
jsoFeedAttachment.Free;
end;
end;
end;
end; // end of attachments parsing
if (iMsgID > 0) and (iMsgDate > 0) and (iMsgSender > 0) and (sMsgText <> '') then
begin
// remove empty subject, if user would like to
if DBGetContactSettingByte(0, piShortName, opt_UserRemoveEmptySubj, 1) = 1 then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_ParseMsgs) ... message ' + IntToStr(i) + '(' + IntToStr(iMsgID) + '), removing empty subject'));
sMsgTitle := StringReplaceW(sMsgTitle, 'Re: ...', '', []);
sMsgTitle := StringReplaceW(sMsgTitle, ' ... ', '', []);
if Length(sMsgTitle) > 4 then
if (sMsgTitle[1] = 'R') and (sMsgTitle[2] = 'e') and (sMsgTitle[3] = '(') then
begin
ii := 4;
while (sMsgTitle[ii] in [widechar('0')..widechar('9')]) and (ii <= Length(sMsgTitle) - 1) do
Inc(ii);
temppos := PosEx('): ...', sMsgTitle);
if (temppos = ii) then
Delete(sMsgTitle, 1, temppos + 6);
end;
end; // end of empty subject removal
if Trim(sMsgTitle) <> '' then
sMsgText := sMsgTitle + ': <br><br>' + sMsgText;
sMsgText := StringReplaceW(sMsgText, '<br>', Chr(13) + Chr(10), [rfReplaceAll, rfIgnoreCase]);
sMsgText := HTMLDecodeW(sMsgText);
sMsgText := Trim(sMsgText);
Netlib_Log(vk_hNetlibUser, PChar('(vk_ParseMsgs) ... message ' + IntToStr(i) + '(' + IntToStr(iMsgID) + '), re-formatted text: ' + string(sMsgText)));
// add message to the final array
SetLength(Result, High(Result) + 2);
Result[High(Result)].bOutgoing := bOutgoing;
Result[High(Result)].bReadState := bReadState;
Result[High(Result)].iMsgID := iMsgID;
Result[High(Result)].iMsgDate := iMsgDate;
Result[High(Result)].iMsgSender := iMsgSender;
Result[High(Result)].sMsgText := sMsgText;
end;
end;
finally
jsoFeed.Free;
end;
end;
end;
// =============================================================================
// procedure to read old messages from the site, if they are not in miranda's DB
// -----------------------------------------------------------------------------
procedure vk_GetMsgsSynch();
var
sHTML: string;
iLastMessage,
i: integer;
aMessages: TMessages;
hContact: THandle;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsSynch) Synchronizing old messages. Getting incoming messages ...'));
iLastMessage := DBGetContactSettingDword(0, piShortName, opt_MsgsLastMsgDateTime, 0);
sHTML := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_messages_synch_inc, [iLastMessage])));
aMessages := vk_ParseMsgs(sHTML);
for i := 0 to High(aMessages) do
if aMessages[i].bReadState = true then // process already read messages only
begin
hContact := GetContactByID(aMessages[i].iMsgSender);
if hContact > 0 then // process messages from the contacts in our list only
begin
vk_ReceiveMessageCheckUnique(hContact, aMessages[i].sMsgText, aMessages[i].iMsgDate);
end;
end;
SetLength(aMessages, 0);
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsSynch) Synchronizing old messages. Getting outgoing messages ...'));
sHTML := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_messages_synch_out, [iLastMessage])));
aMessages := vk_ParseMsgs(sHTML);
for i := 0 to High(aMessages) do
begin
hContact := GetContactByID(aMessages[i].iMsgSender);
if hContact > 0 then // process messages from the contacts in our list only
begin
vk_ReceiveMessageCheckUnique(hContact, aMessages[i].sMsgText, aMessages[i].iMsgDate, true); // add as sent messages
end;
end;
SetLength(aMessages, 0);
DBWriteContactSettingDWord(0, piShortName, opt_MsgsLastMsgDateTime, DateTimeToUnix(Now));
end;
// =============================================================================
// procedure to receive new messages
// (it is called from separate thread, so minimum number of WRITE global variables is used)
// -----------------------------------------------------------------------------
procedure vk_GetMsgsFriendsEtc();
var
sHTML, sHTMLInbox: string; // html content of the pages received
MsgsCount: integer; // temp variable to keep number of new msgs received
FriendsCount: integer; // temp variable to keep number of new authorization requests received
sMsgID: string;
sMsgText: WideString;
sMsgSenderName: WideString;
i: integer;
MsgDate: TDateTime;
MsgSender: integer;
ccs_chain: TCCSDATA;
pCurBlob: PChar;
hTempFriend: THandle; // id of temp contact if msg received from unknown contact
pre: TPROTORECVEVENT; // varibable required to add message to Miranda
FeedRoot, FeedMsgs, FeedMsgsItems,
jsoFeedProfile: TlkJSONobject; // objects to keep parsed JSON data
aMessages: TMessages;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) Checking for new incoming messages, new authorization requests (friends) etc...'));
// check for presence of new messages, friends etc. via the feed
sHTML := HTTP_NL_Get(vk_url + vk_url_feed2);
// correct json text
for i := 1 to length(sHTML) - 2 do // don't check first and last symbols
begin
if sHTML[i] = '{' then
if (sHTML[i + 1] <> '"') and (sHTML[i - 1] = ':') then
Insert('"', sHTML, i + 1);
if (sHTML[i] = ',') then
if (sHTML[i + 1] <> '"') and (sHTML[i - 1] = '"') then
Insert('"', sHTML, i + 1);
if (sHTML[i] = ':') and (sHTML[i + 1] = '"') then
if sHTML[i - 1] <> '"' then
Insert('"', sHTML, i);
end;
if Trim(sHTML) <> '' then
begin
// to support Russian characters, we need to utf8 encode html text
FeedRoot := TlkJSON.ParseText(Utf8Encode(sHTML)) as TlkJSONobject;
if Assigned(FeedRoot) then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... checking messages count'));
FeedMsgs := FeedRoot.Field['messages'] as TlkJSONobject;
if Assigned(FeedMsgs) then
MsgsCount := FeedMsgs.getInt('count')
else
MsgsCount := 0;
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... ' + IntToStr(MsgsCount) + ' message(s) received'));
if MsgsCount > 0 then // got new messages!
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... processing of new message(s) received started'));
sHTMLInbox := HTTP_NL_Get(GenerateApiUrl(vk_url_api_messages_get));
// parse messages first
aMessages := vk_ParseMsgs(sHTMLInbox);
// now process them one by one
if High(aMessages) > -1 then
begin
for i := 0 to High(aMessages) do
begin
// if message from unknown contact then
// we add contact to our list temporary
hTempFriend := GetContactByID(aMessages[i].iMsgSender);
if hTempFriend = 0 then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_ReceiveMessages) ... message ' + IntToStr(i) + '(' + IntToStr(aMessages[i].iMsgID) + ') received from unknown contact, adding him/her to the contact list temporarily'));
// add sender to our contact list
// now we don't read user's status, so it is added as offline -
// field online doesn't work in VK API
// getting MsgSenderName
Netlib_Log(vk_hNetlibUser, PChar('(vk_ReceiveMessages) ... message ' + IntToStr(i) + '(' + IntToStr(aMessages[i].iMsgID) + '), getting name of unknown contact...'));
sHTML := HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_getprofiles, [IntToStr(aMessages[i].iMsgSender), 'first_name,last_name,nickname,sex,online'])));
if Pos('error', sHTML) > 0 then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_ReceiveMessages) ... message ' + IntToStr(i) + '(' + IntToStr(aMessages[i].iMsgID) + '), unable to get name, error code: ' + IntToStr(GetJSONError(sHTML))));
sMsgSenderName := 'id' + IntToStr(aMessages[i].iMsgSender); // define name with id instead
end
else
begin
// TODO: verify if it works properly with unicode symbols
sHTML := HTMLDecodeW(sHTML);
jsoFeedProfile := TlkJSON.ParseText(sHTML) as TlkJSONobject;
try
sMsgSenderName := jsoFeedProfile.Field['response'].Child[0].Field['first_name'].Value + ' ' + jsoFeedProfile.Field['response'].Child[0].Field['last_name'].Value;
finally
jsoFeedProfile.Free;
end;
end;
hTempFriend := vk_AddFriend(aMessages[i].iMsgSender, sMsgSenderName, ID_STATUS_OFFLINE, 0);
// and make it as temporary contact
DBWriteContactSettingByte(hTempFriend, 'CList', 'NotOnList', 1);
DBWriteContactSettingByte(hTempFriend, 'CList', 'Hidden', 1);
end; // if temp friend end
Netlib_Log(vk_hNetlibUser, PChar('(vk_ReceiveMessages) ... message ' + IntToStr(i) + '(' + IntToStr(aMessages[i].iMsgID) + '), adding to miranda database'));
if vk_ReceiveMessage(hTempFriend, aMessages[i].sMsgText, aMessages[i].iMsgDate) = True then // true - if message added successfully
begin
// GAP: Result is not validated
HTTP_NL_Get(GenerateApiUrl(Format(vk_url_api_messages_markasread, [aMessages[i].iMsgID])));
end;
end;
end;
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... processing of new message(s) received finished'));
end; // receiving of new messages completed
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... checking for new incoming messages finished'));
if DBGetContactSettingByte(0, piShortName, opt_UserAuthorizationsReceive, 1) = 1 then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... checking new authorization requests count'));
FeedMsgs := FeedRoot.Field['friends'] as TlkJSONobject;
if Assigned(FeedMsgs) then
FriendsCount := FeedMsgs.getInt('count')
else
FriendsCount := 0;
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... ' + IntToStr(MsgsCount) + ' new authorization request(s) received'));
if FriendsCount > 0 then // got new authorization request(s)!
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... getting new authorization request(s) details'));
FeedMsgsItems := FeedMsgs.Field['items'] as TlkJSONobject;
for i := 0 to FriendsCount - 1 do // now processing all authorization requests one-by-one
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... new authorization request ' + IntToStr(i + 1) + ', getting id and name'));
sMsgID := FeedMsgsItems.NameOf[i];
sMsgSenderName := HTMLDecodeW(FeedMsgsItems.getWideString(i));
// {"user":{"id":123456},"friends":{"count":2,"items":{"1234567":"Ïåòð ♁ Ô-Ô ♁ Âëàäèìèðîâ","26322232":"Äàíèèë Ïåòðîâ"}},"messages":{"count":0},"events":{"count":0},"groups":{"count":0},"photos":{"count":0},"videos":{"count":0},"notes":{"count":0},"opinions":{"count":0},"questions":{"count":0},"gifts":{"count":0},"lang":{"id":"0","p_id":0},"activity":{"updated":0}}
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... new authorization request ' + IntToStr(i + 1) + ', from id: ' + sMsgID));
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... new authorization request ' + IntToStr(i + 1) + ', from person: ' + string(sMsgSenderName)));
if (Trim(sMsgID) <> '') and (TryStrToInt(sMsgID, MsgSender)) {and (Trim(MsgSenderName)<>'')} then
begin
// everything seems to be OK, may proceed
// temporary add contact if required
hTempFriend := GetContactByID(MsgSender);
if hTempFriend = 0 then
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... temporary add contact to our list'));
// add sender to our contact list
// now we don't read user's status, so it is added as offline
hTempFriend := vk_AddFriend(MsgSender, sMsgSenderName, ID_STATUS_OFFLINE, 0);
// and make it as temporary contact
DBWriteContactSettingByte(hTempFriend, 'CList', 'NotOnList', 1);
DBWriteContactSettingByte(hTempFriend, 'CList', 'Hidden', 1);
end;
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... new authorization request ' + IntToStr(i + 1) + ', adding to miranda database'));
FillChar(pre, SizeOf(pre), 0);
pre.flags := PREF_UTF;
MsgDate := Now;
pre.timestamp := DateTimeToUnix(MsgDate);
// GAP(?): use AnsiStrings below, as unicode not supported
//blob is: uin( DWORD ), hContact( HANDLE ), nick( ASCIIZ ), first( ASCIIZ ), last( ASCIIZ ), email( ASCIIZ ), reason( ASCIIZ )
sMsgText := '(text of authorization request is not supported currently)';
pre.lParam := sizeof(DWORD) + sizeof(THANDLE) + Length(ansistring(sMsgSenderName)) + Length(sMsgID) + Length(ansistring(sMsgText)) + 8;
pCurBlob := AllocMem(pre.lParam);
pre.szMessage := PChar(pCurBlob);
PDWORD(pCurBlob)^ := 0;
Inc(pCurBlob, sizeof(DWORD));
PHANDLE(pCurBlob)^ := hTempFriend;
Inc(pCurBlob, sizeof(THANDLE));
StrCopy(pCurBlob, PChar(ansistring(sMsgSenderName)));
// lstrcpyw(pCurBlob, PWideChar(MsgSenderName));
Inc(pCurBlob, Length(ansistring(sMsgSenderName)) + 1);
pCurBlob^ := #0; //firstName
Inc(pCurBlob);
pCurBlob^ := #0; //lastName
Inc(pCurBlob);
pCurBlob^ := #0; //e-mail
Inc(pCurBlob);
StrCopy(pCurBlob, PChar(ansistring(sMsgText))); //reason
// lstrcpyw(pCurBlob, PWideChar(MsgText));
FillChar(ccs_chain, SizeOf(ccs_chain), 0);
ccs_chain.szProtoService := PSR_AUTH; // so, AuthRequestReceived will be called,
ccs_chain.hContact := hTempFriend; // if filtering is passed
ccs_chain.wParam := 0;
ccs_chain.flags := 0;
ccs_chain.lParam := Windows.lParam(@pre);
PluginLink^.CallService(MS_PROTO_CHAINRECV, 0, Windows.lParam(@ccs_chain));
end;
end;
end;
FeedRoot.Free;
end else
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... checking of new authorization requests skipped'));
end;
end;
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetMsgsFriendsEtc) ... checking for new authorization requests finished'));
end;
// =============================================================================
// function to send message
// -----------------------------------------------------------------------------
function ProtoMessageSend(wParam: wParam; lParam: lParam): integer; cdecl;
var
ccs: VK_PCCSDATA;
res: longword;
begin
New(ccs);
ccs^.ccsData := PCCSDATA(lParam)^;
ccs^.trx_id := StrToInt(FormatDateTime('nnsszzz', Now)); // generate trx (message) number
Netlib_Log(vk_hNetlibUser, PChar('(ProtoMessageSend) Sending message, text: ' + PWideChar(ccs^.ccsData.lParam + lstrlen(PChar(ccs^.ccsData.lParam)) + 1)));
SleepEx(10, True);
// call separate thread to send the msg
CloseHandle(BeginThread(nil, 0, @MessageSendThread, ccs, 0, res));
// return the transaction id we've assigned to the trx
Result := ccs^.trx_id;
end;
// =============================================================================
// function to receive messages
// -----------------------------------------------------------------------------
function ProtoMessageReceive(wParam: wParam; lParam: lParam): integer; cdecl;
var
ccs_sm: PCCSDATA;
dbeo: TDBEVENTINFO; // varibable required to add message to Miranda
pre: TPROTORECVEVENT;
begin
ccs_sm := PCCSDATA(lParam);
pre := PPROTORECVEVENT(ccs_sm.lParam)^;
FillChar(dbeo, SizeOf(dbeo), 0);
with dbeo do
begin
cbSize := SizeOf(dbeo);
eventType := EVENTTYPE_MESSAGE; // message
szModule := piShortName;
pBlob := PByte(pre.szMessage); // data
cbBlob := Length(pre.szMessage) + 1; // SizeOf(pBlob);
flags := 0;
if pre.flags and PREF_UTF <> 0 then
flags := flags + DBEF_UTF;
if pre.flags and PREF_CREATEREAD <> 0 then
flags := flags + DBEF_READ; // add message as read
if pre.flags and PREF_CREATESENT <> 0 then
flags := flags + DBEF_SENT; // add message as sent, needed for msgs synchronization
timestamp := pre.timestamp;
end;
PluginLink^.CallService(MS_DB_EVENT_ADD, ccs_sm.hContact, dword(@dbeo));
Result := 0;
end;
// =============================================================================
// function to initiate support of messages sending and receiving
// -----------------------------------------------------------------------------
procedure MsgsInit();
begin
vk_hProtoMessageSend := CreateProtoServiceFunction(piShortName, PSS_MESSAGE, ProtoMessageSend);
vk_hProtoMessageSendW := CreateProtoServiceFunction(piShortName, PSS_MESSAGEW, ProtoMessageSend);
vk_hProtoMessageReceive := CreateProtoServiceFunction(piShortName, PSR_MESSAGE, ProtoMessageReceive);
// no need to support PSR_MESSAGEW - it is not used by new versions of Miranda
// vk_hProtoMessageReceive := CreateProtoServiceFunction(piShortName, PSR_MESSAGEW, ProtoMessageReceive);
end;
// =============================================================================
// function to destroy support of messages sending and receiving
// -----------------------------------------------------------------------------
procedure MsgsDestroy();
begin
pluginLink^.DestroyServiceFunction(vk_hProtoMessageSend);
pluginLink^.DestroyServiceFunction(vk_hProtoMessageSendW);
pluginLink^.DestroyServiceFunction(vk_hProtoMessageReceive);
end;
// =============================================================================
// thread to send message
// -----------------------------------------------------------------------------
function MessageSendThread(ccs: VK_PCCSDATA): longword;
var
trx_id: integer;
hContact: THandle;
MsgText: WideString;
ResultTemp: TResultDetailed;
iResult: integer;
bPostingOnTheWall: boolean;
sWord: WideString;
iWordLength: byte;
begin
Result := 0;
Netlib_Log(vk_hNetlibUser, PChar('(MessageSendThread) Thread started...'));
trx_id := ccs^.trx_id;
hContact := ccs^.ccsData.hContact;
if (ccs^.ccsData.wParam and PREF_UTF) <> 0 then
MsgText := PChar(ccs^.ccsData.lParam) // GAP: not checked
else
if (ccs^.ccsData.wParam and PREF_UNICODE) <> 0 then
MsgText := PWideChar(ccs^.ccsData.lParam + lstrlen(PChar(ccs^.ccsData.lParam)) + 1)
else
MsgText := PChar(ccs^.ccsData.lParam); // GAP: not checked
Dispose(ccs);
if vk_Status = ID_STATUS_OFFLINE then // if offline - send failed ack
begin
ProtoBroadcastAck(piShortName, hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, THandle(trx_id), Windows.lParam(Translate(err_sendmgs_offline)));
Exit;
end;
// verifying if the message should be posted on the wall
// (in this case it should be started with 'wall:' (or user defined value) or
// with translated equivalent
bPostingOnTheWall := False;
sWord := DBReadUnicode(0, piShortName, opt_WallMessagesWord, 'wall:');
iWordLength := Length(TranslateW(PWideChar(sWord))) - 1;
if (Copy(MsgText, 0, iWordLength) = TranslateW(PWideChar(sWord))) then
bPostingOnTheWall := True
else
begin
iWordLength := Length(sWord);
if (Copy(MsgText, 0, iWordLength) = sWord) then
bPostingOnTheWall := True;
end;
if bPostingOnTheWall then
begin // posting message on the wall
MsgText := Copy(MsgText, iWordLength + 1, Length(MsgText) - iWordLength);
ResultTemp := vk_WallPostMessage(DBGetContactSettingDWord(hContact, piShortName, 'ID', 0),
Trim(MsgText),
0);
case ResultTemp.Code of
0: // 0 - successful
ProtoBroadcastAck(piShortName, hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, THandle(trx_id), 0);
1: // 1 - failed
ProtoBroadcastAck(piShortName, hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, THandle(trx_id), Windows.lParam(ResultTemp.Text));
end;
end
else // calling function to send normal message
begin
iResult := vk_SendMessage(DBGetContactSettingDWord(hContact, piShortName, 'ID', 0), MsgText);
case iResult of
0..10: // not successful
ProtoBroadcastAck(piShortName, hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, THandle(trx_id), Windows.lParam(Translate(PChar(err_messages_send[iResult]))));
else // successful
// the ACK contains reference (thandle) to the trx number
ProtoBroadcastAck(piShortName, hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, THandle(trx_id), 0);
end;
end;
Netlib_Log(vk_hNetlibUser, PChar('(TThreadSendMsg) ... thread finished'));
end;
// *****************************************************************************
// *****************************************************************************
// News support part
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// =============================================================================
// procedure to get minimal news
// -----------------------------------------------------------------------------
{
function vk_GetNewsMinimal(): TNewsRecords;
var
NewsPosStart, DayWrapPosStart: integer;
HTML, HTMLDay: string;
nNType, nIDstr, nNTimestr: string;
nText: WideString;
nID: integer;
nNTime: TDateTime;
fSettings: TFormatSettings;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsMinimal) Receiving minimal news...'));
HTML := HTTP_NL_Get(vk_url_pda + vk_url_pda_news);
HTML := UTF8Decode(HTML);
if Trim(HTML) <> '' then
begin
DayWrapPosStart := Pos('stRows', HTML);
if DayWrapPosStart > 0 then
begin
HTMLDay := TextBetweenTagsAttrInc(HTML, 'div', 'class', 'stRows');
NewsPosStart := Pos('<div>', HTMLDay);
while NewsPosStart > 0 do
begin
nNType := 'unknown';
nIDstr := TextBetween(HTMLDay, 'href="/id', '">');
nText := TextBetweenInc(HTMLDay, '<a', '<span class="stTime">');
nText := StringReplace(nText, #10, '', [rfReplaceAll]);
nText := StringReplace(nText, Chr(9), '', [rfReplaceAll]);
nText := StringReplace(nText, '<br/>', ' ', [rfReplaceAll, rfIgnoreCase]);
nText := Trim(HTMLDecodeW(nText));
nNTimestr := TextBetweenInc(HTMLDay, '<span class="stTime">', '</span>');
nNTimestr := Trim(HTMLRemoveTags(nNTimestr));
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, fSettings);
fSettings.TimeSeparator := ':';
if (nText <> '') and (nNType <> '') and (TryStrToInt(nIDstr, nID)) and (TryStrToTime(nNTimestr, nNTime, fSettings)) then
begin
// data seems to be correct
nNTime := Date + nNTime;
SetLength(Result, High(Result) + 2);
Result[High(Result)].NTime := nNTime;
Result[High(Result)].ID := nID;
Result[High(Result)].NType := nNType;
Result[High(Result)].NText := nText;
end;
Delete(HTMLDay, 1, Pos('</div>', HTMLDay) + 5);
NewsPosStart := Pos('<div>', HTMLDay);
end;
end;
end;
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsMinimal) ... receiving minimal news finished'));
end;
// =============================================================================
// procedure to get full version of news
// -----------------------------------------------------------------------------
function vk_GetNewsFull(): TNewsRecords;
var
DayWrapPosStart, feedTablePosStart: integer;
DayTime: TDateTime;
HTML, HTMLDay, HTMLNews, HTMLDate: string;
nNType, nIDstr, nNTimestr: string;
nText: WideString;
nID: integer;
nNTime: TDateTime;
ImgStart, ImgEnd: integer;
fSettings: TFormatSettings;
HasNews: boolean;
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsFull) Receiving news...'));
// get user lang id
HTML := HTTP_NL_Get(vk_url + vk_url_feed2);
vk_UserLangId := TextBetween(HTML, '"lang":{"id":"', '","p_id":');
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsFull) LangId is: ' + vk_UserLangId));
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsFull) LangHash is: ' + vk_UserLangHash));
if vk_UserLangId <> '0' then // change lang to Russian for correct parsing
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsFull) Temporary changing site lang to Russian for parsing...'));
HTTP_NL_Get(Format(vk_url + vk_lang_change, ['0', vk_UserLangHash]));
end;
HTML := HTTP_NL_Get(vk_url + vk_url_news);
if vk_UserLangId <> '0' then // return user default lang
begin
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsFull) Now getting back user default site lang...'));
HTTP_NL_Get(Format(vk_url + vk_lang_change, [vk_UserLangId, vk_UserLangHash]));
end;
if Trim(HTML) <> '' then
begin
HasNews := True;
DayWrapPosStart := Pos('feedDayWrap', HTML);
while HasNews and (DayWrapPosStart > 0) do
begin
HTMLDate := TextBetween(HTML, '<div class="feedDay">', '</div>');
HTMLDate := Trim(HTMLRemoveTags(HTMLDate));
Netlib_Log(vk_hNetlibUser, PChar('(vk_GetNewsFull) HTMLDate: ' + HTMLDate));
;
if (Pos('ñåãîäíÿ', HTMLDate) > 0) or (Pos('ñüîãîäí³', HTMLDate) > 0) then
DayTime := Date
else
if (Pos('â÷åðà', HTMLDate) > 0) or (Pos('â÷îðà', HTMLDate) > 0) then
DayTime := Date - 1
else
DayTime := RusDateToDateTime(HTMLDate, True);
HTMLDay := TextBetweenTagsAttrInc(HTML, 'div', 'class', 'items_wrap');
feedTablePosStart := Pos('<table class="feedTable', HTMLDay);
while HasNews and (feedTablePosStart > 0) do
begin
HTMLNews := TextBetweenTagsInc(HTMLDay, 'table');
// support of new icon names
nNType := TextBetween(HTMLNews, 'images/icons/', '_s.gif?2"');
// support of old icon names
if nNType = '' then
nNType := TextBetween(HTMLNews, 'images/icons/', '_icon.gif?2"');
// support of apps icons
if Pos('x.gif', HTMLNews) > 0 then
nNType := 'apps';
nIDstr := TextBetween(HTMLNews, 'href="/id', '">');
nText := TextBetweenInc(HTMLNews, '<td class="feedStory', '</td>');
// remove images
ImgStart := Pos('<div class="feedFriendImg">', nText);
while ImgStart > 0 do
begin
ImgEnd := PosEx('</div>', nText, ImgStart) + 6;
Delete(nText, ImgStart, ImgEnd - ImgStart + 1);
ImgStart := Pos('<div class="feedFriendImg">', nText);
end;
nText := StringReplace(nText, #10, '', [rfReplaceAll]);
nText := StringReplace(nText, Chr(9), '', [rfReplaceAll]);
nText := StringReplace(nText, '<br/>', ' ', [rfReplaceAll, rfIgnoreCase]);
// remove extra spaces (when contact added more than 1 friend)
nText := StringReplace(nText, '<div class="feedFriend"> <div class="feedFriendText"> ', '<div class="feedFriend"><div class="feedFriendText">', [rfReplaceAll]);
nText := Trim(HTMLDecodeW(nText));
if nNType = 'people' then
nText := TranslateW(DBReadUnicode(0, piShortName, opt_NewsStatusWord, TranslateW('Status:'))) + WideString(' ') + nText;
// nText := LeftStr(nText, Length(nText)-1); // remove trailing dot - doesn't work correctly when contact added more than 1 friend
nNTimestr := TextBetweenInc(HTMLNews, '<td class="feedTime', '</td>');
nNTimestr := Trim(HTMLRemoveTags(nNTimestr));
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, fSettings);
fSettings.TimeSeparator := ':';
if (nText <> '') and (nNType <> '') and (TryStrToInt(nIDstr, nID)) and (TryStrToTime(nNTimestr, nNTime, fSettings)) then
begin
// data seems to be correct
nNTime := DayTime + nNTime;
SetLength(Result, High(Result) + 2);
Result[High(Result)].NTime := nNTime;
Result[High(Result)].ID := nID;
Result[High(Result)].NType := nNType;
Result[High(Result)].NText := nText;
end;
// small optimization trick: checking if we have news or not
if DateTimeToFileDate(nNTime) > DBGetContactSettingDWord(0, piShortName, opt_NewsLastNewsDateTime, 539033600) then
HasNews := True
else
HasNews := False;