Remove experimental IRC support (#5547)

This commit is contained in:
pajlada
2024-08-18 14:04:26 +02:00
committed by GitHub
parent cc8bd538b9
commit 998920d244
44 changed files with 478 additions and 2841 deletions
+7 -10
View File
@@ -14,7 +14,6 @@
#include "messages/MessageColor.hpp"
#include "messages/MessageElement.hpp"
#include "messages/MessageThread.hpp"
#include "providers/irc/AbstractIrcServer.hpp"
#include "providers/twitch/ChannelPointReward.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchAccountManager.hpp"
@@ -171,7 +170,7 @@ void updateReplyParticipatedStatus(const QVariantMap &tags,
}
ChannelPtr channelOrEmptyByTarget(const QString &target,
IAbstractIrcServer &server)
ITwitchIrcServer &server)
{
QString channelName;
if (!trimChannelName(target, channelName))
@@ -679,10 +678,9 @@ std::vector<MessagePtr> IrcMessageHandler::parseMessageWithReply(
}
void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message,
ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer)
ITwitchIrcServer &twitchServer)
{
auto chan = channelOrEmptyByTarget(message->target(), abstractIrcServer);
auto chan = channelOrEmptyByTarget(message->target(), twitchServer);
if (chan->isEmpty())
{
return;
@@ -988,9 +986,8 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *ircMessage)
}
}
void IrcMessageHandler::handleUserNoticeMessage(
Communi::IrcMessage *message, ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer)
void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
ITwitchIrcServer &twitchServer)
{
auto tags = message->tags();
auto parameters = message->parameters();
@@ -1003,7 +1000,7 @@ void IrcMessageHandler::handleUserNoticeMessage(
content = parameters[1];
}
auto chn = abstractIrcServer.getChannelOrEmpty(target);
auto chn = twitchServer.getChannelOrEmpty(target);
if (isIgnoredMessage({
.message = content,
.twitchUserID = tags.value("user-id").toString(),
@@ -1096,7 +1093,7 @@ void IrcMessageHandler::handleUserNoticeMessage(
return;
}
auto chan = abstractIrcServer.getChannelOrEmpty(channelName);
auto chan = twitchServer.getChannelOrEmpty(channelName);
if (!chan->isEmpty())
{
+2 -5
View File
@@ -9,7 +9,6 @@
namespace chatterino {
class IAbstractIrcServer;
class ITwitchIrcServer;
class Channel;
using ChannelPtr = std::shared_ptr<Channel>;
@@ -39,8 +38,7 @@ public:
std::vector<MessagePtr> &otherLoaded);
void handlePrivMessage(Communi::IrcPrivateMessage *message,
ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer);
ITwitchIrcServer &twitchServer);
void handleRoomStateMessage(Communi::IrcMessage *message);
void handleClearChatMessage(Communi::IrcMessage *message);
@@ -50,8 +48,7 @@ public:
void handleWhisperMessage(Communi::IrcMessage *ircMessage);
void handleUserNoticeMessage(Communi::IrcMessage *message,
ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer);
ITwitchIrcServer &twitchServer);
void handleNoticeMessage(Communi::IrcNoticeMessage *message);
-1
View File
@@ -9,7 +9,6 @@
#include "debug/AssertInGuiThread.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/irc/IrcMessageBuilder.hpp"
#include "providers/IvrApi.hpp"
#include "providers/seventv/SeventvAPI.hpp"
#include "providers/twitch/api/Helix.hpp"
+348 -19
View File
@@ -2,35 +2,45 @@
#include "Application.hpp"
#include "common/Channel.hpp"
#include "common/Common.hpp"
#include "common/Env.hpp"
#include "common/QLogging.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/bttv/BttvLiveUpdates.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/seventv/eventapi/Subscription.hpp"
#include "providers/irc/IrcConnection2.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
#include "providers/seventv/SeventvEventAPI.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/ChannelPointReward.hpp"
#include "providers/twitch/IrcMessageHandler.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Settings.hpp"
#include "util/Helpers.hpp"
#include "util/PostToThread.hpp"
#include "util/RatelimitBucket.hpp"
#include <IrcCommand>
#include <IrcMessage>
#include <pajlada/signals/signal.hpp>
#include <pajlada/signals/signalholder.hpp>
#include <QCoreApplication>
#include <QMetaEnum>
#include <cassert>
#include <functional>
#include <mutex>
using namespace std::chrono_literals;
namespace {
// Ratelimits for joinBucket_
constexpr int JOIN_RATELIMIT_BUDGET = 18;
constexpr int JOIN_RATELIMIT_COOLDOWN = 12500;
using namespace chatterino;
void sendHelixMessage(const std::shared_ptr<TwitchChannel> &channel,
@@ -140,12 +150,77 @@ TwitchIrcServer::TwitchIrcServer()
, automodChannel(new Channel("/automod", Channel::Type::TwitchAutomod))
, watchingChannel(Channel::getEmpty(), Channel::Type::TwitchWatching)
{
this->initializeIrc();
// Initialize the connections
// XXX: don't create write connection if there is no separate write connection.
this->writeConnection_.reset(new IrcConnection);
this->writeConnection_->moveToThread(
QCoreApplication::instance()->thread());
// getSettings()->twitchSeperateWriteConnection.connect([this](auto, auto) {
// this->connect(); },
// this->signalHolder_,
// false);
// 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));
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());
});
this->connections_.managedConnect(
this->writeConnection_->connectionLost, [this](bool timeout) {
qCDebug(chatterinoIrc)
<< "Write connection reconnect requested. Timeout:" << timeout;
this->writeConnection_->smartReconnect();
});
// Listen to read connection message signals
this->readConnection_.reset(new IrcConnection);
this->readConnection_->moveToThread(QCoreApplication::instance()->thread());
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::messageReceived, this,
[this](auto msg) {
this->readConnectionMessageReceived(msg);
});
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::privateMessageReceived, this,
[this](auto msg) {
this->privateMessageReceived(msg);
});
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::connected, this, [this] {
this->onReadConnected(this->readConnection_.get());
});
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::disconnected, this, [this] {
this->onDisconnected();
});
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();
});
this->connections_.managedConnect(this->readConnection_->heartbeat, [this] {
this->markChannelsConnected();
});
}
void TwitchIrcServer::initialize()
@@ -246,14 +321,12 @@ std::shared_ptr<Channel> TwitchIrcServer::createChannel(
void TwitchIrcServer::privateMessageReceived(
Communi::IrcPrivateMessage *message)
{
IrcMessageHandler::instance().handlePrivMessage(message, *this, *this);
IrcMessageHandler::instance().handlePrivMessage(message, *this);
}
void TwitchIrcServer::readConnectionMessageReceived(
Communi::IrcMessage *message)
{
AbstractIrcServer::readConnectionMessageReceived(message);
if (message->type() == Communi::IrcMessage::Type::Private)
{
// We already have a handler for private messages
@@ -293,7 +366,7 @@ void TwitchIrcServer::readConnectionMessageReceived(
}
else if (command == "USERNOTICE")
{
handler.handleUserNoticeMessage(message, *this, *this);
handler.handleUserNoticeMessage(message, *this);
}
else if (command == "NOTICE")
{
@@ -344,6 +417,84 @@ void TwitchIrcServer::writeConnectionMessageReceived(
}
}
void TwitchIrcServer::onReadConnected(IrcConnection *connection)
{
(void)connection;
std::lock_guard lock(this->channelMutex);
// join channels
for (auto &&weak : this->channels)
{
if (auto channel = weak.lock())
{
this->joinBucket_->send(channel->getName());
}
}
// connected/disconnected message
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);
}
else
{
chan->addMessage(connectedMsg, MessageContext::Original);
}
}
this->falloffCounter_ = 1;
}
void TwitchIrcServer::onWriteConnected(IrcConnection *connection)
{
(void)connection;
}
void TwitchIrcServer::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, MessageContext::Original);
if (auto *channel = dynamic_cast<TwitchChannel *>(chan.get()))
{
channel->markDisconnected();
}
}
}
std::shared_ptr<Channel> TwitchIrcServer::getCustomChannel(
const QString &channelName)
{
@@ -521,12 +672,6 @@ QString TwitchIrcServer::cleanChannelName(const QString &dirtyChannelName)
}
}
bool TwitchIrcServer::hasSeparateWriteConnection() const
{
return true;
// return getSettings()->twitchSeperateWriteConnection;
}
bool TwitchIrcServer::prepareToSend(
const std::shared_ptr<TwitchChannel> &channel)
{
@@ -779,4 +924,188 @@ void TwitchIrcServer::dropSeventvChannel(const QString &userID,
}
}
void TwitchIrcServer::markChannelsConnected()
{
this->forEachChannel([](const ChannelPtr &chan) {
if (auto *channel = dynamic_cast<TwitchChannel *>(chan.get()))
{
channel->markConnected();
}
});
}
void TwitchIrcServer::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 TwitchIrcServer::addGlobalSystemMessage(const QString &messageText)
{
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, MessageContext::Original);
}
}
void TwitchIrcServer::forEachChannel(std::function<void(ChannelPtr)> func)
{
std::lock_guard<std::mutex> lock(this->channelMutex);
for (std::weak_ptr<Channel> &weak : this->channels.values())
{
ChannelPtr chan = weak.lock();
if (!chan)
{
continue;
}
func(chan);
}
}
void TwitchIrcServer::connect()
{
this->disconnect();
this->initializeConnection(this->writeConnection_.get(),
ConnectionType::Write);
this->initializeConnection(this->readConnection_.get(),
ConnectionType::Read);
}
void TwitchIrcServer::disconnect()
{
std::lock_guard<std::mutex> locker(this->connectionMutex_);
this->readConnection_->close();
this->writeConnection_->close();
}
void TwitchIrcServer::sendMessage(const QString &channelName,
const QString &message)
{
this->sendRawMessage("PRIVMSG #" + channelName + " :" + message);
}
void TwitchIrcServer::sendRawMessage(const QString &rawMessage)
{
std::lock_guard<std::mutex> locker(this->connectionMutex_);
this->writeConnection_->sendRaw(rawMessage);
}
ChannelPtr TwitchIrcServer::getOrAddChannel(const QString &dirtyChannelName)
{
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);
this->connections_.managedConnect(chan->destroyed, [this, channelName] {
// fourtf: issues when the server itself is destroyed
qCDebug(chatterinoIrc) << "[TwitchIrcServer::addChannel]" << channelName
<< "was destroyed";
this->channels.remove(channelName);
if (this->readConnection_)
{
this->readConnection_->sendRaw("PART #" + channelName);
}
});
// join IRC channel
{
std::lock_guard<std::mutex> lock2(this->connectionMutex_);
if (this->readConnection_)
{
if (this->readConnection_->isConnected())
{
this->joinBucket_->send(channelName);
}
}
}
return chan;
}
ChannelPtr TwitchIrcServer::getChannelOrEmpty(const QString &dirtyChannelName)
{
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();
}
void TwitchIrcServer::open(ConnectionType type)
{
std::lock_guard<std::mutex> lock(this->connectionMutex_);
if (type == ConnectionType::Write)
{
this->writeConnection_->open();
}
if (type == ConnectionType::Read)
{
this->readConnection_->open();
}
}
} // namespace chatterino
+80 -12
View File
@@ -2,12 +2,18 @@
#include "common/Atomic.hpp"
#include "common/Channel.hpp"
#include "providers/irc/AbstractIrcServer.hpp"
#include "common/Common.hpp"
#include "providers/irc/IrcConnection2.hpp"
#include "util/RatelimitBucket.hpp"
#include <IrcMessage>
#include <pajlada/signals/signal.hpp>
#include <pajlada/signals/signalholder.hpp>
#include <chrono>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
namespace chatterino {
@@ -18,11 +24,30 @@ class TwitchChannel;
class BttvEmotes;
class FfzEmotes;
class SeventvEmotes;
class RatelimitBucket;
class ITwitchIrcServer
{
public:
ITwitchIrcServer() = default;
virtual ~ITwitchIrcServer() = default;
ITwitchIrcServer(const ITwitchIrcServer &) = delete;
ITwitchIrcServer(ITwitchIrcServer &&) = delete;
ITwitchIrcServer &operator=(const ITwitchIrcServer &) = delete;
ITwitchIrcServer &operator=(ITwitchIrcServer &&) = delete;
virtual void connect() = 0;
virtual void sendRawMessage(const QString &rawMessage) = 0;
virtual ChannelPtr getOrAddChannel(const QString &dirtyChannelName) = 0;
virtual ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) = 0;
virtual void addFakeMessage(const QString &data) = 0;
virtual void addGlobalSystemMessage(const QString &messageText) = 0;
virtual void forEachChannel(std::function<void(ChannelPtr)> func) = 0;
virtual void forEachChannelAndSpecialChannels(
std::function<void(ChannelPtr)> func) = 0;
@@ -46,9 +71,14 @@ public:
// Update this interface with TwitchIrcServer methods as needed
};
class TwitchIrcServer final : public AbstractIrcServer, public ITwitchIrcServer
class TwitchIrcServer final : public ITwitchIrcServer, public QObject
{
public:
enum class ConnectionType {
Read,
Write,
};
TwitchIrcServer();
~TwitchIrcServer() override = default;
@@ -88,6 +118,25 @@ public:
void dropSeventvChannel(const QString &userID,
const QString &emoteSetID) override;
void addFakeMessage(const QString &data) override;
void addGlobalSystemMessage(const QString &messageText) override;
// iteration
void forEachChannel(std::function<void(ChannelPtr)> func) override;
void connect() override;
void disconnect();
void sendMessage(const QString &channelName, const QString &message);
void sendRawMessage(const QString &rawMessage) override;
ChannelPtr getOrAddChannel(const QString &dirtyChannelName) override;
ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) override;
void open(ConnectionType type);
private:
Atomic<QString> lastUserThatWhisperedMe;
@@ -109,19 +158,21 @@ public:
void setLastUserThatWhisperedMe(const QString &user) override;
protected:
void initializeConnection(IrcConnection *connection,
ConnectionType type) override;
std::shared_ptr<Channel> createChannel(const QString &channelName) override;
void initializeConnection(IrcConnection *connection, ConnectionType type);
std::shared_ptr<Channel> createChannel(const QString &channelName);
void privateMessageReceived(Communi::IrcPrivateMessage *message) override;
void readConnectionMessageReceived(Communi::IrcMessage *message) override;
void writeConnectionMessageReceived(Communi::IrcMessage *message) override;
void privateMessageReceived(Communi::IrcPrivateMessage *message);
void readConnectionMessageReceived(Communi::IrcMessage *message);
void writeConnectionMessageReceived(Communi::IrcMessage *message);
std::shared_ptr<Channel> getCustomChannel(
const QString &channelname) override;
void onReadConnected(IrcConnection *connection);
void onWriteConnected(IrcConnection *connection);
void onDisconnected();
void markChannelsConnected();
QString cleanChannelName(const QString &dirtyChannelName) override;
bool hasSeparateWriteConnection() const override;
std::shared_ptr<Channel> getCustomChannel(const QString &channelname);
QString cleanChannelName(const QString &dirtyChannelName);
private:
void onMessageSendRequested(const std::shared_ptr<TwitchChannel> &channel,
@@ -132,6 +183,23 @@ private:
bool prepareToSend(const std::shared_ptr<TwitchChannel> &channel);
QMap<QString, std::weak_ptr<Channel>> channels;
std::mutex channelMutex;
QObjectPtr<IrcConnection> writeConnection_ = nullptr;
QObjectPtr<IrcConnection> readConnection_ = nullptr;
// Our rate limiting bucket for the Twitch join rate limits
// https://dev.twitch.tv/docs/irc/guide#rate-limits
QObjectPtr<RatelimitBucket> joinBucket_;
QTimer reconnectTimer_;
int falloffCounter_ = 1;
std::mutex connectionMutex_;
pajlada::Signals::SignalHolder connections_;
std::mutex lastMessageMutex_;
std::queue<std::chrono::steady_clock::time_point> lastMessagePleb_;
std::queue<std::chrono::steady_clock::time_point> lastMessageMod_;