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