tokio-xmpp: rustfmt

This commit is contained in:
Astro 2020-05-30 00:14:32 +02:00
parent 537b4825a8
commit 1e1f593233
9 changed files with 51 additions and 47 deletions

View file

@ -1,5 +1,5 @@
use std::io::{Read, stdin};
use std::env::args; use std::env::args;
use std::io::{stdin, Read};
use std::process::exit; use std::process::exit;
use std::str::FromStr; use std::str::FromStr;
use tokio; use tokio;

View file

@ -20,7 +20,6 @@ use crate::xmpp_codec::Packet;
use crate::xmpp_stream; use crate::xmpp_stream;
use crate::{Error, ProtocolError}; use crate::{Error, ProtocolError};
/// XMPP client connection and state /// XMPP client connection and state
/// ///
/// It is able to reconnect. TODO: implement session management. /// It is able to reconnect. TODO: implement session management.
@ -86,13 +85,15 @@ impl Client {
// Unencryped XMPPStream // Unencryped XMPPStream
let xmpp_stream = let xmpp_stream =
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()).await?; xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned())
.await?;
let xmpp_stream = if xmpp_stream.stream_features.can_starttls() { let xmpp_stream = if xmpp_stream.stream_features.can_starttls() {
// TlsStream // TlsStream
let tls_stream = starttls(xmpp_stream).await?; let tls_stream = starttls(xmpp_stream).await?;
// Encrypted XMPPStream // Encrypted XMPPStream
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()).await? xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned())
.await?
} else { } else {
return Err(Error::Protocol(ProtocolError::NoTls)); return Err(Error::Protocol(ProtocolError::NoTls));
}; };
@ -104,7 +105,8 @@ impl Client {
// Authenticated (unspecified) stream // Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?; let stream = auth(xmpp_stream, creds).await?;
// Authenticated XMPPStream // Authenticated XMPPStream
let xmpp_stream = xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?; let xmpp_stream =
xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?;
// XMPPStream bound to user session // XMPPStream bound to user session
let xmpp_stream = bind(xmpp_stream).await?; let xmpp_stream = bind(xmpp_stream).await?;

View file

@ -24,10 +24,7 @@ pub async fn auth<S: AsyncRead + AsyncWrite + Unpin>(
Box::new(|| Box::new(Anonymous::new())), Box::new(|| Box::new(Anonymous::new())),
]; ];
let remote_mechs: HashSet<String> = stream let remote_mechs: HashSet<String> = stream.stream_features.sasl_mechanisms()?.collect();
.stream_features
.sasl_mechanisms()?
.collect();
for local_mech in local_mechs { for local_mech in local_mechs {
let mut mechanism = local_mech(); let mut mechanism = local_mech();

View file

@ -1,8 +1,8 @@
mod auth; mod auth;
mod bind; mod bind;
pub mod simple_client;
pub mod async_client; 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_SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
pub const NS_XMPP_BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind"; pub const NS_XMPP_BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind";

View file

@ -8,13 +8,13 @@ use tokio::{net::TcpStream, stream::StreamExt};
use tokio_tls::TlsStream; use tokio_tls::TlsStream;
use xmpp_parsers::{Element, Jid}; use xmpp_parsers::{Element, Jid};
use super::auth::auth;
use super::bind::bind;
use crate::happy_eyeballs::connect; use crate::happy_eyeballs::connect;
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;
use crate::{Error, ProtocolError}; use crate::{Error, ProtocolError};
use super::auth::auth;
use super::bind::bind;
/// A simple XMPP client connection /// A simple XMPP client connection
/// ///
@ -51,13 +51,15 @@ impl Client {
// Unencryped XMPPStream // Unencryped XMPPStream
let xmpp_stream = let xmpp_stream =
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()).await?; xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned())
.await?;
let xmpp_stream = if xmpp_stream.stream_features.can_starttls() { let xmpp_stream = if xmpp_stream.stream_features.can_starttls() {
// TlsStream // TlsStream
let tls_stream = starttls(xmpp_stream).await?; let tls_stream = starttls(xmpp_stream).await?;
// Encrypted XMPPStream // Encrypted XMPPStream
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()).await? xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned())
.await?
} else { } else {
return Err(Error::Protocol(ProtocolError::NoTls)); return Err(Error::Protocol(ProtocolError::NoTls));
}; };
@ -69,7 +71,8 @@ impl Client {
// Authenticated (unspecified) stream // Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?; let stream = auth(xmpp_stream, creds).await?;
// Authenticated XMPPStream // Authenticated XMPPStream
let xmpp_stream = xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?; let xmpp_stream =
xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?;
// XMPPStream bound to user session // XMPPStream bound to user session
let xmpp_stream = bind(xmpp_stream).await?; let xmpp_stream = bind(xmpp_stream).await?;
@ -115,16 +118,18 @@ impl Stream for Client {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
loop { loop {
match Pin::new(&mut self.stream).poll_next(cx) { match Pin::new(&mut self.stream).poll_next(cx) {
Poll::Pending => Poll::Pending => return Poll::Pending,
return Poll::Pending, Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => {
Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => return Poll::Ready(Some(Ok(stanza)))
return Poll::Ready(Some(Ok(stanza))), }
Poll::Ready(Some(Ok(Packet::Text(_)))) => { Poll::Ready(Some(Ok(Packet::Text(_)))) => {
// Ignore, retry // Ignore, retry
} }
Poll::Ready(_) => Poll::Ready(_) =>
// Unexpected and errors, just end // Unexpected and errors, just end
return Poll::Ready(None), {
return Poll::Ready(None)
}
} }
} }
} }

