Changeset

1342:947d94e3619f

mod_console: Redirect print() to console session when executing commands in global environment
author Matthew Wild <mwild1@gmail.com>
date Fri, 12 Jun 2009 15:43:24 +0100
parents 1341:53decd1ee351
children 1343:a0bee511d144 1346:e34f9455b779
files plugins/mod_console.lua
diffstat 1 files changed, 7 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_console.lua	Fri Jun 12 15:42:43 2009 +0100
+++ b/plugins/mod_console.lua	Fri Jun 12 15:43:24 2009 +0100
@@ -8,6 +8,8 @@
 
 module.host = "*";
 
+local _G = _G;
+
 local prosody = _G.prosody;
 local hosts = prosody.hosts;
 local connlisteners_register = require "net.connlisteners".register;
@@ -21,6 +23,10 @@
 local def_env = {};
 local default_env_mt = { __index = def_env };
 
+local function redirect_output(_G, session)
+	return setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end, __newindex = function (t, k, v) rawset(_G, k, v); end });
+end
+
 console = {};
 
 function console:new_session(conn)
@@ -86,7 +92,7 @@
 				end
 			end
 			
-			setfenv(chunk, (useglobalenv and _G) or session.env or nil);
+			setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
 			
 			local ranok, taskok, message = pcall(chunk);