xmpp-rs/xmpp/src/lib.rs

174 lines
5.2 KiB
Rust
Raw Normal View History

2019-03-21 17:41:29 +00:00
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![deny(bare_trait_objects)]
use std::path::{Path, PathBuf};
2023-06-01 09:44:49 +00:00
use std::sync::{Arc, RwLock};
pub use tokio_xmpp::parsers;
use tokio_xmpp::parsers::{
disco::DiscoInfoResult,
message::{Body, Message, MessageType},
2019-03-21 17:41:29 +00:00
};
use tokio_xmpp::AsyncClient as TokioXmppClient;
pub use tokio_xmpp::{BareJid, Element, FullJid, Jid};
#[macro_use]
extern crate log;
2019-03-21 17:41:29 +00:00
pub mod builder;
pub mod disco;
2023-12-29 20:56:37 +00:00
pub mod event;
pub mod event_loop;
2023-12-29 20:41:09 +00:00
pub mod feature;
2023-12-29 17:18:38 +00:00
pub mod iq;
2023-12-29 17:08:04 +00:00
pub mod message;
pub mod muc;
2023-12-29 17:08:04 +00:00
pub mod presence;
pub mod pubsub;
pub mod upload;
2019-03-21 17:41:29 +00:00
// Module re-exports
pub use builder::{ClientBuilder, ClientType};
2023-12-29 20:56:37 +00:00
pub use event::Event;
2023-12-29 20:41:09 +00:00
pub use feature::ClientFeature;
2019-03-21 17:41:29 +00:00
pub type Error = tokio_xmpp::Error;
pub type Id = Option<String>;
pub type RoomNick = String;
2019-03-21 17:41:29 +00:00
pub struct Agent {
client: TokioXmppClient,
2023-06-01 09:44:49 +00:00
default_nick: Arc<RwLock<String>>,
lang: Arc<Vec<String>>,
disco: DiscoInfoResult,
node: String,
uploads: Vec<(String, Jid, PathBuf)>,
awaiting_disco_bookmarks_type: bool,
}
impl Agent {
pub async fn disconnect(&mut self) -> Result<(), Error> {
self.client.send_end().await
}
pub async fn join_room(
&mut self,
room: BareJid,
nick: Option<String>,
password: Option<String>,
lang: &str,
status: &str,
) {
muc::room::join_room(self, room, nick, password, lang, status).await
2019-03-21 17:41:29 +00:00
}
/// Send a "leave room" request to the server (specifically, an "unavailable" presence stanza).
2023-12-08 18:38:52 +00:00
///
/// The returned future will resolve when the request has been sent,
/// not when the room has actually been left.
///
/// If successful, a `RoomLeft` event should be received later as a confirmation.
///
/// See: https://xmpp.org/extensions/xep-0045.html#exit
///
/// Note that this method does NOT remove the room from the auto-join list; the latter
/// is more a list of bookmarks that the account knows about and that have a flag set
/// to indicate that they should be joined automatically after connecting (see the JoinRoom event).
///
/// Regarding the latter, see the these minutes about auto-join behavior:
/// https://docs.modernxmpp.org/meetings/2019-01-brussels/#bookmarks
///
2023-12-08 18:38:52 +00:00
/// # Arguments
///
/// * `room_jid`: The JID of the room to leave.
/// * `nickname`: The nickname to use in the room.
2023-12-08 18:38:52 +00:00
/// * `lang`: The language of the status message.
/// * `status`: The status message to send.
2023-12-08 19:02:52 +00:00
pub async fn leave_room(
&mut self,
room_jid: BareJid,
nickname: RoomNick,
2023-12-08 19:02:52 +00:00
lang: impl Into<String>,
status: impl Into<String>,
) {
muc::room::leave_room(self, room_jid, nickname, lang, status).await
2023-12-08 18:38:52 +00:00
}
pub async fn send_message(
&mut self,
recipient: Jid,
type_: MessageType,
lang: &str,
text: &str,
) {
2019-03-21 17:41:29 +00:00
let mut message = Message::new(Some(recipient));
message.type_ = type_;
message
.bodies
.insert(String::from(lang), Body(String::from(text)));
let _ = self.client.send_stanza(message.into()).await;
}
pub async fn send_room_private_message(
&mut self,
room: BareJid,
recipient: RoomNick,
lang: &str,
text: &str,
) {
muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
}
2023-12-03 21:51:36 +00:00
/// Wait for new events.
///
/// # Returns
///
/// - `Some(events)` if there are new events; multiple may be returned at once.
/// - `None` if the underlying stream is closed.
pub async fn wait_for_events(&mut self) -> Option<Vec<Event>> {
event_loop::wait_for_events(self).await
2019-03-21 17:41:29 +00:00
}
pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
upload::send::upload_file_with(self, service, path).await
}
}
#[cfg(test)]
mod tests {
use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event};
use std::str::FromStr;
2020-05-29 22:24:58 +00:00
use tokio_xmpp::AsyncClient as TokioXmppClient;
#[tokio::test]
async fn test_simple() {
let jid = BareJid::from_str("foo@bar").unwrap();
let client = TokioXmppClient::new(jid.clone(), "meh");
// Client instance
let client_builder = ClientBuilder::new(jid, "meh")
.set_client(ClientType::Bot, "xmpp-rs")
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
.set_default_nick("bot")
.enable_feature(ClientFeature::ContactList);
#[cfg(feature = "avatars")]
let client_builder = client_builder.enable_feature(ClientFeature::Avatars);
let mut agent: Agent = client_builder.build_impl(client);
while let Some(events) = agent.wait_for_events().await {
assert!(match events[0] {
Event::Disconnected(_) => true,
_ => false,
});
assert_eq!(events.len(), 1);
break;
}
}
}