|
|
@@ -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(); |
|
|
|
} |
|
|
|
} |