tools/ejabberd2prosody: Fixed private storage export
[prosody.git] / core / objectmanager.lua
1 \r
2 local new_multitable = require "util.multitable".new;\r
3 local t_insert = table.insert;\r
4 local t_concat = table.concat;\r
5 local tostring = tostring;\r
6 local unpack = unpack;\r
7 local pairs = pairs;\r
8 local error = error;\r
9 local type = type;\r
10 local _G = _G;\r
11 \r
12 local data = new_multitable();\r
13 \r
14 module "objectmanager"\r
15 \r
16 function set(...)\r
17         return data:set(...);\r
18 end\r
19 function remove(...)\r
20         return data:remove(...);\r
21 end\r
22 function get(...)\r
23         return data:get(...);\r
24 end\r
25 \r
26 local function get_path(path)\r
27         if type(path) == "table" then return path; end\r
28         local s = {};\r
29         for part in tostring(path):gmatch("[%w_]+") do\r
30                 t_insert(s, part);\r
31         end\r
32         return s;\r
33 end\r
34 \r
35 function get_object(path)\r
36         path = get_path(path)\r
37         return data:get(unpack(path)), path;\r
38 end\r
39 function set_object(path, object)\r
40         path = get_path(path);\r
41         data:set(unpack(path), object);\r
42 end\r
43 \r
44 data:set("ls", function(_dir)\r
45         local obj, dir = get_object(_dir);\r
46         if not obj then error("object not found: " .. t_concat(dir, '/')); end\r
47         local r = {};\r
48         if type(obj) == "table" then\r
49                 for key, val in pairs(obj) do\r
50                         r[key] = type(val);\r
51                 end\r
52         end\r
53         return r;\r
54 end);\r
55 data:set("get", get_object);\r
56 data:set("set", set_object);\r
57 data:set("echo", function(...) return {...}; end);\r
58 data:set("_G", _G);\r
59 \r
60 return _M;\r