Changeset

4344:a24ee48ec30d

modulemanager: Add module:hook_global(name, handler, priority) to hook global (server-wide) events
author Matthew Wild <mwild1@gmail.com>
date Sun, 14 Aug 2011 13:56:13 -0400
parents 4343:203137823046
children 4345:f6d694b1cdb3
files core/modulemanager.lua
diffstat 1 files changed, 11 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/core/modulemanager.lua	Sun Aug 14 13:39:40 2011 -0400
+++ b/core/modulemanager.lua	Sun Aug 14 13:56:13 2011 -0400
@@ -177,6 +177,12 @@
 			(hosts[host] or prosody).events.remove_handler(event, handler);
 		end
 	end
+	-- unhook event handlers hooked by module:hook_global
+	for event, handlers in pairs(hooks:get("*", name) or NULL) do
+		for handler in pairs(handlers or NULL) do
+			prosody.events.remove_handler(event, handler);
+		end
+	end
 	hooks:remove(host, name);
 	if mod.module.items then -- remove items
 		for key,t in pairs(mod.module.items) do
@@ -291,6 +297,11 @@
 	(hosts[self.host] or prosody).events.add_handler(event, handler, priority);
 end
 
+function api:hook_global(event, handler, priority)
+	hooks:set("*", self.name, event, handler, true);
+	prosody.events.add_handler(event, handler, priority);
+end
+
 function api:hook_stanza(xmlns, name, handler, priority)
 	if not handler and type(name) == "function" then
 		-- If only 2 options then they specified no xmlns