This commit is contained in:
fourtf 2018-01-05 02:05:59 +01:00
parent 43568556be
commit 947589358b
3 changed files with 57 additions and 5 deletions

View file

@ -4,6 +4,9 @@
#include <QFile> #include <QFile>
#include <QRegularExpression> #include <QRegularExpression>
#include "channel.hpp"
#include "twitch/twitchchannel.hpp"
namespace chatterino { namespace chatterino {
namespace singletons { namespace singletons {
CommandManager &CommandManager::getInstance() CommandManager &CommandManager::getInstance()
@ -58,10 +61,11 @@ QStringList CommandManager::getCommands()
return this->commandsStringList; return this->commandsStringList;
} }
QString CommandManager::execCommand(QString text, bool dryRun) QString CommandManager::execCommand(const QString &text, std::shared_ptr<Channel> channel,
bool dryRun)
{ {
Command command;
QStringList words = text.split(' ', QString::SkipEmptyParts); QStringList words = text.split(' ', QString::SkipEmptyParts);
Command command;
{ {
std::lock_guard<std::mutex> lock(this->mutex); std::lock_guard<std::mutex> lock(this->mutex);
@ -72,6 +76,44 @@ QString CommandManager::execCommand(QString text, bool dryRun)
QString commandName = words[0]; QString commandName = words[0];
// check if default command exists
auto *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(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); auto it = this->commands.find(commandName);
if (it == this->commands.end()) { if (it == this->commands.end()) {
@ -81,6 +123,11 @@ QString CommandManager::execCommand(QString text, bool dryRun)
command = it.value(); command = it.value();
} }
return this->execCustomCommand(words, command);
}
QString CommandManager::execCustomCommand(const QStringList &words, const Command &command)
{
QString result; QString result;
static QRegularExpression parseCommand("(^|[^{])({{)*{(\\d+\\+?)}"); static QRegularExpression parseCommand("(^|[^{])({{)*{(\\d+\\+?)}");

View file

@ -5,6 +5,8 @@
#include <mutex> #include <mutex>
namespace chatterino { namespace chatterino {
class Channel;
namespace singletons { namespace singletons {
// //
@ -18,7 +20,7 @@ class CommandManager
public: public:
static CommandManager &getInstance(); static CommandManager &getInstance();
QString execCommand(QString text, bool dryRun); QString execCommand(const QString &text, std::shared_ptr<Channel> channel, bool dryRun);
void loadCommands(); void loadCommands();
void saveCommands(); void saveCommands();
@ -38,6 +40,8 @@ private:
QMap<QString, Command> commands; QMap<QString, Command> commands;
std::mutex mutex; std::mutex mutex;
QStringList commandsStringList; QStringList commandsStringList;
QString execCustomCommand(const QStringList &words, const Command &command);
}; };
} }
} }

View file

@ -82,7 +82,7 @@ SplitInput::SplitInput(Split *_chatWidget)
QString message = textInput.toPlainText(); QString message = textInput.toPlainText();
QString sendMessage = QString sendMessage =
singletons::CommandManager::getInstance().execCommand(message, false); singletons::CommandManager::getInstance().execCommand(message, c, false);
sendMessage = sendMessage.replace('\n', ' '); sendMessage = sendMessage.replace('\n', ' ');
c->sendMessage(sendMessage); c->sendMessage(sendMessage);
@ -232,7 +232,8 @@ void SplitInput::editTextChanged()
static QRegularExpression spaceRegex("\\s\\s+"); static QRegularExpression spaceRegex("\\s\\s+");
text = text.replace(spaceRegex, " "); text = text.replace(spaceRegex, " ");
text = singletons::CommandManager::getInstance().execCommand(text, true); text = singletons::CommandManager::getInstance().execCommand(
text, this->chatWidget->getChannel(), true);
QString labelText; QString labelText;