split up driver and library

This commit is contained in:
2025-01-30 09:28:28 -08:00
parent 5218ea0892
commit 281937e2c5
31 changed files with 176 additions and 105 deletions

12
driver/CMakeLists.txt Normal file
View File

@@ -0,0 +1,12 @@
add_executable(xbot
main.cpp
settings.cpp
)
target_link_libraries(xbot PRIVATE
myirc
OpenSSL::SSL
Boost::signals2 Boost::log Boost::asio
tomlplusplus_tomlplusplus
PkgConfig::LIBHS
mysocks5 mybase64)

113
driver/main.cpp Normal file
View File

@@ -0,0 +1,113 @@
#include "bot.hpp"
#include "challenge.hpp"
#include "client.hpp"
#include "connection.hpp"
#include "openssl_utils.hpp"
#include "registration.hpp"
#include "settings.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
{
X509_Ref cert;
if (settings.use_tls && not settings.tls_certfile.empty())
{
cert = cert_from_file(settings.tls_certfile);
}
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({
.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);
const auto bot = Bot::start(client);
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);
});
}
}
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](auto &cmd) {
std::cout << "COMMAND " << cmd.command << " from " << cmd.account << std::endl;
if (cmd.oper == "glguy" && cmd.command == "ping") {
connection->send_notice("glguy", cmd.arguments);
}
});
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(const char *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();
}

28
driver/settings.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include "settings.hpp"
#define TOML_ENABLE_FORMATTERS 0
#include <toml++/toml.hpp>
auto Settings::from_stream(std::istream &in) -> Settings
{
const auto config = toml::parse(in);
return Settings{
.host = config["host"].value_or(std::string{}),
.service = config["port"].value_or(std::uint16_t{6667}),
.password = config["password"].value_or(std::string{}),
.username = config["username"].value_or(std::string{}),
.realname = config["realname"].value_or(std::string{}),
.nickname = config["nickname"].value_or(std::string{}),
.sasl_mechanism = config["sasl_mechanism"].value_or(std::string{}),
.sasl_authcid = config["sasl_authcid"].value_or(std::string{}),
.sasl_authzid = config["sasl_authzid"].value_or(std::string{}),
.sasl_password = config["sasl_password"].value_or(std::string{}),
.tls_hostname = config["tls_hostname"].value_or(std::string{}),
.tls_certfile = config["tls_certfile"].value_or(std::string{}),
.tls_keyfile = config["tls_keyfile"].value_or(std::string{}),
. challenge_username = config["challenge_username"].value_or(std::string{}),
. challenge_key_file = config["challenge_key_file"].value_or(std::string{}),
. challenge_key_password = config["challenge_key_password"].value_or(std::string{}),
.use_tls = config["use_tls"].value_or(false),
};
}

31
driver/settings.hpp Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <istream>
#include <string>
struct Settings
{
std::string host;
std::uint16_t service;
std::string password;
std::string username;
std::string realname;
std::string nickname;
std::string sasl_mechanism;
std::string sasl_authcid;
std::string sasl_authzid;
std::string sasl_password;
std::string tls_hostname;
std::string tls_certfile;
std::string tls_keyfile;
std::string challenge_username;
std::string challenge_key_file;
std::string challenge_key_password;
bool use_tls;
static auto from_stream(std::istream &in) -> Settings;
};