Compare commits
No commits in common. "61bd4b558ea56c2735662cdff21562583335c971" and "7c6ef18a6cdbfb51e6aeb5aa43a40d1f440b0ff2" have entirely different histories.
61bd4b558e
...
7c6ef18a6c
|
@ -39,7 +39,7 @@ add_custom_command(
|
||||||
|
|
||||||
add_executable(xbot
|
add_executable(xbot
|
||||||
main.cpp irc_commands.inc ircmsg.cpp settings.cpp connection.cpp
|
main.cpp irc_commands.inc ircmsg.cpp settings.cpp connection.cpp
|
||||||
snote_thread.cpp watchdog_thread.cpp write_irc.cpp
|
thread.cpp snote_thread.cpp watchdog_thread.cpp write_irc.cpp
|
||||||
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)
|
self_thread.cpp command_thread.cpp)
|
||||||
target_include_directories(xbot PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
|
target_include_directories(xbot PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
template <typename> struct CCallback_;
|
|
||||||
template <typename F, typename R, typename... Ts>
|
|
||||||
struct CCallback_<R (F::*) (Ts...) const>
|
|
||||||
{
|
|
||||||
static R invoke(Ts... args, void* u)
|
|
||||||
{
|
|
||||||
return (*reinterpret_cast<F*>(u))(args...);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
using CCallback = CCallback_<decltype(&F::operator())>;
|
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "event.hpp"
|
#include "thread.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,6 @@ 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;
|
|
||||||
make_event<LineEvent>(line);
|
make_event<LineEvent>(line);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -91,7 +90,6 @@ 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;
|
|
||||||
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));
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "event.hpp"
|
|
||||||
#include "linebuffer.hpp"
|
#include "linebuffer.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
|
#include "thread.hpp"
|
||||||
|
|
||||||
#include <eventpp/eventdispatcher.h>
|
#include <eventpp/eventdispatcher.h>
|
||||||
#include <eventpp/utilities/argumentadapter.h>
|
#include <eventpp/utilities/argumentadapter.h>
|
||||||
|
|
|
@ -162,7 +162,7 @@ struct RecognizedCommand {
|
||||||
438, IrcCommand::ERR_NICKTOOFAST
|
438, IrcCommand::ERR_NICKTOOFAST
|
||||||
440, IrcCommand::ERR_SERVICESDOWN
|
440, IrcCommand::ERR_SERVICESDOWN
|
||||||
441, IrcCommand::ERR_USERNOTINCHANNEL
|
441, IrcCommand::ERR_USERNOTINCHANNEL
|
||||||
442, IrcCommand::ERR_NOTONCHANNEL, 3, 3
|
442, IrcCommand::ERR_NOTONCHANNEL
|
||||||
443, IrcCommand::ERR_USERONCHANNEL
|
443, IrcCommand::ERR_USERONCHANNEL
|
||||||
444, IrcCommand::ERR_NOLOGIN
|
444, IrcCommand::ERR_NOLOGIN
|
||||||
445, IrcCommand::ERR_SUMMONDISABLED
|
445, IrcCommand::ERR_SUMMONDISABLED
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
auto IrcParseThread::start(Connection& connection) -> void
|
auto irc_parse_thread(Connection& connection) -> void
|
||||||
{
|
{
|
||||||
connection.add_listener<LineEvent>([&connection](LineEvent const& event)
|
connection.add_listener<LineEvent>([&connection](LineEvent const& event)
|
||||||
{
|
{
|
||||||
|
@ -21,11 +21,6 @@ auto IrcParseThread::start(Connection& connection) -> void
|
||||||
&& recognized->min_args <= msg.args.size()
|
&& recognized->min_args <= msg.args.size()
|
||||||
&& recognized->max_args >= msg.args.size()
|
&& recognized->max_args >= msg.args.size()
|
||||||
? recognized->command : IrcCommand::UNKNOWN;
|
? recognized->command : IrcCommand::UNKNOWN;
|
||||||
|
|
||||||
if (IrcCommand::UNKNOWN == command)
|
|
||||||
{
|
|
||||||
std::cout << "Unrecognized command: " << msg.command << " " << msg.args.size() << std::endl;
|
|
||||||
}
|
|
||||||
connection.make_event<IrcMsgEvent>(command, msg);
|
connection.make_event<IrcMsgEvent>(command, msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "event.hpp"
|
#include "thread.hpp"
|
||||||
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
@ -293,7 +293,4 @@ struct IrcMsgEvent : Event
|
||||||
IrcMsg const& irc;
|
IrcMsg const& irc;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct IrcParseThread
|
auto irc_parse_thread(Connection& connection) -> void;
|
||||||
{
|
|
||||||
static auto start(Connection& connection) -> void;
|
|
||||||
};
|
|
||||||
|
|
47
main.cpp
47
main.cpp
|
@ -1,19 +1,19 @@
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "event.hpp"
|
|
||||||
#include "ircmsg.hpp"
|
#include "ircmsg.hpp"
|
||||||
#include "linebuffer.hpp"
|
#include "linebuffer.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
|
#include "thread.hpp"
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
#include "command_thread.hpp"
|
|
||||||
#include "irc_parse_thread.hpp"
|
#include "irc_parse_thread.hpp"
|
||||||
#include "ping_thread.hpp"
|
|
||||||
#include "registration_thread.hpp"
|
#include "registration_thread.hpp"
|
||||||
#include "self_thread.hpp"
|
#include "ping_thread.hpp"
|
||||||
#include "snote_thread.hpp"
|
|
||||||
#include "watchdog_thread.hpp"
|
#include "watchdog_thread.hpp"
|
||||||
|
#include "snote_thread.hpp"
|
||||||
|
#include "self_thread.hpp"
|
||||||
|
#include "command_thread.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
@ -34,6 +34,23 @@
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
auto unhandled_message_thread(Connection& connection) -> void
|
||||||
|
{
|
||||||
|
connection.add_listener<IrcMsgEvent>([](IrcMsgEvent const& event)
|
||||||
|
{
|
||||||
|
if (IrcCommand::UNKNOWN == event.command)
|
||||||
|
{
|
||||||
|
auto& irc = event.irc;
|
||||||
|
std::cout << "Unknown message " << irc.command;
|
||||||
|
for (auto const arg : irc.args)
|
||||||
|
{
|
||||||
|
std::cout << " " << arg;
|
||||||
|
}
|
||||||
|
std::cout << "\n";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
auto echo_thread(Connection& connection) -> void
|
auto echo_thread(Connection& connection) -> void
|
||||||
{
|
{
|
||||||
connection.add_listener<CommandEvent>([&connection](CommandEvent& event)
|
connection.add_listener<CommandEvent>([&connection](CommandEvent& event)
|
||||||
|
@ -42,7 +59,9 @@ auto echo_thread(Connection& connection) -> void
|
||||||
and "glguy" == event.oper
|
and "glguy" == event.oper
|
||||||
and "glguy" == event.account)
|
and "glguy" == event.account)
|
||||||
{
|
{
|
||||||
connection.write_line(std::string{event.arg});
|
auto txt = std::string{event.arg};
|
||||||
|
txt += "\r\n";
|
||||||
|
connection.write_line(std::move(txt));
|
||||||
event.handled_ = true;
|
event.handled_ = true;
|
||||||
send_notice(connection, event.nick, "ack");
|
send_notice(connection, event.nick, "ack");
|
||||||
}
|
}
|
||||||
|
@ -53,14 +72,20 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void
|
||||||
{
|
{
|
||||||
auto connection = std::make_shared<Connection>(io);
|
auto connection = std::make_shared<Connection>(io);
|
||||||
|
|
||||||
WatchdogThread::start(*connection);
|
watchdog_thread(*connection);
|
||||||
IrcParseThread::start(*connection);
|
irc_parse_thread(*connection);
|
||||||
PingThread::start(*connection);
|
ping_thread(*connection);
|
||||||
SelfThread::start(*connection);
|
auto const self_thread = SelfThread::start(*connection);
|
||||||
RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname);
|
registration_thread(*connection, settings.password, settings.username, settings.realname, settings.nickname);
|
||||||
SnoteThread::start(*connection);
|
SnoteThread::start(*connection);
|
||||||
CommandThread::start(*connection);
|
CommandThread::start(*connection);
|
||||||
echo_thread(*connection);
|
echo_thread(*connection);
|
||||||
|
unhandled_message_thread(*connection);
|
||||||
|
|
||||||
|
connection->add_listener<SnoteEvent>([](SnoteEvent& event)
|
||||||
|
{
|
||||||
|
std::cout << "Snote match " << static_cast<int>(event.tag) << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
boost::asio::co_spawn(
|
boost::asio::co_spawn(
|
||||||
io,
|
io,
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "irc_parse_thread.hpp"
|
#include "irc_parse_thread.hpp"
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
auto PingThread::start(Connection& connection) -> void
|
auto ping_thread(Connection& connection) -> void
|
||||||
{
|
{
|
||||||
connection.add_listener<IrcMsgEvent>([&connection](IrcMsgEvent& event)
|
connection.add_listener<IrcMsgEvent>([&connection](IrcMsgEvent& event)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "event.hpp"
|
#include "thread.hpp"
|
||||||
|
|
||||||
struct PingThread
|
auto ping_thread(Connection& connection) -> void;
|
||||||
{
|
|
||||||
static auto start(Connection& connection) -> void;
|
|
||||||
};
|
|
||||||
|
|
|
@ -4,6 +4,48 @@
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
struct RegistrationThread : std::enable_shared_from_this<RegistrationThread>
|
||||||
|
{
|
||||||
|
Connection& connection_;
|
||||||
|
std::string password_;
|
||||||
|
std::string username_;
|
||||||
|
std::string realname_;
|
||||||
|
std::string nickname_;
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::string> caps;
|
||||||
|
std::unordered_set<std::string> outstanding;
|
||||||
|
|
||||||
|
Connection::Handle<ConnectEvent> connect_handle_;
|
||||||
|
Connection::Handle<IrcMsgEvent> message_handle_;
|
||||||
|
|
||||||
|
enum class Stage
|
||||||
|
{
|
||||||
|
LsReply,
|
||||||
|
AckReply,
|
||||||
|
};
|
||||||
|
|
||||||
|
Stage stage_;
|
||||||
|
|
||||||
|
RegistrationThread(
|
||||||
|
Connection& connection_,
|
||||||
|
std::string password,
|
||||||
|
std::string username,
|
||||||
|
std::string realname,
|
||||||
|
std::string nickname
|
||||||
|
);
|
||||||
|
|
||||||
|
auto on_connect() -> void;
|
||||||
|
|
||||||
|
auto send_req() -> void;
|
||||||
|
|
||||||
|
auto capack(IrcMsg const& msg) -> void;
|
||||||
|
|
||||||
|
auto capls(IrcMsg const& msg) -> void;
|
||||||
|
|
||||||
|
auto on_msg(IrcMsg const& msg) -> void;
|
||||||
|
};
|
||||||
|
|
||||||
RegistrationThread::RegistrationThread(
|
RegistrationThread::RegistrationThread(
|
||||||
Connection& connection,
|
Connection& connection,
|
||||||
std::string password,
|
std::string password,
|
||||||
|
@ -16,8 +58,8 @@ RegistrationThread::RegistrationThread(
|
||||||
, username_{username}
|
, username_{username}
|
||||||
, realname_{realname}
|
, realname_{realname}
|
||||||
, nickname_{nickname}
|
, nickname_{nickname}
|
||||||
{
|
, stage_{Stage::LsReply}
|
||||||
}
|
{}
|
||||||
|
|
||||||
auto RegistrationThread::on_connect() -> void
|
auto RegistrationThread::on_connect() -> void
|
||||||
{
|
{
|
||||||
|
@ -58,23 +100,23 @@ auto RegistrationThread::send_req() -> void
|
||||||
outstanding.insert(cap);
|
outstanding.insert(cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
connection_.remove_listener(message_handle_);
|
|
||||||
|
|
||||||
if (not outstanding.empty())
|
if (not outstanding.empty())
|
||||||
{
|
{
|
||||||
request.pop_back();
|
request.pop_back();
|
||||||
send_cap_req(connection_, request);
|
send_cap_req(connection_, request);
|
||||||
|
stage_ = Stage::AckReply;
|
||||||
listen_for_cap_ack();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
send_cap_end(connection_);
|
send_cap_end(connection_);
|
||||||
|
connection_.remove_listener(message_handle_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto RegistrationThread::on_msg_cap_ack(IrcMsg const& msg) -> void
|
auto RegistrationThread::capack(IrcMsg const& msg) -> void
|
||||||
|
{
|
||||||
|
auto const n = msg.args.size();
|
||||||
|
if (n >= 2 && "*" == msg.args[0] && "ACK" == msg.args[1])
|
||||||
{
|
{
|
||||||
auto in = std::istringstream{std::string{msg.args[2]}};
|
auto in = std::istringstream{std::string{msg.args[2]}};
|
||||||
std::for_each(
|
std::for_each(
|
||||||
|
@ -90,18 +132,22 @@ auto RegistrationThread::on_msg_cap_ack(IrcMsg const& msg) -> void
|
||||||
connection_.remove_listener(message_handle_);
|
connection_.remove_listener(message_handle_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto RegistrationThread::on_msg_cap_ls(IrcMsg const& msg) -> void
|
auto RegistrationThread::capls(IrcMsg const& msg) -> void
|
||||||
|
{
|
||||||
|
auto const n = msg.args.size();
|
||||||
|
if (n >= 2 && "*" == msg.args[0] && "LS" == msg.args[1])
|
||||||
{
|
{
|
||||||
std::string_view const* kvs;
|
std::string_view const* kvs;
|
||||||
bool last;
|
bool last;
|
||||||
|
|
||||||
if (3 == msg.args.size())
|
if (3 == n)
|
||||||
{
|
{
|
||||||
kvs = &msg.args[2];
|
kvs = &msg.args[2];
|
||||||
last = true;
|
last = true;
|
||||||
}
|
}
|
||||||
else if (4 == msg.args.size() && "*" == msg.args[2])
|
else if (4 == n && "*" == msg.args[2])
|
||||||
{
|
{
|
||||||
kvs = &msg.args[3];
|
kvs = &msg.args[3];
|
||||||
last = false;
|
last = false;
|
||||||
|
@ -134,45 +180,37 @@ auto RegistrationThread::on_msg_cap_ls(IrcMsg const& msg) -> void
|
||||||
send_req();
|
send_req();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto RegistrationThread::start(
|
auto RegistrationThread::on_msg(IrcMsg const& msg) -> void
|
||||||
|
{
|
||||||
|
switch (stage_)
|
||||||
|
{
|
||||||
|
case Stage::LsReply: capls(msg); return;
|
||||||
|
case Stage::AckReply: capack(msg); return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
auto registration_thread(
|
||||||
Connection& connection,
|
Connection& connection,
|
||||||
std::string password,
|
std::string password,
|
||||||
std::string username,
|
std::string username,
|
||||||
std::string realname,
|
std::string realname,
|
||||||
std::string nickname
|
std::string nickname
|
||||||
) -> std::shared_ptr<RegistrationThread>
|
) -> void
|
||||||
{
|
{
|
||||||
auto const thread = std::make_shared<RegistrationThread>(connection, password, username, realname, nickname);
|
auto const thread = std::make_shared<RegistrationThread>(connection, password, username, realname, nickname);
|
||||||
|
thread->message_handle_ = connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent const& event)
|
||||||
thread->listen_for_cap_ls();
|
{
|
||||||
|
if (IrcCommand::CAP == event.command)
|
||||||
|
{
|
||||||
|
thread->on_msg(event.irc);
|
||||||
|
}
|
||||||
|
});
|
||||||
thread->connect_handle_ = connection.add_listener<ConnectEvent>([thread](ConnectEvent const&)
|
thread->connect_handle_ = connection.add_listener<ConnectEvent>([thread](ConnectEvent const&)
|
||||||
{
|
{
|
||||||
thread->on_connect();
|
thread->on_connect();
|
||||||
});
|
});
|
||||||
|
|
||||||
return thread;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto RegistrationThread::listen_for_cap_ack() -> void
|
|
||||||
{
|
|
||||||
message_handle_ = connection_.add_listener<IrcMsgEvent>([thread = shared_from_this()](IrcMsgEvent const& event)
|
|
||||||
{
|
|
||||||
if (IrcCommand::CAP == event.command && event.irc.args.size() >= 2 && "*" == event.irc.args[0] && "ACK" == event.irc.args[1])
|
|
||||||
{
|
|
||||||
thread->on_msg_cap_ack(event.irc);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
auto RegistrationThread::listen_for_cap_ls() -> void
|
|
||||||
{
|
|
||||||
message_handle_ = connection_.add_listener<IrcMsgEvent>([thread = shared_from_this()](IrcMsgEvent const& event)
|
|
||||||
{
|
|
||||||
if (IrcCommand::CAP == event.command && event.irc.args.size() >= 2 && "*" == event.irc.args[0] && "LS" == event.irc.args[1])
|
|
||||||
{
|
|
||||||
thread->on_msg_cap_ls(event.irc);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,53 +1,18 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "thread.hpp"
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "event.hpp"
|
|
||||||
#include "irc_parse_thread.hpp"
|
#include "irc_parse_thread.hpp"
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
#include <eventpp/eventdispatcher.h>
|
#include <eventpp/eventdispatcher.h>
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
|
||||||
#include <unordered_set>
|
|
||||||
|
|
||||||
class RegistrationThread : public std::enable_shared_from_this<RegistrationThread>
|
auto registration_thread(
|
||||||
{
|
|
||||||
Connection& connection_;
|
|
||||||
std::string password_;
|
|
||||||
std::string username_;
|
|
||||||
std::string realname_;
|
|
||||||
std::string nickname_;
|
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> caps;
|
|
||||||
std::unordered_set<std::string> outstanding;
|
|
||||||
|
|
||||||
Connection::Handle<ConnectEvent> connect_handle_;
|
|
||||||
Connection::Handle<IrcMsgEvent> message_handle_;
|
|
||||||
|
|
||||||
auto on_connect() -> void;
|
|
||||||
auto send_req() -> void;
|
|
||||||
auto on_msg_cap_ls(IrcMsg const& msg) -> void;
|
|
||||||
auto on_msg_cap_ack(IrcMsg const& msg) -> void;
|
|
||||||
|
|
||||||
auto listen_for_cap_ack() -> void;
|
|
||||||
auto listen_for_cap_ls() -> void;
|
|
||||||
|
|
||||||
public:
|
|
||||||
RegistrationThread(
|
|
||||||
Connection& connection_,
|
|
||||||
std::string password,
|
|
||||||
std::string username,
|
|
||||||
std::string realname,
|
|
||||||
std::string nickname
|
|
||||||
);
|
|
||||||
|
|
||||||
static auto start(
|
|
||||||
Connection& connection,
|
Connection& connection,
|
||||||
std::string password,
|
std::string password,
|
||||||
std::string username,
|
std::string username,
|
||||||
std::string realname,
|
std::string realname,
|
||||||
std::string nickname
|
std::string nickname
|
||||||
) -> std::shared_ptr<RegistrationThread>;
|
) -> void;
|
||||||
};
|
|
||||||
|
|
|
@ -15,3 +15,4 @@ public:
|
||||||
SelfThread(Connection& connection) : connection_{connection} {}
|
SelfThread(Connection& connection) : connection_{connection} {}
|
||||||
static auto start(Connection&) -> std::shared_ptr<SelfThread>;
|
static auto start(Connection&) -> std::shared_ptr<SelfThread>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
106
snote_thread.cpp
106
snote_thread.cpp
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
#include "irc_parse_thread.hpp"
|
#include "irc_parse_thread.hpp"
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "c_callback.hpp"
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
@ -17,12 +16,14 @@ struct SnotePattern
|
||||||
SnotePattern(SnoteTag tag, char const* expression, unsigned flags = 0)
|
SnotePattern(SnoteTag tag, char const* expression, unsigned flags = 0)
|
||||||
: tag{tag}
|
: tag{tag}
|
||||||
, expression{expression}
|
, expression{expression}
|
||||||
|
, flags{flags}
|
||||||
, regex{expression, std::regex_constants::ECMAScript | std::regex_constants::optimize}
|
, regex{expression, std::regex_constants::ECMAScript | std::regex_constants::optimize}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SnoteTag tag;
|
SnoteTag tag;
|
||||||
char const* expression;
|
char const* expression;
|
||||||
|
unsigned flags;
|
||||||
std::regex regex;
|
std::regex regex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -33,61 +34,31 @@ SnotePattern const patterns[] =
|
||||||
|
|
||||||
{SnoteTag::ClientExiting,
|
{SnoteTag::ClientExiting,
|
||||||
R"(^Client exiting: ([^ ]+) \(([^@ ]+)@([^) ]+)\) \[(.*)\] \[(.*)\]$)"},
|
R"(^Client exiting: ([^ ]+) \(([^@ ]+)@([^) ]+)\) \[(.*)\] \[(.*)\]$)"},
|
||||||
|
|
||||||
{SnoteTag::RejectingKlined,
|
|
||||||
R"(^Rejecting K-Lined user ([^ ]+)\[([^@]+)@([^\]]+)\] \[([^\] ]+)\] \((.*)\)$)"},
|
|
||||||
|
|
||||||
{SnoteTag::NickChange,
|
|
||||||
R"(^Nick change: From ([^ ]+) to ([^ ]+) \[([^@]+)@([^ ]+)\]$)"},
|
|
||||||
|
|
||||||
{SnoteTag::CreateChannel,
|
|
||||||
R"(^([^ ]+) is creating new channel ([^ ]+)$)"},
|
|
||||||
|
|
||||||
{SnoteTag::TemporaryKlineExpired,
|
|
||||||
R"(^Temporary K-line for \[([^ ]+)\] expired$)"},
|
|
||||||
|
|
||||||
{SnoteTag::PropagatedBanExpired,
|
|
||||||
R"(^Propagated ban for \[([^ ]+)\] expired$)"},
|
|
||||||
|
|
||||||
{SnoteTag::DisconnectingKlined,
|
|
||||||
R"(^Disconnecting K-Lined user ([^ ]+)\[([^@]+)@([^ ]+)\] \((.*)\)$)"},
|
|
||||||
|
|
||||||
{SnoteTag::NewPropagatedKline,
|
|
||||||
R"(^([^ ]+)!([^ ]+)@([^ ]+)\{([^ ]+)\} added global ([^ ]+) min\. K-Line for \[([^ ]+)\] \[(.*)\]$)"},
|
|
||||||
|
|
||||||
{SnoteTag::NewTemporaryKline,
|
|
||||||
R"(^([^ ]+)!([^ ]+)@([^ ]+)\{([^ ]+)\} added temporary ([^ ]+) min\. K-Line for \[([^ ]+)\] \[(.*)\]$)"},
|
|
||||||
|
|
||||||
{SnoteTag::LoginAttempts,
|
|
||||||
"^Warning: \x02([^ ]+)\x02 failed login attempts to \x02([^ ]+)\x02\\. Last attempt received from \x02(.+)\x02.*$"},
|
|
||||||
|
|
||||||
{SnoteTag::PossibleFlooder,
|
|
||||||
R"(^Possible Flooder ([^ ]+)\[([^ ]+)@[^ ]+\] on ([^ ]+) target: ([^ ]+)$)"},
|
|
||||||
|
|
||||||
{SnoteTag::Killed,
|
|
||||||
R"(^Received KILL message for ([^ ]+)!([^ ]+)@([^ ]+)\. From ([^ ]+) Path: ([^ ]+) \((.*)\)$)"},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto setup_database() -> hs_database_t*
|
auto setup_database() -> std::unique_ptr<hs_database_t, SnoteThread::DbDeleter>
|
||||||
{
|
{
|
||||||
auto const n = std::size(patterns);
|
|
||||||
std::vector<char const*> expressions;
|
std::vector<char const*> expressions;
|
||||||
std::vector<unsigned> flags(n, HS_FLAG_SINGLEMATCH);
|
std::vector<unsigned> flags;
|
||||||
std::vector<unsigned> ids;
|
std::vector<unsigned> ids;
|
||||||
|
|
||||||
expressions.reserve(n);
|
expressions.reserve(std::size(patterns));
|
||||||
ids.reserve(n);
|
flags.reserve(std::size(patterns));
|
||||||
|
ids.reserve(std::size(patterns));
|
||||||
|
|
||||||
for (std::size_t i = 0; i < n; i++)
|
unsigned id = 0;
|
||||||
|
|
||||||
|
for (auto const& pattern : patterns)
|
||||||
{
|
{
|
||||||
expressions.push_back(patterns[i].expression);
|
expressions.push_back(pattern.expression);
|
||||||
ids.push_back(i);
|
flags.push_back(pattern.flags);
|
||||||
|
ids.push_back(id++);
|
||||||
}
|
}
|
||||||
|
|
||||||
hs_database_t* db;
|
hs_database_t* db;
|
||||||
hs_compile_error *error;
|
hs_compile_error *error;
|
||||||
hs_platform_info_t *platform = nullptr; // target current platform
|
|
||||||
switch (hs_compile_multi(expressions.data(), flags.data(), ids.data(), expressions.size(), HS_MODE_BLOCK, platform, &db, &error))
|
switch (hs_compile_multi(expressions.data(), flags.data(), ids.data(), expressions.size(), HS_MODE_BLOCK, nullptr, &db, &error))
|
||||||
{
|
{
|
||||||
case HS_COMPILER_ERROR:
|
case HS_COMPILER_ERROR:
|
||||||
{
|
{
|
||||||
|
@ -100,7 +71,7 @@ auto setup_database() -> hs_database_t*
|
||||||
default:
|
default:
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
return db;
|
return std::unique_ptr<hs_database_t, SnoteThread::DbDeleter>{db};
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -109,47 +80,41 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
|
||||||
{
|
{
|
||||||
auto thread = std::make_shared<SnoteThread>();
|
auto thread = std::make_shared<SnoteThread>();
|
||||||
|
|
||||||
thread->db_.reset(setup_database());
|
thread->db_ = setup_database();
|
||||||
|
|
||||||
hs_scratch_t* scratch = nullptr;
|
hs_scratch_t* scratch = nullptr;
|
||||||
if (HS_SUCCESS != hs_alloc_scratch(thread->db_.get(), &scratch))
|
if (HS_SUCCESS != hs_alloc_scratch(thread->db_.get(), &scratch))
|
||||||
{
|
{
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
thread->scratch_.reset(scratch);
|
thread->scratch_ = std::unique_ptr<hs_scratch_t, ScratchDeleter>{scratch};
|
||||||
|
|
||||||
static char const* const prefix = "*** Notice -- ";
|
static char const* const prefix = "*** Notice -- ";
|
||||||
connection.add_listener<IrcMsgEvent>([&connection, thread](IrcMsgEvent& event)
|
connection.add_listener<IrcMsgEvent>([&connection, thread](IrcMsgEvent& event)
|
||||||
{
|
{
|
||||||
auto& args = event.irc.args;
|
auto& args = event.irc.args;
|
||||||
if (IrcCommand::NOTICE == event.command && "*" == args[0] && args[1].starts_with(prefix))
|
if (IrcCommand::NOTICE == event.command
|
||||||
|
&& "*" == args[0]
|
||||||
|
&& args[1].starts_with(prefix))
|
||||||
{
|
{
|
||||||
event.handled_ = true;
|
event.handled_ = true;
|
||||||
auto const message = args[1].substr(strlen(prefix));
|
auto message = args[1].substr(strlen(prefix));
|
||||||
|
unsigned int match_id = -1;
|
||||||
unsigned match_id;
|
auto const scan_result = hs_scan(thread->db_.get(), message.data(), message.size(), 0, thread->scratch_.get(),
|
||||||
auto cb = [&match_id](unsigned id, unsigned long long, unsigned long long, unsigned) -> int
|
[](unsigned int id, unsigned long long from, unsigned long long to, unsigned int flags, void *context) -> int
|
||||||
{
|
{
|
||||||
match_id = id;
|
int* const match_id = static_cast<int*>(context);
|
||||||
|
*match_id = id;
|
||||||
return 1; // stop scanning
|
return 1; // stop scanning
|
||||||
};
|
}
|
||||||
|
, &match_id);
|
||||||
|
|
||||||
auto const scan_result =
|
if (scan_result != HS_SUCCESS && scan_result != HS_SCAN_TERMINATED)
|
||||||
hs_scan(
|
|
||||||
thread->db_.get(),
|
|
||||||
message.data(), message.size(),
|
|
||||||
0, // no flags
|
|
||||||
thread->scratch_.get(),
|
|
||||||
CCallback<decltype(cb)>::invoke, &cb
|
|
||||||
);
|
|
||||||
|
|
||||||
switch (scan_result)
|
|
||||||
{
|
{
|
||||||
case HS_SUCCESS:
|
abort();
|
||||||
std::cout << "Unknown snote: " << message << std::endl;
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case HS_SCAN_TERMINATED:
|
if (match_id != -1)
|
||||||
{
|
{
|
||||||
auto& pattern = patterns[match_id];
|
auto& pattern = patterns[match_id];
|
||||||
std::match_results<std::string_view::const_iterator> results;
|
std::match_results<std::string_view::const_iterator> results;
|
||||||
|
@ -165,11 +130,6 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
|
||||||
parts.push_back(std::string_view{sub.first, sub.second});
|
parts.push_back(std::string_view{sub.first, sub.second});
|
||||||
}
|
}
|
||||||
connection.make_event<SnoteEvent>(pattern.tag, std::move(parts));
|
connection.make_event<SnoteEvent>(pattern.tag, std::move(parts));
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
abort();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "event.hpp"
|
#include "thread.hpp"
|
||||||
|
|
||||||
#include <hs.h>
|
#include <hs.h>
|
||||||
|
|
||||||
|
@ -12,17 +12,6 @@ enum class SnoteTag
|
||||||
{
|
{
|
||||||
ClientConnecting,
|
ClientConnecting,
|
||||||
ClientExiting,
|
ClientExiting,
|
||||||
RejectingKlined,
|
|
||||||
NickChange,
|
|
||||||
CreateChannel,
|
|
||||||
TemporaryKlineExpired,
|
|
||||||
PropagatedBanExpired,
|
|
||||||
DisconnectingKlined,
|
|
||||||
NewPropagatedKline,
|
|
||||||
NewTemporaryKline,
|
|
||||||
LoginAttempts,
|
|
||||||
PossibleFlooder,
|
|
||||||
Killed,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SnoteEvent : Event
|
struct SnoteEvent : Event
|
||||||
|
|
1
thread.cpp
Normal file
1
thread.cpp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
#include "thread.hpp"
|
|
@ -6,6 +6,6 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct Event {
|
struct Event {
|
||||||
virtual ~Event() = default;
|
virtual ~Event() {}
|
||||||
bool handled_ = false;
|
bool handled_ = false;
|
||||||
};
|
};
|
|
@ -11,20 +11,28 @@
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
WatchdogThread::WatchdogThread(Connection& connection)
|
namespace {
|
||||||
|
|
||||||
|
struct WatchdogThread : std::enable_shared_from_this<WatchdogThread>
|
||||||
|
{
|
||||||
|
WatchdogThread(Connection& connection)
|
||||||
: connection_{connection}
|
: connection_{connection}
|
||||||
, timer_{connection.get_executor()}
|
, timer_{connection.get_executor()}
|
||||||
, tried_ping{false}
|
, tried_ping{false}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
auto WatchdogThread::on_activity() -> void
|
Connection& connection_;
|
||||||
|
boost::asio::steady_timer timer_;
|
||||||
|
bool tried_ping;
|
||||||
|
|
||||||
|
auto on_activity() -> void
|
||||||
{
|
{
|
||||||
tried_ping = false;
|
tried_ping = false;
|
||||||
timer_.expires_from_now(30s);
|
timer_.expires_from_now(30s);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto WatchdogThread::timeout_token()
|
auto timeout_token()
|
||||||
{
|
{
|
||||||
return [weak = weak_from_this()](auto const& error)
|
return [weak = weak_from_this()](auto const& error)
|
||||||
{
|
{
|
||||||
|
@ -38,7 +46,7 @@ auto WatchdogThread::timeout_token()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto WatchdogThread::on_timeout() -> void
|
auto on_timeout() -> void
|
||||||
{
|
{
|
||||||
if (tried_ping)
|
if (tried_ping)
|
||||||
{
|
{
|
||||||
|
@ -53,18 +61,21 @@ auto WatchdogThread::on_timeout() -> void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto WatchdogThread::on_connect() -> void
|
auto on_connect() -> void
|
||||||
{
|
{
|
||||||
on_activity();
|
on_activity();
|
||||||
timer_.async_wait(timeout_token());
|
timer_.async_wait(timeout_token());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto WatchdogThread::on_disconnect() -> void
|
auto on_disconnect() -> void
|
||||||
{
|
{
|
||||||
timer_.cancel();
|
timer_.cancel();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
auto WatchdogThread::start(Connection& connection) -> void
|
} // namespace
|
||||||
|
|
||||||
|
auto watchdog_thread(Connection& connection) -> void
|
||||||
{
|
{
|
||||||
auto const thread = std::make_shared<WatchdogThread>(connection);
|
auto const thread = std::make_shared<WatchdogThread>(connection);
|
||||||
connection.add_listener<ConnectEvent>([thread](auto&)
|
connection.add_listener<ConnectEvent>([thread](auto&)
|
||||||
|
|
|
@ -1,24 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/asio/steady_timer.hpp>
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
class Connection;
|
class Connection;
|
||||||
|
auto watchdog_thread(Connection& connection) -> void;
|
||||||
class WatchdogThread : std::enable_shared_from_this<WatchdogThread>
|
|
||||||
{
|
|
||||||
Connection& connection_;
|
|
||||||
boost::asio::steady_timer timer_;
|
|
||||||
bool tried_ping;
|
|
||||||
|
|
||||||
auto on_activity() -> void;
|
|
||||||
auto timeout_token();
|
|
||||||
auto on_timeout() -> void;
|
|
||||||
auto on_connect() -> void;
|
|
||||||
auto on_disconnect() -> void;
|
|
||||||
|
|
||||||
public:
|
|
||||||
WatchdogThread(Connection& connection);
|
|
||||||
static auto start(Connection& connection) -> void;
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user