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.

classifiers.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef CLASSIFIERS_H
  17. #define CLASSIFIERS_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "contrib/libev/ev.h"
  21. #define RSPAMD_DEFAULT_CLASSIFIER "bayes"
  22. /* Consider this value as 0 */
  23. #define ALPHA 0.0001
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. struct rspamd_classifier_config;
  28. struct rspamd_task;
  29. struct rspamd_config;
  30. struct rspamd_classifier;
  31. struct token_node_s;
  32. struct rspamd_stat_classifier {
  33. char *name;
  34. gboolean (*init_func)(struct rspamd_config *cfg,
  35. struct ev_loop *ev_base,
  36. struct rspamd_classifier *cl);
  37. gboolean (*classify_func)(struct rspamd_classifier *ctx,
  38. GPtrArray *tokens,
  39. struct rspamd_task *task);
  40. gboolean (*learn_spam_func)(struct rspamd_classifier *ctx,
  41. GPtrArray *input,
  42. struct rspamd_task *task,
  43. gboolean is_spam,
  44. gboolean unlearn,
  45. GError **err);
  46. void (*fin_func)(struct rspamd_classifier *cl);
  47. };
  48. /* Bayes algorithm */
  49. gboolean bayes_init(struct rspamd_config *cfg,
  50. struct ev_loop *ev_base,
  51. struct rspamd_classifier *);
  52. gboolean bayes_classify(struct rspamd_classifier *ctx,
  53. GPtrArray *tokens,
  54. struct rspamd_task *task);
  55. gboolean bayes_learn_spam(struct rspamd_classifier *ctx,
  56. GPtrArray *tokens,
  57. struct rspamd_task *task,
  58. gboolean is_spam,
  59. gboolean unlearn,
  60. GError **err);
  61. void bayes_fin(struct rspamd_classifier *);
  62. /* Generic lua classifier */
  63. gboolean lua_classifier_init(struct rspamd_config *cfg,
  64. struct ev_loop *ev_base,
  65. struct rspamd_classifier *);
  66. gboolean lua_classifier_classify(struct rspamd_classifier *ctx,
  67. GPtrArray *tokens,
  68. struct rspamd_task *task);
  69. gboolean lua_classifier_learn_spam(struct rspamd_classifier *ctx,
  70. GPtrArray *tokens,
  71. struct rspamd_task *task,
  72. gboolean is_spam,
  73. gboolean unlearn,
  74. GError **err);
  75. extern int rspamd_bayes_log_id;
  76. #define msg_debug_bayes(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \
  77. rspamd_bayes_log_id, "bayes", task->task_pool->tag.uid, \
  78. G_STRFUNC, \
  79. __VA_ARGS__)
  80. #define msg_debug_bayes_cfg(...) rspamd_conditional_debug_fast(NULL, NULL, \
  81. rspamd_bayes_log_id, "bayes", cfg->cfg_pool->tag.uid, \
  82. G_STRFUNC, \
  83. __VA_ARGS__)
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif