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