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