c7d9407c7af2d558db5c4a467794d7cea98a3140
[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 core_route_stanza = core_route_stanza;
17 local jid_split = require "util.jid".split;
18 local st = require "util.stanza";
19 local hosts = hosts;
20
21 local pairs, type, tostring = pairs, type, tostring;
22
23 local components = {};
24
25 local disco_items = require "util.multitable".new();
26 local NULL = {};
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();
31                 end
32                 return true;
33         end
34 end);
35
36
37 module "componentmanager"
38
39 local function default_component_handler(origin, stanza)
40         log("warn", "Stanza being handled by default component, bouncing error");
41         if stanza.attr.type ~= "error" then
42                 core_route_stanza(nil, st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
43         end
44 end
45
46
47 function load_enabled_components(config)
48         local defined_hosts = config or configmanager.getconfig();
49                 
50         for host, host_config in pairs(defined_hosts) do
51                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
52                         hosts[host] = { type = "component", host = host, connected = false, s2sout = {} };
53                         components[host] = default_component_handler;
54                         local ok, err = modulemanager.load(host, host_config.core.component_module);
55                         if not ok then
56                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
57                         else
58                                 log("info", "Activated %s component: %s", host_config.core.component_module, host);
59                         end
60                 end
61         end
62 end
63
64 eventmanager.add_event_hook("server-starting", load_enabled_components);
65
66 function handle_stanza(origin, stanza)
67         local node, host = jid_split(stanza.attr.to);
68         local component = nil;
69         if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
70         if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
71         if not component then component = components[host]; end
72         if component then
73                 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
74                 component(origin, stanza, hosts[host]);
75         else
76                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
77         end
78 end
79
80 function create_component(host, component)
81         -- TODO check for host well-formedness
82         local session = session or { type = "component", host = host, connected = true, s2sout = {} };
83         return session;
84 end
85
86 function register_component(host, component, session)
87         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
88                 components[host] = component;
89                 hosts[host] = session or create_component(host, component);
90                 -- add to disco_items
91                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
92                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
93                 end
94                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
95                 modulemanager.load(host, "dialback");
96                 log("debug", "component added: "..host);
97                 return session or hosts[host];
98         else
99                 log("error", "Attempt to set component for existing host: "..host);
100         end
101 end
102
103 function deregister_component(host)
104         if components[host] then
105                 modulemanager.unload(host, "dialback");
106                 local host_config = configmanager.getconfig()[host];
107                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
108                         -- Set default handler
109                         components[host] = default_component_handler;
110                 else
111                         -- Component not in config, or disabled, remove
112                         hosts[host] = nil;
113                         components[host] = nil;
114                 end
115                 -- remove from disco_items
116                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
117                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
118                 end
119                 log("debug", "component removed: "..host);
120                 return true;
121         else
122                 log("error", "Attempt to remove component for non-existing host: "..host);
123         end
124 end
125
126 function set_component_handler(host, handler)
127         components[host] = handler;
128 end
129
130 return _M;