summaryrefslogtreecommitdiffstats
path: root/src/plugins/factoids/mod.rs
blob: 3f89943a5453e1cf6a34fa3eb7f59e6f24799efd (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
extern crate rlua;

use std::fmt;
use std::str::FromStr;
use std::sync::Mutex;
use self::rlua::prelude::*;
use irc::client::prelude::*;
use irc::error::IrcError;
use error::FrippyError;
use error::PluginError;
use failure::Fail;

use time;
use chrono::NaiveDateTime;

use plugin::*;
pub mod database;
use self::database::{Database, DbResponse};

mod utils;
use self::utils::*;

static LUA_SANDBOX: &'static str = include_str!("sandbox.lua");

#[derive(PluginName)]
pub struct Factoids<T: Database> {
    factoids: Mutex<T>,
}

macro_rules! try_lock {
    ( $m:expr ) => {
        match $m.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        }
    }
}

impl<T: Database> Factoids<T> {
    pub fn new(db: T) -> Factoids<T> {
        Factoids {
            factoids: Mutex::new(db),
        }
    }

    fn create_factoid(&self, name: &str, content: &str, author: &str) -> Result<&str, FrippyError> {
        let count = try_lock!(self.factoids).count_factoids(name).map_err(|e| PluginError::Factoids { error: e.to_owned() })?;
        let tm = time::now().to_timespec();

        let factoid = database::NewFactoid {
            name: name,
            idx: count,
            content: content,
            author: author,
            created: NaiveDateTime::from_timestamp(tm.sec, 0u32),
        };

        match try_lock!(self.factoids).insert_factoid(&factoid) {
            DbResponse::Success => Ok("Successfully added"),
            DbResponse::Failed(e) => Err(PluginError::Factoids { error: e.to_owned() })?,
        }
    }

    fn add(&self, client: &IrcClient, command: &mut PluginCommand) -> Result<&str, FrippyError> {
        if command.tokens.len() < 2 {
            return Ok(self.invalid_command(client, command).map(|()| "")?);
        }

        let name = command.tokens.remove(0);
        let content = command.tokens.join(" ");

        Ok(self.create_factoid(&name, &content, &command.source)?)
    }

    fn add_from_url(
        &self,
        client: &IrcClient,
        command: &mut PluginCommand,
    ) -> Result<&str, FrippyError> {
        if command.tokens.len() < 2 {
            return Ok(self.invalid_command(client, command).map(|()| "")?);
        }

        let name = command.tokens.remove(0);
        let url = &command.tokens[0];
        let content = ::utils::download(1024, url)?;

        Ok(self.create_factoid(&name, &content, &command.source)?)
    }

    fn remove(&self, client: &IrcClient, command: &mut PluginCommand) -> Result<&str, FrippyError> {
        if command.tokens.len() < 1 {
            return Ok(self.invalid_command(client, command).map(|()| "")?);
        }

        let name = command.tokens.remove(0);
        let count = try_lock!(self.factoids).count_factoids(&name).map_err(|e| PluginError::Factoids { error: e.to_owned() } )?;

        match try_lock!(self.factoids).delete_factoid(&name, count - 1) {
            DbResponse::Success => Ok("Successfully removed"),
            DbResponse::Failed(e) => Err(PluginError::Factoids { error: e.to_owned() })?,
        }
    }

    fn get(&self, client: &IrcClient, command: &PluginCommand) -> Result<String, FrippyError> {
        let (name, idx) = match command.tokens.len() {
            0 => return Ok(self.invalid_command(client, command).map(|()| String::new())?),
            1 => {
                let name = &command.tokens[0];
                let count = try_lock!(self.factoids).count_factoids(name).map_err(|e| PluginError::Factoids { error: e.to_owned() } )?;

                if count < 1 {
                    Err(PluginError::Factoids { error: format!("{} does not exist", name) })?;
                }

                (name, count - 1)
            }
            _ => {
                let name = &command.tokens[0];
                let idx = match i32::from_str(&command.tokens[1]) {
                    Ok(i) => i,
                    Err(_) => Err(PluginError::Factoids { error: String::from("Invalid index") })?,
                };

                (name, idx)
            }
        };

        let factoid = match try_lock!(self.factoids).get_factoid(name, idx) {
            Some(v) => v,
            None => Err(PluginError::Factoids { error: format!("{}~{} does not exist", name, idx) })?,
        };

        let message = factoid.content.replace("\n", "|").replace("\r", "");

        Ok(format!("{}: {}", factoid.name, message))
    }

