aboutsummaryrefslogtreecommitdiffstats
path: root/util/datamanager.lua
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2008-11-15 13:47:17 +0100
committerTobias Markmann <tm@ayena.de>2008-11-15 13:47:17 +0100
commitefb34b5c4af68c37a568e61986a0b93535a50814 (patch)
treeba77cb7ad14158323a31bfb9e332c508e94d5b31 /util/datamanager.lua
parent18e785078a2edf69bf4ec728290a18de2ed28cd7 (diff)
parentaefcb845c34c7bf15a370812b28b5da27fbc983b (diff)
downloadprosody-efb34b5c4af68c37a568e61986a0b93535a50814.tar.gz
prosody-efb34b5c4af68c37a568e61986a0b93535a50814.zip
Merging my new SASL code with Waqas' adjusted saslauth module.
Diffstat (limited to 'util/datamanager.lua')
-rw-r--r--util/datamanager.lua71
1 files changed, 64 insertions, 7 deletions
diff --git a/util/datamanager.lua b/util/datamanager.lua
index aad370d1..80b35733 100644
--- a/util/datamanager.lua
+++ b/util/datamanager.lua
@@ -1,14 +1,15 @@
local format = string.format;
local setmetatable, type = setmetatable, type;
-local pairs = pairs;
+local pairs, ipairs = pairs, ipairs;
local char = string.char;
local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
local log = log;
local io_open = io.open;
local os_remove = os.remove;
-local tostring = tostring;
+local tostring, tonumber = tostring, tonumber;
local error = error;
local next = next;
+local t_insert = table.insert;
local indent = function(f, i)
for n = 1, i do
@@ -69,13 +70,14 @@ end
------- API -------------
-function getpath(username, host, datastore)
+function getpath(username, host, datastore, ext)
+ ext = ext or "dat";
if username then
- return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
+ return format("data/%s/%s/%s.%s", encode(host), datastore, encode(username), ext);
elseif host then
- return format("data/%s/%s.dat", encode(host), datastore);
+ return format("data/%s/%s.%s", encode(host), datastore, ext);
else
- return format("data/%s.dat", datastore);
+ return format("data/%s.%s", datastore, ext);
end
end
@@ -115,4 +117,59 @@ function store(username, host, datastore, data)
return true;
end
-return _M; \ No newline at end of file
+function list_append(username, host, datastore, data)
+ if not data then return; end
+ -- save the datastore
+ local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
+ if not f then
+ log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return;
+ end
+ f:write("item(");
+ simplesave(f, data, 1);
+ f:write(");\n");
+ f:close();
+ return true;
+end
+
+function list_store(username, host, datastore, data)
+ if not data then
+ data = {};
+ end
+ -- save the datastore
+ local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
+ if not f then
+ log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return;
+ end
+ for _, d in ipairs(data) do
+ f:write("item(");
+ simplesave(f, d, 1);
+ f:write(");\n");
+ end
+ f:close();
+ if not next(data) then -- try to delete empty datastore
+ os_remove(getpath(username, host, datastore, "list"));
+ end
+ -- we write data even when we are deleting because lua doesn't have a
+ -- platform independent way of checking for non-exisitng files
+ return true;
+end
+
+function list_load(username, host, datastore)
+ local data, ret = loadfile(getpath(username, host, datastore, "list"));
+ if not data then
+ log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return nil;
+ 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"));
+ return nil;
+ end
+ return items;
+end
+
+return _M;