local exports = {}
local known_ids = {}
+local on_load_added = false
+
+local function register_settings_cb()
+ for _,set in pairs(known_ids) do
+ local s = set.settings
+ local enabled_symbols = {}
+ local disabled_symbols = {}
+
+ -- Enabled map
+ if s.symbols_enabled then
+ for _,sym in ipairs(s.symbols_enabled) do
+ enabled_symbols[sym] = true
+ end
+ end
+ if s.groups_enabled then
+ for _,gr in ipairs(s.groups_enabled) do
+ local syms = rspamd_config:get_group_symbols()
+
+ if syms then
+ for _,sym in ipairs(syms) do
+ enabled_symbols[sym] = true
+ end
+ end
+ end
+ end
+
+ -- Disabled map
+ if s.symbols_disabled then
+ for _,sym in ipairs(s.symbols_disabled) do
+ disabled_symbols[sym] = true
+ end
+ end
+ if s.groups_disabled then
+ for _,gr in ipairs(s.groups_disabled) do
+ local syms = rspamd_config:get_group_symbols()
+
+ if syms then
+ for _,sym in ipairs(syms) do
+ disabled_symbols[sym] = true
+ end
+ end
+ end
+ end
+
+ rspamd_config:register_settings_id(set.name, enabled_symbols, disabled_symbols)
+
+ -- Remove to avoid clash
+ s.symbols_disabled = nil
+ s.symbols_enabled = nil
+ s.groups_enabled = nil
+ s.groups_disabled = nil
+ end
+end
-- Returns numeric representation of the settings id
local function numeric_settings_id(str)
}
end
+ if not on_load_added then
+ rspamd_config:add_on_load(register_settings_cb)
+ on_load_added = true
+ end
+
return numeric_id
end
exports.register_settings_id = register_settings_id
-local function reset_ids()
- known_ids = {}
-end
-
-exports.reset_ids = reset_ids
local function settings_by_id(id)
return known_ids[id]
local selectors_cache = {} -- Used to speed up selectors in settings
-local function apply_settings(task, to_apply)
+local function apply_settings(task, to_apply, id)
task:set_settings(to_apply)
task:cache_set('settings', to_apply)
+ if id then
+ task:set_settings_id(id)
+ end
+
if to_apply['add_headers'] or to_apply['remove_headers'] then
local rep = {
add_headers = to_apply['add_headers'] or {},
local res,err = parser:parse_string(tostring(query_set))
if res then
local settings_obj = parser:get_object()
- apply_settings(task, settings_obj)
+ apply_settings(task, settings_obj, nil)
return true
else
if not settings_id then
rspamd_logger.infox(task, 'apply maxscore = %s', nset.actions)
- apply_settings(task, nset)
+ apply_settings(task, nset, nil)
return true
end
end
if nset then
elt.apply = lua_util.override_defaults(nset, elt.apply)
end
- apply_settings(task, elt['apply'])
+ apply_settings(task, elt['apply'], settings_id)
rspamd_logger.infox(task, "applying settings id %s", settings_id)
return true
end
rspamd_logger.warnx(task, 'no settings id "%s" has been found', settings_id)
if nset then
rspamd_logger.infox(task, 'apply maxscore = %s', nset.actions)
- apply_settings(task, nset)
+ apply_settings(task, nset, nil)
return true
end
end
else
if nset then
rspamd_logger.infox(task, 'apply maxscore = %s', nset.actions)
- apply_settings(task, nset)
+ apply_settings(task, nset, nil)
return true
end
end
rspamd_logger.infox(task, "<%s> apply settings according to rule %s (%s matched)",
task:get_message_id(), s.name, table.concat(matched, ','))
if s.rule['apply'] then
- apply_settings(task, s.rule['apply'])
+ apply_settings(task, s.rule.apply, s.rule.id)
applied = true
end
if s.rule['symbols'] then
end
-- Process settings based on their priority
-local function process_settings_table(tbl)
+local function process_settings_table(tbl, allow_ids)
local get_priority = function(elt)
local pri_tonum = function(p)
if p then
name, elt.symbols)
out['symbols'] = elt['symbols']
end
- if not elt.id then
- elt.id = name
- end
+
if elt['apply'] then
-- Just insert all metric results to the action key
return nil
end
- if elt['id'] then
- out.id = lua_settings.register_settings_id(elt.id, out)
- lua_util.debugm(N, rspamd_config, 'added settings id to "%s": %s -> %s',
- name, elt.id, out.id)
+ if allow_ids then
+ if not elt.id then
+ elt.id = name
+ end
+
+ if elt['id'] then
+ out.id = lua_settings.register_settings_id(elt.id, out)
+ lua_util.debugm(N, rspamd_config, 'added settings id to "%s": %s -> %s',
+ name, elt.id, out.id)
+ end
+ else
+ if elt['id'] then
+ rspamd_logger.errx(rspamd_config, 'cannot set static IDs from dynamic settings, please read the docs')
+ end
end
return out
-- clear all settings
max_pri = 0
local nrules = 0
- lua_settings.reset_ids()
for k in pairs(settings) do settings[k]={} end
-- fill new settings by priority
fun.for_each(function(k, v)
else
local obj = parser:get_object()
if obj['settings'] then
- process_settings_table(obj['settings'])
+ process_settings_table(obj['settings'], false)
else
- process_settings_table(obj)
+ process_settings_table(obj, false)
end
end
local obj = parser:get_object()
rspamd_logger.infox(task, "<%1> apply settings according to redis rule %2",
task:get_message_id(), id)
- apply_settings(task, obj)
+ apply_settings(task, obj, nil)
break
end
end
rspamd_logger.errx(rspamd_config, 'cannot load settings from %1', set_section)
end
elseif set_section and type(set_section) == "table" then
- process_settings_table(set_section)
+ process_settings_table(set_section, true)
end
rspamd_config:register_symbol({