xbot/main.cpp

66 lines
1.6 KiB
C++

#include "connection.hpp"
#include "settings.hpp"
#include <boost/asio.hpp>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include "registration_thread.hpp"
#include "self_thread.hpp"
#include "bot.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, settings.host, settings.service),
[&io, &settings](std::exception_ptr e) {
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
{
std::cerr << "Unable to open config.toml\n";
std::exit(1);
}
}
auto main() -> int
{
const auto settings = get_settings();
auto io = boost::asio::io_context{};
start(io, settings);
io.run();
}