Run cargo fmt on the entire project.

This commit is contained in:
Emmanuel Gil Peyrot 2018-12-18 19:04:31 +01:00
commit 51bae9b0e5
14 changed files with 591 additions and 607 deletions

View file

@ -1,23 +1,22 @@
use futures::{sink, Async, Future, Poll, Stream};
use minidom::Element;
use sasl::client::mechanisms::{Anonymous, Plain, Scram};
use sasl::client::Mechanism;
use sasl::common::scram::{Sha1, Sha256};
use sasl::common::Credentials;
use std::mem::replace;
use std::str::FromStr;
use futures::{Future, Poll, Async, sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite};
use sasl::common::Credentials;
use sasl::common::scram::{Sha1, Sha256};
use sasl::client::Mechanism;
use sasl::client::mechanisms::{Scram, Plain, Anonymous};
use minidom::Element;
use xmpp_parsers::sasl::{Auth, Challenge, Response, Success, Failure, Mechanism as XMPPMechanism};
use try_from::TryFrom;
use xmpp_parsers::sasl::{Auth, Challenge, Failure, Mechanism as XMPPMechanism, Response, Success};
use crate::stream_start::StreamStart;
use crate::xmpp_codec::Packet;
use crate::xmpp_stream::XMPPStream;
use crate::stream_start::StreamStart;
use crate::{Error, AuthError, ProtocolError};
use crate::{AuthError, Error, ProtocolError};
const NS_XMPP_SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
pub struct ClientAuth<S: AsyncWrite> {
state: ClientAuthState<S>,
mechanism: Box<Mechanism>,
@ -39,8 +38,9 @@ impl<S: AsyncWrite> ClientAuth<S> {
Box::new(Anonymous::new()),
];
let mech_names: Vec<String> =
stream.stream_features.get_child("mechanisms", NS_XMPP_SASL)
let mech_names: Vec<String> = stream
.stream_features
.get_child("mechanisms", NS_XMPP_SASL)
.ok_or(AuthError::NoMechanism)?
.children()
.filter(|child| child.is("mechanism", NS_XMPP_SASL))
@ -52,20 +52,18 @@ impl<S: AsyncWrite> ClientAuth<S> {
let name = mech.name().to_owned();
if mech_names.iter().any(|name1| *name1 == name) {
// println!("SASL mechanism selected: {:?}", name);
let initial = mech.initial()
.map_err(AuthError::Sasl)?;
let initial = mech.initial().map_err(AuthError::Sasl)?;
let mut this = ClientAuth {
state: ClientAuthState::Invalid,
mechanism: mech,
};
let mechanism = XMPPMechanism::from_str(&name)
.map_err(ProtocolError::Parsers)?;
let mechanism = XMPPMechanism::from_str(&name).map_err(ProtocolError::Parsers)?;
this.send(
stream,
Auth {
mechanism,
data: initial,
}
},
);
return Ok(this);
}
@ -89,61 +87,55 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
let state = replace(&mut self.state, ClientAuthState::Invalid);
match state {
ClientAuthState::WaitSend(mut send) =>
match send.poll() {
Ok(Async::Ready(stream)) => {
self.state = ClientAuthState::WaitRecv(stream);
ClientAuthState::WaitSend(mut send) => match send.poll() {
Ok(Async::Ready(stream)) => {
self.state = ClientAuthState::WaitRecv(stream);
self.poll()
}
Ok(Async::NotReady) => {
self.state = ClientAuthState::WaitSend(send);
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ClientAuthState::WaitRecv(mut stream) => match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
if let Ok(challenge) = Challenge::try_from(stanza.clone()) {
let response = self
.mechanism
.response(&challenge.data)
.map_err(AuthError::Sasl)?;
self.send(stream, Response { data: response });
self.poll()
},
Ok(Async::NotReady) => {
self.state = ClientAuthState::WaitSend(send);
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?,
},
ClientAuthState::WaitRecv(mut stream) =>
match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
if let Ok(challenge) = Challenge::try_from(stanza.clone()) {
let response = self.mechanism.response(&challenge.data)
.map_err(AuthError::Sasl)?;
self.send(stream, Response { data: response });
self.poll()
} else if let Ok(_) = Success::try_from(stanza.clone()) {
let start = stream.restart();
self.state = ClientAuthState::Start(start);
self.poll()
} else if let Ok(failure) = Failure::try_from(stanza) {
Err(AuthError::Fail(failure.defined_condition))?
} else {
Ok(Async::NotReady)
}
}
Ok(Async::Ready(_event)) => {
// println!("ClientAuth ignore {:?}", _event);
Ok(Async::NotReady)
},
Ok(_) => {
self.state = ClientAuthState::WaitRecv(stream);
Ok(Async::NotReady)
},
Err(e) =>
Err(ProtocolError::Parser(e))?
},
ClientAuthState::Start(mut start) =>
match start.poll() {
Ok(Async::Ready(stream)) =>
Ok(Async::Ready(stream)),
Ok(Async::NotReady) => {
} else if let Ok(_) = Success::try_from(stanza.clone()) {
let start = stream.restart();
self.state = ClientAuthState::Start(start);
self.poll()
} else if let Ok(failure) = Failure::try_from(stanza) {
Err(AuthError::Fail(failure.defined_condition))?
} else {
Ok(Async::NotReady)
},
Err(e) =>
Err(e)
},
ClientAuthState::Invalid =>
unreachable!(),
}
}
Ok(Async::Ready(_event)) => {
// println!("ClientAuth ignore {:?}", _event);
Ok(Async::NotReady)
}
Ok(_) => {
self.state = ClientAuthState::WaitRecv(stream);
Ok(Async::NotReady)
}
Err(e) => Err(ProtocolError::Parser(e))?,
},
ClientAuthState::Start(mut start) => match start.poll() {
Ok(Async::Ready(stream)) => Ok(Async::Ready(stream)),
Ok(Async::NotReady) => {
self.state = ClientAuthState::Start(start);
Ok(Async::NotReady)
}
Err(e) => Err(e),
},
ClientAuthState::Invalid => unreachable!(),
}
}
}

