Skip to content

Commit

Permalink
[Minor] Move config stuff out
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Aug 7, 2023
1 parent 416ba55 commit 1e84018
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 50 deletions.
67 changes: 67 additions & 0 deletions lualib/lua_cfg_utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--[[
Copyright (c) 2023, Vsevolod Stakhov <vsevolod@rspamd.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--

--[[[
-- @module lua_cfg_utils
-- This module contains utility functions for configuration of Rspamd modules
--]]
local exports = {}
--[[[
-- @function lua_util.disable_module(modname, how[, reason])
-- Disables a plugin
-- @param {string} modname name of plugin to disable
-- @param {string} how 'redis' to disable redis, 'config' to disable startup
-- @param {string} reason optional reason for failure
--]]
exports.disable_module = function(modname, how, reason)
if rspamd_plugins_state.enabled[modname] then
rspamd_plugins_state.enabled[modname] = nil
end
if how == 'redis' then
rspamd_plugins_state.disabled_redis[modname] = {}
elseif how == 'config' then
rspamd_plugins_state.disabled_unconfigured[modname] = {}
elseif how == 'experimental' then
rspamd_plugins_state.disabled_experimental[modname] = {}
elseif how == 'failed' then
rspamd_plugins_state.disabled_failed[modname] = { reason = reason }
else
rspamd_plugins_state.disabled_unknown[modname] = {}
end
end
--[[[
-- @function lua_util.push_config_error(module, err)
-- Pushes a configuration error to the state
-- @param {string} module name of module
-- @param {string} err error string
--]]
exports.push_config_error = function(module, err)
if not rspamd_plugins_state.config_errors then
rspamd_plugins_state.config_errors = {}
end
if not rspamd_plugins_state.config_errors[module] then
rspamd_plugins_state.config_errors[module] = {}
end
table.insert(rspamd_plugins_state.config_errors[module], err)
end
return exports
50 changes: 4 additions & 46 deletions lualib/lua_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -380,52 +380,10 @@ end
exports.spairs = spairs
--[[[
-- @function lua_util.disable_module(modname, how[, reason])
-- Disables a plugin
-- @param {string} modname name of plugin to disable
-- @param {string} how 'redis' to disable redis, 'config' to disable startup
-- @param {string} reason optional reason for failure
--]]
local function disable_module(modname, how, reason)
if rspamd_plugins_state.enabled[modname] then
rspamd_plugins_state.enabled[modname] = nil
end
if how == 'redis' then
rspamd_plugins_state.disabled_redis[modname] = {}
elseif how == 'config' then
rspamd_plugins_state.disabled_unconfigured[modname] = {}
elseif how == 'experimental' then
rspamd_plugins_state.disabled_experimental[modname] = {}
elseif how == 'failed' then
rspamd_plugins_state.disabled_failed[modname] = { reason = reason }
else
rspamd_plugins_state.disabled_unknown[modname] = {}
end
end
exports.disable_module = disable_module
--[[[
-- @function lua_util.push_config_error(module, err)
-- Pushes a configuration error to the state
-- @param {string} module name of module
-- @param {string} err error string
--]]
local function push_config_error(module, err)
if not rspamd_plugins_state.config_errors then
rspamd_plugins_state.config_errors = {}
end
if not rspamd_plugins_state.config_errors[module] then
rspamd_plugins_state.config_errors[module] = {}
end
table.insert(rspamd_plugins_state.config_errors[module], err)
end
local lua_cfg_utils = require "lua_cfg_utils"
exports.push_config_error = push_config_error
exports.config_utils = lua_cfg_utils
exports.disable_module = lua_cfg_utils.disable_module
--[[[
-- @function lua_util.disable_module(modname)
Expand All @@ -437,7 +395,7 @@ local function check_experimental(modname)
if rspamd_config:experimental_enabled() then
return true
else
disable_module(modname, 'experimental')
lua_cfg_utils.disable_module(modname, 'experimental')
end
return false
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/lua/antivirus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ if opts and type(opts) == 'table' then

if not cb then
rspamd_logger.errx(rspamd_config, 'cannot add rule: "' .. k .. '"')
lua_util.push_config_error(N, 'cannot add AV rule: "' .. k .. '"')
lua_util.config_utils.push_config_error(N, 'cannot add AV rule: "' .. k .. '"')
else
rspamd_logger.infox(rspamd_config, 'added antivirus engine %s -> %s', k, m.symbol)
local t = {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/lua/bimi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ local res, err = settings_schema:transform(settings)
if not res then
rspamd_logger.warnx(rspamd_config, 'plugin is misconfigured: %s', err)
local err_msg = string.format("schema error: %s", res)
lua_util.push_config_error(N, err_msg)
lua_util.config_utils.push_config_error(N, err_msg)
lua_util.disable_module(N, "failed", err_msg)
return
end
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/lua/reputation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,10 @@ if opts['rules'] then
for k, v in pairs(opts['rules']) do
if not ((v or E).selector) then
rspamd_logger.errx(rspamd_config, "no selector defined for rule %s", k)
lua_util.push_config_error(N, "no selector defined for rule: " .. k)
lua_util.config_utils.push_config_error(N, "no selector defined for rule: " .. k)
else
if not parse_rule(k, v) then
lua_util.push_config_error(N, "reputation rule is misconfigured: " .. k)
lua_util.config_utils.push_config_error(N, "reputation rule is misconfigured: " .. k)
end
end
end
Expand Down

0 comments on commit 1e84018

Please sign in to comment.