parsers: port Iq to use the derive macros
This commit is contained in:
parent
8d8a19380a
commit
dc8c3eac4c
8 changed files with 486 additions and 312 deletions
|
|
@ -85,6 +85,17 @@ XXXX-YY-ZZ RELEASER <admin@example.com>
|
||||||
optional `parent` attribute.
|
optional `parent` attribute.
|
||||||
- presence::Presence has been ported to use xso (!477). It also uses the
|
- presence::Presence has been ported to use xso (!477). It also uses the
|
||||||
message::Lang newtype since !561.
|
message::Lang newtype since !561.
|
||||||
|
- iq::Iq has been ported to use xso (!572). That entails a couple
|
||||||
|
breaking changes:
|
||||||
|
|
||||||
|
- The IqType type has been removed and loosely replaced by the
|
||||||
|
IqPayload type.
|
||||||
|
- The Iq type is now an enum. Access to `from`, `to` and `id` needs
|
||||||
|
to happen via the provided accessor functions, by `match`-ing the
|
||||||
|
enum variant or by splitting the Iq into its IqHeader and IqPayload
|
||||||
|
using the `split()` method.
|
||||||
|
- The inverse is possible through the `assemble()` methods on
|
||||||
|
IqPayload, IqHeader and Iq, whichever is more convenient.
|
||||||
* New parsers/serialisers:
|
* New parsers/serialisers:
|
||||||
- Stream Features (RFC 6120) (!400)
|
- Stream Features (RFC 6120) (!400)
|
||||||
- Spam Reporting (XEP-0377) (!506)
|
- Spam Reporting (XEP-0377) (!506)
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@ use crate::ns;
|
||||||
use crate::stanza_error::StanzaError;
|
use crate::stanza_error::StanzaError;
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
use minidom::IntoAttributeValue;
|
use xso::{AsXml, FromXml};
|
||||||
use xso::error::{Error, FromElementError};
|
|
||||||
|
|
||||||
/// Should be implemented on every known payload of an `<iq type='get'/>`.
|
/// Should be implemented on every known payload of an `<iq type='get'/>`.
|
||||||
pub trait IqGetPayload: TryFrom<Element> + Into<Element> {}
|
pub trait IqGetPayload: TryFrom<Element> + Into<Element> {}
|
||||||
|
|
@ -21,228 +20,354 @@ pub trait IqSetPayload: TryFrom<Element> + Into<Element> {}
|
||||||
/// Should be implemented on every known payload of an `<iq type='result'/>`.
|
/// Should be implemented on every known payload of an `<iq type='result'/>`.
|
||||||
pub trait IqResultPayload: TryFrom<Element> + Into<Element> {}
|
pub trait IqResultPayload: TryFrom<Element> + Into<Element> {}
|
||||||
|
|
||||||
/// Represents one of the four possible iq types.
|
/// Metadata of an IQ stanza.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
pub struct IqHeader {
|
||||||
pub enum IqType {
|
/// The sender JID.
|
||||||
/// This is a request for accessing some data.
|
pub from: Option<Jid>,
|
||||||
|
|
||||||
|
/// The reciepient JID.
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Payload of an IQ stanza, by type.
|
||||||
|
pub enum IqPayload {
|
||||||
|
/// Payload of a type='get' stanza.
|
||||||
Get(Element),
|
Get(Element),
|
||||||
|
|
||||||
/// This is a request for modifying some data.
|
/// Payload of a type='set' stanza.
|
||||||
Set(Element),
|
Set(Element),
|
||||||
|
|
||||||
/// This is a result containing some data.
|
/// Payload of a type='result' stanza.
|
||||||
Result(Option<Element>),
|
Result(Option<Element>),
|
||||||
|
|
||||||
/// A get or set request failed.
|
/// The error carride in a type='error' stanza.
|
||||||
Error(StanzaError),
|
Error(StanzaError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoAttributeValue for &IqType {
|
impl IqPayload {
|
||||||
fn into_attribute_value(self) -> Option<String> {
|
/// Combine the data with an [`IqHeader`] to create a full [`Iq`] stanza.
|
||||||
Some(
|
pub fn assemble(self, IqHeader { from, to, id }: IqHeader) -> Iq {
|
||||||
match *self {
|
match self {
|
||||||
IqType::Get(_) => "get",
|
Self::Get(payload) => Iq::Get {
|
||||||
IqType::Set(_) => "set",
|
from,
|
||||||
IqType::Result(_) => "result",
|
to,
|
||||||
IqType::Error(_) => "error",
|
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,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The main structure representing the `<iq/>` stanza.
|
/// The main structure representing the `<iq/>` stanza.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
||||||
pub struct Iq {
|
#[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.
|
/// The JID emitting this stanza.
|
||||||
pub from: Option<Jid>,
|
#[xml(attribute(default))]
|
||||||
|
from: Option<Jid>,
|
||||||
|
|
||||||
/// The recipient of this stanza.
|
/// The recipient of this stanza.
|
||||||
pub to: Option<Jid>,
|
#[xml(attribute(default))]
|
||||||
|
to: Option<Jid>,
|
||||||
|
|
||||||
/// The @id attribute of this stanza, which is required in order to match a
|
/// The @id attribute of this stanza, which is required in order to match a
|
||||||
/// request with its result/error.
|
/// request with its result/error.
|
||||||
pub id: String,
|
#[xml(attribute)]
|
||||||
|
id: String,
|
||||||
|
|
||||||
/// The payload content of this stanza.
|
/// The payload content of this stanza.
|
||||||
pub payload: IqType,
|
#[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>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iq {
|
impl Iq {
|
||||||
|
/// Assemble a new Iq stanza from an [`IqHeader`] and the given
|
||||||
|
/// [`IqPayload`].
|
||||||
|
pub fn assemble(header: IqHeader, data: IqPayload) -> Self {
|
||||||
|
data.assemble(header)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates an `<iq/>` stanza containing a get request.
|
/// Creates an `<iq/>` stanza containing a get request.
|
||||||
pub fn from_get<S: Into<String>>(id: S, payload: impl IqGetPayload) -> Iq {
|
pub fn from_get<S: Into<String>>(id: S, payload: impl IqGetPayload) -> Iq {
|
||||||
Iq {
|
Iq::Get {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
payload: IqType::Get(payload.into()),
|
payload: payload.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an `<iq/>` stanza containing a set request.
|
/// Creates an `<iq/>` stanza containing a set request.
|
||||||
pub fn from_set<S: Into<String>>(id: S, payload: impl IqSetPayload) -> Iq {
|
pub fn from_set<S: Into<String>>(id: S, payload: impl IqSetPayload) -> Iq {
|
||||||
Iq {
|
Iq::Set {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
payload: IqType::Set(payload.into()),
|
payload: payload.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an empty `<iq type="result"/>` stanza.
|
/// Creates an empty `<iq type="result"/>` stanza.
|
||||||
pub fn empty_result<S: Into<String>>(to: Jid, id: S) -> Iq {
|
pub fn empty_result<S: Into<String>>(to: Jid, id: S) -> Iq {
|
||||||
Iq {
|
Iq::Result {
|
||||||
from: None,
|
from: None,
|
||||||
to: Some(to),
|
to: Some(to),
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
payload: IqType::Result(None),
|
payload: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an `<iq/>` stanza containing a result.
|
/// Creates an `<iq/>` stanza containing a result.
|
||||||
pub fn from_result<S: Into<String>>(id: S, payload: Option<impl IqResultPayload>) -> Iq {
|
pub fn from_result<S: Into<String>>(id: S, payload: Option<impl IqResultPayload>) -> Iq {
|
||||||
Iq {
|
Iq::Result {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
payload: IqType::Result(payload.map(Into::into)),
|
payload: payload.map(Into::into),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an `<iq/>` stanza containing an error.
|
/// Creates an `<iq/>` stanza containing an error.
|
||||||
pub fn from_error<S: Into<String>>(id: S, payload: StanzaError) -> Iq {
|
pub fn from_error<S: Into<String>>(id: S, payload: StanzaError) -> Iq {
|
||||||
Iq {
|
Iq::Error {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
payload: IqType::Error(payload),
|
error: payload,
|
||||||
|
payload: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the recipient of this stanza.
|
/// Sets the recipient of this stanza.
|
||||||
pub fn with_to(mut self, to: Jid) -> Iq {
|
pub fn with_to(mut self, to: Jid) -> Iq {
|
||||||
self.to = Some(to);
|
*self.to_mut() = Some(to);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the emitter of this stanza.
|
/// Sets the emitter of this stanza.
|
||||||
pub fn with_from(mut self, from: Jid) -> Iq {
|
pub fn with_from(mut self, from: Jid) -> Iq {
|
||||||
self.from = Some(from);
|
*self.from_mut() = Some(from);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the id of this stanza, in order to later match its response.
|
/// Sets the id of this stanza, in order to later match its response.
|
||||||
pub fn with_id(mut self, id: String) -> Iq {
|
pub fn with_id(mut self, id: String) -> Iq {
|
||||||
self.id = id;
|
*self.id_mut() = id;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Element> for Iq {
|
/// Access the sender address.
|
||||||
type Error = FromElementError;
|
pub fn from(&self) -> Option<&Jid> {
|
||||||
|
match self {
|
||||||
fn try_from(root: Element) -> Result<Iq, FromElementError> {
|
Self::Get { from, .. }
|
||||||
check_self!(root, "iq", DEFAULT_NS);
|
| Self::Set { from, .. }
|
||||||
let from = get_attr!(root, "from", Option);
|
| Self::Result { from, .. }
|
||||||
let to = get_attr!(root, "to", Option);
|
| Self::Error { from, .. } => from.as_ref(),
|
||||||
let id = get_attr!(root, "id", Required);
|
|
||||||
let type_: String = get_attr!(root, "type", Required);
|
|
||||||
|
|
||||||
let mut payload = None;
|
|
||||||
let mut error_payload = None;
|
|
||||||
for elem in root.children() {
|
|
||||||
if payload.is_some() {
|
|
||||||
return Err(Error::Other("Wrong number of children in iq element.").into());
|
|
||||||
}
|
|
||||||
if type_ == "error" {
|
|
||||||
if elem.is("error", ns::DEFAULT_NS) {
|
|
||||||
if error_payload.is_some() {
|
|
||||||
return Err(Error::Other("Wrong number of children in iq element.").into());
|
|
||||||
}
|
|
||||||
error_payload = Some(StanzaError::try_from(elem.clone())?);
|
|
||||||
} else if root.children().count() != 2 {
|
|
||||||
return Err(Error::Other("Wrong number of children in iq element.").into());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
payload = Some(elem.clone());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let type_ = if type_ == "get" {
|
/// Access the sender address, mutably.
|
||||||
if let Some(payload) = payload {
|
pub fn from_mut(&mut self) -> &mut Option<Jid> {
|
||||||
IqType::Get(payload)
|
match self {
|
||||||
} else {
|
Self::Get { ref mut from, .. }
|
||||||
return Err(Error::Other("Wrong number of children in iq element.").into());
|
| Self::Set { ref mut from, .. }
|
||||||
|
| Self::Result { ref mut from, .. }
|
||||||
|
| Self::Error { ref mut from, .. } => from,
|
||||||
}
|
}
|
||||||
} else if type_ == "set" {
|
|
||||||
if let Some(payload) = payload {
|
|
||||||
IqType::Set(payload)
|
|
||||||
} else {
|
|
||||||
return Err(Error::Other("Wrong number of children in iq element.").into());
|
|
||||||
}
|
}
|
||||||
} else if type_ == "result" {
|
|
||||||
if let Some(payload) = payload {
|
|
||||||
IqType::Result(Some(payload))
|
|
||||||
} else {
|
|
||||||
IqType::Result(None)
|
|
||||||
}
|
|
||||||
} else if type_ == "error" {
|
|
||||||
if let Some(payload) = error_payload {
|
|
||||||
IqType::Error(payload)
|
|
||||||
} else {
|
|
||||||
return Err(Error::Other("Wrong number of children in iq element.").into());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(Error::Other("Unknown iq type.").into());
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Iq {
|
/// 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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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,
|
from,
|
||||||
to,
|
to,
|
||||||
id,
|
id,
|
||||||
payload: type_,
|
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)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Iq> for Element {
|
/// Return the [`IqHeader`] of this stanza, discarding the payload.
|
||||||
fn from(iq: Iq) -> Element {
|
pub fn into_header(self) -> IqHeader {
|
||||||
let mut stanza = Element::builder("iq", ns::DEFAULT_NS)
|
self.split().0
|
||||||
.attr("from", iq.from)
|
|
||||||
.attr("to", iq.to)
|
|
||||||
.attr("id", iq.id)
|
|
||||||
.attr("type", &iq.payload)
|
|
||||||
.build();
|
|
||||||
let elem = match iq.payload {
|
|
||||||
IqType::Get(elem) | IqType::Set(elem) | IqType::Result(Some(elem)) => elem,
|
|
||||||
IqType::Error(error) => error.into(),
|
|
||||||
IqType::Result(None) => return stanza,
|
|
||||||
};
|
|
||||||
stanza.append_child(elem);
|
|
||||||
stanza
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::xso::FromXml for Iq {
|
/// Return the [`IqPayload`] of this stanza, discarding the header.
|
||||||
type Builder = ::xso::minidom_compat::FromEventsViaElement<Iq>;
|
///
|
||||||
|
/// Note that this also discards the optional original error-inducing
|
||||||
fn from_events(
|
/// [`payload`][`Self::Error::payload`] of the
|
||||||
qname: ::xso::exports::rxml::QName,
|
/// [`Iq::Error`][`Self::Error`] variant.
|
||||||
attrs: ::xso::exports::rxml::AttrMap,
|
pub fn into_payload(self) -> IqPayload {
|
||||||
_ctx: &::xso::Context<'_>,
|
self.split().1
|
||||||
) -> Result<Self::Builder, ::xso::error::FromEventsError> {
|
|
||||||
if qname.0 != crate::ns::DEFAULT_NS || qname.1 != "iq" {
|
|
||||||
return Err(::xso::error::FromEventsError::Mismatch { name: qname, attrs });
|
|
||||||
}
|
|
||||||
Self::Builder::new(qname, attrs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ::xso::AsXml for Iq {
|
|
||||||
type ItemIter<'x> = ::xso::minidom_compat::AsItemsViaElement<'x>;
|
|
||||||
|
|
||||||
fn as_xml_iter(&self) -> Result<Self::ItemIter<'_>, ::xso::error::Error> {
|
|
||||||
::xso::minidom_compat::AsItemsViaElement::new(self.clone())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -251,19 +376,22 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::disco::DiscoInfoQuery;
|
use crate::disco::DiscoInfoQuery;
|
||||||
use crate::stanza_error::{DefinedCondition, ErrorType};
|
use crate::stanza_error::{DefinedCondition, ErrorType};
|
||||||
|
use xso::error::{Error, FromElementError};
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size() {
|
fn test_size() {
|
||||||
assert_size!(IqType, 108);
|
assert_size!(IqHeader, 44);
|
||||||
assert_size!(Iq, 152);
|
assert_size!(IqPayload, 108);
|
||||||
|
assert_size!(Iq, 212);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "64")]
|
#[cfg(target_pointer_width = "64")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size() {
|
fn test_size() {
|
||||||
assert_size!(IqType, 216);
|
assert_size!(IqHeader, 88);
|
||||||
assert_size!(Iq, 304);
|
assert_size!(IqPayload, 216);
|
||||||
|
assert_size!(Iq, 424);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -277,12 +405,18 @@ mod tests {
|
||||||
FromElementError::Invalid(Error::Other(string)) => string,
|
FromElementError::Invalid(Error::Other(string)) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert_eq!(message, "Required attribute 'id' missing.");
|
assert_eq!(message, "Missing discriminator attribute.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_require_id() {
|
||||||
|
for type_ in ["get", "set", "result", "error"] {
|
||||||
#[cfg(not(feature = "component"))]
|
#[cfg(not(feature = "component"))]
|
||||||
let elem: Element = "<iq xmlns='jabber:client' id='coucou'/>".parse().unwrap();
|
let elem: Element = format!("<iq xmlns='jabber:client' type='{}'/>", type_)
|
||||||
|
.parse()
|
||||||
|
.unwrap();
|
||||||
#[cfg(feature = "component")]
|
#[cfg(feature = "component")]
|
||||||
let elem: Element = "<iq xmlns='jabber:component:accept' id='coucou'/>"
|
let elem: Element = format!("<iq xmlns='jabber:component:accept' type='{}'/>", type_)
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let error = Iq::try_from(elem).unwrap_err();
|
let error = Iq::try_from(elem).unwrap_err();
|
||||||
|
|
@ -290,7 +424,10 @@ mod tests {
|
||||||
FromElementError::Invalid(Error::Other(string)) => string,
|
FromElementError::Invalid(Error::Other(string)) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert_eq!(message, "Required attribute 'type' missing.");
|
// Slicing here, because the rest of the error message is specific
|
||||||
|
// about the enum variant.
|
||||||
|
assert_eq!(&message[..33], "Required attribute field 'id' on ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -309,11 +446,11 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let iq = Iq::try_from(elem).unwrap();
|
let iq = Iq::try_from(elem).unwrap();
|
||||||
let query: Element = "<foo xmlns='bar'/>".parse().unwrap();
|
let query: Element = "<foo xmlns='bar'/>".parse().unwrap();
|
||||||
assert_eq!(iq.from, None);
|
assert_eq!(iq.from(), None);
|
||||||
assert_eq!(iq.to, None);
|
assert_eq!(iq.to(), None);
|
||||||
assert_eq!(&iq.id, "foo");
|
assert_eq!(iq.id(), "foo");
|
||||||
assert!(match iq.payload {
|
assert!(match iq {
|
||||||
IqType::Get(element) => element == query,
|
Iq::Get { payload, .. } => payload == query,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -334,11 +471,11 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let iq = Iq::try_from(elem).unwrap();
|
let iq = Iq::try_from(elem).unwrap();
|
||||||
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
|
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
|
||||||
assert_eq!(iq.from, None);
|
assert_eq!(iq.from(), None);
|
||||||
assert_eq!(iq.to, None);
|
assert_eq!(iq.to(), None);
|
||||||
assert_eq!(&iq.id, "vcard");
|
assert_eq!(iq.id(), "vcard");
|
||||||
assert!(match iq.payload {
|
assert!(match iq {
|
||||||
IqType::Set(element) => element == vcard,
|
Iq::Set { payload, .. } => payload == vcard,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -354,11 +491,11 @@ mod tests {
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let iq = Iq::try_from(elem).unwrap();
|
let iq = Iq::try_from(elem).unwrap();
|
||||||
assert_eq!(iq.from, None);
|
assert_eq!(iq.from(), None);
|
||||||
assert_eq!(iq.to, None);
|
assert_eq!(iq.to(), None);
|
||||||
assert_eq!(&iq.id, "res");
|
assert_eq!(iq.id(), "res");
|
||||||
assert!(match iq.payload {
|
assert!(match iq {
|
||||||
IqType::Result(None) => true,
|
Iq::Result { payload: None, .. } => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -381,11 +518,14 @@ mod tests {
|
||||||
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(iq.from, None);
|
assert_eq!(iq.from(), None);
|
||||||
assert_eq!(iq.to, None);
|
assert_eq!(iq.to(), None);
|
||||||
assert_eq!(&iq.id, "res");
|
assert_eq!(iq.id(), "res");
|
||||||
assert!(match iq.payload {
|
assert!(match iq {
|
||||||
IqType::Result(Some(element)) => element == query,
|
Iq::Result {
|
||||||
|
payload: Some(element),
|
||||||
|
..
|
||||||
|
} => element == query,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -411,11 +551,11 @@ mod tests {
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let iq = Iq::try_from(elem).unwrap();
|
let iq = Iq::try_from(elem).unwrap();
|
||||||
assert_eq!(iq.from, None);
|
assert_eq!(iq.from(), None);
|
||||||
assert_eq!(iq.to, None);
|
assert_eq!(iq.to(), None);
|
||||||
assert_eq!(iq.id, "err1");
|
assert_eq!(iq.id(), "err1");
|
||||||
match iq.payload {
|
match iq {
|
||||||
IqType::Error(error) => {
|
Iq::Error { error, .. } => {
|
||||||
assert_eq!(error.type_, ErrorType::Cancel);
|
assert_eq!(error.type_, ErrorType::Cancel);
|
||||||
assert_eq!(error.by, None);
|
assert_eq!(error.by, None);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
@ -444,7 +584,7 @@ mod tests {
|
||||||
FromElementError::Invalid(Error::Other(string)) => string,
|
FromElementError::Invalid(Error::Other(string)) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert_eq!(message, "Wrong number of children in iq element.");
|
assert_eq!(message, "Missing child field 'error' in Iq::Error element.");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -457,11 +597,11 @@ mod tests {
|
||||||
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'/>"
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'/>"
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let iq2 = Iq {
|
let iq2 = Iq::Result {
|
||||||
from: None,
|
from: None,
|
||||||
to: None,
|
to: None,
|
||||||
id: String::from("res"),
|
id: String::from("res"),
|
||||||
payload: IqType::Result(None),
|
payload: None,
|
||||||
};
|
};
|
||||||
let elem2 = iq2.into();
|
let elem2 = iq2.into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
|
|
@ -474,8 +614,8 @@ mod tests {
|
||||||
#[cfg(feature = "component")]
|
#[cfg(feature = "component")]
|
||||||
let elem: Element = "<iq xmlns='jabber:component:accept' type='get' id='disco'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='get' id='disco'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
||||||
let iq = Iq::try_from(elem).unwrap();
|
let iq = Iq::try_from(elem).unwrap();
|
||||||
let disco_info = match iq.payload {
|
let disco_info = match iq {
|
||||||
IqType::Get(payload) => DiscoInfoQuery::try_from(payload).unwrap(),
|
Iq::Get { payload, .. } => DiscoInfoQuery::try_from(payload).unwrap(),
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
assert!(disco_info.node.is_none());
|
assert!(disco_info.node.is_none());
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use xmpp_parsers::{
|
||||||
caps::{compute_disco, hash_caps, Caps},
|
caps::{compute_disco, hash_caps, Caps},
|
||||||
disco::{DiscoInfoQuery, DiscoInfoResult, Feature, Identity},
|
disco::{DiscoInfoQuery, DiscoInfoResult, Feature, Identity},
|
||||||
hashes::Algo,
|
hashes::Algo,
|
||||||
iq::{Iq, IqType},
|
iq::Iq,
|
||||||
jid::{BareJid, Jid},
|
jid::{BareJid, Jid},
|
||||||
ns,
|
ns,
|
||||||
presence::{Presence, Type as PresenceType},
|
presence::{Presence, Type as PresenceType},
|
||||||
|
|
@ -54,24 +54,24 @@ async fn main() {
|
||||||
client.send_stanza(presence.into()).await.unwrap();
|
client.send_stanza(presence.into()).await.unwrap();
|
||||||
} else if let Some(stanza) = event.into_stanza() {
|
} else if let Some(stanza) = event.into_stanza() {
|
||||||
match stanza {
|
match stanza {
|
||||||
Stanza::Iq(iq) => {
|
Stanza::Iq(Iq::Get {
|
||||||
if let IqType::Get(payload) = iq.payload {
|
payload, id, from, ..
|
||||||
|
}) => {
|
||||||
if payload.is("query", ns::DISCO_INFO) {
|
if payload.is("query", ns::DISCO_INFO) {
|
||||||
let query = DiscoInfoQuery::try_from(payload);
|
let query = DiscoInfoQuery::try_from(payload);
|
||||||
match query {
|
match query {
|
||||||
Ok(query) => {
|
Ok(query) => {
|
||||||
let mut disco = disco_info.clone();
|
let mut disco = disco_info.clone();
|
||||||
disco.node = query.node;
|
disco.node = query.node;
|
||||||
let iq = Iq::from_result(iq.id, Some(disco))
|
let iq = Iq::from_result(id, Some(disco)).with_to(from.unwrap());
|
||||||
.with_to(iq.from.unwrap());
|
|
||||||
client.send_stanza(iq.into()).await.unwrap();
|
client.send_stanza(iq.into()).await.unwrap();
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
client
|
client
|
||||||
.send_stanza(
|
.send_stanza(
|
||||||
make_error(
|
make_error(
|
||||||
iq.from.unwrap(),
|
from.unwrap(),
|
||||||
iq.id,
|
id,
|
||||||
ErrorType::Modify,
|
ErrorType::Modify,
|
||||||
DefinedCondition::BadRequest,
|
DefinedCondition::BadRequest,
|
||||||
&format!("{}", err),
|
&format!("{}", err),
|
||||||
|
|
@ -87,8 +87,8 @@ async fn main() {
|
||||||
client
|
client
|
||||||
.send_stanza(
|
.send_stanza(
|
||||||
make_error(
|
make_error(
|
||||||
iq.from.unwrap(),
|
from.unwrap(),
|
||||||
iq.id,
|
id,
|
||||||
ErrorType::Cancel,
|
ErrorType::Cancel,
|
||||||
DefinedCondition::ServiceUnavailable,
|
DefinedCondition::ServiceUnavailable,
|
||||||
"No handler defined for this kind of iq.",
|
"No handler defined for this kind of iq.",
|
||||||
|
|
@ -98,19 +98,25 @@ async fn main() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
} else if let IqType::Result(Some(payload)) = iq.payload {
|
}
|
||||||
|
Stanza::Iq(Iq::Result {
|
||||||
|
payload: Some(payload),
|
||||||
|
from,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
if payload.is("pubsub", ns::PUBSUB) {
|
if payload.is("pubsub", ns::PUBSUB) {
|
||||||
let pubsub = PubSub::try_from(payload).unwrap();
|
let pubsub = PubSub::try_from(payload).unwrap();
|
||||||
let from = iq.from.clone().unwrap_or(jid.clone().into());
|
let from = from.unwrap_or(jid.clone().into());
|
||||||
handle_iq_result(pubsub, &from);
|
handle_iq_result(pubsub, &from);
|
||||||
}
|
}
|
||||||
} else if let IqType::Set(_) = iq.payload {
|
}
|
||||||
|
Stanza::Iq(Iq::Set { from, id, .. }) => {
|
||||||
// We MUST answer unhandled set iqs with a service-unavailable error.
|
// We MUST answer unhandled set iqs with a service-unavailable error.
|
||||||
client
|
client
|
||||||
.send_stanza(
|
.send_stanza(
|
||||||
make_error(
|
make_error(
|
||||||
iq.from.unwrap(),
|
from.unwrap(),
|
||||||
iq.id,
|
id,
|
||||||
ErrorType::Cancel,
|
ErrorType::Cancel,
|
||||||
DefinedCondition::ServiceUnavailable,
|
DefinedCondition::ServiceUnavailable,
|
||||||
"No handler defined for this kind of iq.",
|
"No handler defined for this kind of iq.",
|
||||||
|
|
@ -120,7 +126,7 @@ async fn main() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
Stanza::Iq(Iq::Error { .. }) | Stanza::Iq(Iq::Result { payload: None, .. }) => (),
|
||||||
Stanza::Message(message) => {
|
Stanza::Message(message) => {
|
||||||
let from = message.from.clone().unwrap();
|
let from = message.from.clone().unwrap();
|
||||||
if let Some(body) = message.get_best_body(vec!["en"]) {
|
if let Some(body) = message.get_best_body(vec!["en"]) {
|
||||||
|
|
|
||||||
|
|
@ -88,8 +88,7 @@ async fn main() {
|
||||||
_ = ping_timer.tick() => {
|
_ = ping_timer.tick() => {
|
||||||
log::info!("sending ping for fun & profit");
|
log::info!("sending ping for fun & profit");
|
||||||
ping_ctr = ping_ctr.wrapping_add(1);
|
ping_ctr = ping_ctr.wrapping_add(1);
|
||||||
let mut iq = Iq::from_get(format!("ping-{}", ping_ctr), ping::Ping);
|
let iq = Iq::from_get(format!("ping-{}", ping_ctr), ping::Ping).with_to(domain.clone());
|
||||||
iq.to = Some(domain.clone());
|
|
||||||
stream.send(Box::new(iq.into())).await;
|
stream.send(Box::new(iq.into())).await;
|
||||||
}
|
}
|
||||||
ev = stream.next() => match ev {
|
ev = stream.next() => match ev {
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,7 @@ use std::sync::Mutex;
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
|
|
||||||
use xmpp_parsers::{
|
use xmpp_parsers::{iq::Iq, stanza_error::StanzaError};
|
||||||
iq::{Iq, IqType},
|
|
||||||
stanza_error::StanzaError,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
event::make_id,
|
event::make_id,
|
||||||
|
|
@ -40,11 +37,21 @@ pub enum IqRequest {
|
||||||
Set(Element),
|
Set(Element),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<IqRequest> for IqType {
|
impl IqRequest {
|
||||||
fn from(other: IqRequest) -> IqType {
|
fn into_iq(self, from: Option<Jid>, to: Option<Jid>, id: String) -> Iq {
|
||||||
match other {
|
match self {
|
||||||
IqRequest::Get(v) => Self::Get(v),
|
Self::Get(payload) => Iq::Get {
|
||||||
IqRequest::Set(v) => Self::Set(v),
|
from,
|
||||||
|
to,
|
||||||
|
id,
|
||||||
|
payload,
|
||||||
|
},
|
||||||
|
Self::Set(payload) => Iq::Set {
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
id,
|
||||||
|
payload,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -59,11 +66,22 @@ pub enum IqResponse {
|
||||||
Error(StanzaError),
|
Error(StanzaError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<IqResponse> for IqType {
|
impl IqResponse {
|
||||||
fn from(other: IqResponse) -> IqType {
|
fn into_iq(self, from: Option<Jid>, to: Option<Jid>, id: String) -> Iq {
|
||||||
match other {
|
match self {
|
||||||
IqResponse::Result(v) => Self::Result(v),
|
Self::Error(error) => Iq::Error {
|
||||||
IqResponse::Error(v) => Self::Error(v),
|
from,
|
||||||
|
to,
|
||||||
|
id,
|
||||||
|
error,
|
||||||
|
payload: None,
|
||||||
|
},
|
||||||
|
Self::Result(payload) => Iq::Result {
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
id,
|
||||||
|
payload,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -251,22 +269,28 @@ impl IqResponseTracker {
|
||||||
/// Returns the IQ stanza unharmed if it is not an IQ response matching
|
/// Returns the IQ stanza unharmed if it is not an IQ response matching
|
||||||
/// any request which is still being tracked.
|
/// any request which is still being tracked.
|
||||||
pub fn handle_iq(&self, iq: Iq) -> ControlFlow<(), Iq> {
|
pub fn handle_iq(&self, iq: Iq) -> ControlFlow<(), Iq> {
|
||||||
let payload = match iq.payload {
|
let (from, to, id, payload) = match iq {
|
||||||
IqType::Error(error) => IqResponse::Error(error),
|
Iq::Error {
|
||||||
IqType::Result(result) => IqResponse::Result(result),
|
from,
|
||||||
|
to,
|
||||||
|
id,
|
||||||
|
error,
|
||||||
|
payload: _,
|
||||||
|
} => (from, to, id, IqResponse::Error(error)),
|
||||||
|
Iq::Result {
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
id,
|
||||||
|
payload,
|
||||||
|
} => (from, to, id, IqResponse::Result(payload)),
|
||||||
_ => return ControlFlow::Continue(iq),
|
_ => return ControlFlow::Continue(iq),
|
||||||
};
|
};
|
||||||
let key = (iq.from, iq.id);
|
let key = (from, id);
|
||||||
let mut map = self.map.lock().unwrap();
|
let mut map = self.map.lock().unwrap();
|
||||||
match map.remove(&key) {
|
match map.remove(&key) {
|
||||||
None => {
|
None => {
|
||||||
log::trace!("not handling IQ response from {:?} with id {:?}: no active tracker for this tuple", key.0, key.1);
|
log::trace!("not handling IQ response from {:?} with id {:?}: no active tracker for this tuple", key.0, key.1);
|
||||||
ControlFlow::Continue(Iq {
|
ControlFlow::Continue(payload.into_iq(key.0, to, key.1))
|
||||||
from: key.0,
|
|
||||||
id: key.1,
|
|
||||||
to: iq.to,
|
|
||||||
payload: payload.into(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Some(sink) => {
|
Some(sink) => {
|
||||||
sink.complete(payload);
|
sink.complete(payload);
|
||||||
|
|
@ -298,14 +322,6 @@ impl IqResponseTracker {
|
||||||
inner: rx,
|
inner: rx,
|
||||||
};
|
};
|
||||||
map.insert(key.clone(), sink);
|
map.insert(key.clone(), sink);
|
||||||
(
|
(req.into_iq(from, key.0, key.1), token)
|
||||||
Iq {
|
|
||||||
from,
|
|
||||||
to: key.0,
|
|
||||||
id: key.1,
|
|
||||||
payload: req.into(),
|
|
||||||
},
|
|
||||||
token,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,10 +43,11 @@ impl Stanza {
|
||||||
pub fn ensure_id(&mut self) -> &str {
|
pub fn ensure_id(&mut self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
Self::Iq(iq) => {
|
Self::Iq(iq) => {
|
||||||
if iq.id.is_empty() {
|
let id = iq.id_mut();
|
||||||
iq.id = make_id();
|
if id.is_empty() {
|
||||||
|
*id = make_id();
|
||||||
}
|
}
|
||||||
&iq.id
|
id
|
||||||
}
|
}
|
||||||
Self::Message(message) => message.id.get_or_insert_with(|| Id(make_id())).0.as_ref(),
|
Self::Message(message) => message.id.get_or_insert_with(|| Id(make_id())).0.as_ref(),
|
||||||
Self::Presence(presence) => presence.id.get_or_insert_with(make_id),
|
Self::Presence(presence) => presence.id.get_or_insert_with(make_id),
|
||||||
|
|
@ -97,7 +98,7 @@ impl TryFrom<Stanza> for Presence {
|
||||||
impl TryFrom<Stanza> for Iq {
|
impl TryFrom<Stanza> for Iq {
|
||||||
type Error = Stanza;
|
type Error = Stanza;
|
||||||
|
|
||||||
fn try_from(other: Stanza) -> Result<Self, Self::Error> {
|
fn try_from(other: Stanza) -> Result<Self, Stanza> {
|
||||||
match other {
|
match other {
|
||||||
Stanza::Iq(st) => Ok(st),
|
Stanza::Iq(st) => Ok(st),
|
||||||
other => Err(other),
|
other => Err(other),
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use futures::{ready, Sink, Stream};
|
||||||
|
|
||||||
use xmpp_parsers::{
|
use xmpp_parsers::{
|
||||||
bind::{BindQuery, BindResponse},
|
bind::{BindQuery, BindResponse},
|
||||||
iq::{Iq, IqType},
|
iq::Iq,
|
||||||
jid::{FullJid, Jid},
|
jid::{FullJid, Jid},
|
||||||
sm,
|
sm,
|
||||||
stream_error::{DefinedCondition, StreamError},
|
stream_error::{DefinedCondition, StreamError},
|
||||||
|
|
@ -200,10 +200,12 @@ impl NegotiationState {
|
||||||
|
|
||||||
match item {
|
match item {
|
||||||
Ok(XmppStreamElement::Stanza(data)) => match data {
|
Ok(XmppStreamElement::Stanza(data)) => match data {
|
||||||
Stanza::Iq(iq) if iq.id == BIND_REQ_ID => {
|
Stanza::Iq(iq) if iq.id() == BIND_REQ_ID => {
|
||||||
let error = match iq.payload {
|
let error = match iq {
|
||||||
IqType::Result(Some(payload)) => {
|
Iq::Result {
|
||||||
match BindResponse::try_from(payload) {
|
payload: Some(payload),
|
||||||
|
..
|
||||||
|
} => match BindResponse::try_from(payload) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
let bound_jid = v.into();
|
let bound_jid = v.into();
|
||||||
if *sm_supported {
|
if *sm_supported {
|
||||||
|
|
@ -222,9 +224,10 @@ impl NegotiationState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => e.to_string(),
|
Err(e) => e.to_string(),
|
||||||
|
},
|
||||||
|
Iq::Result { payload: None, .. } => {
|
||||||
|
"Bind response has no payload".to_owned()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
IqType::Result(None) => "Bind response has no payload".to_owned(),
|
|
||||||
_ => "Unexpected IQ type in response to bind request".to_owned(),
|
_ => "Unexpected IQ type in response to bind request".to_owned(),
|
||||||
};
|
};
|
||||||
log::warn!("Received IQ matching the bind request, but parsing failed ({error})! Emitting stream error.");
|
log::warn!("Received IQ matching the bind request, but parsing failed ({error})! Emitting stream error.");
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
use tokio_xmpp::parsers::iq::{Iq, IqType};
|
use tokio_xmpp::parsers::iq::{Iq, IqHeader, IqPayload};
|
||||||
|
|
||||||
use crate::{Agent, Event};
|
use crate::{Agent, Event};
|
||||||
|
|
||||||
|
|
@ -14,16 +14,14 @@ pub mod set;
|
||||||
|
|
||||||
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
|
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
|
||||||
let mut events = vec![];
|
let mut events = vec![];
|
||||||
let from = iq
|
let (IqHeader { from, to, id }, data) = iq.split();
|
||||||
.from
|
let from = from.unwrap_or_else(|| agent.client.bound_jid().unwrap().to_bare().into());
|
||||||
.clone()
|
if let IqPayload::Get(payload) = data {
|
||||||
.unwrap_or_else(|| agent.client.bound_jid().unwrap().to_bare().into());
|
get::handle_iq_get(agent, &mut events, from, to, id, payload).await;
|
||||||
if let IqType::Get(payload) = iq.payload {
|
} else if let IqPayload::Result(Some(payload)) = data {
|
||||||
get::handle_iq_get(agent, &mut events, from, iq.to, iq.id, payload).await;
|
result::handle_iq_result(agent, &mut events, from, to, id, payload).await;
|
||||||
} else if let IqType::Result(Some(payload)) = iq.payload {
|
} else if let IqPayload::Set(payload) = data {
|
||||||
result::handle_iq_result(agent, &mut events, from, iq.to, iq.id, payload).await;
|
set::handle_iq_set(agent, &mut events, from, to, id, payload).await;
|
||||||
} else if let IqType::Set(payload) = iq.payload {
|
|
||||||
set::handle_iq_set(agent, &mut events, from, iq.to, iq.id, payload).await;
|
|
||||||
}
|
}
|
||||||
events
|
events
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue