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::{AsyncClient as TokioXmppClient, BareJid, Element, FullJid, Jid};
13
14use crate::{event_loop, message, muc, upload, Error, Event, RoomNick};
15
16pub struct Agent<C: ServerConnector> {
17 pub(crate) client: TokioXmppClient<C>,
18 pub(crate) default_nick: Arc<RwLock<String>>,
19 pub(crate) lang: Arc<Vec<String>>,
20 pub(crate) disco: DiscoInfoResult,
21 pub(crate) node: String,
22 pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
23 pub(crate) awaiting_disco_bookmarks_type: bool,
24}
25
26impl<C: ServerConnector> Agent<C> {
27 pub async fn disconnect(&mut self) -> Result<(), Error> {
28 self.client.send_end().await
29 }
30
31 pub async fn join_room(
32 &mut self,
33 room: BareJid,
34 nick: Option<String>,
35 password: Option<String>,
36 lang: &str,
37 status: &str,
38 ) {
39 muc::room::join_room(self, room, nick, password, lang, status).await
40 }
41
42 /// Send a "leave room" request to the server (specifically, an "unavailable" presence stanza).
43 ///
44 /// The returned future will resolve when the request has been sent,
45 /// not when the room has actually been left.
46 ///
47 /// If successful, a `RoomLeft` event should be received later as a confirmation.
48 ///
49 /// See: https://xmpp.org/extensions/xep-0045.html#exit
50 ///
51 /// Note that this method does NOT remove the room from the auto-join list; the latter
52 /// is more a list of bookmarks that the account knows about and that have a flag set
53 /// to indicate that they should be joined automatically after connecting (see the JoinRoom event).
54 ///
55 /// Regarding the latter, see the these minutes about auto-join behavior:
56 /// https://docs.modernxmpp.org/meetings/2019-01-brussels/#bookmarks
57 ///
58 /// # Arguments
59 ///
60 /// * `room_jid`: The JID of the room to leave.
61 /// * `nickname`: The nickname to use in the room.
62 /// * `lang`: The language of the status message.
63 /// * `status`: The status message to send.
64 pub async fn leave_room(
65 &mut self,
66 room_jid: BareJid,
67 nickname: RoomNick,
68 lang: impl Into<String>,
69 status: impl Into<String>,
70 ) {
71 muc::room::leave_room(self, room_jid, nickname, lang, status).await
72 }
73
74 pub async fn send_message(
75 &mut self,
76 recipient: Jid,
77 type_: MessageType,
78 lang: &str,
79 text: &str,
80 ) {
81 message::send::send_message(self, recipient, type_, lang, text).await
82 }
83
84 pub async fn send_room_private_message(
85 &mut self,
86 room: BareJid,
87 recipient: RoomNick,
88 lang: &str,
89 text: &str,
90 ) {
91 muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
92 }
93
94 /// Wait for new events.
95 ///
96 /// # Returns
97 ///
98 /// - `Some(events)` if there are new events; multiple may be returned at once.
99 /// - `None` if the underlying stream is closed.
100 pub async fn wait_for_events(&mut self) -> Option<Vec<Event>> {
101 event_loop::wait_for_events(self).await
102 }
103
104 pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
105 upload::send::upload_file_with(self, service, path).await
106 }
107
108 /// Get the bound jid of the client.
109 ///
110 /// If the client is not connected, this will be None.
111 pub fn bound_jid(&self) -> Option<&Jid> {
112 self.client.bound_jid()
113 }
114}