Base minimal working example with presence and Roster
Some checks failed
PR Check / on-push-commit-check (push) Failing after 2m31s
Some checks failed
PR Check / on-push-commit-check (push) Failing after 2m31s
This commit is contained in:
parent
5c250ac434
commit
850c7e5412
4 changed files with 102 additions and 29 deletions
|
@ -8,6 +8,8 @@
|
||||||
#include <larra/printer_stream.hpp>
|
#include <larra/printer_stream.hpp>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
|
#include "larra/bind.hpp"
|
||||||
|
|
||||||
namespace iq = larra::xmpp::iq;
|
namespace iq = larra::xmpp::iq;
|
||||||
|
|
||||||
auto Coroutine() -> boost::asio::awaitable<void> {
|
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"},
|
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
|
||||||
{.useTls = larra::xmpp::client::Options::kNever});
|
{.useTls = larra::xmpp::client::Options::kNever});
|
||||||
|
|
||||||
// TODO(unknown):
|
|
||||||
// rfc6120 7.1
|
// rfc6120 7.1
|
||||||
// After a client authenticates with a server,
|
// 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.
|
// 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(
|
co_await std::visit(
|
||||||
[](auto& client) -> boost::asio::awaitable<void> {
|
[](auto& client) -> boost::asio::awaitable<void> {
|
||||||
|
|
54
library/include/larra/bind.hpp
Normal file
54
library/include/larra/bind.hpp
Normal 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
|
|
@ -22,7 +22,10 @@
|
||||||
#include <larra/xml_stream.hpp>
|
#include <larra/xml_stream.hpp>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "larra/bind.hpp"
|
||||||
#include "larra/iq.hpp"
|
#include "larra/iq.hpp"
|
||||||
|
#include "larra/jid.hpp"
|
||||||
#include "larra/roster.hpp"
|
#include "larra/roster.hpp"
|
||||||
|
|
||||||
namespace rng = std::ranges;
|
namespace rng = std::ranges;
|
||||||
|
@ -77,28 +80,41 @@ struct Client {
|
||||||
return this->jid;
|
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> {
|
auto UpdateListOfContacts() -> boost::asio::awaitable<void> {
|
||||||
SPDLOG_INFO("Send IQ: Get::Roster");
|
SPDLOG_INFO("Send IQ: Get::Roster");
|
||||||
co_await std::visit(
|
co_await std::visit(
|
||||||
[&](auto&& query) -> boost::asio::awaitable<void> {
|
[&](auto&& query) -> boost::asio::awaitable<void> {
|
||||||
co_await this->Send(query);
|
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>();
|
||||||
//IqRoster roster = co_await connection.template Read<IqRoster>();
|
// const auto& roster_result = std::get<::iq::ResultRoster>(roster);
|
||||||
//const auto& roster_result = std::get<::iq::ResultRoster>(roster);
|
const auto roster_result = co_await connection.template Read<::iq::ResultRoster>();
|
||||||
::iq::ResultRoster roster_result = co_await connection.template Read<::iq::ResultRoster>();
|
for(const auto& jid : roster_result.payload.items) {
|
||||||
for (const auto& jid : roster_result.payload.items) {
|
|
||||||
SPDLOG_INFO("\t'{}'", ToString(jid));
|
SPDLOG_INFO("\t'{}'", ToString(jid));
|
||||||
}
|
}
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool active = true;
|
bool active = true;
|
||||||
XmlStream<Connection> connection{};
|
XmlStream<Connection> connection{};
|
||||||
BareJid jid;
|
BareJid jid;
|
||||||
|
std::string resource_bind;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StartTlsNegotiationError : std::runtime_error {
|
struct StartTlsNegotiationError : std::runtime_error {
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
#include <libxml++/libxml++.h>
|
#include <libxml++/libxml++.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <larra/iq.hpp>
|
||||||
#include <larra/jid.hpp>
|
#include <larra/jid.hpp>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <larra/iq.hpp>
|
|
||||||
|
|
||||||
namespace larra::xmpp::iq {
|
namespace larra::xmpp::iq {
|
||||||
|
|
||||||
|
@ -24,32 +24,29 @@ struct Roster {
|
||||||
SPDLOG_DEBUG("No items at Iq::Roster");
|
SPDLOG_DEBUG("No items at Iq::Roster");
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {.items = item_nodes | std::views::transform([](const xmlpp::Node* node) {
|
||||||
.items = item_nodes
|
|
||||||
| std::views::transform([](const xmlpp::Node* node) {
|
|
||||||
auto item_element = dynamic_cast<const xmlpp::Element*>(node);
|
auto item_element = dynamic_cast<const xmlpp::Element*>(node);
|
||||||
if (!item_element) {
|
if(!item_element) {
|
||||||
throw std::runtime_error("Can't convert xmlpp::Node to xmlpp::Element");
|
throw std::runtime_error("Can't convert xmlpp::Node to xmlpp::Element");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto jid_ptr = item_element->get_attribute("jid");
|
auto jid_ptr = item_element->get_attribute("jid");
|
||||||
if (!jid_ptr) {
|
if(!jid_ptr) {
|
||||||
throw std::runtime_error("Not found attribute 'jid' for parse Roster item");
|
throw std::runtime_error("Not found attribute 'jid' for parse Roster item");
|
||||||
}
|
}
|
||||||
|
|
||||||
return BareJid::Parse(jid_ptr->get_value());
|
return BareJid::Parse(jid_ptr->get_value());
|
||||||
})
|
}) |
|
||||||
| std::ranges::to<std::vector<BareJid>>()};
|
std::ranges::to<std::vector<BareJid>>()};
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using GetRoster = Get<Roster>;
|
using GetRoster = Get<Roster>;
|
||||||
using ResultRoster = Result<Roster>;
|
using ResultRoster = Result<Roster>;
|
||||||
using IqRoster = Iq<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{}}};
|
IqRoster iq_roster{std::in_place_type<GetRoster>, GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}}};
|
||||||
return iq_roster;
|
return iq_roster;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue