xmpp/
lib.rs

1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6//! # Cargo features
7//!
8//! ## TLS backends
9//!
10//! - `aws_lc_rs` (default) enables rustls with the `aws_lc_rs` backend.
11//! - `ring` enables rustls with the `ring` backend`.
12//! - `rustls-any-backend` enables rustls, but without enabling a backend. It
13//!   is the application's responsibility to ensure that a backend is enabled
14//!   and installed.
15//! - `ktls` enables the use of ktls.
16//!   **Important:** Currently, connections will fail if the `tls` kernel
17//!   module is not available. There is no fallback to non-ktls connections!
18//! - `native-tls` enables the system-native TLS library (commonly
19//!   libssl/OpenSSL).
20//!
21//! **Note:** It is not allowed to mix rustls-based TLS backends with
22//! `tls-native`. Attempting to do so will result in a compilation error.
23//!
24//! **Note:** The `ktls` feature requires at least one `rustls` backend to be
25//! enabled (`aws_lc_rs` or `ring`).
26//!
27//! **Note:** When enabling not exactly one rustls backend, it is the
28//! application's responsibility to make sure that a default crypto provider is
29//! installed in `rustls`. Otherwise, all TLS connections will fail.
30//!
31//! ## Certificate validation
32//!
33//! When using `native-tls`, the system's native certificate store is used.
34//! Otherwise, you need to pick one of the following to ensure that TLS
35//! connections will succeed:
36//!
37//! - `rustls-native-certs` (default): Uses [rustls-native-certs](https://crates.io/crates/rustls-native-certs).
38//! - `webpki-roots`: Uses [webpki-roots](https://crates.io/crates/webpki-roots).
39//!
40//! ## Other features
41//!
42//! - `starttls` (default): Enables support for `<starttls/>`. Required as per
43//!   RFC 6120.
44//! - `avatars` (default): Enables support for avatars.
45//! - `serde`: Enable the `serde` feature in `tokio-xmpp`.
46//! - `escape-hatch`: Allow access to low-level API to bypass shortcomings of the current API.
47
48#![deny(bare_trait_objects)]
49#![cfg_attr(docsrs, feature(doc_cfg))]
50#![cfg_attr(docsrs, doc(auto_cfg))]
51
52extern crate alloc;
53
54pub use tokio_xmpp;
55pub use tokio_xmpp::jid;
56pub use tokio_xmpp::minidom;
57pub use tokio_xmpp::parsers;
58
59#[macro_use]
60extern crate log;
61
62use core::fmt;
63use jid::{ResourcePart, ResourceRef};
64use parsers::message::Id as MessageId;
65
66pub mod agent;
67pub mod builder;
68pub mod config;
69pub mod delay;
70pub mod disco;
71pub mod event;
72pub mod event_loop;
73pub mod feature;
74pub mod iq;
75pub mod message;
76pub mod muc;
77pub mod presence;
78pub mod pubsub;
79pub mod upload;
80
81pub use agent::Agent;
82pub use builder::ClientBuilder;
83pub use config::{ClientType, Config};
84pub use event::Event;
85pub use feature::ClientFeature;
86
87pub type Error = tokio_xmpp::Error;
88
89/// Nickname for a person in a chatroom.
90///
91/// This nickname is not associated with a specific chatroom, or with a certain
92/// user account.
93///
94// TODO: Introduce RoomMember and track by occupant-id
95#[derive(Clone, Debug)]
96pub struct RoomNick(ResourcePart);
97
98impl RoomNick {
99    pub fn new(nick: ResourcePart) -> Self {
100        Self(nick)
101    }
102
103    pub fn from_resource_ref(nick: &ResourceRef) -> Self {
104        Self(nick.to_owned())
105    }
106}
107
108impl AsRef<ResourceRef> for RoomNick {
109    fn as_ref(&self) -> &ResourceRef {
110        self.0.as_ref()
111    }
112}
113
114impl From<RoomNick> for ResourcePart {
115    fn from(room_nick: RoomNick) -> Self {
116        room_nick.0
117    }
118}
119
120impl fmt::Display for RoomNick {
121    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122        write!(f, "{}", self.0)
123    }
124}
125
126impl core::str::FromStr for RoomNick {
127    type Err = crate::jid::Error;
128
129    fn from_str(s: &str) -> Result<Self, Self::Err> {
130        Ok(Self::new(ResourcePart::new(s)?.into()))
131    }
132}
133
134impl core::ops::Deref for RoomNick {
135    type Target = ResourcePart;
136
137    fn deref(&self) -> &ResourcePart {
138        &self.0
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    #[test]
145    fn reexports() {
146        #[allow(unused_imports)]
147        use crate::jid;
148        #[allow(unused_imports)]
149        use crate::minidom;
150        #[allow(unused_imports)]
151        use crate::parsers;
152        #[allow(unused_imports)]
153        use crate::tokio_xmpp;
154    }
155}
156
157// The test below is dysfunctional since we have moved to StanzaStream. The
158// StanzaStream will attempt to connect to foo@bar indefinitely.
159// Keeping it here as inspiration for future integration tests.
160/*
161#[cfg(all(test, any(feature = "starttls-rust", feature = "starttls-native")))]
162mod tests {
163    use super::jid::{BareJid, ResourcePart};
164    use super::{ClientBuilder, ClientFeature, ClientType, Event};
165    use std::str::FromStr;
166    use tokio_xmpp::Client as TokioXmppClient;
167
168    #[tokio::test]
169    async fn test_simple() {
170        let jid = BareJid::from_str("foo@bar").unwrap();
171        let nick = RoomNick::from_str("bot").unwrap();
172
173        let client = TokioXmppClient::new(jid.clone(), "meh");
174
175        // Client instance
176        let client_builder = ClientBuilder::new(jid, "meh")
177            .set_client(ClientType::Bot, "xmpp-rs")
178            .set_website("https://xmpp.rs")
179            .set_default_nick(nick)
180            .enable_feature(ClientFeature::ContactList);
181
182        #[cfg(feature = "avatars")]
183        let client_builder = client_builder.enable_feature(ClientFeature::Avatars);
184
185        let mut agent = client_builder.build_impl(client);
186
187        loop {
188            let events = agent.wait_for_events().await;
189            assert!(match events[0] {
190                Event::Disconnected(_) => true,
191                _ => false,
192            });
193            assert_eq!(events.len(), 1);
194            break;
195        }
196    }
197}
198*/