xbot/connection.cpp

313 lines
7.7 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#include "connection.hpp"
2023-11-27 19:09:45 -08:00
#include "linebuffer.hpp"
2025-01-25 12:25:38 -08:00
#include <mybase64.hpp>
2023-11-29 13:13:48 -08:00
#include <boost/log/trivial.hpp>
2025-01-22 23:49:48 -08:00
namespace {
#include "irc_commands.inc"
} // namespace
2025-01-24 14:48:15 -08:00
using namespace std::literals;
2023-11-25 09:22:55 -08:00
Connection::Connection(boost::asio::io_context & io)
: stream_{io}
2025-01-24 14:48:15 -08:00
, watchdog_timer_{io}
2025-01-24 16:57:34 -08:00
, write_posted_{false}
2025-01-24 14:48:15 -08:00
, stalled_{false}
2023-11-25 09:22:55 -08:00
{
}
2025-01-24 16:57:34 -08:00
auto Connection::write_buffers() -> void
2023-11-22 19:59:34 -08:00
{
std::vector<boost::asio::const_buffer> buffers;
buffers.reserve(write_strings_.size());
for (auto const& elt : write_strings_)
{
buffers.push_back(boost::asio::buffer(elt));
}
boost::asio::async_write(
stream_,
buffers,
2025-01-24 14:48:15 -08:00
[this, strings = std::move(write_strings_)](boost::system::error_code const& error, std::size_t)
2023-11-22 19:59:34 -08:00
{
2025-01-24 16:57:34 -08:00
if (not error) {
if (write_strings_.empty()) {
write_posted_ = false;
} else {
write_buffers();
2023-11-22 19:59:34 -08:00
}
}
});
2025-01-24 16:57:34 -08:00
write_strings_.clear();
2023-11-22 19:59:34 -08:00
}
auto Connection::connect(
boost::asio::io_context & io,
2023-11-25 09:22:55 -08:00
std::string host,
std::string port
) -> boost::asio::awaitable<void>
2023-11-22 19:59:34 -08:00
{
2023-11-25 09:22:55 -08:00
using namespace std::placeholders;
// keep connection alive while coroutine is active
auto const self = shared_from_this();
2023-11-22 19:59:34 -08:00
{
auto resolver = boost::asio::ip::tcp::resolver{io};
2023-11-25 09:22:55 -08:00
auto const endpoints = co_await resolver.async_resolve(host, port, boost::asio::use_awaitable);
2025-01-25 12:25:38 -08:00
auto const endpoint = co_await boost::asio::async_connect(stream_, endpoints, boost::asio::use_awaitable);
BOOST_LOG_TRIVIAL(debug) << "CONNECTED: " << endpoint;
2023-11-22 19:59:34 -08:00
2025-01-22 23:49:48 -08:00
sig_connect();
2023-11-22 19:59:34 -08:00
}
2025-01-23 21:23:32 -08:00
// Start the queue writer after connection
2025-01-24 14:48:15 -08:00
watchdog();
2025-01-23 21:23:32 -08:00
2023-11-22 19:59:34 -08:00
for(LineBuffer buffer{32'768};;)
{
boost::system::error_code error;
auto const n = co_await stream_.async_read_some(buffer.get_buffer(), boost::asio::redirect_error(boost::asio::use_awaitable, error));
if (error)
{
break;
}
buffer.add_bytes(n, [this](char * line) {
2023-11-29 13:13:48 -08:00
BOOST_LOG_TRIVIAL(debug) << "RECV: " << line;
2025-01-24 14:48:15 -08:00
watchdog_activity();
2025-01-23 12:46:52 -08:00
dispatch_line(line);
2023-11-22 19:59:34 -08:00
});
}
2025-01-24 14:48:15 -08:00
watchdog_timer_.cancel();
stream_.close();
2025-01-22 23:49:48 -08:00
sig_disconnect();
2023-11-22 19:59:34 -08:00
}
2025-01-24 14:48:15 -08:00
auto Connection::watchdog() -> void
{
watchdog_timer_.expires_after(watchdog_duration);
watchdog_timer_.async_wait([this](auto const& error)
{
if (not error)
{
if (stalled_)
{
BOOST_LOG_TRIVIAL(debug) << "Watchdog timer elapsed, closing stream";
close();
}
else
{
send_ping("watchdog");
stalled_ = true;
watchdog();
}
}
});
}
auto Connection::watchdog_activity() -> void
{
stalled_ = false;
watchdog_timer_.expires_after(watchdog_duration);
}
2025-01-23 12:46:52 -08:00
/// Parse IRC message line and dispatch it to the ircmsg slot.
auto Connection::dispatch_line(char *line) -> void
{
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;
2025-01-23 21:23:32 -08:00
switch (command) {
// Respond to pings immediate and discard
case IrcCommand::PING:
send_pong(msg.args[0]);
break;
// Unknown message generate warnings but do not dispatch
// Messages can be unknown due to bad command or bad argument count
case IrcCommand::UNKNOWN:
2025-01-23 12:46:52 -08:00
BOOST_LOG_TRIVIAL(warning) << "Unrecognized command: " << msg.command << " " << msg.args.size();
2025-01-23 21:23:32 -08:00
break;
2025-01-25 12:25:38 -08:00
case IrcCommand::AUTHENTICATE:
on_authenticate(msg.args[0]);
break;
2025-01-23 21:23:32 -08:00
// Server notice generate snote events but not IRC command events
case IrcCommand::NOTICE:
if (auto match = snoteCore.match(msg)) {
2025-01-24 14:48:15 -08:00
sig_snote(*match);
2025-01-23 21:23:32 -08:00
break;
}
/* FALLTHROUGH */
// Normal IRC commands
default:
sig_ircmsg(command, msg);
break;
2025-01-23 12:46:52 -08:00
}
2025-01-23 21:23:32 -08:00
2025-01-23 12:46:52 -08:00
}
2023-11-26 19:59:12 -08:00
auto Connection::write_line(std::string message) -> void
2023-11-22 19:59:34 -08:00
{
2023-11-29 13:13:48 -08:00
BOOST_LOG_TRIVIAL(debug) << "SEND: " << message;
2023-11-26 19:59:12 -08:00
message += "\r\n";
2023-11-22 19:59:34 -08:00
write_strings_.push_back(std::move(message));
2025-01-24 16:57:34 -08:00
if (not write_posted_) {
write_posted_ = true;
boost::asio::post(stream_.get_executor(), [weak = weak_from_this()](){
if (auto self = weak.lock())
{
self->write_buffers();
}
});
2023-11-22 19:59:34 -08:00
}
}
2023-11-25 09:22:55 -08:00
auto Connection::close() -> void
{
stream_.close();
2023-11-27 14:12:20 -08:00
}
2025-01-23 21:23:32 -08:00
static
auto is_invalid_last(char x) -> bool
{
return x == '\0' || x == '\r' || x == '\n';
}
auto Connection::write_irc(std::string message) -> void
{
write_line(std::move(message));
}
auto Connection::write_irc(std::string front, std::string_view last) -> void
{
if (last.end() != std::find_if(last.begin(), last.end(), is_invalid_last))
{
throw std::runtime_error{"bad irc argument"};
}
front += " :";
front += last;
write_irc(std::move(front));
}
auto Connection::send_ping(std::string_view txt) -> void
{
write_irc("PING", txt);
}
auto Connection::send_pong(std::string_view txt) -> void
{
write_irc("PONG", txt);
}
auto Connection::send_pass(std::string_view password) -> void
{
write_irc("PASS", password);
}
auto Connection::send_user(std::string_view user, std::string_view real) -> void
{
write_irc("USER", user, "*", "*", real);
}
auto Connection::send_nick(std::string_view nick) -> void
{
write_irc("NICK", nick);
}
auto Connection::send_cap_ls() -> void
{
write_irc("CAP", "LS", "302");
}
auto Connection::send_cap_end() -> void
{
write_irc("CAP", "END");
}
auto Connection::send_cap_req(std::string_view caps) -> void
{
write_irc("CAP", "REQ", caps);
}
auto Connection::send_privmsg(std::string_view target, std::string_view message) -> void
{
write_irc("PRIVMSG", target, message);
}
auto Connection::send_notice(std::string_view target, std::string_view message) -> void
{
write_irc("NOTICE", target, message);
}
auto Connection::send_authenticate(std::string_view message) -> void
{
write_irc("AUTHENTICATE", message);
}
2025-01-25 12:25:38 -08:00
auto Connection::on_authenticate(const std::string_view chunk) -> void
{
if (chunk != "+"sv) {
authenticate_buffer_ += chunk;
}
if (chunk.size() != 400)
{
std::string decoded;
decoded.resize(mybase64::decoded_size(authenticate_buffer_.size()));
std::size_t len;
if (mybase64::decode(authenticate_buffer_, decoded.data(), &len))
{
decoded.resize(len);
sig_authenticate(decoded);
} else {
BOOST_LOG_TRIVIAL(debug) << "Invalid AUTHENTICATE base64"sv;
send_authenticate("*"sv); // abort SASL
}
authenticate_buffer_.clear();
}
else if (authenticate_buffer_.size() > 1024)
{
BOOST_LOG_TRIVIAL(debug) << "AUTHENTICATE buffer overflow"sv;
authenticate_buffer_.clear();
send_authenticate("*"sv); // abort SASL
}
}
auto Connection::send_authenticate_abort() -> void
{
send_authenticate("*");
}
auto Connection::send_authenticate_encoded(std::string_view body) -> void
{
std::string encoded(mybase64::encoded_size(body.size()), 0);
mybase64::encode(body, encoded.data());
for (size_t lo = 0; lo < encoded.size(); lo += 400) {
const auto hi = std::min(lo + 400, encoded.size());
const std::string_view chunk { encoded.begin() + lo, encoded.begin() + hi };
send_authenticate(chunk);
}
if (encoded.size() % 400 == 0)
{
send_authenticate("+"sv);
}
}