123 lines
3.1 KiB
C++
123 lines
3.1 KiB
C++
#include <boost/asio.hpp>
|
|
|
|
#include "connection.hpp"
|
|
#include "ircmsg.hpp"
|
|
#include "linebuffer.hpp"
|
|
#include "settings.hpp"
|
|
#include "thread.hpp"
|
|
#include "write_irc.hpp"
|
|
|
|
#include "irc_parse_thread.hpp"
|
|
#include "registration_thread.hpp"
|
|
#include "ping_thread.hpp"
|
|
#include "watchdog_thread.hpp"
|
|
#include "snote_thread.hpp"
|
|
#include "self_thread.hpp"
|
|
#include "command_thread.hpp"
|
|
|
|
#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;
|
|
|
|
auto unhandled_message_thread(Connection& connection) -> void
|
|
{
|
|
connection.add_listener<IrcMsgEvent>([](IrcMsgEvent const& event)
|
|
{
|
|
if (IrcCommand::UNKNOWN == event.command)
|
|
{
|
|
auto& irc = event.irc;
|
|
std::cout << "Unknown message " << irc.command;
|
|
for (auto const arg : irc.args)
|
|
{
|
|
std::cout << " " << arg;
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
});
|
|
}
|
|
|
|
auto echo_thread(Connection& connection) -> void
|
|
{
|
|
connection.add_listener<CommandEvent>([&connection](CommandEvent& event)
|
|
{
|
|
if ("raw" == event.command
|
|
and "glguy" == event.oper
|
|
and "glguy" == event.account)
|
|
{
|
|
auto txt = std::string{event.arg};
|
|
txt += "\r\n";
|
|
connection.write_line(std::move(txt));
|
|
event.handled_ = true;
|
|
send_notice(connection, event.nick, "ack");
|
|
}
|
|
});
|
|
}
|
|
|
|
auto start(boost::asio::io_context & io, Settings const& settings) -> void
|
|
{
|
|
auto connection = std::make_shared<Connection>(io);
|
|
|
|
watchdog_thread(*connection);
|
|
irc_parse_thread(*connection);
|
|
ping_thread(*connection);
|
|
auto const self_thread = SelfThread::start(*connection);
|
|
registration_thread(*connection, settings.password, settings.username, settings.realname, settings.nickname);
|
|
SnoteThread::start(*connection);
|
|
CommandThread::start(*connection);
|
|
echo_thread(*connection);
|
|
unhandled_message_thread(*connection);
|
|
|
|
connection->add_listener<SnoteEvent>([](SnoteEvent& event)
|
|
{
|
|
std::cout << "Snote match " << static_cast<int>(event.tag) << 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_from_now(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
|
|
{
|
|
auto const settings = get_settings();
|
|
auto io = boost::asio::io_context{};
|
|
start(io, settings);
|
|
io.run();
|
|
}
|