From c0a4e14968496fad21d83e326a495eba31787262 Mon Sep 17 00:00:00 2001 From: NGnius Date: Wed, 20 May 2020 19:59:16 -0400 Subject: [PATCH] Add limited file attachment info functionality --- Cargo.toml | 2 +- src/commands.rs | 2 + src/commands/cmd_preview.rs | 101 ++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/commands/cmd_preview.rs diff --git a/Cargo.toml b/Cargo.toml index c1a3d13..f10a44e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leo" -version = "0.2.0" +version = "0.3.0" authors = ["NGnius "] edition = "2018" diff --git a/src/commands.rs b/src/commands.rs index 00dc72e..cad7073 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,5 +1,7 @@ mod cmd_macro; +mod cmd_preview; pub use self::{ cmd_macro::CmdMacro, + cmd_preview::CmdPreview, }; diff --git a/src/commands/cmd_preview.rs b/src/commands/cmd_preview.rs new file mode 100644 index 0000000..a61c604 --- /dev/null +++ b/src/commands/cmd_preview.rs @@ -0,0 +1,101 @@ +extern crate serenity; +use serenity::{ + model::channel::Message, + model::channel::Attachment, + prelude::*, + utils::MessageBuilder, + utils::Colour, +}; + +extern crate regex; +use regex::{Regex, RegexBuilder}; + +use crate::traits::Command; + +pub struct CmdPreview { + format: Regex, + help_format: Regex, + extension_format: Regex, +} + +impl Command for CmdPreview { + fn execute(&mut self, ctx: &Context, msg: &Message) { + for attachment in &msg.attachments { + if self.attachment_extension(&attachment) != "" { + if let Err(why) = msg.channel_id.send_message(&ctx.http, |m| { + return m.embed(|e| { + return e.colour(Colour::from_rgb(200, 200, 0)) + .title(self.title_attachment(&attachment)) + .description(self.describe_attachment(&attachment)); + }); + }) { + println!("Failed to send file preview message {:?}", why); + } + } + } + } + + fn valid(&self, _ctx: &Context, msg: &Message) -> bool { + return self.format.is_match(&msg.content) && msg.attachments.len() != 0 && !msg.author.bot; + } + + fn help(&self, ctx: &Context, msg:&Message) { + let mut response = MessageBuilder::new(); + response.push("**Generate file attachment previews**\n*This has no standalone commands*"); + if let Err(why) = msg.channel_id.say(&ctx.http, &response.build()) { + println!("Failed to send file preview help message {:?}", why); + } + } + + fn valid_help(&self, _ctx: &Context, msg: &Message) -> bool { + return self.help_format.is_match(&msg.content); + } +} + +impl CmdPreview { + pub fn new() -> CmdPreview { + return CmdPreview { + format: + RegexBuilder::new(r#".*"#) + .multi_line(true) + .case_insensitive(true) + .build() + .unwrap(), + help_format: + RegexBuilder::new(r#"^!help\s*(?:preview|file)"#) + .case_insensitive(true) + .build() + .unwrap(), + extension_format: + RegexBuilder::new(r#"\.(dll|exe|so)$"#) + .case_insensitive(true) + .build() + .unwrap(), + }; + } + + fn describe_attachment(&self, attachment: &Attachment) -> String { + let ext = self.attachment_extension(attachment); + if ext == "dll" { + return "Microsoft Windows **__D__ynamic __L__ink __L__ibrary**\nPlease make sure you trust who sent this before downloading this file.".to_string(); + } else if ext == "exe" { + return "Microsoft Windows **__Exe__cutable**\nPlease make sure you trust who sent this before downloading or running this file.".to_string(); + } else if ext == "so" { + return "Generic Operating System **__S__hared (__O__bject) Library**\nPlease make sure you trust who sent this before downloading or running this file.".to_string(); + } + return "".to_string(); + } + + fn title_attachment(&self, attachment: &Attachment) -> String { + return attachment.filename.to_uppercase(); + } + + fn attachment_extension(&self, attachment: &Attachment) -> String { + if let Some(parsed) = self.extension_format.captures(&attachment.filename) { + if let Some(ext) = parsed.get(1) { + return ext.as_str().to_lowercase().to_string(); + } + } + return "".to_string(); + } +} diff --git a/src/main.rs b/src/main.rs index 691d351..f131a49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,6 +67,7 @@ fn main() { // register commands; let mut commands = std::vec::Vec::>::new(); commands.push(Box::new(commands::CmdMacro::new())); + commands.push(Box::new(commands::CmdPreview::new())); // start bot let mut client = Client::new(&token, event_handler).expect("Error creating client"); {