2024-08-11 11:53:20 +02:00
|
|
|
|
use futures::{task::Poll, Future, Sink, Stream};
|
2024-08-10 15:05:42 +02:00
|
|
|
|
use std::io;
|
2020-05-30 00:07:36 +02:00
|
|
|
|
use std::mem::replace;
|
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
use std::task::Context;
|
|
|
|
|
|
use tokio::task::JoinHandle;
|
2024-08-10 15:05:42 +02:00
|
|
|
|
use xmpp_parsers::{
|
|
|
|
|
|
jid::{FullJid, Jid},
|
|
|
|
|
|
stream_features::StreamFeatures,
|
|
|
|
|
|
};
|
2020-05-30 00:07:36 +02:00
|
|
|
|
|
2024-08-06 17:00:53 +02:00
|
|
|
|
use crate::{
|
2024-08-10 15:05:42 +02:00
|
|
|
|
client::{login::client_login, Client},
|
2024-08-06 17:00:53 +02:00
|
|
|
|
connect::{AsyncReadAndWrite, ServerConnector},
|
2024-08-10 15:05:42 +02:00
|
|
|
|
error::Error,
|
|
|
|
|
|
xmlstream::{xmpp::XmppStreamElement, ReadError, XmppStream},
|
|
|
|
|
|
Event, Stanza,
|
2024-08-06 17:00:53 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-08-11 11:53:20 +02:00
|
|
|
|
pub(crate) enum ClientState<S: AsyncReadAndWrite> {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
Invalid,
|
|
|
|
|
|
Disconnected,
|
2024-08-10 15:05:42 +02:00
|
|
|
|
Connecting(JoinHandle<Result<(Option<FullJid>, StreamFeatures, XmppStream<S>), Error>>),
|
|
|
|
|
|
Connected {
|
|
|
|
|
|
stream: XmppStream<S>,
|
|
|
|
|
|
features: StreamFeatures,
|
|
|
|
|
|
bound_jid: Jid,
|
|
|
|
|
|
},
|
2020-05-30 00:07:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Incoming XMPP events
|
|
|
|
|
|
///
|
|
|
|
|
|
/// In an `async fn` you may want to use this with `use
|
|
|
|
|
|
/// futures::stream::StreamExt;`
|
2023-12-29 01:09:33 -05:00
|
|
|
|
impl<C: ServerConnector> Stream for Client<C> {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
type Item = Event;
|
|
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
|
|
let state = replace(&mut self.state, ClientState::Invalid);
|
|
|
|
|
|
|
|
|
|
|
|
match state {
|
|
|
|
|
|
ClientState::Invalid => panic!("Invalid client state"),
|
|
|
|
|
|
ClientState::Disconnected if self.reconnect => {
|
|
|
|
|
|
// TODO: add timeout
|
2023-12-30 00:07:59 -05:00
|
|
|
|
let connect = tokio::spawn(client_login(
|
2024-08-06 20:40:24 +02:00
|
|
|
|
self.connector.clone(),
|
|
|
|
|
|
self.jid.clone(),
|
|
|
|
|
|
self.password.clone(),
|
2020-09-11 17:05:49 +02:00
|
|
|
|
));
|
2022-03-24 03:26:30 +01:00
|
|
|
|
self.state = ClientState::Connecting(connect);
|
2020-05-30 00:07:36 +02:00
|
|
|
|
self.poll_next(cx)
|
|
|
|
|
|
}
|
2023-12-19 20:20:03 +01:00
|
|
|
|
ClientState::Disconnected => {
|
|
|
|
|
|
self.state = ClientState::Disconnected;
|
2024-08-08 14:57:05 +02:00
|
|
|
|
Poll::Ready(None)
|
2023-12-19 20:20:03 +01:00
|
|
|
|
}
|
2022-03-22 23:29:25 +01:00
|
|
|
|
ClientState::Connecting(mut connect) => match Pin::new(&mut connect).poll(cx) {
|
2024-08-10 15:05:42 +02:00
|
|
|
|
Poll::Ready(Ok(Ok((bound_jid, features, stream)))) => {
|
|
|
|
|
|
let bound_jid = bound_jid.map(Jid::from).unwrap_or_else(|| self.jid.clone());
|
|
|
|
|
|
self.state = ClientState::Connected {
|
|
|
|
|
|
stream,
|
|
|
|
|
|
bound_jid: bound_jid.clone(),
|
|
|
|
|
|
features,
|
|
|
|
|
|
};
|
2022-03-22 23:29:25 +01:00
|
|
|
|
Poll::Ready(Some(Event::Online {
|
|
|
|
|
|
bound_jid,
|
|
|
|
|
|
resumed: false,
|
|
|
|
|
|
}))
|
2020-05-30 00:07:36 +02:00
|
|
|
|
}
|
2022-03-22 23:29:25 +01: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 => {
|
|
|
|
|
|
self.state = ClientState::Connecting(connect);
|
|
|
|
|
|
Poll::Pending
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2024-08-10 15:05:42 +02:00
|
|
|
|
ClientState::Connected {
|
|
|
|
|
|
mut stream,
|
|
|
|
|
|
features,
|
|
|
|
|
|
bound_jid,
|
|
|
|
|
|
} => {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
// Poll sink
|
|
|
|
|
|
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
|
2023-06-21 13:43:24 +02:00
|
|
|
|
//
|
|
|
|
|
|
// This needs to be a loop in order to ignore packets we don’t care about, or those
|
|
|
|
|
|
// we want to handle elsewhere. Returning something isn’t correct in those two
|
2024-08-06 17:00:53 +02:00
|
|
|
|
// cases because it would signal to tokio that the XmppStream is also done, while
|
2023-06-21 13:43:24 +02:00
|
|
|
|
// there could be additional packets waiting for us.
|
|
|
|
|
|
//
|
|
|
|
|
|
// The proper solution is thus a loop which we exit once we have something to
|
|
|
|
|
|
// return.
|
|
|
|
|
|
loop {
|
|
|
|
|
|
match Pin::new(&mut stream).poll_next(cx) {
|
2024-08-10 15:05:42 +02:00
|
|
|
|
Poll::Ready(None)
|
|
|
|
|
|
| Poll::Ready(Some(Err(ReadError::StreamFooterReceived))) => {
|
2023-06-21 13:43:24 +02:00
|
|
|
|
// EOF
|
|
|
|
|
|
self.state = ClientState::Disconnected;
|
|
|
|
|
|
return Poll::Ready(Some(Event::Disconnected(Error::Disconnected)));
|
|
|
|
|
|
}
|
2024-08-10 15:05:42 +02:00
|
|
|
|
Poll::Ready(Some(Err(ReadError::HardError(e)))) => {
|
|
|
|
|
|
// Treat stream as dead on I/O errors
|
|
|
|
|
|
self.state = ClientState::Disconnected;
|
|
|
|
|
|
return Poll::Ready(Some(Event::Disconnected(e.into())));
|
2023-06-21 13:43:24 +02:00
|
|
|
|
}
|
2024-08-10 15:05:42 +02:00
|
|
|
|
Poll::Ready(Some(Err(ReadError::ParseError(e)))) => {
|
|
|
|
|
|
// Treat stream as dead on parse errors, too (for now...)
|
2023-06-21 13:43:24 +02:00
|
|
|
|
self.state = ClientState::Disconnected;
|
|
|
|
|
|
return Poll::Ready(Some(Event::Disconnected(
|
2024-08-10 15:05:42 +02:00
|
|
|
|
io::Error::new(io::ErrorKind::InvalidData, e).into(),
|
2023-06-21 13:43:24 +02:00
|
|
|
|
)));
|
|
|
|
|
|
}
|
2024-08-10 15:05:42 +02:00
|
|
|
|
Poll::Ready(Some(Err(ReadError::SoftTimeout))) => {
|
|
|
|
|
|
// TODO: do something smart about this.
|
|
|
|
|
|
}
|
|
|
|
|
|
Poll::Ready(Some(Ok(XmppStreamElement::Iq(stanza)))) => {
|
|
|
|
|
|
// Receive stanza
|
|
|
|
|
|
self.state = ClientState::Connected {
|
|
|
|
|
|
stream,
|
|
|
|
|
|
features,
|
|
|
|
|
|
bound_jid,
|
|
|
|
|
|
};
|
|
|
|
|
|
// TODO: use specific stanza types instead of going back to elements...
|
|
|
|
|
|
return Poll::Ready(Some(Event::Stanza(stanza.into())));
|
|
|
|
|
|
}
|
|
|
|
|
|
Poll::Ready(Some(Ok(XmppStreamElement::Message(stanza)))) => {
|
|
|
|
|
|
// Receive stanza
|
|
|
|
|
|
self.state = ClientState::Connected {
|
|
|
|
|
|
stream,
|
|
|
|
|
|
features,
|
|
|
|
|
|
bound_jid,
|
|
|
|
|
|
};
|
|
|
|
|
|
// TODO: use specific stanza types instead of going back to elements...
|
|
|
|
|
|
return Poll::Ready(Some(Event::Stanza(stanza.into())));
|
|
|
|
|
|
}
|
|
|
|
|
|
Poll::Ready(Some(Ok(XmppStreamElement::Presence(stanza)))) => {
|
|
|
|
|
|
// Receive stanza
|
|
|
|
|
|
self.state = ClientState::Connected {
|
|
|
|
|
|
stream,
|
|
|
|
|
|
features,
|
|
|
|
|
|
bound_jid,
|
|
|
|
|
|
};
|
|
|
|
|
|
// TODO: use specific stanza types instead of going back to elements...
|
|
|
|
|
|
return Poll::Ready(Some(Event::Stanza(stanza.into())));
|
|
|
|
|
|
}
|
|
|
|
|
|
Poll::Ready(Some(Ok(_))) => {
|
|
|
|
|
|
// We ignore these for now.
|
2023-06-21 13:43:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
Poll::Pending => {
|
|
|
|
|
|
// Try again later
|
2024-08-10 15:05:42 +02:00
|
|
|
|
self.state = ClientState::Connected {
|
|
|
|
|
|
stream,
|
|
|
|
|
|
features,
|
|
|
|
|
|
bound_jid,
|
|
|
|
|
|
};
|
2023-06-21 13:43:24 +02:00
|
|
|
|
return Poll::Pending;
|
|
|
|
|
|
}
|
2020-05-30 00:07:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Outgoing XMPP packets
|
|
|
|
|
|
///
|
|
|
|
|
|
/// See `send_stanza()` for an `async fn`
|
2024-08-10 15:05:42 +02:00
|
|
|
|
impl<C: ServerConnector> Sink<Stanza> for Client<C> {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
2024-08-10 15:05:42 +02:00
|
|
|
|
fn start_send(mut self: Pin<&mut Self>, item: Stanza) -> Result<(), Self::Error> {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
match self.state {
|
2024-08-10 15:05:42 +02:00
|
|
|
|
ClientState::Connected { ref mut stream, .. } => Pin::new(stream)
|
|
|
|
|
|
.start_send(&item.into())
|
|
|
|
|
|
.map_err(|e| e.into()),
|
2020-05-30 00:07:36 +02:00
|
|
|
|
_ => Err(Error::InvalidState),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
|
|
match self.state {
|
2024-08-10 15:05:42 +02:00
|
|
|
|
ClientState::Connected { ref mut stream, .. } => {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
Pin::new(stream).poll_ready(cx).map_err(|e| e.into())
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => Poll::Pending,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
|
|
match self.state {
|
2024-08-10 15:05:42 +02:00
|
|
|
|
ClientState::Connected { ref mut stream, .. } => {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
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 {
|
2024-08-10 15:05:42 +02:00
|
|
|
|
ClientState::Connected { ref mut stream, .. } => {
|
2020-05-30 00:07:36 +02:00
|
|
|
|
Pin::new(stream).poll_close(cx).map_err(|e| e.into())
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => Poll::Pending,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|