add files
This commit is contained in:
parent
7ea1d8c322
commit
7eb725fd5b
47
priv_thread.cpp
Normal file
47
priv_thread.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "priv_thread.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "command_thread.hpp"
|
||||
|
||||
PrivThread::PrivThread(Connection& connection) : connection_{connection} {}
|
||||
|
||||
auto PrivThread::check_command(CommandEvent& event, std::string priv) -> bool
|
||||
{
|
||||
return
|
||||
(not event.account.empty() &&
|
||||
(check(account_privs, priv, "") ||
|
||||
check(account_privs, priv, std::string{event.account}))) ||
|
||||
(not event.oper.empty() &&
|
||||
(check(oper_privs, priv, "") ||
|
||||
check(oper_privs, priv, std::string{event.oper})));
|
||||
}
|
||||
|
||||
auto PrivThread::check(
|
||||
std::unordered_map<std::string, std::unordered_set<std::string>> const& privs,
|
||||
std::string const& priv,
|
||||
std::string const& name
|
||||
) -> bool
|
||||
{
|
||||
auto const cursor = privs.find(name);
|
||||
return cursor != privs.end() && cursor->second.contains(priv);
|
||||
}
|
||||
|
||||
auto PrivThread::on_command(CommandEvent& event) -> void
|
||||
{
|
||||
if (event.command == "setup")
|
||||
{
|
||||
oper_privs["glguy"].insert("owner");
|
||||
}
|
||||
}
|
||||
|
||||
auto PrivThread::start(Connection& connection) -> std::shared_ptr<PrivThread>
|
||||
{
|
||||
auto thread = std::make_shared<PrivThread>(connection);
|
||||
|
||||
connection.add_listener<CommandEvent>([thread](CommandEvent& event)
|
||||
{
|
||||
thread->on_command(event);
|
||||
});
|
||||
|
||||
return thread;
|
||||
}
|
31
priv_thread.hpp
Normal file
31
priv_thread.hpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
class Connection;
|
||||
class CommandEvent;
|
||||
|
||||
class PrivThread : std::enable_shared_from_this<PrivThread>
|
||||
{
|
||||
Connection& connection_;
|
||||
std::unordered_map<std::string, std::unordered_set<std::string>> oper_privs;
|
||||
std::unordered_map<std::string, std::unordered_set<std::string>> account_privs;
|
||||
|
||||
auto on_command(CommandEvent&) -> void;
|
||||
|
||||
auto check(
|
||||
std::unordered_map<std::string, std::unordered_set<std::string>> const& privs,
|
||||
std::string const& priv,
|
||||
std::string const& name) -> bool;
|
||||
|
||||
public:
|
||||
PrivThread(Connection&);
|
||||
auto check_command(CommandEvent& event, std::string priv) -> bool;
|
||||
|
||||
static constexpr char const* owner_priv = "owner";
|
||||
|
||||
static auto start(Connection&) -> std::shared_ptr<PrivThread>;
|
||||
};
|
Loading…
Reference in New Issue
Block a user