Replace {from,to}Ascii with {from,to}Latin1
[quassel.git] / src / core / corenetwork.cpp
index 5ef2557..b7d496a 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2013 by the Quassel Project                        *
+ *   Copyright (C) 2005-2014 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -18,6 +18,8 @@
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
+#include <QHostInfo>
+
 #include "corenetwork.h"
 
 #include "core.h"
@@ -40,10 +42,10 @@ CoreNetwork::CoreNetwork(const NetworkId &networkid, CoreSession *session)
 
     _lastPingTime(0),
     _pingCount(0),
+    _sendPings(false),
     _requestedUserModes('-')
 {
     _autoReconnectTimer.setSingleShot(true);
-    _socketCloseTimer.setSingleShot(true);
     connect(&_socketCloseTimer, SIGNAL(timeout()), this, SLOT(socketCloseTimeout()));
 
     setPingInterval(networkConfig()->pingInterval());
@@ -182,8 +184,13 @@ void CoreNetwork::connectToIrc(bool reconnecting)
         socket.setProxy(QNetworkProxy::NoProxy);
     }
 
+    enablePingTimeout();
+
+    // Qt caches DNS entries for a minute, resulting in round-robin (e.g. for chat.freenode.net) not working if several users
+    // connect at a similar time. QHostInfo::fromName(), however, always performs a fresh lookup, overwriting the cache entry.
+    QHostInfo::fromName(server.host);
+
 #ifdef HAVE_SSL
-    socket.setProtocol((QSsl::SslProtocol)server.sslVersion);
     if (server.useSsl) {
         CoreIdentity *identity = identityPtr();
         if (identity) {
@@ -261,13 +268,16 @@ void CoreNetwork::putCmd(const QString &cmd, const QList<QByteArray> &params, co
 
     if (!prefix.isEmpty())
         msg += ":" + prefix + " ";
-    msg += cmd.toUpper().toAscii();
+    msg += cmd.toUpper().toLatin1();
+
+    for (int i = 0; i < params.size(); i++) {
+        msg += " ";
 
-    for (int i = 0; i < params.size() - 1; i++) {
-        msg += " " + params[i];
+        if (i == params.size() - 1 && (params[i].contains(' ') || (!params[i].isEmpty() && params[i][0] == ':')))
+            msg += ":";
+
+        msg += params[i];
     }
-    if (!params.isEmpty())
-        msg += " :" + params.last();
 
     putRawLine(msg);
 }
@@ -436,7 +446,9 @@ void CoreNetwork::socketInitialized()
     if (server.useSsl && !socket.isEncrypted())
         return;
 #endif
-
+#if QT_VERSION >= 0x040600
+    socket.setSocketOption(QAbstractSocket::KeepAliveOption, true);
+#endif
     CoreIdentity *identity = identityPtr();
     if (!identity) {
         qCritical() << "Identity invalid!";
@@ -551,7 +563,7 @@ void CoreNetwork::networkInitialized()
 
     sendPerform();
 
-    enablePingTimeout();
+    _sendPings = true;
 
     if (networkConfig()->autoWhoEnabled()) {
         _autoWhoCycleTimer.start();
@@ -795,7 +807,9 @@ void CoreNetwork::sendPing()
     else {
         _lastPingTime = now;
         _pingCount++;
-        userInputHandler()->handlePing(BufferInfo(), QString());
+        // Don't send pings until the network is initialized
+        if(_sendPings)
+            userInputHandler()->handlePing(BufferInfo(), QString());
     }
 }
 
@@ -815,6 +829,7 @@ void CoreNetwork::enablePingTimeout(bool enable)
 void CoreNetwork::disablePingTimeout()
 {
     _pingTimer.stop();
+    _sendPings = false;
     resetPingTimeout();
 }