f2f64a7da3a7f458bc1e44e3650e9e73f3d57615
[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 = prosody.events.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 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 create_component(host, component, events)
61         -- TODO check for host well-formedness
62         return { type = "component", host = host, connected = true, s2sout = {},
63                         events = events or events_new(),
64                         dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen(),
65                         disallow_s2s = configmanager.get(host, "core", "disallow_s2s"); };
66 end
67
68 function register_component(host, component)
69         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
70                 local old_events = hosts[host] and hosts[host].events;
71
72                 components[host] = component;
73                 hosts[host] = create_component(host, component, old_events);
74
75                 -- Add events object if not already one
76                 if not hosts[host].events then
77                         hosts[host].events = old_events or events_new();
78                 end
79
80                 if not hosts[host].dialback_secret then
81                         hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
82                 end
83
84                 -- add to disco_items
85                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
86                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
87                 end
88                 modulemanager.load(host, "dialback");
89                 modulemanager.load(host, "tls");
90                 log("debug", "component added: "..host);
91                 return hosts[host];
92         else
93                 log("error", "Attempt to set component for existing host: "..host);
94         end
95 end
96
97 function deregister_component(host)
98         if components[host] then
99                 modulemanager.unload(host, "tls");
100                 modulemanager.unload(host, "dialback");
101                 hosts[host].connected = nil;
102                 local host_config = configmanager.getconfig()[host];
103                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
104                         -- Set default handler
105                         components[host] = default_component_handler;
106                 else
107                         -- Component not in config, or disabled, remove
108                         hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
109                         components[host] = nil;
110                 end
111                 -- remove from disco_items
112                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
113                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
114                 end
115                 log("debug", "component removed: "..host);
116                 return true;
117         else
118                 log("error", "Attempt to remove component for non-existing host: "..host);
119         end
120 end
121
122 function get_children(host)
123         return disco_items:get(host) or NULL;
124 end
125
126 return _M;