/***************************************************************************
- * Copyright (C) 2005-2015 by the Quassel Project *
+ * Copyright (C) 2005-2018 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
_userInputHandler(new CoreUserInputHandler(this)),
_autoReconnectCount(0),
_quitRequested(false),
+ _disconnectExpected(false),
_previousConnectionAttemptFailed(false),
_lastUsedServerIndex(0),
connect(this, SIGNAL(newEvent(Event *)), coreSession()->eventManager(), SLOT(postEvent(Event *)));
if (Quassel::isOptionSet("oidentd")) {
- connect(this, SIGNAL(socketOpen(const CoreIdentity*, QHostAddress, quint16, QHostAddress, quint16)), Core::instance()->oidentdConfigGenerator(), SLOT(addSocket(const CoreIdentity*, QHostAddress, quint16, QHostAddress, quint16)), Qt::BlockingQueuedConnection);
+ connect(this, SIGNAL(socketInitialized(const CoreIdentity*, QHostAddress, quint16, QHostAddress, quint16)), Core::instance()->oidentdConfigGenerator(), SLOT(addSocket(const CoreIdentity*, QHostAddress, quint16, QHostAddress, quint16)), Qt::BlockingQueuedConnection);
connect(this, SIGNAL(socketDisconnected(const CoreIdentity*, QHostAddress, quint16, QHostAddress, quint16)), Core::instance()->oidentdConfigGenerator(), SLOT(removeSocket(const CoreIdentity*, QHostAddress, quint16, QHostAddress, quint16)));
}
}
CoreNetwork::~CoreNetwork()
{
- if (connectionState() != Disconnected && connectionState() != Network::Reconnecting)
- disconnectFromIrc(false); // clean up, but this does not count as requested disconnect!
+ // Request a proper disconnect, but don't count as user-requested disconnect
+ if (socketConnected()) {
+ // Only try if the socket's fully connected (not initializing or disconnecting).
+ // Force an immediate disconnect, jumping the command queue. Ensures the proper QUIT is
+ // shown even if other messages are queued.
+ disconnectFromIrc(false, QString(), false, true);
+ // Process the putCmd events that trigger the quit. Without this, shutting down the core
+ // results in abrubtly closing the socket rather than sending the QUIT as expected.
+ QCoreApplication::processEvents();
+ // Wait briefly for each network to disconnect. Sometimes it takes a little while to send.
+ if (!forceDisconnect()) {
+ qWarning() << "Timed out quitting network" << networkName() <<
+ "(user ID " << userId() << ")";
+ }
+ }
disconnect(&socket, 0, this, 0); // this keeps the socket from triggering events during clean up
delete _userInputHandler;
}
+bool CoreNetwork::forceDisconnect(int msecs)
+{
+ if (socket.state() == QAbstractSocket::UnconnectedState) {
+ // Socket already disconnected.
+ return true;
+ }
+ // Request a socket-level disconnect if not already happened
+ socket.disconnectFromHost();
+ // Return the result of waiting for disconnect; true if successful, otherwise false
+ return socket.waitForDisconnected(msecs);
+}
+
+
QString CoreNetwork::channelDecode(const QString &bufferName, const QByteArray &string) const
{
if (!bufferName.isEmpty()) {
}
else if (_previousConnectionAttemptFailed) {
// cycle to next server if previous connection attempt failed
+ _previousConnectionAttemptFailed = false;
displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Connection failed. Cycling to next Server"));
if (++_lastUsedServerIndex >= serverList().size()) {
_lastUsedServerIndex = 0;
}
}
- _previousConnectionAttemptFailed = false;
+ else {
+ // Start out with the top server in the list
+ _lastUsedServerIndex = 0;
+ }
Server server = usedServer();
displayStatusMsg(tr("Connecting to %1:%2...").arg(server.host).arg(server.port));
// 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);
-
+ if (! server.useProxy) {
+ //Avoid hostname lookups when a proxy is specified. The lookups won't use the proxy and may therefore leak the DNS
+ //hostname of the server. Qt's DNS cache also isn't used by the proxy so we don't need to refresh the entry.
+ QHostInfo::fromName(server.host);
+ }
#ifdef HAVE_SSL
if (server.useSsl) {
CoreIdentity *identity = identityPtr();
}
-void CoreNetwork::disconnectFromIrc(bool requested, const QString &reason, bool withReconnect)
+void CoreNetwork::disconnectFromIrc(bool requested, const QString &reason, bool withReconnect,
+ bool forceImmediate)
{
+ // Disconnecting from the network, should expect a socket close or error
+ _disconnectExpected = true;
_quitRequested = requested; // see socketDisconnected();
if (!withReconnect) {
_autoReconnectTimer.stop();
socketDisconnected();
} else {
if (socket.state() == QAbstractSocket::ConnectedState) {
- userInputHandler()->issueQuit(_quitReason);
+ userInputHandler()->issueQuit(_quitReason, forceImmediate);
} else {
socket.close();
}
}
-void CoreNetwork::putRawLine(QByteArray s)
+void CoreNetwork::putRawLine(const QByteArray s, const bool prepend)
{
- if (_tokenBucket > 0)
+ if (_tokenBucket > 0) {
writeToSocket(s);
- else
- _msgQueue.append(s);
+ } else {
+ if (prepend) {
+ _msgQueue.prepend(s);
+ } else {
+ _msgQueue.append(s);
+ }
+ }
}
-void CoreNetwork::putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix)
+void CoreNetwork::putCmd(const QString &cmd, const QList<QByteArray> ¶ms, const QByteArray &prefix, const bool prepend)
{
QByteArray msg;
msg += params[i];
}
- putRawLine(msg);
+ putRawLine(msg, prepend);
}
-void CoreNetwork::putCmd(const QString &cmd, const QList<QList<QByteArray>> ¶ms, const QByteArray &prefix)
+void CoreNetwork::putCmd(const QString &cmd, const QList<QList<QByteArray>> ¶ms, const QByteArray &prefix, const bool prependAll)
{
QListIterator<QList<QByteArray>> i(params);
while (i.hasNext()) {
QList<QByteArray> msg = i.next();
- putCmd(cmd, msg, prefix);
+ putCmd(cmd, msg, prefix, prependAll);
}
}
void CoreNetwork::socketError(QAbstractSocket::SocketError error)
{
- if (_quitRequested && error == QAbstractSocket::RemoteHostClosedError)
+ // Ignore socket closed errors if expected
+ if (_disconnectExpected && error == QAbstractSocket::RemoteHostClosedError) {
return;
+ }
_previousConnectionAttemptFailed = true;
qWarning() << qPrintable(tr("Could not connect to %1 (%2)").arg(networkName(), socket.errorString()));
return;
}
- emit socketOpen(identity, localAddress(), localPort(), peerAddress(), peerPort());
-
Server server = usedServer();
+
#ifdef HAVE_SSL
- if (server.useSsl && !socket.isEncrypted())
+ // Non-SSL connections enter here only once, always emit socketInitialized(...) in these cases
+ // SSL connections call socketInitialized() twice, only emit socketInitialized(...) on the first (not yet encrypted) run
+ if (!server.useSsl || !socket.isEncrypted()) {
+ emit socketInitialized(identity, localAddress(), localPort(), peerAddress(), peerPort());
+ }
+
+ if (server.useSsl && !socket.isEncrypted()) {
+ // We'll finish setup once we're encrypted, and called again
return;
+ }
+#else
+ emit socketInitialized(identity, localAddress(), localPort(), peerAddress(), peerPort());
#endif
- socket.setSocketOption(QAbstractSocket::KeepAliveOption, true);
- emit socketInitialized(identity, localAddress(), localPort(), peerAddress(), peerPort());
+ socket.setSocketOption(QAbstractSocket::KeepAliveOption, true);
// TokenBucket to avoid sending too much at once
_messageDelay = 2200; // this seems to be a safe value (2.2 seconds delay)
else {
nick = identity->nicks()[0];
}
- putRawLine(serverEncode(QString("NICK :%1").arg(nick)));
+ putRawLine(serverEncode(QString("NICK %1").arg(nick)));
putRawLine(serverEncode(QString("USER %1 8 * :%2").arg(identity->ident(), identity->realName())));
}
setConnected(false);
emit disconnected(networkId());
emit socketDisconnected(identityPtr(), localAddress(), localPort(), peerAddress(), peerPort());
+ // Reset disconnect expectations
+ _disconnectExpected = false;
if (_quitRequested) {
_quitRequested = false;
setConnectionState(Network::Disconnected);
{
setConnectionState(Network::Initialized);
setConnected(true);
+ _disconnectExpected = false;
_quitRequested = false;
if (useAutoReconnect()) {