2017-10-31 15:48:11 +00:00
|
|
|
// 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/.
|
|
|
|
|
2019-01-13 11:39:51 +00:00
|
|
|
use crate::util::error::Error;
|
2018-12-18 14:32:05 +00:00
|
|
|
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::ns;
|
2018-12-18 14:32:05 +00:00
|
|
|
use jid::Jid;
|
|
|
|
use minidom::Element;
|
2019-04-12 08:58:42 +00:00
|
|
|
use std::convert::TryFrom;
|
2017-10-31 15:48:11 +00:00
|
|
|
|
2018-08-02 16:15:56 +00:00
|
|
|
generate_empty_element!(
|
|
|
|
/// The element requesting the blocklist, the result iq will contain a
|
|
|
|
/// [BlocklistResult].
|
2018-12-18 14:32:05 +00:00
|
|
|
BlocklistRequest,
|
|
|
|
"blocklist",
|
|
|
|
BLOCKING
|
2018-08-02 16:15:56 +00:00
|
|
|
);
|
2017-10-31 15:48:11 +00:00
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqGetPayload for BlocklistRequest {}
|
|
|
|
|
2017-10-31 15:48:11 +00:00
|
|
|
macro_rules! generate_blocking_element {
|
2018-08-02 16:15:56 +00:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:tt) => (
|
|
|
|
$(#[$meta])*
|
2017-10-31 15:48:11 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct $elem {
|
2018-08-02 16:15:56 +00:00
|
|
|
/// List of JIDs affected by this command.
|
2017-10-31 15:48:11 +00:00
|
|
|
pub items: Vec<Jid>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for $elem {
|
2019-04-12 08:58:42 +00:00
|
|
|
type Error = Error;
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<$elem, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
check_self!(elem, $name, BLOCKING);
|
2017-10-31 15:48:11 +00:00
|
|
|
check_no_attributes!(elem, $name);
|
2017-10-31 18:53:51 +00:00
|
|
|
let mut items = vec!();
|
|
|
|
for child in elem.children() {
|
2018-05-14 14:30:28 +00:00
|
|
|
check_self!(child, "item", BLOCKING);
|
2017-10-31 18:53:51 +00:00
|
|
|
check_no_unknown_attributes!(child, "item", ["jid"]);
|
|
|
|
check_no_children!(child, "item");
|
2019-02-24 19:48:19 +00:00
|
|
|
items.push(get_attr!(child, "jid", Required));
|
2017-10-31 18:53:51 +00:00
|
|
|
}
|
|
|
|
Ok($elem { items })
|
2017-10-31 15:48:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<$elem> for Element {
|
|
|
|
fn from(elem: $elem) -> Element {
|
|
|
|
Element::builder($name)
|
|
|
|
.ns(ns::BLOCKING)
|
2019-07-24 22:20:38 +00:00
|
|
|
.append_all(elem.items.into_iter().map(|jid| {
|
2017-10-31 15:48:11 +00:00
|
|
|
Element::builder("item")
|
|
|
|
.ns(ns::BLOCKING)
|
|
|
|
.attr("jid", jid)
|
2019-07-24 22:20:38 +00:00
|
|
|
}))
|
2017-10-31 15:48:11 +00:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:15:56 +00:00
|
|
|
generate_blocking_element!(
|
|
|
|
/// The element containing the current blocklist, as a reply from
|
|
|
|
/// [BlocklistRequest].
|
2018-12-18 14:32:05 +00:00
|
|
|
BlocklistResult,
|
|
|
|
"blocklist"
|
2018-08-02 16:15:56 +00:00
|
|
|
);
|
2017-10-31 15:48:11 +00:00
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqResultPayload for BlocklistResult {}
|
2018-08-02 16:15:56 +00:00
|
|
|
|
|
|
|
// TODO: Prevent zero elements from being allowed.
|
|
|
|
generate_blocking_element!(
|
|
|
|
/// A query to block one or more JIDs.
|
2018-12-18 14:32:05 +00:00
|
|
|
Block,
|
|
|
|
"block"
|
2018-08-02 16:15:56 +00:00
|
|
|
);
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqSetPayload for Block {}
|
2018-08-02 16:15:56 +00:00
|
|
|
|
|
|
|
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.
|
2018-12-18 14:32:05 +00:00
|
|
|
Unblock,
|
|
|
|
"unblock"
|
2018-08-02 16:15:56 +00:00
|
|
|
);
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqSetPayload for Unblock {}
|
|
|
|
|
2018-08-02 16:15:56 +00:00
|
|
|
generate_empty_element!(
|
|
|
|
/// The application-specific error condition when a message is blocked.
|
2018-12-18 14:32:05 +00:00
|
|
|
Blocked,
|
|
|
|
"blocked",
|
|
|
|
BLOCKING_ERRORS
|
2018-08-02 16:15:56 +00:00
|
|
|
);
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-10 21:17:09 +00:00
|
|
|
use jid::BareJid;
|
2017-10-31 15:48:11 +00:00
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
#[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")]
|
2018-10-26 12:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(BlocklistRequest, 0);
|
|
|
|
assert_size!(BlocklistResult, 24);
|
|
|
|
assert_size!(Block, 24);
|
|
|
|
assert_size!(Unblock, 24);
|
|
|
|
}
|
|
|
|
|
2017-10-31 15:48:11 +00:00
|
|
|
#[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();
|
2018-12-18 14:32:05 +00:00
|
|
|
let two_items = vec![
|
2019-06-10 21:17:09 +00:00
|
|
|
Jid::Bare(BareJid {
|
2017-10-31 15:48:11 +00:00
|
|
|
node: Some(String::from("coucou")),
|
|
|
|
domain: String::from("coucou"),
|
2019-06-10 21:17:09 +00:00
|
|
|
}),
|
|
|
|
Jid::Bare(BareJid {
|
2017-10-31 15:48:11 +00:00
|
|
|
node: None,
|
|
|
|
domain: String::from("domain"),
|
2019-06-10 21:17:09 +00:00
|
|
|
}),
|
2018-12-18 14:32:05 +00:00
|
|
|
];
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2017-10-31 15:48:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-10-31 15:48:11 +00:00
|
|
|
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.");
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-10-31 15:48:11 +00:00
|
|
|
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.");
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-10-31 15:48:11 +00:00
|
|
|
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.");
|
|
|
|
}
|
2019-01-12 19:41:40 +00:00
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 19:41:40 +00:00
|
|
|
#[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.");
|
|
|
|
}
|
2017-10-31 15:48:11 +00:00
|
|
|
}
|