blocking.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 jid::Jid;
 12use minidom::Element;
 13
 14use error::Error;
 15
 16use ns;
 17use iq::{IqGetPayload, IqSetPayload, IqResultPayload};
 18
 19generate_empty_element!(
 20    /// The element requesting the blocklist, the result iq will contain a
 21    /// [BlocklistResult].
 22    BlocklistRequest, "blocklist", BLOCKING
 23);
 24
 25impl IqGetPayload for BlocklistRequest {}
 26
 27macro_rules! generate_blocking_element {
 28    ($(#[$meta:meta])* $elem:ident, $name:tt) => (
 29        $(#[$meta])*
 30        #[derive(Debug, Clone)]
 31        pub struct $elem {
 32            /// List of JIDs affected by this command.
 33            pub items: Vec<Jid>,
 34        }
 35
 36        impl TryFrom<Element> for $elem {
 37            type Err = Error;
 38
 39            fn try_from(elem: Element) -> Result<$elem, Error> {
 40                check_self!(elem, $name, BLOCKING);
 41                check_no_attributes!(elem, $name);
 42                let mut items = vec!();
 43                for child in elem.children() {
 44                    check_self!(child, "item", BLOCKING);
 45                    check_no_unknown_attributes!(child, "item", ["jid"]);
 46                    check_no_children!(child, "item");
 47                    items.push(get_attr!(child, "jid", required));
 48                }
 49                Ok($elem { items })
 50            }
 51        }
 52
 53        impl From<$elem> for Element {
 54            fn from(elem: $elem) -> Element {
 55                Element::builder($name)
 56                        .ns(ns::BLOCKING)
 57                        .append(elem.items.into_iter().map(|jid| {
 58                             Element::builder("item")
 59                                     .ns(ns::BLOCKING)
 60                                     .attr("jid", jid)
 61                                     .build()
 62                         }).collect::<Vec<_>>())
 63                        .build()
 64            }
 65        }
 66    );
 67}
 68
 69generate_blocking_element!(
 70    /// The element containing the current blocklist, as a reply from
 71    /// [BlocklistRequest].
 72    BlocklistResult, "blocklist"
 73);
 74
 75impl IqResultPayload for BlocklistResult {}
 76
 77// TODO: Prevent zero elements from being allowed.
 78generate_blocking_element!(
 79    /// A query to block one or more JIDs.
 80    Block, "block"
 81);
 82
 83impl IqSetPayload for Block {}
 84
 85generate_blocking_element!(
 86    /// A query to unblock one or more JIDs, or all of them.
 87    ///
 88    /// Warning: not putting any JID there means clearing out the blocklist.
 89    Unblock, "unblock"
 90);
 91
 92impl IqSetPayload for Unblock {}
 93
 94generate_empty_element!(
 95    /// The application-specific error condition when a message is blocked.
 96    Blocked, "blocked", BLOCKING_ERRORS
 97);
 98
 99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_simple() {
105        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'/>".parse().unwrap();
106        let request_elem = elem.clone();
107        BlocklistRequest::try_from(request_elem).unwrap();
108
109        let result_elem = elem.clone();
110        let result = BlocklistResult::try_from(result_elem).unwrap();
111        assert_eq!(result.items, vec!());
112
113        let elem: Element = "<block xmlns='urn:xmpp:blocking'/>".parse().unwrap();
114        let block = Block::try_from(elem).unwrap();
115        assert_eq!(block.items, vec!());
116
117        let elem: Element = "<unblock xmlns='urn:xmpp:blocking'/>".parse().unwrap();
118        let unblock = Unblock::try_from(elem).unwrap();
119        assert_eq!(unblock.items, vec!());
120    }
121
122    #[test]
123    fn test_items() {
124        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
125        let two_items = vec!(
126            Jid {
127                node: Some(String::from("coucou")),
128                domain: String::from("coucou"),
129                resource: None,
130            },
131            Jid {
132                node: None,
133                domain: String::from("domain"),
134                resource: None,
135            },
136        );
137
138        let request_elem = elem.clone();
139        let error = BlocklistRequest::try_from(request_elem).unwrap_err();
140        let message = match error {
141            Error::ParseError(string) => string,
142            _ => panic!(),
143        };
144        assert_eq!(message, "Unknown child in blocklist element.");
145
146        let result_elem = elem.clone();
147        let result = BlocklistResult::try_from(result_elem).unwrap();
148        assert_eq!(result.items, two_items);
149
150        let elem: Element = "<block xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></block>".parse().unwrap();
151        let block = Block::try_from(elem).unwrap();
152        assert_eq!(block.items, two_items);
153
154        let elem: Element = "<unblock xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></unblock>".parse().unwrap();
155        let unblock = Unblock::try_from(elem).unwrap();
156        assert_eq!(unblock.items, two_items);
157    }
158
159    #[test]
160    fn test_invalid() {
161        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>".parse().unwrap();
162        let request_elem = elem.clone();
163        let error = BlocklistRequest::try_from(request_elem).unwrap_err();
164        let message = match error {
165            Error::ParseError(string) => string,
166            _ => panic!(),
167        };
168        assert_eq!(message, "Unknown attribute in blocklist element.");
169
170        let result_elem = elem.clone();
171        let error = BlocklistResult::try_from(result_elem).unwrap_err();
172        let message = match error {
173            Error::ParseError(string) => string,
174            _ => panic!(),
175        };
176        assert_eq!(message, "Unknown attribute in blocklist element.");
177
178        let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>".parse().unwrap();
179        let error = Block::try_from(elem).unwrap_err();
180        let message = match error {
181            Error::ParseError(string) => string,
182            _ => panic!(),
183        };
184        assert_eq!(message, "Unknown attribute in block element.");
185
186        let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>".parse().unwrap();
187        let error = Unblock::try_from(elem).unwrap_err();
188        let message = match error {
189            Error::ParseError(string) => string,
190            _ => panic!(),
191        };
192        assert_eq!(message, "Unknown attribute in unblock element.");
193    }
194}