Browse Source

[Feature] Use generic global string split function

tags/1.3.0
Vsevolod Stakhov 8 years ago
parent
commit
316af6576a
3 changed files with 13 additions and 56 deletions
  1. 8
    0
      src/lua/global_functions.lua
  2. 2
    27
      src/plugins/lua/hfilter.lua
  3. 3
    29
      src/plugins/lua/ratelimit.lua

+ 8
- 0
src/lua/global_functions.lua View File

@@ -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

+ 2
- 27
src/plugins/lua/hfilter.lua View File

@@ -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

+ 3
- 29
src/plugins/lua/ratelimit.lua View File

@@ -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

Loading…
Cancel
Save