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