Datamanager Fixes and improvements
[prosody.git] / util / datamanager.lua
1 local format = string.format;
2 local setmetatable, type = setmetatable, type;
3 local pairs = pairs;
4 local char = string.char;
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
6 local log = log;
7 local io_open = io.open;
8 local tostring = tostring;
9 local error = error;
10
11 local indent = function(f, i)
12         for n = 1, i do
13                 f:write("\t");
14         end
15 end
16
17 module "datamanager"
18
19
20 ---- utils -----
21 local encode, decode;
22
23 local log = function (type, msg) return log(type, "datamanager", msg); end
24
25 do 
26         local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
27
28         decode = function (s)
29                 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
30         end
31
32         encode = function (s)
33                 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
34         end
35 end
36
37 local function basicSerialize (o)
38         if type(o) == "number" or type(o) == "boolean" then
39                 return tostring(o);
40         else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
41                 return (format("%q", tostring(o)):gsub("\\\n", "\\n"));
42         end
43 end
44
45
46 local function simplesave (f, o, ind)
47         if type(o) == "number" then
48                 f:write(o)
49         elseif type(o) == "string" then
50                 f:write((format("%q", o):gsub("\\\n", "\\n")))
51         elseif type(o) == "table" then
52                 f:write("{\n")
53                 for k,v in pairs(o) do
54                         indent(f, ind);
55                         f:write("[", basicSerialize(k), "] = ")
56                         simplesave(f, v, ind+1)
57                         f:write(",\n")
58                 end
59                 indent(f, ind-1);
60                 f:write("}")
61         elseif type(o) == "boolean" then
62                 f:write(o and "true" or "false");
63         else
64                 error("cannot serialize a " .. type(o))
65         end
66 end
67
68 ------- API -------------
69
70 function getpath(username, host, datastore)
71         if username then
72                 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
73         elseif host then
74                 return format("data/%s/%s.dat", encode(host), datastore);
75         else
76                 return format("data/%s.dat", datastore);
77         end
78 end
79
80 function load(username, host, datastore)
81         local data, ret = loadfile(getpath(username, host, datastore));
82         if not data then
83                 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
84                 return nil;
85         end
86         setfenv(data, {});
87         local success, ret = pcall(data);
88         if not success then
89                 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
90                 return nil;
91         end
92         return ret;
93 end
94
95 function store(username, host, datastore, data)
96         local f, msg = io_open(getpath(username, host, datastore), "w+");
97         if not f then
98                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
99                 return nil;
100         end
101         f:write("return ");
102         simplesave(f, data, 1);
103         f:close();
104         return true;
105 end
106
107 return _M;