1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "clientsyncer.h"
23 #ifndef QT_NO_NETWORKPROXY
24 # include <QNetworkProxy>
31 #include "ircchannel.h"
33 #include "signalproxy.h"
36 ClientSyncer::ClientSyncer(QObject *parent)
42 connect(Client::signalProxy(), SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
45 ClientSyncer::~ClientSyncer() {
48 void ClientSyncer::coreHasData() {
50 while(SignalProxy::readDataFromDevice(socket, blockSize, item)) {
51 emit recvPartialItem(1,1);
52 QVariantMap msg = item.toMap();
53 if(!msg.contains("MsgType")) {
54 // This core is way too old and does not even speak our init protocol...
55 emit connectionError(tr("The Quassel Core you try to connect to is too old! Please consider upgrading."));
59 if(msg["MsgType"] == "ClientInitAck") {
61 } else if(msg["MsgType"] == "ClientInitReject") {
62 emit connectionError(msg["Error"].toString());
65 } else if(msg["MsgType"] == "CoreSetupAck") {
66 emit coreSetupSuccess();
67 } else if(msg["MsgType"] == "CoreSetupReject") {
68 emit coreSetupFailed(msg["Error"].toString());
69 } else if(msg["MsgType"] == "ClientLoginReject") {
70 emit loginFailed(msg["Error"].toString());
71 } else if(msg["MsgType"] == "ClientLoginAck") {
72 // prevent multiple signal connections
73 disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
74 connect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
76 } else if(msg["MsgType"] == "SessionInit") {
77 sessionStateReceived(msg["SessionState"].toMap());
78 break; // this is definitively the last message we process here!
80 emit connectionError(tr("<b>Invalid data received from core!</b><br>Disconnecting."));
86 emit recvPartialItem(socket->bytesAvailable(), blockSize);
90 void ClientSyncer::coreSocketError(QAbstractSocket::SocketError) {
91 qDebug() << "coreSocketError" << socket << socket->errorString();
92 emit connectionError(socket->errorString());
93 socket->deleteLater();
96 void ClientSyncer::disconnectFromCore() {
97 if(socket) socket->close();
100 void ClientSyncer::connectToCore(const QVariantMap &conn) {
101 // TODO implement SSL
102 coreConnectionInfo = conn;
103 //if(isConnected()) {
104 // emit coreConnectionError(tr("Already connected to Core!"));
108 socket->deleteLater();
111 if(conn["Host"].toString().isEmpty()) {
112 emit connectionError(tr("Internal connections not yet supported."));
113 return; // FIXME implement internal connections
114 //clientMode = LocalCore;
115 socket = new QBuffer(this);
116 connect(socket, SIGNAL(readyRead()), this, SLOT(coreHasData()));
117 socket->open(QIODevice::ReadWrite);
118 //QVariant state = connectToLocalCore(coreConnectionInfo["User"].toString(), coreConnectionInfo["Password"].toString());
120 coreSocketConnected();
122 //clientMode = RemoteCore;
123 //emit coreConnectionMsg(tr("Connecting..."));
126 #ifndef QT_NO_OPENSSL
127 QSslSocket *sock = new QSslSocket(Client::instance());
129 if(conn["useSsl"].toBool()) {
130 emit connectionError(tr("<b>This client is built without SSL Support!</b><br />Disable the usage of SSL in the account settings."));
131 emit encrypted(false);
134 QTcpSocket *sock = new QTcpSocket(Client::instance());
136 #ifndef QT_NO_NETWORKPROXY
137 if(conn.contains("useProxy") && conn["useProxy"].toBool()) {
138 QNetworkProxy proxy((QNetworkProxy::ProxyType)conn["proxyType"].toInt(), conn["proxyHost"].toString(), conn["proxyPort"].toUInt(), conn["proxyUser"].toString(), conn["proxyPassword"].toString());
139 sock->setProxy(proxy);
143 connect(sock, SIGNAL(readyRead()), this, SLOT(coreHasData()));
144 connect(sock, SIGNAL(connected()), this, SLOT(coreSocketConnected()));
145 connect(sock, SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
146 connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(coreSocketError(QAbstractSocket::SocketError)));
147 connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)));
148 sock->connectToHost(conn["Host"].toString(), conn["Port"].toUInt());
152 void ClientSyncer::coreSocketConnected() {
153 //connect(this, SIGNAL(recvPartialItem(uint, uint)), this, SIGNAL(coreConnectionProgress(uint, uint)));
154 // Phase One: Send client info and wait for core info
156 //emit coreConnectionMsg(tr("Synchronizing to core..."));
157 QVariantMap clientInit;
158 clientInit["MsgType"] = "ClientInit";
159 clientInit["ClientVersion"] = Global::quasselVersion;
160 clientInit["ClientDate"] = Global::quasselDate;
161 clientInit["ClientBuild"] = Global::quasselBuild; // this is a minimum, since we probably won't update for every commit
162 clientInit["UseSsl"] = coreConnectionInfo["useSsl"];
163 #ifndef QT_NO_COMPRESS
164 clientInit["UseCompression"] = true;
166 clientInit["UseCompression"] = false;
170 SignalProxy::writeDataToDevice(socket, clientInit);
173 void ClientSyncer::coreSocketDisconnected() {
174 emit socketDisconnected();
175 Client::instance()->disconnectFromCore();
177 // FIXME handle disconnects gracefully in here as well!
179 coreConnectionInfo.clear();
181 channelsToSync.clear();
184 //restartPhaseNull();
187 void ClientSyncer::clientInitAck(const QVariantMap &msg) {
188 // Core has accepted our version info and sent its own. Let's see if we accept it as well...
189 if(msg["CoreBuild"].toUInt() < Global::coreBuildNeeded) {
190 emit connectionError(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
191 "Need at least a Core Version %1 (Build >= %2) to connect.").arg(Global::quasselVersion).arg(Global::coreBuildNeeded));
192 disconnectFromCore();
195 emit connectionMsg(msg["CoreInfo"].toString());
197 #ifndef QT_NO_OPENSSL
198 if(coreConnectionInfo["useSsl"].toBool()) {
199 if(msg["SupportSsl"].toBool()) {
200 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
202 connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
203 sslSocket->startClientEncryption();
204 emit encrypted(true);
205 Client::instance()->setSecuredConnection();
207 emit connectionError(tr("<b>The Quassel Core you are trying to connect to does not support SSL!</b><br />If you want to connect anyways, disable the usage of SSL in the account settings."));
208 emit encrypted(false);
209 disconnectFromCore();
215 #ifndef QT_NO_COMPRESS
216 if(msg["SupportsCompression"].toBool()) {
217 socket->setProperty("UseCompression", true);
221 if(!msg["Configured"].toBool()) {
223 emit startCoreSetup(msg["StorageBackends"].toList());
224 } else if(msg["LoginEnabled"].toBool()) {
229 void ClientSyncer::doCoreSetup(const QVariant &setupData) {
231 setup["MsgType"] = "CoreSetupData";
232 setup["SetupData"] = setupData;
233 SignalProxy::writeDataToDevice(socket, setup);
236 void ClientSyncer::loginToCore(const QString &user, const QString &passwd) {
237 emit connectionMsg(tr("Logging in..."));
238 QVariantMap clientLogin;
239 clientLogin["MsgType"] = "ClientLogin";
240 clientLogin["User"] = user;
241 clientLogin["Password"] = passwd;
242 SignalProxy::writeDataToDevice(socket, clientLogin);
245 void ClientSyncer::sessionStateReceived(const QVariantMap &state) {
246 emit sessionProgress(1, 1);
247 disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
248 disconnect(socket, 0, this, 0); // rest of communication happens through SignalProxy
249 //Client::signalProxy()->addPeer(socket);
250 Client::instance()->setConnectedToCore(socket, coreConnectionInfo["AccountId"].value<AccountId>());
254 void ClientSyncer::syncToCore(const QVariantMap &sessionState) {
257 foreach(QVariant vid, sessionState["Identities"].toList()) {
258 Client::instance()->coreIdentityCreated(vid.value<Identity>());
262 // FIXME: get rid of this crap
263 QVariantList bufferinfos = sessionState["BufferInfos"].toList();
264 foreach(QVariant vinfo, bufferinfos) Client::buffer(vinfo.value<BufferInfo>()); // create Buffers and BufferItems
266 QVariantList networkids = sessionState["NetworkIds"].toList();
268 // prepare sync progress thingys... FIXME: Care about removal of networks
269 numNetsToSync = networkids.count();
270 numChannelsToSync = 0; //sessionState["IrcChannelCount"].toUInt();
271 numUsersToSync = 0; // sessionState["IrcUserCount"].toUInt(); qDebug() << numUsersToSync;
272 emit networksProgress(0, numNetsToSync);
273 emit channelsProgress(0, numChannelsToSync);
274 emit ircUsersProgress(0, numUsersToSync);
276 // create network objects
277 foreach(QVariant networkid, networkids) {
278 NetworkId netid = networkid.value<NetworkId>();
279 Network *net = new Network(netid, Client::instance());
280 netsToSync.insert(net);
281 connect(net, SIGNAL(initDone()), this, SLOT(networkInitDone()));
282 connect(net, SIGNAL(ircUserInitDone(IrcUser *)), this, SLOT(ircUserInitDone(IrcUser *)));
283 connect(net, SIGNAL(ircUserAdded(IrcUser *)), this, SLOT(ircUserAdded(IrcUser *)));
284 connect(net, SIGNAL(ircUserRemoved(QObject *)), this, SLOT(ircUserRemoved(QObject *)));
285 connect(net, SIGNAL(ircChannelInitDone(IrcChannel *)), this, SLOT(ircChannelInitDone(IrcChannel *)));
286 connect(net, SIGNAL(ircChannelAdded(IrcChannel *)), this, SLOT(ircChannelAdded(IrcChannel *)));
287 connect(net, SIGNAL(ircChannelRemoved(QObject *)), this, SLOT(ircChannelRemoved(QObject *)));
288 Client::addNetwork(net);
293 void ClientSyncer::networkInitDone() {
294 netsToSync.remove(sender());
295 emit networksProgress(numNetsToSync - netsToSync.count(), numNetsToSync);
299 void ClientSyncer::ircChannelInitDone(IrcChannel *chan) {
300 channelsToSync.remove(chan);
301 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
305 void ClientSyncer::ircChannelAdded(IrcChannel *chan) {
306 if(!chan->isInitialized()) {
307 channelsToSync.insert(chan);
309 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
314 void ClientSyncer::ircChannelRemoved(QObject *chan) {
315 if(channelsToSync.contains(chan)) {
317 channelsToSync.remove(chan);
318 emit channelsProgress(numChannelsToSync - channelsToSync.count(), numChannelsToSync);
323 void ClientSyncer::ircUserInitDone(IrcUser *user) {
324 usersToSync.remove(user);
325 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
329 void ClientSyncer::ircUserAdded(IrcUser *user) {
330 if(!user->isInitialized()) {
331 usersToSync.insert(user);
333 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
338 void ClientSyncer::ircUserRemoved(QObject *user) {
339 if(usersToSync.contains(user)) {
341 usersToSync.remove(user);
342 emit ircUsersProgress(numUsersToSync - usersToSync.count(), numUsersToSync);
347 void ClientSyncer::checkSyncState() {
348 if(usersToSync.count() + channelsToSync.count() + netsToSync.count() == 0) {
352 foreach(Network *net, _networks.values()) {
353 //disconnect(net, 0, this, SLOT(networkInitDone()));
354 //disconnect(net, 0, this, SLOT(ircUserInitDone(IrcUser *)));
355 //disconnect(net, 0, this, SLOT(ircUserAdded(IrcUser *)));
356 //disconnect(net, 0, this, SLOT(ircUserRemoved(QObject *)));
357 //disconnect(net, 0, this, SLOT(ircChannelInitDone(IrcChannel *)));
358 //disconnect(net, 0, this, SLOT(ircChannelAdded(IrcChannel *)));
359 //disconnect(net, 0, this, SLOT(ircChannelRemoved(QObject *)));
360 qDebug() << "disconnecting";
361 disconnect(net, SIGNAL(initDone()), this, SLOT(networkInitDone()));
362 disconnect(net, SIGNAL(ircUserInitDone(IrcUser *)), this, SLOT(ircUserInitDone(IrcUser *)));
363 disconnect(net, SIGNAL(ircUserAdded(IrcUser *)), this, SLOT(ircUserAdded(IrcUser *)));
364 disconnect(net, SIGNAL(ircUserRemoved(QObject *)), this, SLOT(ircUserRemoved(QObject *)));
365 disconnect(net, SIGNAL(ircChannelInitDone(IrcChannel *)), this, SLOT(ircChannelInitDone(IrcChannel *)));
366 disconnect(net, SIGNAL(ircChannelAdded(IrcChannel *)), this, SLOT(ircChannelAdded(IrcChannel *)));
367 disconnect(net, SIGNAL(ircChannelRemoved(QObject *)), this, SLOT(ircChannelRemoved(QObject *)));
371 Client::instance()->setSyncedToCore();
374 //emit connectionStateChanged(true);
379 #ifndef QT_NO_OPENSSL
380 void ClientSyncer::sslErrors(const QList<QSslError> &errors) {
381 qDebug() << "SSL Errors:";
382 foreach(QSslError err, errors)
383 qDebug() << " " << err;
385 QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
387 socket->ignoreSslErrors();