134 lines
4.0 KiB
C++
134 lines
4.0 KiB
C++
#include "settings.hpp"
|
|
#include "web.hpp"
|
|
|
|
#include "myirc/bot.hpp"
|
|
#include "myirc/challenge.hpp"
|
|
#include "myirc/client.hpp"
|
|
#include "myirc/connection.hpp"
|
|
#include "myirc/openssl_utils.hpp"
|
|
#include "myirc/registration.hpp"
|
|
#include "myirc/sasl_mechanism.hpp"
|
|
#include "myirc/ref.hpp"
|
|
#include "myirc/irc_coroutine.hpp"
|
|
|
|
#include <boost/asio.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <boost/log/expressions.hpp>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <fstream>
|
|
#include <memory>
|
|
|
|
using namespace std::literals;
|
|
using myirc::Bot;
|
|
using myirc::Client;
|
|
using myirc::Connection;
|
|
using myirc::Registration;
|
|
using myirc::Challenge;
|
|
using myirc::Ref;
|
|
|
|
static auto start_irc(
|
|
boost::asio::io_context &io,
|
|
const Settings &settings,
|
|
std::shared_ptr<Webhooks> webhook
|
|
) -> void
|
|
{
|
|
Ref<X509> tls_cert;
|
|
if (settings.use_tls && not settings.tls_cert_file.empty())
|
|
{
|
|
tls_cert = myirc::cert_from_file(settings.tls_cert_file);
|
|
}
|
|
|
|
Ref<EVP_PKEY> tls_key;
|
|
if (settings.use_tls && not settings.tls_key_file.empty())
|
|
{
|
|
tls_key = myirc::key_from_file(settings.tls_key_file, settings.tls_key_password);
|
|
}
|
|
|
|
const auto connection = std::make_shared<Connection>(io);
|
|
const auto client = Client::start(connection);
|
|
const auto bot = Bot::start(client);
|
|
|
|
Registration::start({
|
|
.nickname = settings.nickname,
|
|
.realname = settings.realname,
|
|
.username = settings.username,
|
|
.password = settings.password,
|
|
.sasl_mechanism = configure_sasl(settings),
|
|
}, client);
|
|
|
|
// Configure CHALLENGE on registration if applicable
|
|
if (not settings.challenge_username.empty() && not settings.challenge_key_file.empty()) {
|
|
if (auto key = myirc::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);
|
|
});
|
|
}
|
|
}
|
|
|
|
client->sig_registered.connect([client, webhook]() {
|
|
webhook->set_client(client);
|
|
});
|
|
|
|
// On disconnect reconnect in 5 seconds
|
|
// connection is captured in the disconnect handler so it can keep itself alive
|
|
connection->sig_disconnect.connect(
|
|
[&io, &settings, connection, webhook]() {
|
|
webhook->clear_client();
|
|
auto timer = std::make_shared<boost::asio::steady_timer>(io);
|
|
timer->expires_after(5s);
|
|
timer->async_wait([&io, &settings, timer, webhook](auto) { start_irc(io, settings, webhook); });
|
|
}
|
|
);
|
|
|
|
// Dispatch commands to the webhook logic
|
|
bot->sig_command.connect([webhook, connection](const Bot::Command &cmd) {
|
|
auto cursor = webhook_commands.find(std::string{cmd.command});
|
|
if (cursor != webhook_commands.end()) {
|
|
try {
|
|
cursor->second(webhook, cmd);
|
|
} catch (const std::exception &e) {
|
|
BOOST_LOG_TRIVIAL(error) << "Command handler failed: " << e.what();
|
|
}
|
|
}
|
|
});
|
|
|
|
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 != 3) {
|
|
BOOST_LOG_TRIVIAL(error) << "Bad arguments";
|
|
return 1;
|
|
}
|
|
const auto settings = get_settings(argv[1]);
|
|
auto io = boost::asio::io_context{};
|
|
auto webhooks = start_webhook(io, argv[2]);
|
|
start_irc(io, settings, webhooks);
|
|
io.run();
|
|
}
|