159311673ce45f8f800ab76b84efee6e57b342a4
[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
11
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";
16 local jid_split = require "util.jid".split;
17 local hosts = hosts;
18
19 local pairs, type, tostring = pairs, type, tostring;
20
21 local components = {};
22
23 module "componentmanager"
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
43 function handle_stanza(origin, stanza)
44         local node, host = jid_split(stanza.attr.to);
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
48         if not component then component = components[host]; end
49         if component then
50                 log("debug", "stanza being handled by component: "..host);
51                 component(origin, stanza, hosts[host]);
52         else
53                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
54         end
55 end
56
57 function create_component(host, component)
58                 -- TODO check for host well-formedness
59                 session = session or { type = "component", host = host, connected = true, s2sout = {}, send = component };
60                 return session;
61 end
62
63 function register_component(host, component, session)
64         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
65                 components[host] = component;
66                 hosts[host] = session or create_component(host, component);
67                 
68                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
69                 modulemanager.load(host, "dialback");
70                 log("debug", "component added: "..host);
71                 return session or hosts[host];
72         else
73                 log("error", "Attempt to set component for existing host: "..host);
74         end
75 end
76
77 function deregister_component(host)
78         if components[host] then
79                 modulemanager.unload(host, "dialback");
80                 components[host] = nil;
81                 hosts[host] = nil;
82                 log("debug", "component removed: "..host);
83                 return true;
84         else
85                 log("error", "Attempt to remove component for non-existing host: "..host);
86         end
87 end
88
89 return _M;