core/modulemanager.lua

Sun, 23 Nov 2008 05:49:08 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 23 Nov 2008 05:49:08 +0000
changeset 400
068a813b6454
parent 397
d07ae6788196
parent 398
79f84fc3e9ae
child 438
193f9dd64f17
permissions
-rw-r--r--

Merge from waqas

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

mercurial