2018-02-05 15:11:50 +01:00
|
|
|
#include "abstractircserver.hpp"
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "messages/limitedqueuesnapshot.hpp"
|
|
|
|
#include "messages/message.hpp"
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
using namespace chatterino::messages;
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace providers {
|
|
|
|
namespace irc {
|
|
|
|
|
|
|
|
AbstractIrcServer::AbstractIrcServer()
|
|
|
|
{
|
|
|
|
// Initialize the connections
|
2018-06-04 21:05:18 +02:00
|
|
|
this->writeConnection.reset(new IrcConnection);
|
2018-02-05 15:11:50 +01:00
|
|
|
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
|
2018-06-04 21:05:18 +02:00
|
|
|
this->readConnection.reset(new IrcConnection);
|
2018-02-05 15:11:50 +01:00
|
|
|
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(); });
|
2018-06-04 21:37:19 +02:00
|
|
|
|
|
|
|
// listen to reconnect request
|
|
|
|
this->readConnection->reconnectRequested.connect([this] { this->connect(); });
|
|
|
|
// this->writeConnection->reconnectRequested.connect([this] { this->connect(); });
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:05:18 +02:00
|
|
|
IrcConnection *AbstractIrcServer::getReadConnection() const
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
|
|
|
return this->readConnection.get();
|
|
|
|
}
|
|
|
|
|
2018-06-05 18:51:14 +02:00
|
|
|
IrcConnection *AbstractIrcServer::getWriteConnection() const
|
|
|
|
{
|
|
|
|
return this->writeConnection.get();
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-07 00:47:35 +02:00
|
|
|
// this->onConnected();
|
|
|
|
// possbile event: started to connect
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-21 00:40:17 +02:00
|
|
|
std::shared_ptr<Channel> AbstractIrcServer::getOrAddChannel(const QString &dirtyChannelName)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-04-21 00:40:17 +02:00
|
|
|
auto channelName = this->cleanChannelName(dirtyChannelName);
|
2018-04-01 15:10:15 +02:00
|
|
|
|
2018-02-05 21:20:38 +01:00
|
|
|
// try get channel
|
2018-04-21 00:40:17 +02:00
|
|
|
ChannelPtr chan = this->getChannelOrEmpty(channelName);
|
2018-02-05 21:20:38 +01:00
|
|
|
if (chan != Channel::getEmpty()) {
|
|
|
|
return chan;
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 21:20:38 +01:00
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
// value doesn't exist
|
2018-02-05 21:20:38 +01:00
|
|
|
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] {
|
2018-02-05 21:20:38 +01:00
|
|
|
// fourtf: issues when the server itself is destroyed
|
2018-02-05 15:11:50 +01:00
|
|
|
|
|
|
|
debug::Log("[AbstractIrcServer::addChannel] {} was destroyed", clojuresInCppAreShit);
|
|
|
|
this->channels.remove(clojuresInCppAreShit);
|
2018-04-21 00:40:17 +02:00
|
|
|
|
|
|
|
if (this->readConnection) {
|
|
|
|
this->readConnection->sendRaw("PART #" + clojuresInCppAreShit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->writeConnection) {
|
|
|
|
this->writeConnection->sendRaw("PART #" + clojuresInCppAreShit);
|
|
|
|
}
|
2018-02-05 15:11:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// join irc channel
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock2(this->connectionMutex);
|
2018-02-05 21:20:38 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
if (this->readConnection) {
|
|
|
|
this->readConnection->sendRaw("JOIN #" + channelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->writeConnection) {
|
|
|
|
this->writeConnection->sendRaw("JOIN #" + channelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2018-04-21 00:40:17 +02:00
|
|
|
std::shared_ptr<Channel> AbstractIrcServer::getChannelOrEmpty(const QString &dirtyChannelName)
|
2018-02-05 15:11:50 +01:00
|
|
|
{
|
2018-04-21 00:40:17 +02:00
|
|
|
auto channelName = this->cleanChannelName(dirtyChannelName);
|
2018-04-01 15:10:15 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
std::lock_guard<std::mutex> lock(this->channelMutex);
|
|
|
|
|
2018-02-05 21:20:38 +01:00
|
|
|
// 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()) {
|
2018-02-05 21:20:38 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
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");
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(const QString &channelName)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-21 00:40:17 +02:00
|
|
|
QString AbstractIrcServer::cleanChannelName(const QString &dirtyChannelName)
|
2018-04-01 15:10:15 +02:00
|
|
|
{
|
|
|
|
return dirtyChannelName;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
void AbstractIrcServer::addFakeMessage(const QString &data)
|
|
|
|
{
|
|
|
|
auto fakeMessage = Communi::IrcMessage::fromData(data.toUtf8(), this->readConnection.get());
|
|
|
|
|
2018-06-05 14:08:55 +02:00
|
|
|
this->messageReceived(fakeMessage);
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractIrcServer::messageReceived(Communi::IrcMessage *message)
|
|
|
|
{
|
|
|
|
}
|
2018-02-05 21:20:38 +01:00
|
|
|
|
|
|
|
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 irc
|
|
|
|
} // namespace providers
|
|
|
|
} // namespace chatterino
|