jay/component/config.cpp

93 lines
2.4 KiB
C++

// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "config.h"
Config::Config(const std::string& path):
root(YAML::LoadFile(path))
{}
std::string Config::getBareJID() const {
return root["jid"].as<std::string>();
}
std::string Config::getPassword() const {
return root["password"].as<std::string>();
}
std::string Config::getResource() const {
return root["resource"].as<std::string>("bot");
}
std::string Config::getFullJID() const {
return getBareJID() + "/" + getResource();
}
gloox::LogLevel Config::getLogLevel() const {
std::string level = root["logLevel"].as<std::string>("warning");
if (level == "debug")
return gloox::LogLevelDebug;
else if (level == "error")
return gloox::LogLevelError;
else
return gloox::LogLevelWarning;
}
gloox::TLSPolicy Config::getTLSPolicy() const {
std::string level = root["tls"].as<std::string>("optional");
if (level == "disabled")
return gloox::TLSDisabled;
else if (level == "required")
return gloox::TLSRequired;
else
return gloox::TLSOptional;
}
std::map<std::string, std::string> Config::getActors() const {
std::map<std::string, std::string> result;
YAML::Node owners = root["actors"];
if (!owners.IsMap())
return result;
for (const auto& node : owners)
result.emplace(node.first.as<std::string>(), node.second.as<std::string>());
return result;
}
bool Config::isValid() const {
return !getBareJID().empty() && !getPassword().empty();
}
Config::Module Config::getModuleConfig(const std::string& name) const {
YAML::Node modules = root["modules"];
if (!modules || !modules.IsMap())
return { false };
YAML::Node conf = modules[name];
if (!conf || !conf.IsMap())
return { false };
Module result {
conf["enabled"].as<bool>(true),
conf["alias"].as<std::string>(name)
};
YAML::Node prm = conf["permissions"];
if (prm.IsMap()) {
for (const auto& node : prm) {
Module::List& list = result.permissions.emplace(node.first.as<std::string>(), Module::List()).first->second;
YAML::Node lst = node.second;
if (lst.IsSequence())
for (const YAML::Node& member : lst)
list.emplace_back(member.as<std::string>());
}
}
return result;
}