Software /
code /
prosody
Comparison
core/modulemanager.lua @ 3536:fab65a4692ac
modulemanager: Removed legacy events API, and related code.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 16 Oct 2010 07:31:50 +0500 |
parent | 3530:73909cca846c |
child | 3538:3ea38f44420c |
comparison
equal
deleted
inserted
replaced
3535:b953b0c0f203 | 3536:fab65a4692ac |
---|---|
49 api = {}; | 49 api = {}; |
50 local api = api; -- Module API container | 50 local api = api; -- Module API container |
51 | 51 |
52 local modulemap = { ["*"] = {} }; | 52 local modulemap = { ["*"] = {} }; |
53 | 53 |
54 local stanza_handlers = multitable_new(); | |
55 local handler_info = {}; | |
56 | |
57 local modulehelpers = setmetatable({}, { __index = _G }); | 54 local modulehelpers = setmetatable({}, { __index = _G }); |
58 | 55 |
59 local handler_table = multitable_new(); | |
60 local hooked = multitable_new(); | 56 local hooked = multitable_new(); |
61 local hooks = multitable_new(); | 57 local hooks = multitable_new(); |
62 local event_hooks = multitable_new(); | 58 local event_hooks = multitable_new(); |
63 | 59 |
64 local NULL = {}; | 60 local NULL = {}; |
188 | 184 |
189 if module_has_method(mod, "unload") then | 185 if module_has_method(mod, "unload") then |
190 local ok, err = call_module_method(mod, "unload"); | 186 local ok, err = call_module_method(mod, "unload"); |
191 if (not ok) and err then | 187 if (not ok) and err then |
192 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); | 188 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); |
193 end | |
194 end | |
195 local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns} | |
196 for _, param in pairs(params or NULL) do | |
197 local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]); | |
198 if handlers then | |
199 handler_info[handlers[1]] = nil; | |
200 stanza_handlers:remove(param[1], param[2], param[3], param[4]); | |
201 end | 189 end |
202 end | 190 end |
203 event_hooks:remove(host, name); | 191 event_hooks:remove(host, name); |
204 -- unhook event handlers hooked by module:hook | 192 -- unhook event handlers hooked by module:hook |
205 for event, handlers in pairs(hooks:get(host, name) or NULL) do | 193 for event, handlers in pairs(hooks:get(host, name) or NULL) do |
273 else | 261 else |
274 log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type); | 262 log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type); |
275 return true; | 263 return true; |
276 end | 264 end |
277 end | 265 end |
278 local handlers = stanza_handlers:get(host, origin_type, name, xmlns); | 266 if stanza.attr.xmlns == nil then |
279 if not handlers then handlers = stanza_handlers:get("*", origin_type, name, xmlns); end | 267 log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it |
280 if handlers then | 268 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then |
281 log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name); | 269 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
282 (handlers[1])(origin, stanza); | 270 end |
283 return true; | 271 elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features |
284 else | 272 log("warn", "Unhandled %s stream element: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it |
285 if stanza.attr.xmlns == nil then | 273 origin:close("unsupported-stanza-type"); |
286 log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it | |
287 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then | |
288 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
289 end | |
290 elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features | |
291 log("warn", "Unhandled %s stream element: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it | |
292 origin:close("unsupported-stanza-type"); | |
293 end | |
294 end | 274 end |
295 end | 275 end |
296 | 276 |
297 function module_has_method(module, method) | 277 function module_has_method(module, method) |
298 return type(module.module[method]) == "function"; | 278 return type(module.module[method]) == "function"; |
328 self.host = "*"; | 308 self.host = "*"; |
329 -- Update the logger | 309 -- Update the logger |
330 local _log = logger.init("mod_"..self.name); | 310 local _log = logger.init("mod_"..self.name); |
331 self.log = function (self, ...) return _log(...); end; | 311 self.log = function (self, ...) return _log(...); end; |
332 self._log = _log; | 312 self._log = _log; |
333 end | |
334 | |
335 local function _add_handler(module, origin_type, tag, xmlns, handler) | |
336 local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns); | |
337 local msg = (tag == "iq") and "namespace" or "payload namespace"; | |
338 if not handlers then | |
339 stanza_handlers:add(module.host, origin_type, tag, xmlns, handler); | |
340 handler_info[handler] = module; | |
341 handler_table:add(module.host, module.name, {module.host, origin_type, tag, xmlns}); | |
342 --module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns); | |
343 else | |
344 module:log("warn", "I wanted to handle tag '%s' [%s] with %s '%s' but mod_%s already handles that", tag, origin_type, msg, xmlns, handler_info[handlers[1]].module.name); | |
345 end | |
346 end | |
347 | |
348 function api:add_handler(origin_type, tag, xmlns, handler) | |
349 if not (origin_type and tag and xmlns and handler) then return false; end | |
350 if type(origin_type) == "table" then | |
351 for _, origin_type in ipairs(origin_type) do | |
352 _add_handler(self, origin_type, tag, xmlns, handler); | |
353 end | |
354 else | |
355 _add_handler(self, origin_type, tag, xmlns, handler); | |
356 end | |
357 end | 313 end |
358 | 314 |
359 function api:add_feature(xmlns) | 315 function api:add_feature(xmlns) |
360 self:add_item("feature", xmlns); | 316 self:add_item("feature", xmlns); |
361 end | 317 end |