tokio-xmpp: rewrite for futures-0.3

This commit is contained in:
Astro 2020-03-05 01:25:24 +01:00
commit 23cb34e026
18 changed files with 801 additions and 1222 deletions

View file

@ -1,6 +1,6 @@
use futures::{sink, Async, Future, Poll, Stream};
use std::mem::replace;
use tokio_io::{AsyncRead, AsyncWrite};
use futures::stream::StreamExt;
use std::marker::Unpin;
use tokio::io::{AsyncRead, AsyncWrite};
use xmpp_parsers::component::Handshake;
use crate::xmpp_codec::Packet;
@ -9,81 +9,27 @@ use crate::{AuthError, Error};
const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
pub struct ComponentAuth<S: AsyncWrite> {
state: ComponentAuthState<S>,
}
pub async fn auth<S: AsyncRead + AsyncWrite + Unpin>(
stream: &mut XMPPStream<S>,
password: String,
) -> Result<(), Error> {
let nonza = Handshake::from_password_and_stream_id(&password, &stream.id);
stream.send_stanza(nonza).await?;
enum ComponentAuthState<S: AsyncWrite> {
WaitSend(sink::Send<XMPPStream<S>>),
WaitRecv(XMPPStream<S>),
Invalid,
}
impl<S: AsyncWrite> ComponentAuth<S> {
// TODO: doesn't have to be a Result<> actually
pub fn new(stream: XMPPStream<S>, password: String) -> Result<Self, Error> {
// FIXME: huge hack, shouldnt be an element!
let sid = stream.stream_features.name().to_owned();
let mut this = ComponentAuth {
state: ComponentAuthState::Invalid,
};
this.send(
stream,
Handshake::from_password_and_stream_id(&password, &sid),
);
Ok(this)
}
fn send(&mut self, stream: XMPPStream<S>, handshake: Handshake) {
let nonza = handshake;
let send = stream.send_stanza(nonza);
self.state = ComponentAuthState::WaitSend(send);
}
}
impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
type Item = XMPPStream<S>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let state = replace(&mut self.state, ComponentAuthState::Invalid);
match state {
ComponentAuthState::WaitSend(mut send) => match send.poll() {
Ok(Async::Ready(stream)) => {
self.state = ComponentAuthState::WaitRecv(stream);
self.poll()
}
Ok(Async::NotReady) => {
self.state = ComponentAuthState::WaitSend(send);
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ComponentAuthState::WaitRecv(mut stream) => match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
{
self.state = ComponentAuthState::Invalid;
Ok(Async::Ready(stream))
}
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
if stanza.is("error", "http://etherx.jabber.org/streams") =>
{
Err(AuthError::ComponentFail.into())
}
Ok(Async::Ready(_event)) => {
// println!("ComponentAuth ignore {:?}", _event);
Ok(Async::NotReady)
}
Ok(_) => {
self.state = ComponentAuthState::WaitRecv(stream);
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ComponentAuthState::Invalid => unreachable!(),
loop {
match stream.next().await {
Some(Ok(Packet::Stanza(ref stanza)))
if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
{
return Ok(());
}
Some(Ok(Packet::Stanza(ref stanza)))
if stanza.is("error", "http://etherx.jabber.org/streams") =>
{
return Err(AuthError::ComponentFail.into());
}
Some(_) => {}
None => return Err(Error::Disconnected),
}
}
}