xbot/driver/main.cpp

114 lines
3.2 KiB
C++
Raw Normal View History

2025-01-27 18:55:19 -08:00
#include "bot.hpp"
2025-01-28 17:15:13 -08:00
#include "challenge.hpp"
2025-01-27 18:55:19 -08:00
#include "client.hpp"
2023-11-25 09:22:55 -08:00
#include "connection.hpp"
2025-01-29 09:54:17 -08:00
#include "openssl_utils.hpp"
2025-01-27 18:55:19 -08:00
#include "registration.hpp"
2023-11-22 19:59:34 -08:00
#include "settings.hpp"
2025-01-28 20:01:51 -08:00
#include "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 <iostream>
#include <memory>
2025-01-26 19:35:56 -08:00
using namespace std::literals;
2025-01-23 00:47:05 -08:00
2025-01-27 18:55:19 -08:00
static auto start(boost::asio::io_context &io, const Settings &settings) -> void
{
2025-01-28 17:15:13 -08:00
X509_Ref cert;
2025-01-27 18:55:19 -08:00
if (settings.use_tls && not settings.tls_certfile.empty())
{
cert = cert_from_file(settings.tls_certfile);
}
2025-01-28 17:15:13 -08:00
EVP_PKEY_Ref key;
2025-01-27 18:55:19 -08:00
if (settings.use_tls && not settings.tls_keyfile.empty())
{
2025-01-28 17:15:13 -08:00
key = key_from_file(settings.tls_keyfile, "");
2025-01-27 18:55:19 -08:00
}
2025-01-25 15:45:31 -08:00
const auto connection = std::make_shared<Connection>(io);
2025-01-26 19:35:56 -08:00
const auto client = Client::start(*connection);
2025-01-30 09:28:28 -08:00
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,
}, client);
2023-11-22 19:59:34 -08:00
2025-01-26 19:35:56 -08:00
const auto bot = Bot::start(client);
2025-01-22 23:49:48 -08:00
2025-01-29 09:54:17 -08:00
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);
});
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-01-26 19:35:56 -08:00
connection->sig_disconnect.connect(
[&io, &settings, client, bot]() {
client->shutdown();
bot->shutdown();
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-01-26 14:51:44 -08:00
timer->async_wait([&io, &settings, timer](auto) { start(io, settings); });
}
);
2025-01-30 09:28:28 -08:00
bot->sig_command.connect([connection](auto &cmd) {
2025-01-26 19:35:56 -08:00
std::cout << "COMMAND " << cmd.command << " from " << cmd.account << std::endl;
2025-01-29 20:43:03 -08:00
if (cmd.oper == "glguy" && cmd.command == "ping") {
connection->send_notice("glguy", cmd.arguments);
}
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-27 18:55:19 -08:00
.client_cert = std::move(cert),
.client_key = std::move(key),
2025-01-26 14:51:44 -08:00
});
2023-11-22 19:59:34 -08:00
}
2025-01-28 20:01:51 -08:00
static auto get_settings(const char *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-01-28 20:01:51 -08:00
if (argc != 2) {
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{};
start(io, settings);
io.run();
}