1// Copyright (c) 2023 xmpp-rs contributors.
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
7use std::path::{Path, PathBuf};
8use std::sync::Arc;
9use tokio::sync::RwLock;
10use tokio_xmpp::connect::ServerConnector;
11pub use tokio_xmpp::parsers;
12use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
13pub use tokio_xmpp::{
14 jid::{BareJid, FullJid, Jid},
15 minidom::Element,
16 Client as TokioXmppClient,
17};
18
19use crate::{event_loop, message, muc, upload, Error, Event, RoomNick};
20
21pub struct Agent<C: ServerConnector> {
22 pub(crate) client: TokioXmppClient<C>,
23 pub(crate) default_nick: Arc<RwLock<String>>,
24 pub(crate) lang: Arc<Vec<String>>,
25 pub(crate) disco: DiscoInfoResult,
26 pub(crate) node: String,
27 pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
28 pub(crate) awaiting_disco_bookmarks_type: bool,
29}
30
31impl<C: ServerConnector> Agent<C> {
32 pub async fn disconnect(&mut self) -> Result<(), Error> {
33 self.client.send_end().await
34 }
35
36 pub async fn join_room(
37 &mut self,
38 room: BareJid,
39 nick: Option<String>,
40 password: Option<String>,
41 lang: &str,
42 status: &str,
43 ) {
44 muc::room::join_room(self, room, nick, password, lang, status).await
45 }
46
47 /// Request to leave a chatroom.
48 ///
49 /// If successful, an [Event::RoomLeft] event will be produced. This method does not remove the room
50 /// from bookmarks nor remove the autojoin flag. See [muc::room::leave_room] for more information.
51 ///
52 /// # Arguments
53 ///
54 /// * `room_jid`: The JID of the room to leave.
55 /// * `nickname`: The nickname to use in the room.
56 /// * `lang`: The language of the status message (empty string when unknown).
57 /// * `status`: The status message to send.
58 pub async fn leave_room(
59 &mut self,
60 room_jid: BareJid,
61 nickname: RoomNick,
62 lang: impl Into<String>,
63 status: impl Into<String>,
64 ) {
65 muc::room::leave_room(self, room_jid, nickname, lang, status).await
66 }
67
68 pub async fn send_message(
69 &mut self,
70 recipient: Jid,
71 type_: MessageType,
72 lang: &str,
73 text: &str,
74 ) {
75 message::send::send_message(self, recipient, type_, lang, text).await
76 }
77
78 pub async fn send_room_private_message(
79 &mut self,
80 room: BareJid,
81 recipient: RoomNick,
82 lang: &str,
83 text: &str,
84 ) {
85 muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
86 }
87
88 /// Wait for new events, or Error::Disconnected when connection is closed and will not reconnect.
89 pub async fn wait_for_events(&mut self) -> Vec<Event> {
90 event_loop::wait_for_events(self).await
91 }
92
93 pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
94 upload::send::upload_file_with(self, service, path).await
95 }
96
97 /// Get the bound jid of the client.
98 ///
99 /// If the client is not connected, this will be None.
100 pub fn bound_jid(&self) -> Option<&Jid> {
101 self.client.bound_jid()
102 }
103}