208f42e4e720e9a3106d6b02b57f29a76fd49a04
[prosody.git] / core / componentmanager.lua
1 -- Prosody IM
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 local prosody = prosody;
12 local log = require "util.logger".init("componentmanager");
13 local configmanager = require "core.configmanager";
14 local modulemanager = require "core.modulemanager";
15 local core_route_stanza = core_route_stanza;
16 local jid_split = require "util.jid".split;
17 local fire_event = require "core.eventmanager".fire_event;
18 local events_new = require "util.events".new;
19 local st = require "util.stanza";
20 local hosts = hosts;
21 local serialize = require "util.serialization".serialize
22
23 local pairs, type, tostring = pairs, type, tostring;
24
25 local components = {};
26
27 local disco_items = require "util.multitable".new();
28 local NULL = {};
29 require "core.discomanager".addDiscoItemsHandler("*host", function(reply, to, from, node)
30         if #node == 0 and hosts[to] then
31                 for jid in pairs(disco_items:get(to) or NULL) do
32                         reply:tag("item", {jid = jid}):up();
33                 end
34                 return true;
35         end
36 end);
37
38 prosody.events.add_handler("server-starting", function () core_route_stanza = _G.core_route_stanza; end);
39
40 module "componentmanager"
41
42 local function default_component_handler(origin, stanza)
43         log("warn", "Stanza being handled by default component, bouncing error");
44         if stanza.attr.type ~= "error" then
45                 core_route_stanza(nil, st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
46         end
47 end
48
49 local components_loaded_once;
50 function load_enabled_components(config)
51         local defined_hosts = config or configmanager.getconfig();
52                 
53         for host, host_config in pairs(defined_hosts) do
54                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
55                         hosts[host] = create_component(host);
56                         hosts[host].connected = false;
57                         components[host] = default_component_handler;
58                         local ok, err = modulemanager.load(host, host_config.core.component_module);
59                         if not ok then
60                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
61                         else
62                                 fire_event("component-activated", host, host_config);
63                                 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
64                         end
65                 end
66         end
67 end
68
69 prosody.events.add_handler("server-starting", load_enabled_components);
70
71 function handle_stanza(origin, stanza)
72         local node, host = jid_split(stanza.attr.to);
73         local component = nil;
74         if host then
75                 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
76                 if not component then component = components[host]; end
77         end
78         if component then
79                 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
80                 component(origin, stanza, hosts[host]);
81         else
82                 log("error", "Component manager recieved a stanza for a non-existing component: " .. (stanza.attr.to or serialize(stanza)));
83         end
84 end
85
86 function create_component(host, component)
87         -- TODO check for host well-formedness
88         return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() };
89 end
90
91 function register_component(host, component, session)
92         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
93                 local old_events = hosts[host] and hosts[host].events;
94
95                 components[host] = component;
96                 hosts[host] = session or create_component(host, component);
97                 
98                 -- Add events object if not already one
99                 if not hosts[host].events then
100                         hosts[host].events = old_events or events_new();
101                 end
102                 
103                 -- add to disco_items
104                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
105                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
106                 end
107                 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
108                 modulemanager.load(host, "dialback");
109                 log("debug", "component added: "..host);
110                 return session or hosts[host];
111         else
112                 log("error", "Attempt to set component for existing host: "..host);
113         end
114 end
115
116 function deregister_component(host)
117         if components[host] then
118                 modulemanager.unload(host, "dialback");
119                 hosts[host].connected = nil;
120                 local host_config = configmanager.getconfig()[host];
121                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
122                         -- Set default handler
123                         components[host] = default_component_handler;
124                 else
125                         -- Component not in config, or disabled, remove
126                         hosts[host] = nil;
127                         components[host] = nil;
128                 end
129                 -- remove from disco_items
130                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
131                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
132                 end
133                 log("debug", "component removed: "..host);
134                 return true;
135         else
136                 log("error", "Attempt to remove component for non-existing host: "..host);
137         end
138 end
139
140 function set_component_handler(host, handler)
141         components[host] = handler;
142 end
143
144 return _M;