Refactored in a way modules have access to bot infrastructure

This commit is contained in:
Blue 2025-03-09 21:01:41 +02:00
parent c605b3ba93
commit 89c04254b8
Signed by: blue
GPG key ID: 9B203B252A63EE38
25 changed files with 245 additions and 216 deletions

View file

@ -3,6 +3,7 @@ set(SOURCES
logger.cpp
actor.cpp
router.cpp
core.cpp
)
set(HEADERS
@ -10,6 +11,7 @@ set(HEADERS
logger.h
actor.h
router.h
core.h
)
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})

View file

@ -11,3 +11,7 @@ Actor::Actor(const std::string& jid, const std::string& group) :
void Actor::setGroup(const std::string& newGroup) {
group = newGroup;
}
std::string Actor::getGroup() const {
return group;
}

View file

@ -10,6 +10,7 @@ public:
Actor(const std::string& jid, const std::string& group);
void setGroup(const std::string& newGroup);
std::string getGroup() const;
public:
const std::string jid;

View file

@ -46,7 +46,7 @@ gloox::TLSPolicy Config::getTLSPolicy() const {
}
std::map<std::string, std::string> Config::getOwners() const {
std::map<std::string, std::string> Config::getActors() const {
std::map<std::string, std::string> result;
YAML::Node owners = root["actors"];
if (!owners.IsMap())

View file

@ -30,7 +30,7 @@ public:
std::string getFullJID() const;
std::string getPassword() const;
std::string getResource() const;
std::map<std::string, std::string> getOwners() const;
std::map<std::string, std::string> getActors() const;
gloox::LogLevel getLogLevel() const;
gloox::TLSPolicy getTLSPolicy() const;
Module getModuleConfig(const std::string& name) const;

10
component/core.cpp Normal file
View file

@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core.h"
Core::Core(const std::string& configPath):
config(configPath),
router(),
logger()
{}

20
component/core.h Normal file
View file

@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <string>
#include "config.h"
#include "router.h"
#include "logger.h"
class Core {
public:
Core(const std::string& configPath);
Config config;
Router router;
Logger logger;
};

View file

@ -78,15 +78,10 @@ void writeTags(gloox::LogArea area) {
std::cout << '\t';
}
Logger::Logger(gloox::LogSink& sink, gloox::LogLevel level):
sink(sink)
{
sink.registerLogHandler(level, gloox::LogAreaAll, this);
}
Logger::Logger()
{}
Logger::~Logger() {
sink.removeLogHandler(this);
}
Logger::~Logger() {}
void Logger::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) {
writeTimestamp();

View file

@ -6,15 +6,10 @@
#include <gloox/loghandler.h>
#include <gloox/logsink.h>
class Logger : public gloox::LogHandler {
class Logger: public gloox::LogHandler {
public:
Logger(gloox::LogSink& sink, gloox::LogLevel level);
Logger();
~Logger();
void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) override;
private:
gloox::LogSink& sink;
gloox::LogLevel level;
};

View file

@ -3,6 +3,8 @@
#include "router.h"
#include "module/module.h"
Router::Router():
actors(),
defaultGroup("Stranger")
@ -43,4 +45,13 @@ void Router::routeMessage(const std::string& sender, const std::string& body) {
module->message(aItr->second, args);
}
std::map<std::string, std::string> Router::getActors() const {
std::map<std::string, std::string> result;
for (const std::pair<const std::string, std::shared_ptr<Actor>>& pair : actors)
result.emplace(pair.first, pair.second->getGroup());
return result;
}

View file

@ -9,7 +9,10 @@
#include <memory>
#include "actor.h"
#include "module/module.h"
namespace Module {
class Module;
};
class Router {
public:
@ -19,6 +22,8 @@ public:
void registerActor(const std::string& key, const std::string& group);
void routeMessage(const std::string& sender, const std::string& body);
std::map<std::string, std::string> getActors() const;
private:
typedef std::map<std::string, std::weak_ptr<Module::Module>> Modules;
typedef std::map<std::string, std::shared_ptr<Actor>> Actors;