initial logging

This commit is contained in:
Eric Mertens 2023-11-29 13:13:48 -08:00
parent 5f0eb57e83
commit 7e4346a50e
7 changed files with 64 additions and 50 deletions

View File

@ -6,7 +6,7 @@ project(xbot
LANGUAGES C CXX LANGUAGES C CXX
) )
find_package(Boost REQUIRED) find_package(Boost REQUIRED COMPONENTS log)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBHS libhs REQUIRED IMPORTED_TARGET) pkg_check_modules(LIBHS libhs REQUIRED IMPORTED_TARGET)
@ -43,4 +43,4 @@ add_executable(xbot
ping_thread.cpp irc_parse_thread.cpp registration_thread.cpp ping_thread.cpp irc_parse_thread.cpp registration_thread.cpp
self_thread.cpp command_thread.cpp priv_thread.cpp) self_thread.cpp command_thread.cpp priv_thread.cpp)
target_include_directories(xbot PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(xbot PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(xbot PRIVATE Boost::headers tomlplusplus_tomlplusplus eventpp PkgConfig::LIBHS) target_link_libraries(xbot PRIVATE Boost::log Boost::headers tomlplusplus_tomlplusplus eventpp PkgConfig::LIBHS)

View File

@ -2,6 +2,8 @@
#include "linebuffer.hpp" #include "linebuffer.hpp"
#include <boost/log/trivial.hpp>
Connection::Connection(boost::asio::io_context & io) Connection::Connection(boost::asio::io_context & io)
: stream_{io} : stream_{io}
, write_timer_{io, std::chrono::steady_clock::time_point::max()} , write_timer_{io, std::chrono::steady_clock::time_point::max()}
@ -83,7 +85,7 @@ auto Connection::connect(
break; break;
} }
buffer.add_bytes(n, [this](char * line) { buffer.add_bytes(n, [this](char * line) {
std::cout << "RECV: " << line << std::endl; BOOST_LOG_TRIVIAL(debug) << "RECV: " << line;
make_event<LineEvent>(line); make_event<LineEvent>(line);
}); });
} }
@ -93,7 +95,7 @@ auto Connection::connect(
auto Connection::write_line(std::string message) -> void auto Connection::write_line(std::string message) -> void
{ {
std::cout << "SEND: " << message << std::endl; BOOST_LOG_TRIVIAL(debug) << "SEND: " << message;
message += "\r\n"; message += "\r\n";
auto const need_cancel = write_strings_.empty(); auto const need_cancel = write_strings_.empty();
write_strings_.push_back(std::move(message)); write_strings_.push_back(std::move(message));

View File

@ -3,6 +3,8 @@
#include "connection.hpp" #include "connection.hpp"
#include "ircmsg.hpp" #include "ircmsg.hpp"
#include <boost/log/trivial.hpp>
#include <cstring> #include <cstring>
namespace { namespace {
@ -28,7 +30,7 @@ auto IrcParseThread::start(Connection& connection) -> void
if (IrcCommand::UNKNOWN == command) if (IrcCommand::UNKNOWN == command)
{ {
std::cout << "Unrecognized command: " << msg.command << " " << msg.args.size() << std::endl; BOOST_LOG_TRIVIAL(warning) << "Unrecognized command: " << msg.command << " " << msg.args.size();
} }
connection.make_event<IrcMsgEvent>(command, msg); connection.make_event<IrcMsgEvent>(command, msg);
}); });

View File

@ -1,4 +1,3 @@
#include <boost/asio.hpp>
#include "connection.hpp" #include "connection.hpp"
#include "event.hpp" #include "event.hpp"
@ -16,6 +15,8 @@
#include "watchdog_thread.hpp" #include "watchdog_thread.hpp"
#include "priv_thread.hpp" #include "priv_thread.hpp"
#include <boost/asio.hpp>
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>

View File

@ -6,7 +6,8 @@
#include <toml++/toml.hpp> #include <toml++/toml.hpp>
#include <cstdio> #include <boost/log/trivial.hpp>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <filesystem> #include <filesystem>
@ -19,23 +20,17 @@ PrivThread::PrivThread(Connection& connection, std::string config_path)
auto PrivThread::check_command(CommandEvent& event, std::string priv) -> bool auto PrivThread::check_command(CommandEvent& event, std::string priv) -> bool
{ {
return auto check_priv = [&priv](auto& map, std::string const& name) {
(not event.account.empty() && auto const cursor = map.find(name);
(check(account_privs_, priv, wildcard) || return cursor != map.end() && cursor->second.contains(priv);
check(account_privs_, priv, std::string{event.account}))) || };
(not event.oper.empty() && auto check = [&check_priv](auto& map, auto& name) {
(check(oper_privs_, priv, wildcard) || return
check(oper_privs_, priv, std::string{event.oper}))); (not name.empty() &&
} (check_priv(map, wildcard) ||
check_priv(map, std::string{name})));
auto PrivThread::check( };
std::unordered_map<std::string, std::unordered_set<std::string>> const& privs, return check(oper_privs_, event.oper) || check(account_privs_, event.account);
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::list_privs(std::string_view nick) -> void auto PrivThread::list_privs(std::string_view nick) -> void
@ -93,6 +88,7 @@ auto PrivThread::on_command(CommandEvent& event) -> void
oper_privs_[name].insert(priv); oper_privs_[name].insert(priv);
save_config(); save_config();
send_notice(connection_, event.nick, "ack"); send_notice(connection_, event.nick, "ack");
BOOST_LOG_TRIVIAL(info) << "Priv added. by:" << event.nick << " oper:" << name << " priv:" << priv;
return; return;
} }
else if ("account" == kind) else if ("account" == kind)
@ -100,6 +96,7 @@ auto PrivThread::on_command(CommandEvent& event) -> void
account_privs_[name].insert(priv); account_privs_[name].insert(priv);
save_config(); save_config();
send_notice(connection_, event.nick, "ack"); send_notice(connection_, event.nick, "ack");
BOOST_LOG_TRIVIAL(info) << "Priv added. by:" << event.nick << " account:" << name << " priv:" << priv;
return; return;
} }
} }
@ -116,18 +113,34 @@ auto PrivThread::on_command(CommandEvent& event) -> void
{ {
if ("oper" == kind) if ("oper" == kind)
{ {
oper_privs_[name].erase(priv); if (wildcard == priv)
if (oper_privs_[name].empty()) oper_privs_.erase(name); {
oper_privs_.erase(name);
}
else
{
oper_privs_[name].erase(priv);
if (oper_privs_[name].empty()) oper_privs_.erase(name);
}
save_config(); save_config();
send_notice(connection_, event.nick, "ack"); send_notice(connection_, event.nick, "ack");
BOOST_LOG_TRIVIAL(info) << "Priv removed. by:" << event.nick << " oper:" << name << " priv:" << priv;
return; return;
} }
else if ("account" == kind) else if ("account" == kind)
{ {
account_privs_[name].erase(priv); if (wildcard == priv)
if (account_privs_[name].empty()) account_privs_.erase(name); {
account_privs_.erase(name);
}
else
{
account_privs_[name].erase(priv);
if (account_privs_[name].empty()) account_privs_.erase(name);
}
save_config(); save_config();
send_notice(connection_, event.nick, "ack"); send_notice(connection_, event.nick, "ack");
BOOST_LOG_TRIVIAL(info) << "Priv removed. by:" << event.nick << " account:" << name << " priv:" << priv;
return; return;
} }
} }
@ -138,27 +151,26 @@ auto PrivThread::on_command(CommandEvent& event) -> void
auto PrivThread::save_config() -> void auto PrivThread::save_config() -> void
{ {
auto serialize_table = [](auto map) { auto config = toml::table{};
auto tab = toml::table{};
for (auto const& [oper, privs] : map) auto serialize_table = [&config](auto name, auto& map) {
{ if (not map.empty()) {
auto privset = toml::array{}; auto tab = toml::table{};
for (auto const& priv : privs) for (auto const& [oper, privs] : map)
{ {
privset.push_back(priv); auto privset = toml::array{};
for (auto const& priv : privs)
{
privset.push_back(priv);
}
tab.insert(oper, privset);
} }
tab.insert(oper, privset); config.insert(name, tab);
} }
return tab;
}; };
auto config = toml::table{}; serialize_table("oper", oper_privs_);
if (not oper_privs_.empty()) { serialize_table("account", account_privs_);
config.insert("oper", serialize_table(oper_privs_));
}
if (not account_privs_.empty()) {
config.insert("account", serialize_table(account_privs_));
}
std::string tmp = config_path_ + ".tmp"; std::string tmp = config_path_ + ".tmp";
std::ofstream{tmp} << config; std::ofstream{tmp} << config;

View File

@ -17,11 +17,6 @@ class PrivThread : std::enable_shared_from_this<PrivThread>
std::unordered_map<std::string, std::unordered_set<std::string>> account_privs_; std::unordered_map<std::string, std::unordered_set<std::string>> account_privs_;
auto on_command(CommandEvent&) -> void; auto on_command(CommandEvent&) -> void;
auto check(
std::unordered_map<std::string, std::unordered_set<std::string>> const& privs,
std::string const& priv,
std::string const& name) -> bool;
auto load_config() -> void; auto load_config() -> void;
auto save_config() -> void; auto save_config() -> void;

View File

@ -7,6 +7,8 @@
#include <hs.h> #include <hs.h>
#include <boost/log/trivial.hpp>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <regex> #include <regex>
@ -149,7 +151,7 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
switch (scan_result) switch (scan_result)
{ {
case HS_SUCCESS: case HS_SUCCESS:
std::cout << "Unknown snote: " << message << std::endl; BOOST_LOG_TRIVIAL(warning) << "Unknown snote: " << message;
break; break;
case HS_SCAN_TERMINATED: case HS_SCAN_TERMINATED: