prosody: Add prosody.installed flag to indicate whether Prosody has been installed...
[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 events_new = require "util.events".new;
15 local st = require "util.stanza";
16 local hosts = hosts;
17
18 local pairs, type, tostring = pairs, type, tostring;
19
20 local components = {};
21
22 local disco_items = require "util.multitable".new();
23 local NULL = {};
24
25 module "componentmanager"
26
27 local function default_component_handler(origin, stanza)
28         log("warn", "Stanza being handled by default component, bouncing error");
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 load_enabled_components(config)
35         local defined_hosts = config or configmanager.getconfig();
36                 
37         for host, host_config in pairs(defined_hosts) do
38                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
39                         hosts[host] = { type = "component", host = host, connected = false, s2sout = {}, events = events_new() };
40                         components[host] = default_component_handler;
41                         local ok, err = modulemanager.load(host, host_config.core.component_module);
42                         if not ok then
43                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
44                         else
45                                 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
46                         end
47                 end
48         end
49 end
50
51 prosody.events.add_handler("server-starting", load_enabled_components);
52
53 function handle_stanza(origin, stanza)
54         local node, host = jid_split(stanza.attr.to);
55         local component = nil;
56         if host then
57                 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
58                 if not component then component = components[host]; end
59         end
60         if component then
61                 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
62                 component(origin, stanza, hosts[host]);
63         else
64                 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
65         end
66 end
67
68 function create_component(host, component)
69         -- TODO check for host well-formedness
70         return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() };
71 end
72
73 function register_component(host, component, session)
74         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
75                 local old_events = hosts[host] and hosts[host].events;
76
77                 components[host] = component;
78                 hosts[host] = session or create_component(host, component);
79                 
80                 -- Add events object if not already one
81                 if not hosts[host].events then
82                         hosts[host].events = old_events or events_new();
83                 end
84                 
85                 -- add to disco_items
86                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
87                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
88                 end
89                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
90                 modulemanager.load(host, "dialback");
91                 log("debug", "component added: "..host);
92                 return session or hosts[host];
93         else
94                 log("error", "Attempt to set component for existing host: "..host);
95         end
96 end
97
98 function deregister_component(host)
99         if components[host] then
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;
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 set_component_handler(host, handler)
123         components[host] = handler;
124 end
125
126 function get_children(host)
127         return disco_items:get(host) or NULL;
128 end
129
130 return _M;