Remove some debugging from pposix.c
[prosody.git] / core / modulemanager.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
23
24 local logger = require "util.logger";
25 local log = logger.init("modulemanager");
26 local addDiscoInfoHandler = require "core.discomanager".addDiscoInfoHandler;
27 local eventmanager = require "core.eventmanager";
28 local config = require "core.configmanager";
29 local multitable_new = require "util.multitable".new;
30
31
32 local loadfile, pcall = loadfile, pcall;
33 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
34 local pairs, ipairs = pairs, ipairs;
35 local t_insert = table.insert;
36 local type = type;
37
38 local tostring, print = tostring, print;
39
40 -- We need this to let modules access the real global namespace
41 local _G = _G;
42
43 module "modulemanager"
44
45 local api = {}; -- Module API container
46
47 local modulemap = { ["*"] = {} };
48
49 local m_handler_info = multitable_new();
50 local m_stanza_handlers = multitable_new();
51 local handler_info = {};
52 local stanza_handlers = {};
53
54 local modulehelpers = setmetatable({}, { __index = _G });
55
56 -- Load modules when a host is activated
57 function load_modules_for_host(host)
58         local modules_enabled = config.get(host, "core", "modules_enabled");
59         if modules_enabled then
60                 for _, module in pairs(modules_enabled) do
61                         load(host, module);
62                 end
63         end
64 end
65 eventmanager.add_event_hook("host-activated", load_modules_for_host);
66 --
67
68 function load(host, module_name, config)
69         if not (host and module_name) then
70                 return nil, "insufficient-parameters";
71         end
72         
73         if not modulemap[host] then
74                 modulemap[host] = {};
75                 stanza_handlers[host] = {};
76         elseif modulemap[host][module_name] then
77                 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
78                 return nil, "module-already-loaded";
79         elseif modulemap["*"][module_name] then
80                 return nil, "global-module-already-loaded";
81         end
82         
83
84         local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
85         if not mod then
86                 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
87                 return nil, err;
88         end
89
90         local _log = logger.init(host..":"..module_name);
91         local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
92
93         local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
94         
95         setfenv(mod, pluginenv);
96         
97         local success, ret = pcall(mod);
98         if not success then
99                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
100                 return nil, ret;
101         end
102         
103         -- Use modified host, if the module set one
104         modulemap[api_instance.host][module_name] = mod;
105         
106         return true;
107 end
108
109 function is_loaded(host, name)
110         return modulemap[host] and modulemap[host][name] and true;
111 end
112
113 function unload(host, name, ...)
114         local mod = modulemap[host] and modulemap[host][name];
115         if not mod then return nil, "module-not-loaded"; end
116         
117         if type(mod.unload) == "function" then
118                 local ok, err = pcall(mod.unload, ...)
119                 if (not ok) and err then
120                         log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
121                 end
122         end
123         
124 end
125
126 function _handle_stanza(host, origin, stanza)
127         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
128         
129         local handlers = stanza_handlers[host];
130         if not handlers then
131                 log("warn", "No handlers for %s", host);
132                 return false;
133         end
134         
135         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
136                 local child = stanza.tags[1];
137                 if child then
138                         local xmlns = child.attr.xmlns or xmlns;
139                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
140                         local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
141                         if handler then
142                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
143                                 return handler(origin, stanza) or true;
144                         end
145                 end
146         elseif handlers[origin_type] then
147                 local handler = handlers[origin_type][name];
148                 if  handler then
149                         handler = handler[xmlns];
150                         if handler then
151                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
152                                 return handler(origin, stanza) or true;
153                         end
154                 end
155         end
156         log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
157         return false; -- we didn't handle it
158 end
159 function handle_stanza(host, origin, stanza)
160         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
161         if name == "iq" and xmlns == "jabber:client" then
162                 xmlns = stanza.tags[1].attr.xmlns;
163                 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
164         end
165         local handlers = m_stanza_handlers:get(host, origin_type, name, xmlns);
166         if handlers then
167                 log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name);
168                 (handlers[1])(origin, stanza);
169                 return true;
170         else
171                 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); -- we didn't handle it
172         end
173 end
174
175 ----- API functions exposed to modules -----------
176 -- Must all be in api.* 
177
178 -- Returns the name of the current module
179 function api:get_name()
180         return self.name;
181 end
182
183 -- Returns the host that the current module is serving
184 function api:get_host()
185         return self.host;
186 end
187
188
189 local function __add_iq_handler(module, origin_type, xmlns, handler)
190         local handlers = stanza_handlers[module.host];
191         handlers[origin_type] = handlers[origin_type] or {};
192         handlers[origin_type].iq = handlers[origin_type].iq or {};
193         if not handlers[origin_type].iq[xmlns] then
194                 handlers[origin_type].iq[xmlns]= handler;
195                 handler_info[handler] = module;
196                 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
197         else
198                 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);
199         end
200 end
201 local function _add_iq_handler(module, origin_type, xmlns, handler)
202         local handlers = m_stanza_handlers:get(module.host, origin_type, "iq", xmlns);
203         if not handlers then
204                 m_stanza_handlers:add(module.host, origin_type, "iq", xmlns, handler);
205                 handler_info[handler] = module;
206                 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
207         else
208                 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[1]].name);
209         end
210 end
211
212 function api:add_iq_handler(origin_type, xmlns, handler)
213         if not (origin_type and handler and xmlns) then return false; end
214         if type(origin_type) == "table" then
215                 for _, origin_type in ipairs(origin_type) do
216                         _add_iq_handler(self, origin_type, xmlns, handler);
217                 end
218                 return;
219         end
220         _add_iq_handler(self, origin_type, xmlns, handler);
221 end
222
223 function api:add_feature(xmlns)
224         addDiscoInfoHandler(self.host, function(reply, to, from, node)
225                 if #node == 0 then
226                         reply:tag("feature", {var = xmlns}):up();
227                         return true;
228                 end
229         end);
230 end
231
232 function api:add_event_hook (...) return eventmanager.add_event_hook(...); end
233
234 local function __add_handler(module, origin_type, tag, xmlns, handler)
235         local handlers = stanza_handlers[module.host];
236         handlers[origin_type] = handlers[origin_type] or {};
237         if not handlers[origin_type][tag] then
238                 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
239                 handlers[origin_type][tag][xmlns]= handler;
240                 handler_info[handler] = module;
241                 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
242         elseif handler_info[handlers[origin_type][tag]] then
243                 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);
244         end
245 end
246 local function _add_handler(module, origin_type, tag, xmlns, handler)
247         local handlers = m_stanza_handlers:get(module.host, origin_type, tag, xmlns);
248         if not handlers then
249                 m_stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
250                 handler_info[handler] = module;
251                 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
252         else
253                 module:log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[1]].module.name);
254         end
255 end
256
257 function api:add_handler(origin_type, tag, xmlns, handler)
258         if not (origin_type and tag and xmlns and handler) then return false; end
259         if type(origin_type) == "table" then
260                 for _, origin_type in ipairs(origin_type) do
261                         _add_handler(self, origin_type, tag, xmlns, handler);
262                 end
263                 return;
264         end
265         _add_handler(self, origin_type, tag, xmlns, handler);
266 end
267
268 --------------------------------------------------------------------
269
270 return _M;