2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local log = require "util.logger".init("componentmanager");
13 local configmanager = require "core.configmanager";
14 local eventmanager = require "core.eventmanager";
15 local modulemanager = require "core.modulemanager";
\r
16 local jid_split = require "util.jid".split;
\r
19 local pairs, type, tostring = pairs, type, tostring;
\r
21 local components = {};
\r
23 module "componentmanager"
\r
25 function load_enabled_components(config)
26 local defined_hosts = config or configmanager.getconfig();
28 for host, host_config in pairs(defined_hosts) do
29 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
30 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
31 modulemanager.load(host, "dialback");
32 local ok, err = modulemanager.load(host, host_config.core.component_module);
34 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
36 log("info", "Activated %s component: %s", host_config.core.component_module, host);
39 local ok, component_handler = modulemanager.call_module_method(modulemanager.get_module(host, host_config.core.component_module), "load_component");
41 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(component_handler));
43 components[host] = component_handler;
49 eventmanager.add_event_hook("server-starting", load_enabled_components);
51 function handle_stanza(origin, stanza)
\r
52 local node, host = jid_split(stanza.attr.to);
\r
53 local component = nil;
54 if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
55 if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
\r
56 if not component then component = components[host]; end
58 log("debug", "stanza being handled by component: "..host);
\r
59 component(origin, stanza, hosts[host]);
\r
61 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
\r
65 function register_component(host, component)
\r
66 if not hosts[host] then
\r
67 -- TODO check for host well-formedness
\r
68 components[host] = component;
\r
69 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
70 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
71 modulemanager.load(host, "dialback");
\r
72 log("debug", "component added: "..host);
\r
75 log("error", "Attempt to set component for existing host: "..host);
\r
79 function deregister_component(host)
80 if components[host] then
81 modulemanager.unload(host, "dialback");
82 components[host] = nil;
84 log("debug", "component removed: "..host);
87 log("error", "Attempt to remove component for non-existing host: "..host);