0c33e7f69b63f77d8ca799c74ccb406d40d66393
[prosody.git] / core / componentmanager.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21 \r
22 \r
23 local log = require "util.logger".init("componentmanager");
24 local configmanager = require "core.configmanager";
25 local eventmanager = require "core.eventmanager";
26 local modulemanager = require "core.modulemanager";\r
27 local jid_split = require "util.jid".split;\r
28 local hosts = hosts;
29
30 local pairs, type, tostring = pairs, type, tostring;\r
31 \r
32 local components = {};\r
33 \r
34 module "componentmanager"\r
35
36 function load_enabled_components(config)
37         local defined_hosts = config or configmanager.getconfig();
38                 
39         for host, host_config in pairs(defined_hosts) do
40                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
41                         hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
42                         modulemanager.load(host, "dialback");
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                                 log("info", "Activated %s component: %s", host_config.core.component_module, host);
48                         end
49                         
50                         local ok, component_handler = modulemanager.call_module_method(modulemanager.get_module(host, host_config.core.component_module), "load_component");
51                         if not ok then
52                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(component_handler));
53                         else
54                                 components[host] = component_handler;
55                         end
56                 end
57         end
58 end
59
60 eventmanager.add_event_hook("server-starting", load_enabled_components);
61 \r
62 function handle_stanza(origin, stanza)\r
63         local node, host = jid_split(stanza.attr.to);\r
64         local component = nil;
65         if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
66         if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server\r
67         if not component then component = components[host]; end
68         if component then\r
69                 log("debug", "stanza being handled by component: "..host);\r
70                 component(origin, stanza, hosts[host]);\r
71         else\r
72                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);\r
73         end\r
74 end\r
75 \r
76 function register_component(host, component)\r
77         if not hosts[host] then\r
78                 -- TODO check for host well-formedness\r
79                 components[host] = component;\r
80                 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
81                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
82                 modulemanager.load(host, "dialback");\r
83                 log("debug", "component added: "..host);\r
84                 return hosts[host];\r
85         else\r
86                 log("error", "Attempt to set component for existing host: "..host);\r
87         end\r
88 end\r
89
90 function deregister_component(host)
91         if components[host] then
92                 modulemanager.unload(host, "dialback");
93                 components[host] = nil;
94                 hosts[host] = nil;
95                 log("debug", "component removed: "..host);
96                 return true;
97         else
98                 log("error", "Attempt to remove component for non-existing host: "..host);
99         end
100 end
101 \r
102 return _M;\r