# HG changeset patch # User Kim Alvefur # Date 1689529752 -7200 # Node ID c9ef35fab0b122a9db235b5189129ccc2a4012e6 # Parent aa6c2692a4be0eb6f9ff6de3e0ea1877ddfa075a 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. diff -r aa6c2692a4be -r c9ef35fab0b1 .luacheckrc --- a/.luacheckrc Tue Oct 05 15:36:38 2021 +0200 +++ b/.luacheckrc Sun Jul 16 19:49:12 2023 +0200 @@ -77,6 +77,7 @@ "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", diff -r aa6c2692a4be -r c9ef35fab0b1 CHANGES --- a/CHANGES Tue Oct 05 15:36:38 2021 +0200 +++ b/CHANGES Sun Jul 16 19:49:12 2023 +0200 @@ -42,6 +42,7 @@ - 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 -r aa6c2692a4be -r c9ef35fab0b1 core/features.lua --- a/core/features.lua Tue Oct 05 15:36:38 2021 +0200 +++ b/core/features.lua Sun Jul 16 19:49:12 2023 +0200 @@ -19,5 +19,6 @@ -- new moduleapi methods "getopt-enum"; "getopt-interval"; + "getopt-period"; }; }; diff -r aa6c2692a4be -r c9ef35fab0b1 core/moduleapi.lua --- a/core/moduleapi.lua Tue Oct 05 15:36:38 2021 +0200 +++ b/core/moduleapi.lua Sun Jul 16 19:49:12 2023 +0200 @@ -21,6 +21,7 @@ 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 @@ 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