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