diff --git a/CMakeLists.txt b/CMakeLists.txt index 65bfb6a..5aa5c69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ project(xbot LANGUAGES C CXX ) -find_package(Boost REQUIRED) +find_package(Boost REQUIRED COMPONENTS log) find_package(PkgConfig REQUIRED) 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 self_thread.cpp command_thread.cpp priv_thread.cpp) 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) diff --git a/connection.cpp b/connection.cpp index 9e37f63..dc64199 100644 --- a/connection.cpp +++ b/connection.cpp @@ -2,6 +2,8 @@ #include "linebuffer.hpp" +#include + Connection::Connection(boost::asio::io_context & io) : stream_{io} , write_timer_{io, std::chrono::steady_clock::time_point::max()} @@ -83,7 +85,7 @@ auto Connection::connect( break; } buffer.add_bytes(n, [this](char * line) { - std::cout << "RECV: " << line << std::endl; + BOOST_LOG_TRIVIAL(debug) << "RECV: " << line; make_event(line); }); } @@ -93,7 +95,7 @@ auto Connection::connect( auto Connection::write_line(std::string message) -> void { - std::cout << "SEND: " << message << std::endl; + BOOST_LOG_TRIVIAL(debug) << "SEND: " << message; message += "\r\n"; auto const need_cancel = write_strings_.empty(); write_strings_.push_back(std::move(message)); diff --git a/irc_parse_thread.cpp b/irc_parse_thread.cpp index b4fe1cb..539fa5c 100644 --- a/irc_parse_thread.cpp +++ b/irc_parse_thread.cpp @@ -3,6 +3,8 @@ #include "connection.hpp" #include "ircmsg.hpp" +#include + #include namespace { @@ -28,7 +30,7 @@ auto IrcParseThread::start(Connection& connection) -> void 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(command, msg); }); diff --git a/main.cpp b/main.cpp index fef919b..6f9195d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,3 @@ -#include #include "connection.hpp" #include "event.hpp" @@ -16,6 +15,8 @@ #include "watchdog_thread.hpp" #include "priv_thread.hpp" +#include + #include #include #include diff --git a/priv_thread.cpp b/priv_thread.cpp index d656512..b06a313 100644 --- a/priv_thread.cpp +++ b/priv_thread.cpp @@ -6,7 +6,8 @@ #include -#include +#include + #include #include #include @@ -19,23 +20,17 @@ PrivThread::PrivThread(Connection& connection, std::string config_path) auto PrivThread::check_command(CommandEvent& event, std::string priv) -> bool { - return - (not event.account.empty() && - (check(account_privs_, priv, wildcard) || - check(account_privs_, priv, std::string{event.account}))) || - (not event.oper.empty() && - (check(oper_privs_, priv, wildcard) || - 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 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); } 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); save_config(); send_notice(connection_, event.nick, "ack"); + BOOST_LOG_TRIVIAL(info) << "Priv added. by:" << event.nick << " oper:" << name << " priv:" << priv; return; } else if ("account" == kind) @@ -100,6 +96,7 @@ auto PrivThread::on_command(CommandEvent& event) -> void account_privs_[name].insert(priv); save_config(); send_notice(connection_, event.nick, "ack"); + BOOST_LOG_TRIVIAL(info) << "Priv added. by:" << event.nick << " account:" << name << " priv:" << priv; return; } } @@ -116,18 +113,34 @@ auto PrivThread::on_command(CommandEvent& event) -> void { if ("oper" == kind) { - oper_privs_[name].erase(priv); - if (oper_privs_[name].empty()) oper_privs_.erase(name); + if (wildcard == priv) + { + oper_privs_.erase(name); + } + else + { + oper_privs_[name].erase(priv); + if (oper_privs_[name].empty()) oper_privs_.erase(name); + } save_config(); send_notice(connection_, event.nick, "ack"); + BOOST_LOG_TRIVIAL(info) << "Priv removed. by:" << event.nick << " oper:" << name << " priv:" << priv; return; } else if ("account" == kind) { - account_privs_[name].erase(priv); - if (account_privs_[name].empty()) account_privs_.erase(name); + if (wildcard == priv) + { + account_privs_.erase(name); + } + else + { + account_privs_[name].erase(priv); + if (account_privs_[name].empty()) account_privs_.erase(name); + } save_config(); send_notice(connection_, event.nick, "ack"); + BOOST_LOG_TRIVIAL(info) << "Priv removed. by:" << event.nick << " account:" << name << " priv:" << priv; return; } } @@ -138,27 +151,26 @@ auto PrivThread::on_command(CommandEvent& event) -> void auto PrivThread::save_config() -> void { - auto serialize_table = [](auto map) { - auto tab = toml::table{}; - for (auto const& [oper, privs] : map) - { - auto privset = toml::array{}; - for (auto const& priv : privs) + 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) { - 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{}; - if (not oper_privs_.empty()) { - config.insert("oper", serialize_table(oper_privs_)); - } - if (not account_privs_.empty()) { - config.insert("account", serialize_table(account_privs_)); - } + serialize_table("oper", oper_privs_); + serialize_table("account", account_privs_); std::string tmp = config_path_ + ".tmp"; std::ofstream{tmp} << config; diff --git a/priv_thread.hpp b/priv_thread.hpp index 7fde165..5f7b48c 100644 --- a/priv_thread.hpp +++ b/priv_thread.hpp @@ -17,11 +17,6 @@ class PrivThread : std::enable_shared_from_this 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; auto load_config() -> void; auto save_config() -> void; diff --git a/snote_thread.cpp b/snote_thread.cpp index 6a74427..5ccc3db 100644 --- a/snote_thread.cpp +++ b/snote_thread.cpp @@ -7,6 +7,8 @@ #include +#include + #include #include #include @@ -149,7 +151,7 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr switch (scan_result) { case HS_SUCCESS: - std::cout << "Unknown snote: " << message << std::endl; + BOOST_LOG_TRIVIAL(warning) << "Unknown snote: " << message; break; case HS_SCAN_TERMINATED: