use crate::ns;
use crate::util::error::Error;
use jid::Jid;
use minidom::{Element, IntoAttributeValue};
use std::collections::BTreeMap;
use std::str::FromStr;
pub trait PresencePayload: TryFrom<Element> + Into<Element> {}
#[derive(Debug, Clone, PartialEq)]
pub enum Show {
Away,
Chat,
Dnd,
Xa,
}
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.")),
})
}
}
impl From<Show> for Element {
fn from(show: Show) -> Element {
Element::builder("show", ns::DEFAULT_NS)
.append(match show {
Show::Away => "away",
Show::Chat => "chat",
Show::Dnd => "dnd",
Show::Xa => "xa",
})
.build()
}
}
type Lang = String;
type Status = String;
type Priority = i8;
#[derive(Debug, Default, Clone, PartialEq)]
pub enum Type {
#[default]
None,
Error,
Probe,
Subscribe,
Subscribed,
Unavailable,
Unsubscribe,
Unsubscribed,
}
impl FromStr for Type {
type Err = Error;
fn from_str(s: &str) -> Result<Type, Error> {
Ok(match s {
"error" => Type::Error,
"probe" => Type::Probe,
"subscribe" => Type::Subscribe,
"subscribed" => Type::Subscribed,
"unavailable" => Type::Unavailable,
"unsubscribe" => Type::Unsubscribe,
"unsubscribed" => Type::Unsubscribed,
_ => {
return Err(Error::ParseError(
"Invalid 'type' attribute on presence element.",
));
}
})
}
}
impl IntoAttributeValue for Type {
fn into_attribute_value(self) -> Option<String> {
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(),
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Presence {
pub from: Option<Jid>,
pub to: Option<Jid>,
pub id: Option<String>,
pub type_: Type,
pub show: Option<Show>,
pub statuses: BTreeMap<Lang, Status>,
pub priority: Priority,
pub payloads: Vec<Element>,
}
impl Presence {
pub fn new(type_: Type) -> Presence {
Presence {
from: None,
to: None,
id: None,
type_,
show: None,
statuses: BTreeMap::new(),
priority: 0i8,
payloads: vec![],
}
}
pub fn available() -> Presence {
Self::new(Type::None)
}
pub fn error() -> Presence {
Self::new(Type::Error)
}
pub fn probe() -> Presence {
Self::new(Type::Probe)
}
pub fn subscribe() -> Presence {
Self::new(Type::Subscribe)
}
pub fn subscribed() -> Presence {
Self::new(Type::Subscribed)
}
pub fn unavailable() -> Presence {
Self::new(Type::Unavailable)
}
pub fn unsubscribe() -> Presence {
Self::new(Type::Unsubscribe)
}
pub fn with_from<J: Into<Jid>>(mut self, from: J) -> Presence {
self.from = Some(from.into());
self
}
pub fn with_to<J: Into<Jid>>(mut self, to: J) -> Presence {
self.to = Some(to.into());
self
}
pub fn with_id(mut self, id: String) -> Presence {
self.id = Some(id);
self
}
pub fn with_show(mut self, show: Show) -> Presence {
self.show = Some(show);
self
}
pub fn with_priority(mut self, priority: i8) -> Presence {
self.priority = priority;
self
}
pub fn with_payload<P: PresencePayload>(mut self, payload: P) -> Presence {
self.payloads.push(payload.into());
self
}
pub fn with_payloads(mut self, payloads: Vec<Element>) -> Presence {
self.payloads = payloads;
self
}
pub fn set_status<L, S>(&mut self, lang: L, status: S)
where
L: Into<Lang>,
S: Into<Status>,
{
self.statuses.insert(lang.into(), status.into());
}
pub fn add_payload<P: PresencePayload>(&mut self, payload: P) {
self.payloads.push(payload.into());
}
}
impl TryFrom<Element> for Presence {
type Error = Error;
fn try_from(root: Element) -> Result<Presence, Error> {
check_self!(root, "presence", DEFAULT_NS);
let mut show = None;
let mut priority = None;
let mut presence = Presence {
from: get_attr!(root, "from", Option),
to: get_attr!(root, "to", Option),
id: get_attr!(root, "id", Option),
type_: get_attr!(root, "type", Default),
show: None,
statuses: BTreeMap::new(),
priority: 0i8,
payloads: vec![],
};
for elem in root.children() {
if elem.is("show", ns::DEFAULT_NS) {
if show.is_some() {
return Err(Error::ParseError(
"More than one show element in a presence.",
));
}
check_no_attributes!(elem, "show");
check_no_children!(elem, "show");
show = Some(Show::from_str(elem.text().as_ref())?);
} else if elem.is("status", ns::DEFAULT_NS) {
check_no_unknown_attributes!(elem, "status", ["xml:lang"]);
check_no_children!(elem, "status");
let lang = get_attr!(elem, "xml:lang", Default);
if presence.statuses.insert(lang, elem.text()).is_some() {
return Err(Error::ParseError(
"Status element present twice for the same xml:lang.",
));
}
} else if elem.is("priority", ns::DEFAULT_NS) {
if priority.is_some() {
return Err(Error::ParseError(
"More than one priority element in a presence.",
));
}
check_no_attributes!(elem, "priority");
check_no_children!(elem, "priority");
priority = Some(Priority::from_str(elem.text().as_ref())?);
} else {
presence.payloads.push(elem.clone());
}
}
presence.show = show;
if let Some(priority) = priority {
presence.priority = priority;
}
Ok(presence)
}
}
impl From<Presence> for Element {
fn from(presence: Presence) -> Element {
Element::builder("presence", ns::DEFAULT_NS)
.attr("from", presence.from)
.attr("to", presence.to)
.attr("id", presence.id)
.attr("type", presence.type_)
.append_all(presence.show)
.append_all(presence.statuses.into_iter().map(|(lang, status)| {
Element::builder("status", ns::DEFAULT_NS)
.attr(
"xml:lang",
match lang.as_ref() {
"" => None,
lang => Some(lang),
},
)
.append(status)
}))
.append_all(if presence.priority == 0 {
None
} else {
Some(
Element::builder("priority", ns::DEFAULT_NS)
.append(format!("{}", presence.priority)),
)
})
.append_all(presence.payloads)
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
use jid::{BareJid, FullJid};
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Show, 1);
assert_size!(Type, 1);
assert_size!(Presence, 72);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Show, 1);
assert_size!(Type, 1);
assert_size!(Presence, 144);
}
#[test]
fn test_simple() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'/>"
.parse()
.unwrap();
let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.from, None);
assert_eq!(presence.to, None);
assert_eq!(presence.id, None);
assert_eq!(presence.type_, Type::None);
assert!(presence.payloads.is_empty());
}
#[test]
fn test_serialise() {
#[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();
let presence = Presence::new(Type::Unavailable);
let elem2 = presence.into();
assert_eq!(elem, elem2);
}
#[test]
fn test_show() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><show>chat</show></presence>"
.parse()
.unwrap();
let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.show, Some(Show::Chat));
}
#[test]
fn test_empty_show_value() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
#[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);
}
#[test]
fn test_missing_show_value() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><show/></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><show/></presence>"
.parse()
.unwrap();
let error = Presence::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Invalid value for show.");
}
#[test]
fn test_invalid_show() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><show>online</show></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><show>online</show></presence>"
.parse()
.unwrap();
let error = Presence::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Invalid value for show.");
}
#[test]
fn test_empty_status() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status/></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status/></presence>"
.parse()
.unwrap();
let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.statuses.len(), 1);
assert_eq!(presence.statuses[""], "");
}
#[test]
fn test_status() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><status>Here!</status></presence>"
.parse()
.unwrap();
let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.statuses.len(), 1);
assert_eq!(presence.statuses[""], "Here!");
}
#[test]
fn test_multiple_statuses() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.statuses.len(), 2);
assert_eq!(presence.statuses[""], "Here!");
assert_eq!(presence.statuses["fr"], "Là!");
}
#[test]
fn test_invalid_multiple_statuses() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status xml:lang='fr'>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
#[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();
let error = Presence::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(
message,
"Status element present twice for the same xml:lang."
);
}
#[test]
fn test_priority() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><priority>-1</priority></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><priority>-1</priority></presence>"
.parse()
.unwrap();
let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.priority, -1i8);
}
#[test]
fn test_invalid_priority() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><priority>128</priority></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><priority>128</priority></presence>"
.parse()
.unwrap();
let error = Presence::try_from(elem).unwrap_err();
match error {
Error::ParseIntError(_) => (),
_ => panic!(),
};
}
#[test]
fn test_unknown_child() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><test xmlns='invalid'/></presence>"
.parse()
.unwrap();
let presence = Presence::try_from(elem).unwrap();
let payload = &presence.payloads[0];
assert!(payload.is("test", "invalid"));
}
#[cfg(not(feature = "disable-validation"))]
#[test]
fn test_invalid_status_child() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status><coucou/></status></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><status><coucou/></status></presence>"
.parse()
.unwrap();
let error = Presence::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in status element.");
}
#[cfg(not(feature = "disable-validation"))]
#[test]
fn test_invalid_attribute() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status coucou=''/></presence>"
.parse()
.unwrap();
#[cfg(feature = "component")]
let elem: Element =
"<presence xmlns='jabber:component:accept'><status coucou=''/></presence>"
.parse()
.unwrap();
let error = Presence::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in status element.");
}
#[test]
fn test_serialise_status() {
let status = Status::from("Hello world!");
let mut presence = Presence::new(Type::Unavailable);
presence.statuses.insert(String::from(""), status);
let elem: Element = presence.into();
assert!(elem.is("presence", ns::DEFAULT_NS));
assert!(elem.children().next().unwrap().is("status", ns::DEFAULT_NS));
}
#[test]
fn test_serialise_priority() {
let presence = Presence::new(Type::None).with_priority(42);
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");
}
#[test]
fn presence_with_to() {
let presence = Presence::new(Type::None);
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), None);
let presence = Presence::new(Type::None).with_to(Jid::new("localhost").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("localhost"));
let presence = Presence::new(Type::None).with_to(BareJid::new("localhost").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("localhost"));
let presence =
Presence::new(Type::None).with_to(Jid::new("test@localhost/coucou").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("test@localhost/coucou"));
let presence =
Presence::new(Type::None).with_to(FullJid::new("test@localhost/coucou").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("test@localhost/coucou"));
}
}