e7b5e4dcaac9bb102ea91eb7b127bcb753ad2d28
[prosody.git] / core / componentmanager.lua
1 -- Prosody IM v0.4
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 local disco_items = require "util.multitable".new();
24 local NULL = {};
25 require "core.discomanager".addDiscoItemsHandler("*host", function(reply, to, from, node)
26         if #node == 0 and hosts[to] then
27                 for jid in pairs(disco_items:get(to) or NULL) do
28                         reply:tag("item", {jid = jid}):up();
29                 end
30                 return true;
31         end
32 end);
33
34
35 module "componentmanager"
36
37 function load_enabled_components(config)
38         local defined_hosts = config or configmanager.getconfig();
39                 
40         for host, host_config in pairs(defined_hosts) do
41                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
42                         hosts[host] = { type = "component", host = host, connected = false, s2sout = {} };
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                 end
50         end
51 end
52
53 eventmanager.add_event_hook("server-starting", load_enabled_components);
54
55 function handle_stanza(origin, stanza)
56         local node, host = jid_split(stanza.attr.to);
57         local component = nil;
58         if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
59         if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
60         if not component then component = components[host]; end
61         if component then
62                 log("debug", "stanza being handled by component: "..host);
63                 component(origin, stanza, hosts[host]);
64         else
65                 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
66         end
67 end
68
69 function create_component(host, component)
70                 -- TODO check for host well-formedness
71                 session = session or { type = "component", host = host, connected = true, s2sout = {}, send = component };
72                 return session;
73 end
74
75 function register_component(host, component, session)
76         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
77                 components[host] = component;
78                 hosts[host] = session or create_component(host, component);
79                 -- add to disco_items
80                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
81                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
82                 end
83                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
84                 modulemanager.load(host, "dialback");
85                 log("debug", "component added: "..host);
86                 return session or hosts[host];
87         else
88                 log("error", "Attempt to set component for existing host: "..host);
89         end
90 end
91
92 function deregister_component(host)
93         if components[host] then
94                 modulemanager.unload(host, "dialback");
95                 components[host] = nil;
96                 hosts[host] = nil;
97                 -- remove from disco_items
98                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
99                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
100                 end
101                 log("debug", "component removed: "..host);
102                 return true;
103         else
104                 log("error", "Attempt to remove component for non-existing host: "..host);
105         end
106 end
107
108 return _M;