04909a071921c151ec37a2a1700fc14e2baa551d
[prosody.git] / core / componentmanager.lua
1 -- Prosody IM v0.3
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
10 \r
11 \r
12 local log = require "util.logger".init("componentmanager");
13 local configmanager = require "core.configmanager";
14 local eventmanager = require "core.eventmanager";
15 local modulemanager = require "core.modulemanager";\r
16 local jid_split = require "util.jid".split;\r
17 local hosts = hosts;
18
19 local pairs, type, tostring = pairs, type, tostring;\r
20 \r
21 local components = {};\r
22 \r
23 module "componentmanager"\r
24
25 function load_enabled_components(config)
26         local defined_hosts = config or configmanager.getconfig();
27                 
28         for host, host_config in pairs(defined_hosts) do
29                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
30                         hosts[host] = { type = "component", host = host, connected = false, s2sout = {} };
31                         local ok, err = modulemanager.load(host, host_config.core.component_module);
32                         if not ok then
33                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
34                         else
35                                 log("info", "Activated %s component: %s", host_config.core.component_module, host);
36                         end
37                 end
38         end
39 end
40
41 eventmanager.add_event_hook("server-starting", load_enabled_components);
42 \r
43 function handle_stanza(origin, stanza)\r
44         local node, host = jid_split(stanza.attr.to);\r
45         local component = nil;
46         if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
47         if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server\r
48         if not component then component = components[host]; end
49         if component then\r
50                 log("debug", "stanza being handled by component: "..host);\r
51                 component(origin, stanza, hosts[host]);\r
52         else\r
53                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);\r
54         end\r
55 end\r
56 \r
57 function register_component(host, component)\r
58         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then\r
59                 -- TODO check for host well-formedness\r
60                 components[host] = component;\r
61                 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
62                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
63                 modulemanager.load(host, "dialback");\r
64                 log("debug", "component added: "..host);\r
65                 return hosts[host];\r
66         else\r
67                 log("error", "Attempt to set component for existing host: "..host);\r
68         end\r
69 end\r
70
71 function deregister_component(host)
72         if components[host] then
73                 modulemanager.unload(host, "dialback");
74                 components[host] = nil;
75                 hosts[host] = nil;
76                 log("debug", "component removed: "..host);
77                 return true;
78         else
79                 log("error", "Attempt to remove component for non-existing host: "..host);
80         end
81 end
82 \r
83 return _M;\r