diff options
Diffstat (limited to 'res/res_sorcery_config.c')
-rw-r--r-- | res/res_sorcery_config.c | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/res/res_sorcery_config.c b/res/res_sorcery_config.c index 1b43a187e..635eeef92 100644 --- a/res/res_sorcery_config.c +++ b/res/res_sorcery_config.c @@ -32,6 +32,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") +#include <regex.h> + #include "asterisk/module.h" #include "asterisk/sorcery.h" #include "asterisk/astobj2.h" @@ -70,6 +72,9 @@ struct sorcery_config_fields_cmp_params { /*! \brief Pointer to the fields to check */ const struct ast_variable *fields; + /*! \brief Regular expression for checking object id */ + regex_t *regex; + /*! \brief Optional container to put object into */ struct ao2_container *container; }; @@ -81,6 +86,7 @@ static void *sorcery_config_retrieve_id(const struct ast_sorcery *sorcery, void static void *sorcery_config_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields); static void sorcery_config_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields); +static void sorcery_config_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex); static void sorcery_config_close(void *data); static struct ast_sorcery_wizard config_object_wizard = { @@ -91,6 +97,7 @@ static struct ast_sorcery_wizard config_object_wizard = { .retrieve_id = sorcery_config_retrieve_id, .retrieve_fields = sorcery_config_retrieve_fields, .retrieve_multiple = sorcery_config_retrieve_multiple, + .retrieve_regex = sorcery_config_retrieve_regex, .close = sorcery_config_close, }; @@ -126,13 +133,19 @@ static int sorcery_config_fields_cmp(void *obj, void *arg, int flags) RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy); RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy); - /* If we can't turn the object into an object set OR if differences exist between the fields - * passed in and what are present on the object they are not a match. - */ - if (params->fields && + if (params->regex) { + /* If a regular expression has been provided see if it matches, otherwise move on */ + if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) { + ao2_link(params->container, obj); + } + return 0; + } else if (params->fields && (!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) || (ast_sorcery_changeset_create(objset, params->fields, &diff)) || diff)) { + /* If we can't turn the object into an object set OR if differences exist between the fields + * passed in and what are present on the object they are not a match. + */ return 0; } @@ -190,6 +203,25 @@ static void sorcery_config_retrieve_multiple(const struct ast_sorcery *sorcery, ao2_callback(config_objects, 0, sorcery_config_fields_cmp, ¶ms); } +static void sorcery_config_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex) +{ + struct sorcery_config *config = data; + RAII_VAR(struct ao2_container *, config_objects, ao2_global_obj_ref(config->objects), ao2_cleanup); + regex_t expression; + struct sorcery_config_fields_cmp_params params = { + .sorcery = sorcery, + .container = objects, + .regex = &expression, + }; + + if (!config_objects || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) { + return; + } + + ao2_callback(config_objects, 0, sorcery_config_fields_cmp, ¶ms); + regfree(&expression); +} + /*! \brief Internal function which determines if criteria has been met for considering an object set applicable */ static int sorcery_is_criteria_met(struct ast_variable *objset, struct ast_variable *criteria) { |