2024-08-06 17:00:53 +02:00
|
|
|
|
//! `XmppStream` provides encoding/decoding for XMPP
|
|
|
|
|
|
|
|
|
|
|
|
use futures::{
|
|
|
|
|
|
sink::{Send, SinkExt},
|
|
|
|
|
|
stream::StreamExt,
|
|
|
|
|
|
task::Poll,
|
|
|
|
|
|
Sink, Stream,
|
|
|
|
|
|
};
|
2024-07-24 20:39:27 +02:00
|
|
|
|
use minidom::Element;
|
2022-09-07 13:10:38 +02:00
|
|
|
|
use rand::{thread_rng, Rng};
|
2020-03-05 01:25:24 +01:00
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
use std::task::Context;
|
|
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
|
|
use tokio_util::codec::Framed;
|
2024-08-06 17:00:53 +02:00
|
|
|
|
use xmpp_parsers::{jid::Jid, ns, stream_features::StreamFeatures, Error as ParsersError};
|
2017-06-05 02:50:22 +02:00
|
|
|
|
|
2024-08-06 17:00:53 +02:00
|
|
|
|
use crate::error::{Error, ProtocolError};
|
|
|
|
|
|
use crate::proto::{Packet, XmppCodec};
|
2017-06-05 02:50:22 +02:00
|
|
|
|
|
2023-06-04 19:27:46 +02:00
|
|
|
|
fn make_id() -> String {
|
2022-09-07 13:10:38 +02:00
|
|
|
|
let id: u64 = thread_rng().gen();
|
|
|
|
|
|
format!("{}", id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-04 19:27:46 +02:00
|
|
|
|
pub(crate) fn add_stanza_id(mut stanza: Element, default_ns: &str) -> Element {
|
|
|
|
|
|
if stanza.is("iq", default_ns)
|
|
|
|
|
|
|| stanza.is("message", default_ns)
|
|
|
|
|
|
|| stanza.is("presence", default_ns)
|
|
|
|
|
|
{
|
|
|
|
|
|
if stanza.attr("id").is_none() {
|
|
|
|
|
|
stanza.set_attr("id", make_id());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stanza
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-16 00:34:46 +01:00
|
|
|
|
/// Wraps a binary stream (tokio's `AsyncRead + AsyncWrite`) to decode
|
|
|
|
|
|
/// and encode XMPP packets.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Implements `Sink + Stream`
|
2024-08-06 17:00:53 +02:00
|
|
|
|
pub struct XmppStream<S: AsyncRead + AsyncWrite + Unpin> {
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// The local Jabber-Id
|
2017-06-14 01:55:56 +02:00
|
|
|
|
pub jid: Jid,
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// Codec instance
|
2024-06-15 09:21:20 -04:00
|
|
|
|
pub stream: Framed<S, XmppCodec>,
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// `<stream:features/>` for XMPP version 1.0
|
2020-03-18 01:12:48 +01:00
|
|
|
|
pub stream_features: StreamFeatures,
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// Root namespace
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This is different for either c2s, s2s, or component
|
|
|
|
|
|
/// connections.
|
2017-07-19 01:02:45 +02:00
|
|
|
|
pub ns: String,
|
2020-03-05 01:25:24 +01:00
|
|
|
|
/// Stream `id` attribute
|
|
|
|
|
|
pub id: String,
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-06 17:00:53 +02:00
|
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> XmppStream<S> {
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// Constructor
|
2018-12-18 19:04:31 +01:00
|
|
|
|
pub fn new(
|
|
|
|
|
|
jid: Jid,
|
2024-06-15 09:21:20 -04:00
|
|
|
|
stream: Framed<S, XmppCodec>,
|
2018-12-18 19:04:31 +01:00
|
|
|
|
ns: String,
|
2020-03-05 01:25:24 +01:00
|
|
|
|
id: String,
|
2024-08-01 17:54:00 +02:00
|
|
|
|
stream_features: StreamFeatures,
|
2018-12-18 19:04:31 +01:00
|
|
|
|
) -> Self {
|
2024-08-06 17:00:53 +02:00
|
|
|
|
XmppStream {
|
2018-12-18 19:04:31 +01:00
|
|
|
|
jid,
|
2022-04-19 23:49:24 +02:00
|
|
|
|
stream,
|
2024-08-01 17:54:00 +02:00
|
|
|
|
stream_features,
|
2018-12-18 19:04:31 +01:00
|
|
|
|
ns,
|
2020-03-05 01:25:24 +01:00
|
|
|
|
id,
|
2018-12-18 19:04:31 +01:00
|
|
|
|
}
|
2017-06-14 01:55:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// Send a `<stream:stream>` start tag
|
2022-03-22 23:29:25 +01:00
|
|
|
|
pub async fn start(stream: S, jid: Jid, ns: String) -> Result<Self, Error> {
|
2024-08-06 17:00:53 +02:00
|
|
|
|
let mut stream = Framed::new(stream, XmppCodec::new());
|
|
|
|
|
|
let attrs = [
|
|
|
|
|
|
("to".to_owned(), jid.domain().to_string()),
|
|
|
|
|
|
("version".to_owned(), "1.0".to_owned()),
|
|
|
|
|
|
("xmlns".to_owned(), ns.clone()),
|
|
|
|
|
|
("xmlns:stream".to_owned(), ns::STREAM.to_owned()),
|
|
|
|
|
|
]
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.cloned()
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
stream.send(Packet::StreamStart(attrs)).await?;
|
|
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let stream_ns = stream_attrs
|
|
|
|
|
|
.get("xmlns")
|
|
|
|
|
|
.ok_or(ProtocolError::NoStreamNamespace)?
|
|
|
|
|
|
.clone();
|
|
|
|
|
|
let stream_id = stream_attrs
|
|
|
|
|
|
.get("id")
|
|
|
|
|
|
.ok_or(ProtocolError::NoStreamId)?
|
|
|
|
|
|
.clone();
|
|
|
|
|
|
if stream_ns == "jabber:client" && stream_attrs.get("version").is_some() {
|
|
|
|
|
|
loop {
|
|
|
|
|
|
match stream.next().await {
|
|
|
|
|
|
Some(Ok(Packet::Stanza(stanza))) => {
|
|
|
|
|
|
let stream_features = StreamFeatures::try_from(stanza)
|
|
|
|
|
|
.map_err(|e| Error::Protocol(ParsersError::from(e).into()))?;
|
|
|
|
|
|
return Ok(XmppStream::new(jid, stream, ns, stream_id, stream_features));
|
|
|
|
|
|
}
|
|
|
|
|
|
Some(Ok(_)) => {}
|
|
|
|
|
|
Some(Err(e)) => return Err(e.into()),
|
|
|
|
|
|
None => return Err(Error::Disconnected),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// FIXME: huge hack, shouldn’t be an element!
|
|
|
|
|
|
return Ok(XmppStream::new(
|
|
|
|
|
|
jid,
|
|
|
|
|
|
stream,
|
|
|
|
|
|
ns,
|
|
|
|
|
|
stream_id.clone(),
|
|
|
|
|
|
StreamFeatures::default(),
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// Unwraps the inner stream
|
2017-06-05 02:50:22 +02:00
|
|
|
|
pub fn into_inner(self) -> S {
|
2022-04-19 23:49:24 +02:00
|
|
|
|
self.stream.into_inner()
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
|
/// Re-run `start()`
|
2022-03-22 23:29:25 +01:00
|
|
|
|
pub async fn restart(self) -> Result<Self, Error> {
|
2022-04-19 23:49:24 +02:00
|
|
|
|
let stream = self.stream.into_inner();
|
2020-03-05 01:25:24 +01:00
|
|
|
|
Self::start(stream, self.jid, self.ns).await
|
2017-06-06 01:38:48 +02:00
|
|
|
|
}
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-06 17:00:53 +02:00
|
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> XmppStream<S> {
|
2018-08-02 00:19:06 +02:00
|
|
|
|
/// Convenience method
|
2020-03-05 01:25:24 +01:00
|
|
|
|
pub fn send_stanza<E: Into<Element>>(&mut self, e: E) -> Send<Self, Packet> {
|
2023-06-04 19:01:45 +02:00
|
|
|
|
self.send(Packet::Stanza(e.into()))
|
2018-08-02 00:19:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-06 01:29:20 +02:00
|
|
|
|
/// Proxy to self.stream
|
2024-08-06 17:00:53 +02:00
|
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> Sink<Packet> for XmppStream<S> {
|
2020-03-05 01:25:24 +01:00
|
|
|
|
type Error = crate::Error;
|
|
|
|
|
|
|
|
|
|
|
|
fn poll_ready(self: Pin<&mut Self>, _ctx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
|
|
// Pin::new(&mut self.stream).poll_ready(ctx)
|
|
|
|
|
|
// .map_err(|e| e.into())
|
|
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 15:34:26 +01:00
|
|
|
|
fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> {
|
2022-04-19 23:49:24 +02:00
|
|
|
|
Pin::new(&mut self.stream)
|
2020-03-05 01:25:24 +01:00
|
|
|
|
.start_send(item)
|
|
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
|
|
}
|
2017-06-05 02:50:22 +02:00
|
|
|
|
|
2023-12-04 15:34:26 +01:00
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
2022-04-19 23:49:24 +02:00
|
|
|
|
Pin::new(&mut self.stream)
|
2020-03-05 01:25:24 +01:00
|
|
|
|
.poll_flush(cx)
|
|
|
|
|
|
.map_err(|e| e.into())
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 15:34:26 +01:00
|
|
|
|
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
2022-04-19 23:49:24 +02:00
|
|
|
|
Pin::new(&mut self.stream)
|
2020-03-05 01:25:24 +01:00
|
|
|
|
.poll_close(cx)
|
|
|
|
|
|
.map_err(|e| e.into())
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-06 01:29:20 +02:00
|
|
|
|
/// Proxy to self.stream
|
2024-08-06 17:00:53 +02:00
|
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> Stream for XmppStream<S> {
|
2020-03-05 01:25:24 +01:00
|
|
|
|
type Item = Result<Packet, crate::Error>;
|
2017-06-05 02:50:22 +02:00
|
|
|
|
|
2023-12-04 15:34:26 +01:00
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
2022-04-19 23:49:24 +02:00
|
|
|
|
Pin::new(&mut self.stream)
|
2020-03-05 01:25:24 +01:00
|
|
|
|
.poll_next(cx)
|
|
|
|
|
|
.map(|result| result.map(|result| result.map_err(|e| e.into())))
|
2017-06-05 02:50:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|