Fix for previous commit (again)
[prosody.git] / core / modulemanager.lua
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;
7 local t_insert = table.insert;
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 local function _add_iq_handler(module, 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] = module;
27                 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", module.name, xmlns);
28         else
29                 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         end
31 end
32
33 function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
34         if not (origin_type and handler and xmlns) then return false; end
35         if type(origin_type) == "table" then
36                 for _, origin_type in ipairs(origin_type) do
37                         _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
38                 end
39                 return;
40         end
41         _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
42 end
43
44 local function _add_handler(module, origin_type, tag, xmlns, handler)
45         handlers[origin_type] = handlers[origin_type] or {};
46         if not handlers[origin_type][tag] then
47                 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
48                 handlers[origin_type][tag][xmlns]= handler;
49                 handler_info[handler] = module;
50                 log("debug", "mod_%s now handles tag '%s'", module.name, tag);
51         elseif handler_info[handlers[origin_type][tag]] then
52                 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);
53         end
54 end
55
56 function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
57         if not (origin_type and tag and xmlns and handler) then return false; end
58         if type(origin_type) == "table" then
59                 for _, origin_type in ipairs(origin_type) do
60                         _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
61                 end
62                 return;
63         end
64         _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
65 end
66
67 function loadall()
68         load("saslauth");
69         load("legacyauth");
70         load("roster");
71         load("register");
72         load("tls");
73         load("vcard");
74         load("private");
75         load("version");
76         load("dialback");
77 end
78
79 function load(name)
80         local mod, err = loadfile("plugins/mod_"..name..".lua");
81         if not mod then
82                 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
83                 return;
84         end
85         
86         local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
87         
88         setfenv(mod, pluginenv);
89         local success, ret = pcall(mod);
90         if not success then
91                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
92                 return;
93         end
94 end
95
96 function handle_stanza(origin, stanza)
97         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
98         
99         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
100                 log("debug", "Stanza is an <iq/>");
101                 local child = stanza.tags[1];
102                 if child then
103                         local xmlns = child.attr.xmlns;
104                         log("debug", "Stanza has xmlns: %s", xmlns);
105                         local handler = handlers[origin_type][name][xmlns];
106                         if  handler then
107                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
108                                 return handler(origin, stanza) or true;
109                         end
110
111                 end
112         elseif handlers[origin_type] then
113                 local handler = handlers[origin_type][name];
114                 if  handler then
115                         handler = handler[xmlns];
116                         if handler then
117                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
118                                 return handler(origin, stanza) or true;
119                         end
120                 end
121         end
122         log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
123         return false; -- we didn't handle it
124 end
125
126 do
127         local event_handlers = {};
128         
129         function modulehelpers.add_event_hook(name, handler)
130                 if not event_handlers[name] then
131                         event_handlers[name] = {};
132                 end
133                 t_insert(event_handlers[name] , handler);
134         end
135         
136         function fire_event(name, ...)
137                 local event_handlers = event_handlers[name];
138                 if event_handlers then
139                         for name, handler in ipairs(event_handlers) do
140                                 handler(...);
141                         end
142                 end
143         end
144 end
145
146 return _M;