Compare commits

...

3 Commits

Author SHA1 Message Date
d92c6fee21 add coroutine experiment 2025-01-23 00:47:05 -08:00
7665f4c0f5 migrate to signals2 2025-01-22 23:49:48 -08:00
d11412e73f checkpoint 2025-01-22 20:33:17 -08:00
21 changed files with 478 additions and 239 deletions

View File

@ -6,7 +6,7 @@ project(xbot
LANGUAGES C CXX
)
find_package(Boost REQUIRED COMPONENTS log)
find_package(Boost 1.83.0 CONFIG COMPONENTS log)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBHS libhs REQUIRED IMPORTED_TARGET)
@ -20,13 +20,6 @@ FetchContent_Declare(
)
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(
OUTPUT irc_commands.inc
COMMAND
@ -37,10 +30,18 @@ add_custom_command(
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/irc_commands.gperf
VERBATIM)
add_subdirectory(mybase64)
add_executable(xbot
main.cpp irc_commands.inc ircmsg.cpp settings.cpp connection.cpp
snote_thread.cpp watchdog_thread.cpp write_irc.cpp
ping_thread.cpp irc_parse_thread.cpp registration_thread.cpp
self_thread.cpp command_thread.cpp priv_thread.cpp)
write_irc.cpp
registration_thread.cpp
ping_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_link_libraries(xbot PRIVATE Boost::log Boost::headers tomlplusplus_tomlplusplus eventpp PkgConfig::LIBHS)
target_link_libraries(xbot PRIVATE Boost::log Boost::headers tomlplusplus_tomlplusplus PkgConfig::LIBHS mybase64)

View File

@ -8,10 +8,19 @@ class Connection;
struct CommandEvent : Event
{
/// @brief oper account of sender
std::string_view oper;
/// @brief nickserv acccount of sender
std::string_view account;
/// @brief nickname of sender
std::string_view nick;
/// @brief command name excluding sigil
std::string_view command;
/// @brief complete argument excluding space after command
std::string_view arg;
};

View File

@ -4,6 +4,10 @@
#include <boost/log/trivial.hpp>
namespace {
#include "irc_commands.inc"
} // namespace
Connection::Connection(boost::asio::io_context & io)
: stream_{io}
, 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 endpoint = co_await boost::asio::async_connect(stream_, endpoints, boost::asio::use_awaitable);
make_event<ConnectEvent>();
sig_connect();
}
self->writer();
@ -86,11 +90,24 @@ auto Connection::connect(
}
buffer.add_bytes(n, [this](char * 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

View File

@ -3,10 +3,8 @@
#include "event.hpp"
#include "settings.hpp"
#include <eventpp/eventdispatcher.h>
#include <eventpp/utilities/argumentadapter.h>
#include <boost/asio.hpp>
#include <boost/signals2.hpp>
#include <chrono>
#include <functional>
@ -20,43 +18,16 @@
#include <utility>
#include <variant>
#include <vector>
#include <typeinfo>
#include <typeindex>
struct ConnectEvent : Event
{
};
struct DisconnectEvent : Event
{
};
struct LineEvent : Event
{
explicit LineEvent(char * line) : line{line} {}
char * line;
};
#include "ircmsg.hpp"
#include "irc_command.hpp"
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:
boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
std::list<std::string> write_strings_;
EventDispatcher dispatcher_;
auto writer() -> void;
auto writer_immediate() -> void;
@ -64,37 +35,14 @@ private:
public:
Connection(boost::asio::io_context & io);
template <typename T, typename F>
auto add_listener(F f) -> Handle<T>
{
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);
}
boost::signals2::signal<void()> sig_connect;
boost::signals2::signal<void()> sig_disconnect;
boost::signals2::signal<void(IrcCommand, const IrcMsg &)> sig_ircmsg;
auto get_executor() -> boost::asio::any_io_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.
auto write_line(std::string message) -> void;

View File

@ -1,12 +1,3 @@
#pragma once
#include "event.hpp"
#include <string_view>
class Connection;
class IrcMsg;
enum class IrcCommand
{
UNKNOWN,
@ -284,15 +275,3 @@ enum class IrcCommand
SETNAME,
TOPIC,
};
struct IrcMsgEvent : Event
{
IrcMsgEvent(IrcCommand command, IrcMsg const& irc);
IrcCommand command;
IrcMsg const& irc;
};
struct IrcParseThread
{
static auto start(Connection& connection) -> void;
};

View File

@ -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);
});
}

