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 fire_event = require "core.eventmanager".fire_event;
18 local events_new = require "util.events".new;
19 local st = require "util.stanza";
21 local serialize = require "util.serialization".serialize
23 local pairs, type, tostring = pairs, type, tostring;
25 local components = {};
27 local disco_items = require "util.multitable".new();
29 require "core.discomanager".addDiscoItemsHandler("*host", function(reply, to, from, node)
30 if #node == 0 and hosts[to] then
31 for jid in pairs(disco_items:get(to) or NULL) do
32 reply:tag("item", {jid = jid}):up();
38 prosody.events.add_handler("server-starting", function () core_route_stanza = _G.core_route_stanza; end);
40 module "componentmanager"
42 local function default_component_handler(origin, stanza)
43 log("warn", "Stanza being handled by default component, bouncing error");
44 if stanza.attr.type ~= "error" then
45 core_route_stanza(nil, st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
49 local components_loaded_once;
50 function load_enabled_components(config)
51 local defined_hosts = config or configmanager.getconfig();
53 for host, host_config in pairs(defined_hosts) do
54 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
55 hosts[host] = create_component(host);
56 hosts[host].connected = false;
57 components[host] = default_component_handler;
58 local ok, err = modulemanager.load(host, host_config.core.component_module);
60 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
62 fire_event("component-activated", host, host_config);
63 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
69 prosody.events.add_handler("server-starting", load_enabled_components);
71 function handle_stanza(origin, stanza)
72 local node, host = jid_split(stanza.attr.to);
73 local component = nil;
75 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
76 if not component then component = components[host]; end
79 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
80 component(origin, stanza, hosts[host]);
82 log("error", "Component manager recieved a stanza for a non-existing component: " .. (stanza.attr.to or serialize(stanza)));
86 function create_component(host, component)
87 -- TODO check for host well-formedness
88 return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() };
91 function register_component(host, component, session)
92 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
93 local old_events = hosts[host] and hosts[host].events;
95 components[host] = component;
96 hosts[host] = session or create_component(host, component);
98 -- Add events object if not already one
99 if not hosts[host].events then
100 hosts[host].events = old_events or events_new();
103 -- add to disco_items
104 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
105 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
107 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
108 modulemanager.load(host, "dialback");
109 log("debug", "component added: "..host);
110 return session or hosts[host];
112 log("error", "Attempt to set component for existing host: "..host);
116 function deregister_component(host)
117 if components[host] then
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
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;