Software /
code /
prosody
Changeset
5021:85b2689dbcfe
Eliminate direct setfenv usage
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 08 Jun 2012 05:04:38 +0200 |
parents | 5020:ef1eb65acbba |
children | 5022:776a57ca0d84 |
files | core/configmanager.lua core/moduleapi.lua core/modulemanager.lua core/rostermanager.lua plugins/mod_admin_telnet.lua plugins/storage/sqlbasic.lib.lua tools/migration/migrator/prosody_files.lua tools/migration/prosody-migrator.lua util/datamanager.lua util/pluginloader.lua util/serialization.lua |
diffstat | 11 files changed, 52 insertions(+), 54 deletions(-) [+] |
line wrap: on
line diff
--- a/core/configmanager.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/core/configmanager.lua Fri Jun 08 05:04:38 2012 +0200 @@ -13,6 +13,7 @@ local fire_event = prosody and prosody.events.fire_event or function () end; +local envload = require"util.envload".envload; local lfs = require "lfs"; local path_sep = package.config:sub(1,1); @@ -164,8 +165,8 @@ -- Built-in Lua parser do - local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable; - local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring; + local pcall, setmetatable = _G.pcall, _G.setmetatable; + local rawget, tostring = _G.rawget, _G.tostring; parsers.lua = {}; function parsers.lua.load(data, config_file, config) local env; @@ -263,14 +264,12 @@ return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file)); end - local chunk, err = loadstring(data, "@"..config_file); + local chunk, err = envload(data, "@"..config_file, env); if not chunk then return nil, err; end - setfenv(chunk, env); - local ok, err = pcall(chunk); if not ok then
--- a/core/moduleapi.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/core/moduleapi.lua Fri Jun 08 05:04:38 2012 +0200 @@ -17,7 +17,7 @@ local multitable_new = require "util.multitable".new; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; -local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type; +local error, setmetatable, type = error, setmetatable, type; local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack; local tonumber, tostring = tonumber, tostring; @@ -99,12 +99,11 @@ api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9 function api:require(lib) - local f, n = pluginloader.load_code(self.name, lib..".lib.lua"); + local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment); if not f then - f, n = pluginloader.load_code(lib, lib..".lib.lua"); + f, n = pluginloader.load_code(lib, lib..".lib.lua", self.environment); end if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message - setfenv(f, self.environment); return f(); end
--- a/core/modulemanager.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/core/modulemanager.lua Fri Jun 08 05:04:38 2012 +0200 @@ -18,7 +18,7 @@ local prosody = prosody; local pcall, xpcall = pcall, xpcall; -local setmetatable, rawget, setfenv = setmetatable, rawget, setfenv; +local setmetatable, rawget = setmetatable, rawget; local pairs, type, tostring = pairs, type, tostring; local debug_traceback = debug.traceback; @@ -152,22 +152,23 @@ end - local mod, err = pluginloader.load_code(module_name); - if not mod then - log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); - return nil, err; - end local _log = logger.init(host..":"..module_name); - local api_instance = setmetatable({ name = module_name, host = host, path = err, + local api_instance = setmetatable({ name = module_name, host = host, _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable() } , { __index = api }); local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); api_instance.environment = pluginenv; - setfenv(mod, pluginenv); - + local mod, err = pluginloader.load_code(module_name, nil, pluginenv); + if not mod then + log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); + return nil, err; + end + + api_instance.path = err; + modulemap[host][module_name] = pluginenv; local ok, err = pcall(mod); if ok then
--- a/core/rostermanager.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/core/rostermanager.lua Fri Jun 08 05:04:38 2012 +0200 @@ -13,7 +13,7 @@ local setmetatable = setmetatable; local format = string.format; -local loadfile, setfenv, pcall = loadfile, setfenv, pcall; +local pcall = pcall; local pairs, ipairs = pairs, ipairs; local tostring = tostring;
--- 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
--- a/plugins/storage/sqlbasic.lib.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/plugins/storage/sqlbasic.lib.lua Fri Jun 08 05:04:38 2012 +0200 @@ -3,12 +3,12 @@ -- This driver stores data as simple key-values local ser = require "util.serialization".serialize; +local envload = require "util.envload".envload; local deser = function(data) module:log("debug", "deser: %s", tostring(data)); if not data then return nil; end - local f = loadstring("return "..data); + local f = envload("return "..data, nil, {}); if not f then return nil; end - setfenv(f, {}); local s, d = pcall(f); if not s then return nil; end return d;
--- a/tools/migration/migrator/prosody_files.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/tools/migration/migrator/prosody_files.lua Fri Jun 08 05:04:38 2012 +0200 @@ -7,7 +7,6 @@ local coroutine = coroutine; local lfs = require "lfs"; local loadfile = loadfile; -local setfenv = setfenv; local pcall = pcall; local mtools = require "migrator.mtools"; local next = next;
--- a/tools/migration/prosody-migrator.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/tools/migration/prosody-migrator.lua Fri Jun 08 05:04:38 2012 +0200 @@ -30,16 +30,22 @@ end table.remove(arg, handled_opts); +if CFG_SOURCEDIR then + package.path = CFG_SOURCEDIR.."/?.lua;"..package.path; + package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath; +else + package.path = "../../?.lua;"..package.path + package.cpath = "../../?.so;"..package.cpath +end + +local envloadfile = require "util.envload".envloadfile; + -- Load config file local function loadfilein(file, env) if loadin then return loadin(env, io.open(file):read("*a")); else - local chunk, err = loadfile(file); - if chunk then - setfenv(chunk, env); - end - return chunk, err; + return envloadfile(file, env); end end @@ -59,14 +65,6 @@ config_chunk(); -if CFG_SOURCEDIR then - package.path = CFG_SOURCEDIR.."/?.lua;"..package.path; - package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath; -elseif not package.loaded["util.json"] then - package.path = "../../?.lua;"..package.path - package.cpath = "../../?.so;"..package.cpath -end - local have_err; if #arg > 0 and #arg ~= 2 then have_err = true;
--- a/util/datamanager.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/util/datamanager.lua Fri Jun 08 05:04:38 2012 +0200 @@ -11,7 +11,7 @@ local setmetatable, type = setmetatable, type; local pairs, ipairs = pairs, ipairs; local char = string.char; -local loadfile, setfenv, pcall = loadfile, setfenv, pcall; +local pcall = pcall; local log = require "util.logger".init("datamanager"); local io_open = io.open; local os_remove = os.remove; @@ -20,6 +20,7 @@ local next = next; local t_insert = table.insert; local append = require "util.serialization".append; +local envloadfile = require"util.envload".envloadfile; local path_separator = assert ( package.config:match ( "^([^\n]+)" ) , "package.config not in standard form" ) -- Extract directory seperator from package.config (an undocumented string that comes with lua) local lfs = require "lfs"; local prosody = prosody; @@ -111,7 +112,7 @@ end function load(username, host, datastore) - local data, ret = loadfile(getpath(username, host, datastore)); + local data, ret = envloadfile(getpath(username, host, datastore), {}); if not data then local mode = lfs.attributes(getpath(username, host, datastore), "mode"); if not mode then @@ -123,7 +124,7 @@ return nil, "Error reading storage"; end end - setfenv(data, {}); + local success, ret = pcall(data); if not success then log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); @@ -203,7 +204,8 @@ end function list_load(username, host, datastore) - local data, ret = loadfile(getpath(username, host, datastore, "list")); + local items = {}; + local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); if not data then local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); if not mode then @@ -215,8 +217,7 @@ return nil, "Error reading storage"; end end - local items = {}; - setfenv(data, {item = function(i) t_insert(items, i); end}); + local success, ret = pcall(data); if not success then log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
--- a/util/pluginloader.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/util/pluginloader.lua Fri Jun 08 05:04:38 2012 +0200 @@ -16,6 +16,7 @@ local io_open, os_time = io.open, os.time; local loadstring, pairs = loadstring, pairs; +local envload = require "util.envload".envload; module "pluginloader" @@ -48,11 +49,11 @@ return load_file(names); end -function load_code(plugin, resource) +function load_code(plugin, resource, env) local content, err = load_resource(plugin, resource); if not content then return content, err; end local path = err; - local f, err = loadstring(content, "@"..path); + local f, err = envload(content, "@"..path, env); if not f then return f, err; end return f, path; end
--- a/util/serialization.lua Thu Jun 07 23:27:26 2012 +0200 +++ b/util/serialization.lua Fri Jun 08 05:04:38 2012 +0200 @@ -16,11 +16,12 @@ local next = next; local loadstring = loadstring; -local setfenv = setfenv; local pcall = pcall; local debug_traceback = debug.traceback; local log = require "util.logger".init("serialization"); +local envload = require"util.envload".envload; + module "serialization" local indent = function(i) @@ -84,9 +85,8 @@ function deserialize(str) if type(str) ~= "string" then return nil; end str = "return "..str; - local f, err = loadstring(str, "@data"); + local f, err = envload(str, "@data", {}); if not f then return nil, err; end - setfenv(f, {}); local success, ret = pcall(f); if not success then return nil, ret; end return ret;