summaryrefslogtreecommitdiff
path: root/funcs
diff options
context:
space:
mode:
authorGeorge Joseph <george.joseph@fairview5.com>2014-03-06 22:39:54 +0000
committerGeorge Joseph <george.joseph@fairview5.com>2014-03-06 22:39:54 +0000
commita4906e9f867cf6db946fc273d5f83cd7503e5bb8 (patch)
treeadbd83f29fb3eb5c2bb6f9defb585fdfc287b60c /funcs
parentf0b8590c14d9f5b899f5edc1158ee90200ace6dc (diff)
sorcery: Create AST_SORCERY dialplan function.
This patch creates the AST_SORCERY dialplan function which allows someone to retrieve any value from a sorcery-based config file. It's similar to AST_CONFIG. The creation of the function itself was fairly straightforward but it required changes to the underlying sorcery infrastructure that rippled into individual sorcery objects. The changes stemmed from inconsistencies in how sorcery created ast_variable objectsets from sorcery objects and the inconsistency in how individual objects used that feature especially when it came to parameters that can be specified multiple times like contact in aor and match in identify. You can read more here... http://lists.digium.com/pipermail/asterisk-dev/2014-February/065202.html So, what this patch does, besides actually creating the AST_SORCERY function, is the following... * Creates ast_variable_list_append which is a helper to append one ast_variable list to another. * Modifies the ast_sorcery_object_field_register functions to accept the already-defined sorcery_fields_handler callback. * Modifies ast_sorcery_objectset_create to accept a parameter indicating return type preference...a single ast_variable with all values concatenated or an ast_variable list with multiple entries. Also fixed a few bugs. * Modifies individual sorcery object implementations to use the new function definition of the ast_sorcery_object_field_register functions. * Modifies location.c and res_pjsip_endpoint_identifier_ip.c to implement sorcery_fields_handler handlers so they return multiple occurrences as an ast_variable_list. * Added a whole bunch of tests to test_sorcery. (closes issue ASTERISK-22537) Review: http://reviewboard.asterisk.org/r/3254/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@410042 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'funcs')
-rw-r--r--funcs/func_sorcery.c221
1 files changed, 221 insertions, 0 deletions
diff --git a/funcs/func_sorcery.c b/funcs/func_sorcery.c
new file mode 100644
index 000000000..1671b3f05
--- /dev/null
+++ b/funcs/func_sorcery.c
@@ -0,0 +1,221 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.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 Get a field from a sorcery object
+ *
+ * \author \verbatim George Joseph <george.joseph@fairview5.com> \endverbatim
+ *
+ * \ingroup functions
+ *
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "")
+
+#include "asterisk/app.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+
+/*** DOCUMENTATION
+ <function name="AST_SORCERY" language="en_US">
+ <synopsis>
+ Get a field from a sorcery object
+ </synopsis>
+ <syntax>
+ <parameter name="module_name" required="true">
+ <para>The name of the module owning the sorcery instance.</para>
+ </parameter>
+ <parameter name="object_type" required="true">
+ <para>The type of object to query.</para>
+ </parameter>
+ <parameter name="object_id" required="true">
+ <para>The id of the object to query.</para>
+ </parameter>
+ <parameter name="field_name" required="true">
+ <para>The name of the field.</para>
+ </parameter>
+ <parameter name="retrieval_method" required="false">
+ <para>Fields that have multiple occurrences may be retrieved in two ways.</para>
+ <enumlist>
+ <enum name="concat"><para>Returns all matching fields concatenated
+ in a single string separated by <replaceable>separator</replaceable>
+ which defaults to <literal>,</literal>.</para></enum>
+
+ <enum name="single"><para>Returns the nth occurrence of the field
+ as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
+ </para></enum>
+ </enumlist>
+ <para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
+ </parameter>
+ <parameter name="retrieval_details" required="false">
+ <para>Specifies either the separator for <literal>concat</literal>
+ or the occurrence number for <literal>single</literal>.</para>
+ </parameter>
+ </syntax>
+ </function>
+***/
+
+static int sorcery_function_read(struct ast_channel *chan,
+ const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+ char *parsed_data = ast_strdupa(data);
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+ RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
+ struct ast_variable *change_set;
+ struct ast_variable *it_change_set;
+ int found, field_number = 1, ix, method;
+ char *separator = ",";
+
+ enum methods {
+ CONCAT,
+ SINGLE,
+ };
+
+ AST_DECLARE_APP_ARGS(args,
+ AST_APP_ARG(module_name);
+ AST_APP_ARG(object_type);
+ AST_APP_ARG(object_id);
+ AST_APP_ARG(field_name);
+ AST_APP_ARG(method);
+ AST_APP_ARG(method_arg);
+ );
+
+ /* Check for zero arguments */
+ if (ast_strlen_zero(parsed_data)) {
+ ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
+ return -1;
+ }
+
+ AST_STANDARD_APP_ARGS(args, parsed_data);
+
+ if (ast_strlen_zero(args.module_name)) {
+ ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
+ return -1;
+ }
+
+ if (ast_strlen_zero(args.object_type)) {
+ ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
+ return -1;
+ }
+
+ if (ast_strlen_zero(args.object_id)) {
+ ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
+ return -1;
+ }
+
+ if (ast_strlen_zero(args.field_name)) {
+ ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
+ return -1;
+ }
+
+ if (ast_strlen_zero(args.method)) {
+ method = CONCAT;
+ } else {
+ if (strcmp(args.method, "concat") == 0) {
+ method = CONCAT;
+ if (ast_strlen_zero(args.method_arg)) {
+ separator = ",";
+ } else {
+ separator = args.method_arg;
+ }
+
+ } else if (strcmp(args.method, "single") == 0) {
+ method = SINGLE;
+ if (!ast_strlen_zero(args.method_arg)) {
+ if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
+ ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
+ return -1;
+ }
+ }
+ } else {
+ ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
+ return -1;
+ }
+ }
+
+ sorcery = ast_sorcery_retrieve_by_module_name(args.module_name);
+ if (!sorcery) {
+ ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
+ return -1;
+ }
+
+ sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
+ if (!sorcery_obj) {
+ return -1;
+ }
+
+ change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
+ if (!change_set) {
+ return -1;
+ }
+
+ ix=1;
+ found = 0;
+ for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
+
+ if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
+ ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
+ found = 1;
+ continue;
+ }
+
+ if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0 && ix++ == field_number) {
+ ast_str_set(buf, len, "%s", it_change_set->value);
+ found = 1;
+ break;
+ }
+ }
+
+ ast_variables_destroy(change_set);
+
+ if (!found) {
+ return -1;
+ }
+
+ if (method == CONCAT) {
+ ast_str_truncate(*buf, -1);
+ }
+
+ return 0;
+}
+
+static struct ast_custom_function sorcery_function = {
+ .name = "AST_SORCERY",
+ .read2 = sorcery_function_read,
+};
+
+static int unload_module(void)
+{
+ return ast_custom_function_unregister(&sorcery_function);
+}
+
+static int load_module(void)
+{
+ return ast_custom_function_register(&sorcery_function);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");
+