aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugin.rs
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-02-23 22:42:56 +0100
committerJokler <jokler.contact@gmail.com>2018-02-23 22:42:56 +0100
commit5278972ee7e980cb6cace8db71d1e1ed8cd07c11 (patch)
treec691517d6d32afff330871262248cf85b7d68439 /src/plugin.rs
parent297ceaa0899a6228f47f1f14e4bd261ec4cc6619 (diff)
parent968c837365c4a332fe3c802fd4ecab2562eb4d5a (diff)
downloadfrippy-5278972ee7e980cb6cace8db71d1e1ed8cd07c11.tar.gz
frippy-5278972ee7e980cb6cace8db71d1e1ed8cd07c11.zip
Merge branch 'dev' into factoid-plugin
Diffstat (limited to 'src/plugin.rs')
-rw-r--r--src/plugin.rs52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/plugin.rs b/src/plugin.rs
index d14c129..a67d68f 100644
--- a/src/plugin.rs
+++ b/src/plugin.rs
@@ -2,20 +2,35 @@
use std::fmt;
use irc::client::prelude::*;
-use irc::error::Error as IrcError;
+use irc::error::IrcError;
+
+/// Describes if a [`Plugin`](trait.Plugin.html) is done working on a
+/// [`Message`](../../irc/proto/message/struct.Message.html) or if another thread is required.
+#[derive(Debug)]
+pub enum ExecutionStatus {
+ /// The [`Plugin`](trait.Plugin.html) does not need to do any more work on this [`Message`](../../irc/proto/message/struct.Message.html).
+ Done,
+ /// An error occured during the execution.
+ Err(IrcError),
+ /// The execution needs to be done by [`execute_threaded()`](trait.Plugin.html#tymethod.execute_threaded).
+ RequiresThread,
+}
/// `Plugin` has to be implemented for any struct that should be usable
-/// as a plugin in frippy.
+/// as a `Plugin` in frippy.
pub trait Plugin: PluginName + Send + Sync + fmt::Debug {
- /// This should return true if the `Plugin` wants to do work on the message.
- fn is_allowed(&self, server: &IrcServer, message: &Message) -> bool;
- /// Handles messages which are not commands but still necessary.
- fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError>;
+ /// Handles messages which are not commands or returns [`RequiresThread`](enum.ExecutionStatus.html#variant.RequiresThread)
+ /// if [`execute_threaded()`](trait.Plugin.html#tymethod.execute_threaded) should be used instead.
+ fn execute(&self, server: &IrcClient, message: &Message) -> ExecutionStatus;
+ /// Handles messages which are not commands in a new thread.
+ fn execute_threaded(&self, server: &IrcClient, message: &Message) -> Result<(), IrcError>;
/// Handles any command directed at this plugin.
- fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>;
+ fn command(&self, server: &IrcClient, command: PluginCommand) -> Result<(), IrcError>;
+ /// Similar to [`command()`](trait.Plugin.html#tymethod.command) but return a String instead of sending messages directly to IRC.
+ fn evaluate(&self, server: &IrcClient, command: PluginCommand) -> Result<String, String>;
}
-/// `PluginName` is required by `Plugin`.
+/// `PluginName` is required by [`Plugin`](trait.Plugin.html).
///
/// To implement it simply add `#[derive(PluginName)]`
/// above the definition of the struct.
@@ -28,7 +43,7 @@ pub trait Plugin: PluginName + Send + Sync + fmt::Debug {
/// struct Foo;
/// ```
pub trait PluginName: Send + Sync + fmt::Debug {
- /// Returns the name of the plugin.
+ /// Returns the name of the `Plugin`.
fn name(&self) -> &str;
}
@@ -45,28 +60,23 @@ pub struct PluginCommand {
}
impl PluginCommand {
- /// Creates a `PluginCommand` from `Message` if it is a `PRIVMSG`
+ /// Creates a `PluginCommand` from [`Message`](../../irc/proto/message/struct.Message.html)
+ /// if it contains a [`PRIVMSG`](../../irc/proto/command/enum.Command.html#variant.PRIVMSG)
/// that starts with the provided `nick`.
pub fn from(nick: &str, message: &Message) -> Option<PluginCommand> {
-
// Get the actual message out of PRIVMSG
if let Command::PRIVMSG(_, ref content) = message.command {
-
// Split content by spaces and filter empty tokens
let mut tokens: Vec<String> = content.split(' ').map(ToOwned::to_owned).collect();
// Commands start with our name
if tokens[0].to_lowercase().starts_with(nick) {
-
// Remove the bot's name from the first token
tokens[0].drain(..nick.len());
// We assume that only ':' and ',' are used as suffixes on IRC
// If there are any other chars we assume that it is not ment for the bot
- tokens[0] = tokens[0]
- .chars()
- .filter(|&c| !":,".contains(c))
- .collect();
+ tokens[0] = tokens[0].chars().filter(|&c| !":,".contains(c)).collect();
if !tokens[0].is_empty() {
return None;
}
@@ -75,10 +85,10 @@ impl PluginCommand {
tokens.remove(0);
Some(PluginCommand {
- source: message.source_nickname().unwrap().to_string(),
- target: message.response_target().unwrap().to_string(),
- tokens: tokens,
- })
+ source: message.source_nickname().unwrap().to_string(),
+ target: message.response_target().unwrap().to_string(),
+ tokens: tokens,
+ })
} else {
None
}