func_sorcery.c File Reference

Get a field from a sorcery object. More...

#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"

Include dependency graph for func_sorcery.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int load_module (void)
static int sorcery_function_read (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Get a field from a sorcery object" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function sorcery_function


Detailed Description

Get a field from a sorcery object.

Author:
George Joseph <george.joseph@fairview5.com> 

Definition in file func_sorcery.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 220 of file func_sorcery.c.

static void __unreg_module ( void   )  [static]

Definition at line 220 of file func_sorcery.c.

static int load_module ( void   )  [static]

Definition at line 215 of file func_sorcery.c.

References ast_custom_function_register.

00216 {
00217    return ast_custom_function_register(&sorcery_function);
00218 }

static int sorcery_function_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  buf,
ssize_t  len 
) [static]

Definition at line 81 of file func_sorcery.c.

References ao2_cleanup, args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sorcery_objectset_create, ast_sorcery_retrieve_by_id(), ast_sorcery_retrieve_by_module_name(), ast_sorcery_unref(), AST_STANDARD_APP_ARGS, ast_str_append(), ast_str_set(), ast_str_truncate(), ast_strdupa, ast_strlen_zero, ast_variables_destroy(), method, methods, ast_variable::name, ast_variable::next, NULL, RAII_VAR, sorcery, and ast_variable::value.

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 }

static int unload_module ( void   )  [static]

Definition at line 210 of file func_sorcery.c.

References ast_custom_function_unregister().

00211 {
00212    return ast_custom_function_unregister(&sorcery_function);
00213 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Get a field from a sorcery object" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, } [static]

Definition at line 220 of file func_sorcery.c.

Definition at line 220 of file func_sorcery.c.

Initial value:

 {
   .name = "AST_SORCERY",
   .read2 = sorcery_function_read,
}

Definition at line 205 of file func_sorcery.c.


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