summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 5a85db0b46df8958c2a60fe42b84f512aeb76d56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]

//! Frippy is an IRC bot that runs plugins on each message
//! received.
//!
//! # Example
//! ```no_run
//! extern crate frippy;
//!
//! frippy::run();
//! ```

#[macro_use]
extern crate lazy_static;

extern crate irc;
extern crate regex;

#[macro_use]
mod plugin;
mod plugins;

use std::thread::spawn;
use std::sync::{Arc, Mutex};
use regex::Regex;
use irc::client::prelude::*;
use irc::proto::Command::PRIVMSG;
use irc::error::Error as IrcError;

use plugin::Plugin;
use plugin::PluginCommand;

/// Runs the bot
///
/// # Remarks
///
/// This blocks the current thread while the bot is running
pub fn run() {
    let server = IrcServer::new("config.toml").unwrap();
    server.identify().unwrap();

    let plugins: Vec<Arc<Mutex<Plugin>>> =
        vec![Arc::new(Mutex::new(plugins::emoji::Emoji::new())),
             Arc::new(Mutex::new(plugins::currency::Currency::new()))];

    let plugin_names: Vec<String> = plugins
        .iter()
        .map(|p| p.lock().unwrap().to_string().to_lowercase())
        .collect();

    server
        .for_each_incoming(|message| {
            let message = Arc::new(message);
            let command = get_command(&server.current_nickname().to_lowercase(), &message);

            if let Some(ref c) = command {
                if c.tokens.is_empty() {
                    let help = format!("Use \"{} help\" to get help", server.current_nickname());
                    server.send_notice(&c.source, &help).unwrap();

                } else if "help" == &c.tokens[0].to_lowercase() {
                    send_help_message(&server, c).unwrap();

                } else if !plugin_names.contains(&c.tokens[0].to_lowercase()) {

                    let help = format!("\"{} {}\" is not a command, \
                                       try \"{0} help\" instead.",
                                       server.current_nickname(),
                                       c.tokens[0]);

                    server.send_notice(&c.source, &help).unwrap();
                }
            }

            for plugin in plugins.clone() {
                let server = server.clone();
                let message = Arc::clone(&message);
                let command = command.clone();

                spawn(move || {
                    let mut plugin = match plugin.lock() {
                        Ok(plugin) => plugin,
                        Err(poisoned) => poisoned.into_inner(),
                    };

                    if plugin.is_allowed(&server, &message) {
                        plugin.execute(&server, &message).unwrap();
                    }

                    if let Some(mut c) = command {
                        if !c.tokens.is_empty() &&
                           plugin.to_string().to_lowercase() == c.tokens[0].to_lowercase() {

                            c.tokens.remove(0);
                            plugin.command(&server, c).unwrap();
                        }
                    }
                });
            }
        })
        .unwrap();
}

fn send_help_message(server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> {
    server.send_notice(&command.source, "Help has not been added yet.")
}

fn get_command(nick: &str, message: &Message) -> Option<PluginCommand> {
    if let PRIVMSG(_, ref content) = message.command {
        let mut tokens: Vec<String> = content
            .split(' ')
            .filter(|&x| !x.is_empty())
            .map(ToOwned::to_owned)
            .collect();

        if tokens.is_empty() {
            return None;
        }

        lazy_static! {
            static ref RE: Regex = Regex::new("^[:,]*?$").unwrap();
        }

        if tokens[0].to_lowercase().starts_with(nick) {
            tokens[0].drain(..nick.len());

            if !RE.is_match(&tokens[0]) {
                return None;
            }

            tokens.remove(0);

            Some(PluginCommand {
                     source: message.source_nickname().unwrap().to_string(),
                     target: message.response_target().unwrap().to_string(),
                     tokens: tokens,
                 })
        } else {
            None
        }
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
}