initial logging
This commit is contained in:
parent
5f0eb57e83
commit
7e4346a50e
@ -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)
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
#include "linebuffer.hpp"
|
||||
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
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<LineEvent>(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));
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "connection.hpp"
|
||||
#include "ircmsg.hpp"
|
||||
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
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<IrcMsgEvent>(command, msg);
|
||||
});
|
||||
|
3
main.cpp
3
main.cpp
@ -1,4 +1,3 @@
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "event.hpp"
|
||||
@ -16,6 +15,8 @@
|
||||
#include "watchdog_thread.hpp"
|
||||
#include "priv_thread.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
|
@ -6,7 +6,8 @@
|
||||
|
||||
#include <toml++/toml.hpp>
|
||||
|
||||
#include <cstdio>
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
@ -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<std::string, std::unordered_set<std::string>> 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;
|
||||
|
@ -17,11 +17,6 @@ class PrivThread : std::enable_shared_from_this<PrivThread>
|
||||
std::unordered_map<std::string, std::unordered_set<std::string>> account_privs_;
|
||||
|
||||
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 save_config() -> void;
|
||||
|
@ -7,6 +7,8 @@
|
||||
|
||||
#include <hs.h>
|
||||
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <regex>
|
||||
@ -149,7 +151,7 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
|
||||
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:
|
||||
|
Loading…
Reference in New Issue
Block a user