1671b3f05552e2ef2bfddae1b992a098f8fd5ea5
[asterisk/asterisk.git] / funcs / func_sorcery.c
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 ASTERISK_FILE_VERSION(__FILE__, "")
36
37 #include "asterisk/app.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/sorcery.h"
41
42 /*** DOCUMENTATION
43         <function name="AST_SORCERY" language="en_US">
44                 <synopsis>
45                         Get a field from a sorcery object
46                 </synopsis>
47                 <syntax>
48                         <parameter name="module_name" required="true">
49                                 <para>The name of the module owning the sorcery instance.</para>
50                         </parameter>
51                         <parameter name="object_type" required="true">
52                                 <para>The type of object to query.</para>
53                         </parameter>
54                         <parameter name="object_id" required="true">
55                                 <para>The id of the object to query.</para>
56                         </parameter>
57                         <parameter name="field_name" required="true">
58                                 <para>The name of the field.</para>
59                         </parameter>
60                         <parameter name="retrieval_method" required="false">
61                                 <para>Fields that have multiple occurrences may be retrieved in two ways.</para>
62                                 <enumlist>
63                                         <enum name="concat"><para>Returns all matching fields concatenated
64                                         in a single string separated by <replaceable>separator</replaceable>
65                                         which defaults to <literal>,</literal>.</para></enum>
66
67                                         <enum name="single"><para>Returns the nth occurrence of the field
68                                         as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
69                                         </para></enum>
70                                 </enumlist>
71                                 <para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
72                         </parameter>
73                         <parameter name="retrieval_details" required="false">
74                                 <para>Specifies either the separator for <literal>concat</literal>
75                                 or the occurrence number for <literal>single</literal>.</para>
76                         </parameter>
77                 </syntax>
78         </function>
79 ***/
80
81 static int sorcery_function_read(struct ast_channel *chan,
82         const char *cmd, char *data, struct ast_str **buf, ssize_t len)
83 {
84         char *parsed_data = ast_strdupa(data);
85         RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
86         RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
87         struct ast_variable *change_set;
88         struct ast_variable *it_change_set;
89         int found, field_number = 1, ix, method;
90         char *separator = ",";
91
92         enum methods {
93                 CONCAT,
94                 SINGLE,
95         };
96
97         AST_DECLARE_APP_ARGS(args,
98                 AST_APP_ARG(module_name);
99                 AST_APP_ARG(object_type);
100                 AST_APP_ARG(object_id);
101                 AST_APP_ARG(field_name);
102                 AST_APP_ARG(method);
103                 AST_APP_ARG(method_arg);
104         );
105
106         /* Check for zero arguments */
107         if (ast_strlen_zero(parsed_data)) {
108                 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
109                 return -1;
110         }
111
112         AST_STANDARD_APP_ARGS(args, parsed_data);
113
114         if (ast_strlen_zero(args.module_name)) {
115                 ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
116                 return -1;
117         }
118
119         if (ast_strlen_zero(args.object_type)) {
120                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
121                 return -1;
122         }
123
124         if (ast_strlen_zero(args.object_id)) {
125                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
126                 return -1;
127         }
128
129         if (ast_strlen_zero(args.field_name)) {
130                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
131                 return -1;
132         }
133
134         if (ast_strlen_zero(args.method)) {
135                 method = CONCAT;
136         } else {
137                 if (strcmp(args.method, "concat") == 0) {
138                         method = CONCAT;
139                         if (ast_strlen_zero(args.method_arg)) {
140                                 separator = ",";
141                         } else {
142                                 separator = args.method_arg;
143                         }
144
145                 } else if (strcmp(args.method, "single") == 0) {
146                         method = SINGLE;
147                         if (!ast_strlen_zero(args.method_arg)) {
148                                 if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
149                                         ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
150                                         return -1;
151                                 }
152                         }
153                 } else {
154                         ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
155                         return -1;
156                 }
157         }
158
159         sorcery = ast_sorcery_retrieve_by_module_name(args.module_name);
160         if (!sorcery) {
161                 ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
162                 return -1;
163         }
164
165         sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
166         if (!sorcery_obj) {
167                 return -1;
168         }
169
170         change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
171         if (!change_set) {
172                 return -1;
173         }
174
175         ix=1;
176         found = 0;
177         for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
178
179                 if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
180                         ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
181                         found = 1;
182                         continue;
183                 }
184
185                 if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0  && ix++ == field_number) {
186                         ast_str_set(buf, len, "%s", it_change_set->value);
187                         found = 1;
188                         break;
189                 }
190         }
191
192         ast_variables_destroy(change_set);
193
194         if (!found) {
195                 return -1;
196         }
197
198         if (method == CONCAT) {
199                 ast_str_truncate(*buf, -1);
200         }
201
202         return 0;
203 }
204
205 static struct ast_custom_function sorcery_function = {
206         .name = "AST_SORCERY",
207         .read2 = sorcery_function_read,
208 };
209
210 static int unload_module(void)
211 {
212         return ast_custom_function_unregister(&sorcery_function);
213 }
214
215 static int load_module(void)
216 {
217         return ast_custom_function_register(&sorcery_function);
218 }
219
220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");
221