initial logging
This commit is contained in:
parent
5f0eb57e83
commit
7e4346a50e
|
@ -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)
|
||||||
|
|
|
@ -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));
|
||||||
|
|
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
3
main.cpp
3
main.cpp
|
@ -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>
|
||||||
|
|
|
@ -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
|
||||||
{
|
{
|
||||||
|
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
|
return
|
||||||
(not event.account.empty() &&
|
(not name.empty() &&
|
||||||
(check(account_privs_, priv, wildcard) ||
|
(check_priv(map, wildcard) ||
|
||||||
check(account_privs_, priv, std::string{event.account}))) ||
|
check_priv(map, std::string{name})));
|
||||||
(not event.oper.empty() &&
|
};
|
||||||
(check(oper_privs_, priv, wildcard) ||
|
return check(oper_privs_, event.oper) || check(account_privs_, event.account);
|
||||||
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 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,19 +112,35 @@ auto PrivThread::on_command(CommandEvent& event) -> void
|
||||||
if (in >> kind >> name >> priv)
|
if (in >> kind >> name >> priv)
|
||||||
{
|
{
|
||||||
if ("oper" == kind)
|
if ("oper" == kind)
|
||||||
|
{
|
||||||
|
if (wildcard == priv)
|
||||||
|
{
|
||||||
|
oper_privs_.erase(name);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
oper_privs_[name].erase(priv);
|
oper_privs_[name].erase(priv);
|
||||||
if (oper_privs_[name].empty()) oper_privs_.erase(name);
|
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)
|
||||||
|
{
|
||||||
|
if (wildcard == priv)
|
||||||
|
{
|
||||||
|
account_privs_.erase(name);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
account_privs_[name].erase(priv);
|
account_privs_[name].erase(priv);
|
||||||
if (account_privs_[name].empty()) account_privs_.erase(name);
|
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,7 +151,10 @@ 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 serialize_table = [&config](auto name, auto& map) {
|
||||||
|
if (not map.empty()) {
|
||||||
auto tab = toml::table{};
|
auto tab = toml::table{};
|
||||||
for (auto const& [oper, privs] : map)
|
for (auto const& [oper, privs] : map)
|
||||||
{
|
{
|
||||||
|
@ -149,16 +165,12 @@ auto PrivThread::save_config() -> void
|
||||||
}
|
}
|
||||||
tab.insert(oper, privset);
|
tab.insert(oper, privset);
|
||||||
}
|
}
|
||||||
return tab;
|
config.insert(name, 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;
|
||||||
|
|
|
@ -18,11 +18,6 @@ class PrivThread : std::enable_shared_from_this<PrivThread>
|
||||||
|
|
||||||
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;
|
||||||
auto list_privs(std::string_view nick) -> void;
|
auto list_privs(std::string_view nick) -> void;
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user