From 5b19afa0a49f6bd2327472d8a93183b241c69f76 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Wed, 29 Nov 2023 09:44:36 -0800 Subject: [PATCH] make privthread file configurable --- .gitignore | 1 + main.cpp | 8 +++-- priv_thread.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++---- priv_thread.hpp | 10 ++++-- 4 files changed, 90 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index f58b540..3bf80e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /out /config.toml +/privs.toml /.ccls /archive /.vscode diff --git a/main.cpp b/main.cpp index 9e37488..fef919b 100644 --- a/main.cpp +++ b/main.cpp @@ -41,7 +41,7 @@ auto echo_thread( { connection.add_listener([&connection, priv_thread](CommandEvent& event) { - if ("raw" == event.command && priv_thread->check_command(event, "owner")) + if ("raw" == event.command && priv_thread->check_command(event, PrivThread::owner_priv)) { connection.write_line(std::string{event.arg}); event.handled_ = true; @@ -52,7 +52,7 @@ auto echo_thread( auto start(boost::asio::io_context & io, Settings const& settings) -> void { - auto connection = std::make_shared(io); + auto const connection = std::make_shared(io); WatchdogThread::start(*connection); IrcParseThread::start(*connection); @@ -61,9 +61,10 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname); SnoteThread::start(*connection); CommandThread::start(*connection); - auto const priv_thread = PrivThread::start(*connection); + auto const priv_thread = PrivThread::start(*connection, "privs.toml"); echo_thread(*connection, priv_thread); +/* connection->add_listener([](SnoteEvent& event) { std::cout << "SNOTE " << static_cast(event.get_tag()) << std::endl; for (auto c : event.get_results()) @@ -71,6 +72,7 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void std::cout << " " << std::string_view{c.first, c.second} << std::endl; } }); +*/ boost::asio::co_spawn( io, diff --git a/priv_thread.cpp b/priv_thread.cpp index 8a34725..c6ab671 100644 --- a/priv_thread.cpp +++ b/priv_thread.cpp @@ -1,9 +1,18 @@ #include "priv_thread.hpp" -#include "connection.hpp" #include "command_thread.hpp" +#include "connection.hpp" +#include "write_irc.hpp" -PrivThread::PrivThread(Connection& connection) : connection_{connection} {} +#include + +#include + +PrivThread::PrivThread(Connection& connection, std::string config_path) +: connection_{connection} +, config_path_{std::move(config_path)} +{ +} auto PrivThread::check_command(CommandEvent& event, std::string priv) -> bool { @@ -28,15 +37,77 @@ auto PrivThread::check( auto PrivThread::on_command(CommandEvent& event) -> void { - if (event.command == "setup") + if (event.command == "list_privs" && check_command(event, PrivThread::owner_priv)) { - oper_privs["glguy"].insert("owner"); + for (auto const& [oper, privset] : oper_privs) + { + std::string message = "Oper "; + message += oper; + message += ":"; + + for (auto const& priv : privset) + { + message += " "; + message += priv; + } + + send_notice(connection_, event.nick, message); + } + + for (auto const& [account, privset] : account_privs) + { + std::string message = "Account "; + message += account; + message += ":"; + + for (auto const& priv : privset) + { + message += " "; + message += priv; + } + + send_notice(connection_, event.nick, message); + } } } -auto PrivThread::start(Connection& connection) -> std::shared_ptr +auto PrivThread::load_config() -> void { - auto thread = std::make_shared(connection); + if (auto in = std::ifstream{config_path_}) + { + auto const config = toml::parse(in); + + if (config.contains("oper")) + { + for (auto const [oper, privs] : *config["oper"].as_table()) + { + auto& privset = oper_privs[std::string{oper.str()}]; + for (auto const& priv : *privs.as_array()) + { + privset.insert(priv.as_string()->get()); + } + } + } + + if (config.contains("account")) + { + for (auto const [account, privs] : *config["account"].as_table()) + { + auto& privset = account_privs[std::string{account.str()}]; + for (auto const& priv : *privs.as_array()) + { + privset.insert(priv.as_string()->get()); + } + } + } + } +} + +auto PrivThread::start(Connection& connection, std::string config_path) -> std::shared_ptr +{ + auto thread = std::make_shared(connection, config_path); + + thread->load_config(); connection.add_listener([thread](CommandEvent& event) { diff --git a/priv_thread.hpp b/priv_thread.hpp index 4e0a99a..b1dd245 100644 --- a/priv_thread.hpp +++ b/priv_thread.hpp @@ -11,6 +11,8 @@ class CommandEvent; class PrivThread : std::enable_shared_from_this { Connection& connection_; + std::string config_path_; + std::unordered_map> oper_privs; std::unordered_map> account_privs; @@ -21,11 +23,13 @@ class PrivThread : std::enable_shared_from_this std::string const& priv, std::string const& name) -> bool; + auto load_config() -> void; + public: - PrivThread(Connection&); + PrivThread(Connection&, std::string config_path); 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 + static auto start(Connection&, std::string config_path) -> std::shared_ptr; +};