2017-04-29 21:14:34 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2017-05-30 21:02:56 +00:00
|
|
|
// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
|
2017-04-29 21:14:34 +00: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/.
|
|
|
|
|
2017-07-20 19:03:15 +00:00
|
|
|
use try_from::TryFrom;
|
2017-04-23 16:30:23 +00:00
|
|
|
use std::str::FromStr;
|
2017-04-30 21:07:32 +00:00
|
|
|
use std::collections::BTreeMap;
|
2017-04-23 16:30:23 +00:00
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter};
|
2017-04-23 16:30:23 +00:00
|
|
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
2017-05-06 20:13:53 +00:00
|
|
|
use stanza_error::StanzaError;
|
2017-05-30 21:02:56 +00:00
|
|
|
use muc::Muc;
|
2017-05-27 11:21:32 +00:00
|
|
|
use caps::Caps;
|
2017-05-06 19:42:12 +00:00
|
|
|
use delay::Delay;
|
2017-05-21 19:30:42 +00:00
|
|
|
use idle::Idle;
|
2017-05-06 19:58:22 +00:00
|
|
|
use ecaps2::ECaps2;
|
2017-04-23 16:30:23 +00:00
|
|
|
|
2017-04-30 20:03:04 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Show {
|
2017-05-28 16:10:12 +00:00
|
|
|
None,
|
2017-04-30 20:03:04 +00:00
|
|
|
Away,
|
|
|
|
Chat,
|
|
|
|
Dnd,
|
|
|
|
Xa,
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
impl Default for Show {
|
|
|
|
fn default() -> Show {
|
|
|
|
Show::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:30:33 +00: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.")),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
impl IntoElements for Show {
|
|
|
|
fn into_elements(self, emitter: &mut ElementEmitter) {
|
|
|
|
if self == Show::None {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitter.append_child(
|
|
|
|
Element::builder("show")
|
|
|
|
.append(match self {
|
|
|
|
Show::None => unreachable!(),
|
|
|
|
Show::Away => Some("away"),
|
|
|
|
Show::Chat => Some("chat"),
|
|
|
|
Show::Dnd => Some("dnd"),
|
|
|
|
Show::Xa => Some("xa"),
|
|
|
|
})
|
|
|
|
.build())
|
2017-04-30 20:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 21:07:32 +00:00
|
|
|
pub type Lang = String;
|
2017-04-30 19:33:53 +00:00
|
|
|
pub type Status = String;
|
|
|
|
|
2017-04-30 20:29:55 +00:00
|
|
|
pub type Priority = i8;
|
|
|
|
|
2017-04-23 16:30:23 +00:00
|
|
|
/// Lists every known payload of a `<presence/>`.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum PresencePayload {
|
2017-05-06 20:13:53 +00:00
|
|
|
StanzaError(StanzaError),
|
2017-05-30 21:02:56 +00:00
|
|
|
Muc(Muc),
|
2017-05-27 11:21:32 +00:00
|
|
|
Caps(Caps),
|
2017-05-06 19:42:12 +00:00
|
|
|
Delay(Delay),
|
2017-05-21 19:30:42 +00:00
|
|
|
Idle(Idle),
|
2017-05-06 19:58:22 +00:00
|
|
|
ECaps2(ECaps2),
|
2017-05-19 01:09:23 +00:00
|
|
|
|
|
|
|
Unknown(Element),
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
impl TryFrom<Element> for PresencePayload {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-05-18 22:32:44 +00:00
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
fn try_from(elem: Element) -> Result<PresencePayload, Error> {
|
2017-05-18 22:32:44 +00:00
|
|
|
Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) {
|
2017-07-29 05:49:02 +00:00
|
|
|
("error", ns::DEFAULT_NS) => PresencePayload::StanzaError(StanzaError::try_from(elem)?),
|
2017-05-18 22:32:44 +00:00
|
|
|
|
2017-05-30 21:02:56 +00:00
|
|
|
// XEP-0045
|
|
|
|
("x", ns::MUC) => PresencePayload::Muc(Muc::try_from(elem)?),
|
|
|
|
|
2017-05-27 11:21:32 +00:00
|
|
|
// XEP-0115
|
|
|
|
("c", ns::CAPS) => PresencePayload::Caps(Caps::try_from(elem)?),
|
|
|
|
|
2017-05-18 22:32:44 +00:00
|
|
|
// XEP-0203
|
|
|
|
("delay", ns::DELAY) => PresencePayload::Delay(Delay::try_from(elem)?),
|
|
|
|
|
2017-05-21 19:30:42 +00:00
|
|
|
// XEP-0319
|
|
|
|
("idle", ns::IDLE) => PresencePayload::Idle(Idle::try_from(elem)?),
|
|
|
|
|
2017-05-18 22:32:44 +00:00
|
|
|
// XEP-0390
|
|
|
|
("c", ns::ECAPS2) => PresencePayload::ECaps2(ECaps2::try_from(elem)?),
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
_ => PresencePayload::Unknown(elem),
|
2017-05-18 22:32:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<PresencePayload> for Element {
|
|
|
|
fn from(payload: PresencePayload) -> Element {
|
|
|
|
match payload {
|
2017-05-23 22:31:33 +00:00
|
|
|
PresencePayload::StanzaError(stanza_error) => stanza_error.into(),
|
2017-05-30 21:02:56 +00:00
|
|
|
PresencePayload::Muc(muc) => muc.into(),
|
2017-05-27 11:21:32 +00:00
|
|
|
PresencePayload::Caps(caps) => caps.into(),
|
2017-05-23 22:31:33 +00:00
|
|
|
PresencePayload::Delay(delay) => delay.into(),
|
|
|
|
PresencePayload::Idle(idle) => idle.into(),
|
|
|
|
PresencePayload::ECaps2(ecaps2) => ecaps2.into(),
|
2017-05-19 01:09:23 +00:00
|
|
|
|
2017-05-24 20:30:33 +00:00
|
|
|
PresencePayload::Unknown(elem) => elem,
|
2017-05-18 22:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 16:30:23 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-05-28 16:10:12 +00:00
|
|
|
pub enum Type {
|
2017-04-23 16:30:23 +00:00
|
|
|
/// This value is not an acceptable 'type' attribute, it is only used
|
|
|
|
/// internally to signal the absence of 'type'.
|
2017-05-28 15:33:43 +00:00
|
|
|
None,
|
2017-04-23 16:30:23 +00:00
|
|
|
Error,
|
|
|
|
Probe,
|
|
|
|
Subscribe,
|
|
|
|
Subscribed,
|
|
|
|
Unavailable,
|
|
|
|
Unsubscribe,
|
|
|
|
Unsubscribed,
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
impl Default for Type {
|
|
|
|
fn default() -> Type {
|
|
|
|
Type::None
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
impl FromStr for Type {
|
2017-04-23 16:30:23 +00:00
|
|
|
type Err = Error;
|
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
fn from_str(s: &str) -> Result<Type, Error> {
|
2017-04-23 16:30:23 +00:00
|
|
|
Ok(match s {
|
2017-05-28 16:10:12 +00: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 16:30:23 +00:00
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Invalid 'type' attribute on presence element.")),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:10:12 +00:00
|
|
|
impl IntoAttributeValue for Type {
|
2017-04-23 16:30:23 +00:00
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
Some(match self {
|
2017-05-28 16:10:12 +00:00
|
|
|
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",
|
2017-04-23 16:30:23 +00:00
|
|
|
}.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 22:10:13 +00:00
|
|
|
/// The main structure representing the `<presence/>` stanza.
|
2017-04-23 16:30:23 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Presence {
|
|
|
|
pub from: Option<Jid>,
|
|
|
|
pub to: Option<Jid>,
|
|
|
|
pub id: Option<String>,
|
2017-05-28 16:10:12 +00:00
|
|
|
pub type_: Type,
|
|
|
|
pub show: Show,
|
2017-04-30 21:07:32 +00:00
|
|
|
pub statuses: BTreeMap<Lang, Status>,
|
2017-04-30 21:00:29 +00:00
|
|
|
pub priority: Priority,
|
2017-05-18 22:32:44 +00:00
|
|
|
pub payloads: Vec<Element>,
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:49:16 +00:00
|
|
|
impl Presence {
|
|
|
|
pub fn new(type_: Type) -> Presence {
|
|
|
|
Presence {
|
|
|
|
from: None,
|
|
|
|
to: None,
|
|
|
|
id: None,
|
|
|
|
type_: type_,
|
|
|
|
show: Show::None,
|
|
|
|
statuses: BTreeMap::new(),
|
|
|
|
priority: 0i8,
|
|
|
|
payloads: vec!(),
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 23:21:44 +00:00
|
|
|
|
|
|
|
pub fn with_from(mut self, from: Option<Jid>) -> Presence {
|
|
|
|
self.from = from;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_to(mut self, to: Option<Jid>) -> Presence {
|
|
|
|
self.to = to;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_id(mut self, id: Option<String>) -> Presence {
|
|
|
|
self.id = id;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_show(mut self, show: Show) -> Presence {
|
|
|
|
self.show = show;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_priority(mut self, priority: i8) -> Presence {
|
|
|
|
self.priority = priority;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_payloads(mut self, payloads: Vec<Element>) -> Presence {
|
|
|
|
self.payloads = payloads;
|
|
|
|
self
|
|
|
|
}
|
2017-06-26 18:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
impl TryFrom<Element> for Presence {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-05-06 20:24:17 +00:00
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
fn try_from(root: Element) -> Result<Presence, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
check_self!(root, "presence", DEFAULT_NS);
|
2017-05-28 16:10:12 +00:00
|
|
|
let mut show = None;
|
2017-05-06 20:24:17 +00:00
|
|
|
let mut priority = None;
|
2017-05-24 20:30:33 +00:00
|
|
|
let mut presence = Presence {
|
|
|
|
from: get_attr!(root, "from", optional),
|
|
|
|
to: get_attr!(root, "to", optional),
|
|
|
|
id: get_attr!(root, "id", optional),
|
|
|
|
type_: get_attr!(root, "type", default),
|
2017-05-28 16:10:12 +00:00
|
|
|
show: Show::None,
|
2017-05-24 20:30:33 +00:00
|
|
|
statuses: BTreeMap::new(),
|
|
|
|
priority: 0i8,
|
|
|
|
payloads: vec!(),
|
|
|
|
};
|
2017-05-06 20:24:17 +00:00
|
|
|
for elem in root.children() {
|
2017-07-29 05:49:02 +00:00
|
|
|
if elem.is("show", ns::DEFAULT_NS) {
|
2017-05-28 16:10:12 +00:00
|
|
|
if show.is_some() {
|
2017-05-06 20:24:17 +00:00
|
|
|
return Err(Error::ParseError("More than one show element in a presence."));
|
|
|
|
}
|
2018-05-12 15:59:04 +00:00
|
|
|
check_no_attributes!(elem, "show");
|
2017-05-06 20:24:17 +00:00
|
|
|
for _ in elem.children() {
|
|
|
|
return Err(Error::ParseError("Unknown child in show element."));
|
|
|
|
}
|
2017-05-28 16:10:12 +00:00
|
|
|
show = Some(Show::from_str(elem.text().as_ref())?);
|
2017-07-29 05:49:02 +00:00
|
|
|
} else if elem.is("status", ns::DEFAULT_NS) {
|
2018-05-12 15:59:04 +00:00
|
|
|
check_no_unknown_attributes!(elem, "status", ["xml:lang"]);
|
2017-05-06 20:24:17 +00:00
|
|
|
for _ in elem.children() {
|
|
|
|
return Err(Error::ParseError("Unknown child in status element."));
|
|
|
|
}
|
2017-05-22 18:00:04 +00:00
|
|
|
let lang = get_attr!(elem, "xml:lang", default);
|
2017-05-24 20:30:33 +00:00
|
|
|
if presence.statuses.insert(lang, elem.text()).is_some() {
|
2017-05-06 20:24:17 +00:00
|
|
|
return Err(Error::ParseError("Status element present twice for the same xml:lang."));
|
|
|
|
}
|
2017-07-29 05:49:02 +00:00
|
|
|
} else if elem.is("priority", ns::DEFAULT_NS) {
|
2017-05-06 20:24:17 +00:00
|
|
|
if priority.is_some() {
|
|
|
|
return Err(Error::ParseError("More than one priority element in a presence."));
|
|
|
|
}
|
2018-05-12 15:59:04 +00:00
|
|
|
check_no_attributes!(elem, "status");
|
2017-05-06 20:24:17 +00:00
|
|
|
for _ in elem.children() {
|
|
|
|
return Err(Error::ParseError("Unknown child in priority element."));
|
|
|
|
}
|
|
|
|
priority = Some(Priority::from_str(elem.text().as_ref())?);
|
2017-04-30 19:33:53 +00:00
|
|
|
} else {
|
2017-05-24 20:30:33 +00:00
|
|
|
presence.payloads.push(elem.clone());
|
2017-05-06 20:24:17 +00:00
|
|
|
}
|
2017-04-30 19:33:53 +00:00
|
|
|
}
|
2017-05-28 16:10:12 +00:00
|
|
|
if let Some(show) = show {
|
|
|
|
presence.show = show;
|
|
|
|
}
|
2017-05-24 20:30:33 +00:00
|
|
|
if let Some(priority) = priority {
|
|
|
|
presence.priority = priority;
|
|
|
|
}
|
|
|
|
Ok(presence)
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<Presence> for Element {
|
|
|
|
fn from(presence: Presence) -> Element {
|
2017-05-24 20:30:33 +00:00
|
|
|
Element::builder("presence")
|
2017-07-29 05:49:02 +00:00
|
|
|
.ns(ns::DEFAULT_NS)
|
2017-07-29 05:28:20 +00:00
|
|
|
.attr("from", presence.from)
|
|
|
|
.attr("to", presence.to)
|
2017-07-20 19:36:13 +00:00
|
|
|
.attr("id", presence.id)
|
|
|
|
.attr("type", presence.type_)
|
|
|
|
.append(presence.show)
|
2017-07-20 22:09:22 +00:00
|
|
|
.append(presence.statuses.into_iter().map(|(lang, status)| {
|
2017-05-24 20:30:33 +00:00
|
|
|
Element::builder("status")
|
|
|
|
.attr("xml:lang", match lang.as_ref() {
|
|
|
|
"" => None,
|
|
|
|
lang => Some(lang),
|
|
|
|
})
|
|
|
|
.append(status)
|
|
|
|
.build()
|
|
|
|
}).collect::<Vec<_>>())
|
2017-07-20 19:36:13 +00:00
|
|
|
.append(if presence.priority == 0 { None } else { Some(format!("{}", presence.priority)) })
|
|
|
|
.append(presence.payloads)
|
2017-05-24 20:30:33 +00:00
|
|
|
.build()
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:24:17 +00:00
|
|
|
use super::*;
|
2017-08-18 23:04:18 +00:00
|
|
|
use compare_elements::NamespaceAwareCompare;
|
2017-04-23 16:30:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 16:30:23 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-23 16:30:23 +00:00
|
|
|
assert_eq!(presence.from, None);
|
|
|
|
assert_eq!(presence.to, None);
|
|
|
|
assert_eq!(presence.id, None);
|
2017-05-28 16:10:12 +00:00
|
|
|
assert_eq!(presence.type_, Type::None);
|
2017-04-23 16:30:23 +00:00
|
|
|
assert!(presence.payloads.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:client' type='unavailable'/>/>".parse().unwrap();
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept' type='unavailable'/>/>".parse().unwrap();
|
2017-06-26 18:49:16 +00:00
|
|
|
let presence = Presence::new(Type::Unavailable);
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem2 = presence.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
assert!(elem.compare_to(&elem2));
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|
2017-04-30 19:33:53 +00:00
|
|
|
|
2017-04-30 20:03:04 +00:00
|
|
|
#[test]
|
|
|
|
fn test_show() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 20:03:04 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><show>chat</show></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 21:00:29 +00:00
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
2017-05-28 16:10:12 +00:00
|
|
|
assert_eq!(presence.show, Show::Chat);
|
2017-04-30 20:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_missing_show_value() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 20:03:04 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><show/></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><show/></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 20:03:04 +00: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 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 20:03:04 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><show>online</show></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><show>online</show></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 20:03:04 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Invalid value for show.");
|
|
|
|
}
|
|
|
|
|
2017-04-30 21:00:29 +00:00
|
|
|
#[test]
|
|
|
|
fn test_empty_status() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 21:00:29 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status/></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status/></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 21:00:29 +00:00
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
assert_eq!(presence.statuses.len(), 1);
|
2017-04-30 21:07:32 +00:00
|
|
|
assert_eq!(presence.statuses[""], "");
|
2017-04-30 21:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:33:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_status() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 21:00:29 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status>Here!</status></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 21:00:29 +00:00
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
assert_eq!(presence.statuses.len(), 1);
|
2017-04-30 21:07:32 +00:00
|
|
|
assert_eq!(presence.statuses[""], "Here!");
|
2017-04-30 21:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_statuses() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 21:00:29 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00: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 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 21:00:29 +00:00
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
assert_eq!(presence.statuses.len(), 2);
|
2017-04-30 21:07:32 +00:00
|
|
|
assert_eq!(presence.statuses[""], "Here!");
|
|
|
|
assert_eq!(presence.statuses["fr"], "Là!");
|
2017-04-30 19:33:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 21:45:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_multiple_statuses() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 21:45:39 +00: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 05:49:02 +00: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 22:31:33 +00:00
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 21:45:39 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Status element present twice for the same xml:lang.");
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:29:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_priority() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 20:29:55 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><priority>-1</priority></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><priority>-1</priority></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-04-30 21:00:29 +00:00
|
|
|
assert_eq!(presence.payloads.len(), 0);
|
|
|
|
assert_eq!(presence.priority, -1i8);
|
2017-04-30 20:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_priority() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 20:29:55 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><priority>128</priority></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><priority>128</priority></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 20:29:55 +00:00
|
|
|
match error {
|
|
|
|
Error::ParseIntError(_) => (),
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-30 19:33:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unknown_child() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-30 19:33:53 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><test xmlns='invalid'/></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
2017-05-18 22:32:44 +00:00
|
|
|
let payload = &presence.payloads[0];
|
|
|
|
assert!(payload.is("test", "invalid"));
|
2017-04-30 19:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_status_child() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-05-21 15:44:35 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status><coucou/></status></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status><coucou/></status></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 19:33:53 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown child in status element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_attribute() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-05-21 15:44:35 +00:00
|
|
|
let elem: Element = "<presence xmlns='jabber:client'><status coucou=''/></presence>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<presence xmlns='jabber:component:accept'><status coucou=''/></presence>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Presence::try_from(elem).unwrap_err();
|
2017-04-30 19:33:53 +00: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 20:24:17 +00:00
|
|
|
let status = Status::from("Hello world!");
|
2017-06-26 18:49:16 +00:00
|
|
|
let mut presence = Presence::new(Type::Unavailable);
|
|
|
|
presence.statuses.insert(String::from(""), status);
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem: Element = presence.into();
|
2017-07-29 05:49:02 +00:00
|
|
|
assert!(elem.is("presence", ns::DEFAULT_NS));
|
|
|
|
assert!(elem.children().next().unwrap().is("status", ns::DEFAULT_NS));
|
2017-04-30 19:33:53 +00:00
|
|
|
}
|
2017-04-23 16:30:23 +00:00
|
|
|
}
|