mirror-chatterino2/ircmanager.cpp

261 lines
7.4 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
Account *IrcManager::account = nullptr;
2017-01-11 18:52:09 +01:00
IrcConnection *IrcManager::connection = NULL;
2017-01-11 19:33:46 +01:00
QMutex IrcManager::connectionMutex;
2017-01-11 18:52:09 +01:00
long IrcManager::connectionIteration = 0;
const QString IrcManager::defaultClientId = "7ue61iz46fz11y3cugd0l3tawb4taal";
2017-01-11 19:33:46 +01:00
QNetworkAccessManager IrcManager::m_accessManager;
2017-01-04 15:12:31 +01:00
2017-01-11 19:33:46 +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
{
IrcManager::account = const_cast<Account *>(Account::anon());
2017-01-03 21:19:33 +01:00
int iteration = ++connectionIteration;
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
QString username = account->username();
QString oauthClient = account->oauthClient();
QString oauthToken = account->oauthToken();
{
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-11 19:33:46 +01:00
QNetworkReply *reply = m_accessManager.get(req);
2017-01-11 18:52:09 +01:00
QObject::connect(reply, &QNetworkReply::finished, [=] {
2017-01-11 19:33:46 +01: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-01-11 19:33:46 +01:00
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-11 19:33:46 +01:00
twitchBlockedUsers.insert(
2017-01-11 18:52:09 +01:00
user.value("name").toString().toLower(), true);
}
2017-01-11 19:33:46 +01:00
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-11 19:33:46 +01:00
QNetworkReply *reply = m_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-11 19:33:46 +01:00
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-11 19:33:46 +01:00
twitchBlockedUsers.insert(
2017-01-11 18:52:09 +01:00
user.value("name").toString().toLower(), true);
}
2017-01-11 19:33:46 +01:00
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");
2017-01-03 22:08:20 +01:00
c->sendRaw("JOIN #fourtf");
2017-01-03 21:19:33 +01:00
c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/commands"));
c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/tags"));
c->open();
2017-01-11 19:33:46 +01:00
connectionMutex.lock();
2017-01-03 21:19:33 +01:00
if (iteration == connectionIteration) {
2017-01-03 22:08:20 +01:00
delete connection;
c->moveToThread(QCoreApplication::instance()->thread());
2017-01-03 21:19:33 +01:00
connection = c;
2017-01-11 18:52:09 +01:00
} else {
2017-01-03 21:19:33 +01:00
delete c;
}
2017-01-11 19:33:46 +01:00
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-11 19:33:46 +01:00
connectionMutex.lock();
2017-01-03 21:19:33 +01:00
if (connection != NULL) {
delete connection;
connection = NULL;
}
2017-01-11 19:33:46 +01:00
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-04 15:12:31 +01:00
qInfo(message->command().toStdString().c_str());
2017-01-05 16:07:20 +01:00
// if (message->command() == "")
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
{
qInfo(message->content().toStdString().c_str());
2017-01-05 16:07:20 +01:00
qInfo(message->target().toStdString().c_str());
auto c = Channels::getChannel(message->target().mid(1));
2017-01-05 16:07:20 +01:00
if (c != NULL) {
2017-01-11 01:08:20 +01:00
c->addMessage(std::shared_ptr<Message>(new 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-11 19:33:46 +01:00
twitchBlockedUsersMutex.lock();
2017-01-04 15:12:31 +01:00
2017-01-11 19:33:46 +01:00
auto iterator = twitchBlockedUsers.find(username);
2017-01-04 15:12:31 +01:00
2017-01-11 19:33:46 +01:00
if (iterator == twitchBlockedUsers.end()) {
twitchBlockedUsersMutex.unlock();
2017-01-04 15:12:31 +01:00
return false;
}
2017-01-11 19:33:46 +01:00
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
{
QUrl url("https://api.twitch.tv/kraken/users/" + account->username() +
2017-01-11 18:52:09 +01:00
"/blocks/" + username + "?oauth_token=" + account->oauthToken() +
2017-01-04 15:12:31 +01:00
"&client_id=" + account->oauthClient());
QNetworkRequest request(url);
2017-01-11 19:33:46 +01:00
auto reply = m_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-11 19:33:46 +01:00
twitchBlockedUsersMutex.lock();
twitchBlockedUsers.insert(username, true);
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
{
QUrl url("https://api.twitch.tv/kraken/users/" + account->username() +
2017-01-11 18:52:09 +01:00
"/blocks/" + username + "?oauth_token=" + account->oauthToken() +
2017-01-04 15:12:31 +01:00
"&client_id=" + account->oauthClient());
QNetworkRequest request(url);
2017-01-11 19:33:46 +01:00
auto reply = m_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-11 19:33:46 +01:00
twitchBlockedUsersMutex.lock();
twitchBlockedUsers.remove(username);
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
}
}