From c051da2f4a161de0974ebb917f7a52d01f5a001f Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 26 Feb 2025 20:30:02 +0200 Subject: [PATCH] Add config setting controlling if a self-introduction message is posted after joining a room Fixes https://github.com/etkecc/baibot/issues/32 --- etc/app/config.yml.dist | 4 ++++ src/bot/implementation.rs | 4 ++++ src/bot/load_config.rs | 3 +++ src/controller/join/mod.rs | 6 ++++++ src/entity/cfg/config.rs | 25 +++++++++++++++++++++++++ src/entity/cfg/defaults.rs | 4 ++++ src/entity/cfg/env.rs | 3 +++ 7 files changed, 49 insertions(+) diff --git a/etc/app/config.yml.dist b/etc/app/config.yml.dist index e5ec30c..68f5149 100644 --- a/etc/app/config.yml.dist +++ b/etc/app/config.yml.dist @@ -32,6 +32,10 @@ user: # Command prefix. Leave empty to use the default (!bai). command_prefix: "!bai" +room: + # Whether the bot should send an introduction message after joining a room. + post_join_self_introduction_enabled: true + access: # Space-separated list of MXID patterns which specify who is an admin. admin_patterns: diff --git a/src/bot/implementation.rs b/src/bot/implementation.rs index aad0e3b..fdae986 100644 --- a/src/bot/implementation.rs +++ b/src/bot/implementation.rs @@ -140,6 +140,10 @@ impl Bot { &self.inner.config.command_prefix } + pub(crate) fn post_join_self_introduction_enabled(&self) -> bool { + self.inner.config.room.post_join_self_introduction_enabled + } + pub(crate) fn homeserver_name(&self) -> &str { &self.inner.config.homeserver.server_name } diff --git a/src/bot/load_config.rs b/src/bot/load_config.rs index d37d3e6..6ece90f 100644 --- a/src/bot/load_config.rs +++ b/src/bot/load_config.rs @@ -35,6 +35,9 @@ pub fn load() -> anyhow::Result { } cfg_env::BAIBOT_USER_NAME => config.user.name = value, cfg_env::BAIBOT_COMMAND_PREFIX => config.command_prefix = value, + cfg_env::BAIBOT_ROOM_POST_JOIN_SELF_INTRODUCTION_ENABLED => { + config.room.post_join_self_introduction_enabled = value.parse::()?; + } cfg_env::BAIBOT_LOGGING => { config.logging = value; } diff --git a/src/controller/join/mod.rs b/src/controller/join/mod.rs index 2818598..bd19e75 100644 --- a/src/controller/join/mod.rs +++ b/src/controller/join/mod.rs @@ -8,6 +8,12 @@ pub async fn handle( room: &mxlink::matrix_sdk::Room, room_config_context: &RoomConfigContext, ) -> anyhow::Result<()> { + if !bot.post_join_self_introduction_enabled() { + tracing::debug!("Post-join self-introduction is disabled - not sending introduction message"); + + return Ok(()); + } + let agent_manager = bot.agent_manager(); bot.messaging() diff --git a/src/entity/cfg/config.rs b/src/entity/cfg/config.rs index 8d1397a..1f899e5 100644 --- a/src/entity/cfg/config.rs +++ b/src/entity/cfg/config.rs @@ -19,6 +19,9 @@ pub struct Config { #[serde(default = "super::defaults::command_prefix")] pub command_prefix: String, + #[serde(default)] + pub room: ConfigRoom, + pub access: ConfigAccess, pub agents: ConfigAgents, @@ -37,6 +40,7 @@ impl Config { self.homeserver.validate()?; self.user.validate()?; self.persistence.validate()?; + self.room.validate()?; self.access.validate()?; if self.command_prefix.is_empty() { @@ -243,6 +247,27 @@ impl PersistenceConfig { } } + +#[derive(Debug, Serialize, Deserialize)] +pub struct ConfigRoom { + #[serde(default = "super::defaults::room_post_join_self_introduction_enabled")] + pub post_join_self_introduction_enabled: bool, +} + +impl ConfigRoom { + pub fn validate(&self) -> anyhow::Result<()> { + Ok(()) + } +} + +impl Default for ConfigRoom { + fn default() -> Self { + Self { + post_join_self_introduction_enabled: super::defaults::room_post_join_self_introduction_enabled(), + } + } +} + #[derive(Debug, Serialize, Deserialize)] pub struct ConfigAccess { // Contains the admin whitelist patterns before parsing into regex. diff --git a/src/entity/cfg/defaults.rs b/src/entity/cfg/defaults.rs index cf5113b..ba7d5fa 100644 --- a/src/entity/cfg/defaults.rs +++ b/src/entity/cfg/defaults.rs @@ -18,6 +18,10 @@ pub(super) fn command_prefix() -> String { COMMAND_PREFIX.to_owned() } +pub(super) fn room_post_join_self_introduction_enabled() -> bool { + true +} + pub(super) fn persistence_data_dir_path() -> Option { None } diff --git a/src/entity/cfg/env.rs b/src/entity/cfg/env.rs index 89f0647..820cef7 100644 --- a/src/entity/cfg/env.rs +++ b/src/entity/cfg/env.rs @@ -11,6 +11,9 @@ pub const BAIBOT_USER_ENCRYPTION_RECOVERY_PASSPHRASE: &str = pub const BAIBOT_COMMAND_PREFIX: &str = "BAIBOT_COMMAND_PREFIX"; +pub const BAIBOT_ROOM_POST_JOIN_SELF_INTRODUCTION_ENABLED: &str = + "BAIBOT_ROOM_POST_JOIN_SELF_INTRODUCTION_ENABLED"; + pub const BAIBOT_LOGGING: &str = "BAIBOT_LOGGING"; pub const BAIBOT_ACCESS_ADMIN_PATTERNS: &str = "BAIBOT_ACCESS_ADMIN_PATTERNS";