diff options
-rw-r--r-- | src/lua/global_functions.lua | 8 | ||||
-rw-r--r-- | src/plugins/lua/hfilter.lua | 29 | ||||
-rw-r--r-- | src/plugins/lua/ratelimit.lua | 32 |
3 files changed, 13 insertions, 56 deletions
diff --git a/src/lua/global_functions.lua b/src/lua/global_functions.lua index d2c742a9c..9cf5f9a6f 100644 --- a/src/lua/global_functions.lua +++ b/src/lua/global_functions.lua @@ -55,3 +55,11 @@ function rspamd_parse_redis_server(module_name) return ret end + +function rspamd_str_split(s, sep) + local lpeg = require "lpeg" + sep = lpeg.P(sep) + local elem = lpeg.C((1 - sep)^0) + local p = lpeg.Ct(elem * (sep * elem)^0) -- make a table capture + return lpeg.match(p, s) +end
\ No newline at end of file diff --git a/src/plugins/lua/hfilter.lua b/src/plugins/lua/hfilter.lua index 1785e8a7b..3e39638a6 100644 --- a/src/plugins/lua/hfilter.lua +++ b/src/plugins/lua/hfilter.lua @@ -125,31 +125,6 @@ local function check_regexp(str, regexp_text) return false end -local function split(str, delim, maxNb) - -- Eliminate bad cases... - if string.find(str, delim) == nil then - return { str } - end - if maxNb == nil or maxNb < 1 then - maxNb = 0 -- No limit - end - local result = {} - local pat = "(.-)" .. delim .. "()" - local nb = 0 - local lastPos - for part, pos in string.gmatch(str, pat) do - nb = nb + 1 - result[nb] = part - lastPos = pos - if nb == maxNb then break end - end - -- Handle the last field - if nb ~= maxNb then - result[nb + 1] = string.sub(str, lastPos) - end - return result -end - local function check_fqdn(domain) if check_regexp(domain, '(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\\.)+[a-zA-Z0-9-]{2,63}\\.?$)') then return true @@ -407,7 +382,7 @@ local function hfilter(task) if from then --FROM host check for _,fr in ipairs(from) do - local fr_split = split(fr['addr'], '@', 0) + local fr_split = rspamd_str_split(fr['addr'], '@') if table.maxn(fr_split) == 2 then check_host(task, fr_split[2], 'FROMHOST', '', '') if fr_split[1] == 'postmaster' then @@ -440,7 +415,7 @@ local function hfilter(task) if config['mid_enabled'] then local message_id = task:get_message_id() if message_id then - local mid_split = split(message_id, '@', 0) + local mid_split = rspamd_str_split(message_id, '@') if table.maxn(mid_split) == 2 and not string.find(mid_split[2], 'local') then check_host(task, mid_split[2], 'MID') end diff --git a/src/plugins/lua/ratelimit.lua b/src/plugins/lua/ratelimit.lua index 42ae569c6..1032a35b9 100644 --- a/src/plugins/lua/ratelimit.lua +++ b/src/plugins/lua/ratelimit.lua @@ -51,36 +51,10 @@ local rspamd_util = require "rspamd_util" local _ = require "fun" --local dumper = require 'pl.pretty'.dump ---- Utility function for split string to table -local function split(str, delim, maxNb) - -- Eliminate bad cases... - if string.find(str, delim) == nil then - return { str } - end - if maxNb == nil or maxNb < 1 then - maxNb = 0 -- No limit - end - local result = {} - local pat = "(.-)" .. delim .. "()" - local nb = 0 - local lastPos - for part, pos in string.gmatch(str, pat) do - nb = nb + 1 - result[nb] = part - lastPos = pos - if nb == maxNb then break end - end - -- Handle the last field - if nb ~= maxNb then - result[nb + 1] = string.sub(str, lastPos) - end - return result -end - --- Parse atime and bucket of limit local function parse_limits(data) local function parse_limit_elt(str) - local elts = split(str, ':', 3) + local elts = rspamd_str_split(str, ':') if not elts or #elts < 2 then return {0, 0, 0} else @@ -341,7 +315,7 @@ end --- Parse a single limit description local function parse_limit(str) - local params = split(str, ':', 0) + local params = rspamd_str_split(str, ':') local function set_limit(limit, burst, rate) limit[1] = tonumber(burst) @@ -396,7 +370,7 @@ if opts then rspamd_logger.infox(rspamd_config, 'enabled rate buckets: %s', enabled_limits) if opts['whitelisted_rcpts'] and type(opts['whitelisted_rcpts']) == 'string' then - whitelisted_rcpts = split(opts['whitelisted_rcpts'], ',') + whitelisted_rcpts = rspamd_str_split(opts['whitelisted_rcpts'], ',') elseif type(opts['whitelisted_rcpts']) == 'table' then whitelisted_rcpts = opts['whitelisted_rcpts'] end |