yail/main.cpp

59 lines
2.4 KiB
C++
Raw Normal View History

2024-10-22 18:23:46 +00:00
#include <unordered_set>
#include <utempl/tuple.hpp>
#include <yail/yail.hpp>
2024-10-23 17:54:36 +00:00
struct UserEvent {};
2024-10-22 18:23:46 +00:00
auto Test() -> boost::asio::awaitable<void> {
try {
2024-10-22 18:28:09 +00:00
auto executor = co_await boost::asio::this_coro::executor;
2024-10-22 18:23:46 +00:00
// 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
2024-10-24 09:50:00 +00:00
.With([](auto& self, yail::event::AnyMessage message) -> boost::asio::awaitable<void> {
std::println("Readed message: {}", message.data);
co_return;
})
// NOLINTNEXTLINE
2024-10-23 17:54:36 +00:00
.With([](auto& self, UserEvent event) -> boost::asio::awaitable<void> {
std::println("User event!");
co_return;
})
// NOLINTNEXTLINE
2024-10-22 18:23:46 +00:00
.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");
2024-10-23 17:54:36 +00:00
co_await self.SendEvent(UserEvent{});
2024-10-22 18:23:46 +00:00
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();
};