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