// SPDX-FileCopyrightText: 2024 Yury Gubich // SPDX-License-Identifier: GPL-3.0-or-later #include "config.h" Config::Config(const std::filesystem::path& path): path(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(); } Shared::Logger::Level Config::getLogLevel() const { std::string level = root["logLevel"].as("info"); if (level == "trace") return Shared::Logger::trace; else if (level == "debug") return Shared::Logger::debug; else if (level == "info") return Shared::Logger::info; else if (level == "warn" || level == "warning") return Shared::Logger::warning; else if (level == "error") return Shared::Logger::error; else return Shared::Logger::info; } 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) { Shared::Strings& list = result.permissions.emplace(node.first.as(), Shared::Strings()).first->second; YAML::Node lst = node.second; if (lst.IsSequence()) for (const YAML::Node& member : lst) list.emplace_back(member.as()); } } return result; } Shared::Responses Config::getResponses() const { Shared::Responses responses; YAML::Node replies = root["replies"]; if (!replies || !replies.IsMap()) return responses; for (const std::pair& entry : Shared::results) { YAML::Node options = replies[entry.second]; if (!options.IsSequence()) continue; Shared::Strings& out = responses.emplace(entry.first, Shared::Strings()).first->second; for (const YAML::Node& option : options) out.emplace_back(option.as()); } return responses; } void Config::setActors(const std::map& actors) { YAML::Node cfg = root["actors"] = YAML::Node(YAML::NodeType::Map); for (const std::pair& pair : actors) cfg[pair.first] = pair.second; std::ofstream out(path); out << YAML::Dump(root); out.close(); }