From 3cfb00d61fc9cbf8714a23df4d6694c71cc3109f Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 16 Dec 2017 19:46:27 +0100 Subject: [PATCH] We now handle some write-connection messages like if users have been banned/timed out successfully by the user, or unbanned etc --- src/ircmanager.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ src/ircmanager.hpp | 3 +++ 2 files changed, 56 insertions(+) diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 30d53e628..1e2bd605c 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -45,6 +45,9 @@ IrcManager::IrcManager(ChannelManager &_channelManager, Resources &_resources, this->writeConnection.reset(new Communi::IrcConnection); this->writeConnection->moveToThread(QCoreApplication::instance()->thread()); + QObject::connect(this->writeConnection.get(), &Communi::IrcConnection::messageReceived, this, + &IrcManager::writeConnectionMessageReceived); + this->readConnection.reset(new Communi::IrcConnection); this->readConnection->moveToThread(QCoreApplication::instance()->thread()); @@ -106,6 +109,12 @@ void IrcManager::initializeConnection(const std::unique_ptrsendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands")); connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags")); + } else { + connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags")); + + connection->sendCommand( + Communi::IrcCommand::createCapability("REQ", "twitch.tv/membership")); + connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands")); } connection->setHost("irc.chat.twitch.tv"); @@ -247,6 +256,16 @@ void IrcManager::messageReceived(Communi::IrcMessage *message) } } +void IrcManager::writeConnectionMessageReceived(Communi::IrcMessage *message) +{ + switch (message->type()) { + case Communi::IrcMessage::Type::Notice: { + this->handleWriteConnectionNoticeMessage( + static_cast(message)); + } break; + } +} + void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message) { const auto &tags = message->tags(); @@ -440,6 +459,40 @@ void IrcManager::handleNoticeMessage(Communi::IrcNoticeMessage *message) c->addMessage(msg); } +void IrcManager::handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message) +{ + auto rawChannelName = message->target(); + + assert(rawChannelName.length() >= 2); + + auto trimmedChannelName = rawChannelName.mid(1); + + auto c = this->channelManager.getTwitchChannel(trimmedChannelName); + + if (!c) { + debug::Log("[IrcManager:handleNoticeMessage] Channel {} not found in channel manager", + trimmedChannelName); + return; + } + + QVariant v = message->tag("msg-id"); + if (!v.isValid()) { + return; + } + QString msg_id = v.toString(); + + static QList idsToSkip = {"timeout_success", "ban_success"}; + + if (idsToSkip.contains(msg_id)) { + // Already handled in the read-connection + return; + } + + std::shared_ptr msg(Message::createSystemMessage(message->content())); + + c->addMessage(msg); +} + void IrcManager::onConnected() { std::shared_ptr msg(Message::createSystemMessage("connected to chat")); diff --git a/src/ircmanager.hpp b/src/ircmanager.hpp index ca247c9fd..ac143a04f 100644 --- a/src/ircmanager.hpp +++ b/src/ircmanager.hpp @@ -79,6 +79,8 @@ private: void privateMessageReceived(Communi::IrcPrivateMessage *message); void messageReceived(Communi::IrcMessage *message); + void writeConnectionMessageReceived(Communi::IrcMessage *message); + void handleRoomStateMessage(Communi::IrcMessage *message); void handleClearChatMessage(Communi::IrcMessage *message); void handleUserStateMessage(Communi::IrcMessage *message); @@ -86,6 +88,7 @@ private: void handleUserNoticeMessage(Communi::IrcMessage *message); void handleModeMessage(Communi::IrcMessage *message); void handleNoticeMessage(Communi::IrcNoticeMessage *message); + void handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message); void onConnected(); void onDisconnected();