Software /
code /
prosody
Comparison
core/modulemanager.lua @ 2151:3bb7c1daa93f
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
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 23 Nov 2009 16:07:33 +0000 |
parent | 2072:464a5392bc80 |
child | 2270:97f25da177af |
comparison
equal
deleted
inserted
replaced
2146:5c54097ef84a | 2151:3bb7c1daa93f |
---|---|
26 local t_insert, t_concat = table.insert, table.concat; | 26 local t_insert, t_concat = table.insert, table.concat; |
27 local type = type; | 27 local type = type; |
28 local next = next; | 28 local next = next; |
29 local rawget = rawget; | 29 local rawget = rawget; |
30 local error = error; | 30 local error = error; |
31 local tostring = tostring; | 31 local tostring, tonumber = tostring, tonumber; |
32 | |
33 local array, set = require "util.array", require "util.set"; | |
32 | 34 |
33 local autoload_modules = {"presence", "message", "iq"}; | 35 local autoload_modules = {"presence", "message", "iq"}; |
34 | 36 |
35 -- We need this to let modules access the real global namespace | 37 -- We need this to let modules access the real global namespace |
36 local _G = _G; | 38 local _G = _G; |
398 end | 400 end |
399 end | 401 end |
400 return value; | 402 return value; |
401 end | 403 end |
402 | 404 |
405 function api:get_option_string(...) | |
406 local value = self:get_option(...); | |
407 if type(value) == "table" then | |
408 if #value > 1 then | |
409 self:log("error", "Config option '%s' does not take a list, using just the first item", name); | |
410 end | |
411 value = value[1]; | |
412 end | |
413 if value == nil then | |
414 return nil; | |
415 end | |
416 return tostring(value); | |
417 end | |
418 | |
419 function api:get_option_number(name, ...) | |
420 local value = self:get_option(name, ...); | |
421 if type(value) == "table" then | |
422 if #value > 1 then | |
423 self:log("error", "Config option '%s' does not take a list, using just the first item", name); | |
424 end | |
425 value = value[1]; | |
426 end | |
427 local ret = tonumber(value); | |
428 if value ~= nil and ret == nil then | |
429 self:log("error", "Config option '%s' not understood, expecting a number", name); | |
430 end | |
431 return ret; | |
432 end | |
433 | |
434 function api:get_option_boolean(name, ...) | |
435 local value = self:get_option(name, ...); | |
436 if type(value) == "table" then | |
437 if #value > 1 then | |
438 self:log("error", "Config option '%s' does not take a list, using just the first item", name); | |
439 end | |
440 value = value[1]; | |
441 end | |
442 if value == nil then | |
443 return nil; | |
444 end | |
445 local ret = value == true or value == "true" or value == 1 or nil; | |
446 if ret == nil then | |
447 ret = (value == false or value == "false" or value == 0); | |
448 if ret then | |
449 ret = false; | |
450 else | |
451 ret = nil; | |
452 end | |
453 end | |
454 if ret == nil then | |
455 self:log("error", "Config option '%s' not understood, expecting true/false", name); | |
456 end | |
457 return ret; | |
458 end | |
459 | |
460 function api:get_option_array(name, ...) | |
461 local value = self:get_option(name, ...); | |
462 | |
463 if value == nil then | |
464 return nil; | |
465 end | |
466 | |
467 if type(value) ~= "table" then | |
468 return array{ value }; -- Assume any non-list is a single-item list | |
469 end | |
470 | |
471 return array():append(value); -- Clone | |
472 end | |
473 | |
474 function api:get_option_set(name, ...) | |
475 local value = self:get_option_array(name, ...); | |
476 | |
477 if value == nil then | |
478 return nil; | |
479 end | |
480 | |
481 return set.new(value); | |
482 end | |
483 | |
403 local t_remove = _G.table.remove; | 484 local t_remove = _G.table.remove; |
404 local module_items = multitable_new(); | 485 local module_items = multitable_new(); |
405 function api:add_item(key, value) | 486 function api:add_item(key, value) |
406 self.items = self.items or {}; | 487 self.items = self.items or {}; |
407 self.items[key] = self.items[key] or {}; | 488 self.items[key] = self.items[key] or {}; |