diff options
| -rw-r--r-- | Cargo.lock | 40 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | bin/main.rs | 29 | ||||
| -rw-r--r-- | configs/config.toml | 2 | ||||
| -rw-r--r-- | src/plugin.rs | 39 | ||||
| -rw-r--r-- | src/plugins/currency.rs | 5 | ||||
| -rw-r--r-- | src/plugins/emoji.rs | 4 | ||||
| -rw-r--r-- | src/plugins/help.rs | 4 | ||||
| -rw-r--r-- | systemd/frippy.service | 15 |
9 files changed, 86 insertions, 54 deletions
@@ -1,23 +1,3 @@ -[root] -name = "frippy" -version = "0.2.0" -dependencies = [ - "clippy 0.0.166 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "irc 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "plugin_derive 0.1.0", - "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "reqwest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode_names 0.1.7 (git+https://github.com/Jokler/unicode_names?branch=update-to-latest-unicode)", -] - [[package]] name = "adler32" version = "1.0.2" @@ -293,6 +273,26 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "frippy" +version = "0.3.1" +dependencies = [ + "clippy 0.0.166 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "irc 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "plugin_derive 0.1.0", + "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "reqwest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode_names 0.1.7 (git+https://github.com/Jokler/unicode_names?branch=update-to-latest-unicode)", +] + +[[package]] name = "fuchsia-zircon" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1,6 +1,6 @@ [package] name = "frippy" -version = "0.3.0" +version = "0.3.1" authors = ["Jokler <jokler.contact@gmail.com>"] repository = "https://github.com/Mavulp/frippy" readme = "README.md" diff --git a/bin/main.rs b/bin/main.rs index 18f362e..86910c0 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -8,21 +8,40 @@ struct Logger; impl log::Log for Logger { fn enabled(&self, metadata: &LogMetadata) -> bool { - metadata.level() <= LogLevel::Info + metadata.target().contains("frippy") } fn log(&self, record: &LogRecord) { if self.enabled(record.metadata()) { - println!("[{}]({}) {}", time::now().rfc822(), record.level(), record.args()); + if record.metadata().level() >= LogLevel::Debug { + println!("[{}]({}) {} -> {}", + time::now().rfc822(), + record.level(), + record.target(), + record.args()); + } else { + println!("[{}]({}) {}", + time::now().rfc822(), + record.level(), + record.args()); + } } } } fn main() { + + let log_level = if cfg!(debug_assertions) { + LogLevelFilter::Debug + } else { + LogLevelFilter::Info + }; + log::set_logger(|max_log_level| { - max_log_level.set(LogLevelFilter::Info); - Box::new(Logger) - }).unwrap(); + max_log_level.set(log_level); + Box::new(Logger) + }) + .unwrap(); frippy::run(); } diff --git a/configs/config.toml b/configs/config.toml index 510e1ae..5ad66e2 100644 --- a/configs/config.toml +++ b/configs/config.toml @@ -12,7 +12,7 @@ encoding = "UTF-8" channels = ["#frippy"] umodes = "+B" user_info = "IRC Bot" -version = "frippy v0.3.0" +version = "frippy v0.3.1" source = "https://github.com/Mavulp/frippy" #ping_time = 180 #ping_timeout = 10 diff --git a/src/plugin.rs b/src/plugin.rs index e0a4ce2..d1f849a 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,15 +1,15 @@ use std::fmt; use std::collections::HashMap; use std::thread::spawn; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use irc::client::prelude::*; use irc::error::Error as IrcError; pub trait Plugin: PluginName + Send + Sync + fmt::Debug { fn is_allowed(&self, server: &IrcServer, message: &Message) -> bool; - fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError>; - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>; + fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError>; + fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>; } pub trait PluginName: Send + Sync + fmt::Debug { @@ -65,19 +65,9 @@ impl PluginCommand { } } -// Lock the mutex and ignore if it is poisoned -macro_rules! lock_plugin { - ($e:expr) => { - match $e.lock() { - Ok(plugin) => plugin, - Err(poisoned) => poisoned.into_inner(), - } - } -} - #[derive(Clone, Debug)] pub struct ThreadedPlugins { - plugins: HashMap<String, Arc<Mutex<Plugin>>>, + plugins: HashMap<String, Arc<Plugin>>, } impl ThreadedPlugins { @@ -87,7 +77,7 @@ impl ThreadedPlugins { pub fn add<T: Plugin + 'static>(&mut self, plugin: T) { let name = plugin.name().to_lowercase(); - let safe_plugin = Arc::new(Mutex::new(plugin)); + let safe_plugin = Arc::new(plugin); self.plugins.insert(name, safe_plugin); } @@ -96,7 +86,11 @@ impl ThreadedPlugins { for (name, plugin) in self.plugins.clone() { // Send the message to the plugin if the plugin needs it - if lock_plugin!(plugin).is_allowed(server, &message) { + if plugin.is_allowed(server, &message) { + + debug!("Executing {} with {}", + name, + message.to_string().replace("\r\n", "")); // Clone everything before the move // The server uses an Arc internally too @@ -106,7 +100,7 @@ impl ThreadedPlugins { // Execute the plugin in another thread spawn(move || { - if let Err(e) = lock_plugin!(plugin).execute(&server, &message) { + if let Err(e) = plugin.execute(&server, &message) { error!("Error in {} - {}", name, e); }; }); @@ -114,7 +108,10 @@ impl ThreadedPlugins { } } - pub fn handle_command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { + pub fn handle_command(&mut self, + server: &IrcServer, + mut command: PluginCommand) + -> Result<(), IrcError> { if !command.tokens.iter().any(|s| !s.is_empty()) { let help = format!("Use \"{} help\" to get help", server.current_nickname()); @@ -127,11 +124,13 @@ impl ThreadedPlugins { // The first token contains the name of the plugin let name = command.tokens.remove(0); + debug!("Sending command \"{:?}\" to {}", command, name); + // Clone for the move - the server uses an Arc internally let server = server.clone(); let plugin = Arc::clone(plugin); spawn(move || { - if let Err(e) = lock_plugin!(plugin).command(&server, command) { + if let Err(e) = plugin.command(&server, command) { error!("Error in {} command - {}", name, e); }; }); @@ -153,7 +152,7 @@ impl fmt::Display for ThreadedPlugins { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let plugin_names = self.plugins .iter() - .map(|(_, p)| lock_plugin!(p).name().to_string()) + .map(|(_, p)| p.name().to_string()) .collect::<Vec<String>>(); write!(f, "{}", plugin_names.join(", ")) } diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 78ae593..d6cf928 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -1,7 +1,6 @@ extern crate reqwest; extern crate serde; extern crate serde_json; -extern crate regex; use std::io::Read; use std::num::ParseFloatError; @@ -131,11 +130,11 @@ impl Plugin for Currency { false } - fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { + fn execute(&self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { panic!("Currency does not implement the execute function!") } - fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { if command.tokens.is_empty() { return self.invalid_command(server, &command); diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index 09d5c27..1bb714c 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -105,7 +105,7 @@ impl Plugin for Emoji { } } - fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { + fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { match message.command { Command::PRIVMSG(_, ref content) => { self.emoji(server, content, message.response_target().unwrap()) @@ -114,7 +114,7 @@ impl Plugin for Emoji { } } - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { server.send_notice(&command.source, "This Plugin does not implement any commands.") } diff --git a/src/plugins/help.rs b/src/plugins/help.rs index c4ddcd4..8f3fb4d 100644 --- a/src/plugins/help.rs +++ b/src/plugins/help.rs @@ -21,11 +21,11 @@ impl Plugin for Help { false } - fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { + fn execute(&self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { panic!("Help does not implement the execute function!") } - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { self.help(server, command) } } diff --git a/systemd/frippy.service b/systemd/frippy.service new file mode 100644 index 0000000..1fb6cd7 --- /dev/null +++ b/systemd/frippy.service @@ -0,0 +1,15 @@ +[Unit] +Description=IRC Bot written in Rust +After=network-online.target + +[Service] +Type=simple +User=frippy +Group=frippy +WorkingDirectory=/etc/frippy +ExecStart=/usr/bin/frippy +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target |
