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 jid_split = require "util.jid".split;
14 local fire_event = require "core.eventmanager".fire_event;
15 local events_new = require "util.events".new;
16 local st = require "util.stanza";
19 local pairs, type, tostring = pairs, type, tostring;
21 local components = {};
23 local disco_items = require "util.multitable".new();
26 module "componentmanager"
28 local function default_component_handler(origin, stanza)
29 log("warn", "Stanza being handled by default component, bouncing error");
30 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
31 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
35 function load_enabled_components(config)
36 local defined_hosts = config or configmanager.getconfig();
38 for host, host_config in pairs(defined_hosts) do
39 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
40 hosts[host] = create_component(host);
41 hosts[host].connected = false;
42 components[host] = default_component_handler;
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 fire_event("component-activated", host, host_config);
48 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
54 prosody.events.add_handler("server-starting", load_enabled_components);
56 function handle_stanza(origin, stanza)
57 local node, host = jid_split(stanza.attr.to);
58 local component = nil;
60 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
61 if not component then component = components[host]; end
64 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
65 component(origin, stanza, hosts[host]);
67 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
71 function create_component(host, component, events)
72 -- TODO check for host well-formedness
73 return { type = "component", host = host, connected = true, s2sout = {}, events = events or events_new() };
76 function register_component(host, component, session)
77 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
78 local old_events = hosts[host] and hosts[host].events;
80 components[host] = component;
81 hosts[host] = session or create_component(host, component, old_events);
83 -- Add events object if not already one
84 if not hosts[host].events then
85 hosts[host].events = old_events or events_new();
89 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
90 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
92 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
93 modulemanager.load(host, "dialback");
94 log("debug", "component added: "..host);
95 return session or hosts[host];
97 log("error", "Attempt to set component for existing host: "..host);
101 function deregister_component(host)
102 if components[host] then
103 modulemanager.unload(host, "dialback");
104 hosts[host].connected = nil;
105 local host_config = configmanager.getconfig()[host];
106 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
107 -- Set default handler
108 components[host] = default_component_handler;
110 -- Component not in config, or disabled, remove
112 components[host] = nil;
114 -- remove from disco_items
115 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
116 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
118 log("debug", "component removed: "..host);
121 log("error", "Attempt to remove component for non-existing host: "..host);
125 function set_component_handler(host, handler)
126 components[host] = handler;
129 function get_children(host)
130 return disco_items:get(host) or NULL;