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



return ret return ret
end 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

return false return false
end 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) local function check_fqdn(domain)
if check_regexp(domain, '(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\\.)+[a-zA-Z0-9-]{2,63}\\.?$)') then if check_regexp(domain, '(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\\.)+[a-zA-Z0-9-]{2,63}\\.?$)') then
return true return true
if from then if from then
--FROM host check --FROM host check
for _,fr in ipairs(from) do 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 if table.maxn(fr_split) == 2 then
check_host(task, fr_split[2], 'FROMHOST', '', '') check_host(task, fr_split[2], 'FROMHOST', '', '')
if fr_split[1] == 'postmaster' then if fr_split[1] == 'postmaster' then
if config['mid_enabled'] then if config['mid_enabled'] then
local message_id = task:get_message_id() local message_id = task:get_message_id()
if message_id then 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 if table.maxn(mid_split) == 2 and not string.find(mid_split[2], 'local') then
check_host(task, mid_split[2], 'MID') check_host(task, mid_split[2], 'MID')
end end

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

local _ = require "fun" local _ = require "fun"
--local dumper = require 'pl.pretty'.dump --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 --- Parse atime and bucket of limit
local function parse_limits(data) local function parse_limits(data)
local function parse_limit_elt(str) local function parse_limit_elt(str)
local elts = split(str, ':', 3)
local elts = rspamd_str_split(str, ':')
if not elts or #elts < 2 then if not elts or #elts < 2 then
return {0, 0, 0} return {0, 0, 0}
else else


--- Parse a single limit description --- Parse a single limit description
local function parse_limit(str) local function parse_limit(str)
local params = split(str, ':', 0)
local params = rspamd_str_split(str, ':')


local function set_limit(limit, burst, rate) local function set_limit(limit, burst, rate)
limit[1] = tonumber(burst) limit[1] = tonumber(burst)
rspamd_logger.infox(rspamd_config, 'enabled rate buckets: %s', enabled_limits) rspamd_logger.infox(rspamd_config, 'enabled rate buckets: %s', enabled_limits)


if opts['whitelisted_rcpts'] and type(opts['whitelisted_rcpts']) == 'string' then 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 elseif type(opts['whitelisted_rcpts']) == 'table' then
whitelisted_rcpts = opts['whitelisted_rcpts'] whitelisted_rcpts = opts['whitelisted_rcpts']
end end

Loading…
Cancel
Save