X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=core%2Fmodulemanager.lua;h=8e62aecb8231ee8e6e50b8f2a071b4197b44b33d;hb=02db59e68c9b69b848102d97db373ca2a92bdc40;hp=465b9dd8cd3dc2eff5067446fc90e595220bcaa6;hpb=863f00a39e9e9a50569b3390c5bc16f27c4e88ea;p=prosody.git diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 465b9dd8..8e62aecb 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -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. @@ -13,14 +13,13 @@ local log = logger.init("modulemanager"); local eventmanager = require "core.eventmanager"; local config = require "core.configmanager"; local multitable_new = require "util.multitable".new; -local register_actions = require "core.actions".register; local st = require "util.stanza"; 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; @@ -28,7 +27,17 @@ local type = type; local next = next; local rawget = rawget; local error = error; -local tostring = tostring; +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"}; @@ -56,22 +65,27 @@ local NULL = {}; -- Load modules when a host is activated function load_modules_for_host(host) + local disabled_set = {}; + local modules_disabled = config.get(host, "core", "modules_disabled"); + if modules_disabled then + for _, module in ipairs(modules_disabled) do + disabled_set[module] = true; + end + end + + -- Load auto-loaded modules for this host + if hosts[host].type == "local" then + for _, module in ipairs(autoload_modules) do + if not disabled_set[module] then + load(host, module); + end + end + end + + -- Load modules from global section if config.get(host, "core", "load_global_modules") ~= false then - -- Load modules from global section local modules_enabled = config.get("*", "core", "modules_enabled"); - local modules_disabled = config.get(host, "core", "modules_disabled"); - local disabled_set = {}; if modules_enabled then - if modules_disabled then - for _, module in ipairs(modules_disabled) do - disabled_set[module] = true; - end - end - for _, module in ipairs(autoload_modules) do - if not disabled_set[module] then - load(host, module); - end - end for _, module in ipairs(modules_enabled) do if not disabled_set[module] and not is_loaded(host, module) then load(host, module); @@ -101,7 +115,6 @@ function load(host, module_name, config) if not modulemap[host] then modulemap[host] = {}; - hosts[host].modules = modulemap[host]; end if modulemap[host][module_name] then @@ -122,31 +135,43 @@ 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 hosts[host] = { type = "component", host = host, connected = false, s2sout = {} }; end - - local success, ret = pcall(mod); - if not success then - log("error", "Error initialising module '%s': %s", module_name or "nil", ret or "nil"); - return nil, ret; + if not hosts[host] then + local create_component = _G.require "core.componentmanager".create_component; + hosts[host] = create_component(host); + hosts[host].connected = false; + log("debug", "Created new component: %s", host); end + hosts[host].modules = modulemap[host]; + modulemap[host][module_name] = pluginenv; - if module_has_method(pluginenv, "load") then - local ok, err = call_module_method(pluginenv, "load"); - if (not ok) and err then - log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err); + local success, err = pcall(mod); + if success then + if module_has_method(pluginenv, "load") then + success, err = call_module_method(pluginenv, "load"); + if not success then + log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil"); + end end - end - -- Use modified host, if the module set one - modulemap[api_instance.host][module_name] = pluginenv; - - if api_instance.host == "*" and host ~= "*" then - api_instance:set_global(); + -- Use modified host, if the module set one + if api_instance.host == "*" and host ~= "*" then + modulemap[host][module_name] = nil; + modulemap["*"][module_name] = pluginenv; + api_instance:set_global(); + end + else + log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil"); + end + if success then + (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); + return nil, err; end - - return true; end function get_module(host, name) @@ -158,7 +183,7 @@ function is_loaded(host, name) end function unload(host, name, ...) - local mod = get_module(host, name); + local mod = get_module(host, name); if not mod then return nil, "module-not-loaded"; end if module_has_method(mod, "unload") then @@ -167,7 +192,6 @@ function unload(host, name, ...) log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); end end - modulemap[host][name] = nil; local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns} for _, param in pairs(params or NULL) do local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]); @@ -184,6 +208,17 @@ function unload(host, name, ...) end end hooks:remove(host, name); + if mod.module.items then -- remove items + for key,t in pairs(mod.module.items) do + for i = #t,1,-1 do + local value = t[i]; + t[i] = nil; + hosts[host].events.fire_event("item-removed/"..key, {source = self, item = value}); + end + end + end + modulemap[host][name] = nil; + (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host }); return true; end @@ -247,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")); @@ -264,7 +299,7 @@ function module_has_method(module, method) end function call_module_method(module, method, ...) - if module_has_method(module, method) then + if module_has_method(module, method) then local f = module.module[method]; return pcall(f, ...); else @@ -273,7 +308,7 @@ function call_module_method(module, method, ...) end ----- API functions exposed to modules ----------- --- Must all be in api.* +-- Must all be in api.* -- Returns the name of the current module function api:get_name() @@ -371,12 +406,98 @@ 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 function api:get_option(name, default_value) - return config.get(self.host, self.name, name) or config.get(self.host, "core", name) or default_value; + local value = config.get(self.host, self.name, name); + if value == nil then + value = config.get(self.host, "core", name); + if value == nil then + value = default_value; + end + end + return value; +end + +function api:get_option_string(name, default_value) + local value = self:get_option(name, default_value); + if type(value) == "table" then + if #value > 1 then + self:log("error", "Config option '%s' does not take a list, using just the first item", name); + end + value = value[1]; + end + if value == nil then + return nil; + end + return tostring(value); +end + +function api:get_option_number(name, ...) + local value = self:get_option(name, ...); + if type(value) == "table" then + if #value > 1 then + self:log("error", "Config option '%s' does not take a list, using just the first item", name); + end + value = value[1]; + end + local ret = tonumber(value); + if value ~= nil and ret == nil then + self:log("error", "Config option '%s' not understood, expecting a number", name); + end + return ret; +end + +function api:get_option_boolean(name, ...) + local value = self:get_option(name, ...); + if type(value) == "table" then + if #value > 1 then + self:log("error", "Config option '%s' does not take a list, using just the first item", name); + end + value = value[1]; + end + if value == nil then + return nil; + end + local ret = value == true or value == "true" or value == 1 or nil; + if ret == nil then + ret = (value == false or value == "false" or value == 0); + if ret then + ret = false; + else + ret = nil; + end + end + if ret == nil then + self:log("error", "Config option '%s' not understood, expecting true/false", name); + end + return ret; +end + +function api:get_option_array(name, ...) + local value = self:get_option(name, ...); + + if value == nil then + return nil; + end + + if type(value) ~= "table" then + return array{ value }; -- Assume any non-list is a single-item list + end + + return array():append(value); -- Clone +end + +function api:get_option_set(name, ...) + local value = self:get_option_array(name, ...); + + if value == nil then + return nil; + end + + return set.new(value); end local t_remove = _G.table.remove; @@ -419,19 +540,4 @@ function api:get_host_items(key) return result; end --------------------------------------------------------------------- - -local actions = {}; - -function actions.load(params) - --return true, "Module loaded ("..params.module.." on "..params.host..")"; - return load(params.host, params.module); -end - -function actions.unload(params) - return unload(params.host, params.module); -end - -register_actions("/modules", actions); - return _M;