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.

filter.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file filter.h
  3. * Filters logic implementation
  4. */
  5. #ifndef RSPAMD_FILTER_H
  6. #define RSPAMD_FILTER_H
  7. #include "config.h"
  8. #include "rspamd_symcache.h"
  9. #include "task.h"
  10. struct rspamd_task;
  11. struct rspamd_settings;
  12. struct rspamd_classifier_config;
  13. struct rspamd_symbol_option {
  14. gchar *option;
  15. struct rspamd_symbol_option *prev, *next;
  16. };
  17. enum rspamd_symbol_result_flags {
  18. RSPAMD_SYMBOL_RESULT_NORMAL = 0,
  19. RSPAMD_SYMBOL_RESULT_IGNORED = (1 << 0)
  20. };
  21. struct kh_rspamd_options_hash_s;
  22. /**
  23. * Rspamd symbol
  24. */
  25. struct rspamd_symbol_result {
  26. double score; /**< symbol's score */
  27. struct kh_rspamd_options_hash_s *options; /**< list of symbol's options */
  28. struct rspamd_symbol_option *opts_head; /**< head of linked list of options */
  29. const gchar *name;
  30. struct rspamd_symbol *sym; /**< symbol configuration */
  31. guint nshots;
  32. enum rspamd_symbol_result_flags flags;
  33. };
  34. #define RSPAMD_PASSTHROUGH_NORMAL 1
  35. #define RSPAMD_PASSTHROUGH_LOW 0
  36. #define RSPAMD_PASSTHROUGH_HIGH 2
  37. #define RSPAMD_PASSTHROUGH_CRITICAL 3
  38. struct rspamd_passthrough_result {
  39. struct rspamd_action *action;
  40. guint priority;
  41. double target_score;
  42. const gchar *message;
  43. const gchar *module;
  44. struct rspamd_passthrough_result *prev, *next;
  45. };
  46. struct rspamd_action_result {
  47. gdouble cur_limit;
  48. struct rspamd_action *action;
  49. };
  50. struct kh_rspamd_symbols_hash_s;
  51. struct kh_rspamd_symbols_group_hash_s;
  52. struct rspamd_metric_result {
  53. double score; /**< total score */
  54. double grow_factor; /**< current grow factor */
  55. struct rspamd_passthrough_result *passthrough_result;
  56. guint npositive;
  57. guint nnegative;
  58. double positive_score;
  59. double negative_score;
  60. struct kh_rspamd_symbols_hash_s *symbols; /**< symbols of metric */
  61. struct kh_rspamd_symbols_group_hash_s *sym_groups; /**< groups of symbols */
  62. struct rspamd_action_result *actions_limits;
  63. guint nactions;
  64. };
  65. /**
  66. * Create or return existing result for the specified metric name
  67. * @param task task object
  68. * @return metric result or NULL if metric `name` has not been found
  69. */
  70. struct rspamd_metric_result * rspamd_create_metric_result (struct rspamd_task *task);
  71. /**
  72. * Adds a new passthrough result to a task
  73. * @param task
  74. * @param action
  75. * @param priority
  76. * @param target_score
  77. * @param message
  78. * @param module
  79. */
  80. void rspamd_add_passthrough_result (struct rspamd_task *task,
  81. struct rspamd_action *action,
  82. guint priority,
  83. double target_score,
  84. const gchar *message,
  85. const gchar *module);
  86. enum rspamd_symbol_insert_flags {
  87. RSPAMD_SYMBOL_INSERT_DEFAULT = 0,
  88. RSPAMD_SYMBOL_INSERT_SINGLE = (1 << 0),
  89. RSPAMD_SYMBOL_INSERT_ENFORCE = (1 << 1),
  90. };
  91. /**
  92. * Insert a result to task
  93. * @param task worker's task that present message from user
  94. * @param metric_name metric's name to which we need to insert result
  95. * @param symbol symbol to insert
  96. * @param weight numeric weight for symbol
  97. * @param opts list of symbol's options
  98. */
  99. struct rspamd_symbol_result* rspamd_task_insert_result_full (struct rspamd_task *task,
  100. const gchar *symbol,
  101. double weight,
  102. const gchar *opts,
  103. enum rspamd_symbol_insert_flags flags);
  104. #define rspamd_task_insert_result_single(task, symbol, flag, opts) \
  105. rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_SINGLE)
  106. #define rspamd_task_insert_result(task, symbol, flag, opts) \
  107. rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT)
  108. /**
  109. * Adds new option to symbol
  110. * @param task
  111. * @param s
  112. * @param opt
  113. */
  114. gboolean rspamd_task_add_result_option (struct rspamd_task *task,
  115. struct rspamd_symbol_result *s, const gchar *opt);
  116. /**
  117. * Finds symbol result
  118. * @param task
  119. * @param sym
  120. * @return
  121. */
  122. struct rspamd_symbol_result* rspamd_task_find_symbol_result (
  123. struct rspamd_task *task, const char *sym);
  124. /**
  125. * Compatibility function to iterate on symbols hash
  126. * @param task
  127. * @param func
  128. * @param ud
  129. */
  130. void rspamd_task_symbol_result_foreach (struct rspamd_task *task,
  131. GHFunc func,
  132. gpointer ud);
  133. /**
  134. * Default consolidation function for metric, it get all symbols and multiply symbol
  135. * weight by some factor that is specified in config. Default factor is 1.
  136. * @param task worker's task that present message from user
  137. * @param metric_name name of metric
  138. * @return result metric weight
  139. */
  140. double rspamd_factor_consolidation_func (struct rspamd_task *task,
  141. const gchar *metric_name,
  142. const gchar *unused);
  143. /**
  144. * Check thresholds and return action for a task
  145. * @param task
  146. * @return
  147. */
  148. struct rspamd_action* rspamd_check_action_metric (struct rspamd_task *task);
  149. #endif