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/.
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
use xso::{AsXml, FromXml};
|
2024-06-21 15:54:12 +00:00
|
|
|
|
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;
|
2019-10-22 23:32:41 +00:00
|
|
|
use jid::Jid;
|
2017-10-31 15:48:11 +00:00
|
|
|
|
2024-06-21 15:54:12 +00:00
|
|
|
/// The element requesting the blocklist, the result iq will contain a
|
|
|
|
/// [BlocklistResult].
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-21 15:54:12 +00:00
|
|
|
#[xml(namespace = ns::BLOCKING, name = "blocklist")]
|
|
|
|
pub struct BlocklistRequest;
|
2017-10-31 15:48:11 +00:00
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqGetPayload for BlocklistRequest {}
|
|
|
|
|
2024-08-04 19:01:25 +00:00
|
|
|
/// The element containing the current blocklist, as a reply from
|
|
|
|
/// [BlocklistRequest].
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
#[xml(namespace = ns::BLOCKING, name = "blocklist")]
|
|
|
|
pub struct BlocklistResult {
|
|
|
|
/// List of JIDs affected by this command.
|
|
|
|
#[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
|
|
|
|
pub items: Vec<Jid>,
|
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
|
|
|
|
2024-08-04 19:01:25 +00:00
|
|
|
/// A query to block one or more JIDs.
|
2018-08-02 16:15:56 +00:00
|
|
|
// TODO: Prevent zero elements from being allowed.
|
2024-08-04 19:01:25 +00:00
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
#[xml(namespace = ns::BLOCKING, name = "block")]
|
|
|
|
pub struct Block {
|
|
|
|
/// List of JIDs affected by this command.
|
|
|
|
#[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
|
|
|
|
pub items: Vec<Jid>,
|
|
|
|
}
|
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
|
|
|
|
2024-08-04 19:01:25 +00:00
|
|
|
/// A query to unblock one or more JIDs, or all of them.
|
|
|
|
///
|
|
|
|
/// Warning: not putting any JID there means clearing out the blocklist.
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
#[xml(namespace = ns::BLOCKING, name = "unblock")]
|
|
|
|
pub struct Unblock {
|
|
|
|
/// List of JIDs affected by this command.
|
|
|
|
#[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
|
|
|
|
pub items: Vec<Jid>,
|
|
|
|
}
|
2018-08-02 16:15:56 +00:00
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqSetPayload for Unblock {}
|
|
|
|
|
2024-06-21 15:54:12 +00:00
|
|
|
/// The application-specific error condition when a message is blocked.
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-21 15:54:12 +00:00
|
|
|
#[xml(namespace = ns::BLOCKING_ERRORS, name = "blocked")]
|
|
|
|
pub struct Blocked;
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-08-04 19:01:25 +00:00
|
|
|
use xso::error::{Error, FromElementError};
|
2024-06-21 14:27:43 +00:00
|
|
|
|
2017-10-31 15:48:11 +00:00
|
|
|
use super::*;
|
2024-08-04 19:01:25 +00:00
|
|
|
use minidom::Element;
|
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();
|
2020-12-10 19:36:54 +00:00
|
|
|
assert!(result.items.is_empty());
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
let elem: Element = "<block xmlns='urn:xmpp:blocking'/>".parse().unwrap();
|
|
|
|
let block = Block::try_from(elem).unwrap();
|
2020-12-10 19:36:54 +00:00
|
|
|
assert!(block.items.is_empty());
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
let elem: Element = "<unblock xmlns='urn:xmpp:blocking'/>".parse().unwrap();
|
|
|
|
let unblock = Unblock::try_from(elem).unwrap();
|
2020-12-10 19:36:54 +00:00
|
|
|
assert!(unblock.items.is_empty());
|
2017-10-31 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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![
|
2023-06-20 12:07:50 +00:00
|
|
|
Jid::new("coucou@coucou").unwrap(),
|
|
|
|
Jid::new("domain").unwrap(),
|
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 {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 15:48:11 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-06-22 13:35:56 +00:00
|
|
|
assert_eq!(message, "Unknown attribute in BlocklistRequest element.");
|
2017-10-31 15:48:11 +00:00
|
|
|
|
|
|
|
let result_elem = elem.clone();
|
|
|
|
let error = BlocklistResult::try_from(result_elem).unwrap_err();
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 15:48:11 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-08-04 19:01:25 +00:00
|
|
|
assert_eq!(message, "Unknown attribute in BlocklistResult element.");
|
2017-10-31 15:48:11 +00:00
|
|
|
|
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 {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 15:48:11 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-08-04 19:01:25 +00:00
|
|
|
assert_eq!(message, "Unknown attribute in Block element.");
|
2017-10-31 15:48:11 +00:00
|
|
|
|
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 {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 15:48:11 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-08-04 19:01:25 +00:00
|
|
|
assert_eq!(message, "Unknown attribute in Unblock element.");
|
2017-10-31 15:48:11 +00:00
|
|
|
}
|
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 {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-01-12 19:41:40 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-06-22 13:35:56 +00:00
|
|
|
assert_eq!(message, "Unknown child in BlocklistRequest element.");
|
2019-01-12 19:41:40 +00:00
|
|
|
}
|
2017-10-31 15:48:11 +00:00
|
|
|
}
|