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.
11 local prosody = prosody;
12 local log = require "util.logger".init("componentmanager");
13 local configmanager = require "core.configmanager";
14 local modulemanager = require "core.modulemanager";
15 local core_route_stanza = core_route_stanza;
16 local jid_split = require "util.jid".split;
17 local events_new = require "util.events".new;
18 local st = require "util.stanza";
21 local pairs, type, tostring = pairs, type, tostring;
23 local components = {};
25 local disco_items = require "util.multitable".new();
27 require "core.discomanager".addDiscoItemsHandler("*host", function(reply, to, from, node)
28 if #node == 0 and hosts[to] then
29 for jid in pairs(disco_items:get(to) or NULL) do
30 reply:tag("item", {jid = jid}):up();
36 prosody.events.add_handler("server-starting", function () core_route_stanza = _G.core_route_stanza; end);
38 module "componentmanager"
40 local function default_component_handler(origin, stanza)
41 log("warn", "Stanza being handled by default component, bouncing error");
42 if stanza.attr.type ~= "error" then
43 core_route_stanza(nil, st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
47 local components_loaded_once;
48 function load_enabled_components(config)
49 local defined_hosts = config or configmanager.getconfig();
51 for host, host_config in pairs(defined_hosts) do
52 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
53 hosts[host] = { type = "component", host = host, connected = false, s2sout = {}, events = events_new() };
54 components[host] = default_component_handler;
55 local ok, err = modulemanager.load(host, host_config.core.component_module);
57 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
59 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
65 prosody.events.add_handler("server-starting", load_enabled_components);
67 function handle_stanza(origin, stanza)
68 local node, host = jid_split(stanza.attr.to);
69 local component = nil;
71 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
72 if not component then component = components[host]; end
75 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
76 component(origin, stanza, hosts[host]);
78 log("error", "Component manager recieved a stanza for a non-existing component: " .. (stanza.attr.to or stanza));
82 function create_component(host, component)
83 -- TODO check for host well-formedness
84 return { type = "component", host = host, connected = true, s2sout = {}, events = 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);
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 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
104 modulemanager.load(host, "dialback");
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;