2017-04-29 22:14:34 +01:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2019-06-26 02:06:38 +02:00
|
|
|
|
// Copyright (c) 2017 Maxime “pep” Buquet <pep@bouah.net>
|
2017-04-29 22:14:34 +01:00
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
2018-12-18 16:07:46 +01:00
|
|
|
|
use crate::ns;
|
2019-10-23 01:32:41 +02:00
|
|
|
|
use crate::util::error::Error;
|
2018-12-18 16:07:46 +01:00
|
|
|
|
use jid::Jid;
|
2021-10-11 15:22:19 +02:00
|
|
|
|
use minidom::{Element, IntoAttributeValue};
|
2017-04-30 22:07:32 +01:00
|
|
|
|
use std::collections::BTreeMap;
|
2019-04-12 10:58:42 +02:00
|
|
|
|
use std::convert::TryFrom;
|
2019-10-23 01:32:41 +02:00
|
|
|
|
use std::str::FromStr;
|
2017-04-23 17:30:23 +01:00
|
|
|
|
|
2018-09-20 20:51:48 +02:00
|
|
|
|
/// Should be implemented on every known payload of a `<presence/>`.
|
|
|
|
|
|
pub trait PresencePayload: TryFrom<Element> + Into<Element> {}
|
2017-04-23 17:30:23 +01:00
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Specifies the availability of an entity or resource.
|
2017-04-30 21:03:04 +01:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
|
pub enum Show {
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// The entity or resource is temporarily away.
|
2017-04-30 21:03:04 +01:00
|
|
|
|
Away,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The entity or resource is actively interested in chatting.
|
2017-04-30 21:03:04 +01:00
|
|
|
|
Chat,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The entity or resource is busy (dnd = "Do Not Disturb").
|
2017-04-30 21:03:04 +01:00
|
|
|
|
Dnd,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The entity or resource is away for an extended period (xa = "eXtended
|
|
|
|
|
|
/// Away").
|
2017-04-30 21:03:04 +01:00
|
|
|
|
Xa,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-24 21:30:33 +01:00
|
|
|
|
impl FromStr for Show {
|
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Show, Error> {
|
|
|
|
|
|
Ok(match s {
|
|
|
|
|
|
"away" => Show::Away,
|
|
|
|
|
|
"chat" => Show::Chat,
|
|
|
|
|
|
"dnd" => Show::Dnd,
|
|
|
|
|
|
"xa" => Show::Xa,
|
|
|
|
|
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Invalid value for show.")),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-11 15:22:19 +02:00
|
|
|
|
impl From<Show> for Element {
|
|
|
|
|
|
fn from(show: Show) -> Element {
|
2020-03-28 13:07:26 +01:00
|
|
|
|
Element::builder("show", ns::DEFAULT_NS)
|
2021-10-11 15:22:19 +02:00
|
|
|
|
.append(match show {
|
2019-07-25 00:20:38 +02:00
|
|
|
|
Show::Away => "away",
|
|
|
|
|
|
Show::Chat => "chat",
|
|
|
|
|
|
Show::Dnd => "dnd",
|
|
|
|
|
|
Show::Xa => "xa",
|
|
|
|
|
|
})
|
|
|
|
|
|
.build()
|
2017-04-30 21:03:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
type Lang = String;
|
|
|
|
|
|
type Status = String;
|
2017-04-30 20:33:53 +01:00
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
type Priority = i8;
|
2017-04-30 21:29:55 +01:00
|
|
|
|
|
2018-12-18 15:32:05 +01:00
|
|
|
|
///
|
2017-04-23 17:30:23 +01:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-05-28 17:10:12 +01:00
|
|
|
|
pub enum Type {
|
2017-04-23 17:30:23 +01:00
|
|
|
|
/// This value is not an acceptable 'type' attribute, it is only used
|
|
|
|
|
|
/// internally to signal the absence of 'type'.
|
2017-05-28 16:33:43 +01:00
|
|
|
|
None,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// An error has occurred regarding processing of a previously sent
|
|
|
|
|
|
/// presence stanza; if the presence stanza is of type "error", it MUST
|
2022-12-27 16:31:58 +01:00
|
|
|
|
/// include an \<error/\> child element (refer to
|
|
|
|
|
|
/// [XMPP‑CORE](https://xmpp.org/rfcs/rfc6120.html)).
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Error,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// A request for an entity's current presence; SHOULD be generated only by
|
|
|
|
|
|
/// a server on behalf of a user.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Probe,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The sender wishes to subscribe to the recipient's presence.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Subscribe,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The sender has allowed the recipient to receive their presence.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Subscribed,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The sender is no longer available for communication.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Unavailable,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The sender is unsubscribing from the receiver's presence.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Unsubscribe,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The subscription request has been denied or a previously granted
|
|
|
|
|
|
/// subscription has been canceled.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Unsubscribed,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-28 17:10:12 +01:00
|
|
|
|
impl Default for Type {
|
|
|
|
|
|
fn default() -> Type {
|
|
|
|
|
|
Type::None
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-28 17:10:12 +01:00
|
|
|
|
impl FromStr for Type {
|
2017-04-23 17:30:23 +01:00
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
2017-05-28 17:10:12 +01:00
|
|
|
|
fn from_str(s: &str) -> Result<Type, Error> {
|
2017-04-23 17:30:23 +01:00
|
|
|
|
Ok(match s {
|
2017-05-28 17:10:12 +01:00
|
|
|
|
"error" => Type::Error,
|
|
|
|
|
|
"probe" => Type::Probe,
|
|
|
|
|
|
"subscribe" => Type::Subscribe,
|
|
|
|
|
|
"subscribed" => Type::Subscribed,
|
|
|
|
|
|
"unavailable" => Type::Unavailable,
|
|
|
|
|
|
"unsubscribe" => Type::Unsubscribe,
|
|
|
|
|
|
"unsubscribed" => Type::Unsubscribed,
|
2017-04-23 17:30:23 +01:00
|
|
|
|
|
2018-12-18 15:32:05 +01:00
|
|
|
|
_ => {
|
|
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
|
"Invalid 'type' attribute on presence element.",
|
2018-12-29 18:29:11 +01:00
|
|
|
|
));
|
2018-12-18 15:32:05 +01:00
|
|
|
|
}
|
2017-04-23 17:30:23 +01:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-28 17:10:12 +01:00
|
|
|
|
impl IntoAttributeValue for Type {
|
2017-04-23 17:30:23 +01:00
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
Some(
|
|
|
|
|
|
match self {
|
|
|
|
|
|
Type::None => return None,
|
|
|
|
|
|
|
|
|
|
|
|
Type::Error => "error",
|
|
|
|
|
|
Type::Probe => "probe",
|
|
|
|
|
|
Type::Subscribe => "subscribe",
|
|
|
|
|
|
Type::Subscribed => "subscribed",
|
|
|
|
|
|
Type::Unavailable => "unavailable",
|
|
|
|
|
|
Type::Unsubscribe => "unsubscribe",
|
|
|
|
|
|
Type::Unsubscribed => "unsubscribed",
|
|
|
|
|
|
}
|
|
|
|
|
|
.to_owned(),
|
|
|
|
|
|
)
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-20 23:10:13 +01:00
|
|
|
|
/// The main structure representing the `<presence/>` stanza.
|
2022-12-04 17:07:02 +01:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-23 17:30:23 +01:00
|
|
|
|
pub struct Presence {
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// The sender of this presence.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
pub from: Option<Jid>,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The recipient of this presence.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
pub to: Option<Jid>,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The identifier, unique on this stream, of this stanza.
|
2017-04-23 17:30:23 +01:00
|
|
|
|
pub id: Option<String>,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The type of this presence stanza.
|
2017-05-28 17:10:12 +01:00
|
|
|
|
pub type_: Type,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The availability of the sender of this presence.
|
2019-06-26 01:50:52 +02:00
|
|
|
|
pub show: Option<Show>,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// A localised list of statuses defined in this presence.
|
2017-04-30 22:07:32 +01:00
|
|
|
|
pub statuses: BTreeMap<Lang, Status>,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// The sender’s resource priority, if negative it won’t receive messages
|
|
|
|
|
|
/// that haven’t been directed to it.
|
2017-04-30 22:00:29 +01:00
|
|
|
|
pub priority: Priority,
|
2018-09-20 21:08:46 +02:00
|
|
|
|
|
|
|
|
|
|
/// A list of payloads contained in this presence.
|
2017-05-18 23:32:44 +01:00
|
|
|
|
pub payloads: Vec<Element>,
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-26 19:49:16 +01:00
|
|
|
|
impl Presence {
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Create a new presence of this type.
|
2017-06-26 19:49:16 +01:00
|
|
|
|
pub fn new(type_: Type) -> Presence {
|
|
|
|
|
|
Presence {
|
|
|
|
|
|
from: None,
|
|
|
|
|
|
to: None,
|
|
|
|
|
|
id: None,
|
2019-02-21 21:00:58 +01:00
|
|
|
|
type_,
|
2019-06-26 01:50:52 +02:00
|
|
|
|
show: None,
|
2017-06-26 19:49:16 +01:00
|
|
|
|
statuses: BTreeMap::new(),
|
|
|
|
|
|
priority: 0i8,
|
2018-12-18 15:32:05 +01:00
|
|
|
|
payloads: vec![],
|
2017-06-26 19:49:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-19 00:21:44 +01:00
|
|
|
|
|
2023-06-03 10:55:34 +02:00
|
|
|
|
/// Create a presence without a type, which means available
|
|
|
|
|
|
pub fn available() -> Presence {
|
|
|
|
|
|
Self::new(Type::None)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds a presence of type Error
|
|
|
|
|
|
pub fn error() -> Presence {
|
|
|
|
|
|
Self::new(Type::Error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds a presence of type Probe
|
|
|
|
|
|
pub fn probe() -> Presence {
|
|
|
|
|
|
Self::new(Type::Probe)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds a presence of type Subscribe
|
|
|
|
|
|
pub fn subscribe() -> Presence {
|
|
|
|
|
|
Self::new(Type::Subscribe)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds a presence of type Subscribed
|
|
|
|
|
|
pub fn subscribed() -> Presence {
|
|
|
|
|
|
Self::new(Type::Subscribed)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds a presence of type Unavailable
|
|
|
|
|
|
pub fn unavailable() -> Presence {
|
|
|
|
|
|
Self::new(Type::Unavailable)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds a presence of type Unsubscribe
|
|
|
|
|
|
pub fn unsubscribe() -> Presence {
|
|
|
|
|
|
Self::new(Type::Unsubscribe)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Set the emitter of this presence, this should only be useful for
|
|
|
|
|
|
/// servers and components, as clients can only send presences from their
|
|
|
|
|
|
/// own resource (which is implicit).
|
2019-09-06 15:00:14 +02:00
|
|
|
|
pub fn with_from<J: Into<Jid>>(mut self, from: J) -> Presence {
|
|
|
|
|
|
self.from = Some(from.into());
|
2017-07-19 00:21:44 +01:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Set the recipient of this presence, this is only useful for directed
|
|
|
|
|
|
/// presences.
|
2019-09-06 15:00:14 +02:00
|
|
|
|
pub fn with_to<J: Into<Jid>>(mut self, to: J) -> Presence {
|
|
|
|
|
|
self.to = Some(to.into());
|
2017-07-19 00:21:44 +01:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Set the identifier for this presence.
|
2019-09-06 15:00:14 +02:00
|
|
|
|
pub fn with_id(mut self, id: String) -> Presence {
|
|
|
|
|
|
self.id = Some(id);
|
2017-07-19 00:21:44 +01:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Set the availability information of this presence.
|
2017-07-19 00:21:44 +01:00
|
|
|
|
pub fn with_show(mut self, show: Show) -> Presence {
|
2019-06-26 01:50:52 +02:00
|
|
|
|
self.show = Some(show);
|
2017-07-19 00:21:44 +01:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Set the priority of this presence.
|
2017-07-19 00:21:44 +01:00
|
|
|
|
pub fn with_priority(mut self, priority: i8) -> Presence {
|
|
|
|
|
|
self.priority = priority;
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-03 10:55:34 +02:00
|
|
|
|
/// Set a payload inside this presence.
|
|
|
|
|
|
pub fn with_payload<P: PresencePayload>(mut self, payload: P) -> Presence {
|
|
|
|
|
|
self.payloads.push(payload.into());
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 21:08:46 +02:00
|
|
|
|
/// Set the payloads of this presence.
|
2017-07-19 00:21:44 +01:00
|
|
|
|
pub fn with_payloads(mut self, payloads: Vec<Element>) -> Presence {
|
|
|
|
|
|
self.payloads = payloads;
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2018-11-02 16:29:11 +01:00
|
|
|
|
|
|
|
|
|
|
/// Set the availability information of this presence.
|
2019-02-26 19:51:41 +01:00
|
|
|
|
pub fn set_status<L, S>(&mut self, lang: L, status: S)
|
2019-10-23 01:32:41 +02:00
|
|
|
|
where
|
|
|
|
|
|
L: Into<Lang>,
|
|
|
|
|
|
S: Into<Status>,
|
2019-02-26 19:51:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
self.statuses.insert(lang.into(), status.into());
|
2018-11-02 16:29:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Add a payload to this presence.
|
|
|
|
|
|
pub fn add_payload<P: PresencePayload>(&mut self, payload: P) {
|
|
|
|
|
|
self.payloads.push(payload.into());
|
|
|
|
|
|
}
|
2017-06-26 19:49:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-23 23:31:33 +01:00
|
|
|
|
impl TryFrom<Element> for Presence {
|
2019-04-12 10:58:42 +02:00
|
|
|
|
type Error = Error;
|
2017-05-06 21:24:17 +01:00
|
|
|
|
|
2017-05-23 23:31:33 +01:00
|
|
|
|
fn try_from(root: Element) -> Result<Presence, Error> {
|
2018-05-14 16:30:28 +02:00
|
|
|
|
check_self!(root, "presence", DEFAULT_NS);
|
2017-05-28 17:10:12 +01:00
|
|
|
|
let mut show = None;
|
2017-05-06 21:24:17 +01:00
|
|
|
|
let mut priority = None;
|
2017-05-24 21:30:33 +01:00
|
|
|
|
let mut presence = Presence {
|
2019-02-24 20:48:19 +01:00
|
|
|
|
from: get_attr!(root, "from", Option),
|
|
|
|
|
|
to: get_attr!(root, "to", Option),
|
|
|
|
|
|
id: get_attr!(root, "id", Option),
|
|
|
|
|
|
type_: get_attr!(root, "type", Default),
|
2019-06-26 01:50:52 +02:00
|
|
|
|
show: None,
|
2017-05-24 21:30:33 +01:00
|
|
|
|
statuses: BTreeMap::new(),
|
|
|
|
|
|
priority: 0i8,
|
2018-12-18 15:32:05 +01:00
|
|
|
|
payloads: vec![],
|
2017-05-24 21:30:33 +01:00
|
|
|
|
};
|
2017-05-06 21:24:17 +01:00
|
|
|
|
for elem in root.children() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
if elem.is("show", ns::DEFAULT_NS) {
|
2017-05-28 17:10:12 +01:00
|
|
|
|
if show.is_some() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
|
"More than one show element in a presence.",
|
|
|
|
|
|
));
|
2017-05-06 21:24:17 +01:00
|
|
|
|
}
|
2018-05-12 17:59:04 +02:00
|
|
|
|
check_no_attributes!(elem, "show");
|
2018-05-14 16:33:47 +02:00
|
|
|
|
check_no_children!(elem, "show");
|
2017-05-28 17:10:12 +01:00
|
|
|
|
show = Some(Show::from_str(elem.text().as_ref())?);
|
2017-07-29 06:49:02 +01:00
|
|
|
|
} else if elem.is("status", ns::DEFAULT_NS) {
|
2018-05-12 17:59:04 +02:00
|
|
|
|
check_no_unknown_attributes!(elem, "status", ["xml:lang"]);
|
2018-05-14 16:33:47 +02:00
|
|
|
|
check_no_children!(elem, "status");
|
2019-02-24 20:48:19 +01:00
|
|
|
|
let lang = get_attr!(elem, "xml:lang", Default);
|
2017-05-24 21:30:33 +01:00
|
|
|
|
if presence.statuses.insert(lang, elem.text()).is_some() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
|
"Status element present twice for the same xml:lang.",
|
|
|
|
|
|
));
|
2017-05-06 21:24:17 +01:00
|
|
|
|
}
|
2017-07-29 06:49:02 +01:00
|
|
|
|
} else if elem.is("priority", ns::DEFAULT_NS) {
|
2017-05-06 21:24:17 +01:00
|
|
|
|
if priority.is_some() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
|
"More than one priority element in a presence.",
|
|
|
|
|
|
));
|
2017-05-06 21:24:17 +01:00
|
|
|
|
}
|
2018-05-14 16:33:47 +02:00
|
|
|
|
check_no_attributes!(elem, "priority");
|
|
|
|
|
|
check_no_children!(elem, "priority");
|
2017-05-06 21:24:17 +01:00
|
|
|
|
priority = Some(Priority::from_str(elem.text().as_ref())?);
|
2017-04-30 20:33:53 +01:00
|
|
|
|
} else {
|
2017-05-24 21:30:33 +01:00
|
|
|
|
presence.payloads.push(elem.clone());
|
2017-05-06 21:24:17 +01:00
|
|
|
|
}
|
2017-04-30 20:33:53 +01:00
|
|
|
|
}
|
2019-06-26 01:50:52 +02:00
|
|
|
|
presence.show = show;
|
2017-05-24 21:30:33 +01:00
|
|
|
|
if let Some(priority) = priority {
|
|
|
|
|
|
presence.priority = priority;
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(presence)
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-20 20:36:13 +01:00
|
|
|
|
impl From<Presence> for Element {
|
|
|
|
|
|
fn from(presence: Presence) -> Element {
|
2020-03-28 13:07:26 +01:00
|
|
|
|
Element::builder("presence", ns::DEFAULT_NS)
|
2018-12-18 15:32:05 +01:00
|
|
|
|
.attr("from", presence.from)
|
|
|
|
|
|
.attr("to", presence.to)
|
|
|
|
|
|
.attr("id", presence.id)
|
|
|
|
|
|
.attr("type", presence.type_)
|
2019-07-25 00:20:38 +02:00
|
|
|
|
.append_all(presence.show.into_iter())
|
2019-10-23 01:32:41 +02:00
|
|
|
|
.append_all(presence.statuses.into_iter().map(|(lang, status)| {
|
2020-03-28 13:07:26 +01:00
|
|
|
|
Element::builder("status", ns::DEFAULT_NS)
|
2019-10-23 01:32:41 +02:00
|
|
|
|
.attr(
|
|
|
|
|
|
"xml:lang",
|
|
|
|
|
|
match lang.as_ref() {
|
|
|
|
|
|
"" => None,
|
|
|
|
|
|
lang => Some(lang),
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
.append(status)
|
|
|
|
|
|
}))
|
2019-09-06 16:03:58 +02:00
|
|
|
|
.append_all(if presence.priority == 0 {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
None
|
|
|
|
|
|
} else {
|
2020-04-02 22:45:20 +02:00
|
|
|
|
Some(
|
|
|
|
|
|
Element::builder("priority", ns::DEFAULT_NS)
|
|
|
|
|
|
.append(format!("{}", presence.priority)),
|
|
|
|
|
|
)
|
2019-09-06 16:03:58 +02:00
|
|
|
|
})
|
2019-07-25 00:20:38 +02:00
|
|
|
|
.append_all(presence.payloads.into_iter())
|
2018-12-18 15:32:05 +01:00
|
|
|
|
.build()
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
2017-05-06 21:24:17 +01:00
|
|
|
|
use super::*;
|
2019-09-06 15:00:14 +02:00
|
|
|
|
use jid::{BareJid, FullJid};
|
2017-04-23 17:30:23 +01:00
|
|
|
|
|
2018-10-28 13:10:48 +01:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
|
|
|
|
|
assert_size!(Show, 1);
|
|
|
|
|
|
assert_size!(Type, 1);
|
2019-04-22 14:34:25 +02:00
|
|
|
|
assert_size!(Presence, 120);
|
2018-10-28 13:10:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 14:26:16 +02:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
|
|
|
|
|
assert_size!(Show, 1);
|
|
|
|
|
|
assert_size!(Type, 1);
|
2019-04-22 11:24:11 +01:00
|
|
|
|
assert_size!(Presence, 240);
|
2018-10-26 14:26:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-23 17:30:23 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_simple() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 17:30:23 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-23 17:30:23 +01:00
|
|
|
|
assert_eq!(presence.from, None);
|
|
|
|
|
|
assert_eq!(presence.to, None);
|
|
|
|
|
|
assert_eq!(presence.id, None);
|
2017-05-28 17:10:12 +01:00
|
|
|
|
assert_eq!(presence.type_, Type::None);
|
2017-04-23 17:30:23 +01:00
|
|
|
|
assert!(presence.payloads.is_empty());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_serialise() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client' type='unavailable'/>/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept' type='unavailable'/>/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-06-26 19:49:16 +01:00
|
|
|
|
let presence = Presence::new(Type::Unavailable);
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let elem2 = presence.into();
|
2019-11-29 14:33:17 +01:00
|
|
|
|
assert_eq!(elem, elem2);
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|
2017-04-30 20:33:53 +01:00
|
|
|
|
|
2017-04-30 21:03:04 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_show() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><show>chat</show></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 22:00:29 +01:00
|
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
2019-06-26 01:50:52 +02:00
|
|
|
|
assert_eq!(presence.show, Some(Show::Chat));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_empty_show_value() {
|
|
|
|
|
|
#[cfg(not(feature = "component"))]
|
2019-10-23 01:32:41 +02:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
|
2019-06-26 01:50:52 +02:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
|
|
|
|
|
assert_eq!(presence.show, None);
|
2017-04-30 21:03:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_missing_show_value() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><show/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><show/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 21:03:04 +01:00
|
|
|
|
let message = match error {
|
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
|
|
|
|
|
assert_eq!(message, "Invalid value for show.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_show() {
|
|
|
|
|
|
// "online" used to be a pretty common mistake.
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><show>online</show></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><show>online</show></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 21:03:04 +01:00
|
|
|
|
let message = match error {
|
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
|
|
|
|
|
assert_eq!(message, "Invalid value for show.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-30 22:00:29 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_empty_status() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 22:00:29 +01:00
|
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
|
|
assert_eq!(presence.statuses.len(), 1);
|
2017-04-30 22:07:32 +01:00
|
|
|
|
assert_eq!(presence.statuses[""], "");
|
2017-04-30 22:00:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-30 20:33:53 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_status() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><status>Here!</status></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 22:00:29 +01:00
|
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
|
|
assert_eq!(presence.statuses.len(), 1);
|
2017-04-30 22:07:32 +01:00
|
|
|
|
assert_eq!(presence.statuses[""], "Here!");
|
2017-04-30 22:00:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_multiple_statuses() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 22:00:29 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 22:00:29 +01:00
|
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
|
|
assert_eq!(presence.statuses.len(), 2);
|
2017-04-30 22:07:32 +01:00
|
|
|
|
assert_eq!(presence.statuses[""], "Here!");
|
|
|
|
|
|
assert_eq!(presence.statuses["fr"], "Là!");
|
2017-04-30 20:33:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-30 22:45:39 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_multiple_statuses() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 22:45:39 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status xml:lang='fr'>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status xml:lang='fr'>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 22:45:39 +01:00
|
|
|
|
let message = match error {
|
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2018-12-18 15:32:05 +01:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Status element present twice for the same xml:lang."
|
|
|
|
|
|
);
|
2017-04-30 22:45:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-30 21:29:55 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_priority() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><priority>-1</priority></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><priority>-1</priority></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 22:00:29 +01:00
|
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
|
|
assert_eq!(presence.priority, -1i8);
|
2017-04-30 21:29:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_priority() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><priority>128</priority></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><priority>128</priority></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 21:29:55 +01:00
|
|
|
|
match error {
|
|
|
|
|
|
Error::ParseIntError(_) => (),
|
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-30 20:33:53 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_unknown_child() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><test xmlns='invalid'/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-05-18 23:32:44 +01:00
|
|
|
|
let payload = &presence.payloads[0];
|
|
|
|
|
|
assert!(payload.is("test", "invalid"));
|
2017-04-30 20:33:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 22:00:46 +01:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2017-04-30 20:33:53 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_status_child() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status><coucou/></status></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><status><coucou/></status></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 20:33:53 +01:00
|
|
|
|
let message = match error {
|
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
|
|
|
|
|
assert_eq!(message, "Unknown child in status element.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 22:00:46 +01:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2017-04-30 20:33:53 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_attribute() {
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status coucou=''/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
#[cfg(feature = "component")]
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<presence xmlns='jabber:component:accept'><status coucou=''/></presence>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 20:33:53 +01:00
|
|
|
|
let message = match error {
|
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
|
|
|
|
|
assert_eq!(message, "Unknown attribute in status element.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_serialise_status() {
|
2017-05-06 21:24:17 +01:00
|
|
|
|
let status = Status::from("Hello world!");
|
2017-06-26 19:49:16 +01:00
|
|
|
|
let mut presence = Presence::new(Type::Unavailable);
|
|
|
|
|
|
presence.statuses.insert(String::from(""), status);
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let elem: Element = presence.into();
|
2017-07-29 06:49:02 +01:00
|
|
|
|
assert!(elem.is("presence", ns::DEFAULT_NS));
|
|
|
|
|
|
assert!(elem.children().next().unwrap().is("status", ns::DEFAULT_NS));
|
2017-04-30 20:33:53 +01:00
|
|
|
|
}
|
2019-03-21 01:39:21 +01:00
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_serialise_priority() {
|
2019-10-23 01:32:41 +02:00
|
|
|
|
let presence = Presence::new(Type::None).with_priority(42);
|
2019-03-21 01:39:21 +01:00
|
|
|
|
let elem: Element = presence.into();
|
|
|
|
|
|
assert!(elem.is("presence", ns::DEFAULT_NS));
|
|
|
|
|
|
let priority = elem.children().next().unwrap();
|
|
|
|
|
|
assert!(priority.is("priority", ns::DEFAULT_NS));
|
|
|
|
|
|
assert_eq!(priority.text(), "42");
|
|
|
|
|
|
}
|
2019-09-06 15:00:14 +02:00
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn presence_with_to() {
|
|
|
|
|
|
let presence = Presence::new(Type::None);
|
|
|
|
|
|
let elem: Element = presence.into();
|
|
|
|
|
|
assert_eq!(elem.attr("to"), None);
|
|
|
|
|
|
|
2023-06-20 14:07:50 +02:00
|
|
|
|
let presence = Presence::new(Type::None).with_to(Jid::new("localhost").unwrap());
|
2019-09-06 15:00:14 +02:00
|
|
|
|
let elem: Element = presence.into();
|
|
|
|
|
|
assert_eq!(elem.attr("to"), Some("localhost"));
|
|
|
|
|
|
|
2023-06-20 14:07:50 +02:00
|
|
|
|
let presence = Presence::new(Type::None).with_to(BareJid::new("localhost").unwrap());
|
2019-09-06 15:00:14 +02:00
|
|
|
|
let elem: Element = presence.into();
|
|
|
|
|
|
assert_eq!(elem.attr("to"), Some("localhost"));
|
|
|
|
|
|
|
2023-06-20 14:07:50 +02:00
|
|
|
|
let presence =
|
|
|
|
|
|
Presence::new(Type::None).with_to(Jid::new("test@localhost/coucou").unwrap());
|
2019-09-06 15:00:14 +02:00
|
|
|
|
let elem: Element = presence.into();
|
|
|
|
|
|
assert_eq!(elem.attr("to"), Some("test@localhost/coucou"));
|
|
|
|
|
|
|
2019-10-23 01:32:41 +02:00
|
|
|
|
let presence =
|
2023-06-20 14:07:50 +02:00
|
|
|
|
Presence::new(Type::None).with_to(FullJid::new("test@localhost/coucou").unwrap());
|
2019-09-06 15:00:14 +02:00
|
|
|
|
let elem: Element = presence.into();
|
|
|
|
|
|
assert_eq!(elem.attr("to"), Some("test@localhost/coucou"));
|
|
|
|
|
|
}
|
2017-04-23 17:30:23 +01:00
|
|
|
|
}
|