xmlhandlers: Don't restrict CDATA
[prosody.git] / core / componentmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 local prosody = _G.prosody;
10 local log = require "util.logger".init("componentmanager");
11 local certmanager = require "core.certmanager";
12 local configmanager = require "core.configmanager";
13 local modulemanager = require "core.modulemanager";
14 local jid_split = require "util.jid".split;
15 local fire_event = require "core.eventmanager".fire_event;
16 local events_new = require "util.events".new;
17 local st = require "util.stanza";
18 local prosody, hosts = prosody, prosody.hosts;
19 local ssl = ssl;
20 local uuid_gen = require "util.uuid".generate;
21
22 local pairs, setmetatable, type, tostring = pairs, setmetatable, type, tostring;
23
24 local components = {};
25
26 local disco_items = require "util.multitable".new();
27 local NULL = {};
28
29 module "componentmanager"
30
31 local function default_component_handler(origin, stanza)
32         log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
33         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
34                 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
35         end
36 end
37
38 function load_enabled_components(config)
39         local defined_hosts = config or configmanager.getconfig();
40                 
41         for host, host_config in pairs(defined_hosts) do
42                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
43                         hosts[host] = create_component(host);
44                         hosts[host].connected = false;
45                         components[host] = default_component_handler;
46                         local ok, err = modulemanager.load(host, host_config.core.component_module);
47                         if not ok then
48                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
49                         else
50                                 fire_event("component-activated", host, host_config);
51                                 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
52                         end
53                 end
54         end
55 end
56
57 if prosody and prosody.events then
58         prosody.events.add_handler("server-starting", load_enabled_components);
59 end
60
61 function handle_stanza(origin, stanza)
62         local node, host = jid_split(stanza.attr.to);
63         local component = nil;
64         if host then
65                 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
66                 if not component then component = components[host]; end
67         end
68         if component then
69                 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
70                 component(origin, stanza, hosts[host]);
71         else
72                 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
73                 default_component_handler(origin, stanza);
74         end
75 end
76
77 function create_component(host, component, events)
78         -- TODO check for host well-formedness
79         local ssl_ctx, ssl_ctx_in;
80         if host and ssl then
81                 -- We need to find SSL context to use...
82                 -- Discussion in prosody@ concluded that
83                 -- 1 level back is usually enough by default
84                 local base_host = host:gsub("^[^%.]+%.", "");
85                 if hosts[base_host] then
86                         ssl_ctx = hosts[base_host].ssl_ctx;
87                         ssl_ctx_in = hosts[base_host].ssl_ctx_in;
88                 else
89                         -- We have no cert, and no parent host to borrow a cert from
90                         -- Use global/default cert if there is one
91                         ssl_ctx = certmanager.create_context(host, "client");
92                         ssl_ctx_in = certmanager.create_context(host, "server");
93                 end
94         end
95         return { type = "component", host = host, connected = true, s2sout = {}, 
96                         ssl_ctx = ssl_ctx, ssl_ctx_in = ssl_ctx_in, events = events or events_new(),
97                         dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen() };
98 end
99
100 function register_component(host, component, session)
101         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
102                 local old_events = hosts[host] and hosts[host].events;
103
104                 components[host] = component;
105                 hosts[host] = session or create_component(host, component, old_events);
106
107                 -- Add events object if not already one
108                 if not hosts[host].events then
109                         hosts[host].events = old_events or events_new();
110                 end
111
112                 if not hosts[host].dialback_secret then
113                         hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
114                 end
115
116                 -- add to disco_items
117                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
118                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
119                 end
120                 modulemanager.load(host, "dialback");
121                 modulemanager.load(host, "tls");
122                 log("debug", "component added: "..host);
123                 return session or hosts[host];
124         else
125                 log("error", "Attempt to set component for existing host: "..host);
126         end
127 end
128
129 function deregister_component(host)
130         if components[host] then
131                 modulemanager.unload(host, "tls");
132                 modulemanager.unload(host, "dialback");
133                 hosts[host].connected = nil;
134                 local host_config = configmanager.getconfig()[host];
135                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
136                         -- Set default handler
137                         components[host] = default_component_handler;
138                 else
139                         -- Component not in config, or disabled, remove
140                         hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
141                         components[host] = nil;
142                 end
143                 -- remove from disco_items
144                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
145                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
146                 end
147                 log("debug", "component removed: "..host);
148                 return true;
149         else
150                 log("error", "Attempt to remove component for non-existing host: "..host);
151         end
152 end
153
154 function set_component_handler(host, handler)
155         components[host] = handler;
156 end
157
158 function get_children(host)
159         return disco_items:get(host) or NULL;
160 end
161
162 return _M;