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;
20 local pairs, setmetatable, type, tostring = pairs, setmetatable, type, tostring;
22 local components = {};
24 local disco_items = require "util.multitable".new();
27 module "componentmanager"
29 local function default_component_handler(origin, stanza)
30 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
31 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
32 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
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] = create_component(host);
42 hosts[host].connected = false;
43 components[host] = default_component_handler;
44 local ok, err = modulemanager.load(host, host_config.core.component_module);
46 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
48 fire_event("component-activated", host, host_config);
49 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
55 if prosody and prosody.events then
56 prosody.events.add_handler("server-starting", load_enabled_components);
59 function handle_stanza(origin, stanza)
60 local node, host = jid_split(stanza.attr.to);
61 local component = nil;
63 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
64 if not component then component = components[host]; end
67 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
68 component(origin, stanza, hosts[host]);
70 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
71 default_component_handler(origin, stanza);
75 function create_component(host, component, events)
76 -- TODO check for host well-formedness
77 local ssl_ctx, ssl_ctx_in;
79 -- We need to find SSL context to use...
80 -- Discussion in prosody@ concluded that
81 -- 1 level back is usually enough by default
82 local base_host = host:gsub("^[^%.]+%.", "");
83 if hosts[base_host] then
84 ssl_ctx = hosts[base_host].ssl_ctx;
85 ssl_ctx_in = hosts[base_host].ssl_ctx_in;
86 elseif prosody.global_ssl_ctx then
87 -- We have no cert, and no parent host to borrow a cert from
88 -- Use global/default cert if there is one
89 ssl_ctx = ssl.newcontext(prosody.global_ssl_ctx);
90 ssl_ctx_in = ssl.newcontext(setmetatable({ mode = "server" }, { __index = prosody.global_ssl_ctx }));
93 return { type = "component", host = host, connected = true, s2sout = {},
94 ssl_ctx = ssl_ctx, ssl_ctx_in = ssl_ctx_in, events = events or events_new() };
97 function register_component(host, component, session)
98 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
99 local old_events = hosts[host] and hosts[host].events;
101 components[host] = component;
102 hosts[host] = session or create_component(host, component, old_events);
104 -- Add events object if not already one
105 if not hosts[host].events then
106 hosts[host].events = old_events or events_new();
109 -- add to disco_items
110 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
111 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
113 modulemanager.load(host, "dialback");
114 modulemanager.load(host, "tls");
115 log("debug", "component added: "..host);
116 return session or hosts[host];
118 log("error", "Attempt to set component for existing host: "..host);
122 function deregister_component(host)
123 if components[host] then
124 modulemanager.unload(host, "tls");
125 modulemanager.unload(host, "dialback");
126 hosts[host].connected = nil;
127 local host_config = configmanager.getconfig()[host];
128 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
129 -- Set default handler
130 components[host] = default_component_handler;
132 -- Component not in config, or disabled, remove
133 hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
134 components[host] = nil;
136 -- remove from disco_items
137 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
138 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
140 log("debug", "component removed: "..host);
143 log("error", "Attempt to remove component for non-existing host: "..host);
147 function set_component_handler(host, handler)
148 components[host] = handler;
151 function get_children(host)
152 return disco_items:get(host) or NULL;