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.
9 local prosody = prosody;
10 local log = require "util.logger".init("componentmanager");
11 local configmanager = require "core.configmanager";
12 local modulemanager = require "core.modulemanager";
13 local core_route_stanza = core_route_stanza;
14 local jid_split = require "util.jid".split;
15 local events_new = require "util.events".new;
16 local st = require "util.stanza";
18 local serialize = require "util.serialization".serialize
20 local pairs, type, tostring = pairs, type, tostring;
22 local components = {};
24 local disco_items = require "util.multitable".new();
27 prosody.events.add_handler("server-starting", function () core_route_stanza = _G.core_route_stanza; end);
29 module "componentmanager"
31 local function default_component_handler(origin, stanza)
32 log("warn", "Stanza being handled by default component, bouncing error");
33 if stanza.attr.type ~= "error" then
34 core_route_stanza(nil, st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
38 local components_loaded_once;
39 function load_enabled_components(config)
40 local defined_hosts = config or configmanager.getconfig();
42 for host, host_config in pairs(defined_hosts) do
43 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
44 hosts[host] = { type = "component", host = host, connected = false, s2sout = {}, events = events_new() };
45 components[host] = default_component_handler;
46 local ok, err = modulemanager.load(host, host_config.core.component_module);
48 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
50 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
56 prosody.events.add_handler("server-starting", load_enabled_components);
58 function handle_stanza(origin, stanza)
59 local node, host = jid_split(stanza.attr.to);
60 local component = nil;
62 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
63 if not component then component = components[host]; end
66 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
67 component(origin, stanza, hosts[host]);
69 log("error", "Component manager recieved a stanza for a non-existing component: " .. (stanza.attr.to or serialize(stanza)));
73 function create_component(host, component)
74 -- TODO check for host well-formedness
75 return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() };
78 function register_component(host, component, session)
79 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
80 local old_events = hosts[host] and hosts[host].events;
82 components[host] = component;
83 hosts[host] = session or create_component(host, component);
85 -- Add events object if not already one
86 if not hosts[host].events then
87 hosts[host].events = old_events or events_new();
91 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
92 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
94 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
95 modulemanager.load(host, "dialback");
96 log("debug", "component added: "..host);
97 return session or hosts[host];
99 log("error", "Attempt to set component for existing host: "..host);
103 function deregister_component(host)
104 if components[host] then
105 modulemanager.unload(host, "dialback");
106 hosts[host].connected = nil;
107 local host_config = configmanager.getconfig()[host];
108 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
109 -- Set default handler
110 components[host] = default_component_handler;
112 -- Component not in config, or disabled, remove
114 components[host] = nil;
116 -- remove from disco_items
117 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
118 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
120 log("debug", "component removed: "..host);
123 log("error", "Attempt to remove component for non-existing host: "..host);
127 function set_component_handler(host, handler)
128 components[host] = handler;
131 function get_children(host)
132 return disco_items:get(host) or NULL;