d313130c0e334c1b6a0876c9616a3017a23be7b2
[prosody.git] / core / modulemanager.lua
1
2 local log = require "util.logger".init("modulemanager")
3
4 local loadfile, pcall = loadfile, pcall;
5 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
6 local pairs, ipairs = pairs, ipairs;
7 local t_insert = table.insert;
8 local type = type;
9
10 local tostring, print = tostring, print;
11
12 local _G = _G;
13 local debug = debug;
14
15 module "modulemanager"
16
17 local handler_info = {};
18 local handlers = {};
19                                         
20 local modulehelpers = setmetatable({}, { __index = _G });
21
22 local function _add_iq_handler(module, origin_type, xmlns, handler)
23         handlers[origin_type] = handlers[origin_type] or {};
24         handlers[origin_type].iq = handlers[origin_type].iq or {};
25         if not handlers[origin_type].iq[xmlns] then
26                 handlers[origin_type].iq[xmlns]= handler;
27                 handler_info[handler] = module;
28                 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", module.name, xmlns);
29         else
30                 log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
31         end
32 end
33
34 function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
35         if not (origin_type and handler and xmlns) then return false; end
36         if type(origin_type) == "table" then
37                 for _, origin_type in ipairs(origin_type) do
38                         _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
39                 end
40                 return;
41         end
42         _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
43 end
44
45 local function _add_handler(module, origin_type, tag, xmlns, handler)
46         handlers[origin_type] = handlers[origin_type] or {};
47         if not handlers[origin_type][tag] then
48                 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
49                 handlers[origin_type][tag][xmlns]= handler;
50                 handler_info[handler] = module;
51                 log("debug", "mod_%s now handles tag '%s'", module.name, tag);
52         elseif handler_info[handlers[origin_type][tag]] then
53                 log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
54         end
55 end
56
57 function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
58         if not (origin_type and tag and xmlns and handler) then return false; end
59         if type(origin_type) == "table" then
60                 for _, origin_type in ipairs(origin_type) do
61                         _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
62                 end
63                 return;
64         end
65         _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
66 end
67
68 function load(name)
69         local mod, err = loadfile("plugins/mod_"..name..".lua");
70         if not mod then
71                 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
72                 return nil, err;
73         end
74         
75         local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
76         
77         setfenv(mod, pluginenv);
78         local success, ret = pcall(mod);
79         if not success then
80                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
81                 return nil, ret;
82         end
83         return true;
84 end
85
86 function handle_stanza(origin, stanza)
87         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
88         
89         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
90                 log("debug", "Stanza is an <iq/>");
91                 local child = stanza.tags[1];
92                 if child then
93                         local xmlns = child.attr.xmlns or xmlns;
94                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
95                         local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
96                         if handler then
97                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
98                                 return handler(origin, stanza) or true;
99                         end
100                 end
101         elseif handlers[origin_type] then
102                 local handler = handlers[origin_type][name];
103                 if  handler then
104                         handler = handler[xmlns];
105                         if handler then
106                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
107                                 return handler(origin, stanza) or true;
108                         end
109                 end
110         end
111         log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
112         return false; -- we didn't handle it
113 end
114
115 do
116         local event_handlers = {};
117         
118         function modulehelpers.add_event_hook(name, handler)
119                 if not event_handlers[name] then
120                         event_handlers[name] = {};
121                 end
122                 t_insert(event_handlers[name] , handler);
123         end
124         
125         function fire_event(name, ...)
126                 local event_handlers = event_handlers[name];
127                 if event_handlers then
128                         for name, handler in ipairs(event_handlers) do
129                                 handler(...);
130                         end
131                 end
132         end
133 end
134
135 return _M;