From e22e755bf5c5f291925d92decace6d8a33c39278 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Wed, 20 Mar 2024 19:08:01 -0400 Subject: [PATCH 1/2] other bots cant run botcommands --- .cargo/config.toml | 4 ++++ Cargo.toml | 5 +++-- README.md | 7 +++++++ src/core/identity.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..2bcdad5 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,4 @@ + +[env] +# Based on https://doc.rust-lang.org/cargo/reference/config.html +OtherBots = "Supibot,buttsbot,PotatBotat,StreamElements" diff --git a/Cargo.toml b/Cargo.toml index 5b20c49..87f8cfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,8 @@ futures = "0.3" async-trait = "0.1.77" casual_logger = "0.6.5" - [lib] name = "bot_lib" -path = "src/lib.rs" \ No newline at end of file +path = "src/lib.rs" + + diff --git a/README.md b/README.md index c736061..3b200de 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,11 @@ login_name = access_token = bot_channels = , prefix = +``` + + +2. If required, adjust the following additional `.cargo\config.toml` configurations : +``` +[env] +OtherBots = # Other Bots for special handling (e.g., bot commands cant be ran by bots) ``` \ No newline at end of file diff --git a/src/core/identity.rs b/src/core/identity.rs index 877fb6c..5cdfd5f 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -12,11 +12,40 @@ use crate::core::botinstance::ChType; use crate::core::botlog; use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, ModulesManager}; +use dotenv::dotenv; +use std::env; + fn adminvector() -> Vec { vec![String::from("ModulatingForce")] //vec![] } + +pub fn otherbots_vector() -> Vec { + // vec![String::from("ModulatingForce")] + // //vec![] + + dotenv().ok(); + let mut other_bots = Vec::new(); + + if let Ok(value) = env::var("OtherBots") { + for bot in value.split(',') { + other_bots.push(String::from(bot).to_lowercase()) + } + } + + botlog::debug( + &format!( + "OtherBots : {:?}",other_bots, + ), + Some("identity.rs > otherbots_vector()".to_string()), + None, + ); + + other_bots +} + + pub async fn init(mgr: Arc) { botlog::trace( "Went into Identity Module init", @@ -780,6 +809,24 @@ impl IdentityManager { let usr = usr.to_lowercase(); + + let bot_vector = otherbots_vector() ; // result of pulling from Cargo.toml + + botlog::trace( + &format!( + "Checking user is part of known bots: bot_vector.contains(&usr) : {:?}",bot_vector.contains(&usr) + ), + Some("identity.rs > can_user_run()".to_string()), + None, + ); + + if bot_vector.contains(&usr) { + return ( + Permissible::Block, + ChangeResult::NoChange("Other Bots Cannot Run Commands".to_string()), + ); + } + // if cmdreqroles.len() == 0 { if cmdreqroles.is_empty() { // return Ok(Permissible::Allow) -- 2.46.1 From e86fa4884bab2e252072dc513d7b94e4d671bec0 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Wed, 20 Mar 2024 19:26:04 -0400 Subject: [PATCH 2/2] otherbots unit test --- src/core/identity.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/identity.rs b/src/core/identity.rs index 5cdfd5f..f5e710b 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -578,7 +578,7 @@ pub enum UserRole { Broadcaster, BotAdmin, } - +#[derive(Debug, PartialEq, Eq)] pub enum Permissible { Allow, Block, @@ -1429,6 +1429,34 @@ mod core_identity { ); } + + #[tokio::test] + async fn otherbots_checks() { + Log::set_file_ext(Extension::Log); + + let mut test_id_mgr = IdentityManager::init(); + + for bot in otherbots_vector() { + + let (usr, channelname, chat_badge, cmdreqroles) = ( + bot, + ChType::Channel("twitchchanneltest".to_string()), + None, + vec![] + ); + + let rslt = test_id_mgr.can_user_run(usr, channelname, chat_badge, cmdreqroles).await; + + assert_eq!( + (Permissible::Block, + ChangeResult::NoChange("Other Bots Cannot Run Commands".to_string())), + rslt + ); + + } + + } + #[tokio::test] async fn promote_workflow_01() { Log::set_file_ext(Extension::Log); -- 2.46.1