use shared-ptr more consistently

This commit is contained in:
2025-01-31 08:38:14 -08:00
parent eb01b304e3
commit 7728bc6aee
8 changed files with 64 additions and 64 deletions

View File

@@ -23,23 +23,26 @@ using namespace std::literals;
static auto start(boost::asio::io_context &io, const Settings &settings) -> void
{
Ref<X509> cert;
if (settings.use_tls && not settings.tls_certfile.empty())
Ref<X509> tls_cert;
if (settings.use_tls && not settings.tls_cert_file.empty())
{
cert = cert_from_file(settings.tls_certfile);
tls_cert = cert_from_file(settings.tls_cert_file);
}
Ref<EVP_PKEY> key;
if (settings.use_tls && not settings.tls_keyfile.empty())
Ref<EVP_PKEY> tls_key;
if (settings.use_tls && not settings.tls_key_file.empty())
{
key = key_from_file(settings.tls_keyfile, "");
tls_key = key_from_file(settings.tls_key_file, settings.tls_key_password);
}
Ref<EVP_PKEY> sasl_key;
if (not settings.sasl_key_file.empty())
{
sasl_key = key_from_file(settings.sasl_key_file, settings.sasl_key_password);
}
const auto connection = std::make_shared<Connection>(io);
const auto client = Client::start(*connection);
Ref<EVP_PKEY> sasl_key;
if (not settings.sasl_key_file.empty())
sasl_key = key_from_file(settings.sasl_key_file, settings.sasl_key_password);
const auto client = Client::start(connection);
Registration::start({
.nickname = settings.nickname,
.realname = settings.realname,
@@ -51,32 +54,33 @@ static auto start(boost::asio::io_context &io, const Settings &settings) -> void
.sasl_password = settings.sasl_password,
.sasl_key = std::move(sasl_key),
}, client);
const auto bot = Bot::start(client);
// Configure CHALLENGE on registration if applicable
if (not settings.challenge_username.empty() && not settings.challenge_key_file.empty()) {
if (auto key = key_from_file(settings.challenge_key_file, settings.challenge_key_password)) {
client->sig_registered.connect([&settings, connection, key = std::move(key)]() {
Challenge::start(*connection, settings.challenge_username, key);
Challenge::start(connection, settings.challenge_username, key);
});
}
}
// On disconnect tear down the various layers and reconnect in 5 seconds
// connection is captured in the disconnect handler so it can keep itself alive
connection->sig_disconnect.connect(
[&io, &settings, client, bot]() {
client->shutdown();
bot->shutdown();
[&io, &settings, connection]() {
auto timer = std::make_shared<boost::asio::steady_timer>(io);
timer->expires_after(5s);
timer->async_wait([&io, &settings, timer](auto) { start(io, settings); });
}
);
bot->sig_command.connect([connection](auto &cmd) {
std::cout << "COMMAND " << cmd.command << " from " << cmd.account << std::endl;
// Simple example of a command handler
bot->sig_command.connect([connection](const Bot::Command &cmd) {
if (cmd.oper == "glguy" && cmd.command == "ping") {
connection->send_notice("glguy", cmd.arguments);
if (auto bang = cmd.source.find('!'); bang != cmd.source.npos) {
connection->send_notice(cmd.source.substr(0, bang), cmd.arguments);
}
}
});
@@ -85,12 +89,12 @@ static auto start(boost::asio::io_context &io, const Settings &settings) -> void
.host = settings.host,
.port = settings.service,
.verify = settings.tls_hostname,
.client_cert = std::move(cert),
.client_key = std::move(key),
.client_cert = std::move(tls_cert),
.client_key = std::move(tls_key),
});
}
static auto get_settings(const char *filename) -> Settings
static auto get_settings(const char * const filename) -> Settings
{
if (auto config_stream = std::ifstream{filename})
{

View File

@@ -20,11 +20,12 @@ auto Settings::from_stream(std::istream &in) -> Settings
.sasl_key_file = config["sasl_key_file"].value_or(std::string{}),
.sasl_key_password = config["sasl_key_password"].value_or(std::string{}),
.tls_hostname = config["tls_hostname"].value_or(std::string{}),
.tls_certfile = config["tls_certfile"].value_or(std::string{}),
.tls_keyfile = config["tls_keyfile"].value_or(std::string{}),
. challenge_username = config["challenge_username"].value_or(std::string{}),
. challenge_key_file = config["challenge_key_file"].value_or(std::string{}),
. challenge_key_password = config["challenge_key_password"].value_or(std::string{}),
.tls_cert_file = config["tls_cert_file"].value_or(std::string{}),
.tls_key_file = config["tls_key_file"].value_or(std::string{}),
.tls_key_password = config["tls_key_password"].value_or(std::string{}),
.challenge_username = config["challenge_username"].value_or(std::string{}),
.challenge_key_file = config["challenge_key_file"].value_or(std::string{}),
.challenge_key_password = config["challenge_key_password"].value_or(std::string{}),
.use_tls = config["use_tls"].value_or(false),
};
}

View File

@@ -20,8 +20,9 @@ struct Settings
std::string sasl_key_password;
std::string tls_hostname;
std::string tls_certfile;
std::string tls_keyfile;
std::string tls_cert_file;
std::string tls_key_file;
std::string tls_key_password;
std::string challenge_username;
std::string challenge_key_file;