From 5fdb7198bc73035cf5621ded8183000c3fcbbe29 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sun, 24 Dec 2017 02:00:00 +0100 Subject: Add 'remove' command to Factoids and make the Database trait public --- src/plugins/factoids/mod.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/plugins/factoids/mod.rs') diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index bb83700..fcbbb79 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -11,7 +11,7 @@ use time; use chrono::NaiveDateTime; use plugin::*; -mod database; +pub mod database; use self::database::{Database, DbResponse}; static LUA_SANDBOX: &'static str = include_str!("sandbox.lua"); @@ -63,6 +63,23 @@ impl Factoids { } } + fn remove(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> { + if command.tokens.len() < 1 { + return self.invalid_command(server, command); + } + + let name = command.tokens.remove(0); + let count = match try_lock!(self.factoids).count(&name) { + Ok(c) => c, + Err(e) => return server.send_notice(&command.source, e), + }; + + match try_lock!(self.factoids).delete(&name, count - 1) { + DbResponse::Success => server.send_notice(&command.source, "Successfully removed"), + DbResponse::Failed(e) => server.send_notice(&command.source, &e), + } + } + fn get(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { let (name, idx) = match command.tokens.len() { @@ -257,6 +274,7 @@ impl Plugin for Factoids { let sub_command = command.tokens.remove(0); match sub_command.as_ref() { "add" => self.add(server, &mut command), + "remove" => self.remove(server, &mut command), "get" => self.get(server, &command), "info" => self.info(server, &command), "exec" => self.exec(server, command, true), -- cgit v1.2.3-70-g09d2