View File

@ -6,15 +6,6 @@
#include "settings.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 <algorithm>
@ -33,47 +24,89 @@
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <coroutine>
#include "ping_thread.hpp"
#include "registration_thread.hpp"
#include "self_thread.hpp"
#include "snote_thread.hpp"
using namespace std::chrono_literals;
auto echo_thread(
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");
struct irc_promise;
struct irc_coroutine : std::coroutine_handle<irc_promise> {
using promise_type = irc_promise;
};
struct irc_promise {
std::exception_ptr exception_;
irc_coroutine get_return_object() { return {irc_coroutine::from_promise(*this)}; }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {
exception_ = std::current_exception();
}
};
struct wait_command {
Connection& connection_;
IrcCommand want_cmd_;
const IrcMsg *result;
boost::signals2::connection ircmsg_connection_;
boost::signals2::connection disconnect_connection_;
wait_command(Connection& connection, IrcCommand want_cmd)
: connection_{connection}, want_cmd_{want_cmd} {}
bool await_ready() noexcept { return false; }
void await_suspend(std::coroutine_handle<irc_promise> handle) {
ircmsg_connection_ = connection_.sig_ircmsg.connect([this, handle](auto cmd, auto &msg) {
if (cmd == want_cmd_) {
ircmsg_connection_.disconnect();
disconnect_connection_.disconnect();
result = &msg;
handle.resume();
}
});
disconnect_connection_ = connection_.sig_disconnect.connect([this, handle]() {
ircmsg_connection_.disconnect();
disconnect_connection_.disconnect();
handle.destroy(); // XXX
});
}
const IrcMsg &await_resume() { return *result; }
};
irc_coroutine example(Connection& connection) {
auto & msg1 = co_await wait_command {connection, IrcCommand::RPL_WELCOME};
std::cout << "WELCOME " << msg1.args[0] << "\n";
auto & msg5 = co_await wait_command {connection, IrcCommand::RPL_ISUPPORT};
std::cout << "ISUPPORT " << msg5.args[0] << "\n";
}
auto start(boost::asio::io_context & io, Settings const& settings) -> void
{
auto const connection = std::make_shared<Connection>(io);
WatchdogThread::start(*connection);
IrcParseThread::start(*connection);
RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname);
PingThread::start(*connection);
SelfThread::start(*connection);
RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname);
SnoteThread::start(*connection);
CommandThread::start(*connection);
auto const priv_thread = PrivThread::start(*connection, "privs.toml");
echo_thread(*connection, priv_thread);
auto const snote_thread = SnoteThread::start(*connection);
/*
connection->add_listener<SnoteEvent>([](SnoteEvent& event) {
std::cout << "SNOTE " << static_cast<int>(event.get_tag()) << std::endl;
for (auto c : event.get_results())
snote_thread->sig_snote.connect([](auto tag, auto &match) {
std::cout << "SNOTE " << static_cast<int>(tag) << std::endl;
for (auto c : match.get_results())
{
std::cout << " " << std::string_view{c.first, c.second} << std::endl;
}
});
*/
auto logic = example(*connection);
boost::asio::co_spawn(
io,
@ -81,7 +114,8 @@ auto start(boost::asio::io_context & io, Settings const& settings) -> void
[&io, &settings](std::exception_ptr e)
{
auto timer = std::make_shared<boost::asio::steady_timer>(io);
timer->expires_from_now(5s);
timer->expires_after(5s);
timer->async_wait([&io, &settings, timer](auto) {
start(io, settings);
});

2
mybase64/CMakeLists.txt Normal file
View File

@ -0,0 +1,2 @@
add_library(mybase64 STATIC mybase64.cpp)
target_include_directories(mybase64 PUBLIC include)

View File

@ -0,0 +1,44 @@
/**
* @file mybase64.hpp
* @author Eric Mertens (emertens@gmail.com)
* @brief Base64 encoding and decoding
*
*/
#pragma once
#include <cstddef>
#include <string_view>
namespace mybase64
{
inline constexpr auto encoded_size(std::size_t len) -> std::size_t
{
return (len + 2) / 3 * 4;
}
inline constexpr auto decoded_size(std::size_t len) -> std::size_t
{
return (len + 3) / 4 * 3;
}
/**
* @brief Encode a string into base64
*
* @param input input text
* @param output Target buffer for encoded value
*/
auto encode(std::string_view input, char* output) -> void;
/**
* @brief Decode a base64 encoded string
*
* @param input Base64 input text
* @param output Target buffer for decoded value
* @param outlen Output parameter for decoded length
* @return true success
* @return false failure
*/
auto decode(std::string_view input, char* output, std::size_t* outlen) -> bool;
} // namespace

113
mybase64/mybase64.cpp Normal file
View File

@ -0,0 +1,113 @@
#include "mybase64.hpp"
#include <climits>
#include <cstdint>
#include <string_view>
namespace mybase64
{
static_assert(CHAR_BIT == 8);
auto encode(std::string_view const input, char* output) -> void
{
static char const* const alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
auto cursor = std::begin(input);
auto const end = std::end(input);
while (end - cursor >= 3)
{
uint32_t buffer = uint8_t(*cursor++);
buffer <<= 8; buffer |= uint8_t(*cursor++);
buffer <<= 8; buffer |= uint8_t(*cursor++);
*output++ = alphabet[(buffer >> 6 * 3) % 64];
*output++ = alphabet[(buffer >> 6 * 2) % 64];
*output++ = alphabet[(buffer >> 6 * 1) % 64];
*output++ = alphabet[(buffer >> 6 * 0) % 64];
}
if (cursor < end)
{
uint32_t buffer = uint8_t(*cursor++) << 10;
if (cursor < end) buffer |= uint8_t(*cursor) << 2;
*output++ = alphabet[(buffer >> 12) % 64];
*output++ = alphabet[(buffer >> 6) % 64];
*output++ = cursor < end ? alphabet[(buffer % 64)] : '=';
*output++ = '=';
}
*output = '\0';
}
auto decode(std::string_view const input, char* const output, std::size_t* const outlen) -> bool
{
static int8_t const alphabet_values[] = {
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 0x3e, -1, -1, -1, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, -1, -1, -1, -1, -1, -1,
-1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, -1, -1, -1, -1, -1,
-1, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
0x31, 0x32, 0x33, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
};
uint32_t buffer = 1;
char* cursor = output;
for (char c : input) {
int8_t const value = alphabet_values[uint8_t(c)];
if (-1 == value) continue;
buffer = (buffer << 6) | value;
if (buffer & 1<<6*4) {
*cursor++ = buffer >> 8*2;
*cursor++ = buffer >> 8*1;
*cursor++ = buffer >> 8*0;
buffer = 1;
}
}
if (buffer & 1<<6*3) {
*cursor++ = buffer >> 10;
*cursor++ = buffer >> 2;
} else if (buffer & 1<<6*2) {
*cursor++ = buffer >> 4;
} else if (buffer & 1<<6*1) {
return false;
}
*outlen = cursor - output;
return true;
}
} // namespace

View File

@ -1,19 +1,16 @@
#include "ping_thread.hpp"
#include "connection.hpp"
#include "irc_parse_thread.hpp"
#include "ircmsg.hpp"
#include "write_irc.hpp"
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 == event.command)
if (IrcCommand::PING == cmd)
{
send_pong(connection, irc.args[0]);
event.handled_ = true;
send_pong(connection, msg.args[0]);
}
});
}

