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