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.

symbols_cache.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*-
  2. * Copyright 2016 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_SYMBOLS_CACHE_H
  17. #define RSPAMD_SYMBOLS_CACHE_H
  18. #include "config.h"
  19. #include "ucl.h"
  20. #include <lua.h>
  21. #include <event.h>
  22. struct rspamd_task;
  23. struct rspamd_config;
  24. struct symbols_cache;
  25. typedef void (*symbol_func_t)(struct rspamd_task *task, gpointer user_data);
  26. enum rspamd_symbol_type {
  27. SYMBOL_TYPE_NORMAL = (1 << 0),
  28. SYMBOL_TYPE_VIRTUAL = (1 << 1),
  29. SYMBOL_TYPE_CALLBACK = (1 << 2),
  30. SYMBOL_TYPE_GHOST = (1 << 3),
  31. SYMBOL_TYPE_SKIPPED = (1 << 4),
  32. SYMBOL_TYPE_COMPOSITE = (1 << 5),
  33. SYMBOL_TYPE_CLASSIFIER = (1 << 6),
  34. SYMBOL_TYPE_FINE = (1 << 7)
  35. };
  36. /**
  37. * Creates new cache structure
  38. * @return
  39. */
  40. struct symbols_cache* rspamd_symbols_cache_new (struct rspamd_config *cfg);
  41. /**
  42. * Remove the cache structure syncing data if needed
  43. * @param cache
  44. */
  45. void rspamd_symbols_cache_destroy (struct symbols_cache *cache);
  46. /**
  47. * Load symbols cache from file, must be called _after_ init_symbols_cache
  48. */
  49. gboolean rspamd_symbols_cache_init (struct symbols_cache* cache);
  50. /**
  51. * Generic function to register a symbol
  52. * @param cache
  53. * @param name
  54. * @param weight
  55. * @param priority
  56. * @param func
  57. * @param user_data
  58. * @param type
  59. * @param parent
  60. */
  61. gint rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
  62. const gchar *name,
  63. gint priority,
  64. symbol_func_t func,
  65. gpointer user_data,
  66. enum rspamd_symbol_type type,
  67. gint parent);
  68. /**
  69. * Add condition to the specific symbol in cache
  70. * @param cache
  71. * @param id id of symbol
  72. * @param L lua state pointer
  73. * @param cbref callback reference (returned by luaL_ref)
  74. * @return TRUE if condition has been added
  75. */
  76. gboolean rspamd_symbols_cache_add_condition (struct symbols_cache *cache,
  77. gint id, lua_State *L, gint cbref);
  78. /**
  79. * Add delayed condition to the specific symbol in cache. So symbol can be absent
  80. * to the moment of addition
  81. * @param cache
  82. * @param id id of symbol
  83. * @param L lua state pointer
  84. * @param cbref callback reference (returned by luaL_ref)
  85. * @return TRUE if condition has been added
  86. */
  87. gboolean rspamd_symbols_cache_add_condition_delayed (struct symbols_cache *cache,
  88. const gchar *sym, lua_State *L, gint cbref);
  89. /**
  90. * Find symbol in cache by id and returns its id resolving virtual symbols if
  91. * applicable
  92. * @param cache
  93. * @param name
  94. * @return id of symbol or (-1) if a symbol has not been found
  95. */
  96. gint rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name);
  97. /**
  98. * Call function for cached symbol using saved callback
  99. * @param task task object
  100. * @param cache symbols cache
  101. * @param saved_item pointer to currently saved item
  102. */
  103. gboolean rspamd_symbols_cache_process_symbols (struct rspamd_task *task,
  104. struct symbols_cache *cache);
  105. /**
  106. * Validate cache items agains theirs weights defined in metrics
  107. * @param cache symbols cache
  108. * @param cfg configuration
  109. * @param strict do strict checks - symbols MUST be described in metrics
  110. */
  111. gboolean rspamd_symbols_cache_validate (struct symbols_cache *cache,
  112. struct rspamd_config *cfg,
  113. gboolean strict);
  114. /**
  115. * Return statistics about the cache as ucl object (array of objects one per item)
  116. * @param cache
  117. * @return
  118. */
  119. ucl_object_t *rspamd_symbols_cache_counters (struct symbols_cache * cache);
  120. /**
  121. * Start cache reloading
  122. * @param cache
  123. * @param ev_base
  124. */
  125. void rspamd_symbols_cache_start_refresh (struct symbols_cache * cache,
  126. struct event_base *ev_base);
  127. /**
  128. * Increases counter for a specific symbol
  129. * @param cache
  130. * @param symbol
  131. */
  132. void rspamd_symbols_cache_inc_frequency (struct symbols_cache *cache,
  133. const gchar *symbol);
  134. /**
  135. * Add dependency relation between two symbols identified by id (source) and
  136. * a symbolic name (destination). Destination could be virtual or real symbol.
  137. * Callback destinations are not yet supported.
  138. * @param id_from source symbol
  139. * @param to destination name
  140. */
  141. void rspamd_symbols_cache_add_dependency (struct symbols_cache *cache,
  142. gint id_from, const gchar *to);
  143. /**
  144. * Add delayed dependency that is resolved on cache post-load routine
  145. * @param cache
  146. * @param from
  147. * @param to
  148. */
  149. void rspamd_symbols_cache_add_delayed_dependency (struct symbols_cache *cache,
  150. const gchar *from, const gchar *to);
  151. #endif