mirror-chatterino2/src/providers/irc/AbstractIrcServer.cpp

281 lines
8.2 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "AbstractIrcServer.hpp"
2018-02-05 15:11:50 +01:00
2018-06-26 15:33:51 +02:00
#include "common/Common.hpp"
2018-06-26 14:09:39 +02:00
#include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Message.hpp"
2018-02-05 15:11:50 +01:00
2018-06-04 21:05:18 +02:00
#include <QCoreApplication>
2018-02-05 15:11:50 +01:00
namespace chatterino {
AbstractIrcServer::AbstractIrcServer()
{
// Initialize the connections
2018-06-22 23:24:45 +02:00
this->writeConnection_.reset(new IrcConnection);
2018-08-06 21:17:03 +02:00
this->writeConnection_->moveToThread(
QCoreApplication::instance()->thread());
2018-02-05 15:11:50 +01:00
2018-08-06 21:17:03 +02:00
QObject::connect(
this->writeConnection_.get(), &Communi::IrcConnection::messageReceived,
[this](auto msg) { this->writeConnectionMessageReceived(msg); });
2018-02-05 15:11:50 +01:00
// Listen to read connection message signals
2018-06-22 23:24:45 +02:00
this->readConnection_.reset(new IrcConnection);
this->readConnection_->moveToThread(QCoreApplication::instance()->thread());
2018-02-05 15:11:50 +01:00
2018-08-06 21:17:03 +02:00
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::messageReceived,
2018-02-05 15:11:50 +01:00
[this](auto msg) { this->messageReceived(msg); });
2018-08-06 21:17:03 +02:00
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::privateMessageReceived,
2018-02-05 15:11:50 +01:00
[this](auto msg) { this->privateMessageReceived(msg); });
2018-08-06 21:17:03 +02:00
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::connected,
2018-02-05 15:11:50 +01:00
[this] { this->onConnected(); });
2018-08-06 21:17:03 +02:00
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::disconnected,
2018-02-05 15:11:50 +01:00
[this] { this->onDisconnected(); });
// listen to reconnect request
2018-08-06 21:17:03 +02:00
this->readConnection_->reconnectRequested.connect(
[this] { this->connect(); });
// this->writeConnection->reconnectRequested.connect([this] {
// this->connect(); });
2018-02-05 15:11:50 +01:00
}
void AbstractIrcServer::connect()
{
this->disconnect();
bool separateWriteConnection = this->hasSeparateWriteConnection();
if (separateWriteConnection) {
this->initializeConnection(this->writeConnection_.get(), false, true);
this->initializeConnection(this->readConnection_.get(), true, false);
} else {
this->initializeConnection(this->readConnection_.get(), true, true);
}
2018-02-05 15:11:50 +01:00
// fourtf: this should be asynchronous
{
2018-06-22 23:24:45 +02:00
std::lock_guard<std::mutex> lock1(this->connectionMutex_);
2018-02-05 15:11:50 +01:00
std::lock_guard<std::mutex> lock2(this->channelMutex);
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
2018-08-02 14:23:27 +02:00
if (auto channel = std::shared_ptr<Channel>(weak.lock())) {
this->readConnection_->sendRaw("JOIN #" + channel->getName());
2018-02-05 15:11:50 +01:00
}
}
2018-06-22 23:24:45 +02:00
this->writeConnection_->open();
this->readConnection_->open();
2018-02-05 15:11:50 +01:00
}
// this->onConnected();
// possbile event: started to connect
2018-02-05 15:11:50 +01:00
}
void AbstractIrcServer::disconnect()
{
2018-06-22 23:24:45 +02:00
std::lock_guard<std::mutex> locker(this->connectionMutex_);
2018-02-05 15:11:50 +01:00
2018-06-22 23:24:45 +02:00
this->readConnection_->close();
this->writeConnection_->close();
2018-02-05 15:11:50 +01:00
}
2018-08-06 21:17:03 +02:00
void AbstractIrcServer::sendMessage(const QString &channelName,
const QString &message)
2018-02-05 15:11:50 +01:00
{
this->sendRawMessage("PRIVMSG #" + channelName + " :" + message);
}
2018-02-05 15:11:50 +01:00
void AbstractIrcServer::sendRawMessage(const QString &rawMessage)
{
std::lock_guard<std::mutex> locker(this->connectionMutex_);
2018-02-05 15:11:50 +01:00
if (this->hasSeparateWriteConnection()) {
this->writeConnection_->sendRaw(rawMessage);
} else {
this->readConnection_->sendRaw(rawMessage);
2018-02-05 15:11:50 +01:00
}
}
2018-08-06 21:17:03 +02:00
void AbstractIrcServer::writeConnectionMessageReceived(
Communi::IrcMessage *message)
2018-02-05 15:11:50 +01:00
{
}
2018-08-06 21:17:03 +02:00
std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel(
const QString &dirtyChannelName)
2018-02-05 15:11:50 +01:00
{
auto channelName = this->cleanChannelName(dirtyChannelName);
// try get channel
ChannelPtr chan = this->getChannelOrEmpty(channelName);
if (chan != Channel::getEmpty()) {
return chan;
2018-02-05 15:11:50 +01:00
}
std::lock_guard<std::mutex> lock(this->channelMutex);
2018-02-05 15:11:50 +01:00
// value doesn't exist
chan = this->createChannel(channelName);
2018-02-05 15:11:50 +01:00
if (!chan) {
return Channel::getEmpty();
}
QString clojuresInCppAreShit = channelName;
this->channels.insert(channelName, chan);
chan->destroyed.connect([this, clojuresInCppAreShit] {
// fourtf: issues when the server itself is destroyed
2018-02-05 15:11:50 +01:00
2018-08-06 21:17:03 +02:00
Log("[AbstractIrcServer::addChannel] {} was destroyed",
clojuresInCppAreShit);
2018-02-05 15:11:50 +01:00
this->channels.remove(clojuresInCppAreShit);
2018-06-22 23:24:45 +02:00
if (this->readConnection_) {
this->readConnection_->sendRaw("PART #" + clojuresInCppAreShit);
}
2018-06-22 23:24:45 +02:00
if (this->writeConnection_) {
this->writeConnection_->sendRaw("PART #" + clojuresInCppAreShit);
}
2018-02-05 15:11:50 +01:00
});
// join irc channel
{
2018-06-22 23:24:45 +02:00
std::lock_guard<std::mutex> lock2(this->connectionMutex_);
2018-06-22 23:24:45 +02:00
if (this->readConnection_) {
this->readConnection_->sendRaw("JOIN #" + channelName);
2018-02-05 15:11:50 +01:00
}
2018-06-22 23:24:45 +02:00
if (this->writeConnection_) {
this->writeConnection_->sendRaw("JOIN #" + channelName);
2018-02-05 15:11:50 +01:00
}
}
return chan;
}
2018-08-06 21:17:03 +02:00
std::shared_ptr<Channel> AbstractIrcServer::getChannelOrEmpty(
const QString &dirtyChannelName)
2018-02-05 15:11:50 +01:00
{
auto channelName = this->cleanChannelName(dirtyChannelName);
2018-02-05 15:11:50 +01:00
std::lock_guard<std::mutex> lock(this->channelMutex);
// try get special channel
ChannelPtr chan = this->getCustomChannel(channelName);
if (chan) {
return chan;
}
2018-02-05 15:11:50 +01:00
// value exists
auto it = this->channels.find(channelName);
if (it != this->channels.end()) {
chan = it.value().lock();
2018-02-05 15:11:50 +01:00
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();
2018-08-06 21:17:03 +02:00
bool replaceMessage = snapshot.getLength() > 0 &&
snapshot[snapshot.getLength() - 1]->flags &
Message::DisconnectedMessage;
2018-02-05 15:11:50 +01:00
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");
2018-04-18 09:12:29 +02:00
msg->flags |= Message::DisconnectedMessage;
2018-02-05 15:11:50 +01:00
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
if (!chan) {
continue;
}
chan->addMessage(msg);
}
}
2018-08-06 21:17:03 +02:00
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(
const QString &channelName)
2018-02-05 15:11:50 +01:00
{
return nullptr;
}
QString AbstractIrcServer::cleanChannelName(const QString &dirtyChannelName)
{
return dirtyChannelName;
}
2018-02-05 15:11:50 +01:00
void AbstractIrcServer::addFakeMessage(const QString &data)
{
2018-08-06 21:17:03 +02:00
auto fakeMessage = Communi::IrcMessage::fromData(
data.toUtf8(), this->readConnection_.get());
2018-02-05 15:11:50 +01:00
2018-06-24 16:24:40 +02:00
if (fakeMessage->command() == "PRIVMSG") {
2018-08-06 21:17:03 +02:00
this->privateMessageReceived(
static_cast<Communi::IrcPrivateMessage *>(fakeMessage));
2018-06-24 16:24:40 +02:00
} else {
this->messageReceived(fakeMessage);
}
2018-02-05 15:11:50 +01:00
}
2018-08-06 21:17:03 +02:00
void AbstractIrcServer::privateMessageReceived(
Communi::IrcPrivateMessage *message)
2018-02-05 15:11:50 +01:00
{
}
void AbstractIrcServer::messageReceived(Communi::IrcMessage *message)
{
}
void AbstractIrcServer::forEachChannel(std::function<void(ChannelPtr)> func)
{
std::lock_guard<std::mutex> lock(this->channelMutex);
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
if (!chan) {
continue;
}
func(chan);
}
}
2018-04-01 16:43:30 +02:00
2018-02-05 15:11:50 +01:00
} // namespace chatterino