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