Bumper commit for the new modulemanager API \o/ Updates all the modules, though some...
[prosody.git] / core / modulemanager.lua
1
2
3 local logger = require "util.logger";
4 local log = logger.init("modulemanager")
5
6 local loadfile, pcall = loadfile, pcall;
7 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
8 local pairs, ipairs = pairs, ipairs;
9 local t_insert = table.insert;
10 local type = type;
11
12 local tostring, print = tostring, print;
13
14 local _G = _G;
15 local debug = debug;
16
17 module "modulemanager"
18
19 local api = {}; -- Module API container
20
21 local modulemap = {};
22
23 local handler_info = {};
24 local stanza_handlers = {};
25
26 local modulehelpers = setmetatable({}, { __index = _G });
27
28
29 function load(host, module_name, config)
30         local mod, err = loadfile("plugins/mod_"..module_name..".lua");
31         if not mod then
32                 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
33                 return nil, err;
34         end
35         
36         if not modulemap[host] then
37                 modulemap[host] = {};
38                 stanza_handlers[host] = {};
39         elseif modulemap[host][module_name] then
40                 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
41                 return nil, "module-already-loaded";
42         end
43         
44         local _log = logger.init(host..":"..module_name);
45         local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
46
47         local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
48         
49         setfenv(mod, pluginenv);
50         
51         local success, ret = pcall(mod);
52         if not success then
53                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
54                 return nil, ret;
55         end
56         
57         modulemap[host][module_name] = mod;
58         
59         return true;
60 end
61
62 function handle_stanza(host, origin, stanza)
63         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
64         
65         local handlers = stanza_handlers[host];
66         if not handlers then
67                 log("warn", "No handlers for %s", host);
68                 return false;
69         end
70         
71         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
72                 local child = stanza.tags[1];
73                 if child then
74                         local xmlns = child.attr.xmlns or xmlns;
75                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
76                         local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
77                         if handler then
78                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
79                                 return handler(origin, stanza) or true;
80                         end
81                 end
82         elseif handlers[origin_type] then
83                 local handler = handlers[origin_type][name];
84                 if  handler then
85                         handler = handler[xmlns];
86                         if handler then
87                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
88                                 return handler(origin, stanza) or true;
89                         end
90                 end
91         end
92         log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
93         return false; -- we didn't handle it
94 end
95
96 ----- API functions exposed to modules -----------
97 -- Must all be in api.* 
98
99 -- Returns the name of the current module
100 function api:get_name()
101         return self.name;
102 end
103
104 -- Returns the host that the current module is serving
105 function api:get_host()
106         return self.host;
107 end
108
109
110 local function _add_iq_handler(module, origin_type, xmlns, handler)
111         local handlers = stanza_handlers[module.host];
112         handlers[origin_type] = handlers[origin_type] or {};
113         handlers[origin_type].iq = handlers[origin_type].iq or {};
114         if not handlers[origin_type].iq[xmlns] then
115                 handlers[origin_type].iq[xmlns]= handler;
116                 handler_info[handler] = module;
117                 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
118         else
119                 module:log("warn", "I wanted to handle tag 'iq' [%s] with payload namespace '%s' but mod_%s already handles that", origin_type, xmlns, handler_info[handlers[origin_type].iq[xmlns]].name);
120         end
121 end
122
123 function api:add_iq_handler(origin_type, xmlns, handler)
124         if not (origin_type and handler and xmlns) then return false; end
125         if type(origin_type) == "table" then
126                 for _, origin_type in ipairs(origin_type) do
127                         _add_iq_handler(self, origin_type, xmlns, handler);
128                 end
129                 return;
130         end
131         _add_iq_handler(self, origin_type, xmlns, handler);
132 end
133
134
135 do
136         local event_handlers = {};
137         
138         function api:add_event_hook(name, handler)
139                 if not event_handlers[name] then
140                         event_handlers[name] = {};
141                 end
142                 t_insert(event_handlers[name] , handler);
143                 self:log("debug", "Subscribed to %s", name);
144         end
145         
146         function fire_event(name, ...)
147                 local event_handlers = event_handlers[name];
148                 if event_handlers then
149                         for name, handler in ipairs(event_handlers) do
150                                 handler(...);
151                         end
152                 end
153         end
154 end
155
156
157 local function _add_handler(module, origin_type, tag, xmlns, handler)
158         local handlers = stanza_handlers[module.host];
159         handlers[origin_type] = handlers[origin_type] or {};
160         if not handlers[origin_type][tag] then
161                 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
162                 handlers[origin_type][tag][xmlns]= handler;
163                 handler_info[handler] = module;
164                 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
165         elseif handler_info[handlers[origin_type][tag]] then
166                 log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[origin_type][tag]].module.name);
167         end
168 end
169
170 function api:add_handler(origin_type, tag, xmlns, handler)
171         if not (origin_type and tag and xmlns and handler) then return false; end
172         if type(origin_type) == "table" then
173                 for _, origin_type in ipairs(origin_type) do
174                         _add_handler(self, origin_type, tag, xmlns, handler);
175                 end
176                 return;
177         end
178         _add_handler(self, origin_type, tag, xmlns, handler);
179 end
180
181 --------------------------------------------------------------------
182
183 return _M;