Fixed URL encoding to generate %0x instead of %x
[prosody.git] / util / datamanager.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21 local format = string.format;
22 local setmetatable, type = setmetatable, type;
23 local pairs, ipairs = pairs, ipairs;
24 local char = string.char;
25 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
26 local log = require "util.logger".init("datamanager");
27 local io_open = io.open;
28 local os_remove = os.remove;
29 local tostring, tonumber = tostring, tonumber;
30 local error = error;
31 local next = next;
32 local t_insert = table.insert;
33 local append = require "util.serialization".append;
34
35 module "datamanager"
36
37 ---- utils -----
38 local encode, decode;
39 do 
40         local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
41
42         decode = function (s)
43                 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
44         end
45
46         encode = function (s)
47                 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
48         end
49 end
50
51 ------- API -------------
52
53 local data_path = "data";
54 function set_data_path(path)
55         log("info", "Setting data path to %s", path);
56         data_path = path;
57 end
58
59 function getpath(username, host, datastore, ext)
60         ext = ext or "dat";
61         if username then
62                 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
63         elseif host then
64                 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
65         else
66                 return format("%s/%s.%s", data_path, datastore, ext);
67         end
68 end
69
70 function load(username, host, datastore)
71         local data, ret = loadfile(getpath(username, host, datastore));
72         if not data then
73                 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
74                 return nil;
75         end
76         setfenv(data, {});
77         local success, ret = pcall(data);
78         if not success then
79                 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
80                 return nil;
81         end
82         return ret;
83 end
84
85 function store(username, host, datastore, data)
86         if not data then
87                 data = {};
88         end
89         -- save the datastore
90         local f, msg = io_open(getpath(username, host, datastore), "w+");
91         if not f then
92                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
93                 return;
94         end
95         f:write("return ");
96         append(f, data);
97         f:close();
98         if not next(data) then -- try to delete empty datastore
99                 os_remove(getpath(username, host, datastore));
100         end
101         -- we write data even when we are deleting because lua doesn't have a
102         -- platform independent way of checking for non-exisitng files
103         return true;
104 end
105
106 function list_append(username, host, datastore, data)
107         if not data then return; end
108         -- save the datastore
109         local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
110         if not f then
111                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
112                 return;
113         end
114         f:write("item(");
115         append(f, data);
116         f:write(");\n");
117         f:close();
118         return true;
119 end
120
121 function list_store(username, host, datastore, data)
122         if not data then
123                 data = {};
124         end
125         -- save the datastore
126         local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
127         if not f then
128                 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
129                 return;
130         end
131         for _, d in ipairs(data) do
132                 f:write("item(");
133                 append(f, d);
134                 f:write(");\n");
135         end
136         f:close();
137         if not next(data) then -- try to delete empty datastore
138                 os_remove(getpath(username, host, datastore, "list"));
139         end
140         -- we write data even when we are deleting because lua doesn't have a
141         -- platform independent way of checking for non-exisitng files
142         return true;
143 end
144
145 function list_load(username, host, datastore)
146         local data, ret = loadfile(getpath(username, host, datastore, "list"));
147         if not data then
148                 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
149                 return nil;
150         end
151         local items = {};
152         setfenv(data, {item = function(i) t_insert(items, i); end});
153         local success, ret = pcall(data);
154         if not success then
155                 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
156                 return nil;
157         end
158         return items;
159 end
160
161 return _M;