Changeset

686:13ed38531f69

modulemanager: Per-host event hooks for plugins - solves issue 41
author Waqas Hussain <waqas20@gmail.com>
date Fri, 09 Jan 2009 23:01:21 +0500
parents 685:55d1bc45acf1
children 687:a92d647624a1
files core/modulemanager.lua
diffstat 1 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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
 
 --------------------------------------------------------------------