View file

@ -1,9 +1,9 @@
use futures::{sink, Async, Future, Poll, Stream};
use std::mem::replace;
use futures::{Future, Poll, Async, sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite};
use xmpp_parsers::iq::{Iq, IqType};
use xmpp_parsers::bind::Bind;
use try_from::TryFrom;
use xmpp_parsers::bind::Bind;
use xmpp_parsers::iq::{Iq, IqType};
use crate::xmpp_codec::Packet;
use crate::xmpp_stream::XMPPStream;
@ -26,16 +26,17 @@ impl<S: AsyncWrite> ClientBind<S> {
pub fn new(stream: XMPPStream<S>) -> Self {
match stream.stream_features.get_child("bind", NS_XMPP_BIND) {
None =>
// No resource binding available,
// return the (probably // usable) stream immediately
ClientBind::Unsupported(stream),
// No resource binding available,
// return the (probably // usable) stream immediately
{
ClientBind::Unsupported(stream)
}
Some(_) => {
let resource = stream.jid.resource.clone();
let iq = Iq::from_set(Bind::new(resource))
.with_id(BIND_REQ_ID.to_string());
let iq = Iq::from_set(Bind::new(resource)).with_id(BIND_REQ_ID.to_string());
let send = stream.send_stanza(iq);
ClientBind::WaitSend(send)
},
}
}
}
}
@ -48,59 +49,51 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
let state = replace(self, ClientBind::Invalid);
match state {
ClientBind::Unsupported(stream) =>
Ok(Async::Ready(stream)),
ClientBind::WaitSend(mut send) => {
match send.poll() {
Ok(Async::Ready(stream)) => {
replace(self, ClientBind::WaitRecv(stream));
self.poll()
},
Ok(Async::NotReady) => {
replace(self, ClientBind::WaitSend(send));
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?
ClientBind::Unsupported(stream) => Ok(Async::Ready(stream)),
ClientBind::WaitSend(mut send) => match send.poll() {
Ok(Async::Ready(stream)) => {
replace(self, ClientBind::WaitRecv(stream));
self.poll()
}
Ok(Async::NotReady) => {
replace(self, ClientBind::WaitSend(send));
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ClientBind::WaitRecv(mut stream) => {
match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) =>
match Iq::try_from(stanza) {
Ok(iq) => if iq.id == Some(BIND_REQ_ID.to_string()) {
match iq.payload {
IqType::Result(payload) => {
payload
.and_then(|payload| Bind::try_from(payload).ok())
.map(|bind| match bind {
Bind::Jid(jid) => stream.jid = jid,
_ => {}
});
Ok(Async::Ready(stream))
},
_ =>
Err(ProtocolError::InvalidBindResponse)?,
ClientBind::WaitRecv(mut stream) => match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => match Iq::try_from(stanza) {
Ok(iq) => {
if iq.id == Some(BIND_REQ_ID.to_string()) {
match iq.payload {
IqType::Result(payload) => {
payload
.and_then(|payload| Bind::try_from(payload).ok())
.map(|bind| match bind {
Bind::Jid(jid) => stream.jid = jid,
_ => {}
});
Ok(Async::Ready(stream))
}
} else {
Ok(Async::NotReady)
},
_ => Ok(Async::NotReady),
},
Ok(Async::Ready(_)) => {
replace(self, ClientBind::WaitRecv(stream));
self.poll()
},
Ok(Async::NotReady) => {
replace(self, ClientBind::WaitRecv(stream));
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?,
_ => Err(ProtocolError::InvalidBindResponse)?,
}
} else {
Ok(Async::NotReady)
}
}
_ => Ok(Async::NotReady),
},
Ok(Async::Ready(_)) => {
replace(self, ClientBind::WaitRecv(stream));
self.poll()
}
Ok(Async::NotReady) => {
replace(self, ClientBind::WaitRecv(stream));
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ClientBind::Invalid =>
unreachable!(),
ClientBind::Invalid => unreachable!(),
}
}
}

View file

@ -1,19 +1,19 @@
use futures::{done, Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
use idna;
use jid::{Jid, JidParseError};
use minidom::Element;
use sasl::common::{ChannelBinding, Credentials};
use std::mem::replace;
use std::str::FromStr;
use tokio::net::TcpStream;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_tls::TlsStream;
use futures::{Future, Stream, Poll, Async, Sink, StartSend, AsyncSink, done};
use minidom::Element;
use jid::{Jid, JidParseError};
use sasl::common::{Credentials, ChannelBinding};
use idna;
use super::event::Event;
use super::happy_eyeballs::Connecter;
use super::starttls::{StartTlsClient, NS_XMPP_TLS};
use super::xmpp_codec::Packet;
use super::xmpp_stream;
use super::starttls::{NS_XMPP_TLS, StartTlsClient};
use super::happy_eyeballs::Connecter;
use super::event::Event;
use super::{Error, ProtocolError};
mod auth;
@ -34,7 +34,7 @@ const NS_JABBER_CLIENT: &str = "jabber:client";
enum ClientState {
Invalid,
Disconnected,
Connecting(Box<Future<Item=XMPPStream, Error=Error>>),
Connecting(Box<Future<Item = XMPPStream, Error = Error>>),
Connected(XMPPStream),
}
@ -53,51 +53,62 @@ impl Client {
})
}
fn make_connect(jid: Jid, password: String) -> impl Future<Item=XMPPStream, Error=Error> {
fn make_connect(jid: Jid, password: String) -> impl Future<Item = XMPPStream, Error = Error> {
let username = jid.node.as_ref().unwrap().to_owned();
let jid1 = jid.clone();
let jid2 = jid.clone();
let password = password;
done(idna::domain_to_ascii(&jid.domain))
.map_err(|_| Error::Idna)
.and_then(|domain|
done(Connecter::from_lookup(&domain, Some("_xmpp-client._tcp"), 5222))
)
.and_then(|domain| {
done(Connecter::from_lookup(
&domain,
Some("_xmpp-client._tcp"),
5222,
))
})
.flatten()
.and_then(move |tcp_stream|
xmpp_stream::XMPPStream::start(tcp_stream, jid1, NS_JABBER_CLIENT.to_owned())
).and_then(|xmpp_stream| {
.and_then(move |tcp_stream| {
xmpp_stream::XMPPStream::start(tcp_stream, jid1, NS_JABBER_CLIENT.to_owned())
})
.and_then(|xmpp_stream| {
if Self::can_starttls(&xmpp_stream) {
Ok(Self::starttls(xmpp_stream))
} else {
Err(Error::Protocol(ProtocolError::NoTls))
}
}).flatten()
.and_then(|tls_stream|
XMPPStream::start(tls_stream, jid2, NS_JABBER_CLIENT.to_owned())
).and_then(move |xmpp_stream|
done(Self::auth(xmpp_stream, username, password))
// TODO: flatten?
).and_then(|auth| auth)
})
.flatten()
.and_then(|tls_stream| XMPPStream::start(tls_stream, jid2, NS_JABBER_CLIENT.to_owned()))
.and_then(
move |xmpp_stream| done(Self::auth(xmpp_stream, username, password)), // TODO: flatten?
)
.and_then(|auth| auth)
.and_then(|xmpp_stream| Self::bind(xmpp_stream))
.and_then(|xmpp_stream| {
Self::bind(xmpp_stream)
}).and_then(|xmpp_stream| {
// println!("Bound to {}", xmpp_stream.jid);
Ok(xmpp_stream)
})
}
fn can_starttls<S>(stream: &xmpp_stream::XMPPStream<S>) -> bool {
stream.stream_features
stream
.stream_features
.get_child("starttls", NS_XMPP_TLS)
.is_some()
}
fn starttls<S: AsyncRead + AsyncWrite>(stream: xmpp_stream::XMPPStream<S>) -> StartTlsClient<S> {
fn starttls<S: AsyncRead + AsyncWrite>(
stream: xmpp_stream::XMPPStream<S>,
) -> StartTlsClient<S> {
StartTlsClient::from_stream(stream)
}
fn auth<S: AsyncRead + AsyncWrite>(stream: xmpp_stream::XMPPStream<S>, username: String, password: String) -> Result<ClientAuth<S>, Error> {
fn auth<S: AsyncRead + AsyncWrite>(
stream: xmpp_stream::XMPPStream<S>,
username: String,
password: String,
) -> Result<ClientAuth<S>, Error> {
let creds = Credentials::default()
.with_username(username)
.with_password(password)
@ -118,31 +129,25 @@ impl Stream for Client {
let state = replace(&mut self.state, ClientState::Invalid);
match state {
ClientState::Invalid =>
Err(Error::InvalidState),
ClientState::Disconnected =>
Ok(Async::Ready(None)),
ClientState::Connecting(mut connect) => {
match connect.poll() {
Ok(Async::Ready(stream)) => {
self.state = ClientState::Connected(stream);
Ok(Async::Ready(Some(Event::Online)))
},
Ok(Async::NotReady) => {
self.state = ClientState::Connecting(connect);
Ok(Async::NotReady)
},
Err(e) =>
Err(e),
ClientState::Invalid => Err(Error::InvalidState),
ClientState::Disconnected => Ok(Async::Ready(None)),
ClientState::Connecting(mut connect) => match connect.poll() {
Ok(Async::Ready(stream)) => {
self.state = ClientState::Connected(stream);
Ok(Async::Ready(Some(Event::Online)))
}
Ok(Async::NotReady) => {
self.state = ClientState::Connecting(connect);
Ok(Async::NotReady)
}
Err(e) => Err(e),
},
ClientState::Connected(mut stream) => {
// Poll sink
match stream.poll_complete() {
Ok(Async::NotReady) => (),
Ok(Async::Ready(())) => (),
Err(e) =>
return Err(e)?,
Err(e) => return Err(e)?,
};
// Poll stream
@ -151,20 +156,18 @@ impl Stream for Client {
// EOF
self.state = ClientState::Disconnected;
Ok(Async::Ready(Some(Event::Disconnected)))
},
}
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
self.state = ClientState::Connected(stream);
Ok(Async::Ready(Some(Event::Stanza(stanza))))
},
Ok(Async::NotReady) |
Ok(Async::Ready(_)) => {
}
Ok(Async::NotReady) | Ok(Async::Ready(_)) => {
self.state = ClientState::Connected(stream);
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?,
}
Err(e) => Err(e)?,
}
},
}
}
}
}
@ -175,30 +178,23 @@ impl Sink for Client {
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
match self.state {
ClientState::Connected(ref mut stream) =>
match stream.start_send(Packet::Stanza(item)) {
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) =>
Ok(AsyncSink::NotReady(stanza)),
Ok(AsyncSink::NotReady(_)) =>
panic!("Client.start_send with stanza but got something else back"),
Ok(AsyncSink::Ready) => {
Ok(AsyncSink::Ready)
},
Err(e) =>
Err(e)?,
},
_ =>
Ok(AsyncSink::NotReady(item)),
ClientState::Connected(ref mut stream) => match stream.start_send(Packet::Stanza(item))
{
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) => Ok(AsyncSink::NotReady(stanza)),
Ok(AsyncSink::NotReady(_)) => {
panic!("Client.start_send with stanza but got something else back")
}
Ok(AsyncSink::Ready) => Ok(AsyncSink::Ready),
Err(e) => Err(e)?,
},
_ => Ok(AsyncSink::NotReady(item)),
}
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
match self.state {
ClientState::Connected(ref mut stream) =>
stream.poll_complete()
.map_err(|e| e.into()),
_ =>
Ok(Async::Ready(())),
ClientState::Connected(ref mut stream) => stream.poll_complete().map_err(|e| e.into()),
_ => Ok(Async::Ready(())),
}
}
}