5efb1f5175b650e1d2d16a1fa5f5799a4c44165c
[prosody.git] / core / componentmanager.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11
12 local log = require "util.logger".init("componentmanager");
13 local configmanager = require "core.configmanager";
14 local eventmanager = require "core.eventmanager";
15 local modulemanager = require "core.modulemanager";
16 local jid_split = require "util.jid".split;
17 local hosts = hosts;
18
19 local pairs, type, tostring = pairs, type, tostring;
20
21 local components = {};
22
23 local disco_items = require "util.multitable".new();
24 local NULL = {};
25 require "core.discomanager".addDiscoItemsHandler("*host", function(reply, to, from, node)
26         if #node == 0 and hosts[to] then
27                 for jid in pairs(disco_items:get(to) or NULL) do
28                         reply:tag("item", {jid = jid}):up();
29                 end
30                 return true;
31         end
32 end);
33
34
35 module "componentmanager"
36
37 local function default_component_handler(origin, stanza)
38         origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
39 end
40
41
42 function load_enabled_components(config)
43         local defined_hosts = config or configmanager.getconfig();
44                 
45         for host, host_config in pairs(defined_hosts) do
46                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
47                         hosts[host] = { type = "component", host = host, connected = false, s2sout = {} };
48                         components[host] = default_component_handler;
49                         local ok, err = modulemanager.load(host, host_config.core.component_module);
50                         if not ok then
51                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
52                         else
53                                 log("info", "Activated %s component: %s", host_config.core.component_module, host);
54                         end
55                 end
56         end
57 end
58
59 eventmanager.add_event_hook("server-starting", load_enabled_components);
60
61 function handle_stanza(origin, stanza)
62         local node, host = jid_split(stanza.attr.to);
63         local component = nil;
64         if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
65         if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
66         if not component then component = components[host]; end
67         if component then
68                 log("debug", "stanza being handled by component: "..host);
69                 component(origin, stanza, hosts[host]);
70         else
71                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
72         end
73 end
74
75 function create_component(host, component)
76                 -- TODO check for host well-formedness
77                 local session = session or { type = "component", host = host, connected = true, s2sout = {}, send = component };
78                 return session;
79 end
80
81 function register_component(host, component, session)
82         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
83                 components[host] = component;
84                 hosts[host] = session or create_component(host, component);
85                 -- add to disco_items
86                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
87                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
88                 end
89                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
90                 modulemanager.load(host, "dialback");
91                 log("debug", "component added: "..host);
92                 return session or hosts[host];
93         else
94                 log("error", "Attempt to set component for existing host: "..host);
95         end
96 end
97
98 function deregister_component(host)
99         if components[host] then
100                 modulemanager.unload(host, "dialback");
101                 components[host] = nil;
102                 local host_config = defined_hosts[host];
103                 if ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
104                         -- Set default handler
105                 else
106                         -- Component not in config, or disabled, remove
107                         hosts[host] = nil;
108                 end
109                 -- remove from disco_items
110                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
111                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
112                 end
113                 log("debug", "component removed: "..host);
114                 return true;
115         else
116                 log("error", "Attempt to remove component for non-existing host: "..host);
117         end
118 end
119
120 function set_component_handler(host, handler)
121         components[host] = handler;
122 end
123
124 return _M;