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 delay;
 69pub mod disco;
 70pub mod event;
 71pub mod event_loop;
 72pub mod feature;
 73pub mod iq;
 74pub mod message;
 75pub mod muc;
 76pub mod presence;
 77pub mod pubsub;
 78pub mod upload;
 79
 80pub use agent::Agent;
 81pub use builder::{ClientBuilder, ClientType};
 82pub use event::Event;
 83pub use feature::ClientFeature;
 84
 85pub type Error = tokio_xmpp::Error;
 86
 87/// Nickname for a person in a chatroom.
 88///
 89/// This nickname is not associated with a specific chatroom, or with a certain
 90/// user account.
 91///
 92// TODO: Introduce RoomMember and track by occupant-id
 93#[derive(Clone, Debug)]
 94pub struct RoomNick(ResourcePart);
 95
 96impl RoomNick {
 97    pub fn new(nick: ResourcePart) -> Self {
 98        Self(nick)
 99    }
100
101    pub fn from_resource_ref(nick: &ResourceRef) -> Self {
102        Self(nick.to_owned())
103    }
104}
105
106impl AsRef<ResourceRef> for RoomNick {
107    fn as_ref(&self) -> &ResourceRef {
108        self.0.as_ref()
109    }
110}
111
112impl From<RoomNick> for ResourcePart {
113    fn from(room_nick: RoomNick) -> Self {
114        room_nick.0
115    }
116}
117
118impl fmt::Display for RoomNick {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        write!(f, "{}", self.0)
121    }
122}
123
124impl core::str::FromStr for RoomNick {
125    type Err = crate::jid::Error;
126
127    fn from_str(s: &str) -> Result<Self, Self::Err> {
128        Ok(Self::new(ResourcePart::new(s)?.into()))
129    }
130}
131
132impl core::ops::Deref for RoomNick {
133    type Target = ResourcePart;
134
135    fn deref(&self) -> &ResourcePart {
136        &self.0
137    }
138}
139
140#[cfg(test)]
141mod tests {
142    #[test]
143    fn reexports() {
144        #[allow(unused_imports)]
145        use crate::jid;
146        #[allow(unused_imports)]
147        use crate::minidom;
148        #[allow(unused_imports)]
149        use crate::parsers;
150        #[allow(unused_imports)]
151        use crate::tokio_xmpp;
152    }
153}
154
155// The test below is dysfunctional since we have moved to StanzaStream. The
156// StanzaStream will attempt to connect to foo@bar indefinitely.
157// Keeping it here as inspiration for future integration tests.
158/*
159#[cfg(all(test, any(feature = "starttls-rust", feature = "starttls-native")))]
160mod tests {
161    use super::jid::{BareJid, ResourcePart};
162    use super::{ClientBuilder, ClientFeature, ClientType, Event};
163    use std::str::FromStr;
164    use tokio_xmpp::Client as TokioXmppClient;
165
166    #[tokio::test]
167    async fn test_simple() {
168        let jid = BareJid::from_str("foo@bar").unwrap();
169        let nick = RoomNick::from_str("bot").unwrap();
170
171        let client = TokioXmppClient::new(jid.clone(), "meh");
172
173        // Client instance
174        let client_builder = ClientBuilder::new(jid, "meh")
175            .set_client(ClientType::Bot, "xmpp-rs")
176            .set_website("https://xmpp.rs")
177            .set_default_nick(nick)
178            .enable_feature(ClientFeature::ContactList);
179
180        #[cfg(feature = "avatars")]
181        let client_builder = client_builder.enable_feature(ClientFeature::Avatars);
182
183        let mut agent = client_builder.build_impl(client);
184
185        loop {
186            let events = agent.wait_for_events().await;
187            assert!(match events[0] {
188                Event::Disconnected(_) => true,
189                _ => false,
190            });
191            assert_eq!(events.len(), 1);
192            break;
193        }
194    }
195}
196*/