Make prefix configurable

This commit is contained in:
kageru 2020-01-03 11:19:53 +01:00
parent 4e605d1648
commit 2003ab5cb9
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
4 changed files with 48 additions and 12 deletions

View File

@ -1,9 +1,9 @@
secret = "your login secret" secret = "your login secret"
prefix = ">"
[[command]] [[command]]
# The prefix (a constant in the source code) # The prefix does not need to be added here.
# does not need to be specified here. # That is done automatically.
# It is added automatically.
trigger = "create_file" trigger = "create_file"
# Spaces are not escaped here # Spaces are not escaped here
command = "touch /tmp/test-rce" command = "touch /tmp/test-rce"

View File

@ -1,9 +1,9 @@
use super::config::CONFIG; use super::config::*;
use cmd_lib::{CmdResult, Process}; use cmd_lib::{CmdResult, Process};
use serde::Deserialize; use serde::Deserialize;
use serenity::model::channel::Message; use serenity::model::channel::Message;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Command { pub struct Command {
trigger: String, trigger: String,
command: String, command: String,
@ -14,8 +14,10 @@ pub fn print_commands() {
CONFIG.commands.iter().for_each(|c| println!("{:?}", c)); CONFIG.commands.iter().for_each(|c| println!("{:?}", c));
} }
pub fn find_matching(message: &str) -> Option<&Command> { pub fn find_matching<'a>(message: &str, cfg: &'a Config) -> Option<&'a Command> {
CONFIG.commands.iter().find(|&c| message[1..] == c.trigger) cfg.commands
.iter()
.find(|&c| message[cfg.prefix.len()..] == c.trigger)
} }
impl Command { impl Command {
@ -33,4 +35,12 @@ impl Command {
.wait::<CmdResult>() .wait::<CmdResult>()
.map_err(|e| format!("{:?}", e)) .map_err(|e| format!("{:?}", e))
} }
pub fn new(command: &str, trigger: &str, users: Vec<u64>) -> Self {
Self {
command: String::from(command),
trigger: String::from(trigger),
users,
}
}
} }

View File

@ -11,6 +11,7 @@ pub struct Config {
#[serde(rename = "command")] #[serde(rename = "command")]
pub commands: Vec<Command>, pub commands: Vec<Command>,
pub secret: String, pub secret: String,
pub prefix: String,
} }
lazy_static! { lazy_static! {

View File

@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
use commands::*; use commands::*;
use config::CONFIG;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use serenity::model::id::ChannelId; use serenity::model::id::ChannelId;
use serenity::prelude::*; use serenity::prelude::*;
@ -15,18 +16,16 @@ pub fn main() {
.expect("Could not connect to discord"); .expect("Could not connect to discord");
} }
const PREFIX: char = '>';
pub struct Handler; pub struct Handler;
impl EventHandler for Handler { impl EventHandler for Handler {
fn message(&self, ctx: Context, msg: Message) { fn message(&self, ctx: Context, msg: Message) {
if !msg.content.starts_with(PREFIX) { if !msg.content.starts_with(&CONFIG.prefix) {
return; return;
} }
if let Some(command) = find_matching(&msg.content) { if let Some(command) = find_matching(&msg.content, &CONFIG) {
let response = match command.execute(&msg) { let response = match command.execute(&msg) {
Err(e) => e, Err(e) => e,
Ok(()) => "Done".to_owned(), Ok(()) => String::from("Done."),
}; };
send(msg.channel_id, &response, &ctx); send(msg.channel_id, &response, &ctx);
} }
@ -38,3 +37,29 @@ pub fn send(target: ChannelId, message: &str, ctx: &Context) {
println!("Could not send message: {}", cause); println!("Could not send message: {}", cause);
} }
} }
#[cfg(test)]
mod tests {
use super::commands::*;
use super::config::*;
use super::*;
#[test]
fn find_matching_test() {
let cmd = Command::new("ls -l", "show_files", vec![1234567890]);
let cfg = Config {
prefix: String::from(">"),
secret: String::from("1qay2wsx3edc45fv"),
commands: vec![cmd.clone()],
};
assert_eq!(find_matching(&">show_files", &cfg), Some(&cmd));
assert_eq!(find_matching(&">show something else", &cfg), None);
let cfg = Config {
prefix: String::from("!!"),
..cfg
};
assert_eq!(find_matching(&">show_files", &cfg), None);
assert_eq!(find_matching(&"!!show_files", &cfg), Some(&cmd));
}
}