3a6b7845447582fff378144ce9cb508aac067e8b
[quassel.git] / src / client / coreconnection.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by the Quassel Project                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
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 program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "coreconnection.h"
22
23 #ifndef QT_NO_NETWORKPROXY
24 #  include <QNetworkProxy>
25 #endif
26
27 #include "client.h"
28 #include "clientsettings.h"
29 #include "coreaccountmodel.h"
30 #include "identity.h"
31 #include "network.h"
32 #include "networkmodel.h"
33 #include "quassel.h"
34 #include "signalproxy.h"
35 #include "util.h"
36
37 CoreConnection::CoreConnection(CoreAccountModel *model, QObject *parent)
38   : QObject(parent),
39   _model(model),
40   _blockSize(0),
41   _state(Disconnected),
42   _progressMinimum(0),
43   _progressMaximum(-1),
44   _progressValue(-1)
45 {
46   qRegisterMetaType<ConnectionState>("CoreConnection::ConnectionState");
47
48 }
49
50 void CoreConnection::init() {
51   connect(Client::signalProxy(), SIGNAL(disconnected()), SLOT(coreSocketDisconnected()));
52 }
53
54 void CoreConnection::setProgressText(const QString &text) {
55   if(_progressText != text) {
56     _progressText = text;
57     emit progressTextChanged(text);
58   }
59 }
60
61 void CoreConnection::setProgressValue(int value) {
62   if(_progressValue != value) {
63     _progressValue = value;
64     emit progressValueChanged(value);
65   }
66 }
67
68 void CoreConnection::setProgressMinimum(int minimum) {
69   if(_progressMinimum != minimum) {
70     _progressMinimum = minimum;
71     emit progressRangeChanged(minimum, _progressMaximum);
72   }
73 }
74
75 void CoreConnection::setProgressMaximum(int maximum) {
76   if(_progressMaximum != maximum) {
77     _progressMaximum = maximum;
78     emit progressRangeChanged(_progressMinimum, maximum);
79   }
80 }
81
82 void CoreConnection::updateProgress(int value, int max) {
83   if(max != _progressMaximum) {
84     _progressMaximum = max;
85     emit progressRangeChanged(_progressMinimum, _progressMaximum);
86   }
87   setProgressValue(value);
88 }
89
90 void CoreConnection::resetConnection() {
91   if(_socket) {
92     disconnect(_socket, 0, this, 0);
93     _socket->deleteLater();
94     _socket = 0;
95   }
96   _blockSize = 0;
97
98   _coreMsgBuffer.clear();
99
100   _netsToSync.clear();
101   _numNetsToSync = 0;
102
103   setProgressMaximum(-1); // disable
104   setState(Disconnected);
105   emit connectionMsg(tr("Disconnected from core."));
106   emit encrypted(false);
107 }
108
109 bool CoreConnection::isEncrypted() const {
110 #ifndef HAVE_SSL
111   return false;
112 #else
113   QSslSocket *sock = qobject_cast<QSslSocket *>(_socket);
114   return isConnected() && sock && sock->isEncrypted();
115 #endif
116 }
117
118 void CoreConnection::socketStateChanged(QAbstractSocket::SocketState socketState) {
119   QString text;
120
121   switch(socketState) {
122   case QAbstractSocket::UnconnectedState:
123     text = tr("Disconnected");
124     break;
125   case QAbstractSocket::HostLookupState:
126     text = tr("Looking up %1...").arg(currentAccount().hostName());
127     break;
128   case QAbstractSocket::ConnectingState:
129     text = tr("Connecting to %1...").arg(currentAccount().hostName());
130     break;
131   case QAbstractSocket::ConnectedState:
132     text = tr("Connected to %1").arg(currentAccount().hostName());
133     break;
134   case QAbstractSocket::ClosingState:
135     text = tr("Disconnecting from %1...").arg(currentAccount().hostName());
136     break;
137   default:
138     break;
139   }
140
141   if(!text.isEmpty())
142     emit progressTextChanged(text);
143
144   setState(socketState);
145 }
146
147 void CoreConnection::setState(QAbstractSocket::SocketState socketState) {
148   ConnectionState state;
149
150   switch(socketState) {
151   case QAbstractSocket::UnconnectedState:
152     state = Disconnected;
153     break;
154   case QAbstractSocket::HostLookupState:
155   case QAbstractSocket::ConnectingState:
156     state = Connecting;
157     break;
158   default:
159     state = Disconnected;
160   }
161
162   setState(state);
163 }
164
165 void CoreConnection::setState(ConnectionState state) {
166   if(state != _state) {
167     _state = state;
168     emit stateChanged(state);
169     if(state == Disconnected)
170       emit disconnected();
171   }
172 }
173
174 void CoreConnection::coreSocketError(QAbstractSocket::SocketError) {
175   qDebug() << "coreSocketError" << _socket << _socket->errorString();
176   emit connectionError(_socket->errorString());
177   resetConnection();
178 }
179
180 void CoreConnection::coreSocketDisconnected() {
181   emit disconnected();
182   resetConnection();
183   // FIXME handle disconnects gracefully
184 }
185
186 void CoreConnection::coreHasData() {
187   QVariant item;
188   while(SignalProxy::readDataFromDevice(_socket, _blockSize, item)) {
189     QVariantMap msg = item.toMap();
190     if(!msg.contains("MsgType")) {
191       // This core is way too old and does not even speak our init protocol...
192       emit connectionErrorPopup(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
193       disconnectFromCore();
194       return;
195     }
196     if(msg["MsgType"] == "ClientInitAck") {
197       clientInitAck(msg);
198     } else if(msg["MsgType"] == "ClientInitReject") {
199       emit connectionErrorPopup(msg["Error"].toString());
200       disconnectFromCore();
201       return;
202     } else if(msg["MsgType"] == "CoreSetupAck") {
203       emit coreSetupSuccess();
204     } else if(msg["MsgType"] == "CoreSetupReject") {
205       emit coreSetupFailed(msg["Error"].toString());
206     } else if(msg["MsgType"] == "ClientLoginReject") {
207       loginFailed(msg["Error"].toString());
208     } else if(msg["MsgType"] == "ClientLoginAck") {
209       loginSuccess();
210     } else if(msg["MsgType"] == "SessionInit") {
211       // that's it, let's hand over to the signal proxy
212       // if the socket is an orphan, the signalProxy adopts it.
213       // -> we don't need to care about it anymore
214       _socket->setParent(0);
215       Client::signalProxy()->addPeer(_socket);
216
217       sessionStateReceived(msg["SessionState"].toMap());
218       break; // this is definitively the last message we process here!
219     } else {
220       disconnectFromCore(tr("Invalid data received from core"));
221       return;
222     }
223   }
224   if(_blockSize > 0) {
225     updateProgress(_socket->bytesAvailable(), _blockSize);
226   }
227 }
228
229 void CoreConnection::disconnectFromCore(const QString &errorString) {
230   if(errorString.isEmpty())
231     emit connectionError(tr("Disconnected"));
232   else
233     emit connectionError(errorString);
234
235   Client::signalProxy()->removeAllPeers();
236   resetConnection();
237 }
238
239 void CoreConnection::reconnectToCore() {
240   if(currentAccount().isValid())
241     connectToCore(currentAccount().accountId());
242 }
243
244 bool CoreConnection::connectToCore(AccountId accId) {
245   if(isConnected())
246     return false;
247
248   CoreAccountSettings s;
249
250   // FIXME: Don't force connection to internal core in mono client
251   if(Quassel::runMode() == Quassel::Monolithic) {
252     _account = accountModel()->account(accountModel()->internalAccount());
253     Q_ASSERT(_account.isValid());
254   } else {
255     if(!accId.isValid()) {
256       // check our settings and figure out what to do
257       if(!s.autoConnectOnStartup())
258         return false;
259       if(s.autoConnectToFixedAccount())
260         accId = s.autoConnectAccount();
261       else
262         accId = s.lastAccount();
263       if(!accId.isValid())
264         return false;
265     }
266     _account = accountModel()->account(accId);
267     if(!_account.accountId().isValid()) {
268       return false;
269     }
270     if(Quassel::runMode() != Quassel::Monolithic) {
271       if(_account.isInternal())
272         return false;
273     }
274   }
275
276   s.setLastAccount(accId);
277   connectToCurrentAccount();
278   return true;
279 }
280
281 void CoreConnection::connectToCurrentAccount() {
282   resetConnection();
283
284   if(currentAccount().isInternal()) {
285     if(Quassel::runMode() != Quassel::Monolithic) {
286       qWarning() << "Cannot connect to internal core in client-only mode!";
287       return;
288     }
289     emit startInternalCore();
290     emit connectToInternalCore(Client::instance()->signalProxy());
291     return;
292   }
293
294   CoreAccountSettings s;
295
296   Q_ASSERT(!_socket);
297 #ifdef HAVE_SSL
298   QSslSocket *sock = new QSslSocket(Client::instance());
299   // make sure the warning is shown if we happen to connect without SSL support later
300   s.setAccountValue("ShowNoClientSslWarning", true);
301 #else
302   if(_account.useSsl()) {
303     if(s.accountValue("ShowNoClientSslWarning", true).toBool()) {
304       bool accepted = false;
305       emit handleNoSslInClient(&accepted);
306       if(!accepted) {
307         emit connectionError(tr("Unencrypted connection canceled"));
308         return;
309       }
310       s.setAccountValue("ShowNoClientSslWarning", false);
311     }
312   }
313   QTcpSocket *sock = new QTcpSocket(Client::instance());
314 #endif
315
316 #ifndef QT_NO_NETWORKPROXY
317   if(_account.useProxy()) {
318     QNetworkProxy proxy(_account.proxyType(), _account.proxyHostName(), _account.proxyPort(), _account.proxyUser(), _account.proxyPassword());
319     sock->setProxy(proxy);
320   }
321 #endif
322
323   _socket = sock;
324   connect(sock, SIGNAL(readyRead()), SLOT(coreHasData()));
325   connect(sock, SIGNAL(connected()), SLOT(coreSocketConnected()));
326   connect(sock, SIGNAL(disconnected()), SLOT(coreSocketDisconnected()));
327   connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(coreSocketError(QAbstractSocket::SocketError)));
328   connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(socketStateChanged(QAbstractSocket::SocketState)));
329
330   emit connectionMsg(tr("Connecting to %1...").arg(currentAccount().accountName()));
331   sock->connectToHost(_account.hostName(), _account.port());
332 }
333
334 void CoreConnection::coreSocketConnected() {
335   // Phase One: Send client info and wait for core info
336
337   emit connectionMsg(tr("Synchronizing to core..."));
338
339   QVariantMap clientInit;
340   clientInit["MsgType"] = "ClientInit";
341   clientInit["ClientVersion"] = Quassel::buildInfo().fancyVersionString;
342   clientInit["ClientDate"] = Quassel::buildInfo().buildDate;
343   clientInit["ProtocolVersion"] = Quassel::buildInfo().protocolVersion;
344   clientInit["UseSsl"] = _account.useSsl();
345 #ifndef QT_NO_COMPRESS
346   clientInit["UseCompression"] = true;
347 #else
348   clientInit["UseCompression"] = false;
349 #endif
350
351   SignalProxy::writeDataToDevice(_socket, clientInit);
352 }
353
354 void CoreConnection::clientInitAck(const QVariantMap &msg) {
355   // Core has accepted our version info and sent its own. Let's see if we accept it as well...
356   uint ver = msg["ProtocolVersion"].toUInt();
357   if(ver < Quassel::buildInfo().clientNeedsProtocol) {
358     emit connectionErrorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
359         "Need at least core/client protocol v%1 to connect.").arg(Quassel::buildInfo().clientNeedsProtocol));
360     disconnectFromCore();
361     return;
362   }
363
364 #ifndef QT_NO_COMPRESS
365   if(msg["SupportsCompression"].toBool()) {
366     _socket->setProperty("UseCompression", true);
367   }
368 #endif
369
370   _coreMsgBuffer = msg;
371
372 #ifdef HAVE_SSL
373   CoreAccountSettings s;
374   if(currentAccount().useSsl()) {
375     if(msg["SupportSsl"].toBool()) {
376       // Make sure the warning is shown next time we don't have SSL in the core
377       s.setAccountValue("ShowNoCoreSslWarning", true);
378
379       QSslSocket *sslSocket = qobject_cast<QSslSocket *>(_socket);
380       Q_ASSERT(sslSocket);
381       connect(sslSocket, SIGNAL(encrypted()), SLOT(sslSocketEncrypted()));
382       connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), SLOT(sslErrors()));
383       sslSocket->startClientEncryption();
384     } else {
385       if(s.accountValue("ShowNoCoreSslWarning", true).toBool()) {
386         bool accepted = false;
387         emit handleNoSslInCore(&accepted);
388         if(!accepted) {
389           disconnectFromCore(tr("Unencrypted connection canceled"));
390           return;
391         }
392         s.setAccountValue("ShowNoCoreSslWarning", false);
393         s.setAccountValue("SslCert", QString());
394       }
395       connectionReady();
396     }
397     return;
398   }
399 #endif
400   // if we use SSL we wait for the next step until every SSL warning has been cleared
401   connectionReady();
402 }
403
404 #ifdef HAVE_SSL
405
406 void CoreConnection::sslSocketEncrypted() {
407   QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
408   Q_ASSERT(socket);
409
410   if(!socket->sslErrors().count()) {
411     // Cert is valid, so we don't want to store it as known
412     // That way, a warning will appear in case it becomes invalid at some point
413     CoreAccountSettings s;
414     s.setAccountValue("SSLCert", QString());
415   }
416
417   emit encrypted(true);
418   connectionReady();
419 }
420
421 void CoreConnection::sslErrors() {
422   QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
423   Q_ASSERT(socket);
424
425   CoreAccountSettings s;
426   QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
427
428   if(knownDigest != socket->peerCertificate().digest()) {
429     bool accepted = false;
430     bool permanently = false;
431     emit handleSslErrors(socket, &accepted, &permanently);
432
433     if(!accepted) {
434       disconnectFromCore(tr("Unencrypted connection canceled"));
435       return;
436     }
437
438     if(permanently)
439       s.setAccountValue("SslCert", socket->peerCertificate().digest());
440     else
441       s.setAccountValue("SslCert", QString());
442   }
443
444   socket->ignoreSslErrors();
445 }
446
447 #endif /* HAVE_SSL */
448
449 void CoreConnection::connectionReady() {
450   setState(Connected);
451   emit connectionMsg(tr("Connected to %1").arg(currentAccount().accountName()));
452
453   if(!_coreMsgBuffer["Configured"].toBool()) {
454     // start wizard
455     emit startCoreSetup(_coreMsgBuffer["StorageBackends"].toList());
456   } else if(_coreMsgBuffer["LoginEnabled"].toBool()) {
457     loginToCore();
458   }
459   _coreMsgBuffer.clear();
460 }
461
462 void CoreConnection::loginToCore(const QString &user, const QString &password, bool remember) {
463   _account.setUser(user);
464   _account.setPassword(password);
465   _account.setStorePassword(remember);
466   loginToCore();
467 }
468
469 void CoreConnection::loginToCore(const QString &prevError) {
470   emit connectionMsg(tr("Logging in..."));
471   if(currentAccount().user().isEmpty() || currentAccount().password().isEmpty() || !prevError.isEmpty()) {
472     bool valid = false;
473     emit userAuthenticationRequired(&_account, &valid, prevError);  // *must* be a synchronous call
474     if(!valid || currentAccount().user().isEmpty() || currentAccount().password().isEmpty()) {
475       disconnectFromCore(tr("Login canceled"));
476       return;
477     }
478   }
479
480   QVariantMap clientLogin;
481   clientLogin["MsgType"] = "ClientLogin";
482   clientLogin["User"] = currentAccount().user();
483   clientLogin["Password"] = currentAccount().password();
484   SignalProxy::writeDataToDevice(_socket, clientLogin);
485 }
486
487 void CoreConnection::loginFailed(const QString &error) {
488   loginToCore(error);
489 }
490
491 void CoreConnection::loginSuccess() {
492   updateProgress(0, 0);
493
494   // save current account data
495   _model->createOrUpdateAccount(currentAccount());
496   _model->save();
497
498   setProgressText(tr("Receiving session state"));
499   setState(Synchronizing);
500   emit connectionMsg(tr("Synchronizing to %1...").arg(currentAccount().accountName()));
501 }
502
503 void CoreConnection::sessionStateReceived(const QVariantMap &state) {
504   updateProgress(100, 100);
505
506   // rest of communication happens through SignalProxy...
507   disconnect(_socket, SIGNAL(readyRead()), this, 0);
508   disconnect(_socket, SIGNAL(connected()), this, 0);
509
510   syncToCore(state);
511 }
512
513 void CoreConnection::internalSessionStateReceived(const QVariant &packedState) {
514   updateProgress(100, 100);
515
516   setState(Synchronizing);
517   syncToCore(packedState.toMap());
518 }
519
520 void CoreConnection::syncToCore(const QVariantMap &sessionState) {
521   setProgressText(tr("Receiving network states"));
522   updateProgress(0, 100);
523
524   // create identities
525   foreach(QVariant vid, sessionState["Identities"].toList()) {
526     Client::instance()->coreIdentityCreated(vid.value<Identity>());
527   }
528
529   // create buffers
530   // FIXME: get rid of this crap -- why?
531   QVariantList bufferinfos = sessionState["BufferInfos"].toList();
532   NetworkModel *networkModel = Client::networkModel();
533   Q_ASSERT(networkModel);
534   foreach(QVariant vinfo, bufferinfos)
535     networkModel->bufferUpdated(vinfo.value<BufferInfo>());  // create BufferItems
536
537   QVariantList networkids = sessionState["NetworkIds"].toList();
538
539   // prepare sync progress thingys...
540   // FIXME: Care about removal of networks
541   _numNetsToSync = networkids.count();
542   updateProgress(0, _numNetsToSync);
543
544   // create network objects
545   foreach(QVariant networkid, networkids) {
546     NetworkId netid = networkid.value<NetworkId>();
547     if(Client::network(netid))
548       continue;
549     Network *net = new Network(netid, Client::instance());
550     _netsToSync.insert(net);
551     connect(net, SIGNAL(initDone()), SLOT(networkInitDone()));
552     connect(net, SIGNAL(destroyed()), SLOT(networkInitDone()));
553     Client::addNetwork(net);
554   }
555   checkSyncState();
556 }
557
558 void CoreConnection::networkInitDone() {
559   Network *net = qobject_cast<Network *>(sender());
560   Q_ASSERT(net);
561   disconnect(net, 0, this, 0);
562   _netsToSync.remove(net);
563   updateProgress(_numNetsToSync - _netsToSync.count(), _numNetsToSync);
564   checkSyncState();
565 }
566
567 void CoreConnection::checkSyncState() {
568   if(_netsToSync.isEmpty()) {
569     setState(Synchronized);
570     setProgressText(tr("Synchronized to %1").arg(currentAccount().accountName()));
571     setProgressMaximum(-1);
572     emit synchronized();
573   }
574 }
575
576 void CoreConnection::doCoreSetup(const QVariant &setupData) {
577   QVariantMap setup;
578   setup["MsgType"] = "CoreSetupData";
579   setup["SetupData"] = setupData;
580   SignalProxy::writeDataToDevice(_socket, setup);
581 }