From bc73af1e5ed1f6a285a7c61096a80170c1034214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 4 Jun 2023 19:01:45 +0200 Subject: [PATCH] tokio-xmpp: @id wasn't correctly added to every stanza MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit moves the code adding @id to AsyncClient and SimpleClient, instead of on the lower level send_stanza helper, which seemed to only be used internally. Support is also added for Component. This removes the addition of @id on elements like or , which probably weren't required anyway? Signed-off-by: Maxime “pep” Buquet --- tokio-xmpp/src/client/async_client.rs | 8 ++++++-- tokio-xmpp/src/client/simple_client.rs | 8 ++++++-- tokio-xmpp/src/component/mod.rs | 7 ++++++- tokio-xmpp/src/xmpp_stream.rs | 8 ++------ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tokio-xmpp/src/client/async_client.rs b/tokio-xmpp/src/client/async_client.rs index 44d9bdf9d4330594ee637c0d7fecab4c20a2806e..91233b7e6479c3284d59d4a6c85b4e0f802fdc10 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; +use crate::xmpp_stream::{self, make_id}; use crate::{Error, ProtocolError}; /// XMPP client connection and state @@ -161,7 +161,11 @@ impl Client { /// Send stanza pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> { - self.send(Packet::Stanza(stanza)).await + let mut el: Element = stanza; + if el.attr("id").is_none() { + el.set_attr("id", make_id()); + } + self.send(Packet::Stanza(el)).await } /// End connection by sending `` diff --git a/tokio-xmpp/src/client/simple_client.rs b/tokio-xmpp/src/client/simple_client.rs index df87ef0e5965b077a09f6e86b3e914ed1018ff0d..a55463ff9b6ef101a0b93b5a1cfa9b67baf90eef 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; +use crate::xmpp_stream::{self, make_id}; use crate::{Error, ProtocolError}; /// A simple XMPP client connection @@ -98,7 +98,11 @@ impl Client { where E: Into, { - self.send(Packet::Stanza(stanza.into())).await + 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 } /// End connection by sending `` diff --git a/tokio-xmpp/src/component/mod.rs b/tokio-xmpp/src/component/mod.rs index 6ac1371fd70748b91d749791cf556395aca34e70..8b0c3b5502bd9cc5e355b6e410e315f06069c91f 100644 --- a/tokio-xmpp/src/component/mod.rs +++ b/tokio-xmpp/src/component/mod.rs @@ -12,6 +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; mod auth; @@ -53,7 +54,11 @@ impl Component { /// Send stanza pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> { - self.send(stanza).await + let mut el: Element = stanza; + if el.attr("id").is_none() { + el.set_attr("id", make_id()); + } + self.send(el).await } /// End connection diff --git a/tokio-xmpp/src/xmpp_stream.rs b/tokio-xmpp/src/xmpp_stream.rs index 3c5f0477de07ef79b9e73ef636c569ac3f6b3096..83ea8a64f2d6e8b8748aa0ae0eebcd89c8427792 100644 --- a/tokio-xmpp/src/xmpp_stream.rs +++ b/tokio-xmpp/src/xmpp_stream.rs @@ -14,7 +14,7 @@ use crate::stream_start; use crate::xmpp_codec::{Packet, XMPPCodec}; use crate::Error; -fn make_id() -> String { +pub(crate) fn make_id() -> String { let id: u64 = thread_rng().gen(); format!("{}", id) } @@ -78,11 +78,7 @@ impl XMPPStream { impl XMPPStream { /// Convenience method pub fn send_stanza>(&mut self, e: E) -> Send { - let mut el: Element = e.into(); - if el.attr("id").is_none() { - el.set_attr("id", make_id()); - } - self.send(Packet::Stanza(el)) + self.send(Packet::Stanza(e.into())) } }