Base minimal working example with presence and Roster
Some checks failed
PR Check / on-push-commit-check (push) Failing after 2m31s

This commit is contained in:
Ivan-lis 2024-11-09 13:22:42 +00:00
parent 5c250ac434
commit 850c7e5412
4 changed files with 102 additions and 29 deletions

View file

@ -8,6 +8,8 @@
#include <larra/printer_stream.hpp>
#include <print>
#include "larra/bind.hpp"
namespace iq = larra::xmpp::iq;
auto Coroutine() -> boost::asio::awaitable<void> {
@ -18,10 +20,14 @@ auto Coroutine() -> boost::asio::awaitable<void> {
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
{.useTls = larra::xmpp::client::Options::kNever});
// TODO(unknown):
// 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> {

View file

@ -0,0 +1,54 @@
#pragma once
#include <libxml++/libxml++.h>
#include <spdlog/spdlog.h>
#include <larra/iq.hpp>
#include <larra/jid.hpp>
#include <optional>
namespace larra::xmpp::iq {
struct Bind {
static constexpr auto kDefaultName = "bind";
static constexpr auto kDefaultNamespace = "urn:ietf:params:xml:ns:xmpp-bind";
std::optional<FullJid> jid;
friend constexpr auto operator<<(xmlpp::Element* element, const Bind& Bind) {
element->set_attribute("xmlns", Bind::kDefaultNamespace);
if(Bind.jid) {
auto* jid_el = element->add_child_element("jid");
jid_el->add_child_text(ToString(*Bind.jid));
}
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Bind {
const auto* jid_node = element->get_first_child("jid");
if(!jid_node) {
SPDLOG_DEBUG("No Jid Node at Iq::Bind");
return {};
}
auto* jid_el = dynamic_cast<const xmlpp::Element*>(jid_node);
if(!jid_el) {
throw std::runtime_error("dynamic_cast to const xmlpp::Element* failed");
}
const auto* text = jid_el->get_first_child_text();
if(!jid_el) {
throw std::runtime_error("No text at Iq::Bind jid child");
}
return {.jid = (jid_node ? std::optional{FullJid::Parse(text->get_content())} : std::nullopt)};
}
};
using SetBind = Set<Bind>;
using ResultBind = Result<Bind>;
using IqBind = Iq<Bind>;
auto MakeSetBind() {
IqBind iq_bind{std::in_place_type<SetBind>, SetBind{.id = "1", .payload = Bind{}}};
return iq_bind;
}
} // namespace larra::xmpp::iq

View file

@ -22,7 +22,10 @@
#include <larra/xml_stream.hpp>
#include <ranges>
#include <utility>
#include "larra/bind.hpp"
#include "larra/iq.hpp"
#include "larra/jid.hpp"
#include "larra/roster.hpp"
namespace rng = std::ranges;
@ -77,28 +80,41 @@ struct Client {
return this->jid;
}
auto CreateResourceBind() -> boost::asio::awaitable<void> {
SPDLOG_INFO("Send IQ: Set::Bind");
co_await std::visit(
[&](auto&& query) -> boost::asio::awaitable<void> {
co_await this->Send(query);
},
::iq::MakeSetBind());
auto bind_result = co_await connection.template Read<::iq::ResultBind>();
resource_bind = std::move(bind_result.payload.jid->resource);
co_return;
}
auto UpdateListOfContacts() -> boost::asio::awaitable<void> {
SPDLOG_INFO("Send IQ: Get::Roster");
co_await std::visit(
[&](auto&& query) -> boost::asio::awaitable<void> {
co_await this->Send(query);
}, ::iq::MakeGetRoster(jid));
},
::iq::MakeGetRoster(FullJid{}.Username(jid.username).Server(jid.server).Resource(resource_bind)));
// IqRoster roster = co_await connection.template Read<IqRoster>();
// const auto& roster_result = std::get<::iq::ResultRoster>(roster);
::iq::ResultRoster roster_result = co_await connection.template Read<::iq::ResultRoster>();
const auto roster_result = co_await connection.template Read<::iq::ResultRoster>();
for(const auto& jid : roster_result.payload.items) {
SPDLOG_INFO("\t'{}'", ToString(jid));
}
co_return;
}
private:
bool active = true;
XmlStream<Connection> connection{};
BareJid jid;
std::string resource_bind;
};
struct StartTlsNegotiationError : std::runtime_error {

View file

@ -2,10 +2,10 @@
#include <libxml++/libxml++.h>
#include <spdlog/spdlog.h>
#include <larra/iq.hpp>
#include <larra/jid.hpp>
#include <ranges>
#include <vector>
#include <larra/iq.hpp>
namespace larra::xmpp::iq {
@ -24,9 +24,7 @@ struct Roster {
SPDLOG_DEBUG("No items at Iq::Roster");
}
return {
.items = item_nodes
| std::views::transform([](const xmlpp::Node* node) {
return {.items = item_nodes | std::views::transform([](const xmlpp::Node* node) {
auto item_element = dynamic_cast<const xmlpp::Element*>(node);
if(!item_element) {
throw std::runtime_error("Can't convert xmlpp::Node to xmlpp::Element");
@ -38,17 +36,16 @@ struct Roster {
}
return BareJid::Parse(jid_ptr->get_value());
})
| std::ranges::to<std::vector<BareJid>>()};
}) |
std::ranges::to<std::vector<BareJid>>()};
}
};
using GetRoster = Get<Roster>;
using ResultRoster = Result<Roster>;
using IqRoster = Iq<Roster>;
auto MakeGetRoster(const BareJid& jid) {
auto MakeGetRoster(const FullJid& jid) {
IqRoster iq_roster{std::in_place_type<GetRoster>, GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}}};
return iq_roster;
}