# HG changeset patch # User Waqas Hussain # Date 1231524081 -18000 # Node ID 13ed38531f69439e175e7559be2f1392cbbb73b0 # Parent 55d1bc45acf117011b8a9b4c72c23d97d94901bb modulemanager: Per-host event hooks for plugins - solves issue 41 diff -r 55d1bc45acf1 -r 13ed38531f69 core/modulemanager.lua --- a/core/modulemanager.lua Fri Jan 09 20:49:39 2009 +0500 +++ b/core/modulemanager.lua Fri Jan 09 23:01:21 2009 +0500 @@ -55,6 +55,9 @@ local features_table = multitable_new(); local handler_table = multitable_new(); +local hooked = multitable_new(); +local event_hooks = multitable_new(); + local NULL = {}; -- Load modules when a host is activated @@ -151,6 +154,7 @@ handler_info[handlers[1]] = nil; stanza_handlers:remove(param[1], param[2], param[3], param[4]); end + event_hooks:remove(host, name); return true; end @@ -234,7 +238,19 @@ features_table:set(self.host, self.name, xmlns, true); end -function api:add_event_hook (...) return eventmanager.add_event_hook(...); end +local event_hook = function(host, mod_name, event_name, ...) + if type((...)) == "table" and (...).host and (...).host ~= host then return; end + for handler in pairs(event_hooks:get(host, mod_name, event_name) or NULL) do + handler(...); + end +end; +function api:add_event_hook(name, handler) + if not hooked:get(self.host, self.name, name) then + eventmanager.add_event_hook(name, function(...) event_hook(self.host, self.name, name, ...); end); + hooked:set(self.host, self.name, name, true); + end + event_hooks:set(self.host, self.name, name, handler, true); +end --------------------------------------------------------------------