Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const 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 <[email protected]> 

Definition in file func_sorcery.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 218 of file func_sorcery.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 218 of file func_sorcery.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 218 of file func_sorcery.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 213 of file func_sorcery.c.

References ast_custom_function_register.

214 {
216 }
static struct ast_custom_function sorcery_function
Definition: func_sorcery.c:203
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ sorcery_function_read()

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 79 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.

81 {
82  char *parsed_data = ast_strdupa(data);
84  RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
85  struct ast_variable *change_set;
86  struct ast_variable *it_change_set;
87  int found, field_number = 1, ix, method;
88  char *separator = ",";
89 
90  enum methods {
91  CONCAT,
92  SINGLE,
93  };
94 
96  AST_APP_ARG(module_name);
97  AST_APP_ARG(object_type);
98  AST_APP_ARG(object_id);
99  AST_APP_ARG(field_name);
100  AST_APP_ARG(method);
101  AST_APP_ARG(method_arg);
102  );
103 
104  /* Check for zero arguments */
105  if (ast_strlen_zero(parsed_data)) {
106  ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
107  return -1;
108  }
109 
110  AST_STANDARD_APP_ARGS(args, parsed_data);
111 
112  if (ast_strlen_zero(args.module_name)) {
113  ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
114  return -1;
115  }
116 
117  if (ast_strlen_zero(args.object_type)) {
118  ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
119  return -1;
120  }
121 
122  if (ast_strlen_zero(args.object_id)) {
123  ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
124  return -1;
125  }
126 
127  if (ast_strlen_zero(args.field_name)) {
128  ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
129  return -1;
130  }
131 
132  if (ast_strlen_zero(args.method)) {
133  method = CONCAT;
134  } else {
135  if (strcmp(args.method, "concat") == 0) {
136  method = CONCAT;
137  if (ast_strlen_zero(args.method_arg)) {
138  separator = ",";
139  } else {
140  separator = args.method_arg;
141  }
142 
143  } else if (strcmp(args.method, "single") == 0) {
144  method = SINGLE;
145  if (!ast_strlen_zero(args.method_arg)) {
146  if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
147  ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
148  return -1;
149  }
150  }
151  } else {
152  ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
153  return -1;
154  }
155  }
156 
158  if (!sorcery) {
159  ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
160  return -1;
161  }
162 
163  sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
164  if (!sorcery_obj) {
165  return -1;
166  }
167 
168  change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
169  if (!change_set) {
170  return -1;
171  }
172 
173  ix=1;
174  found = 0;
175  for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
176 
177  if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
178  ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
179  found = 1;
180  continue;
181  }
182 
183  if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0 && ix++ == field_number) {
184  ast_str_set(buf, len, "%s", it_change_set->value);
185  found = 1;
186  break;
187  }
188  }
189 
190  ast_variables_destroy(change_set);
191 
192  if (!found) {
193  return -1;
194  }
195 
196  if (method == CONCAT) {
197  ast_str_truncate(*buf, -1);
198  }
199 
200  return 0;
201 }
struct ast_variable * next
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Structure for variables, used for configurations and for channel variables.
Full structure for sorcery.
Definition: sorcery.c:230
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
static struct @481 methods[]
const char * args
#define NULL
Definition: resample.c:96
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
Definition: strings.h:738
#define ast_strlen_zero(foo)
Definition: strings.h:52
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1065
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
const char * method
Definition: res_pjsip.c:4335
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1136
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
struct ast_sorcery * ast_sorcery_retrieve_by_module_name(const char *module)
Retrieves an existing sorcery instance by module name.
Definition: sorcery.c:672
#define AST_APP_ARG(name)
Define an application argument.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 208 of file func_sorcery.c.

References ast_custom_function_unregister().

209 {
211 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function sorcery_function
Definition: func_sorcery.c:203

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 218 of file func_sorcery.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 218 of file func_sorcery.c.

◆ sorcery_function

struct ast_custom_function sorcery_function
static
Initial value:
= {
.name = "AST_SORCERY",
}
static int sorcery_function_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_sorcery.c:79

Definition at line 203 of file func_sorcery.c.