X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=core%2Fmoduleapi.lua;h=ed75669b60b64f6c90e5c19f9ee843328f20ec55;hb=b85f75e110d99b890fc22186cb2ad4d0c8f1d0a5;hp=44ae9a07e96d9bb29627c61ed7dc8c01827a650c;hpb=100a0b85cfba041ccd1b1749bde4f3022c6d636b;p=prosody.git diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 44ae9a07..ed75669b 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -7,22 +7,25 @@ -- local config = require "core.configmanager"; -local modulemanager = require "modulemanager"; +local modulemanager = require "modulemanager"; -- This is necessary to avoid require loops local array = require "util.array"; local set = require "util.set"; local logger = require "util.logger"; local pluginloader = require "util.pluginloader"; - -local multitable_new = require "util.multitable".new; +local timer = require "util.timer"; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; -local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type; +local error, setmetatable, type = error, setmetatable, type; local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack; local tonumber, tostring = tonumber, tostring; local prosody = prosody; local hosts = prosody.hosts; +-- FIXME: This assert() is to try and catch an obscure bug (2013-04-05) +local core_post_stanza = assert(prosody.core_post_stanza, + "prosody.core_post_stanza is nil, please report this as a bug"); + -- Registry of shared module data local shared_data = setmetatable({}, { __mode = "v" }); @@ -41,7 +44,7 @@ function api:get_host() end function api:get_host_type() - return hosts[self.host].type; + return self.host ~= "*" and hosts[self.host].type or nil; end function api:set_global() @@ -50,6 +53,7 @@ function api:set_global() local _log = logger.init("mod_"..self.name); self.log = function (self, ...) return _log(...); end; self._log = _log; + self.global = true; end function api:add_feature(xmlns) @@ -61,16 +65,34 @@ end function api:add_extension(data) self:add_item("extension", data); end +function api:has_feature(xmlns) + for _, feature in ipairs(self:get_host_items("feature")) do + if feature == xmlns then return true; end + end + return false; +end +function api:has_identity(category, type, name) + for _, id in ipairs(self:get_host_items("identity")) do + if id.category == category and id.type == type and id.name == name then + return true; + end + end + return false; +end function api:fire_event(...) return (hosts[self.host] or prosody).events.fire_event(...); end function api:hook_object_event(object, event, handler, priority) - self.event_handlers[handler] = { name = event, priority = priority, object = object }; + self.event_handlers:set(object, event, handler, true); return object.add_handler(event, handler, priority); end +function api:unhook_object_event(object, event, handler) + return object.remove_handler(event, handler); +end + function api:hook(event, handler, priority) return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority); end @@ -79,7 +101,7 @@ function api:hook_global(event, handler, priority) return self:hook_object_event(prosody.events, event, handler, priority); end -function api:hook_stanza(xmlns, name, handler, priority) +function api:hook_tag(xmlns, name, handler, priority) if not handler and type(name) == "function" then -- If only 2 options then they specified no xmlns xmlns, name, handler, priority = nil, xmlns, name, handler; @@ -89,14 +111,14 @@ function api:hook_stanza(xmlns, name, handler, priority) end return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority); end +api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9 function api:require(lib) - local f, n = pluginloader.load_code(self.name, lib..".lib.lua"); + local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment); if not f then - f, n = pluginloader.load_code(lib, lib..".lib.lua"); + f, n = pluginloader.load_code(lib, lib..".lib.lua", self.environment); end if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message - setfenv(f, self.environment); return f(); end @@ -104,7 +126,7 @@ function api:depends(name) if not self.dependencies then self.dependencies = {}; self:hook("module-reloaded", function (event) - if self.dependencies[event.module] then + if self.dependencies[event.module] and not self.reloading then self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module); modulemanager.reload(self.host, self.name); return; @@ -118,6 +140,10 @@ function api:depends(name) end); end local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name); + if mod and mod.module.host == "*" and self.host ~= "*" + and modulemanager.module_has_method(mod, "add_host") then + mod = nil; -- Target is a shared module, so we still want to load it on our host + end if not mod then local err; mod, err = modulemanager.load(self.host, name); @@ -129,13 +155,38 @@ function api:depends(name) return mod; end +-- Returns one or more shared tables at the specified virtual paths +-- Intentionally does not allow the table at a path to be _set_, it +-- is auto-created if it does not exist. +function api:shared(...) + if not self.shared_data then self.shared_data = {}; end + local paths = { n = select("#", ...), ... }; + local data_array = {}; + local default_path_components = { self.host, self.name }; + for i = 1, paths.n do + local path = paths[i]; + if path:sub(1,1) ~= "/" then -- Prepend default components + local n_components = select(2, path:gsub("/", "%1")); + path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path; + end + local shared = shared_data[path]; + if not shared then + shared = {}; + if path:match("%-cache$") then + setmetatable(shared, { __mode = "kv" }); + end + shared_data[path] = shared; + end + t_insert(data_array, shared); + self.shared_data[path] = shared; + end + return unpack(data_array); +end + function api:get_option(name, default_value) - local value = config.get(self.host, self.name, name); + local value = config.get(self.host, name); if value == nil then - value = config.get(self.host, "core", name); - if value == nil then - value = default_value; - end + value = default_value; end return value; end @@ -219,7 +270,22 @@ function api:get_option_set(name, ...) return set.new(value); end -local module_items = multitable_new(); +function api:get_option_inherited_set(name, ...) + local value = self:get_option_set(name, ...); + local global_value = self:context("*"):get_option_set(name, ...); + if not value then + return global_value; + elseif not global_value then + return value; + end + value:include(global_value); + return value; +end + +function api:context(host) + return setmetatable({host=host or "*"}, {__index=self,__newindex=self}); +end + function api:add_item(key, value) self.items = self.items or {}; self.items[key] = self.items[key] or {}; @@ -238,23 +304,7 @@ function api:remove_item(key, value) end function api:get_host_items(key) - local result = {}; - for mod_name, module in pairs(modulemanager.get_modules(self.host)) do - module = module.module; - if module.items then - for _, item in ipairs(module.items[key] or NULL) do - t_insert(result, item); - end - end - end - for mod_name, module in pairs(modulemanager.get_modules("*")) do - module = module.module; - if module.items then - for _, item in ipairs(module.items[key] or NULL) do - t_insert(result, item); - end - end - end + local result = modulemanager.get_items(key, self.host) or {}; return result; end @@ -268,4 +318,50 @@ function api:handle_items(type, added_cb, removed_cb, existing) end end +function api:provides(name, item) + -- if not item then item = setmetatable({}, { __index = function(t,k) return rawget(self.environment, k); end }); end + if not item then + item = {} + for k,v in pairs(self.environment) do + if k ~= "module" then item[k] = v; end + end + end + if not item.name then + local item_name = self.name; + -- Strip a provider prefix to find the item name + -- (e.g. "auth_foo" -> "foo" for an auth provider) + if item_name:find(name.."_", 1, true) == 1 then + item_name = item_name:sub(#name+2); + end + item.name = item_name; + end + item._provided_by = self.name; + self:add_item(name.."-provider", item); +end + +function api:send(stanza) + return core_post_stanza(hosts[self.host], stanza); +end + +function api:add_timer(delay, callback) + return timer.add_task(delay, function (t) + if self.loaded == false then return; end + return callback(t); + end); +end + +local path_sep = package.config:sub(1,1); +function api:get_directory() + return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; +end + +function api:load_resource(path, mode) + path = config.resolve_relative_path(self:get_directory(), path); + return io.open(path, mode); +end + +function api:open_store(name, type) + return storagemanager.open(self.host, name or self.name, type); +end + return api;