xbot/main.cpp

148 lines
3.9 KiB
C++
Raw Normal View History

2025-01-27 18:55:19 -08:00
#include "bot.hpp"
#include "c_callback.hpp"
#include "client.hpp"
2023-11-25 09:22:55 -08:00
#include "connection.hpp"
2025-01-27 18:55:19 -08:00
#include "registration.hpp"
2023-11-22 19:59:34 -08:00
#include "settings.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>
2023-11-29 13:13:48 -08:00
2025-01-27 18:55:19 -08:00
#include <openssl/err.h>
#include <openssl/pem.h>
#include <cstdio>
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 log_openssl_errors(const std::string_view prefix) -> void
2023-11-22 19:59:34 -08:00
{
2025-01-27 18:55:19 -08:00
auto err_cb = [prefix](const char *str, size_t len) -> int {
BOOST_LOG_TRIVIAL(error) << prefix << std::string_view{str, len};
return 0;
};
ERR_print_errors_cb(CCallback<decltype(err_cb)>::invoke, &err_cb);
}
static auto cert_from_file(const std::string &filename) -> ConnectSettings::X509_Ref
{
ConnectSettings::X509_Ref cert;
if (const auto fp = fopen(filename.c_str(), "r"))
{
cert = PEM_read_X509(fp, nullptr, nullptr, nullptr);
if (cert.get() == nullptr)
{
log_openssl_errors("Reading certificate: "sv);
}
fclose(fp);
}
else
{
const auto err = strerror(errno);
BOOST_LOG_TRIVIAL(error) << "Opening certificate: " << err;
}
return cert;
}
static auto key_from_file(const std::string &filename) -> ConnectSettings::EVP_PKEY_Ref
{
ConnectSettings::EVP_PKEY_Ref key;
if (const auto fp = fopen(filename.c_str(), "r"))
{
key = PEM_read_PrivateKey(fp, nullptr, nullptr, nullptr);
if (key.get() == nullptr)
{
log_openssl_errors("Reading private key: "sv);
}
fclose(fp);
}
else
{
const auto err = strerror(errno);
BOOST_LOG_TRIVIAL(error) << "Opening private key: " << err;
}
return key;
}
static auto start(boost::asio::io_context &io, const Settings &settings) -> void
{
ConnectSettings::X509_Ref cert;
if (settings.use_tls && not settings.tls_certfile.empty())
{
cert = cert_from_file(settings.tls_certfile);
}
ConnectSettings::EVP_PKEY_Ref key;
if (settings.use_tls && not settings.tls_keyfile.empty())
{
key = key_from_file(settings.tls_keyfile);
}
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);
Registration::start(settings, 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-24 14:48:15 -08:00
connection->sig_snote.connect([](auto &match) {
std::cout << "SNOTE " << static_cast<int>(match.get_tag()) << std::endl;
2025-01-22 23:49:48 -08:00
for (auto c : match.get_results())
2023-11-28 11:34:27 -08:00
{
std::cout << " " << std::string_view{c.first, c.second} << std::endl;
}
});
2025-01-26 19:44:35 -08:00
client->sig_registered.connect([connection]() {
2025-01-26 19:35:56 -08:00
connection->send_join("##glguy"sv);
2025-01-27 18:55:19 -08:00
connection->send_whois("bot"sv);
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-26 19:35:56 -08:00
bot->sig_command.connect([connection](const Command &cmd) {
std::cout << "COMMAND " << cmd.command << " from " << cmd.account << std::endl;
});
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-27 18:55:19 -08:00
static auto get_settings() -> Settings
2023-11-22 19:59:34 -08:00
{
2025-01-25 15:45:31 -08:00
if (auto config_stream = std::ifstream{"config.toml"})
2023-11-22 19:59:34 -08:00
{
return Settings::from_stream(config_stream);
}
else
{
2025-01-26 14:38:13 -08:00
BOOST_LOG_TRIVIAL(error) << "Unable to open config.toml";
2023-11-22 19:59:34 -08:00
std::exit(1);
}
}
auto main() -> int
{
2025-01-25 15:45:31 -08:00
const auto settings = get_settings();
2023-11-22 19:59:34 -08:00
auto io = boost::asio::io_context{};
start(io, settings);
io.run();
}