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