2018-06-26 14:09:39 +02:00
|
|
|
#include "IrcMessageHandler.hpp"
|
|
|
|
|
|
|
|
#include "Application.hpp"
|
|
|
|
#include "controllers/highlights/HighlightController.hpp"
|
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "messages/LimitedQueue.hpp"
|
|
|
|
#include "messages/Message.hpp"
|
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
|
|
|
#include "providers/twitch/TwitchHelpers.hpp"
|
|
|
|
#include "providers/twitch/TwitchMessageBuilder.hpp"
|
|
|
|
#include "providers/twitch/TwitchServer.hpp"
|
|
|
|
#include "singletons/ResourceManager.hpp"
|
|
|
|
#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
|
|
|
|
2018-06-24 17:33:22 +02:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-01-01 23:54:54 +01:00
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
IrcMessageHandler &IrcMessageHandler::getInstance()
|
|
|
|
{
|
2018-04-14 21:59:51 +02:00
|
|
|
static IrcMessageHandler instance;
|
2018-01-01 23:54:54 +01:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-06-04 12:23:23 +02:00
|
|
|
void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message, TwitchServer &server)
|
|
|
|
{
|
2018-06-07 15:49:24 +02:00
|
|
|
this->addMessage(message, message->target(), message->content(), server, false,
|
|
|
|
message->isAction());
|
2018-06-04 12:23:23 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 15:49:24 +02:00
|
|
|
void IrcMessageHandler::addMessage(Communi::IrcMessage *_message, const QString &target,
|
|
|
|
const QString &content, TwitchServer &server, bool isSub,
|
|
|
|
bool isAction)
|
2018-06-04 12:23:23 +02:00
|
|
|
{
|
|
|
|
QString channelName;
|
|
|
|
if (!trimChannelName(target, channelName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto chan = server.getChannelOrEmpty(channelName);
|
|
|
|
|
|
|
|
if (chan->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
MessageParseArgs args;
|
2018-06-04 12:23:23 +02:00
|
|
|
if (isSub) {
|
|
|
|
args.trimSubscriberUsername = true;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:19:52 +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
|
|
|
|
|
|
|
if (isSub || !builder.isIgnored()) {
|
2018-06-28 19:38:57 +02:00
|
|
|
MessagePtr msg = builder.build();
|
2018-06-04 12:23:23 +02:00
|
|
|
|
|
|
|
if (isSub) {
|
2018-06-28 19:38:57 +02:00
|
|
|
msg->flags |= Message::Subscription;
|
|
|
|
msg->flags &= ~Message::Highlighted;
|
2018-06-04 12:23:23 +02:00
|
|
|
} else {
|
2018-06-28 19:38:57 +02:00
|
|
|
if (msg->flags & Message::Highlighted) {
|
2018-06-04 12:23:23 +02:00
|
|
|
server.mentionsChannel->addMessage(msg);
|
|
|
|
getApp()->highlights->addHighlight(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chan->addMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (!trimChannelName(message->parameter(0), chanName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
|
2018-06-26 17:06:17 +02:00
|
|
|
TwitchChannel *twitchChannel = dynamic_cast<TwitchChannel *>(chan.get());
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
if (twitchChannel) {
|
|
|
|
// room-id
|
|
|
|
decltype(tags.find("xD")) it;
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
if ((it = tags.find("room-id")) != tags.end()) {
|
|
|
|
auto roomID = it.value().toString();
|
2018-04-28 15:20:18 +02:00
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
twitchChannel->setRoomID(roomID);
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
app->resources->loadChannelData(roomID);
|
|
|
|
}
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
// Room modes
|
|
|
|
TwitchChannel::RoomModes roomModes = twitchChannel->getRoomModes();
|
2018-04-21 00:40:17 +02:00
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
if ((it = tags.find("emote-only")) != tags.end()) {
|
|
|
|
roomModes.emoteOnly = it.value() == "1";
|
2018-04-21 00:40:17 +02:00
|
|
|
}
|
2018-05-24 08:58:34 +02:00
|
|
|
if ((it = tags.find("subs-only")) != tags.end()) {
|
|
|
|
roomModes.submode = it.value() == "1";
|
|
|
|
}
|
|
|
|
if ((it = tags.find("slow")) != tags.end()) {
|
|
|
|
roomModes.slowMode = it.value().toInt();
|
|
|
|
}
|
|
|
|
if ((it = tags.find("r9k")) != tags.end()) {
|
|
|
|
roomModes.r9k = it.value() == "1";
|
|
|
|
}
|
|
|
|
if ((it = tags.find("broadcaster-lang")) != tags.end()) {
|
|
|
|
roomModes.broadcasterLang = it.value().toString();
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
twitchChannel->setRoomModes(roomModes);
|
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
|
|
|
|
if (message->parameters().length() < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString chanName;
|
2018-05-24 08:58:34 +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);
|
|
|
|
|
|
|
|
if (chan->isEmpty()) {
|
2018-06-26 17:20:03 +02:00
|
|
|
Log("[IrcMessageHandler:handleClearChatMessage] Twitch channel {} not found", chanName);
|
2018-05-17 13:43:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the chat has been cleared by a moderator
|
|
|
|
if (message->parameters().length() == 1) {
|
2018-06-22 23:44:02 +02:00
|
|
|
chan->disableAllMessages();
|
2018-05-17 13:43:01 +02:00
|
|
|
chan->addMessage(Message::createSystemMessage("Chat has been cleared by a moderator."));
|
|
|
|
|
|
|
|
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");
|
|
|
|
if (v.isValid()) {
|
|
|
|
durationInSeconds = v.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
v = message->tag("ban-reason");
|
|
|
|
if (v.isValid()) {
|
|
|
|
reason = v.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto timeoutMsg = Message::createTimeoutMessage(username, durationInSeconds, reason, false);
|
|
|
|
chan->addOrReplaceTimeout(timeoutMsg);
|
|
|
|
|
|
|
|
// refresh all
|
|
|
|
app->windows->repaintVisibleChatWidgets(chan.get());
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
2018-01-17 18:36:12 +01:00
|
|
|
QVariant _mod = message->tag("mod");
|
|
|
|
|
|
|
|
if (_mod.isValid()) {
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2018-03-25 11:37:57 +02:00
|
|
|
QString channelName;
|
2018-05-24 08:58:34 +02:00
|
|
|
if (!trimChannelName(message->parameter(0), channelName)) {
|
2018-03-25 11:37:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-01-17 18:36:12 +01:00
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
auto c = app->twitch.server->getChannelOrEmpty(channelName);
|
2018-04-21 00:40:17 +02:00
|
|
|
if (c->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
TwitchChannel *tc = dynamic_cast<TwitchChannel *>(c.get());
|
2018-01-17 18:36:12 +01:00
|
|
|
if (tc != nullptr) {
|
|
|
|
tc->setMod(_mod == "1");
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Received whisper!");
|
2018-06-28 19:38:57 +02:00
|
|
|
MessageParseArgs args;
|
2018-02-04 16:33:46 +01:00
|
|
|
|
|
|
|
args.isReceivedWhisper = true;
|
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
auto c = app->twitch.server->whispersChannel.get();
|
2018-02-04 16:33:46 +01:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
TwitchMessageBuilder builder(c, message, args, message->parameter(1), false);
|
2018-02-04 16:33:46 +01:00
|
|
|
|
|
|
|
if (!builder.isIgnored()) {
|
2018-06-28 19:38:57 +02:00
|
|
|
MessagePtr _message = builder.build();
|
2018-04-18 20:38:08 +02:00
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
if (_message->flags & Message::Highlighted) {
|
2018-04-28 15:20:18 +02:00
|
|
|
app->twitch.server->mentionsChannel->addMessage(_message);
|
2018-02-04 16:33:46 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
app->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
|
|
|
|
|
2018-02-04 16:33:46 +01:00
|
|
|
c->addMessage(_message);
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
_message->flags |= Message::DoNotTriggerNotification;
|
2018-06-26 20:37:04 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
if (app->settings->inlineWhispers) {
|
2018-04-28 15:20:18 +02:00
|
|
|
app->twitch.server->forEachChannel([_message](ChannelPtr channel) {
|
2018-02-04 16:33:46 +01:00
|
|
|
channel->addMessage(_message); //
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 12:23:23 +02:00
|
|
|
void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message, TwitchServer &server)
|
2018-01-01 23:54:54 +01:00
|
|
|
{
|
2018-06-04 12:23:23 +02:00
|
|
|
auto data = message->toData();
|
|
|
|
|
2018-06-05 14:14:00 +02:00
|
|
|
auto tags = message->tags();
|
|
|
|
auto parameters = message->parameters();
|
2018-06-04 12:23:23 +02:00
|
|
|
|
2018-06-05 14:14:00 +02:00
|
|
|
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()) {
|
2018-06-07 15:49:24 +02:00
|
|
|
this->addMessage(message, target, content, server, true, false);
|
2018-06-05 14:14:00 +02:00
|
|
|
}
|
2018-06-04 12:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto it = tags.find("system-msg");
|
|
|
|
|
|
|
|
if (it != tags.end()) {
|
|
|
|
auto newMessage =
|
2018-06-28 19:38:57 +02:00
|
|
|
Message::createSystemMessage(parseTagString(it.value().toString()));
|
2018-06-04 12:23:23 +02:00
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
newMessage->flags |= Message::Subscription;
|
2018-06-04 12:23:23 +02:00
|
|
|
|
|
|
|
QString channelName;
|
|
|
|
|
|
|
|
if (message->parameters().size() < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!trimChannelName(message->parameter(0), channelName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto chan = server.getChannelOrEmpty(channelName);
|
|
|
|
|
|
|
|
if (!chan->isEmpty()) {
|
|
|
|
chan->addMessage(newMessage);
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
auto channel = app->twitch.server->getChannelOrEmpty(message->parameter(0).remove(0, 1));
|
2018-04-21 00:40:17 +02:00
|
|
|
|
|
|
|
if (channel->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-05 15:11:50 +01:00
|
|
|
|
2018-01-01 23:54:54 +01:00
|
|
|
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)
|
|
|
|
{
|
2018-06-23 23:08:15 +02:00
|
|
|
auto app = getApp();
|
|
|
|
MessagePtr msg = Message::createSystemMessage(message->content());
|
|
|
|
|
|
|
|
QString channelName;
|
|
|
|
if (!trimChannelName(message->target(), channelName)) {
|
|
|
|
// 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()) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[IrcManager:handleNoticeMessage] Channel {} not found in channel manager ",
|
2018-06-26 17:20:03 +02:00
|
|
|
channelName);
|
2018-06-23 23:08:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->addMessage(msg);
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message)
|
|
|
|
{
|
2018-06-24 17:33:22 +02:00
|
|
|
static std::unordered_set<std::string> readConnectionOnlyIDs{
|
2018-06-26 23:22:42 +02:00
|
|
|
"host_on", "host_off", "host_target_went_offline", "emote_only_on", "emote_only_off",
|
|
|
|
"slow_on", "slow_off", "subs_on", "subs_off", "r9k_on", "r9k_off",
|
2018-06-24 17:33:22 +02:00
|
|
|
|
|
|
|
// Display for user who times someone out. This implies you're a moderator, at which point
|
|
|
|
// you will be connected to PubSub and receive a better message from there
|
2018-06-26 23:22:42 +02:00
|
|
|
"timeout_success", "ban_success",
|
2018-06-24 17:33:22 +02:00
|
|
|
};
|
|
|
|
|
2018-01-01 23:54:54 +01:00
|
|
|
QVariant v = message->tag("msg-id");
|
2018-06-24 17:33:22 +02:00
|
|
|
if (v.isValid()) {
|
|
|
|
std::string msgID = v.toString().toStdString();
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-06-24 17:33:22 +02:00
|
|
|
if (readConnectionOnlyIDs.find(msgID) != readConnectionOnlyIDs.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("Showing notice message from write connection with message id '{}'", msgID);
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->handleNoticeMessage(message);
|
|
|
|
}
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2018-05-26 18:06:55 +02:00
|
|
|
void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
auto channel = app->twitch.server->getChannelOrEmpty(message->parameter(0).remove(0, 1));
|
|
|
|
|
|
|
|
if (TwitchChannel *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get())) {
|
|
|
|
twitchChannel->addJoinedUser(message->nick());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
auto channel = app->twitch.server->getChannelOrEmpty(message->parameter(0).remove(0, 1));
|
|
|
|
|
|
|
|
if (TwitchChannel *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get())) {
|
|
|
|
twitchChannel->addPartedUser(message->nick());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 02:22:57 +01:00
|
|
|
} // namespace chatterino
|