1 /***************************************************************************
2 * Copyright (C) 2005-2014 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 socket()->setSocketOption(QAbstractSocket::KeepAliveOption, true);
149 _peer = new LegacyPeer(this, socket(), this);
151 connect(_peer, SIGNAL(transferProgress(int,int)), SIGNAL(transferProgress(int,int)));
154 connect(_peer, SIGNAL(protocolVersionMismatch(int,int)), SLOT(onProtocolVersionMismatch(int,int)));
156 emit statusMessage(tr("Synchronizing to core..."));
160 useSsl = _account.useSsl();
163 _peer->dispatch(RegisterClient(Quassel::buildInfo().fancyVersionString, useSsl));
167 void ClientAuthHandler::onProtocolVersionMismatch(int actual, int expected)
169 emit errorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
170 "We need at least protocol v%1, but the core speaks v%2 only.").arg(expected, actual));
171 requestDisconnect(tr("Incompatible protocol version, connection to core refused"));
175 void ClientAuthHandler::handle(const ClientDenied &msg)
177 emit errorPopup(msg.errorString);
178 requestDisconnect(tr("The core refused connection from this client"));
182 void ClientAuthHandler::handle(const ClientRegistered &msg)
184 _coreConfigured = msg.coreConfigured;
185 _backendInfo = msg.backendInfo;
187 Client::setCoreFeatures(static_cast<Quassel::Features>(msg.coreFeatures));
190 CoreAccountSettings s;
191 if (_account.useSsl()) {
192 if (msg.sslSupported) {
193 // Make sure the warning is shown next time we don't have SSL in the core
194 s.setAccountValue("ShowNoCoreSslWarning", true);
196 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
198 connect(sslSocket, SIGNAL(encrypted()), SLOT(onSslSocketEncrypted()));
199 connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), SLOT(onSslErrors()));
200 qDebug() << "Starting encryption...";
202 sslSocket->startClientEncryption();
205 if (s.accountValue("ShowNoCoreSslWarning", true).toBool()) {
206 bool accepted = false;
207 emit handleNoSslInCore(&accepted);
209 requestDisconnect(tr("Unencrypted connection cancelled"));
212 s.setAccountValue("ShowNoCoreSslWarning", false);
213 s.setAccountValue("SslCert", QString());
220 // if we use SSL we wait for the next step until every SSL warning has been cleared
227 void ClientAuthHandler::onSslSocketEncrypted()
229 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
232 if (!socket->sslErrors().count()) {
233 // Cert is valid, so we don't want to store it as known
234 // That way, a warning will appear in case it becomes invalid at some point
235 CoreAccountSettings s;
236 s.setAccountValue("SSLCert", QString());
239 emit encrypted(true);
244 void ClientAuthHandler::onSslErrors()
246 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
249 CoreAccountSettings s;
250 QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
252 if (knownDigest != socket->peerCertificate().digest()) {
253 bool accepted = false;
254 bool permanently = false;
255 emit handleSslErrors(socket, &accepted, &permanently);
258 requestDisconnect(tr("Unencrypted connection canceled"));
263 s.setAccountValue("SslCert", socket->peerCertificate().digest());
265 s.setAccountValue("SslCert", QString());
268 socket->ignoreSslErrors();
271 #endif /* HAVE_SSL */
274 void ClientAuthHandler::onConnectionReady()
276 emit connectionReady();
277 emit statusMessage(tr("Connected to %1").arg(_account.accountName()));
279 if (!_coreConfigured) {
281 emit startCoreSetup(_backendInfo);
283 else // TODO: check if we need LoginEnabled
288 void ClientAuthHandler::setupCore(const SetupData &setupData)
290 _peer->dispatch(setupData);
294 void ClientAuthHandler::handle(const SetupFailed &msg)
296 emit coreSetupFailed(msg.errorString);
300 void ClientAuthHandler::handle(const SetupDone &msg)
304 emit coreSetupSuccessful();
308 void ClientAuthHandler::login(const QString &user, const QString &password, bool remember)
310 _account.setUser(user);
311 _account.setPassword(password);
312 _account.setStorePassword(remember);
317 void ClientAuthHandler::login(const QString &previousError)
319 emit statusMessage(tr("Logging in..."));
320 if (_account.user().isEmpty() || _account.password().isEmpty() || !previousError.isEmpty()) {
322 emit userAuthenticationRequired(&_account, &valid, previousError); // *must* be a synchronous call
323 if (!valid || _account.user().isEmpty() || _account.password().isEmpty()) {
324 requestDisconnect(tr("Login canceled"));
329 _peer->dispatch(Login(_account.user(), _account.password()));
333 void ClientAuthHandler::handle(const LoginFailed &msg)
335 login(msg.errorString);
339 void ClientAuthHandler::handle(const LoginSuccess &msg)
343 emit loginSuccessful(_account);
347 void ClientAuthHandler::handle(const SessionState &msg)
349 disconnect(socket(), 0, this, 0); // this is the last message we shall ever get
351 // give up ownership of the peer; CoreSession takes responsibility now
353 emit handshakeComplete(_peer, msg);