yail/main.cpp

58 lines
2.4 KiB
C++

#include <unordered_set>
#include <utempl/tuple.hpp>
#include <yail/yail.hpp>
struct UserEvent {};
auto Test() -> boost::asio::awaitable<void> {
try {
auto executor = co_await boost::asio::this_coro::executor;
// NOLINTNEXTLINE
auto sock = co_await yail::Socks5ProxyConnect("127.0.0.1", 4447, "irc.ilita.i2p", 6667);
auto client =
yail::IrcClient<std::tuple<>, utempl::Tuple{}, std::tuple<>>{// clangd bug :(
.socket = std::move(sock),
.nick = "sha512sum_bot",
.server = "irc.ilita.i2p"}
.AddState<std::unordered_set<yail::User, yail::User::Hash>, "ruUsers">()
// NOLINTNEXTLINE
.With([](auto& self, yail::event::AnyMessage message) -> boost::asio::awaitable<void> {
std::println("Readed message: {}", message.data);
co_return;
})
// NOLINTNEXTLINE
.With([](auto& self, UserEvent event) -> boost::asio::awaitable<void> {
std::println("User event!");
co_return;
})
// NOLINTNEXTLINE
.With([](auto& self, yail::event::Ping ping) -> boost::asio::awaitable<void> {
co_await self.Pong(ping.data);
})
// NOLINTNEXTLINE
.With([](auto& self, yail::event::Welcome message) -> boost::asio::awaitable<void> {
std::println("Welcome: {}", message.message);
co_await self.Join("#ru");
co_await self.SendEvent(UserEvent{});
co_await self.SendMessage("#ru", "Hello from yet another irc library");
})
// NOLINTNEXTLINE
.With([](auto& self, yail::event::ChannelUsers event) -> boost::asio::awaitable<void> {
std::unordered_set<yail::User, yail::User::Hash>& ruUsers = self.template GetState<"ruUsers">();
for(yail::UserView view : event.users) {
ruUsers.emplace(view);
}
co_return;
});
co_await client.Connect("sha512sum_bot");
co_await client.Loop();
} catch(const std::exception& err) {
std::println("Err: {}", err.what());
}
};
auto main() -> int {
boost::asio::io_context context;
boost::asio::co_spawn(context, Test(), boost::asio::detached);
context.run();
};