diff options
author | Matthew Wild <mwild1@gmail.com> | 2009-11-23 16:07:33 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2009-11-23 16:07:33 +0000 |
commit | 0ccdad92eff7fd98d9d4a6d3c0edbd8db21279c0 (patch) | |
tree | f3f580b57f7d4315b8405a8dbd3b439aa598edbb /core | |
parent | a1cca576be8eeb34d63963f14785445d99bc355f (diff) | |
download | prosody-0ccdad92eff7fd98d9d4a6d3c0edbd8db21279c0.tar.gz prosody-0ccdad92eff7fd98d9d4a6d3c0edbd8db21279c0.zip |
modulemanager: New module API methods for getting config options with type conversion, get_option_string, get_option_number, get_option_boolean, get_option_array, get_option_set
Diffstat (limited to 'core')
-rw-r--r-- | core/modulemanager.lua | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 9cd56187..d1f7d413 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -28,7 +28,9 @@ local type = type; local next = next; local rawget = rawget; local error = error; -local tostring = tostring; +local tostring, tonumber = tostring, tonumber; + +local array, set = require "util.array", require "util.set"; local autoload_modules = {"presence", "message", "iq"}; @@ -400,6 +402,85 @@ function api:get_option(name, default_value) return value; end +function api:get_option_string(...) + local value = self:get_option(...); + 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 t_remove = _G.table.remove; local module_items = multitable_new(); function api:add_item(key, value) |