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 7.1KB

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