2018-12-18 18:04:31 +00:00
|
|
|
use futures::{sink, Async, Future, Poll, Stream};
|
2019-09-08 19:28:44 +00:00
|
|
|
use std::convert::TryFrom;
|
2017-06-19 00:16:47 +00:00
|
|
|
use std::mem::replace;
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2019-09-08 19:28:44 +00:00
|
|
|
use xmpp_parsers::bind::{BindQuery, BindResponse};
|
2018-12-18 18:04:31 +00:00
|
|
|
use xmpp_parsers::iq::{Iq, IqType};
|
2019-10-22 23:32:41 +00:00
|
|
|
use xmpp_parsers::Jid;
|
2017-06-19 00:16:47 +00:00
|
|
|
|
2018-12-18 17:29:31 +00:00
|
|
|
use crate::xmpp_codec::Packet;
|
|
|
|
use crate::xmpp_stream::XMPPStream;
|
|
|
|
use crate::{Error, ProtocolError};
|
2017-06-19 00:16:47 +00:00
|
|
|
|
|
|
|
const NS_XMPP_BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind";
|
|
|
|
const BIND_REQ_ID: &str = "resource-bind";
|
|
|
|
|
|
|
|
pub enum ClientBind<S: AsyncWrite> {
|
|
|
|
Unsupported(XMPPStream<S>),
|
|
|
|
WaitSend(sink::Send<XMPPStream<S>>),
|
|
|
|
WaitRecv(XMPPStream<S>),
|
|
|
|
Invalid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsyncWrite> ClientBind<S> {
|
|
|
|
/// Consumes and returns the stream to express that you cannot use
|
|
|
|
/// the stream for anything else until the resource binding
|
|
|
|
/// req/resp are done.
|
|
|
|
pub fn new(stream: XMPPStream<S>) -> Self {
|
2017-07-17 18:53:00 +00:00
|
|
|
match stream.stream_features.get_child("bind", NS_XMPP_BIND) {
|
2017-06-19 00:16:47 +00:00
|
|
|
None =>
|
2018-12-18 18:04:31 +00:00
|
|
|
// No resource binding available,
|
|
|
|
// return the (probably // usable) stream immediately
|
|
|
|
{
|
|
|
|
ClientBind::Unsupported(stream)
|
|
|
|
}
|
2017-06-19 00:16:47 +00:00
|
|
|
Some(_) => {
|
2019-09-08 19:28:44 +00:00
|
|
|
let resource;
|
|
|
|
if let Jid::Full(jid) = stream.jid.clone() {
|
|
|
|
resource = Some(jid.resource);
|
|
|
|
} else {
|
|
|
|
resource = None;
|
|
|
|
}
|
|
|
|
let iq = Iq::from_set(BIND_REQ_ID, BindQuery::new(resource));
|
2018-08-01 22:19:06 +00:00
|
|
|
let send = stream.send_stanza(iq);
|
2017-06-19 00:16:47 +00:00
|
|
|
ClientBind::WaitSend(send)
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
2017-06-19 00:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
|
|
|
|
type Item = XMPPStream<S>;
|
2018-09-06 15:46:06 +00:00
|
|
|
type Error = Error;
|
2017-06-19 00:16:47 +00:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let state = replace(self, ClientBind::Invalid);
|
|
|
|
|
|
|
|
match state {
|
2018-12-18 18:04:31 +00:00
|
|
|
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)
|
2017-06-19 00:16:47 +00:00
|
|
|
}
|
2018-12-18 18:04:31 +00:00
|
|
|
Err(e) => Err(e)?,
|
2017-06-19 00:16:47 +00:00
|
|
|
},
|
2018-12-18 18:04:31 +00:00
|
|
|
ClientBind::WaitRecv(mut stream) => match stream.poll() {
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => match Iq::try_from(stanza) {
|
|
|
|
Ok(iq) => {
|
2019-02-26 20:35:38 +00:00
|
|
|
if iq.id == BIND_REQ_ID {
|
2018-12-18 18:04:31 +00:00
|
|
|
match iq.payload {
|
|
|
|
IqType::Result(payload) => {
|
|
|
|
payload
|
2019-09-08 19:28:44 +00:00
|
|
|
.and_then(|payload| BindResponse::try_from(payload).ok())
|
|
|
|
.map(|bind| stream.jid = bind.into());
|
2018-12-18 18:04:31 +00:00
|
|
|
Ok(Async::Ready(stream))
|
2018-08-01 23:01:41 +00:00
|
|
|
}
|
2018-12-18 18:04:31 +00:00
|
|
|
_ => 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)
|
2017-06-19 00:16:47 +00:00
|
|
|
}
|
2018-12-18 18:04:31 +00:00
|
|
|
Err(e) => Err(e)?,
|
2017-06-19 00:16:47 +00:00
|
|
|
},
|
2018-12-18 18:04:31 +00:00
|
|
|
ClientBind::Invalid => unreachable!(),
|
2017-06-19 00:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|