rspamd.local.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. rspamd_config.MAILCOW_AUTH = {
  2. callback = function(task)
  3. local uname = task:get_user()
  4. if uname then
  5. return 1
  6. end
  7. end
  8. }
  9. rspamd_config:register_symbol({
  10. name = 'TAG_MOO',
  11. type = 'postfilter',
  12. callback = function(task)
  13. local util = require("rspamd_util")
  14. local rspamd_logger = require "rspamd_logger"
  15. local tagged_rcpt = task:get_symbol("TAGGED_RCPT")
  16. local mailcow_domain = task:get_symbol("RCPT_MAILCOW_DOMAIN")
  17. if tagged_rcpt and mailcow_domain then
  18. local tag = tagged_rcpt[1].options[1]
  19. rspamd_logger.infox("found tag: %s", tag)
  20. local action = task:get_metric_action('default')
  21. rspamd_logger.infox("metric action now: %s", action)
  22. if action ~= 'no action' and action ~= 'greylist' then
  23. rspamd_logger.infox("skipping tag handler for action: %s", action)
  24. task:set_metric_action('default', action)
  25. return true
  26. end
  27. local wants_subject_tag = task:get_symbol("RCPT_WANTS_SUBJECT_TAG")
  28. if wants_subject_tag then
  29. rspamd_logger.infox("user wants subject modified for tagged mail")
  30. local sbj = task:get_header('Subject')
  31. new_sbj = '=?UTF-8?B?' .. tostring(util.encode_base64('[' .. tag .. '] ' .. sbj)) .. '?='
  32. task:set_milter_reply({
  33. remove_headers = {['Subject'] = 1},
  34. add_headers = {['Subject'] = new_sbj}
  35. })
  36. else
  37. rspamd_logger.infox("Add X-Moo-Tag header")
  38. task:set_milter_reply({
  39. add_headers = {['X-Moo-Tag'] = 'YES'}
  40. })
  41. end
  42. end
  43. end,
  44. priority = 11
  45. })
  46. rspamd_config:register_symbol({
  47. name = 'DYN_RL_CHECK',
  48. type = 'prefilter',
  49. callback = function(task)
  50. local util = require("rspamd_util")
  51. local redis_params = rspamd_parse_redis_server('dyn_rl')
  52. local rspamd_logger = require "rspamd_logger"
  53. local envfrom = task:get_from(1)
  54. local env_from_domain = envfrom[1].domain:lower() -- get smtp from domain in lower case
  55. local env_from_addr = envfrom[1].addr:lower() -- get smtp from addr in lower case
  56. local function redis_cb_user(err, data)
  57. if err or type(data) ~= 'string' then
  58. rspamd_logger.infox(rspamd_config, "dynamic ratelimit request for user %s returned invalid or empty data (\"%s\") or error (\"%s\") - trying dynamic ratelimit for domain...", env_from_addr, data, err)
  59. local function redis_key_cb_domain(err, data)
  60. if err or type(data) ~= 'string' then
  61. rspamd_logger.infox(rspamd_config, "dynamic ratelimit request for domain %s returned invalid or empty data (\"%s\") or error (\"%s\")", env_from_domain, data, err)
  62. else
  63. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for domain %s with value %s", env_from_domain, data)
  64. task:insert_result('DYN_RL', 0.0, data)
  65. end
  66. end
  67. local redis_ret_domain = rspamd_redis_make_request(task,
  68. redis_params, -- connect params
  69. env_from_domain, -- hash key
  70. false, -- is write
  71. redis_key_cb_domain, --callback
  72. 'HGET', -- command
  73. {'RL_VALUE', env_from_domain} -- arguments
  74. )
  75. if not redis_ret_domain then
  76. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for domain")
  77. end
  78. else
  79. rspamd_logger.infox(rspamd_config, "found dynamic ratelimit in redis for user %s with value %s", env_from_addr, data)
  80. task:insert_result('DYN_RL', 0.0, data)
  81. end
  82. end
  83. local redis_ret_user = rspamd_redis_make_request(task,
  84. redis_params, -- connect params
  85. env_from_addr, -- hash key
  86. false, -- is write
  87. redis_cb_user, --callback
  88. 'HGET', -- command
  89. {'RL_VALUE', env_from_addr} -- arguments
  90. )
  91. if not redis_ret_user then
  92. rspamd_logger.infox(rspamd_config, "cannot make request to load ratelimit for user")
  93. end
  94. return true
  95. end,
  96. priority = 20
  97. })
  98. rspamd_config:register_symbol({
  99. name = 'NO_LOG_STAT_MAILFLOW',
  100. type = 'postfilter',
  101. callback = function(task)
  102. local sender = task:get_header('From')
  103. if sender == 'monitoring-system@everycloudtech.us' then
  104. task:set_flag('no_log')
  105. task:set_flag('no_stat')
  106. end
  107. end
  108. })