View File

@ -1,7 +1,6 @@
#include "registration_thread.hpp"
#include "connection.hpp"
#include "irc_parse_thread.hpp"
#include "ircmsg.hpp"
#include "write_irc.hpp"
@ -17,10 +16,10 @@ RegistrationThread::RegistrationThread(
std::string nickname
)
: connection_{connection}
, password_{password}
, username_{username}
, realname_{realname}
, nickname_{nickname}
, password_{std::move(password)}
, username_{std::move(username)}
, realname_{std::move(realname)}
, nickname_{std::move(nickname)}
{
}
@ -30,8 +29,6 @@ auto RegistrationThread::on_connect() -> void
send_pass(connection_, password_);
send_user(connection_, username_, realname_);
send_nick(connection_, nickname_);
connection_.remove_listener(connect_handle_);
}
auto RegistrationThread::send_req() -> void
@ -53,7 +50,7 @@ auto RegistrationThread::send_req() -> void
"solanum.chat/realhost",
};
for (auto cap : want)
for (auto const cap : want)
{
if (caps.contains(cap))
{
@ -63,8 +60,6 @@ auto RegistrationThread::send_req() -> void
}
}
connection_.remove_listener(message_handle_);
if (not outstanding.empty())
{
request.pop_back();
@ -90,8 +85,8 @@ auto RegistrationThread::on_msg_cap_ack(IrcMsg const& msg) -> void
);
if (outstanding.empty())
{
message_handle_.disconnect();
send_cap_end(connection_);
connection_.remove_listener(message_handle_);
}
}
@ -135,6 +130,7 @@ auto RegistrationThread::on_msg_cap_ls(IrcMsg const& msg) -> void
if (last)
{
message_handle_.disconnect();
send_req();
}
}
@ -151,8 +147,9 @@ auto RegistrationThread::start(
thread->listen_for_cap_ls();
thread->connect_handle_ = connection.add_listener<ConnectEvent>([thread](ConnectEvent const&)
connection.sig_connect.connect_extended([thread](auto& handle)
{
handle.disconnect();
thread->on_connect();
});
@ -161,22 +158,22 @@ auto RegistrationThread::start(
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
{
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);
}
});
}

