2023-11-25 09:22:55 -08:00
|
|
|
#include "registration_thread.hpp"
|
|
|
|
|
2023-11-27 19:09:45 -08:00
|
|
|
#include "connection.hpp"
|
|
|
|
#include "irc_parse_thread.hpp"
|
|
|
|
#include "ircmsg.hpp"
|
|
|
|
#include "write_irc.hpp"
|
|
|
|
|
2023-11-25 20:09:20 -08:00
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
2023-11-27 19:09:45 -08:00
|
|
|
#include <unordered_set>
|
2023-11-25 20:09:20 -08:00
|
|
|
|
2023-11-25 09:22:55 -08:00
|
|
|
RegistrationThread::RegistrationThread(
|
2023-11-26 15:40:40 -08:00
|
|
|
Connection& connection,
|
2023-11-25 09:22:55 -08:00
|
|
|
std::string password,
|
|
|
|
std::string username,
|
|
|
|
std::string realname,
|
|
|
|
std::string nickname
|
|
|
|
)
|
2023-11-25 20:09:20 -08:00
|
|
|
: connection_{connection}
|
2023-11-25 09:22:55 -08:00
|
|
|
, password_{password}
|
|
|
|
, username_{username}
|
|
|
|
, realname_{realname}
|
|
|
|
, nickname_{nickname}
|
2023-11-27 14:12:20 -08:00
|
|
|
{
|
|
|
|
}
|
2023-11-25 09:22:55 -08:00
|
|
|
|
2023-11-25 20:09:20 -08:00
|
|
|
auto RegistrationThread::on_connect() -> void
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-26 15:40:40 -08:00
|
|
|
send_cap_ls(connection_);
|
|
|
|
send_pass(connection_, password_);
|
|
|
|
send_user(connection_, username_, realname_);
|
|
|
|
send_nick(connection_, nickname_);
|
2023-11-25 20:09:20 -08:00
|
|
|
|
2023-11-26 15:40:40 -08:00
|
|
|
connection_.remove_listener(connect_handle_);
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
|
|
|
|
2023-11-25 20:09:20 -08:00
|
|
|
auto RegistrationThread::send_req() -> void
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
|
|
|
std::string request;
|
2023-11-25 20:09:20 -08:00
|
|
|
char const* const want[] = {
|
|
|
|
"extended-join",
|
|
|
|
"account-notify",
|
|
|
|
"draft/chathistory",
|
|
|
|
"batch",
|
|
|
|
"soju.im/no-implicit-names",
|
|
|
|
"chghost",
|
|
|
|
"setname",
|
|
|
|
"account-tag",
|
|
|
|
"solanum.chat/oper",
|
|
|
|
"solanum.chat/identify-msg",
|
|
|
|
"solanum.chat/realhost",
|
|
|
|
"server-time",
|
|
|
|
"invite-notify",
|
|
|
|
"extended-join"
|
|
|
|
};
|
|
|
|
|
2023-11-25 09:22:55 -08:00
|
|
|
for (auto cap : want)
|
|
|
|
{
|
|
|
|
if (caps.contains(cap))
|
|
|
|
{
|
|
|
|
request.append(cap);
|
|
|
|
request.push_back(' ');
|
|
|
|
outstanding.insert(cap);
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 14:12:20 -08:00
|
|
|
|
|
|
|
connection_.remove_listener(message_handle_);
|
|
|
|
|
2023-11-25 09:22:55 -08:00
|
|
|
if (not outstanding.empty())
|
|
|
|
{
|
|
|
|
request.pop_back();
|
2023-11-26 15:40:40 -08:00
|
|
|
send_cap_req(connection_, request);
|
2023-11-27 14:12:20 -08:00
|
|
|
|
|
|
|
listen_for_cap_ack();
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-26 15:40:40 -08:00
|
|
|
send_cap_end(connection_);
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 14:12:20 -08:00
|
|
|
auto RegistrationThread::on_msg_cap_ack(IrcMsg const& msg) -> void
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
auto in = std::istringstream{std::string{msg.args[2]}};
|
|
|
|
std::for_each(
|
|
|
|
std::istream_iterator<std::string>{in},
|
|
|
|
std::istream_iterator<std::string>{},
|
|
|
|
[this](std::string x) {
|
|
|
|
outstanding.erase(x);
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
2023-11-27 14:12:20 -08:00
|
|
|
);
|
|
|
|
if (outstanding.empty())
|
|
|
|
{
|
|
|
|
send_cap_end(connection_);
|
|
|
|
connection_.remove_listener(message_handle_);
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 14:12:20 -08:00
|
|
|
auto RegistrationThread::on_msg_cap_ls(IrcMsg const& msg) -> void
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
std::string_view const* kvs;
|
|
|
|
bool last;
|
|
|
|
|
|
|
|
if (3 == msg.args.size())
|
|
|
|
{
|
|
|
|
kvs = &msg.args[2];
|
|
|
|
last = true;
|
|
|
|
}
|
|
|
|
else if (4 == msg.args.size() && "*" == msg.args[2])
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
kvs = &msg.args[3];
|
|
|
|
last = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-11-25 09:22:55 -08:00
|
|
|
|
2023-11-27 14:12:20 -08:00
|
|
|
auto in = std::istringstream{std::string{*kvs}};
|
2023-11-25 09:22:55 -08:00
|
|
|
|
2023-11-27 14:12:20 -08:00
|
|
|
std::for_each(
|
|
|
|
std::istream_iterator<std::string>{in},
|
|
|
|
std::istream_iterator<std::string>{},
|
|
|
|
[this](std::string x) {
|
|
|
|
auto const eq = x.find('=');
|
|
|
|
if (eq == x.npos)
|
|
|
|
{
|
|
|
|
caps.emplace(x, std::string{});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
caps.emplace(std::string{x, 0, eq}, std::string{x, eq+1, x.npos});
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
|
|
|
}
|
2023-11-27 14:12:20 -08:00
|
|
|
);
|
2023-11-25 09:22:55 -08:00
|
|
|
|
2023-11-27 14:12:20 -08:00
|
|
|
if (last)
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
send_req();
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 14:12:20 -08:00
|
|
|
auto RegistrationThread::start(
|
2023-11-26 15:40:40 -08:00
|
|
|
Connection& connection,
|
2023-11-25 20:09:20 -08:00
|
|
|
std::string password,
|
|
|
|
std::string username,
|
|
|
|
std::string realname,
|
|
|
|
std::string nickname
|
2023-11-27 14:12:20 -08:00
|
|
|
) -> std::shared_ptr<RegistrationThread>
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-26 15:08:55 -08:00
|
|
|
auto const thread = std::make_shared<RegistrationThread>(connection, password, username, realname, nickname);
|
2023-11-27 14:12:20 -08:00
|
|
|
|
|
|
|
thread->listen_for_cap_ls();
|
|
|
|
|
|
|
|
thread->connect_handle_ = connection.add_listener<ConnectEvent>([thread](ConnectEvent const&)
|
|
|
|
{
|
|
|
|
thread->on_connect();
|
|
|
|
});
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto RegistrationThread::listen_for_cap_ack() -> void
|
|
|
|
{
|
|
|
|
message_handle_ = connection_.add_listener<IrcMsgEvent>([thread = shared_from_this()](IrcMsgEvent const& event)
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
if (IrcCommand::CAP == event.command && event.irc.args.size() >= 2 && "*" == event.irc.args[0] && "ACK" == event.irc.args[1])
|
2023-11-26 15:08:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
thread->on_msg_cap_ack(event.irc);
|
2023-11-26 15:08:55 -08:00
|
|
|
}
|
2023-11-25 20:09:20 -08:00
|
|
|
});
|
2023-11-27 14:12:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto RegistrationThread::listen_for_cap_ls() -> void
|
|
|
|
{
|
|
|
|
message_handle_ = connection_.add_listener<IrcMsgEvent>([thread = shared_from_this()](IrcMsgEvent const& event)
|
2023-11-25 09:22:55 -08:00
|
|
|
{
|
2023-11-27 14:12:20 -08:00
|
|
|
if (IrcCommand::CAP == event.command && event.irc.args.size() >= 2 && "*" == event.irc.args[0] && "LS" == event.irc.args[1])
|
|
|
|
{
|
|
|
|
thread->on_msg_cap_ls(event.irc);
|
|
|
|
}
|
2023-11-25 20:09:20 -08:00
|
|
|
});
|
2023-11-25 09:22:55 -08:00
|
|
|
}
|