Sat, 04 Oct 2008 02:09:46 +0100
Small fixes
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 | ||
42 | 34 | function modulehelpers.add_handler(origin_type, tag, xmlns, handler) |
35 | if not (origin_type and tag and xmlns and handler) then return false; end | |
38 | 36 | handlers[origin_type] = handlers[origin_type] or {}; |
37 | if not handlers[origin_type][tag] then | |
38 | handlers[origin_type][tag]= handler; | |
39 | handler_info[handler] = getfenv(2).module; | |
40 | log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag); | |
41 | elseif handler_info[handlers[origin_type][tag]] then | |
42 | log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", getfenv(2).module.name, tag, handler_info[handlers[origin_type][tag]].module.name); | |
43 | end | |
30 | 44 | end |
45 | ||
46 | function loadall() | |
38 | 47 | load("saslauth"); |
30 | 48 | load("legacyauth"); |
49 | load("roster"); | |
50 | end | |
51 | ||
52 | function load(name) | |
53 | local mod, err = loadfile("plugins/mod_"..name..".lua"); | |
54 | if not mod then | |
55 | log("error", "Unable to load module '%s': %s", name or "nil", err or "nil"); | |
56 | return; | |
57 | end | |
58 | ||
59 | local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers }); | |
60 | ||
61 | setfenv(mod, pluginenv); | |
62 | local success, ret = pcall(mod); | |
63 | if not success then | |
64 | log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil"); | |
65 | return; | |
66 | end | |
67 | end | |
68 | ||
69 | function handle_stanza(origin, stanza) | |
38 | 70 | local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; |
30 | 71 | |
38 | 72 | if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then |
30 | 73 | log("debug", "Stanza is an <iq/>"); |
74 | local child = stanza.tags[1]; | |
75 | if child then | |
76 | local xmlns = child.attr.xmlns; | |
77 | log("debug", "Stanza has xmlns: %s", xmlns); | |
78 | local handler = handlers[origin_type][name][xmlns]; | |
79 | if handler then | |
80 | log("debug", "Passing stanza to mod_%s", handler_info[handler].name); | |
81 | return handler(origin, stanza) or true; | |
82 | end | |
83 | ||
84 | end | |
38 | 85 | elseif handlers[origin_type] then |
86 | local handler = handlers[origin_type][name]; | |
87 | if handler then | |
88 | log("debug", "Passing stanza to mod_%s", handler_info[handler].name); | |
89 | return handler(origin, stanza) or true; | |
90 | end | |
30 | 91 | end |
92 | log("debug", "Stanza unhandled by any modules"); | |
93 | return false; -- we didn't handle it | |
94 | end | |
39
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
95 | |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
96 | do |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
97 | local event_handlers = {}; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
98 | |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
99 | 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
|
100 | if not event_handlers[name] then |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
101 | event_handlers[name] = {}; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
102 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
103 | t_insert(event_handlers[name] , handler); |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
104 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
105 | |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
106 | function fire_event(name, ...) |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
107 | local event_handlers = event_handlers[name]; |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
108 | if event_handlers then |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
109 | 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
|
110 | handler(...); |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
111 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
112 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
113 | end |
89877d61ac51
Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents:
38
diff
changeset
|
114 | end |
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 | return _M; |