// SPDX-FileCopyrightText: 2024 Yury Gubich // 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 Config::getPassword() const { return root["password"].as(); } std::string Config::getResource() const { return root["resource"].as("bot"); } std::string Config::getFullJID() const { return getBareJID() + "/" + getResource(); } gloox::LogLevel Config::getLogLevel() const { std::string level = root["logLevel"].as("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("optional"); if (level == "disabled") return gloox::TLSDisabled; else if (level == "required") return gloox::TLSRequired; else return gloox::TLSOptional; } std::map Config::getActors() const { std::map result; YAML::Node owners = root["actors"]; if (!owners.IsMap()) return result; for (const auto& node : owners) result.emplace(node.first.as(), node.second.as()); 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(true), conf["alias"].as(name) }; YAML::Node prm = conf["permissions"]; if (prm.IsMap()) { for (const auto& node : prm) { Module::List& list = result.permissions.emplace(node.first.as(), Module::List()).first->second; YAML::Node lst = node.second; if (lst.IsSequence()) for (const YAML::Node& member : lst) list.emplace_back(member.as()); } } return result; }