536d16339e247c539c74978743476d08b29e4e37
[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 core_route_stanza = core_route_stanza;
14 local jid_split = require "util.jid".split;
15 local events_new = require "util.events".new;
16 local st = require "util.stanza";
17 local hosts = hosts;
18 local serialize = require "util.serialization".serialize
19
20 local pairs, type, tostring = pairs, type, tostring;
21
22 local components = {};
23
24 local disco_items = require "util.multitable".new();
25 local NULL = {};
26
27 prosody.events.add_handler("server-starting", function () core_route_stanza = _G.core_route_stanza; end);
28
29 module "componentmanager"
30
31 local function default_component_handler(origin, stanza)
32         log("warn", "Stanza being handled by default component, bouncing error");
33         if stanza.attr.type ~= "error" then
34                 core_route_stanza(nil, st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
35         end
36 end
37
38 local components_loaded_once;
39 function load_enabled_components(config)
40         local defined_hosts = config or configmanager.getconfig();
41                 
42         for host, host_config in pairs(defined_hosts) do
43                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
44                         hosts[host] = { type = "component", host = host, connected = false, s2sout = {}, events = events_new() };
45                         components[host] = default_component_handler;
46                         local ok, err = modulemanager.load(host, host_config.core.component_module);
47                         if not ok then
48                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
49                         else
50                                 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
51                         end
52                 end
53         end
54 end
55
56 prosody.events.add_handler("server-starting", load_enabled_components);
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: " .. (stanza.attr.to or serialize(stanza)));
70         end
71 end
72
73 function create_component(host, component)
74         -- TODO check for host well-formedness
75         return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() };
76 end
77
78 function register_component(host, component, session)
79         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
80                 local old_events = hosts[host] and hosts[host].events;
81
82                 components[host] = component;
83                 hosts[host] = session or create_component(host, component);
84                 
85                 -- Add events object if not already one
86                 if not hosts[host].events then
87                         hosts[host].events = old_events or events_new();
88                 end
89                 
90                 -- add to disco_items
91                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
92                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
93                 end
94                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
95                 modulemanager.load(host, "dialback");
96                 log("debug", "component added: "..host);
97                 return session or hosts[host];
98         else
99                 log("error", "Attempt to set component for existing host: "..host);
100         end
101 end
102
103 function deregister_component(host)
104         if components[host] then
105                 modulemanager.unload(host, "dialback");
106                 hosts[host].connected = nil;
107                 local host_config = configmanager.getconfig()[host];
108                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
109                         -- Set default handler
110                         components[host] = default_component_handler;
111                 else
112                         -- Component not in config, or disabled, remove
113                         hosts[host] = nil;
114                         components[host] = nil;
115                 end
116                 -- remove from disco_items
117                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
118                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
119                 end
120                 log("debug", "component removed: "..host);
121                 return true;
122         else
123                 log("error", "Attempt to remove component for non-existing host: "..host);
124         end
125 end
126
127 function set_component_handler(host, handler)
128         components[host] = handler;
129 end
130
131 function get_children(host)
132         return disco_items:get(host) or NULL;
133 end
134
135 return _M;