1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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 crate::util::error::Error;
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns;
use jid::Jid;
use minidom::Element;
use std::convert::TryFrom;

generate_empty_element!(
    /// The element requesting the blocklist, the result iq will contain a
    /// [BlocklistResult].
    BlocklistRequest,
    "blocklist",
    BLOCKING
);

impl IqGetPayload for BlocklistRequest {}

macro_rules! generate_blocking_element {
    ($(#[$meta:meta])* $elem:ident, $name:tt) => (
        $(#[$meta])*
        #[derive(Debug, Clone)]
        pub struct $elem {
            /// List of JIDs affected by this command.
            pub items: Vec<Jid>,
        }

        impl TryFrom<Element> for $elem {
            type Error = Error;

            fn try_from(elem: Element) -> Result<$elem, Error> {
                check_self!(elem, $name, BLOCKING);
                check_no_attributes!(elem, $name);
                let mut items = vec!();
                for child in elem.children() {
                    check_self!(child, "item", BLOCKING);
                    check_no_unknown_attributes!(child, "item", ["jid"]);
                    check_no_children!(child, "item");
                    items.push(get_attr!(child, "jid", Required));
                }
                Ok($elem { items })
            }
        }

        impl From<$elem> for Element {
            fn from(elem: $elem) -> Element {
                Element::builder($name)
                        .ns(ns::BLOCKING)
                        .append(elem.items.into_iter().map(|jid| {
                             Element::builder("item")
                                     .ns(ns::BLOCKING)
                                     .attr("jid", jid)
                                     .build()
                         }).collect::<Vec<_>>())
                        .build()
            }
        }
    );
}

generate_blocking_element!(
    /// The element containing the current blocklist, as a reply from
    /// [BlocklistRequest].
    BlocklistResult,
    "blocklist"
);

impl IqResultPayload for BlocklistResult {}

// TODO: Prevent zero elements from being allowed.
generate_blocking_element!(
    /// A query to block one or more JIDs.
    Block,
    "block"
);

impl IqSetPayload for Block {}

generate_blocking_element!(
    /// A query to unblock one or more JIDs, or all of them.
    ///
    /// Warning: not putting any JID there means clearing out the blocklist.
    Unblock,
    "unblock"
);

impl IqSetPayload for Unblock {}

generate_empty_element!(
    /// The application-specific error condition when a message is blocked.
    Blocked,
    "blocked",
    BLOCKING_ERRORS
);

#[cfg(test)]
mod tests {
    use super::*;
    use jid::BareJid;

    #[cfg(target_pointer_width = "32")]
    #[test]
    fn test_size() {
        assert_size!(BlocklistRequest, 0);
        assert_size!(BlocklistResult, 12);
        assert_size!(Block, 12);
        assert_size!(Unblock, 12);
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn test_size() {
        assert_size!(BlocklistRequest, 0);
        assert_size!(BlocklistResult, 24);
        assert_size!(Block, 24);
        assert_size!(Unblock, 24);
    }

    #[test]
    fn test_simple() {
        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'/>".parse().unwrap();
        let request_elem = elem.clone();
        BlocklistRequest::try_from(request_elem).unwrap();

        let result_elem = elem.clone();
        let result = BlocklistResult::try_from(result_elem).unwrap();
        assert_eq!(result.items, vec!());

        let elem: Element = "<block xmlns='urn:xmpp:blocking'/>".parse().unwrap();
        let block = Block::try_from(elem).unwrap();
        assert_eq!(block.items, vec!());

        let elem: Element = "<unblock xmlns='urn:xmpp:blocking'/>".parse().unwrap();
        let unblock = Unblock::try_from(elem).unwrap();
        assert_eq!(unblock.items, vec!());
    }

    #[test]
    fn test_items() {
        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
        let two_items = vec![
            Jid::Bare(BareJid {
                node: Some(String::from("coucou")),
                domain: String::from("coucou"),
            }),
            Jid::Bare(BareJid {
                node: None,
                domain: String::from("domain"),
            }),
        ];

        let result_elem = elem.clone();
        let result = BlocklistResult::try_from(result_elem).unwrap();
        assert_eq!(result.items, two_items);

        let elem: Element = "<block xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></block>".parse().unwrap();
        let block = Block::try_from(elem).unwrap();
        assert_eq!(block.items, two_items);

        let elem: Element = "<unblock xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></unblock>".parse().unwrap();
        let unblock = Unblock::try_from(elem).unwrap();
        assert_eq!(unblock.items, two_items);
    }

    #[cfg(not(feature = "disable-validation"))]
    #[test]
    fn test_invalid() {
        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>"
            .parse()
            .unwrap();
        let request_elem = elem.clone();
        let error = BlocklistRequest::try_from(request_elem).unwrap_err();
        let message = match error {
            Error::ParseError(string) => string,
            _ => panic!(),
        };
        assert_eq!(message, "Unknown attribute in blocklist element.");

        let result_elem = elem.clone();
        let error = BlocklistResult::try_from(result_elem).unwrap_err();
        let message = match error {
            Error::ParseError(string) => string,
            _ => panic!(),
        };
        assert_eq!(message, "Unknown attribute in blocklist element.");

        let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
            .parse()
            .unwrap();
        let error = Block::try_from(elem).unwrap_err();
        let message = match error {
            Error::ParseError(string) => string,
            _ => panic!(),
        };
        assert_eq!(message, "Unknown attribute in block element.");

        let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
            .parse()
            .unwrap();
        let error = Unblock::try_from(elem).unwrap_err();
        let message = match error {
            Error::ParseError(string) => string,
            _ => panic!(),
        };
        assert_eq!(message, "Unknown attribute in unblock element.");
    }

    #[cfg(not(feature = "disable-validation"))]
    #[test]
    fn test_non_empty_blocklist_request() {
        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
        let error = BlocklistRequest::try_from(elem).unwrap_err();
        let message = match error {
            Error::ParseError(string) => string,
            _ => panic!(),
        };
        assert_eq!(message, "Unknown child in blocklist element.");
    }
}