larra/examples/src/connect.cpp
Ivan-lis 55ea39a89c
All checks were successful
PR Check / on-push-commit-check (push) Successful in 19m6s
Added tests for Iq Stanza errors
2024-12-19 17:50:01 +00:00

67 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <larra/client/client.hpp>
#include <larra/presence.hpp>
#include <larra/printer_stream.hpp>
#include <print>
namespace iq = larra::xmpp::iq;
auto Coroutine() -> boost::asio::awaitable<void> {
SPDLOG_INFO("Connecting client...");
try {
auto client = co_await larra::xmpp::client::CreateClient<larra::xmpp::PrintStream<boost::asio::ip::tcp::socket>>(
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
{.useTls = larra::xmpp::client::Options::kNever});
// rfc6120 7.1
// After a client authenticates with a server,
// it MUST bind a specific resource to the stream so that the server can properly address the client.
co_await std::visit(
[](auto& client) -> boost::asio::awaitable<void> {
co_await client.CreateResourceBind();
},
client);
co_await std::visit(
[](auto& client) -> boost::asio::awaitable<void> {
co_await client.UpdateListOfContacts();
},
client);
// rfc6120 2.2
// Upon authenticating with a server and binding a resource (thus becoming a connected resource as defined in [XMPPCORE]),
// a client SHOULD request the roster before sending initial presence
co_await std::visit(
[](auto& client) -> boost::asio::awaitable<void> {
SPDLOG_INFO("Send presence: Available");
co_await client.Send(larra::xmpp::presence::c2s::Available{});
},
client);
} catch(const std::exception& err) {
SPDLOG_ERROR("{}", err.what());
co_return;
}
SPDLOG_INFO("Done connecting client!");
}
auto main() -> int {
spdlog::set_level(spdlog::level::trace);
#ifdef SPDLOG_ACTIVE_LEVEL
std::println("\n\tCompiled max availabel log level: {}\n\tCurrently set log level: {}",
SPDLOG_ACTIVE_LEVEL,
std::to_underlying(spdlog::get_level()));
#else
std::println("\n\tCurrently set log level: {}", std::to_underlying(spdlog::get_level()));
#endif
boost::asio::io_context io_context;
boost::asio::co_spawn(io_context, Coroutine(), boost::asio::detached);
io_context.run();
}