2017-06-19 00:16:47 +00:00
|
|
|
use std::mem::replace;
|
|
|
|
use std::error::Error;
|
|
|
|
use std::str::FromStr;
|
2017-07-18 20:54:10 +00:00
|
|
|
use futures::{Future, Poll, Async, sink, Sink, Stream};
|
2017-06-19 00:16:47 +00:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
use jid::Jid;
|
2017-07-17 18:53:00 +00:00
|
|
|
use minidom::Element;
|
2018-08-01 22:00:00 +00:00
|
|
|
use xmpp_parsers::bind::Bind;
|
2017-06-19 00:16:47 +00:00
|
|
|
|
2017-07-18 20:54:10 +00:00
|
|
|
use xmpp_codec::Packet;
|
|
|
|
use xmpp_stream::XMPPStream;
|
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 =>
|
|
|
|
// No resource binding available,
|
|
|
|
// return the (probably // usable) stream immediately
|
|
|
|
ClientBind::Unsupported(stream),
|
|
|
|
Some(_) => {
|
2018-08-01 22:00:00 +00:00
|
|
|
let resource = stream.jid.resource.clone();
|
2018-08-01 22:19:06 +00:00
|
|
|
let iq = Bind::new(resource);
|
|
|
|
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>;
|
|
|
|
type Error = String;
|
|
|
|
|
|
|
|
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) =>
|
|
|
|
Err(e.description().to_owned()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientBind::WaitRecv(mut stream) => {
|
|
|
|
match stream.poll() {
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(ref iq))))
|
2017-07-17 18:53:00 +00:00
|
|
|
if iq.name() == "iq"
|
|
|
|
&& iq.attr("id") == Some(BIND_REQ_ID) => {
|
|
|
|
match iq.attr("type") {
|
2017-06-19 00:16:47 +00:00
|
|
|
Some("result") => {
|
2017-07-20 22:19:08 +00:00
|
|
|
get_bind_response_jid(iq)
|
2017-06-19 00:16:47 +00:00
|
|
|
.map(|jid| stream.jid = jid);
|
|
|
|
Ok(Async::Ready(stream))
|
|
|
|
},
|
|
|
|
_ =>
|
|
|
|
Err("resource bind response".to_owned()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
replace(self, ClientBind::WaitRecv(stream));
|
|
|
|
self.poll()
|
|
|
|
},
|
2017-06-19 00:33:01 +00:00
|
|
|
Ok(Async::NotReady) => {
|
2017-06-19 00:16:47 +00:00
|
|
|
replace(self, ClientBind::WaitRecv(stream));
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(e.description().to_owned()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientBind::Invalid =>
|
|
|
|
unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:53:00 +00:00
|
|
|
fn get_bind_response_jid(iq: &Element) -> Option<Jid> {
|
|
|
|
iq.get_child("bind", NS_XMPP_BIND)
|
2017-06-19 00:16:47 +00:00
|
|
|
.and_then(|bind_el|
|
2017-07-17 18:53:00 +00:00
|
|
|
bind_el.get_child("jid", NS_XMPP_BIND)
|
2017-06-19 00:16:47 +00:00
|
|
|
)
|
|
|
|
.and_then(|jid_el|
|
2017-07-17 18:53:00 +00:00
|
|
|
Jid::from_str(&jid_el.text())
|
2017-06-19 00:16:47 +00:00
|
|
|
.ok()
|
|
|
|
)
|
|
|
|
}
|