diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-15 16:24:44 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-15 16:24:44 +0000 |
commit | 5321190dc5e445cb5a81143be7bfd6fe0afc13b0 (patch) | |
tree | 0b18a4b5ef360b180eaa2c7ddb3bd0fa8c227c02 /lualib | |
parent | d54cddeeee88a24bcfa75a9144989812fcd11208 (diff) | |
download | rspamd-5321190dc5e445cb5a81143be7bfd6fe0afc13b0.tar.gz rspamd-5321190dc5e445cb5a81143be7bfd6fe0afc13b0.zip |
[Project] Add some generic code to squeeze lua rules
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/lua_squeeze_rules.lua | 97 | ||||
-rw-r--r-- | lualib/lua_util.lua | 1 |
2 files changed, 98 insertions, 0 deletions
diff --git a/lualib/lua_squeeze_rules.lua b/lualib/lua_squeeze_rules.lua new file mode 100644 index 000000000..d740e2ee0 --- /dev/null +++ b/lualib/lua_squeeze_rules.lua @@ -0,0 +1,97 @@ +--[[ +Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru> + +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. +]]-- + +local exports = {} +local logger = require 'rspamd_logger' + +-- Squeezed rules part +local squeezed_rules = {} -- plain vector of all rules squeezed +local squeezed_symbols = {} -- indexed by name of symbol +local squeezed_deps = {} -- squeezed deps +local SN = 'lua_squeeze' +local squeeze_function_id + +local function lua_squeeze_function(task) + if not squeezed_symbols then + for k,v in pairs(squeezed_symbols) do + if not squeezed_exceptions[k] then + logger.debugm(SN, task, 'added squeezed rule: %s', k) + table.insert(squeezed_rules, v) + else + logger.debugm(SN, task, 'skipped squeezed rule: %s', k) + end + end + + squeezed_symbols = nil + end + + for _,func in ipairs(squeezed_rules) do + local ret = func(task) + + if ret then + -- Function has returned something, so it is rule, not a plugin + logger.errx(task, 'hui: %s', ret) + end + end +end + +exports.squeeze_rule = function(s, func) + if s then + if not squeezed_symbols[s] then + squeezed_symbols[s] = { + cb = func, + order = 0, + } + logger.debugm(SN, rspamd_config, 'squeezed rule: %s', s) + else + logger.warnx(rspamd_config, 'duplicate symbol registered: %s, skip', s) + end + else + -- Unconditionally add function to the squeezed rules + logger.debugm(SN, rspamd_config, 'squeezed unnamed rule: %s', #squeezed_rules) + table.insert(squeezed_rules, func) + end + + if not squeeze_function_id then + squeeze_function_id = rspamd_config:register_symbol{ + type = 'callback', + callback = lua_squeeze_function, + name = 'LUA_SQUEEZE', + description = 'Meta rule for Lua rules that can be squeezed', + no_squeeze = true, -- to avoid infinite recursion + } + end + + return squeeze_function_id +end + +exports.squeeze_dependency = function(from, to) + logger.debugm(SN, rspamd_config, 'squeeze dep %s->%s', to, from) + + if not squeezed_deps[to] then + squeezed_deps[to] = {} + end + + if not squeezed_symbols[to][from] then + squeezed_symbols[to][from] = true + else + logger.warnx('duplicate dependency %s->%s', to, from) + end + + return true +end + +return exports
\ No newline at end of file diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index d41d79fea..33d7e3714 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -22,6 +22,7 @@ limitations under the License. local exports = {} local lpeg = require 'lpeg' + local split_grammar = {} local function rspamd_str_split(s, sep) local gr = split_grammar[sep] |