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