Software /
code /
prosody
Annotate
core/modulemanager.lua @ 194:4ea1ec218976
Fix MD5 loading check
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 02 Nov 2008 01:19:23 +0000 |
parent | 191:e64c8a44060f |
child | 196:ebe23269b377 |
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 | |
21 function modulehelpers.add_iq_handler(origin_type, xmlns, handler) | |
42 | 22 if not (origin_type and handler and xmlns) then return false; end |
30 | 23 handlers[origin_type] = handlers[origin_type] or {}; |
24 handlers[origin_type].iq = handlers[origin_type].iq or {}; | |
25 if not handlers[origin_type].iq[xmlns] then | |
26 handlers[origin_type].iq[xmlns]= handler; | |
27 handler_info[handler] = getfenv(2).module; | |
38 | 28 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns); |
30 | 29 else |
38 | 30 log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", getfenv(2).module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name); |
30 | 31 end |
32 end | |
33 | |
191 | 34 local function _add_handler(module, origin_type, tag, xmlns, handler) |
38 | 35 handlers[origin_type] = handlers[origin_type] or {}; |
36 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
|
37 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
|
38 handlers[origin_type][tag][xmlns]= handler; |
191 | 39 handler_info[handler] = module; |
40 log("debug", "mod_%s now handles tag '%s'", module.name, tag); | |
38 | 41 elseif handler_info[handlers[origin_type][tag]] then |
191 | 42 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 | 43 end |
30 | 44 end |
47
33ed4c6ac249
Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
45 |
191 | 46 function modulehelpers.add_handler(origin_type, tag, xmlns, handler) |
47 if not (origin_type and tag and xmlns and handler) then return false; end | |
48 if type(origin_type) == "table" then | |
49 for _, origin_type in ipairs(origin_type) do | |
50 _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler); | |
51 end | |
52 return; | |
53 end | |
54 _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler); | |
55 end | |
56 | |
30 | 57 function loadall() |
38 | 58 load("saslauth"); |
30 | 59 load("legacyauth"); |
60 load("roster"); | |
63
4c27740fdeff
mod_InBandRegistration -> mod_register
Matthew Wild <mwild1@gmail.com>
parents:
60
diff
changeset
|
61 load("register"); |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
63
diff
changeset
|
62 load("tls"); |
86
a2085854c72c
Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
65
diff
changeset
|
63 load("vcard"); |
185
a67c88ce1c6a
Added support for XEP-0049: Private XML Storage (mod_private)
Waqas Hussain <waqas20@gmail.com>
parents:
86
diff
changeset
|
64 load("private"); |
188 | 65 load("version"); |
191 | 66 load("dialback"); |
30 | 67 end |
68 | |
69 function load(name) | |
70 local mod, err = loadfile("plugins/mod_"..name..".lua"); | |
71 if not mod then | |
72 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil"); | |
73 return; | |
74 end | |
75 | |
76 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers }); | |
77 | |
78 setfenv(mod, pluginenv); | |
79 local success, ret = pcall(mod); | |
80 if not success then | |
81 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil"); | |
82 return; | |
83 end | |
84 end | |
85 | |
86 function handle_stanza(origin, stanza) | |
38 | 87 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; |
30 | 88 |
38 | 89 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then |
30 | 90 log("debug", "Stanza is an <iq/>"); |
91 local child = stanza.tags[1]; | |
92 if child then | |
93 local xmlns = child.attr.xmlns; | |
94 log("debug", "Stanza has xmlns: %s", xmlns); | |
95 local handler = handlers[origin_type][name][xmlns]; | |
96 if handler then | |
97 log("debug", "Passing stanza to mod_%s", handler_info[handler].name); | |
98 return handler(origin, stanza) or true; | |
99 end | |
100 | |
101 end | |
38 | 102 elseif handlers[origin_type] then |
103 local handler = handlers[origin_type][name]; | |
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 |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
116 do |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
117 local event_handlers = {}; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
118 |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
119 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
|
120 if not event_handlers[name] then |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
121 event_handlers[name] = {}; |
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 t_insert(event_handlers[name] , handler); |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
124 end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
125 |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
126 function fire_event(name, ...) |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
127 local event_handlers = event_handlers[name]; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
128 if event_handlers then |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
129 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
|
130 handler(...); |
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 end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
134 end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
135 |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
136 return _M; |