121 lines
3.2 KiB
C++
121 lines
3.2 KiB
C++
#include <boost/asio.hpp>
|
|
#include <boost/asio/ssl.hpp>
|
|
|
|
#include "connection.hpp"
|
|
#include "ircmsg.hpp"
|
|
#include "linebuffer.hpp"
|
|
#include "settings.hpp"
|
|
#include "thread.hpp"
|
|
|
|
#include "irc_parse_thread.hpp"
|
|
#include "ping_thread.hpp"
|
|
#include "registration_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;
|
|
|
|
|
|
struct ChatThread : public Thread
|
|
{
|
|
auto priority() const -> priority_type override
|
|
{
|
|
return 100;
|
|
}
|
|
auto on_event(Event const& event) -> callback_result override
|
|
{
|
|
if (auto const* irc_event = dynamic_cast<IrcMsgEvent const*>(&event))
|
|
{
|
|
auto const& irc = irc_event->irc;
|
|
if (irc.command == "PRIVMSG" && 2 == irc.args.size())
|
|
{
|
|
std::cout << "Chat from " << irc.source << ": " << irc.args[1] << std::endl;
|
|
return { ThreadOutcome::Continue, EventOutcome::Consume };
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
};
|
|
|
|
struct UnhandledThread : public Thread
|
|
{
|
|
auto priority() const -> priority_type override
|
|
{
|
|
return std::numeric_limits<Thread::priority_type>::max();
|
|
}
|
|
|
|
auto on_event(Event const& event) -> callback_result override
|
|
{
|
|
if (auto irc_event = dynamic_cast<IrcMsgEvent const*>(&event))
|
|
{
|
|
auto& irc = irc_event->irc;
|
|
std::cout << "Unhandled message " << irc.command;
|
|
for (auto const arg : irc.args)
|
|
{
|
|
std::cout << " " << arg;
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
return {};
|
|
}
|
|
};
|
|
|
|
auto start(boost::asio::io_context & io, Settings const& settings) -> void
|
|
{
|
|
auto connection = std::make_shared<Connection>(io);
|
|
|
|
connection->add_thread(std::make_shared<IrcParseThread>(connection.get()));
|
|
connection->add_thread(std::make_shared<PingThread>(connection.get()));
|
|
connection->add_thread(std::make_shared<RegistrationThread>(connection.get(), settings.password, settings.username, settings.realname, settings.nickname));
|
|
connection->add_thread(std::make_shared<ChatThread>());
|
|
connection->add_thread(std::make_shared<UnhandledThread>());
|
|
|
|
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();
|
|
}
|