From 76853855592a50e1f972350cc06feca337e16b6d Mon Sep 17 00:00:00 2001 From: Ivan-lis Date: Sun, 17 Nov 2024 23:29:51 +0000 Subject: [PATCH] Feature: iq::Roster --- .vscode/launch.json | 12 ++++ .vscode/tasks.json | 2 +- examples/src/connect.cpp | 24 ++++++++ library/include/larra/client/client.hpp | 56 +++++++++++++----- library/include/larra/roster.hpp | 76 +++++++++++++++++++++++++ library/include/larra/utils.hpp | 14 +++++ tests/roster.cpp | 39 +++++++++++++ 7 files changed, 207 insertions(+), 16 deletions(-) create mode 100644 library/include/larra/roster.hpp create mode 100644 tests/roster.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 2a94c2f..974f9e6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,6 +12,18 @@ "args": [], "cwd": "${workspaceFolder}", "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" } ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index ebd295f..4720fa7 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -104,7 +104,7 @@ "presentation": { "clear": true }, - "hide": true, + "hide": false, "group": { "kind": "build" } diff --git a/examples/src/connect.cpp b/examples/src/connect.cpp index a7abdce..9f24137 100644 --- a/examples/src/connect.cpp +++ b/examples/src/connect.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -7,6 +8,8 @@ #include #include +namespace iq = larra::xmpp::iq; + auto Coroutine() -> boost::asio::awaitable { SPDLOG_INFO("Connecting client..."); @@ -14,11 +17,32 @@ auto Coroutine() -> boost::asio::awaitable { auto client = co_await larra::xmpp::client::CreateClient>( 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 { + co_await client.CreateResourceBind(); + }, + client); + + co_await std::visit( + [](auto& client) -> boost::asio::awaitable { + 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 { + 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; diff --git a/library/include/larra/client/client.hpp b/library/include/larra/client/client.hpp index 5ee658c..ba80489 100644 --- a/library/include/larra/client/client.hpp +++ b/library/include/larra/client/client.hpp @@ -11,16 +11,24 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include #include +#include + +namespace rng = std::ranges; +namespace views = std::views; +namespace iq = larra::xmpp::iq; + namespace larra::xmpp { constexpr auto kDefaultXmppPort = 5222; @@ -29,12 +37,10 @@ constexpr auto kDefaultXmppPort = 5222; namespace larra::xmpp::client { -namespace rng = std::ranges; -namespace views = std::views; - template struct Client { - constexpr Client(BareJid jid, XmlStream connection) : jid(std::move(jid)), connection(std::move(connection)) {}; + constexpr Client(BareJid jid, XmlStream connection) : jid(std::move(jid)), connection(std::move(connection)) { + } template Token = boost::asio::use_awaitable_t<>> constexpr auto Close(Token token = {}) { this->active = false; @@ -68,14 +74,35 @@ struct Client { auto Send(const T& object) -> boost::asio::awaitable { co_await this->connection.Send(object); } - [[nodiscard]] constexpr auto Jid() const -> const BareJid& { + [[nodiscard]] constexpr auto Jid() const -> const FullJid& { return this->jid; } + auto CreateResourceBind() -> boost::asio::awaitable { + 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 { + 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("New roster: {}", ToString(roster)); + co_return; + } + private: bool active = true; XmlStream connection{}; - BareJid jid; + FullJid jid; + ::iq::Roster roster; }; struct StartTlsNegotiationError : std::runtime_error { @@ -116,15 +143,14 @@ struct Challenge { throw std::runtime_error(std::format("Invalid name {} for challenge", node->get_name())); } std::string decoded = DecodeBase64(node->get_first_child_text()->get_content()); - auto params = std::views::split(decoded, ',') // - | std::views::transform([](auto param) { // - return std::string_view{param}; // - }) // - | std::views::transform([](std::string_view param) -> std::pair { // - auto v = param.find("="); // - return {param.substr(0, v), param.substr(v + 1)}; // - }) // - | std::ranges::to>(); + auto params = std::views::split(decoded, ',') | std::views::transform([](auto param) { + return std::string_view{param}; + }) | + std::views::transform([](std::string_view param) -> std::pair { + auto v = param.find("="); + return {param.substr(0, v), param.substr(v + 1)}; + }) | + std::ranges::to>(); return {.body = std::move(decoded), .serverNonce = params.at("r"), .salt = DecodeBase64(params.at("s")), diff --git a/library/include/larra/roster.hpp b/library/include/larra/roster.hpp new file mode 100644 index 0000000..b1e6350 --- /dev/null +++ b/library/include/larra/roster.hpp @@ -0,0 +1,76 @@ +#pragma once +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace larra::xmpp::iq { + +struct Roster { + static constexpr auto kDefaultName = "query"; + static constexpr auto kDefaultNamespace = "jabber:iq:roster"; + + std::vector items; + + friend auto ToString(const Roster& roster) -> std::string { + static constexpr std::string_view prefix = "Roster: [\n\t"; + static constexpr std::string_view suffix = "]"; + // \n\r\t + std::size_t total_length = std::ranges::fold_left(roster.items | std::views::transform([](const auto& el) { + return larra::xmpp::utils::AccumulateFieldLength(el) + 3; + }), + prefix.length() + suffix.length(), + std::plus<>{}); + + std::string s; + s.resize(total_length); + s = prefix; + for(const auto& el : roster.items) { + s += ToString(el); + s += "\n\t"; + } + return s += suffix; + } + friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) { + element->set_attribute("xmlns", Roster::kDefaultNamespace); + std::ranges::for_each(roster.items, [element](const auto& item) { + element->add_child_element("item")->set_attribute("jid", ToString(item)); + }); + } + [[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(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>()}; + } +}; + +using GetRoster = Get; +using ResultRoster = Result; +using IqRoster = Iq; + +inline auto MakeGetRoster(const FullJid& jid) { + return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}}; +} + +} // namespace larra::xmpp::iq diff --git a/library/include/larra/utils.hpp b/library/include/larra/utils.hpp index 1fb05a5..f3a702e 100644 --- a/library/include/larra/utils.hpp +++ b/library/include/larra/utils.hpp @@ -295,4 +295,18 @@ struct RangeToWrapper : T { : T{std::forward(args)...} {}; }; +template +concept LengthCalculatable = requires(const T& obj) { + { obj.length() } -> std::convertible_to; // Checks if obj has a length() method returning a type convertible to std::size_t +} || std::convertible_to; + +template +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 diff --git a/tests/roster.cpp b/tests/roster.cpp new file mode 100644 index 0000000..f6b6c45 --- /dev/null +++ b/tests/roster.cpp @@ -0,0 +1,39 @@ +#include + +#include + +#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"); + + xmlpp::Document doc; + auto node = doc.create_root_node("iq"); + node << roster; + + auto parse_res = decltype(roster)::Parse(node); + + ASSERT_EQ(roster.payload.items.size(), parse_res.payload.items.size()); + for(const auto& [idx, expect_el, parsed_el] : std::views::zip(std::views::iota(0), roster.payload.items, parse_res.payload.items)) { + EXPECT_EQ(expect_el, parsed_el) << "Mismatched on idx: " << idx; + // std::cerr << " " << "idx: " << idx << "; expect_el: " << expect_el << "; parsed_el: " << parsed_el << '\n'; + } +} + +TEST(Roster, Print) { + 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"); + + EXPECT_NO_THROW({ std::cerr << "[ ] Roster payload: " << ToString(roster.payload) << '\n'; }); +} + +} // namespace larra::xmpp