2018-08-02 19:58:19 +02:00
|
|
|
//! Components in XMPP are services/gateways that are logged into an
|
|
|
|
|
//! XMPP server under a JID consisting of just a domain name. They are
|
|
|
|
|
//! allowed to use any user and resource identifiers in their stanzas.
|
2024-08-11 12:21:06 +02:00
|
|
|
use futures::sink::SinkExt;
|
2024-07-24 20:39:27 +02:00
|
|
|
use minidom::Element;
|
2017-07-22 01:59:51 +01:00
|
|
|
use std::str::FromStr;
|
2024-07-24 20:39:27 +02:00
|
|
|
use xmpp_parsers::{jid::Jid, ns};
|
2017-07-22 01:59:51 +01:00
|
|
|
|
2024-08-11 12:21:06 +02:00
|
|
|
use crate::{
|
|
|
|
|
component::login::component_login,
|
|
|
|
|
connect::ServerConnector,
|
|
|
|
|
proto::{add_stanza_id, XmppStream},
|
|
|
|
|
Error,
|
|
|
|
|
};
|
2017-07-22 01:59:51 +01:00
|
|
|
|
2024-08-06 20:40:24 +02:00
|
|
|
#[cfg(any(feature = "starttls", feature = "insecure-tcp"))]
|
|
|
|
|
use crate::connect::DnsConfig;
|
|
|
|
|
#[cfg(feature = "insecure-tcp")]
|
|
|
|
|
use crate::connect::TcpServerConnector;
|
|
|
|
|
|
2024-08-11 12:21:06 +02:00
|
|
|
mod login;
|
|
|
|
|
mod stream;
|
2023-12-30 22:08:37 -05:00
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
/// Component connection to an XMPP server
|
2020-03-05 01:25:24 +01:00
|
|
|
///
|
2024-08-06 17:00:53 +02:00
|
|
|
/// This simplifies the `XmppStream` to a `Stream`/`Sink` of `Element`
|
2020-03-05 01:25:24 +01:00
|
|
|
/// (stanzas). Connection handling however is up to the user.
|
2023-12-30 22:08:37 -05:00
|
|
|
pub struct Component<C: ServerConnector> {
|
2018-08-02 19:58:19 +02:00
|
|
|
/// The component's Jabber-Id
|
2017-07-22 01:59:51 +01:00
|
|
|
pub jid: Jid,
|
2024-08-06 17:00:53 +02:00
|
|
|
stream: XmppStream<C::Stream>,
|
2017-07-22 01:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-11 12:21:06 +02:00
|
|
|
impl<C: ServerConnector> Component<C> {
|
|
|
|
|
/// Send stanza
|
|
|
|
|
pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> {
|
|
|
|
|
self.send(add_stanza_id(stanza, ns::COMPONENT_ACCEPT)).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// End connection
|
|
|
|
|
pub async fn send_end(&mut self) -> Result<(), Error> {
|
|
|
|
|
self.close().await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 20:40:24 +02:00
|
|
|
#[cfg(feature = "insecure-tcp")]
|
|
|
|
|
impl Component<TcpServerConnector> {
|
|
|
|
|
/// Start a new XMPP component over plaintext TCP to localhost:5347
|
2024-08-11 12:21:06 +02:00
|
|
|
#[cfg(feature = "insecure-tcp")]
|
2024-08-06 20:40:24 +02:00
|
|
|
pub async fn new(jid: &str, password: &str) -> Result<Self, Error> {
|
|
|
|
|
Self::new_plaintext(jid, password, DnsConfig::addr("127.0.0.1:5347")).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Start a new XMPP component over plaintext TCP
|
2024-08-11 12:21:06 +02:00
|
|
|
#[cfg(feature = "insecure-tcp")]
|
2024-08-06 20:40:24 +02:00
|
|
|
pub async fn new_plaintext(
|
|
|
|
|
jid: &str,
|
|
|
|
|
password: &str,
|
|
|
|
|
dns_config: DnsConfig,
|
|
|
|
|
) -> Result<Self, Error> {
|
2024-08-11 12:21:06 +02:00
|
|
|
Component::new_with_connector(jid, password, TcpServerConnector::from(dns_config)).await
|
2024-08-06 20:40:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 22:08:37 -05:00
|
|
|
impl<C: ServerConnector> Component<C> {
|
2024-08-10 17:39:55 +02:00
|
|
|
/// Start a new XMPP component.
|
|
|
|
|
///
|
2024-08-11 20:06:34 +02:00
|
|
|
/// Unfortunately [`StartTlsConnector`](crate::connect::StartTlsServerConnector) is not supported yet.
|
2024-08-11 19:42:07 +02:00
|
|
|
/// The tracking issue is [#143](https://gitlab.com/xmpp-rs/xmpp-rs/-/issues/143).
|
2024-01-01 01:13:51 -05:00
|
|
|
pub async fn new_with_connector(
|
|
|
|
|
jid: &str,
|
|
|
|
|
password: &str,
|
|
|
|
|
connector: C,
|
|
|
|
|
) -> Result<Self, Error> {
|
2018-08-02 20:10:26 +02:00
|
|
|
let jid = Jid::from_str(jid)?;
|
2017-07-22 01:59:51 +01:00
|
|
|
let password = password.to_owned();
|
2023-12-30 22:08:37 -05:00
|
|
|
let stream = component_login(connector, jid.clone(), password).await?;
|
2020-03-05 01:25:24 +01:00
|
|
|
Ok(Component { jid, stream })
|
2017-07-22 01:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|