admin and updateConfig

ver3
acetone 2023-03-04 14:56:50 +03:00
parent 526859848a
commit ca9e459dc1
2 changed files with 86 additions and 10 deletions

View File

@ -22,6 +22,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "logger.h"
#include <QFile>
#include <QListIterator>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
@ -38,6 +39,7 @@ 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_IRCABOT_ADMIN[] = "ircabotAdmin";
constexpr const char CFG_NETWORK_NETWORKNAME[] = "networkName";
constexpr const char CFG_NETWORK_NICKNAME[] = "nickname";
constexpr const char CFG_NETWORK_PASSWORD[] = "password";
@ -104,12 +106,12 @@ bool Config::initFromJson(const QJsonObject &config, QString* errorString)
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
m_webUiThreads = webUi.value(CFG_WEBUI_THREADS).toInt(0); // if <1, then system number
QJsonArray jnetworks = config.value(CFG_NETWORKS).toArray();
if (jnetworks.isEmpty())
{
if (errorString) *errorString = "Networks array is undefined";
if (errorString) *errorString = "Networks is undefined";
return false;
}
@ -122,6 +124,7 @@ bool Config::initFromJson(const QJsonObject &config, QString* errorString)
Network network;
network.ircabotAdmin = jnetobj.value(CFG_NETWORK_IRCABOT_ADMIN) .toString();
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();
@ -135,12 +138,6 @@ bool Config::initFromJson(const QJsonObject &config, QString* errorString)
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();
@ -154,7 +151,12 @@ bool Config::initFromJson(const QJsonObject &config, QString* errorString)
}
if (network.channels.isEmpty())
{
if (errorString) *errorString = "No valid channels defined for " + network.networkName;
qWarning().noquote() << "No channels defined for network " + network.networkName;
}
if (network.host.isEmpty())
{
if (errorString) *errorString = "Host undefined for network " + network.networkName;
return false;
}
@ -164,3 +166,75 @@ bool Config::initFromJson(const QJsonObject &config, QString* errorString)
return true;
}
bool Config::updateConfigWithCurrentData(QString *errorString)
{
QFile file( configurationFilePath() );
if (not file.open(QIODevice::WriteOnly))
{
if (errorString) *errorString = "Can't open file";
return false;
}
QJsonObject config;
config.insert(CFG_LOG_LEVEL, logger::levelString(logger::level));
config.insert(CFG_LOG_FILE, logFilePath());
config.insert(CFG_LOG_FILE_DATE_FORMAT, logFileDateFormat());
config.insert(CFG_WORKING_DIRECTORY, workingDirectory());
QJsonObject webui;
webui.insert(CFG_WEBUI_ADDRESS, webUiAddress());
webui.insert(CFG_WEBUI_PORT, static_cast<int>(webUiPort()));
webui.insert(CFG_WEBUI_THREADS, webUiThreads());
config.insert(CFG_WEBUI, webui);
QListIterator<Network> networksIter(m_networks);
QJsonArray networks;
while (networksIter.hasNext())
{
auto network = networksIter.next();
QJsonObject jnet;
jnet.insert(CFG_NETWORK_IRCABOT_ADMIN, network.ircabotAdmin);
jnet.insert(CFG_NETWORK_NETWORKNAME, network.networkName);
jnet.insert(CFG_NETWORK_NICKNAME, network.nickname);
jnet.insert(CFG_NETWORK_PASSWORD, network.password);
jnet.insert(CFG_NETWORK_IDENTIFY_FMT, network.identifyFormat);
jnet.insert(CFG_NETWORK_HOST, network.host);
jnet.insert(CFG_NETWORK_PORT, static_cast<int>(network.port));
jnet.insert(CFG_NETWORK_SSL, network.ssl);
jnet.insert(CFG_NETWORK_IDENT, network.ident);
jnet.insert(CFG_NETWORK_REAL_NAME, network.realName);
jnet.insert(CFG_NETWORK_AUTORECONNECT, network.autoReconnect);
jnet.insert(CFG_NETWORK_VOICEGATE, network.voiceGate);
QJsonArray jchannels;
QStringListIterator chanIter(network.channels);
while (chanIter.hasNext())
{
jchannels.push_back(chanIter.next());
}
jnet.insert(CFG_NETWORK_CHANNELS, jchannels);
networks.push_back(jnet);
}
config.insert(CFG_NETWORKS, networks);
QJsonObject generated;
generated.insert("name", info::NAME);
generated.insert("verion", info::VERSION);
generated.insert("date", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
config.insert("generated", generated);
QByteArray bytes = QJsonDocument(config).toJson(QJsonDocument::JsonFormat::Indented);
auto written = file.write(bytes);
file.close();
if (written != bytes.size())
{
if (errorString) *errorString = "Writing failed: written " + QString::number(written) + " of " + QString::number(bytes.size()) + " bytes";
return false;
}
return true;
}

View File

@ -35,6 +35,7 @@ public:
struct Network
{
// default values defined in initFromJson()
QString ircabotAdmin;
QString networkName;
QString host;
quint16 port;
@ -52,6 +53,7 @@ public:
static void setConfigurationFilePath(const QString& path) { m_configurationFilePath = path; }
static bool initFromFile(QString* errorString = nullptr);
static bool initFromJson(const QJsonObject& config, QString* errorString = nullptr);
static bool updateConfigWithCurrentData(QString* errorString = nullptr);
static const QString& configurationFilePath() { return m_configurationFilePath; }
static const QString& logFilePath() { return m_logFilePath; }