Diff

plugins/mod_admin_telnet.lua @ 5021:85b2689dbcfe

Eliminate direct setfenv usage
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 08 Jun 2012 05:04:38 +0200
parent 4913:02dbed57a355
child 5023:dcc8e789df36
line wrap: on
line diff
--- a/plugins/mod_admin_telnet.lua	Thu Jun 07 23:27:26 2012 +0200
+++ b/plugins/mod_admin_telnet.lua	Fri Jun 08 05:04:38 2012 +0200
@@ -21,6 +21,8 @@
 local jid_bare, jid_split = jid.bare, jid.split;
 local set, array = require "util.set", require "util.array";
 local cert_verify_identity = require "util.x509".verify_identity;
+local envload = require "util.envload".envload;
+local envloadfile = require "util.envload".envloadfile;
 
 local commands = module:shared("commands")
 local def_env = module:shared("env");
@@ -29,9 +31,9 @@
 local function redirect_output(_G, session)
 	local env = setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end });
 	env.dofile = function(name)
-		local f, err = loadfile(name);
+		local f, err = envloadfile(name, env);
 		if not f then return f, err; end
-		return setfenv(f, env)();
+		return f();
 	end;
 	return env;
 end
@@ -98,9 +100,10 @@
 		session.env._ = data;
 		
 		local chunkname = "=console";
-		local chunk, err = loadstring("return "..data, chunkname);
+		local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil
+		local chunk, err = envload("return "..data, chunkname, env);
 		if not chunk then
-			chunk, err = loadstring(data, chunkname);
+			chunk, err = envload(data, chunkname, env);
 			if not chunk then
 				err = err:gsub("^%[string .-%]:%d+: ", "");
 				err = err:gsub("^:%d+: ", "");
@@ -110,8 +113,6 @@
 			end
 		end
 		
-		setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
-		
 		local ranok, taskok, message = pcall(chunk);
 		
 		if not (ranok or message or useglobalenv) and commands[data:lower()] then
@@ -880,8 +881,7 @@
 	if type(option) == "string" then
 		session.print(option)
 	elseif type(option) == "function" then
-		setfenv(option, redirect_output(_G, session));
-		pcall(option, session);
+		module:log("warn", "Using functions as value for the console_banner option is no longer supported");
 	end
 end
 end