    fn info(&self, client: &IrcClient, command: &PluginCommand) -> Result<String, FrippyError> {
        match command.tokens.len() {
            0 => Ok(self.invalid_command(client, command).map(|()| String::new())?),
            1 => {
                let name = &command.tokens[0];
                let count = try_lock!(self.factoids).count_factoids(name).map_err(|e| PluginError::Factoids { error: e.to_owned() } )?;

                Ok(match count {
                    0 => Err(PluginError::Factoids { error: format!("{} does not exist", name) })?,
                    1 => format!("There is 1 version of {}", name),
                    _ => format!("There are {} versions of {}", count, name),
                })
            }
            _ => {
                let name = &command.tokens[0];
                let idx = i32::from_str(&command.tokens[1]).map_err(|_| PluginError::Factoids { error: String::from("Invalid index") })?;

                let factoid = match try_lock!(self.factoids).get_factoid(name, idx) {
                    Some(v) => v,
                    None => return Ok(format!("{}~{} does not exist", name, idx)),
                };

                Ok(format!("{}: Added by {} at {} UTC", name, factoid.author, factoid.created))
            }
        }
    }

    fn exec(
        &self,
        client: &IrcClient,
        mut command: PluginCommand,
    ) -> Result<String, FrippyError> {
        if command.tokens.len() < 1 {
            Ok(self.invalid_command(client, &command).map(|()| String::new())?)
        } else {
            let name = command.tokens.remove(0);
            let count = try_lock!(self.factoids).count_factoids(&name).map_err(|e| PluginError::Factoids { error: e.to_owned() } )?;
            let factoid = try_lock!(self.factoids).get_factoid(&name, count - 1).ok_or(PluginError::Factoids { error: format!("The factoid \"{}\" does not exist", name) })?;

            let content = factoid.content;
            let value = if content.starts_with('>') {
                let content = String::from(&content[1..]);

                if content.starts_with('>') {
                    content
                } else {
                    match self.run_lua(&name, &content, &command) {
                        Ok(v) => v,
                        Err(e) => format!("\"{}\"", e),
                    }
                }
            } else {
                content
            };

            Ok(value.replace("\n", "|").replace("\r", ""))
        }
    }

    fn run_lua(
        &self,
        name: &str,
        code: &str,
        command: &PluginCommand,
    ) -> Result<String, rlua::Error> {
        let args = command
            .tokens
            .iter()
            .filter(|x| !x.is_empty())
            .map(ToOwned::to_owned)
            .collect::<Vec<String>>();

        let lua = unsafe { Lua::new_with_debug() };
        let globals = lua.globals();

        globals.set("factoid", code)?;
        globals.set("download", lua.create_function(download)?)?;
        globals.set("sleep", lua.create_function(sleep)?)?;
        globals.set("args", args)?;
        globals.set("input", command.tokens.join(" "))?;
        globals.set("user", command.source.clone())?;
        globals.set("channel", command.target.clone())?;
        globals.set("output", lua.create_table()?)?;

        lua.exec::<()>(LUA_SANDBOX, Some(name))?;
        let output: Vec<String> = globals.get::<_, Vec<String>>("output")?;

        Ok(output.join("|"))
    }

    fn invalid_command(&self, client: &IrcClient, command: &PluginCommand) -> Result<(), IrcError> {
        client.send_notice(&command.source, "Invalid Command")
    }
}

impl<T: Database> Plugin for Factoids<T> {
    fn execute(&self, _: &IrcClient, message: &Message) -> ExecutionStatus {
        match message.command {
            Command::PRIVMSG(_, ref content) => if content.starts_with('!') {
                ExecutionStatus::RequiresThread
            } else {
                ExecutionStatus::Done
            },
            _ => ExecutionStatus::Done,
        }
    }

    fn execute_threaded(&self, client: &IrcClient, message: &Message) -> Result<(), IrcError> {
        if let Command::PRIVMSG(_, mut content) = message.command.clone() {
            content.remove(0);

            let t: Vec<String> = content.split(' ').map(ToOwned::to_owned).collect();

            let c = PluginCommand {
                source: message.source_nickname().unwrap().to_owned(),
                target: message.response_target().unwrap().to_owned(),
                tokens: t,
            };

            match self.exec(client, c) {
                Ok(f) => client.send_privmsg(&message.response_target().unwrap(), &f),
                Err(_) => Ok(()),
            }
        } else {
            Ok(())
        }
    }

    fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), IrcError> {
        if command.tokens.is_empty() {
            return self.invalid_command(client, &command);
        }

        let target = command.target.clone();
        let source = command.source.clone();

        let sub_command = command.tokens.remove(0);
        let result = match sub_command.as_ref() {
            "add" => self.add(client, &mut command).map(|s| s.to_owned()),
            "fromurl" => self.add_from_url(client, &mut command).map(|s| s.to_owned()),
            "remove" => self.remove(client, &mut command).map(|s| s.to_owned()),
            "get" => self.get(client, &command),
            "info" => self.info(client, &command),
            "exec" => self.exec(client, command),
            _ => self.invalid_command(client, &command).map(|()| String::new()).map_err(|e| e.into()),
        };

        Ok(match result {
            Ok(v) => client.send_privmsg(&target, &v),
            Err(e) => client.send_notice(&source, &e.cause().unwrap().to_string()),
        }?)
    }

    fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result<String, String> {
        Err(String::from(
            "Evaluation of commands is not implemented for Factoids at this time",
        ))
    }
}

impl<T: Database> fmt::Debug for Factoids<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Factoids {{ ... }}")
    }
}