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
|
/*
* Copyright (C) 2008-2009 Manjeet Dahiya
*
* Author:
* Manjeet Dahiya
*
* Source:
* http://code.google.com/p/qxmpp
*
* This file is a part of QXmpp library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
*/
#include "QXmppStream.h"
#include "QXmppPacket.h"
#include "QXmppUtils.h"
#include "QXmppClient.h"
#include "QXmppRoster.h"
#include "QXmppPresence.h"
#include "QXmppIq.h"
#include "QXmppBind.h"
#include "QXmppSession.h"
#include "QXmppRosterIq.h"
#include "QXmppMessage.h"
#include "QXmppConstants.h"
#include "QXmppVCard.h"
#include "QXmppNonSASLAuth.h"
#include "QXmppInformationRequestResult.h"
#include "QXmppIbbIqs.h"
#include "QXmppDataIq.h"
#include "QXmppIbbTransferManager.h"
#include "QXmppLogger.h"
#include <QDomDocument>
#include <QStringList>
#include <QRegExp>
#include <QHostAddress>
#include <QXmlStreamWriter>
static const QByteArray streamRootElementStart = "<?xml version=\"1.0\"?><stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:client\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n";
static const QByteArray streamRootElementEnd = "</stream:stream>";
QXmppStream::QXmppStream(QXmppClient* client)
: QObject(client), m_client(client), m_roster(this),
m_sessionAvaliable(false), m_vCardManager(m_client)
{
bool check = QObject::connect(&m_socket, SIGNAL(hostFound()),
this, SLOT(socketHostFound()));
Q_ASSERT(check);
check = QObject::connect(&m_socket, SIGNAL(connected()),
this, SLOT(socketConnected()));
Q_ASSERT(check);
check = QObject::connect(&m_socket, SIGNAL(disconnected()),
this, SLOT(socketDisconnected()));
Q_ASSERT(check);
check = QObject::connect(&m_socket, SIGNAL(readyRead()),
this, SLOT(socketReadReady()));
Q_ASSERT(check);
check = QObject::connect(&m_socket, SIGNAL(encrypted()),
this, SLOT(socketEncrypted()));
Q_ASSERT(check);
check = QObject::connect(&m_socket,
SIGNAL(sslErrors(const QList<QSslError>&)), this,
SLOT(socketSslErrors(const QList<QSslError>&)));
Q_ASSERT(check);
check = QObject::connect(&m_socket,
SIGNAL(error(QAbstractSocket::SocketError)), this,
SLOT(socketError(QAbstractSocket::SocketError)));
Q_ASSERT(check);
check = QObject::connect(this,
SIGNAL(presenceReceived(const QXmppPresence&)),
&m_roster,
SLOT(presenceReceived(const QXmppPresence&)));
Q_ASSERT(check);
check = QObject::connect(this, SIGNAL(rosterIqReceived(const QXmppRosterIq&)),
&m_roster, SLOT(rosterIqReceived(const QXmppRosterIq&)));
Q_ASSERT(check);
check = QObject::connect(this, SIGNAL(rosterRequestIqReceived(const QXmppRosterIq&)),
&m_roster, SLOT(rosterRequestIqReceived(const QXmppRosterIq&)));
Q_ASSERT(check);
check = QObject::connect(this, SIGNAL(vCardIqReceived(const QXmppVCard&)),
&m_vCardManager, SLOT(vCardIqReceived(const QXmppVCard&)));
Q_ASSERT(check);
}
QXmppStream::~QXmppStream()
{
}
QXmppConfiguration& QXmppStream::getConfiguration()
{
return m_client->getConfiguration();
}
void QXmppStream::connect()
{
log(QString("Connecting to: %1:%2").arg(getConfiguration().
getHost()).arg(getConfiguration().getPort()));
m_socket.setProxy(getConfiguration().getNetworkProxy());
m_socket.connectToHost(getConfiguration().
getHost(), getConfiguration().getPort());
}
void QXmppStream::socketSslErrors(const QList<QSslError> & error)
{
log(QString("SSL errors"));
m_socket.ignoreSslErrors();
for(int i = 0; i< error.count(); ++i)
log(error.at(i).errorString());
}
void QXmppStream::socketHostFound()
{
log(QString("Host found"));
emit hostFound();
}
void QXmppStream::socketConnected()
{
flushDataBuffer();
log(QString("Connected"));
emit connected();
sendStartStream();
}
void QXmppStream::socketDisconnected()
{
flushDataBuffer();
log(QString("Disconnected"));
emit disconnected();
}
void QXmppStream::socketEncrypted()
{
log(QString("Encrypted"));
sendStartStream();
}
void QXmppStream::socketError(QAbstractSocket::SocketError ee)
{
m_socketError = ee;
emit error(QXmppClient::SocketError);
log(QString("Socket error: " + m_socket.errorString()));
}
void QXmppStream::socketReadReady()
{
QByteArray data = m_socket.readAll();
log("SERVER [COULD BE PARTIAL DATA]:" + data.left(20));
parser(data);
}
void QXmppStream::sendNonSASLAuthQuery( const QString &to )
{
QXmppNonSASLAuthTypesRequestIq authQuery;
authQuery.setTo(to);
authQuery.setUsername(getConfiguration().getUser());
sendPacket(authQuery);
}
void QXmppStream::parser(const QByteArray& data)
{
QDomDocument doc;
QByteArray completeXml;
m_dataBuffer = m_dataBuffer + data;
if(hasStartStreamElement(m_dataBuffer))
{
completeXml = m_dataBuffer + streamRootElementEnd;
}
else if(hasEndStreamElement(data))
{
completeXml = streamRootElementStart + m_dataBuffer;
}
else
{
completeXml = streamRootElementStart + m_dataBuffer + streamRootElementEnd;
}
if(doc.setContent(completeXml, true))
{
log("SERVER:" + m_dataBuffer);
flushDataBuffer();
QDomElement nodeRecv = doc.documentElement().firstChildElement();
if(nodeRecv.isNull())
{
QDomElement streamElement = doc.documentElement();
if(m_streamId.isEmpty())
m_streamId = streamElement.attribute("id");
if(m_XMPPVersion.isEmpty())
{
m_XMPPVersion = streamElement.attribute("version");
if(m_XMPPVersion.isEmpty())
{
// no version specified, signals XMPP Version < 1.0.
// switch to old auth mechanism
sendNonSASLAuthQuery(doc.documentElement().attribute("from"));
}
}
}
else
{
//TODO: Make a login error here.
}
while(!nodeRecv.isNull())
{
QString ns = nodeRecv.namespaceURI();
log("Namespace: " + ns + " Tag: " + nodeRecv.tagName() );
if(ns == ns_stream && nodeRecv.tagName() == "features")
{
bool nonSaslAvailable = nodeRecv.firstChildElement("auth").
namespaceURI() == ns_authFeature;
bool saslAvailable = nodeRecv.firstChildElement("mechanisms").
namespaceURI() == ns_sasl;
bool useSasl = getConfiguration().getUseSASLAuthentication();
if(nodeRecv.firstChildElement("starttls").namespaceURI()
== ns_tls && !m_socket.isEncrypted())
{
if(nodeRecv.firstChildElement("starttls").
firstChildElement().tagName() == "required")
{
// TLS is must from the server side
sendStartTls();
return;
}
else
{
// TLS is optional from the server side
switch(getConfiguration().getStreamSecurityMode())
{
case QXmppConfiguration::TLSEnabled:
case QXmppConfiguration::TLSRequired:
sendStartTls();
return;
case QXmppConfiguration::TLSDisabled:
break;
}
}
}
else if(!m_socket.isEncrypted()) // TLS not supported by server
{
if(getConfiguration().getStreamSecurityMode() ==
QXmppConfiguration::TLSRequired)
{
// disconnect as the for client TLS is compulsory but
// not available on the server
//
log(QString("Disconnecting as TLS not available at the server"));
disconnect();
return;
}
}
if((saslAvailable && nonSaslAvailable && !useSasl) ||
(!saslAvailable && nonSaslAvailable))
{
sendNonSASLAuthQuery(doc.documentElement().attribute("from"));
}
else if(saslAvailable)
{
// SASL Authentication
QDomElement element = nodeRecv.firstChildElement("mechanisms");
log(QString("Mechanisms:"));
QDomElement subElement = element.firstChildElement();
QStringList mechanisms;
while(!subElement.isNull())
{
if(subElement.tagName() == "mechanism")
{
log(subElement.text());
mechanisms << subElement.text();
}
subElement = subElement.nextSiblingElement();
}
switch(getConfiguration().getSASLAuthMechanism())
{
case QXmppConfiguration::SASLPlain:
if(mechanisms.contains("PLAIN"))
{
sendAuthPlain();
break;
}
case QXmppConfiguration::SASLDigestMD5:
if(mechanisms.contains("DIGEST-MD5"))
{
sendAuthDigestMD5();
break;
}
default:
log(QString("Desired SASL Auth mechanism not available trying the available ones"));
if(mechanisms.contains("DIGEST-MD5"))
sendAuthDigestMD5();
else if(mechanisms.contains("PLAIN"))
sendAuthPlain();
else
{
log(QString("SASL Auth mechanism not available"));
disconnect();
return;
}
break;
}
}
if(nodeRecv.firstChildElement("bind").
namespaceURI() == ns_bind)
{
sendBindIQ();
}
if(nodeRecv.firstChildElement("session").
namespaceURI() == ns_session)
{
m_sessionAvaliable = true;
}
}
else if(ns == ns_tls)
{
if(nodeRecv.tagName() == "proceed")
{
log(QString("Starting encryption"));
m_socket.startClientEncryption();
return;
}
}
else if(ns == ns_sasl)
{
if(nodeRecv.tagName() == "success")
{
log(QString("Authenticated"));
sendStartStream();
}
else if(nodeRecv.tagName() == "challenge")
{
sendAuthDigestMD5Response(nodeRecv.text());
}
}
else if(ns == ns_client)
{
if(nodeRecv.tagName() == "iq")
{
QDomElement element = nodeRecv.firstChildElement();
QString id = nodeRecv.attribute("id");
QString to = nodeRecv.attribute("to");
QString from = nodeRecv.attribute("from");
QString type = nodeRecv.attribute("type");
if(type.isEmpty())
qWarning("QXmppStream: iq type can't be empty");
QXmppIq iqPacket; // to emit
QDomElement elemen = nodeRecv.firstChildElement("error");
QXmppStanza::Error error = parseStanzaError(elemen);
if( QXmppIbbOpenIq::isIbbOpenIq( nodeRecv ) )
{
QXmppIbbOpenIq openIqPacket;
openIqPacket.parse( nodeRecv );
QXmppIbbTransferJob *mgr = m_client->getIbbTransferManager()->
getIbbTransferJob(openIqPacket.getId());
mgr->gotOpen( openIqPacket );
}
else if( QXmppIbbErrorIq::isIbbErrorIq( nodeRecv ) &&
m_client->getIbbTransferManager()->isIbbTransferJobId( id ))
{
QXmppIbbErrorIq errorIqPacket;
errorIqPacket.parse(nodeRecv);
QXmppIbbTransferJob *mgr = m_client->getIbbTransferManager()->
getIbbTransferJob(errorIqPacket.getId());
mgr->gotError( errorIqPacket );
}
else if( QXmppIbbAckIq::isIbbAckIq( nodeRecv ) &&
m_client->getIbbTransferManager()->isIbbTransferJobId( id ))
{
QXmppIbbAckIq ackIqPacket;
ackIqPacket.parse(nodeRecv);
QXmppIbbTransferJob *mgr = m_client->getIbbTransferManager()->getIbbTransferJob(ackIqPacket.getId());
mgr->gotAck();
}
else if( QXmppDataIq::isDataIq( nodeRecv ) &&
m_client->getIbbTransferManager()->isIbbTransferJobId( id ))
{
QXmppDataIq dataIqPacket;
dataIqPacket.parse(nodeRecv);
QXmppIbbTransferJob *mgr = m_client->getIbbTransferManager()->getIbbTransferJob(dataIqPacket.getId());
mgr->gotData(dataIqPacket);
}
else if( QXmppIbbCloseIq::isIbbCloseIq( nodeRecv ) &&
m_client->getIbbTransferManager()->isIbbTransferJobId( id ))
{
QXmppIbbCloseIq closeIqPacket;
closeIqPacket.parse(nodeRecv);
QXmppIbbTransferJob *mgr = m_client->getIbbTransferManager()->getIbbTransferJob(closeIqPacket.getId());
mgr->gotClose(closeIqPacket);
}
else if(id == m_sessionId)
{
// get back add configuration whether to send
// roster and intial presence in beginning
// process SessionIq
// xmpp connection made
emit xmppConnected();
sendRosterRequest();
sendInitialPresence();
QXmppBind session(type);
session.setId(id);
session.setTo(to);
session.setFrom(from);
iqPacket = session;
}
else if(id == m_bindId)
{
QXmppBind bind(type);
QString jid = nodeRecv.firstChildElement("bind").
firstChildElement("jid").text();
bind.setResource(jidToResource(jid));
bind.setJid(jidToBareJid(jid));
bind.setId(id);
bind.setTo(to);
bind.setFrom(from);
processBindIq(bind);
iqPacket = bind;
}
else if(nodeRecv.firstChildElement("query").
namespaceURI() == ns_roster)
{
QDomElement itemElement = nodeRecv.
firstChildElement("query").
firstChildElement("item");
QXmppRosterIq rosterIq(nodeRecv.attribute("type"));
rosterIq.setId(id);
rosterIq.setTo(to);
rosterIq.setFrom(from);
while(!itemElement.isNull())
{
QXmppRosterIq::Item item;
item.setName(itemElement.attribute("name"));
item.setBareJid(itemElement.attribute("jid"));
item.setSubscriptionTypeFromStr(
itemElement.attribute("subscription"));
item.setSubscriptionStatus(
itemElement.attribute("ask"));
item.addGroup(
itemElement.firstChildElement("group").firstChildElement().text());
rosterIq.addItem(item);
itemElement = itemElement.nextSiblingElement();
}
processRosterIq(rosterIq);
iqPacket = rosterIq;
}
// extensions
// vCard - XEP-0054
// http://xmpp.org/extensions/xep-0054.html
else if(nodeRecv.firstChildElement("vCard").
namespaceURI() == ns_vcard)
{
QXmppVCard vcardIq;
vcardIq.parse(nodeRecv);
emit vCardIqReceived(vcardIq);
iqPacket = vcardIq;
}
// XEP-0030 info query
else if(nodeRecv.firstChildElement("query").
namespaceURI() == ns_disco_info &&
type == "get")
{
QXmppInformationRequestResult qxmppFeatures;
qxmppFeatures.setId(id);
qxmppFeatures.setTo(from);
qxmppFeatures.setFrom(to);
sendPacket(qxmppFeatures);
}
else if(id == m_nonSASLAuthId && type == "result")
{
// successful Non-SASL Authentication
log(QString("Authenticated (Non-SASL)"));
emit xmppConnected();
sendRosterRequest();
sendInitialPresence();
}
else if(nodeRecv.firstChildElement("query").
namespaceURI() == ns_auth)
{
if(type == "result")
{
bool digest = !nodeRecv.firstChildElement("query").
firstChildElement("digest").isNull();
bool plain = !nodeRecv.firstChildElement("query").
firstChildElement("password").isNull();
bool plainText = false;
if(plain && digest)
{
if(getConfiguration().getNonSASLAuthMechanism() ==
QXmppConfiguration::NonSASLDigest)
plainText = false;
else
plainText = true;
}
else if(plain)
plainText = true;
else if(digest)
plainText = false;
else
{
//TODO Login error
return;
}
sendNonSASLAuth(plainText);
}
}
else // didn't understant the iq...reply with error
{
if(type != "result") // but not incase of result iqs
{
QXmppIq iq(QXmppIq::Error);
iq.setId(id);
iq.setTo(from);
iq.setFrom(to);
QXmppStanza::Error error(QXmppStanza::Error::Cancel,
QXmppStanza::Error::FeatureNotImplemented);
iq.setError(error);
sendPacket(iq);
}
}
iqPacket.setError(error);
processIq(iqPacket);
}
else if(nodeRecv.tagName() == "presence")
{
QXmppPresence presence;
presence.setTypeFromStr(nodeRecv.attribute("type"));
presence.setFrom(nodeRecv.attribute("from"));
presence.setTo(nodeRecv.attribute("to"));
QString statusText = nodeRecv.
firstChildElement("status").text();
QString show = nodeRecv.
firstChildElement("show").text();
int priority = nodeRecv.
firstChildElement("priority").text().toInt();
QXmppPresence::Status status;
status.setTypeFromStr(show);
status.setStatusText(statusText);
status.setPriority(priority);
presence.setStatus(status);
QDomElement errorElement = nodeRecv.
firstChildElement("error");
if(!errorElement.isNull())
{
QXmppStanza::Error error =
parseStanzaError(errorElement);
presence.setError(error);
}
processPresence(presence);
}
else if(nodeRecv.tagName() == "message")
{
QString from = nodeRecv.attribute("from");
QString to = nodeRecv.attribute("to");
QString type = nodeRecv.attribute("type");
QString body = unescapeString(
nodeRecv.firstChildElement("body").text());
QString sub = unescapeString(
nodeRecv.firstChildElement("subject").text());
QString thread = nodeRecv.firstChildElement("thread").text();
QXmppMessage message(from, to, body, thread);
message.setSubject(sub);
message.setTypeFromStr(type);
QDomElement errorElement = nodeRecv.
firstChildElement("error");
if(!errorElement.isNull())
{
QXmppStanza::Error error = parseStanzaError(errorElement);
message.setError(error);
}
processMessage(message);
}
}
nodeRecv = nodeRecv.nextSiblingElement();
}
}
else
{
//wait for complete packet
}
}
void QXmppStream::sendStartStream()
{
QByteArray data = "<?xml version='1.0'?><stream:stream to='";
data.append(getConfiguration().getDomain());
data.append("' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
sendToServer(data);
}
void QXmppStream::sendToServer(const QByteArray& packet)
{
log("CLIENT: " + packet);
m_socket.write( packet );
}
bool QXmppStream::hasStartStreamElement(const QByteArray& data)
{
QString str(data);
QRegExp regex("(<\\?xml.*\\?>)?\\s*<stream:stream.*>");
regex.setMinimal(true);
if(str.contains(regex))
return true;
else
return false;
}
bool QXmppStream::hasEndStreamElement(const QByteArray& data)
{
QString str(data);
QRegExp regex("</stream:stream>");
regex.setMinimal(true);
if(str.contains(regex))
return true;
else
return false;
}
void QXmppStream::sendStartTls()
{
sendToServer("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
}
void QXmppStream::sendNonSASLAuth(bool plainText)
{
QXmppNonSASLAuthIq authQuery;
authQuery.setUsername(getConfiguration().getUser());
authQuery.setPassword(getConfiguration().getPasswd());
authQuery.setResource(getConfiguration().getResource());
authQuery.setStreamId(m_streamId);
authQuery.setUsePlainText(plainText);
m_nonSASLAuthId = authQuery.getId();
sendPacket(authQuery);
}
void QXmppStream::sendAuthPlain()
{
QByteArray data = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>";
QString userPass('\0' + getConfiguration().getUser() +
'\0' + getConfiguration().getPasswd());
data += userPass.toUtf8().toBase64();
data += "</auth>";
sendToServer(data);
}
void QXmppStream::sendAuthDigestMD5()
{
QByteArray packet = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>";
sendToServer(packet);
}
// challenge is BASE64 encoded string
void QXmppStream::sendAuthDigestMD5Response(const QString& challenge)
{
QByteArray ba = QByteArray::fromBase64(challenge.toUtf8());
//log(ba);
ba.replace('"', QString(""));
QList<QByteArray> list = ba.split(',');
QMap<QByteArray, QByteArray> map;
QList<QByteArray> list2;
for(int i = 0; i < list.count(); ++i)
{
list2 = list.at(i).split('=');
if(list2.count() == 2)
map[list2.at(0).trimmed()] = list2.at(1).trimmed();
else
log(QString("Invalid challenge send"));
}
QByteArray user = getConfiguration().getUser().toUtf8();
QByteArray passwd = getConfiguration().getPasswd().toUtf8();
QByteArray domain = getConfiguration().getDomain().toUtf8();
QByteArray realm;
if(map.contains("realm"))
realm = map["realm"];
QByteArray response;
// First challenge
if(map.contains("nonce"))
{
QByteArray cnonce(32, 'm');
for(int n = 0; n < cnonce.size(); ++n)
cnonce[n] = (char)(256.0*qrand()/(RAND_MAX+1.0));
QByteArray nc = "00000001";
QByteArray digest_uri = "xmpp/" + domain;
QByteArray a1 = user + ':' + realm + ':' + passwd;
QByteArray ha1 = QCryptographicHash::hash(a1, QCryptographicHash::Md5);
ha1 += ':' + map["nonce"] + ':' + cnonce;
if(map.contains("authzid"))
ha1 += ':' + map["authzid"];
QByteArray A1(ha1);
QByteArray A2 = "AUTHENTICATE:" + digest_uri;
QByteArray HA1 = QCryptographicHash::hash(A1, QCryptographicHash::Md5).toHex();
QByteArray HA2 = QCryptographicHash::hash(A2, QCryptographicHash::Md5).toHex();
QByteArray KD = HA1 + ':' + map["nonce"] + ':' + nc + ':' + cnonce + ':'
+ "auth" + ':' + HA2;
QByteArray Z = QCryptographicHash::hash(KD, QCryptographicHash::Md5).toHex();
response += "username=\"" + user + "\",";
if(!realm.isEmpty())
response += "realm=\"" + realm + "\",";
response += "nonce=\"" + map["nonce"] + "\",";
response += "cnonce=\"" + cnonce + "\",";
response += "nc=" + nc + ",";
response += "qop=auth,";
response += "digest-uri=\"" + digest_uri + "\",";
response += "response=" + Z + ",";
if(map.contains("authzid"))
response += "authzid=\"" + map["authzid"] + "\",";
response += "charset=utf-8";
log(response);
QByteArray packet = "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
+ response.toBase64() + "</response>";
sendToServer(packet);
}
else if(map.contains("rspauth"))
{
sendToServer("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>");
}
else
{
disconnect();
log(QString("sendAuthDigestMD5Response: Invalid input"));
}
}
void QXmppStream::sendBindIQ()
{
QXmppBind bind(QXmppIq::Set);
bind.setResource(getConfiguration().getResource());
m_bindId = bind.getId();
sendPacket(bind);
}
void QXmppStream::sendSessionIQ()
{
QXmppSession session(QXmppIq::Set);
session.setTo(getConfiguration().getDomain());
m_sessionId = session.getId();
sendPacket(session);
}
void QXmppStream::sendInitialPresence()
{
if(m_client)
sendPacket(m_client->getClientPresence());
}
void QXmppStream::acceptSubscriptionRequest(const QString& from, bool accept)
{
QXmppPresence presence;
presence.setTo(from);
if(accept)
presence.setType(QXmppPresence::Subscribed);
else
presence.setType(QXmppPresence::Unsubscribed);
sendPacket(presence);
}
void QXmppStream::sendSubscriptionRequest(const QString& to)
{
if(to.isEmpty())
return;
QXmppPresence presence;
presence.setTo(to);
presence.setType(QXmppPresence::Subscribe);
sendPacket(presence);
}
void QXmppStream::sendRosterRequest()
{
QXmppRosterIq roster(QXmppIq::Get);
roster.setFrom(getConfiguration().getJid());
m_rosterReqId = roster.getId();
sendPacket(roster);
}
void QXmppStream::disconnect()
{
sendEndStream();
m_socket.disconnectFromHost();
}
QXmppRoster& QXmppStream::getRoster()
{
return m_roster;
}
void QXmppStream::sendPacket(const QXmppPacket& packet)
{
if(QXmppLogger::getLogger()->getLoggingType() != QXmppLogger::NONE)
{
QByteArray logPacket;
QXmlStreamWriter xmlStreamLog(&logPacket);
packet.toXml(&xmlStreamLog);
log("CLIENT: "+ logPacket);
}
QXmlStreamWriter xmlStream(&m_socket);
packet.toXml(&xmlStream);
}
void QXmppStream::processPresence(const QXmppPresence& presence)
{
switch(presence.getType())
{
case QXmppPresence::Error:
break;
case QXmppPresence::Available:
break;
case QXmppPresence::Unavailable:
break;
case QXmppPresence::Subscribe:
if(!presence.getFrom().isEmpty())
{
if(getConfiguration().getAutoAcceptSubscriptions())
acceptSubscriptionRequest(presence.getFrom());
emit subscriptionRequestReceived(presence.getFrom());
}
break;
case QXmppPresence::Unsubscribe:
break;
case QXmppPresence::Unsubscribed:
break;
case QXmppPresence::Probe:
break;
default:
break;
}
emit presenceReceived(presence);
}
void QXmppStream::processMessage(const QXmppMessage& message)
{
emit messageReceived(message);
}
void QXmppStream::processIq(const QXmppIq& iq)
{
emit iqReceived(iq);
}
void QXmppStream::sendEndStream()
{
sendToServer(streamRootElementEnd);
}
void QXmppStream::processBindIq(const QXmppBind& bind)
{
switch(bind.getType())
{
case QXmppIq::Result:
if(!bind.getResource().isEmpty())
getConfiguration().setResource(bind.getResource());
if(m_sessionAvaliable)
sendSessionIQ();
break;
default:
break;
}
}
void QXmppStream::processRosterIq(const QXmppRosterIq& rosterIq)
{
if(m_rosterReqId == rosterIq.getId())
emit rosterRequestIqReceived(rosterIq);
else
emit rosterIqReceived(rosterIq);
switch(rosterIq.getType())
{
case QXmppIq::Set:
// when contact subscribes user...user sends 'subscribed' presence
// then after recieving following iq user requests contact for subscription
// check thet "from" is newly added in the roster...and remove this ask thing...and do this for all items
if(rosterIq.getItems().at(0).getSubscriptionType() ==
QXmppRosterIq::Item::From && rosterIq.getItems().at(0).
getSubscriptionStatus().isEmpty())
sendSubscriptionRequest(rosterIq.getItems().at(0).getBareJid());
break;
default:
break;
}
}
QXmppStanza::Error QXmppStream::parseStanzaError(QDomElement & errorElement)
{
QXmppStanza::Error error;
if(errorElement.isNull())
return error;
QString type = errorElement.attribute("type");
QString text;
QString cond;
QDomElement element = errorElement.firstChildElement();
while(!element.isNull())
{
if(element.tagName() == "text")
text = element.text();
else if(element.namespaceURI() == ns_stanza)
{
cond = element.tagName();
}
element = element.nextSiblingElement();
}
error.setConditionFromStr(cond);
error.setTypeFromStr(type);
error.setText(text);
return error;
}
QAbstractSocket::SocketError QXmppStream::getSocketError()
{
return m_socketError;
}
QXmppVCardManager& QXmppStream::getVCardManager()
{
return m_vCardManager;
}
void QXmppStream::flushDataBuffer()
{
m_dataBuffer.clear();
}
|