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.

scan_result.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @file scan_result.h
  3. * Scan result holder
  4. */
  5. #ifndef RSPAMD_SCAN_RESULT_H
  6. #define RSPAMD_SCAN_RESULT_H
  7. #include "config.h"
  8. #include "rspamd_symcache.h"
  9. #include "task.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. struct rspamd_task;
  14. struct rspamd_settings;
  15. struct rspamd_classifier_config;
  16. struct rspamd_symbol_option {
  17. gchar *option;
  18. gsize optlen;
  19. struct rspamd_symbol_option *prev, *next;
  20. };
  21. enum rspamd_symbol_result_flags {
  22. RSPAMD_SYMBOL_RESULT_NORMAL = 0,
  23. RSPAMD_SYMBOL_RESULT_IGNORED = (1 << 0)
  24. };
  25. struct kh_rspamd_options_hash_s;
  26. /**
  27. * Rspamd symbol
  28. */
  29. struct rspamd_symbol_result {
  30. double score; /**< symbol's score */
  31. struct kh_rspamd_options_hash_s *options; /**< list of symbol's options */
  32. struct rspamd_symbol_option *opts_head; /**< head of linked list of options */
  33. const gchar *name;
  34. struct rspamd_symbol *sym; /**< symbol configuration */
  35. gssize opts_len; /**< total size of all options (negative if truncated option is added) */
  36. guint nshots;
  37. int flags;
  38. struct rspamd_symbol_result *next;
  39. };
  40. #define RSPAMD_PASSTHROUGH_NORMAL 1
  41. #define RSPAMD_PASSTHROUGH_LOW 0
  42. #define RSPAMD_PASSTHROUGH_HIGH 2
  43. #define RSPAMD_PASSTHROUGH_CRITICAL 3
  44. #define RSPAMD_PASSTHROUGH_LEAST (1u << 0u)
  45. #define RSPAMD_PASSTHROUGH_NO_SMTP_MESSAGE (1u << 1u)
  46. #define RSPAMD_PASSTHROUGH_PROCESS_ALL (1u << 2u)
  47. struct rspamd_passthrough_result {
  48. struct rspamd_action *action;
  49. guint priority;
  50. guint flags;
  51. double target_score;
  52. const gchar *message;
  53. const gchar *module;
  54. struct rspamd_passthrough_result *prev, *next;
  55. };
  56. enum rspamd_action_config_flags {
  57. RSPAMD_ACTION_RESULT_DEFAULT = 0,
  58. RSPAMD_ACTION_RESULT_NO_THRESHOLD = (1u << 0u),
  59. RSPAMD_ACTION_RESULT_DISABLED = (1u << 1u),
  60. };
  61. struct rspamd_action_config {
  62. gdouble cur_limit;
  63. int flags;
  64. struct rspamd_action *action;
  65. };
  66. struct kh_rspamd_symbols_hash_s;
  67. struct kh_rspamd_symbols_group_hash_s;
  68. struct rspamd_scan_result {
  69. double score; /**< total score */
  70. double grow_factor; /**< current grow factor */
  71. struct rspamd_passthrough_result *passthrough_result;
  72. double positive_score;
  73. double negative_score;
  74. struct kh_rspamd_symbols_hash_s *symbols; /**< symbols of metric */
  75. struct kh_rspamd_symbols_group_hash_s *sym_groups; /**< groups of symbols */
  76. struct rspamd_action_config *actions_config;
  77. const gchar *name; /**< for named results, NULL is the default result */
  78. struct rspamd_task *task; /**< back reference */
  79. gint symbol_cbref; /**< lua function that defines if a symbol can be inserted, -1 if unused */
  80. guint nactions;
  81. guint npositive;
  82. guint nnegative;
  83. guint nresults; /**< all results: positive, negative, passthrough etc */
  84. guint nresults_postfilters; /**< how many results are there before postfilters stage */
  85. struct rspamd_scan_result *prev, *next; /**< double linked list of results */
  86. };
  87. /**
  88. * Create or return existing result for the specified metric name
  89. * @param task task object
  90. * @return metric result or NULL if metric `name` has not been found
  91. */
  92. struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task,
  93. const gchar *name, gint lua_sym_cbref);
  94. /**
  95. * Find result with a specific name (NULL means the default result)
  96. * @param task
  97. * @param name
  98. * @return
  99. */
  100. struct rspamd_scan_result *rspamd_find_metric_result (struct rspamd_task *task,
  101. const gchar *name);
  102. /**
  103. * Adds a new passthrough result to a task
  104. * @param task
  105. * @param action
  106. * @param priority
  107. * @param target_score
  108. * @param message
  109. * @param module
  110. */
  111. bool rspamd_add_passthrough_result (struct rspamd_task *task,
  112. struct rspamd_action *action, guint priority,
  113. double target_score, const gchar *message,
  114. const gchar *module, guint flags,
  115. struct rspamd_scan_result *scan_result);
  116. enum rspamd_symbol_insert_flags {
  117. RSPAMD_SYMBOL_INSERT_DEFAULT = 0,
  118. RSPAMD_SYMBOL_INSERT_SINGLE = (1 << 0),
  119. RSPAMD_SYMBOL_INSERT_ENFORCE = (1 << 1),
  120. };
  121. /**
  122. * Insert a result to task
  123. * @param task worker's task that present message from user
  124. * @param metric_name metric's name to which we need to insert result
  125. * @param symbol symbol to insert
  126. * @param weight numeric weight for symbol
  127. * @param opts list of symbol's options
  128. */
  129. struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task *task,
  130. const gchar *symbol,
  131. double weight,
  132. const gchar *opts,
  133. enum rspamd_symbol_insert_flags flags,
  134. struct rspamd_scan_result *result);
  135. #define rspamd_task_insert_result_single(task, symbol, weight, opts) \
  136. rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_SINGLE, NULL)
  137. #define rspamd_task_insert_result(task, symbol, weight, opts) \
  138. rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_DEFAULT, NULL)
  139. /**
  140. * Removes a symbol from a specific symbol result
  141. * @param task
  142. * @param symbol
  143. * @param result
  144. * @return
  145. */
  146. struct rspamd_symbol_result* rspamd_task_remove_symbol_result (
  147. struct rspamd_task *task,
  148. const gchar *symbol,
  149. struct rspamd_scan_result *result);
  150. /**
  151. * Adds new option to symbol
  152. * @param task
  153. * @param s
  154. * @param opt
  155. */
  156. gboolean rspamd_task_add_result_option (struct rspamd_task *task,
  157. struct rspamd_symbol_result *s,
  158. const gchar *opt,
  159. gsize vlen);
  160. /**
  161. * Finds symbol result
  162. * @param task
  163. * @param sym
  164. * @return
  165. */
  166. struct rspamd_symbol_result *
  167. rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym,
  168. struct rspamd_scan_result *result);
  169. /**
  170. * Compatibility function to iterate on symbols hash
  171. * @param task
  172. * @param func
  173. * @param ud
  174. */
  175. void rspamd_task_symbol_result_foreach (struct rspamd_task *task,
  176. struct rspamd_scan_result *result,
  177. GHFunc func,
  178. gpointer ud);
  179. /**
  180. * Default consolidation function for metric, it get all symbols and multiply symbol
  181. * weight by some factor that is specified in config. Default factor is 1.
  182. * @param task worker's task that present message from user
  183. * @param metric_name name of metric
  184. * @return result metric weight
  185. */
  186. double rspamd_factor_consolidation_func (struct rspamd_task *task,
  187. const gchar *metric_name,
  188. const gchar *unused);
  189. /**
  190. * Check thresholds and return action for a task
  191. * @param task
  192. * @return
  193. */
  194. struct rspamd_action *rspamd_check_action_metric (struct rspamd_task *task,
  195. struct rspamd_passthrough_result **ppr,
  196. struct rspamd_scan_result *scan_result);
  197. struct rspamd_action_config *rspamd_find_action_config_for_action (struct rspamd_scan_result *scan_result,
  198. struct rspamd_action *act);
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif