2017-04-29 21:14:34 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
|
|
|
|
//
|
|
|
|
// 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-05-04 00:20:28 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2017-04-23 19:38:13 +00:00
|
|
|
use minidom::Element;
|
|
|
|
use minidom::IntoAttributeValue;
|
|
|
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
2017-05-06 20:13:53 +00:00
|
|
|
use stanza_error::StanzaError;
|
2017-05-06 20:01:15 +00:00
|
|
|
use disco::Disco;
|
2017-05-04 22:11:10 +00:00
|
|
|
use ibb::IBB;
|
2017-05-04 00:20:28 +00:00
|
|
|
use jingle::Jingle;
|
2017-05-04 00:31:13 +00:00
|
|
|
use ping::Ping;
|
2017-04-23 20:12:27 +00:00
|
|
|
|
2017-04-23 19:38:13 +00:00
|
|
|
/// Lists every known payload of a `<iq/>`.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum IqPayload {
|
2017-05-06 20:01:15 +00:00
|
|
|
Disco(Disco),
|
2017-05-04 22:11:10 +00:00
|
|
|
IBB(IBB),
|
2017-05-04 00:20:28 +00:00
|
|
|
Jingle(Jingle),
|
2017-05-04 00:31:13 +00:00
|
|
|
Ping(Ping),
|
2017-05-19 01:09:23 +00:00
|
|
|
|
|
|
|
Unknown(Element),
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 22:50:08 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for IqPayload {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: &'a Element) -> Result<IqPayload, Error> {
|
|
|
|
Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) {
|
|
|
|
// XEP-0030
|
|
|
|
("query", ns::DISCO_INFO) => IqPayload::Disco(Disco::try_from(elem)?),
|
|
|
|
|
|
|
|
// XEP-0047
|
|
|
|
("open", ns::IBB)
|
|
|
|
| ("data", ns::IBB)
|
|
|
|
| ("close", ns::IBB) => IqPayload::IBB(IBB::try_from(elem)?),
|
|
|
|
|
|
|
|
// XEP-0166
|
|
|
|
("jingle", ns::JINGLE) => IqPayload::Jingle(Jingle::try_from(elem)?),
|
|
|
|
|
|
|
|
// XEP-0199
|
|
|
|
("ping", ns::PING) => IqPayload::Ping(Ping::try_from(elem)?),
|
|
|
|
|
2017-05-19 01:09:23 +00:00
|
|
|
_ => IqPayload::Unknown(elem.clone()),
|
2017-05-18 22:50:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 19:38:13 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum IqType {
|
2017-05-18 23:04:42 +00:00
|
|
|
Get(Element),
|
|
|
|
Set(Element),
|
|
|
|
Result(Option<Element>),
|
2017-05-06 20:13:53 +00:00
|
|
|
Error(StanzaError),
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoAttributeValue for IqType {
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
Some(match self {
|
|
|
|
IqType::Get(_) => "get",
|
|
|
|
IqType::Set(_) => "set",
|
|
|
|
IqType::Result(_) => "result",
|
|
|
|
IqType::Error(_) => "error",
|
|
|
|
}.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Iq {
|
|
|
|
pub from: Option<Jid>,
|
|
|
|
pub to: Option<Jid>,
|
|
|
|
pub id: Option<String>,
|
|
|
|
pub payload: IqType,
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for Iq {
|
|
|
|
type Error = Error;
|
2017-04-23 19:38:13 +00:00
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
fn try_from(root: &'a Element) -> Result<Iq, Error> {
|
|
|
|
if !root.is("iq", ns::JABBER_CLIENT) {
|
|
|
|
return Err(Error::ParseError("This is not an iq element."));
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
2017-05-06 20:16:56 +00:00
|
|
|
let from = root.attr("from")
|
|
|
|
.and_then(|value| value.parse().ok());
|
|
|
|
let to = root.attr("to")
|
|
|
|
.and_then(|value| value.parse().ok());
|
|
|
|
let id = root.attr("id")
|
|
|
|
.and_then(|value| value.parse().ok());
|
|
|
|
let type_ = match root.attr("type") {
|
|
|
|
Some(type_) => type_,
|
|
|
|
None => return Err(Error::ParseError("Iq element requires a 'type' attribute.")),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut payload = None;
|
|
|
|
let mut error_payload = None;
|
|
|
|
for elem in root.children() {
|
|
|
|
if payload.is_some() {
|
2017-04-23 19:38:13 +00:00
|
|
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
|
|
|
}
|
2017-05-06 20:16:56 +00:00
|
|
|
if type_ == "error" {
|
|
|
|
if elem.is("error", ns::JABBER_CLIENT) {
|
|
|
|
if error_payload.is_some() {
|
|
|
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
|
|
|
}
|
|
|
|
error_payload = Some(StanzaError::try_from(elem)?);
|
|
|
|
} else if root.children().collect::<Vec<_>>().len() != 2 {
|
|
|
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
|
|
|
}
|
2017-04-23 20:12:27 +00:00
|
|
|
} else {
|
2017-05-18 23:07:51 +00:00
|
|
|
payload = Some(elem.clone());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
let type_ = if type_ == "get" {
|
2017-05-18 23:07:51 +00:00
|
|
|
if let Some(payload) = payload {
|
|
|
|
IqType::Get(payload)
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
|
|
|
}
|
|
|
|
} else if type_ == "set" {
|
2017-05-18 23:07:51 +00:00
|
|
|
if let Some(payload) = payload {
|
|
|
|
IqType::Set(payload)
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
|
|
|
}
|
|
|
|
} else if type_ == "result" {
|
2017-05-18 23:07:51 +00:00
|
|
|
if let Some(payload) = payload {
|
|
|
|
IqType::Result(Some(payload))
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
|
|
|
IqType::Result(None)
|
|
|
|
}
|
|
|
|
} else if type_ == "error" {
|
|
|
|
if let Some(payload) = error_payload.clone() {
|
2017-05-18 23:07:51 +00:00
|
|
|
IqType::Error(payload)
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
} else {
|
2017-05-18 23:10:23 +00:00
|
|
|
return Err(Error::ParseError("Unknown iq type."));
|
2017-05-06 20:16:56 +00:00
|
|
|
};
|
2017-04-23 19:38:13 +00:00
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
Ok(Iq {
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
id: id,
|
|
|
|
payload: type_,
|
|
|
|
})
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
impl<'a> Into<Element> for &'a IqPayload {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
match *self {
|
|
|
|
IqPayload::Disco(ref disco) => disco.into(),
|
|
|
|
IqPayload::IBB(ref ibb) => ibb.into(),
|
|
|
|
IqPayload::Jingle(ref jingle) => jingle.into(),
|
|
|
|
IqPayload::Ping(ref ping) => ping.into(),
|
2017-05-19 01:09:23 +00:00
|
|
|
|
|
|
|
IqPayload::Unknown(ref elem) => elem.clone(),
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
2017-04-23 20:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
impl<'a> Into<Element> for &'a Iq {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
let mut stanza = Element::builder("iq")
|
|
|
|
.ns(ns::JABBER_CLIENT)
|
|
|
|
.attr("from", self.from.clone().and_then(|value| Some(String::from(value))))
|
|
|
|
.attr("to", self.to.clone().and_then(|value| Some(String::from(value))))
|
|
|
|
.attr("id", self.id.clone())
|
|
|
|
.attr("type", self.payload.clone())
|
|
|
|
.build();
|
|
|
|
let elem = match self.payload.clone() {
|
2017-05-18 23:04:42 +00:00
|
|
|
IqType::Get(elem)
|
|
|
|
| IqType::Set(elem)
|
|
|
|
| IqType::Result(Some(elem)) => elem,
|
2017-05-06 20:16:56 +00:00
|
|
|
IqType::Error(error) => (&error).into(),
|
|
|
|
IqType::Result(None) => return stanza,
|
|
|
|
};
|
|
|
|
stanza.append_child(elem);
|
|
|
|
stanza
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:13:53 +00:00
|
|
|
use super::*;
|
|
|
|
use stanza_error::{ErrorType, DefinedCondition};
|
2017-04-23 19:38:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_require_type() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client'/>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let error = Iq::try_from(&elem).unwrap_err();
|
2017-04-23 19:38:13 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Iq element requires a 'type' attribute.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='get'>
|
2017-04-23 20:12:27 +00:00
|
|
|
<foo/>
|
2017-04-23 19:38:13 +00:00
|
|
|
</iq>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq = Iq::try_from(&elem).unwrap();
|
2017-04-23 20:12:27 +00:00
|
|
|
let query: Element = "<foo xmlns='jabber:client'/>".parse().unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
|
|
|
assert_eq!(iq.id, None);
|
|
|
|
assert!(match iq.payload {
|
2017-05-18 23:04:42 +00:00
|
|
|
IqType::Get(element) => element == query,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='set'>
|
|
|
|
<vCard xmlns='vcard-temp'/>
|
|
|
|
</iq>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq = Iq::try_from(&elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
|
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
|
|
|
assert_eq!(iq.id, None);
|
|
|
|
assert!(match iq.payload {
|
2017-05-18 23:04:42 +00:00
|
|
|
IqType::Set(element) => element == vcard,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_result_empty() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq = Iq::try_from(&elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
|
|
|
assert_eq!(iq.id, None);
|
|
|
|
assert!(match iq.payload {
|
2017-05-06 20:16:56 +00:00
|
|
|
IqType::Result(None) => true,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_result() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result'>
|
|
|
|
<query xmlns='http://jabber.org/protocol/disco#items'/>
|
|
|
|
</iq>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq = Iq::try_from(&elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
|
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
|
|
|
assert_eq!(iq.id, None);
|
|
|
|
assert!(match iq.payload {
|
2017-05-18 23:04:42 +00:00
|
|
|
IqType::Result(Some(element)) => element == query,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='error'>
|
|
|
|
<ping xmlns='urn:xmpp:ping'/>
|
|
|
|
<error type='cancel'>
|
|
|
|
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
|
|
|
</error>
|
|
|
|
</iq>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq = Iq::try_from(&elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
|
|
|
assert_eq!(iq.id, None);
|
2017-05-01 00:23:56 +00:00
|
|
|
match iq.payload {
|
2017-05-06 20:16:56 +00:00
|
|
|
IqType::Error(error) => {
|
2017-05-06 20:13:53 +00:00
|
|
|
assert_eq!(error.type_, ErrorType::Cancel);
|
2017-05-01 00:50:38 +00:00
|
|
|
assert_eq!(error.by, None);
|
2017-05-06 20:13:53 +00:00
|
|
|
assert_eq!(error.defined_condition, DefinedCondition::ServiceUnavailable);
|
2017-05-01 00:50:38 +00:00
|
|
|
assert_eq!(error.texts.len(), 0);
|
|
|
|
assert_eq!(error.other, None);
|
2017-05-01 00:23:56 +00:00
|
|
|
},
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_children_invalid() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='error'></iq>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let error = Iq::try_from(&elem).unwrap_err();
|
2017-04-23 19:38:13 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Wrong number of children in iq element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq2 = Iq {
|
2017-04-23 19:38:13 +00:00
|
|
|
from: None,
|
|
|
|
to: None,
|
|
|
|
id: None,
|
2017-05-06 20:16:56 +00:00
|
|
|
payload: IqType::Result(None),
|
2017-04-23 19:38:13 +00:00
|
|
|
};
|
2017-05-06 20:16:56 +00:00
|
|
|
let elem2 = (&iq2).into();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
2017-04-23 20:12:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_disco() {
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='get'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq = Iq::try_from(&elem).unwrap();
|
2017-05-18 23:04:42 +00:00
|
|
|
let payload = match iq.payload {
|
|
|
|
IqType::Get(ref payload) => IqPayload::try_from(payload).unwrap(),
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert!(match payload {
|
|
|
|
IqPayload::Disco(Disco { .. }) => true,
|
2017-04-23 20:12:27 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|