xbot/driver/main.cpp

164 lines
4.9 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#include "settings.hpp"
2025-02-01 11:04:33 -08:00
#include "web.hpp"
#include "myirc/bot.hpp"
#include "myirc/challenge.hpp"
#include "myirc/client.hpp"
#include "myirc/connection.hpp"
#include "myirc/openssl_utils.hpp"
#include "myirc/registration.hpp"
#include "myirc/sasl_mechanism.hpp"
#include "myirc/ref.hpp"
#include "myirc/irc_coroutine.hpp"
2023-11-25 09:22:55 -08:00
2023-11-29 13:13:48 -08:00
#include <boost/asio.hpp>
2025-01-26 14:38:13 -08:00
#include <boost/log/trivial.hpp>
2025-01-29 20:43:03 -08:00
#include <boost/log/expressions.hpp>
2023-11-29 13:13:48 -08:00
2025-01-27 18:55:19 -08:00
#include <openssl/pem.h>
2023-11-22 19:59:34 -08:00
#include <fstream>
#include <memory>
2025-01-26 19:35:56 -08:00
using namespace std::literals;
2025-02-01 11:04:33 -08:00
using myirc::SaslMechanism;
using myirc::SaslPlain;
using myirc::SaslExternal;
using myirc::SaslEcdsa;
using myirc::Bot;
using myirc::Client;
using myirc::Connection;
using myirc::Registration;
using myirc::Challenge;
using myirc::Ref;
2025-01-23 00:47:05 -08:00
2025-01-31 09:36:08 -08:00
auto configure_sasl(const Settings &settings) -> std::unique_ptr<SaslMechanism>
{
if (settings.sasl_mechanism == "PLAIN" &&
not settings.sasl_authcid.empty()
) {
return std::make_unique<SaslPlain>(
settings.sasl_authcid,
settings.sasl_authzid,
settings.sasl_password);
} else if (settings.sasl_mechanism == "EXTERNAL") {
return std::make_unique<SaslExternal>(settings.sasl_authzid);
} else if (
settings.sasl_mechanism == "ECDSA" &&
not settings.sasl_authcid.empty() &&
not settings.sasl_key_file.empty()
) {
2025-02-01 11:04:33 -08:00
if (auto sasl_key = myirc::key_from_file(settings.sasl_key_file, settings.sasl_key_password))
2025-01-31 09:36:08 -08:00
return std::make_unique<SaslEcdsa>(
settings.sasl_authcid,
settings.sasl_authzid,
std::move(sasl_key));
}
return nullptr;
}
2025-01-27 18:55:19 -08:00
2025-02-01 20:57:57 -08:00
static auto start(
boost::asio::io_context &io,
const Settings &settings,
2025-02-02 15:02:08 -08:00
std::shared_ptr<Webhooks> webhook
2025-02-01 20:57:57 -08:00
) -> void
2025-01-27 18:55:19 -08:00
{
2025-01-31 08:38:14 -08:00
Ref<X509> tls_cert;
if (settings.use_tls && not settings.tls_cert_file.empty())
2025-01-27 18:55:19 -08:00
{
2025-02-01 11:04:33 -08:00
tls_cert = myirc::cert_from_file(settings.tls_cert_file);
2025-01-27 18:55:19 -08:00
}
2025-01-31 08:38:14 -08:00
Ref<EVP_PKEY> tls_key;
if (settings.use_tls && not settings.tls_key_file.empty())
2025-01-27 18:55:19 -08:00
{
2025-02-01 11:04:33 -08:00
tls_key = myirc::key_from_file(settings.tls_key_file, settings.tls_key_password);
2025-01-27 18:55:19 -08:00
}
2025-01-31 08:38:14 -08:00
const auto connection = std::make_shared<Connection>(io);
const auto client = Client::start(connection);
2025-01-31 16:14:13 -08:00
const auto bot = Bot::start(client);
2025-01-30 09:28:28 -08:00
Registration::start({
.nickname = settings.nickname,
.realname = settings.realname,
.username = settings.username,
.password = settings.password,
2025-01-31 16:14:13 -08:00
.sasl_mechanism = configure_sasl(settings),
2025-01-30 09:28:28 -08:00
}, client);
2025-01-22 23:49:48 -08:00
2025-01-31 08:38:14 -08:00
// Configure CHALLENGE on registration if applicable
2025-01-29 09:54:17 -08:00
if (not settings.challenge_username.empty() && not settings.challenge_key_file.empty()) {
2025-02-01 11:04:33 -08:00
if (auto key = myirc::key_from_file(settings.challenge_key_file, settings.challenge_key_password)) {
2025-01-29 09:54:17 -08:00
client->sig_registered.connect([&settings, connection, key = std::move(key)]() {
2025-01-31 08:38:14 -08:00
Challenge::start(connection, settings.challenge_username, key);
2025-01-29 09:54:17 -08:00
});
2025-01-28 17:15:13 -08:00
}
2025-01-29 09:54:17 -08:00
}
2025-01-28 21:43:44 -08:00
2025-02-01 20:57:57 -08:00
client->sig_registered.connect([connection, webhook]() {
webhook->set_connection(connection);
});
2025-01-31 16:14:13 -08:00
// On disconnect reconnect in 5 seconds
2025-01-31 08:38:14 -08:00
// connection is captured in the disconnect handler so it can keep itself alive
2025-01-26 19:35:56 -08:00
connection->sig_disconnect.connect(
2025-02-01 20:57:57 -08:00
[&io, &settings, connection, webhook]() {
webhook->clear_connection();
2023-11-25 09:22:55 -08:00
auto timer = std::make_shared<boost::asio::steady_timer>(io);
2025-01-22 20:33:17 -08:00
timer->expires_after(5s);
2025-02-01 20:57:57 -08:00
timer->async_wait([&io, &settings, timer, webhook](auto) { start(io, settings, webhook); });
2025-01-26 14:51:44 -08:00
}
);
2025-02-02 14:56:34 -08:00
// Dispatch commands to the webhook logic
bot->sig_command.connect([webhook, connection](const Bot::Command &cmd) {
auto cursor = webhook_commands.find(std::string{cmd.command});
if (cursor != webhook_commands.end()) {
cursor->second(webhook, cmd);
2025-01-29 20:43:03 -08:00
}
2025-01-26 19:35:56 -08:00
});
connection->start({
2025-01-26 14:51:44 -08:00
.tls = settings.use_tls,
.host = settings.host,
.port = settings.service,
.verify = settings.tls_hostname,
2025-01-31 08:38:14 -08:00
.client_cert = std::move(tls_cert),
.client_key = std::move(tls_key),
2025-01-26 14:51:44 -08:00
});
2023-11-22 19:59:34 -08:00
}
2025-01-31 08:38:14 -08:00
static auto get_settings(const char * const filename) -> Settings
2023-11-22 19:59:34 -08:00
{
2025-01-28 20:01:51 -08:00
if (auto config_stream = std::ifstream{filename})
2023-11-22 19:59:34 -08:00
{
return Settings::from_stream(config_stream);
}
else
{
2025-01-28 20:01:51 -08:00
BOOST_LOG_TRIVIAL(error) << "Unable to open configuration";
2023-11-22 19:59:34 -08:00
std::exit(1);
}
}
2025-01-28 20:01:51 -08:00
auto main(int argc, char *argv[]) -> int
2023-11-22 19:59:34 -08:00
{
2025-01-30 09:28:28 -08:00
//boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::warning);
2025-02-01 20:57:57 -08:00
if (argc != 3) {
2025-01-28 20:01:51 -08:00
BOOST_LOG_TRIVIAL(error) << "Bad arguments";
return 1;
}
const auto settings = get_settings(argv[1]);
2023-11-22 19:59:34 -08:00
auto io = boost::asio::io_context{};
2025-02-01 20:57:57 -08:00
auto webhooks = start_webhook(io, argv[2]);
start(io, settings, webhooks);
2023-11-22 19:59:34 -08:00
io.run();
}