3
* Copyright (C) 2001, 2002 Justin Karneges
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include <QMessageBox>
24
#include "miniclient.h"
27
#include "psiaccount.h"
28
#include "sslcertdlg.h"
29
#include "xmpp_tasks.h"
33
MiniClient::MiniClient(QObject *parent)
43
error_disconnect = true;
46
MiniClient::~MiniClient()
52
void MiniClient::reset()
65
void MiniClient::connectToServer(const Jid &jid, bool legacy_ssl_probe, bool legacy_ssl, bool forcessl, const QString &_host, int _port, ProxyManager *pm, int proxy, QString *_pass)
73
if(!_host.isEmpty()) {
79
AdvancedConnector::Proxy p;
81
const ProxyItem &pi = pm->getItem(proxy-1);
82
if(pi.type == "http") // HTTP Connect
83
p.setHttpConnect(pi.settings.host, pi.settings.port);
84
else if(pi.type == "socks") // SOCKS
85
p.setSocks(pi.settings.host, pi.settings.port);
86
else if(pi.type == "poll") { // HTTP Poll
87
QUrl u = pi.settings.url;
88
if(u.queryItems().isEmpty()) {
90
u.addQueryItem("server",host + ':' + QString::number(port));
92
u.addQueryItem("server",jid.host());
94
p.setHttpPoll(pi.settings.host, pi.settings.port, u.toString());
98
if(pi.settings.useAuth)
99
p.setUserPass(pi.settings.user, pi.settings.pass);
102
conn = new AdvancedConnector;
104
tls->setTrustedCertificates(CertUtil::allCertificates());
105
tlsHandler = new QCATLSHandler(tls);
106
tlsHandler->setXMPPCertCheck(true);
107
connect(tlsHandler, SIGNAL(tlsHandshaken()), SLOT(tls_handshaken()));
110
conn->setOptHostPort(host, port);
111
conn->setOptSSL(legacy_ssl);
114
conn->setOptProbe(legacy_ssl_probe);
117
stream = new ClientStream(conn, tlsHandler);
118
connect(stream, SIGNAL(connected()), SLOT(cs_connected()));
119
connect(stream, SIGNAL(securityLayerActivated(int)), SLOT(cs_securityLayerActivated(int)));
120
connect(stream, SIGNAL(needAuthParams(bool, bool, bool)), SLOT(cs_needAuthParams(bool, bool, bool)));
121
connect(stream, SIGNAL(authenticated()), SLOT(cs_authenticated()));
122
connect(stream, SIGNAL(connectionClosed()), SLOT(cs_connectionClosed()));
123
connect(stream, SIGNAL(delayedCloseFinished()), SLOT(cs_delayedCloseFinished()));
124
connect(stream, SIGNAL(warning(int)), SLOT(cs_warning(int)));
125
connect(stream, SIGNAL(error(int)), SLOT(cs_error(int)), Qt::QueuedConnection);
130
_client->connectToServer(stream, j);
134
_client->connectToServer(stream, j, false);
138
void MiniClient::close()
144
Client *MiniClient::client()
149
void MiniClient::setErrorOnDisconnect(bool b)
151
error_disconnect = b;
154
void MiniClient::tls_handshaken()
156
QCA::Certificate cert = tls->peerCertificateChain().primary();
157
int r = tls->peerIdentityResult();
158
if (r == QCA::TLS::Valid && !tlsHandler->certMatchesHostname()) r = QCA::TLS::HostMismatch;
159
if(r != QCA::TLS::Valid) {
160
QCA::Validity validity = tls->peerCertificateValidity();
161
QString str = CertUtil::resultToString(r,validity);
163
int n = QMessageBox::warning(0,
164
tr("Server Authentication"),
165
tr("The %1 certificate failed the authenticity test.").arg(j.host()) + '\n' + tr("Reason: %1.").arg(str),
168
tr("&Cancel"), 0, 2);
170
SSLCertDlg::showCert(cert, r, validity);
173
tlsHandler->continueAfterHandshake();
184
tlsHandler->continueAfterHandshake();
187
void MiniClient::cs_connected()
191
void MiniClient::cs_securityLayerActivated(int)
195
void MiniClient::cs_needAuthParams(bool user, bool password, bool realm)
198
stream->setUsername(j.user());
200
stream->setPassword(pass);
202
stream->setRealm(j.domain());
203
stream->continueAfterParams();
206
void MiniClient::cs_authenticated()
208
_client->start(j.host(), j.user(), "", "");
210
if (!stream->old() && auth) {
211
JT_Session *j = new JT_Session(_client->rootTask());
212
connect(j,SIGNAL(finished()),SLOT(sessionStart_finished()));
220
void MiniClient::sessionStart_finished()
222
JT_Session *j = (JT_Session*)sender();
223
if ( j->success() ) {
231
void MiniClient::cs_connectionClosed()
233
if (error_disconnect)
239
void MiniClient::cs_delayedCloseFinished()
243
void MiniClient::cs_warning(int err)
245
if (err == ClientStream::WarnNoTLS && force_ssl) {
247
QMessageBox::critical(0, tr("Server Error"), tr("The server does not support TLS encryption."));
250
stream->continueAfterWarning();
254
void MiniClient::cs_error(int err)
259
PsiAccount::getErrorInfo(err, conn, stream, tlsHandler, &str, &reconn);
262
QMessageBox::critical(0, tr("Server Error"), tr("There was an error communicating with the Jabber server.\nDetails: %1").arg(str));