2020-03-05 00:25:24 +00:00
|
|
|
|
use futures::{sink::SinkExt, stream::StreamExt};
|
|
|
|
|
use std::marker::Unpin;
|
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
|
use tokio_util::codec::Framed;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use xmpp_parsers::{Element, Jid};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
2018-12-18 18:04:31 +00:00
|
|
|
|
use crate::xmpp_codec::{Packet, XMPPCodec};
|
2018-12-18 17:29:31 +00:00
|
|
|
|
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";
|
|
|
|
|
|
2020-03-15 23:34:46 +00:00
|
|
|
|
/// Sends a `<stream:stream>`, then wait for one from the server, and
|
|
|
|
|
/// construct an XMPPStream.
|
2020-03-05 00:25:24 +00:00
|
|
|
|
pub async fn start<S: AsyncRead + AsyncWrite + Unpin>(
|
|
|
|
|
mut stream: Framed<S, XMPPCodec>,
|
2017-06-13 23:55:56 +00:00
|
|
|
|
jid: Jid,
|
2017-07-18 23:02:45 +00:00
|
|
|
|
ns: String,
|
2020-03-05 00:25:24 +00:00
|
|
|
|
) -> Result<XMPPStream<S>, Error> {
|
|
|
|
|
let attrs = [
|
|
|
|
|
("to".to_owned(), jid.clone().domain()),
|
|
|
|
|
("version".to_owned(), "1.0".to_owned()),
|
|
|
|
|
("xmlns".to_owned(), ns.clone()),
|
|
|
|
|
("xmlns:stream".to_owned(), NS_XMPP_STREAM.to_owned()),
|
|
|
|
|
]
|
|
|
|
|
.iter()
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
stream.send(Packet::StreamStart(attrs)).await?;
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
|
let stream_attrs;
|
|
|
|
|
loop {
|
|
|
|
|
match stream.next().await {
|
|
|
|
|
Some(Ok(Packet::StreamStart(attrs))) => {
|
|
|
|
|
stream_attrs = attrs;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(_)) => {}
|
|
|
|
|
Some(Err(e)) => return Err(e.into()),
|
|
|
|
|
None => return Err(Error::Disconnected),
|
2017-06-05 00:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
|
let stream_ns = stream_attrs
|
|
|
|
|
.get("xmlns")
|
|
|
|
|
.ok_or(ProtocolError::NoStreamNamespace)?
|
|
|
|
|
.clone();
|
|
|
|
|
let stream_id = stream_attrs
|
|
|
|
|
.get("id")
|
|
|
|
|
.ok_or(ProtocolError::NoStreamId)?
|
|
|
|
|
.clone();
|
|
|
|
|
let stream = if stream_ns == "jabber:client" && stream_attrs.get("version").is_some() {
|
|
|
|
|
let stream_features;
|
|
|
|
|
loop {
|
|
|
|
|
match stream.next().await {
|
|
|
|
|
Some(Ok(Packet::Stanza(stanza))) if stanza.is("features", NS_XMPP_STREAM) => {
|
|
|
|
|
stream_features = stanza;
|
|
|
|
|
break;
|
2018-12-18 18:04:31 +00:00
|
|
|
|
}
|
2020-03-05 00:25:24 +00:00
|
|
|
|
Some(Ok(_)) => {}
|
|
|
|
|
Some(Err(e)) => return Err(e.into()),
|
|
|
|
|
None => return Err(Error::Disconnected),
|
|
|
|
|
}
|
2017-06-05 00:50:22 +00:00
|
|
|
|
}
|
2020-03-05 00:25:24 +00:00
|
|
|
|
XMPPStream::new(jid, stream, ns, stream_id, stream_features)
|
|
|
|
|
} else {
|
|
|
|
|
// FIXME: huge hack, shouldn’t be an element!
|
|
|
|
|
XMPPStream::new(
|
|
|
|
|
jid,
|
|
|
|
|
stream,
|
|
|
|
|
ns,
|
|
|
|
|
stream_id.clone(),
|
|
|
|
|
Element::builder(stream_id).build(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
Ok(stream)
|
2017-06-05 00:50:22 +00:00
|
|
|
|
}
|