tokio-xmpp: Ensure id is added only to stanza

The previous commit didn't fix a bug where @id would be added to
elements that didn't need it / where it was invalid (e.g., stream
management).

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-06-04 19:27:46 +02:00
commit ae67949e7a
No known key found for this signature in database
GPG key ID: DEDA74AEECA9D0F2
4 changed files with 25 additions and 19 deletions

View file

@ -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.
///