Mercurial > prosody-hg
comparison core/moduleapi.lua @ 13204:c9ef35fab0b1
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.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 16 Jul 2023 19:49:12 +0200 |
| parents | aa6c2692a4be |
| children | 0ccd82b965d5 |
comparison
equal
deleted
inserted
replaced
| 13203:aa6c2692a4be | 13204:c9ef35fab0b1 |
|---|---|
| 19 local time_now = require "prosody.util.time".now; | 19 local time_now = require "prosody.util.time".now; |
| 20 local format = require "prosody.util.format".format; | 20 local format = require "prosody.util.format".format; |
| 21 local jid_node = require "prosody.util.jid".node; | 21 local jid_node = require "prosody.util.jid".node; |
| 22 local jid_split = require "prosody.util.jid".split; | 22 local jid_split = require "prosody.util.jid".split; |
| 23 local jid_resource = require "prosody.util.jid".resource; | 23 local jid_resource = require "prosody.util.jid".resource; |
| 24 local human_io = require "prosody.util.human.io"; | |
| 24 | 25 |
| 25 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; | 26 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
| 26 local error, setmetatable, type = error, setmetatable, type; | 27 local error, setmetatable, type = error, setmetatable, type; |
| 27 local ipairs, pairs, select = ipairs, pairs, select; | 28 local ipairs, pairs, select = ipairs, pairs, select; |
| 28 local tonumber, tostring = tonumber, tostring; | 29 local tonumber, tostring = tonumber, tostring; |
| 247 return min; | 248 return min; |
| 248 end | 249 end |
| 249 if max and ret > max then | 250 if max and ret > max then |
| 250 self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max); | 251 self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max); |
| 251 return max; | 252 return max; |
| 253 end | |
| 254 return ret; | |
| 255 end | |
| 256 | |
| 257 function api:get_option_period(name, default_value) | |
| 258 local value = self:get_option_scalar(name, default_value); | |
| 259 local num = tonumber(value); | |
| 260 if num then | |
| 261 -- assume seconds | |
| 262 return num; | |
| 263 end | |
| 264 local ret = human_io.parse_duration(value); | |
| 265 if value ~= nil and ret == nil then | |
| 266 self:log("error", "Config option '%s' not understood, expecting a period", name); | |
| 252 end | 267 end |
| 253 return ret; | 268 return ret; |
| 254 end | 269 end |
| 255 | 270 |
| 256 function api:get_option_boolean(name, ...) | 271 function api:get_option_boolean(name, ...) |
