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