1 /***************************************************************************
2 * Copyright (C) 2005-2013 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
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 program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "clientauthhandler.h"
23 // TODO: support system application proxy (new in Qt 4.6)
32 #include "clientsettings.h"
34 #include "protocols/legacy/legacypeer.h"
36 using namespace Protocol;
38 ClientAuthHandler::ClientAuthHandler(CoreAccount account, QObject *parent)
39 : AuthHandler(parent),
47 void ClientAuthHandler::connectToCore()
49 CoreAccountSettings s;
52 QSslSocket *socket = new QSslSocket(this);
53 // make sure the warning is shown if we happen to connect without SSL support later
54 s.setAccountValue("ShowNoClientSslWarning", true);
56 if (_account.useSsl()) {
57 if (s.accountValue("ShowNoClientSslWarning", true).toBool()) {
58 bool accepted = false;
59 emit handleNoSslInClient(&accepted);
61 emit errorMessage(tr("Unencrypted connection canceled"));
64 s.setAccountValue("ShowNoClientSslWarning", false);
67 QTcpSocket *socket = new QTcpSocket(this);
70 // TODO: Handle system proxy
71 #ifndef QT_NO_NETWORKPROXY
72 if (_account.useProxy()) {
73 QNetworkProxy proxy(_account.proxyType(), _account.proxyHostName(), _account.proxyPort(), _account.proxyUser(), _account.proxyPassword());
74 socket->setProxy(proxy);
79 // handled by the base class for now; may need to rethink for protocol detection
80 //connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError)));
81 //connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
82 connect(socket, SIGNAL(connected()), SLOT(onSocketConnected()));
84 emit statusMessage(tr("Connecting to %1...").arg(_account.accountName()));
85 socket->connectToHost(_account.hostName(), _account.port());
89 // TODO: handle protocol detection
90 // This method might go away anyway, unless we really need our own states...
92 void ClientAuthHandler::onSocketStateChanged(QAbstractSocket::SocketState socketState)
94 qDebug() << Q_FUNC_INFO << socketState;
96 AuthHandler::State state = UnconnectedState;
99 case QAbstractSocket::UnconnectedState:
100 text = tr("Disconnected");
101 state = UnconnectedState;
103 case QAbstractSocket::HostLookupState:
104 text = tr("Looking up %1...").arg(_account.hostName());
105 state = HostLookupState;
107 case QAbstractSocket::ConnectingState:
108 text = tr("Connecting to %1...").arg(_account.hostName());
109 state = ConnectingState;
111 case QAbstractSocket::ConnectedState:
112 text = tr("Connected to %1").arg(_account.hostName());
113 state = ConnectedState;
115 case QAbstractSocket::ClosingState:
116 text = tr("Disconnecting from %1...").arg(_account.hostName());
117 state = ClosingState;
123 if (!text.isEmpty()) {
125 emit statusMessage(text);
130 // TODO: handle protocol detection
132 void ClientAuthHandler::onSocketError(QAbstractSocket::SocketError error)
134 emit socketError(error, socket()->errorString());
138 void ClientAuthHandler::onSocketConnected()
140 // TODO: protocol detection
143 qWarning() << Q_FUNC_INFO << "Peer already exists!";
147 _peer = new LegacyPeer(this, socket(), this);
149 connect(_peer, SIGNAL(transferProgress(int,int)), SIGNAL(transferProgress(int,int)));
152 connect(_peer, SIGNAL(protocolVersionMismatch(int,int)), SLOT(onProtocolVersionMismatch(int,int)));
154 emit statusMessage(tr("Synchronizing to core..."));
158 useSsl = _account.useSsl();
161 _peer->dispatch(RegisterClient(Quassel::buildInfo().fancyVersionString, useSsl));
165 void ClientAuthHandler::onProtocolVersionMismatch(int actual, int expected)
167 emit errorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
168 "We need at least protocol v%1, but the core speaks v%2 only.").arg(expected, actual));
173 void ClientAuthHandler::handle(const ClientDenied &msg)
175 emit errorPopup(msg.errorString);
180 void ClientAuthHandler::handle(const ClientRegistered &msg)
182 _coreConfigured = msg.coreConfigured;
183 _backendInfo = msg.backendInfo;
185 Client::setCoreFeatures(static_cast<Quassel::Features>(msg.coreFeatures));
188 CoreAccountSettings s;
189 if (_account.useSsl()) {
190 if (msg.sslSupported) {
191 // Make sure the warning is shown next time we don't have SSL in the core
192 s.setAccountValue("ShowNoCoreSslWarning", true);
194 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
196 connect(sslSocket, SIGNAL(encrypted()), SLOT(onSslSocketEncrypted()));
197 connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), SLOT(onSslErrors()));
198 qDebug() << "Starting encryption...";
200 sslSocket->startClientEncryption();
203 if (s.accountValue("ShowNoCoreSslWarning", true).toBool()) {
204 bool accepted = false;
205 emit handleNoSslInCore(&accepted);
207 requestDisconnect(tr("Unencrypted connection cancelled"));
210 s.setAccountValue("ShowNoCoreSslWarning", false);
211 s.setAccountValue("SslCert", QString());
218 // if we use SSL we wait for the next step until every SSL warning has been cleared
225 void ClientAuthHandler::onSslSocketEncrypted()
227 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
230 if (!socket->sslErrors().count()) {
231 // Cert is valid, so we don't want to store it as known
232 // That way, a warning will appear in case it becomes invalid at some point
233 CoreAccountSettings s;
234 s.setAccountValue("SSLCert", QString());
237 emit encrypted(true);
242 void ClientAuthHandler::onSslErrors()
244 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
247 CoreAccountSettings s;
248 QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
250 if (knownDigest != socket->peerCertificate().digest()) {
251 bool accepted = false;
252 bool permanently = false;
253 emit handleSslErrors(socket, &accepted, &permanently);
256 requestDisconnect(tr("Unencrypted connection canceled"));
261 s.setAccountValue("SslCert", socket->peerCertificate().digest());
263 s.setAccountValue("SslCert", QString());
266 socket->ignoreSslErrors();
269 #endif /* HAVE_SSL */
272 void ClientAuthHandler::onConnectionReady()
274 emit connectionReady();
275 emit statusMessage(tr("Connected to %1").arg(_account.accountName()));
277 if (!_coreConfigured) {
279 emit startCoreSetup(_backendInfo);
281 else // TODO: check if we need LoginEnabled
286 void ClientAuthHandler::setupCore(const SetupData &setupData)
288 _peer->dispatch(setupData);
292 void ClientAuthHandler::handle(const SetupFailed &msg)
294 emit coreSetupFailed(msg.errorString);
298 void ClientAuthHandler::handle(const SetupDone &msg)
302 emit coreSetupSuccessful();
306 void ClientAuthHandler::login(const QString &user, const QString &password, bool remember)
308 _account.setUser(user);
309 _account.setPassword(password);
310 _account.setStorePassword(remember);
315 void ClientAuthHandler::login(const QString &previousError)
317 emit statusMessage(tr("Logging in..."));
318 if (_account.user().isEmpty() || _account.password().isEmpty() || !previousError.isEmpty()) {
320 emit userAuthenticationRequired(&_account, &valid, previousError); // *must* be a synchronous call
321 if (!valid || _account.user().isEmpty() || _account.password().isEmpty()) {
322 requestDisconnect(tr("Login canceled"));
327 _peer->dispatch(Login(_account.user(), _account.password()));
331 void ClientAuthHandler::handle(const LoginFailed &msg)
333 login(msg.errorString);
337 void ClientAuthHandler::handle(const LoginSuccess &msg)
341 emit loginSuccessful(_account);
345 void ClientAuthHandler::handle(const SessionState &msg)
347 disconnect(socket(), 0, this, 0); // this is the last message we shall ever get
349 // give up ownership of the peer; CoreSession takes responsibility now
351 emit handshakeComplete(_peer, msg);