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_all(elem.items.into_iter().map(|jid| {
55 Element::builder("item")
56 .ns(ns::BLOCKING)
57 .attr("jid", jid)
58 }))
59 .build()
60 }
61 }
62 );
63}
64
65generate_blocking_element!(
66 /// The element containing the current blocklist, as a reply from
67 /// [BlocklistRequest].
68 BlocklistResult,
69 "blocklist"
70);
71
72impl IqResultPayload for BlocklistResult {}
73
74// TODO: Prevent zero elements from being allowed.
75generate_blocking_element!(
76 /// A query to block one or more JIDs.
77 Block,
78 "block"
79);
80
81impl IqSetPayload for Block {}
82
83generate_blocking_element!(
84 /// A query to unblock one or more JIDs, or all of them.
85 ///
86 /// Warning: not putting any JID there means clearing out the blocklist.
87 Unblock,
88 "unblock"
89);
90
91impl IqSetPayload for Unblock {}
92
93generate_empty_element!(
94 /// The application-specific error condition when a message is blocked.
95 Blocked,
96 "blocked",
97 BLOCKING_ERRORS
98);
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use jid::BareJid;
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::Bare(BareJid {
147 node: Some(String::from("coucou")),
148 domain: String::from("coucou"),
149 }),
150 Jid::Bare(BareJid {
151 node: None,
152 domain: String::from("domain"),
153 }),
154 ];
155
156 let result_elem = elem.clone();
157 let result = BlocklistResult::try_from(result_elem).unwrap();
158 assert_eq!(result.items, two_items);
159
160 let elem: Element = "<block xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></block>".parse().unwrap();
161 let block = Block::try_from(elem).unwrap();
162 assert_eq!(block.items, two_items);
163
164 let elem: Element = "<unblock xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></unblock>".parse().unwrap();
165 let unblock = Unblock::try_from(elem).unwrap();
166 assert_eq!(unblock.items, two_items);
167 }
168
169 #[cfg(not(feature = "disable-validation"))]
170 #[test]
171 fn test_invalid() {
172 let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>"
173 .parse()
174 .unwrap();
175 let request_elem = elem.clone();
176 let error = BlocklistRequest::try_from(request_elem).unwrap_err();
177 let message = match error {
178 Error::ParseError(string) => string,
179 _ => panic!(),
180 };
181 assert_eq!(message, "Unknown attribute in blocklist element.");
182
183 let result_elem = elem.clone();
184 let error = BlocklistResult::try_from(result_elem).unwrap_err();
185 let message = match error {
186 Error::ParseError(string) => string,
187 _ => panic!(),
188 };
189 assert_eq!(message, "Unknown attribute in blocklist element.");
190
191 let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
192 .parse()
193 .unwrap();
194 let error = Block::try_from(elem).unwrap_err();
195 let message = match error {
196 Error::ParseError(string) => string,
197 _ => panic!(),
198 };
199 assert_eq!(message, "Unknown attribute in block element.");
200
201 let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
202 .parse()
203 .unwrap();
204 let error = Unblock::try_from(elem).unwrap_err();
205 let message = match error {
206 Error::ParseError(string) => string,
207 _ => panic!(),
208 };
209 assert_eq!(message, "Unknown attribute in unblock element.");
210 }
211
212 #[cfg(not(feature = "disable-validation"))]
213 #[test]
214 fn test_non_empty_blocklist_request() {
215 let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
216 let error = BlocklistRequest::try_from(elem).unwrap_err();
217 let message = match error {
218 Error::ParseError(string) => string,
219 _ => panic!(),
220 };
221 assert_eq!(message, "Unknown child in blocklist element.");
222 }
223}