Forced merge.
[prosody.git] / core / modulemanager.lua
1
2
3 local logger = require "util.logger";
4 local log = logger.init("modulemanager")
5
6 local loadfile, pcall = loadfile, pcall;
7 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
8 local pairs, ipairs = pairs, ipairs;
9 local t_insert = table.insert;
10 local type = type;
11
12 local tostring, print = tostring, print;
13
14 local _G = _G;
15 local debug = debug;
16
17 module "modulemanager"
18
19 local api = {}; -- Module API container
20
21 local modulemap = {};
22
23 local handler_info = {};
24 local stanza_handlers = {};
25
26 local modulehelpers = setmetatable({}, { __index = _G });
27
28
29 function load(host, module_name, config)
30         if not (host and module_name) then
31                 return nil, "insufficient-parameters";
32         end
33         local mod, err = loadfile("plugins/mod_"..module_name..".lua");
34         if not mod then
35                 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
36                 return nil, err;
37         end
38         
39         if not modulemap[host] then
40                 modulemap[host] = {};
41                 stanza_handlers[host] = {};
42         elseif modulemap[host][module_name] then
43                 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
44                 return nil, "module-already-loaded";
45         end
46         
47         local _log = logger.init(host..":"..module_name);
48         local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
49
50         local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
51         
52         setfenv(mod, pluginenv);
53         
54         local success, ret = pcall(mod);
55         if not success then
56                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
57                 return nil, ret;
58         end
59         
60         modulemap[host][module_name] = mod;
61         
62         return true;
63 end
64
65 function is_loaded(host, name)
66         return modulemap[host] and modulemap[host][name] and true;
67 end
68
69 function unload(host, name, ...)
70         local mod = modulemap[host] and modulemap[host][name];
71         if not mod then return nil, "module-not-loaded"; end
72         
73         if type(mod.unload) == "function" then
74                 local ok, err = pcall(mod.unload, ...)
75                 if (not ok) and err then
76                         log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
77                 end
78         end
79         
80 end
81
82 function handle_stanza(host, origin, stanza)
83         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
84         
85         local handlers = stanza_handlers[host];
86         if not handlers then
87                 log("warn", "No handlers for %s", host);
88                 return false;
89         end
90         
91         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
92                 local child = stanza.tags[1];
93                 if child then
94                         local xmlns = child.attr.xmlns or xmlns;
95                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
96                         local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
97                         if handler then
98                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
99                                 return handler(origin, stanza) or true;
100                         end
101                 end
102         elseif handlers[origin_type] then
103                 local handler = handlers[origin_type][name];
104                 if  handler then
105                         handler = handler[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                 end
111         end
112         log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
113         return false; -- we didn't handle it
114 end
115
116 ----- API functions exposed to modules -----------
117 -- Must all be in api.* 
118
119 -- Returns the name of the current module
120 function api:get_name()
121         return self.name;
122 end
123
124 -- Returns the host that the current module is serving
125 function api:get_host()
126         return self.host;
127 end
128
129
130 local function _add_iq_handler(module, origin_type, xmlns, handler)
131         local handlers = stanza_handlers[module.host];
132         handlers[origin_type] = handlers[origin_type] or {};
133         handlers[origin_type].iq = handlers[origin_type].iq or {};
134         if not handlers[origin_type].iq[xmlns] then
135                 handlers[origin_type].iq[xmlns]= handler;
136                 handler_info[handler] = module;
137                 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
138         else
139                 module:log("warn", "I wanted to handle tag 'iq' [%s] with payload namespace '%s' but mod_%s already handles that", origin_type, xmlns, handler_info[handlers[origin_type].iq[xmlns]].name);
140         end
141 end
142
143 function api:add_iq_handler(origin_type, xmlns, handler)
144         if not (origin_type and handler and xmlns) then return false; end
145         if type(origin_type) == "table" then
146                 for _, origin_type in ipairs(origin_type) do
147                         _add_iq_handler(self, origin_type, xmlns, handler);
148                 end
149                 return;
150         end
151         _add_iq_handler(self, origin_type, xmlns, handler);
152 end
153
154
155 do
156         local event_handlers = {};
157         
158         function api:add_event_hook(name, handler)
159                 if not event_handlers[name] then
160                         event_handlers[name] = {};
161                 end
162                 t_insert(event_handlers[name] , handler);
163                 self:log("debug", "Subscribed to %s", name);
164         end
165         
166         function fire_event(name, ...)
167                 local event_handlers = event_handlers[name];
168                 if event_handlers then
169                         for name, handler in ipairs(event_handlers) do
170                                 handler(...);
171                         end
172                 end
173         end
174 end
175
176
177 local function _add_handler(module, origin_type, tag, xmlns, handler)
178         local handlers = stanza_handlers[module.host];
179         handlers[origin_type] = handlers[origin_type] or {};
180         if not handlers[origin_type][tag] then
181                 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
182                 handlers[origin_type][tag][xmlns]= handler;
183                 handler_info[handler] = module;
184                 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
185         elseif handler_info[handlers[origin_type][tag]] then
186                 log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[origin_type][tag]].module.name);
187         end
188 end
189
190 function api:add_handler(origin_type, tag, xmlns, handler)
191         if not (origin_type and tag and xmlns and handler) then return false; end
192         if type(origin_type) == "table" then
193                 for _, origin_type in ipairs(origin_type) do
194                         _add_handler(self, origin_type, tag, xmlns, handler);
195                 end
196                 return;
197         end
198         _add_handler(self, origin_type, tag, xmlns, handler);
199 end
200
201 --------------------------------------------------------------------
202
203 return _M;