Changeset

847:2d424936723c

core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Feb 2009 02:05:37 +0000
parents 846:77244ba297ac
children 848:b1f3977fd140
files core/componentmanager.lua
diffstat 1 files changed, 41 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/core/componentmanager.lua	Fri Feb 27 18:40:17 2009 +0000
+++ b/core/componentmanager.lua	Sat Feb 28 02:05:37 2009 +0000
@@ -7,20 +7,20 @@
 --
 
 
-
-
+
+
 local log = require "util.logger".init("componentmanager");
 local configmanager = require "core.configmanager";
 local eventmanager = require "core.eventmanager";
-local modulemanager = require "core.modulemanager";
-local jid_split = require "util.jid".split;
+local modulemanager = require "core.modulemanager";
+local jid_split = require "util.jid".split;
 local hosts = hosts;
 
-local pairs, type, tostring = pairs, type, tostring;
-
-local components = {};
-
-module "componentmanager"
+local pairs, type, tostring = pairs, type, tostring;
+
+local components = {};
+
+module "componentmanager"
 
 function load_enabled_components(config)
 	local defined_hosts = config or configmanager.getconfig();
@@ -39,34 +39,40 @@
 end
 
 eventmanager.add_event_hook("server-starting", load_enabled_components);
-
-function handle_stanza(origin, stanza)
-	local node, host = jid_split(stanza.attr.to);
+
+function handle_stanza(origin, stanza)
+	local node, host = jid_split(stanza.attr.to);
 	local component = nil;
 	if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
-	if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
+	if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
 	if not component then component = components[host]; end
-	if component then
-		log("debug", "stanza being handled by component: "..host);
-		component(origin, stanza, hosts[host]);
-	else
-		log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
-	end
-end
-
-function register_component(host, component)
-	if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
-		-- TODO check for host well-formedness
-		components[host] = component;
-		hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
+	if component then
+		log("debug", "stanza being handled by component: "..host);
+		component(origin, stanza, hosts[host]);
+	else
+		log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
+	end
+end
+
+function create_component(host, component)
+		-- TODO check for host well-formedness
+		session = session or { type = "component", host = host, connected = true, s2sout = {}, send = component };
+		return session;
+end
+
+function register_component(host, component, session)
+	if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
+		components[host] = component;
+		hosts[host] = session or create_component(host, component);
+		
 		-- FIXME only load for a.b.c if b.c has dialback, and/or check in config
-		modulemanager.load(host, "dialback");
-		log("debug", "component added: "..host);
-		return hosts[host];
-	else
-		log("error", "Attempt to set component for existing host: "..host);
-	end
-end
+		modulemanager.load(host, "dialback");
+		log("debug", "component added: "..host);
+		return session or hosts[host];
+	else
+		log("error", "Attempt to set component for existing host: "..host);
+	end
+end
 
 function deregister_component(host)
 	if components[host] then
@@ -79,5 +85,5 @@
 		log("error", "Attempt to remove component for non-existing host: "..host);
 	end
 end
-
-return _M;
+
+return _M;