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