xbot/main.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2023-11-25 09:22:55 -08:00
#include "connection.hpp"
2023-11-22 19:59:34 -08:00
#include "ircmsg.hpp"
#include "settings.hpp"
2023-11-25 20:09:20 -08:00
#include "write_irc.hpp"
2023-11-25 09:22:55 -08:00
2023-11-29 13:13:48 -08:00
#include <boost/asio.hpp>
2023-11-22 19:59:34 -08:00
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
2025-01-22 23:49:48 -08:00
#include "ping_thread.hpp"
#include "registration_thread.hpp"
#include "self_thread.hpp"
#include "snote_thread.hpp"
2023-11-22 19:59:34 -08:00
2025-01-23 12:46:52 -08:00
#include "irc_coroutine.hpp"
2025-01-23 00:47:05 -08:00
2025-01-23 12:46:52 -08:00
using namespace std::chrono_literals;
2025-01-23 00:47:05 -08:00
irc_coroutine example(Connection& connection) {
2025-01-23 12:46:52 -08:00
auto [cmd1, msg1] = co_await wait_command{IrcCommand::RPL_WELCOME};
std::cout << "WELCOME " << msg1.args[0] << "\n";
auto [cmd5, msg5] = co_await wait_command{IrcCommand::RPL_ISUPPORT};
std::cout << "ISUPPORT " << msg5.args[0] << "\n";
2025-01-23 00:47:05 -08:00
}
2023-11-22 19:59:34 -08:00
auto start(boost::asio::io_context & io, Settings const& settings) -> void
{
2023-11-29 09:44:36 -08:00
auto const connection = std::make_shared<Connection>(io);
2023-11-22 19:59:34 -08:00
2025-01-22 23:49:48 -08:00
RegistrationThread::start(*connection, settings.password, settings.username, settings.realname, settings.nickname);
2023-11-27 14:12:20 -08:00
PingThread::start(*connection);
SelfThread::start(*connection);
2025-01-22 23:49:48 -08:00
auto const snote_thread = SnoteThread::start(*connection);
2025-01-23 00:47:05 -08:00
/*
2025-01-22 23:49:48 -08:00
snote_thread->sig_snote.connect([](auto tag, auto &match) {
std::cout << "SNOTE " << static_cast<int>(tag) << std::endl;
for (auto c : match.get_results())
2023-11-28 11:34:27 -08:00
{
std::cout << " " << std::string_view{c.first, c.second} << std::endl;
}
});
2025-01-23 00:47:05 -08:00
*/
auto logic = example(*connection);
2025-01-23 12:46:52 -08:00
logic.start(*connection);
2023-11-28 11:34:27 -08:00
2023-11-22 19:59:34 -08:00
boost::asio::co_spawn(
io,
2023-11-25 09:22:55 -08:00
connection->connect(io, settings.host, settings.service),
2023-11-22 19:59:34 -08:00
[&io, &settings](std::exception_ptr e)
{
2023-11-25 09:22:55 -08:00
auto timer = std::make_shared<boost::asio::steady_timer>(io);
2025-01-22 23:49:48 -08:00
2025-01-22 20:33:17 -08:00
timer->expires_after(5s);
2023-11-25 09:22:55 -08:00
timer->async_wait([&io, &settings, timer](auto) {
2023-11-22 19:59:34 -08:00
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
{
auto const settings = get_settings();
auto io = boost::asio::io_context{};
start(io, settings);
io.run();
}