From 424eeb05d9831b3af071534033521ff9d0369fc3 Mon Sep 17 00:00:00 2001 From: Date: Mon, 1 Jan 2018 23:54:54 +0100 Subject: [PATCH] Created helper class IrcMessageHandler --- chatterino.pro | 6 +- src/singletons/helper/ircmessagehandler.cpp | 186 ++++++++++++++++++++ src/singletons/helper/ircmessagehandler.hpp | 32 ++++ src/singletons/ircmanager.cpp | 168 ++---------------- src/singletons/ircmanager.hpp | 9 - 5 files changed, 232 insertions(+), 169 deletions(-) create mode 100644 src/singletons/helper/ircmessagehandler.cpp create mode 100644 src/singletons/helper/ircmessagehandler.hpp diff --git a/chatterino.pro b/chatterino.pro index 5e4794b42..b7d3c74a8 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -111,7 +111,8 @@ SOURCES += \ src/singletons/thememanager.cpp \ src/twitch/twitchaccountmanager.cpp \ src/singletons/helper/completionmodel.cpp \ - src/singletons/resourcemanager.cpp + src/singletons/resourcemanager.cpp \ + src/singletons/helper/ircmessagehandler.cpp HEADERS += \ src/precompiled_headers.hpp \ @@ -193,7 +194,8 @@ HEADERS += \ src/singletons/helper/completionmodel.hpp \ src/singletons/helper/chatterinosetting.hpp \ src/singletons/resourcemanager.hpp \ - src/util/emotemap.hpp + src/util/emotemap.hpp \ + src/singletons/helper/ircmessagehandler.hpp PRECOMPILED_HEADER = diff --git a/src/singletons/helper/ircmessagehandler.cpp b/src/singletons/helper/ircmessagehandler.cpp new file mode 100644 index 000000000..69a6d98c9 --- /dev/null +++ b/src/singletons/helper/ircmessagehandler.cpp @@ -0,0 +1,186 @@ +#include "ircmessagehandler.hpp" + +#include + +#include "debug/log.hpp" +#include "messages/limitedqueue.hpp" +#include "messages/message.hpp" +#include "singletons/channelmanager.hpp" +#include "singletons/resourcemanager.hpp" +#include "singletons/windowmanager.hpp" +#include "twitch/twitchchannel.hpp" + +using namespace chatterino::messages; + +namespace chatterino { +namespace singletons { +namespace helper { + +IrcMessageHandler::IrcMessageHandler(ChannelManager &_channelManager, + ResourceManager &_resourceManager) + : channelManager(_channelManager) + , resourceManager(_resourceManager) +{ +} + +IrcMessageHandler &IrcMessageHandler::getInstance() +{ + static IrcMessageHandler instance(ChannelManager::getInstance(), + ResourceManager::getInstance()); + return instance; +} + +void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message) +{ + const auto &tags = message->tags(); + + auto iterator = tags.find("room-id"); + + if (iterator != tags.end()) { + auto roomID = iterator.value().toString(); + + auto channel = + this->channelManager.getTwitchChannel(QString(message->toData()).split("#").at(1)); + auto twitchChannel = dynamic_cast(channel.get()); + if (twitchChannel != nullptr) { + twitchChannel->setRoomID(roomID); + } + + this->resourceManager.loadChannelData(roomID); + } +} + +void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message) +{ + assert(message->parameters().length() >= 1); + + auto rawChannelName = message->parameter(0); + + assert(rawChannelName.length() >= 2); + + auto trimmedChannelName = rawChannelName.mid(1); + + auto c = this->channelManager.getTwitchChannel(trimmedChannelName); + + if (!c) { + debug::Log( + "[IrcMessageHandler:handleClearChatMessage] Channel {} not found in channel manager", + trimmedChannelName); + return; + } + + // check if the chat has been cleared by a moderator + if (message->parameters().length() == 1) { + std::shared_ptr msg( + Message::createSystemMessage("Chat has been cleared by a moderator.")); + + c->addMessage(msg); + + return; + } + + assert(message->parameters().length() >= 2); + + // get username, duration and message of the timed out user + QString username = message->parameter(1); + QString durationInSeconds, reason; + QVariant v = message->tag("ban-duration"); + if (v.isValid()) { + durationInSeconds = v.toString(); + } + + v = message->tag("ban-reason"); + if (v.isValid()) { + reason = v.toString(); + } + + // add the notice that the user has been timed out + SharedMessage msg(Message::createTimeoutMessage(username, durationInSeconds, reason)); + + c->addMessage(msg); + + // disable the messages from the user + LimitedQueueSnapshot snapshot = c->getMessageSnapshot(); + for (int i = 0; i < snapshot.getLength(); i++) { + if (snapshot[i]->getTimeoutUser() == username) { + snapshot[i]->setDisabled(true); + } + } + + // refresh all + WindowManager::getInstance().layoutVisibleChatWidgets(c.get()); +} + +void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message) +{ + // TODO: Implement +} + +void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message) +{ + // TODO: Implement +} + +void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message) +{ + // do nothing +} + +void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message) +{ + auto channel = channelManager.getTwitchChannel(message->parameter(0).remove(0, 1)); + if (message->parameter(1) == "+o") { + channel->modList.append(message->parameter(2)); + } else if (message->parameter(1) == "-o") { + channel->modList.append(message->parameter(2)); + } +} + +void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message) +{ + auto rawChannelName = message->target(); + + bool broadcast = rawChannelName.length() < 2; + std::shared_ptr msg(Message::createSystemMessage(message->content())); + + if (broadcast) { + this->channelManager.doOnAll([msg](const auto &c) { + c->addMessage(msg); // + }); + + return; + } + + 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; + } + + c->addMessage(msg); +} + +void IrcMessageHandler::handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message) +{ + 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; + } + + this->handleNoticeMessage(message); +} +} +} +} diff --git a/src/singletons/helper/ircmessagehandler.hpp b/src/singletons/helper/ircmessagehandler.hpp new file mode 100644 index 000000000..81fec9b1e --- /dev/null +++ b/src/singletons/helper/ircmessagehandler.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +namespace chatterino { +namespace singletons { +class ChannelManager; +class ResourceManager; + +namespace helper { +class IrcMessageHandler +{ + IrcMessageHandler(ChannelManager &channelManager, ResourceManager &resourceManager); + + ChannelManager &channelManager; + ResourceManager &resourceManager; + +public: + static IrcMessageHandler &getInstance(); + + void handleRoomStateMessage(Communi::IrcMessage *message); + void handleClearChatMessage(Communi::IrcMessage *message); + void handleUserStateMessage(Communi::IrcMessage *message); + void handleWhisperMessage(Communi::IrcMessage *message); + void handleUserNoticeMessage(Communi::IrcMessage *message); + void handleModeMessage(Communi::IrcMessage *message); + void handleNoticeMessage(Communi::IrcNoticeMessage *message); + void handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message); +}; +} +} +} diff --git a/src/singletons/ircmanager.cpp b/src/singletons/ircmanager.cpp index bc5aedf48..db09e55ae 100644 --- a/src/singletons/ircmanager.cpp +++ b/src/singletons/ircmanager.cpp @@ -6,6 +6,7 @@ #include "singletons/accountmanager.hpp" #include "singletons/channelmanager.hpp" #include "singletons/emotemanager.hpp" +#include "singletons/helper/ircmessagehandler.hpp" #include "singletons/resourcemanager.hpp" #include "singletons/settingsmanager.hpp" #include "singletons/windowmanager.hpp" @@ -262,19 +263,20 @@ void IrcManager::messageReceived(Communi::IrcMessage *message) const QString &command = message->command(); if (command == "ROOMSTATE") { - this->handleRoomStateMessage(message); + helper::IrcMessageHandler::getInstance().handleRoomStateMessage(message); } else if (command == "CLEARCHAT") { - this->handleClearChatMessage(message); + helper::IrcMessageHandler::getInstance().handleClearChatMessage(message); } else if (command == "USERSTATE") { - this->handleUserStateMessage(message); + helper::IrcMessageHandler::getInstance().handleUserStateMessage(message); } else if (command == "WHISPER") { - this->handleWhisperMessage(message); + helper::IrcMessageHandler::getInstance().handleWhisperMessage(message); } else if (command == "USERNOTICE") { - this->handleUserNoticeMessage(message); + helper::IrcMessageHandler::getInstance().handleUserNoticeMessage(message); } else if (command == "MODE") { - this->handleModeMessage(message); + helper::IrcMessageHandler::getInstance().handleModeMessage(message); } else if (command == "NOTICE") { - this->handleNoticeMessage(static_cast(message)); + helper::IrcMessageHandler::getInstance().handleNoticeMessage( + static_cast(message)); } } @@ -282,116 +284,12 @@ void IrcManager::writeConnectionMessageReceived(Communi::IrcMessage *message) { switch (message->type()) { case Communi::IrcMessage::Type::Notice: { - this->handleWriteConnectionNoticeMessage( + helper::IrcMessageHandler::getInstance().handleWriteConnectionNoticeMessage( static_cast(message)); } break; } } -void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message) -{ - const auto &tags = message->tags(); - - auto iterator = tags.find("room-id"); - - if (iterator != tags.end()) { - auto roomID = iterator.value().toString(); - - auto channel = channelManager.getTwitchChannel(QString(message->toData()).split("#").at(1)); - auto twitchChannel = dynamic_cast(channel.get()); - if (twitchChannel != nullptr) { - twitchChannel->setRoomID(roomID); - } - - this->resources.loadChannelData(roomID); - } -} - -void IrcManager::handleClearChatMessage(Communi::IrcMessage *message) -{ - assert(message->parameters().length() >= 1); - - auto rawChannelName = message->parameter(0); - - assert(rawChannelName.length() >= 2); - - auto trimmedChannelName = rawChannelName.mid(1); - - auto c = this->channelManager.getTwitchChannel(trimmedChannelName); - - if (!c) { - debug::Log("[IrcManager:handleClearChatMessage] Channel {} not found in channel manager", - trimmedChannelName); - return; - } - - // check if the chat has been cleared by a moderator - if (message->parameters().length() == 1) { - std::shared_ptr msg( - Message::createSystemMessage("Chat has been cleared by a moderator.")); - - c->addMessage(msg); - - return; - } - - assert(message->parameters().length() >= 2); - - // get username, duration and message of the timed out user - QString username = message->parameter(1); - QString durationInSeconds, reason; - QVariant v = message->tag("ban-duration"); - if (v.isValid()) { - durationInSeconds = v.toString(); - } - - v = message->tag("ban-reason"); - if (v.isValid()) { - reason = v.toString(); - } - - // add the notice that the user has been timed out - SharedMessage msg(Message::createTimeoutMessage(username, durationInSeconds, reason)); - - c->addMessage(msg); - - // disable the messages from the user - LimitedQueueSnapshot snapshot = c->getMessageSnapshot(); - for (int i = 0; i < snapshot.getLength(); i++) { - if (snapshot[i]->getTimeoutUser() == username) { - snapshot[i]->setDisabled(true); - } - } - - // refresh all - WindowManager::getInstance().layoutVisibleChatWidgets(c.get()); -} - -void IrcManager::handleUserStateMessage(Communi::IrcMessage *message) -{ - // TODO: Implement -} - -void IrcManager::handleWhisperMessage(Communi::IrcMessage *message) -{ - // TODO: Implement -} - -void IrcManager::handleUserNoticeMessage(Communi::IrcMessage *message) -{ - // do nothing -} - -void IrcManager::handleModeMessage(Communi::IrcMessage *message) -{ - auto channel = channelManager.getTwitchChannel(message->parameter(0).remove(0, 1)); - if (message->parameter(1) == "+o") { - channel->modList.append(message->parameter(2)); - } else if (message->parameter(1) == "-o") { - channel->modList.append(message->parameter(2)); - } -} - // XXX: This does not fit in IrcManager bool IrcManager::isTwitchBlockedUser(QString const &username) { @@ -476,52 +374,6 @@ void IrcManager::removeIgnoredUser(QString const &username) } } -void IrcManager::handleNoticeMessage(Communi::IrcNoticeMessage *message) -{ - auto rawChannelName = message->target(); - - bool broadcast = rawChannelName.length() < 2; - std::shared_ptr msg(Message::createSystemMessage(message->content())); - - if (broadcast) { - this->channelManager.doOnAll([msg](const auto &c) { - c->addMessage(msg); // - }); - - return; - } - - 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; - } - - c->addMessage(msg); -} - -void IrcManager::handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message) -{ - 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; - } - - this->handleNoticeMessage(message); -} - void IrcManager::onConnected() { std::shared_ptr msg(Message::createSystemMessage("connected to chat")); diff --git a/src/singletons/ircmanager.hpp b/src/singletons/ircmanager.hpp index 49248245d..5b59ee098 100644 --- a/src/singletons/ircmanager.hpp +++ b/src/singletons/ircmanager.hpp @@ -85,15 +85,6 @@ private: void writeConnectionMessageReceived(Communi::IrcMessage *message); - void handleRoomStateMessage(Communi::IrcMessage *message); - void handleClearChatMessage(Communi::IrcMessage *message); - void handleUserStateMessage(Communi::IrcMessage *message); - void handleWhisperMessage(Communi::IrcMessage *message); - void handleUserNoticeMessage(Communi::IrcMessage *message); - void handleModeMessage(Communi::IrcMessage *message); - void handleNoticeMessage(Communi::IrcNoticeMessage *message); - void handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message); - void onConnected(); void onDisconnected();