Comparison

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
comparison
equal deleted inserted replaced
5020:ef1eb65acbba 5021:85b2689dbcfe
19 local keys, values = iterators.keys, iterators.values; 19 local keys, values = iterators.keys, iterators.values;
20 local jid = require "util.jid"; 20 local jid = require "util.jid";
21 local jid_bare, jid_split = jid.bare, jid.split; 21 local jid_bare, jid_split = jid.bare, jid.split;
22 local set, array = require "util.set", require "util.array"; 22 local set, array = require "util.set", require "util.array";
23 local cert_verify_identity = require "util.x509".verify_identity; 23 local cert_verify_identity = require "util.x509".verify_identity;
24 local envload = require "util.envload".envload;
25 local envloadfile = require "util.envload".envloadfile;
24 26
25 local commands = module:shared("commands") 27 local commands = module:shared("commands")
26 local def_env = module:shared("env"); 28 local def_env = module:shared("env");
27 local default_env_mt = { __index = def_env }; 29 local default_env_mt = { __index = def_env };
28 30
29 local function redirect_output(_G, session) 31 local function redirect_output(_G, session)
30 local env = setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end }); 32 local env = setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end });
31 env.dofile = function(name) 33 env.dofile = function(name)
32 local f, err = loadfile(name); 34 local f, err = envloadfile(name, env);
33 if not f then return f, err; end 35 if not f then return f, err; end
34 return setfenv(f, env)(); 36 return f();
35 end; 37 end;
36 return env; 38 return env;
37 end 39 end
38 40
39 console = {}; 41 console = {};
96 end 98 end
97 99
98 session.env._ = data; 100 session.env._ = data;
99 101
100 local chunkname = "=console"; 102 local chunkname = "=console";
101 local chunk, err = loadstring("return "..data, chunkname); 103 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil
104 local chunk, err = envload("return "..data, chunkname, env);
102 if not chunk then 105 if not chunk then
103 chunk, err = loadstring(data, chunkname); 106 chunk, err = envload(data, chunkname, env);
104 if not chunk then 107 if not chunk then
105 err = err:gsub("^%[string .-%]:%d+: ", ""); 108 err = err:gsub("^%[string .-%]:%d+: ", "");
106 err = err:gsub("^:%d+: ", ""); 109 err = err:gsub("^:%d+: ", "");
107 err = err:gsub("'<eof>'", "the end of the line"); 110 err = err:gsub("'<eof>'", "the end of the line");
108 session.print("Sorry, I couldn't understand that... "..err); 111 session.print("Sorry, I couldn't understand that... "..err);
109 return; 112 return;
110 end 113 end
111 end 114 end
112
113 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
114 115
115 local ranok, taskok, message = pcall(chunk); 116 local ranok, taskok, message = pcall(chunk);
116 117
117 if not (ranok or message or useglobalenv) and commands[data:lower()] then 118 if not (ranok or message or useglobalenv) and commands[data:lower()] then
118 commands[data:lower()](session, data); 119 commands[data:lower()](session, data);
878 end 879 end
879 if option and option ~= "short" and option ~= "full" and option ~= "graphic" then 880 if option and option ~= "short" and option ~= "full" and option ~= "graphic" then
880 if type(option) == "string" then 881 if type(option) == "string" then
881 session.print(option) 882 session.print(option)
882 elseif type(option) == "function" then 883 elseif type(option) == "function" then
883 setfenv(option, redirect_output(_G, session)); 884 module:log("warn", "Using functions as value for the console_banner option is no longer supported");
884 pcall(option, session);
885 end 885 end
886 end 886 end
887 end 887 end
888 888
889 module:add_item("net-provider", { 889 module:add_item("net-provider", {