2023-11-22 19:59:34 -08:00
|
|
|
#pragma once
|
|
|
|
|
2025-01-23 12:46:52 -08:00
|
|
|
#include "ircmsg.hpp"
|
|
|
|
#include "irc_command.hpp"
|
2025-01-23 21:23:32 -08:00
|
|
|
#include "snote.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
|
|
|
|
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:
|
2023-11-22 19:59:34 -08:00
|
|
|
boost::asio::ip::tcp::socket 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-24 16:57:34 -08:00
|
|
|
auto write_buffers() -> void;
|
2025-01-23 12:46:52 -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-23 21:23:32 -08:00
|
|
|
/// Write bytes into the socket. Messages should be properly newline terminated.
|
|
|
|
auto write_line(std::string message) -> void;
|
|
|
|
|
|
|
|
/// 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>
|
|
|
|
auto write_irc(std::string front, std::string_view next, Args ...rest) -> void;
|
|
|
|
|
2023-11-22 19:59:34 -08:00
|
|
|
public:
|
2023-11-25 09:22:55 -08:00
|
|
|
Connection(boost::asio::io_context & io);
|
2023-11-25 20:09:20 -08:00
|
|
|
|
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;
|
2023-11-25 20:09:20 -08:00
|
|
|
|
|
|
|
auto get_executor() -> boost::asio::any_io_executor {
|
|
|
|
return stream_.get_executor();
|
|
|
|
}
|
2023-11-22 19:59:34 -08:00
|
|
|
|
|
|
|
auto connect(
|
|
|
|
boost::asio::io_context & io,
|
2023-11-25 09:22:55 -08:00
|
|
|
std::string host,
|
|
|
|
std::string port
|
2023-11-22 19:59:34 -08:00
|
|
|
) -> boost::asio::awaitable<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;
|
|
|
|
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;
|
2023-11-22 19:59:34 -08:00
|
|
|
};
|
|
|
|
|
2025-01-23 21:23:32 -08:00
|
|
|
template <typename... Args>
|
|
|
|
auto Connection::write_irc(std::string front, std::string_view next, Args ...rest) -> void
|
|
|
|
{
|
|
|
|
auto const is_invalid = [](char const x) -> bool
|
|
|
|
{
|
|
|
|
return x == '\0' || x == '\r' || x == '\n' || x == ' ';
|
|
|
|
};
|
|
|
|
|
|
|
|
if (next.empty()
|
|
|
|
|| next.front() == ':'
|
|
|
|
|| next.end() != std::find_if(next.begin(), next.end(), is_invalid))
|
|
|
|
{
|
|
|
|
throw std::runtime_error{"bad irc argument"};
|
|
|
|
}
|
|
|
|
|
|
|
|
front += " ";
|
|
|
|
front += next;
|
|
|
|
write_irc(std::move(front), rest...);
|
|
|
|
}
|