Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

scan_result.h 7.4KB

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