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