xbot/priv_thread.cpp

225 lines
6.6 KiB
C++
Raw Normal View History

2023-11-29 09:07:20 -08:00
#include "priv_thread.hpp"
#include "command_thread.hpp"
2023-11-29 09:44:36 -08:00
#include "connection.hpp"
#include "write_irc.hpp"
#include <toml++/toml.hpp>
2023-11-29 09:07:20 -08:00
2023-11-29 13:13:48 -08:00
#include <boost/log/trivial.hpp>
2023-11-29 09:44:36 -08:00
#include <fstream>
2023-11-29 11:07:24 -08:00
#include <sstream>
#include <filesystem>
2023-11-29 09:44:36 -08:00
PrivThread::PrivThread(Connection& connection, std::string config_path)
: connection_{connection}
, config_path_{std::move(config_path)}
{
}
2023-11-29 09:07:20 -08:00
auto PrivThread::check_command(CommandEvent& event, std::string priv) -> bool
{
2023-11-29 13:13:48 -08:00
auto check_priv = [&priv](auto& map, std::string const& name) {
auto const cursor = map.find(name);
return cursor != map.end() && cursor->second.contains(priv);
};
auto check = [&check_priv](auto& map, auto& name) {
return
(not name.empty() &&
(check_priv(map, wildcard) ||
check_priv(map, std::string{name})));
};
return check(oper_privs_, event.oper) || check(account_privs_, event.account);
2023-11-29 09:07:20 -08:00
}
2023-11-29 11:07:24 -08:00
auto PrivThread::list_privs(std::string_view nick) -> void
2023-11-29 09:07:20 -08:00
{
2023-11-29 11:07:24 -08:00
for (auto const& [oper, privset] : oper_privs_)
2023-11-29 09:07:20 -08:00
{
2023-11-29 11:07:24 -08:00
std::string message = "Oper ";
message += oper;
message += ":";
for (auto const& priv : privset)
2023-11-29 09:44:36 -08:00
{
2023-11-29 11:07:24 -08:00
message += " ";
message += priv;
}
2023-11-29 09:44:36 -08:00
2023-11-29 11:07:24 -08:00
send_notice(connection_, nick, message);
}
2023-11-29 09:44:36 -08:00
2023-11-29 11:07:24 -08:00
for (auto const& [account, privset] : account_privs_)
{
std::string message = "Account ";
message += account;
message += ":";
2023-11-29 09:44:36 -08:00
2023-11-29 11:07:24 -08:00
for (auto const& priv : privset)
2023-11-29 09:44:36 -08:00
{
2023-11-29 11:07:24 -08:00
message += " ";
message += priv;
}
send_notice(connection_, nick, message);
}
}
2023-11-29 09:44:36 -08:00
2023-11-29 11:07:24 -08:00
auto PrivThread::on_command(CommandEvent& event) -> void
{
if ("list_privs" == event.command)
{
if (check_command(event, PrivThread::owner_priv))
{
list_privs(event.nick);
}
}
else if ("add_priv" == event.command)
{
if (check_command(event, PrivThread::owner_priv))
{
auto in = std::istringstream{std::string{event.arg}};
std::string kind, name, priv;
if (in >> kind >> name >> priv)
{
if ("oper" == kind)
{
oper_privs_[name].insert(priv);
save_config();
send_notice(connection_, event.nick, "ack");
2023-11-29 13:13:48 -08:00
BOOST_LOG_TRIVIAL(info) << "Priv added. by:" << event.nick << " oper:" << name << " priv:" << priv;
2023-11-29 11:07:24 -08:00
return;
}
else if ("account" == kind)
{
account_privs_[name].insert(priv);
save_config();
send_notice(connection_, event.nick, "ack");
2023-11-29 13:13:48 -08:00
BOOST_LOG_TRIVIAL(info) << "Priv added. by:" << event.nick << " account:" << name << " priv:" << priv;
2023-11-29 11:07:24 -08:00
return;
}
}
send_notice(connection_, event.nick, "nak");
}
}
else if ("remove_priv" == event.command)
{
if (check_command(event, PrivThread::owner_priv))
{
auto in = std::istringstream{std::string{event.arg}};
std::string kind, name, priv;
if (in >> kind >> name >> priv)
2023-11-29 09:44:36 -08:00
{
2023-11-29 11:07:24 -08:00
if ("oper" == kind)
{
2023-11-29 13:13:48 -08:00
if (wildcard == priv)
{
oper_privs_.erase(name);
}
else
{
oper_privs_[name].erase(priv);
if (oper_privs_[name].empty()) oper_privs_.erase(name);
}
2023-11-29 11:07:24 -08:00
save_config();
send_notice(connection_, event.nick, "ack");
2023-11-29 13:13:48 -08:00
BOOST_LOG_TRIVIAL(info) << "Priv removed. by:" << event.nick << " oper:" << name << " priv:" << priv;
2023-11-29 11:07:24 -08:00
return;
}
else if ("account" == kind)
{
2023-11-29 13:13:48 -08:00
if (wildcard == priv)
{
account_privs_.erase(name);
}
else
{
account_privs_[name].erase(priv);
if (account_privs_[name].empty()) account_privs_.erase(name);
}
2023-11-29 11:07:24 -08:00
save_config();
send_notice(connection_, event.nick, "ack");
2023-11-29 13:13:48 -08:00
BOOST_LOG_TRIVIAL(info) << "Priv removed. by:" << event.nick << " account:" << name << " priv:" << priv;
2023-11-29 11:07:24 -08:00
return;
}
2023-11-29 09:44:36 -08:00
}
2023-11-29 11:07:24 -08:00
send_notice(connection_, event.nick, "nak");
}
}
}
2023-11-29 09:44:36 -08:00
2023-11-29 11:07:24 -08:00
auto PrivThread::save_config() -> void
{
2023-11-29 13:13:48 -08:00
auto config = toml::table{};
auto serialize_table = [&config](auto name, auto& map) {
if (not map.empty()) {
auto tab = toml::table{};
for (auto const& [oper, privs] : map)
2023-11-29 11:07:24 -08:00
{
2023-11-29 13:13:48 -08:00
auto privset = toml::array{};
for (auto const& priv : privs)
{
privset.push_back(priv);
}
tab.insert(oper, privset);
2023-11-29 11:07:24 -08:00
}
2023-11-29 13:13:48 -08:00
config.insert(name, tab);
2023-11-29 09:44:36 -08:00
}
2023-11-29 11:07:24 -08:00
};
2023-11-29 13:13:48 -08:00
serialize_table("oper", oper_privs_);
serialize_table("account", account_privs_);
2023-11-29 11:07:24 -08:00
std::string tmp = config_path_ + ".tmp";
std::ofstream{tmp} << config;
std::filesystem::rename(tmp, config_path_);
2023-11-29 09:07:20 -08:00
}
2023-11-29 09:44:36 -08:00
auto PrivThread::load_config() -> void
2023-11-29 09:07:20 -08:00
{
2023-11-29 09:44:36 -08:00
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())
{
2023-11-29 11:07:24 -08:00
auto& privset = oper_privs_[std::string{oper.str()}];
2023-11-29 09:44:36 -08:00
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())
{
2023-11-29 11:07:24 -08:00
auto& privset = account_privs_[std::string{account.str()}];
2023-11-29 09:44:36 -08:00
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();
2023-11-29 09:07:20 -08:00
connection.add_listener<CommandEvent>([thread](CommandEvent& event)
{
thread->on_command(event);
});
return thread;
}