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";
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 if prosody and prosody.events then
55 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: "..tostring(stanza));
70 default_component_handler(origin, stanza);
74 function create_component(host, component, events)
75 -- TODO check for host well-formedness
78 -- We need to find SSL context to use...
79 -- Discussion in prosody@ concluded that
80 -- 1 level back is usually enough by default
81 local base_host = host:gsub("^[^%.]+%.", "");
82 if hosts[base_host] then
83 ssl_ctx = hosts[base_host].ssl_ctx;
86 return { type = "component", host = host, connected = true, s2sout = {},
87 ssl_ctx = ssl_ctx, events = events or events_new() };
90 function register_component(host, component, session)
91 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
92 local old_events = hosts[host] and hosts[host].events;
94 components[host] = component;
95 hosts[host] = session or create_component(host, component, old_events);
97 -- Add events object if not already one
98 if not hosts[host].events then
99 hosts[host].events = old_events or events_new();
102 -- add to disco_items
103 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
104 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
106 modulemanager.load(host, "dialback");
107 modulemanager.load(host, "tls");
108 log("debug", "component added: "..host);
109 return session or hosts[host];
111 log("error", "Attempt to set component for existing host: "..host);
115 function deregister_component(host)
116 if components[host] then
117 modulemanager.unload(host, "tls");
118 modulemanager.unload(host, "dialback");
119 hosts[host].connected = nil;
120 local host_config = configmanager.getconfig()[host];
121 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
122 -- Set default handler
123 components[host] = default_component_handler;
125 -- Component not in config, or disabled, remove
126 hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
127 components[host] = nil;
129 -- remove from disco_items
130 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
131 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
133 log("debug", "component removed: "..host);
136 log("error", "Attempt to remove component for non-existing host: "..host);
140 function set_component_handler(host, handler)
141 components[host] = handler;
144 function get_children(host)
145 return disco_items:get(host) or NULL;