2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 local log = require "util.logger".init("componentmanager");
24 local configmanager = require "core.configmanager";
25 local eventmanager = require "core.eventmanager";
26 local modulemanager = require "core.modulemanager";
\r
27 local jid_split = require "util.jid".split;
\r
30 local pairs, type, tostring = pairs, type, tostring;
\r
32 local components = {};
\r
34 module "componentmanager"
\r
36 function load_enabled_components(config)
37 local defined_hosts = config or configmanager.getconfig();
39 for host, host_config in pairs(defined_hosts) do
40 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
41 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
42 modulemanager.load(host, "dialback");
43 local ok, err = modulemanager.load(host, host_config.core.component_module);
45 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
47 log("info", "Activated %s component: %s", host_config.core.component_module, host);
50 local ok, component_handler = modulemanager.call_module_method(modulemanager.get_module(host, host_config.core.component_module), "load_component");
52 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(component_handler));
54 components[host] = component_handler;
60 eventmanager.add_event_hook("server-starting", load_enabled_components);
62 function handle_stanza(origin, stanza)
\r
63 local node, host = jid_split(stanza.attr.to);
\r
64 local component = nil;
65 if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
66 if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
\r
67 if not component then component = components[host]; end
69 log("debug", "stanza being handled by component: "..host);
\r
70 component(origin, stanza, hosts[host]);
\r
72 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
\r
76 function register_component(host, component)
\r
77 if not hosts[host] then
\r
78 -- TODO check for host well-formedness
\r
79 components[host] = component;
\r
80 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
81 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
82 modulemanager.load(host, "dialback");
\r
83 log("debug", "component added: "..host);
\r
86 log("error", "Attempt to set component for existing host: "..host);
\r
90 function deregister_component(host)
91 if components[host] then
92 modulemanager.unload(host, "dialback");
93 components[host] = nil;
95 log("debug", "component removed: "..host);
98 log("error", "Attempt to remove component for non-existing host: "..host);