disco.rs

  1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7#![deny(missing_docs)]
  8
  9use try_from::TryFrom;
 10
 11use minidom::Element;
 12use jid::Jid;
 13
 14use error::Error;
 15use ns;
 16
 17use iq::{IqGetPayload, IqResultPayload};
 18use data_forms::{DataForm, DataFormType};
 19
 20generate_element_with_only_attributes!(
 21/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#info'/>` element.
 22///
 23/// It should only be used in an `<iq type='get'/>`, as it can only represent
 24/// the request, and not a result.
 25DiscoInfoQuery, "query", DISCO_INFO, [
 26    /// Node on which we are doing the discovery.
 27    node: Option<String> = "node" => optional,
 28]);
 29
 30impl IqGetPayload for DiscoInfoQuery {}
 31
 32generate_element_with_only_attributes!(
 33/// Structure representing a `<feature xmlns='http://jabber.org/protocol/disco#info'/>` element.
 34#[derive(PartialEq)]
 35Feature, "feature", DISCO_INFO, [
 36    /// Namespace of the feature we want to represent.
 37    var: String = "var" => required,
 38]);
 39
 40/// Structure representing an `<identity xmlns='http://jabber.org/protocol/disco#info'/>` element.
 41#[derive(Debug, Clone)]
 42pub struct Identity {
 43    /// Category of this identity.
 44    pub category: String, // TODO: use an enum here.
 45
 46    /// Type of this identity.
 47    pub type_: String, // TODO: use an enum here.
 48
 49    /// Lang of the name of this identity.
 50    pub lang: Option<String>,
 51
 52    /// Name of this identity.
 53    pub name: Option<String>,
 54}
 55
 56impl TryFrom<Element> for Identity {
 57    type Err = Error;
 58
 59    fn try_from(elem: Element) -> Result<Identity, Error> {
 60        check_self!(elem, "identity", DISCO_INFO, "disco#info identity");
 61        check_no_children!(elem, "disco#info identity");
 62        check_no_unknown_attributes!(elem, "disco#info identity", ["category", "type", "xml:lang", "name"]);
 63
 64        let category = get_attr!(elem, "category", required);
 65        if category == "" {
 66            return Err(Error::ParseError("Identity must have a non-empty 'category' attribute."))
 67        }
 68
 69        let type_ = get_attr!(elem, "type", required);
 70        if type_ == "" {
 71            return Err(Error::ParseError("Identity must have a non-empty 'type' attribute."))
 72        }
 73
 74        Ok(Identity {
 75            category: category,
 76            type_: type_,
 77            lang: get_attr!(elem, "xml:lang", optional),
 78            name: get_attr!(elem, "name", optional),
 79        })
 80    }
 81}
 82
 83impl From<Identity> for Element {
 84    fn from(identity: Identity) -> Element {
 85        Element::builder("identity")
 86                .ns(ns::DISCO_INFO)
 87                .attr("category", identity.category)
 88                .attr("type", identity.type_)
 89                .attr("xml:lang", identity.lang)
 90                .attr("name", identity.name)
 91                .build()
 92    }
 93}
 94
 95/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#info'/>` element.
 96///
 97/// It should only be used in an `<iq type='result'/>`, as it can only
 98/// represent the result, and not a request.
 99#[derive(Debug, Clone)]
