2020-03-05 00:25:24 +00:00
|
|
|
|
use futures::{sink::SinkExt, stream::StreamExt};
|
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
|
use tokio_util::codec::Framed;
|
2020-05-29 23:19:06 +00:00
|
|
|
|
use xmpp_parsers::{ns, Element, Jid};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
2024-06-15 13:21:20 +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
|
|
|
|
|
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>(
|
2024-06-15 13:21:20 +00:00
|
|
|
|
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 = [
|
2024-03-03 15:15:04 +00:00
|
|
|
|
("to".to_owned(), jid.domain().to_string()),
|
2020-03-05 00:25:24 +00:00
|
|
|
|
("version".to_owned(), "1.0".to_owned()),
|
|
|
|
|
("xmlns".to_owned(), ns.clone()),
|
2020-05-29 23:19:06 +00:00
|
|
|
|
("xmlns:stream".to_owned(), ns::STREAM.to_owned()),
|
2020-03-05 00:25:24 +00:00
|
|
|
|
]
|
|
|
|
|
.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 {
|
2020-05-29 23:19:06 +00:00
|
|
|
|
Some(Ok(Packet::Stanza(stanza))) if stanza.is("features", ns::STREAM) => {
|
2020-03-05 00:25:24 +00:00
|
|
|
|
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(),
|
2020-05-29 23:19:06 +00:00
|
|
|
|
Element::builder(stream_id, ns::STREAM).build(),
|
2020-03-05 00:25:24 +00:00
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
Ok(stream)
|
2017-06-05 00:50:22 +00:00
|
|
|
|
}
|