xbot/main.cpp

123 lines
3.1 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#include <boost/asio.hpp>
2023-11-25 09:22:55 -08:00
#include "connection.hpp"
2023-11-22 19:59:34 -08:00
#include "ircmsg.hpp"
2023-11-25 09:22:55 -08:00
#include "linebuffer.hpp"
2023-11-22 19:59:34 -08:00
#include "settings.hpp"
2023-11-25 09:22:55 -08:00
#include "thread.hpp"
2023-11-25 20:09:20 -08:00
#include "write_irc.hpp"
2023-11-25 09:22:55 -08:00
#include "irc_parse_thread.hpp"
#include "registration_thread.hpp"
2023-11-25 20:09:20 -08:00
#include "ping_thread.hpp"
#include "watchdog_thread.hpp"
#include "snote_thread.hpp"
2023-11-26 16:48:21 -08:00
#include "self_thread.hpp"
2023-11-26 19:59:12 -08:00
#include "command_thread.hpp"
2023-11-22 19:59:34 -08:00
#include <algorithm>
#include <chrono>
#include <fstream>
#include <coroutine>
#include <iostream>
#include <limits>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std::chrono_literals;
2023-11-26 15:40:40 -08:00
auto unhandled_message_thread(Connection& connection) -> void
2023-11-22 19:59:34 -08:00
{
2023-11-26 15:40:40 -08:00
connection.add_listener<IrcMsgEvent>([](IrcMsgEvent const& event)
2023-11-22 19:59:34 -08:00
{
2023-11-26 15:08:55 -08:00
if (IrcCommand::UNKNOWN == event.command)
2023-11-22 19:59:34 -08:00
{
2023-11-25 20:09:20 -08:00
auto& irc = event.irc;
2023-11-26 15:08:55 -08:00
std::cout << "Unknown message " << irc.command;
2023-11-25 09:22:55 -08:00
for (auto const arg : irc.args)
2023-11-22 19:59:34 -08:00
{
2023-11-25 09:22:55 -08:00
std::cout << " " << arg;
2023-11-22 19:59:34 -08:00
}
2023-11-25 09:22:55 -08:00
std::cout << "\n";
2023-11-22 19:59:34 -08:00
}
2023-11-25 20:09:20 -08:00
});
}
2023-11-22 19:59:34 -08:00
2023-11-26 16:48:21 -08:00
auto echo_thread(Connection& connection) -> void
{
2023-11-26 19:59:12 -08:00
connection.add_listener<CommandEvent>([&connection](CommandEvent& event)
2023-11-26 16:48:21 -08:00
{
2023-11-26 19:59:12 -08:00
if ("raw" == event.command
and "glguy" == event.oper
and "glguy" == event.account)
2023-11-26 16:48:21 -08:00
{
2023-11-26 19:59:12 -08:00
auto txt = std::string{event.arg};
txt += "\r\n";
connection.write_line(std::move(txt));
event.handled_ = true;
send_notice(connection, event.nick, "ack");
2023-11-26 16:48:21 -08:00
}
});
}
2023-11-22 19:59:34 -08:00
auto start(boost::asio::io_context & io, Settings const& settings) -> void
{
auto connection = std::make_shared<Connection>(io);
2023-11-26 15:32:52 -08:00
watchdog_thread(*connection);
2023-11-26 15:40:40 -08:00
irc_parse_thread(*connection);
ping_thread(*connection);
2023-11-26 16:48:21 -08:00
auto const self_thread = SelfThread::start(*connection);
2023-11-26 15:40:40 -08:00
registration_thread(*connection, settings.password, settings.username, settings.realname, settings.nickname);
2023-11-26 21:16:56 -08:00
SnoteThread::start(*connection);
2023-11-26 19:59:12 -08:00
CommandThread::start(*connection);
2023-11-26 16:48:21 -08:00
echo_thread(*connection);
2023-11-26 15:40:40 -08:00
unhandled_message_thread(*connection);
2023-11-22 19:59:34 -08:00
2023-11-26 21:16:56 -08:00
connection->add_listener<SnoteEvent>([](SnoteEvent& event)
{
std::cout << "Snote match " << static_cast<int>(event.tag) << std::endl;
});
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);
timer->expires_from_now(5s);
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();
}