prosody: Read list of possible config extensions from configmanager, removes old...
[prosody.git] / core / objectmanager.lua
1 -- Prosody IM
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 new_multitable = require "util.multitable".new;
11 local t_insert = table.insert;
12 local t_concat = table.concat;
13 local tostring = tostring;
14 local unpack = unpack;
15 local pairs = pairs;
16 local error = error;
17 local type = type;
18 local _G = _G;
19
20 local data = new_multitable();
21
22 module "objectmanager"
23
24 function set(...)
25         return data:set(...);
26 end
27 function remove(...)
28         return data:remove(...);
29 end
30 function get(...)
31         return data:get(...);
32 end
33
34 local function get_path(path)
35         if type(path) == "table" then return path; end
36         local s = {};
37         for part in tostring(path):gmatch("[%w_]+") do
38                 t_insert(s, part);
39         end
40         return s;
41 end
42
43 function get_object(path)
44         path = get_path(path)
45         return data:get(unpack(path)), path;
46 end
47 function set_object(path, object)
48         path = get_path(path);
49         data:set(unpack(path), object);
50 end
51
52 data:set("ls", function(_dir)
53         local obj, dir = get_object(_dir);
54         if not obj then error("object not found: " .. t_concat(dir, '/')); end
55         local r = {};
56         if type(obj) == "table" then
57                 for key, val in pairs(obj) do
58                         r[key] = type(val);
59                 end
60         end
61         return r;
62 end);
63 data:set("get", get_object);
64 data:set("set", set_object);
65 data:set("echo", function(...) return {...}; end);
66 data:set("_G", _G);
67
68 return _M;