diff options
author | Kim Alvefur <zash@zash.se> | 2023-07-16 19:49:12 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2023-07-16 19:49:12 +0200 |
commit | 3c4dc9a7545791bb70f3a542eac910e6487e4ba4 (patch) | |
tree | ff96eeec5cc8df58ed0cc0dd9c76cad8d750efb2 | |
parent | 924064a30ad560e9aecc2d5e3b72f6a4a85b40ae (diff) | |
download | prosody-3c4dc9a7545791bb70f3a542eac910e6487e4ba4.tar.gz prosody-3c4dc9a7545791bb70f3a542eac910e6487e4ba4.zip |
core.moduleapi: Add :get_option_period for parsing time intervals
E.g. for use in mod_mam and others that take an amount of time before
some (usually cleanup) action is taken.
-rw-r--r-- | .luacheckrc | 1 | ||||
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | core/features.lua | 1 | ||||
-rw-r--r-- | core/moduleapi.lua | 15 |
4 files changed, 18 insertions, 0 deletions
diff --git a/.luacheckrc b/.luacheckrc index 473015d8..9d89fbe6 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -77,6 +77,7 @@ files["plugins/"] = { "module.get_option_inherited_set", "module.get_option_number", "module.get_option_path", + "module.get_option_period", "module.get_option_scalar", "module.get_option_set", "module.get_option_string", @@ -42,6 +42,7 @@ TRUNK - Config interface API can require that string values be picked from a provided set - Acceptable interval can be specified for number options +- Method for parsing time periods / intervals from config ## Changes diff --git a/core/features.lua b/core/features.lua index 41d8ce70..49c34111 100644 --- a/core/features.lua +++ b/core/features.lua @@ -19,5 +19,6 @@ return { -- new moduleapi methods "getopt-enum"; "getopt-interval"; + "getopt-period"; }; }; diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 87a25ae7..66652e09 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -21,6 +21,7 @@ local format = require "prosody.util.format".format; local jid_node = require "prosody.util.jid".node; local jid_split = require "prosody.util.jid".split; local jid_resource = require "prosody.util.jid".resource; +local human_io = require "prosody.util.human.io"; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; local error, setmetatable, type = error, setmetatable, type; @@ -253,6 +254,20 @@ function api:get_option_number(name, default_value, min, max) return ret; end +function api:get_option_period(name, default_value) + local value = self:get_option_scalar(name, default_value); + local num = tonumber(value); + if num then + -- assume seconds + return num; + end + local ret = human_io.parse_duration(value); + if value ~= nil and ret == nil then + self:log("error", "Config option '%s' not understood, expecting a period", name); + end + return ret; +end + function api:get_option_boolean(name, ...) local value = self:get_option_scalar(name, ...); if value == nil then |