From e501addb96fc03b443a39d603c9040959eb0268f Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 30 May 2020 01:19:06 +0200 Subject: [PATCH] tokio-xmpp: use xmpp_parsers::ns everywhere --- tokio-xmpp/src/client/async_client.rs | 9 ++++----- tokio-xmpp/src/client/mod.rs | 3 --- tokio-xmpp/src/client/simple_client.rs | 9 ++++----- tokio-xmpp/src/component/auth.rs | 6 ++---- tokio-xmpp/src/component/mod.rs | 5 ++--- tokio-xmpp/src/starttls.rs | 7 ++----- tokio-xmpp/src/stream_features.rs | 12 +++++------- tokio-xmpp/src/stream_start.rs | 10 ++++------ tokio-xmpp/src/xmpp_stream.rs | 3 --- 9 files changed, 23 insertions(+), 41 deletions(-) diff --git a/tokio-xmpp/src/client/async_client.rs b/tokio-xmpp/src/client/async_client.rs index d9d55ee5..26b114da 100644 --- a/tokio-xmpp/src/client/async_client.rs +++ b/tokio-xmpp/src/client/async_client.rs @@ -9,7 +9,7 @@ use tokio::net::TcpStream; use tokio::task::JoinHandle; use tokio::task::LocalSet; use tokio_tls::TlsStream; -use xmpp_parsers::{Element, Jid, JidParseError}; +use xmpp_parsers::{ns, Element, Jid, JidParseError}; use super::auth::auth; use super::bind::bind; @@ -35,7 +35,6 @@ pub struct Client { } type XMPPStream = xmpp_stream::XMPPStream>; -const NS_JABBER_CLIENT: &str = "jabber:client"; enum ClientState { Invalid, @@ -85,14 +84,14 @@ impl Client { // Unencryped XMPPStream let xmpp_stream = - xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()) + xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), ns::JABBER_CLIENT.to_owned()) .await?; let xmpp_stream = if xmpp_stream.stream_features.can_starttls() { // TlsStream let tls_stream = starttls(xmpp_stream).await?; // Encrypted XMPPStream - xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()) + xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), ns::JABBER_CLIENT.to_owned()) .await? } else { return Err(Error::Protocol(ProtocolError::NoTls)); @@ -106,7 +105,7 @@ impl Client { let stream = auth(xmpp_stream, creds).await?; // Authenticated XMPPStream let xmpp_stream = - xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?; + xmpp_stream::XMPPStream::start(stream, jid, ns::JABBER_CLIENT.to_owned()).await?; // XMPPStream bound to user session let xmpp_stream = bind(xmpp_stream).await?; diff --git a/tokio-xmpp/src/client/mod.rs b/tokio-xmpp/src/client/mod.rs index c98b01bd..f54e625e 100644 --- a/tokio-xmpp/src/client/mod.rs +++ b/tokio-xmpp/src/client/mod.rs @@ -3,6 +3,3 @@ mod bind; pub mod async_client; pub mod simple_client; - -pub const NS_XMPP_SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl"; -pub const NS_XMPP_BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind"; diff --git a/tokio-xmpp/src/client/simple_client.rs b/tokio-xmpp/src/client/simple_client.rs index 65eceb88..2146e216 100644 --- a/tokio-xmpp/src/client/simple_client.rs +++ b/tokio-xmpp/src/client/simple_client.rs @@ -6,7 +6,7 @@ use std::str::FromStr; use std::task::{Context, Poll}; use tokio::{net::TcpStream, stream::StreamExt}; use tokio_tls::TlsStream; -use xmpp_parsers::{Element, Jid}; +use xmpp_parsers::{ns, Element, Jid}; use super::auth::auth; use super::bind::bind; @@ -25,7 +25,6 @@ pub struct Client { } type XMPPStream = xmpp_stream::XMPPStream>; -const NS_JABBER_CLIENT: &str = "jabber:client"; impl Client { /// Start a new XMPP client and wait for a usable session @@ -51,14 +50,14 @@ impl Client { // Unencryped XMPPStream let xmpp_stream = - xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()) + xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), ns::JABBER_CLIENT.to_owned()) .await?; let xmpp_stream = if xmpp_stream.stream_features.can_starttls() { // TlsStream let tls_stream = starttls(xmpp_stream).await?; // Encrypted XMPPStream - xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()) + xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), ns::JABBER_CLIENT.to_owned()) .await? } else { return Err(Error::Protocol(ProtocolError::NoTls)); @@ -72,7 +71,7 @@ impl Client { let stream = auth(xmpp_stream, creds).await?; // Authenticated XMPPStream let xmpp_stream = - xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?; + xmpp_stream::XMPPStream::start(stream, jid, ns::JABBER_CLIENT.to_owned()).await?; // XMPPStream bound to user session let xmpp_stream = bind(xmpp_stream).await?; diff --git a/tokio-xmpp/src/component/auth.rs b/tokio-xmpp/src/component/auth.rs index 739c97f0..58ad8126 100644 --- a/tokio-xmpp/src/component/auth.rs +++ b/tokio-xmpp/src/component/auth.rs @@ -1,14 +1,12 @@ use futures::stream::StreamExt; use std::marker::Unpin; use tokio::io::{AsyncRead, AsyncWrite}; -use xmpp_parsers::component::Handshake; +use xmpp_parsers::{component::Handshake, ns}; use crate::xmpp_codec::Packet; use crate::xmpp_stream::XMPPStream; use crate::{AuthError, Error}; -const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept"; - pub async fn auth( stream: &mut XMPPStream, password: String, @@ -19,7 +17,7 @@ pub async fn auth( loop { match stream.next().await { Some(Ok(Packet::Stanza(ref stanza))) - if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) => + if stanza.is("handshake", ns::COMPONENT_ACCEPT) => { return Ok(()); } diff --git a/tokio-xmpp/src/component/mod.rs b/tokio-xmpp/src/component/mod.rs index 35a2267f..3e2ab479 100644 --- a/tokio-xmpp/src/component/mod.rs +++ b/tokio-xmpp/src/component/mod.rs @@ -6,7 +6,7 @@ use std::pin::Pin; use std::str::FromStr; use std::task::Context; use tokio::net::TcpStream; -use xmpp_parsers::{Element, Jid}; +use xmpp_parsers::{ns, Element, Jid}; use super::happy_eyeballs::connect; use super::xmpp_codec::Packet; @@ -26,7 +26,6 @@ pub struct Component { } type XMPPStream = xmpp_stream::XMPPStream; -const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept"; impl Component { /// Start a new XMPP component @@ -46,7 +45,7 @@ impl Component { let password = password; let tcp_stream = connect(server, None, port).await?; let mut xmpp_stream = - xmpp_stream::XMPPStream::start(tcp_stream, jid, NS_JABBER_COMPONENT_ACCEPT.to_owned()) + xmpp_stream::XMPPStream::start(tcp_stream, jid, ns::COMPONENT_ACCEPT.to_owned()) .await?; auth::auth(&mut xmpp_stream, password).await?; Ok(xmpp_stream) diff --git a/tokio-xmpp/src/starttls.rs b/tokio-xmpp/src/starttls.rs index 28923c47..1515d050 100644 --- a/tokio-xmpp/src/starttls.rs +++ b/tokio-xmpp/src/starttls.rs @@ -2,21 +2,18 @@ use futures::{sink::SinkExt, stream::StreamExt}; use native_tls::TlsConnector as NativeTlsConnector; use tokio::io::{AsyncRead, AsyncWrite}; use tokio_tls::{TlsConnector, TlsStream}; -use xmpp_parsers::Element; +use xmpp_parsers::{ns, Element}; use crate::xmpp_codec::Packet; use crate::xmpp_stream::XMPPStream; use crate::{Error, ProtocolError}; -/// XMPP TLS XML namespace -pub const NS_XMPP_TLS: &str = "urn:ietf:params:xml:ns:xmpp-tls"; - /// Performs `` on an XMPPStream and returns a binary /// TlsStream. pub async fn starttls( mut xmpp_stream: XMPPStream, ) -> Result, Error> { - let nonza = Element::builder("starttls", NS_XMPP_TLS).build(); + let nonza = Element::builder("starttls", ns::TLS).build(); let packet = Packet::Stanza(nonza); xmpp_stream.send(packet).await?; diff --git a/tokio-xmpp/src/stream_features.rs b/tokio-xmpp/src/stream_features.rs index 37de82ae..6b97ec35 100644 --- a/tokio-xmpp/src/stream_features.rs +++ b/tokio-xmpp/src/stream_features.rs @@ -1,9 +1,7 @@ //! Contains wrapper for `` -use crate::client::{NS_XMPP_BIND, NS_XMPP_SASL}; use crate::error::AuthError; -use crate::starttls::NS_XMPP_TLS; -use xmpp_parsers::Element; +use xmpp_parsers::{ns, Element}; /// Wraps ``, usually the very first nonza of an /// XMPPStream. @@ -20,22 +18,22 @@ impl StreamFeatures { /// Can initiate TLS session with this server? pub fn can_starttls(&self) -> bool { - self.0.get_child("starttls", NS_XMPP_TLS).is_some() + self.0.get_child("starttls", ns::TLS).is_some() } /// Iterate over SASL mechanisms pub fn sasl_mechanisms<'a>(&'a self) -> Result + 'a, AuthError> { Ok(self .0 - .get_child("mechanisms", NS_XMPP_SASL) + .get_child("mechanisms", ns::SASL) .ok_or(AuthError::NoMechanism)? .children() - .filter(|child| child.is("mechanism", NS_XMPP_SASL)) + .filter(|child| child.is("mechanism", ns::SASL)) .map(|mech_el| mech_el.text())) } /// Does server support user resource binding? pub fn can_bind(&self) -> bool { - self.0.get_child("bind", NS_XMPP_BIND).is_some() + self.0.get_child("bind", ns::BIND).is_some() } } diff --git a/tokio-xmpp/src/stream_start.rs b/tokio-xmpp/src/stream_start.rs index b9e898ae..c80345c5 100644 --- a/tokio-xmpp/src/stream_start.rs +++ b/tokio-xmpp/src/stream_start.rs @@ -2,14 +2,12 @@ use futures::{sink::SinkExt, stream::StreamExt}; use std::marker::Unpin; use tokio::io::{AsyncRead, AsyncWrite}; use tokio_util::codec::Framed; -use xmpp_parsers::{Element, Jid}; +use xmpp_parsers::{ns, Element, Jid}; use crate::xmpp_codec::{Packet, XMPPCodec}; use crate::xmpp_stream::XMPPStream; use crate::{Error, ProtocolError}; -const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams"; - /// Sends a ``, then wait for one from the server, and /// construct an XMPPStream. pub async fn start( @@ -21,7 +19,7 @@ pub async fn start( ("to".to_owned(), jid.clone().domain()), ("version".to_owned(), "1.0".to_owned()), ("xmlns".to_owned(), ns.clone()), - ("xmlns:stream".to_owned(), NS_XMPP_STREAM.to_owned()), + ("xmlns:stream".to_owned(), ns::STREAM.to_owned()), ] .iter() .cloned() @@ -53,7 +51,7 @@ pub async fn start( let stream_features; loop { match stream.next().await { - Some(Ok(Packet::Stanza(stanza))) if stanza.is("features", NS_XMPP_STREAM) => { + Some(Ok(Packet::Stanza(stanza))) if stanza.is("features", ns::STREAM) => { stream_features = stanza; break; } @@ -70,7 +68,7 @@ pub async fn start( stream, ns, stream_id.clone(), - Element::builder(stream_id, NS_XMPP_STREAM).build(), + Element::builder(stream_id, ns::STREAM).build(), ) }; Ok(stream) diff --git a/tokio-xmpp/src/xmpp_stream.rs b/tokio-xmpp/src/xmpp_stream.rs index 173f5de6..d9553f0d 100644 --- a/tokio-xmpp/src/xmpp_stream.rs +++ b/tokio-xmpp/src/xmpp_stream.rs @@ -15,9 +15,6 @@ use crate::stream_start; use crate::xmpp_codec::{Packet, XMPPCodec}; use crate::Error; -/// namespace -pub const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams"; - /// Wraps a binary stream (tokio's `AsyncRead + AsyncWrite`) to decode /// and encode XMPP packets. ///