migrate to signals2
This commit is contained in:
parent
d11412e73f
commit
7665f4c0f5
@ -20,13 +20,6 @@ FetchContent_Declare(
|
|||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(tomlplusplus)
|
FetchContent_MakeAvailable(tomlplusplus)
|
||||||
|
|
||||||
FetchContent_Declare(
|
|
||||||
eventpp
|
|
||||||
GIT_REPOSITORY https://github.com/wqking/eventpp.git
|
|
||||||
GIT_TAG v0.1.3
|
|
||||||
)
|
|
||||||
FetchContent_MakeAvailable(eventpp)
|
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT irc_commands.inc
|
OUTPUT irc_commands.inc
|
||||||
COMMAND
|
COMMAND
|
||||||
@ -41,9 +34,14 @@ add_subdirectory(mybase64)
|
|||||||
|
|
||||||
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
|
write_irc.cpp
|
||||||
ping_thread.cpp irc_parse_thread.cpp registration_thread.cpp
|
registration_thread.cpp
|
||||||
self_thread.cpp command_thread.cpp priv_thread.cpp
|
ping_thread.cpp
|
||||||
sasl_thread.cpp)
|
snote_thread.cpp
|
||||||
|
self_thread.cpp
|
||||||
|
watchdog_thread.cpp
|
||||||
|
)
|
||||||
|
# command_thread.cpp priv_thread.cpp
|
||||||
|
# sasl_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::log Boost::headers tomlplusplus_tomlplusplus eventpp PkgConfig::LIBHS mybase64)
|
target_link_libraries(xbot PRIVATE Boost::log Boost::headers tomlplusplus_tomlplusplus PkgConfig::LIBHS mybase64)
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
#include "irc_commands.inc"
|
||||||
|
} // namespace
|
||||||
|
|
||||||
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()}
|
||||||
@ -72,7 +76,7 @@ auto Connection::connect(
|
|||||||
auto const endpoints = co_await resolver.async_resolve(host, port, boost::asio::use_awaitable);
|
auto const endpoints = co_await resolver.async_resolve(host, port, boost::asio::use_awaitable);
|
||||||
auto const endpoint = co_await boost::asio::async_connect(stream_, endpoints, boost::asio::use_awaitable);
|
auto const endpoint = co_await boost::asio::async_connect(stream_, endpoints, boost::asio::use_awaitable);
|
||||||
|
|
||||||
make_event<ConnectEvent>();
|
sig_connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
self->writer();
|
self->writer();
|
||||||
@ -86,11 +90,24 @@ auto Connection::connect(
|
|||||||
}
|
}
|
||||||
buffer.add_bytes(n, [this](char * line) {
|
buffer.add_bytes(n, [this](char * line) {
|
||||||
BOOST_LOG_TRIVIAL(debug) << "RECV: " << line;
|
BOOST_LOG_TRIVIAL(debug) << "RECV: " << line;
|
||||||
make_event<LineEvent>(line);
|
|
||||||
|
auto const msg = parse_irc_message(line);
|
||||||
|
auto const recognized = IrcCommandHash::in_word_set(msg.command.data(), msg.command.size());
|
||||||
|
auto const command
|
||||||
|
= recognized
|
||||||
|
&& recognized->min_args <= msg.args.size()
|
||||||
|
&& recognized->max_args >= msg.args.size()
|
||||||
|
? recognized->command : IrcCommand::UNKNOWN;
|
||||||
|
|
||||||
|
if (IrcCommand::UNKNOWN == command)
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(warning) << "Unrecognized command: " << msg.command << " " << msg.args.size();
|
||||||
|
}
|
||||||
|
sig_ircmsg(command, msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
make_event<DisconnectEvent>();
|
sig_disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Connection::write_line(std::string message) -> void
|
auto Connection::write_line(std::string message) -> void
|
||||||
|
@ -3,10 +3,8 @@
|
|||||||
#include "event.hpp"
|
#include "event.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
|
|
||||||
#include <eventpp/eventdispatcher.h>
|
|
||||||
#include <eventpp/utilities/argumentadapter.h>
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
#include <boost/signals2.hpp>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -20,43 +18,16 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <typeinfo>
|
|
||||||
#include <typeindex>
|
|
||||||
|
|
||||||
struct ConnectEvent : Event
|
#include "ircmsg.hpp"
|
||||||
{
|
#include "irc_command.hpp"
|
||||||
};
|
|
||||||
|
|
||||||
struct DisconnectEvent : Event
|
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
struct LineEvent : Event
|
|
||||||
{
|
|
||||||
explicit LineEvent(char * line) : line{line} {}
|
|
||||||
char * line;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Connection : public std::enable_shared_from_this<Connection>
|
class Connection : public std::enable_shared_from_this<Connection>
|
||||||
{
|
{
|
||||||
using EventDispatcher = eventpp::EventDispatcher<std::type_index, void(Event&)>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
template <typename T>
|
|
||||||
class Handle
|
|
||||||
{
|
|
||||||
EventDispatcher::Handle handle;
|
|
||||||
Handle(EventDispatcher::Handle handle) : handle{handle} {}
|
|
||||||
public:
|
|
||||||
Handle() : handle{} {}
|
|
||||||
friend Connection;
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::asio::ip::tcp::socket stream_;
|
boost::asio::ip::tcp::socket stream_;
|
||||||
boost::asio::steady_timer write_timer_;
|
boost::asio::steady_timer write_timer_;
|
||||||
std::list<std::string> write_strings_;
|
std::list<std::string> write_strings_;
|
||||||
EventDispatcher dispatcher_;
|
|
||||||
|
|
||||||
auto writer() -> void;
|
auto writer() -> void;
|
||||||
auto writer_immediate() -> void;
|
auto writer_immediate() -> void;
|
||||||
@ -64,37 +35,14 @@ private:
|
|||||||
public:
|
public:
|
||||||
Connection(boost::asio::io_context & io);
|
Connection(boost::asio::io_context & io);
|
||||||
|
|
||||||
template <typename T, typename F>
|
boost::signals2::signal<void()> sig_connect;
|
||||||
auto add_listener(F f) -> Handle<T>
|
boost::signals2::signal<void()> sig_disconnect;
|
||||||
{
|
boost::signals2::signal<void(IrcCommand, const IrcMsg &)> sig_ircmsg;
|
||||||
return Handle<T>{dispatcher_.appendListener(
|
|
||||||
typeid(T),
|
|
||||||
eventpp::argumentAdapter<void(T&)>(f)
|
|
||||||
)};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto remove_listener(Handle<T> handle) -> void
|
|
||||||
{
|
|
||||||
dispatcher_.removeListener(typeid(T), handle.handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto dispatch(T& event) -> void
|
|
||||||
{
|
|
||||||
dispatcher_.dispatch(typeid(T), event);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto get_executor() -> boost::asio::any_io_executor {
|
auto get_executor() -> boost::asio::any_io_executor {
|
||||||
return stream_.get_executor();
|
return stream_.get_executor();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
|
||||||
auto make_event(Args&& ... args) {
|
|
||||||
auto event = T{std::forward<Args>(args)...};
|
|
||||||
dispatch<T>(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Write bytes into the socket. Messages should be properly newline terminated.
|
/// Write bytes into the socket. Messages should be properly newline terminated.
|
||||||
auto write_line(std::string message) -> void;
|
auto write_line(std::string message) -> void;
|
||||||
|
|
||||||
|
@ -1,12 +1,3 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "event.hpp"
|
|
||||||
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
class Connection;
|
|
||||||
class IrcMsg;
|
|
||||||
|
|
||||||
enum class IrcCommand
|
enum class IrcCommand
|
||||||
{
|
{
|
||||||
UNKNOWN,
|
UNKNOWN,
|
||||||
@ -284,15 +275,3 @@ enum class IrcCommand
|
|||||||
SETNAME,
|
SETNAME,
|
||||||
TOPIC,
|
TOPIC,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct IrcMsgEvent : Event
|
|
||||||
{
|
|
||||||
IrcMsgEvent(IrcCommand command, IrcMsg const& irc);
|
|
||||||
IrcCommand command;
|
|
||||||
IrcMsg const& irc;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct IrcParseThread
|
|
||||||
{
|
|
||||||
static auto start(Connection& connection) -> void;
|
|
||||||
};
|
|
@ -1,37 +0,0 @@
|
|||||||
#include "irc_parse_thread.hpp"
|
|
||||||
|
|
||||||
#include "connection.hpp"
|
|
||||||
#include "ircmsg.hpp"
|
|
||||||
|
|
||||||
#include <boost/log/trivial.hpp>
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
#include "irc_commands.inc"
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
IrcMsgEvent::IrcMsgEvent(IrcCommand command, IrcMsg const& irc)
|
|
||||||
: command{command}, irc{irc} {}
|
|
||||||
|
|
||||||
auto IrcParseThread::start(Connection& connection) -> void
|
|
||||||
{
|
|
||||||
connection.add_listener<LineEvent>([&connection](LineEvent const& event)
|
|
||||||
{
|
|
||||||
auto const msg = parse_irc_message(event.line);
|
|
||||||
auto const recognized = IrcCommandHash::in_word_set(msg.command.data(), msg.command.size());
|
|
||||||
auto const command
|
|
||||||
= recognized
|
|
||||||
&& recognized->min_args <= msg.args.size()
|
|
||||||
&& recognized->max_args >= msg.args.size()
|
|
||||||
? recognized->command : IrcCommand::UNKNOWN;
|
|
||||||
|
|
||||||
if (IrcCommand::UNKNOWN == command)
|
|
||||||
{
|
|
||||||
BOOST_LOG_TRIVIAL(warning) << "Unrecognized command: " << msg.command << " " << msg.args.size();
|
|
||||||
}
|
|
||||||
connection.make_event<IrcMsgEvent>(command, msg);
|
|
||||||
});
|
|
||||||
}
|
|
50
main.cpp
50
main.cpp
@ -6,15 +6,6 @@
|
|||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
#include "command_thread.hpp"
|
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
#include "ping_thread.hpp"
|
|
||||||
#include "registration_thread.hpp"
|
|
||||||
#include "self_thread.hpp"
|
|
||||||
#include "snote_thread.hpp"
|
|
||||||
#include "watchdog_thread.hpp"
|
|
||||||
#include "priv_thread.hpp"
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -34,46 +25,37 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
#include "ping_thread.hpp"
|
||||||
|
#include "registration_thread.hpp"
|
||||||
|
#include "self_thread.hpp"
|
||||||
|
#include "snote_thread.hpp"
|
||||||
|
|
||||||
auto echo_thread(
|
using namespace std::chrono_literals;
|
||||||
Connection& connection,
|
|
||||||
std::shared_ptr<PrivThread> priv_thread) -> void
|
|
||||||
{
|
|
||||||
connection.add_listener<CommandEvent>([&connection, priv_thread](CommandEvent& event)
|
|
||||||
{
|
|
||||||
if ("raw" == event.command && priv_thread->check_command(event, PrivThread::owner_priv))
|
|
||||||
{
|
|
||||||
connection.write_line(std::string{event.arg});
|
|
||||||
event.handled_ = true;
|
|
||||||
send_notice(connection, event.nick, "ack");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
auto start(boost::asio::io_context & io, Settings const& settings) -> void
|
auto start(boost::asio::io_context & io, Settings const& settings) -> void
|
||||||
{
|
{
|
||||||
auto const connection = std::make_shared<Connection>(io);
|
auto const connection = std::make_shared<Connection>(io);
|
||||||
|
|
||||||
WatchdogThread::start(*connection);
|
RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname);
|
||||||
IrcParseThread::start(*connection);
|
|
||||||
PingThread::start(*connection);
|
PingThread::start(*connection);
|
||||||
SelfThread::start(*connection);
|
SelfThread::start(*connection);
|
||||||
RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname);
|
auto const snote_thread = SnoteThread::start(*connection);
|
||||||
SnoteThread::start(*connection);
|
|
||||||
|
/*
|
||||||
|
WatchdogThread::start(*connection);
|
||||||
|
IrcParseThread::start(*connection);
|
||||||
CommandThread::start(*connection);
|
CommandThread::start(*connection);
|
||||||
auto const priv_thread = PrivThread::start(*connection, "privs.toml");
|
auto const priv_thread = PrivThread::start(*connection, "privs.toml");
|
||||||
echo_thread(*connection, priv_thread);
|
echo_thread(*connection, priv_thread);
|
||||||
|
|
||||||
/*
|
*/
|
||||||
connection->add_listener<SnoteEvent>([](SnoteEvent& event) {
|
snote_thread->sig_snote.connect([](auto tag, auto &match) {
|
||||||
std::cout << "SNOTE " << static_cast<int>(event.get_tag()) << std::endl;
|
std::cout << "SNOTE " << static_cast<int>(tag) << std::endl;
|
||||||
for (auto c : event.get_results())
|
for (auto c : match.get_results())
|
||||||
{
|
{
|
||||||
std::cout << " " << std::string_view{c.first, c.second} << std::endl;
|
std::cout << " " << std::string_view{c.first, c.second} << std::endl;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
*/
|
|
||||||
|
|
||||||
boost::asio::co_spawn(
|
boost::asio::co_spawn(
|
||||||
io,
|
io,
|
||||||
@ -81,7 +63,7 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void
|
|||||||
[&io, &settings](std::exception_ptr e)
|
[&io, &settings](std::exception_ptr e)
|
||||||
{
|
{
|
||||||
auto timer = std::make_shared<boost::asio::steady_timer>(io);
|
auto timer = std::make_shared<boost::asio::steady_timer>(io);
|
||||||
|
|
||||||
timer->expires_after(5s);
|
timer->expires_after(5s);
|
||||||
timer->async_wait([&io, &settings, timer](auto) {
|
timer->async_wait([&io, &settings, timer](auto) {
|
||||||
start(io, settings);
|
start(io, settings);
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
#include "ping_thread.hpp"
|
#include "ping_thread.hpp"
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
#include "ircmsg.hpp"
|
#include "ircmsg.hpp"
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
auto PingThread::start(Connection& connection) -> void
|
auto PingThread::start(Connection& connection) -> void
|
||||||
{
|
{
|
||||||
connection.add_listener<IrcMsgEvent>([&connection](IrcMsgEvent& event)
|
connection.sig_ircmsg.connect([&connection](auto cmd, auto& msg)
|
||||||
{
|
{
|
||||||
auto& irc = event.irc;
|
if (IrcCommand::PING == cmd)
|
||||||
if (IrcCommand::PING == event.command)
|
|
||||||
{
|
{
|
||||||
send_pong(connection, irc.args[0]);
|
send_pong(connection, msg.args[0]);
|
||||||
event.handled_ = true;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include "registration_thread.hpp"
|
#include "registration_thread.hpp"
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
#include "ircmsg.hpp"
|
#include "ircmsg.hpp"
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
@ -17,10 +16,10 @@ RegistrationThread::RegistrationThread(
|
|||||||
std::string nickname
|
std::string nickname
|
||||||
)
|
)
|
||||||
: connection_{connection}
|
: connection_{connection}
|
||||||
, password_{password}
|
, password_{std::move(password)}
|
||||||
, username_{username}
|
, username_{std::move(username)}
|
||||||
, realname_{realname}
|
, realname_{std::move(realname)}
|
||||||
, nickname_{nickname}
|
, nickname_{std::move(nickname)}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,12 +59,12 @@ auto RegistrationThread::send_req() -> void
|
|||||||
outstanding.insert(cap);
|
outstanding.insert(cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (not outstanding.empty())
|
if (not outstanding.empty())
|
||||||
{
|
{
|
||||||
request.pop_back();
|
request.pop_back();
|
||||||
send_cap_req(connection_, request);
|
send_cap_req(connection_, request);
|
||||||
|
|
||||||
listen_for_cap_ack();
|
listen_for_cap_ack();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -86,7 +85,7 @@ auto RegistrationThread::on_msg_cap_ack(IrcMsg const& msg) -> void
|
|||||||
);
|
);
|
||||||
if (outstanding.empty())
|
if (outstanding.empty())
|
||||||
{
|
{
|
||||||
connection_.remove_listener(message_handle_);
|
message_handle_.disconnect();
|
||||||
send_cap_end(connection_);
|
send_cap_end(connection_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,7 +130,7 @@ auto RegistrationThread::on_msg_cap_ls(IrcMsg const& msg) -> void
|
|||||||
|
|
||||||
if (last)
|
if (last)
|
||||||
{
|
{
|
||||||
connection_.remove_listener(message_handle_);
|
message_handle_.disconnect();
|
||||||
send_req();
|
send_req();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,9 +147,9 @@ auto RegistrationThread::start(
|
|||||||
|
|
||||||
thread->listen_for_cap_ls();
|
thread->listen_for_cap_ls();
|
||||||
|
|
||||||
thread->connect_handle_ = connection.add_listener<ConnectEvent>([thread](ConnectEvent const&)
|
connection.sig_connect.connect_extended([thread](auto& handle)
|
||||||
{
|
{
|
||||||
thread->connection_.remove_listener(thread->connect_handle_);
|
handle.disconnect();
|
||||||
thread->on_connect();
|
thread->on_connect();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -159,22 +158,22 @@ auto RegistrationThread::start(
|
|||||||
|
|
||||||
auto RegistrationThread::listen_for_cap_ack() -> void
|
auto RegistrationThread::listen_for_cap_ack() -> void
|
||||||
{
|
{
|
||||||
message_handle_ = connection_.add_listener<IrcMsgEvent>([thread = shared_from_this()](IrcMsgEvent const& event)
|
message_handle_ = connection_.sig_ircmsg.connect([thread = shared_from_this()](IrcCommand cmd, IrcMsg const& msg)
|
||||||
{
|
{
|
||||||
if (IrcCommand::CAP == event.command && event.irc.args.size() >= 2 && "*" == event.irc.args[0] && "ACK" == event.irc.args[1])
|
if (IrcCommand::CAP == cmd && msg.args.size() >= 2 && "*" == msg.args[0] && "ACK" == msg.args[1])
|
||||||
{
|
{
|
||||||
thread->on_msg_cap_ack(event.irc);
|
thread->on_msg_cap_ack(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
auto RegistrationThread::listen_for_cap_ls() -> void
|
auto RegistrationThread::listen_for_cap_ls() -> void
|
||||||
{
|
{
|
||||||
message_handle_ = connection_.add_listener<IrcMsgEvent>([thread = shared_from_this()](IrcMsgEvent const& event)
|
message_handle_ = connection_.sig_ircmsg.connect([thread = shared_from_this()](IrcCommand cmd, IrcMsg const& msg)
|
||||||
{
|
{
|
||||||
if (IrcCommand::CAP == event.command && event.irc.args.size() >= 2 && "*" == event.irc.args[0] && "LS" == event.irc.args[1])
|
if (IrcCommand::CAP == cmd && msg.args.size() >= 2 && "*" == msg.args[0] && "LS" == msg.args[1])
|
||||||
{
|
{
|
||||||
thread->on_msg_cap_ls(event.irc);
|
thread->on_msg_cap_ls(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <eventpp/eventdispatcher.h>
|
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -21,8 +18,8 @@ class RegistrationThread : public std::enable_shared_from_this<RegistrationThrea
|
|||||||
std::unordered_map<std::string, std::string> caps;
|
std::unordered_map<std::string, std::string> caps;
|
||||||
std::unordered_set<std::string> outstanding;
|
std::unordered_set<std::string> outstanding;
|
||||||
|
|
||||||
Connection::Handle<ConnectEvent> connect_handle_;
|
boost::signals2::connection connect_handle_;
|
||||||
Connection::Handle<IrcMsgEvent> message_handle_;
|
boost::signals2::connection message_handle_;
|
||||||
|
|
||||||
auto on_connect() -> void;
|
auto on_connect() -> void;
|
||||||
auto send_req() -> void;
|
auto send_req() -> void;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
#include "self_thread.hpp"
|
#include "self_thread.hpp"
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "ircmsg.hpp"
|
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
|
|
||||||
auto SelfThread::on_welcome(IrcMsg const& irc) -> void
|
auto SelfThread::on_welcome(IrcMsg const& irc) -> void
|
||||||
{
|
{
|
||||||
@ -84,40 +82,40 @@ auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
|
|||||||
{
|
{
|
||||||
auto thread = std::make_shared<SelfThread>(connection);
|
auto thread = std::make_shared<SelfThread>(connection);
|
||||||
|
|
||||||
connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent& event)
|
connection.sig_ircmsg.connect([thread](auto cmd, auto& msg)
|
||||||
{
|
{
|
||||||
switch (event.command)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
// Learn nickname from 001
|
// Learn nickname from 001
|
||||||
case IrcCommand::RPL_WELCOME:
|
case IrcCommand::RPL_WELCOME:
|
||||||
thread->on_welcome(event.irc);
|
thread->on_welcome(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Track changes to our nickname
|
// Track changes to our nickname
|
||||||
case IrcCommand::NICK:
|
case IrcCommand::NICK:
|
||||||
thread->on_nick(event.irc);
|
thread->on_nick(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Re-establish user modes
|
// Re-establish user modes
|
||||||
case IrcCommand::RPL_UMODEIS:
|
case IrcCommand::RPL_UMODEIS:
|
||||||
thread->on_umodeis(event.irc);
|
thread->on_umodeis(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IrcCommand::JOIN:
|
case IrcCommand::JOIN:
|
||||||
thread->on_join(event.irc);
|
thread->on_join(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IrcCommand::KICK:
|
case IrcCommand::KICK:
|
||||||
thread->on_kick(event.irc);
|
thread->on_kick(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IrcCommand::PART:
|
case IrcCommand::PART:
|
||||||
thread->on_part(event.irc);
|
thread->on_part(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Interpret self mode changes
|
// Interpret self mode changes
|
||||||
case IrcCommand::MODE:
|
case IrcCommand::MODE:
|
||||||
thread->on_mode(event.irc);
|
thread->on_mode(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default: break;
|
default: break;
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
#include "snote_thread.hpp"
|
#include "snote_thread.hpp"
|
||||||
|
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
#include "ircmsg.hpp"
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "c_callback.hpp"
|
#include "c_callback.hpp"
|
||||||
|
|
||||||
@ -74,7 +72,7 @@ SnotePattern static const patterns[] =
|
|||||||
|
|
||||||
{SnoteTag::TooManyGlobalConnections,
|
{SnoteTag::TooManyGlobalConnections,
|
||||||
R"(^Too many global connections for ([^ ]+)\[([^ ]+)@([^ ]+)\] \[(.*)\]$)"},
|
R"(^Too many global connections for ([^ ]+)\[([^ ]+)@([^ ]+)\] \[(.*)\]$)"},
|
||||||
|
|
||||||
{SnoteTag::SetVhostOnMarkedAccount,
|
{SnoteTag::SetVhostOnMarkedAccount,
|
||||||
"^\x02([^ ]+)\x02 set vhost ([^ ]+) on the \x02MARKED\x02 account ([^ ]+).$"},
|
"^\x02([^ ]+)\x02 set vhost ([^ ]+) on the \x02MARKED\x02 account ([^ ]+).$"},
|
||||||
};
|
};
|
||||||
@ -130,14 +128,13 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
|
|||||||
thread->scratch_.reset(scratch);
|
thread->scratch_.reset(scratch);
|
||||||
|
|
||||||
static char const* const prefix = "*** Notice -- ";
|
static char const* const prefix = "*** Notice -- ";
|
||||||
connection.add_listener<IrcMsgEvent>([&connection, thread](IrcMsgEvent& event)
|
connection.sig_ircmsg.connect([&connection, thread](auto cmd, auto& msg)
|
||||||
{
|
{
|
||||||
auto& args = event.irc.args;
|
auto& args = msg.args;
|
||||||
if (IrcCommand::NOTICE == event.command && "*" == args[0] && args[1].starts_with(prefix))
|
if (IrcCommand::NOTICE == cmd && "*" == args[0] && args[1].starts_with(prefix))
|
||||||
{
|
{
|
||||||
event.handled_ = true;
|
|
||||||
auto const message = args[1].substr(strlen(prefix));
|
auto const message = args[1].substr(strlen(prefix));
|
||||||
|
|
||||||
unsigned match_id;
|
unsigned match_id;
|
||||||
auto cb = [&match_id](unsigned id, unsigned long long, unsigned long long, unsigned) -> int
|
auto cb = [&match_id](unsigned id, unsigned long long, unsigned long long, unsigned) -> int
|
||||||
{
|
{
|
||||||
@ -159,11 +156,12 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
|
|||||||
case HS_SUCCESS:
|
case HS_SUCCESS:
|
||||||
BOOST_LOG_TRIVIAL(warning) << "Unknown snote: " << message;
|
BOOST_LOG_TRIVIAL(warning) << "Unknown snote: " << message;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case HS_SCAN_TERMINATED:
|
case HS_SCAN_TERMINATED:
|
||||||
{
|
{
|
||||||
auto& pattern = patterns[match_id];
|
auto& pattern = patterns[match_id];
|
||||||
connection.make_event<SnoteEvent>(pattern.tag, pattern.regex, message);
|
auto match = SnoteMatch{pattern.regex, message};
|
||||||
|
thread->sig_snote(pattern.tag, match);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,12 +174,7 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
|
|||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto SnoteEvent::get_tag() const -> SnoteTag
|
auto SnoteMatch::get_results() -> std::match_results<std::string_view::const_iterator> const&
|
||||||
{
|
|
||||||
return tag_;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto SnoteEvent::get_results() -> std::match_results<std::string_view::const_iterator> const&
|
|
||||||
{
|
{
|
||||||
if (auto results = std::get_if<1>(&components_)) {
|
if (auto results = std::get_if<1>(&components_)) {
|
||||||
return *results;
|
return *results;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "event.hpp"
|
#include <boost/signals2.hpp>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
@ -30,15 +30,13 @@ enum class SnoteTag
|
|||||||
SetVhostOnMarkedAccount,
|
SetVhostOnMarkedAccount,
|
||||||
};
|
};
|
||||||
|
|
||||||
class SnoteEvent : public Event
|
class SnoteMatch
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SnoteEvent(SnoteTag tag, std::regex const& regex, std::string_view full)
|
SnoteMatch(std::regex const& regex, std::string_view full)
|
||||||
: tag_{tag}
|
: components_{std::make_pair(std::ref(regex), full)}
|
||||||
, components_{std::make_pair(std::ref(regex), full)}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
auto get_tag() const -> SnoteTag;
|
|
||||||
auto get_results() -> std::match_results<std::string_view::const_iterator> const&;
|
auto get_results() -> std::match_results<std::string_view::const_iterator> const&;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -64,5 +62,7 @@ struct SnoteThread
|
|||||||
/// @brief HyperScan scratch space
|
/// @brief HyperScan scratch space
|
||||||
std::unique_ptr<hs_scratch, ScratchDeleter> scratch_;
|
std::unique_ptr<hs_scratch, ScratchDeleter> scratch_;
|
||||||
|
|
||||||
|
boost::signals2::signal<void(SnoteTag, SnoteMatch&)> sig_snote;
|
||||||
|
|
||||||
static auto start(Connection& connection) -> std::shared_ptr<SnoteThread>;
|
static auto start(Connection& connection) -> std::shared_ptr<SnoteThread>;
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include "watchdog_thread.hpp"
|
#include "watchdog_thread.hpp"
|
||||||
|
|
||||||
#include "connection.hpp"
|
#include "connection.hpp"
|
||||||
#include "irc_parse_thread.hpp"
|
|
||||||
#include "write_irc.hpp"
|
#include "write_irc.hpp"
|
||||||
|
|
||||||
#include <boost/asio/steady_timer.hpp>
|
#include <boost/asio/steady_timer.hpp>
|
||||||
@ -65,15 +64,15 @@ auto WatchdogThread::on_disconnect() -> void
|
|||||||
auto WatchdogThread::start(Connection& connection) -> void
|
auto WatchdogThread::start(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.sig_connect.connect([thread]()
|
||||||
{
|
{
|
||||||
thread->on_connect();
|
thread->on_connect();
|
||||||
});
|
});
|
||||||
connection.add_listener<DisconnectEvent>([thread](auto&)
|
connection.sig_disconnect.connect([thread]()
|
||||||
{
|
{
|
||||||
thread->on_disconnect();
|
thread->on_disconnect();
|
||||||
});
|
});
|
||||||
connection.add_listener<IrcMsgEvent>([thread](auto&)
|
connection.sig_ircmsg.connect([thread](auto, auto&)
|
||||||
{
|
{
|
||||||
thread->on_activity();
|
thread->on_activity();
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user