xbot/connection.hpp

128 lines
3.7 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#pragma once
2025-01-23 12:46:52 -08:00
#include "irc_command.hpp"
2025-01-25 15:45:31 -08:00
#include "ircmsg.hpp"
2025-01-23 21:23:32 -08:00
#include "snote.hpp"
2025-01-26 14:38:13 -08:00
#include "stream.hpp"
2023-11-22 19:59:34 -08:00
#include <boost/asio.hpp>
2025-01-22 23:49:48 -08:00
#include <boost/signals2.hpp>
2023-11-22 19:59:34 -08:00
#include <list>
#include <memory>
#include <string>
2023-11-25 09:22:55 -08:00
2025-01-26 14:38:13 -08:00
template <typename T, int(*UpRef)(T*), void(*Free)(T*)>
class Ref {
struct Deleter { auto operator()(auto ptr) { Free(ptr); }};
std::unique_ptr<T, Deleter> obj;
public:
Ref() = default;
Ref(T* t) : obj{t} { if (t) UpRef(t); }
auto get() const -> T* { return obj.get(); }
};
struct ConnectSettings
{
2025-01-27 18:55:19 -08:00
using X509_Ref = Ref<X509, X509_up_ref, X509_free>;
using EVP_PKEY_Ref = Ref<EVP_PKEY, EVP_PKEY_up_ref, EVP_PKEY_free>;
2025-01-26 14:38:13 -08:00
bool tls;
std::string host;
std::uint16_t port;
2025-01-27 18:55:19 -08:00
X509_Ref client_cert;
EVP_PKEY_Ref client_key;
2025-01-26 14:38:13 -08:00
std::string verify;
std::string sni;
std::string socks_host;
std::uint16_t socks_port;
std::string socks_user;
std::string socks_pass;
};
2023-11-22 19:59:34 -08:00
class Connection : public std::enable_shared_from_this<Connection>
{
2023-11-25 20:09:20 -08:00
private:
2025-01-26 14:38:13 -08:00
Stream stream_;
2025-01-24 14:48:15 -08:00
boost::asio::steady_timer watchdog_timer_;
2023-11-22 19:59:34 -08:00
std::list<std::string> write_strings_;
2025-01-24 16:57:34 -08:00
bool write_posted_;
2023-11-22 19:59:34 -08:00
2025-01-24 14:48:15 -08:00
// Set true when watchdog triggers.
// Set false when message received.
bool stalled_;
2025-01-25 12:25:38 -08:00
// AUTHENTICATE support
std::string authenticate_buffer_;
2025-01-24 16:57:34 -08:00
auto write_buffers() -> void;
2025-01-25 15:45:31 -08:00
auto dispatch_line(char *line) -> void;
2023-11-22 19:59:34 -08:00
2025-01-24 14:48:15 -08:00
static constexpr std::chrono::seconds watchdog_duration = std::chrono::seconds{30};
auto watchdog() -> void;
auto watchdog_activity() -> void;
2025-01-26 14:51:44 -08:00
auto connect(ConnectSettings settings) -> boost::asio::awaitable<void>;
auto on_authenticate(std::string_view) -> void;
2025-01-26 14:51:44 -08:00
2025-01-23 21:23:32 -08:00
/// Build and send well-formed IRC message from individual parameters
auto write_irc(std::string) -> void;
auto write_irc(std::string, std::string_view) -> void;
template <typename... Args>
2025-01-25 15:45:31 -08:00
auto write_irc(std::string front, std::string_view next, Args... rest) -> void;
2025-01-23 21:23:32 -08:00
public:
2025-01-22 23:49:48 -08:00
boost::signals2::signal<void()> sig_connect;
boost::signals2::signal<void()> sig_disconnect;
boost::signals2::signal<void(IrcCommand, const IrcMsg &)> sig_ircmsg;
2025-01-24 14:48:15 -08:00
boost::signals2::signal<void(SnoteMatch &)> sig_snote;
2025-01-25 12:25:38 -08:00
boost::signals2::signal<void(std::string_view)> sig_authenticate;
2023-11-25 20:09:20 -08:00
Connection(boost::asio::io_context &io);
/// Write bytes into the socket.
auto write_line(std::string message) -> void;
2025-01-25 15:45:31 -08:00
auto get_executor() -> boost::asio::any_io_executor
{
2023-11-25 20:09:20 -08:00
return stream_.get_executor();
}
2023-11-22 19:59:34 -08:00
2025-01-26 14:51:44 -08:00
auto start(ConnectSettings) -> void;
2023-11-25 09:22:55 -08:00
auto close() -> void;
2025-01-23 21:23:32 -08:00
auto send_ping(std::string_view) -> void;
auto send_pong(std::string_view) -> void;
auto send_pass(std::string_view) -> void;
auto send_user(std::string_view, std::string_view) -> void;
auto send_nick(std::string_view) -> void;
2025-01-25 21:24:33 -08:00
auto send_join(std::string_view) -> void;
2025-01-23 21:23:32 -08:00
auto send_cap_ls() -> void;
auto send_cap_end() -> void;
auto send_cap_req(std::string_view) -> void;
auto send_privmsg(std::string_view, std::string_view) -> void;
auto send_notice(std::string_view, std::string_view) -> void;
auto send_authenticate(std::string_view message) -> void;
2025-01-25 12:25:38 -08:00
auto send_authenticate_encoded(std::string_view message) -> void;
auto send_authenticate_abort() -> void;
2025-01-27 18:55:19 -08:00
auto send_whois(std::string_view, std::string_view = {}) -> void;
2023-11-22 19:59:34 -08:00
};
2025-01-23 21:23:32 -08:00
template <typename... Args>
2025-01-25 15:45:31 -08:00
auto Connection::write_irc(std::string front, std::string_view next, Args... rest) -> void
2025-01-23 21:23:32 -08:00
{
using namespace std::literals;
2025-01-23 21:23:32 -08:00
if (next.empty() || next.front() == ':' || next.find_first_of("\r\n \0"sv) != next.npos)
2025-01-23 21:23:32 -08:00
{
throw std::runtime_error{"bad irc argument"};
}
front += " ";
front += next;
write_irc(std::move(front), rest...);
}