Software /
code /
prosody
Annotate
core/componentmanager.lua @ 751:7c22619fdb19
componentmanager: Add support for loading components defined in the config
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 29 Jan 2009 02:13:58 +0000 |
parent | 703:f9909efed20c |
child | 758:b1885732e979 |
rev | line source |
---|---|
615 | 1 -- Prosody IM v0.2 |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
2 -- Copyright (C) 2008 Matthew Wild |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
3 -- Copyright (C) 2008 Waqas Hussain |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
4 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
5 -- This program is free software; you can redistribute it and/or |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
6 -- modify it under the terms of the GNU General Public License |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
7 -- as published by the Free Software Foundation; either version 2 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
8 -- of the License, or (at your option) any later version. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
9 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
10 -- This program is distributed in the hope that it will be useful, |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
13 -- GNU General Public License for more details. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
14 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
15 -- You should have received a copy of the GNU General Public License |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
16 -- along with this program; if not, write to the Free Software |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
18 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
19 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
270
diff
changeset
|
20 |
212 | 21 |
22 | |
610
d98106902f74
Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
23 local log = require "util.logger".init("componentmanager"); |
751
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
24 local configmanager = require "core.configmanager"; |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
25 local eventmanager = require "core.eventmanager"; |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
26 local modulemanager = require "core.modulemanager"; |
212 | 27 local jid_split = require "util.jid".split; |
751
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
28 local hosts = hosts; |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
29 |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
30 local pairs, type, tostring = pairs, type, tostring; |
212 | 31 |
32 local components = {}; | |
33 | |
34 module "componentmanager" | |
751
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
35 |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
36 function load_enabled_components(config) |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
37 local defined_hosts = config or configmanager.getconfig(); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
38 |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
39 for host, host_config in pairs(defined_hosts) do |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
40 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
41 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} }; |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
42 modulemanager.load(host, "dialback"); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
43 local ok, err = modulemanager.load(host, host_config.core.component_module); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
44 if not ok then |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
45 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err)); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
46 else |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
47 log("info", "Activated %s component: %s", host_config.core.component_module, host); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
48 end |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
49 |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
50 local ok, component_handler = modulemanager.call_module_method(modulemanager.get_module(host, host_config.core.component_module), "load_component"); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
51 if not ok then |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
52 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(component_handler)); |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
53 else |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
54 components[host] = component_handler; |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
55 end |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
56 end |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
57 end |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
58 end |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
59 |
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
60 eventmanager.add_event_hook("server-starting", load_enabled_components); |
212 | 61 |
62 function handle_stanza(origin, stanza) | |
63 local node, host = jid_split(stanza.attr.to); | |
638
1915c64c9436
Changed order of checking for component hosts to check the full and bare JIDs before the hostname
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
64 local component = nil; |
1915c64c9436
Changed order of checking for component hosts to check the full and bare JIDs before the hostname
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
65 if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource |
212 | 66 if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server |
638
1915c64c9436
Changed order of checking for component hosts to check the full and bare JIDs before the hostname
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
67 if not component then component = components[host]; end |
212 | 68 if component then |
69 log("debug", "stanza being handled by component: "..host); | |
217
d522f3a25dda
Re-applying my changes to componentmanager. Sigh.
Matthew Wild <mwild1@gmail.com>
parents:
212
diff
changeset
|
70 component(origin, stanza, hosts[host]); |
212 | 71 else |
72 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to); | |
73 end | |
74 end | |
75 | |
76 function register_component(host, component) | |
77 if not hosts[host] then | |
78 -- TODO check for host well-formedness | |
79 components[host] = component; | |
610
d98106902f74
Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
80 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} }; |
d98106902f74
Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
81 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config |
751
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
82 modulemanager.load(host, "dialback"); |
212 | 83 log("debug", "component added: "..host); |
270
837c7f701a56
Return registered host table when registering a component
Matthew Wild <mwild1@gmail.com>
parents:
261
diff
changeset
|
84 return hosts[host]; |
212 | 85 else |
86 log("error", "Attempt to set component for existing host: "..host); | |
87 end | |
88 end | |
673
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
89 |
703
f9909efed20c
componentmanager: Removed unneeded parameter from componentmanager.deregister_component
Waqas Hussain <waqas20@gmail.com>
parents:
673
diff
changeset
|
90 function deregister_component(host) |
673
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
91 if components[host] then |
751
7c22619fdb19
componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents:
703
diff
changeset
|
92 modulemanager.unload(host, "dialback"); |
673
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
93 components[host] = nil; |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
94 hosts[host] = nil; |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
95 log("debug", "component removed: "..host); |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
96 return true; |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
97 else |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
98 log("error", "Attempt to remove component for non-existing host: "..host); |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
99 end |
c9bc58e84e96
componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents:
638
diff
changeset
|
100 end |
212 | 101 |
217
d522f3a25dda
Re-applying my changes to componentmanager. Sigh.
Matthew Wild <mwild1@gmail.com>
parents:
212
diff
changeset
|
102 return _M; |