Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

composites_internal.hxx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*-
  2. * Copyright 2021 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 RSPAMD_COMPOSITES_INTERNAL_HXX
  17. #define RSPAMD_COMPOSITES_INTERNAL_HXX
  18. #pragma once
  19. #include <string>
  20. #include "libutil/expression.h"
  21. #include "libutil/cxx/hash_util.hxx"
  22. #include "libserver/cfg_file.h"
  23. namespace rspamd::composites {
  24. /**
  25. * Subr for composite expressions
  26. */
  27. extern const struct rspamd_atom_subr composite_expr_subr;
  28. enum class rspamd_composite_policy {
  29. RSPAMD_COMPOSITE_POLICY_REMOVE_ALL = 0,
  30. RSPAMD_COMPOSITE_POLICY_REMOVE_SYMBOL,
  31. RSPAMD_COMPOSITE_POLICY_REMOVE_WEIGHT,
  32. RSPAMD_COMPOSITE_POLICY_LEAVE,
  33. RSPAMD_COMPOSITE_POLICY_UNKNOWN
  34. };
  35. /**
  36. * Static composites structure
  37. */
  38. struct rspamd_composite {
  39. std::string str_expr;
  40. std::string sym;
  41. struct rspamd_expression *expr;
  42. int id;
  43. rspamd_composite_policy policy;
  44. };
  45. #define COMPOSITE_MANAGER_FROM_PTR(ptr) (reinterpret_cast<rspamd::composites::composites_manager *>(ptr))
  46. class composites_manager {
  47. public:
  48. composites_manager(struct rspamd_config *_cfg)
  49. : cfg(_cfg)
  50. {
  51. rspamd_mempool_add_destructor(_cfg->cfg_pool, composites_manager_dtor, this);
  52. }
  53. auto size(void) const -> std::size_t
  54. {
  55. return all_composites.size();
  56. }
  57. auto find(std::string_view name) const -> const rspamd_composite *
  58. {
  59. auto found = composites.find(std::string(name));
  60. if (found != composites.end()) {
  61. return found->second.get();
  62. }
  63. return nullptr;
  64. }
  65. auto add_composite(std::string_view, const ucl_object_t *, bool silent_duplicate) -> rspamd_composite *;
  66. auto add_composite(std::string_view name, std::string_view expression, bool silent_duplicate, double score = NAN) -> rspamd_composite *;
  67. private:
  68. ~composites_manager() = default;
  69. static void composites_manager_dtor(void *ptr)
  70. {
  71. delete COMPOSITE_MANAGER_FROM_PTR(ptr);
  72. }
  73. auto new_composite(std::string_view composite_name, rspamd_expression *expr,
  74. std::string_view composite_expression) -> auto
  75. {
  76. auto &composite = all_composites.emplace_back(std::make_shared<rspamd_composite>());
  77. composite->expr = expr;
  78. composite->id = all_composites.size() - 1;
  79. composite->str_expr = composite_expression;
  80. composite->sym = composite_name;
  81. composites[composite->sym] = composite;
  82. return composite;
  83. }
  84. ankerl::unordered_dense::map<std::string,
  85. std::shared_ptr<rspamd_composite>, rspamd::smart_str_hash, rspamd::smart_str_equal>
  86. composites;
  87. /* Store all composites here, even if we have duplicates */
  88. std::vector<std::shared_ptr<rspamd_composite>> all_composites;
  89. struct rspamd_config *cfg;
  90. };
  91. }// namespace rspamd::composites
  92. #endif//RSPAMD_COMPOSITES_INTERNAL_HXX