xmpp/
agent.rs

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 alloc::sync::Arc;
8use std::collections::HashMap;
9use std::path::{Path, PathBuf};
10#[cfg(feature = "escape-hatch")]
11use tokio::io;
12use tokio::sync::RwLock;
13
14use crate::{
15    Config, Error, Event, RoomNick, event_loop,
16    jid::{BareJid, Jid},
17    message, muc,
18    parsers::disco::DiscoInfoResult,
19    upload,
20};
21use tokio_xmpp::Client as TokioXmppClient;
22#[cfg(feature = "escape-hatch")]
23use tokio_xmpp::{Stanza, stanzastream::StanzaToken};
24
25pub struct Agent {
26    pub(crate) client: TokioXmppClient,
27    config: Arc<RwLock<Config>>,
28    pub(crate) disco: DiscoInfoResult,
29    pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
30    pub(crate) awaiting_disco_bookmarks_type: bool,
31    // Mapping of room->nick
32    pub(crate) rooms_joined: HashMap<BareJid, RoomNick>,
33    pub(crate) rooms_joining: HashMap<BareJid, RoomNick>,
34    pub(crate) rooms_leaving: HashMap<BareJid, RoomNick>,
35}
36
37impl Agent {
38    pub fn new(client: TokioXmppClient, config: Config, disco: DiscoInfoResult) -> Agent {
39        Agent {
40            client,
41            config: Arc::new(RwLock::new(config)),
42            disco,
43            uploads: Vec::new(),
44            awaiting_disco_bookmarks_type: false,
45            rooms_joined: HashMap::new(),
46            rooms_joining: HashMap::new(),
47            rooms_leaving: HashMap::new(),
48        }
49    }
50
51    /// Reset the agent configuration to the provided Config struct
52    // TODO: refresh everything that is affected by this reset?
53    pub async fn set_config(&mut self, config: Config) {
54        let mut c = self.config.write().await;
55        *c = config;
56    }
57
58    pub async fn get_config(&self) -> Config {
59        self.config.read().await.clone()
60    }
61
62    pub async fn disconnect(self) -> Result<(), Error> {
63        self.client.send_end().await
64    }
65
66    #[cfg(feature = "escape-hatch")]
67    pub async fn send_stanza<S: Into<Stanza>>(&mut self, st: S) -> Result<StanzaToken, io::Error> {
68        self.client.send_stanza(st.into()).await
69    }
70
71    pub async fn join_room<'a>(&mut self, settings: muc::room::JoinRoomSettings<'a>) {
72        muc::room::join_room(self, settings).await
73    }
74
75    /// Request to leave a chatroom.
76    ///
77    /// If successful, an [Event::RoomLeft] event will be produced. This method does not remove the room
78    /// from bookmarks nor remove the autojoin flag. See [muc::room::leave_room] for more information.
79    pub async fn leave_room<'a>(&mut self, settings: muc::room::LeaveRoomSettings<'a>) {
80        muc::room::leave_room(self, settings).await
81    }
82
83    pub async fn send_raw_message<'a>(&mut self, settings: message::send::RawMessageSettings<'a>) {
84        message::send::send_raw_message(self, settings).await
85    }
86
87    pub async fn send_message<'a>(&mut self, settings: message::send::MessageSettings<'a>) {
88        message::send::send_message(self, settings).await
89    }
90
91    pub async fn send_room_message<'a>(&mut self, settings: muc::room::RoomMessageSettings<'a>) {
92        muc::room::send_room_message(self, settings).await
93    }
94
95    pub async fn send_room_private_message<'a>(
96        &mut self,
97        settings: muc::private_message::RoomPrivateMessageSettings<'a>,
98    ) {
99        muc::private_message::send_room_private_message(self, settings).await
100    }
101
102    /// Wait for new events, or Error::Disconnected when connection is closed and will not reconnect.
103    pub async fn wait_for_events(&mut self) -> Vec<Event> {
104        event_loop::wait_for_events(self).await
105    }
106
107    pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
108        upload::send::upload_file_with(self, service, path).await
109    }
110
111    /// Get the bound jid of the client.
112    ///
113    /// If the client is not connected, this will be None.
114    pub fn bound_jid(&self) -> Option<&Jid> {
115        self.client.bound_jid()
116    }
117}