149 lines
3.9 KiB
C++
149 lines
3.9 KiB
C++
#include "bot.hpp"
|
|
#include "c_callback.hpp"
|
|
#include "client.hpp"
|
|
#include "connection.hpp"
|
|
#include "registration.hpp"
|
|
#include "settings.hpp"
|
|
|
|
#include <boost/asio.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
#include <openssl/err.h>
|
|
#include <openssl/pem.h>
|
|
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <memory>
|
|
|
|
using namespace std::literals;
|
|
|
|
static auto log_openssl_errors(const std::string_view prefix) -> void
|
|
{
|
|
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);
|
|
}
|
|
|
|
const auto connection = std::make_shared<Connection>(io);
|
|
const auto client = Client::start(*connection);
|
|
Registration::start(settings, client);
|
|
|
|
const auto bot = Bot::start(client);
|
|
|
|
/*
|
|
connection->sig_snote.connect([](auto &match) {
|
|
std::cout << "SNOTE " << static_cast<int>(match.get_tag()) << std::endl;
|
|
for (auto c : match.get_results())
|
|
{
|
|
std::cout << " " << std::string_view{c.first, c.second} << std::endl;
|
|
}
|
|
});
|
|
*/
|
|
client->sig_registered.connect([connection, client]() {
|
|
connection->send_join("##glguy"sv);
|
|
connection->send_whois(client->get_my_nick());
|
|
});
|
|
|
|
connection->sig_disconnect.connect(
|
|
[&io, &settings, client, bot]() {
|
|
client->shutdown();
|
|
bot->shutdown();
|
|
|
|
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](const Command &cmd) {
|
|
std::cout << "COMMAND " << cmd.command << " from " << cmd.account << std::endl;
|
|
});
|
|
|
|
connection->start({
|
|
.tls = settings.use_tls,
|
|
.host = settings.host,
|
|
.port = settings.service,
|
|
.verify = settings.tls_hostname,
|
|
.client_cert = std::move(cert),
|
|
.client_key = std::move(key),
|
|
});
|
|
}
|
|
|
|
static auto get_settings() -> Settings
|
|
{
|
|
if (auto config_stream = std::ifstream{"config.toml"})
|
|
{
|
|
return Settings::from_stream(config_stream);
|
|
}
|
|
else
|
|
{
|
|
BOOST_LOG_TRIVIAL(error) << "Unable to open config.toml";
|
|
std::exit(1);
|
|
}
|
|
}
|
|
|
|
auto main() -> int
|
|
{
|
|
const auto settings = get_settings();
|
|
auto io = boost::asio::io_context{};
|
|
start(io, settings);
|
|
io.run();
|
|
}
|