diff options
-rw-r--r-- | core/configmanager.lua | 9 | ||||
-rw-r--r-- | core/moduleapi.lua | 7 | ||||
-rw-r--r-- | core/modulemanager.lua | 19 | ||||
-rw-r--r-- | core/rostermanager.lua | 2 | ||||
-rw-r--r-- | plugins/mod_admin_telnet.lua | 16 | ||||
-rw-r--r-- | plugins/storage/sqlbasic.lib.lua | 4 | ||||
-rw-r--r-- | tools/migration/migrator/prosody_files.lua | 1 | ||||
-rw-r--r-- | tools/migration/prosody-migrator.lua | 24 | ||||
-rw-r--r-- | util/datamanager.lua | 13 | ||||
-rw-r--r-- | util/pluginloader.lua | 5 | ||||
-rw-r--r-- | util/serialization.lua | 6 |
11 files changed, 52 insertions, 54 deletions
diff --git a/core/configmanager.lua b/core/configmanager.lua index e2253171..8fea3be5 100644 --- a/core/configmanager.lua +++ b/core/configmanager.lua @@ -13,6 +13,7 @@ local format, math_max = string.format, math.max; 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 @@ end -- 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 @@ do 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 diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 24d29dfe..2bcf9b84 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -17,7 +17,7 @@ local timer = require "util.timer"; 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 @@ end 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 diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 488319c3..9c5e4a4a 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -18,7 +18,7 @@ local hosts = hosts; 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 @@ local function do_load_module(host, module_name) 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 diff --git a/core/rostermanager.lua b/core/rostermanager.lua index 59ba6579..9d1ff5e2 100644 --- a/core/rostermanager.lua +++ b/core/rostermanager.lua @@ -13,7 +13,7 @@ local log = require "util.logger".init("rostermanager"); 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; diff --git a/plugins/mod_admin_telnet.lua b/plugins/mod_admin_telnet.lua index b56f9291..8787427e 100644 --- a/plugins/mod_admin_telnet.lua +++ b/plugins/mod_admin_telnet.lua @@ -21,6 +21,8 @@ local jid = require "util.jid"; 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 default_env_mt = { __index = def_env }; 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 @@ function console_listener.onincoming(conn, data) 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 @@ function console_listener.onincoming(conn, data) 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 option and option ~= "short" and option ~= "full" and option ~= "graphic" the 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 diff --git a/plugins/storage/sqlbasic.lib.lua b/plugins/storage/sqlbasic.lib.lua index f1202287..ab3648f9 100644 --- a/plugins/storage/sqlbasic.lib.lua +++ b/plugins/storage/sqlbasic.lib.lua @@ -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; diff --git a/tools/migration/migrator/prosody_files.lua b/tools/migration/migrator/prosody_files.lua index be0c49f8..4462fb3e 100644 --- a/tools/migration/migrator/prosody_files.lua +++ b/tools/migration/migrator/prosody_files.lua @@ -7,7 +7,6 @@ local char = string.char; local coroutine = coroutine; local lfs = require "lfs"; local loadfile = loadfile; -local setfenv = setfenv; local pcall = pcall; local mtools = require "migrator.mtools"; local next = next; diff --git a/tools/migration/prosody-migrator.lua b/tools/migration/prosody-migrator.lua index 2a8bf1c3..7c933b88 100644 --- a/tools/migration/prosody-migrator.lua +++ b/tools/migration/prosody-migrator.lua @@ -30,16 +30,22 @@ for i = 1, #arg do 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 @@ end 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; diff --git a/util/datamanager.lua b/util/datamanager.lua index a5d676cc..884d1161 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -11,7 +11,7 @@ local format = string.format; 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 error = error; 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 @@ function getpath(username, host, datastore, ext, create) 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 @@ function load(username, host, datastore) 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 @@ function list_store(username, host, datastore, data) 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 @@ function list_load(username, host, datastore) 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")); diff --git a/util/pluginloader.lua b/util/pluginloader.lua index 555e41bf..8b4dbd16 100644 --- a/util/pluginloader.lua +++ b/util/pluginloader.lua @@ -16,6 +16,7 @@ end 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 @@ function load_resource(plugin, resource) 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 diff --git a/util/serialization.lua b/util/serialization.lua index e193b64f..8a259184 100644 --- a/util/serialization.lua +++ b/util/serialization.lua @@ -16,11 +16,12 @@ local pairs = pairs; 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 @@ end 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; |