diff --git a/tokio-xmpp/Cargo.toml b/tokio-xmpp/Cargo.toml index 795bac8f..fe0ce1c1 100644 --- a/tokio-xmpp/Cargo.toml +++ b/tokio-xmpp/Cargo.toml @@ -29,6 +29,7 @@ xmpp-parsers = "0.19" minidom = "0.15" rxml = "^0.8.0" webpki-roots = { version = "0.22", optional = true } +rand = "^0.8" [build-dependencies] rustc_version = "0.4" diff --git a/tokio-xmpp/src/xmpp_stream.rs b/tokio-xmpp/src/xmpp_stream.rs index 86ca75b9..3c5f0477 100644 --- a/tokio-xmpp/src/xmpp_stream.rs +++ b/tokio-xmpp/src/xmpp_stream.rs @@ -2,6 +2,7 @@ use futures::sink::Send; use futures::{sink::SinkExt, task::Poll, Sink, Stream}; +use rand::{thread_rng, Rng}; use std::pin::Pin; use std::task::Context; use tokio::io::{AsyncRead, AsyncWrite}; @@ -13,6 +14,11 @@ use crate::stream_start; use crate::xmpp_codec::{Packet, XMPPCodec}; use crate::Error; +fn make_id() -> String { + let id: u64 = thread_rng().gen(); + format!("{}", id) +} + /// Wraps a binary stream (tokio's `AsyncRead + AsyncWrite`) to decode /// and encode XMPP packets. /// @@ -72,7 +78,11 @@ impl XMPPStream { impl XMPPStream { /// Convenience method pub fn send_stanza>(&mut self, e: E) -> Send { - self.send(Packet::Stanza(e.into())) + let mut el: Element = e.into(); + if el.attr("id").is_none() { + el.set_attr("id", make_id()); + } + self.send(Packet::Stanza(el)) } }