View File

@ -1,9 +1,6 @@
#pragma once
#include <eventpp/eventdispatcher.h>
#include "connection.hpp"
#include "irc_parse_thread.hpp"
#include <memory>
#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_set<std::string> outstanding;
Connection::Handle<ConnectEvent> connect_handle_;
Connection::Handle<IrcMsgEvent> message_handle_;
boost::signals2::connection connect_handle_;
boost::signals2::connection message_handle_;
auto on_connect() -> void;
auto send_req() -> void;

55
sasl_thread.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "sasl_thread.hpp"
#include <mybase64.hpp>
#include <boost/log/trivial.hpp>
#include "connection.hpp"
#include "write_irc.hpp"
#include "irc_parse_thread.hpp"
#include "ircmsg.hpp"
auto SaslThread::start(Connection& connection) -> std::shared_ptr<SaslThread>
{
auto thread = std::make_shared<SaslThread>(connection);
connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent const& event){
if (event.command == IrcCommand::AUTHENTICATE)
{
thread->on_authenticate(event.irc.args[0]);
}
});
return thread;
}
auto SaslThread::on_authenticate(std::string_view chunk) -> void
{
if (chunk != "+") {
buffer_ += chunk;
}
if (chunk.size() != 400)
{
std::string decoded;
decoded.resize(mybase64::decoded_size(buffer_.size()));
std::size_t len;
if (mybase64::decode(buffer_, decoded.data(), &len))
{
decoded.resize(len);
connection_.make_event<SaslMessage>(std::move(decoded));
} else {
BOOST_LOG_TRIVIAL(debug) << "Invalid AUTHENTICATE base64";
send_authenticate(connection_, "*"); // abort SASL
}
buffer_.clear();
}
else if (buffer_.size() > MAX_BUFFER)
{
BOOST_LOG_TRIVIAL(debug) << "AUTHENTICATE buffer overflow";
buffer_.clear();
send_authenticate(connection_, "*"); // abort SASL
}
}

