Merge (for some reason)
[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 os_remove = os.remove;
9 local tostring = tostring;
10 local error = error;
11 local next = next;
12
13 local indent = function(f, i)
14         for n = 1, i do
15                 f:write("\t");
16         end
17 end
18
19 module "datamanager"
20
21
22 ---- utils -----
23 local encode, decode;
24
25 local log = function (type, msg) return log(type, "datamanager", msg); end
26
27 do 
28         local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
29
30         decode = function (s)
31                 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
32         end
33
34         encode = function (s)
35                 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
36         end
37 end
38
39 local function basicSerialize (o)
40         if type(o) == "number" or type(o) == "boolean" then
41                 return tostring(o);
42         else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
43                 return (format("%q", tostring(o)):gsub("\\\n", "\\n"));
44         end
45 end
46
47
48 local function simplesave (f, o, ind)
49         if type(o) == "number" then
50                 f:write(o)
51         elseif type(o) == "string" then
52                 f:write((format("%q", o):gsub("\\\n", "\\n")))
53         elseif type(o) == "table" then
54                 f:write("{\n")
55                 for k,v in pairs(o) do
56                         indent(f, ind);
57                         f:write("[", basicSerialize(k), "] = ")
58                         simplesave(f, v, ind+1)
59                         f:write(",\n")
60                 end
61                 indent(f, ind-1);
62                 f:write("}")
63         elseif type(o) == "boolean" then
64                 f:write(o and "true" or "false");
65         else
66                 error("cannot serialize a " .. type(o))
67         end
68 end
69
70 ------- API -------------
71
72 function getpath(username, host, datastore)
73         if username then
74                 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
75         elseif host then
76                 return format("data/%s/%s.dat", encode(host), datastore);
77         else
78                 return format("data/%s.dat", datastore);
79         end
80 end
81
82 function load(username, host, datastore)
83         local data, ret = loadfile(getpath(username, host, datastore));
84         if not data then
85                 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
86                 return nil;
87         end
88         setfenv(data, {});
89         local success, ret = pcall(data);
90         if not success then
91                 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
92                 return nil;
93         end
94         return ret;
95 end
96
97 function store(username, host, datastore, data)
98         if not data then
99                 data = {};
100         end
101         -- save the datastore
102         local f, msg = io_open(getpath(username, host, datastore), "w+");
103         if not f then
104                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
105                 return;
106         end
107         f:write("return ");
108         simplesave(f, data, 1);
109         f:close();
110         if not next(data) then -- try to delete empty datastore
111                 os_remove(getpath(username, host, datastore));
112         end
113         -- we write data even when we are deleting because lua doesn't have a
114         -- platform independent way of checking for non-exisitng files
115         return true;
116 end
117
118 return _M;