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