core/modulemanager.lua

Thu, 02 Oct 2008 01:08:58 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 02 Oct 2008 01:08:58 +0100
changeset 38
3fdfd6e0cb4e
parent 30
bcf539295f2d
child 39
89877d61ac51
permissions
-rw-r--r--

SASL!
(but before you get too excited, no resource binding yet. And yes, there are still plenty of rough edges to the code...)
((eg. must move <stream:features> out of xmlhandlers.lua o_O ))

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;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local type = type;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local tostring, print = tostring, print;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local _G = _G;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module "modulemanager"
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 local handler_info = {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local handlers = {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local modulehelpers = setmetatable({}, { __index = _G });
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 function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 handlers[origin_type] = handlers[origin_type] or {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 handlers[origin_type].iq = handlers[origin_type].iq or {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if not handlers[origin_type].iq[xmlns] then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 handlers[origin_type].iq[xmlns]= handler;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 handler_info[handler] = getfenv(2).module;
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
26 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 else
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
28 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
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
32 function modulehelpers.add_handler(origin_type, tag, handler)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
33 handlers[origin_type] = handlers[origin_type] or {};
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
34 if not handlers[origin_type][tag] then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
35 handlers[origin_type][tag]= handler;
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
36 handler_info[handler] = getfenv(2).module;
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
37 log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
38 elseif handler_info[handlers[origin_type][tag]] then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
39 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);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
40 end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 function loadall()
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
44 load("saslauth");
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 load("legacyauth");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 load("roster");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function load(name)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local mod, err = loadfile("plugins/mod_"..name..".lua");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if not mod then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 setfenv(mod, pluginenv);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local success, ret = pcall(mod);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not success then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function handle_stanza(origin, stanza)
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
67 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
68
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
69 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
70 log("debug", "Stanza is an <iq/>");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local child = stanza.tags[1];
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 if child then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 local xmlns = child.attr.xmlns;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 log("debug", "Stanza has xmlns: %s", xmlns);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local handler = handlers[origin_type][name][xmlns];
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if handler then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return handler(origin, stanza) or true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
82 --FIXME: All iq's must be replied to, here we should return service-unavailable I think
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
83 elseif handlers[origin_type] then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
84 local handler = handlers[origin_type][name];
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
85 if handler then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
86 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
87 return handler(origin, stanza) or true;
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
88 end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 log("debug", "Stanza unhandled by any modules");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return false; -- we didn't handle it
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end

mercurial