2019-09-08 22:27:57 +02:00
|
|
|
#include "AbstractIrcServer.hpp"
|
|
|
|
|
|
|
|
#include "common/Channel.hpp"
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "messages/LimitedQueueSnapshot.hpp"
|
|
|
|
#include "messages/Message.hpp"
|
|
|
|
#include "messages/MessageBuilder.hpp"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
const int RECONNECT_BASE_INTERVAL = 2000;
|
|
|
|
// 60 falloff counter means it will try to reconnect at most every 60*2 seconds
|
|
|
|
const int MAX_FALLOFF_COUNTER = 60;
|
|
|
|
|
2021-08-04 23:18:34 +02:00
|
|
|
// Ratelimits for joinBucket_
|
|
|
|
const int JOIN_RATELIMIT_BUDGET = 18;
|
2021-09-04 12:45:40 +02:00
|
|
|
const int JOIN_RATELIMIT_COOLDOWN = 12500;
|
2021-08-04 23:18:34 +02:00
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
AbstractIrcServer::AbstractIrcServer()
|
|
|
|
{
|
|
|
|
// Initialize the connections
|
2021-06-06 16:25:13 +02:00
|
|
|
// XXX: don't create write connection if there is no separate write connection.
|
2019-09-08 22:27:57 +02:00
|
|
|
this->writeConnection_.reset(new IrcConnection);
|
|
|
|
this->writeConnection_->moveToThread(
|
|
|
|
QCoreApplication::instance()->thread());
|
|
|
|
|
2021-08-04 23:18:34 +02:00
|
|
|
// Apply a leaky bucket rate limiting to JOIN messages
|
|
|
|
auto actuallyJoin = [&](QString message) {
|
|
|
|
if (!this->channels.contains(message))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->readConnection_->sendRaw("JOIN #" + message);
|
|
|
|
};
|
|
|
|
this->joinBucket_.reset(new RatelimitBucket(
|
|
|
|
JOIN_RATELIMIT_BUDGET, JOIN_RATELIMIT_COOLDOWN, actuallyJoin, this));
|
|
|
|
|
2020-11-08 12:02:19 +01:00
|
|
|
QObject::connect(this->writeConnection_.get(),
|
|
|
|
&Communi::IrcConnection::messageReceived, this,
|
|
|
|
[this](auto msg) {
|
|
|
|
this->writeConnectionMessageReceived(msg);
|
|
|
|
});
|
|
|
|
QObject::connect(this->writeConnection_.get(),
|
|
|
|
&Communi::IrcConnection::connected, this, [this] {
|
|
|
|
this->onWriteConnected(this->writeConnection_.get());
|
|
|
|
});
|
2021-12-19 15:57:56 +01:00
|
|
|
this->connections_.managedConnect(
|
|
|
|
this->writeConnection_->connectionLost, [this](bool timeout) {
|
|
|
|
qCDebug(chatterinoIrc)
|
|
|
|
<< "Write connection reconnect requested. Timeout:" << timeout;
|
|
|
|
this->writeConnection_->smartReconnect.invoke();
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
// Listen to read connection message signals
|
|
|
|
this->readConnection_.reset(new IrcConnection);
|
|
|
|
this->readConnection_->moveToThread(QCoreApplication::instance()->thread());
|
|
|
|
|
2020-11-08 12:02:19 +01:00
|
|
|
QObject::connect(this->readConnection_.get(),
|
|
|
|
&Communi::IrcConnection::messageReceived, this,
|
|
|
|
[this](auto msg) {
|
|
|
|
this->readConnectionMessageReceived(msg);
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
QObject::connect(this->readConnection_.get(),
|
2019-09-18 13:03:16 +02:00
|
|
|
&Communi::IrcConnection::privateMessageReceived, this,
|
2020-11-08 12:02:19 +01:00
|
|
|
[this](auto msg) {
|
|
|
|
this->privateMessageReceived(msg);
|
|
|
|
});
|
|
|
|
QObject::connect(this->readConnection_.get(),
|
|
|
|
&Communi::IrcConnection::connected, this, [this] {
|
|
|
|
this->onReadConnected(this->readConnection_.get());
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
QObject::connect(this->readConnection_.get(),
|
2020-11-08 12:02:19 +01:00
|
|
|
&Communi::IrcConnection::disconnected, this, [this] {
|
|
|
|
this->onDisconnected();
|
|
|
|
});
|
2021-12-19 15:57:56 +01:00
|
|
|
this->connections_.managedConnect(
|
|
|
|
this->readConnection_->connectionLost, [this](bool timeout) {
|
|
|
|
qCDebug(chatterinoIrc)
|
|
|
|
<< "Read connection reconnect requested. Timeout:" << timeout;
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
// Show additional message since this is going to interrupt a
|
|
|
|
// connection that is still "connected"
|
|
|
|
this->addGlobalSystemMessage(
|
|
|
|
"Server connection timed out, reconnecting");
|
|
|
|
}
|
|
|
|
this->readConnection_->smartReconnect.invoke();
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 12:05:08 +01:00
|
|
|
void AbstractIrcServer::initializeIrc()
|
|
|
|
{
|
|
|
|
assert(!this->initialized_);
|
|
|
|
|
|
|
|
if (this->hasSeparateWriteConnection())
|
|
|
|
{
|
|
|
|
this->initializeConnectionSignals(this->writeConnection_.get(),
|
|
|
|
ConnectionType::Write);
|
|
|
|
this->initializeConnectionSignals(this->readConnection_.get(),
|
|
|
|
ConnectionType::Read);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->initializeConnectionSignals(this->readConnection_.get(),
|
|
|
|
ConnectionType::Both);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->initialized_ = true;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
void AbstractIrcServer::connect()
|
|
|
|
{
|
2020-03-01 12:05:08 +01:00
|
|
|
assert(this->initialized_);
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
this->disconnect();
|
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
if (this->hasSeparateWriteConnection())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
this->initializeConnection(this->writeConnection_.get(), Write);
|
|
|
|
this->initializeConnection(this->readConnection_.get(), Read);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
this->initializeConnection(this->readConnection_.get(), Both);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
2019-09-18 13:03:16 +02:00
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
void AbstractIrcServer::open(ConnectionType type)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->connectionMutex_);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
if (type == Write)
|
|
|
|
{
|
2019-09-08 22:27:57 +02:00
|
|
|
this->writeConnection_->open();
|
2019-09-18 13:03:16 +02:00
|
|
|
}
|
|
|
|
if (type & Read)
|
|
|
|
{
|
2019-09-08 22:27:57 +02:00
|
|
|
this->readConnection_->open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 22:14:25 +01:00
|
|
|
void AbstractIrcServer::addGlobalSystemMessage(const QString &messageText)
|
2021-01-09 18:05:02 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
MessageBuilder b(systemMessage, messageText);
|
|
|
|
auto message = b.release();
|
|
|
|
|
|
|
|
for (std::weak_ptr<Channel> &weak : this->channels.values())
|
|
|
|
{
|
|
|
|
std::shared_ptr<Channel> chan = weak.lock();
|
|
|
|
if (!chan)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
chan->addMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
void AbstractIrcServer::disconnect()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> locker(this->connectionMutex_);
|
|
|
|
|
|
|
|
this->readConnection_->close();
|
2019-09-18 13:03:16 +02:00
|
|
|
if (this->hasSeparateWriteConnection())
|
|
|
|
{
|
|
|
|
this->writeConnection_->close();
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::sendMessage(const QString &channelName,
|
|
|
|
const QString &message)
|
|
|
|
{
|
|
|
|
this->sendRawMessage("PRIVMSG #" + channelName + " :" + message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::sendRawMessage(const QString &rawMessage)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> locker(this->connectionMutex_);
|
|
|
|
|
|
|
|
if (this->hasSeparateWriteConnection())
|
|
|
|
{
|
|
|
|
this->writeConnection_->sendRaw(rawMessage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->readConnection_->sendRaw(rawMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::writeConnectionMessageReceived(
|
|
|
|
Communi::IrcMessage *message)
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
(void)message;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
ChannelPtr AbstractIrcServer::getOrAddChannel(const QString &dirtyChannelName)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
auto channelName = this->cleanChannelName(dirtyChannelName);
|
|
|
|
|
|
|
|
// try get channel
|
|
|
|
ChannelPtr chan = this->getChannelOrEmpty(channelName);
|
|
|
|
if (chan != Channel::getEmpty())
|
|
|
|
{
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
// value doesn't exist
|
|
|
|
chan = this->createChannel(channelName);
|
|
|
|
if (!chan)
|
|
|
|
{
|
|
|
|
return Channel::getEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->channels.insert(channelName, chan);
|
2021-12-19 15:57:56 +01:00
|
|
|
this->connections_.managedConnect(chan->destroyed, [this, channelName] {
|
|
|
|
// fourtf: issues when the server itself is destroyed
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2021-12-19 15:57:56 +01:00
|
|
|
qCDebug(chatterinoIrc) << "[AbstractIrcServer::addChannel]"
|
|
|
|
<< channelName << "was destroyed";
|
|
|
|
this->channels.remove(channelName);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2021-12-19 15:57:56 +01:00
|
|
|
if (this->readConnection_)
|
|
|
|
{
|
|
|
|
this->readConnection_->sendRaw("PART #" + channelName);
|
|
|
|
}
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2021-10-17 15:06:58 +02:00
|
|
|
// join IRC channel
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock2(this->connectionMutex_);
|
|
|
|
|
|
|
|
if (this->readConnection_)
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
if (this->readConnection_->isConnected())
|
|
|
|
{
|
2021-08-04 23:18:34 +02:00
|
|
|
this->joinBucket_->send(channelName);
|
2019-09-18 13:03:16 +02:00
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
ChannelPtr AbstractIrcServer::getChannelOrEmpty(const QString &dirtyChannelName)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
auto channelName = this->cleanChannelName(dirtyChannelName);
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
// try get special channel
|
|
|
|
ChannelPtr chan = this->getCustomChannel(channelName);
|
|
|
|
if (chan)
|
|
|
|
{
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
// value exists
|
|
|
|
auto it = this->channels.find(channelName);
|
|
|
|
if (it != this->channels.end())
|
|
|
|
{
|
|
|
|
chan = it.value().lock();
|
|
|
|
|
|
|
|
if (chan)
|
|
|
|
{
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Channel::getEmpty();
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
std::vector<std::weak_ptr<Channel>> AbstractIrcServer::getChannels()
|
|
|
|
{
|
|
|
|
std::lock_guard lock(this->channelMutex);
|
|
|
|
std::vector<std::weak_ptr<Channel>> channels;
|
|
|
|
|
|
|
|
for (auto &&weak : this->channels.values())
|
|
|
|
{
|
|
|
|
channels.push_back(weak);
|
|
|
|
}
|
|
|
|
|
|
|
|
return channels;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
void AbstractIrcServer::onReadConnected(IrcConnection *connection)
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
(void)connection;
|
|
|
|
|
|
|
|
std::lock_guard lock(this->channelMutex);
|
|
|
|
|
|
|
|
// join channels
|
|
|
|
for (auto &&weak : this->channels)
|
|
|
|
{
|
|
|
|
if (auto channel = weak.lock())
|
|
|
|
{
|
2021-08-04 23:18:34 +02:00
|
|
|
this->joinBucket_->send(channel->getName());
|
2019-09-18 13:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2019-09-18 13:03:16 +02:00
|
|
|
// connected/disconnected message
|
2019-09-08 22:27:57 +02:00
|
|
|
auto connectedMsg = makeSystemMessage("connected");
|
|
|
|
connectedMsg->flags.set(MessageFlag::ConnectedMessage);
|
|
|
|
auto reconnected = makeSystemMessage("reconnected");
|
|
|
|
reconnected->flags.set(MessageFlag::ConnectedMessage);
|
|
|
|
|
|
|
|
for (std::weak_ptr<Channel> &weak : this->channels.values())
|
|
|
|
{
|
|
|
|
std::shared_ptr<Channel> chan = weak.lock();
|
|
|
|
if (!chan)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LimitedQueueSnapshot<MessagePtr> snapshot = chan->getMessageSnapshot();
|
|
|
|
|
|
|
|
bool replaceMessage =
|
|
|
|
snapshot.size() > 0 && snapshot[snapshot.size() - 1]->flags.has(
|
|
|
|
MessageFlag::DisconnectedMessage);
|
|
|
|
|
|
|
|
if (replaceMessage)
|
|
|
|
{
|
|
|
|
chan->replaceMessage(snapshot[snapshot.size() - 1], reconnected);
|
2022-08-06 18:18:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chan->addMessage(connectedMsg);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 18:18:34 +02:00
|
|
|
chan->connected.invoke();
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->falloffCounter_ = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::onWriteConnected(IrcConnection *connection)
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
(void)connection;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::onDisconnected()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
MessageBuilder b(systemMessage, "disconnected");
|
|
|
|
b->flags.set(MessageFlag::DisconnectedMessage);
|
|
|
|
auto disconnectedMsg = b.release();
|
|
|
|
|
|
|
|
for (std::weak_ptr<Channel> &weak : this->channels.values())
|
|
|
|
{
|
|
|
|
std::shared_ptr<Channel> chan = weak.lock();
|
|
|
|
if (!chan)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
chan->addMessage(disconnectedMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(
|
|
|
|
const QString &channelName)
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
(void)channelName;
|
2019-09-08 22:27:57 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AbstractIrcServer::cleanChannelName(const QString &dirtyChannelName)
|
|
|
|
{
|
2022-11-16 17:54:59 +01:00
|
|
|
// This function is a Noop only for IRC, for Twitch it removes a leading '#' and lowercases the name
|
|
|
|
return dirtyChannelName;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::addFakeMessage(const QString &data)
|
|
|
|
{
|
|
|
|
auto fakeMessage = Communi::IrcMessage::fromData(
|
|
|
|
data.toUtf8(), this->readConnection_.get());
|
|
|
|
|
|
|
|
if (fakeMessage->command() == "PRIVMSG")
|
|
|
|
{
|
|
|
|
this->privateMessageReceived(
|
|
|
|
static_cast<Communi::IrcPrivateMessage *>(fakeMessage));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->readConnectionMessageReceived(fakeMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::privateMessageReceived(
|
|
|
|
Communi::IrcPrivateMessage *message)
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
(void)message;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::readConnectionMessageReceived(
|
|
|
|
Communi::IrcMessage *message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::forEachChannel(std::function<void(ChannelPtr)> func)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
|
|
|
for (std::weak_ptr<Channel> &weak : this->channels.values())
|
|
|
|
{
|
2019-09-18 13:03:16 +02:00
|
|
|
ChannelPtr chan = weak.lock();
|
2019-09-08 22:27:57 +02:00
|
|
|
if (!chan)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
func(chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|