Change default maximum inactivity period to 60s from 30s
[prosody.git] / core / modulemanager.lua
1 -- Prosody IM v0.2
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 local next = next;
38 local rawget = rawget;
39
40 local tostring, print = tostring, print;
41
42 -- We need this to let modules access the real global namespace
43 local _G = _G;
44
45 module "modulemanager"
46
47 local api = {}; -- Module API container
48
49 local modulemap = { ["*"] = {} };
50
51 local stanza_handlers = multitable_new();
52 local handler_info = {};
53
54 local modulehelpers = setmetatable({}, { __index = _G });
55
56 local features_table = multitable_new();
57 local handler_table = multitable_new();
58 local NULL = {};
59
60 -- Load modules when a host is activated
61 function load_modules_for_host(host)
62         -- Load modules from global section
63         local modules_enabled = config.get("*", "core", "modules_enabled");
64         local modules_disabled = config.get(host, "core", "modules_disabled");
65         local disabled_set = {};
66         if modules_enabled then
67                 if modules_disabled then
68                         for _, module in pairs(modules_disabled) do
69                                 disabled_set[module] = true;
70                         end
71                 end
72                 for _, module in pairs(modules_enabled) do
73                         if not disabled_set[module] then
74                                 load(host, module);
75                         end
76                 end
77         end
78
79         -- Load modules from just this host
80         local modules_enabled = config.get(host, "core", "modules_enabled");
81         if modules_enabled then
82                 for _, module in pairs(modules_enabled) do
83                         if not is_loaded(host, module) then
84                                 load(host, module);
85                         end
86                 end
87         end
88 end
89 eventmanager.add_event_hook("host-activated", load_modules_for_host);
90 --
91
92 function load(host, module_name, config)
93         if not (host and module_name) then
94                 return nil, "insufficient-parameters";
95         end
96         
97         if not modulemap[host] then
98                 modulemap[host] = {};
99         elseif modulemap[host][module_name] then
100                 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
101                 return nil, "module-already-loaded";
102         elseif modulemap["*"][module_name] then
103                 return nil, "global-module-already-loaded";
104         end
105         
106
107         local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
108         if not mod then
109                 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
110                 return nil, err;
111         end
112
113         local _log = logger.init(host..":"..module_name);
114         local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
115
116         local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
117         
118         setfenv(mod, pluginenv);
119         
120         local success, ret = pcall(mod);
121         if not success then
122                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
123                 return nil, ret;
124         end
125         
126         -- Use modified host, if the module set one
127         modulemap[api_instance.host][module_name] = pluginenv;
128         
129         return true;
130 end
131
132 function is_loaded(host, name)
133         return modulemap[host] and modulemap[host][name] and true;
134 end
135
136 function unload(host, name, ...)
137         local mod = modulemap[host] and modulemap[host][name];
138         if not mod then return nil, "module-not-loaded"; end
139         
140         if type(rawget(mod, "unload")) == "function" then
141                 local ok, err = pcall(rawget(mod, "unload"), ...)
142                 if (not ok) and err then
143                         log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
144                 end
145         end
146         modulemap[host][name] = nil;
147         features_table:remove(host, name);
148         local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns}
149         for _, param in pairs(params) do
150                 local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]);
151                 handler_info[handlers[1]] = nil;
152                 stanza_handlers:remove(param[1], param[2], param[3], param[4]);
153         end
154         return true;
155 end
156
157 function handle_stanza(host, origin, stanza)
158         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
159         if name == "iq" and xmlns == "jabber:client" then
160                 if stanza.attr.type == "get" or stanza.attr.type == "set" then
161                         xmlns = stanza.tags[1].attr.xmlns;
162                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
163                 else
164                         log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type);
165                         return true;
166                 end
167         end
168         local handlers = stanza_handlers:get(host, origin_type, name, xmlns);
169         if handlers then
170                 log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name);
171                 (handlers[1])(origin, stanza);
172                 return true;
173         else
174                 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); -- we didn't handle it
175         end
176 end
177
178 ----- API functions exposed to modules -----------
179 -- Must all be in api.* 
180
181 -- Returns the name of the current module
182 function api:get_name()
183         return self.name;
184 end
185
186 -- Returns the host that the current module is serving
187 function api:get_host()
188         return self.host;
189 end
190
191 local function _add_handler(module, origin_type, tag, xmlns, handler)
192         local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns);
193         local msg = (tag == "iq") and "namespace" or "payload namespace";
194         if not handlers then
195                 stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
196                 handler_info[handler] = module;
197                 handler_table:add(module.host, module.name, {module.host, origin_type, tag, xmlns});
198                 module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns);
199         else
200                 module:log("warn", "I wanted to handle tag '%s' [%s] with %s '%s' but mod_%s already handles that", tag, origin_type, msg, xmlns, handler_info[handlers[1]].module.name);
201         end
202 end
203
204 function api:add_handler(origin_type, tag, xmlns, handler)
205         if not (origin_type and tag and xmlns and handler) then return false; end
206         if type(origin_type) == "table" then
207                 for _, origin_type in ipairs(origin_type) do
208                         _add_handler(self, origin_type, tag, xmlns, handler);
209                 end
210         else
211                 _add_handler(self, origin_type, tag, xmlns, handler);
212         end
213 end
214 function api:add_iq_handler(origin_type, xmlns, handler)
215         self:add_handler(origin_type, "iq", xmlns, handler);
216 end
217
218 addDiscoInfoHandler("*host", function(reply, to, from, node)
219         if #node == 0 then
220                 local done = {};
221                 for module, features in pairs(features_table:get(to) or NULL) do -- for each module
222                         for feature in pairs(features) do
223                                 if not done[feature] then
224                                         reply:tag("feature", {var = feature}):up(); -- TODO cache
225                                         done[feature] = true;
226                                 end
227                         end
228                 end
229                 return next(done) ~= nil;
230         end
231 end);
232
233 function api:add_feature(xmlns)
234         features_table:set(self.host, self.name, xmlns, true);
235 end
236
237 function api:add_event_hook (...) return eventmanager.add_event_hook(...); end
238
239 --------------------------------------------------------------------
240
241 return _M;