Thu, 27 Nov 2008 17:12:05 +0000
Merge from waqas
30 | 1 | |
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
|
2 | |
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
|
3 | 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
|
4 | local log = logger.init("modulemanager") |
30 | 5 | |
6 | local loadfile, pcall = loadfile, pcall; | |
7 | local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv; | |
8 | local pairs, ipairs = pairs, ipairs; | |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
9 | local t_insert = table.insert; |
30 | 10 | local type = type; |
11 | ||
12 | local tostring, print = tostring, print; | |
13 | ||
14 | local _G = _G; | |
300
0ebf2ef5124e
If iq child element has no xmlns, use parent's
Matthew Wild <mwild1@gmail.com>
parents:
229
diff
changeset
|
15 | local debug = debug; |
30 | 16 | |
17 | module "modulemanager" | |
18 | ||
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
|
19 | 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
|
20 | |
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
|
21 | 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
|
22 | |
30 | 23 | 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
|
24 | 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
|
25 | |
30 | 26 | local modulehelpers = setmetatable({}, { __index = _G }); |
27 | ||
28 | ||
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
|
29 | function load(host, module_name, config) |
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
30 | if not (host and module_name) then |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
31 | return nil, "insufficient-parameters"; |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
32 | 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
|
33 | local mod, err = loadfile("plugins/mod_"..module_name..".lua"); |
30 | 34 | 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
|
35 | 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
|
36 | return nil, err; |
30 | 37 | end |
38 | ||
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
|
39 | 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
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | |
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
|
47 | 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
|
48 | 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
|
49 | |
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 | local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); |
30 | 51 | |
52 | 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
|
53 | |
30 | 54 | local success, ret = pcall(mod); |
55 | if not success then | |
56 | 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
|
57 | return nil, ret; |
30 | 58 | 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
|
59 | |
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 | 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
|
61 | |
229
01bd24ea488d
We now fail if modules fail to load at startup.
Waqas Hussain <waqas20@gmail.com>
parents:
218
diff
changeset
|
62 | return true; |
30 | 63 | end |
64 | ||
439
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
65 | function is_loaded(host, name) |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
66 | 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
|
67 | end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
68 | |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
69 | function unload(host, name, ...) |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
70 | 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
|
71 | 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
|
72 | |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
73 | if type(mod.unload) == "function" then |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
74 | local ok, err = pcall(mod.unload, ...) |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
75 | if (not ok) and err then |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
76 | 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
|
77 | end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
78 | end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
79 | |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
80 | end |
6608ad3a72f3
is_loaded() and incomplete unload() for modules
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
81 | |
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
|
82 | function handle_stanza(host, origin, stanza) |
38 | 83 | local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; |
30 | 84 | |
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
|
85 | 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
|
86 | 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
|
87 | 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
|
88 | 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
|
89 | 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
|
90 | |
38 | 91 | if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then |
30 | 92 | local child = stanza.tags[1]; |
93 | if child then | |
300
0ebf2ef5124e
If iq child element has no xmlns, use parent's
Matthew Wild <mwild1@gmail.com>
parents:
229
diff
changeset
|
94 | 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
|
95 | 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
|
96 | 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
|
97 | if handler then |
30 | 98 | log("debug", "Passing stanza to mod_%s", handler_info[handler].name); |
99 | return handler(origin, stanza) or true; | |
100 | end | |
101 | end | |
38 | 102 | elseif handlers[origin_type] then |
103 | 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
|
104 | if handler then |
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
105 | handler = handler[xmlns]; |
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
106 | if handler then |
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
107 | 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
|
108 | 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
|
109 | end |
38 | 110 | end |
30 | 111 | end |
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
112 | log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); |
30 | 113 | return false; -- we didn't handle it |
114 | end | |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
115 | |
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
|
116 | ----- 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
|
117 | -- 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
|
118 | |
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
|
119 | -- 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
|
120 | 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
|
121 | 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
|
122 | 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
|
123 | |
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
|
124 | -- 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
|
125 | 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
|
126 | 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
|
127 | 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
|
128 | |
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
|
129 | |
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
|
130 | 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
|
131 | 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
|
132 | 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
|
133 | 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
|
134 | 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
|
135 | 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
|
136 | 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
|
137 | 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
|
138 | 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
|
139 | 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
|
140 | 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
|
141 | 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
|
142 | |
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 | 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
|
144 | 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
|
145 | 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
|
146 | 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
|
147 | _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
|
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 | 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
|
150 | 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
|
151 | _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
|
152 | 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
|
153 | |
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 | |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
155 | do |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
156 | local event_handlers = {}; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
157 | |
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
|
158 | 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
|
159 | if not event_handlers[name] then |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
160 | event_handlers[name] = {}; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
161 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
162 | 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
|
163 | 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
|
164 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
165 | |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
166 | function fire_event(name, ...) |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
167 | local event_handlers = event_handlers[name]; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
168 | if event_handlers then |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
169 | 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
|
170 | handler(...); |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
171 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
172 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
173 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
174 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
175 | |
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
|
176 | |
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
|
177 | 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
|
178 | 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
|
179 | 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
|
180 | 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
|
181 | 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
|
182 | 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
|
183 | 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
|
184 | 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
|
185 | 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
|
186 | 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
|
187 | 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
|
188 | 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
|
189 | |
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
|
190 | 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
|
191 | 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
|
192 | 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
|
193 | 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
|
194 | _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
|
195 | 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
|
196 | 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
|
197 | 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
|
198 | _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
|
199 | 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
|
200 | |
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 | -------------------------------------------------------------------- |
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 | |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
203 | return _M; |