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