xbot/driver/main.cpp

123 lines
3.8 KiB
C++

#include "bot.hpp"
#include "challenge.hpp"
#include "client.hpp"
#include "connection.hpp"
#include "openssl_utils.hpp"
#include "registration.hpp"
#include "settings.hpp"
#include "ref.hpp"
#include "irc_coroutine.hpp"
#include <boost/asio.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <openssl/pem.h>
#include <fstream>
#include <iostream>
#include <memory>
using namespace std::literals;
static auto start(boost::asio::io_context &io, const Settings &settings) -> void
{
Ref<X509> tls_cert;
if (settings.use_tls && not settings.tls_cert_file.empty())
{
tls_cert = cert_from_file(settings.tls_cert_file);
}
Ref<EVP_PKEY> tls_key;
if (settings.use_tls && not settings.tls_key_file.empty())
{
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);
Registration::start({
.nickname = settings.nickname,
.realname = settings.realname,
.username = settings.username,
.password = settings.password,
.sasl_mechanism = settings.sasl_mechanism,
.sasl_authcid = settings.sasl_authcid,
.sasl_authzid = settings.sasl_authzid,
.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);
});
}
}
// 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, 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); });
}
);
// Simple example of a command handler
bot->sig_command.connect([connection](const Bot::Command &cmd) {
if (cmd.oper == "glguy" && cmd.command == "ping") {
if (auto bang = cmd.source.find('!'); bang != cmd.source.npos) {
connection->send_notice(cmd.source.substr(0, bang), cmd.arguments);
}
}
});
connection->start({
.tls = settings.use_tls,
.host = settings.host,
.port = settings.service,
.verify = settings.tls_hostname,
.client_cert = std::move(tls_cert),
.client_key = std::move(tls_key),
});
}
static auto get_settings(const char * const filename) -> Settings
{
if (auto config_stream = std::ifstream{filename})
{
return Settings::from_stream(config_stream);
}
else
{
BOOST_LOG_TRIVIAL(error) << "Unable to open configuration";
std::exit(1);
}
}
auto main(int argc, char *argv[]) -> int
{
//boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::warning);
if (argc != 2) {
BOOST_LOG_TRIVIAL(error) << "Bad arguments";
return 1;
}
const auto settings = get_settings(argv[1]);
auto io = boost::asio::io_context{};
start(io, settings);
io.run();
}