aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/datamanager.lua13
-rw-r--r--util/pluginloader.lua5
-rw-r--r--util/serialization.lua6
3 files changed, 13 insertions, 11 deletions
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;