Merge with 0.5
[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 = 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 prosody.events.add_handler("server-starting", load_enabled_components);
55
56 function handle_stanza(origin, stanza)
57         local node, host = jid_split(stanza.attr.to);
58         local component = nil;
59         if host then
60                 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
61                 if not component then component = components[host]; end
62         end
63         if component then
64                 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
65                 component(origin, stanza, hosts[host]);
66         else
67                 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
68         end
69 end
70
71 function create_component(host, component, events)
72         -- TODO check for host well-formedness
73         local ssl_ctx;
74         if host then
75                 -- We need to find SSL context to use...
76                 -- Discussion in prosody@ concluded that
77                 -- 1 level back is usually enough by default
78                 local base_host = host:gsub("^[^%.]+%.", "");
79                 if hosts[base_host] then
80                         ssl_ctx = hosts[base_host].ssl_ctx;
81                 end
82         end
83         return { type = "component", host = host, connected = true, s2sout = {}, 
84                         ssl_ctx = ssl_ctx, events = events or events_new() };
85 end
86
87 function register_component(host, component, session)
88         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
89                 local old_events = hosts[host] and hosts[host].events;
90
91                 components[host] = component;
92                 hosts[host] = session or create_component(host, component, old_events);
93                 
94                 -- Add events object if not already one
95                 if not hosts[host].events then
96                         hosts[host].events = old_events or events_new();
97                 end
98                 
99                 -- add to disco_items
100                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
101                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
102                 end
103                 modulemanager.load(host, "dialback");
104                 modulemanager.load(host, "tls");
105                 log("debug", "component added: "..host);
106                 return session or hosts[host];
107         else
108                 log("error", "Attempt to set component for existing host: "..host);
109         end
110 end
111
112 function deregister_component(host)
113         if components[host] then
114                 modulemanager.unload(host, "dialback");
115                 hosts[host].connected = nil;
116                 local host_config = configmanager.getconfig()[host];
117                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
118                         -- Set default handler
119                         components[host] = default_component_handler;
120                 else
121                         -- Component not in config, or disabled, remove
122                         hosts[host] = nil;
123                         components[host] = nil;
124                 end
125                 -- remove from disco_items
126                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
127                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
128                 end
129                 log("debug", "component removed: "..host);
130                 return true;
131         else
132                 log("error", "Attempt to remove component for non-existing host: "..host);
133         end
134 end
135
136 function set_component_handler(host, handler)
137         components[host] = handler;
138 end
139
140 function get_children(host)
141         return disco_items:get(host) or NULL;
142 end
143
144 return _M;