You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_cfg_utils.lua 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --[[
  2. Copyright (c) 2023, Vsevolod Stakhov <vsevolod@rspamd.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. --[[[
  14. -- @module lua_cfg_utils
  15. -- This module contains utility functions for configuration of Rspamd modules
  16. --]]
  17. local rspamd_logger = require "rspamd_logger"
  18. local exports = {}
  19. --[[[
  20. -- @function lua_util.disable_module(modname, how[, reason])
  21. -- Disables a plugin
  22. -- @param {string} modname name of plugin to disable
  23. -- @param {string} how 'redis' to disable redis, 'config' to disable startup
  24. -- @param {string} reason optional reason for failure
  25. --]]
  26. exports.disable_module = function(modname, how, reason)
  27. if rspamd_plugins_state.enabled[modname] then
  28. rspamd_plugins_state.enabled[modname] = nil
  29. end
  30. if how == 'redis' then
  31. rspamd_plugins_state.disabled_redis[modname] = {}
  32. elseif how == 'config' then
  33. rspamd_plugins_state.disabled_unconfigured[modname] = {}
  34. elseif how == 'experimental' then
  35. rspamd_plugins_state.disabled_experimental[modname] = {}
  36. elseif how == 'failed' then
  37. rspamd_plugins_state.disabled_failed[modname] = { reason = reason }
  38. else
  39. rspamd_plugins_state.disabled_unknown[modname] = {}
  40. end
  41. end
  42. --[[[
  43. -- @function lua_util.push_config_error(module, err)
  44. -- Pushes a configuration error to the state
  45. -- @param {string} module name of module
  46. -- @param {string} err error string
  47. --]]
  48. exports.push_config_error = function(module, err)
  49. if not rspamd_plugins_state.config_errors then
  50. rspamd_plugins_state.config_errors = {}
  51. end
  52. if not rspamd_plugins_state.config_errors[module] then
  53. rspamd_plugins_state.config_errors[module] = {}
  54. end
  55. table.insert(rspamd_plugins_state.config_errors[module], err)
  56. end
  57. exports.check_configuration_errors = function()
  58. local ret = true
  59. if type(rspamd_plugins_state.config_errors) == 'table' then
  60. -- We have some errors found during the configuration, so we need to show them
  61. for m, errs in pairs(rspamd_plugins_state.config_errors) do
  62. for _, err in ipairs(errs) do
  63. rspamd_logger.errx(rspamd_config, 'configuration error: module %s: %s', m, err)
  64. ret = false
  65. end
  66. end
  67. end
  68. return ret
  69. end
  70. return exports