mirror of https://notabug.org/acetone/ircabot.git
167 lines
6.2 KiB
C++
167 lines
6.2 KiB
C++
/*
|
|
This file is part of IRCaBot.
|
|
IRCaBot is IRC logger with features.
|
|
Source code: https://notabug.org/acetone/ircabot.
|
|
Copyright (C) 2023, acetone.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "config.h"
|
|
#include "logger.h"
|
|
|
|
#include <QFile>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QJsonDocument>
|
|
#include <QJsonParseError>
|
|
|
|
constexpr const char CFG_LOG_LEVEL[] = "logLevel";
|
|
constexpr const char CFG_LOG_FILE[] = "logFile";
|
|
constexpr const char CFG_LOG_FILE_DATE_FORMAT[] = "logFileDateFormat";
|
|
constexpr const char CFG_WORKING_DIRECTORY[] = "workingDirectory";
|
|
|
|
constexpr const char CFG_WEBUI[] = "webUI";
|
|
constexpr const char CFG_WEBUI_ADDRESS[] = "address";
|
|
constexpr const char CFG_WEBUI_PORT[] = "port";
|
|
constexpr const char CFG_WEBUI_THREADS[] = "threads";
|
|
|
|
constexpr const char CFG_NETWORKS[] = "networks";
|
|
constexpr const char CFG_NETWORK_NETWORKNAME[] = "networkName";
|
|
constexpr const char CFG_NETWORK_NICKNAME[] = "nickname";
|
|
constexpr const char CFG_NETWORK_PASSWORD[] = "password";
|
|
constexpr const char CFG_NETWORK_IDENTIFY_FMT[] = "identifyFormat";
|
|
constexpr const char CFG_NETWORK_HOST[] = "host";
|
|
constexpr const char CFG_NETWORK_PORT[] = "port";
|
|
constexpr const char CFG_NETWORK_SSL[] = "ssl";
|
|
constexpr const char CFG_NETWORK_IDENT[] = "ident";
|
|
constexpr const char CFG_NETWORK_REAL_NAME[] = "realName";
|
|
constexpr const char CFG_NETWORK_AUTORECONNECT[] = "autoReconnect";
|
|
constexpr const char CFG_NETWORK_VOICEGATE[] = "voiceGate";
|
|
constexpr const char CFG_NETWORK_CHANNELS[] = "channels";
|
|
|
|
QString Config::m_configurationFilePath;
|
|
QString Config::m_logFilePath;
|
|
QString Config::m_logFileDateFormat = "yyyy-MM-dd hh:mm:ss";
|
|
|
|
QString Config::m_workingDirectory = "";
|
|
|
|
QString Config::m_webUiAddress;
|
|
quint16 Config::m_webUiPort;
|
|
uint Config::m_webUiThreads;
|
|
|
|
QList<Config::Network> Config::m_networks;
|
|
|
|
bool Config::initFromFile(QString *errorString)
|
|
{
|
|
QFile file( configurationFilePath() );
|
|
if (not file.open(QIODevice::ReadOnly))
|
|
{
|
|
if (errorString) *errorString = "Can't open file";
|
|
|
|
return false;
|
|
}
|
|
|
|
QJsonParseError jerr;
|
|
QJsonObject jcfg = QJsonDocument::fromJson(file.readAll(), &jerr).object();
|
|
file.close();
|
|
|
|
if (jerr.error != QJsonParseError::NoError)
|
|
{
|
|
if (errorString) *errorString = "Invalid JSON";
|
|
|
|
return false;
|
|
}
|
|
|
|
return initFromJson(jcfg, errorString);
|
|
}
|
|
|
|
bool Config::initFromJson(const QJsonObject &config, QString* errorString)
|
|
{
|
|
logger::level = logger::levelEnum(config.value(CFG_LOG_LEVEL).toString());
|
|
|
|
m_logFilePath = config.value(CFG_LOG_FILE).toString();
|
|
|
|
m_workingDirectory = config.value(CFG_WORKING_DIRECTORY).toString();
|
|
|
|
QString buffer = config.value(CFG_LOG_FILE_DATE_FORMAT).toString();
|
|
if (not buffer.isEmpty())
|
|
{
|
|
m_logFileDateFormat = buffer;
|
|
}
|
|
|
|
QJsonObject webUi = config.value(CFG_WEBUI).toObject();
|
|
m_webUiAddress = webUi.value(CFG_WEBUI_ADDRESS).toString("127.0.0.1");
|
|
m_webUiPort = webUi.value(CFG_WEBUI_PORT) .toInt(7666);
|
|
m_webUiThreads = webUi.value(CFG_WEBUI_THREADS).toInt(-1); // if <1, then system number
|
|
|
|
QJsonArray jnetworks = config.value(CFG_NETWORKS).toArray();
|
|
if (jnetworks.isEmpty())
|
|
{
|
|
if (errorString) *errorString = "Networks array is undefined";
|
|
|
|
return false;
|
|
}
|
|
|
|
uint unnamedNetworkCounter = 0;
|
|
for (const auto& jnet: jnetworks)
|
|
{
|
|
auto jnetobj = jnet.toObject();
|
|
if (jnetobj.isEmpty()) continue;
|
|
|
|
Network network;
|
|
|
|
network.networkName = jnetobj.value(CFG_NETWORK_NETWORKNAME) .toString("Unnamed-" + QString::number(unnamedNetworkCounter++));
|
|
network.nickname = jnetobj.value(CFG_NETWORK_NICKNAME) .toString(info::NAME);
|
|
network.password = jnetobj.value(CFG_NETWORK_PASSWORD) .toString();
|
|
network.identifyFormat = jnetobj.value(CFG_NETWORK_IDENTIFY_FMT) .toString("PRIVMSG NickServ identify $nickname $password"); // from libircclient/network.cpp
|
|
network.ident = jnetobj.value(CFG_NETWORK_IDENT) .toString(QString(info::NAME) + " " + QString(info::VERSION));
|
|
network.host = jnetobj.value(CFG_NETWORK_HOST) .toString();
|
|
network.realName = jnetobj.value(CFG_NETWORK_REAL_NAME) .toString(info::SOURCE_CODE);
|
|
network.autoReconnect = jnetobj.value(CFG_NETWORK_AUTORECONNECT) .toBool(true);
|
|
network.voiceGate = jnetobj.value(CFG_NETWORK_VOICEGATE) .toBool(true);
|
|
network.ssl = jnetobj.value(CFG_NETWORK_SSL) .toBool(false);
|
|
network.port = jnetobj.value(CFG_NETWORK_PORT) .toInt(network.ssl ? 6697 : 6667);
|
|
|
|
QJsonArray jchannels = jnetobj.value(CFG_NETWORK_CHANNELS).toArray();
|
|
if (jchannels.isEmpty())
|
|
{
|
|
if (errorString) *errorString = "No channels defined for " + network.networkName;
|
|
|
|
return false;
|
|
}
|
|
for (const auto& jchan: jchannels)
|
|
{
|
|
QString channel = jchan.toString();
|
|
channel.remove(' ');
|
|
if (channel.isEmpty()) continue;
|
|
if (not channel.startsWith('#'))
|
|
{
|
|
channel.push_front('#');
|
|
}
|
|
network.channels.push_back(channel);
|
|
}
|
|
if (network.channels.isEmpty())
|
|
{
|
|
if (errorString) *errorString = "No valid channels defined for " + network.networkName;
|
|
|
|
return false;
|
|
}
|
|
|
|
m_networks.push_back(network);
|
|
}
|
|
|
|
return true;
|
|
}
|