View file

@ -7,11 +7,11 @@ mod stream_start;
mod xmpp_codec; mod xmpp_codec;
pub use crate::xmpp_codec::Packet; pub use crate::xmpp_codec::Packet;
mod event; mod event;
mod happy_eyeballs; pub use event::Event;
pub mod xmpp_stream;
pub mod stream_features;
pub use crate::event::Event;
mod client; mod client;
mod happy_eyeballs;
pub mod stream_features;
pub mod xmpp_stream;
pub use client::{async_client::Client as AsyncClient, simple_client::Client as SimpleClient}; pub use client::{async_client::Client as AsyncClient, simple_client::Client as SimpleClient};
mod component; mod component;
pub use crate::component::Component; pub use crate::component::Component;

View file

@ -1,9 +1,9 @@
//! Contains wrapper for `<stream:features/>` //! Contains wrapper for `<stream:features/>`
use xmpp_parsers::Element; use crate::client::{NS_XMPP_BIND, NS_XMPP_SASL};
use crate::starttls::NS_XMPP_TLS;
use crate::client::{NS_XMPP_SASL, NS_XMPP_BIND};
use crate::error::AuthError; use crate::error::AuthError;
use crate::starttls::NS_XMPP_TLS;
use xmpp_parsers::Element;
/// Wraps `<stream:features/>`, usually the very first nonza of an /// Wraps `<stream:features/>`, usually the very first nonza of an
/// XMPPStream. /// XMPPStream.
@ -20,26 +20,22 @@ impl StreamFeatures {
/// Can initiate TLS session with this server? /// Can initiate TLS session with this server?
pub fn can_starttls(&self) -> bool { pub fn can_starttls(&self) -> bool {
self.0 self.0.get_child("starttls", NS_XMPP_TLS).is_some()
.get_child("starttls", NS_XMPP_TLS)
.is_some()
} }
/// Iterate over SASL mechanisms /// Iterate over SASL mechanisms
pub fn sasl_mechanisms<'a>(&'a self) -> Result<impl Iterator<Item=String> + 'a, AuthError> { pub fn sasl_mechanisms<'a>(&'a self) -> Result<impl Iterator<Item = String> + 'a, AuthError> {
Ok(self.0 Ok(self
.get_child("mechanisms", NS_XMPP_SASL) .0
.ok_or(AuthError::NoMechanism)? .get_child("mechanisms", NS_XMPP_SASL)
.children() .ok_or(AuthError::NoMechanism)?
.filter(|child| child.is("mechanism", NS_XMPP_SASL)) .children()
.map(|mech_el| mech_el.text()) .filter(|child| child.is("mechanism", NS_XMPP_SASL))
) .map(|mech_el| mech_el.text()))
} }
/// Does server support user resource binding? /// Does server support user resource binding?
pub fn can_bind(&self) -> bool { pub fn can_bind(&self) -> bool {
self.0 self.0.get_child("bind", NS_XMPP_BIND).is_some()
.get_child("bind", NS_XMPP_BIND)
.is_some()
} }
} }

View file

@ -250,7 +250,13 @@ impl Decoder for XMPPCodec {
return result; return result;
} }
Err(e) => { Err(e) => {
error!("error {} at {}/{} in {:?}", e, e.valid_up_to(), buf1.len(), buf1); error!(
"error {} at {}/{} in {:?}",
e,
e.valid_up_to(),
buf1.len(),
buf1
);
return Err(ParserError::Utf8(e)); return Err(ParserError::Utf8(e));
} }
} }
@ -309,9 +315,7 @@ impl Encoder for XMPPCodec {
Ok(()) Ok(())
}) })
.map_err(to_io_err), .map_err(to_io_err),
Packet::StreamEnd => Packet::StreamEnd => write!(dst, "</stream:stream>\n").map_err(to_io_err),
write!(dst, "</stream:stream>\n")
.map_err(to_io_err),
} }
} }
} }

View file

@ -10,8 +10,8 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed; use tokio_util::codec::Framed;
use xmpp_parsers::{Element, Jid}; use xmpp_parsers::{Element, Jid};
use crate::stream_start;
use crate::stream_features::StreamFeatures; use crate::stream_features::StreamFeatures;
use crate::stream_start;
use crate::xmpp_codec::{Packet, XMPPCodec}; use crate::xmpp_codec::{Packet, XMPPCodec};
use crate::Error; use crate::Error;