diff options
| author | Jokler <jokler.contact@gmail.com> | 2017-10-15 05:08:43 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2017-12-24 00:12:07 +0100 |
| commit | 5f1b2465080b7343869256c09ceda8025b30f3fa (patch) | |
| tree | 0ddf69a187f62d5470fe0b4ab955e3414bfdf2b1 | |
| parent | 11593a68b0ea23ed55f867e70df6a2bb947951fd (diff) | |
| download | frippy-5f1b2465080b7343869256c09ceda8025b30f3fa.tar.gz frippy-5f1b2465080b7343869256c09ceda8025b30f3fa.zip | |
Prepare environment for the Sandbox
| -rw-r--r-- | src/plugins/factoids.rs | 52 | ||||
| -rw-r--r-- | src/plugins/sandbox.lua | 1 |
2 files changed, 40 insertions, 13 deletions
diff --git a/src/plugins/factoids.rs b/src/plugins/factoids.rs index b3a4446..a6cb5c3 100644 --- a/src/plugins/factoids.rs +++ b/src/plugins/factoids.rs @@ -1,8 +1,8 @@ extern crate rlua; +use self::rlua::prelude::*; use irc::client::prelude::*; use irc::error::Error as IrcError; -use self::rlua::Lua; use std::collections::HashMap; use std::sync::Mutex; @@ -23,6 +23,8 @@ macro_rules! try_lock { } } +static LUA_SANDBOX: &'static str = include_str!("sandbox.lua"); + impl Factoids { pub fn new() -> Factoids { Factoids { factoids: Mutex::new(HashMap::new()) } @@ -59,21 +61,21 @@ impl Factoids { } } - fn exec(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { + fn exec(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { if command.tokens.len() < 1 { - self.invalid_command(server, command) + self.invalid_command(server, &command) } else { - let name = &command.tokens[0]; + let name = command.tokens.remove(0); let factoids = try_lock!(self.factoids); - let factoid = match factoids.get(name) { + let factoid = match factoids.get(&name) { Some(v) => v, - None => return self.invalid_command(server, command), + None => return self.invalid_command(server, &command), }; - let value = match self.run_lua(name, factoid) { + let value = match self.run_lua(&name, factoid, &command) { Ok(v) => v, Err(e) => format!("{}", e), }; @@ -82,9 +84,33 @@ impl Factoids { } } - fn run_lua(&self, name: &str, code: &str) -> Result<String, rlua::Error> { + 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 = Lua::new(); - lua.eval::<String>(code, Some(name)) + let globals = lua.globals(); + + globals.set("factoid", lua.load(code, Some(name))?)?; + 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, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { @@ -106,13 +132,13 @@ impl Plugin for Factoids { let t: Vec<String> = content.split(' ').map(ToOwned::to_owned).collect(); - let mut c = PluginCommand { + let c = PluginCommand { source: message.source_nickname().unwrap().to_string(), target: message.response_target().unwrap().to_string(), tokens: t, }; - self.exec(server, &mut c) + self.exec(server, c) } else { Ok(()) @@ -127,8 +153,8 @@ impl Plugin for Factoids { let sub_command = command.tokens.remove(0); match sub_command.as_ref() { "add" => self.add(server, &mut command), - "get" => self.get(server, &mut command), - "exec" => self.exec(server, &mut command), + "get" => self.get(server, &command), + "exec" => self.exec(server, command), _ => self.invalid_command(server, &command), } } diff --git a/src/plugins/sandbox.lua b/src/plugins/sandbox.lua new file mode 100644 index 0000000..9b4f52e --- /dev/null +++ b/src/plugins/sandbox.lua @@ -0,0 +1 @@ +factoid() |
