#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),
_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);
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<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
#endif
QVariantList serverList = network()->serverList();
Identity *identity = coreSession()->identity(network()->identity());
if(!serverList.count()) {
- qWarning() << "Server list empty, ignoring connect request!";
+ quWarning() << "Server list empty, ignoring connect request!";
return;
}
if(!identity) {
- qWarning() << "Invalid identity configures, ignoring connect request!";
+ quWarning() << "Invalid identity configures, ignoring connect request!";
return;
}
// use a random server?
network()->setConnected(true);
emit connected(networkId());
+ _pingTimer.start();
+
if(_autoWhoEnabled) {
_autoWhoCycleTimer.start();
_autoWhoTimer.start();
}
void NetworkConnection::disconnectFromIrc(bool requested) {
+ _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) {
+ socket.close();
+ // we might be in a state waiting for a timeout... we don't care... set a disconnected state
socketDisconnected();
} else {
_socketCloseTimer.start(10000); // the irc server has 10 seconds to close the socket
}
-
- // 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() {
void NetworkConnection::socketError(QAbstractSocket::SocketError) {
_previousConnectionAttemptFailed = true;
- qDebug() << qPrintable(tr("Could not connect to %1 (%2)").arg(network()->networkName(), socket.errorString()));
+ quWarning() << 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
//exit(1);
}
-#ifndef QT_NO_OPENSSL
+#ifdef HAVE_SSL
void NetworkConnection::sslErrors(const QList<QSslError> &sslErrors) {
Q_UNUSED(sslErrors)
socketInitialized();
}
-#endif // QT_NO_OPENSSL
+#endif // HAVE_SSL
void NetworkConnection::socketConnected() {
-#ifdef QT_NO_OPENSSL
+#ifndef HAVE_SSL
socketInitialized();
return;
#else
//emit connected(networkId()); initialize first!
Identity *identity = coreSession()->identity(network()->identity());
if(!identity) {
- qWarning() << "Identity invalid!";
+ quError() << "Identity invalid!";
disconnectFromIrc();
return;
}
}
void NetworkConnection::socketDisconnected() {
+ _pingTimer.stop();
_autoWhoCycleTimer.stop();
_autoWhoTimer.stop();
_autoWhoQueue.clear();
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();
}
}
void NetworkConnection::doAutoReconnect() {
if(connectionState() != Network::Disconnected && connectionState() != Network::Reconnecting) {
- qWarning() << "NetworkConnection::doAutoReconnect(): Cannot reconnect while not being disconnected!";
+ quWarning() << "NetworkConnection::doAutoReconnect(): Cannot reconnect while not being disconnected!";
return;
}
if(_autoReconnectCount > 0) _autoReconnectCount--;
// that means that the last message can be as long as:
// 512 - nicklen - userlen - hostlen - commandlen - sum(param[0]..param[n-1])) - 2 (for CRLF) - 4 (":!@" + 1space between prefix and command) - max(paramcount - 1, 0) (space for simple params) - 2 (space and colon for last param)
IrcUser *me = network()->me();
- 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++) {
if(cmd == "PRIVMSG" && params.count() > 1) {
int overrun = lastParamOverrun(cmd, params);
if(overrun) {
- QList<QByteArray> paramCopy1;
- QList<QByteArray> paramCopy2;
- for(int i = 0; i < params.count() - 1; i++) {
- paramCopy1 << params[i];
- paramCopy2 << params[i];
- }
+ QList<QByteArray> paramCopy1 = params;
+ paramCopy1.removeLast();
+ QList<QByteArray> paramCopy2 = paramCopy1;
QByteArray lastPart = params.last();
QByteArray splitter(" .,-");
for(int i = 0; i < splitter.size(); i++) {
splitPos = qMax(splitPos, lastPart.lastIndexOf(splitter[i], maxSplitPos));
}
-
- if(splitPos == -1) {
+ if(splitPos <= 0) {
splitPos = maxSplitPos;
}
putRawLine(msg);
}
+void NetworkConnection::sendPing() {
+ userInputHandler()->handlePing(BufferInfo(), QString());
+}
+
void NetworkConnection::sendAutoWho() {
while(!_autoWhoQueue.isEmpty()) {
QString chan = _autoWhoQueue.takeFirst();