mirror-chatterino2/src/providers/twitch/IrcMessageHandler.cpp

629 lines
17 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "IrcMessageHandler.hpp"
#include "Application.hpp"
2019-09-17 12:10:38 +02:00
#include "controllers/accounts/AccountController.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/highlights/HighlightController.hpp"
#include "debug/Log.hpp"
#include "messages/LimitedQueue.hpp"
#include "messages/Message.hpp"
2019-09-17 12:10:38 +02:00
#include "providers/twitch/TwitchAccountManager.hpp"
2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchHelpers.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
2019-09-17 12:10:38 +02:00
#include "providers/twitch/TwitchMessageBuilder.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
#include "util/IrcHelpers.hpp"
2018-06-04 12:23:23 +02:00
#include <IrcMessage>
2018-01-01 23:54:54 +01:00
#include <unordered_set>
2018-01-01 23:54:54 +01:00
namespace chatterino {
static QMap<QString, QString> parseBadges(QString badgesString)
{
QMap<QString, QString> badges;
for (auto badgeData : badgesString.split(','))
{
auto parts = badgeData.split('/');
if (parts.length() != 2)
{
continue;
}
badges.insert(parts[0], parts[1]);
}
return badges;
}
2018-01-01 23:54:54 +01:00
IrcMessageHandler &IrcMessageHandler::getInstance()
{
static IrcMessageHandler instance;
2018-01-01 23:54:54 +01:00
return instance;
}
std::vector<MessagePtr> IrcMessageHandler::parseMessage(
Channel *channel, Communi::IrcMessage *message)
{
std::vector<MessagePtr> builtMessages;
auto command = message->command();
if (command == "PRIVMSG")
{
return this->parsePrivMessage(
channel, static_cast<Communi::IrcPrivateMessage *>(message));
}
else if (command == "USERNOTICE")
{
return this->parseUserNoticeMessage(channel, message);
}
else if (command == "NOTICE")
{
return this->parseNoticeMessage(
static_cast<Communi::IrcNoticeMessage *>(message));
}
return builtMessages;
}
std::vector<MessagePtr> IrcMessageHandler::parsePrivMessage(
Channel *channel, Communi::IrcPrivateMessage *message)
{
std::vector<MessagePtr> builtMessages;
MessageParseArgs args;
TwitchMessageBuilder builder(channel, message, args, message->content(),
message->isAction());
if (!builder.isIgnored())
{
builtMessages.emplace_back(builder.build());
builder.triggerHighlights();
}
return builtMessages;
}
2018-08-06 21:17:03 +02:00
void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message,
TwitchIrcServer &server)
2018-06-04 12:23:23 +02:00
{
2018-08-06 21:17:03 +02:00
this->addMessage(message, message->target(), message->content(), server,
false, message->isAction());
2018-06-04 12:23:23 +02:00
}
2018-08-06 21:17:03 +02:00
void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
const QString &target,
2019-09-17 12:10:38 +02:00
const QString &content,
TwitchIrcServer &server, bool isSub,
bool isAction)
2018-06-04 12:23:23 +02:00
{
QString channelName;
2018-10-21 13:43:02 +02:00
if (!trimChannelName(target, channelName))
{
2018-06-04 12:23:23 +02:00
return;
}
auto chan = server.getChannelOrEmpty(channelName);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-06-04 12:23:23 +02:00
return;
}
MessageParseArgs args;
2018-10-21 13:43:02 +02:00
if (isSub)
{
2018-06-04 12:23:23 +02:00
args.trimSubscriberUsername = true;
}
2018-10-21 13:43:02 +02:00
if (chan->isBroadcaster())
{
args.isStaffOrBroadcaster = true;
}
2018-06-07 15:49:24 +02:00
TwitchMessageBuilder builder(chan.get(), _message, args, content, isAction);
2018-06-04 12:23:23 +02:00
2018-10-21 13:43:02 +02:00
if (isSub || !builder.isIgnored())
{
if (isSub)
{
2018-08-07 07:55:31 +02:00
builder->flags.set(MessageFlag::Subscription);
builder->flags.unset(MessageFlag::Highlighted);
2018-08-07 01:35:24 +02:00
}
auto msg = builder.build();
builder.triggerHighlights();
2018-08-30 20:18:38 +02:00
auto highlighted = msg->flags.has(MessageFlag::Highlighted);
2018-08-07 01:35:24 +02:00
2018-10-21 13:43:02 +02:00
if (!isSub)
{
if (highlighted)
{
2018-06-04 12:23:23 +02:00
server.mentionsChannel->addMessage(msg);
getApp()->highlights->addHighlight(msg);
}
}
chan->addMessage(msg);
2019-09-17 12:10:38 +02:00
if (auto chatters = dynamic_cast<ChannelChatters *>(chan.get()))
{
chatters->addRecentChatter(msg->displayName);
}
2018-06-04 12:23:23 +02:00
}
}
2018-01-01 23:54:54 +01:00
void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{
const auto &tags = message->tags();
2018-05-24 08:58:34 +02:00
auto app = getApp();
2018-01-01 23:54:54 +01:00
2018-05-24 08:58:34 +02:00
// get twitch channel
QString chanName;
2018-10-21 13:43:02 +02:00
if (!trimChannelName(message->parameter(0), chanName))
{
2018-05-24 08:58:34 +02:00
return;
}
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
2018-01-01 23:54:54 +01:00
2018-10-21 13:43:02 +02:00
if (auto *twitchChannel = dynamic_cast<TwitchChannel *>(chan.get()))
{
2018-05-24 08:58:34 +02:00
// room-id
decltype(tags.find("xD")) it;
2018-02-05 15:11:50 +01:00
2018-10-21 13:43:02 +02:00
if ((it = tags.find("room-id")) != tags.end())
{
2018-07-14 14:24:18 +02:00
auto roomId = it.value().toString();
2018-07-15 20:28:54 +02:00
twitchChannel->setRoomId(roomId);
2018-05-24 08:58:34 +02:00
}
2018-02-05 15:11:50 +01:00
2018-05-24 08:58:34 +02:00
// Room modes
2018-07-15 20:28:54 +02:00
{
2018-08-06 18:25:47 +02:00
auto roomModes = *twitchChannel->accessRoomModes();
2018-10-21 13:43:02 +02:00
if ((it = tags.find("emote-only")) != tags.end())
{
2018-08-06 18:25:47 +02:00
roomModes.emoteOnly = it.value() == "1";
2018-07-15 20:28:54 +02:00
}
2018-10-21 13:43:02 +02:00
if ((it = tags.find("subs-only")) != tags.end())
{
2018-08-06 18:25:47 +02:00
roomModes.submode = it.value() == "1";
2018-07-15 20:28:54 +02:00
}
2018-10-21 13:43:02 +02:00
if ((it = tags.find("slow")) != tags.end())
{
2018-08-06 18:25:47 +02:00
roomModes.slowMode = it.value().toInt();
2018-07-15 20:28:54 +02:00
}
2018-10-21 13:43:02 +02:00
if ((it = tags.find("r9k")) != tags.end())
{
2018-08-06 18:25:47 +02:00
roomModes.r9k = it.value() == "1";
2018-07-15 20:28:54 +02:00
}
2018-10-21 13:43:02 +02:00
if ((it = tags.find("broadcaster-lang")) != tags.end())
{
2018-08-06 18:25:47 +02:00
roomModes.broadcasterLang = it.value().toString();
2018-07-15 20:28:54 +02:00
}
if ((it = tags.find("followers-only")) != tags.end())
{
roomModes.followerOnly = it.value().toInt();
}
2018-08-06 18:25:47 +02:00
twitchChannel->setRoomModes(roomModes);
2018-01-01 23:54:54 +01:00
}
2018-07-15 20:28:54 +02:00
twitchChannel->roomModesChanged.invoke();
2018-01-01 23:54:54 +01:00
}
}
void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
{
2018-05-17 13:43:01 +02:00
// check parameter count
2018-10-21 13:43:02 +02:00
if (message->parameters().length() < 1)
{
2018-05-17 13:43:01 +02:00
return;
}
QString chanName;
2018-10-21 13:43:02 +02:00
if (!trimChannelName(message->parameter(0), chanName))
{
2018-05-17 13:43:01 +02:00
return;
}
auto app = getApp();
// get channel
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-08-11 14:20:53 +02:00
log("[IrcMessageHandler:handleClearChatMessage] Twitch channel {} not "
2018-08-06 21:17:03 +02:00
"found",
chanName);
2018-05-17 13:43:01 +02:00
return;
}
// check if the chat has been cleared by a moderator
2018-10-21 13:43:02 +02:00
if (message->parameters().length() == 1)
{
chan->disableAllMessages();
2018-08-07 01:35:24 +02:00
chan->addMessage(
makeSystemMessage("Chat has been cleared by a moderator."));
2018-05-17 13:43:01 +02:00
return;
}
// get username, duration and message of the timed out user
QString username = message->parameter(1);
QString durationInSeconds, reason;
QVariant v = message->tag("ban-duration");
2018-10-21 13:43:02 +02:00
if (v.isValid())
{
2018-05-17 13:43:01 +02:00
durationInSeconds = v.toString();
}
v = message->tag("ban-reason");
2018-10-21 13:43:02 +02:00
if (v.isValid())
{
2018-05-17 13:43:01 +02:00
reason = v.toString();
}
2018-08-07 01:35:24 +02:00
auto timeoutMsg = MessageBuilder(timeoutMessage, username,
durationInSeconds, reason, false)
.release();
2018-05-17 13:43:01 +02:00
chan->addOrReplaceTimeout(timeoutMsg);
// refresh all
app->windows->repaintVisibleChatWidgets(chan.get());
if (getSettings()->hideModerated)
{
app->windows->forceLayoutChannelViews();
}
2018-01-01 23:54:54 +01:00
}
void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
{
// check parameter count
if (message->parameters().length() < 1)
{
return;
}
QString chanName;
if (!trimChannelName(message->parameter(0), chanName))
{
return;
}
auto app = getApp();
// get channel
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
if (chan->isEmpty())
{
log("[IrcMessageHandler:handleClearMessageMessage] Twitch channel {} "
"not "
"found",
chanName);
return;
}
auto tags = message->tags();
QString targetID = tags.value("target-msg-id").toString();
chan->deleteMessage(targetID);
}
2018-01-01 23:54:54 +01:00
void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
{
auto app = getApp();
2018-01-17 18:36:12 +01:00
QString channelName;
if (!trimChannelName(message->parameter(0), channelName))
2018-10-21 13:43:02 +02:00
{
return;
}
auto c = app->twitch.server->getChannelOrEmpty(channelName);
if (c->isEmpty())
{
return;
}
2018-01-17 18:36:12 +01:00
QVariant _badges = message->tag("badges");
if (_badges.isValid())
{
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(c.get());
if (tc != nullptr)
2018-10-21 13:43:02 +02:00
{
auto parsedBadges = parseBadges(_badges.toString());
tc->setVIP(parsedBadges.contains("vip"));
tc->setStaff(parsedBadges.contains("staff"));
}
}
QVariant _mod = message->tag("mod");
if (_mod.isValid())
{
2018-06-26 17:06:17 +02:00
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(c.get());
2018-10-21 13:43:02 +02:00
if (tc != nullptr)
{
2018-01-17 18:36:12 +01:00
tc->setMod(_mod == "1");
}
}
2018-01-01 23:54:54 +01:00
}
void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
{
auto app = getApp();
2018-08-11 14:20:53 +02:00
log("Received whisper!");
MessageParseArgs args;
args.isReceivedWhisper = true;
auto c = app->twitch.server->whispersChannel.get();
2018-08-06 21:17:03 +02:00
TwitchMessageBuilder builder(c, message, args, message->parameter(1),
false);
2018-10-21 13:43:02 +02:00
if (!builder.isIgnored())
{
2019-03-20 20:46:20 +01:00
builder->flags.set(MessageFlag::Whisper);
MessagePtr _message = builder.build();
builder.triggerHighlights();
2018-09-01 14:32:39 +02:00
app->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
2018-10-21 13:43:02 +02:00
if (_message->flags.has(MessageFlag::Highlighted))
{
app->twitch.server->mentionsChannel->addMessage(_message);
}
c->addMessage(_message);
auto overrideFlags = boost::optional<MessageFlags>(_message->flags);
overrideFlags->set(MessageFlag::DoNotTriggerNotification);
overrideFlags->set(MessageFlag::DoNotLog);
2018-10-21 13:43:02 +02:00
if (getSettings()->inlineWhispers)
{
app->twitch.server->forEachChannel(
2019-02-27 20:26:19 +01:00
[&_message, overrideFlags](ChannelPtr channel) {
channel->addMessage(_message, overrideFlags);
});
}
}
2018-01-01 23:54:54 +01:00
}
std::vector<MessagePtr> IrcMessageHandler::parseUserNoticeMessage(
Channel *channel, Communi::IrcMessage *message)
{
std::vector<MessagePtr> builtMessages;
auto data = message->toData();
auto tags = message->tags();
auto parameters = message->parameters();
auto target = parameters[0];
QString msgType = tags.value("msg-id", "").toString();
QString content;
if (parameters.size() >= 2)
{
content = parameters[1];
}
if (msgType == "sub" || msgType == "resub" || msgType == "subgift")
{
// Sub-specific message. I think it's only allowed for "resub" messages
// atm
if (!content.isEmpty())
{
MessageParseArgs args;
args.trimSubscriberUsername = true;
TwitchMessageBuilder builder(channel, message, args, content,
false);
builder->flags.set(MessageFlag::Subscription);
builder->flags.unset(MessageFlag::Highlighted);
builtMessages.emplace_back(builder.build());
}
}
auto it = tags.find("system-msg");
if (it != tags.end())
{
auto b = MessageBuilder(systemMessage,
parseTagString(it.value().toString()));
b->flags.set(MessageFlag::Subscription);
auto newMessage = b.release();
builtMessages.emplace_back(newMessage);
}
return builtMessages;
}
2018-08-06 21:17:03 +02:00
void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
TwitchIrcServer &server)
2018-01-01 23:54:54 +01:00
{
2018-06-04 12:23:23 +02:00
auto data = message->toData();
auto tags = message->tags();
auto parameters = message->parameters();
2018-06-04 12:23:23 +02:00
auto target = parameters[0];
QString msgType = tags.value("msg-id", "").toString();
QString content;
2018-10-21 13:43:02 +02:00
if (parameters.size() >= 2)
{
content = parameters[1];
}
2018-10-21 13:43:02 +02:00
if (msgType == "sub" || msgType == "resub" || msgType == "subgift")
{
2018-08-06 21:17:03 +02:00
// Sub-specific message. I think it's only allowed for "resub" messages
// atm
2018-10-21 13:43:02 +02:00
if (!content.isEmpty())
{
2018-06-07 15:49:24 +02:00
this->addMessage(message, target, content, server, true, false);
}
2018-06-04 12:23:23 +02:00
}
auto it = tags.find("system-msg");
2018-10-21 13:43:02 +02:00
if (it != tags.end())
{
2018-08-07 01:35:24 +02:00
auto b = MessageBuilder(systemMessage,
parseTagString(it.value().toString()));
2018-06-04 12:23:23 +02:00
2018-08-07 07:55:31 +02:00
b->flags.set(MessageFlag::Subscription);
2018-08-07 01:35:24 +02:00
auto newMessage = b.release();
2018-06-04 12:23:23 +02:00
QString channelName;
2018-10-21 13:43:02 +02:00
if (message->parameters().size() < 1)
{
2018-06-04 12:23:23 +02:00
return;
}
2018-10-21 13:43:02 +02:00
if (!trimChannelName(message->parameter(0), channelName))
{
2018-06-04 12:23:23 +02:00
return;
}
auto chan = server.getChannelOrEmpty(channelName);
2018-10-21 13:43:02 +02:00
if (!chan->isEmpty())
{
2018-06-04 12:23:23 +02:00
chan->addMessage(newMessage);
}
}
2018-01-01 23:54:54 +01:00
}
void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message)
{
auto app = getApp();
2018-08-06 21:17:03 +02:00
auto channel = app->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
2018-10-21 13:43:02 +02:00
if (channel->isEmpty())
{
return;
}
2018-02-05 15:11:50 +01:00
2018-10-21 13:43:02 +02:00
if (message->parameter(1) == "+o")
{
2018-01-01 23:54:54 +01:00
channel->modList.append(message->parameter(2));
2018-10-21 13:43:02 +02:00
}
else if (message->parameter(1) == "-o")
{
2018-01-01 23:54:54 +01:00
channel->modList.append(message->parameter(2));
}
}
std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
Communi::IrcNoticeMessage *message)
{
2019-08-21 02:00:42 +02:00
if (message->content().startsWith("Login auth", Qt::CaseInsensitive))
{
return {MessageBuilder(systemMessage,
"Login expired! Try logging in again.")
.release()};
}
else
{
std::vector<MessagePtr> builtMessages;
2019-08-21 02:00:42 +02:00
builtMessages.emplace_back(makeSystemMessage(message->content()));
2019-08-21 02:00:42 +02:00
return builtMessages;
}
} // namespace chatterino
2018-01-01 23:54:54 +01:00
void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
auto app = getApp();
auto builtMessages = this->parseNoticeMessage(message);
for (auto msg : builtMessages)
2018-10-21 13:43:02 +02:00
{
QString channelName;
2019-05-05 16:15:35 +02:00
if (!trimChannelName(message->target(), channelName) ||
channelName == "jtv")
{
// Notice wasn't targeted at a single channel, send to all twitch
// channels
app->twitch.server->forEachChannelAndSpecialChannels(
[msg](const auto &c) {
c->addMessage(msg); //
});
return;
}
auto channel = app->twitch.server->getChannelOrEmpty(channelName);
if (channel->isEmpty())
{
log("[IrcManager:handleNoticeMessage] Channel {} not found in "
"channel "
"manager ",
channelName);
return;
}
QString tags = message->tags().value("msg-id", "").toString();
if (tags == "bad_delete_message_error" || tags == "usage_delete")
{
channel->addMessage(makeSystemMessage(
"Usage: \"/delete <msg-id>\" - can't take more "
"than one argument"));
}
else
{
channel->addMessage(msg);
}
}
2018-01-01 23:54:54 +01:00
}
void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
{
auto app = getApp();
2018-08-06 21:17:03 +02:00
auto channel = app->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
2018-08-06 21:17:03 +02:00
if (TwitchChannel *twitchChannel =
2018-10-21 13:43:02 +02:00
dynamic_cast<TwitchChannel *>(channel.get()))
{
2019-09-17 12:10:38 +02:00
if (message->nick() !=
getApp()->accounts->twitch.getCurrent()->getUserName() &&
getSettings()->showJoins.getValue())
{
twitchChannel->addJoinedUser(message->nick());
}
}
}
void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message)
{
auto app = getApp();
2018-08-06 21:17:03 +02:00
auto channel = app->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
2018-08-06 21:17:03 +02:00
if (TwitchChannel *twitchChannel =
2018-10-21 13:43:02 +02:00
dynamic_cast<TwitchChannel *>(channel.get()))
{
2019-09-17 12:10:38 +02:00
if (message->nick() !=
getApp()->accounts->twitch.getCurrent()->getUserName() &&
getSettings()->showJoins.getValue())
{
twitchChannel->addPartedUser(message->nick());
}
}
}
} // namespace chatterino