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