diff --git a/src/channel.cpp b/src/channel.cpp index 42a41f12a..311ecd03c 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -118,7 +118,11 @@ void Channel::reloadChannelEmotes() void Channel::sendMessage(const QString &message) { qDebug() << "Channel send message: " << message; - this->ircManager.sendMessage(name, message); + + // Do last message processing + QString parsedMessage = this->emoteManager.replaceShortCodes(message); + + this->ircManager.sendMessage(name, parsedMessage); } } // namespace chatterino diff --git a/src/emotemanager.cpp b/src/emotemanager.cpp index eded4ed5a..871909554 100644 --- a/src/emotemanager.cpp +++ b/src/emotemanager.cpp @@ -24,6 +24,7 @@ namespace chatterino { EmoteManager::EmoteManager(WindowManager &_windowManager, Resources &_resources) : windowManager(_windowManager) , resources(_resources) + , findShortCodesRegex(":([-+\\w]+):") { // Note: Do not use this->resources in ctor pajlada::Settings::Setting roomID( @@ -294,6 +295,36 @@ void EmoteManager::parseEmojis(std::vector> &pars } } +QString EmoteManager::replaceShortCodes(const QString &text) +{ + QString ret(text); + auto it = this->findShortCodesRegex.globalMatch(text); + + int32_t offset = 0; + + while (it.hasNext()) { + auto match = it.next(); + + auto capturedString = match.captured(); + + QString matchString = capturedString.toLower().mid(1, capturedString.size() - 2); + + auto emojiIt = this->emojiShortCodeToEmoji.constFind(matchString); + + if (emojiIt == this->emojiShortCodeToEmoji.constEnd()) { + continue; + } + + auto emojiData = emojiIt.value(); + + ret.replace(offset + match.capturedStart(), match.capturedLength(), emojiData.value); + + offset += emojiData.value.size() - match.capturedLength(); + } + + return ret; +} + void EmoteManager::refreshTwitchEmotes(const std::string &roomID) { std::string oauthClient = diff --git a/src/emotemanager.hpp b/src/emotemanager.hpp index 7324ce67e..768fec222 100644 --- a/src/emotemanager.hpp +++ b/src/emotemanager.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -75,6 +76,8 @@ private: Resources &resources; /// Emojis + QRegularExpression findShortCodesRegex; + // shortCodeToEmoji maps strings like "sunglasses" to its emoji QMap emojiShortCodeToEmoji; @@ -89,6 +92,8 @@ private: public: void parseEmojis(std::vector> &parsedWords, const QString &text); + QString replaceShortCodes(const QString &text); + /// Twitch emotes void refreshTwitchEmotes(const std::string &roomID);