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/.
|
|
|
|
|
2019-07-25 13:03:22 +00:00
|
|
|
#![deny(bare_trait_objects)]
|
|
|
|
|
2021-12-26 23:48:37 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-06-01 09:44:49 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2023-08-21 08:57:33 +00:00
|
|
|
pub use tokio_xmpp::parsers;
|
2023-08-17 14:17:53 +00:00
|
|
|
use tokio_xmpp::parsers::{
|
2023-12-29 23:13:25 +00:00
|
|
|
disco::DiscoInfoResult,
|
2019-10-22 23:32:41 +00:00
|
|
|
message::{Body, Message, MessageType},
|
2019-03-21 17:41:29 +00:00
|
|
|
};
|
2023-12-29 23:13:25 +00:00
|
|
|
use tokio_xmpp::AsyncClient as TokioXmppClient;
|
2023-08-21 08:57:33 +00:00
|
|
|
pub use tokio_xmpp::{BareJid, Element, FullJid, Jid};
|
2019-09-29 02:08:56 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2023-12-29 19:58:26 +00:00
|
|
|
pub mod builder;
|
2023-12-29 21:07:46 +00:00
|
|
|
pub mod disco;
|
2023-12-29 20:56:37 +00:00
|
|
|
pub mod event;
|
2023-12-29 23:13:25 +00:00
|
|
|
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;
|
2023-12-29 23:28:57 +00:00
|
|
|
pub mod muc;
|
2023-12-29 17:08:04 +00:00
|
|
|
pub mod presence;
|
|
|
|
pub mod pubsub;
|
2023-12-29 17:29:22 +00:00
|
|
|
pub mod upload;
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2023-12-29 19:58:26 +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
|
|
|
|
2023-12-29 19:58:26 +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
|
|
|
|
2019-07-05 16:59:05 +00:00
|
|
|
pub struct Agent {
|
2020-03-08 18:57:59 +00:00
|
|
|
client: TokioXmppClient,
|
2023-06-01 09:44:49 +00:00
|
|
|
default_nick: Arc<RwLock<String>>,
|
|
|
|
lang: Arc<Vec<String>>,
|
2020-03-08 18:57:59 +00:00
|
|
|
disco: DiscoInfoResult,
|
|
|
|
node: String,
|
2021-12-26 23:48:37 +00:00
|
|
|
uploads: Vec<(String, Jid, PathBuf)>,
|
2023-12-16 15:47:10 +00:00
|
|
|
awaiting_disco_bookmarks_type: bool,
|
2019-07-05 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Agent {
|
2021-12-26 23:50:23 +00:00
|
|
|
pub async fn disconnect(&mut self) -> Result<(), Error> {
|
|
|
|
self.client.send_end().await
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
pub async fn join_room(
|
2019-10-22 23:32:41 +00:00
|
|
|
&mut self,
|
|
|
|
room: BareJid,
|
|
|
|
nick: Option<String>,
|
|
|
|
password: Option<String>,
|
|
|
|
lang: &str,
|
|
|
|
status: &str,
|
|
|
|
) {
|
2023-12-29 23:28:57 +00:00
|
|
|
muc::room::join_room(self, room, nick, password, lang, status).await
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 12:26:23 +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
|
|
|
|
///
|
2023-12-09 12:26:23 +00:00
|
|
|
/// 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).
|
|
|
|
///
|
2023-12-09 12:29:32 +00:00
|
|
|
/// 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
|
|
|
|
///
|
2023-12-08 19:21:28 +00:00
|
|
|
/// * `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,
|
2023-12-08 19:21:28 +00:00
|
|
|
room_jid: BareJid,
|
|
|
|
nickname: RoomNick,
|
2023-12-08 19:02:52 +00:00
|
|
|
lang: impl Into<String>,
|
|
|
|
status: impl Into<String>,
|
|
|
|
) {
|
2023-12-29 23:28:57 +00:00
|
|
|
muc::room::leave_room(self, room_jid, nickname, lang, status).await
|
2023-12-08 18:38:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 18:57:59 +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_;
|
2019-10-22 23:32:41 +00:00
|
|
|
message
|
|
|
|
.bodies
|
|
|
|
.insert(String::from(lang), Body(String::from(text)));
|
2020-03-08 18:57:59 +00:00
|
|
|
let _ = self.client.send_stanza(message.into()).await;
|
|
|
|
}
|
|
|
|
|
2023-06-07 14:38:03 +00:00
|
|
|
pub async fn send_room_private_message(
|
|
|
|
&mut self,
|
|
|
|
room: BareJid,
|
|
|
|
recipient: RoomNick,
|
|
|
|
lang: &str,
|
|
|
|
text: &str,
|
|
|
|
) {
|
2023-12-29 23:33:04 +00:00
|
|
|
muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
|
2023-06-07 14:38:03 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2020-03-08 18:57:59 +00:00
|
|
|
pub async fn wait_for_events(&mut self) -> Option<Vec<Event>> {
|
2023-12-29 23:13:25 +00:00
|
|
|
event_loop::wait_for_events(self).await
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
2021-12-26 23:48:37 +00:00
|
|
|
|
|
|
|
pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
|
2023-12-29 23:06:11 +00:00
|
|
|
upload::send::upload_file_with(self, service, path).await
|
2021-12-26 23:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 21:46:50 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-06-01 09:58:04 +00:00
|
|
|
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;
|
2019-10-22 21:46:50 +00:00
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_simple() {
|
2023-06-01 09:58:04 +00:00
|
|
|
let jid = BareJid::from_str("foo@bar").unwrap();
|
|
|
|
|
|
|
|
let client = TokioXmppClient::new(jid.clone(), "meh");
|
2019-10-22 21:46:50 +00:00
|
|
|
|
|
|
|
// Client instance
|
2023-06-01 09:58:04 +00:00
|
|
|
let client_builder = ClientBuilder::new(jid, "meh")
|
2019-10-22 21:46:50 +00:00
|
|
|
.set_client(ClientType::Bot, "xmpp-rs")
|
|
|
|
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
|
|
|
.set_default_nick("bot")
|
|
|
|
.enable_feature(ClientFeature::ContactList);
|
|
|
|
|
2023-08-17 20:29:04 +00:00
|
|
|
#[cfg(feature = "avatars")]
|
|
|
|
let client_builder = client_builder.enable_feature(ClientFeature::Avatars);
|
|
|
|
|
2023-06-01 09:58:04 +00:00
|
|
|
let mut agent: Agent = client_builder.build_impl(client);
|
2019-10-22 21:46:50 +00:00
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
while let Some(events) = agent.wait_for_events().await {
|
|
|
|
assert!(match events[0] {
|
2023-12-11 12:48:39 +00:00
|
|
|
Event::Disconnected(_) => true,
|
2020-03-08 18:57:59 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
assert_eq!(events.len(), 1);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-22 21:46:50 +00:00
|
|
|
}
|
|
|
|
}
|