mirror-chatterino2/ircmanager.cpp

327 lines
9.1 KiB
C++
Raw Normal View History

2017-01-03 21:19:33 +01:00
#include "ircmanager.h"
2017-05-27 16:16:39 +02:00
#include "accountmanager.h"
2017-01-11 18:52:09 +01:00
#include "asyncexec.h"
2017-01-05 16:07:20 +01:00
#include "channel.h"
2017-04-12 17:46:44 +02:00
#include "channelmanager.h"
#include "messages/messageparseargs.h"
#include "twitch/twitchmessagebuilder.h"
#include "twitch/twitchparsemessage.h"
#include "twitch/twitchuser.h"
#include <irccommand.h>
#include <ircconnection.h>
2017-04-12 17:46:44 +02:00
#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-01-18 21:30:23 +01:00
2017-04-12 17:46:44 +02:00
IrcManager IrcManager::instance;
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
const QString IrcManager::defaultClientId("7ue61iz46fz11y3cugd0l3tawb4taal");
2017-01-03 22:08:20 +01:00
2017-01-03 21:19:33 +01:00
IrcManager::IrcManager()
2017-04-13 16:06:23 +02:00
: _account(AccountManager::getInstance().getTwitchAnon())
2017-04-12 17:46:44 +02:00
, _connection()
, _connectionMutex()
, _connectionGeneration(0)
, _twitchBlockedUsers()
, _twitchBlockedUsersMutex()
, _accessManager()
2017-01-03 21:19:33 +01:00
{
}
2017-04-12 17:46:44 +02:00
const twitch::TwitchUser &IrcManager::getUser() const
2017-01-03 21:19:33 +01:00
{
2017-04-12 17:46:44 +02:00
return _account;
}
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
void IrcManager::setUser(const twitch::TwitchUser &account)
{
_account = account;
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
{
2017-04-12 17:46:44 +02:00
disconnect();
2017-04-12 17:46:44 +02:00
async_exec([this] { beginConnecting(); });
}
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
void IrcManager::beginConnecting()
{
int generation = ++IrcManager::_connectionGeneration;
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
Communi::IrcConnection *c = new Communi::IrcConnection;
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
QObject::connect(c, &Communi::IrcConnection::messageReceived, this,
&IrcManager::messageReceived);
QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this,
&IrcManager::privateMessageReceived);
QString username = _account.getUserName();
QString oauthClient = _account.getOAuthClient();
QString oauthToken = _account.getOAuthToken();
c->setUserName(username);
c->setNickName(username);
c->setRealName(username);
if (!_account.isAnon()) {
c->setPassword(oauthToken);
2017-01-04 15:12:31 +01:00
// fetch ignored users
2017-01-04 15:12:31 +01:00
{
2017-04-12 17:46:44 +02:00
QString nextLink = "https://api.twitch.tv/kraken/users/" + username +
"/blocks?limit=" + 100 + "&client_id=" + oauthClient;
2017-01-11 18:52:09 +01:00
2017-01-30 19:14:25 +01:00
QNetworkAccessManager *manager = new QNetworkAccessManager();
2017-01-11 18:52:09 +01:00
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
2017-01-30 19:14:25 +01:00
QNetworkReply *reply = manager->get(req);
2017-01-11 18:52:09 +01:00
QObject::connect(reply, &QNetworkReply::finished, [=] {
2017-04-12 17:46:44 +02:00
_twitchBlockedUsersMutex.lock();
_twitchBlockedUsers.clear();
_twitchBlockedUsersMutex.unlock();
2017-01-11 18:52:09 +01:00
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
// nextLink =
// root.value("_links").toObject().value("next").toString();
auto blocks = root.value("blocks").toArray();
2017-04-12 17:46:44 +02:00
_twitchBlockedUsersMutex.lock();
2017-01-11 18:52:09 +01:00
for (QJsonValue block : blocks) {
2017-04-12 17:46:44 +02:00
QJsonObject user = block.toObject().value("user").toObject();
2017-01-11 18:52:09 +01:00
// display_name
2017-04-12 17:46:44 +02:00
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
2017-01-11 18:52:09 +01:00
}
2017-04-12 17:46:44 +02:00
_twitchBlockedUsersMutex.unlock();
2017-01-30 19:14:25 +01:00
manager->deleteLater();
2017-01-11 18:52:09 +01:00
});
2017-01-04 15:12:31 +01:00
}
}
2017-01-04 15:12:31 +01:00
// fetch available twitch emtoes
{
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username +
"/emotes?oauth_token=" + oauthToken +
"&client_id=" + oauthClient));
QNetworkReply *reply = _accessManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [=] {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
// nextLink =
// root.value("_links").toObject().value("next").toString();
auto blocks = root.value("blocks").toArray();
_twitchBlockedUsersMutex.lock();
for (QJsonValue block : blocks) {
QJsonObject user = block.toObject().value("user").toObject();
// display_name
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
}
_twitchBlockedUsersMutex.unlock();
});
2017-01-04 15:12:31 +01:00
}
2017-01-03 21:19:33 +01:00
c->setHost("irc.chat.twitch.tv");
c->setPort(6667);
2017-04-12 17:46:44 +02:00
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
QMutexLocker locker(&_connectionMutex);
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
if (generation == _connectionGeneration) {
2017-01-03 22:08:20 +01:00
c->moveToThread(QCoreApplication::instance()->thread());
2017-04-12 17:46:44 +02:00
_connection = std::shared_ptr<Communi::IrcConnection>(c);
2017-01-30 19:14:25 +01:00
2017-04-12 17:46:44 +02:00
for (auto &channel : ChannelManager::getInstance().getItems()) {
c->sendRaw("JOIN #" + channel->getName());
2017-01-30 19:14:25 +01:00
}
c->open();
2017-01-11 18:52:09 +01:00
} else {
2017-01-03 21:19:33 +01:00
delete c;
}
}
2017-04-12 17:46:44 +02:00
void IrcManager::disconnect()
2017-01-03 21:19:33 +01:00
{
2017-04-12 17:46:44 +02:00
_connectionMutex.lock();
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
auto c = _connection;
if (_connection.get() != NULL) {
_connection = std::shared_ptr<Communi::IrcConnection>();
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
_connectionMutex.unlock();
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
void IrcManager::send(QString raw)
2017-01-29 13:23:22 +01:00
{
2017-04-12 17:46:44 +02:00
_connectionMutex.lock();
_connection->sendRaw(raw);
_connectionMutex.unlock();
2017-01-29 13:23:22 +01:00
}
2017-04-12 17:46:44 +02:00
void IrcManager::sendJoin(const QString &channel)
2017-01-17 00:15:44 +01:00
{
2017-04-12 17:46:44 +02:00
_connectionMutex.lock();
2017-04-12 17:46:44 +02:00
if (_connection.get() != NULL) {
_connection->sendRaw("JOIN #" + channel);
}
_connectionMutex.unlock();
}
void IrcManager::sendMessage(const QString &channelName, const QString &message)
{
_connectionMutex.lock();
if (_connection.get() != nullptr) {
qDebug() << "IRC Manager send message " << message << " to channel " << channelName;
QString xd = "PRIVMSG #" + channelName + " :" + message;
qDebug() << xd;
_connection->sendRaw(xd);
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
_connectionMutex.unlock();
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
void IrcManager::partChannel(const QString &channel)
2017-01-17 00:15:44 +01:00
{
2017-04-12 17:46:44 +02:00
_connectionMutex.lock();
2017-04-12 17:46:44 +02:00
if (_connection.get() != NULL) {
_connection.get()->sendRaw("PART #" + channel);
2017-01-17 00:15:44 +01:00
}
2017-04-12 17:46:44 +02:00
_connectionMutex.unlock();
2017-01-03 21:19:33 +01:00
}
2017-05-27 16:16:39 +02:00
void IrcManager::messageReceived(Communi::IrcMessage * /*message*/)
2017-01-03 21:19:33 +01:00
{
2017-05-27 16:16:39 +02:00
// qInfo(message->command().toStdString().c_str());
2017-01-04 15:12:31 +01:00
2017-05-27 16:16:39 +02:00
/*
2017-01-18 01:04:54 +01:00
const QString &command = message->command();
2017-05-27 16:16:39 +02:00
if (command == "CLEARCHAT") {
} else if (command == "ROOMSTATE") {
} else if (command == "USERSTATE") {
} else if (command == "WHISPER") {
} else if (command == "USERNOTICE") {
}
*/
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message)
2017-01-03 21:19:33 +01:00
{
2017-04-12 17:46:44 +02:00
auto c = ChannelManager::getInstance().getChannel(message->target().mid(1));
2017-01-05 16:07:20 +01:00
if (c != NULL) {
2017-04-12 17:46:44 +02:00
messages::MessageParseArgs args;
c->addMessage(twitch::TwitchMessageBuilder::parse(message, c.get(), args));
2017-01-05 16:07:20 +01:00
}
2017-01-03 21:19:33 +01:00
}
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
bool IrcManager::isTwitchBlockedUser(QString const &username)
2017-01-04 15:12:31 +01:00
{
2017-04-12 17:46:44 +02:00
QMutexLocker locker(&_twitchBlockedUsersMutex);
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
auto iterator = _twitchBlockedUsers.find(username);
2017-01-04 15:12:31 +01:00
2017-04-12 17:46:44 +02:00
return iterator != _twitchBlockedUsers.end();
2017-01-04 15:12:31 +01:00
}
2017-04-12 17:46:44 +02:00
bool IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
2017-01-04 15:12:31 +01:00
{
2017-04-12 17:46:44 +02:00
QUrl url("https://api.twitch.tv/kraken/users/" + _account.getUserName() + "/blocks/" +
username + "?oauth_token=" + _account.getOAuthToken() +
"&client_id=" + _account.getOAuthClient());
2017-01-04 15:12:31 +01:00
QNetworkRequest request(url);
2017-04-12 17:46:44 +02:00
auto reply = _accessManager.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-04-12 17:46:44 +02:00
_twitchBlockedUsersMutex.lock();
_twitchBlockedUsers.insert(username, true);
_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;
}
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
}
}
2017-04-12 17:46:44 +02:00
bool IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
2017-01-04 15:12:31 +01:00
{
2017-04-12 17:46:44 +02:00
QUrl url("https://api.twitch.tv/kraken/users/" + _account.getUserName() + "/blocks/" +
username + "?oauth_token=" + _account.getOAuthToken() +
"&client_id=" + _account.getOAuthClient());
2017-01-04 15:12:31 +01:00
QNetworkRequest request(url);
2017-04-12 17:46:44 +02:00
auto reply = _accessManager.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-04-12 17:46:44 +02:00
_twitchBlockedUsersMutex.lock();
_twitchBlockedUsers.remove(username);
_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;
}
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
QNetworkAccessManager &IrcManager::getAccessManager()
{
return _accessManager;
}
2017-05-27 16:16:39 +02:00
} // namespace chatterino