xbot/priv_thread.cpp

48 lines
1.3 KiB
C++
Raw Normal View History

2023-11-29 09:07:20 -08:00
#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;
}