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

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