dfc2cd783bd89a60b251897402ae41bd768c883b
[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 module "componentmanager"
26
27 local function default_component_handler(origin, stanza)
28         log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
29         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
30                 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
31         end
32 end
33
34 function create_component(host, component, events)
35         -- TODO check for host well-formedness
36         return { type = "component", host = host, s2sout = {},
37                         events = events or events_new(),
38                         dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen(),
39                         disallow_s2s = configmanager.get(host, "core", "disallow_s2s"); };
40 end
41
42 function register_component(host, component)
43         if not hosts[host] or hosts[host].type == 'component' then
44                 local old_events = hosts[host] and hosts[host].events;
45
46                 components[host] = component;
47                 hosts[host] = create_component(host, component, old_events);
48
49                 -- Add events object if not already one
50                 if not hosts[host].events then
51                         hosts[host].events = old_events or events_new();
52                 end
53
54                 if not hosts[host].dialback_secret then
55                         hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
56                 end
57
58                 modulemanager.load(host, "dialback");
59                 modulemanager.load(host, "tls");
60                 log("debug", "component added: "..host);
61                 return hosts[host];
62         else
63                 log("error", "Attempt to set component for existing host: "..host);
64         end
65 end
66
67 function deregister_component(host)
68         if components[host] then
69                 modulemanager.unload(host, "tls");
70                 modulemanager.unload(host, "dialback");
71                 local host_config = configmanager.getconfig()[host];
72                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
73                         -- Set default handler
74                         components[host] = default_component_handler;
75                 else
76                         -- Component not in config, or disabled, remove
77                         hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
78                         components[host] = nil;
79                 end
80                 log("debug", "component removed: "..host);
81                 return true;
82         else
83                 log("error", "Attempt to remove component for non-existing host: "..host);
84         end
85 end
86
87 return _M;