86 lines
2.5 KiB
C++
86 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "ircmsg.hpp"
|
|
#include "irc_command.hpp"
|
|
#include "snote.hpp"
|
|
|
|
#include <boost/asio.hpp>
|
|
#include <boost/signals2.hpp>
|
|
|
|
#include <list>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
class Connection : public std::enable_shared_from_this<Connection>
|
|
{
|
|
private:
|
|
boost::asio::ip::tcp::socket stream_;
|
|
boost::asio::steady_timer write_timer_;
|
|
std::list<std::string> write_strings_;
|
|
|
|
auto writer() -> void;
|
|
auto writer_immediate() -> void;
|
|
auto dispatch_line(char * line) -> void;
|
|
|
|
/// 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;
|
|
|
|
public:
|
|
Connection(boost::asio::io_context & io);
|
|
|
|
boost::signals2::signal<void()> sig_connect;
|
|
boost::signals2::signal<void()> sig_disconnect;
|
|
boost::signals2::signal<void(IrcCommand, const IrcMsg &)> sig_ircmsg;
|
|
boost::signals2::signal<void(SnoteTag, SnoteMatch &)> sig_snote;
|
|
|
|
auto get_executor() -> boost::asio::any_io_executor {
|
|
return stream_.get_executor();
|
|
}
|
|
|
|
auto connect(
|
|
boost::asio::io_context & io,
|
|
std::string host,
|
|
std::string port
|
|
) -> boost::asio::awaitable<void>;
|
|
|
|
auto close() -> void;
|
|
|
|
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;
|
|
};
|
|
|
|
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...);
|
|
}
|