diff --git a/src/config.rs b/src/config.rs index 5733e42..f25dbef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -138,6 +138,9 @@ pub struct Exprs { /// forgejo-guard configuration #[derive(Deserialize)] pub struct Config { + /// Dry run, without banning the users + #[serde(default)] + pub dry_run: bool, /// Configuration for the forgejo guard itself pub forgejo: Forgejo, /// Configuration of the telegram bot diff --git a/src/main.rs b/src/main.rs index 94c6c14..28cf832 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,7 @@ async fn try_main() -> error::GuardResult<()> { let (sus_sender, sus_receiver) = sync::mpsc::channel::(100); tracing::info!("The instance: {}", config.forgejo.instance); + tracing::info!("Dry run: {}", config.dry_run); tracing::debug!("The config exprs: {:#?}", config.expressions); rust_i18n::set_locale(config.telegram.lang.as_str()); diff --git a/src/telegram_bot/callback_handler.rs b/src/telegram_bot/callback_handler.rs index 69a8adf..4b9d04c 100644 --- a/src/telegram_bot/callback_handler.rs +++ b/src/telegram_bot/callback_handler.rs @@ -30,7 +30,8 @@ use teloxide::{ use crate::{config::Config, forgejo_api}; -/// Inline keyboard with a single button that links to the Forgejo Guardian repository. +/// Inline keyboard with a single button that links to the Forgejo Guardian +/// repository. fn source_inline_keyboard(text: &str) -> InlineKeyboardMarkup { InlineKeyboardMarkup::new([[InlineKeyboardButton::new( text, @@ -58,15 +59,17 @@ pub async fn callback_handler( match command { "b" => { // ban the user - let button_text = if forgejo_api::ban_user( - &Client::new(), - &config.forgejo.instance, - &config.forgejo.token, - data, - ) - .await - .is_ok() + let button_text = if config.dry_run + || forgejo_api::ban_user( + &Client::new(), + &config.forgejo.instance, + &config.forgejo.token, + data, + ) + .await + .is_ok() { + tracing::info!("Suspicious user @{data} has been blocked"); t!("messages.ban_success") } else { t!("messages.ban_failed") diff --git a/src/users_fetcher.rs b/src/users_fetcher.rs index 105e398..2809e52 100644 --- a/src/users_fetcher.rs +++ b/src/users_fetcher.rs @@ -62,6 +62,10 @@ async fn check_new_user( ) { if let Some(re) = config.expressions.ban.is_match(&user) { tracing::info!("@{} has been banned because `{re}`", user.username); + if config.dry_run { + return; + } + if let Err(err) = forgejo_api::ban_user( request_client, &config.forgejo.instance,