You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

trie.lua 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- Trie is rspamd module designed to define and operate with suffix trie
  2. local tries = {}
  3. local function split(str, delim, maxNb)
  4. -- Eliminate bad cases...
  5. if string.find(str, delim) == nil then
  6. return { str }
  7. end
  8. if maxNb == nil or maxNb < 1 then
  9. maxNb = 0 -- No limit
  10. end
  11. local result = {}
  12. local pat = "(.-)" .. delim .. "()"
  13. local nb = 0
  14. local lastPos
  15. for part, pos in string.gfind(str, pat) do
  16. nb = nb + 1
  17. result[nb] = part
  18. lastPos = pos
  19. if nb == maxNb then break end
  20. end
  21. -- Handle the last field
  22. if nb ~= maxNb then
  23. result[nb + 1] = string.sub(str, lastPos)
  24. end
  25. return result
  26. end
  27. local function add_trie(params)
  28. local symbol = params[1]
  29. file = io.open(params[2])
  30. if file then
  31. local trie = {}
  32. trie['trie'] = rspamd_trie:create(true)
  33. num = 0
  34. for line in file:lines() do
  35. trie['trie']:add_pattern(line, num)
  36. num = num + 1
  37. end
  38. if type(rspamd_config.get_api_version) ~= 'nil' then
  39. rspamd_config:register_virtual_symbol(symbol, 1.0)
  40. end
  41. file:close()
  42. trie['symbol'] = symbol
  43. table.insert(tries, trie)
  44. else
  45. local patterns = split(params[2], ',')
  46. local trie = {}
  47. trie['trie'] = rspamd_trie:create(true)
  48. print (type(trie['trie']))
  49. for num,pattern in ipairs(patterns) do
  50. trie['trie']:add_pattern(pattern, num)
  51. end
  52. if type(rspamd_config.get_api_version) ~= 'nil' then
  53. rspamd_config:register_virtual_symbol(symbol, 1.0)
  54. end
  55. trie['symbol'] = symbol
  56. table.insert(tries, trie)
  57. end
  58. end
  59. function check_trie(task)
  60. for _,trie in ipairs(tries) do
  61. print (type(trie['trie']))
  62. if trie['trie']:search_task(task) then
  63. task:insert_result(trie['symbol'], 1)
  64. end
  65. end
  66. end
  67. -- Registration
  68. if type(rspamd_config.get_api_version) ~= 'nil' then
  69. if rspamd_config:get_api_version() >= 1 then
  70. rspamd_config:register_module_option('trie', 'rule', 'string')
  71. end
  72. end
  73. local opts = rspamd_config:get_all_opt('trie')
  74. if opts then
  75. local strrules = opts['rule']
  76. if strrules then
  77. if type(strrules) == 'table' then
  78. for _,value in ipairs(strrules) do
  79. local params = split(value, ':')
  80. add_trie(params)
  81. end
  82. elseif type(strrules) == 'string' then
  83. local params = split(strrules, ':')
  84. add_trie (params)
  85. end
  86. end
  87. if table.maxn(tries) then
  88. if type(rspamd_config.get_api_version) ~= 'nil' then
  89. rspamd_config:register_callback_symbol('TRIE', 1.0, 'check_trie')
  90. else
  91. rspamd_config:register_symbol('TRIE', 1.0, 'check_trie')
  92. end
  93. end
  94. end