func_sorcery.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Fairview 5 Engineering, LLC
00005  *
00006  * George Joseph <george.joseph@fairview5.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Get a field from a sorcery object
00022  *
00023  * \author \verbatim George Joseph <george.joseph@fairview5.com> \endverbatim
00024  *
00025  * \ingroup functions
00026  *
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "")
00036 
00037 #include "asterisk/app.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/sorcery.h"
00041 
00042 /*** DOCUMENTATION
00043    <function name="AST_SORCERY" language="en_US">
00044       <synopsis>
00045          Get a field from a sorcery object
00046       </synopsis>
00047       <syntax>
00048          <parameter name="module_name" required="true">
00049             <para>The name of the module owning the sorcery instance.</para>
00050          </parameter>
00051          <parameter name="object_type" required="true">
00052             <para>The type of object to query.</para>
00053          </parameter>
00054          <parameter name="object_id" required="true">
00055             <para>The id of the object to query.</para>
00056          </parameter>
00057          <parameter name="field_name" required="true">
00058             <para>The name of the field.</para>
00059          </parameter>
00060          <parameter name="retrieval_method" required="false">
00061             <para>Fields that have multiple occurrences may be retrieved in two ways.</para>
00062             <enumlist>
00063                <enum name="concat"><para>Returns all matching fields concatenated
00064                in a single string separated by <replaceable>separator</replaceable>
00065                which defaults to <literal>,</literal>.</para></enum>
00066 
00067                <enum name="single"><para>Returns the nth occurrence of the field
00068                as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
00069                </para></enum>
00070             </enumlist>
00071             <para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
00072          </parameter>
00073          <parameter name="retrieval_details" required="false">
00074             <para>Specifies either the separator for <literal>concat</literal>
00075             or the occurrence number for <literal>single</literal>.</para>
00076          </parameter>
00077       </syntax>
00078    </function>
00079 ***/
00080 
00081 static int sorcery_function_read(struct ast_channel *chan,
00082    const char *cmd, char *data, struct ast_str **buf, ssize_t len)
00083 {
00084    char *parsed_data = ast_strdupa(data);
00085    RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
00086    RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
00087    struct ast_variable *change_set;
00088    struct ast_variable *it_change_set;
00089    int found, field_number = 1, ix, method;
00090    char *separator = ",";
00091 
00092    enum methods {
00093       CONCAT,
00094       SINGLE,
00095    };
00096 
00097    AST_DECLARE_APP_ARGS(args,
00098       AST_APP_ARG(module_name);
00099       AST_APP_ARG(object_type);
00100       AST_APP_ARG(object_id);
00101       AST_APP_ARG(field_name);
00102       AST_APP_ARG(method);
00103       AST_APP_ARG(method_arg);
00104    );
00105 
00106    /* Check for zero arguments */
00107    if (ast_strlen_zero(parsed_data)) {
00108       ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
00109       return -1;
00110    }
00111 
00112    AST_STANDARD_APP_ARGS(args, parsed_data);
00113 
00114    if (ast_strlen_zero(args.module_name)) {
00115       ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
00116       return -1;
00117    }
00118 
00119    if (ast_strlen_zero(args.object_type)) {
00120       ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
00121       return -1;
00122    }
00123 
00124    if (ast_strlen_zero(args.object_id)) {
00125       ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
00126       return -1;
00127    }
00128 
00129    if (ast_strlen_zero(args.field_name)) {
00130       ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
00131       return -1;
00132    }
00133 
00134    if (ast_strlen_zero(args.method)) {
00135       method = CONCAT;
00136    } else {
00137       if (strcmp(args.method, "concat") == 0) {
00138          method = CONCAT;
00139          if (ast_strlen_zero(args.method_arg)) {
00140             separator = ",";
00141          } else {
00142             separator = args.method_arg;
00143          }
00144 
00145       } else if (strcmp(args.method, "single") == 0) {
00146          method = SINGLE;
00147          if (!ast_strlen_zero(args.method_arg)) {
00148             if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
00149                ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
00150                return -1;
00151             }
00152          }
00153       } else {
00154          ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
00155          return -1;
00156       }
00157    }
00158 
00159    sorcery = ast_sorcery_retrieve_by_module_name(args.module_name);
00160    if (!sorcery) {
00161       ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
00162       return -1;
00163    }
00164 
00165    sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
00166    if (!sorcery_obj) {
00167       return -1;
00168    }
00169 
00170    change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
00171    if (!change_set) {
00172       return -1;
00173    }
00174 
00175    ix=1;
00176    found = 0;
00177    for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
00178 
00179       if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
00180          ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
00181          found = 1;
00182          continue;
00183       }
00184 
00185       if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0  && ix++ == field_number) {
00186          ast_str_set(buf, len, "%s", it_change_set->value);
00187          found = 1;
00188          break;
00189       }
00190    }
00191 
00192    ast_variables_destroy(change_set);
00193 
00194    if (!found) {
00195       return -1;
00196    }
00197 
00198    if (method == CONCAT) {
00199       ast_str_truncate(*buf, -1);
00200    }
00201 
00202    return 0;
00203 }
00204 
00205 static struct ast_custom_function sorcery_function = {
00206    .name = "AST_SORCERY",
00207    .read2 = sorcery_function_read,
00208 };
00209 
00210 static int unload_module(void)
00211 {
00212    return ast_custom_function_unregister(&sorcery_function);
00213 }
00214 
00215 static int load_module(void)
00216 {
00217    return ast_custom_function_register(&sorcery_function);
00218 }
00219 
00220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");
00221 

Generated on Thu Apr 16 06:27:36 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6