9 local prosody = prosody; |
9 local prosody = prosody; |
10 local log = require "util.logger".init("componentmanager"); |
10 local log = require "util.logger".init("componentmanager"); |
11 local configmanager = require "core.configmanager"; |
11 local configmanager = require "core.configmanager"; |
12 local modulemanager = require "core.modulemanager"; |
12 local modulemanager = require "core.modulemanager"; |
13 local jid_split = require "util.jid".split; |
13 local jid_split = require "util.jid".split; |
|
14 local fire_event = require "core.eventmanager".fire_event; |
14 local events_new = require "util.events".new; |
15 local events_new = require "util.events".new; |
15 local st = require "util.stanza"; |
16 local st = require "util.stanza"; |
16 local hosts = hosts; |
17 local hosts = hosts; |
17 |
18 |
18 local pairs, type, tostring = pairs, type, tostring; |
19 local pairs, type, tostring = pairs, type, tostring; |
34 function load_enabled_components(config) |
35 function load_enabled_components(config) |
35 local defined_hosts = config or configmanager.getconfig(); |
36 local defined_hosts = config or configmanager.getconfig(); |
36 |
37 |
37 for host, host_config in pairs(defined_hosts) do |
38 for host, host_config in pairs(defined_hosts) do |
38 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then |
39 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then |
39 hosts[host] = { type = "component", host = host, connected = false, s2sout = {}, events = events_new() }; |
40 hosts[host] = create_component(host); |
|
41 hosts[host].connected = false; |
40 components[host] = default_component_handler; |
42 components[host] = default_component_handler; |
41 local ok, err = modulemanager.load(host, host_config.core.component_module); |
43 local ok, err = modulemanager.load(host, host_config.core.component_module); |
42 if not ok then |
44 if not ok then |
43 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err)); |
45 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err)); |
44 else |
46 else |
|
47 fire_event("component-activated", host, host_config); |
45 log("debug", "Activated %s component: %s", host_config.core.component_module, host); |
48 log("debug", "Activated %s component: %s", host_config.core.component_module, host); |
46 end |
49 end |
47 end |
50 end |
48 end |
51 end |
49 end |
52 end |
63 else |
66 else |
64 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza)); |
67 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza)); |
65 end |
68 end |
66 end |
69 end |
67 |
70 |
68 function create_component(host, component) |
71 function create_component(host, component, events) |
69 -- TODO check for host well-formedness |
72 -- TODO check for host well-formedness |
70 return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() }; |
73 return { type = "component", host = host, connected = true, s2sout = {}, events = events or events_new() }; |
71 end |
74 end |
72 |
75 |
73 function register_component(host, component, session) |
76 function register_component(host, component, session) |
74 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then |
77 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then |
75 local old_events = hosts[host] and hosts[host].events; |
78 local old_events = hosts[host] and hosts[host].events; |
76 |
79 |
77 components[host] = component; |
80 components[host] = component; |
78 hosts[host] = session or create_component(host, component); |
81 hosts[host] = session or create_component(host, component, old_events); |
79 |
82 |
80 -- Add events object if not already one |
83 -- Add events object if not already one |
81 if not hosts[host].events then |
84 if not hosts[host].events then |
82 hosts[host].events = old_events or events_new(); |
85 hosts[host].events = old_events or events_new(); |
83 end |
86 end |