xbot/registration.cpp

212 lines
4.8 KiB
C++
Raw Normal View History

2025-01-26 19:35:56 -08:00
#include "registration.hpp"
2023-11-25 09:22:55 -08:00
2023-11-27 19:09:45 -08:00
#include "connection.hpp"
#include "ircmsg.hpp"
2025-01-25 12:25:38 -08:00
#include "sasl_mechanism.hpp"
2023-11-27 19:09:45 -08:00
2023-11-25 20:09:20 -08:00
#include <memory>
#include <random>
2023-11-25 20:09:20 -08:00
#include <unordered_map>
2023-11-27 19:09:45 -08:00
#include <unordered_set>
2023-11-25 20:09:20 -08:00
2025-01-26 19:35:56 -08:00
Registration::Registration(
2025-01-25 12:25:38 -08:00
const Settings &settings,
std::shared_ptr<Client> client
2023-11-25 09:22:55 -08:00
)
: settings_{settings}
, client_{std::move(client)}
2023-11-27 14:12:20 -08:00
{
}
2023-11-25 09:22:55 -08:00
2025-01-26 19:35:56 -08:00
auto Registration::on_connect() -> void
2023-11-25 09:22:55 -08:00
{
client_->get_connection().send_cap_ls();
slot_ = client_->get_connection().sig_ircmsg.connect(
[self = shared_from_this()](const auto cmd, auto &msg)
{
self->on_ircmsg(cmd, msg);
}
);
2025-01-26 19:35:56 -08:00
2025-01-25 15:45:31 -08:00
if (not settings_.password.empty())
{
client_->get_connection().send_pass(settings_.password);
2025-01-25 15:45:31 -08:00
}
client_->get_connection().send_user(settings_.username, settings_.realname);
client_->get_connection().send_nick(settings_.nickname);
2023-11-25 09:22:55 -08:00
}
2025-01-26 19:35:56 -08:00
auto Registration::send_req() -> void
2023-11-25 09:22:55 -08:00
{
std::string request;
2025-01-25 15:45:31 -08:00
std::vector<const char *> want{
2023-11-25 20:09:20 -08:00
"account-notify",
2023-11-29 13:54:34 -08:00
"account-tag",
2023-11-25 20:09:20 -08:00
"batch",
"chghost",
2023-11-29 13:54:34 -08:00
"draft/chathistory",
"extended-join",
"invite-notify",
"server-time",
2023-11-25 20:09:20 -08:00
"setname",
2023-11-29 13:54:34 -08:00
"soju.im/no-implicit-names",
2023-11-25 20:09:20 -08:00
"solanum.chat/identify-msg",
2023-11-29 13:54:34 -08:00
"solanum.chat/oper",
2023-11-25 20:09:20 -08:00
"solanum.chat/realhost",
};
2025-01-25 15:45:31 -08:00
if (not settings_.sasl_mechanism.empty())
{
2025-01-25 12:25:38 -08:00
want.push_back("sasl");
}
2025-01-25 15:45:31 -08:00
for (const auto cap : want)
2023-11-25 09:22:55 -08:00
{
if (caps.contains(cap))
{
request.append(cap);
request.push_back(' ');
outstanding.insert(cap);
}
}
2025-01-22 23:49:48 -08:00
2023-11-25 09:22:55 -08:00
if (not outstanding.empty())
{
request.pop_back();
client_->get_connection().send_cap_req(request);
2023-11-25 09:22:55 -08:00
}
else
{
client_->get_connection().send_cap_end();
2023-11-25 09:22:55 -08:00
}
}
2025-01-26 19:35:56 -08:00
auto Registration::on_msg_cap_ack(const IrcMsg &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())
{
2025-01-25 15:45:31 -08:00
if (settings_.sasl_mechanism.empty())
{
client_->get_connection().send_cap_end();
2025-01-25 15:45:31 -08:00
}
else
{
client_->start_sasl(std::make_unique<SaslPlain>(settings_.sasl_authcid, settings_.sasl_authzid, settings_.sasl_password));
2025-01-25 12:25:38 -08:00
}
2023-11-25 09:22:55 -08:00
}
}
2025-01-26 19:35:56 -08:00
auto Registration::on_msg_cap_ls(const IrcMsg &msg) -> void
2023-11-25 09:22:55 -08:00
{
2025-01-25 15:45:31 -08:00
const std::string_view *kvs;
2023-11-27 14:12:20 -08:00
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) {
2025-01-25 15:45:31 -08:00
const auto eq = x.find('=');
2023-11-27 14:12:20 -08:00
if (eq == x.npos)
{
caps.emplace(x, std::string{});
}
else
{
2025-01-25 15:45:31 -08:00
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
}
}
2025-01-26 19:35:56 -08:00
auto Registration::start(
2025-01-25 12:25:38 -08:00
const Settings &settings,
std::shared_ptr<Client> client
2025-01-26 19:35:56 -08:00
) -> std::shared_ptr<Registration>
2023-11-25 09:22:55 -08:00
{
const auto thread = std::make_shared<Registration>(std::move(settings), std::move(client));
2023-11-27 14:12:20 -08:00
thread->slot_ = thread->client_->get_connection().sig_connect.connect([thread]() {
2025-01-26 19:35:56 -08:00
thread->slot_.disconnect();
2023-11-27 14:12:20 -08:00
thread->on_connect();
});
return thread;
}
auto Registration::randomize_nick() -> void
2023-11-27 14:12:20 -08:00
{
std::string new_nick;
new_nick += settings_.nickname.substr(0, 8);
std::random_device rd;
std::mt19937 gen{rd()};
std::uniform_int_distribution<> distrib(0, 35);
for (int i = 0; i < 8; ++i) {
const auto x = distrib(gen);
new_nick += x < 10 ? '0' + x : 'A' + (x-10);
}
client_->get_connection().send_nick(new_nick);
2023-11-27 14:12:20 -08:00
}
auto Registration::on_ircmsg(const IrcCommand cmd, const IrcMsg &msg) -> void
2023-11-27 14:12:20 -08:00
{
switch (cmd)
{
default: break;
case IrcCommand::CAP:
if (msg.args.size() >= 2 && "LS" == msg.args[1]) {
on_msg_cap_ls(msg);
} else if (msg.args.size() >= 2 && "ACK" == msg.args[1]) {
on_msg_cap_ack(msg);
2025-01-25 15:45:31 -08:00
}
break;
case IrcCommand::ERR_NICKNAMEINUSE:
randomize_nick();
break;
case IrcCommand::RPL_WELCOME:
slot_.disconnect();
break;
case IrcCommand::RPL_SASLSUCCESS:
case IrcCommand::ERR_SASLFAIL:
client_->get_connection().send_cap_end();
break;
}
2023-11-25 09:22:55 -08:00
}