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 /// Request to leave a chatroom.
43 ///
44 /// If successful, an [Event::RoomLeft] event will be produced. This method does not remove the room
45 /// from bookmarks nor remove the autojoin flag. See [muc::room::leave_room] for more information.
46 ///
47 /// # Arguments
48 ///
49 /// * `room_jid`: The JID of the room to leave.
50 /// * `nickname`: The nickname to use in the room.
51 /// * `lang`: The language of the status message (empty string when unknown).
52 /// * `status`: The status message to send.
53 pub async fn leave_room(
54 &mut self,
55 room_jid: BareJid,
56 nickname: RoomNick,
57 lang: impl Into<String>,
58 status: impl Into<String>,
59 ) {
60 muc::room::leave_room(self, room_jid, nickname, lang, status).await
61 }
62
63 pub async fn send_message(
64 &mut self,
65 recipient: Jid,
66 type_: MessageType,
67 lang: &str,
68 text: &str,
69 ) {
70 message::send::send_message(self, recipient, type_, lang, text).await
71 }
72
73 pub async fn send_room_private_message(
74 &mut self,
75 room: BareJid,
76 recipient: RoomNick,
77 lang: &str,
78 text: &str,
79 ) {
80 muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
81 }
82
83 /// Wait for new events.
84 ///
85 /// # Returns
86 ///
87 /// - `Some(events)` if there are new events; multiple may be returned at once.
88 /// - `None` if the underlying stream is closed.
89 pub async fn wait_for_events(&mut self) -> Option<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}