diff options
Diffstat (limited to 'util')
-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 |
5 files changed, 49 insertions, 11 deletions
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; |