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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. struct rspamd_action_result {
  57. gdouble cur_limit;
  58. struct rspamd_action *action;
  59. };
  60. struct kh_rspamd_symbols_hash_s;
  61. struct kh_rspamd_symbols_group_hash_s;
  62. struct rspamd_scan_result {
  63. double score; /**< total score */
  64. double grow_factor; /**< current grow factor */
  65. struct rspamd_passthrough_result *passthrough_result;
  66. double positive_score;
  67. double negative_score;
  68. struct kh_rspamd_symbols_hash_s *symbols; /**< symbols of metric */
  69. struct kh_rspamd_symbols_group_hash_s *sym_groups; /**< groups of symbols */
  70. struct rspamd_action_result *actions_limits;
  71. const gchar *name; /**< for named results, NULL is the default result */
  72. struct rspamd_task *task; /**< back reference */
  73. gint symbol_cbref; /**< lua function that defines if a symbol can be inserted, -1 if unused */
  74. guint nactions;
  75. guint npositive;
  76. guint nnegative;
  77. guint nresults; /**< all results: positive, negative, passthrough etc */
  78. guint nresults_postfilters; /**< how many results are there before postfilters stage */
  79. struct rspamd_scan_result *prev, *next; /**< double linked list of results */
  80. };
  81. /**
  82. * Create or return existing result for the specified metric name
  83. * @param task task object
  84. * @return metric result or NULL if metric `name` has not been found
  85. */
  86. struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task,
  87. const gchar *name, gint lua_sym_cbref);
  88. /**
  89. * Find result with a specific name (NULL means the default result)
  90. * @param task
  91. * @param name
  92. * @return
  93. */
  94. struct rspamd_scan_result *rspamd_find_metric_result (struct rspamd_task *task,
  95. const gchar *name);
  96. /**
  97. * Adds a new passthrough result to a task
  98. * @param task
  99. * @param action
  100. * @param priority
  101. * @param target_score
  102. * @param message
  103. * @param module
  104. */
  105. void rspamd_add_passthrough_result (struct rspamd_task *task,
  106. struct rspamd_action *action, guint priority,
  107. double target_score, const gchar *message,
  108. const gchar *module, guint flags,
  109. struct rspamd_scan_result *scan_result);
  110. enum rspamd_symbol_insert_flags {
  111. RSPAMD_SYMBOL_INSERT_DEFAULT = 0,
  112. RSPAMD_SYMBOL_INSERT_SINGLE = (1 << 0),
  113. RSPAMD_SYMBOL_INSERT_ENFORCE = (1 << 1),
  114. };
  115. /**
  116. * Insert a result to task
  117. * @param task worker's task that present message from user
  118. * @param metric_name metric's name to which we need to insert result
  119. * @param symbol symbol to insert
  120. * @param weight numeric weight for symbol
  121. * @param opts list of symbol's options
  122. */
  123. struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task *task,
  124. const gchar *symbol,
  125. double weight,
  126. const gchar *opts,
  127. enum rspamd_symbol_insert_flags flags,
  128. struct rspamd_scan_result *result);
  129. #define rspamd_task_insert_result_single(task, symbol, weight, opts) \
  130. rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_SINGLE, NULL)
  131. #define rspamd_task_insert_result(task, symbol, weight, opts) \
  132. rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_DEFAULT, NULL)
  133. /**
  134. * Removes a symbol from a specific symbol result
  135. * @param task
  136. * @param symbol
  137. * @param result
  138. * @return
  139. */
  140. struct rspamd_symbol_result* rspamd_task_remove_symbol_result (
  141. struct rspamd_task *task,
  142. const gchar *symbol,
  143. struct rspamd_scan_result *result);
  144. /**
  145. * Adds new option to symbol
  146. * @param task
  147. * @param s
  148. * @param opt
  149. */
  150. gboolean rspamd_task_add_result_option (struct rspamd_task *task,
  151. struct rspamd_symbol_result *s,
  152. const gchar *opt,
  153. gsize vlen);
  154. /**
  155. * Finds symbol result
  156. * @param task
  157. * @param sym
  158. * @return
  159. */
  160. struct rspamd_symbol_result *
  161. rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym,
  162. struct rspamd_scan_result *result);
  163. /**
  164. * Compatibility function to iterate on symbols hash
  165. * @param task
  166. * @param func
  167. * @param ud
  168. */
  169. void rspamd_task_symbol_result_foreach (struct rspamd_task *task,
  170. struct rspamd_scan_result *result,
  171. GHFunc func,
  172. gpointer ud);
  173. /**
  174. * Default consolidation function for metric, it get all symbols and multiply symbol
  175. * weight by some factor that is specified in config. Default factor is 1.
  176. * @param task worker's task that present message from user
  177. * @param metric_name name of metric
  178. * @return result metric weight
  179. */
  180. double rspamd_factor_consolidation_func (struct rspamd_task *task,
  181. const gchar *metric_name,
  182. const gchar *unused);
  183. /**
  184. * Check thresholds and return action for a task
  185. * @param task
  186. * @return
  187. */
  188. struct rspamd_action *rspamd_check_action_metric (struct rspamd_task *task,
  189. struct rspamd_passthrough_result **ppr,
  190. struct rspamd_scan_result *scan_result);
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif