Statistics
| Branch: | Tag: | Revision:

root / modules / jabber_protocol / client / mini-client.cpp @ d8068106

History | View | Annotate | Download (6.2 kB)

1
/*
2
 * %kadu copyright begin%
3
 * Copyright 2009 Wojciech Treter (juzefwt@gmail.com)
4
 * Copyright 2009 Juzef (juzefwt@tlen.pl)
5
 * Copyright 2010 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
6
 * Copyright 2009 Bartłomiej Zimoń (uzi18@go2.pl)
7
 * %kadu copyright end%
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License as
11
 * published by the Free Software Foundation; either version 2 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/*
24
 * miniclient.cpp
25
 * Copyright (C) 2001, 2002  Justin Karneges
26
 */
27
28
#include <QtCrypto>
29
#include <QMessageBox>
30
31
#include "debug.h"
32
#include "misc/path-conversion.h"
33
34
#include "certificates/certificate-helpers.h"
35
#include "certificates/certificate-error-dialog.h"
36
#include "client/jabber-client.h"
37
#include "mini-client.h"
38
#include "xmpp_tasks.h"
39
40
using namespace XMPP;
41
42
MiniClient::MiniClient(QObject *parent)
43
:QObject(parent)
44
{
45
        _client = new Client;
46
47
                QObject::connect ( _client, SIGNAL ( xmlIncoming(const QString& ) ),
48
                                   this, SLOT ( slotDebug(const QString &) ) );
49
                QObject::connect ( _client, SIGNAL ( xmlOutgoing(const QString& ) ),
50
                                   this, SLOT ( slotDebug(const QString &) ) );
51
        conn = 0;
52
        tls = 0;
53
        tlsHandler = 0;
54
        stream = 0;
55
        auth = false;
56
        force_ssl = false;
57
        error_disconnect = true;
58
}
59
60
MiniClient::~MiniClient()
61
{
62
        delete _client;
63
        reset();
64
}
65
66
void MiniClient::reset()
67
{
68
        delete stream;
69
        stream = 0;
70
71
        delete tls;
72
        tls = 0;
73
        tlsHandler = 0;
74
75
        delete conn;
76
        conn = 0;
77
}
78
79
void MiniClient::connectToServer(const Jid &jid, bool legacy_ssl_probe, bool legacy_ssl, bool forcessl, const QString &_host, int _port/*, ProxyManager *pm, QString proxy, QString *_pass*/)
80
{
81
        j = jid;
82
83
        QString host;
84
        int port = -1;
85
        bool useHost = false;
86
        force_ssl = forcessl;
87
        if(!_host.isEmpty()) {
88
                useHost = true;
89
                host = _host;
90
                port = _port;
91
        }
92
/*
93
        AdvancedConnector::Proxy p;
94
        if(proxy != "") {
95
                const ProxyItem &pi = pm->getItem(proxy);
96
                if(pi.type == "http") // HTTP Connect
97
                        p.setHttpConnect(pi.settings.host, pi.settings.port);
98
                else if(pi.type == "socks") // SOCKS
99
                        p.setSocks(pi.settings.host, pi.settings.port);
100
                else if(pi.type == "poll") { // HTTP Poll
101
                        QUrl u = pi.settings.url;
102
                        if(u.queryItems().isEmpty()) {
103
                                if (useHost)
104
                                        u.addQueryItem("server",host + ':' + QString::number(port));
105
                                else
106
                                        u.addQueryItem("server",jid.host());
107
                        }
108
                        p.setHttpPoll(pi.settings., pi.settings.port, u.toString());
109
                        p.setPollInterval(2);
110
                }
111
112
                if(pi.settings.useAuth)
113
                        p.setUserPass(pi.settings.user, pi.settings.pass);
114
        }
115
*/
116
117
        conn = new AdvancedConnector;
118
        tls = new QCA::TLS;
119
        tls->setTrustedCertificates(CertificateHelpers::allCertificates(CertificateHelpers::getCertificateStoreDirs()));
120
        tlsHandler = new QCATLSHandler(tls);
121
        tlsHandler->setXMPPCertCheck(true);
122
        connect(tlsHandler, SIGNAL(tlsHandshaken()), SLOT(tls_handshaken()));
123
///        conn->setProxy(p);
124
        if (useHost) {
125
                conn->setOptHostPort(host, port);
126
                conn->setOptSSL(legacy_ssl);
127
        }
128
        else {
129
                conn->setOptProbe(legacy_ssl_probe);
130
        }
131
132
        stream = new ClientStream(conn, tlsHandler);
133
        connect(stream, SIGNAL(connected()), SLOT(cs_connected()));
134
        connect(stream, SIGNAL(securityLayerActivated(int)), SLOT(cs_securityLayerActivated(int)));
135
        connect(stream, SIGNAL(needAuthParams(bool, bool, bool)), SLOT(cs_needAuthParams(bool, bool, bool)));
136
        connect(stream, SIGNAL(authenticated()), SLOT(cs_authenticated()));
137
        connect(stream, SIGNAL(connectionClosed()), SLOT(cs_connectionClosed()));
138
        connect(stream, SIGNAL(delayedCloseFinished()), SLOT(cs_delayedCloseFinished()));
139
        connect(stream, SIGNAL(warning(int)), SLOT(cs_warning(int)));
140
        connect(stream, SIGNAL(error(int)), SLOT(cs_error(int)), Qt::QueuedConnection);
141
/*
142
        if(_pass) {
143
                auth = true;
144
                pass = *_pass;
145
                _client->connectToServer(stream, j);
146
        }
147
        else {
148
*/                auth = false;
149
                _client->connectToServer(stream, j, false);
150
//        }
151
}
152
153
void MiniClient::close()
154
{
155
        _client->close();
156
        reset();
157
}
158
159
Client *MiniClient::client()
160
{
161
        return _client;
162
}
163
164
void MiniClient::setErrorOnDisconnect(bool b)
165
{
166
        error_disconnect = b;
167
}
168
169
void MiniClient::tls_handshaken()
170
{
171
        if (CertificateHelpers::checkCertificate(tls, tlsHandler, TlsOverrideDomain, TlsOverrideCert,
172
                                                                                 tr("Server Authentication"),
173
                                                                                 j.domain(), this)) {
174
                tlsHandler->continueAfterHandshake();
175
        } else {
176
                close();
177
                error();
178
        }
179
}
180
181
void MiniClient::cs_connected()
182
{
183
}
184
185
void MiniClient::cs_securityLayerActivated(int)
186
{
187
}
188
189
void MiniClient::cs_needAuthParams(bool user, bool password, bool realm)
190
{
191
        if(user) 
192
                stream->setUsername(j.node());
193
        if(password)
194
                stream->setPassword(pass);
195
        if(realm)
196
                stream->setRealm(j.domain());
197
        stream->continueAfterParams();
198
}
199
200
void MiniClient::cs_authenticated()
201
{
202
        _client->start(j.domain(), j.node(), "", "");
203
204
        if (!stream->old() && auth) {
205
                JT_Session *j = new JT_Session(_client->rootTask());
206
                connect(j,SIGNAL(finished()),SLOT(sessionStart_finished()));
207
                j->go(true);
208
        }
209
        else {
210
                handshaken();
211
        }
212
}
213
214
void MiniClient::sessionStart_finished()
215
{
216
        JT_Session *j = (JT_Session*)sender();
217
        if ( j->success() ) {
218
                handshaken();
219
        }
220
        else {
221
                cs_error(-1);
222
        }
223
}
224
225
void MiniClient::cs_connectionClosed()
226
{
227
        if (error_disconnect)
228
                cs_error(-1);
229
        else
230
                emit disconnected();
231
}
232
233
void MiniClient::cs_delayedCloseFinished()
234
{
235
}
236
237
void MiniClient::cs_warning(int err)
238
{
239
        if (err == ClientStream::WarnNoTLS && force_ssl) {
240
                close();
241
                QMessageBox::critical(0, tr("Server Error"), tr("The server does not support TLS encryption."));
242
        }
243
        else {
244
                stream->continueAfterWarning();
245
        }
246
}
247
248
void MiniClient::cs_error(int err)
249
{
250
        QString str;
251
        bool reconn;
252
        JabberClient::getErrorInfo(err, conn, stream, tlsHandler, &str, &reconn);
253
        close();
254
255
        QMessageBox::critical(0, tr("Server Error"), tr("There was an error communicating with the Jabber server.\nDetails: %1").arg(str));
256
        error();
257
}
258
259
void MiniClient::slotDebug(const QString &text)
260
{
261
        kdebugm(KDEBUG_WARNING, "Jabber MiniClient debug:  %s\n", qPrintable(text));
262
}
263