xmpp-rs/tokio-xmpp/src/client/mod.rs

296 lines
10 KiB
Rust
Raw Normal View History

2020-03-05 00:25:24 +00:00
use futures::{sink::SinkExt, task::Poll, Future, Sink, Stream};
2018-12-18 18:04:31 +00:00
use idna;
use sasl::common::{ChannelBinding, Credentials};
2017-06-20 19:26:51 +00:00
use std::mem::replace;
2020-03-05 00:25:24 +00:00
use std::pin::Pin;
2017-06-20 19:26:51 +00:00
use std::str::FromStr;
2020-03-05 00:25:24 +00:00
use std::task::Context;
2018-09-01 19:59:02 +00:00
use tokio::net::TcpStream;
2020-03-05 00:25:24 +00:00
use tokio::task::JoinHandle;
use tokio::task::LocalSet;
2017-06-20 19:26:51 +00:00
use tokio_tls::TlsStream;
2020-03-05 00:25:24 +00:00
use xmpp_parsers::{Element, Jid, JidParseError};
2017-06-20 19:26:51 +00:00
2018-12-18 18:04:31 +00:00
use super::event::Event;
2020-03-05 00:25:24 +00:00
use super::happy_eyeballs::connect;
use super::starttls::starttls;
2017-06-20 19:26:51 +00:00
use super::xmpp_codec::Packet;
use super::xmpp_stream;
2018-09-06 15:46:06 +00:00
use super::{Error, ProtocolError};
2017-06-20 19:26:51 +00:00
mod auth;
use auth::auth;
2017-06-20 19:26:51 +00:00
mod bind;
use bind::bind;
pub const NS_XMPP_SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
pub const NS_XMPP_BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind";
2017-06-20 19:26:51 +00:00
2018-08-02 17:58:19 +00:00
/// XMPP client connection and state
2020-03-15 23:34:46 +00:00
///
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
/// [`Sink`](#impl-Sink<Packet>) traits.
2017-06-20 19:26:51 +00:00
pub struct Client {
state: ClientState,
2020-03-05 00:25:24 +00:00
jid: Jid,
password: String,
reconnect: bool,
2017-06-20 19:26:51 +00:00
}
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
2017-07-18 23:02:45 +00:00
const NS_JABBER_CLIENT: &str = "jabber:client";
2017-06-20 19:26:51 +00:00
enum ClientState {
Invalid,
Disconnected,
2020-03-05 00:25:24 +00:00
Connecting(JoinHandle<Result<XMPPStream, Error>>, LocalSet),
2017-06-20 19:26:51 +00:00
Connected(XMPPStream),
}
impl Client {
2018-08-02 17:58:19 +00:00
/// Start a new XMPP client
///
/// Start polling the returned instance so that it will connect
/// and yield events.
2020-03-05 00:25:24 +00:00
pub fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, JidParseError> {
2018-08-02 18:10:26 +00:00
let jid = Jid::from_str(jid)?;
2020-03-05 00:25:24 +00:00
let client = Self::new_with_jid(jid, password.into());
Ok(client)
}
/// Start a new client given that the JID is already parsed.
2020-03-05 00:25:24 +00:00
pub fn new_with_jid(jid: Jid, password: String) -> Self {
let local = LocalSet::new();
let connect = local.spawn_local(Self::connect(jid.clone(), password.clone()));
let client = Client {
2020-03-05 00:25:24 +00:00
jid,
password,
state: ClientState::Connecting(connect, local),
reconnect: false,
};
client
2017-06-20 19:26:51 +00:00
}
2020-03-15 23:34:46 +00:00
/// Set whether to reconnect (`true`) or let the stream end
/// (`false`) when a connection to the server has ended.
2020-03-05 00:25:24 +00:00
pub fn set_reconnect(&mut self, reconnect: bool) -> &mut Self {
self.reconnect = reconnect;
self
}
async fn connect(jid: Jid, password: String) -> Result<XMPPStream, Error> {
2019-09-08 19:28:44 +00:00
let username = jid.clone().node().unwrap();
2017-06-20 19:26:51 +00:00
let password = password;
2020-03-05 00:25:24 +00:00
let domain = idna::domain_to_ascii(&jid.clone().domain()).map_err(|_| Error::Idna)?;
// TCP connection
2020-03-05 00:25:24 +00:00
let tcp_stream = connect(&domain, Some("_xmpp-client._tcp"), 5222).await?;
// Unencryped XMPPStream
2020-03-05 00:25:24 +00:00
let xmpp_stream =
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()).await?;
let xmpp_stream = if xmpp_stream.stream_features.can_starttls() {
// TlsStream
let tls_stream = starttls(xmpp_stream).await?;
// Encrypted XMPPStream
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), NS_JABBER_CLIENT.to_owned()).await?
2020-03-05 00:25:24 +00:00
} else {
return Err(Error::Protocol(ProtocolError::NoTls));
};
2017-06-20 19:26:51 +00:00
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
let xmpp_stream = xmpp_stream::XMPPStream::start(stream, jid, NS_JABBER_CLIENT.to_owned()).await?;
2017-06-20 19:26:51 +00:00
// XMPPStream bound to user session
let xmpp_stream = bind(xmpp_stream).await?;
Ok(xmpp_stream)
2017-06-20 19:26:51 +00:00
}
/// Get the client's bound JID (the one reported by the XMPP
/// server).
pub fn bound_jid(&self) -> Option<&Jid> {
match self.state {
ClientState::Connected(ref stream) => Some(&stream.jid),
_ => None,
}
}
2020-03-05 00:25:24 +00:00
/// Send stanza
pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> {
self.send(Packet::Stanza(stanza)).await
}
2020-03-15 23:34:46 +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.
///
/// Make sure to disable reconnect.
2020-03-05 00:25:24 +00:00
pub async fn send_end(&mut self) -> Result<(), Error> {
self.send(Packet::StreamEnd).await
}
2017-06-20 19:26:51 +00:00
}
2020-03-15 23:34:46 +00:00
/// Incoming XMPP events
///
/// In an `async fn` you may want to use this with `use
/// futures::stream::StreamExt;`
2017-06-20 19:26:51 +00:00
impl Stream for Client {
type Item = Event;
2017-06-20 19:26:51 +00:00
2020-03-15 23:34:46 +00:00
/// Low-level read on the XMPP stream, allowing the underlying
/// machinery to:
///
/// * connect,
/// * starttls,
/// * authenticate,
/// * bind a session, and finally
/// * receive stanzas
///
/// ...for your client
2020-03-05 00:25:24 +00:00
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
2017-06-20 19:26:51 +00:00
let state = replace(&mut self.state, ClientState::Invalid);
match state {
2020-03-05 00:25:24 +00:00
ClientState::Invalid => panic!("Invalid client state"),
ClientState::Disconnected if self.reconnect => {
// TODO: add timeout
let mut local = LocalSet::new();
let connect =
local.spawn_local(Self::connect(self.jid.clone(), self.password.clone()));
let _ = Pin::new(&mut local).poll(cx);
self.state = ClientState::Connecting(connect, local);
self.poll_next(cx)
}
ClientState::Disconnected => Poll::Ready(None),
ClientState::Connecting(mut connect, mut local) => {
match Pin::new(&mut connect).poll(cx) {
Poll::Ready(Ok(Ok(stream))) => {
let bound_jid = stream.jid.clone();
self.state = ClientState::Connected(stream);
Poll::Ready(Some(Event::Online {
bound_jid,
resumed: false,
}))
2020-03-05 00:25:24 +00:00
}
Poll::Ready(Ok(Err(e))) => {
self.state = ClientState::Disconnected;
return Poll::Ready(Some(Event::Disconnected(e.into())));
}
Poll::Ready(Err(e)) => {
self.state = ClientState::Disconnected;
panic!("connect task: {}", e);
}
Poll::Pending => {
let _ = Pin::new(&mut local).poll(cx);
self.state = ClientState::Connecting(connect, local);
Poll::Pending
}
2017-06-20 19:26:51 +00:00
}
2020-03-05 00:25:24 +00:00
}
2017-06-20 19:26:51 +00:00
ClientState::Connected(mut stream) => {
// Poll sink
2020-03-05 00:25:24 +00:00
match Pin::new(&mut stream).poll_ready(cx) {
Poll::Pending => (),
Poll::Ready(Ok(())) => (),
Poll::Ready(Err(e)) => {
self.state = ClientState::Disconnected;
return Poll::Ready(Some(Event::Disconnected(e.into())));
}
};
// Poll stream
2020-03-05 00:25:24 +00:00
match Pin::new(&mut stream).poll_next(cx) {
Poll::Ready(None) => {
2017-06-20 19:26:51 +00:00
// EOF
self.state = ClientState::Disconnected;
2020-03-05 00:25:24 +00:00
Poll::Ready(Some(Event::Disconnected(Error::Disconnected)))
2018-12-18 18:04:31 +00:00
}
2020-03-05 00:25:24 +00:00
Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => {
2019-01-26 20:07:15 +00:00
// Receive stanza
2017-06-20 19:26:51 +00:00
self.state = ClientState::Connected(stream);
2020-03-05 00:25:24 +00:00
Poll::Ready(Some(Event::Stanza(stanza)))
2018-12-18 18:04:31 +00:00
}
2020-03-05 00:25:24 +00:00
Poll::Ready(Some(Ok(Packet::Text(_)))) => {
2019-01-26 20:07:15 +00:00
// Ignore text between stanzas
self.state = ClientState::Connected(stream);
2020-03-05 00:25:24 +00:00
Poll::Pending
2019-01-26 20:07:15 +00:00
}
2020-03-05 00:25:24 +00:00
Poll::Ready(Some(Ok(Packet::StreamStart(_)))) => {
2019-01-26 20:07:15 +00:00
// <stream:stream>
2020-03-05 00:25:24 +00:00
self.state = ClientState::Disconnected;
Poll::Ready(Some(Event::Disconnected(
ProtocolError::InvalidStreamStart.into(),
)))
2019-01-26 20:07:15 +00:00
}
2020-03-05 00:25:24 +00:00
Poll::Ready(Some(Ok(Packet::StreamEnd))) => {
2019-01-26 20:07:15 +00:00
// End of stream: </stream:stream>
2020-03-05 00:25:24 +00:00
self.state = ClientState::Disconnected;
Poll::Ready(Some(Event::Disconnected(Error::Disconnected)))
2019-01-26 20:07:15 +00:00
}
2020-03-05 00:25:24 +00:00
Poll::Pending => {
2019-01-26 20:07:15 +00:00
// Try again later
2017-06-20 19:26:51 +00:00
self.state = ClientState::Connected(stream);
2020-03-05 00:25:24 +00:00
Poll::Pending
}
Poll::Ready(Some(Err(e))) => {
self.state = ClientState::Disconnected;
Poll::Ready(Some(Event::Disconnected(e.into())))
2018-12-18 18:04:31 +00:00
}
2017-06-20 19:26:51 +00:00
}
2018-12-18 18:04:31 +00:00
}
2017-06-20 19:26:51 +00:00
}
}
}
2020-03-15 23:34:46 +00:00
/// Outgoing XMPP packets
///
/// See `send_stanza()` for an `async fn`
2020-03-05 00:25:24 +00:00
impl Sink<Packet> for Client {
type Error = Error;
2020-03-05 00:25:24 +00:00
fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> {
match self.state {
2020-03-05 00:25:24 +00:00
ClientState::Connected(ref mut stream) => {
Pin::new(stream).start_send(item).map_err(|e| e.into())
}
_ => Err(Error::InvalidState),
}
}
2020-03-05 00:25:24 +00:00
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
2017-07-20 22:19:08 +00:00
match self.state {
2020-03-05 00:25:24 +00:00
ClientState::Connected(ref mut stream) => {
Pin::new(stream).poll_ready(cx).map_err(|e| e.into())
}
_ => Poll::Pending,
}
}
2020-03-05 00:25:24 +00:00
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
match self.state {
2020-03-05 00:25:24 +00:00
ClientState::Connected(ref mut stream) => {
Pin::new(stream).poll_flush(cx).map_err(|e| e.into())
}
_ => Poll::Pending,
}
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
match self.state {
ClientState::Connected(ref mut stream) => {
Pin::new(stream).poll_close(cx).map_err(|e| e.into())
}
_ => Poll::Pending,
}
}
}