make privthread file configurable

This commit is contained in:
Eric Mertens 2023-11-29 09:44:36 -08:00
parent 7eb725fd5b
commit 5b19afa0a4
4 changed files with 90 additions and 12 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
/out
/config.toml
/privs.toml
/.ccls
/archive
/.vscode

View File

@ -41,7 +41,7 @@ auto echo_thread(
{
connection.add_listener<CommandEvent>([&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<Connection>(io);
auto const connection = std::make_shared<Connection>(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>([](SnoteEvent& event) {
std::cout << "SNOTE " << static_cast<int>(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,

View File

@ -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 <toml++/toml.hpp>
#include <fstream>
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<PrivThread>
auto PrivThread::load_config() -> void
{
auto thread = std::make_shared<PrivThread>(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<PrivThread>
{
auto thread = std::make_shared<PrivThread>(connection, config_path);
thread->load_config();
connection.add_listener<CommandEvent>([thread](CommandEvent& event)
{

View File

@ -11,6 +11,8 @@ class CommandEvent;
class PrivThread : std::enable_shared_from_this<PrivThread>
{
Connection& connection_;
std::string config_path_;
std::unordered_map<std::string, std::unordered_set<std::string>> oper_privs;
std::unordered_map<std::string, std::unordered_set<std::string>> account_privs;
@ -21,11 +23,13 @@ class PrivThread : std::enable_shared_from_this<PrivThread>
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<PrivThread>;
static auto start(Connection&, std::string config_path) -> std::shared_ptr<PrivThread>;
};