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
75 -- We need to find SSL context to use...
76 -- Discussion in prosody@ concluded that
77 -- 1 level back is usually enough by default
78 local base_host = host:gsub("^[^%.]+%.", "");
79 if hosts[base_host] then
80 ssl_ctx = hosts[base_host].ssl_ctx;
83 return { type = "component", host = host, connected = true, s2sout = {},
84 ssl_ctx = ssl_ctx, events = events or events_new() };
87 function register_component(host, component, session)
88 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
89 local old_events = hosts[host] and hosts[host].events;
91 components[host] = component;
92 hosts[host] = session or create_component(host, component, old_events);
94 -- Add events object if not already one
95 if not hosts[host].events then
96 hosts[host].events = old_events or events_new();
100 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
101 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
103 modulemanager.load(host, "dialback");
104 modulemanager.load(host, "tls");
105 log("debug", "component added: "..host);
106 return session or hosts[host];
108 log("error", "Attempt to set component for existing host: "..host);
112 function deregister_component(host)
113 if components[host] then
114 modulemanager.unload(host, "dialback");
115 hosts[host].connected = nil;
116 local host_config = configmanager.getconfig()[host];
117 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
118 -- Set default handler
119 components[host] = default_component_handler;
121 -- Component not in config, or disabled, remove
123 components[host] = nil;
125 -- remove from disco_items
126 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
127 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
129 log("debug", "component removed: "..host);
132 log("error", "Attempt to remove component for non-existing host: "..host);
136 function set_component_handler(host, handler)
137 components[host] = handler;
140 function get_children(host)
141 return disco_items:get(host) or NULL;