A bit better module config

This commit is contained in:
Blue 2025-03-31 13:02:45 +03:00
parent 647b8f3072
commit 98bfab4ba5
Signed by: blue
GPG Key ID: 9B203B252A63EE38
13 changed files with 67 additions and 45 deletions

View File

@ -69,28 +69,36 @@ bool Config::isValid() const {
return !getBareJID().empty() && !getPassword().empty();
}
Config::Module Config::getModuleConfig(const std::string& name) const {
std::vector<Config::Module> Config::getModules() const {
std::vector<Module> result;
YAML::Node modules = root["modules"];
if (!modules || !modules.IsMap())
return { false };
if (!modules || !modules.IsSequence())
return result;
YAML::Node conf = modules[name];
if (!conf || !conf.IsMap())
return { false };
for (const YAML::Node& module : modules) {
if (!module.IsMap())
continue;
Module result {
conf["enabled"].as<bool>(true),
conf["alias"].as<std::string>(name)
};
std::string type = module["type"].as<std::string>();
if (type.empty())
continue;
YAML::Node prm = conf["permissions"];
if (prm.IsMap()) {
for (const auto& node : prm) {
Shared::Strings& list = result.permissions.emplace(node.first.as<std::string>(), Shared::Strings()).first->second;
YAML::Node lst = node.second;
if (lst.IsSequence())
for (const YAML::Node& member : lst)
list.emplace_back(member.as<std::string>());
Module& conf = result.emplace_back(
module["enabled"].as<bool>(true),
type,
module["alias"].as<std::string>(type)
);
YAML::Node prm = module["permissions"];
if (prm.IsMap()) {
for (const auto& node : prm) {
Shared::Strings& list = conf.permissions.emplace(node.first.as<std::string>(), Shared::Strings()).first->second;
YAML::Node lst = node.second;
if (lst.IsSequence())
for (const YAML::Node& member : lst)
list.emplace_back(member.as<std::string>());
}
}
}

View File

@ -5,6 +5,7 @@
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <filesystem>
@ -20,6 +21,7 @@ class Config {
public:
struct Module {
bool enabled;
std::string type;
std::string alias;
Shared::Permissions permissions;
};
@ -36,7 +38,7 @@ public:
std::map<std::string, std::string> getActors() const;
Shared::Logger::Level getLogLevel() const;
gloox::TLSPolicy getTLSPolicy() const;
Module getModuleConfig(const std::string& name) const;
std::vector<Module> getModules() const;
Shared::Responses getResponses() const;
void setActors(const std::map<std::string, std::string>& actors);

View File

@ -19,8 +19,9 @@ Router::Router(const Shared::Logger& logger):
generator(std::random_device{}())
{}
void Router::registerModule(const std::string& key, const std::shared_ptr<Module::Module>& module) {
modules[key] = module;
void Router::registerModule(const std::shared_ptr<Module::Module>& module) {
info("Registering module " + module->name + " as " + module->alias);
modules[module->alias] = module;
}
void Router::registerActor(const std::string& key, const std::string& group) {

View File

@ -24,7 +24,7 @@ class Router : private Shared::Loggable {
public:
Router(const Shared::Logger& logger);
void registerModule(const std::string& key, const std::shared_ptr<Module::Module>& module);
void registerModule(const std::shared_ptr<Module::Module>& module);
void registerActor(const std::string& key, const std::string& group);
void routeMessage(const std::string& sender, const std::string& body);

View File

@ -99,9 +99,11 @@ std::string Connection::errorTypeToString(gloox::StanzaErrorType err) {
return "Modify";
case gloox::StanzaErrorTypeWait:
return "Wait";
case gloox::StanzaErrorTypeUndefined:
case gloox::StanzaErrorTypeUndefined:
return "Undefined";
}
return "Unknown";
}
void Connection::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {

View File

@ -11,14 +11,14 @@ actors:
default: Stranger
modules:
actor:
- type: actor
alias: actor
enabled: true
permissions:
read: [Owner, User]
write: [Owner]
publish:
- type: publish
alias: publish
enabled: true
permissions:

22
jay.cpp
View File

@ -11,18 +11,18 @@ static const std::map<
std::function<
std::shared_ptr<Module::Module>(
const std::shared_ptr<Core>&,
const Shared::Permissions& permissions
const Config::Module& conf
)
>
> moduleNames = {
{"actor", [](
const std::shared_ptr<Core>& core,
const Shared::Permissions& permissions
) { return std::make_shared<Module::Actor>(core, permissions); }},
const Config::Module& conf
) { return std::make_shared<Module::Actor>(core, conf); }},
{"publish", [](
const std::shared_ptr<Core>& core,
const Shared::Permissions& permissions
) { return std::make_shared<Module::Publish>(core, permissions); }}
const Config::Module& conf
) { return std::make_shared<Module::Publish>(core, conf); }}
};
Jay::Jay(const std::string& configPath):
@ -51,13 +51,15 @@ void Jay::initialize() {
}
void Jay::createModules() {
for (const auto& pair : moduleNames) {
Config::Module conf = core->config.getModuleConfig(pair.first);
for (const Config::Module& conf : core->config.getModules()) {
if (!conf.enabled)
continue;
core->logger.log(Shared::Logger::info, "enabling module " + pair.first, {"Jay"});
modules.emplace_back(pair.second(core, conf.permissions));
core->router.registerModule(pair.first, modules.back());
auto itr = moduleNames.find(conf.type);
if (itr == moduleNames.end())
continue;
modules.emplace_back(itr->second(core, conf));
core->router.registerModule(modules.back());
}
}

View File

@ -3,8 +3,8 @@
#include "actor.h"
Module::Actor::Actor(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions):
Module(core, permissions, "Actor")
Module::Actor::Actor(const std::shared_ptr<Core>& core, const Config::Module& conf):
Module(core, conf, "Actor")
{}
Module::Actor::~Actor() noexcept {}

View File

@ -11,7 +11,7 @@ namespace Module {
class Actor : public Module {
public:
Actor(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions);
Actor(const std::shared_ptr<Core>& core, const Config::Module& conf);
~Actor() noexcept;
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) override;

View File

@ -7,10 +7,12 @@
#include "gloox/message.h"
Module::Module::Module(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions, const std::string& name):
Shared::Loggable(core->logger, {"Module", name}),
Module::Module::Module(const std::shared_ptr<Core>& core, const Config::Module& conf, const std::string& name):
Shared::Loggable(core->logger, {"Module", name, conf.alias}),
name(name),
alias(conf.alias),
core(core),
permissions(permissions)
permissions(conf.permissions)
{}
bool Module::Module::hasPermission(const std::string& permission, const std::shared_ptr<::Actor>& actor) const {

View File

@ -14,12 +14,13 @@
#include "shared/loggable.h"
#include "component/core.h"
#include "component/actor.h"
#include "component/config.h"
namespace Module {
class Module : protected Shared::Loggable {
protected:
Module(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions, const std::string& name);
Module(const std::shared_ptr<Core>& core, const Config::Module& conf, const std::string& name);
bool hasPermission(const std::string& permission, const std::shared_ptr<::Actor>& actor) const;
@ -30,6 +31,10 @@ public:
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) = 0;
public:
const std::string name;
const std::string alias;
protected:
std::shared_ptr<Core> core;
Shared::Permissions permissions;

View File

@ -5,8 +5,8 @@
#include <exception>
Module::Publish::Publish(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions):
Module(core, permissions, "Actor")
Module::Publish::Publish(const std::shared_ptr<Core>& core, const Config::Module& conf):
Module(core, conf, "Publish")
{}
Module::Publish::~Publish() noexcept {}

View File

@ -13,7 +13,7 @@ namespace Module {
class Publish : public Module {
public:
Publish(const std::shared_ptr<Core>& core, const Shared::Permissions& permissions);
Publish(const std::shared_ptr<Core>& core, const Config::Module& conf);
~Publish() noexcept;
virtual Shared::Result message(const std::shared_ptr<::Actor>& actor, const Shared::Strings& args) override;