core/modulemanager.lua

Wed, 03 Dec 2008 14:39:07 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 03 Dec 2008 14:39:07 +0000
changeset 519
cccd610a0ef9
parent 467
66f145f5c932
child 540
ec03f6968fa8
permissions
-rw-r--r--

Insert copyright/license headers

519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
1 -- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
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: 467
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: 467
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
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: 467
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
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: 467
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: 467
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: 467
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 467
diff changeset
20
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
22 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
23
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
24 local logger = require "util.logger";
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
25 local log = logger.init("modulemanager")
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local loadfile, pcall = loadfile, pcall;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local pairs, ipairs = pairs, ipairs;
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
30 local t_insert = table.insert;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local type = type;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local tostring, print = tostring, print;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
35 -- We need this to let modules access the real global namespace
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local _G = _G;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 module "modulemanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
40 local api = {}; -- Module API container
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
41
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
42 local modulemap = {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
43
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local handler_info = {};
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
45 local stanza_handlers = {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
46
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local modulehelpers = setmetatable({}, { __index = _G });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
50 function load(host, module_name, config)
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
51 if not (host and module_name) then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
52 return nil, "insufficient-parameters";
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
53 end
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
54 local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if not mod then
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
56 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
229
01bd24ea488d We now fail if modules fail to load at startup.
Waqas Hussain <waqas20@gmail.com>
parents: 218
diff changeset
57 return nil, err;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
60 if not modulemap[host] then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
61 modulemap[host] = {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
62 stanza_handlers[host] = {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
63 elseif modulemap[host][module_name] then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
64 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
65 return nil, "module-already-loaded";
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
66 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
67
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
68 local _log = logger.init(host..":"..module_name);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
69 local api_instance = setmetatable({ name = module_name, host = host, config = config, _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
70
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
71 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 setfenv(mod, pluginenv);
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
74
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local success, ret = pcall(mod);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if not success then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
391
79bd7a3e906c Typo prevented modulemanager.load() from returning the error if load failed.
Matthew Wild <mwild1@gmail.com>
parents: 385
diff changeset
78 return nil, ret;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
80
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
81 modulemap[host][module_name] = mod;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
82
229
01bd24ea488d We now fail if modules fail to load at startup.
Waqas Hussain <waqas20@gmail.com>
parents: 218
diff changeset
83 return true;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
439
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
86 function is_loaded(host, name)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
87 return modulemap[host] and modulemap[host][name] and true;
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
88 end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
89
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
90 function unload(host, name, ...)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
91 local mod = modulemap[host] and modulemap[host][name];
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
92 if not mod then return nil, "module-not-loaded"; end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
93
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
94 if type(mod.unload) == "function" then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
95 local ok, err = pcall(mod.unload, ...)
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
96 if (not ok) and err then
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
97 log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
98 end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
99 end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
100
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
101 end
6608ad3a72f3 is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
102
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
103 function handle_stanza(host, origin, stanza)
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
104 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
106 local handlers = stanza_handlers[host];
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
107 if not handlers then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
108 log("warn", "No handlers for %s", host);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
109 return false;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
110 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
111
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
112 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local child = stanza.tags[1];
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 if child then
300
0ebf2ef5124e If iq child element has no xmlns, use parent's
Matthew Wild <mwild1@gmail.com>
parents: 229
diff changeset
115 local xmlns = child.attr.xmlns or xmlns;
0ebf2ef5124e If iq child element has no xmlns, use parent's
Matthew Wild <mwild1@gmail.com>
parents: 229
diff changeset
116 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
398
79f84fc3e9ae Check to prevent error on IQs from completely unhandled origins
Waqas Hussain <waqas20@gmail.com>
parents: 391
diff changeset
117 local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
79f84fc3e9ae Check to prevent error on IQs from completely unhandled origins
Waqas Hussain <waqas20@gmail.com>
parents: 391
diff changeset
118 if handler then
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return handler(origin, stanza) or true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
123 elseif handlers[origin_type] then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
124 local handler = handlers[origin_type][name];
391
79bd7a3e906c Typo prevented modulemanager.load() from returning the error if load failed.
Matthew Wild <mwild1@gmail.com>
parents: 385
diff changeset
125 if handler then
47
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
126 handler = handler[xmlns];
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
127 if handler then
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
128 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
129 return handler(origin, stanza) or true;
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
130 end
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
131 end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
47
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
133 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 return false; -- we didn't handle it
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
136
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
137 ----- API functions exposed to modules -----------
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
138 -- Must all be in api.*
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
139
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
140 -- Returns the name of the current module
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
141 function api:get_name()
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
142 return self.name;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
143 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
144
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
145 -- Returns the host that the current module is serving
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
146 function api:get_host()
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
147 return self.host;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
148 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
149
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
150
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
151 local function _add_iq_handler(module, origin_type, xmlns, handler)
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
152 local handlers = stanza_handlers[module.host];
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
153 handlers[origin_type] = handlers[origin_type] or {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
154 handlers[origin_type].iq = handlers[origin_type].iq or {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
155 if not handlers[origin_type].iq[xmlns] then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
156 handlers[origin_type].iq[xmlns]= handler;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
157 handler_info[handler] = module;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
158 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
159 else
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
160 module:log("warn", "I wanted to handle tag 'iq' [%s] with payload namespace '%s' but mod_%s already handles that", origin_type, xmlns, handler_info[handlers[origin_type].iq[xmlns]].name);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
161 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
162 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
163
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
164 function api:add_iq_handler(origin_type, xmlns, handler)
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
165 if not (origin_type and handler and xmlns) then return false; end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
166 if type(origin_type) == "table" then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
167 for _, origin_type in ipairs(origin_type) do
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
168 _add_iq_handler(self, origin_type, xmlns, handler);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
169 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
170 return;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
171 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
172 _add_iq_handler(self, origin_type, xmlns, handler);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
173 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
174
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
175
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
176 do
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
177 local event_handlers = {};
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
178
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
179 function api:add_event_hook(name, handler)
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
180 if not event_handlers[name] then
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
181 event_handlers[name] = {};
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
182 end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
183 t_insert(event_handlers[name] , handler);
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
184 self:log("debug", "Subscribed to %s", name);
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
185 end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
186
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
187 function fire_event(name, ...)
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
188 local event_handlers = event_handlers[name];
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
189 if event_handlers then
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
190 for name, handler in ipairs(event_handlers) do
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
191 handler(...);
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
192 end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
193 end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
194 end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
195 end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
196
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
197
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
198 local function _add_handler(module, origin_type, tag, xmlns, handler)
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
199 local handlers = stanza_handlers[module.host];
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
200 handlers[origin_type] = handlers[origin_type] or {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
201 if not handlers[origin_type][tag] then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
202 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
203 handlers[origin_type][tag][xmlns]= handler;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
204 handler_info[handler] = module;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
205 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
206 elseif handler_info[handlers[origin_type][tag]] then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
207 log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[origin_type][tag]].module.name);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
208 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
209 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
210
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
211 function api:add_handler(origin_type, tag, xmlns, handler)
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
212 if not (origin_type and tag and xmlns and handler) then return false; end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
213 if type(origin_type) == "table" then
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
214 for _, origin_type in ipairs(origin_type) do
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
215 _add_handler(self, origin_type, tag, xmlns, handler);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
216 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
217 return;
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
218 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
219 _add_handler(self, origin_type, tag, xmlns, handler);
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
220 end
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
221
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
222 --------------------------------------------------------------------
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 400
diff changeset
223
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
224 return _M;

mercurial