~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to src/miniclient.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * miniclient.cpp
 
3
 * Copyright (C) 2001, 2002  Justin Karneges
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include <QtCrypto>
 
22
#include <QMessageBox>
 
23
 
 
24
#include "miniclient.h"
 
25
#include "proxy.h"
 
26
#include "certutil.h"
 
27
#include "psiaccount.h"
 
28
#include "sslcertdlg.h"
 
29
#include "xmpp_tasks.h"
 
30
 
 
31
using namespace XMPP;
 
32
 
 
33
MiniClient::MiniClient(QObject *parent)
 
34
:QObject(parent)
 
35
{
 
36
        _client = new Client;
 
37
        conn = 0;
 
38
        tls = 0;
 
39
        tlsHandler = 0;
 
40
        stream = 0;
 
41
        auth = false;
 
42
        force_ssl = false;
 
43
        error_disconnect = true;
 
44
}
 
45
 
 
46
MiniClient::~MiniClient()
 
47
{
 
48
        delete _client;
 
49
        reset();
 
50
}
 
51
 
 
52
void MiniClient::reset()
 
53
{
 
54
        delete stream;
 
55
        stream = 0;
 
56
 
 
57
        delete tls;
 
58
        tls = 0;
 
59
        tlsHandler = 0;
 
60
 
 
61
        delete conn;
 
62
        conn = 0;
 
63
}
 
64
 
 
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)
 
66
{
 
67
        j = jid;
 
68
 
 
69
        QString host;
 
70
        int port = -1;
 
71
        bool useHost = false;
 
72
        force_ssl = forcessl;
 
73
        if(!_host.isEmpty()) {
 
74
                useHost = true;
 
75
                host = _host;
 
76
                port = _port;
 
77
        }
 
78
 
 
79
        AdvancedConnector::Proxy p;
 
80
        if(proxy > 0) {
 
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()) {
 
89
                                if (useHost)
 
90
                                        u.addQueryItem("server",host + ':' + QString::number(port));
 
91
                                else
 
92
                                        u.addQueryItem("server",jid.host());
 
93
                        }
 
94
                        p.setHttpPoll(pi.settings.host, pi.settings.port, u.toString());
 
95
                        p.setPollInterval(2);
 
96
                }
 
97
 
 
98
                if(pi.settings.useAuth)
 
99
                        p.setUserPass(pi.settings.user, pi.settings.pass);
 
100
        }
 
101
 
 
102
        conn = new AdvancedConnector;
 
103
        tls = new QCA::TLS;
 
104
        tls->setTrustedCertificates(CertUtil::allCertificates());
 
105
        tlsHandler = new QCATLSHandler(tls);
 
106
        tlsHandler->setXMPPCertCheck(true);
 
107
        connect(tlsHandler, SIGNAL(tlsHandshaken()), SLOT(tls_handshaken()));
 
108
        conn->setProxy(p);
 
109
        if (useHost) {
 
110
                conn->setOptHostPort(host, port);
 
111
                conn->setOptSSL(legacy_ssl);
 
112
        }
 
113
        else {
 
114
                conn->setOptProbe(legacy_ssl_probe);
 
115
        }
 
116
 
 
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);
 
126
 
 
127
        if(_pass) {
 
128
                auth = true;
 
129
                pass = *_pass;
 
130
                _client->connectToServer(stream, j);
 
131
        }
 
132
        else {
 
133
                auth = false;
 
134
                _client->connectToServer(stream, j, false);
 
135
        }
 
136
}
 
137
 
 
138
void MiniClient::close()
 
139
{
 
140
        _client->close();
 
141
        reset();
 
142
}
 
143
 
 
144
Client *MiniClient::client()
 
145
{
 
146
        return _client;
 
147
}
 
148
 
 
149
void MiniClient::setErrorOnDisconnect(bool b)
 
150
{
 
151
        error_disconnect = b;
 
152
}
 
153
 
 
154
void MiniClient::tls_handshaken()
 
155
{
 
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);
 
162
                while(1) {
 
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),
 
166
                                tr("&Details..."),
 
167
                                tr("Co&ntinue"),
 
168
                                tr("&Cancel"), 0, 2);
 
169
                        if(n == 0) {
 
170
                                SSLCertDlg::showCert(cert, r, validity);
 
171
                        }
 
172
                        else if(n == 1) {
 
173
                                tlsHandler->continueAfterHandshake();
 
174
                                break;
 
175
                        }
 
176
                        else if(n == 2) {
 
177
                                close();
 
178
                                error();
 
179
                                break;
 
180
                        }
 
181
                }
 
182
        }
 
183
        else
 
184
                tlsHandler->continueAfterHandshake();
 
185
}
 
186
 
 
187
void MiniClient::cs_connected()
 
188
{
 
189
}
 
190
 
 
191
void MiniClient::cs_securityLayerActivated(int)
 
192
{
 
193
}
 
194
 
 
195
void MiniClient::cs_needAuthParams(bool user, bool password, bool realm)
 
196
{
 
197
        if(user) 
 
198
                stream->setUsername(j.user());
 
199
        if(password)
 
200
                stream->setPassword(pass);
 
201
        if(realm)
 
202
                stream->setRealm(j.domain());
 
203
        stream->continueAfterParams();
 
204
}
 
205
 
 
206
void MiniClient::cs_authenticated()
 
207
{
 
208
        _client->start(j.host(), j.user(), "", "");
 
209
 
 
210
        if (!stream->old() && auth) {
 
211
                JT_Session *j = new JT_Session(_client->rootTask());
 
212
                connect(j,SIGNAL(finished()),SLOT(sessionStart_finished()));
 
213
                j->go(true);
 
214
        }
 
215
        else {
 
216
                handshaken();
 
217
        }
 
218
}
 
219
 
 
220
void MiniClient::sessionStart_finished()
 
221
{
 
222
        JT_Session *j = (JT_Session*)sender();
 
223
        if ( j->success() ) {
 
224
                handshaken();
 
225
        }
 
226
        else {
 
227
                cs_error(-1);
 
228
        }
 
229
}
 
230
 
 
231
void MiniClient::cs_connectionClosed()
 
232
{
 
233
        if (error_disconnect)
 
234
                cs_error(-1);
 
235
        else
 
236
                emit disconnected();
 
237
}
 
238
 
 
239
void MiniClient::cs_delayedCloseFinished()
 
240
{
 
241
}
 
242
 
 
243
void MiniClient::cs_warning(int err)
 
244
{
 
245
        if (err == ClientStream::WarnNoTLS && force_ssl) {
 
246
                close();
 
247
                QMessageBox::critical(0, tr("Server Error"), tr("The server does not support TLS encryption."));
 
248
        }
 
249
        else {
 
250
                stream->continueAfterWarning();
 
251
        }
 
252
}
 
253
 
 
254
void MiniClient::cs_error(int err)
 
255
{
 
256
        QString str;
 
257
        bool reconn;
 
258
 
 
259
        PsiAccount::getErrorInfo(err, conn, stream, tlsHandler, &str, &reconn);
 
260
        close();
 
261
 
 
262
        QMessageBox::critical(0, tr("Server Error"), tr("There was an error communicating with the Jabber server.\nDetails: %1").arg(str));
 
263
        error();
 
264
}
 
265