Software / code / prosody
Annotate
core/modulemanager.lua @ 218:1263896ab2f1
Reworked the way lxmppd.cfg is used
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Thu, 06 Nov 2008 16:52:39 +0500 |
| parent | 198:e4755408d40b |
| child | 229:01bd24ea488d |
| rev | line source |
|---|---|
| 30 | 1 |
| 2 local log = require "util.logger".init("modulemanager") | |
| 3 | |
| 4 local loadfile, pcall = loadfile, pcall; | |
| 5 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv; | |
| 6 local pairs, ipairs = pairs, ipairs; | |
|
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
7 local t_insert = table.insert; |
| 30 | 8 local type = type; |
| 9 | |
| 10 local tostring, print = tostring, print; | |
| 11 | |
| 12 local _G = _G; | |
| 13 | |
| 14 module "modulemanager" | |
| 15 | |
| 16 local handler_info = {}; | |
| 17 local handlers = {}; | |
| 18 | |
| 19 local modulehelpers = setmetatable({}, { __index = _G }); | |
| 20 | |
|
196
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
21 local function _add_iq_handler(module, origin_type, xmlns, handler) |
| 30 | 22 handlers[origin_type] = handlers[origin_type] or {}; |
| 23 handlers[origin_type].iq = handlers[origin_type].iq or {}; | |
| 24 if not handlers[origin_type].iq[xmlns] then | |
| 25 handlers[origin_type].iq[xmlns]= handler; | |
|
196
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
26 handler_info[handler] = module; |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
27 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", module.name, xmlns); |
| 30 | 28 else |
|
196
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
29 log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name); |
| 30 | 30 end |
| 31 end | |
| 32 | |
|
196
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
33 function modulehelpers.add_iq_handler(origin_type, xmlns, handler) |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
34 if not (origin_type and handler and xmlns) then return false; end |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
35 if type(origin_type) == "table" then |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
36 for _, origin_type in ipairs(origin_type) do |
|
198
e4755408d40b
Fix for previous commit (again)
Matthew Wild <mwild1@gmail.com>
parents:
197
diff
changeset
|
37 _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler); |
|
196
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
38 end |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
39 return; |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
40 end |
| 197 | 41 _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler); |
|
196
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
42 end |
|
ebe23269b377
Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents:
191
diff
changeset
|
43 |
| 191 | 44 local function _add_handler(module, origin_type, tag, xmlns, handler) |
| 38 | 45 handlers[origin_type] = handlers[origin_type] or {}; |
| 46 if not handlers[origin_type][tag] then | |
|
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
47 handlers[origin_type][tag] = handlers[origin_type][tag] or {}; |
|
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
48 handlers[origin_type][tag][xmlns]= handler; |
| 191 | 49 handler_info[handler] = module; |
| 50 log("debug", "mod_%s now handles tag '%s'", module.name, tag); | |
| 38 | 51 elseif handler_info[handlers[origin_type][tag]] then |
| 191 | 52 log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name); |
| 38 | 53 end |
| 30 | 54 end |
|
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
55 |
| 191 | 56 function modulehelpers.add_handler(origin_type, tag, xmlns, handler) |
| 57 if not (origin_type and tag and xmlns and handler) then return false; end | |
| 58 if type(origin_type) == "table" then | |
| 59 for _, origin_type in ipairs(origin_type) do | |
| 60 _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler); | |
| 61 end | |
| 62 return; | |
| 63 end | |
| 64 _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler); | |
| 65 end | |
| 66 | |
| 30 | 67 function load(name) |
| 68 local mod, err = loadfile("plugins/mod_"..name..".lua"); | |
| 69 if not mod then | |
| 70 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil"); | |
| 71 return; | |
| 72 end | |
| 73 | |
| 74 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers }); | |
| 75 | |
| 76 setfenv(mod, pluginenv); | |
| 77 local success, ret = pcall(mod); | |
| 78 if not success then | |
| 79 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil"); | |
| 80 return; | |
| 81 end | |
| 82 end | |
| 83 | |
| 84 function handle_stanza(origin, stanza) | |
| 38 | 85 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; |
| 30 | 86 |
| 38 | 87 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then |
| 30 | 88 log("debug", "Stanza is an <iq/>"); |
| 89 local child = stanza.tags[1]; | |
| 90 if child then | |
| 91 local xmlns = child.attr.xmlns; | |
| 92 log("debug", "Stanza has xmlns: %s", xmlns); | |
| 93 local handler = handlers[origin_type][name][xmlns]; | |
| 94 if handler then | |
| 95 log("debug", "Passing stanza to mod_%s", handler_info[handler].name); | |
| 96 return handler(origin, stanza) or true; | |
| 97 end | |
| 98 | |
| 99 end | |
| 38 | 100 elseif handlers[origin_type] then |
| 101 local handler = handlers[origin_type][name]; | |
| 102 if handler then | |
|
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
103 handler = handler[xmlns]; |
|
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
104 if handler then |
|
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
105 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
|
106 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
|
107 end |
| 38 | 108 end |
| 30 | 109 end |
|
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
110 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); |
| 30 | 111 return false; -- we didn't handle it |
| 112 end | |
|
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
113 |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
114 do |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
115 local event_handlers = {}; |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
116 |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
117 function modulehelpers.add_event_hook(name, handler) |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
118 if not event_handlers[name] then |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
119 event_handlers[name] = {}; |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
120 end |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
121 t_insert(event_handlers[name] , handler); |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
122 end |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
123 |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
124 function fire_event(name, ...) |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
125 local event_handlers = event_handlers[name]; |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
126 if event_handlers then |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
127 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
|
128 handler(...); |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
129 end |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
130 end |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
131 end |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
132 end |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
133 |
|
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
134 return _M; |