2023-12-29 23:43:10 +00:00
|
|
|
// Copyright (c) 2023 xmpp-rs contributors.
|
|
|
|
//
|
|
|
|
// 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/.
|
|
|
|
|
2024-12-16 17:43:05 +00:00
|
|
|
use std::collections::HashMap;
|
2023-12-29 23:43:10 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-08-06 14:04:04 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::RwLock;
|
2023-12-29 23:43:10 +00:00
|
|
|
pub use tokio_xmpp::parsers;
|
|
|
|
use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
|
2024-07-24 18:52:10 +00:00
|
|
|
pub use tokio_xmpp::{
|
2024-12-17 16:21:08 +00:00
|
|
|
jid::{BareJid, FullJid, Jid, ResourcePart},
|
2024-07-24 18:52:10 +00:00
|
|
|
minidom::Element,
|
2024-08-06 19:00:11 +00:00
|
|
|
Client as TokioXmppClient,
|
2024-07-24 18:52:10 +00:00
|
|
|
};
|
2023-12-29 23:43:10 +00:00
|
|
|
|
2023-12-31 06:17:29 +00:00
|
|
|
use crate::{event_loop, message, muc, upload, Error, Event, RoomNick};
|
2023-12-29 23:43:10 +00:00
|
|
|
|
2024-08-20 14:54:48 +00:00
|
|
|
pub struct Agent {
|
|
|
|
pub(crate) client: TokioXmppClient,
|
2024-12-17 16:21:08 +00:00
|
|
|
pub(crate) default_nick: Arc<RwLock<ResourcePart>>,
|
2023-12-29 23:43:10 +00:00
|
|
|
pub(crate) lang: Arc<Vec<String>>,
|
|
|
|
pub(crate) disco: DiscoInfoResult,
|
|
|
|
pub(crate) node: String,
|
|
|
|
pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
|
|
|
|
pub(crate) awaiting_disco_bookmarks_type: bool,
|
2024-12-16 17:43:05 +00:00
|
|
|
// Mapping of room->nick
|
2024-12-17 16:21:08 +00:00
|
|
|
pub(crate) rooms_joined: HashMap<BareJid, ResourcePart>,
|
|
|
|
pub(crate) rooms_joining: HashMap<BareJid, ResourcePart>,
|
|
|
|
pub(crate) rooms_leaving: HashMap<BareJid, ResourcePart>,
|
2023-12-29 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 14:54:48 +00:00
|
|
|
impl Agent {
|
|
|
|
pub async fn disconnect(self) -> Result<(), Error> {
|
2023-12-29 23:43:10 +00:00
|
|
|
self.client.send_end().await
|
|
|
|
}
|
|
|
|
|
2024-12-16 19:19:51 +00:00
|
|
|
pub async fn join_room<'a>(&mut self, settings: muc::room::JoinRoomSettings<'a>) {
|
|
|
|
muc::room::join_room(self, settings).await
|
2023-12-29 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 17:18:58 +00:00
|
|
|
/// Request to leave a chatroom.
|
2023-12-29 23:43:10 +00:00
|
|
|
///
|
2024-01-27 17:18:58 +00:00
|
|
|
/// If successful, an [Event::RoomLeft] event will be produced. This method does not remove the room
|
|
|
|
/// from bookmarks nor remove the autojoin flag. See [muc::room::leave_room] for more information.
|
2024-12-16 19:19:51 +00:00
|
|
|
pub async fn leave_room<'a>(&mut self, settings: muc::room::LeaveRoomSettings<'a>) {
|
|
|
|
muc::room::leave_room(self, settings).await
|
2023-12-29 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_message(
|
|
|
|
&mut self,
|
|
|
|
recipient: Jid,
|
|
|
|
type_: MessageType,
|
|
|
|
lang: &str,
|
|
|
|
text: &str,
|
|
|
|
) {
|
|
|
|
message::send::send_message(self, recipient, type_, lang, text).await
|
|
|
|
}
|
|
|
|
|
2024-12-16 19:19:51 +00:00
|
|
|
pub async fn send_room_message<'a>(&mut self, settings: muc::room::RoomMessageSettings<'a>) {
|
|
|
|
muc::room::send_room_message(self, settings).await
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:43:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-08 13:14:57 +00:00
|
|
|
/// Wait for new events, or Error::Disconnected when connection is closed and will not reconnect.
|
|
|
|
pub async fn wait_for_events(&mut self) -> Vec<Event> {
|
2023-12-29 23:43:10 +00:00
|
|
|
event_loop::wait_for_events(self).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
|
|
|
|
upload::send::upload_file_with(self, service, path).await
|
|
|
|
}
|
2023-12-26 16:44:37 +00:00
|
|
|
|
|
|
|
/// Get the bound jid of the client.
|
|
|
|
///
|
|
|
|
/// If the client is not connected, this will be None.
|
|
|
|
pub fn bound_jid(&self) -> Option<&Jid> {
|
|
|
|
self.client.bound_jid()
|
|
|
|
}
|
2023-12-29 23:43:10 +00:00
|
|
|
}
|