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_fuzzy.lua 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. --[[
  2. Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. --[[[
  14. -- @module lua_fuzzy
  15. -- This module contains helper functions for supporting fuzzy check module
  16. --]]
  17. local N = "lua_fuzzy"
  18. local lua_util = require "lua_util"
  19. local rspamd_logger = require "rspamd_logger"
  20. local ts = require("tableshape").types
  21. -- Filled by C code, indexed by number in this table
  22. local rules = {}
  23. -- Pre-defined rules options
  24. local policies = {
  25. recommended = {
  26. min_bytes = 1024,
  27. min_height = 500,
  28. min_width = 500,
  29. min_length = 100, -- short words multiplier
  30. text_multiplier = 4.0, -- divide min_bytes by 4 for texts
  31. mime_types = {"*"},
  32. short_text_direct_hash = true,
  33. }
  34. }
  35. local default_policy = policies.recommended
  36. local policy_schema = ts.shape{
  37. min_bytes = ts.number + ts.string / tonumber,
  38. min_height = ts.number + ts.string / tonumber,
  39. min_width = ts.number + ts.string / tonumber,
  40. min_length = ts.number + ts.string / tonumber,
  41. text_multiplier = ts.number,
  42. mime_types = ts.array_of(ts.string),
  43. short_text_direct_hash = ts.bool,
  44. }
  45. local exports = {}
  46. --[[[
  47. -- @function lua_fuzzy.register_policy(name, policy)
  48. -- Adds a new policy with name `name`. Must be valid, checked using policy_schema
  49. --]]
  50. exports.register_policy = function(name, policy)
  51. if policies[name] then
  52. rspamd_logger.warnx(rspamd_config, "overriding policy %s", name)
  53. end
  54. local parsed_policy,err = policy_schema:transform(policy)
  55. if not parsed_policy then
  56. rspamd_logger.errx(rspamd_config, 'invalid fuzzy rule policy %s: %s',
  57. name, err)
  58. return
  59. else
  60. policies.name = parsed_policy
  61. end
  62. end
  63. --[[[
  64. -- @function lua_fuzzy.process_rule(rule)
  65. -- Processes fuzzy rule (applying policies or defaults if needed). Returns policy id
  66. --]]
  67. exports.process_rule = function(rule)
  68. local processed_rule = lua_util.shallowcopy(rule)
  69. local policy = default_policy
  70. if processed_rule.policy then
  71. policy = policies[processed_rule.policy]
  72. if policy then
  73. processed_rule = lua_util.override_defaults(policy, processed_rule)
  74. else
  75. rspamd_logger.warnx(rspamd_config, "unknown policy %s", processed_rule.policy)
  76. end
  77. end
  78. table.insert(rules, processed_rule)
  79. return #rules
  80. end
  81. return exports