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