2024-08-20 16:54:48 +02:00
|
|
|
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
2024-08-10 15:05:42 +02:00
|
|
|
use xmpp_parsers::{jid::Jid, stream_features::StreamFeatures};
|
2024-08-11 11:53:20 +02:00
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
connect::ServerConnector,
|
|
|
|
|
error::Error,
|
2024-08-20 16:54:48 +02:00
|
|
|
stanzastream::{StanzaStage, StanzaState, StanzaStream, StanzaToken},
|
|
|
|
|
xmlstream::Timeouts,
|
2024-08-10 15:05:42 +02:00
|
|
|
Stanza,
|
2024-08-11 11:53:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[cfg(any(feature = "starttls", feature = "insecure-tcp"))]
|
|
|
|
|
use crate::connect::DnsConfig;
|
|
|
|
|
#[cfg(feature = "starttls")]
|
|
|
|
|
use crate::connect::StartTlsServerConnector;
|
|
|
|
|
#[cfg(feature = "insecure-tcp")]
|
|
|
|
|
use crate::connect::TcpServerConnector;
|
|
|
|
|
|
2024-09-02 17:26:01 +02:00
|
|
|
pub(crate) mod login;
|
2024-08-11 11:53:20 +02:00
|
|
|
mod stream;
|
|
|
|
|
|
|
|
|
|
/// XMPP client connection and state
|
|
|
|
|
///
|
|
|
|
|
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
|
|
|
|
|
/// [`Sink`](#impl-Sink<Packet>) traits.
|
2024-08-20 16:54:48 +02:00
|
|
|
pub struct Client {
|
|
|
|
|
stream: StanzaStream,
|
|
|
|
|
bound_jid: Option<Jid>,
|
|
|
|
|
features: Option<StreamFeatures>,
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 16:54:48 +02:00
|
|
|
impl Client {
|
2024-08-11 11:53:20 +02:00
|
|
|
/// Get the client's bound JID (the one reported by the XMPP
|
|
|
|
|
/// server).
|
|
|
|
|
pub fn bound_jid(&self) -> Option<&Jid> {
|
2024-08-20 16:54:48 +02:00
|
|
|
self.bound_jid.as_ref()
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send stanza
|
2024-08-20 16:54:48 +02:00
|
|
|
pub async fn send_stanza(&mut self, mut stanza: Stanza) -> Result<StanzaToken, io::Error> {
|
2024-08-10 15:05:42 +02:00
|
|
|
stanza.ensure_id();
|
2024-08-20 16:54:48 +02:00
|
|
|
let mut token = self.stream.send(Box::new(stanza)).await;
|
|
|
|
|
match token.wait_for(StanzaStage::Sent).await {
|
|
|
|
|
// Queued < Sent, so it cannot be reached.
|
|
|
|
|
Some(StanzaState::Queued) => unreachable!(),
|
|
|
|
|
|
|
|
|
|
None | Some(StanzaState::Dropped) => Err(io::Error::new(
|
|
|
|
|
io::ErrorKind::NotConnected,
|
|
|
|
|
"stream disconnected fatally before stanza could be sent",
|
|
|
|
|
)),
|
|
|
|
|
Some(StanzaState::Failed { error }) => Err(error.into_io_error()),
|
|
|
|
|
Some(StanzaState::Sent { .. }) | Some(StanzaState::Acked { .. }) => Ok(token),
|
|
|
|
|
}
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the stream features (`<stream:features/>`) of the underlying stream
|
|
|
|
|
pub fn get_stream_features(&self) -> Option<&StreamFeatures> {
|
2024-08-20 16:54:48 +02:00
|
|
|
self.features.as_ref()
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// End connection by sending `</stream:stream>`
|
|
|
|
|
///
|
|
|
|
|
/// You may expect the server to respond with the same. This
|
|
|
|
|
/// client will then drop its connection.
|
2024-08-20 16:54:48 +02:00
|
|
|
pub async fn send_end(self) -> Result<(), Error> {
|
|
|
|
|
self.stream.close().await;
|
|
|
|
|
Ok(())
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "starttls")]
|
2024-08-20 16:54:48 +02:00
|
|
|
impl Client {
|
2024-08-11 11:53:20 +02:00
|
|
|
/// Start a new XMPP client using StartTLS transport and autoreconnect
|
|
|
|
|
///
|
|
|
|
|
/// Start polling the returned instance so that it will connect
|
|
|
|
|
/// and yield events.
|
|
|
|
|
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
|
|
|
|
|
let jid = jid.into();
|
2024-08-20 16:54:48 +02:00
|
|
|
let dns_config = DnsConfig::srv(&jid.domain().to_string(), "_xmpp-client._tcp", 5222);
|
|
|
|
|
Self::new_starttls(jid, password, dns_config, Timeouts::default())
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Start a new XMPP client with StartTLS transport and specific DNS config
|
|
|
|
|
pub fn new_starttls<J: Into<Jid>, P: Into<String>>(
|
|
|
|
|
jid: J,
|
|
|
|
|
password: P,
|
|
|
|
|
dns_config: DnsConfig,
|
2024-08-18 17:40:39 +02:00
|
|
|
timeouts: Timeouts,
|
2024-08-11 11:53:20 +02:00
|
|
|
) -> Self {
|
2024-08-18 17:40:39 +02:00
|
|
|
Self::new_with_connector(
|
|
|
|
|
jid,
|
|
|
|
|
password,
|
|
|
|
|
StartTlsServerConnector::from(dns_config),
|
|
|
|
|
timeouts,
|
|
|
|
|
)
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "insecure-tcp")]
|
2024-08-20 16:54:48 +02:00
|
|
|
impl Client {
|
2024-08-11 11:53:20 +02:00
|
|
|
/// Start a new XMPP client with plaintext insecure connection and specific DNS config
|
|
|
|
|
pub fn new_plaintext<J: Into<Jid>, P: Into<String>>(
|
|
|
|
|
jid: J,
|
|
|
|
|
password: P,
|
|
|
|
|
dns_config: DnsConfig,
|
2024-08-18 17:40:39 +02:00
|
|
|
timeouts: Timeouts,
|
2024-08-11 11:53:20 +02:00
|
|
|
) -> Self {
|
2024-08-18 17:40:39 +02:00
|
|
|
Self::new_with_connector(
|
|
|
|
|
jid,
|
|
|
|
|
password,
|
|
|
|
|
TcpServerConnector::from(dns_config),
|
|
|
|
|
timeouts,
|
|
|
|
|
)
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 01:12:48 +01:00
|
|
|
|
2024-08-20 16:54:48 +02:00
|
|
|
impl Client {
|
2024-08-11 11:53:20 +02:00
|
|
|
/// Start a new client given that the JID is already parsed.
|
2024-08-20 16:54:48 +02:00
|
|
|
pub fn new_with_connector<J: Into<Jid>, P: Into<String>, C: ServerConnector>(
|
2024-08-11 11:53:20 +02:00
|
|
|
jid: J,
|
|
|
|
|
password: P,
|
|
|
|
|
connector: C,
|
2024-08-18 17:40:39 +02:00
|
|
|
timeouts: Timeouts,
|
2024-08-11 11:53:20 +02:00
|
|
|
) -> Self {
|
2024-08-20 16:54:48 +02:00
|
|
|
Self {
|
|
|
|
|
stream: StanzaStream::new_c2s(connector, jid.into(), password.into(), timeouts, 16),
|
|
|
|
|
bound_jid: None,
|
|
|
|
|
features: None,
|
|
|
|
|
}
|
2024-08-11 11:53:20 +02:00
|
|
|
}
|
|
|
|
|
}
|