From 7eb725fd5be0e1722ae41bdb6296d73365a1e558 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Wed, 29 Nov 2023 09:07:20 -0800 Subject: [PATCH] add files --- priv_thread.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ priv_thread.hpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 priv_thread.cpp create mode 100644 priv_thread.hpp diff --git a/priv_thread.cpp b/priv_thread.cpp new file mode 100644 index 0000000..8a34725 --- /dev/null +++ b/priv_thread.cpp @@ -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> 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 +{ + auto thread = std::make_shared(connection); + + connection.add_listener([thread](CommandEvent& event) + { + thread->on_command(event); + }); + + return thread; +} diff --git a/priv_thread.hpp b/priv_thread.hpp new file mode 100644 index 0000000..4e0a99a --- /dev/null +++ b/priv_thread.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include +#include + +class Connection; +class CommandEvent; + +class PrivThread : std::enable_shared_from_this +{ + Connection& connection_; + std::unordered_map> oper_privs; + std::unordered_map> account_privs; + + auto on_command(CommandEvent&) -> void; + + auto check( + std::unordered_map> 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; +}; \ No newline at end of file