Compare commits
6 commits
7685385559
...
bd80c9684d
Author | SHA1 | Date | |
---|---|---|---|
bd80c9684d | |||
947076fe8f | |||
850c7e5412 | |||
5c250ac434 | |||
7078c25c2a | |||
c77614992d |
12 changed files with 330 additions and 53 deletions
|
@ -10,7 +10,7 @@ FROM archlinux@sha256:a10e51dd0694d6c4142754e9d06cbce7baf91ace8031a30df37064d109
|
||||||
RUN pacman -Syyu --noconfirm \
|
RUN pacman -Syyu --noconfirm \
|
||||||
&& pacman -Syyu --noconfirm git less vim sudo python-pip wget which pkgconf \
|
&& pacman -Syyu --noconfirm git less vim sudo python-pip wget which pkgconf \
|
||||||
&& pacman -Syyu --noconfirm cmake make gcc ninja meson shellcheck \
|
&& pacman -Syyu --noconfirm cmake make gcc ninja meson shellcheck \
|
||||||
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0
|
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0 gtest
|
||||||
|
|
||||||
# Create a non-root user 'dev'
|
# Create a non-root user 'dev'
|
||||||
RUN useradd -ms /bin/bash dev \
|
RUN useradd -ms /bin/bash dev \
|
||||||
|
@ -37,23 +37,25 @@ ADD https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/L
|
||||||
|
|
||||||
# Create the LLVM directory and extract only binaries into it
|
# Create the LLVM directory and extract only binaries into it
|
||||||
RUN sudo mkdir -p /home/LLVM-${LLVM_VER}
|
RUN sudo mkdir -p /home/LLVM-${LLVM_VER}
|
||||||
RUN sudo tar -xJf /home/artifacts/LLVM-${LLVM_VER}-Linux-X64.tar.xz -C /home/LLVM-${LLVM_VER} --strip-components=1 \
|
RUN sudo tar -xJf /home/artifacts/LLVM-${LLVM_VER}-Linux-X64.tar.xz -C /home/LLVM-${LLVM_VER} --strip-components=1
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \
|
#RUN sudo tar -xJf /home/artifacts/LLVM-${LLVM_VER}-Linux-X64.tar.xz -C /home/LLVM-${LLVM_VER} --strip-components=1 \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clangd \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/lld \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/ld.lld \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/ld64.lld \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/lld \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-scan-deps \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/ld.lld \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/llvm-symbolizer \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/ld64.lld \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-format \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang-scan-deps \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-tidy \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/llvm-symbolizer \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-apply-replacements \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang-format \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/include/ \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang-tidy \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/lib/ \
|
# LLVM-${LLVM_VER}-Linux-X64/bin/clang-apply-replacements \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/libexec/ \
|
# LLVM-${LLVM_VER}-Linux-X64/include/ \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/local/ \
|
# LLVM-${LLVM_VER}-Linux-X64/lib/ \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/share/
|
# LLVM-${LLVM_VER}-Linux-X64/libexec/ \
|
||||||
|
# LLVM-${LLVM_VER}-Linux-X64/local/ \
|
||||||
|
# LLVM-${LLVM_VER}-Linux-X64/share/
|
||||||
|
|
||||||
# Add /home/LLVM-${LLVM_VER}/bin to the PATH environment variable
|
# Add /home/LLVM-${LLVM_VER}/bin to the PATH environment variable
|
||||||
ENV PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
ENV PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||||
|
|
12
.vscode/launch.json
vendored
12
.vscode/launch.json
vendored
|
@ -12,6 +12,18 @@
|
||||||
"args": [],
|
"args": [],
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"preLaunchTask": "GCC: Build"
|
"preLaunchTask": "GCC: Build"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug: tests",
|
||||||
|
"program": "${workspaceFolder}/build/larra_xmpp_tests",
|
||||||
|
"args": [
|
||||||
|
// --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
|
||||||
|
"--gtest_filter=Roster*"
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"preLaunchTask": "GCC: Build"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include <boost/asio/co_spawn.hpp>
|
#include <boost/asio/co_spawn.hpp>
|
||||||
|
@ -7,6 +8,10 @@
|
||||||
#include <larra/printer_stream.hpp>
|
#include <larra/printer_stream.hpp>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
|
#include "larra/bind.hpp"
|
||||||
|
|
||||||
|
namespace iq = larra::xmpp::iq;
|
||||||
|
|
||||||
auto Coroutine() -> boost::asio::awaitable<void> {
|
auto Coroutine() -> boost::asio::awaitable<void> {
|
||||||
SPDLOG_INFO("Connecting client...");
|
SPDLOG_INFO("Connecting client...");
|
||||||
|
|
||||||
|
@ -14,11 +19,32 @@ auto Coroutine() -> boost::asio::awaitable<void> {
|
||||||
auto client = co_await larra::xmpp::client::CreateClient<larra::xmpp::PrintStream<boost::asio::ip::tcp::socket>>(
|
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"},
|
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
|
||||||
{.useTls = larra::xmpp::client::Options::kNever});
|
{.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(
|
co_await std::visit(
|
||||||
[](auto& client) -> boost::asio::awaitable<void> {
|
[](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 [XMPP‑CORE]),
|
||||||
|
// 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{});
|
co_await client.Send(larra::xmpp::presence::c2s::Available{});
|
||||||
},
|
},
|
||||||
client);
|
client);
|
||||||
|
|
||||||
} catch(const std::exception& err) {
|
} catch(const std::exception& err) {
|
||||||
SPDLOG_ERROR("{}", err.what());
|
SPDLOG_ERROR("{}", err.what());
|
||||||
co_return;
|
co_return;
|
||||||
|
@ -27,7 +53,7 @@ auto Coroutine() -> boost::asio::awaitable<void> {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto main() -> int {
|
auto main() -> int {
|
||||||
spdlog::set_level(spdlog::level::trace);
|
spdlog::set_level(spdlog::level::debug);
|
||||||
boost::asio::io_context io_context;
|
boost::asio::io_context io_context;
|
||||||
boost::asio::co_spawn(io_context, Coroutine(), boost::asio::detached);
|
boost::asio::co_spawn(io_context, Coroutine(), boost::asio::detached);
|
||||||
io_context.run();
|
io_context.run();
|
||||||
|
|
53
library/include/larra/bind.hpp
Normal file
53
library/include/larra/bind.hpp
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
#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>;
|
||||||
|
|
||||||
|
inline auto MakeSetBind() {
|
||||||
|
return SetBind{.id = "1", .payload = Bind{}};
|
||||||
|
}
|
||||||
|
} // namespace larra::xmpp::iq
|
|
@ -21,6 +21,17 @@
|
||||||
#include <larra/user_account.hpp>
|
#include <larra/user_account.hpp>
|
||||||
#include <larra/xml_stream.hpp>
|
#include <larra/xml_stream.hpp>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "larra/bind.hpp"
|
||||||
|
#include "larra/iq.hpp"
|
||||||
|
#include "larra/jid.hpp"
|
||||||
|
#include "larra/roster.hpp"
|
||||||
|
|
||||||
|
namespace rng = std::ranges;
|
||||||
|
namespace views = std::views;
|
||||||
|
namespace iq = larra::xmpp::iq;
|
||||||
|
|
||||||
namespace larra::xmpp {
|
namespace larra::xmpp {
|
||||||
|
|
||||||
constexpr auto kDefaultXmppPort = 5222;
|
constexpr auto kDefaultXmppPort = 5222;
|
||||||
|
@ -29,12 +40,10 @@ constexpr auto kDefaultXmppPort = 5222;
|
||||||
|
|
||||||
namespace larra::xmpp::client {
|
namespace larra::xmpp::client {
|
||||||
|
|
||||||
namespace rng = std::ranges;
|
|
||||||
namespace views = std::views;
|
|
||||||
|
|
||||||
template <typename Connection>
|
template <typename Connection>
|
||||||
struct Client {
|
struct Client {
|
||||||
constexpr Client(BareJid jid, XmlStream<Connection> connection) : jid(std::move(jid)), connection(std::move(connection)) {};
|
constexpr Client(BareJid jid, XmlStream<Connection> connection) : jid(std::move(jid)), connection(std::move(connection)) {
|
||||||
|
}
|
||||||
template <boost::asio::completion_token_for<void()> Token = boost::asio::use_awaitable_t<>>
|
template <boost::asio::completion_token_for<void()> Token = boost::asio::use_awaitable_t<>>
|
||||||
constexpr auto Close(Token token = {}) {
|
constexpr auto Close(Token token = {}) {
|
||||||
this->active = false;
|
this->active = false;
|
||||||
|
@ -68,14 +77,35 @@ struct Client {
|
||||||
auto Send(const T& object) -> boost::asio::awaitable<void> {
|
auto Send(const T& object) -> boost::asio::awaitable<void> {
|
||||||
co_await this->connection.Send(object);
|
co_await this->connection.Send(object);
|
||||||
}
|
}
|
||||||
[[nodiscard]] constexpr auto Jid() const -> const BareJid& {
|
[[nodiscard]] constexpr auto Jid() const -> const FullJid& {
|
||||||
return this->jid;
|
return this->jid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto CreateResourceBind() -> boost::asio::awaitable<void> {
|
||||||
|
SPDLOG_INFO("Send IQ: Set::Bind");
|
||||||
|
co_await this->Send(::iq::MakeSetBind());
|
||||||
|
|
||||||
|
auto bind_result = co_await connection.template Read<::iq::ResultBind>();
|
||||||
|
jid.resource = std::move(bind_result.payload.jid->resource);
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto UpdateListOfContacts() -> boost::asio::awaitable<void> {
|
||||||
|
SPDLOG_INFO("Send IQ: Get::Roster");
|
||||||
|
co_await this->Send(::iq::MakeGetRoster(jid));
|
||||||
|
|
||||||
|
const auto roster_result = co_await connection.template Read<::iq::ResultRoster>();
|
||||||
|
roster = std::move(roster_result.payload);
|
||||||
|
|
||||||
|
SPDLOG_INFO("{}", ToString(roster));
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool active = true;
|
bool active = true;
|
||||||
XmlStream<Connection> connection{};
|
XmlStream<Connection> connection{};
|
||||||
BareJid jid;
|
FullJid jid;
|
||||||
|
::iq::Roster roster;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StartTlsNegotiationError : std::runtime_error {
|
struct StartTlsNegotiationError : std::runtime_error {
|
||||||
|
@ -116,15 +146,14 @@ struct Challenge {
|
||||||
throw std::runtime_error(std::format("Invalid name {} for challenge", node->get_name()));
|
throw std::runtime_error(std::format("Invalid name {} for challenge", node->get_name()));
|
||||||
}
|
}
|
||||||
std::string decoded = DecodeBase64(node->get_first_child_text()->get_content());
|
std::string decoded = DecodeBase64(node->get_first_child_text()->get_content());
|
||||||
auto params = std::views::split(decoded, ',') //
|
auto params = std::views::split(decoded, ',') | std::views::transform([](auto param) {
|
||||||
| std::views::transform([](auto param) { //
|
return std::string_view{param};
|
||||||
return std::string_view{param}; //
|
}) |
|
||||||
}) //
|
std::views::transform([](std::string_view param) -> std::pair<std::string_view, std::string_view> {
|
||||||
| std::views::transform([](std::string_view param) -> std::pair<std::string_view, std::string_view> { //
|
auto v = param.find("=");
|
||||||
auto v = param.find("="); //
|
return {param.substr(0, v), param.substr(v + 1)};
|
||||||
return {param.substr(0, v), param.substr(v + 1)}; //
|
}) |
|
||||||
}) //
|
std::ranges::to<std::unordered_map<std::string_view, std::string_view>>();
|
||||||
| std::ranges::to<std::unordered_map<std::string_view, std::string_view>>();
|
|
||||||
return {.body = std::move(decoded),
|
return {.body = std::move(decoded),
|
||||||
.serverNonce = params.at("r"),
|
.serverNonce = params.at("r"),
|
||||||
.salt = DecodeBase64(params.at("s")),
|
.salt = DecodeBase64(params.at("s")),
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <larra/serialization.hpp>
|
#include <larra/serialization.hpp>
|
||||||
#include <larra/stream_error.hpp>
|
#include <larra/stream_error.hpp>
|
||||||
#include <larra/utils.hpp>
|
#include <larra/utils.hpp>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace larra::xmpp {
|
namespace larra::xmpp {
|
||||||
|
@ -11,6 +12,8 @@ namespace iq {
|
||||||
template <auto& Name, typename PayloadType>
|
template <auto& Name, typename PayloadType>
|
||||||
struct BaseImplWithPayload {
|
struct BaseImplWithPayload {
|
||||||
std::string id;
|
std::string id;
|
||||||
|
std::optional<std::string> from{};
|
||||||
|
std::optional<std::string> to{};
|
||||||
PayloadType payload;
|
PayloadType payload;
|
||||||
static const inline std::string kName = Name;
|
static const inline std::string kName = Name;
|
||||||
static constexpr auto kDefaultName = "iq";
|
static constexpr auto kDefaultName = "iq";
|
||||||
|
@ -19,16 +22,32 @@ struct BaseImplWithPayload {
|
||||||
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
|
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
|
||||||
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
|
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
|
||||||
}
|
}
|
||||||
|
template <typename Self>
|
||||||
|
[[nodiscard]] constexpr auto To(this Self&& self, std::string to) -> std::decay_t<Self> {
|
||||||
|
return utils::FieldSetHelper::With<"to", BaseImplWithPayload>(std::forward<Self>(self), std::move(to));
|
||||||
|
}
|
||||||
|
template <typename Self>
|
||||||
|
[[nodiscard]] constexpr auto From(this Self&& self, std::string from) -> std::decay_t<Self> {
|
||||||
|
return utils::FieldSetHelper::With<"from", BaseImplWithPayload>(std::forward<Self>(self), std::move(from));
|
||||||
|
}
|
||||||
template <typename NewPayloadType, typename Self>
|
template <typename NewPayloadType, typename Self>
|
||||||
[[nodiscard]] constexpr auto Payload(this Self&& self, NewPayloadType value) {
|
[[nodiscard]] constexpr auto Payload(this Self&& self, NewPayloadType value) {
|
||||||
return utils::FieldSetHelper::With<"payload", BaseImplWithPayload, false>(std::forward<Self>(self), std::move(value));
|
return utils::FieldSetHelper::With<"payload", BaseImplWithPayload, false>(std::forward<Self>(self), std::move(value));
|
||||||
}
|
}
|
||||||
friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) {
|
friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) {
|
||||||
element->set_attribute("id", self.id);
|
element->set_attribute("id", self.id);
|
||||||
|
|
||||||
|
if (self.to) {
|
||||||
|
element->set_attribute("to", *self.to);
|
||||||
|
}
|
||||||
|
if (self.from) {
|
||||||
|
element->set_attribute("from", *self.from);
|
||||||
|
}
|
||||||
element->set_attribute("type", kName);
|
element->set_attribute("type", kName);
|
||||||
using S = Serialization<PayloadType>;
|
using S = Serialization<PayloadType>;
|
||||||
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
|
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> BaseImplWithPayload {
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> BaseImplWithPayload {
|
||||||
auto node = element->get_attribute("type");
|
auto node = element->get_attribute("type");
|
||||||
if(!node) {
|
if(!node) {
|
||||||
|
@ -41,6 +60,9 @@ struct BaseImplWithPayload {
|
||||||
if(!idNode) {
|
if(!idNode) {
|
||||||
throw std::runtime_error("Not found attribute id for parse Iq");
|
throw std::runtime_error("Not found attribute id for parse Iq");
|
||||||
}
|
}
|
||||||
|
auto from = element->get_attribute("from");
|
||||||
|
auto to = element->get_attribute("to");
|
||||||
|
|
||||||
|
|
||||||
using S = Serialization<PayloadType>;
|
using S = Serialization<PayloadType>;
|
||||||
auto payload = element->get_first_child(S::kDefaultName);
|
auto payload = element->get_first_child(S::kDefaultName);
|
||||||
|
@ -51,7 +73,11 @@ struct BaseImplWithPayload {
|
||||||
if(!payload2) {
|
if(!payload2) {
|
||||||
throw std::runtime_error("Invalid payload for parse Iq");
|
throw std::runtime_error("Invalid payload for parse Iq");
|
||||||
}
|
}
|
||||||
return {.id = idNode->get_value(), .payload = S::Parse(payload2)};
|
return {
|
||||||
|
.id = idNode->get_value(),
|
||||||
|
.from = (from ? std::optional{from->get_value()} : std::nullopt),
|
||||||
|
.to = (to ? std::optional{to->get_value()} : std::nullopt),
|
||||||
|
.payload = S::Parse(payload2)};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
static constexpr auto kGetName = "get";
|
static constexpr auto kGetName = "get";
|
||||||
|
|
|
@ -7,24 +7,6 @@
|
||||||
|
|
||||||
namespace larra::xmpp {
|
namespace larra::xmpp {
|
||||||
|
|
||||||
struct BareJid {
|
|
||||||
std::string username;
|
|
||||||
std::string server;
|
|
||||||
[[nodiscard]] static auto Parse(std::string_view jid) -> BareJid;
|
|
||||||
friend auto ToString(const BareJid& jid) -> std::string;
|
|
||||||
|
|
||||||
constexpr auto operator==(const BareJid&) const -> bool = default;
|
|
||||||
template <typename Self>
|
|
||||||
[[nodiscard]] constexpr auto Username(this Self&& self, std::string username) -> std::decay_t<Self> {
|
|
||||||
return utils::FieldSetHelper::With<"username", BareJid>(std::forward<Self>(self), std::move(username));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Self>
|
|
||||||
[[nodiscard]] constexpr auto Server(this Self&& self, std::string server) -> std::decay_t<Self> {
|
|
||||||
return utils::FieldSetHelper::With<"server", BareJid>(std::forward<Self>(self), std::move(server));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BareResourceJid {
|
struct BareResourceJid {
|
||||||
std::string server;
|
std::string server;
|
||||||
std::string resource;
|
std::string resource;
|
||||||
|
@ -69,6 +51,27 @@ struct FullJid {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BareJid {
|
||||||
|
std::string username;
|
||||||
|
std::string server;
|
||||||
|
constexpr operator FullJid() const {
|
||||||
|
return FullJid{.username = username, .server = server, .resource = ""};
|
||||||
|
}
|
||||||
|
[[nodiscard]] static auto Parse(std::string_view jid) -> BareJid;
|
||||||
|
friend auto ToString(const BareJid& jid) -> std::string;
|
||||||
|
|
||||||
|
constexpr auto operator==(const BareJid&) const -> bool = default;
|
||||||
|
template <typename Self>
|
||||||
|
[[nodiscard]] constexpr auto Username(this Self&& self, std::string username) -> std::decay_t<Self> {
|
||||||
|
return utils::FieldSetHelper::With<"username", BareJid>(std::forward<Self>(self), std::move(username));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Self>
|
||||||
|
[[nodiscard]] constexpr auto Server(this Self&& self, std::string server) -> std::decay_t<Self> {
|
||||||
|
return utils::FieldSetHelper::With<"server", BareJid>(std::forward<Self>(self), std::move(server));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
|
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
|
||||||
|
|
||||||
struct Jid : JidVariant {
|
struct Jid : JidVariant {
|
||||||
|
|
78
library/include/larra/roster.hpp
Normal file
78
library/include/larra/roster.hpp
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
#pragma once
|
||||||
|
#include <libxml++/libxml++.h>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <larra/iq.hpp>
|
||||||
|
#include <larra/jid.hpp>
|
||||||
|
#include <larra/utils.hpp>
|
||||||
|
#include <ranges>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace larra::xmpp::iq {
|
||||||
|
|
||||||
|
struct Roster {
|
||||||
|
static constexpr auto kDefaultName = "query";
|
||||||
|
static constexpr auto kDefaultNamespace = "jabber:iq:roster";
|
||||||
|
|
||||||
|
std::vector<BareJid> items;
|
||||||
|
|
||||||
|
friend auto ToString(const Roster& roster) -> std::string {
|
||||||
|
static constexpr std::string_view prefix = "Roster: [";
|
||||||
|
static constexpr std::string_view suffix = "]";
|
||||||
|
std::size_t total_length = std::ranges::fold_left(roster.items | std::views::transform([](const auto& el) {
|
||||||
|
return larra::xmpp::utils::AccumulateFieldLength(el) + 2;
|
||||||
|
}),
|
||||||
|
std::size_t{0},
|
||||||
|
std::plus<>{}) +
|
||||||
|
prefix.length() + suffix.length();
|
||||||
|
|
||||||
|
std::string s;
|
||||||
|
s.resize(total_length);
|
||||||
|
s = prefix;
|
||||||
|
for(const auto& el : roster.items) {
|
||||||
|
s += ToString(el);
|
||||||
|
s += "\n\t";
|
||||||
|
}
|
||||||
|
s += suffix;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) {
|
||||||
|
element->set_attribute("xmlns", "jabber:iq:roster");
|
||||||
|
// TODO: Add adding elements to xmlpp::Element
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster {
|
||||||
|
const auto& item_nodes = element->get_children("item");
|
||||||
|
if(item_nodes.empty()) {
|
||||||
|
SPDLOG_DEBUG("No items at Iq::Roster");
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto jid_ptr = item_element->get_attribute("jid");
|
||||||
|
if(!jid_ptr) {
|
||||||
|
throw std::runtime_error("Not found attribute 'jid' for parse Roster item");
|
||||||
|
}
|
||||||
|
|
||||||
|
return BareJid::Parse(jid_ptr->get_value());
|
||||||
|
}) |
|
||||||
|
std::ranges::to<std::vector<BareJid>>()};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using GetRoster = Get<Roster>;
|
||||||
|
using ResultRoster = Result<Roster>;
|
||||||
|
using IqRoster = Iq<Roster>;
|
||||||
|
|
||||||
|
inline auto MakeGetRoster(const FullJid& jid) {
|
||||||
|
return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace larra::xmpp::iq
|
|
@ -87,6 +87,10 @@ struct SerializationBase {
|
||||||
}) {
|
}) {
|
||||||
return T::StartCheck(element);
|
return T::StartCheck(element);
|
||||||
} else {
|
} else {
|
||||||
|
if (element)
|
||||||
|
{
|
||||||
|
SPDLOG_DEBUG("kDefaultName: {}, get_name: {}", kDefaultName, element->get_name());
|
||||||
|
}
|
||||||
return element && element->get_name() == kDefaultName;
|
return element && element->get_name() == kDefaultName;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -295,4 +295,18 @@ struct RangeToWrapper : T {
|
||||||
: T{std::forward<Args>(args)...} {};
|
: T{std::forward<Args>(args)...} {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept LengthCalculatable = requires(const T& obj) {
|
||||||
|
{ obj.length() } -> std::convertible_to<std::size_t>; // Checks if obj has a length() method returning a type convertible to std::size_t
|
||||||
|
} || std::convertible_to<T, std::string>;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto AccumulateFieldLength(const T& obj) -> std::size_t {
|
||||||
|
std::size_t total_length = 0;
|
||||||
|
boost::pfr::for_each_field(obj, [&](const LengthCalculatable auto& field) {
|
||||||
|
total_length += field.length(); // Accumulate length of each field
|
||||||
|
});
|
||||||
|
return total_length;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace larra::xmpp::utils
|
} // namespace larra::xmpp::utils
|
||||||
|
|
0
library/query.hpp
Normal file
0
library/query.hpp
Normal file
30
tests/roster.cpp
Normal file
30
tests/roster.cpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <larra/roster.hpp>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
|
#include "larra/jid.hpp"
|
||||||
|
|
||||||
|
namespace larra::xmpp {
|
||||||
|
|
||||||
|
TEST(Roster, SerializeAndParse) {
|
||||||
|
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
||||||
|
auto roster = iq::MakeGetRoster(jid);
|
||||||
|
roster.payload.items.emplace_back("u1", "s1");
|
||||||
|
roster.payload.items.emplace_back("u2", "s2");
|
||||||
|
roster.payload.items.emplace_back("u3", "s3");
|
||||||
|
|
||||||
|
using S = Serialization<iq::GetRoster>;
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("iq");
|
||||||
|
node << roster;
|
||||||
|
auto parse_res = iq::Roster::Parse(node);
|
||||||
|
|
||||||
|
ASSERT_EQ(roster.payload.items.size(), parse_res.items.size());
|
||||||
|
for(const auto& [idx, expect_el, parsed_el] : std::views::zip(std::views::iota(0), roster.payload.items, parse_res.items)) {
|
||||||
|
EXPECT_EQ(expect_el, parsed_el) << "Mismatched on idx: " << idx;
|
||||||
|
// std::cerr << " " << "idx: " << idx << "; expect_el: " << expect_el << "; parsed_el: " << parsed_el << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace larra::xmpp
|
Loading…
Reference in a new issue