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 = _G.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";
17 local prosody, hosts = prosody, prosody.hosts;
19 local uuid_gen = require "util.uuid".generate;
21 local pairs, setmetatable, type, tostring = pairs, setmetatable, type, tostring;
23 local components = {};
25 local disco_items = require "util.multitable".new();
28 module "componentmanager"
30 local function default_component_handler(origin, stanza)
31 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
32 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
33 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
37 function load_enabled_components(config)
38 local defined_hosts = config or configmanager.getconfig();
40 for host, host_config in pairs(defined_hosts) do
41 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
42 hosts[host] = create_component(host);
43 hosts[host].connected = false;
44 components[host] = default_component_handler;
45 local ok, err = modulemanager.load(host, host_config.core.component_module);
47 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
49 fire_event("component-activated", host, host_config);
50 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
56 if prosody and prosody.events then
57 prosody.events.add_handler("server-starting", load_enabled_components);
60 function handle_stanza(origin, stanza)
61 local node, host = jid_split(stanza.attr.to);
62 local component = nil;
64 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
65 if not component then component = components[host]; end
68 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
69 component(origin, stanza, hosts[host]);
71 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
72 default_component_handler(origin, stanza);
76 function create_component(host, component, events)
77 -- TODO check for host well-formedness
78 local ssl_ctx, ssl_ctx_in;
80 -- We need to find SSL context to use...
81 -- Discussion in prosody@ concluded that
82 -- 1 level back is usually enough by default
83 local base_host = host:gsub("^[^%.]+%.", "");
84 if hosts[base_host] then
85 ssl_ctx = hosts[base_host].ssl_ctx;
86 ssl_ctx_in = hosts[base_host].ssl_ctx_in;
87 elseif prosody.global_ssl_ctx then
88 -- We have no cert, and no parent host to borrow a cert from
89 -- Use global/default cert if there is one
90 ssl_ctx = ssl.newcontext(prosody.global_ssl_ctx);
91 ssl_ctx_in = ssl.newcontext(setmetatable({ mode = "server" }, { __index = prosody.global_ssl_ctx }));
94 return { type = "component", host = host, connected = true, s2sout = {},
95 ssl_ctx = ssl_ctx, ssl_ctx_in = ssl_ctx_in, events = events or events_new(),
96 dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen() };
99 function register_component(host, component, session)
100 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
101 local old_events = hosts[host] and hosts[host].events;
103 components[host] = component;
104 hosts[host] = session or create_component(host, component, old_events);
106 -- Add events object if not already one
107 if not hosts[host].events then
108 hosts[host].events = old_events or events_new();
111 if not hosts[host].dialback_secret then
112 hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
115 -- add to disco_items
116 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
117 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
119 modulemanager.load(host, "dialback");
120 modulemanager.load(host, "tls");
121 log("debug", "component added: "..host);
122 return session or hosts[host];
124 log("error", "Attempt to set component for existing host: "..host);
128 function deregister_component(host)
129 if components[host] then
130 modulemanager.unload(host, "tls");
131 modulemanager.unload(host, "dialback");
132 hosts[host].connected = nil;
133 local host_config = configmanager.getconfig()[host];
134 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
135 -- Set default handler
136 components[host] = default_component_handler;
138 -- Component not in config, or disabled, remove
139 hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
140 components[host] = nil;
142 -- remove from disco_items
143 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
144 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
146 log("debug", "component removed: "..host);
149 log("error", "Attempt to remove component for non-existing host: "..host);
153 function set_component_handler(host, handler)
154 components[host] = handler;
157 function get_children(host)
158 return disco_items:get(host) or NULL;