2020-03-18 19:48:54 +00:00
|
|
|
use futures::{sink::SinkExt, Sink, Stream};
|
|
|
|
use idna;
|
|
|
|
use sasl::common::{ChannelBinding, Credentials};
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::task::{Context, Poll};
|
2020-12-04 16:33:55 +00:00
|
|
|
use tokio::net::TcpStream;
|
2021-02-15 18:59:46 +00:00
|
|
|
#[cfg(feature = "tls-native")]
|
|
|
|
use tokio_native_tls::TlsStream;
|
|
|
|
#[cfg(feature = "tls-rust")]
|
|
|
|
use tokio_rustls::client::TlsStream;
|
2020-12-04 16:33:55 +00:00
|
|
|
use tokio_stream::StreamExt;
|
2020-05-29 23:19:06 +00:00
|
|
|
use xmpp_parsers::{ns, Element, Jid};
|
2020-03-18 19:48:54 +00:00
|
|
|
|
2020-05-29 22:14:32 +00:00
|
|
|
use super::auth::auth;
|
|
|
|
use super::bind::bind;
|
2020-12-06 14:23:29 +00:00
|
|
|
use crate::happy_eyeballs::connect_with_srv;
|
2020-03-18 19:48:54 +00:00
|
|
|
use crate::starttls::starttls;
|
|
|
|
use crate::xmpp_codec::Packet;
|
2023-06-04 17:27:46 +00:00
|
|
|
use crate::xmpp_stream::{self, add_stanza_id};
|
2020-03-18 19:48:54 +00:00
|
|
|
use crate::{Error, ProtocolError};
|
|
|
|
|
2020-05-29 22:03:17 +00:00
|
|
|
/// A simple XMPP client connection
|
2020-03-18 19:48:54 +00:00
|
|
|
///
|
|
|
|
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
|
|
|
|
/// [`Sink`](#impl-Sink<Packet>) traits.
|
|
|
|
pub struct Client {
|
|
|
|
stream: XMPPStream,
|
|
|
|
}
|
|
|
|
|
|
|
|
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
/// Start a new XMPP client and wait for a usable session
|
|
|
|
pub async fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, Error> {
|
|
|
|
let jid = Jid::from_str(jid)?;
|
|
|
|
let client = Self::new_with_jid(jid, password.into()).await?;
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start a new client given that the JID is already parsed.
|
|
|
|
pub async fn new_with_jid(jid: Jid, password: String) -> Result<Self, Error> {
|
|
|
|
let stream = Self::connect(jid.clone(), password.clone()).await?;
|
|
|
|
Ok(Client { stream })
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:26:04 +00:00
|
|
|
/// Get direct access to inner XMPP Stream
|
|
|
|
pub fn into_inner(self) -> XMPPStream {
|
|
|
|
self.stream
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:48:54 +00:00
|
|
|
async fn connect(jid: Jid, password: String) -> Result<XMPPStream, Error> {
|
2023-06-21 16:30:25 +00:00
|
|
|
let username = jid.node_str().unwrap();
|
2020-03-18 19:48:54 +00:00
|
|
|
let password = password;
|
2023-06-21 16:30:25 +00:00
|
|
|
let domain = idna::domain_to_ascii(&jid.clone().domain_str()).map_err(|_| Error::Idna)?;
|
2020-03-18 19:48:54 +00:00
|
|
|
|
|
|
|
// TCP connection
|
2020-12-06 14:31:27 +00:00
|
|
|
let tcp_stream = connect_with_srv(&domain, "_xmpp-client._tcp", 5222).await?;
|
2020-03-18 19:48:54 +00:00
|
|
|
|
|
|
|
// Unencryped XMPPStream
|
|
|
|
let xmpp_stream =
|
2020-05-29 23:19:06 +00:00
|
|
|
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
|
2020-05-29 22:14:32 +00:00
|
|
|
.await?;
|
2020-03-18 19:48:54 +00:00
|
|
|
|
|
|
|
let xmpp_stream = if xmpp_stream.stream_features.can_starttls() {
|
|
|
|
// TlsStream
|
|
|
|
let tls_stream = starttls(xmpp_stream).await?;
|
|
|
|
// Encrypted XMPPStream
|
2020-05-29 23:19:06 +00:00
|
|
|
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
|
2020-05-29 22:14:32 +00:00
|
|
|
.await?
|
2020-03-18 19:48:54 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::Protocol(ProtocolError::NoTls));
|
|
|
|
};
|
|
|
|
|
|
|
|
let creds = Credentials::default()
|
|
|
|
.with_username(username)
|
|
|
|
.with_password(password)
|
|
|
|
.with_channel_binding(ChannelBinding::None);
|
|
|
|
// Authenticated (unspecified) stream
|
|
|
|
let stream = auth(xmpp_stream, creds).await?;
|
|
|
|
// Authenticated XMPPStream
|
2020-05-29 22:14:32 +00:00
|
|
|
let xmpp_stream =
|
2020-05-29 23:19:06 +00:00
|
|
|
xmpp_stream::XMPPStream::start(stream, jid, ns::JABBER_CLIENT.to_owned()).await?;
|
2020-03-18 19:48:54 +00:00
|
|
|
|
|
|
|
// XMPPStream bound to user session
|
|
|
|
let xmpp_stream = bind(xmpp_stream).await?;
|
|
|
|
Ok(xmpp_stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the client's bound JID (the one reported by the XMPP
|
|
|
|
/// server).
|
|
|
|
pub fn bound_jid(&self) -> &Jid {
|
|
|
|
&self.stream.jid
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send stanza
|
|
|
|
pub async fn send_stanza<E>(&mut self, stanza: E) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
E: Into<Element>,
|
|
|
|
{
|
2023-06-04 17:27:46 +00:00
|
|
|
self.send(Packet::Stanza(add_stanza_id(
|
|
|
|
stanza.into(),
|
|
|
|
ns::JABBER_CLIENT,
|
|
|
|
)))
|
|
|
|
.await
|
2020-03-18 19:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// End connection by sending `</stream:stream>`
|
|
|
|
///
|
|
|
|
/// You may expect the server to respond with the same. This
|
|
|
|
/// client will then drop its connection.
|
|
|
|
pub async fn end(mut self) -> Result<(), Error> {
|
|
|
|
self.send(Packet::StreamEnd).await?;
|
|
|
|
|
|
|
|
// Wait for stream end from server
|
|
|
|
while let Some(Ok(_)) = self.next().await {}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Incoming XMPP events
|
|
|
|
///
|
|
|
|
/// In an `async fn` you may want to use this with `use
|
|
|
|
/// futures::stream::StreamExt;`
|
|
|
|
impl Stream for Client {
|
|
|
|
type Item = Result<Element, Error>;
|
|
|
|
|
|
|
|
/// Low-level read on the XMPP stream
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
loop {
|
|
|
|
match Pin::new(&mut self.stream).poll_next(cx) {
|
2020-05-29 22:14:32 +00:00
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => {
|
|
|
|
return Poll::Ready(Some(Ok(stanza)))
|
|
|
|
}
|
2020-03-18 19:48:54 +00:00
|
|
|
Poll::Ready(Some(Ok(Packet::Text(_)))) => {
|
|
|
|
// Ignore, retry
|
|
|
|
}
|
|
|
|
Poll::Ready(_) =>
|
2020-05-29 22:14:32 +00:00
|
|
|
// Unexpected and errors, just end
|
|
|
|
{
|
|
|
|
return Poll::Ready(None)
|
|
|
|
}
|
2020-03-18 19:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Outgoing XMPP packets
|
|
|
|
///
|
|
|
|
/// See `send_stanza()` for an `async fn`
|
|
|
|
impl Sink<Packet> for Client {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> {
|
|
|
|
Pin::new(&mut self.stream).start_send(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Pin::new(&mut self.stream).poll_ready(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Pin::new(&mut self.stream).poll_flush(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Pin::new(&mut self.stream).poll_close(cx)
|
|
|
|
}
|
|
|
|
}
|