diff --git a/src/singletons/commandmanager.cpp b/src/singletons/commandmanager.cpp index 998449967..2e27dd202 100644 --- a/src/singletons/commandmanager.cpp +++ b/src/singletons/commandmanager.cpp @@ -4,6 +4,9 @@ #include #include +#include "channel.hpp" +#include "twitch/twitchchannel.hpp" + namespace chatterino { namespace singletons { CommandManager &CommandManager::getInstance() @@ -58,10 +61,11 @@ QStringList CommandManager::getCommands() return this->commandsStringList; } -QString CommandManager::execCommand(QString text, bool dryRun) +QString CommandManager::execCommand(const QString &text, std::shared_ptr channel, + bool dryRun) { - Command command; QStringList words = text.split(' ', QString::SkipEmptyParts); + Command command; { std::lock_guard lock(this->mutex); @@ -72,6 +76,44 @@ QString CommandManager::execCommand(QString text, bool dryRun) QString commandName = words[0]; + // check if default command exists + auto *twitchChannel = dynamic_cast(channel.get()); + + if (!dryRun && twitchChannel != nullptr) { + if (commandName == "/uptime") { + QString messageText = + twitchChannel->isLive ? twitchChannel->streamUptime : "Channel is not live."; + messages::SharedMessage message( + messages::Message::createSystemMessage(messageText)); + channel->addMessage(message); + + return ""; + } else if (commandName == "/ignore" && words.size() >= 2) { + QString messageText; + + if (IrcManager::getInstance().tryAddIgnoredUser(words.at(1), messageText)) { + messageText = "Ignored user \"" + words.at(1) + "\"."; + } + + messages::SharedMessage message( + messages::Message::createSystemMessage(messageText)); + channel->addMessage(message); + return ""; + } else if (commandName == "/unignore") { + QString messageText; + + if (IrcManager::getInstance().tryRemoveIgnoredUser(words.at(1), messageText)) { + messageText = "Ignored user \"" + words.at(1) + "\"."; + } + + messages::SharedMessage message( + messages::Message::createSystemMessage(messageText)); + channel->addMessage(message); + return ""; + } + } + + // check if custom command exists auto it = this->commands.find(commandName); if (it == this->commands.end()) { @@ -81,6 +123,11 @@ QString CommandManager::execCommand(QString text, bool dryRun) command = it.value(); } + return this->execCustomCommand(words, command); +} + +QString CommandManager::execCustomCommand(const QStringList &words, const Command &command) +{ QString result; static QRegularExpression parseCommand("(^|[^{])({{)*{(\\d+\\+?)}"); diff --git a/src/singletons/commandmanager.hpp b/src/singletons/commandmanager.hpp index ddddb57e3..dbeaa00f1 100644 --- a/src/singletons/commandmanager.hpp +++ b/src/singletons/commandmanager.hpp @@ -5,6 +5,8 @@ #include namespace chatterino { +class Channel; + namespace singletons { // @@ -18,7 +20,7 @@ class CommandManager public: static CommandManager &getInstance(); - QString execCommand(QString text, bool dryRun); + QString execCommand(const QString &text, std::shared_ptr channel, bool dryRun); void loadCommands(); void saveCommands(); @@ -38,6 +40,8 @@ private: QMap commands; std::mutex mutex; QStringList commandsStringList; + + QString execCustomCommand(const QStringList &words, const Command &command); }; } } diff --git a/src/widgets/helper/splitinput.cpp b/src/widgets/helper/splitinput.cpp index d9bdc1893..44b0da254 100644 --- a/src/widgets/helper/splitinput.cpp +++ b/src/widgets/helper/splitinput.cpp @@ -82,7 +82,7 @@ SplitInput::SplitInput(Split *_chatWidget) QString message = textInput.toPlainText(); QString sendMessage = - singletons::CommandManager::getInstance().execCommand(message, false); + singletons::CommandManager::getInstance().execCommand(message, c, false); sendMessage = sendMessage.replace('\n', ' '); c->sendMessage(sendMessage); @@ -232,7 +232,8 @@ void SplitInput::editTextChanged() static QRegularExpression spaceRegex("\\s\\s+"); text = text.replace(spaceRegex, " "); - text = singletons::CommandManager::getInstance().execCommand(text, true); + text = singletons::CommandManager::getInstance().execCommand( + text, this->chatWidget->getChannel(), true); QString labelText;