-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jabber.pas
2011 lines (1845 loc) · 53.7 KB
/
Jabber.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
unit Jabber;
interface
uses
System.SysUtils, System.Classes, IdHashMessageDigest, IdCoderMime, Vcl.Dialogs,
Vcl.Controls, Jabber.Types, GmXML, Winapi.Windows, IdGlobal, System.StrUtils,
System.Generics.Collections, System.Win.ScktComp, HGM.Controls.VirtualTable;
type
TJabberClient = class;
TXMPPItem = record
Data: string;
Date: TDateTime;
end;
TXMPPItems = class(TTableData<TXMPPItem>)
end;
TXMPPActions = class;
TXMPPAction = class abstract
private
FOwner: TXMPPActions;
FTimeCreate: Cardinal;
FItem: string;
FFreeAfterExecute: Boolean;
FFreeAfterTimeout: Boolean;
FTimeout: Cardinal;
FIsTimeout: Boolean;
procedure SetOwner(const Value: TXMPPActions);
procedure SetFreeAfterExecute(const Value: Boolean);
procedure SetItem(const Value: string);
procedure SetFreeAfterTimeout(const Value: Boolean);
procedure SetTimeout(const Value: Cardinal);
public
function Execute(Node: TGmXmlNode): Boolean; virtual; abstract;
constructor Create(AOwner: TXMPPActions); virtual;
property Item: string read FItem write SetItem;
property Owner: TXMPPActions read FOwner write SetOwner;
property FreeAfterExecute: Boolean read FFreeAfterExecute write SetFreeAfterExecute;
property FreeAfterTimeout: Boolean read FFreeAfterTimeout write SetFreeAfterTimeout;
property Timeout: Cardinal read FTimeout write SetTimeout;
property TimeCreate: Cardinal read FTimeCreate;
property IsTimeout: Boolean read FIsTimeout write FIsTimeout;
end;
TXMPPActions = class(TList<TXMPPAction>)
private
FJabber: TJabberClient;
procedure SetJabber(const Value: TJabberClient);
public
constructor Create(Client: TJabberClient);
function Execute(Node: TGmXmlNode): Boolean;
function Add(Value: TXMPPAction): Integer;
procedure Delete(Index: Integer); overload;
procedure Delete(Action: TXMPPAction); overload;
procedure CheckTimouts;
procedure Clear;
property Jabber: TJabberClient read FJabber write SetJabber;
end;
TJabberClient = class(TComponent)
private
FConnected: Boolean;
FInReceiveProcess: Integer;
FRosetReceived: Boolean;
FImageVCardSHA: string;
FJabberOnLine: Boolean;
FJabberPort: Word;
FOnConnect: TOnConnect;
FOnConnectError: TOnConnectError;
FOnConnecting: TOnConnect;
FOnDisconnect: TOnDisconnect;
FOnError: TOnError;
FOnGetBookMarks: TOnGetBookMarks;
FOnGetRoster: TOnGetRoster;
FOnIQ: TOnIQ;
FLoginError: Boolean;
FOnJabberOnline: TOnJabberOnline;
FOnLoginError: TOnLoginEror;
FOnMessage: TOnMessage;
FOnPresence: TOnPresence;
FOnReceiveData: TOnReceiveData;
FOnSendData: TOnSendData;
FOnSubscribe: TOnSubscribe;
FPriority: Integer;
FResource: string;
FSocket: TClientSocket;
FUserName: string;
FUserNick: string;
FUserServer: string;
FUserStatus: TShowType;
FUserStatusText: string;
FWaitSetPresence: Boolean;
FXMPPActions: TXMPPActions;
FJabberClientName: string;
FJabberClientVersion: string;
FOnRosterSet: TOnRosterSet;
FVCard: TVCard;
FOnWorkState: TOnWorkState;
FInParse: Boolean;
FAuthHash: string;
function GetJID: string;
procedure ParseReceive(Text: string);
procedure SetJID(const Value: string);
procedure SetPriority(const Value: Integer);
procedure SetUserNick(const Value: string);
procedure SetUserStatus(const Value: TShowType);
procedure SetUserStatusText(const Value: string);
procedure _OnConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure _OnConnectError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure _OnDisconnect(Sender: TObject; Socket: TCustomWinSocket);
procedure _OnReceive(Sender: TObject; Socket: TCustomWinSocket);
procedure _OnSend(Sender: TObject; StrData: string);
procedure _OnSendError(Sender: TObject; Socket: TCustomWinSocket);
procedure SetJabberClientName(const Value: string);
procedure SetJabberClientVersion(const Value: string);
procedure SetInReceiveProcess(const Value: Boolean);
function GetReceiveProcess: Boolean;
function ClientName: string;
procedure SetAuthHash(const Value: string);
property InProcess: Boolean read GetReceiveProcess write SetInReceiveProcess;
protected
XMLStr: string;
FXMPPItems: TXMPPItems;
function GetUniqueID: string;
function GetSASLResponse(AStr: string): string;
public
class function GetUniq: string;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetVersion(AJID: string): TJabberVersion;
function GetVCard(AJID: string): TVCard;
function ConferenceEnter(AConf, ANick: string): TConfPresence;
function GetListOfConference(Server: string): TConfList;
function CheckAccount: Boolean;
function DeleteBadSymbols(Value: string): string;
function SendAddSetContact(Item: TRosterItem): string;
function SendSetVCard(Card: TVCard): string;
function SendAuthRemove(AJID: string): string;
function SendDeleteContact(AJID: string): string;
function SendGetBookmarks: string;
function SendGetVCard(AJID: string): string;
function SendGetDiscoInfo(AServer: string): string;
function SendGetVersion(AJID: string): string;
function SendSetBind: string;
function SendEnterChat(AConf, ANick, APassword: string): string;
function SendGetRoster: string;
function SendInvite(AJID, AConf: string): string;
function SendSetSession: string;
function StrForParsing(var Value: string): string;
function AddContact(Item: TRosterItem): Boolean; overload;
function AddContact(AJID, ANick: string): Boolean; overload;
procedure Connect;
procedure DeleteContact(AJID: string);
procedure Disconnect;
procedure DoError(Sender: TObject; Error: string);
procedure DoGetBookMarks(Sender: TObject; QueryNode: TGmXmlNode);
procedure DoGetIQ(Sender: TObject; QueryNode: TGmXmlNode);
procedure DoGetMessage(Sender: TObject; Item: TJabberMessage);
procedure DoGetPresence(Sender: TObject; QueryNode: TGmXmlNode);
procedure DoGetRoster(Sender: TObject; QueryNode: TGmXmlNode);
procedure DoGetSubscribe(From, Nick: string);
procedure DoJabberOnline(Sender: TObject);
procedure DoLoginError(Sender: TObject; Error: string);
procedure DoRosterSet(Sender: TObject; Item: TRosterItem);
procedure EndSetPresence;
procedure GetBookMarks;
procedure GetRoster;
procedure SetVCard(Card: TVCard);
function RenameContact(Item: TRosterItem): Boolean;
procedure SendAuthRequest(AJID: string);
procedure SendAuthType(AuthType: TMechanisms);
procedure SendBind;
procedure SendData(Str: string);
function SendMessage(AJID, AMessage: string; MessageType: TMessageType): string;
procedure SendIQResult(ID: string);
procedure SendAttention(AJID: string; AMessage: string = '');
function SendPresence(Target: string = ''): string; overload;
procedure SendPresenceUnavailable(Target: string); overload;
procedure SendPing(AJID, ID: string);
procedure SendLast(AJID, ID: string);
procedure SendReadMessage(AJID, MessageID: string);
procedure SendResponse(XMLNX, ResponseValue: string);
procedure SendSASLResponse(ChallengeValue: string);
procedure SendSession;
procedure SendStreamStart;
procedure SendSubscribeAccept(AJID: string);
procedure SendSubscribeCancel(AJID: string);
procedure SendTime(AJID, ID: string);
procedure SendTimeURN(AJID, ID: string);
procedure SendUnsubscribe(AJID: string);
procedure SendVersion(AJID, ID: string);
procedure SendDiscoInfo(AJID, ID: string);
procedure SendDiscoItems(AJID, ID: string);
procedure SetPresence;
procedure StartSetPresence;
procedure UpdateImageHash(BinVal: string);
property Actions: TXMPPActions read FXMPPActions;
property Connected: Boolean read FConnected;
property Online: Boolean read FJabberOnLine;
property VCard: TVCard read FVCard;
property PhotoHash: string read FImageVCardSHA write FImageVCardSHA;
property XMPPItems: TXMPPItems read FXMPPItems;
property RosetReceived: Boolean read FRosetReceived;
published
property JabberPort: Word read FJabberPort write FJabberPort;
property JID: string read GetJID write SetJID;
property OnConnect: TOnConnect read FOnConnect write FOnConnect;
property OnConnectError: TOnConnectError read FOnConnectError write FOnConnectError;
property OnConnecting: TOnConnect read FOnConnecting write FOnConnecting;
property OnDisconnect: TOnDisconnect read FOnDisconnect write FOnDisconnect;
property OnError: TOnError read FOnError write FOnError;
property OnGetBookMarks: TOnGetBookMarks read FOnGetBookMarks write FOnGetBookMarks;
property OnGetRoster: TOnGetRoster read FOnGetRoster write FOnGetRoster;
property OnIQ: TOnIQ read FOnIQ write FOnIQ;
property OnJabberOnline: TOnJabberOnline read FOnJabberOnline write FOnJabberOnline;
property OnLoginError: TOnLoginEror read FOnLoginError write FOnLoginError;
property OnMessage: TOnMessage read FOnMessage write FOnMessage;
property OnPresence: TOnPresence read FOnPresence write FOnPresence;
property OnReceiveData: TOnReceiveData read FOnReceiveData write FOnReceiveData;
property OnRosterSet: TOnRosterSet read FOnRosterSet write FOnRosterSet;
property OnSendData: TOnSendData read FOnSendData write FOnSendData;
property OnSubscribe: TOnSubscribe read FOnSubscribe write FOnSubscribe;
property OnWorkState: TOnWorkState read FOnWorkState write FOnWorkState;
property AuthHash: string read FAuthHash write SetAuthHash;
property Priority: Integer read FPriority write SetPriority;
property Resource: string read FResource write FResource;
property UserName: string read FUserName write FUserName;
property UserNick: string read FUserNick write SetUserNick;
property UserServer: string read FUserServer write FUserServer;
property UserStatus: TShowType read FUserStatus write SetUserStatus;
property UserStatusText: string read FUserStatusText write SetUserStatusText;
property JabberClientName: string read FJabberClientName write SetJabberClientName;
property JabberClientVersion: string read FJabberClientVersion write SetJabberClientVersion;
end;
function LoginFromJID(JID: string): string;
function NickFromJID(JID: string): string;
function NickFromConfJID(JID: string): string;
function GetMechainsms(XMLItem: TGmXmlNode): TMechanisms;
function GetAuthHash(UserName, Server, Password: string): string;
implementation
uses
Math, Jabber.Actions, IM.Main, Vcl.Forms, IM.Tool.Console, System.DateUtils,
System.TimeSpan, CryptUnit;
function GetAuthHash(UserName, Server, Password: string): string;
var
Hasher: TIdHashMessageDigest5;
begin
Hasher := TIdHashMessageDigest5.Create;
Result := Hasher.HashStringAsHex(UserName + ':' + Server + ':' + Password);
Hasher.Free;
end;
function LoginFromJID(JID: string): string;
begin
if Pos('/', JID) > 1 then
Result := Copy(JID, 1, Pos('/', JID) - 1)
else
Result := JID;
end;
function NickFromJID(JID: string): string;
begin
if Pos('@', JID) > 1 then
Result := Copy(JID, 1, Pos('@', JID) - 1)
else
Result := JID;
end;
function NickFromConfJID(JID: string): string;
begin
if Pos('/', JID) > 1 then
Result := Copy(JID, Pos('/', JID) + 1, Length(JID))
else
Result := JID;
end;
function GetMechainsms(XMLItem: TGmXmlNode): TMechanisms;
var
Child: TGmXmlNode;
i: Integer;
begin
Result := mecNONE;
if XMLItem.Params.Values['xmlns'] <> XMLNS_XMPP_SASL then
Exit;
if Assigned(XMLItem) then
begin
for i := 0 to XMLItem.Children.Count - 1 do
begin
Child := XMLItem.Children.Node[i];
if Child.Name = 'mechanism' then
begin
if Child.AsString = 'DIGEST-MD5' then
begin
Exit(mecDIGEST_MD5);
end;
if Child.AsString = 'PLAIN' then
begin
Result := mecPLAIN;
end;
end;
end;
end;
end;
{ TJabberClient }
procedure TJabberClient.DeleteContact(AJID: string);
begin
FXMPPActions.Add(TActionIQContactDelete.Create(FXMPPActions, AJID));
end;
function TJabberClient.CheckAccount: Boolean;
begin
Result := (UserServer <> '') and (UserName <> '') and (JabberPort > 0) and (AuthHash <> '') and (not FLoginError);
end;
procedure TJabberClient.Connect;
begin
if not Connected then
begin
InProcess := True;
FSocket.Host := UserServer;
FSocket.Port := JabberPort;
FSocket.OnError := _OnConnectError;
FSocket.OnConnect := _OnConnect;
FSocket.OnDisconnect := _OnDisconnect;
FSocket.OnRead := _OnReceive;
FSocket.Open;
if Assigned(FOnConnecting) then
FOnConnecting(Self);
end
else
SetPresence;
end;
constructor TJabberClient.Create(AOwner: TComponent);
begin
inherited;
FSocket := TClientSocket.Create(nil);
FXMPPItems := TXMPPItems.Create;
FLoginError := False;
FInReceiveProcess := 0;
FInParse := False;
FRosetReceived := False;
FWaitSetPresence := False;
FUserServer := 'jabber.ru';
FUserNick := '';
FJabberPort := 5222;
FResource := 'jabbrel';
FPriority := 1;
FJabberClientName := 'IMJabber';
FJabberClientVersion := '1.0';
FXMPPActions := TXMPPActions.Create(Self);
//Îáðàáîòêà ïðè ïîëó÷åíèè ñîîáùåíèé
FXMPPActions.Add(TActionMessage.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè ïåðâè÷íûõ äàííûõ ñåðâåðà
FXMPPActions.Add(TActionStreamFeatures.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè îøèáîê
FXMPPActions.Add(TActionStreamError.Create(FXMPPActions));
//Îáðàáîòêà ïðè challenge
FXMPPActions.Add(TActionChallenge.Create(FXMPPActions));
//Îáðàáîòêà ïðè òðåáîâàíèè àâòîðèçàöèè SASL
FXMPPActions.Add(TActionSuccess.Create(FXMPPActions));
//Îáðàáîòêà çàïðîñîâ ïîäïèñêè
FXMPPActions.Add(TActionPresenceSubscribe.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè îøèáêè ïðè àóòåíòèôèêàöèè
FXMPPActions.Add(TActionFailure.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè çàïðîñà âåðñèè
FXMPPActions.Add(TActionIQResponseVersion.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè ping
FXMPPActions.Add(TActionIQResponsePing.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè time
FXMPPActions.Add(TActionIQResponseTime.Create(FXMPPActions));
//Îáðàáîòêà ïðè ïîëó÷åíèè èíô. îá èçìåíåíèè êîíòàêòà
FXMPPActions.Add(TActionIQRosterSet.Create(FXMPPActions));
//Îáðàáîòêà ïðè çàïðîñå âîçìîæíîñòåé êëèåíòà
FXMPPActions.Add(TActionIQResponseDiscoInfo.Create(FXMPPActions));
//Îáðàáîòêà ïðè çàïðîñå ñóùíîñòåé
FXMPPActions.Add(TActionIQResponseDiscoItems.Create(FXMPPActions));
//Îáðàáîòêà ïðè çàïðîñå Last Activity
FXMPPActions.Add(TActionIQResponseLast.Create(FXMPPActions));
end;
procedure TJabberClient.SendStreamStart;
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('stream:stream', True) do
begin
Params.Values['xmlns:stream'] := XMLNS_STREAMS;
Params.Values['version'] := '1.0';
Params.Values['xmlns'] := XMLNS_CLIENT;
Params.Values['to'] := FUserServer;
Params.Values['xml:lang'] := 'en';
Params.Values['xmlns:xml'] := XMLNS_XML;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendSubscribeAccept(AJID: string);
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('presence') do
begin
Params.Values['type'] := 'subscribed';
Params.Values['to'] := AJID;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendSubscribeCancel(AJID: string);
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('presence') do
begin
Params.Values['type'] := 'unsubscribed';
Params.Values['to'] := AJID;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendUnsubscribe(AJID: string);
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('presence') do
begin
Params.Values['type'] := 'unsubscribe';
Params.Values['to'] := AJID;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendVersion(AJID, ID: string);
var
WinV: Word;
begin
WinV := Winapi.Windows.GetVersion and $0000FFFF;
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'result';
Params.Values['to'] := AJID;
Params.Values['from'] := JID + '/' + Resource;
Params.Values['id'] := ID;
with Children.AddOpenTag('query') do
begin
Params.Values['xmlns'] := XMLNS_VERSION;
Children.AddOpenTag('name').AsString := JabberClientName;
Children.AddCloseTag;
Children.AddOpenTag('version').AsString := JabberClientVersion;
Children.AddCloseTag;
Children.AddOpenTag('os').AsString := 'Windows ' + IntToStr(Lo(WinV)) + '.' + IntToStr(Hi(WinV));
Children.AddCloseTag;
end;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendPing(AJID, ID: string);
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'result';
Params.Values['to'] := AJID;
Params.Values['from'] := JID + '/' + Resource;
Params.Values['id'] := ID;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendTime(AJID, ID: string);
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'result';
Params.Values['to'] := AJID;
Params.Values['from'] := JID + '/' + Resource;
Params.Values['id'] := ID;
with Children.AddOpenTag('query') do
begin
Params.Values['xmlns'] := XMLNS_TIME;
Children.AddOpenTag('utc').AsString := DateToStr(Now) + ' ' + TimeToStr(Now);
Children.AddCloseTag;
Children.AddOpenTag('tz').AsString := 'MDT';
Children.AddCloseTag;
Children.AddOpenTag('display').AsString := DateToStr(Now) + ' ' + TimeToStr(Now);
Children.AddCloseTag;
end;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendTimeURN(AJID, ID: string);
begin
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'result';
Params.Values['to'] := AJID;
Params.Values['from'] := JID + '/' + Resource;
Params.Values['id'] := ID;
with Children.AddOpenTag('time') do
begin
Params.Values['xmlns'] := XMLNS_URN_TIME;
Children.AddOpenTag('utc').AsString := FormatDateTime('YYYY-MM-DD''T''HH:MM:SS''Z''', TTimeZone.local.ToUniversalTime(Now));
Children.AddCloseTag;
Children.AddOpenTag('tzo').AsString := TTimeZone.local.UtcOffset.ToString;
Children.AddCloseTag;
end;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient._OnConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
FConnected := True;
SendStreamStart;
InProcess := False;
if Assigned(FOnConnect) then
FOnConnect(Self);
end;
procedure TJabberClient._OnDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
FConnected := False;
FRosetReceived := False;
FJabberOnLine := False;
InProcess := False;
if Assigned(FOnDisconnect) then
FOnDisconnect(Self);
end;
procedure TJabberClient.DoError(Sender: TObject; Error: string);
begin
if Assigned(FOnError) then
FOnError(Self, ERR_PROTOCOL, Error);
end;
procedure TJabberClient._OnConnectError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
if Assigned(FOnConnectError) then
FOnConnectError(Self);
end;
function TJabberClient.DeleteBadSymbols(Value: string): string;
var
i: Integer;
begin
Result := Value;
for i := 0 to Length(Value) do
begin
if (Result[i + 1] < #$20) and (Result[i + 1] <> #$0D) and (Result[i + 1] <> #$0A) then
Result[i + 1] := '?';
end;
end;
destructor TJabberClient.Destroy;
begin
FXMPPItems.Free;
FXMPPActions.Clear;
FXMPPActions.Free;
FSocket.Free;
inherited;
end;
procedure TJabberClient.Disconnect;
begin
if FConnected then
begin
FSocket.Close;
end;
end;
procedure TJabberClient.EndSetPresence;
begin
FWaitSetPresence := False;
SetPresence;
end;
procedure TJabberClient.SendSession;
begin
FXMPPActions.Add(TActionIQSetSession.Create(FXMPPActions));
end;
function TJabberClient.SendSetBind: string;
begin
Result := GetUniqueID;
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'set';
Params.Values['id'] := Result;
with Children.AddOpenTag('bind') do
begin
Params.Values['xmlns'] := XMLNS_XMPP_BIND;
with Children.AddOpenTag('resource') do
begin
AsString := FResource;
end;
end;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendSASLResponse(ChallengeValue: string);
begin
SendResponse(XMLNS_XMPP_SASL, GetSASLResponse(ChallengeValue));
end;
function TJabberClient.SendSetSession: string;
begin
Result := GetUniqueID;
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'set';
Params.Values['id'] := Result;
with Children.AddOpenTag('session') do
begin
Params.Values['xmlns'] := XMLNS_XMPP_SESSION;
end;
end;
SendData(Text);
Free;
end;
end;
function TJabberClient.SendSetVCard(Card: TVCard): string;
var
i: Integer;
FlagAddr: TAddressFlag;
FlagTel: TTelFlag;
FlagEmail: TEmailFlag;
begin
Result := GetUniqueID;
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'set';
Params.Values['id'] := Result;
with Children.AddOpenTag('vCard') do
begin
Params.Values['xmlns'] := XMLNS_VCARD;
Children.AddTagValue('FN', Card.FullName);
//Èìÿ
with Children.AddOpenTag('N') do
begin
Children.AddTagValue('GIVEN', Card.Name.FirstName);
Children.AddTagValue('MIDDLE', Card.Name.MiddleName);
Children.AddTagValue('FAMILY', Card.Name.LastName);
end;
Children.AddCloseTag;
//Îáùàÿ èíô.
Children.AddTagValue('NICKNAME', Card.NickName);
Children.AddTagValue('BDAY', FormatDateTime('YYYY-MM-DD', Card.BirthDay));
Children.AddTagValue('URL', Card.URL);
Children.AddTagValue('TITLE', Card.Title);
Children.AddTagValue('ROLE', Card.Role);
Children.AddTagValue('DESC', Card.Desc);
//Ôîòî
with Children.AddOpenTag('PHOTO') do
begin
Children.AddTagValue('TYPE', Card.Photo.PhotoType);
Children.AddTagValue('BINVAL', Card.Photo.BinVal);
end;
Children.AddCloseTag;
//Àäðåñà
for i := Low(Card.Address) to High(Card.Address) do
begin
with Children.AddOpenTag('ADR') do
begin
for FlagAddr in Card.Address[i].Flags do
Children.AddTagValue(AddressFlagToStr[FlagAddr], '');
Children.AddTagValue('EXTADD', Card.Address[i].ExtAdd);
Children.AddTagValue('STREET', Card.Address[i].Street);
Children.AddTagValue('LOCALITY', Card.Address[i].Locality);
Children.AddTagValue('REGION', Card.Address[i].Region);
Children.AddTagValue('PCODE', Card.Address[i].PCode);
Children.AddTagValue('CTRY', Card.Address[i].Country);
end;
Children.AddCloseTag;
end;
//Òåëåôîíû
for i := Low(Card.Tel) to High(Card.Tel) do
begin
with Children.AddOpenTag('TEL') do
begin
for FlagTel in Card.Tel[i].Flags do
Children.AddTagValue(TelFlagToStr[FlagTel], '');
Children.AddTagValue('NUMBER', Card.Tel[i].Number);
end;
Children.AddCloseTag;
end;
//Ïî÷òà
for i := Low(Card.EMail) to High(Card.EMail) do
begin
with Children.AddOpenTag('EMAIL') do
begin
for FlagEmail in Card.EMail[i].Flags do
Children.AddTagValue(EmailFlagToStr[FlagEmail], '');
Children.AddTagValue('USERID', Card.EMail[i].UserId);
end;
Children.AddCloseTag;
end;
//Îðãàíèçàöèÿ
with Children.AddOpenTag('ORG') do
begin
Children.AddTagValue('ORGNAME', Card.Organisation.Name);
Children.AddTagValue('ORGUNIT', Card.Organisation.OrgUnit);
end;
Children.AddCloseTag;
end;
Children.AddCloseTag;
end;
SendData(Text);
Free;
end;
end;
procedure TJabberClient.SendData(Str: string);
var
Data: UTF8String;
begin
try
Data := UTF8Encode(DeleteBadSymbols(Str));
FSocket.Socket.SendBuf((@Data[1])^, Length(Data));
_OnSend(Self, Str);
except
_OnSendError(Self, FSocket.Socket);
end;
end;
procedure TJabberClient._OnReceive(Sender: TObject; Socket: TCustomWinSocket);
begin
ParseReceive(Socket.ReceiveText);
end;
procedure TJabberClient._OnSend(Sender: TObject; StrData: string);
begin
if Assigned(OnSendData) then
FOnSendData(Sender, StrData);
end;
procedure TJabberClient._OnSendError(Sender: TObject; Socket: TCustomWinSocket);
begin
//
end;
procedure TJabberClient.DoGetSubscribe(From, Nick: string);
begin
if Assigned(FOnSubscribe) then
FOnSubscribe(Self, From, Nick);
end;
procedure TJabberClient.ParseReceive(Text: string);
var
CheckedStr: string;
XMLParser: TGmXML;
XMLItem: TGmXmlNode;
ItemName: string;
Handled: Boolean;
Item: TXMPPItem;
i: Integer;
begin
XMLStr := XMLStr + UTF8ToString(Text);
InProcess := True;
while XMLStr <> '' do
begin
// Â CheckStr âûòàñêèâàåì çàâåðøåííûå XML äàííûå äëÿ äàëüíåéøåãî ðàçáîðà
// Â XMLStr îñòàåòñÿ ïðîäîëæåíèå åñëè îíî åñòü
CheckedStr := StrForParsing(XMLStr);
if CheckedStr = '' then
Break;
// Çàìåíÿåì ñèìâîëû ' íà "
CheckedStr := StringReplace(CheckedStr, '''', '"', [rfReplaceAll]);
// Ãåíåðèðóåì ñîáûòèå ÷òî ïðèøëè äàííûå
if Assigned(FOnReceiveData) then
begin
Handled := False;
FOnReceiveData(Self, CheckedStr, Handled);
if Handled then
Continue;
end;
//Åñëè äàííûå íå áûëè ïåðåõâà÷åíû, òî äîáàâëÿåì èõ â î÷åðåäü
Item.Data := CheckedStr;
Item.Date := Now;
FXMPPItems.Add(Item);
end;
//Ïîêà åñòü ÷òî-òî â î÷åðåäè, ïðîõîäèì ïî î÷åðåäè è ïûòàåìñÿ íàéòè îáðàáîò÷èê äàííûõ
//Çàáèðàåì ýëåìåíò èç î÷åðåäè
//Åñëè îáðàáîò÷èê íàéäåí, òî íà÷èíàåì îáðàáîòêó è ïðåðûâàåì î÷åðåäü
//Åñëè îáðàáîò÷èê íå íàéäåí, òî âîçâðàùàåì ýëåìåíò â î÷åðåäü
//Åñëè íè äëÿ êîãî îáðàáîò÷èê íå íàéäåí, çíà÷èò ìû ÷òî-òî æä¸ì, ïðåðûâàåì âåñü öèêë îáðàáîòêè
//Âåðí¸ìñÿ ñþäà, êîãäà ÷òî-òî ïðèä¸ò
while FXMPPItems.Count > 0 do
begin
Handled := False;
XMLParser := TGmXML.Create;
for i := 0 to FXMPPItems.Count - 1 do
begin
if i > FXMPPItems.Count - 1 then
Break;
Handled := True;
try
Item := FXMPPItems[i];
FXMPPItems.Delete(i);
XMLParser.Nodes.Clear;
XMLParser.Text := Item.Data;
XMLItem := XMLParser.Nodes.Root;
//Îáðàáîò÷èê
if FXMPPActions.Execute(XMLItem) then
begin
Break;
end;
//Òå, ÷òî íå áûëè îáðàáîòàíû
ItemName := XMLItem.Name;
if ItemName = XMLNS_IQUERY then
begin
DoGetIQ(Self, XMLItem);
Break;
end;
if ItemName = XMLNS_STREAM then
begin
Break;
end;
if ItemName = XMLNS_ITEMPRESENCE then
begin
if FRosetReceived then
begin
DoGetPresence(Self, XMLItem);
Break;
end;
end;
except
on E: Exception do
begin
DoError(Self, E.Message);
end
end;
//Åñëè íè ìû, íè êîä âûøå íå îáðàáîòàë äàííûå (debug)
FOnReceiveData(Self, 'Not handled :' + ItemName, Handled);
FXMPPItems.Add(Item);
Handled := False;
end;
XMLParser.Free;
if not Handled then
Break;
end;
InProcess := False;
end;
function TJabberClient.RenameContact(Item: TRosterItem): Boolean;
var
Action: TActionIQContactRename;
begin
Result := False;
Action := TActionIQContactRename.Create(FXMPPActions, Item);
FXMPPActions.Add(Action);
while not Action.IsTimeout do
begin
if Action.Executed then
begin
Result := Action.Status;
Break;
end;
FXMPPActions.CheckTimouts;
Application.ProcessMessages;
end;
FXMPPActions.Delete(Action);
end;
function TJabberClient.AddContact(Item: TRosterItem): Boolean;
var
Action: TActionIQContactAdd;
begin
Result := False;
Action := TActionIQContactAdd.Create(FXMPPActions, Item);
FXMPPActions.Add(Action);
while not Action.IsTimeout do
begin
if Action.Executed then
begin
Result := Action.Status;
Break;
end;
FXMPPActions.CheckTimouts;
Application.ProcessMessages;
end;
FXMPPActions.Delete(Action);
end;
function TJabberClient.AddContact(AJID, ANick: string): Boolean;
var
Item: TRosterItem;
begin
Item := TRosterItem.Create(AJID, ANick);
Result := AddContact(Item);
Item.Free;
end;
// Îòïðàâêà íà ñåðâåð òèïà àóòåíòèôèêàöèè
procedure TJabberClient.SendBind;
begin
FXMPPActions.Add(TActionIQSetBind.Create(FXMPPActions));
end;
function TJabberClient.SendDeleteContact(AJID: string): string;
begin
Result := GetUniq;
with TGmXML.Create do
begin
with Nodes.AddOpenTag('iq') do
begin
Params.Values['type'] := 'set';
Params.Values['id'] := Result;
with Children.AddOpenTag('query') do
begin
Params.Values['xmlns'] := XMLNS_ROSTER;
with Children.AddOpenTag('item') do
begin
Params.Values['subscription'] := 'remove';
Params.Values['jid'] := AJID;
end;
end;
end;
SendData(Text);
Free;
end;
end;
function TJabberClient.ClientName: string;
begin
Result := 'IMJabber';
end;
procedure TJabberClient.SendDiscoInfo(AJID, ID: string);