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::io::{stdin, Read};
use std::process::exit;
use std::str::FromStr;
use tokio;

View file

@ -20,7 +20,6 @@ use crate::xmpp_codec::Packet;
use crate::xmpp_stream;
use crate::{Error, ProtocolError};
/// XMPP client connection and state
///
/// It is able to reconnect. TODO: implement session management.
@ -86,13 +85,15 @@ impl Client {
// Unencryped XMPPStream
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() {
// TlsStream
let tls_stream = starttls(xmpp_stream).await?;
// 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 {
return Err(Error::Protocol(ProtocolError::NoTls));
};
@ -104,7 +105,8 @@ impl Client {
// Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?;
// 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
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())),
];
let remote_mechs: HashSet<String> = stream
.stream_features
.sasl_mechanisms()?
.collect();
let remote_mechs: HashSet<String> = stream.stream_features.sasl_mechanisms()?.collect();
for local_mech in local_mechs {
let mut mechanism = local_mech();

View file

@ -1,8 +1,8 @@
mod auth;
mod bind;
pub mod simple_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_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 xmpp_parsers::{Element, Jid};
use super::auth::auth;
use super::bind::bind;
use crate::happy_eyeballs::connect;
use crate::starttls::starttls;
use crate::xmpp_codec::Packet;
use crate::xmpp_stream;
use crate::{Error, ProtocolError};
use super::auth::auth;
use super::bind::bind;
/// A simple XMPP client connection
///
@ -51,13 +51,15 @@ impl Client {
// Unencryped XMPPStream
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() {
// TlsStream
let tls_stream = starttls(xmpp_stream).await?;
// 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 {
return Err(Error::Protocol(ProtocolError::NoTls));
};
@ -69,7 +71,8 @@ impl Client {
// Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?;
// 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
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>> {
loop {
match Pin::new(&mut self.stream).poll_next(cx) {
Poll::Pending =>
return Poll::Pending,
Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) =>
return Poll::Ready(Some(Ok(stanza))),
Poll::Pending => return Poll::Pending,
Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => {
return Poll::Ready(Some(Ok(stanza)))
}
Poll::Ready(Some(Ok(Packet::Text(_)))) => {
// Ignore, retry
}
Poll::Ready(_) =>
// 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;
pub use crate::xmpp_codec::Packet;
mod event;
mod happy_eyeballs;
pub mod xmpp_stream;
pub mod stream_features;
pub use crate::event::Event;
pub use event::Event;
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};
mod component;
pub use crate::component::Component;

View file

@ -1,9 +1,9 @@
//! Contains wrapper for `<stream:features/>`
use xmpp_parsers::Element;
use crate::starttls::NS_XMPP_TLS;
use crate::client::{NS_XMPP_SASL, NS_XMPP_BIND};
use crate::client::{NS_XMPP_BIND, NS_XMPP_SASL};
use crate::error::AuthError;
use crate::starttls::NS_XMPP_TLS;
use xmpp_parsers::Element;
/// Wraps `<stream:features/>`, usually the very first nonza of an
/// XMPPStream.
@ -20,26 +20,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_XMPP_TLS).is_some()
}
/// Iterate over SASL mechanisms
pub fn sasl_mechanisms<'a>(&'a self) -> Result<impl Iterator<Item = String> + 'a, AuthError> {
Ok(self.0
Ok(self
.0
.get_child("mechanisms", NS_XMPP_SASL)
.ok_or(AuthError::NoMechanism)?
.children()
.filter(|child| child.is("mechanism", NS_XMPP_SASL))
.map(|mech_el| mech_el.text())
)
.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_XMPP_BIND).is_some()
}
}

View file

@ -250,7 +250,13 @@ impl Decoder for XMPPCodec {
return result;
}
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));
}
}
@ -309,9 +315,7 @@ impl Encoder for XMPPCodec {
Ok(())
})
.map_err(to_io_err),
Packet::StreamEnd =>
write!(dst, "</stream:stream>\n")
.map_err(to_io_err),
Packet::StreamEnd => 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 xmpp_parsers::{Element, Jid};
use crate::stream_start;
use crate::stream_features::StreamFeatures;
use crate::stream_start;
use crate::xmpp_codec::{Packet, XMPPCodec};
use crate::Error;