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.

lua_util.lua 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. local exports = {}
  2. local lpeg = require 'lpeg'
  3. local split_grammar = {}
  4. local function rspamd_str_split(s, sep)
  5. local gr = split_grammar[sep]
  6. if not gr then
  7. local _sep = lpeg.P(sep)
  8. local elem = lpeg.C((1 - _sep)^0)
  9. local p = lpeg.Ct(elem * (_sep * elem)^0)
  10. gr = p
  11. split_grammar[sep] = gr
  12. end
  13. return gr:match(s)
  14. end
  15. exports.rspamd_str_split = rspamd_str_split
  16. local space = lpeg.S' \t\n\v\f\r'
  17. local nospace = 1 - space
  18. local ptrim = space^0 * lpeg.C((space^0 * nospace^1)^0)
  19. local match = lpeg.match
  20. exports.rspamd_str_trim = function(s)
  21. return match(ptrim, s)
  22. end
  23. -- Robert Jay Gould http://lua-users.org/wiki/SimpleRound
  24. exports.round = function(num, numDecimalPlaces)
  25. local mult = 10^(numDecimalPlaces or 0)
  26. return math.floor(num * mult) / mult
  27. end
  28. exports.template = function(tmpl, keys)
  29. local var_lit = lpeg.P { lpeg.R("az") + lpeg.R("AZ") + lpeg.R("09") + "_" }
  30. local var = lpeg.P { (lpeg.P("$") / "") * ((var_lit^1) / keys) }
  31. local var_braced = lpeg.P { (lpeg.P("${") / "") * ((var_lit^1) / keys) * (lpeg.P("}") / "") }
  32. local template_grammar = lpeg.Cs((var + var_braced + 1)^0)
  33. return lpeg.match(template_grammar, tmpl)
  34. end
  35. exports.remove_email_aliases = function(email_addr)
  36. local function check_gmail_user(addr)
  37. -- Remove all points
  38. local no_dots_user = string.gsub(addr.user, '%.', '')
  39. local cap, pluses = string.match(no_dots_user, '^([^%+][^%+]*)(%+.*)$')
  40. if cap then
  41. return cap, rspamd_str_split(pluses, '+'), nil
  42. elseif no_dots_user ~= addr.user then
  43. return no_dots_user,{},nil
  44. end
  45. return nil
  46. end
  47. local function check_address(addr)
  48. if addr.user then
  49. local cap, pluses = string.match(addr.user, '^([^%+][^%+]*)(%+.*)$')
  50. if cap then
  51. return cap, rspamd_str_split(pluses, '+'), nil
  52. end
  53. end
  54. return nil
  55. end
  56. local function set_addr(addr, new_user, new_domain)
  57. if new_user then
  58. addr.user = new_user
  59. end
  60. if new_domain then
  61. addr.domain = new_domain
  62. end
  63. if addr.domain then
  64. addr.addr = string.format('%s@%s', addr.user, addr.domain)
  65. else
  66. addr.addr = string.format('%s@', addr.user)
  67. end
  68. if addr.name and #addr.name > 0 then
  69. addr.raw = string.format('"%s" <%s>', addr.name, addr.addr)
  70. else
  71. addr.raw = string.format('<%s>', addr.addr)
  72. end
  73. end
  74. local function check_gmail(addr)
  75. local nu, tags, nd = check_gmail_user(addr)
  76. if nu then
  77. return nu, tags, nd
  78. end
  79. return nil
  80. end
  81. local function check_googlemail(addr)
  82. local nd = 'gmail.com'
  83. local nu, tags = check_gmail_user(addr)
  84. if nu then
  85. return nu, tags, nd
  86. end
  87. return nil, nil, nd
  88. end
  89. local specific_domains = {
  90. ['gmail.com'] = check_gmail,
  91. ['googlemail.com'] = check_googlemail,
  92. }
  93. if email_addr then
  94. if email_addr.domain and specific_domains[email_addr.domain] then
  95. local nu, tags, nd = specific_domains[email_addr.domain](email_addr)
  96. if nu or nd then
  97. set_addr(email_addr, nu, nd)
  98. return nu, tags
  99. end
  100. else
  101. local nu, tags, nd = check_address(email_addr)
  102. if nu or nd then
  103. set_addr(email_addr, nu, nd)
  104. return nu, tags
  105. end
  106. end
  107. return nil
  108. end
  109. end
  110. exports.is_rspamc_or_controller = function(task)
  111. local ua = task:get_request_header('User-Agent') or ''
  112. local pwd = task:get_request_header('Password')
  113. local is_rspamc = false
  114. if tostring(ua) == 'rspamc' or pwd then is_rspamc = true end
  115. return is_rspamc
  116. end
  117. local unpack_function = table.unpack or unpack
  118. exports.unpack = function(t)
  119. return unpack_function(t)
  120. end
  121. return exports