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.

expression.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SRC_LIBUTIL_EXPRESSION_H_
  17. #define SRC_LIBUTIL_EXPRESSION_H_
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "fstring.h"
  21. #define RSPAMD_EXPRESSION_MAX_PRIORITY 1024
  22. #define RSPAMD_EXPRESSION_FLAG_NOOPT (1 << 0)
  23. typedef struct rspamd_expression_atom_s {
  24. /* Opaque userdata */
  25. gpointer data;
  26. /* String representation of atom */
  27. const gchar *str;
  28. /* Length of the string representation of atom */
  29. gsize len;
  30. /* Relative priority */
  31. gint priority;
  32. /* Average execution time (in ticks) */
  33. gdouble avg_ticks;
  34. /* Amount of positive triggers */
  35. guint hits;
  36. } rspamd_expression_atom_t;
  37. struct rspamd_atom_subr {
  38. /* Parses atom from string and returns atom structure */
  39. rspamd_expression_atom_t * (*parse)(const gchar *line, gsize len,
  40. rspamd_mempool_t *pool, gpointer ud, GError **err);
  41. /* Process atom via the opaque pointer (e.g. struct rspamd_task *) */
  42. gint (*process) (gpointer input, rspamd_expression_atom_t *atom);
  43. /* Calculates the relative priority of the expression */
  44. gint (*priority) (rspamd_expression_atom_t *atom);
  45. void (*destroy) (rspamd_expression_atom_t *atom);
  46. };
  47. /* Opaque structure */
  48. struct rspamd_expression;
  49. /**
  50. * Parse symbolic expression and create the expression using the specified subroutines for atoms processing
  51. * @param line line to parse
  52. * @param len length of the line (if 0 then line should be NULL terminated)
  53. * @param subr subroutines for atoms parsing
  54. * @param subr_data opaque dat pointer
  55. * @param pool pool to use for memory allocations
  56. * @param err error pointer
  57. * @param target the target expression
  58. * @return TRUE if an expression have been parsed
  59. */
  60. gboolean rspamd_parse_expression (const gchar *line, gsize len,
  61. const struct rspamd_atom_subr *subr, gpointer subr_data,
  62. rspamd_mempool_t *pool, GError **err,
  63. struct rspamd_expression **target);
  64. /**
  65. * Process the expression and return its value using atom 'process' functions with the specified data pointer
  66. * @param expr expression to process
  67. * @param data opaque data pointer for all the atoms
  68. * @return the value of expression
  69. */
  70. gint rspamd_process_expression (struct rspamd_expression *expr, gint flags,
  71. gpointer data);
  72. /**
  73. * Shows string representation of an expression
  74. * @param expr expression to show
  75. * @return freshly allocated string with expression
  76. */
  77. GString *rspamd_expression_tostring (struct rspamd_expression *expr);
  78. /**
  79. * Callback that is called on @see rspamd_expression_atom_foreach, atom is ephemeral
  80. * and should not be modified within callback
  81. */
  82. typedef void (*rspamd_expression_atom_foreach_cb) (const rspamd_ftok_t *atom,
  83. gpointer ud);
  84. /**
  85. * Traverse over all atoms in the expression
  86. * @param expr expression
  87. * @param cb callback to be called
  88. * @param ud opaque data passed to `cb`
  89. */
  90. void rspamd_expression_atom_foreach (struct rspamd_expression *expr,
  91. rspamd_expression_atom_foreach_cb cb, gpointer cbdata);
  92. #endif /* SRC_LIBUTIL_EXPRESSION_H_ */