xmpp-rs/src/disco.rs

238 lines
9.1 KiB
Rust
Raw Normal View History

2017-04-18 19:44:36 +00:00
extern crate minidom;
use minidom::Element;
use error::Error;
use ns;
2017-04-18 19:44:36 +00:00
use data_forms::{DataForm, DataFormType, parse_data_form};
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone, PartialEq)]
2017-04-18 19:44:36 +00:00
pub struct Feature {
pub var: String,
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone)]
2017-04-18 19:44:36 +00:00
pub struct Identity {
pub category: String, // TODO: use an enum here.
pub type_: String, // TODO: use an enum here.
pub xml_lang: String,
pub name: Option<String>,
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone)]
2017-04-18 19:44:36 +00:00
pub struct Disco {
pub node: Option<String>,
pub identities: Vec<Identity>,
pub features: Vec<Feature>,
pub extensions: Vec<DataForm>,
}
pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
if !root.is("query", ns::DISCO_INFO) {
2017-04-19 23:14:29 +00:00
return Err(Error::ParseError("This is not a disco#info element."));
}
2017-04-18 19:44:36 +00:00
let mut identities: Vec<Identity> = vec!();
let mut features: Vec<Feature> = vec!();
let mut extensions: Vec<DataForm> = vec!();
let node = root.attr("node")
.and_then(|node| node.parse().ok());
for child in root.children() {
if child.is("feature", ns::DISCO_INFO) {
2017-04-18 19:44:36 +00:00
let feature = child.attr("var")
.ok_or(Error::ParseError("Feature must have a 'var' attribute."))?;
features.push(Feature {
var: feature.to_owned(),
});
} else if child.is("identity", ns::DISCO_INFO) {
2017-04-18 19:44:36 +00:00
let category = child.attr("category")
.ok_or(Error::ParseError("Identity must have a 'category' attribute."))?;
if category == "" {
return Err(Error::ParseError("Identity must have a non-empty 'category' attribute."))
}
let type_ = child.attr("type")
.ok_or(Error::ParseError("Identity must have a 'type' attribute."))?;
if type_ == "" {
return Err(Error::ParseError("Identity must have a non-empty 'type' attribute."))
}
// TODO: this must check for the namespace of the attribute, but minidom doesnt support that yet, see issue #2.
let xml_lang = child.attr("lang").unwrap_or("");
let name = child.attr("name")
2017-04-19 01:27:33 +00:00
.and_then(|name| name.parse().ok());
2017-04-18 19:44:36 +00:00
identities.push(Identity {
category: category.to_owned(),
type_: type_.to_owned(),
xml_lang: xml_lang.to_owned(),
name: name,
});
} else if child.is("x", ns::DATA_FORMS) {
2017-04-18 19:44:36 +00:00
let data_form = parse_data_form(child)?;
match data_form.type_ {
DataFormType::Result_ => (),
_ => return Err(Error::ParseError("Data form must have a 'result' type in disco#info.")),
}
match data_form.form_type {
Some(_) => extensions.push(data_form),
None => return Err(Error::ParseError("Data form found without a FORM_TYPE.")),
}
} else {
return Err(Error::ParseError("Unknown element in disco#info."));
}
}
/*
// TODO: encode these restrictions only for result disco#info, not get ones.
2017-04-18 19:44:36 +00:00
if identities.is_empty() {
return Err(Error::ParseError("There must be at least one identity in disco#info."));
}
if features.is_empty() {
return Err(Error::ParseError("There must be at least one feature in disco#info."));
}
if !features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) {
2017-04-18 19:44:36 +00:00
return Err(Error::ParseError("disco#info feature not present in disco#info."));
}
*/
2017-04-18 19:44:36 +00:00
Ok(Disco {
2017-04-18 19:44:36 +00:00
node: node,
identities: identities,
features: features,
extensions: extensions
})
2017-04-18 19:44:36 +00:00
}
pub fn serialise_disco(disco: &Disco) -> Element {
2017-04-20 20:03:02 +00:00
let mut root = Element::builder("query")
.ns(ns::DISCO_INFO)
.attr("node", disco.node.clone())
2017-04-20 20:03:02 +00:00
.build();
for identity in &disco.identities {
2017-04-20 20:03:02 +00:00
let identity_element = Element::builder("identity")
.ns(ns::DISCO_INFO)
.attr("category", identity.category.clone())
.attr("type", identity.type_.clone())
.attr("xml:lang", identity.xml_lang.clone())
.attr("name", identity.name.clone())
2017-04-20 20:03:02 +00:00
.build();
root.append_child(identity_element);
}
for feature in &disco.features {
2017-04-20 20:03:02 +00:00
let feature_element = Element::builder("feature")
.ns(ns::DISCO_INFO)
.attr("var", feature.var.clone())
2017-04-20 20:03:02 +00:00
.build();
root.append_child(feature_element);
}
for _ in &disco.extensions {
2017-04-20 20:03:02 +00:00
panic!("Not yet implemented!");
}
root
}
2017-04-18 19:44:36 +00:00
#[cfg(test)]
mod tests {
use minidom::Element;
use error::Error;
use disco;
#[test]
fn test_simple() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
let query = disco::parse_disco(&elem).unwrap();
assert!(query.node.is_none());
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert!(query.extensions.is_empty());
}
#[test]
fn test_invalid() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown element in disco#info.");
}
2017-04-18 19:44:36 +00:00
#[test]
fn test_invalid_identity() {
2017-04-18 19:44:36 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Identity must have a 'category' attribute.");
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Identity must have a non-empty 'category' attribute.");
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Identity must have a 'type' attribute.");
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Identity must have a non-empty 'type' attribute.");
}
2017-04-18 19:44:36 +00:00
#[test]
fn test_invalid_feature() {
2017-04-18 19:44:36 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Feature must have a 'var' attribute.");
}
#[test]
#[ignore]
fn test_invalid_result() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "There must be at least one identity in disco#info.");
2017-04-18 19:44:36 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "There must be at least one feature in disco#info.");
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#items'/></query>".parse().unwrap();
let error = disco::parse_disco(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "disco#info feature not present in disco#info.");
}
}