1use alloc::sync::Arc;
8use std::collections::HashMap;
9use std::path::{Path, PathBuf};
10use tokio::sync::RwLock;
11
12use crate::{
13 event_loop,
14 jid::{BareJid, Jid},
15 message, muc,
16 parsers::disco::DiscoInfoResult,
17 upload, Error, Event, RoomNick,
18};
19use tokio_xmpp::Client as TokioXmppClient;
20
21pub struct Agent {
22 pub(crate) client: TokioXmppClient,
23 pub(crate) default_nick: Arc<RwLock<RoomNick>>,
24 pub(crate) lang: Arc<Vec<String>>,
25 pub(crate) disco: DiscoInfoResult,
26 pub(crate) node: String,
27 pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
28 pub(crate) awaiting_disco_bookmarks_type: bool,
29 pub(crate) rooms_joined: HashMap<BareJid, RoomNick>,
31 pub(crate) rooms_joining: HashMap<BareJid, RoomNick>,
32 pub(crate) rooms_leaving: HashMap<BareJid, RoomNick>,
33}
34
35impl Agent {
36 pub fn new(
37 client: TokioXmppClient,
38 default_nick: RoomNick,
39 lang: Vec<String>,
40 disco: DiscoInfoResult,
41 node: String,
42 ) -> Agent {
43 Agent {
44 client,
45 default_nick: Arc::new(RwLock::new(default_nick)),
46 lang: Arc::new(lang),
47 disco,
48 node,
49 uploads: Vec::new(),
50 awaiting_disco_bookmarks_type: false,
51 rooms_joined: HashMap::new(),
52 rooms_joining: HashMap::new(),
53 rooms_leaving: HashMap::new(),
54 }
55 }
56
57 pub async fn disconnect(self) -> Result<(), Error> {
58 self.client.send_end().await
59 }
60
61 pub async fn join_room<'a>(&mut self, settings: muc::room::JoinRoomSettings<'a>) {
62 muc::room::join_room(self, settings).await
63 }
64
65 pub async fn leave_room<'a>(&mut self, settings: muc::room::LeaveRoomSettings<'a>) {
70 muc::room::leave_room(self, settings).await
71 }
72
73 pub async fn send_raw_message<'a>(&mut self, settings: message::send::RawMessageSettings<'a>) {
74 message::send::send_raw_message(self, settings).await
75 }
76
77 pub async fn send_message<'a>(&mut self, settings: message::send::MessageSettings<'a>) {
78 message::send::send_message(self, settings).await
79 }
80
81 pub async fn send_room_message<'a>(&mut self, settings: muc::room::RoomMessageSettings<'a>) {
82 muc::room::send_room_message(self, settings).await
83 }
84
85 pub async fn send_room_private_message<'a>(
86 &mut self,
87 settings: muc::private_message::RoomPrivateMessageSettings<'a>,
88 ) {
89 muc::private_message::send_room_private_message(self, settings).await
90 }
91
92 pub async fn wait_for_events(&mut self) -> Vec<Event> {
94 event_loop::wait_for_events(self).await
95 }
96
97 pub async fn upload_file_with(&mut self, service: &str, path: &Path) {
98 upload::send::upload_file_with(self, service, path).await
99 }
100
101 pub fn bound_jid(&self) -> Option<&Jid> {
105 self.client.bound_jid()
106 }
107}