Annotate

core/componentmanager.lua @ 760:90ce865eebd8

Update copyright notices for 2009
author Matthew Wild <mwild1@gmail.com>
date Fri, 30 Jan 2009 17:59:26 +0000
parent 759:5cccfb5da6cb
child 777:f7a87acea220
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
759
5cccfb5da6cb 0.2->0.3
Matthew Wild <mwild1@gmail.com>
parents: 758
diff changeset
1 -- Prosody IM v0.3
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 751
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 751
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
9
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
610
d98106902f74 Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
12 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
13 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
14 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
15 local modulemanager = require "core.modulemanager";
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 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
17 local hosts = hosts;
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
18
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
19 local pairs, type, tostring = pairs, type, tostring;
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local components = {};
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 module "componentmanager"
751
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
24
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
25 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
26 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
27
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
28 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
29 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
30 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
31 modulemanager.load(host, "dialback");
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
32 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
33 if not ok then
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
34 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
35 else
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
36 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
37 end
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 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
40 if not ok then
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
41 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
42 else
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
43 components[host] = component_handler;
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
44 end
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
45 end
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
46 end
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
47 end
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
48
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
49 eventmanager.add_event_hook("server-starting", load_enabled_components);
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 function handle_stanza(origin, stanza)
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 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
53 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
54 if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 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
56 if not component then component = components[host]; end
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 if component then
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 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
59 component(origin, stanza, hosts[host]);
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 else
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 end
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 end
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 function register_component(host, component)
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 if not hosts[host] then
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 -- TODO check for host well-formedness
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 components[host] = component;
610
d98106902f74 Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
69 hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
d98106902f74 Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
70 -- 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
71 modulemanager.load(host, "dialback");
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 log("debug", "component added: "..host);
270
837c7f701a56 Return registered host table when registering a component
Matthew Wild <mwild1@gmail.com>
parents: 261
diff changeset
73 return hosts[host];
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 else
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 log("error", "Attempt to set component for existing host: "..host);
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
673
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
78
703
f9909efed20c componentmanager: Removed unneeded parameter from componentmanager.deregister_component
Waqas Hussain <waqas20@gmail.com>
parents: 673
diff changeset
79 function deregister_component(host)
673
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
80 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
81 modulemanager.unload(host, "dialback");
673
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
82 components[host] = nil;
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
83 hosts[host] = nil;
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
84 log("debug", "component removed: "..host);
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
85 return true;
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
86 else
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
87 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
88 end
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
89 end
212
9d6da9ed9063 Internal component support
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90
217
d522f3a25dda Re-applying my changes to componentmanager. Sigh.
Matthew Wild <mwild1@gmail.com>
parents: 212
diff changeset
91 return _M;