2013-04-27 12:01:29 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Joshua Colp <jcolp@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* \brief Sorcery Realtime Object Wizard
|
|
|
|
*
|
|
|
|
* \author Joshua Colp <jcolp@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
git migration: Refactor the ASTERISK_FILE_VERSION macro
Git does not support the ability to replace a token with a version
string during check-in. While it does have support for replacing a
token on clone, this is somewhat sub-optimal: the token is replaced
with the object hash, which is not particularly easy for human
consumption. What's more, in practice, the source file version was often
not terribly useful. Generally, when triaging bugs, the overall version
of Asterisk is far more useful than an individual SVN version of a file. As a
result, this patch removes Asterisk's support for showing source file
versions.
Specifically, it does the following:
* Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and
remove passing the version in with the macro. Other facilities
than 'core show file version' make use of the file names, such as
setting a debug level only on a specific file. As such, the act of
registering source files with the Asterisk core still has use. The
macro rename now reflects the new macro purpose.
* main/asterisk:
- Refactor the file_version structure to reflect that it no longer
tracks a version field.
- Remove the "core show file version" CLI command. Without the file
version, it is no longer useful.
- Remove the ast_file_version_find function. The file version is no
longer tracked.
- Rename ast_register_file_version/ast_unregister_file_version to
ast_register_file/ast_unregister_file, respectively.
* main/manager: Remove value from the Version key of the ModuleCheck
Action. The actual key itself has not been removed, as doing so would
absolutely constitute a backwards incompatible change. However, since
the file version is no longer tracked, there is no need to attempt to
include it in the Version key.
* UPGRADE: Add notes for:
- Modification to the ModuleCheck AMI Action
- Removal of the "core show file version" CLI command
Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
|
|
|
ASTERISK_REGISTER_FILE()
|
2013-04-27 12:01:29 +00:00
|
|
|
|
|
|
|
#include <regex.h>
|
|
|
|
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/sorcery.h"
|
|
|
|
|
|
|
|
/*! \brief They key field used to store the unique identifier for the object */
|
|
|
|
#define UUID_FIELD "id"
|
|
|
|
|
|
|
|
static void *sorcery_realtime_open(const char *data);
|
|
|
|
static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object);
|
|
|
|
static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
|
|
|
|
static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
|
|
|
|
static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
|
|
|
|
const struct ast_variable *fields);
|
|
|
|
static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
|
|
|
|
static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object);
|
|
|
|
static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object);
|
|
|
|
static void sorcery_realtime_close(void *data);
|
|
|
|
|
|
|
|
static struct ast_sorcery_wizard realtime_object_wizard = {
|
|
|
|
.name = "realtime",
|
|
|
|
.open = sorcery_realtime_open,
|
|
|
|
.create = sorcery_realtime_create,
|
|
|
|
.retrieve_id = sorcery_realtime_retrieve_id,
|
|
|
|
.retrieve_fields = sorcery_realtime_retrieve_fields,
|
|
|
|
.retrieve_multiple = sorcery_realtime_retrieve_multiple,
|
|
|
|
.retrieve_regex = sorcery_realtime_retrieve_regex,
|
|
|
|
.update = sorcery_realtime_update,
|
|
|
|
.delete = sorcery_realtime_delete,
|
|
|
|
.close = sorcery_realtime_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object)
|
|
|
|
{
|
|
|
|
const char *family = data;
|
|
|
|
RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
|
|
|
|
struct ast_variable *id = ast_variable_new(UUID_FIELD, ast_sorcery_object_get_id(object), "");
|
|
|
|
|
|
|
|
if (!fields || !id) {
|
|
|
|
ast_variables_destroy(id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Place the identifier at the front for sanity sake */
|
|
|
|
id->next = fields;
|
|
|
|
fields = id;
|
|
|
|
|
2014-03-14 18:11:55 +00:00
|
|
|
return (ast_store_realtime_fields(family, fields) <= 0) ? -1 : 0;
|
2013-04-27 12:01:29 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
/*! \brief Internal helper function which returns a filtered objectset.
|
|
|
|
*
|
|
|
|
* The following are filtered out of the objectset:
|
|
|
|
* \li The id field. This is returned to the caller in an out parameter.
|
|
|
|
* \li Fields that are not registered with sorcery.
|
|
|
|
*
|
|
|
|
* \param objectset Objectset to filter.
|
|
|
|
* \param[out] id The ID of the sorcery object, as found in the objectset.
|
|
|
|
* \param sorcery The sorcery instance that is requesting an objectset.
|
|
|
|
* \param type The object type
|
|
|
|
*
|
|
|
|
* \return The filtered objectset
|
|
|
|
*/
|
|
|
|
static struct ast_variable *sorcery_realtime_filter_objectset(struct ast_variable *objectset, struct ast_variable **id,
|
|
|
|
const struct ast_sorcery *sorcery, const char *type)
|
2013-04-27 12:01:29 +00:00
|
|
|
{
|
2014-03-07 21:23:39 +00:00
|
|
|
struct ast_variable *previous = NULL, *field = objectset;
|
|
|
|
struct ast_sorcery_object_type *object_type;
|
|
|
|
|
|
|
|
object_type = ast_sorcery_get_object_type(sorcery, type);
|
|
|
|
if (!object_type) {
|
|
|
|
ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
|
|
|
|
/* Continue since we still want to filter out the id */
|
|
|
|
}
|
|
|
|
|
|
|
|
while (field) {
|
|
|
|
int remove_field = 0;
|
|
|
|
int delete_field = 0;
|
2013-04-27 12:01:29 +00:00
|
|
|
|
|
|
|
if (!strcmp(field->name, UUID_FIELD)) {
|
|
|
|
*id = field;
|
2014-03-07 21:23:39 +00:00
|
|
|
remove_field = 1;
|
|
|
|
} else if (object_type &&
|
|
|
|
!ast_sorcery_is_object_field_registered(object_type, field->name)) {
|
|
|
|
ast_debug(1, "Filtering out realtime field '%s' from retrieval\n", field->name);
|
|
|
|
remove_field = 1;
|
|
|
|
delete_field = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_field) {
|
|
|
|
struct ast_variable *removed;
|
2013-04-27 12:01:29 +00:00
|
|
|
|
|
|
|
if (previous) {
|
|
|
|
previous->next = field->next;
|
|
|
|
} else {
|
|
|
|
objectset = field->next;
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
removed = field;
|
|
|
|
field = field->next;
|
|
|
|
removed->next = NULL;
|
|
|
|
if (delete_field) {
|
|
|
|
ast_variables_destroy(removed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
previous = field;
|
|
|
|
field = field->next;
|
2013-04-27 12:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 20:27:27 +00:00
|
|
|
ao2_cleanup(object_type);
|
2015-06-29 17:45:02 +00:00
|
|
|
|
2013-04-27 12:01:29 +00:00
|
|
|
return objectset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
|
|
|
|
{
|
|
|
|
const char *family = data;
|
|
|
|
RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
|
|
|
|
RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
|
|
|
|
void *object = NULL;
|
|
|
|
|
|
|
|
if (!(objectset = ast_load_realtime_fields(family, fields))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
|
2013-04-27 12:01:29 +00:00
|
|
|
|
2015-03-25 18:37:51 +00:00
|
|
|
if (!id
|
|
|
|
|| !(object = ast_sorcery_alloc(sorcery, type, id->value))
|
|
|
|
|| ast_sorcery_objectset_apply(sorcery, object, objectset)) {
|
2013-04-27 12:01:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_variable *, fields, ast_variable_new(UUID_FIELD, id, ""), ast_variables_destroy);
|
|
|
|
|
|
|
|
return sorcery_realtime_retrieve_fields(sorcery, data, type, fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
|
|
|
|
{
|
|
|
|
const char *family = data;
|
|
|
|
RAII_VAR(struct ast_config *, rows, NULL, ast_config_destroy);
|
|
|
|
RAII_VAR(struct ast_variable *, all, NULL, ast_variables_destroy);
|
2014-10-13 16:12:17 +00:00
|
|
|
struct ast_category *row = NULL;
|
2013-04-27 12:01:29 +00:00
|
|
|
|
|
|
|
if (!fields) {
|
|
|
|
char field[strlen(UUID_FIELD) + 6], value[2];
|
|
|
|
|
|
|
|
/* If no fields have been specified we want all rows, so trick realtime into doing it */
|
|
|
|
snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
|
|
|
|
snprintf(value, sizeof(value), "%%");
|
|
|
|
|
|
|
|
if (!(all = ast_variable_new(field, value, ""))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = all;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(rows = ast_load_realtime_multientry_fields(family, fields))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-13 16:12:17 +00:00
|
|
|
while ((row = ast_category_browse_filtered(rows, NULL, row, NULL))) {
|
|
|
|
struct ast_variable *objectset = ast_category_detach_variables(row);
|
2013-04-27 12:01:29 +00:00
|
|
|
RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
|
|
|
|
RAII_VAR(void *, object, NULL, ao2_cleanup);
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
|
2013-04-27 12:01:29 +00:00
|
|
|
|
|
|
|
if (id && (object = ast_sorcery_alloc(sorcery, type, id->value)) && !ast_sorcery_objectset_apply(sorcery, object, objectset)) {
|
|
|
|
ao2_link(objects, object);
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
ast_variables_destroy(objectset);
|
2013-04-27 12:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
|
|
|
|
{
|
|
|
|
char field[strlen(UUID_FIELD) + 6], value[strlen(regex) + 2];
|
|
|
|
RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
|
|
|
|
|
|
|
|
/* The realtime API provides no direct ability to do regex so for now we support a limited subset using pattern matching */
|
|
|
|
if (regex[0] != '^') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
|
|
|
|
snprintf(value, sizeof(value), "%s%%", regex + 1);
|
|
|
|
|
|
|
|
if (!(fields = ast_variable_new(field, value, ""))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object)
|
|
|
|
{
|
|
|
|
const char *family = data;
|
|
|
|
RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
|
|
|
|
|
|
|
|
if (!fields) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ast_update_realtime_fields(family, UUID_FIELD, ast_sorcery_object_get_id(object), fields) <= 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object)
|
|
|
|
{
|
|
|
|
const char *family = data;
|
|
|
|
|
|
|
|
return (ast_destroy_realtime_fields(family, UUID_FIELD, ast_sorcery_object_get_id(object), NULL) <= 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sorcery_realtime_open(const char *data)
|
|
|
|
{
|
|
|
|
/* We require a prefix for family string generation, or else stuff could mix together */
|
|
|
|
if (ast_strlen_zero(data) || !ast_realtime_is_mapping_defined(data)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast_strdup(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_realtime_close(void *data)
|
|
|
|
{
|
|
|
|
ast_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_module(void)
|
|
|
|
{
|
|
|
|
if (ast_sorcery_wizard_register(&realtime_object_wizard)) {
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unload_module(void)
|
|
|
|
{
|
|
|
|
ast_sorcery_wizard_unregister(&realtime_object_wizard);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Realtime Object Wizard",
|
2014-07-25 16:47:17 +00:00
|
|
|
.support_level = AST_MODULE_SUPPORT_CORE,
|
2013-04-27 12:01:29 +00:00
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
|
|
|
.load_pri = AST_MODPRI_REALTIME_DRIVER,
|
|
|
|
);
|