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-28 17:15:13 -08:00
|
|
|
#include "ref.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
|
|
|
struct ConnectSettings
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
2025-01-27 09:30:09 -08:00
|
|
|
|
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>;
|
2025-01-27 09:30:09 -08:00
|
|
|
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
|
|
|
|
2025-01-27 09:30:09 -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
|
|
|
|
2025-01-27 09:30:09 -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-29 15:17:19 -08:00
|
|
|
auto send_names(std::string_view channel) -> void;
|
|
|
|
auto send_kick(std::string_view, std::string_view, std::string_view) -> void;
|
|
|
|
auto send_kill(std::string_view, std::string_view) -> void;
|
|
|
|
auto send_quit(std::string_view) -> void;
|
2025-01-23 21:23:32 -08:00
|
|
|
auto send_cap_ls() -> void;
|
|
|
|
auto send_cap_end() -> void;
|
2025-01-29 15:17:19 -08:00
|
|
|
auto send_map() -> void;
|
|
|
|
auto send_testline(std::string_view) -> void;
|
|
|
|
auto send_testmask(std::string_view) -> void;
|
|
|
|
auto send_testmask_gecos(std::string_view, std::string_view) -> void;
|
|
|
|
auto send_masktrace(std::string_view) -> void;
|
|
|
|
auto send_masktrace_gecos(std::string_view, std::string_view) -> void;
|
|
|
|
auto send_get_topic(std::string_view) -> void;
|
|
|
|
auto send_set_topic(std::string_view, std::string_view) -> void;
|
2025-01-23 21:23:32 -08:00
|
|
|
auto send_cap_req(std::string_view) -> void;
|
|
|
|
auto send_privmsg(std::string_view, std::string_view) -> void;
|
2025-01-29 15:17:19 -08:00
|
|
|
auto send_wallops(std::string_view) -> void;
|
2025-01-23 21:23:32 -08:00
|
|
|
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-28 17:15:13 -08:00
|
|
|
auto send_whois(std::string_view) -> void;
|
|
|
|
auto send_whois_remote(std::string_view, std::string_view) -> void;
|
|
|
|
auto send_challenge(std::string_view) -> void;
|
2025-01-29 15:17:19 -08:00
|
|
|
auto send_oper(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
|
|
|
{
|
2025-01-27 09:30:09 -08:00
|
|
|
using namespace std::literals;
|
2025-01-23 21:23:32 -08:00
|
|
|
|
2025-01-27 09:30:09 -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...);
|
|
|
|
}
|