diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 0db744d38e7eb2329b7a4e5817d8f9e5a6fb6984..c818e495b36db3c32448daafda39d11fc4ad1213 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -5,10 +5,10 @@ use log::error; use std::{process::abort, string::String}; #[allow(dead_code)] -pub enum CmdHandler { - Simple(fn(Vec<String>) -> bool), - Echo(fn(Vec<String>) -> String), - MultiEcho(fn(Vec<String>) -> Vec<String>), +enum CmdHandler { + Simple(Box<dyn Fn(Vec<String>) -> bool>), + Echo(Box<dyn Fn(Vec<String>) -> String>), + MultiEcho(Box<dyn Fn(Vec<String>) -> Vec<String>>), } #[allow(dead_code)] @@ -31,7 +31,7 @@ impl Cmd { abort(); } - match self.handler { + match &self.handler { CmdHandler::Simple(f) => match f(args) { true => "<b>Ok</b>".to_string(), false => "<b>Failed!</b>".to_string(), @@ -45,4 +45,35 @@ impl Cmd { pub fn get_name(&self) -> &str { &self.name } + + #[allow(dead_code)] + pub fn new_simple(name: String, argc: u32, f: Box<dyn Fn(Vec<String>) -> bool>) -> Cmd { + Cmd { + name: name, + argc: argc, + handler: CmdHandler::Simple(f), + } + } + + #[allow(dead_code)] + pub fn new_echo(name: String, argc: u32, f: Box<dyn Fn(Vec<String>) -> String>) -> Cmd { + Cmd { + name: name, + argc: argc, + handler: CmdHandler::Echo(f), + } + } + + #[allow(dead_code)] + pub fn new_multi_echo( + name: String, + argc: u32, + f: Box<dyn Fn(Vec<String>) -> Vec<String>>, + ) -> Cmd { + Cmd { + name: name, + argc: argc, + handler: CmdHandler::MultiEcho(f), + } + } } diff --git a/src/matrix.rs b/src/matrix.rs index 2b2e4aace47a36f52c10190cf83d441452fee813..371832b666811fff8b4cdfa584513444da553ebe 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, cmd::package::CmdPackage, cmd::{ Cmd, CmdHandler}}; +use crate::{cmd::package::CmdPackage, cmd::Cmd, config::Config}; use futures::future::join_all; use log::{error, info, warn}; use matrix_sdk::{ @@ -16,7 +16,7 @@ use matrix_sdk::{ }, Client, Result, SyncSettings, }; -use std::{convert::TryFrom,process::abort}; +use std::{convert::TryFrom, process::abort}; use tokio::time::{sleep, Duration}; async fn join_room(room: &Invited) { @@ -129,8 +129,19 @@ async fn startup_and_log_rooms(client: &Client) { pub async fn connect_and_handle(config: Config) -> Result<()> { // TODO: Refactor + let username_copy = config.user_name.clone(); let _pkg = match config.section("basic") { - Some(section) => CmdPackage::from_config(§ion, vec![]), + Some(section) => CmdPackage::from_config( + §ion, + vec![ + Cmd::new_simple(format!("ping"), 0, Box::new(|_x| true)), + Cmd::new_echo( + format!("bot_name"), + 0, + Box::new(move |_x| username_copy.clone()), + ), + ], + ), None => { abort(); }