mirror-chatterino2/src/singletons/ircmanager.cpp

534 lines
16 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#include "singletons/ircmanager.hpp"
2017-06-11 09:31:45 +02:00
#include "asyncexec.hpp"
#include "channel.hpp"
#include "debug/log.hpp"
2017-06-11 09:31:45 +02:00
#include "messages/messageparseargs.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/accountmanager.hpp"
#include "singletons/channelmanager.hpp"
#include "singletons/emotemanager.hpp"
2017-12-31 02:21:33 +01:00
#include "singletons/resourcemanager.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/settingsmanager.hpp"
#include "singletons/windowmanager.hpp"
2017-06-11 09:31:45 +02:00
#include "twitch/twitchmessagebuilder.hpp"
#include "twitch/twitchparsemessage.hpp"
#include "twitch/twitchuser.hpp"
#include "util/urlfetch.hpp"
#include <irccommand.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QNetworkRequest>
2017-04-12 17:46:44 +02:00
#include <future>
2017-01-04 15:12:31 +01:00
2017-04-14 17:52:22 +02:00
using namespace chatterino::messages;
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-12-31 22:58:35 +01:00
namespace singletons {
2017-01-18 21:30:23 +01:00
2017-12-31 02:21:33 +01:00
IrcManager::IrcManager(ChannelManager &_channelManager, ResourceManager &_resources,
2017-12-31 00:50:07 +01:00
AccountManager &_accountManager)
: channelManager(_channelManager)
, resources(_resources)
2017-12-31 00:50:07 +01:00
, accountManager(_accountManager)
2017-01-03 21:19:33 +01:00
{
this->messageSuffix.append(' ');
this->messageSuffix.append(QChar(0x206D));
2017-12-31 00:50:07 +01:00
this->account = accountManager.Twitch.getCurrent();
accountManager.Twitch.userChanged.connect([this]() {
this->setUser(accountManager.Twitch.getCurrent());
2017-01-03 21:19:33 +01:00
debug::Log("[IrcManager] Reconnecting to Twitch IRC as new user {}",
this->account->getUserName());
postToThread([this] { this->connect(); });
});
// Initialize the connections
this->writeConnection.reset(new Communi::IrcConnection);
this->writeConnection->moveToThread(QCoreApplication::instance()->thread());
QObject::connect(this->writeConnection.get(), &Communi::IrcConnection::messageReceived, this,
&IrcManager::writeConnectionMessageReceived);
this->readConnection.reset(new Communi::IrcConnection);
this->readConnection->moveToThread(QCoreApplication::instance()->thread());
// Listen to read connection message signals
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::messageReceived, this,
&IrcManager::messageReceived);
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::privateMessageReceived,
this, &IrcManager::privateMessageReceived);
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::connected, this,
&IrcManager::onConnected);
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected, this,
&IrcManager::onDisconnected);
2017-04-12 17:46:44 +02:00
}
2017-01-03 21:19:33 +01:00
2017-12-31 00:50:07 +01:00
IrcManager &IrcManager::getInstance()
{
2017-12-31 22:58:35 +01:00
static IrcManager instance(ChannelManager::getInstance(),
singletons::ResourceManager::getInstance(),
2017-12-31 00:50:07 +01:00
AccountManager::getInstance());
return instance;
}
void IrcManager::setUser(std::shared_ptr<twitch::TwitchUser> newAccount)
2017-04-12 17:46:44 +02:00
{
this->account = newAccount;
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
void IrcManager::connect()
2017-01-03 21:19:33 +01:00
{
this->disconnect();
this->initializeConnection(this->writeConnection, false);
this->initializeConnection(this->readConnection, true);
// XXX(pajlada): Disabled the async_exec for now, because if we happen to run the
// `beginConnecting` function in a different thread than last time, we won't be able to connect
// because we can't clean up the previous connection properly
// async_exec([this] { beginConnecting(); });
this->beginConnecting();
2017-04-12 17:46:44 +02:00
}
2017-01-03 21:19:33 +01:00
void IrcManager::initializeConnection(const std::unique_ptr<Communi::IrcConnection> &connection,
bool isReadConnection)
2017-04-12 17:46:44 +02:00
{
assert(this->account);
2017-04-12 17:46:44 +02:00
QString username = this->account->getUserName();
QString oauthClient = this->account->getOAuthClient();
QString oauthToken = this->account->getOAuthToken();
if (!oauthToken.startsWith("oauth:")) {
oauthToken.prepend("oauth:");
}
connection->setUserName(username);
connection->setNickName(username);
connection->setRealName(username);
if (!this->account->isAnon()) {
connection->setPassword(oauthToken);
this->refreshIgnoredUsers(username, oauthClient, oauthToken);
}
2017-01-04 15:12:31 +01:00
if (isReadConnection) {
connection->sendCommand(
Communi::IrcCommand::createCapability("REQ", "twitch.tv/membership"));
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
} else {
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
connection->sendCommand(
Communi::IrcCommand::createCapability("REQ", "twitch.tv/membership"));
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
}
2017-01-11 18:52:09 +01:00
connection->setHost("irc.chat.twitch.tv");
connection->setPort(6667);
}
void IrcManager::refreshIgnoredUsers(const QString &username, const QString &oauthClient,
const QString &oauthToken)
{
QString nextLink = "https://api.twitch.tv/kraken/users/" + username + "/blocks?limit=" + 100 +
"&client_id=" + oauthClient;
2017-01-11 18:52:09 +01:00
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
QNetworkReply *reply = manager->get(req);
2017-01-11 18:52:09 +01:00
QObject::connect(reply, &QNetworkReply::finished, [=] {
2017-09-21 12:15:01 +02:00
this->twitchBlockedUsersMutex.lock();
this->twitchBlockedUsers.clear();
this->twitchBlockedUsersMutex.unlock();
2017-01-11 18:52:09 +01:00
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
2017-01-11 18:52:09 +01:00
// nextLink =
2017-09-21 12:15:01 +02:00
// root.value("this->links").toObject().value("next").toString();
2017-01-30 19:14:25 +01:00
auto blocks = root.value("blocks").toArray();
2017-09-21 12:15:01 +02:00
this->twitchBlockedUsersMutex.lock();
for (QJsonValue block : blocks) {
QJsonObject user = block.toObject().value("user").toObject();
2017-09-21 12:15:01 +02:00
// displaythis->name
this->twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
2017-01-04 15:12:31 +01:00
}
2017-09-21 12:15:01 +02:00
this->twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
manager->deleteLater();
});
}
void IrcManager::beginConnecting()
{
std::lock_guard<std::mutex> locker(this->connectionMutex);
2017-01-03 21:19:33 +01:00
for (auto &channel : this->channelManager.getItems()) {
this->writeConnection->sendRaw("JOIN #" + channel->name);
this->readConnection->sendRaw("JOIN #" + channel->name);
2017-01-03 21:19:33 +01:00
}
this->writeConnection->open();
this->readConnection->open();
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
void IrcManager::disconnect()
2017-01-03 21:19:33 +01:00
{
std::lock_guard<std::mutex> locker(this->connectionMutex);
2017-04-12 17:46:44 +02:00
this->readConnection->close();
this->writeConnection->close();
2017-01-29 13:23:22 +01:00
}
void IrcManager::sendMessage(const QString &channelName, QString message)
2017-01-17 00:15:44 +01:00
{
this->connectionMutex.lock();
static int i = 0;
2017-06-06 16:06:13 +02:00
if (this->writeConnection) {
2017-12-31 22:58:35 +01:00
if (singletons::SettingManager::getInstance().allowDuplicateMessages && (++i % 2) == 0) {
message.append(this->messageSuffix);
}
2017-06-06 16:06:13 +02:00
this->writeConnection->sendRaw("PRIVMSG #" + channelName + " :" + message);
}
this->connectionMutex.unlock();
}
2017-06-06 16:06:13 +02:00
void IrcManager::joinChannel(const QString &channelName)
{
this->connectionMutex.lock();
2017-06-06 16:06:13 +02:00
if (this->readConnection && this->writeConnection) {
this->readConnection->sendRaw("JOIN #" + channelName);
this->writeConnection->sendRaw("JOIN #" + channelName);
2017-01-17 00:15:44 +01:00
}
this->connectionMutex.unlock();
2017-01-17 00:15:44 +01:00
}
2017-06-06 16:06:13 +02:00
void IrcManager::partChannel(const QString &channelName)
2017-01-17 00:15:44 +01:00
{
this->connectionMutex.lock();
if (this->readConnection && this->writeConnection) {
2017-06-06 16:06:13 +02:00
this->readConnection->sendRaw("PART #" + channelName);
this->writeConnection->sendRaw("PART #" + channelName);
2017-01-17 00:15:44 +01:00
}
this->connectionMutex.unlock();
2017-01-03 21:19:33 +01:00
}
void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message)
{
this->onPrivateMessage.invoke(message);
auto c = this->channelManager.getTwitchChannel(message->target().mid(1));
if (!c) {
2017-07-02 18:12:11 +02:00
return;
}
2017-07-02 18:12:11 +02:00
messages::MessageParseArgs args;
2017-12-31 00:50:07 +01:00
twitch::TwitchMessageBuilder builder(c.get(), message, args);
2017-07-02 18:12:11 +02:00
c->addMessage(builder.parse());
}
void IrcManager::messageReceived(Communi::IrcMessage *message)
2017-01-03 21:19:33 +01:00
{
if (message->type() == Communi::IrcMessage::Type::Private) {
// We already have a handler for private messages
return;
}
2017-01-04 15:12:31 +01:00
const QString &command = message->command();
2017-01-18 01:04:54 +01:00
if (command == "ROOMSTATE") {
this->handleRoomStateMessage(message);
} else if (command == "CLEARCHAT") {
this->handleClearChatMessage(message);
2017-05-27 16:16:39 +02:00
} else if (command == "USERSTATE") {
this->handleUserStateMessage(message);
2017-05-27 16:16:39 +02:00
} else if (command == "WHISPER") {
this->handleWhisperMessage(message);
2017-05-27 16:16:39 +02:00
} else if (command == "USERNOTICE") {
this->handleUserNoticeMessage(message);
} else if (command == "MODE") {
this->handleModeMessage(message);
2017-12-16 19:20:57 +01:00
} else if (command == "NOTICE") {
this->handleNoticeMessage(static_cast<Communi::IrcNoticeMessage *>(message));
2017-05-27 16:16:39 +02:00
}
2017-01-03 21:19:33 +01:00
}
void IrcManager::writeConnectionMessageReceived(Communi::IrcMessage *message)
{
switch (message->type()) {
case Communi::IrcMessage::Type::Notice: {
this->handleWriteConnectionNoticeMessage(
static_cast<Communi::IrcNoticeMessage *>(message));
} break;
}
}
void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message)
2017-01-03 21:19:33 +01:00
{
const auto &tags = message->tags();
2017-01-05 16:07:20 +01:00
auto iterator = tags.find("room-id");
2017-04-12 17:46:44 +02:00
if (iterator != tags.end()) {
auto roomID = iterator.value().toString();
2017-12-31 00:50:07 +01:00
auto channel = channelManager.getTwitchChannel(QString(message->toData()).split("#").at(1));
auto twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
if (twitchChannel != nullptr) {
twitchChannel->setRoomID(roomID);
}
this->resources.loadChannelData(roomID);
2017-01-05 16:07:20 +01:00
}
2017-01-03 21:19:33 +01:00
}
2017-01-04 15:12:31 +01:00
void IrcManager::handleClearChatMessage(Communi::IrcMessage *message)
{
assert(message->parameters().length() >= 1);
auto rawChannelName = message->parameter(0);
assert(rawChannelName.length() >= 2);
auto trimmedChannelName = rawChannelName.mid(1);
auto c = this->channelManager.getTwitchChannel(trimmedChannelName);
if (!c) {
debug::Log("[IrcManager:handleClearChatMessage] Channel {} not found in channel manager",
trimmedChannelName);
return;
}
if (message->parameters().length() == 1) {
std::shared_ptr<Message> msg(
Message::createSystemMessage("Chat has been cleared by a moderator."));
c->addMessage(msg);
return;
}
assert(message->parameters().length() >= 2);
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();
}
std::shared_ptr<Message> msg(
Message::createTimeoutMessage(username, durationInSeconds, reason));
c->addMessage(msg);
}
void IrcManager::handleUserStateMessage(Communi::IrcMessage *message)
{
// TODO: Implement
}
void IrcManager::handleWhisperMessage(Communi::IrcMessage *message)
{
// TODO: Implement
}
void IrcManager::handleUserNoticeMessage(Communi::IrcMessage *message)
{
// do nothing
}
void IrcManager::handleModeMessage(Communi::IrcMessage *message)
{
auto channel = channelManager.getTwitchChannel(message->parameter(0).remove(0, 1));
if (message->parameter(1) == "+o") {
channel->modList.append(message->parameter(2));
} else if (message->parameter(1) == "-o") {
channel->modList.append(message->parameter(2));
}
}
// XXX: This does not fit in IrcManager
2017-04-12 17:46:44 +02:00
bool IrcManager::isTwitchBlockedUser(QString const &username)
2017-01-04 15:12:31 +01:00
{
2017-09-21 12:15:01 +02:00
QMutexLocker locker(&this->twitchBlockedUsersMutex);
2017-01-04 15:12:31 +01:00
2017-09-21 12:15:01 +02:00
auto iterator = this->twitchBlockedUsers.find(username);
2017-01-04 15:12:31 +01:00
2017-09-21 12:15:01 +02:00
return iterator != this->twitchBlockedUsers.end();
2017-01-04 15:12:31 +01:00
}
// XXX: This does not fit in IrcManager
2017-04-12 17:46:44 +02:00
bool IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
2017-01-04 15:12:31 +01:00
{
assert(this->account);
QUrl url("https://api.twitch.tv/kraken/users/" + this->account->getUserName() + "/blocks/" +
username + "?oauth_token=" + this->account->getOAuthToken() +
"&client_id=" + this->account->getOAuthClient());
2017-01-04 15:12:31 +01:00
QNetworkRequest request(url);
auto reply = this->networkAccessManager.put(request, QByteArray());
2017-01-04 15:12:31 +01:00
reply->waitForReadyRead(10000);
2017-01-11 18:52:09 +01:00
if (reply->error() == QNetworkReply::NoError) {
2017-09-21 12:15:01 +02:00
this->twitchBlockedUsersMutex.lock();
this->twitchBlockedUsers.insert(username, true);
this->twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
return true;
}
2017-04-12 17:46:44 +02:00
reply->deleteLater();
errorMessage = "Error while ignoring user \"" + username + "\": " + reply->errorString();
2017-01-04 15:12:31 +01:00
return false;
}
// XXX: This does not fit in IrcManager
2017-04-12 17:46:44 +02:00
void IrcManager::addIgnoredUser(QString const &username)
2017-01-04 15:12:31 +01:00
{
QString errorMessage;
2017-01-05 16:07:20 +01:00
if (!tryAddIgnoredUser(username, errorMessage)) {
// TODO: Implement IrcManager::addIgnoredUser
2017-01-04 15:12:31 +01:00
}
}
// XXX: This does not fit in IrcManager
2017-04-12 17:46:44 +02:00
bool IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
2017-01-04 15:12:31 +01:00
{
assert(this->account);
QUrl url("https://api.twitch.tv/kraken/users/" + this->account->getUserName() + "/blocks/" +
username + "?oauth_token=" + this->account->getOAuthToken() +
"&client_id=" + this->account->getOAuthClient());
2017-01-04 15:12:31 +01:00
QNetworkRequest request(url);
auto reply = this->networkAccessManager.deleteResource(request);
2017-01-04 15:12:31 +01:00
reply->waitForReadyRead(10000);
2017-01-11 18:52:09 +01:00
if (reply->error() == QNetworkReply::NoError) {
2017-09-21 12:15:01 +02:00
this->twitchBlockedUsersMutex.lock();
this->twitchBlockedUsers.remove(username);
this->twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
return true;
}
2017-04-12 17:46:44 +02:00
reply->deleteLater();
errorMessage = "Error while unignoring user \"" + username + "\": " + reply->errorString();
2017-01-04 15:12:31 +01:00
return false;
}
// XXX: This does not fit in IrcManager
2017-04-12 17:46:44 +02:00
void IrcManager::removeIgnoredUser(QString const &username)
2017-01-04 15:12:31 +01:00
{
QString errorMessage;
2017-01-05 16:07:20 +01:00
if (!tryRemoveIgnoredUser(username, errorMessage)) {
// TODO: Implement IrcManager::removeIgnoredUser
2017-01-04 15:12:31 +01:00
}
}
2017-04-12 17:46:44 +02:00
2017-12-16 19:20:57 +01:00
void IrcManager::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
auto rawChannelName = message->target();
bool broadcast = rawChannelName.length() < 2;
std::shared_ptr<Message> msg(Message::createSystemMessage(message->content()));
if (broadcast) {
this->channelManager.doOnAll([msg](const auto &c) {
c->addMessage(msg); //
});
return;
}
2017-12-16 19:20:57 +01:00
auto trimmedChannelName = rawChannelName.mid(1);
auto c = this->channelManager.getTwitchChannel(trimmedChannelName);
if (!c) {
debug::Log("[IrcManager:handleNoticeMessage] Channel {} not found in channel manager",
trimmedChannelName);
return;
}
c->addMessage(msg);
}
void IrcManager::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);
}
void IrcManager::onConnected()
{
std::shared_ptr<Message> msg(Message::createSystemMessage("connected to chat"));
2017-12-31 00:50:07 +01:00
this->channelManager.doOnAll([msg](std::shared_ptr<Channel> channel) {
assert(channel);
channel->addMessage(msg);
});
}
void IrcManager::onDisconnected()
{
std::shared_ptr<Message> msg(Message::createSystemMessage("disconnected from chat"));
2017-12-31 00:50:07 +01:00
this->channelManager.doOnAll([msg](std::shared_ptr<Channel> channel) {
assert(channel);
channel->addMessage(msg);
});
}
Communi::IrcConnection *IrcManager::getReadConnection()
{
return this->readConnection.get();
}
2017-05-27 16:16:39 +02:00
} // namespace chatterino
2017-12-31 22:58:35 +01:00
}