mirror-chatterino2/ircmanager.cpp

301 lines
8.8 KiB
C++
Raw Normal View History

2017-01-03 21:19:33 +01:00
#include "ircmanager.h"
2017-01-11 18:52:09 +01:00
#include "asyncexec.h"
2017-01-05 16:07:20 +01:00
#include "channel.h"
#include "channels.h"
#include <irccommand.h>
#include <ircconnection.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <future>
2017-01-04 15:12:31 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
Account *IrcManager::account = nullptr;
2017-01-18 04:33:30 +01:00
IrcConnection *IrcManager::connection = NULL;
QMutex IrcManager::connectionMutex;
long IrcManager::connectionGeneration = 0;
2017-01-11 18:52:09 +01:00
const QString IrcManager::defaultClientId = "7ue61iz46fz11y3cugd0l3tawb4taal";
2017-01-18 04:33:30 +01:00
QNetworkAccessManager IrcManager::accessManager;
2017-01-04 15:12:31 +01:00
2017-01-18 04:33:30 +01:00
QMap<QString, bool> IrcManager::twitchBlockedUsers;
QMutex IrcManager::twitchBlockedUsersMutex;
2017-01-03 22:08:20 +01:00
2017-01-03 21:19:33 +01:00
IrcManager::IrcManager()
{
}
2017-01-11 18:52:09 +01:00
void
IrcManager::connect()
2017-01-03 21:19:33 +01:00
{
disconnect();
2017-01-11 18:52:09 +01:00
async_exec([] { beginConnecting(); });
2017-01-03 21:19:33 +01:00
}
2017-01-11 18:52:09 +01:00
void
IrcManager::beginConnecting()
2017-01-03 21:19:33 +01:00
{
2017-01-18 04:33:30 +01:00
IrcManager::account = const_cast<Account *>(Account::getAnon());
2017-01-18 04:33:30 +01:00
int generation = ++IrcManager::connectionGeneration;
2017-01-03 21:19:33 +01:00
auto c = new IrcConnection();
2017-01-11 18:52:09 +01:00
QObject::connect(c, &IrcConnection::messageReceived, &messageReceived);
QObject::connect(c, &IrcConnection::privateMessageReceived,
2017-01-03 21:19:33 +01:00
&privateMessageReceived);
2017-01-04 15:12:31 +01:00
if (account->isAnon()) {
// fetch ignored users
2017-01-18 04:33:30 +01:00
QString username = account->getUsername();
QString oauthClient = account->getOauthClient();
QString oauthToken = account->getOauthToken();
2017-01-04 15:12:31 +01:00
{
2017-01-11 18:52:09 +01:00
QString nextLink = "https://api.twitch.tv/kraken/users/" +
username + "/blocks?limit=" + 100 +
"&client_id=" + oauthClient;
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
2017-01-18 04:33:30 +01:00
QNetworkReply *reply = IrcManager::accessManager.get(req);
2017-01-11 18:52:09 +01:00
QObject::connect(reply, &QNetworkReply::finished, [=] {
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.lock();
IrcManager::twitchBlockedUsers.clear();
IrcManager::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-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.lock();
2017-01-11 18:52:09 +01:00
for (QJsonValue block : blocks) {
QJsonObject user =
block.toObject().value("user").toObject();
// display_name
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsers.insert(
2017-01-11 18:52:09 +01:00
user.value("name").toString().toLower(), true);
}
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.unlock();
2017-01-11 18:52:09 +01:00
});
2017-01-04 15:12:31 +01:00
}
// fetch available twitch emtoes
{
2017-01-11 18:52:09 +01:00
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" +
username + "/emotes?oauth_token=" +
oauthToken + "&client_id=" + oauthClient));
2017-01-18 04:33:30 +01:00
QNetworkReply *reply = IrcManager::accessManager.get(req);
2017-01-11 18:52:09 +01:00
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();
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.lock();
2017-01-11 18:52:09 +01:00
for (QJsonValue block : blocks) {
QJsonObject user =
block.toObject().value("user").toObject();
// display_name
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsers.insert(
2017-01-11 18:52:09 +01:00
user.value("name").toString().toLower(), true);
}
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.unlock();
2017-01-11 18:52:09 +01:00
});
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);
c->setUserName("justinfan123");
c->setNickName("justinfan123");
c->setRealName("justinfan123");
c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/commands"));
c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/tags"));
c->open();
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.lock();
if (generation == IrcManager::connectionGeneration) {
delete IrcManager::connection;
2017-01-03 22:08:20 +01:00
c->moveToThread(QCoreApplication::instance()->thread());
2017-01-18 04:33:30 +01:00
IrcManager::connection = c;
2017-01-11 18:52:09 +01:00
} else {
2017-01-03 21:19:33 +01:00
delete c;
}
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.unlock();
2017-01-03 21:19:33 +01:00
}
2017-01-11 18:52:09 +01:00
void
IrcManager::disconnect()
2017-01-03 21:19:33 +01:00
{
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.lock();
2017-01-03 21:19:33 +01:00
2017-01-18 04:33:30 +01:00
if (IrcManager::connection != NULL) {
delete IrcManager::connection;
IrcManager::connection = NULL;
2017-01-03 21:19:33 +01:00
}
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.unlock();
2017-01-17 00:15:44 +01:00
}
2017-01-29 13:23:22 +01:00
void
IrcManager::send(QString raw)
{
IrcManager::connectionMutex.lock();
IrcManager::connection->sendRaw(raw);
IrcManager::connectionMutex.unlock();
}
2017-01-17 00:15:44 +01:00
void
IrcManager::joinChannel(const QString &channel)
{
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.lock();
if (IrcManager::connection != NULL) {
IrcManager::connection->sendRaw("JOIN #" + channel);
2017-01-17 00:15:44 +01:00
}
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.unlock();
2017-01-17 00:15:44 +01:00
}
void
IrcManager::partChannel(const QString &channel)
{
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.lock();
if (IrcManager::connection != NULL) {
IrcManager::connection->sendRaw("PART #" + channel);
2017-01-17 00:15:44 +01:00
}
2017-01-18 04:33:30 +01:00
IrcManager::connectionMutex.unlock();
2017-01-03 21:19:33 +01:00
}
2017-01-11 18:52:09 +01:00
void
IrcManager::messageReceived(IrcMessage *message)
2017-01-03 21:19:33 +01:00
{
2017-01-18 01:04:54 +01:00
// qInfo(message->command().toStdString().c_str());
2017-01-04 15:12:31 +01:00
2017-01-18 01:04:54 +01:00
const QString &command = message->command();
// if (command == "CLEARCHAT") {
// message->
// } 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-01-11 18:52:09 +01:00
void
IrcManager::privateMessageReceived(IrcPrivateMessage *message)
2017-01-03 21:19:33 +01:00
{
2017-01-17 00:15:44 +01:00
// qInfo(message->content().toStdString().c_str());
2017-01-05 16:07:20 +01:00
auto c = Channels::getChannel(message->target().mid(1));
2017-01-05 16:07:20 +01:00
if (c != NULL) {
2017-01-18 21:52:36 +01:00
c->addMessage(std::shared_ptr<messages::Message>(
new messages::Message(*message, *c)));
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-01-11 18:52:09 +01:00
bool
IrcManager::isTwitchBlockedUser(QString const &username)
2017-01-04 15:12:31 +01:00
{
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.lock();
2017-01-04 15:12:31 +01:00
2017-01-18 04:33:30 +01:00
auto iterator = IrcManager::twitchBlockedUsers.find(username);
2017-01-04 15:12:31 +01:00
2017-01-18 04:33:30 +01:00
if (iterator == IrcManager::twitchBlockedUsers.end()) {
IrcManager::twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
return false;
}
2017-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
return true;
}
2017-01-11 18:52:09 +01:00
bool
IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
2017-01-04 15:12:31 +01:00
{
2017-01-18 04:33:30 +01: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-01-18 04:33:30 +01:00
auto reply = IrcManager::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-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.lock();
IrcManager::twitchBlockedUsers.insert(username, true);
IrcManager::twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
delete reply;
return true;
}
errorMessage = "Error while ignoring user \"" + username +
"\": " + reply->errorString();
2017-01-04 15:12:31 +01:00
return false;
}
2017-01-11 18:52:09 +01: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-01-11 18:52:09 +01:00
bool
IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
2017-01-04 15:12:31 +01:00
{
2017-01-18 04:33:30 +01: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-01-18 04:33:30 +01:00
auto reply = IrcManager::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-01-18 04:33:30 +01:00
IrcManager::twitchBlockedUsersMutex.lock();
IrcManager::twitchBlockedUsers.remove(username);
IrcManager::twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
delete reply;
return true;
}
errorMessage = "Error while unignoring user \"" + username +
"\": " + reply->errorString();
2017-01-04 15:12:31 +01:00
return false;
}
2017-01-11 18:52:09 +01: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-01-18 21:30:23 +01:00
}