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"
|
2017-01-16 03:15:07 +01:00
|
|
|
#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-15 17:34:48 +01:00
|
|
|
Account *IrcManager::account = nullptr;
|
2017-01-17 00:15:44 +01:00
|
|
|
IrcConnection *IrcManager::m_connection = NULL;
|
|
|
|
QMutex IrcManager::m_connectionMutex;
|
|
|
|
long IrcManager::m_connectionIteration = 0;
|
2017-01-11 18:52:09 +01:00
|
|
|
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-17 00:15:44 +01:00
|
|
|
QMap<QString, bool> IrcManager::m_twitchBlockedUsers;
|
|
|
|
QMutex IrcManager::m_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-15 17:34:48 +01:00
|
|
|
IrcManager::account = const_cast<Account *>(Account::anon());
|
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
int iteration = ++m_connectionIteration;
|
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
|
|
|
|
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-17 00:15:44 +01:00
|
|
|
m_twitchBlockedUsersMutex.lock();
|
|
|
|
m_twitchBlockedUsers.clear();
|
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_twitchBlockedUsers.insert(
|
2017-01-11 18:52:09 +01:00
|
|
|
user.value("name").toString().toLower(), true);
|
|
|
|
}
|
2017-01-17 00:15:44 +01:00
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_twitchBlockedUsers.insert(
|
2017-01-11 18:52:09 +01:00
|
|
|
user.value("name").toString().toLower(), true);
|
|
|
|
}
|
2017-01-17 00:15:44 +01:00
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_connectionMutex.lock();
|
|
|
|
if (iteration == m_connectionIteration) {
|
|
|
|
delete m_connection;
|
2017-01-03 22:08:20 +01:00
|
|
|
c->moveToThread(QCoreApplication::instance()->thread());
|
2017-01-17 00:15:44 +01:00
|
|
|
m_connection = c;
|
2017-01-11 18:52:09 +01:00
|
|
|
} else {
|
2017-01-03 21:19:33 +01:00
|
|
|
delete c;
|
|
|
|
}
|
2017-01-17 00:15:44 +01:00
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_connectionMutex.lock();
|
2017-01-03 21:19:33 +01:00
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
if (m_connection != NULL) {
|
|
|
|
delete m_connection;
|
|
|
|
m_connection = NULL;
|
2017-01-03 21:19:33 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
m_connectionMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IrcManager::joinChannel(const QString &channel)
|
|
|
|
{
|
|
|
|
m_connectionMutex.lock();
|
|
|
|
if (m_connection != NULL) {
|
|
|
|
m_connection->sendRaw("JOIN #" + channel);
|
|
|
|
}
|
|
|
|
m_connectionMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IrcManager::partChannel(const QString &channel)
|
|
|
|
{
|
|
|
|
m_connectionMutex.lock();
|
|
|
|
if (m_connection != NULL) {
|
|
|
|
m_connection->sendRaw("PART #" + channel);
|
|
|
|
}
|
|
|
|
m_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
|
|
|
|
2017-01-16 03:15:07 +01:00
|
|
|
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-17 00:15:44 +01:00
|
|
|
m_twitchBlockedUsersMutex.lock();
|
2017-01-04 15:12:31 +01:00
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
auto iterator = m_twitchBlockedUsers.find(username);
|
2017-01-04 15:12:31 +01:00
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
if (iterator == m_twitchBlockedUsers.end()) {
|
|
|
|
m_twitchBlockedUsersMutex.unlock();
|
2017-01-04 15:12:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-17 00:15:44 +01:00
|
|
|
m_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-17 00:15:44 +01:00
|
|
|
m_twitchBlockedUsersMutex.lock();
|
|
|
|
m_twitchBlockedUsers.insert(username, true);
|
|
|
|
m_twitchBlockedUsersMutex.unlock();
|
2017-01-04 15:12:31 +01:00
|
|
|
|
|
|
|
delete reply;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-16 03:15:07 +01:00
|
|
|
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)) {
|
2017-01-15 17:21:28 +01:00
|
|
|
// 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-17 00:15:44 +01:00
|
|
|
m_twitchBlockedUsersMutex.lock();
|
|
|
|
m_twitchBlockedUsers.remove(username);
|
|
|
|
m_twitchBlockedUsersMutex.unlock();
|
2017-01-04 15:12:31 +01:00
|
|
|
|
|
|
|
delete reply;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-16 03:15:07 +01:00
|
|
|
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)) {
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: Implement IrcManager::removeIgnoredUser
|
2017-01-04 15:12:31 +01:00
|
|
|
}
|
|
|
|
}
|