xmpp-rs/parsers/src/disco.rs

458 lines
16 KiB
Rust
Raw Normal View History

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/.
use xso::{
error::{Error, FromElementError},
FromXml, IntoXml,
};
2018-12-18 14:32:05 +00:00
use crate::data_forms::{DataForm, DataFormType};
use crate::iq::{IqGetPayload, IqResultPayload};
2018-12-18 14:32:05 +00:00
use crate::ns;
use crate::rsm::{SetQuery, SetResult};
use crate::Element;
use jid::Jid;
2017-04-18 19:44:36 +00:00
/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#info'/>` element.
///
/// It should only be used in an `<iq type='get'/>`, as it can only represent
/// the request, and not a result.
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::DISCO_INFO, name = "query")]
pub struct DiscoInfoQuery {
/// Node on which we are doing the discovery.
#[xml(attribute(default))]
pub node: Option<String>,
}
2017-07-20 16:39:59 +00:00
impl IqGetPayload for DiscoInfoQuery {}
generate_element!(
2021-04-10 12:36:12 +00:00
#[derive(Eq, Hash)]
/// Structure representing a `<feature xmlns='http://jabber.org/protocol/disco#info'/>` element.
Feature, "feature", DISCO_INFO,
attributes: [
/// Namespace of the feature we want to represent.
var: Required<String> = "var",
]);
impl Feature {
/// Create a new `<feature/>` with the according `@var`.
pub fn new<S: Into<String>>(var: S) -> Feature {
Feature { var: var.into() }
}
}
2019-02-28 01:26:10 +00:00
generate_element!(
/// Structure representing an `<identity xmlns='http://jabber.org/protocol/disco#info'/>` element.
Identity, "identity", DISCO_INFO,
attributes: [
/// Category of this identity.
// TODO: use an enum here.
category: RequiredNonEmpty<String> = "category",
2019-02-28 01:26:10 +00:00
/// Type of this identity.
// TODO: use an enum here.
type_: RequiredNonEmpty<String> = "type",
2019-02-28 01:26:10 +00:00
/// Lang of the name of this identity.
lang: Option<String> = "xml:lang",
2019-02-28 01:26:10 +00:00
/// Name of this identity.
name: Option<String> = "name",
]
);
2017-04-18 19:44:36 +00:00
impl Identity {
/// Create a new `<identity/>`.
pub fn new<C, T, L, N>(category: C, type_: T, lang: L, name: N) -> Identity
where
C: Into<String>,
T: Into<String>,
L: Into<String>,
N: Into<String>,
{
Identity {
category: category.into(),
type_: type_.into(),
lang: Some(lang.into()),
name: Some(name.into()),
}
}
/// Create a new `<identity/>` without a name.
pub fn new_anonymous<C, T, L, N>(category: C, type_: T) -> Identity
where
C: Into<String>,
T: Into<String>,
{
Identity {
category: category.into(),
type_: type_.into(),
lang: None,
name: None,
}
}
}
/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#info'/>` element.
///
/// It should only be used in an `<iq type='result'/>`, as it can only
/// represent the result, and not a request.
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone)]
2017-07-20 16:39:59 +00:00
pub struct DiscoInfoResult {
/// Node on which we have done this discovery.
2017-04-18 19:44:36 +00:00
pub node: Option<String>,
/// List of identities exposed by this entity.
2017-04-18 19:44:36 +00:00
pub identities: Vec<Identity>,
/// List of features supported by this entity.
2017-04-18 19:44:36 +00:00
pub features: Vec<Feature>,
/// List of extensions reported by this entity.
2017-04-18 19:44:36 +00:00
pub extensions: Vec<DataForm>,
}
impl IqResultPayload for DiscoInfoResult {}
2017-07-20 16:39:59 +00:00
impl TryFrom<Element> for DiscoInfoResult {
type Error = FromElementError;
2017-04-18 19:44:36 +00:00
fn try_from(elem: Element) -> Result<DiscoInfoResult, FromElementError> {
2018-05-14 14:30:28 +00:00
check_self!(elem, "query", DISCO_INFO, "disco#info result");
check_no_unknown_attributes!(elem, "disco#info result", ["node"]);
2017-04-18 19:44:36 +00:00
let mut result = DiscoInfoResult {
node: get_attr!(elem, "node", Option),
2018-12-18 14:32:05 +00:00
identities: vec![],
features: vec![],
extensions: vec![],
};
2017-05-06 20:01:15 +00:00
for child in elem.children() {
if child.is("identity", ns::DISCO_INFO) {
let identity = Identity::try_from(child.clone())?;
result.identities.push(identity);
} else if child.is("feature", ns::DISCO_INFO) {
let feature = Feature::try_from(child.clone())?;
result.features.push(feature);
2017-05-06 20:01:15 +00:00
} else if child.is("x", ns::DATA_FORMS) {
let data_form = DataForm::try_from(child.clone())?;
if data_form.type_ != DataFormType::Result_ {
return Err(
Error::Other("Data form must have a 'result' type in disco#info.").into(),
);
2017-05-06 20:01:15 +00:00
}
if data_form.form_type.is_none() {
return Err(Error::Other("Data form found without a FORM_TYPE.").into());
2017-05-06 20:01:15 +00:00
}
result.extensions.push(data_form);
2017-05-06 20:01:15 +00:00
} else {
return Err(Error::Other("Unknown element in disco#info.").into());
2017-04-18 19:44:36 +00:00
}
}
#[cfg(not(feature = "disable-validation"))]
{
if result.identities.is_empty() {
return Err(
Error::Other("There must be at least one identity in disco#info.").into(),
);
}
if result.features.is_empty() {
return Err(
Error::Other("There must be at least one feature in disco#info.").into(),
);
}
2017-05-06 20:01:15 +00:00
}
Ok(result)
2017-04-18 19:44:36 +00:00
}
}
impl From<DiscoInfoResult> for Element {
fn from(disco: DiscoInfoResult) -> Element {
Element::builder("query", ns::DISCO_INFO)
2018-12-18 14:32:05 +00:00
.attr("node", disco.node)
.append_all(disco.identities)
.append_all(disco.features)
.append_all(disco.extensions.iter().cloned().map(Element::from))
2018-12-18 14:32:05 +00:00
.build()
2017-04-20 20:03:02 +00:00
}
}
generate_element!(
/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#items'/>` element.
///
/// It should only be used in an `<iq type='get'/>`, as it can only represent
/// the request, and not a result.
DiscoItemsQuery, "query", DISCO_ITEMS,
attributes: [
/// Node on which we are doing the discovery.
node: Option<String> = "node",
],
children: [
/// Optional paging via Result Set Management
rsm: Option<crate::rsm::SetQuery> = ("set", RSM) => SetQuery,
]);
2017-07-21 16:33:58 +00:00
impl IqGetPayload for DiscoItemsQuery {}
/// Structure representing an `<item xmlns='http://jabber.org/protocol/disco#items'/>` element.
#[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::DISCO_ITEMS, name = "item")]
pub struct Item {
/// JID of the entity pointed by this item.
#[xml(attribute)]
pub jid: Jid,
/// Node of the entity pointed by this item.
#[xml(attribute(default))]
pub node: Option<String>,
/// Name of the entity pointed by this item.
#[xml(attribute(default))]
pub name: Option<String>,
}
2017-07-21 16:33:58 +00:00
generate_element!(
/// Structure representing a `<query
/// xmlns='http://jabber.org/protocol/disco#items'/>` element.
///
/// It should only be used in an `<iq type='result'/>`, as it can only
/// represent the result, and not a request.
2018-05-14 14:30:28 +00:00
DiscoItemsResult, "query", DISCO_ITEMS,
attributes: [
/// Node on which we have done this discovery.
node: Option<String> = "node"
],
children: [
/// List of items pointed by this entity.
items: Vec<Item> = ("item", DISCO_ITEMS) => Item,
/// Optional paging via Result Set Management
rsm: Option<crate::rsm::SetResult> = ("set", RSM) => SetResult,
]
);
2017-07-21 16:33:58 +00:00
impl IqResultPayload for DiscoItemsResult {}
2017-04-18 19:44:36 +00:00
#[cfg(test)]
mod tests {
2017-05-06 20:01:15 +00:00
use super::*;
use jid::BareJid;
2017-04-18 19:44:36 +00:00
2018-10-28 12:10:48 +00:00
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Identity, 48);
assert_size!(Feature, 12);
assert_size!(DiscoInfoQuery, 12);
assert_size!(DiscoInfoResult, 48);
assert_size!(Item, 40);
assert_size!(DiscoItemsQuery, 52);
assert_size!(DiscoItemsResult, 64);
2018-10-28 12:10:48 +00:00
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Identity, 96);
assert_size!(Feature, 24);
assert_size!(DiscoInfoQuery, 24);
assert_size!(DiscoInfoResult, 96);
assert_size!(Item, 80);
assert_size!(DiscoItemsQuery, 104);
assert_size!(DiscoItemsResult, 128);
}
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_identity_after_feature() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature var='http://jabber.org/protocol/disco#info'/><identity category='client' type='pc'/></query>".parse().unwrap();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert!(query.extensions.is_empty());
}
#[test]
fn test_feature_after_dataform() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>coucou</value></field></x><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert_eq!(query.extensions.len(), 1);
}
#[test]
fn test_extension() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>example</value></field></x></query>".parse().unwrap();
let elem1 = elem.clone();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert!(query.node.is_none());
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert_eq!(query.extensions.len(), 1);
assert_eq!(query.extensions[0].form_type, Some(String::from("example")));
let elem2 = query.into();
assert_eq!(elem1, elem2);
}
2017-04-18 19:44:36 +00:00
#[test]
fn test_invalid() {
2018-12-18 14:32:05 +00:00
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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
assert_eq!(message, "Unknown element in disco#info.");
}
2017-04-18 19:44:36 +00:00
#[test]
fn test_invalid_identity() {
2018-12-18 14:32:05 +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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
assert_eq!(message, "Required attribute 'category' missing.");
2017-04-18 19:44:36 +00:00
2018-12-18 14:32:05 +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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
assert_eq!(message, "Required attribute 'category' must not be empty.");
2017-04-18 19:44:36 +00:00
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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
2019-02-28 01:26:10 +00:00
assert_eq!(message, "Required attribute 'type' must not be empty.");
}
2017-04-18 19:44:36 +00:00
#[test]
fn test_invalid_feature() {
2018-12-18 14:32:05 +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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
assert_eq!(message, "Required attribute 'var' missing.");
}
#[test]
fn test_invalid_result() {
2018-12-18 14:32:05 +00:00
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();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
2018-12-18 14:32:05 +00:00
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 {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-18 19:44:36 +00:00
_ => panic!(),
};
assert_eq!(message, "There must be at least one feature in disco#info.");
}
2017-07-21 16:33:58 +00:00
#[test]
fn test_simple_items() {
2018-12-18 14:32:05 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
.parse()
.unwrap();
2017-07-21 16:33:58 +00:00
let query = DiscoItemsQuery::try_from(elem).unwrap();
assert!(query.node.is_none());
2018-12-18 14:32:05 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>"
.parse()
.unwrap();
2017-07-21 16:33:58 +00:00
let query = DiscoItemsQuery::try_from(elem).unwrap();
assert_eq!(query.node, Some(String::from("coucou")));
}
#[test]
fn test_simple_items_result() {
2018-12-18 14:32:05 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
.parse()
.unwrap();
2017-07-21 16:33:58 +00:00
let query = DiscoItemsResult::try_from(elem).unwrap();
assert!(query.node.is_none());
assert!(query.items.is_empty());
2018-12-18 14:32:05 +00:00
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>"
.parse()
.unwrap();
2017-07-21 16:33:58 +00:00
let query = DiscoItemsResult::try_from(elem).unwrap();
assert_eq!(query.node, Some(String::from("coucou")));
assert!(query.items.is_empty());
}
#[test]
fn test_answers_items_result() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'><item jid='component'/><item jid='component2' node='test' name='A component'/></query>".parse().unwrap();
let query = DiscoItemsResult::try_from(elem).unwrap();
let elem2 = Element::from(query);
let query = DiscoItemsResult::try_from(elem2).unwrap();
2017-07-21 16:33:58 +00:00
assert_eq!(query.items.len(), 2);
assert_eq!(query.items[0].jid, BareJid::new("component").unwrap());
2017-07-21 16:33:58 +00:00
assert_eq!(query.items[0].node, None);
assert_eq!(query.items[0].name, None);
assert_eq!(query.items[1].jid, BareJid::new("component2").unwrap());
2017-07-21 16:33:58 +00:00
assert_eq!(query.items[1].node, Some(String::from("test")));
assert_eq!(query.items[1].name, Some(String::from("A component")));
}
2017-04-18 19:44:36 +00:00
}