From 36542853efea8ca4a4767996408e46b1657ec93e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jan 2012 18:41:55 +0000 Subject: modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay) --- core/moduleapi.lua | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 core/moduleapi.lua (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua new file mode 100644 index 00000000..ee960709 --- /dev/null +++ b/core/moduleapi.lua @@ -0,0 +1,239 @@ +-- Prosody IM +-- Copyright (C) 2008-2012 Matthew Wild +-- Copyright (C) 2008-2012 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +local config = require "core.configmanager"; +local modulemanager = require "modulemanager"; +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 t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; +local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type; +local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack; +local tonumber, tostring = tonumber, tostring; + +local prosody = prosody; +local hosts = prosody.hosts; + +-- Registry of shared module data +local shared_data = setmetatable({}, { __mode = "v" }); + +local NULL = {}; + +local api = {}; + +-- Returns the name of the current module +function api:get_name() + return self.name; +end + +-- Returns the host that the current module is serving +function api:get_host() + return self.host; +end + +function api:get_host_type() + return hosts[self.host].type; +end + +function api:set_global() + self.host = "*"; + -- Update the logger + local _log = logger.init("mod_"..self.name); + self.log = function (self, ...) return _log(...); end; + self._log = _log; +end + +function api:add_feature(xmlns) + self:add_item("feature", xmlns); +end +function api:add_identity(category, type, name) + self:add_item("identity", {category = category, type = type, name = name}); +end +function api:add_extension(data) + self:add_item("extension", data); +end + +function api:fire_event(...) + return (hosts[self.host] or prosody).events.fire_event(...); +end + +function api:hook(event, handler, priority) + hooks:set(self.host, self.name, event, handler, true); + (hosts[self.host] or prosody).events.add_handler(event, handler, priority); +end + +function api:hook_global(event, handler, priority) + hooks:set("*", self.name, event, handler, true); + prosody.events.add_handler(event, handler, priority); +end + +function api:hook_stanza(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; + elseif not (handler and name) then + self:log("warn", "Error: Insufficient parameters to module:hook_stanza()"); + return; + end + return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority); +end + +function api:require(lib) + local f, n = pluginloader.load_code(self.name, lib..".lib.lua"); + if not f then + 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, self.environment); + return f(); +end + +function api:get_option(name, 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 module_items = multitable_new(); +function api:add_item(key, value) + self.items = self.items or {}; + self.items[key] = self.items[key] or {}; + t_insert(self.items[key], value); + self:fire_event("item-added/"..key, {source = self, item = value}); +end +function api:remove_item(key, value) + local t = self.items and self.items[key] or NULL; + for i = #t,1,-1 do + if t[i] == value then + t_remove(self.items[key], i); + self:fire_event("item-removed/"..key, {source = self, item = value}); + return value; + end + end +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 + return result; +end + +function api:handle_items(type, added_cb, removed_cb, existing) + self:hook("item-added/"..type, added_cb); + self:hook("item-removed/"..type, removed_cb); + if existing ~= false then + for _, item in ipairs(self:get_host_items(type)) do + added_cb({ item = item }); + end + end +end + +return api; -- cgit v1.2.3 From 15a073672027cdf47822ad280e00185970ba0bdd Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jan 2012 18:49:11 +0000 Subject: modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object. --- core/moduleapi.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index ee960709..3a28ec62 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -66,14 +66,17 @@ 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 }; + return object.add_handler(event, handler, priority); +end + function api:hook(event, handler, priority) - hooks:set(self.host, self.name, event, handler, true); - (hosts[self.host] or prosody).events.add_handler(event, handler, priority); + return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority); end function api:hook_global(event, handler, priority) - hooks:set("*", self.name, event, handler, true); - prosody.events.add_handler(event, handler, priority); + return self:hook_object_event(prosody.events, event, handler, priority); end function api:hook_stanza(xmlns, name, handler, priority) -- cgit v1.2.3 From 2f397255c26a57c36102ece3883c3765870c0bf9 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jan 2012 19:35:50 +0000 Subject: moduleapi: Add module:depends(), a way to safely depend upon another module at runtime --- core/moduleapi.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 3a28ec62..44ae9a07 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -100,6 +100,35 @@ function api:require(lib) return f(); end +function api:depends(name) + if not self.dependencies then + self.dependencies = {}; + self:hook("module-reloaded", function (event) + if self.dependencies[event.module] then + self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module); + modulemanager.reload(self.host, self.name); + return; + end + end); + self:hook("module-unloaded", function (event) + if self.dependencies[event.module] then + self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module); + modulemanager.unload(self.host, self.name); + end + end); + end + local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name); + if not mod then + local err; + mod, err = modulemanager.load(self.host, name); + if not mod then + return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) )); + end + end + self.dependencies[name] = true; + return mod; +end + function api:get_option(name, default_value) local value = config.get(self.host, self.name, name); if value == nil then -- cgit v1.2.3 From 7c5700ac5402bf47a6c55d55a7f1fa1a8b8cd0d0 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jan 2012 19:48:53 +0000 Subject: moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules --- core/moduleapi.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 44ae9a07..7a4d1fa6 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -129,6 +129,29 @@ 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(...) + 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 = {}; + shared_data[path] = shared; + end + t_insert(data_array, shared); + end + return unpack(data_array); +end + function api:get_option(name, default_value) local value = config.get(self.host, self.name, name); if value == nil then -- cgit v1.2.3 From 2c3103e84ae364bc20782575a8113c9b96c1cbd7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 14 Mar 2012 21:33:15 +0000 Subject: moduleapi: Set module.global = true when module:set_global() is called --- core/moduleapi.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 7a4d1fa6..b11fd7b3 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -50,6 +50,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) -- cgit v1.2.3 From 775261ef27a572da6e1c1ccacf79f59f6ead58ef Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 15 Mar 2012 02:52:31 +0000 Subject: moduleapi: Add module:provides(), a shortcut to add an item with the current module's name --- core/moduleapi.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index b11fd7b3..2177378f 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -292,4 +292,18 @@ function api:handle_items(type, added_cb, removed_cb, existing) end end +function api:provides(name, item) + if not item then item = self.environment; end + if not item.name then + local item_name = module.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 + self:add_item(name, item); +end + return api; -- cgit v1.2.3 From 78cd9a0ddcb2e3af03173eeceec270ef804603ec Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 15 Mar 2012 02:53:05 +0000 Subject: moduleapi: Add module:send() as an alias for core_post_stanza() from the current host's origin --- core/moduleapi.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 2177378f..a577c07a 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -22,6 +22,7 @@ local tonumber, tostring = tonumber, tostring; local prosody = prosody; local hosts = prosody.hosts; +local core_post_stanza = prosody.core_post_stanza; -- Registry of shared module data local shared_data = setmetatable({}, { __mode = "v" }); @@ -306,4 +307,8 @@ function api:provides(name, item) self:add_item(name, item); end +function api:send(stanza) + return core_post_stanza(hosts[self.host], stanza); +end + return api; -- cgit v1.2.3 From 2d053569057683dbba3f960522d760cee3a1aae3 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 21 Apr 2012 22:52:15 +0100 Subject: moduleapi: Have modules internally store a reference to shared tables they use, to ensure they don't get collected while any module that had access to that table is still loaded (thanks Zash) --- core/moduleapi.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index a577c07a..7a3bd1c8 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -135,6 +135,7 @@ end -- 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 }; @@ -150,6 +151,7 @@ function api:shared(...) shared_data[path] = shared; end t_insert(data_array, shared); + self.shared_data[path] = shared; end return unpack(data_array); end -- cgit v1.2.3 From 5b5c8c83b395e259924d28f14b65133bb9a5791c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 14:09:35 +0100 Subject: moduleapi: module:provides(): Fix usage of wrong table --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 7a3bd1c8..61926017 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -298,7 +298,7 @@ end function api:provides(name, item) if not item then item = self.environment; end if not item.name then - local item_name = module.name; + 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 -- cgit v1.2.3 From de1168f2885e614882839f754e039da55fdbe42f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 14:10:04 +0100 Subject: moduleapi: module:provides(): Add "-provider" onto the key name --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 61926017..2223f4d0 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -306,7 +306,7 @@ function api:provides(name, item) end item.name = item_name; end - self:add_item(name, item); + self:add_item(name.."-provider", item); end function api:send(stanza) -- cgit v1.2.3 From 33ffda321d20938d4a28b15716d4f85871b4e6a7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 14:15:07 +0100 Subject: moduleapi: module:depends(): Load shared modules onto the current host even if they are loaded globally already --- core/moduleapi.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 2223f4d0..e680f615 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -120,6 +120,9 @@ 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 modulemanager.module_has_method(mod, "add_host") then + mod = nil; -- This 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); -- cgit v1.2.3 From b6a2692f3f90ed40f7e9a5103b1bb141646a7259 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 15:38:40 +0100 Subject: moduleapi: Add module:add_timer(delay, callback) - automatically halts the timer on module unload --- core/moduleapi.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index e680f615..d16ee410 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -316,4 +316,11 @@ 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 + return api; -- cgit v1.2.3