refactored irc

This commit is contained in:
fourtf
2018-02-05 15:11:50 +01:00
parent 12b30eb2ed
commit b351c40d29
56 changed files with 1397 additions and 1154 deletions
+37
View File
@@ -0,0 +1,37 @@
#include "_ircaccount.hpp"
// namespace chatterino {
// namespace providers {
// namespace irc {
// IrcAccount::IrcAccount(const QString &_userName, const QString &_nickName, const QString
// &_realName,
// const QString &_password)
// : userName(_userName)
// , nickName(_nickName)
// , realName(_realName)
// , password(_password)
//{
//}
// const QString &IrcAccount::getUserName() const
//{
// return this->userName;
//}
// const QString &IrcAccount::getNickName() const
//{
// return this->nickName;
//}
// const QString &IrcAccount::getRealName() const
//{
// return this->realName;
//}
// const QString &IrcAccount::getPassword() const
//{
// return this->password;
//}
//} // namespace irc
//} // namespace providers
//} // namespace chatterino
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include <QString>
// namespace chatterino {
// namespace providers {
// namespace irc {
// class IrcAccount
//{
// public:
// IrcAccount(const QString &userName, const QString &nickName, const QString &realName,
// const QString &password);
// const QString &getUserName() const;
// const QString &getNickName() const;
// const QString &getRealName() const;
// const QString &getPassword() const;
// private:
// QString userName;
// QString nickName;
// QString realName;
// QString password;
//};
//} // namespace irc
//} // namespace providers
//} // namespace chatterino
+11
View File
@@ -0,0 +1,11 @@
#include "_ircchannel.hpp"
namespace chatterino {
namespace providers {
namespace irc {
// IrcChannel::IrcChannel()
//{
//}
} // namespace irc
} // namespace providers
} // namespace chatterino
+13
View File
@@ -0,0 +1,13 @@
#pragma once
namespace chatterino {
namespace providers {
namespace irc {
// class IrcChannel
//{
// public:
// IrcChannel();
//};
} // namespace irc
} // namespace providers
} // namespace chatterino
+14
View File
@@ -0,0 +1,14 @@
#include "_ircserver.hpp"
#include <cassert>
namespace chatterino {
namespace providers {
namespace irc {
// IrcServer::IrcServer(const QString &hostname, int port)
//{
// this->initConnection();
//}
} // namespace irc
} // namespace providers
} // namespace chatterino
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include "providers/irc/_ircaccount.hpp"
#include "providers/irc/abstractircserver.hpp"
namespace chatterino {
namespace providers {
namespace irc {
// class IrcServer
//{
// public:
// IrcServer(const QString &hostname, int port);
// void setAccount(std::shared_ptr<IrcAccount> newAccount);
// std::shared_ptr<IrcAccount> getAccount() const;
// protected:
// virtual void initializeConnection(Communi::IrcConnection *connection, bool isReadConnection);
// virtual void privateMessageReceived(Communi::IrcPrivateMessage *message);
// virtual void messageReceived(Communi::IrcMessage *message);
//};
} // namespace irc
} // namespace providers
} // namespace chatterino
+226
View File
@@ -0,0 +1,226 @@
#include "abstractircserver.hpp"
#include "common.hpp"
#include "messages/limitedqueuesnapshot.hpp"
#include "messages/message.hpp"
using namespace chatterino::messages;
namespace chatterino {
namespace providers {
namespace irc {
AbstractIrcServer::AbstractIrcServer()
{
// Initialize the connections
this->writeConnection.reset(new Communi::IrcConnection);
this->writeConnection->moveToThread(QCoreApplication::instance()->thread());
QObject::connect(this->writeConnection.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->writeConnectionMessageReceived(msg); });
// Listen to read connection message signals
this->readConnection.reset(new Communi::IrcConnection);
this->readConnection->moveToThread(QCoreApplication::instance()->thread());
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->messageReceived(msg); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::privateMessageReceived,
[this](auto msg) { this->privateMessageReceived(msg); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::connected,
[this] { this->onConnected(); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected,
[this] { this->onDisconnected(); });
}
Communi::IrcConnection *AbstractIrcServer::getReadConnection() const
{
return this->readConnection.get();
}
void AbstractIrcServer::connect()
{
this->disconnect();
// if (this->hasSeperateWriteConnection()) {
this->initializeConnection(this->writeConnection.get(), false, true);
this->initializeConnection(this->readConnection.get(), true, false);
// } else {
// this->initializeConnection(this->readConnection.get(), true, true);
// }
// fourtf: this should be asynchronous
{
std::lock_guard<std::mutex> lock1(this->connectionMutex);
std::lock_guard<std::mutex> lock2(this->channelMutex);
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
if (!chan) {
continue;
}
this->writeConnection->sendRaw("JOIN #" + chan->name);
this->readConnection->sendRaw("JOIN #" + chan->name);
}
this->writeConnection->open();
this->readConnection->open();
}
this->onConnected();
this->connected.invoke();
}
void AbstractIrcServer::disconnect()
{
std::lock_guard<std::mutex> locker(this->connectionMutex);
this->readConnection->close();
this->writeConnection->close();
}
void AbstractIrcServer::sendMessage(const QString &channelName, const QString &message)
{
std::lock_guard<std::mutex> locker(this->connectionMutex);
// fourtf: trim the message if it's sent from twitch chat
if (this->writeConnection) {
this->writeConnection->sendRaw("PRIVMSG #" + channelName + " :" + message);
}
}
void AbstractIrcServer::writeConnectionMessageReceived(Communi::IrcMessage *message)
{
}
std::shared_ptr<Channel> AbstractIrcServer::addChannel(const QString &channelName)
{
std::lock_guard<std::mutex> lock(this->channelMutex);
// value exists
auto it = this->channels.find(channelName);
if (it != this->channels.end()) {
std::shared_ptr<Channel> chan = it.value().lock();
if (chan) {
return chan;
}
}
// value doesn't exist
std::shared_ptr<Channel> chan = this->createChannel(channelName);
if (!chan) {
return Channel::getEmpty();
}
QString clojuresInCppAreShit = channelName;
this->channels.insert(channelName, chan);
chan->destroyed.connect([this, clojuresInCppAreShit] {
// fourtf: issues when the server itself in destroyed
debug::Log("[AbstractIrcServer::addChannel] {} was destroyed", clojuresInCppAreShit);
this->channels.remove(clojuresInCppAreShit);
});
// join irc channel
{
std::lock_guard<std::mutex> lock2(this->connectionMutex);
if (this->readConnection) {
this->readConnection->sendRaw("JOIN #" + channelName);
}
if (this->writeConnection) {
this->writeConnection->sendRaw("JOIN #" + channelName);
}
}
return chan;
}
std::shared_ptr<Channel> AbstractIrcServer::getChannel(const QString &channelName)
{
std::lock_guard<std::mutex> lock(this->channelMutex);
// value exists
auto it = this->channels.find(channelName);
if (it != this->channels.end()) {
std::shared_ptr<Channel> chan = it.value().lock();
if (chan) {
return chan;
}
}
return Channel::getEmpty();
}
void AbstractIrcServer::onConnected()
{
std::lock_guard<std::mutex> lock(this->channelMutex);
MessagePtr connMsg = Message::createSystemMessage("connected to chat");
MessagePtr reconnMsg = Message::createSystemMessage("reconnected to chat");
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
if (!chan) {
continue;
}
LimitedQueueSnapshot<MessagePtr> snapshot = chan->getMessageSnapshot();
bool replaceMessage =
snapshot.getLength() > 0 &&
snapshot[snapshot.getLength() - 1]->flags & Message::DisconnectedMessage;
if (replaceMessage) {
chan->replaceMessage(snapshot[snapshot.getLength() - 1], reconnMsg);
continue;
}
chan->addMessage(connMsg);
}
}
void AbstractIrcServer::onDisconnected()
{
std::lock_guard<std::mutex> lock(this->channelMutex);
MessagePtr msg = Message::createSystemMessage("disconnected from chat");
msg->flags &= Message::DisconnectedMessage;
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
if (!chan) {
continue;
}
chan->addMessage(msg);
}
}
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(const QString &channelName)
{
return nullptr;
}
void AbstractIrcServer::addFakeMessage(const QString &data)
{
auto fakeMessage = Communi::IrcMessage::fromData(data.toUtf8(), this->readConnection.get());
this->privateMessageReceived(qobject_cast<Communi::IrcPrivateMessage *>(fakeMessage));
}
void AbstractIrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
{
}
void AbstractIrcServer::messageReceived(Communi::IrcMessage *message)
{
}
} // namespace irc
} // namespace providers
} // namespace chatterino
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include <IrcMessage>
#include <mutex>
#include <pajlada/signals/signal.hpp>
#include "channel.hpp"
namespace chatterino {
namespace providers {
namespace irc {
class AbstractIrcServer
{
public:
// connection
Communi::IrcConnection *getReadConnection() const;
void connect();
void disconnect();
void sendMessage(const QString &channelName, const QString &message);
// channels
std::shared_ptr<Channel> addChannel(const QString &channelName);
std::shared_ptr<Channel> getChannel(const QString &channelName);
// signals
pajlada::Signals::NoArgSignal connected;
pajlada::Signals::NoArgSignal disconnected;
pajlada::Signals::Signal<Communi::IrcPrivateMessage *> onPrivateMessage;
void addFakeMessage(const QString &data);
protected:
AbstractIrcServer();
virtual void initializeConnection(Communi::IrcConnection *connection, bool isRead,
bool isWrite) = 0;
virtual std::shared_ptr<Channel> createChannel(const QString &channelName) = 0;
virtual void privateMessageReceived(Communi::IrcPrivateMessage *message);
virtual void messageReceived(Communi::IrcMessage *message);
virtual void writeConnectionMessageReceived(Communi::IrcMessage *message);
virtual void onConnected();
virtual void onDisconnected();
virtual std::shared_ptr<Channel> getCustomChannel(const QString &channelName);
private:
void initConnection();
QMap<QString, std::weak_ptr<Channel>> channels;
std::unique_ptr<Communi::IrcConnection> writeConnection = nullptr;
std::unique_ptr<Communi::IrcConnection> readConnection = nullptr;
std::mutex connectionMutex;
std::mutex channelMutex;
};
} // namespace irc
} // namespace providers
} // namespace chatterino