Merge 0.6->0.7
[prosody.git] / core / modulemanager.lua
index d8418d832df9cf5008a6cddc41fc98cb80eafcac..8e62aecb8231ee8e6e50b8f2a071b4197b44b33d 100644 (file)
@@ -1,6 +1,6 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -19,7 +19,7 @@ local pluginloader = require "util.pluginloader";
 local hosts = hosts;
 local prosody = prosody;
 
-local loadfile, pcall = loadfile, pcall;
+local loadfile, pcall, xpcall = loadfile, pcall, xpcall;
 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
 local pairs, ipairs = pairs, ipairs;
 local t_insert, t_concat = table.insert, table.concat;
@@ -29,6 +29,14 @@ local rawget = rawget;
 local error = error;
 local tostring, tonumber = tostring, tonumber;
 
+local debug_traceback = debug.traceback;
+local unpack, select = unpack, select;
+pcall = function(f, ...)
+       local n = select("#", ...);
+       local params = {...};
+       return xpcall(function() f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
+end
+
 local array, set = require "util.array", require "util.set";
 
 local autoload_modules = {"presence", "message", "iq"};
@@ -127,6 +135,7 @@ function load(host, module_name, config)
        local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
 
        local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
+       api_instance.environment = pluginenv;
        
        setfenv(mod, pluginenv);
        if not hosts[host] then
@@ -157,7 +166,7 @@ function load(host, module_name, config)
                log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
        end
        if success then
-               hosts[host].events.fire_event("module-loaded", { module = module_name, host = host });
+               (hosts[api_instance.host] or prosody).events.fire_event("module-loaded", { module = module_name, host = host });
                return true;
        else -- load failed, unloading
                unload(api_instance.host, module_name);
@@ -209,7 +218,7 @@ function unload(host, name, ...)
                end
        end
        modulemap[host][name] = nil;
-       hosts[host].events.fire_event("module-unloaded", { module = name, host = host });
+       (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
        return true;
 end
 
@@ -273,7 +282,7 @@ function handle_stanza(host, origin, stanza)
                (handlers[1])(origin, stanza);
                return true;
        else
-               if stanza.attr.xmlns == "jabber:client" then
+               if stanza.attr.xmlns == nil then
                        log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
                        if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
                                origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
@@ -397,7 +406,7 @@ function api:require(lib)
                f, n = pluginloader.load_code(lib, lib..".lib.lua");
        end
        if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
-       setfenv(f, setmetatable({ module = self }, { __index = _G }));
+       setfenv(f, self.environment);
        return f();
 end