Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
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 = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .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 <george.joseph@fairview5.com> 

Definition in file func_sorcery.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 221 of file func_sorcery.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 221 of file func_sorcery.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 221 of file func_sorcery.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 216 of file func_sorcery.c.

217{
219}
static struct ast_custom_function sorcery_function
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562

References ast_custom_function_register, and sorcery_function.

◆ 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 82 of file func_sorcery.c.

84{
85 char *parsed_data = ast_strdupa(data);
87 RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
88 struct ast_variable *change_set;
89 struct ast_variable *it_change_set;
90 int found, field_number = 1, ix, method;
91 char *separator = ",";
92
93 enum methods {
94 CONCAT,
95 SINGLE,
96 };
97
99 AST_APP_ARG(module_name);
100 AST_APP_ARG(object_type);
101 AST_APP_ARG(object_id);
102 AST_APP_ARG(field_name);
104 AST_APP_ARG(method_arg);
105 );
106
107 /* Check for zero arguments */
108 if (ast_strlen_zero(parsed_data)) {
109 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
110 return -1;
111 }
112
113 AST_STANDARD_APP_ARGS(args, parsed_data);
114
115 if (ast_strlen_zero(args.module_name)) {
116 ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
117 return -1;
118 }
119
120 if (ast_strlen_zero(args.object_type)) {
121 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
122 return -1;
123 }
124
125 if (ast_strlen_zero(args.object_id)) {
126 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
127 return -1;
128 }
129
130 if (ast_strlen_zero(args.field_name)) {
131 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
132 return -1;
133 }
134
135 if (ast_strlen_zero(args.method)) {
136 method = CONCAT;
137 } else {
138 if (strcmp(args.method, "concat") == 0) {
139 method = CONCAT;
140 if (ast_strlen_zero(args.method_arg)) {
141 separator = ",";
142 } else {
143 separator = args.method_arg;
144 }
145
146 } else if (strcmp(args.method, "single") == 0) {
147 method = SINGLE;
148 if (!ast_strlen_zero(args.method_arg)) {
149 if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
150 ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
151 return -1;
152 }
153 }
154 } else {
155 ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
156 return -1;
157 }
158 }
159
161 if (!sorcery) {
162 ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
163 return -1;
164 }
165
166 sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
167 if (!sorcery_obj) {
168 return -1;
169 }
170
171 change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
172 if (!change_set) {
173 return -1;
174 }
175
176 ix=1;
177 found = 0;
178 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
179
180 if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
181 ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
182 found = 1;
183 continue;
184 }
185
186 if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0 && ix++ == field_number) {
187 ast_str_set(buf, len, "%s", it_change_set->value);
188 found = 1;
189 break;
190 }
191 }
192
193 ast_variables_destroy(change_set);
194
195 if (!found) {
196 return -1;
197 }
198
199 if (method == CONCAT) {
200 ast_str_truncate(*buf, -1);
201 }
202
203 return 0;
204}
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_log
Definition astobj2.c:42
#define ao2_cleanup(obj)
Definition astobj2.h:1934
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
#define AST_LOG_ERROR
static struct ast_sorcery * sorcery
static struct @487 methods[]
const char * method
Definition res_pjsip.c:1279
static struct @519 args
#define NULL
Definition resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition sorcery.h:1500
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition sorcery.h:1137
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:1917
struct ast_sorcery * ast_sorcery_retrieve_by_module_name(const char *module_name)
Retrieves an existing sorcery instance by module name.
Definition sorcery.c:736
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
Definition strings.h:786
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:1139
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
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:1113
Full structure for sorcery.
Definition sorcery.c:231
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
#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:981

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(), buf, len(), method, methods, ast_variable::name, ast_variable::next, NULL, RAII_VAR, sorcery, and ast_variable::value.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 211 of file func_sorcery.c.

212{
214}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), and sorcery_function.

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 = ASTERISK_GPL_KEY , .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 221 of file func_sorcery.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 221 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 at line 206 of file func_sorcery.c.

206 {
207 .name = "AST_SORCERY",
208 .read2 = sorcery_function_read,
209};

Referenced by load_module(), and unload_module().