X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fnetworkconnection.cpp;h=d177d4c1ed866383af222eee2f21da83a754f193;hp=4ded4a8242153c9641f568fee097826abeeb2049;hb=f7379184b7c0ae4e53d7470809f84e2ad3239ec1;hpb=07171f8337e7236c3ac3432352b41ad3c53e077b diff --git a/src/core/networkconnection.cpp b/src/core/networkconnection.cpp index 4ded4a82..d177d4c1 100644 --- a/src/core/networkconnection.cpp +++ b/src/core/networkconnection.cpp @@ -29,13 +29,14 @@ #include "ircchannel.h" #include "ircuser.h" -#include "network.h" #include "identity.h" #include "ircserverhandler.h" #include "userinputhandler.h" #include "ctcphandler.h" +#include "logger.h" + NetworkConnection::NetworkConnection(Network *network, CoreSession *session) : QObject(network), _connectionState(Network::Disconnected), @@ -64,7 +65,10 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) _autoReconnectTimer.setSingleShot(true); _socketCloseTimer.setSingleShot(true); connect(&_socketCloseTimer, SIGNAL(timeout()), this, SLOT(socketCloseTimeout())); - + + _pingTimer.setInterval(60000); + connect(&_pingTimer, SIGNAL(timeout()), this, SLOT(sendPing())); + _autoWhoTimer.setInterval(_autoWhoDelay * 1000); _autoWhoCycleTimer.setInterval(_autoWhoInterval * 1000); @@ -85,7 +89,7 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) connect(network, SIGNAL(autoReconnectIntervalSet(quint32)), this, SLOT(autoReconnectSettingsChanged())); connect(network, SIGNAL(autoReconnectRetriesSet(quint16)), this, SLOT(autoReconnectSettingsChanged())); -#ifndef QT_NO_OPENSSL +#ifdef HAVE_SSL connect(&socket, SIGNAL(encrypted()), this, SLOT(socketEncrypted())); connect(&socket, SIGNAL(sslErrors(const QList &)), this, SLOT(sslErrors(const QList &))); #endif @@ -215,6 +219,8 @@ void NetworkConnection::networkInitialized(const QString ¤tServer) { network()->setConnected(true); emit connected(networkId()); + _pingTimer.start(); + if(_autoWhoEnabled) { _autoWhoCycleTimer.start(); _autoWhoTimer.start(); @@ -247,21 +253,25 @@ void NetworkConnection::sendPerform() { if(!joinString.isEmpty()) userInputHandler()->handleJoin(statusBuf, joinString); } -void NetworkConnection::disconnectFromIrc(bool requested) { +void NetworkConnection::disconnectFromIrc(bool requested, const QString &reason) { + _quitRequested = requested; // see socketDisconnected(); _autoReconnectTimer.stop(); - _autoReconnectCount = 0; + _autoReconnectCount = 0; // prohibiting auto reconnect displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Disconnecting.")); - if(socket.state() < QAbstractSocket::ConnectedState) { - setConnectionState(Network::Disconnected); + if(socket.state() == QAbstractSocket::UnconnectedState) { + socketDisconnected(); + } else if(socket.state() < QAbstractSocket::ConnectedState || !requested) { + // we might be in a state waiting for a timeout... + // or (!requested) this is a core shutdown... + // in both cases we don't really care... set a disconnected state + socket.close(); socketDisconnected(); } else { - _socketCloseTimer.start(10000); // the irc server has 10 seconds to close the socket + // quit gracefully if it's user requested quit + userInputHandler()->issueQuit(reason); + // the irc server has 10 seconds to close the socket + _socketCloseTimer.start(10000); } - - // this flag triggers quitRequested() once the socket is closed - // it is needed to determine whether or not the connection needs to be - // in the automatic session restore. - _quitRequested = requested; } void NetworkConnection::socketHasData() { @@ -273,12 +283,11 @@ void NetworkConnection::socketHasData() { void NetworkConnection::socketError(QAbstractSocket::SocketError) { _previousConnectionAttemptFailed = true; - qDebug() << qPrintable(tr("Could not connect to %1 (%2)").arg(network()->networkName(), socket.errorString())); + qWarning() << qPrintable(tr("Could not connect to %1 (%2)").arg(network()->networkName(), socket.errorString())); emit connectionError(socket.errorString()); emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Connection failure: %1").arg(socket.errorString())); network()->emitConnectionError(socket.errorString()); if(socket.state() < QAbstractSocket::ConnectedState) { - setConnectionState(Network::Disconnected); socketDisconnected(); } // mark last connection attempt as failed @@ -287,7 +296,7 @@ void NetworkConnection::socketError(QAbstractSocket::SocketError) { //exit(1); } -#ifndef QT_NO_OPENSSL +#ifdef HAVE_SSL void NetworkConnection::sslErrors(const QList &sslErrors) { Q_UNUSED(sslErrors) @@ -311,10 +320,10 @@ void NetworkConnection::socketEncrypted() { socketInitialized(); } -#endif // QT_NO_OPENSSL +#endif // HAVE_SSL void NetworkConnection::socketConnected() { -#ifdef QT_NO_OPENSSL +#ifndef HAVE_SSL socketInitialized(); return; #else @@ -331,7 +340,7 @@ void NetworkConnection::socketInitialized() { //emit connected(networkId()); initialize first! Identity *identity = coreSession()->identity(network()->identity()); if(!identity) { - qWarning() << "Identity invalid!"; + qCritical() << "Identity invalid!"; disconnectFromIrc(); return; } @@ -370,21 +379,31 @@ void NetworkConnection::socketCloseTimeout() { } void NetworkConnection::socketDisconnected() { + _pingTimer.stop(); _autoWhoCycleTimer.stop(); _autoWhoTimer.stop(); _autoWhoQueue.clear(); _autoWhoInProgress.clear(); _socketCloseTimer.stop(); - + + IrcUser *me = network()->me(); + if(me) { + foreach(QString channel, me->channels()) + emit displayMsg(Message::Quit, BufferInfo::ChannelBuffer, channel, "", me->hostmask()); + } + network()->setConnected(false); emit disconnected(networkId()); - if(_autoReconnectCount != 0) { - setConnectionState(Network::Reconnecting); - if(_autoReconnectCount == network()->autoReconnectRetries()) doAutoReconnect(); // first try is immediate - else _autoReconnectTimer.start(); - } else if(_quitRequested) { + if(_quitRequested) { + setConnectionState(Network::Disconnected); emit quitRequested(networkId()); + } else if(_autoReconnectCount != 0) { + setConnectionState(Network::Reconnecting); + if(_autoReconnectCount == network()->autoReconnectRetries()) + doAutoReconnect(); // first try is immediate + else + _autoReconnectTimer.start(); } } @@ -436,7 +455,10 @@ int NetworkConnection::lastParamOverrun(const QString &cmd, const QListme(); - int maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toAscii().count() - 6; + int maxLen = 480 - cmd.toAscii().count(); // educated guess in case we don't know us (yet?) + + if(me) + maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toAscii().count() - 6; if(!params.isEmpty()) { for(int i = 0; i < params.count() - 1; i++) { @@ -459,12 +481,9 @@ void NetworkConnection::putCmd(const QString &cmd, const QList ¶ if(cmd == "PRIVMSG" && params.count() > 1) { int overrun = lastParamOverrun(cmd, params); if(overrun) { - QList paramCopy1; - QList paramCopy2; - for(int i = 0; i < params.count() - 1; i++) { - paramCopy1 << params[i]; - paramCopy2 << params[i]; - } + QList paramCopy1 = params; + paramCopy1.removeLast(); + QList paramCopy2 = paramCopy1; QByteArray lastPart = params.last(); QByteArray splitter(" .,-"); @@ -473,8 +492,7 @@ void NetworkConnection::putCmd(const QString &cmd, const QList ¶ for(int i = 0; i < splitter.size(); i++) { splitPos = qMax(splitPos, lastPart.lastIndexOf(splitter[i], maxSplitPos)); } - - if(splitPos == -1) { + if(splitPos <= 0) { splitPos = maxSplitPos; } @@ -499,6 +517,10 @@ void NetworkConnection::putCmd(const QString &cmd, const QList ¶ putRawLine(msg); } +void NetworkConnection::sendPing() { + userInputHandler()->handlePing(BufferInfo(), QString()); +} + void NetworkConnection::sendAutoWho() { while(!_autoWhoQueue.isEmpty()) { QString chan = _autoWhoQueue.takeFirst();