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