Automated merge with ssh://hg@prosody.im/prosody-hg
[prosody.git] / util / datamanager.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local format = string.format;
11 local setmetatable, type = setmetatable, type;
12 local pairs, ipairs = pairs, ipairs;
13 local char = string.char;
14 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
15 local log = require "util.logger".init("datamanager");
16 local io_open = io.open;
17 local os_remove = os.remove;
18 local io_popen = io.popen;
19 local tostring, tonumber = tostring, tonumber;
20 local error = error;
21 local next = next;
22 local t_insert = table.insert;
23 local append = require "util.serialization".append;
24 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
25
26 module "datamanager"
27
28 ---- utils -----
29 local encode, decode;
30 do 
31         local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
32
33         decode = function (s)
34                 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
35         end
36
37         encode = function (s)
38                 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
39         end
40 end
41
42 local _mkdir = {};
43 local function mkdir(path)
44         path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
45         if not _mkdir[path] then
46                 local x = io_popen("mkdir \""..path.."\" 2>&1"):read("*a");
47                 _mkdir[path] = true;
48         end
49         return path;
50 end
51
52 local data_path = "data";
53 local callback;
54
55 ------- API -------------
56
57 function set_data_path(path)
58         log("info", "Setting data path to: %s", path);
59         data_path = path;
60 end
61 function set_callback(func)
62         callback = func;
63 end
64
65 function getpath(username, host, datastore, ext, create)
66         ext = ext or "dat";
67         host = host and encode(host);
68         username = username and encode(username);
69         if username then
70                 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
71                 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
72         elseif host then
73                 if create then mkdir(mkdir(data_path).."/"..host); end
74                 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
75         else
76                 if create then mkdir(data_path); end
77                 return format("%s/%s.%s", data_path, datastore, ext);
78         end
79 end
80
81 function load(username, host, datastore)
82         local data, ret = loadfile(getpath(username, host, datastore));
83         if not data then
84                 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
85                 return nil;
86         end
87         setfenv(data, {});
88         local success, ret = pcall(data);
89         if not success then
90                 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
91                 return nil;
92         end
93         return ret;
94 end
95
96 function store(username, host, datastore, data)
97         if not data then
98                 data = {};
99         end
100         if callback and callback(username, host, datastore) then return true; end
101         -- save the datastore
102         local f, msg = io_open(getpath(username, host, datastore, nil, true), "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         append(f, data);
109         f:close();
110         if next(data) == nil then -- try to delete empty datastore
111                 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username, host);
112                 os_remove(getpath(username, host, datastore));
113         end
114         -- we write data even when we are deleting because lua doesn't have a
115         -- platform independent way of checking for non-exisitng files
116         return true;
117 end
118
119 function list_append(username, host, datastore, data)
120         if not data then return; end
121         if callback and callback(username, host, datastore) then return true; end
122         -- save the datastore
123         local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
124         if not f then
125                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
126                 return;
127         end
128         f:write("item(");
129         append(f, data);
130         f:write(");\n");
131         f:close();
132         return true;
133 end
134
135 function list_store(username, host, datastore, data)
136         if not data then
137                 data = {};
138         end
139         if callback and callback(username, host, datastore) then return true; end
140         -- save the datastore
141         local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
142         if not f then
143                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
144                 return;
145         end
146         for _, d in ipairs(data) do
147                 f:write("item(");
148                 append(f, d);
149                 f:write(");\n");
150         end
151         f:close();
152         if next(data) == nil then -- try to delete empty datastore
153                 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username, host);
154                 os_remove(getpath(username, host, datastore, "list"));
155         end
156         -- we write data even when we are deleting because lua doesn't have a
157         -- platform independent way of checking for non-exisitng files
158         return true;
159 end
160
161 function list_load(username, host, datastore)
162         local data, ret = loadfile(getpath(username, host, datastore, "list"));
163         if not data then
164                 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
165                 return nil;
166         end
167         local items = {};
168         setfenv(data, {item = function(i) t_insert(items, i); end});
169         local success, ret = pcall(data);
170         if not success then
171                 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
172                 return nil;
173         end
174         return items;
175 end
176
177 return _M;