From 8c36520f60140741cde82860a05b39fbe1844535 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 28 Feb 2009 02:05:37 +0000 Subject: core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler --- core/componentmanager.lua | 76 +++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 35 deletions(-) (limited to 'core') diff --git a/core/componentmanager.lua b/core/componentmanager.lua index 04909a07..15931167 100644 --- a/core/componentmanager.lua +++ b/core/componentmanager.lua @@ -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 @@ function load_enabled_components(config) 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 @@ function deregister_component(host) log("error", "Attempt to remove component for non-existing host: "..host); end end - -return _M; + +return _M; -- cgit v1.2.3