80 lines
2.2 KiB
C++
80 lines
2.2 KiB
C++
#include "connection.hpp"
|
|
#include "settings.hpp"
|
|
|
|
#include <boost/asio.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include "bot.hpp"
|
|
#include "registration_thread.hpp"
|
|
#include "self_thread.hpp"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
auto start(boost::asio::io_context &io, const Settings &settings) -> void
|
|
{
|
|
const auto connection = std::make_shared<Connection>(io);
|
|
|
|
const auto selfThread = SelfThread::start(*connection);
|
|
RegistrationThread::start(*connection, settings, selfThread);
|
|
Bot::start(selfThread);
|
|
|
|
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;
|
|
}
|
|
});
|
|
|
|
boost::asio::co_spawn(
|
|
io, connection->connect(io, ConnectSettings{
|
|
.tls = settings.use_tls,
|
|
.host = settings.host,
|
|
.port = settings.service,
|
|
.verify = settings.tls_hostname,
|
|
}),
|
|
[&io, &settings, connection](std::exception_ptr e) {
|
|
try
|
|
{
|
|
if (e)
|
|
std::rethrow_exception(e);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
BOOST_LOG_TRIVIAL(debug) << "TERMINATED: " << e.what();
|
|
}
|
|
|
|
auto timer = std::make_shared<boost::asio::steady_timer>(io);
|
|
|
|
timer->expires_after(5s);
|
|
timer->async_wait(
|
|
[&io, &settings, timer](auto) { start(io, settings); });
|
|
});
|
|
}
|
|
|
|
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();
|
|
}
|