Fix for never checking if the first module for a host is already loaded (affects...
[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 = true, s2sout = {} };
31                         modulemanager.load(host, "dialback");
32                         local ok, err = modulemanager.load(host, host_config.core.component_module);
33                         if not ok then
34                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
35                         else
36                                 log("info", "Activated %s component: %s", host_config.core.component_module, host);
37                         end
38                         
39                         local ok, component_handler = modulemanager.call_module_method(modulemanager.get_module(host, host_config.core.component_module), "load_component");
40                         if not ok then
41                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(component_handler));
42                         else
43                                 components[host] = component_handler;
44                         end
45                 end
46         end
47 end
48
49 eventmanager.add_event_hook("server-starting", load_enabled_components);
50 \r
51 function handle_stanza(origin, stanza)\r
52         local node, host = jid_split(stanza.attr.to);\r
53         local component = nil;
54         if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
55         if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server\r
56         if not component then component = components[host]; end
57         if component then\r
58                 log("debug", "stanza being handled by component: "..host);\r
59                 component(origin, stanza, hosts[host]);\r
60         else\r
61                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);\r
62         end\r
63 end\r
64 \r
65 function register_component(host, component)\r
66         if not hosts[host] then\r
67                 -- TODO check for host well-formedness\r
68                 components[host] = component;\r
69                 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
70                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
71                 modulemanager.load(host, "dialback");\r
72                 log("debug", "component added: "..host);\r
73                 return hosts[host];\r
74         else\r
75                 log("error", "Attempt to set component for existing host: "..host);\r
76         end\r
77 end\r
78
79 function deregister_component(host)
80         if components[host] then
81                 modulemanager.unload(host, "dialback");
82                 components[host] = nil;
83                 hosts[host] = nil;
84                 log("debug", "component removed: "..host);
85                 return true;
86         else
87                 log("error", "Attempt to remove component for non-existing host: "..host);
88         end
89 end
90 \r
91 return _M;\r