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 | 22 | ||||
-rw-r--r-- | plugins/storage/sqlbasic.lib.lua | 4 | ||||
-rwxr-xr-x | prosody | 8 | ||||
-rwxr-xr-x | prosodyctl | 6 | ||||
-rwxr-xr-x | tools/ejabberd2prosody.lua | 2 | ||||
-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/envload.lua | 34 | ||||
-rw-r--r-- | util/pluginloader.lua | 5 | ||||
-rw-r--r-- | util/prosodyctl.lua | 2 | ||||
-rw-r--r-- | util/serialization.lua | 6 |
16 files changed, 103 insertions, 61 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 572dc179..54077826 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -15,7 +15,7 @@ local pluginloader = require "util.pluginloader"; local timer = require "util.timer"; 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; @@ -97,12 +97,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 524b61be..ac4f4e3d 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 ca37ca7a..ebd817b5 100644 --- a/plugins/mod_admin_telnet.lua +++ b/plugins/mod_admin_telnet.lua @@ -15,12 +15,17 @@ local hosts = prosody.hosts; local console_listener = { default_port = 5582; default_mode = "*a"; interface = "127.0.0.1" }; +local hostmanager = require "core.hostmanager"; +local modulemanager = require "core.modulemanager"; + local iterators = require "util.iterators"; local keys, values = iterators.keys, iterators.values; 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"); @@ -30,9 +35,9 @@ local core_post_stanza = prosody.core_post_stanza; 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 @@ -104,9 +109,10 @@ function console_listener.onincoming(conn, data) session.env._ = line; local chunkname = "=console"; - local chunk, err = loadstring("return "..line, chunkname); + local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil + local chunk, err = envload("return "..line, chunkname, env); if not chunk then - chunk, err = loadstring(line, chunkname); + chunk, err = envload(line, chunkname, env); if not chunk then err = err:gsub("^%[string .-%]:%d+: ", ""); err = err:gsub("^:%d+: ", ""); @@ -115,9 +121,7 @@ function console_listener.onincoming(conn, data) break; 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[line:lower()] then @@ -642,6 +646,7 @@ local function print_errors(print, errors) end function def_env.s2s:showcert(domain) + local ser = require "util.serialization".serialize; local print = self.session.print; local domain_sessions = set.new(array.collect(keys(incoming_s2s))) /function(session) return session.from_host == domain and session or nil; end; @@ -973,8 +978,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; @@ -128,7 +128,7 @@ function log_dependency_warnings() end function sanity_check() - for host, host_config in pairs(configmanager.getconfig()) do + for host, host_config in pairs(config.getconfig()) do if host ~= "*" and host_config.core.enabled ~= false and not host_config.core.component_module then @@ -145,9 +145,13 @@ function sandbox_require() -- for neat sandboxing of modules local _realG = _G; local _real_require = require; + if not getfenv then + -- FIXME: This is a hack to replace getfenv() in Lua 5.2 + function getfenv(f) return debug.getupvalue(debug.getinfo(f or 1).func, 1); end + end function require(...) local curr_env = getfenv(2); - local curr_env_mt = getmetatable(getfenv(2)); + local curr_env_mt = getmetatable(curr_env); local _realG_mt = getmetatable(_realG); if curr_env_mt and curr_env_mt.__index and not curr_env_mt.__newindex and _realG_mt then local old_newindex, old_index; @@ -227,9 +227,9 @@ for hostname, config in pairs(config.getconfig()) do hosts[hostname] = make_host(hostname); end -require "core.modulemanager" +local modulemanager = require "core.modulemanager" -require "util.prosodyctl" +local prosodyctl = require "util.prosodyctl" require "socket" ----------------------- @@ -509,7 +509,7 @@ function commands.about(arg) return 1; end - require "util.array"; + local array = require "util.array"; local keys = require "util.iterators".keys; print("Prosody "..(prosody.version or "(unknown version)")); diff --git a/tools/ejabberd2prosody.lua b/tools/ejabberd2prosody.lua index 545bd922..656f0824 100755 --- a/tools/ejabberd2prosody.lua +++ b/tools/ejabberd2prosody.lua @@ -15,7 +15,7 @@ if arg[0]:match("^./") then package.path = package.path .. ";"..arg[0]:gsub("/ejabberd2prosody.lua$", "/?.lua"); end -require "erlparse"; +local erlparse = require "erlparse"; prosody = {}; 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 30c1e298..344d2eb1 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 %s storage ('%s') for user: %s@%s", datastore, ret, 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 %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); diff --git a/util/envload.lua b/util/envload.lua new file mode 100644 index 00000000..53e28348 --- /dev/null +++ b/util/envload.lua @@ -0,0 +1,34 @@ +-- Prosody IM +-- Copyright (C) 2008-2011 Florian Zeitz +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +local load, loadstring, loadfile, setfenv = load, loadstring, loadfile, setfenv; +local envload; +local envloadfile; + +if setfenv then + function envload(code, source, env) + local f, err = loadstring(code, source); + if f and env then setfenv(f, env); end + return f, err; + end + + function envloadfile(file, env) + local f, err = loadfile(file); + if f and env then setfenv(f, env); end + return f, err; + end +else + function envload(code, source, env) + return load(code, source, nil, env); + end + + function envloadfile(file, env) + return loadfile(file, nil, env); + end +end + +return { envload = envload, envloadfile = envloadfile }; 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/prosodyctl.lua b/util/prosodyctl.lua index 439de551..a598a44a 100644 --- a/util/prosodyctl.lua +++ b/util/prosodyctl.lua @@ -273,3 +273,5 @@ function reload() signal.kill(pid, signal.SIGHUP); return true; end + +return _M; 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; |