2017-06-05 00:50:22 +00:00
|
|
|
|
use std::mem::replace;
|
2017-07-18 20:54:10 +00:00
|
|
|
|
use futures::{Future, Async, Poll, Stream, sink, Sink};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2018-07-31 21:34:04 +00:00
|
|
|
|
use tokio_codec::Framed;
|
2017-06-13 23:55:56 +00:00
|
|
|
|
use jid::Jid;
|
2017-07-22 00:59:51 +00:00
|
|
|
|
use minidom::Element;
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
2018-12-18 17:29:31 +00:00
|
|
|
|
use crate::xmpp_codec::{XMPPCodec, Packet};
|
|
|
|
|
use crate::xmpp_stream::XMPPStream;
|
|
|
|
|
use crate::{Error, ProtocolError};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
|
|
|
|
const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams";
|
|
|
|
|
|
|
|
|
|
pub struct StreamStart<S: AsyncWrite> {
|
|
|
|
|
state: StreamStartState<S>,
|
2017-06-13 23:55:56 +00:00
|
|
|
|
jid: Jid,
|
2017-07-18 23:02:45 +00:00
|
|
|
|
ns: String,
|
2017-06-05 00:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum StreamStartState<S: AsyncWrite> {
|
|
|
|
|
SendStart(sink::Send<Framed<S, XMPPCodec>>),
|
|
|
|
|
RecvStart(Framed<S, XMPPCodec>),
|
2017-07-18 23:02:45 +00:00
|
|
|
|
RecvFeatures(Framed<S, XMPPCodec>, String),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
Invalid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: AsyncWrite> StreamStart<S> {
|
2017-07-18 23:02:45 +00:00
|
|
|
|
pub fn from_stream(stream: Framed<S, XMPPCodec>, jid: Jid, ns: String) -> Self {
|
2017-06-13 23:55:56 +00:00
|
|
|
|
let attrs = [("to".to_owned(), jid.domain.clone()),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
("version".to_owned(), "1.0".to_owned()),
|
2017-07-18 23:02:45 +00:00
|
|
|
|
("xmlns".to_owned(), ns.clone()),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
("xmlns:stream".to_owned(), NS_XMPP_STREAM.to_owned()),
|
|
|
|
|
].iter().cloned().collect();
|
|
|
|
|
let send = stream.send(Packet::StreamStart(attrs));
|
|
|
|
|
|
|
|
|
|
StreamStart {
|
|
|
|
|
state: StreamStartState::SendStart(send),
|
2017-06-13 23:55:56 +00:00
|
|
|
|
jid,
|
2017-07-18 23:02:45 +00:00
|
|
|
|
ns,
|
2017-06-05 00:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: AsyncRead + AsyncWrite> Future for StreamStart<S> {
|
|
|
|
|
type Item = XMPPStream<S>;
|
2018-09-06 15:46:06 +00:00
|
|
|
|
type Error = Error;
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
|
let old_state = replace(&mut self.state, StreamStartState::Invalid);
|
|
|
|
|
let mut retry = false;
|
|
|
|
|
|
|
|
|
|
let (new_state, result) = match old_state {
|
|
|
|
|
StreamStartState::SendStart(mut send) =>
|
|
|
|
|
match send.poll() {
|
|
|
|
|
Ok(Async::Ready(stream)) => {
|
|
|
|
|
retry = true;
|
|
|
|
|
(StreamStartState::RecvStart(stream), Ok(Async::NotReady))
|
|
|
|
|
},
|
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
|
(StreamStartState::SendStart(send), Ok(Async::NotReady)),
|
|
|
|
|
Err(e) =>
|
2018-08-05 19:52:32 +00:00
|
|
|
|
(StreamStartState::Invalid, Err(e.into())),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
},
|
|
|
|
|
StreamStartState::RecvStart(mut stream) =>
|
|
|
|
|
match stream.poll() {
|
|
|
|
|
Ok(Async::Ready(Some(Packet::StreamStart(stream_attrs)))) => {
|
2018-09-06 21:57:42 +00:00
|
|
|
|
let stream_ns = stream_attrs.get("xmlns")
|
|
|
|
|
.ok_or(ProtocolError::NoStreamNamespace)?
|
|
|
|
|
.clone();
|
2017-07-22 00:59:51 +00:00
|
|
|
|
if self.ns == "jabber:client" {
|
|
|
|
|
retry = true;
|
|
|
|
|
// TODO: skip RecvFeatures for version < 1.0
|
|
|
|
|
(StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady))
|
|
|
|
|
} else {
|
2018-09-06 21:57:42 +00:00
|
|
|
|
let id = stream_attrs.get("id")
|
|
|
|
|
.ok_or(ProtocolError::NoStreamId)?
|
|
|
|
|
.clone();
|
|
|
|
|
// FIXME: huge hack, shouldn’t be an element!
|
2017-07-22 00:59:51 +00:00
|
|
|
|
let stream = XMPPStream::new(self.jid.clone(), stream, self.ns.clone(), Element::builder(id).build());
|
|
|
|
|
(StreamStartState::Invalid, Ok(Async::Ready(stream)))
|
|
|
|
|
}
|
2017-06-05 00:50:22 +00:00
|
|
|
|
},
|
|
|
|
|
Ok(Async::Ready(_)) =>
|
2018-09-06 15:46:06 +00:00
|
|
|
|
return Err(ProtocolError::InvalidToken.into()),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
|
(StreamStartState::RecvStart(stream), Ok(Async::NotReady)),
|
|
|
|
|
Err(e) =>
|
2018-09-06 15:46:06 +00:00
|
|
|
|
return Err(ProtocolError::from(e).into()),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
},
|
2017-07-18 23:02:45 +00:00
|
|
|
|
StreamStartState::RecvFeatures(mut stream, stream_ns) =>
|
2017-06-05 00:50:22 +00:00
|
|
|
|
match stream.poll() {
|
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) =>
|
2017-08-14 01:56:08 +00:00
|
|
|
|
if stanza.is("features", NS_XMPP_STREAM) {
|
2017-07-18 23:02:45 +00:00
|
|
|
|
let stream = XMPPStream::new(self.jid.clone(), stream, self.ns.clone(), stanza);
|
2017-06-13 23:55:56 +00:00
|
|
|
|
(StreamStartState::Invalid, Ok(Async::Ready(stream)))
|
2017-06-05 00:50:22 +00:00
|
|
|
|
} else {
|
2017-07-18 23:02:45 +00:00
|
|
|
|
(StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady))
|
2017-06-05 00:50:22 +00:00
|
|
|
|
},
|
2017-07-20 22:19:08 +00:00
|
|
|
|
Ok(Async::Ready(_)) | Ok(Async::NotReady) =>
|
2017-07-18 23:02:45 +00:00
|
|
|
|
(StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady)),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
Err(e) =>
|
2018-09-06 15:46:06 +00:00
|
|
|
|
return Err(ProtocolError::from(e).into()),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
},
|
|
|
|
|
StreamStartState::Invalid =>
|
|
|
|
|
unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.state = new_state;
|
|
|
|
|
if retry {
|
|
|
|
|
self.poll()
|
|
|
|
|
} else {
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|