# HG changeset patch # User Waqas Hussain # Date 1243758680 -18000 # Node ID 6bd11bca972584e134452719851c75851498928c # Parent c199be608546266ca20c40f9a38f36ca1117746a modulemanager: Keep track of event handlers added by module:hook, and remove them on module unload diff -r c199be608546 -r 6bd11bca9725 core/modulemanager.lua --- a/core/modulemanager.lua Sun May 31 02:20:30 2009 +0500 +++ b/core/modulemanager.lua Sun May 31 13:31:20 2009 +0500 @@ -49,6 +49,7 @@ local features_table = multitable_new(); local handler_table = multitable_new(); local hooked = multitable_new(); +local hooks = multitable_new(); local event_hooks = multitable_new(); local NULL = {}; @@ -165,6 +166,13 @@ end end event_hooks:remove(host, name); + -- unhook event handlers hooked by module:hook + for event, handlers in pairs(hooks:get(host, name) or NULL) do + for handler in pairs(handlers or NULL) do + (hosts[host] or prosody).events.remove_handler(event, handler); + end + end + hooks:remove(host, name); return true; end @@ -356,6 +364,7 @@ end function api:hook(event, handler) + hooks:set(self.host, self.name, event, handler, true); (hosts[self.host] or prosody).events.add_handler(event, handler); end