summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2017-10-10 03:51:13 +0200
committerJokler <jokler.contact@gmail.com>2017-10-11 15:41:32 +0200
commitec33c870852f8a52f3cc0cf5a84b99c775dee1e3 (patch)
tree5aea3292f226c450713f76b1e9c017e7cf86da9a
parent32021829a5b3ea2169f006879d52bdd2232b1030 (diff)
downloadfrippy-ec33c870852f8a52f3cc0cf5a84b99c775dee1e3.tar.gz
frippy-ec33c870852f8a52f3cc0cf5a84b99c775dee1e3.zip
Add comments to lib.rs
-rw-r--r--src/lib.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5a85db0..ca1aafe 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -40,20 +40,26 @@ pub fn run() {
let server = IrcServer::new("config.toml").unwrap();
server.identify().unwrap();
+ // The list of plugins in use
let plugins: Vec<Arc<Mutex<Plugin>>> =
vec![Arc::new(Mutex::new(plugins::emoji::Emoji::new())),
Arc::new(Mutex::new(plugins::currency::Currency::new()))];
+ // We need the plugins' names to make sure the user gets a response
+ // if they use an incorrect plugin name
let plugin_names: Vec<String> = plugins
.iter()
.map(|p| p.lock().unwrap().to_string().to_lowercase())
.collect();
+ // The main loop over received messages
server
.for_each_incoming(|message| {
let message = Arc::new(message);
+ // Check for possible command and save the result for later
let command = get_command(&server.current_nickname().to_lowercase(), &message);
+ // Check if the first token of the command is valid
if let Some(ref c) = command {
if c.tokens.is_empty() {
let help = format!("Use \"{} help\" to get help", server.current_nickname());
@@ -74,24 +80,30 @@ pub fn run() {
}
for plugin in plugins.clone() {
+ // Clone everything before the move
let server = server.clone();
let message = Arc::clone(&message);
let command = command.clone();
+ // Spawn a new thread for each plugin
spawn(move || {
+ // Lock the mutex and ignore if it is poisoned
let mut plugin = match plugin.lock() {
Ok(plugin) => plugin,
Err(poisoned) => poisoned.into_inner(),
};
+ // Send the message to the plugin if the plugin needs it
if plugin.is_allowed(&server, &message) {
plugin.execute(&server, &message).unwrap();
}
+ // Check if the command is for this plugin
if let Some(mut c) = command {
if !c.tokens.is_empty() &&
plugin.to_string().to_lowercase() == c.tokens[0].to_lowercase() {
+ // The first token contains the name of the plugin
c.tokens.remove(0);
plugin.command(&server, c).unwrap();
}
@@ -107,28 +119,39 @@ fn send_help_message(server: &IrcServer, command: &PluginCommand) -> Result<(),
}
fn get_command(nick: &str, message: &Message) -> Option<PluginCommand> {
+
+ // Get the actual message out of PRIVMSG
if let PRIVMSG(_, ref content) = message.command {
+
+ // Split content by spaces and filter empty tokens
let mut tokens: Vec<String> = content
.split(' ')
.filter(|&x| !x.is_empty())
.map(ToOwned::to_owned)
.collect();
+ // Check if the message contained notthing but spaces
if tokens.is_empty() {
return None;
}
+ // Only compile the regex once
+ // We assume that only ':' and ',' are used as suffixes on IRC
lazy_static! {
static ref RE: Regex = Regex::new("^[:,]*?$").unwrap();
}
if tokens[0].to_lowercase().starts_with(nick) {
+
+ // Remove the bot's name from the first token
tokens[0].drain(..nick.len());
+ // If the regex does not match the message is not directed at the bot
if !RE.is_match(&tokens[0]) {
return None;
}
+ // The first token contained the name of the bot
tokens.remove(0);
Some(PluginCommand {
@@ -145,5 +168,4 @@ fn get_command(nick: &str, message: &Message) -> Option<PluginCommand> {
}
#[cfg(test)]
-mod tests {
-}
+mod tests {}