Module creation, message routing
This commit is contained in:
parent
bc2dc9bf07
commit
c605b3ba93
14 changed files with 154 additions and 26 deletions
|
@ -3,7 +3,8 @@
|
|||
|
||||
#include "actor.h"
|
||||
|
||||
Actor::Actor(const std::string& group) :
|
||||
Actor::Actor(const std::string& jid, const std::string& group) :
|
||||
jid(jid),
|
||||
group(group)
|
||||
{}
|
||||
|
||||
|
|
|
@ -7,10 +7,13 @@
|
|||
|
||||
class Actor {
|
||||
public:
|
||||
Actor(const std::string& group);
|
||||
Actor(const std::string& jid, const std::string& group);
|
||||
|
||||
void setGroup(const std::string& newGroup);
|
||||
|
||||
public:
|
||||
const std::string jid;
|
||||
|
||||
private:
|
||||
std::string group;
|
||||
};
|
||||
|
|
|
@ -61,3 +61,32 @@ std::map<std::string, std::string> Config::getOwners() const {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,16 @@
|
|||
#include "yaml-cpp/yaml.h"
|
||||
|
||||
class Config {
|
||||
public:
|
||||
struct Module {
|
||||
typedef std::vector<std::string> List;
|
||||
typedef std::map<std::string, List> Permissions;
|
||||
|
||||
bool enabled;
|
||||
std::string alias;
|
||||
Permissions permissions;
|
||||
};
|
||||
|
||||
public:
|
||||
Config(const std::string& path);
|
||||
|
||||
|
@ -23,6 +33,7 @@ public:
|
|||
std::map<std::string, std::string> getOwners() const;
|
||||
gloox::LogLevel getLogLevel() const;
|
||||
gloox::TLSPolicy getTLSPolicy() const;
|
||||
Module getModuleConfig(const std::string& name) const;
|
||||
|
||||
private:
|
||||
YAML::Node root;
|
||||
|
|
|
@ -12,7 +12,6 @@ void Router::registerModule(const std::string& key, const std::shared_ptr<Module
|
|||
modules[key] = module;
|
||||
}
|
||||
|
||||
|
||||
void Router::registerActor(const std::string& key, const std::string& group) {
|
||||
if (key == "default") {
|
||||
defaultGroup = group;
|
||||
|
@ -21,10 +20,27 @@ void Router::registerActor(const std::string& key, const std::string& group) {
|
|||
|
||||
Actors::iterator act = actors.find(key);
|
||||
if (act == actors.end())
|
||||
actors.emplace(key, group);
|
||||
actors.emplace(key, std::make_shared<Actor>(key, group));
|
||||
else
|
||||
act->second.setGroup(group);
|
||||
act->second->setGroup(group);
|
||||
}
|
||||
|
||||
void Router::routeMessage(const std::string& sender, const std::string& body) {
|
||||
Actors::iterator aItr = actors.find(sender);
|
||||
if (aItr == actors.end())
|
||||
aItr = actors.emplace(sender, std::make_shared<Actor>(sender, defaultGroup)).first;
|
||||
|
||||
std::vector<std::string> args = Module::Module::split(body);
|
||||
Modules::iterator mItr = modules.find(args[0]);
|
||||
if (mItr == modules.end())
|
||||
return;
|
||||
|
||||
std::shared_ptr<Module::Module> module = mItr->second.lock();
|
||||
if (!module)
|
||||
return;
|
||||
|
||||
args.erase(args.begin());
|
||||
module->message(aItr->second, args);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "actor.h"
|
||||
|
@ -20,7 +21,7 @@ public:
|
|||
|
||||
private:
|
||||
typedef std::map<std::string, std::weak_ptr<Module::Module>> Modules;
|
||||
typedef std::map<std::string, Actor> Actors;
|
||||
typedef std::map<std::string, std::shared_ptr<Actor>> Actors;
|
||||
|
||||
Modules modules;
|
||||
Actors actors;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue