tokio-xmpp: @id wasn't correctly added to every stanza
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 <auth/> or <bind/>, which probably weren't required anyway? Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
6fa6deddcb
commit
bc73af1e5e
4 changed files with 20 additions and 11 deletions
|
|
@ -17,7 +17,7 @@ use crate::event::Event;
|
||||||
use crate::happy_eyeballs::{connect_to_host, connect_with_srv};
|
use crate::happy_eyeballs::{connect_to_host, connect_with_srv};
|
||||||
use crate::starttls::starttls;
|
use crate::starttls::starttls;
|
||||||
use crate::xmpp_codec::Packet;
|
use crate::xmpp_codec::Packet;
|
||||||
use crate::xmpp_stream;
|
use crate::xmpp_stream::{self, make_id};
|
||||||
use crate::{Error, ProtocolError};
|
use crate::{Error, ProtocolError};
|
||||||
|
|
||||||
/// XMPP client connection and state
|
/// XMPP client connection and state
|
||||||
|
|
@ -161,7 +161,11 @@ impl Client {
|
||||||
|
|
||||||
/// Send stanza
|
/// Send stanza
|
||||||
pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> {
|
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 `</stream:stream>`
|
/// End connection by sending `</stream:stream>`
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ use super::bind::bind;
|
||||||
use crate::happy_eyeballs::connect_with_srv;
|
use crate::happy_eyeballs::connect_with_srv;
|
||||||
use crate::starttls::starttls;
|
use crate::starttls::starttls;
|
||||||
use crate::xmpp_codec::Packet;
|
use crate::xmpp_codec::Packet;
|
||||||
use crate::xmpp_stream;
|
use crate::xmpp_stream::{self, make_id};
|
||||||
use crate::{Error, ProtocolError};
|
use crate::{Error, ProtocolError};
|
||||||
|
|
||||||
/// A simple XMPP client connection
|
/// A simple XMPP client connection
|
||||||
|
|
@ -98,7 +98,11 @@ impl Client {
|
||||||
where
|
where
|
||||||
E: Into<Element>,
|
E: Into<Element>,
|
||||||
{
|
{
|
||||||
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 `</stream:stream>`
|
/// End connection by sending `</stream:stream>`
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use super::happy_eyeballs::connect_to_host;
|
||||||
use super::xmpp_codec::Packet;
|
use super::xmpp_codec::Packet;
|
||||||
use super::xmpp_stream;
|
use super::xmpp_stream;
|
||||||
use super::Error;
|
use super::Error;
|
||||||
|
use crate::xmpp_stream::make_id;
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
|
|
||||||
|
|
@ -53,7 +54,11 @@ impl Component {
|
||||||
|
|
||||||
/// Send stanza
|
/// Send stanza
|
||||||
pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> {
|
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
|
/// End connection
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ use crate::stream_start;
|
||||||
use crate::xmpp_codec::{Packet, XMPPCodec};
|
use crate::xmpp_codec::{Packet, XMPPCodec};
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
|
||||||
fn make_id() -> String {
|
pub(crate) fn make_id() -> String {
|
||||||
let id: u64 = thread_rng().gen();
|
let id: u64 = thread_rng().gen();
|
||||||
format!("{}", id)
|
format!("{}", id)
|
||||||
}
|
}
|
||||||
|
|
@ -78,11 +78,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
|
||||||
impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
|
impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
|
||||||
/// Convenience method
|
/// Convenience method
|
||||||
pub fn send_stanza<E: Into<Element>>(&mut self, e: E) -> Send<Self, Packet> {
|
pub fn send_stanza<E: Into<Element>>(&mut self, e: E) -> Send<Self, Packet> {
|
||||||
let mut el: Element = e.into();
|
self.send(Packet::Stanza(e.into()))
|
||||||
if el.attr("id").is_none() {
|
|
||||||
el.set_attr("id", make_id());
|
|
||||||
}
|
|
||||||
self.send(Packet::Stanza(el))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue