2017-04-29 21:14:34 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// 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-05-06 19:51:39 +00:00
|
|
|
|
2017-05-24 22:47:27 +00:00
|
|
|
use minidom::{Element, IntoElements, ElementEmitter};
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
use error::Error;
|
2017-04-20 22:16:12 +00:00
|
|
|
use ns;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-05-06 19:51:39 +00:00
|
|
|
use data_forms::{DataForm, DataFormType};
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DiscoInfoQuery {
|
|
|
|
pub node: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for DiscoInfoQuery {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-07-20 16:39:59 +00:00
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<DiscoInfoQuery, Error> {
|
|
|
|
if !elem.is("query", ns::DISCO_INFO) {
|
|
|
|
return Err(Error::ParseError("This is not a disco#info element."));
|
|
|
|
}
|
|
|
|
for _ in elem.children() {
|
|
|
|
return Err(Error::ParseError("Unknown child in disco#info."));
|
|
|
|
}
|
|
|
|
for (attr, _) in elem.attrs() {
|
|
|
|
if attr != "node" {
|
|
|
|
return Err(Error::ParseError("Unknown attribute in disco#info."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(DiscoInfoQuery {
|
|
|
|
node: get_attr!(elem, "node", optional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DiscoInfoQuery> for Element {
|
|
|
|
fn from(disco: DiscoInfoQuery) -> Element {
|
|
|
|
Element::builder("query")
|
|
|
|
.ns(ns::DISCO_INFO)
|
|
|
|
.attr("node", disco.node)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-24 22:47:27 +00:00
|
|
|
impl Into<Element> for Feature {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
Element::builder("feature")
|
|
|
|
.ns(ns::DISCO_INFO)
|
|
|
|
.attr("var", self.var)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoElements for Feature {
|
|
|
|
fn into_elements(self, emitter: &mut ElementEmitter) {
|
|
|
|
emitter.append_child(self.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2017-05-27 21:10:00 +00:00
|
|
|
pub lang: Option<String>,
|
2017-04-18 19:44:36 +00:00
|
|
|
pub name: Option<String>,
|
|
|
|
}
|
|
|
|
|
2017-05-24 22:47:27 +00:00
|
|
|
impl Into<Element> for Identity {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
Element::builder("identity")
|
|
|
|
.ns(ns::DISCO_INFO)
|
|
|
|
.attr("category", self.category)
|
|
|
|
.attr("type", self.type_)
|
2017-05-27 21:10:00 +00:00
|
|
|
.attr("xml:lang", self.lang)
|
2017-05-24 22:47:27 +00:00
|
|
|
.attr("name", self.name)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoElements for Identity {
|
|
|
|
fn into_elements(self, emitter: &mut ElementEmitter) {
|
|
|
|
emitter.append_child(self.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2017-07-20 16:39:59 +00:00
|
|
|
pub struct DiscoInfoResult {
|
2017-04-18 19:44:36 +00:00
|
|
|
pub node: Option<String>,
|
|
|
|
pub identities: Vec<Identity>,
|
|
|
|
pub features: Vec<Feature>,
|
|
|
|
pub extensions: Vec<DataForm>,
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
impl TryFrom<Element> for DiscoInfoResult {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
fn try_from(elem: Element) -> Result<DiscoInfoResult, Error> {
|
2017-05-06 20:01:15 +00:00
|
|
|
if !elem.is("query", ns::DISCO_INFO) {
|
|
|
|
return Err(Error::ParseError("This is not a disco#info element."));
|
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-05-06 20:01:15 +00:00
|
|
|
let mut identities: Vec<Identity> = vec!();
|
|
|
|
let mut features: Vec<Feature> = vec!();
|
|
|
|
let mut extensions: Vec<DataForm> = vec!();
|
|
|
|
|
2017-05-22 18:00:04 +00:00
|
|
|
let node = get_attr!(elem, "node", optional);
|
2017-05-06 20:01:15 +00:00
|
|
|
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("feature", ns::DISCO_INFO) {
|
2017-05-22 18:00:04 +00:00
|
|
|
let feature = get_attr!(child, "var", required);
|
2017-05-06 20:01:15 +00:00
|
|
|
features.push(Feature {
|
2017-05-22 18:00:04 +00:00
|
|
|
var: feature,
|
2017-05-06 20:01:15 +00:00
|
|
|
});
|
|
|
|
} else if child.is("identity", ns::DISCO_INFO) {
|
2017-05-22 18:00:04 +00:00
|
|
|
let category = get_attr!(child, "category", required);
|
2017-05-06 20:01:15 +00:00
|
|
|
if category == "" {
|
|
|
|
return Err(Error::ParseError("Identity must have a non-empty 'category' attribute."))
|
|
|
|
}
|
|
|
|
|
2017-05-22 18:00:04 +00:00
|
|
|
let type_ = get_attr!(child, "type", required);
|
2017-05-06 20:01:15 +00:00
|
|
|
if type_ == "" {
|
|
|
|
return Err(Error::ParseError("Identity must have a non-empty 'type' attribute."))
|
|
|
|
}
|
|
|
|
|
2017-05-27 21:10:00 +00:00
|
|
|
let lang = get_attr!(child, "xml:lang", optional);
|
2017-05-22 18:00:04 +00:00
|
|
|
let name = get_attr!(child, "name", optional);
|
2017-05-06 20:01:15 +00:00
|
|
|
identities.push(Identity {
|
2017-05-22 18:00:04 +00:00
|
|
|
category: category,
|
|
|
|
type_: type_,
|
2017-05-27 21:10:00 +00:00
|
|
|
lang: lang,
|
2017-05-06 20:01:15 +00:00
|
|
|
name: name,
|
|
|
|
});
|
|
|
|
} else if child.is("x", ns::DATA_FORMS) {
|
2017-05-23 22:31:33 +00:00
|
|
|
let data_form = DataForm::try_from(child.clone())?;
|
2017-05-22 18:00:04 +00:00
|
|
|
if data_form.type_ != DataFormType::Result_ {
|
|
|
|
return Err(Error::ParseError("Data form must have a 'result' type in disco#info."));
|
2017-05-06 20:01:15 +00:00
|
|
|
}
|
|
|
|
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."));
|
2017-04-18 19:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:01:15 +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() }) {
|
|
|
|
return Err(Error::ParseError("disco#info feature not present in disco#info."));
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
Ok(DiscoInfoResult {
|
2017-05-06 20:01:15 +00:00
|
|
|
node: node,
|
|
|
|
identities: identities,
|
|
|
|
features: features,
|
|
|
|
extensions: extensions
|
|
|
|
})
|
2017-04-18 19:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
impl Into<Element> for DiscoInfoResult {
|
2017-05-06 20:01:15 +00:00
|
|
|
fn into(self) -> Element {
|
2017-05-23 22:31:33 +00:00
|
|
|
for _ in self.extensions {
|
2017-05-06 20:01:15 +00:00
|
|
|
panic!("Not yet implemented!");
|
|
|
|
}
|
2017-05-24 22:47:27 +00:00
|
|
|
Element::builder("query")
|
|
|
|
.ns(ns::DISCO_INFO)
|
|
|
|
.attr("node", self.node)
|
|
|
|
.append(self.identities)
|
|
|
|
.append(self.features)
|
|
|
|
.build()
|
2017-04-20 20:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 19:44:36 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:01:15 +00:00
|
|
|
use super::*;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
#[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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
2017-04-18 19:44:36 +00:00
|
|
|
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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown element in disco#info.");
|
2017-04-20 20:02:51 +00:00
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-04-20 20:02:51 +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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'category' missing.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'type' missing.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Identity must have a non-empty 'type' attribute.");
|
2017-04-20 20:02:51 +00:00
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-04-20 20:02:51 +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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'var' missing.");
|
2017-04-20 20:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_result() {
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-20 20:02:51 +00:00
|
|
|
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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
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();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "disco#info feature not present in disco#info.");
|
|
|
|
}
|
|
|
|
}
|