Make Roster code better
Some checks failed
PR Check / on-push-commit-check (push) Failing after 4m37s

This commit is contained in:
Ivan-lis 2024-11-15 21:17:55 +00:00
parent 850c7e5412
commit 947076fe8f
5 changed files with 79 additions and 51 deletions

View file

@ -48,7 +48,6 @@ 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;
return SetBind{.id = "1", .payload = Bind{}};
}
} // namespace larra::xmpp::iq

View file

@ -42,7 +42,8 @@ namespace larra::xmpp::client {
template <typename Connection>
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<>>
constexpr auto Close(Token token = {}) {
this->active = false;
@ -76,45 +77,35 @@ struct Client {
auto Send(const T& object) -> boost::asio::awaitable<void> {
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<void> {
SPDLOG_INFO("Send IQ: Set::Bind");
co_await std::visit(
[&](auto&& query) -> boost::asio::awaitable<void> {
co_await this->Send(query);
},
::iq::MakeSetBind());
co_await this->Send(::iq::MakeSetBind());
auto bind_result = co_await connection.template Read<::iq::ResultBind>();
resource_bind = std::move(bind_result.payload.jid->resource);
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 std::visit(
[&](auto&& query) -> boost::asio::awaitable<void> {
co_await this->Send(query);
},
::iq::MakeGetRoster(FullJid{}.Username(jid.username).Server(jid.server).Resource(resource_bind)));
co_await this->Send(::iq::MakeGetRoster(jid));
// IqRoster roster = co_await connection.template Read<IqRoster>();
// const auto& roster_result = std::get<::iq::ResultRoster>(roster);
const auto roster_result = co_await connection.template Read<::iq::ResultRoster>();
for(const auto& jid : roster_result.payload.items) {
SPDLOG_INFO("\t'{}'", ToString(jid));
}
roster = std::move(roster_result.payload);
SPDLOG_INFO("{}", ToString(roster));
co_return;
}
private:
bool active = true;
XmlStream<Connection> connection{};
BareJid jid;
std::string resource_bind;
FullJid jid;
::iq::Roster roster;
};
struct StartTlsNegotiationError : std::runtime_error {
@ -155,15 +146,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<std::string_view, std::string_view> { //
auto v = param.find("="); //
return {param.substr(0, v), param.substr(v + 1)}; //
}) //
| std::ranges::to<std::unordered_map<std::string_view, std::string_view>>();
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<std::string_view, std::string_view> {
auto v = param.find("=");
return {param.substr(0, v), param.substr(v + 1)};
}) |
std::ranges::to<std::unordered_map<std::string_view, std::string_view>>();
return {.body = std::move(decoded),
.serverNonce = params.at("r"),
.salt = DecodeBase64(params.at("s")),

View file

@ -7,24 +7,6 @@
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 {
std::string server;
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>;
struct Jid : JidVariant {

View file

@ -4,7 +4,9 @@
#include <larra/iq.hpp>
#include <larra/jid.hpp>
#include <larra/utils.hpp>
#include <ranges>
#include <string>
#include <vector>
namespace larra::xmpp::iq {
@ -15,6 +17,27 @@ struct 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");
}
@ -46,8 +69,7 @@ using ResultRoster = Result<Roster>;
using IqRoster = Iq<Roster>;
auto MakeGetRoster(const FullJid& jid) {
IqRoster iq_roster{std::in_place_type<GetRoster>, GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}}};
return iq_roster;
return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}};
}
} // namespace larra::xmpp::iq

View file

@ -295,4 +295,18 @@ struct RangeToWrapper : T {
: 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