diff --git a/tokio-xmpp/src/client/async_client.rs b/tokio-xmpp/src/client/async_client.rs index 91233b7e..8afd73e1 100644 --- a/tokio-xmpp/src/client/async_client.rs +++ b/tokio-xmpp/src/client/async_client.rs @@ -17,7 +17,7 @@ use crate::event::Event; use crate::happy_eyeballs::{connect_to_host, connect_with_srv}; use crate::starttls::starttls; use crate::xmpp_codec::Packet; -use crate::xmpp_stream::{self, make_id}; +use crate::xmpp_stream::{self, add_stanza_id}; use crate::{Error, ProtocolError}; /// XMPP client connection and state @@ -161,11 +161,8 @@ impl Client { /// Send stanza pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> { - let mut el: Element = stanza; - if el.attr("id").is_none() { - el.set_attr("id", make_id()); - } - self.send(Packet::Stanza(el)).await + self.send(Packet::Stanza(add_stanza_id(stanza, ns::JABBER_CLIENT))) + .await } /// End connection by sending `` diff --git a/tokio-xmpp/src/client/simple_client.rs b/tokio-xmpp/src/client/simple_client.rs index a55463ff..e9116aa7 100644 --- a/tokio-xmpp/src/client/simple_client.rs +++ b/tokio-xmpp/src/client/simple_client.rs @@ -17,7 +17,7 @@ use super::bind::bind; use crate::happy_eyeballs::connect_with_srv; use crate::starttls::starttls; use crate::xmpp_codec::Packet; -use crate::xmpp_stream::{self, make_id}; +use crate::xmpp_stream::{self, add_stanza_id}; use crate::{Error, ProtocolError}; /// A simple XMPP client connection @@ -98,11 +98,11 @@ impl Client { where E: Into, { - let mut el: Element = stanza.into(); - if el.attr("id").is_none() { - el.set_attr("id", make_id()); - } - self.send(Packet::Stanza(el.into())).await + self.send(Packet::Stanza(add_stanza_id( + stanza.into(), + ns::JABBER_CLIENT, + ))) + .await } /// End connection by sending `` diff --git a/tokio-xmpp/src/component/mod.rs b/tokio-xmpp/src/component/mod.rs index 8b0c3b55..dba79fa4 100644 --- a/tokio-xmpp/src/component/mod.rs +++ b/tokio-xmpp/src/component/mod.rs @@ -12,7 +12,7 @@ use super::happy_eyeballs::connect_to_host; use super::xmpp_codec::Packet; use super::xmpp_stream; use super::Error; -use crate::xmpp_stream::make_id; +use crate::xmpp_stream::add_stanza_id; mod auth; @@ -54,11 +54,7 @@ impl Component { /// Send stanza pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> { - let mut el: Element = stanza; - if el.attr("id").is_none() { - el.set_attr("id", make_id()); - } - self.send(el).await + self.send(add_stanza_id(stanza, ns::COMPONENT_ACCEPT)).await } /// End connection diff --git a/tokio-xmpp/src/xmpp_stream.rs b/tokio-xmpp/src/xmpp_stream.rs index 83ea8a64..1b13a0c4 100644 --- a/tokio-xmpp/src/xmpp_stream.rs +++ b/tokio-xmpp/src/xmpp_stream.rs @@ -14,11 +14,24 @@ use crate::stream_start; use crate::xmpp_codec::{Packet, XMPPCodec}; use crate::Error; -pub(crate) fn make_id() -> String { +fn make_id() -> String { let id: u64 = thread_rng().gen(); format!("{}", id) } +pub(crate) fn add_stanza_id(mut stanza: Element, default_ns: &str) -> Element { + if stanza.is("iq", default_ns) + || stanza.is("message", default_ns) + || stanza.is("presence", default_ns) + { + if stanza.attr("id").is_none() { + stanza.set_attr("id", make_id()); + } + } + + stanza +} + /// Wraps a binary stream (tokio's `AsyncRead + AsyncWrite`) to decode /// and encode XMPP packets. ///