89
sasl_thread.hpp Normal file
View File

@ -0,0 +1,89 @@
#pragma once
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include "event.hpp"
struct Connection;
class SaslMechanism
{
public:
virtual ~SaslMechanism() {}
virtual auto mechanism_name() const -> std::string = 0;
virtual auto step(std::string_view msg) -> std::optional<std::string> = 0;
virtual auto is_complete() const -> bool = 0;
};
class SaslPlain final : public SaslMechanism
{
std::string authcid_;
std::string authzid_;
std::string password_;
bool complete_;
public:
SaslPlain(std::string username, std::string password)
: authcid_{std::move(username)}
, password_{std::move(password)}
, complete_{false}
{}
auto mechanism_name() const -> std::string override
{
return "PLAIN";
}
auto step(std::string_view msg) -> std::optional<std::string> override {
if (complete_) {
return {};
} else {
std::string reply;
reply += authzid_;
reply += '\0';
reply += authcid_;
reply += '\0';
reply += password_;
complete_ = true;
return {std::move(reply)};
}
}
auto is_complete() const -> bool override
{
return complete_;
}
};
struct SaslComplete : Event
{
bool success;
};
struct SaslMessage : Event
{
SaslMessage(std::string message) : message{std::move(message)} {}
std::string message;
};
class SaslThread
{
Connection& connection_;
std::string buffer_;
const std::size_t MAX_BUFFER = 1024;
public:
SaslThread(Connection& connection) : connection_{connection} {}
static auto start(Connection& connection) -> std::shared_ptr<SaslThread>;
auto on_authenticate(std::string_view) -> void;
};

View File

