2019-09-08 18:06:43 +02:00
|
|
|
#include "Irc2.hpp"
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
#include <pajlada/serialize.hpp>
|
|
|
|
#include "common/Credentials.hpp"
|
2019-09-08 18:06:43 +02:00
|
|
|
#include "common/SignalVectorModel.hpp"
|
2019-09-10 23:55:43 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
#include "util/CombinePath.hpp"
|
2019-09-09 22:26:14 +02:00
|
|
|
#include "util/RapidjsonHelpers.hpp"
|
2019-09-08 18:06:43 +02:00
|
|
|
#include "util/StandardItemHelper.hpp"
|
|
|
|
|
2019-09-10 23:55:43 +02:00
|
|
|
#include <QSaveFile>
|
2019-09-14 18:38:09 +02:00
|
|
|
#include <QtConcurrent>
|
2019-09-10 23:55:43 +02:00
|
|
|
|
2019-09-08 18:06:43 +02:00
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
namespace {
|
2019-09-10 23:55:43 +02:00
|
|
|
QString configPath()
|
|
|
|
{
|
|
|
|
return combinePath(getPaths()->settingsDirectory, "irc.json");
|
|
|
|
}
|
|
|
|
|
2019-09-11 13:51:48 +02:00
|
|
|
class Model : public SignalVectorModel<IrcServerData>
|
2019-09-08 18:06:43 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Model(QObject *parent)
|
2019-09-14 18:38:09 +02:00
|
|
|
: SignalVectorModel<IrcServerData>(6, parent)
|
2019-09-08 18:06:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn a vector item into a model row
|
2019-09-11 13:51:48 +02:00
|
|
|
IrcServerData getItemFromRow(std::vector<QStandardItem *> &row,
|
|
|
|
const IrcServerData &original)
|
2019-09-08 18:06:43 +02:00
|
|
|
{
|
2019-09-11 13:51:48 +02:00
|
|
|
return IrcServerData{
|
2019-09-10 14:46:43 +02:00
|
|
|
row[0]->data(Qt::EditRole).toString(), // host
|
|
|
|
row[1]->data(Qt::EditRole).toInt(), // port
|
|
|
|
row[2]->data(Qt::CheckStateRole).toBool(), // ssl
|
|
|
|
row[3]->data(Qt::EditRole).toString(), // user
|
|
|
|
row[4]->data(Qt::EditRole).toString(), // nick
|
|
|
|
row[5]->data(Qt::EditRole).toString(), // real
|
2019-09-15 11:35:17 +02:00
|
|
|
original.authType, // authType
|
2019-09-14 18:38:09 +02:00
|
|
|
original.connectCommands, // connectCommands
|
2019-09-10 14:46:43 +02:00
|
|
|
original.id, // id
|
2019-09-08 18:06:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// turns a row in the model into a vector item
|
2019-09-11 13:51:48 +02:00
|
|
|
void getRowFromItem(const IrcServerData &item,
|
2019-09-08 18:06:43 +02:00
|
|
|
std::vector<QStandardItem *> &row)
|
|
|
|
{
|
2019-09-10 23:55:43 +02:00
|
|
|
setStringItem(row[0], item.host, false);
|
2019-09-08 18:06:43 +02:00
|
|
|
setStringItem(row[1], QString::number(item.port));
|
|
|
|
setBoolItem(row[2], item.ssl);
|
|
|
|
setStringItem(row[3], item.user);
|
|
|
|
setStringItem(row[4], item.nick);
|
2019-09-10 14:46:43 +02:00
|
|
|
setStringItem(row[5], item.real);
|
2019-09-08 18:06:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-14 18:38:09 +02:00
|
|
|
inline QString escape(QString str)
|
|
|
|
{
|
|
|
|
return str.replace(":", "::");
|
|
|
|
}
|
2019-09-09 22:26:14 +02:00
|
|
|
|
2019-09-14 18:38:09 +02:00
|
|
|
// This returns a unique id for every server which is understandeable in the systems credential manager.
|
|
|
|
inline QString getCredentialName(const IrcServerData &data)
|
|
|
|
{
|
|
|
|
return escape(QString::number(data.id)) + ":" + escape(data.user) + "@" +
|
|
|
|
escape(data.host);
|
|
|
|
}
|
2019-09-09 22:26:14 +02:00
|
|
|
|
2019-09-14 20:45:01 +02:00
|
|
|
void IrcServerData::getPassword(
|
|
|
|
QObject *receiver, std::function<void(const QString &)> &&onLoaded) const
|
|
|
|
{
|
2019-10-07 22:42:34 +02:00
|
|
|
Credentials::instance().get("irc", getCredentialName(*this), receiver,
|
2019-09-14 20:45:01 +02:00
|
|
|
std::move(onLoaded));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IrcServerData::setPassword(const QString &password)
|
|
|
|
{
|
2019-10-07 22:42:34 +02:00
|
|
|
Credentials::instance().set("irc", getCredentialName(*this), password);
|
2019-09-14 20:45:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 18:06:43 +02:00
|
|
|
Irc::Irc()
|
|
|
|
{
|
2019-09-09 22:26:14 +02:00
|
|
|
this->connections.itemInserted.connect([this](auto &&args) {
|
|
|
|
// make sure only one id can only exist for one server
|
|
|
|
assert(this->servers_.find(args.item.id) == this->servers_.end());
|
|
|
|
|
2019-09-14 18:38:09 +02:00
|
|
|
// add new server
|
2019-09-09 22:26:14 +02:00
|
|
|
if (auto ab = this->abandonedChannels_.find(args.item.id);
|
|
|
|
ab != this->abandonedChannels_.end())
|
|
|
|
{
|
2019-09-10 14:46:43 +02:00
|
|
|
auto server = std::make_unique<IrcServer>(args.item, ab->second);
|
|
|
|
|
|
|
|
// set server of abandoned channels
|
|
|
|
for (auto weak : ab->second)
|
|
|
|
if (auto shared = weak.lock())
|
|
|
|
if (auto ircChannel =
|
|
|
|
dynamic_cast<IrcChannel *>(shared.get()))
|
|
|
|
ircChannel->setServer(server.get());
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
// add new server with abandoned channels
|
2019-09-10 14:46:43 +02:00
|
|
|
this->servers_.emplace(args.item.id, std::move(server));
|
2019-09-09 22:26:14 +02:00
|
|
|
this->abandonedChannels_.erase(ab);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// add new server
|
|
|
|
this->servers_.emplace(args.item.id,
|
|
|
|
std::make_unique<IrcServer>(args.item));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this->connections.itemRemoved.connect([this](auto &&args) {
|
|
|
|
// restore
|
|
|
|
if (auto server = this->servers_.find(args.item.id);
|
|
|
|
server != this->servers_.end())
|
|
|
|
{
|
2019-09-10 14:46:43 +02:00
|
|
|
auto abandoned = server->second->getChannels();
|
|
|
|
|
|
|
|
// set server of abandoned servers to nullptr
|
|
|
|
for (auto weak : abandoned)
|
|
|
|
if (auto shared = weak.lock())
|
|
|
|
if (auto ircChannel =
|
|
|
|
dynamic_cast<IrcChannel *>(shared.get()))
|
|
|
|
ircChannel->setServer(nullptr);
|
|
|
|
|
|
|
|
this->abandonedChannels_[args.item.id] = abandoned;
|
2019-09-09 22:26:14 +02:00
|
|
|
this->servers_.erase(server);
|
2019-09-14 22:58:53 +02:00
|
|
|
}
|
2019-09-14 18:38:09 +02:00
|
|
|
|
2019-09-14 22:58:53 +02:00
|
|
|
if (args.caller != Irc::noEraseCredentialCaller)
|
|
|
|
{
|
2019-10-07 22:42:34 +02:00
|
|
|
Credentials::instance().erase("irc",
|
2019-09-14 18:38:09 +02:00
|
|
|
getCredentialName(args.item));
|
2019-09-09 22:26:14 +02:00
|
|
|
}
|
|
|
|
});
|
2019-09-08 18:06:43 +02:00
|
|
|
|
2019-09-10 23:55:43 +02:00
|
|
|
this->connections.delayedItemsChanged.connect([this] { this->save(); });
|
2019-09-08 18:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractTableModel *Irc::newConnectionModel(QObject *parent)
|
|
|
|
{
|
|
|
|
auto model = new Model(parent);
|
|
|
|
model->init(&this->connections);
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
ChannelPtr Irc::getOrAddChannel(int id, QString name)
|
|
|
|
{
|
|
|
|
if (auto server = this->servers_.find(id); server != this->servers_.end())
|
|
|
|
{
|
|
|
|
return server->second->getOrAddChannel(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-14 18:38:09 +02:00
|
|
|
auto channel = std::make_shared<IrcChannel>(name, nullptr);
|
|
|
|
|
|
|
|
this->abandonedChannels_[id].push_back(channel);
|
|
|
|
|
|
|
|
return std::move(channel);
|
2019-09-09 22:26:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 22:42:34 +02:00
|
|
|
Irc &Irc::instance()
|
2019-09-08 18:06:43 +02:00
|
|
|
{
|
|
|
|
static Irc irc;
|
|
|
|
return irc;
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:55:43 +02:00
|
|
|
int Irc::uniqueId()
|
|
|
|
{
|
|
|
|
int i = this->currentId_ + 1;
|
|
|
|
auto it = this->servers_.find(i);
|
2019-09-11 00:10:49 +02:00
|
|
|
auto it2 = this->abandonedChannels_.find(i);
|
2019-09-10 23:55:43 +02:00
|
|
|
|
2019-09-11 00:10:49 +02:00
|
|
|
while (it != this->servers_.end() || it2 != this->abandonedChannels_.end())
|
2019-09-10 23:55:43 +02:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
it = this->servers_.find(i);
|
2019-09-11 00:10:49 +02:00
|
|
|
it2 = this->abandonedChannels_.find(i);
|
2019-09-10 23:55:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (this->currentId_ = i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Irc::save()
|
|
|
|
{
|
|
|
|
QJsonDocument doc;
|
|
|
|
QJsonObject root;
|
|
|
|
QJsonArray servers;
|
|
|
|
|
|
|
|
for (auto &&conn : this->connections)
|
|
|
|
{
|
|
|
|
QJsonObject obj;
|
|
|
|
obj.insert("host", conn.host);
|
|
|
|
obj.insert("port", conn.port);
|
|
|
|
obj.insert("ssl", conn.ssl);
|
|
|
|
obj.insert("username", conn.user);
|
|
|
|
obj.insert("nickname", conn.nick);
|
|
|
|
obj.insert("realname", conn.real);
|
2019-09-14 18:38:09 +02:00
|
|
|
obj.insert("connectCommands",
|
|
|
|
QJsonArray::fromStringList(conn.connectCommands));
|
2019-09-10 23:55:43 +02:00
|
|
|
obj.insert("id", conn.id);
|
2019-09-15 11:35:17 +02:00
|
|
|
obj.insert("authType", int(conn.authType));
|
|
|
|
|
2019-09-10 23:55:43 +02:00
|
|
|
servers.append(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
root.insert("servers", servers);
|
|
|
|
doc.setObject(root);
|
|
|
|
|
|
|
|
QSaveFile file(configPath());
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(doc.toJson());
|
|
|
|
file.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Irc::load()
|
|
|
|
{
|
|
|
|
if (this->loaded_)
|
|
|
|
return;
|
|
|
|
this->loaded_ = true;
|
|
|
|
|
|
|
|
QString config = configPath();
|
|
|
|
QFile file(configPath());
|
|
|
|
file.open(QIODevice::ReadOnly);
|
2019-09-14 18:38:09 +02:00
|
|
|
auto object = QJsonDocument::fromJson(file.readAll()).object();
|
2019-09-10 23:55:43 +02:00
|
|
|
|
2019-09-14 20:45:01 +02:00
|
|
|
std::unordered_set<int> ids;
|
|
|
|
|
2019-09-14 18:38:09 +02:00
|
|
|
// load servers
|
|
|
|
for (auto server : object.value("servers").toArray())
|
2019-09-10 23:55:43 +02:00
|
|
|
{
|
|
|
|
auto obj = server.toObject();
|
2019-09-14 18:38:09 +02:00
|
|
|
IrcServerData data;
|
|
|
|
data.host = obj.value("host").toString(data.host);
|
|
|
|
data.port = obj.value("port").toInt(data.port);
|
|
|
|
data.ssl = obj.value("ssl").toBool(data.ssl);
|
|
|
|
data.user = obj.value("username").toString(data.user);
|
|
|
|
data.nick = obj.value("nickname").toString(data.nick);
|
|
|
|
data.real = obj.value("realname").toString(data.real);
|
|
|
|
data.connectCommands =
|
|
|
|
obj.value("connectCommands").toVariant().toStringList();
|
|
|
|
data.id = obj.value("id").toInt(data.id);
|
2019-09-15 11:35:17 +02:00
|
|
|
data.authType =
|
|
|
|
IrcAuthType(obj.value("authType").toInt(int(data.authType)));
|
2019-09-10 23:55:43 +02:00
|
|
|
|
|
|
|
// duplicate id's are not allowed :(
|
2019-09-14 20:45:01 +02:00
|
|
|
if (ids.find(data.id) == ids.end())
|
2019-09-10 23:55:43 +02:00
|
|
|
{
|
2019-09-14 20:45:01 +02:00
|
|
|
ids.insert(data.id);
|
|
|
|
|
|
|
|
this->connections.appendItem(data);
|
2019-09-10 23:55:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 18:06:43 +02:00
|
|
|
} // namespace chatterino
|