2024-09-03 15:36:08 +00:00
|
|
|
#include <format>
|
2024-08-30 13:01:35 +00:00
|
|
|
#include <larra/stream.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline auto ToOptionalString(const pugi::xml_attribute& attribute) -> std::optional<std::string> {
|
|
|
|
return attribute ? std::optional{std::string{attribute.as_string()}} : std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool IsJid>
|
|
|
|
inline auto ToOptionalUser(const pugi::xml_attribute& attribute) {
|
|
|
|
if constexpr(IsJid) {
|
|
|
|
return attribute ? std::optional{larra::xmpp::BareJid::Parse(attribute.as_string())} : std::nullopt;
|
|
|
|
} else {
|
|
|
|
return attribute ? std::optional{std::string{attribute.as_string()}} : std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ToString(std::string data) -> std::string {
|
|
|
|
return std::move(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace larra::xmpp {
|
|
|
|
|
|
|
|
template <bool JidFrom, bool JidTo>
|
|
|
|
auto impl::BasicStream<JidFrom, JidTo>::SerializeStream(pugi::xml_node& node) const -> void {
|
|
|
|
if(this->from) {
|
|
|
|
node.append_attribute("from") = ToString(*this->from).c_str();
|
|
|
|
}
|
|
|
|
if(this->to) {
|
|
|
|
node.append_attribute("to") = ToString(*this->to).c_str();
|
|
|
|
}
|
|
|
|
if(this->id) {
|
|
|
|
node.append_attribute("id") = this->id->c_str();
|
|
|
|
}
|
|
|
|
if(this->version) {
|
|
|
|
node.append_attribute("version") = this->version->c_str();
|
|
|
|
}
|
|
|
|
if(this->xmlLang) {
|
|
|
|
node.append_attribute("xml:lang") = this->xmlLang->c_str();
|
|
|
|
}
|
2024-09-03 15:36:08 +00:00
|
|
|
if constexpr(JidFrom || JidTo) {
|
|
|
|
node.append_attribute("xmlns") = "jabber:client";
|
|
|
|
} else {
|
|
|
|
node.append_attribute("xmlns") = "jabber:server";
|
|
|
|
}
|
|
|
|
node.append_attribute("xmlns:stream") = "http://etherx.jabber.org/streams";
|
2024-08-30 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template auto ServerStream::SerializeStream(pugi::xml_node& node) const -> void;
|
|
|
|
template auto ServerToUserStream::SerializeStream(pugi::xml_node& node) const -> void;
|
|
|
|
template auto UserStream::SerializeStream(pugi::xml_node& node) const -> void;
|
|
|
|
|
2024-09-03 15:36:08 +00:00
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
template <bool JidFrom, bool JidTo>
|
|
|
|
inline auto ToStringHelper(const BasicStream<JidFrom, JidTo>& stream) {
|
|
|
|
return std::format("<stream:stream{}{}{}{}{}{} xmlns:stream='http://etherx.jabber.org/streams'>",
|
|
|
|
stream.id ? std::format(" id='{}'", *stream.id) : "",
|
|
|
|
stream.from ? std::format(" from='{}'", *stream.from) : "",
|
|
|
|
stream.to ? std::format(" to='{}'", *stream.to) : "",
|
|
|
|
stream.version ? std::format(" version='{}'", *stream.version) : "",
|
|
|
|
stream.xmlLang ? std::format(" xml:lang='{}'", *stream.xmlLang) : "",
|
|
|
|
JidFrom || JidTo ? " xmlns='jabber:client'" : "xmlns='jabber:server'");
|
|
|
|
};
|
|
|
|
|
|
|
|
auto ToString(const ServerStream& ref) -> std::string {
|
|
|
|
return ToStringHelper(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ToString(const UserStream& ref) -> std::string {
|
|
|
|
return ToStringHelper(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ToString(const ServerToUserStream& ref) -> std::string {
|
|
|
|
return ToStringHelper(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace impl
|
|
|
|
|
2024-08-30 13:01:35 +00:00
|
|
|
template <bool JidFrom, bool JidTo>
|
|
|
|
auto impl::BasicStream<JidFrom, JidTo>::Parse(const pugi::xml_node& node) -> impl::BasicStream<JidFrom, JidTo> {
|
|
|
|
return {ToOptionalUser<JidFrom>(node.attribute("from")),
|
|
|
|
ToOptionalUser<JidTo>(node.attribute("to")),
|
|
|
|
ToOptionalString(node.attribute("id")),
|
|
|
|
ToOptionalString(node.attribute("version")),
|
|
|
|
ToOptionalString(node.attribute("xml:lang"))};
|
|
|
|
}
|
|
|
|
template auto UserStream::Parse(const pugi::xml_node& node) -> UserStream;
|
|
|
|
|
|
|
|
template auto ServerStream::Parse(const pugi::xml_node& node) -> ServerStream;
|
|
|
|
template auto ServerToUserStream::Parse(const pugi::xml_node& node) -> ServerToUserStream;
|
|
|
|
} // namespace larra::xmpp
|