Refactor IrcManager a bit

Create a new readConnection so we can read our own messages
This commit is contained in:
Rasmus Karlsson 2017-06-06 15:57:54 +02:00
parent ccf8e3bd83
commit 1a428a54d5
2 changed files with 125 additions and 108 deletions

View file

@ -29,12 +29,6 @@ const QString IrcManager::defaultClientId("7ue61iz46fz11y3cugd0l3tawb4taal");
IrcManager::IrcManager() IrcManager::IrcManager()
: _account(AccountManager::getInstance().getTwitchUser()) : _account(AccountManager::getInstance().getTwitchUser())
, _connection()
, _connectionMutex()
, _connectionGeneration(0)
, _twitchBlockedUsers()
, _twitchBlockedUsersMutex()
, _accessManager()
{ {
} }
@ -55,32 +49,48 @@ void IrcManager::connect()
async_exec([this] { beginConnecting(); }); async_exec([this] { beginConnecting(); });
} }
void IrcManager::beginConnecting() Communi::IrcConnection *IrcManager::createConnection(bool doRead)
{ {
int generation = ++IrcManager::_connectionGeneration; Communi::IrcConnection *connection = new Communi::IrcConnection;
Communi::IrcConnection *c = new Communi::IrcConnection; if (doRead) {
QObject::connect(connection, &Communi::IrcConnection::messageReceived, this,
QObject::connect(c, &Communi::IrcConnection::messageReceived, this,
&IrcManager::messageReceived); &IrcManager::messageReceived);
QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this, QObject::connect(connection, &Communi::IrcConnection::privateMessageReceived, this,
&IrcManager::privateMessageReceived); &IrcManager::privateMessageReceived);
}
QString username = _account.getUserName(); QString username = _account.getUserName();
QString oauthClient = _account.getOAuthClient(); QString oauthClient = _account.getOAuthClient();
QString oauthToken = _account.getOAuthToken(); QString oauthToken = _account.getOAuthToken();
c->setUserName(username); connection->setUserName(username);
c->setNickName(username); connection->setNickName(username);
c->setRealName(username); connection->setRealName(username);
if (!_account.isAnon()) { if (!_account.isAnon()) {
c->setPassword(oauthToken); connection->setPassword(oauthToken);
// fetch ignored users this->refreshIgnoredUsers(username, oauthClient, oauthToken);
{ }
QString nextLink = "https://api.twitch.tv/kraken/users/" + username +
"/blocks?limit=" + 100 + "&client_id=" + oauthClient; this->refreshTwitchEmotes(username, oauthClient, oauthToken);
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"));
connection->setHost("irc.chat.twitch.tv");
connection->setPort(6667);
return connection;
}
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;
QNetworkAccessManager *manager = new QNetworkAccessManager(); QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken)); QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
@ -110,14 +120,13 @@ void IrcManager::beginConnecting()
manager->deleteLater(); manager->deleteLater();
}); });
} }
}
// fetch available twitch emtoes void IrcManager::refreshTwitchEmotes(const QString &username, const QString &oauthClient,
{ const QString &oauthToken)
{
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username + QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username +
"/emotes?oauth_token=" + oauthToken + "/emotes?oauth_token=" + oauthToken + "&client_id=" + oauthClient));
"&client_id=" + oauthClient));
QNetworkReply *reply = _accessManager.get(req); QNetworkReply *reply = _accessManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [=] { QObject::connect(reply, &QNetworkReply::finished, [=] {
@ -138,27 +147,34 @@ void IrcManager::beginConnecting()
} }
_twitchBlockedUsersMutex.unlock(); _twitchBlockedUsersMutex.unlock();
}); });
} }
c->setHost("irc.chat.twitch.tv"); void IrcManager::beginConnecting()
c->setPort(6667); {
uint32_t generation = ++this->connectionGeneration;
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands")); Communi::IrcConnection *_writeConnection = this->createConnection(false);
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags")); Communi::IrcConnection *_readConnection = this->createConnection(true);
QMutexLocker locker(&_connectionMutex); QMutexLocker locker(&_connectionMutex);
if (generation == _connectionGeneration) { if (generation == this->connectionGeneration) {
c->moveToThread(QCoreApplication::instance()->thread()); this->writeConnection = std::shared_ptr<Communi::IrcConnection>(_writeConnection);
_connection = std::shared_ptr<Communi::IrcConnection>(c); this->readConnection = std::shared_ptr<Communi::IrcConnection>(_readConnection);
this->writeConnection->moveToThread(QCoreApplication::instance()->thread());
this->readConnection->moveToThread(QCoreApplication::instance()->thread());
for (auto &channel : ChannelManager::getInstance().getItems()) { for (auto &channel : ChannelManager::getInstance().getItems()) {
c->sendRaw("JOIN #" + channel->getName()); this->writeConnection->sendRaw("JOIN #" + channel->getName());
this->readConnection->sendRaw("JOIN #" + channel->getName());
} }
c->open(); this->writeConnection->open();
this->readConnection->open();
} else { } else {
delete c; delete _writeConnection;
delete _readConnection;
} }
} }
@ -166,19 +182,11 @@ void IrcManager::disconnect()
{ {
_connectionMutex.lock(); _connectionMutex.lock();
auto c = _connection; auto _readConnection = this->readConnection;
if (_connection.get() != NULL) { auto _writeConnection = this->writeConnection;
_connection = std::shared_ptr<Communi::IrcConnection>();
}
_connectionMutex.unlock(); this->readConnection.reset();
} this->writeConnection.reset();
void IrcManager::send(QString raw)
{
_connectionMutex.lock();
_connection->sendRaw(raw);
_connectionMutex.unlock(); _connectionMutex.unlock();
} }
@ -187,8 +195,9 @@ void IrcManager::sendJoin(const QString &channel)
{ {
_connectionMutex.lock(); _connectionMutex.lock();
if (_connection.get() != NULL) { if (this->readConnection && this->writeConnection) {
_connection->sendRaw("JOIN #" + channel); this->readConnection->sendRaw("JOIN #" + channel);
this->writeConnection->sendRaw("JOIN #" + channel);
} }
_connectionMutex.unlock(); _connectionMutex.unlock();
@ -198,11 +207,8 @@ void IrcManager::sendMessage(const QString &channelName, const QString &message)
{ {
_connectionMutex.lock(); _connectionMutex.lock();
if (_connection.get() != nullptr) { if (this->writeConnection) {
qDebug() << "IRC Manager send message " << message << " to channel " << channelName; this->writeConnection->sendRaw("PRIVMSG #" + channelName + " :" + message);
QString xd = "PRIVMSG #" + channelName + " :" + message;
qDebug() << xd;
_connection->sendRaw(xd);
} }
_connectionMutex.unlock(); _connectionMutex.unlock();
@ -212,19 +218,25 @@ void IrcManager::partChannel(const QString &channel)
{ {
_connectionMutex.lock(); _connectionMutex.lock();
if (_connection.get() != NULL) { if (this->readConnection && this->writeConnection) {
_connection.get()->sendRaw("PART #" + channel); this->readConnection->sendRaw("PART #" + channel);
this->writeConnection->sendRaw("PART #" + channel);
} }
_connectionMutex.unlock(); _connectionMutex.unlock();
} }
void IrcManager::messageReceived(Communi::IrcMessage * /*message*/) void IrcManager::messageReceived(Communi::IrcMessage *message)
{ {
qDebug() << "Message received: " << message->command();
// qInfo(message->command().toStdString().c_str()); // qInfo(message->command().toStdString().c_str());
//
for (const auto &param : message->parameters()) {
qDebug() << "Param: " << param;
}
/*
const QString &command = message->command(); const QString &command = message->command();
/*
if (command == "CLEARCHAT") { if (command == "CLEARCHAT") {
} else if (command == "ROOMSTATE") { } else if (command == "ROOMSTATE") {

View file

@ -1,5 +1,4 @@
#ifndef IRCMANAGER_H #pragma once
#define IRCMANAGER_H
#define TWITCH_MAX_MESSAGELENGTH 500 #define TWITCH_MAX_MESSAGELENGTH 500
@ -31,8 +30,6 @@ public:
void connect(); void connect();
void disconnect(); void disconnect();
void send(QString raw);
bool isTwitchBlockedUser(QString const &username); bool isTwitchBlockedUser(QString const &username);
bool tryAddIgnoredUser(QString const &username, QString &errorMessage); bool tryAddIgnoredUser(QString const &username, QString &errorMessage);
void addIgnoredUser(QString const &username); void addIgnoredUser(QString const &username);
@ -56,9 +53,11 @@ private:
// variables // variables
twitch::TwitchUser _account; twitch::TwitchUser _account;
std::shared_ptr<Communi::IrcConnection> _connection; std::shared_ptr<Communi::IrcConnection> writeConnection = nullptr;
std::shared_ptr<Communi::IrcConnection> readConnection = nullptr;
QMutex _connectionMutex; QMutex _connectionMutex;
long _connectionGeneration; uint32_t connectionGeneration = 0;
QMap<QString, bool> _twitchBlockedUsers; QMap<QString, bool> _twitchBlockedUsers;
QMutex _twitchBlockedUsersMutex; QMutex _twitchBlockedUsersMutex;
@ -66,11 +65,17 @@ private:
QNetworkAccessManager _accessManager; QNetworkAccessManager _accessManager;
// methods // methods
Communi::IrcConnection *createConnection(bool doRead);
void refreshIgnoredUsers(const QString &username, const QString &oauthClient,
const QString &oauthToken);
void refreshTwitchEmotes(const QString &username, const QString &oauthClient,
const QString &oauthToken);
void beginConnecting(); void beginConnecting();
void messageReceived(Communi::IrcMessage *message); void messageReceived(Communi::IrcMessage *message);
void privateMessageReceived(Communi::IrcPrivateMessage *message); void privateMessageReceived(Communi::IrcPrivateMessage *message);
}; };
} // namespace chatterino
#endif // IRCMANAGER_H } // namespace chatterino