2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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 tostring, tonumber = tostring, tonumber;
21 local t_insert = table.insert;
22 local append = require "util.serialization".append;
23 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
26 if prosody.platform == "posix" then
27 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask
29 raw_mkdir = require "lfs".mkdir;
37 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
40 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
44 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
49 local function mkdir(path)
50 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
51 if not _mkdir[path] then
58 local data_path = "data";
61 ------- API -------------
63 function set_data_path(path)
64 log("debug", "Setting data path to: %s", path);
68 local function callback(username, host, datastore, data)
69 for _, f in ipairs(callbacks) do
70 username, host, datastore, data = f(username, host, datastore, data);
71 if username == false then break; end
74 return username, host, datastore, data;
76 function add_callback(func)
77 if not callbacks[func] then -- Would you really want to set the same callback more than once?
78 callbacks[func] = true;
79 callbacks[#callbacks+1] = func;
83 function remove_callback(func)
84 if callbacks[func] then
85 for i, f in ipairs(callbacks) do
95 function getpath(username, host, datastore, ext, create)
97 host = (host and encode(host)) or "_global";
98 username = username and encode(username);
100 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
101 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
103 if create then mkdir(mkdir(data_path).."/"..host); end
104 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
106 if create then mkdir(data_path); end
107 return format("%s/%s.%s", data_path, datastore, ext);
111 function load(username, host, datastore)
112 local data, ret = loadfile(getpath(username, host, datastore));
114 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
118 local success, ret = pcall(data);
120 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
126 function store(username, host, datastore, data)
131 username, host, datastore, data = callback(username, host, datastore, data);
132 if username == false then
133 return true; -- Don't save this data at all
136 -- save the datastore
137 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
139 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
145 if next(data) == nil then -- try to delete empty datastore
146 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
147 os_remove(getpath(username, host, datastore));
149 -- we write data even when we are deleting because lua doesn't have a
150 -- platform independent way of checking for non-exisitng files
154 function list_append(username, host, datastore, data)
155 if not data then return; end
156 if callback(username, host, datastore) == false then return true; end
157 -- save the datastore
158 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
160 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
170 function list_store(username, host, datastore, data)
174 if callback(username, host, datastore) == false then return true; end
175 -- save the datastore
176 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
178 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
181 for _, d in ipairs(data) do
187 if next(data) == nil then -- try to delete empty datastore
188 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
189 os_remove(getpath(username, host, datastore, "list"));
191 -- we write data even when we are deleting because lua doesn't have a
192 -- platform independent way of checking for non-exisitng files
196 function list_load(username, host, datastore)
197 local data, ret = loadfile(getpath(username, host, datastore, "list"));
199 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
203 setfenv(data, {item = function(i) t_insert(items, i); end});
204 local success, ret = pcall(data);
206 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));