1use 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 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 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 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 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 pub fn bound_jid(&self) -> Option<&Jid> {
115 self.client.bound_jid()
116 }
117}