diff --git a/src/bot.rs b/src/bot.rs index 0905591ed2da5bd3ebdd023ad70e959e1ef29511..7a24f7315b7649b6d50d7476ab0d4e988cfedd60 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -2,7 +2,7 @@ use crate::{ cmd::package::CmdPackage, cmd::Cmd, config::{from_file as config_from_file, Config}, - return_if, + log_fatal, return_if, }; use futures::future::join_all; use log::{error, info, warn}; @@ -20,7 +20,6 @@ use matrix_sdk::{ }, UserId, }, - uuid::Uuid, Client, Result, SyncSettings, }; use std::{ @@ -36,6 +35,21 @@ pub struct Bot { client: Option<Client>, } +static GLOBAL_PKG: SyncLazy<Arc<Mutex<Vec<CmdPackage>>>> = + SyncLazy::new(|| Arc::new(Mutex::new(Vec::<CmdPackage>::new()))); + +macro_rules! GLOBAL_PKG_ITER { + () => {{ + GLOBAL_PKG.lock().unwrap().iter() + }}; +} + +macro_rules! GLOBAL_PKG_LOCKED { + () => {{ + GLOBAL_PKG.lock().unwrap() + }}; +} + async fn join_room(room: &Invited) { info!("Autojoining room {}", room.room_id()); let mut delay = 2; // seconds @@ -110,12 +124,7 @@ async fn on_msg_room_event( if body.starts_with("%") { if let Some(stripped) = body.strip_prefix("%") { let args = stripped.split(' ').collect::<Vec<&str>>(); - let (res, emojies) = match GLOBAL_PKG - .lock() - .unwrap() - .iter() - .find(|x| x.has_command(&args)) - { + let (res, emojies) = match GLOBAL_PKG_ITER!().find(|x| x.has_command(&args)) { Some(pkg) => { let (res, emojies) = pkg.handle(&joined, &sender_id, args); (res, emojies.clone()) @@ -127,14 +136,14 @@ async fn on_msg_room_event( let msg = AnyMessageEventContent::RoomMessage( MessageEventContent::text_reply_html(res.clone(), res, &event), ); - let msg_uuid = Uuid::new_v4(); - let send_id = joined.send(msg, Some(msg_uuid)).await.unwrap().event_id; - for emoji in emojies { + let send_id = joined.send(msg, None).await.unwrap().event_id; + join_all(emojies.iter().map(|emoji| { let msg_emoji = AnyMessageEventContent::Reaction(ReactionEventContent::new( Relation::new(send_id.clone(), emoji.clone()), )); - joined.send(msg_emoji, None).await.unwrap(); - } + joined.send(msg_emoji, None) + })) + .await; } } }; @@ -180,18 +189,15 @@ macro_rules! register_handler_if_enabled { }}; } -static GLOBAL_PKG: SyncLazy<Arc<Mutex<Vec<CmdPackage>>>> = - SyncLazy::new(|| Arc::new(Mutex::new(Vec::<CmdPackage>::new()))); - impl Bot { pub fn new(config_file: String) -> Bot { match config_from_file(&config_file) { Err(e) => { - error!( + log_fatal!( "Failed to read config file '{}' with error: {}", - config_file, e + config_file, + e ); - abort(); } Ok(config) => { info!("Using config file: {}", config_file); @@ -257,6 +263,6 @@ impl Bot { abort(); } }; - GLOBAL_PKG.lock().unwrap().push(pkg); + GLOBAL_PKG_LOCKED!().push(pkg); } } diff --git a/src/cmd/acl.rs b/src/cmd/acl.rs index d27de8ee0c2fdde29f2210f4eaae4db06d25463b..a1f087d39676757e6b6ce378b0bc725dd9f76dcd 100644 --- a/src/cmd/acl.rs +++ b/src/cmd/acl.rs @@ -1,10 +1,10 @@ #![allow(dead_code)] -use crate::bool_to_result; -use log::{error, warn}; +use crate::{bool_to_result, log_fatal}; +use log::warn; use matrix_sdk::{room::Joined as JoinedRoom, ruma::UserId}; use std::fmt; -use std::{process::abort, result::Result}; +use std::result::Result; #[derive(Clone)] pub enum CmdAcl { @@ -138,8 +138,7 @@ impl CmdAcl { } else if upper == "ANY" { CmdAcl::Any } else { - error!("Unknown ACL: {}", string); - abort(); + log_fatal!("Unknown ACL: {}", string); } } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 3d911dd8820cf8fa335a513dca1f69f4e7dac417..27e2f60c9f727284533c5851555d97952b5afbb0 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -3,8 +3,8 @@ pub mod acl; pub mod package; -use log::error; -use std::{boxed::Box, process::abort, string::String}; +use crate::log_fatal; +use std::{boxed::Box, string::String}; trait CmdFnSimple = Fn(Vec<&str>) -> bool + Send + Sync; trait CmdFnEcho = Fn(Vec<&str>) -> String + Send + Sync; @@ -34,8 +34,7 @@ impl Cmd { pub fn exec(&self, args: Vec<&str>) -> String { if args.len() != self.argc as usize { - error!("Arg count doesn't match"); - abort(); + log_fatal!("Arg count doesn't match"); } match &self.handler { diff --git a/src/config.rs b/src/config.rs index e2adb2558327137e77ba5683a41550dcb65bee84..7ab3bd1c65f889c0ebf36c2a6612f512315aa382 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ #![allow(dead_code)] +use crate::log_fatal; use ini::{Error::Io as IniErrIo, Error::Parse as IniErrParse, Ini, Properties as IniSection}; use log::{error, info, warn, LevelFilter}; use std::{env, path::Path, process::abort, str::FromStr}; @@ -110,11 +111,10 @@ pub fn check_argument_or_abort(should_write_default: bool) { .to_string() + ".ini"; if Path::new(&default_file).exists() { - error!( + log_fatal!( "Args count is {}, you need to only specify the config file for the bot", env::args().len() - 1 ); - abort(); } else if should_write_default { warn!("Try to write the default config file to {}", default_file); match write_default(&default_file) { diff --git a/src/utils.rs b/src/utils.rs index 529987eefb521c3cf24dc2f8f0d448f4ccd076ee..a984c7129a550116fea851a4f7e61be0f9f2d209 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,14 @@ -#![allow(dead_code)] +#![allow(dead_code, unused_imports)] + +#[macro_export] +macro_rules! log_fatal { + ($($arg:tt)+) => {{ + use log::error; + use std::process::abort; + error!($($arg)+); + abort(); + }}; +} #[macro_export] macro_rules! return_if {