Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_sorcery.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Fairview 5 Engineering, LLC
5 *
6 * George Joseph <george.joseph@fairview5.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Get a field from a sorcery object
22 *
23 * \author \verbatim George Joseph <george.joseph@fairview5.com> \endverbatim
24 *
25 * \ingroup functions
26 *
27 */
28
29/*** MODULEINFO
30 <support_level>core</support_level>
31 ***/
32
33#include "asterisk.h"
34
35#include "asterisk/app.h"
36#include "asterisk/pbx.h"
37#include "asterisk/module.h"
38#include "asterisk/sorcery.h"
39
40/*** DOCUMENTATION
41 <function name="AST_SORCERY" language="en_US">
42 <since>
43 <version>12.2.0</version>
44 </since>
45 <synopsis>
46 Get a field from a sorcery object
47 </synopsis>
48 <syntax>
49 <parameter name="module_name" required="true">
50 <para>The name of the module owning the sorcery instance.</para>
51 </parameter>
52 <parameter name="object_type" required="true">
53 <para>The type of object to query.</para>
54 </parameter>
55 <parameter name="object_id" required="true">
56 <para>The id of the object to query.</para>
57 </parameter>
58 <parameter name="field_name" required="true">
59 <para>The name of the field.</para>
60 </parameter>
61 <parameter name="retrieval_method" required="false">
62 <para>Fields that have multiple occurrences may be retrieved in two ways.</para>
63 <enumlist>
64 <enum name="concat"><para>Returns all matching fields concatenated
65 in a single string separated by <replaceable>separator</replaceable>
66 which defaults to <literal>,</literal>.</para></enum>
67
68 <enum name="single"><para>Returns the nth occurrence of the field
69 as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
70 </para></enum>
71 </enumlist>
72 <para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
73 </parameter>
74 <parameter name="retrieval_details" required="false">
75 <para>Specifies either the separator for <literal>concat</literal>
76 or the occurrence number for <literal>single</literal>.</para>
77 </parameter>
78 </syntax>
79 </function>
80***/
81
82static int sorcery_function_read(struct ast_channel *chan,
83 const char *cmd, char *data, struct ast_str **buf, ssize_t len)
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}
205
207 .name = "AST_SORCERY",
208 .read2 = sorcery_function_read,
209};
210
211static int unload_module(void)
212{
214}
215
216static int load_module(void)
217{
219}
220
221AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");
Asterisk main include file. File version handling, generic pbx functions.
#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 load_module(void)
Definition: func_sorcery.c:216
static int unload_module(void)
Definition: func_sorcery.c:211
static struct ast_custom_function sorcery_function
Definition: func_sorcery.c:206
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:82
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#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:1262
#define AST_LOG_ERROR
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct @453 methods[]
const char * method
Definition: res_pjsip.c:1279
static struct ast_sorcery * sorcery
#define NULL
Definition: resample.c:96
Sorcery Data Access Layer API.
#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:1853
struct ast_sorcery * ast_sorcery_retrieve_by_module_name(const char *module_name)
Retrieves an existing sorcery instance by module name.
Definition: sorcery.c:672
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
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Full structure for sorcery.
Definition: sorcery.c:230
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
const char * args
#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:941