@ -1,10 +1,6 @@
#include "self_thread.hpp"
#include <boost/log/trivial.hpp>
#include "connection.hpp"
#include "ircmsg.hpp"
#include "irc_parse_thread.hpp"
auto SelfThread::on_welcome(IrcMsg const& irc) -> void
{
@ -86,40 +82,40 @@ auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
{
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
case IrcCommand::RPL_WELCOME:
thread->on_welcome(event.irc);
thread->on_welcome(msg);
break;
// Track changes to our nickname
case IrcCommand::NICK:
thread->on_nick(event.irc);
thread->on_nick(msg);
break;
// Re-establish user modes
case IrcCommand::RPL_UMODEIS:
thread->on_umodeis(event.irc);
thread->on_umodeis(msg);
break;
case IrcCommand::JOIN:
thread->on_join(event.irc);
thread->on_join(msg);
break;
case IrcCommand::KICK:
thread->on_kick(event.irc);
thread->on_kick(msg);
break;
case IrcCommand::PART:
thread->on_part(event.irc);
thread->on_part(msg);
break;
// Interpret self mode changes
case IrcCommand::MODE:
thread->on_mode(event.irc);
thread->on_mode(msg);
break;
default: break;

View File

@ -1,7 +1,5 @@
#include "snote_thread.hpp"
#include "irc_parse_thread.hpp"
#include "ircmsg.hpp"
#include "connection.hpp"
#include "c_callback.hpp"
@ -130,12 +128,11 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
thread->scratch_.reset(scratch);
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;
if (IrcCommand::NOTICE == event.command && "*" == args[0] && args[1].starts_with(prefix))
auto& args = msg.args;
if (IrcCommand::NOTICE == cmd && "*" == args[0] && args[1].starts_with(prefix))
{
event.handled_ = true;
auto const message = args[1].substr(strlen(prefix));
unsigned match_id;
@ -163,7 +160,8 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
case HS_SCAN_TERMINATED:
{
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;
}
@ -176,12 +174,7 @@ auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
return thread;
}
auto SnoteEvent::get_tag() const -> SnoteTag
{
return tag_;
}
auto SnoteEvent::get_results() -> std::match_results<std::string_view::const_iterator> const&
auto SnoteMatch::get_results() -> std::match_results<std::string_view::const_iterator> const&
{
if (auto results = std::get_if<1>(&components_)) {
return *results;

View File

@ -1,6 +1,6 @@
#pragma once
#include "event.hpp"
#include <boost/signals2.hpp>
#include <memory>
#include <regex>
@ -30,15 +30,13 @@ enum class SnoteTag
SetVhostOnMarkedAccount,
};
class SnoteEvent : public Event
class SnoteMatch
{
public:
SnoteEvent(SnoteTag tag, std::regex const& regex, std::string_view full)
: tag_{tag}
, components_{std::make_pair(std::ref(regex), full)}
SnoteMatch(std::regex const& regex, std::string_view 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&;
private:
@ -64,5 +62,7 @@ struct SnoteThread
/// @brief HyperScan scratch space
std::unique_ptr<hs_scratch, ScratchDeleter> scratch_;
boost::signals2::signal<void(SnoteTag, SnoteMatch&)> sig_snote;
static auto start(Connection& connection) -> std::shared_ptr<SnoteThread>;
};

View File

@ -1,7 +1,6 @@
#include "watchdog_thread.hpp"
#include "connection.hpp"
#include "irc_parse_thread.hpp"
#include "write_irc.hpp"
#include <boost/asio/steady_timer.hpp>
@ -19,7 +18,7 @@ WatchdogThread::WatchdogThread(Connection& connection)
auto WatchdogThread::on_activity() -> void
{
stalled_ = false;
timer_.expires_from_now(WatchdogThread::TIMEOUT);
timer_.expires_after(WatchdogThread::TIMEOUT);
}
auto WatchdogThread::start_timer()
@ -46,7 +45,7 @@ auto WatchdogThread::on_timeout() -> void
{
send_ping(connection_, "watchdog");
stalled_ = true;
timer_.expires_from_now(WatchdogThread::TIMEOUT);
timer_.expires_after(WatchdogThread::TIMEOUT);
start_timer();
}
}
@ -65,15 +64,15 @@ auto WatchdogThread::on_disconnect() -> void
auto WatchdogThread::start(Connection& connection) -> void
{
auto const thread = std::make_shared<WatchdogThread>(connection);
connection.add_listener<ConnectEvent>([thread](auto&)
connection.sig_connect.connect([thread]()
{
thread->on_connect();
});
connection.add_listener<DisconnectEvent>([thread](auto&)
connection.sig_disconnect.connect([thread]()
{
thread->on_disconnect();
});
connection.add_listener<IrcMsgEvent>([thread](auto&)
connection.sig_ircmsg.connect([thread](auto, auto&)
{
thread->on_activity();
});

View File

@ -100,3 +100,8 @@ auto send_notice(Connection& connection, std::string_view target, std::string_vi
{
write_irc(connection, "NOTICE", target, message);
}
auto send_authenticate(Connection& connection, std::string_view message) -> void
{
write_irc(connection, "AUTHENTICATE", message);
}

View File

@ -14,3 +14,4 @@ auto send_cap_end(Connection&) -> void;
auto send_cap_req(Connection&, std::string_view) -> void;
auto send_privmsg(Connection&, std::string_view, std::string_view) -> void;
auto send_notice(Connection&, std::string_view, std::string_view) -> void;
auto send_authenticate(Connection& connection, std::string_view message) -> void;