2018-01-01 23:54:54 +01:00
|
|
|
#include "ircmessagehandler.hpp"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "debug/log.hpp"
|
|
|
|
#include "messages/limitedqueue.hpp"
|
|
|
|
#include "messages/message.hpp"
|
2018-02-05 15:11:50 +01:00
|
|
|
#include "providers/twitch/twitchchannel.hpp"
|
2018-03-25 11:37:57 +02:00
|
|
|
#include "providers/twitch/twitchhelpers.hpp"
|
2018-02-05 21:20:38 +01:00
|
|
|
#include "providers/twitch/twitchmessagebuilder.hpp"
|
|
|
|
#include "providers/twitch/twitchserver.hpp"
|
2018-01-01 23:54:54 +01:00
|
|
|
#include "singletons/resourcemanager.hpp"
|
|
|
|
#include "singletons/windowmanager.hpp"
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
using namespace chatterino::singletons;
|
2018-01-01 23:54:54 +01:00
|
|
|
using namespace chatterino::messages;
|
|
|
|
|
|
|
|
namespace chatterino {
|
2018-02-05 15:11:50 +01:00
|
|
|
namespace providers {
|
|
|
|
namespace twitch {
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
IrcMessageHandler::IrcMessageHandler(singletons::ResourceManager &_resourceManager)
|
|
|
|
: resourceManager(_resourceManager)
|
2018-01-01 23:54:54 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IrcMessageHandler &IrcMessageHandler::getInstance()
|
|
|
|
{
|
2018-02-05 15:11:50 +01:00
|
|
|
static IrcMessageHandler instance(singletons::ResourceManager::getInstance());
|
2018-01-01 23:54:54 +01:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
|
|
|
const auto &tags = message->tags();
|
|
|
|
auto iterator = tags.find("room-id");
|
|
|
|
|
|
|
|
if (iterator != tags.end()) {
|
|
|
|
auto roomID = iterator.value().toString();
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
QStringList words = QString(message->toData()).split("#");
|
|
|
|
|
|
|
|
// ensure the format is valid
|
|
|
|
if (words.length() < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString channelName = words.at(1);
|
|
|
|
|
|
|
|
auto channel = TwitchServer::getInstance().getChannel(channelName);
|
|
|
|
|
|
|
|
if (auto twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get())) {
|
|
|
|
// set the room id of the channel
|
2018-01-01 23:54:54 +01:00
|
|
|
twitchChannel->setRoomID(roomID);
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
ResourceManager::getInstance().loadChannelData(roomID);
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
2018-02-05 15:11:50 +01:00
|
|
|
// check parameter count
|
2018-03-25 11:37:57 +02:00
|
|
|
if (message->parameters().length() < 1) {
|
2018-02-05 15:11:50 +01:00
|
|
|
return;
|
2018-03-25 11:37:57 +02:00
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-03-25 11:37:57 +02:00
|
|
|
QString chanName;
|
|
|
|
if (!TrimChannelName(message->parameter(0), chanName)) {
|
2018-02-05 15:11:50 +01:00
|
|
|
return;
|
2018-03-25 11:37:57 +02:00
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
// get channel
|
|
|
|
auto chan = TwitchServer::getInstance().getChannel(chanName);
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
if (!chan) {
|
|
|
|
debug::Log("[IrcMessageHandler:handleClearChatMessage] Twitch channel {} not found",
|
|
|
|
chanName);
|
2018-01-01 23:54:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the chat has been cleared by a moderator
|
|
|
|
if (message->parameters().length() == 1) {
|
2018-02-05 15:11:50 +01:00
|
|
|
chan->addMessage(Message::createSystemMessage("Chat has been cleared by a moderator."));
|
2018-01-01 23:54:54 +01: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");
|
|
|
|
if (v.isValid()) {
|
|
|
|
durationInSeconds = v.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
v = message->tag("ban-reason");
|
|
|
|
if (v.isValid()) {
|
|
|
|
reason = v.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the notice that the user has been timed out
|
2018-02-05 15:11:50 +01:00
|
|
|
LimitedQueueSnapshot<MessagePtr> snapshot = chan->getMessageSnapshot();
|
2018-01-05 23:14:55 +01:00
|
|
|
bool addMessage = true;
|
2018-01-11 20:16:25 +01:00
|
|
|
int snapshotLength = snapshot.getLength();
|
2018-01-05 23:14:55 +01:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
for (int i = std::max(0, snapshotLength - 20); i < snapshotLength; i++) {
|
2018-01-28 03:29:42 +01:00
|
|
|
if (snapshot[i]->flags & Message::Timeout && snapshot[i]->loginName == username) {
|
2018-01-11 20:16:25 +01:00
|
|
|
MessagePtr replacement(
|
2018-01-05 23:14:55 +01:00
|
|
|
Message::createTimeoutMessage(username, durationInSeconds, reason, true));
|
2018-02-05 15:11:50 +01:00
|
|
|
chan->replaceMessage(snapshot[i], replacement);
|
2018-01-05 23:14:55 +01:00
|
|
|
addMessage = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-01-05 23:14:55 +01:00
|
|
|
if (addMessage) {
|
2018-02-05 15:11:50 +01:00
|
|
|
chan->addMessage(Message::createTimeoutMessage(username, durationInSeconds, reason, false));
|
2018-01-05 23:14:55 +01:00
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
|
|
|
|
// disable the messages from the user
|
2018-01-11 20:16:25 +01:00
|
|
|
for (int i = 0; i < snapshotLength; i++) {
|
2018-01-28 03:29:42 +01:00
|
|
|
if (!(snapshot[i]->flags & Message::Timeout) && snapshot[i]->loginName == username) {
|
|
|
|
snapshot[i]->flags &= Message::Disabled;
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// refresh all
|
2018-02-05 15:11:50 +01:00
|
|
|
WindowManager::getInstance().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-03-25 11:37:57 +02:00
|
|
|
QString channelName;
|
|
|
|
if (!TrimChannelName(message->parameter(0), channelName)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-17 18:36:12 +01:00
|
|
|
|
2018-03-25 11:37:57 +02:00
|
|
|
auto c = TwitchServer::getInstance().getChannel(channelName);
|
2018-01-17 18:36:12 +01:00
|
|
|
twitch::TwitchChannel *tc = dynamic_cast<twitch::TwitchChannel *>(c.get());
|
|
|
|
if (tc != nullptr) {
|
|
|
|
tc->setMod(_mod == "1");
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
2018-02-04 16:33:46 +01:00
|
|
|
debug::Log("Received whisper!");
|
|
|
|
messages::MessageParseArgs args;
|
|
|
|
|
|
|
|
args.isReceivedWhisper = true;
|
|
|
|
|
2018-02-05 21:20:38 +01:00
|
|
|
auto c = TwitchServer::getInstance().whispersChannel.get();
|
2018-02-04 16:33:46 +01:00
|
|
|
|
|
|
|
twitch::TwitchMessageBuilder builder(c, message, message->parameter(1), args);
|
|
|
|
|
|
|
|
if (!builder.isIgnored()) {
|
|
|
|
messages::MessagePtr _message = builder.build();
|
|
|
|
if (_message->flags & messages::Message::Highlighted) {
|
2018-02-05 21:20:38 +01:00
|
|
|
TwitchServer::getInstance().mentionsChannel->addMessage(_message);
|
2018-02-04 16:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c->addMessage(_message);
|
|
|
|
|
|
|
|
if (SettingManager::getInstance().inlineWhispers) {
|
2018-02-05 21:20:38 +01:00
|
|
|
TwitchServer::getInstance().forEachChannel([_message](ChannelPtr channel) {
|
2018-02-04 16:33:46 +01:00
|
|
|
channel->addMessage(_message); //
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message)
|
|
|
|
{
|
2018-02-05 15:11:50 +01:00
|
|
|
auto channel = TwitchServer::getInstance().getChannel(message->parameter(0).remove(0, 1));
|
|
|
|
|
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-01-11 20:16:25 +01:00
|
|
|
MessagePtr msg = Message::createSystemMessage(message->content());
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-03-25 11:37:57 +02:00
|
|
|
QString channelName;
|
|
|
|
if (!TrimChannelName(message->target(), channelName)) {
|
|
|
|
// Notice wasn't targeted at a single channel, send to all twitch channels
|
2018-02-05 21:20:38 +01:00
|
|
|
TwitchServer::getInstance().forEachChannelAndSpecialChannels([msg](const auto &c) {
|
2018-01-01 23:54:54 +01:00
|
|
|
c->addMessage(msg); //
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-25 11:37:57 +02:00
|
|
|
auto channel = TwitchServer::getInstance().getChannel(channelName);
|
2018-01-01 23:54:54 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
if (!channel) {
|
2018-01-01 23:54:54 +01:00
|
|
|
debug::Log("[IrcManager:handleNoticeMessage] Channel {} not found in channel manager",
|
2018-03-25 11:37:57 +02:00
|
|
|
channelName);
|
2018-01-01 23:54:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
channel->addMessage(msg);
|
2018-01-01 23:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IrcMessageHandler::handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message)
|
|
|
|
{
|
|
|
|
QVariant v = message->tag("msg-id");
|
|
|
|
if (!v.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString msg_id = v.toString();
|
|
|
|
|
|
|
|
static QList<QString> idsToSkip = {"timeout_success", "ban_success"};
|
|
|
|
|
|
|
|
if (idsToSkip.contains(msg_id)) {
|
|
|
|
// Already handled in the read-connection
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->handleNoticeMessage(message);
|
|
|
|
}
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace twitch
|
|
|
|
} // namespace providers
|
2018-01-17 02:22:57 +01:00
|
|
|
} // namespace chatterino
|