diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 1e87106..9498977 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -10,7 +10,7 @@ FROM archlinux@sha256:a10e51dd0694d6c4142754e9d06cbce7baf91ace8031a30df37064d109 RUN pacman -Syyu --noconfirm \ && pacman -Syyu --noconfirm git less vim sudo python-pip wget which pkgconf \ && 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' 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 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 \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \ - LLVM-${LLVM_VER}-Linux-X64/bin/lld \ - LLVM-${LLVM_VER}-Linux-X64/bin/ld.lld \ - LLVM-${LLVM_VER}-Linux-X64/bin/ld64.lld \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang-scan-deps \ - LLVM-${LLVM_VER}-Linux-X64/bin/llvm-symbolizer \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang-format \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang-tidy \ - LLVM-${LLVM_VER}-Linux-X64/bin/clang-apply-replacements \ - LLVM-${LLVM_VER}-Linux-X64/include/ \ - LLVM-${LLVM_VER}-Linux-X64/lib/ \ - LLVM-${LLVM_VER}-Linux-X64/libexec/ \ - LLVM-${LLVM_VER}-Linux-X64/local/ \ - LLVM-${LLVM_VER}-Linux-X64/share/ +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/clangd \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \ +# LLVM-${LLVM_VER}-Linux-X64/bin/lld \ +# LLVM-${LLVM_VER}-Linux-X64/bin/ld.lld \ +# LLVM-${LLVM_VER}-Linux-X64/bin/ld64.lld \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang-scan-deps \ +# LLVM-${LLVM_VER}-Linux-X64/bin/llvm-symbolizer \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang-format \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang-tidy \ +# LLVM-${LLVM_VER}-Linux-X64/bin/clang-apply-replacements \ +# LLVM-${LLVM_VER}-Linux-X64/include/ \ +# LLVM-${LLVM_VER}-Linux-X64/lib/ \ +# 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 ENV PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}" diff --git a/examples/src/connect.cpp b/examples/src/connect.cpp index a7abdce..10c0161 100644 --- a/examples/src/connect.cpp +++ b/examples/src/connect.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -14,11 +15,20 @@ 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}); +/* co_await std::visit( [](auto& client) -> boost::asio::awaitable { + SPDLOG_INFO("Send presence: Available"); co_await client.Send(larra::xmpp::presence::c2s::Available{}); }, client); + */ + + std::visit( + [](auto& client) -> boost::asio::awaitable { + co_await client.UpdateListOfContacts(); + }, + client); } catch(const std::exception& err) { SPDLOG_ERROR("{}", err.what()); co_return; @@ -27,7 +37,7 @@ auto Coroutine() -> boost::asio::awaitable { } auto main() -> int { - spdlog::set_level(spdlog::level::trace); + spdlog::set_level(spdlog::level::debug); boost::asio::io_context io_context; boost::asio::co_spawn(io_context, Coroutine(), boost::asio::detached); io_context.run(); diff --git a/library/include/larra/client/client.hpp b/library/include/larra/client/client.hpp index b3ccddd..8dd0ece 100644 --- a/library/include/larra/client/client.hpp +++ b/library/include/larra/client/client.hpp @@ -21,7 +21,14 @@ #include #include #include +#include #include "larra/iq.hpp" +#include "larra/roster.hpp" + +namespace rng = std::ranges; +namespace views = std::views; +namespace iq = larra::xmpp::iq; + namespace larra::xmpp { constexpr auto kDefaultXmppPort = 5222; @@ -30,9 +37,6 @@ 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)) {}; @@ -72,11 +76,19 @@ struct Client { [[nodiscard]] constexpr auto Jid() const -> const BareJid& { return this->jid; } - [[nodiscard]] auto UpdateListOfContacts() -> boost::asio::awaitable { - Iq> - this->Send(); + + auto UpdateListOfContacts() -> boost::asio::awaitable { + SPDLOG_INFO("Send IQ: Get::Roster"); + co_await this->Send(::iq::GetRoster{}); + + ::iq::ResultRoster roster = co_await connection.template Read<::iq::ResultRoster>(); + for (const auto& jid : roster.payload.items) { + SPDLOG_INFO("\t'{}'", ToString(jid)); + } + co_return; } + private: bool active = true; XmlStream connection{}; diff --git a/library/include/larra/roster.hpp b/library/include/larra/roster.hpp index 06816a1..660e055 100644 --- a/library/include/larra/roster.hpp +++ b/library/include/larra/roster.hpp @@ -1,76 +1,61 @@ #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 constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) { + } + [[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>()}; + } + /* -namespace larra::xmpp::presence { - -namespace c2s { - -struct Unavailable { - static constexpr auto kDefaultName = "presence"; - friend constexpr auto operator<<(xmlpp::Element* element, const Unavailable&) { - element->set_attribute("type", "unavailable"); - } - [[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional { - if(auto type = element->get_attribute("type")) { - if(type->get_value() == "unavailable") { - return Unavailable{}; - }; + friend constexpr auto operator<<(std::iostream& io, const Roster& roster) { + for (const auto& jid : roster.items) { + io << ToString(jid) << ' '; } - return std::nullopt; - } - [[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Unavailable { - return Unavailable::TryParse(element).value(); } + */ + }; -struct Available { - static constexpr auto kDefaultName = "presence"; - friend constexpr auto operator<<(xmlpp::Element*, const Available&) { - } - [[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional { - return Unavailable::TryParse(element).has_value() ? std::nullopt : std::optional{Available{}}; - } - [[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Available { - return Available::TryParse(element).value(); - } -}; +using GetRoster = Get; +using ResultRoster = Result; -} // namespace c2s +} // namespace larra::xmpp::iq -using ClientToServer = std::variant; - -struct Initial { - static constexpr auto kDefaultName = "presence"; - FullJid from; - BareJid to; - template - [[nodiscard]] constexpr auto From(this Self&& self, BareJid value) -> std::decay_t { - return utils::FieldSetHelper::With<"from", Initial>(std::forward(self), std::move(value)); - } - template - [[nodiscard]] constexpr auto To(this Self&& self, BareJid value) -> std::decay_t { - return utils::FieldSetHelper::With<"to", Initial>(std::forward(self), std::move(value)); - } - friend constexpr auto operator<<(xmlpp::Element* element, const Initial& presence) { - element->set_attribute("from", ToString(presence.from)); - element->set_attribute("to", ToString(presence.to)); - } - [[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Initial { - auto from = element->get_attribute("from"); - if(!from) { - throw std::runtime_error("Not found attribute from for Parse presence::Initial"); - } - auto to = element->get_attribute("to"); - if(!to) { - throw std::runtime_error("Not found attribute to for Parse presence::Initial"); - } - return {.from = FullJid::Parse(from->get_value()), .to = BareJid::Parse(to->get_value())}; - } -}; - -} // namespace larra::xmpp -*/ \ No newline at end of file +namespace larra::xmpp { + using IqRoster = Iq; +} diff --git a/library/include/larra/serialization.hpp b/library/include/larra/serialization.hpp index 31b127a..d05f583 100644 --- a/library/include/larra/serialization.hpp +++ b/library/include/larra/serialization.hpp @@ -87,6 +87,10 @@ struct SerializationBase { }) { return T::StartCheck(element); } else { + if (element) + { + SPDLOG_DEBUG("kDefaultName: {}, get_name: {}", kDefaultName, element->get_name()); + } return element && element->get_name() == kDefaultName; } };