fixing a bug in core.cpp (yet another crash on exit)
[quassel.git] / src / core / core.cpp
index 6f20174..0f652d0 100644 (file)
@@ -340,12 +340,23 @@ QHash<BufferId, MsgId> Core::bufferLastSeenMsgIds(UserId user) {
 /*** Network Management ***/
 
 bool Core::startListening(uint port) {
-  if(!server.listen(QHostAddress::Any, port)) {
+  bool success = false;
+
+  // let's see if ipv6 is available
+  success = server.listen(QHostAddress::AnyIPv6, port);
+
+  if(!success && server.serverError() == QAbstractSocket::UnsupportedSocketOperationError) {
+    // fall back to v4
+    success = server.listen(QHostAddress::Any, port);
+  }
+
+  if(!success) {
     qWarning("%s", qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(server.errorString())));
-    return false;
+  } else {
+    qDebug() << "Listening for GUI clients on port" << server.serverPort();
   }
-  qDebug() << "Listening for GUI clients on port" << server.serverPort();
-  return true;
+  
+  return success;
 }
 
 void Core::stopListening() {
@@ -362,7 +373,7 @@ void Core::incomingConnection() {
     
     QVariantMap clientInfo;
     blocksizes.insert(socket, (quint32)0);
-    qDebug() << "Client connected from"  << qPrintable(socket->peerAddress().toString());
+    qDebug() << qPrintable(tr("Client connected from"))  << qPrintable(socket->peerAddress().toString());
 
     if (!configured) {
       server.close();
@@ -394,14 +405,16 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     QVariantMap reply;
 
     // Just version information -- check it!
-    if((msg.contains("ClientBuild") && msg["ClientBuild"].toUInt() < 732)
-       || (!msg.contains("ClientBuild") && msg["ProtocolVersion"].toUInt() < Global::coreNeedsProtocol)) {
+    uint ver = 0;
+    if(!msg.contains("ProtocolVersion") && msg["ClientBuild"].toUInt() >= 732) ver = 1; // FIXME legacy
+    if(msg.contains("ProtocolVersion")) ver = msg["ProtocolVersion"].toUInt();
+    if(ver < Global::coreNeedsProtocol) {
       reply["MsgType"] = "ClientInitReject";
       reply["Error"] = tr("<b>Your Quassel Client is too old!</b><br>"
       "This core needs at least client/core protocol version %1.<br>"
       "Please consider upgrading your client.").arg(Global::coreNeedsProtocol);
       SignalProxy::writeDataToDevice(socket, reply);
-      qWarning() << qPrintable(tr("Client %1 too old, rejecting.").arg(socket->peerAddress().toString()));
+      qWarning() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("too old, rejecting."));
       socket->close(); return;
     }
 
@@ -461,7 +474,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
 #ifndef QT_NO_OPENSSL
     // after we told the client that we are ssl capable we switch to ssl mode
     if(supportSsl && msg["UseSsl"].toBool()) {
-      qDebug() << "Starting TLS for Client:" << qPrintable(socket->peerAddress().toString());
+      qDebug() << qPrintable(tr("Starting TLS for Client:"))  << qPrintable(socket->peerAddress().toString());
       connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
       sslSocket->startServerEncryption();
     }
@@ -481,7 +494,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
       reply["MsgType"] = "ClientLoginReject";
       reply["Error"] = tr("<b>Client not initialized!</b><br>You need to send an init message before trying to login.");
       SignalProxy::writeDataToDevice(socket, reply);
-      qWarning() << qPrintable(tr("Client %1 did not send an init message before trying to login, rejecting.").arg(socket->peerAddress().toString()));
+      qWarning() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("did not send an init message before trying to login, rejecting."));
       socket->close(); return;
     }
     if(msg["MsgType"] == "CoreSetupData") {
@@ -507,7 +520,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
       }
       reply["MsgType"] = "ClientLoginAck";
       SignalProxy::writeDataToDevice(socket, reply);
-      qDebug() << qPrintable(tr("Client %1 initialized and authenticated successfully as \"%2\" (UserId: %3).").arg(socket->peerAddress().toString(), msg["User"].toString()).arg(uid.toInt()));
+      qDebug() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
       setupClientSession(socket, uid);
     }
   }
@@ -519,9 +532,12 @@ void Core::clientDisconnected() {
   if(socket) {
     // here it's safe to call methods on socket!
     qDebug() << qPrintable(tr("Non-authed client disconnected.")) << qPrintable(socket->peerAddress().toString());
+    blocksizes.remove(socket);
+    clientInfo.remove(socket);
     socket->deleteLater();
   } else {
     // we have to crawl through the hashes and see if we find a victim to remove
+    qDebug() << qPrintable(tr("Non-authed client disconnected. (socket allready destroyed)"));
 
     // DO NOT CALL ANY METHODS ON socket!!
     socket = static_cast<QTcpSocket *>(sender());
@@ -563,7 +579,7 @@ void Core::setupClientSession(QTcpSocket *socket, UserId uid) {
   blocksizes.remove(socket);
   clientInfo.remove(socket);
   if(!sess) {
-    qWarning() << qPrintable(tr("Could not initialize session for client %1!").arg(socket->peerAddress().toString()));
+    qWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(socket->peerAddress().toString());
     socket->close();
   }
   sess->addClient(socket);