100pub struct DiscoInfoResult {
101    /// Node on which we have done this discovery.
102    pub node: Option<String>,
103
104    /// List of identities exposed by this entity.
105    pub identities: Vec<Identity>,
106
107    /// List of features supported by this entity.
108    pub features: Vec<Feature>,
109
110    /// List of extensions reported by this entity.
111    pub extensions: Vec<DataForm>,
112}
113
114impl IqResultPayload for DiscoInfoResult {}
115
116impl TryFrom<Element> for DiscoInfoResult {
117    type Err = Error;
118
119    fn try_from(elem: Element) -> Result<DiscoInfoResult, Error> {
120        check_self!(elem, "query", DISCO_INFO, "disco#info result");
121        check_no_unknown_attributes!(elem, "disco#info result", ["node"]);
122
123        let mut result = DiscoInfoResult {
124            node: get_attr!(elem, "node", optional),
125            identities: vec!(),
126            features: vec!(),
127            extensions: vec!(),
128        };
129        let mut parsing_identities_done = false;
130        let mut parsing_features_done = false;
131
132        for child in elem.children() {
133            if child.is("identity", ns::DISCO_INFO) {
134                if parsing_identities_done {
135                    return Err(Error::ParseError("Identity found after features or data forms in disco#info."));
136                }
137                let identity = Identity::try_from(child.clone())?;
138                result.identities.push(identity);
139            } else if child.is("feature", ns::DISCO_INFO) {
140                parsing_identities_done = true;
141                if parsing_features_done {
142                    return Err(Error::ParseError("Feature found after data forms in disco#info."));
143                }
144                let feature = Feature::try_from(child.clone())?;
145                result.features.push(feature);
146            } else if child.is("x", ns::DATA_FORMS) {
147                parsing_identities_done = true;
148                parsing_features_done = true;
149                let data_form = DataForm::try_from(child.clone())?;
150                if data_form.type_ != DataFormType::Result_ {
151                    return Err(Error::ParseError("Data form must have a 'result' type in disco#info."));
152                }
153                if data_form.form_type.is_none() {
154                    return Err(Error::ParseError("Data form found without a FORM_TYPE."));
155                }
156                result.extensions.push(data_form);
157            } else {
158                return Err(Error::ParseError("Unknown element in disco#info."));
159            }
160        }
161
162        if result.identities.is_empty() {
163            return Err(Error::ParseError("There must be at least one identity in disco#info."));
164        }
165        if result.features.is_empty() {
166            return Err(Error::ParseError("There must be at least one feature in disco#info."));
167        }
168        if !result.features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) {
169            return Err(Error::ParseError("disco#info feature not present in disco#info."));
170        }
171
172        Ok(result)
173    }
174}
175
176impl From<DiscoInfoResult> for Element {
177    fn from(disco: DiscoInfoResult) -> Element {
178        Element::builder("query")
179                .ns(ns::DISCO_INFO)
180                .attr("node", disco.node)
181                .append(disco.identities)
182                .append(disco.features)
183                .append(disco.extensions)
184                .build()
185    }
186}
187
188generate_element_with_only_attributes!(
189/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#items'/>` element.
190///
191/// It should only be used in an `<iq type='get'/>`, as it can only represent
192/// the request, and not a result.
193DiscoItemsQuery, "query", DISCO_ITEMS, [
194    /// Node on which we are doing the discovery.
195    node: Option<String> = "node" => optional,
196]);
197
198impl IqGetPayload for DiscoItemsQuery {}
199
200generate_element_with_only_attributes!(
201/// Structure representing an `<item xmlns='http://jabber.org/protocol/disco#items'/>` element.
202Item, "item", DISCO_ITEMS, [
203    /// JID of the entity pointed by this item.
204    jid: Jid = "jid" => required,
205    /// Node of the entity pointed by this item.
206    node: Option<String> = "node" => optional,
207    /// Name of the entity pointed by this item.
208    name: Option<String> = "name" => optional,
209]);
210
211generate_element_with_children!(
212    /// Structure representing a `<query
213    /// xmlns='http://jabber.org/protocol/disco#items'/>` element.
214    ///
215    /// It should only be used in an `<iq type='result'/>`, as it can only
216    /// represent the result, and not a request.
217    DiscoItemsResult, "query", DISCO_ITEMS,
218    attributes: [
219        /// Node on which we have done this discovery.
220        node: Option<String> = "node" => optional
221    ],
222    children: [
223        /// List of items pointed by this entity.
224        items: Vec<Item> = ("item", DISCO_ITEMS) => Item
225    ]
226);
227
228impl IqResultPayload for DiscoItemsResult {}
229
230#[cfg(test)]
231mod tests {
232    use super::*;
233    use compare_elements::NamespaceAwareCompare;
234    use std::str::FromStr;
235
236    #[test]
237    fn test_simple() {
238        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();
239        let query = DiscoInfoResult::try_from(elem).unwrap();
240        assert!(query.node.is_none());
241        assert_eq!(query.identities.len(), 1);
242        assert_eq!(query.features.len(), 1);
243        assert!(query.extensions.is_empty());
244    }
245
246    #[test]
247    fn test_extension() {
248        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();
249        let elem1 = elem.clone();
250        let query = DiscoInfoResult::try_from(elem).unwrap();
251        assert!(query.node.is_none());
252        assert_eq!(query.identities.len(), 1);
253        assert_eq!(query.features.len(), 1);
254        assert_eq!(query.extensions.len(), 1);
255        assert_eq!(query.extensions[0].form_type, Some(String::from("example")));
256
257        let elem2 = query.into();
258        assert!(elem1.compare_to(&elem2));
259    }
260
261    #[test]
262    fn test_invalid() {
263        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>".parse().unwrap();
264        let error = DiscoInfoResult::try_from(elem).unwrap_err();
265        let message = match error {
266            Error::ParseError(string) => string,
267            _ => panic!(),
268        };
269        assert_eq!(message, "Unknown element in disco#info.");
270    }
271
272    #[test]
273    fn test_invalid_identity() {
274        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>".parse().unwrap();
275        let error = DiscoInfoResult::try_from(elem).unwrap_err();
276        let message = match error {
277            Error::ParseError(string) => string,
278            _ => panic!(),
279        };
280        assert_eq!(message, "Required attribute 'category' missing.");
281
282        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
283        let error = DiscoInfoResult::try_from(elem).unwrap_err();
284        let message = match error {
285            Error::ParseError(string) => string,
286            _ => panic!(),
287        };
288        assert_eq!(message, "Identity must have a non-empty 'category' attribute.");
289
290        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
291        let error = DiscoInfoResult::try_from(elem).unwrap_err();
292        let message = match error {
293            Error::ParseError(string) => string,
294            _ => panic!(),
295        };
296        assert_eq!(message, "Required attribute 'type' missing.");
297
298        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
299        let error = DiscoInfoResult::try_from(elem).unwrap_err();
300        let message = match error {
301            Error::ParseError(string) => string,
302            _ => panic!(),
303        };
304        assert_eq!(message, "Identity must have a non-empty 'type' attribute.");
305    }
306
307    #[test]
308    fn test_invalid_feature() {
309        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>".parse().unwrap();
310        let error = DiscoInfoResult::try_from(elem).unwrap_err();
311        let message = match error {
312            Error::ParseError(string) => string,
313            _ => panic!(),
314        };
315        assert_eq!(message, "Required attribute 'var' missing.");
316    }
317
318    #[test]
319    fn test_invalid_result() {
320        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
321        let error = DiscoInfoResult::try_from(elem).unwrap_err();
322        let message = match error {
323            Error::ParseError(string) => string,
324            _ => panic!(),
325        };
326        assert_eq!(message, "There must be at least one identity in disco#info.");
327
328        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
329        let error = DiscoInfoResult::try_from(elem).unwrap_err();
330        let message = match error {
331            Error::ParseError(string) => string,
332            _ => panic!(),
333        };
334        assert_eq!(message, "There must be at least one feature in disco#info.");
335
336        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();
337        let error = DiscoInfoResult::try_from(elem).unwrap_err();
338        let message = match error {
339            Error::ParseError(string) => string,
340            _ => panic!(),
341        };
342        assert_eq!(message, "disco#info feature not present in disco#info.");
343
344        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();
345        let error = DiscoInfoResult::try_from(elem).unwrap_err();
346        let message = match error {
347            Error::ParseError(string) => string,
348            _ => panic!(),
349        };
350        assert_eq!(message, "Identity found after features or data forms in disco#info.");
351
352        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();
353        let error = DiscoInfoResult::try_from(elem).unwrap_err();
354        let message = match error {
355            Error::ParseError(string) => string,
356            _ => panic!(),
357        };
358        assert_eq!(message, "Feature found after data forms in disco#info.");
359    }
360
361    #[test]
362    fn test_simple_items() {
363        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
364        let query = DiscoItemsQuery::try_from(elem).unwrap();
365        assert!(query.node.is_none());
366
367        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>".parse().unwrap();
368        let query = DiscoItemsQuery::try_from(elem).unwrap();
369        assert_eq!(query.node, Some(String::from("coucou")));
370    }
371
372    #[test]
373    fn test_simple_items_result() {
374        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
375        let query = DiscoItemsResult::try_from(elem).unwrap();
376        assert!(query.node.is_none());
377        assert!(query.items.is_empty());
378
379        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>".parse().unwrap();
380        let query = DiscoItemsResult::try_from(elem).unwrap();
381        assert_eq!(query.node, Some(String::from("coucou")));
382        assert!(query.items.is_empty());
383    }
384
385    #[test]
386    fn test_answers_items_result() {
387        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();
388        let query = DiscoItemsResult::try_from(elem).unwrap();
389        let elem2 = Element::from(query);
390        let query = DiscoItemsResult::try_from(elem2).unwrap();
391        assert_eq!(query.items.len(), 2);
392        assert_eq!(query.items[0].jid, Jid::from_str("component").unwrap());
393        assert_eq!(query.items[0].node, None);
394        assert_eq!(query.items[0].name, None);
395        assert_eq!(query.items[1].jid, Jid::from_str("component2").unwrap());
396        assert_eq!(query.items[1].node, Some(String::from("test")));
397        assert_eq!(query.items[1].name, Some(String::from("A component")));
398    }
399}