bhcli

"Strange's fork of n0tr1v's bhcli (onion)"
git clone https://git.dasho.dev/Strange/bhcli.git
Log | Files | Refs | README

commit 762be95965c970a8f6bc10e0f4706936072b9ca9
parent aaa849da8d9f6ebc6c35abffc1566e9e8273a6c6
Author: Strange <StrangeGuy6228@protonmail.com>
Date:   Sat, 30 Mar 2024 02:48:52 +0530

added custom commands read from config file

Diffstat:
Msrc/main.rs | 88++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 87 insertions(+), 1 deletion(-)

diff --git a/src/main.rs b/src/main.rs @@ -1424,6 +1424,28 @@ impl LeChatPHPClient { let input: String = app.input.drain(..).collect(); app.input_idx = 0; + + if app.commands.commands.is_empty() { + log::error!("Commands hashmap is empty!"); + } else { + for (command, action) in &app.commands.commands { + log::error!("command: {}, action: {}", command, action); + } + } + + // Iterate over commands and execute associated actions + for (command, action) in &app.commands.commands { + log::error!("command :{} action :{}", command, action); + let expected_input = format!("!{}", command); + if input == expected_input { + log::error!("inside if"); + // Execute the action by posting a message + self.post_msg(PostType::Post(action.clone(), None)).unwrap(); + // Return Ok(()) if the action is executed successfully + return Ok(()); + } + } + if input == "/dl" { // Delete last message self.post_msg(PostType::DeleteLast).unwrap(); @@ -1527,7 +1549,7 @@ impl LeChatPHPClient { "This is your warning - {}, will be kicked next !rules", msg ); - // log::error!("The Strange end_msg is :{}",end_msg); + log::error!("The Strange end_msg is :{}", end_msg); self.post_msg(PostType::Post(end_msg, None)).unwrap(); } else { if input.starts_with("/") && !input.starts_with("/me ") { @@ -2479,6 +2501,38 @@ fn start_dnmx_mail_notifier(client: &Client, username: &str, password: &str) { }); } +//Strange +#[derive(Debug, Deserialize)] +struct Commands { + commands: HashMap<String, String>, +} + +impl Default for Commands { + fn default() -> Self { + Commands { + commands: HashMap::new(), // Initialize commands with empty HashMap + } + } +} + +// Strange +// Function to read the configuration file and parse it +// Function to read the configuration file and parse it +fn read_commands_file(file_path: &str) -> Result<Commands, Box<dyn std::error::Error>> { + // Read the contents of the file + let commands_content = std::fs::read_to_string(file_path)?; + // log::error!("Read file contents: {}", commands_content); + + // Deserialize the contents into a Commands struct + let commands: Commands = toml::from_str(&commands_content)?; + // log::error!( + // "Deserialized file contents into Commands struct: {:?}", + // commands + // ); + + Ok(commands) +} + fn main() -> anyhow::Result<()> { let mut opts: Opts = Opts::parse(); @@ -3283,10 +3337,41 @@ struct App { members_tag: String, staffs_tag: String, long_message: Option<Message>, + commands: Commands, } impl Default for App { fn default() -> App { + // Read commands from the file and set them as default values + let commands = if let Ok(config_path) = confy::get_configuration_file_path("bhcli", None) { + if let Some(config_path_str) = config_path.to_str() { + match read_commands_file(config_path_str) { + Ok(commands) => commands, + Err(err) => { + log::error!( + "Failed to read commands from config file - {} : +{}", + config_path_str, + err + ); + Commands { + commands: HashMap::new(), + } + } + } + } else { + log::error!("Failed to convert configuration file path to string."); + Commands { + commands: HashMap::new(), + } + } + } else { + log::error!("Failed to get configuration file path."); + Commands { + commands: HashMap::new(), + } + }; + App { input: String::new(), input_idx: 0, @@ -3301,6 +3386,7 @@ impl Default for App { members_tag: "".to_owned(), staffs_tag: "".to_owned(), long_message: None, + commands, } } }