source: psi/trunk/iris/example/xmpptest/xmpptest.cpp

Last change on this file was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 22.6 KB
Line 
1#include<qapplication.h>
2#include<qtextedit.h>
3#include<qgroupbox.h>
4#include<qlineedit.h>
5#include<qlabel.h>
6#include<qcheckbox.h>
7#include<qtextedit.h>
8#include<qcombobox.h>
9#include<qpushbutton.h>
10#include<qmessagebox.h>
11#include<qinputdialog.h>
12#include<qspinbox.h>
13#include<qtimer.h>
14#include<qmenubar.h>
15#include<qpopupmenu.h>
16#include<qtabwidget.h>
17#include<qca.h>
18//#include<iris/xmpp.h>
19#include"xmpp.h"
20#include"im.h"
21
22#include<stdlib.h>
23#include<time.h>
24
25#include"ui_test.h"
26
27#include<stdio.h>
28
29#define AppName "xmpptest"
30
31static QString plain2rich(const QString &plain)
32{
33 QString rich;
34 int col = 0;
35
36 for(int i = 0; i < (int)plain.length(); ++i) {
37 if(plain[i] == '\n') {
38 rich += "<br>";
39 col = 0;
40 }
41 else if(plain[i] == '\t') {
42 rich += QChar::nbsp;
43 while(col % 4) {
44 rich += QChar::nbsp;
45 ++col;
46 }
47 }
48 else if(plain[i].isSpace()) {
49 if(i > 0 && plain[i-1] == ' ')
50 rich += QChar::nbsp;
51 else
52 rich += ' ';
53 }
54 else if(plain[i] == '<')
55 rich += "&lt;";
56 else if(plain[i] == '>')
57 rich += "&gt;";
58 else if(plain[i] == '\"')
59 rich += "&quot;";
60 else if(plain[i] == '\'')
61 rich += "&apos;";
62 else if(plain[i] == '&')
63 rich += "&amp;";
64 else
65 rich += plain[i];
66 ++col;
67 }
68
69 return rich;
70}
71
72/*static void showCertInfo(const QCA::Cert &cert)
73{
74 fprintf(stderr, "-- Cert --\n");
75 fprintf(stderr, " CN: %s\n", cert.subject()["CN"].latin1());
76 fprintf(stderr, " Valid from: %s, until %s\n",
77 cert.notBefore().toString().latin1(),
78 cert.notAfter().toString().latin1());
79 fprintf(stderr, " PEM:\n%s\n", cert.toPEM().latin1());
80}*/
81
82static QString resultToString(int result)
83{
84 QString s;
85 switch(result) {
86 case QCA::TLS::NoCert:
87 s = QObject::tr("No certificate presented.");
88 break;
89 case QCA::TLS::Valid:
90 break;
91 case QCA::TLS::HostMismatch:
92 s = QObject::tr("Hostname mismatch.");
93 break;
94 case QCA::TLS::Rejected:
95 s = QObject::tr("Root CA rejects the specified purpose.");
96 break;
97 case QCA::TLS::Untrusted:
98 s = QObject::tr("Not trusted for the specified purpose.");
99 break;
100 case QCA::TLS::SignatureFailed:
101 s = QObject::tr("Invalid signature.");
102 break;
103 case QCA::TLS::InvalidCA:
104 s = QObject::tr("Invalid CA certificate.");
105 break;
106 case QCA::TLS::InvalidPurpose:
107 s = QObject::tr("Invalid certificate purpose.");
108 break;
109 case QCA::TLS::SelfSigned:
110 s = QObject::tr("Certificate is self-signed.");
111 break;
112 case QCA::TLS::Revoked:
113 s = QObject::tr("Certificate has been revoked.");
114 break;
115 case QCA::TLS::PathLengthExceeded:
116 s = QObject::tr("Maximum cert chain length exceeded.");
117 break;
118 case QCA::TLS::Expired:
119 s = QObject::tr("Certificate has expired.");
120 break;
121 case QCA::TLS::Unknown:
122 default:
123 s = QObject::tr("General validation error.");
124 break;
125 }
126 return s;
127}
128
129class TestDebug : public XMPP::Debug
130{
131public:
132 void msg(const QString &);
133 void outgoingTag(const QString &);
134 void incomingTag(const QString &);
135 void outgoingXml(const QDomElement &);
136 void incomingXml(const QDomElement &);
137};
138
139class TestDlg : public TestUI
140{
141 Q_OBJECT
142public:
143 bool active, connected;
144 XMPP::AdvancedConnector *conn;
145 QCA::TLS *tls;
146 XMPP::QCATLSHandler *tlsHandler;
147 XMPP::ClientStream *stream;
148 XMPP::Jid jid;
149
150 TestDlg(QWidget *parent=0, const char *name=0) : TestUI(parent, name)
151 {
152 setCaption(tr("XMPP Test"));
153
154 connect(ck_probe, SIGNAL(toggled(bool)), SLOT(probe_toggled(bool)));
155 connect(cb_proxy, SIGNAL(activated(int)), SLOT(proxy_activated(int)));
156 connect(pb_go, SIGNAL(clicked()), SLOT(go()));
157 connect(pb_send, SIGNAL(clicked()), SLOT(send()));
158 connect(pb_im, SIGNAL(clicked()), SLOT(sc_im()));
159 connect(pb_msg, SIGNAL(clicked()), SLOT(sc_msg()));
160 connect(pb_iqv, SIGNAL(clicked()), SLOT(sc_iqv()));
161 connect(pb_about, SIGNAL(clicked()), SLOT(about()));
162
163 sb_ssfmin->setMinValue(0);
164 sb_ssfmin->setMaxValue(256);
165 sb_ssfmax->setMinValue(0);
166 sb_ssfmax->setMaxValue(256);
167
168 pb_send->setEnabled(false);
169 proxy_activated(0);
170 ck_probe->setChecked(true);
171 ck_mutual->setChecked(true);
172 pb_go->setText(tr("&Connect"));
173
174 //le_jid->setText("psitest@jabberd.jabberstudio.org/Test");
175 //ck_probe->setChecked(false);
176 //le_host->setText("jabberd.jabberstudio.org:15222");
177 //ck_mutual->setChecked(false);
178 //le_jid->setText("sasltest@e.jabber.ru/Test");
179 //le_jid->setText("psitest@jabber.cz/Test");
180 //le_pass->setText("psitest");
181 //cb_proxy->setCurrentItem(3);
182 //le_proxyurl->setText("http://connect.jabber.cz/");
183
184 // setup xmpp
185 conn = new XMPP::AdvancedConnector;
186 connect(conn, SIGNAL(srvLookup(const QString &)), SLOT(conn_srvLookup(const QString &)));
187 connect(conn, SIGNAL(srvResult(bool)), SLOT(conn_srvResult(bool)));
188 connect(conn, SIGNAL(httpSyncStarted()), SLOT(conn_httpSyncStarted()));
189 connect(conn, SIGNAL(httpSyncFinished()), SLOT(conn_httpSyncFinished()));
190
191 if(QCA::isSupported(QCA::CAP_TLS)) {
192 tls = new QCA::TLS;
193 tlsHandler = new XMPP::QCATLSHandler(tls);
194 connect(tlsHandler, SIGNAL(tlsHandshaken()), SLOT(tls_handshaken()));
195 }
196 else {
197 tls = 0;
198 tlsHandler = 0;
199 }
200
201 stream = new XMPP::ClientStream(conn, tlsHandler);
202 connect(stream, SIGNAL(connected()), SLOT(cs_connected()));
203 connect(stream, SIGNAL(securityLayerActivated(int)), SLOT(cs_securityLayerActivated(int)));
204 connect(stream, SIGNAL(needAuthParams(bool, bool, bool)), SLOT(cs_needAuthParams(bool, bool, bool)));
205 connect(stream, SIGNAL(authenticated()), SLOT(cs_authenticated()));
206 connect(stream, SIGNAL(connectionClosed()), SLOT(cs_connectionClosed()));
207 connect(stream, SIGNAL(delayedCloseFinished()), SLOT(cs_delayedCloseFinished()));
208 connect(stream, SIGNAL(readyRead()), SLOT(cs_readyRead()));
209 connect(stream, SIGNAL(stanzaWritten()), SLOT(cs_stanzaWritten()));
210 connect(stream, SIGNAL(warning(int)), SLOT(cs_warning(int)));
211 connect(stream, SIGNAL(error(int)), SLOT(cs_error(int)));
212
213 QTimer::singleShot(0, this, SLOT(adjustLayout()));
214
215 le_jid->setFocus();
216 active = false;
217 connected = false;
218 }
219
220 ~TestDlg()
221 {
222 delete stream;
223 delete tls; // this destroys the TLSHandler also
224 delete conn;
225 }
226
227private slots:
228 void adjustLayout()
229 {
230 tb_main->setFixedWidth(tb_main->minimumSizeHint().width());
231 resize(minimumSizeHint());
232 show();
233 }
234
235 void about()
236 {
237 QMessageBox::about(this, tr("About %1").arg(AppName), tr(
238 "%1 v1.0\n"
239 "\n"
240 "Utility to demonstrate the Iris XMPP library.\n"
241 "\n"
242 "Currently supports:\n"
243 " draft-ietf-xmpp-core-21\n"
244 " JEP-0025\n"
245 "\n"
246 "Copyright (C) 2003 Justin Karneges").arg(AppName));
247 }
248
249 void probe_toggled(bool)
250 {
251 setHostState();
252 }
253
254 void proxy_activated(int x)
255 {
256 bool ok = (x != 0);
257 bool okpoll = (x == 3);
258 gb_proxy->setEnabled(ok);
259 lb_proxyurl->setEnabled(okpoll);
260 le_proxyurl->setEnabled(okpoll);
261 ck_probe->setEnabled(!okpoll);
262 setHostState();
263 }
264
265 void cleanup()
266 {
267 pb_send->setEnabled(false);
268 pb_go->setEnabled(true);
269 pb_go->setText(tr("&Connect"));
270 pb_go->setFocus();
271 gb_server->setEnabled(true);
272 active = false;
273 connected = false;
274 }
275
276 void start()
277 {
278 if(active)
279 return;
280
281 jid = XMPP::Jid(le_jid->text());
282 if(jid.domain().isEmpty() || jid.node().isEmpty() || jid.resource().isEmpty()) {
283 QMessageBox::information(this, tr("Error"), tr("Please enter the Full JID to connect with."));
284 return;
285 }
286
287 int p = cb_proxy->currentItem();
288 XMPP::AdvancedConnector::Proxy proxy;
289 if(p > 0) {
290 QString s = le_proxyhost->text();
291 QString url = le_proxyurl->text();
292 if(p != 3 && s.isEmpty()) {
293 QMessageBox::information(this, tr("Error"), tr("You must specify a host:port for the proxy."));
294 return;
295 }
296 if(p == 3 && s.isEmpty() && url.isEmpty()) {
297 QMessageBox::information(this, tr("Error"), tr("You must at least enter a URL to use http poll."));
298 return;
299 }
300 QString host;
301 int port = 0;
302 if(!s.isEmpty()) {
303 int n = s.find(':');
304 if(n == -1) {
305 QMessageBox::information(this, tr("Error"), tr("Please enter the proxy host in the form 'host:port'."));
306 return;
307 }
308 host = s.mid(0, n);
309 port = s.mid(n+1).toInt();
310 }
311 if(p == 1)
312 proxy.setHttpConnect(host, port);
313 else if(p == 2)
314 proxy.setSocks(host, port);
315 else if(p == 3) {
316 proxy.setHttpPoll(host, port, url);
317 proxy.setPollInterval(2); // fast during login
318 }
319 proxy.setUserPass(le_proxyuser->text(), le_proxypass->text());
320 }
321 bool probe = (p != 3 && ck_probe->isChecked());
322 bool useHost = (!probe && !le_host->text().isEmpty());
323 QString host;
324 int port = 0;
325 bool ssl = false;
326 if(useHost) {
327 QString s = le_host->text();
328 int n = s.find(':');
329 if(n == -1) {
330 QMessageBox::information(this, tr("Error"), tr("Please enter the host in the form 'host:port'."));
331 return;
332 }
333 host = s.mid(0, n);
334 port = s.mid(n+1).toInt();
335
336 if(ck_ssl->isChecked())
337 ssl = true;
338 }
339 if(sb_ssfmin->value() > sb_ssfmax->value()) {
340 QMessageBox::information(this, tr("Error"), tr("Error: SSF Min is greater than SSF Max."));
341 return;
342 }
343
344 if((probe || ssl) && !tls) {
345 QMessageBox::information(this, tr("Error"), tr("Error: TLS not available. Disable any TLS options."));
346 return;
347 }
348
349 // prepare
350 conn->setProxy(proxy);
351 if(useHost)
352 conn->setOptHostPort(host, port);
353 else
354 conn->setOptHostPort("", 0);
355 conn->setOptProbe(probe);
356 conn->setOptSSL(ssl);
357
358 if(tls) {
359 QPtrList<QCA::Cert> certStore;
360 tls->setCertificateStore(certStore);
361 }
362
363 stream->setNoopTime(55000); // every 55 seconds
364 stream->setAllowPlain(ck_plain->isChecked());
365 stream->setRequireMutualAuth(ck_mutual->isChecked());
366 stream->setSSFRange(sb_ssfmin->value(), sb_ssfmax->value());
367
368 gb_server->setEnabled(false);
369 pb_go->setText(tr("&Disconnect"));
370 pb_go->setFocus();
371 active = true;
372
373 appendSysMsg("Connecting...");
374 stream->connectToServer(jid);
375 }
376
377 void stop()
378 {
379 if(!active)
380 return;
381
382 if(connected) {
383 pb_go->setEnabled(false);
384 appendSysMsg("Disconnecting...");
385 stream->close();
386 }
387 else {
388 stream->close();
389 appendSysMsg("Disconnected");
390 cleanup();
391 }
392 }
393
394 void go()
395 {
396 if(active)
397 stop();
398 else
399 start();
400 }
401
402 void send()
403 {
404 if(te_input->text().isEmpty())
405 return;
406
407 // construct a "temporary" document to parse the input
408 QString str = "<stream xmlns=\"jabber:client\">\n";
409 str += te_input->text() + '\n';
410 str += "</stream>";
411
412 QDomDocument doc;
413 QString errMsg;
414 int errLine, errCol;
415 if(!doc.setContent(str, true, &errMsg, &errLine, &errCol)) {
416 int lines = QStringList::split('\n', str, true).count();
417 --errLine; // skip the first line
418 if(errLine == lines-1) {
419 errLine = lines-2;
420 errCol = te_input->paragraphLength(errLine-1)+1;
421 errMsg = "incomplete input";
422 }
423 te_input->setCursorPosition(errLine-1, errCol-1);
424 QMessageBox::information(this, tr("Error"), tr("Bad XML input (%1,%2): %3\nPlease correct and try again.").arg(errCol).arg(errLine).arg(errMsg));
425 return;
426 }
427 QDomElement e = doc.firstChild().toElement();
428
429 int num = 0;
430 QDomNodeList nl = e.childNodes();
431 QValueList<XMPP::Stanza> stanzaList;
432 for(uint x = 0; x < nl.count(); ++x) {
433 QDomNode n = nl.item(x);
434 if(n.isElement()) {
435 QDomElement e = n.toElement();
436 XMPP::Stanza s = stream->createStanza(e);
437 if(s.isNull()) {
438 QMessageBox::information(this, tr("Error"), tr("Bad Stanza '%1'. Must be 'message', 'presence', or 'iq'").arg(e.tagName()));
439 return;
440 }
441 stanzaList += s;
442 ++num;
443 }
444 }
445 if(num == 0) {
446 QMessageBox::information(this, tr("Error"), tr("You must enter at least one stanza!"));
447 return;
448 }
449
450 // out the door
451 for(QValueList<XMPP::Stanza>::ConstIterator it = stanzaList.begin(); it != stanzaList.end(); ++it) {
452 appendXmlOut(XMPP::Stream::xmlToString((*it).element(), true));
453 stream->write(*it);
454 }
455
456 te_input->setText("");
457 }
458
459 void sc_im()
460 {
461 /*XMPP::Message m("justin@andbit.net/Psi");
462 m.setSubject("Hi");
463 m.setBody("I send you this in order to have your advice.");
464 m.setBody("Escucha lechuga!", "es");
465 XMPP::Stanza stanza = m.toStanza(stream);
466 QString str = stanza.toString();
467 printf("[%s]\n", str.latin1());
468
469 XMPP::Message n;
470 n.fromStanza(stanza);
471 printf("subject: [%s]\n", n.subject().latin1());
472 printf("body: [%s]\n", n.body().latin1());
473 printf("body-es: [%s]\n", n.body("es").latin1());*/
474
475 QString s;
476 s += "<iq type='set' id='sess_1'>\n";
477 s += " <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>\n";
478 s += "</iq>";
479 te_input->setText(s);
480 te_input->setFocus();
481 }
482
483 void sc_msg()
484 {
485 QString to = le_to->text();
486 QString s;
487 if(!to.isEmpty())
488 s += QString("<message to=\"%1\">\n").arg(to);
489 else
490 s += QString("<message to=\"\">\n");
491 s += " <body>hello world</body>\n";
492 s += "</message>";
493 te_input->setText(s);
494 if(!to.isEmpty()) {
495 te_input->setCursorPosition(1, 7);
496 te_input->setSelection(1, 7, 1, 18);
497 }
498 else
499 te_input->setCursorPosition(0, 13);
500 te_input->setFocus();
501 }
502
503 void sc_iqv()
504 {
505 QString to = le_to->text();
506 QString s;
507 if(!to.isEmpty())
508 s += QString("<iq to=\"%1\" type=\"get\" id=\"abcde\">\n").arg(to);
509 else
510 s += QString("<iq to=\"\" type=\"get\" id=\"abcde\">\n");
511 s += " <query xmlns=\"jabber:iq:version\"/>\n";
512 s += "</iq>";
513 te_input->setText(s);
514 if(!to.isEmpty()) {
515 te_input->setCursorPosition(0, 8);
516 te_input->setSelection(0, 8, 0, 8 + to.length());
517 }
518 else
519 te_input->setCursorPosition(0, 8);
520 te_input->setFocus();
521 }
522
523 void conn_srvLookup(const QString &server)
524 {
525 appendLibMsg(QString("SRV lookup on [%1]").arg(server));
526 }
527
528 void conn_srvResult(bool b)
529 {
530 if(b)
531 appendLibMsg("SRV lookup success!");
532 else
533 appendLibMsg("SRV lookup failed");
534 }
535
536 void conn_httpSyncStarted()
537 {
538 appendLibMsg("HttpPoll: syncing");
539 }
540
541 void conn_httpSyncFinished()
542 {
543 appendLibMsg("HttpPoll: done");
544 }
545
546 void tls_handshaken()
547 {
548 QCA::Cert cert = tls->peerCertificate();
549 int vr = tls->certificateValidityResult();
550
551 appendSysMsg("Successful TLS handshake.");
552 if(vr == QCA::TLS::Valid)
553 appendSysMsg("Valid certificate.");
554 else {
555 appendSysMsg(QString("Invalid certificate: %1").arg(resultToString(vr)), Qt::red);
556 appendSysMsg("Continuing anyway");
557 }
558
559 tlsHandler->continueAfterHandshake();
560 }
561
562 void cs_connected()
563 {
564 QString s = "Connected";
565 if(conn->havePeerAddress())
566 s += QString(" (%1:%2)").arg(conn->peerAddress().toString()).arg(conn->peerPort());
567 if(conn->useSSL())
568 s += " [ssl]";
569 appendSysMsg(s);
570 }
571
572 void cs_securityLayerActivated(int type)
573 {
574 appendSysMsg(QString("Security layer activated (%1)").arg((type == XMPP::ClientStream::LayerTLS) ? "TLS": "SASL"));
575 }
576
577 void cs_needAuthParams(bool user, bool pass, bool realm)
578 {
579 QString s = "Need auth parameters -";
580 if(user)
581 s += " (Username)";
582 if(pass)
583 s += " (Password)";
584 if(realm)
585 s += " (Realm)";
586 appendSysMsg(s);
587
588 if(user) {
589 if(!le_user->text().isEmpty())
590 stream->setUsername(le_user->text());
591 else
592 stream->setUsername(jid.node());
593 }
594 if(pass) {
595 if(!le_pass->text().isEmpty())
596 stream->setPassword(le_pass->text());
597 else {
598 conn->changePollInterval(10); // slow down during prompt
599 bool ok;
600 QString s = QInputDialog::getText(tr("Password"), tr("Enter the password for %1").arg(jid.full()), QLineEdit::Password, QString::null, &ok, this);
601 if(!ok) {
602 stop();
603 return;
604 }
605 stream->setPassword(s);
606
607 conn->changePollInterval(2); // resume speed
608 }
609 }
610 if(realm)
611 stream->setRealm(jid.domain());
612
613 stream->continueAfterParams();
614 }
615
616 void cs_authenticated()
617 {
618 connected = true;
619 pb_send->setEnabled(true);
620 conn->changePollInterval(10); // slow down after login
621 appendSysMsg("Authenticated");
622 }
623
624 void cs_connectionClosed()
625 {
626 appendSysMsg("Disconnected by peer");
627 cleanup();
628 }
629
630 void cs_delayedCloseFinished()
631 {
632 appendSysMsg("Disconnected");
633 cleanup();
634 }
635
636 void cs_readyRead()
637 {
638 while(stream->stanzaAvailable()) {
639 XMPP::Stanza s = stream->read();
640 appendXmlIn(XMPP::Stream::xmlToString(s.element(), true));
641 }
642 }
643
644 void cs_stanzaWritten()
645 {
646 appendSysMsg("Stanza sent");
647 }
648
649 void cs_warning(int warn)
650 {
651 if(warn == XMPP::ClientStream::WarnOldVersion) {
652 appendSysMsg("Warning: pre-1.0 protocol server", Qt::red);
653 }
654 else if(warn == XMPP::ClientStream::WarnNoTLS) {
655 appendSysMsg("Warning: TLS not available!", Qt::red);
656 }
657 stream->continueAfterWarning();
658 }
659
660 void cs_error(int err)
661 {
662 if(err == XMPP::ClientStream::ErrParse) {
663 appendErrMsg("XML parsing error");
664 }
665 else if(err == XMPP::ClientStream::ErrProtocol) {
666 appendErrMsg("XMPP protocol error");
667 }
668 else if(err == XMPP::ClientStream::ErrStream) {
669 int x = stream->errorCondition();
670 QString s;
671 if(x == XMPP::Stream::GenericStreamError)
672 s = "generic stream error";
673 else if(x == XMPP::ClientStream::Conflict)
674 s = "conflict (remote login replacing this one)";
675 else if(x == XMPP::ClientStream::ConnectionTimeout)
676 s = "timed out from inactivity";
677 else if(x == XMPP::ClientStream::InternalServerError)
678 s = "internal server error";
679 else if(x == XMPP::ClientStream::InvalidFrom)
680 s = "invalid from address";
681 else if(x == XMPP::ClientStream::InvalidXml)
682 s = "invalid XML";
683 else if(x == XMPP::ClientStream::PolicyViolation)
684 s = "policy violation. go to jail!";
685 else if(x == XMPP::ClientStream::ResourceConstraint)
686 s = "server out of resources";
687 else if(x == XMPP::ClientStream::SystemShutdown)
688 s = "system is shutting down NOW";
689 appendErrMsg(QString("XMPP stream error: %1").arg(s));
690 }
691 else if(err == XMPP::ClientStream::ErrConnection) {
692 int x = conn->errorCode();
693 QString s;
694 if(x == XMPP::AdvancedConnector::ErrConnectionRefused)
695 s = "unable to connect to server";
696 else if(x == XMPP::AdvancedConnector::ErrHostNotFound)
697 s = "host not found";
698 else if(x == XMPP::AdvancedConnector::ErrProxyConnect)
699 s = "proxy connect";
700 else if(x == XMPP::AdvancedConnector::ErrProxyNeg)
701 s = "proxy negotiating";
702 else if(x == XMPP::AdvancedConnector::ErrProxyAuth)
703 s = "proxy authorization";
704 else if(x == XMPP::AdvancedConnector::ErrStream)
705 s = "stream error";
706 appendErrMsg(QString("Connection error: %1").arg(s));
707 }
708 else if(err == XMPP::ClientStream::ErrNeg) {
709 int x = stream->errorCondition();
710 QString s;
711 if(x == XMPP::ClientStream::HostGone)
712 s = "host no longer hosted";
713 else if(x == XMPP::ClientStream::HostUnknown)
714 s = "host unknown";
715 else if(x == XMPP::ClientStream::RemoteConnectionFailed)
716 s = "a required remote connection failed";
717 else if(x == XMPP::ClientStream::SeeOtherHost)
718 s = QString("see other host: [%1]").arg(stream->errorText());
719 else if(x == XMPP::ClientStream::UnsupportedVersion)
720 s = "server does not support proper xmpp version";
721 appendErrMsg(QString("Stream negotiation error: %1").arg(s));
722 }
723 else if(err == XMPP::ClientStream::ErrTLS) {
724 int x = stream->errorCondition();
725 QString s;
726 if(x == XMPP::ClientStream::TLSStart)
727 s = "server rejected STARTTLS";
728 else if(x == XMPP::ClientStream::TLSFail) {
729 int t = tlsHandler->tlsError();
730 if(t == QCA::TLS::ErrHandshake)
731 s = "TLS handshake error";
732 else
733 s = "broken security layer (TLS)";
734 }
735 appendErrMsg(s);
736 }
737 else if(err == XMPP::ClientStream::ErrAuth) {
738 int x = stream->errorCondition();
739 QString s;
740 if(x == XMPP::ClientStream::GenericAuthError)
741 s = "unable to login";
742 else if(x == XMPP::ClientStream::NoMech)
743 s = "no appropriate auth mechanism available for given security settings";
744 else if(x == XMPP::ClientStream::BadProto)
745 s = "bad server response";
746 else if(x == XMPP::ClientStream::BadServ)
747 s = "server failed mutual authentication";
748 else if(x == XMPP::ClientStream::EncryptionRequired)
749 s = "encryption required for chosen SASL mechanism";
750 else if(x == XMPP::ClientStream::InvalidAuthzid)
751 s = "invalid authzid";
752 else if(x == XMPP::ClientStream::InvalidMech)
753 s = "invalid SASL mechanism";
754 else if(x == XMPP::ClientStream::InvalidRealm)
755 s = "invalid realm";
756 else if(x == XMPP::ClientStream::MechTooWeak)
757 s = "SASL mechanism too weak for authzid";
758 else if(x == XMPP::ClientStream::NotAuthorized)
759 s = "not authorized";
760 else if(x == XMPP::ClientStream::TemporaryAuthFailure)
761 s = "temporary auth failure";
762 appendErrMsg(QString("Auth error: %1").arg(s));
763 }
764 else if(err == XMPP::ClientStream::ErrSecurityLayer)
765 appendErrMsg("Broken security layer (SASL)");
766 cleanup();
767 }
768
769private:
770 void setHostState()
771 {
772 bool ok = false;
773 if(!ck_probe->isChecked() && cb_proxy->currentItem() != 3)
774 ok = true;
775 lb_host->setEnabled(ok);
776 le_host->setEnabled(ok);
777 ck_ssl->setEnabled(ok);
778 }
779
780 void appendSysMsg(const QString &s, const QColor &_c=QColor())
781 {
782 QString str;
783 QColor c;
784 if(_c.isValid())
785 c = _c;
786 else
787 c = Qt::blue;
788
789 if(c.isValid())
790 str += QString("<font color=\"%1\">").arg(c.name());
791 str += QString("*** %1").arg(s);
792 if(c.isValid())
793 str += QString("</font>");
794 te_log->append(str);
795 }
796
797public:
798 void appendLibMsg(const QString &s)
799 {
800 appendSysMsg(s, Qt::magenta);
801 }
802
803 void appendErrMsg(const QString &s)
804 {
805 appendSysMsg(s, Qt::red);
806 }
807
808 void appendXmlOut(const QString &s)
809 {
810 QStringList lines = QStringList::split('\n', s, true);
811 QString str;
812 bool first = true;
813 for(QStringList::ConstIterator it = lines.begin(); it != lines.end(); ++it) {
814 if(!first)
815 str += "<br>";
816 str += QString("<font color=\"%1\">%2</font>").arg(Qt::darkGreen.name()).arg(plain2rich(*it));
817 first = false;
818 }
819 te_log->append(str);
820 }
821
822 void appendXmlIn(const QString &s)
823 {
824 QStringList lines = QStringList::split('\n', s, true);
825 QString str;
826 bool first = true;
827 for(QStringList::ConstIterator it = lines.begin(); it != lines.end(); ++it) {
828 if(!first)
829 str += "<br>";
830 str += QString("<font color=\"%1\">%2</font>").arg(Qt::darkBlue.name()).arg(plain2rich(*it));
831 first = false;
832 }
833 te_log->append(str);
834 }
835};
836
837TestDlg *td_glob = 0;
838
839void TestDebug::msg(const QString &s)
840{
841 if(td_glob)
842 td_glob->appendLibMsg(s);
843}
844
845void TestDebug::outgoingTag(const QString &s)
846{
847 if(td_glob)
848 td_glob->appendXmlOut(s);
849}
850
851void TestDebug::incomingTag(const QString &s)
852{
853 if(td_glob)
854 td_glob->appendXmlIn(s);
855}
856
857void TestDebug::outgoingXml(const QDomElement &e)
858{
859 QString out = XMPP::Stream::xmlToString(e, true);
860 if(td_glob)
861 td_glob->appendXmlOut(out);
862}
863
864void TestDebug::incomingXml(const QDomElement &e)
865{
866 QString out = XMPP::Stream::xmlToString(e, true);
867 if(td_glob)
868 td_glob->appendXmlIn(out);
869}
870
871#include"xmpptest.moc"
872
873int main(int argc, char **argv)
874{
875#ifdef Q_OS_WIN32
876 QApplication::addLibraryPath(".");
877 putenv("SASL_PATH=.\\sasl");
878#endif
879 QApplication app(argc, argv);
880
881 // seed the random number generator (needed at least for HttpPoll)
882 srand(time(NULL));
883
884 TestDlg *w = new TestDlg(0);
885 td_glob = w;
886 TestDebug *td = new TestDebug;
887 XMPP::setDebug(td);
888 QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
889 app.exec();
890 XMPP::setDebug(0);
891 delete td;
892 delete w;
893
894 // we need this for a clean exit
895 QCA::unloadAllPlugins();
896
897 return 0;
898}
Note: See TracBrowser for help on using the repository browser.