xmpp-rs/src/client/bind.rs

107 lines
4 KiB
Rust
Raw Normal View History

2017-06-19 00:16:47 +00:00
use std::mem::replace;
2018-08-01 23:01:41 +00:00
use futures::{Future, Poll, Async, sink, Stream};
2017-06-19 00:16:47 +00:00
use tokio_io::{AsyncRead, AsyncWrite};
2018-08-01 23:01:41 +00:00
use xmpp_parsers::iq::{Iq, IqType};
use xmpp_parsers::bind::Bind;
2018-08-01 23:01:41 +00:00
use try_from::TryFrom;
2017-06-19 00:16:47 +00:00
2017-07-18 20:54:10 +00:00
use xmpp_codec::Packet;
use xmpp_stream::XMPPStream;
2018-09-06 15:46:06 +00:00
use {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 {
match stream.stream_features.get_child("bind", NS_XMPP_BIND) {
2017-06-19 00:16:47 +00:00
None =>
// No resource binding available,
// return the (probably // usable) stream immediately
ClientBind::Unsupported(stream),
Some(_) => {
let resource = stream.jid.resource.clone();
2018-08-01 23:01:41 +00:00
let iq = Iq::from_set(Bind::new(resource))
.with_id(BIND_REQ_ID.to_string());
let send = stream.send_stanza(iq);
2017-06-19 00:16:47 +00:00
ClientBind::WaitSend(send)
},
}
}
}
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 {
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) =>
2018-09-06 15:46:06 +00:00
Err(e.into())
2017-06-19 00:16:47 +00:00
}
},
ClientBind::WaitRecv(mut stream) => {
match stream.poll() {
2018-08-01 23:01:41 +00:00
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))
},
_ =>
2018-09-06 15:46:06 +00:00
Err(ProtocolError::InvalidBindResponse.into()),
2018-08-01 23:01:41 +00:00
}
} else {
Ok(Async::NotReady)
},
_ => Ok(Async::NotReady),
2017-06-19 00:16:47 +00:00
},
Ok(Async::Ready(_)) => {
replace(self, ClientBind::WaitRecv(stream));
self.poll()
},
Ok(Async::NotReady) => {
2017-06-19 00:16:47 +00:00
replace(self, ClientBind::WaitRecv(stream));
Ok(Async::NotReady)
},
Err(e) =>
2018-09-06 15:46:06 +00:00
Err(e.into()),
2017-06-19 00:16:47 +00:00
}
},
ClientBind::Invalid =>
unreachable!(),
}
}
}