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