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.

maps.lua 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. local exports = {}
  2. local function rspamd_map_add(mname, optname, mtype, description)
  3. local ret = {
  4. get_key = function(t, k)
  5. if t.__data then
  6. return t.__data:get_key(k)
  7. end
  8. return nil
  9. end
  10. }
  11. local ret_mt = {
  12. __index = function(t, k)
  13. if t.__data then
  14. return t.get_key(k)
  15. end
  16. return nil
  17. end
  18. }
  19. local opt = rspamd_config:get_module_opt(mname, optname)
  20. if not opt then
  21. return nil
  22. end
  23. if type(opt) == 'string' then
  24. -- We have a single string, so we treat it as a map
  25. local map = rspamd_config:add_map{
  26. type = mtype,
  27. description = description,
  28. url = opt,
  29. }
  30. if map then
  31. ret.__data = map
  32. setmetatable(ret, ret_mt)
  33. return ret
  34. end
  35. elseif type(opt) == 'table' then
  36. -- it might be plain map or map of plain elements
  37. if opt[1] then
  38. if mtype == 'radix' then
  39. if string.find(opt[1], '^%d') then
  40. local map = rspamd_config:radix_from_config(mname, optname)
  41. if map then
  42. ret.__data = map
  43. setmetatable(ret, ret_mt)
  44. return ret
  45. end
  46. else
  47. -- Plain table
  48. local map = rspamd_config:add_map{
  49. type = mtype,
  50. description = description,
  51. url = opt,
  52. }
  53. if map then
  54. ret.__data = map
  55. setmetatable(ret, ret_mt)
  56. return ret
  57. end
  58. end
  59. elseif mtype == 'regexp' then
  60. -- Plain table
  61. local map = rspamd_config:add_map{
  62. type = mtype,
  63. description = description,
  64. url = opt,
  65. }
  66. if map then
  67. ret.__data = map
  68. setmetatable(ret, ret_mt)
  69. return ret
  70. end
  71. else
  72. if string.find(opt[1], '^/%a') or string.find(opt[1], '^http') then
  73. -- Plain table
  74. local map = rspamd_config:add_map{
  75. type = mtype,
  76. description = description,
  77. url = opt,
  78. }
  79. if map then
  80. ret.__data = map
  81. setmetatable(ret, ret_mt)
  82. return ret
  83. end
  84. else
  85. local data = {}
  86. local nelts = 0
  87. for _,elt in ipairs(opt) do
  88. if type(elt) == 'string' then
  89. data[elt] = true
  90. nelts = nelts + 1
  91. end
  92. end
  93. if nelts > 0 then
  94. ret.__data = data
  95. ret.get_key = function(t, k)
  96. if k ~= '__data' then
  97. return t.__data[k]
  98. end
  99. return nil
  100. end
  101. return ret
  102. end
  103. end
  104. end
  105. else
  106. local map = rspamd_config:add_map{
  107. type = mtype,
  108. description = description,
  109. url = opt,
  110. }
  111. if map then
  112. ret.__data = map
  113. setmetatable(ret, ret_mt)
  114. return ret
  115. end
  116. end
  117. end
  118. return nil
  119. end
  120. exports.rspamd_map_add = rspamd_map_add
  121. return exports