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