2017-04-29 22:14:34 +01:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2019-06-26 02:06:38 +02:00
|
|
|
// Copyright (c) 2017 Maxime “pep” Buquet <pep@bouah.net>
|
2017-04-29 22:14:34 +01:00
|
|
|
//
|
|
|
|
|
// 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/.
|
|
|
|
|
|
2018-12-18 15:27:30 +01:00
|
|
|
use crate::ns;
|
|
|
|
|
use crate::stanza_error::StanzaError;
|
2019-10-23 01:32:41 +02:00
|
|
|
use jid::Jid;
|
2024-07-24 20:28:22 +02:00
|
|
|
use minidom::Element;
|
2025-04-30 18:07:47 +02:00
|
|
|
use xso::{AsXml, FromXml};
|
2017-04-23 21:12:27 +01:00
|
|
|
|
2018-05-16 14:48:29 +02:00
|
|
|
/// Should be implemented on every known payload of an `<iq type='get'/>`.
|
|
|
|
|
pub trait IqGetPayload: TryFrom<Element> + Into<Element> {}
|
2017-07-20 18:31:17 +01:00
|
|
|
|
2018-05-16 14:48:29 +02:00
|
|
|
/// Should be implemented on every known payload of an `<iq type='set'/>`.
|
|
|
|
|
pub trait IqSetPayload: TryFrom<Element> + Into<Element> {}
|
2017-07-20 18:31:17 +01:00
|
|
|
|
2018-05-16 14:48:29 +02:00
|
|
|
/// Should be implemented on every known payload of an `<iq type='result'/>`.
|
|
|
|
|
pub trait IqResultPayload: TryFrom<Element> + Into<Element> {}
|
2017-07-20 18:31:17 +01:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Metadata of an IQ stanza.
|
|
|
|
|
pub struct IqHeader {
|
|
|
|
|
/// The sender JID.
|
|
|
|
|
pub from: Option<Jid>,
|
|
|
|
|
|
2025-11-22 21:28:34 +01:00
|
|
|
/// The recipient JID.
|
2025-04-30 18:07:47 +02:00
|
|
|
pub to: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The stanza's ID.
|
|
|
|
|
pub id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IqHeader {
|
|
|
|
|
/// Combine a header with [`IqPayload`] to create a full [`Iq`] stanza.
|
|
|
|
|
pub fn assemble(self, data: IqPayload) -> Iq {
|
|
|
|
|
data.assemble(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-13 11:58:37 +02:00
|
|
|
/// Payload of an IQ request stanza.
|
|
|
|
|
pub enum IqRequestPayload {
|
|
|
|
|
/// Payload of a type='get' stanza.
|
|
|
|
|
Get(Element),
|
|
|
|
|
|
|
|
|
|
/// Payload of a type='set' stanza.
|
|
|
|
|
Set(Element),
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Payload of an IQ stanza, by type.
|
|
|
|
|
pub enum IqPayload {
|
|
|
|
|
/// Payload of a type='get' stanza.
|
2017-05-19 00:04:42 +01:00
|
|
|
Get(Element),
|
2018-08-08 19:52:37 +02:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Payload of a type='set' stanza.
|
2017-05-19 00:04:42 +01:00
|
|
|
Set(Element),
|
2018-08-08 19:52:37 +02:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Payload of a type='result' stanza.
|
2017-05-19 00:04:42 +01:00
|
|
|
Result(Option<Element>),
|
2018-08-08 19:52:37 +02:00
|
|
|
|
2025-08-28 22:02:30 +02:00
|
|
|
/// The error carried in a type='error' stanza.
|
2017-05-06 21:13:53 +01:00
|
|
|
Error(StanzaError),
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
impl IqPayload {
|
|
|
|
|
/// Combine the data with an [`IqHeader`] to create a full [`Iq`] stanza.
|
|
|
|
|
pub fn assemble(self, IqHeader { from, to, id }: IqHeader) -> Iq {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get(payload) => Iq::Get {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload,
|
|
|
|
|
},
|
|
|
|
|
Self::Set(payload) => Iq::Set {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload,
|
|
|
|
|
},
|
|
|
|
|
Self::Result(payload) => Iq::Result {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload,
|
|
|
|
|
},
|
|
|
|
|
Self::Error(error) => Iq::Error {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload: None,
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 23:10:13 +01:00
|
|
|
/// The main structure representing the `<iq/>` stanza.
|
2025-04-30 18:07:47 +02:00
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
|
#[xml(namespace = ns::DEFAULT_NS, name = "iq", attribute = "type", exhaustive)]
|
|
|
|
|
pub enum Iq {
|
|
|
|
|
/// An `<iq type='get'/>` stanza.
|
|
|
|
|
#[xml(value = "get")]
|
|
|
|
|
Get {
|
|
|
|
|
/// The JID emitting this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
from: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The recipient of this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
to: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The @id attribute of this stanza, which is required in order to match a
|
|
|
|
|
/// request with its result/error.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
id: String,
|
|
|
|
|
|
|
|
|
|
/// The payload content of this stanza.
|
|
|
|
|
#[xml(element(n = 1))]
|
|
|
|
|
payload: Element,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// An `<iq type='set'/>` stanza.
|
|
|
|
|
#[xml(value = "set")]
|
|
|
|
|
Set {
|
|
|
|
|
/// The JID emitting this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
from: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The recipient of this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
to: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The @id attribute of this stanza, which is required in order to match a
|
|
|
|
|
/// request with its result/error.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
id: String,
|
|
|
|
|
|
|
|
|
|
/// The payload content of this stanza.
|
|
|
|
|
#[xml(element(n = 1))]
|
|
|
|
|
payload: Element,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// An `<iq type='result'/>` stanza.
|
|
|
|
|
#[xml(value = "result")]
|
|
|
|
|
Result {
|
|
|
|
|
/// The JID emitting this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
from: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The recipient of this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
to: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The @id attribute of this stanza, which is required in order to match a
|
|
|
|
|
/// request with its result/error.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
id: String,
|
|
|
|
|
|
|
|
|
|
/// The payload content of this stanza.
|
|
|
|
|
#[xml(element(n = 1, default))]
|
|
|
|
|
payload: Option<Element>,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// An `<iq type='error'/>` stanza.
|
|
|
|
|
#[xml(value = "error")]
|
|
|
|
|
Error {
|
|
|
|
|
/// The JID emitting this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
from: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The recipient of this stanza.
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
to: Option<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The @id attribute of this stanza, which is required in order to match a
|
|
|
|
|
/// request with its result/error.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
id: String,
|
|
|
|
|
|
|
|
|
|
/// The error carried by this stanza.
|
|
|
|
|
#[xml(child)]
|
|
|
|
|
error: StanzaError,
|
|
|
|
|
|
|
|
|
|
/// The optional payload content which caused the error.
|
|
|
|
|
///
|
|
|
|
|
/// As per
|
|
|
|
|
/// [RFC 6120 § 8.3.1](https://datatracker.ietf.org/doc/html/rfc6120#section-8.3.1),
|
|
|
|
|
/// the emitter of an error stanza MAY include the original XML which
|
|
|
|
|
/// caused the error. However, recipients MUST NOT rely on this.
|
|
|
|
|
#[xml(element(n = 1, default))]
|
|
|
|
|
payload: Option<Element>,
|
|
|
|
|
},
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-16 14:49:00 +02:00
|
|
|
impl Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Assemble a new Iq stanza from an [`IqHeader`] and the given
|
|
|
|
|
/// [`IqPayload`].
|
|
|
|
|
pub fn assemble(header: IqHeader, data: IqPayload) -> Self {
|
|
|
|
|
data.assemble(header)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Creates an `<iq/>` stanza containing a get request.
|
2019-02-26 19:25:43 +01:00
|
|
|
pub fn from_get<S: Into<String>>(id: S, payload: impl IqGetPayload) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
Iq::Get {
|
2018-05-16 14:49:00 +02:00
|
|
|
from: None,
|
|
|
|
|
to: None,
|
2019-02-26 19:25:43 +01:00
|
|
|
id: id.into(),
|
2025-04-30 18:07:47 +02:00
|
|
|
payload: payload.into(),
|
2018-05-16 14:49:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Creates an `<iq/>` stanza containing a set request.
|
2019-02-26 19:25:43 +01:00
|
|
|
pub fn from_set<S: Into<String>>(id: S, payload: impl IqSetPayload) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
Iq::Set {
|
2018-05-16 14:49:00 +02:00
|
|
|
from: None,
|
|
|
|
|
to: None,
|
2019-02-26 19:25:43 +01:00
|
|
|
id: id.into(),
|
2025-04-30 18:07:47 +02:00
|
|
|
payload: payload.into(),
|
2018-05-16 14:49:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 20:47:53 +01:00
|
|
|
/// Creates an empty `<iq type="result"/>` stanza.
|
|
|
|
|
pub fn empty_result<S: Into<String>>(to: Jid, id: S) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
Iq::Result {
|
2020-11-27 20:47:53 +01:00
|
|
|
from: None,
|
|
|
|
|
to: Some(to),
|
|
|
|
|
id: id.into(),
|
2025-04-30 18:07:47 +02:00
|
|
|
payload: None,
|
2020-11-27 20:47:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Creates an `<iq/>` stanza containing a result.
|
2019-02-26 19:25:43 +01:00
|
|
|
pub fn from_result<S: Into<String>>(id: S, payload: Option<impl IqResultPayload>) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
Iq::Result {
|
2018-05-16 14:49:00 +02:00
|
|
|
from: None,
|
|
|
|
|
to: None,
|
2019-02-26 19:25:43 +01:00
|
|
|
id: id.into(),
|
2025-04-30 18:07:47 +02:00
|
|
|
payload: payload.map(Into::into),
|
2018-05-16 14:49:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Creates an `<iq/>` stanza containing an error.
|
2019-02-26 19:25:43 +01:00
|
|
|
pub fn from_error<S: Into<String>>(id: S, payload: StanzaError) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
Iq::Error {
|
2018-05-16 14:49:00 +02:00
|
|
|
from: None,
|
|
|
|
|
to: None,
|
2019-02-26 19:25:43 +01:00
|
|
|
id: id.into(),
|
2025-04-30 18:07:47 +02:00
|
|
|
error: payload,
|
|
|
|
|
payload: None,
|
2018-05-16 14:49:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Sets the recipient of this stanza.
|
2018-05-16 14:49:00 +02:00
|
|
|
pub fn with_to(mut self, to: Jid) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
*self.to_mut() = Some(to);
|
2018-05-16 14:49:00 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Sets the emitter of this stanza.
|
2018-05-16 14:49:00 +02:00
|
|
|
pub fn with_from(mut self, from: Jid) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
*self.from_mut() = Some(from);
|
2018-05-16 14:49:00 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:52:37 +02:00
|
|
|
/// Sets the id of this stanza, in order to later match its response.
|
2018-05-16 14:49:00 +02:00
|
|
|
pub fn with_id(mut self, id: String) -> Iq {
|
2025-04-30 18:07:47 +02:00
|
|
|
*self.id_mut() = id;
|
2018-05-16 14:49:00 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Access the sender address.
|
|
|
|
|
pub fn from(&self) -> Option<&Jid> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get { from, .. }
|
|
|
|
|
| Self::Set { from, .. }
|
|
|
|
|
| Self::Result { from, .. }
|
|
|
|
|
| Self::Error { from, .. } => from.as_ref(),
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
2025-04-30 18:07:47 +02:00
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Access the sender address, mutably.
|
|
|
|
|
pub fn from_mut(&mut self) -> &mut Option<Jid> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get { ref mut from, .. }
|
|
|
|
|
| Self::Set { ref mut from, .. }
|
|
|
|
|
| Self::Result { ref mut from, .. }
|
|
|
|
|
| Self::Error { ref mut from, .. } => from,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Access the recipient address.
|
|
|
|
|
pub fn to(&self) -> Option<&Jid> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get { to, .. }
|
|
|
|
|
| Self::Set { to, .. }
|
|
|
|
|
| Self::Result { to, .. }
|
|
|
|
|
| Self::Error { to, .. } => to.as_ref(),
|
|
|
|
|
}
|
2017-05-06 21:16:56 +01:00
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Access the recipient address, mutably.
|
|
|
|
|
pub fn to_mut(&mut self) -> &mut Option<Jid> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get { ref mut to, .. }
|
|
|
|
|
| Self::Set { ref mut to, .. }
|
|
|
|
|
| Self::Result { ref mut to, .. }
|
|
|
|
|
| Self::Error { ref mut to, .. } => to,
|
|
|
|
|
}
|
2017-05-06 21:16:56 +01:00
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Access the id.
|
|
|
|
|
pub fn id(&self) -> &str {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get { id, .. }
|
|
|
|
|
| Self::Set { id, .. }
|
|
|
|
|
| Self::Result { id, .. }
|
|
|
|
|
| Self::Error { id, .. } => id.as_str(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-09 17:01:56 +02:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Access the id mutably.
|
|
|
|
|
pub fn id_mut(&mut self) -> &mut String {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get { ref mut id, .. }
|
|
|
|
|
| Self::Set { ref mut id, .. }
|
|
|
|
|
| Self::Result { ref mut id, .. }
|
|
|
|
|
| Self::Error { ref mut id, .. } => id,
|
2024-08-09 17:01:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Split the IQ stanza in its metadata and data.
|
|
|
|
|
///
|
|
|
|
|
/// Note that this discards the optional original error-inducing
|
|
|
|
|
/// [`payload`][`Self::Error::payload`] of the
|
|
|
|
|
/// [`Iq::Error`][`Self::Error`] variant.
|
|
|
|
|
pub fn split(self) -> (IqHeader, IqPayload) {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Get {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload,
|
|
|
|
|
} => (IqHeader { from, to, id }, IqPayload::Get(payload)),
|
|
|
|
|
Self::Set {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload,
|
|
|
|
|
} => (IqHeader { from, to, id }, IqPayload::Set(payload)),
|
|
|
|
|
Self::Result {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
payload,
|
|
|
|
|
} => (IqHeader { from, to, id }, IqPayload::Result(payload)),
|
|
|
|
|
Self::Error {
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
error,
|
|
|
|
|
payload: _,
|
|
|
|
|
} => (IqHeader { from, to, id }, IqPayload::Error(error)),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-09 17:01:56 +02:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
/// Return the [`IqHeader`] of this stanza, discarding the payload.
|
|
|
|
|
pub fn into_header(self) -> IqHeader {
|
|
|
|
|
self.split().0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return the [`IqPayload`] of this stanza, discarding the header.
|
|
|
|
|
///
|
|
|
|
|
/// Note that this also discards the optional original error-inducing
|
|
|
|
|
/// [`payload`][`Self::Error::payload`] of the
|
|
|
|
|
/// [`Iq::Error`][`Self::Error`] variant.
|
|
|
|
|
pub fn into_payload(self) -> IqPayload {
|
|
|
|
|
self.split().1
|
2024-08-09 17:01:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 20:38:13 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 21:13:53 +01:00
|
|
|
use super::*;
|
2018-12-18 15:27:30 +01:00
|
|
|
use crate::disco::DiscoInfoQuery;
|
2018-12-18 15:32:05 +01:00
|
|
|
use crate::stanza_error::{DefinedCondition, ErrorType};
|
2025-04-30 18:07:47 +02:00
|
|
|
use xso::error::{Error, FromElementError};
|
2017-04-23 20:38:13 +01:00
|
|
|
|
2018-10-28 13:10:48 +01:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_size!(IqHeader, 44);
|
|
|
|
|
assert_size!(IqPayload, 108);
|
|
|
|
|
assert_size!(Iq, 212);
|
2018-10-28 13:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 14:26:16 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_size!(IqHeader, 88);
|
|
|
|
|
assert_size!(IqPayload, 216);
|
|
|
|
|
assert_size!(Iq, 424);
|
2018-10-26 14:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-23 20:38:13 +01:00
|
|
|
#[test]
|
|
|
|
|
fn test_require_type() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 20:38:13 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client'/>".parse().unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept'/>".parse().unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let error = Iq::try_from(elem).unwrap_err();
|
2017-04-23 20:38:13 +01:00
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-23 20:38:13 +01:00
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(message, "Missing discriminator attribute.");
|
|
|
|
|
}
|
2019-02-21 20:48:02 +01:00
|
|
|
|
2025-04-30 18:07:47 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_require_id() {
|
|
|
|
|
for type_ in ["get", "set", "result", "error"] {
|
|
|
|
|
#[cfg(not(feature = "component"))]
|
|
|
|
|
let elem: Element = format!("<iq xmlns='jabber:client' type='{}'/>", type_)
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = format!("<iq xmlns='jabber:component:accept' type='{}'/>", type_)
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let error = Iq::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
// Slicing here, because the rest of the error message is specific
|
|
|
|
|
// about the enum variant.
|
|
|
|
|
assert_eq!(&message[..33], "Required attribute field 'id' on ");
|
|
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='get' id='foo'>
|
2017-07-29 06:49:02 +01:00
|
|
|
<foo xmlns='bar'/>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='get' id='foo'>
|
2017-07-29 06:49:02 +01:00
|
|
|
<foo xmlns='bar'/>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
let query: Element = "<foo xmlns='bar'/>".parse().unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(iq.from(), None);
|
|
|
|
|
assert_eq!(iq.to(), None);
|
|
|
|
|
assert_eq!(iq.id(), "foo");
|
|
|
|
|
assert!(match iq {
|
|
|
|
|
Iq::Get { payload, .. } => payload == query,
|
2018-12-18 15:32:05 +01:00
|
|
|
_ => false,
|
2017-04-23 20:38:13 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_set() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='set' id='vcard'>
|
2017-04-23 20:38:13 +01:00
|
|
|
<vCard xmlns='vcard-temp'/>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='set' id='vcard'>
|
2017-07-29 06:49:02 +01:00
|
|
|
<vCard xmlns='vcard-temp'/>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2017-04-23 20:38:13 +01:00
|
|
|
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(iq.from(), None);
|
|
|
|
|
assert_eq!(iq.to(), None);
|
|
|
|
|
assert_eq!(iq.id(), "vcard");
|
|
|
|
|
assert!(match iq {
|
|
|
|
|
Iq::Set { payload, .. } => payload == vcard,
|
2018-12-18 15:32:05 +01:00
|
|
|
_ => false,
|
2017-04-23 20:38:13 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_result_empty() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-10-23 01:32:41 +02:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result' id='res'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'/>"
|
2018-12-18 15:32:05 +01:00
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(iq.from(), None);
|
|
|
|
|
assert_eq!(iq.to(), None);
|
|
|
|
|
assert_eq!(iq.id(), "res");
|
|
|
|
|
assert!(match iq {
|
|
|
|
|
Iq::Result { payload: None, .. } => true,
|
2017-04-23 20:38:13 +01:00
|
|
|
_ => false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_result() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result' id='res'>
|
2017-04-23 20:38:13 +01:00
|
|
|
<query xmlns='http://jabber.org/protocol/disco#items'/>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'>
|
2017-07-29 06:49:02 +01:00
|
|
|
<query xmlns='http://jabber.org/protocol/disco#items'/>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2018-12-18 15:32:05 +01:00
|
|
|
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(iq.from(), None);
|
|
|
|
|
assert_eq!(iq.to(), None);
|
|
|
|
|
assert_eq!(iq.id(), "res");
|
|
|
|
|
assert!(match iq {
|
|
|
|
|
Iq::Result {
|
|
|
|
|
payload: Some(element),
|
|
|
|
|
..
|
|
|
|
|
} => element == query,
|
2017-04-23 20:38:13 +01:00
|
|
|
_ => false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_error() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='error' id='err1'>
|
2017-04-23 20:38:13 +01:00
|
|
|
<ping xmlns='urn:xmpp:ping'/>
|
|
|
|
|
<error type='cancel'>
|
|
|
|
|
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
|
|
|
|
</error>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='error' id='err1'>
|
2017-07-29 06:49:02 +01:00
|
|
|
<ping xmlns='urn:xmpp:ping'/>
|
|
|
|
|
<error type='cancel'>
|
|
|
|
|
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
|
|
|
|
</error>
|
2018-12-18 15:32:05 +01:00
|
|
|
</iq>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(iq.from(), None);
|
|
|
|
|
assert_eq!(iq.to(), None);
|
|
|
|
|
assert_eq!(iq.id(), "err1");
|
|
|
|
|
match iq {
|
|
|
|
|
Iq::Error { error, .. } => {
|
2017-05-06 21:13:53 +01:00
|
|
|
assert_eq!(error.type_, ErrorType::Cancel);
|
2017-05-01 01:50:38 +01:00
|
|
|
assert_eq!(error.by, None);
|
2018-12-18 15:32:05 +01:00
|
|
|
assert_eq!(
|
|
|
|
|
error.defined_condition,
|
|
|
|
|
DefinedCondition::ServiceUnavailable
|
|
|
|
|
);
|
2017-05-01 01:50:38 +01:00
|
|
|
assert_eq!(error.texts.len(), 0);
|
|
|
|
|
assert_eq!(error.other, None);
|
2018-12-18 15:32:05 +01:00
|
|
|
}
|
2017-05-01 01:23:56 +01:00
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_children_invalid() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='error' id='error'/>"
|
2018-12-18 15:32:05 +01:00
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='error' id='error'/>"
|
2018-12-18 15:32:05 +01:00
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let error = Iq::try_from(elem).unwrap_err();
|
2017-04-23 20:38:13 +01:00
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-23 20:38:13 +01:00
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2025-04-30 18:07:47 +02:00
|
|
|
assert_eq!(message, "Missing child field 'error' in Iq::Error element.");
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialise() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-10-23 01:32:41 +02:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result' id='res'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'/>"
|
2018-12-18 15:32:05 +01:00
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
let iq2 = Iq::Result {
|
2017-04-23 20:38:13 +01:00
|
|
|
from: None,
|
|
|
|
|
to: None,
|
2019-02-21 20:48:02 +01:00
|
|
|
id: String::from("res"),
|
2025-04-30 18:07:47 +02:00
|
|
|
payload: None,
|
2017-04-23 20:38:13 +01:00
|
|
|
};
|
2017-05-23 23:31:33 +01:00
|
|
|
let elem2 = iq2.into();
|
2017-04-23 20:38:13 +01:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
|
}
|
2017-04-23 21:12:27 +01:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_disco() {
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='get' id='disco'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
2017-07-29 06:49:02 +01:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 20:48:02 +01:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='get' id='disco'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2025-04-30 18:07:47 +02:00
|
|
|
let disco_info = match iq {
|
|
|
|
|
Iq::Get { payload, .. } => DiscoInfoQuery::try_from(payload).unwrap(),
|
2017-05-19 00:04:42 +01:00
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2018-05-16 14:48:29 +02:00
|
|
|
assert!(disco_info.node.is_none());
|
2017-04-23 21:12:27 +01:00
|
|
|
}
|
2017-04-23 20:38:13 +01:00
|
|
|
}
|