From e22daa91f1ac7cfab181b906be985b6c2779cf14 Mon Sep 17 00:00:00 2001 From: pep Date: Sun, 16 Nov 2025 22:37:54 +0100 Subject: [PATCH] xmpp: make Agent.config accessible only via getter It may not matter much for the moment as this struct shouldn't change very much during the life of the client, but this prevents the lock from being held too long. Signed-off-by: pep --- xmpp/src/agent.rs | 6 +++++- xmpp/src/config.rs | 4 ++-- xmpp/src/event_loop.rs | 2 +- xmpp/src/message/receive/chat.rs | 2 +- xmpp/src/message/receive/group_chat.rs | 2 +- xmpp/src/muc/room.rs | 3 +-- xmpp/src/pubsub/mod.rs | 10 ++++++---- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/xmpp/src/agent.rs b/xmpp/src/agent.rs index 56e57c621f13b8c45d71506f753288f0271636c0..6b52682292fb8b16d20965ac7f39e5832fb9b0cb 100644 --- a/xmpp/src/agent.rs +++ b/xmpp/src/agent.rs @@ -24,7 +24,7 @@ use tokio_xmpp::{Stanza, stanzastream::StanzaToken}; pub struct Agent { pub(crate) client: TokioXmppClient, - pub(crate) config: Arc>, + config: Arc>, pub(crate) disco: DiscoInfoResult, pub(crate) uploads: Vec<(String, Jid, PathBuf)>, pub(crate) awaiting_disco_bookmarks_type: bool, @@ -55,6 +55,10 @@ impl Agent { *c = config; } + pub async fn get_config(&self) -> Config { + self.config.read().await.clone() + } + pub async fn disconnect(self) -> Result<(), Error> { self.client.send_end().await } diff --git a/xmpp/src/config.rs b/xmpp/src/config.rs index 2d46ba9a72debf34d9a7e9ea77a349e98e364f00..d81408f1ff8f5f306219b9e30f9cd4725210920e 100644 --- a/xmpp/src/config.rs +++ b/xmpp/src/config.rs @@ -8,7 +8,7 @@ use crate::RoomNick; use core::str::FromStr; /// [Disco](https://xmpp.org/registrar/disco-categories.html#client) identity type -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum ClientType { Bot, Pc, @@ -30,7 +30,7 @@ impl ToString for ClientType { } /// Store Agent configuration. Differs from state which is generated at runtime -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Config { /// Synchronize bookmarks based on autojoin flag. /// The client will join and leave based on the value of the `autojoin` flag on the (pubsub) diff --git a/xmpp/src/event_loop.rs b/xmpp/src/event_loop.rs index 40e882f3302938995ea1c80ba03edd9ded91fd4d..32f26a85bf3605f7303122bd82d0bfceed7c5ef1 100644 --- a/xmpp/src/event_loop.rs +++ b/xmpp/src/event_loop.rs @@ -21,7 +21,7 @@ pub async fn wait_for_events(agent: &mut Agent) -> Vec { TokioXmppEvent::Online { resumed: false, .. } => { let presence = presence::send::make_initial_presence( &agent.disco, - &agent.config.read().await.website, + &agent.get_config().await.website, ) .into(); let _ = agent.client.send_stanza(presence).await; diff --git a/xmpp/src/message/receive/chat.rs b/xmpp/src/message/receive/chat.rs index dc887598cc06610a221edfe31025995f59d45eb1..78d7ba4321006cdba2c977de431ad1ffe4a8492e 100644 --- a/xmpp/src/message/receive/chat.rs +++ b/xmpp/src/message/receive/chat.rs @@ -18,7 +18,7 @@ pub async fn handle_message_chat( message: &mut Message, time_info: StanzaTimeInfo, ) { - let config = agent.config.read().await; + let config = agent.get_config().await; let langs: Vec<&str> = config.lang.iter().map(String::as_str).collect(); let confirm = message.extract_valid_payload::(); diff --git a/xmpp/src/message/receive/group_chat.rs b/xmpp/src/message/receive/group_chat.rs index 694ee93d9426c99cce3e42d41d13b95d0086e5e0..a238b274e1bebc5a3d7fb5ae3668b4edf20c62b0 100644 --- a/xmpp/src/message/receive/group_chat.rs +++ b/xmpp/src/message/receive/group_chat.rs @@ -18,7 +18,7 @@ pub async fn handle_message_group_chat( message: &mut Message, time_info: StanzaTimeInfo, ) { - let config = agent.config.read().await; + let config = agent.get_config().await; let langs: Vec<&str> = config.lang.iter().map(String::as_str).collect(); let mut found_subject = false; diff --git a/xmpp/src/muc/room.rs b/xmpp/src/muc/room.rs index 577b5700be6b3eada2af2f6db57a3e1432f78ef8..d21390b0f50e4d2c07b59d47a344ea80e6f0d893 100644 --- a/xmpp/src/muc/room.rs +++ b/xmpp/src/muc/room.rs @@ -78,8 +78,7 @@ pub async fn join_room<'a>(agent: &mut Agent, settings: JoinRoomSettings<'a>) { let nick = if let Some(nick) = nick { nick } else { - let config = agent.config.read().await; - config.default_nick.clone() + agent.get_config().await.default_nick }; let room_jid = room.with_resource(&nick); diff --git a/xmpp/src/pubsub/mod.rs b/xmpp/src/pubsub/mod.rs index e18523c068b49987995a86306290b4f9a01786fe..84d73ab098984d4a7d3471b165f853557eb16f6d 100644 --- a/xmpp/src/pubsub/mod.rs +++ b/xmpp/src/pubsub/mod.rs @@ -50,6 +50,7 @@ pub(crate) async fn handle_event( events.extend(new_events); } ref node if node == ns::BOOKMARKS2 => { + let config = agent.get_config().await; // TODO: Check that our bare JID is the sender. if let [item] = &published[..] { let jid = BareJid::from_str(&item.id.clone().unwrap().0).unwrap(); @@ -67,7 +68,7 @@ pub(crate) async fn handle_event( }) .await; } else { - if agent.config.read().await.bookmarks_autojoin { + if config.bookmarks_autojoin { // So maybe another client of ours left the room... let's leave it too agent.leave_room(LeaveRoomSettings::new(jid)).await; } @@ -77,7 +78,7 @@ pub(crate) async fn handle_event( Err(err) => println!("not bookmark: {}", err), } } else if let [item] = &retracted[..] { - if agent.config.read().await.bookmarks_autojoin { + if config.bookmarks_autojoin { let jid = BareJid::from_str(&item.0).unwrap(); agent.leave_room(LeaveRoomSettings::new(jid)).await; @@ -125,6 +126,7 @@ pub(crate) async fn handle_iq_result( events.extend(new_events); } ref node if node == ns::BOOKMARKS2 => { + let config = agent.get_config().await; // Keep track of the new added/removed rooms in the bookmarks2 list. // The rooms we joined which are no longer in the list should be left ASAP. let mut new_room_list: Vec = Vec::new(); @@ -150,7 +152,7 @@ pub(crate) async fn handle_iq_result( .await; } } else { - if agent.config.read().await.bookmarks_autojoin { + if config.bookmarks_autojoin { // Leave the room that is no longer autojoin agent.leave_room(LeaveRoomSettings::new(jid)).await; } @@ -162,7 +164,7 @@ pub(crate) async fn handle_iq_result( } } - if agent.config.read().await.bookmarks_autojoin { + if config.bookmarks_autojoin { // Now we leave the rooms that are no longer in the bookmarks let mut rooms_to_leave: Vec = Vec::new(); for (room, _nick) in &agent.rooms_joined {