83 lines
2.6 KiB
C++
83 lines
2.6 KiB
C++
|
#include <larra/presence.hpp>
|
||
|
|
||
|
namespace larra::xmpp::presence {
|
||
|
|
||
|
namespace c2s {
|
||
|
|
||
|
auto Unavailable::TryParse(xmlpp::Element* element) -> std::optional<Unavailable> {
|
||
|
if(auto type = element->get_attribute("type")) {
|
||
|
if(type->get_value() == "unavailable") {
|
||
|
return Unavailable{};
|
||
|
}
|
||
|
}
|
||
|
return std::nullopt;
|
||
|
}
|
||
|
|
||
|
auto Unavailable::Parse(xmlpp::Element* element) -> Unavailable {
|
||
|
return Unavailable::TryParse(element).value();
|
||
|
}
|
||
|
|
||
|
auto operator<<(xmlpp::Element* element, const Unavailable&) -> void {
|
||
|
element->set_attribute("type", "unavailable");
|
||
|
}
|
||
|
|
||
|
auto Available::TryParse(xmlpp::Element* element) -> std::optional<Available> {
|
||
|
return Unavailable::TryParse(element).has_value() ? std::nullopt : std::optional{Available{}};
|
||
|
}
|
||
|
|
||
|
auto Available::Parse(xmlpp::Element* element) -> Available {
|
||
|
return Available::TryParse(element).value();
|
||
|
}
|
||
|
|
||
|
auto operator<<(xmlpp::Element* element, const Available&) -> void {
|
||
|
}
|
||
|
|
||
|
} // namespace c2s
|
||
|
auto Initial::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())};
|
||
|
}
|
||
|
|
||
|
auto operator<<(xmlpp::Element* element, const Initial& presence) -> void {
|
||
|
element->set_attribute("from", ToString(presence.from));
|
||
|
element->set_attribute("to", ToString(presence.to));
|
||
|
}
|
||
|
|
||
|
auto Probe::Parse(xmlpp::Element* element) -> Probe {
|
||
|
auto type = element->get_attribute("type");
|
||
|
if(!type) {
|
||
|
throw std::runtime_error("Not found attribute type for Parse presence::Probe");
|
||
|
}
|
||
|
if(type->get_value() != "probe") {
|
||
|
throw std::runtime_error("Invalid attribute type value for Parse presence::Probe");
|
||
|
}
|
||
|
auto from = element->get_attribute("from");
|
||
|
if(!from) {
|
||
|
throw std::runtime_error("Not found attribute from for Parse presence::Probe");
|
||
|
}
|
||
|
auto to = element->get_attribute("to");
|
||
|
if(!to) {
|
||
|
throw std::runtime_error("Not found attribute to for Parse presence::Probe");
|
||
|
}
|
||
|
auto id = element->get_attribute("id");
|
||
|
if(!id) {
|
||
|
throw std::runtime_error("Not found attribute id for Parse presence::Probe");
|
||
|
}
|
||
|
|
||
|
return {.from = BareJid::Parse(from->get_value()), .to = BareJid::Parse(to->get_value()), .id = id->get_value()};
|
||
|
}
|
||
|
|
||
|
auto operator<<(xmlpp::Element* element, const Probe& presence) -> void {
|
||
|
element->set_attribute("from", ToString(presence.from));
|
||
|
element->set_attribute("to", ToString(presence.to));
|
||
|
}
|
||
|
|
||
|
} // namespace larra::xmpp::presence
|