func_pjsip_endpoint: Add PJSIP_ENDPOINT function for querying endpoint details
[asterisk/asterisk.git] / funcs / func_db.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005-2006, Russell Bryant <russelb@clemson.edu> 
5  *
6  * func_db.c adapted from the old app_db.c, copyright by the following people 
7  * Copyright (C) 2005, Mark Spencer <markster@digium.com>
8  * Copyright (C) 2003, Jefferson Noxon <jeff@debian.org>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Functions for interaction with the Asterisk database
24  *
25  * \author Russell Bryant <russelb@clemson.edu>
26  *
27  * \ingroup functions
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <regex.h>
39
40 #include "asterisk/module.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/app.h"
45 #include "asterisk/astdb.h"
46
47 /*** DOCUMENTATION
48         <function name="DB" language="en_US">
49                 <synopsis>
50                         Read from or write to the Asterisk database.
51                 </synopsis>
52                 <syntax argsep="/">
53                         <parameter name="family" required="true" />
54                         <parameter name="key" required="true" />
55                 </syntax>
56                 <description>
57                         <para>This function will read from or write a value to the Asterisk database.  On a
58                         read, this function returns the corresponding value from the database, or blank
59                         if it does not exist.  Reading a database value will also set the variable
60                         DB_RESULT.  If you wish to find out if an entry exists, use the DB_EXISTS
61                         function.</para>
62                 </description>
63                 <see-also>
64                         <ref type="application">DBdel</ref>
65                         <ref type="function">DB_DELETE</ref>
66                         <ref type="application">DBdeltree</ref>
67                         <ref type="function">DB_EXISTS</ref>
68                 </see-also>
69         </function>
70         <function name="DB_EXISTS" language="en_US">
71                 <synopsis>
72                         Check to see if a key exists in the Asterisk database.
73                 </synopsis>
74                 <syntax argsep="/">
75                         <parameter name="family" required="true" />
76                         <parameter name="key" required="true" />
77                 </syntax>
78                 <description>
79                         <para>This function will check to see if a key exists in the Asterisk
80                         database. If it exists, the function will return <literal>1</literal>. If not,
81                         it will return <literal>0</literal>.  Checking for existence of a database key will
82                         also set the variable DB_RESULT to the key's value if it exists.</para>
83                 </description>
84                 <see-also>
85                         <ref type="function">DB</ref>
86                 </see-also>
87         </function>
88         <function name="DB_KEYS" language="en_US">
89                 <synopsis>
90                         Obtain a list of keys within the Asterisk database.
91                 </synopsis>
92                 <syntax>
93                         <parameter name="prefix" />
94                 </syntax>
95                 <description>
96                         <para>This function will return a comma-separated list of keys existing
97                         at the prefix specified within the Asterisk database.  If no argument is
98                         provided, then a list of key families will be returned.</para>
99                 </description>
100         </function>
101         <function name="DB_DELETE" language="en_US">
102                 <synopsis>
103                         Return a value from the database and delete it.
104                 </synopsis>
105                 <syntax argsep="/">
106                         <parameter name="family" required="true" />
107                         <parameter name="key" required="true" />
108                 </syntax>
109                 <description>
110                         <para>This function will retrieve a value from the Asterisk database
111                         and then remove that key from the database. <variable>DB_RESULT</variable>
112                         will be set to the key's value if it exists.</para>
113                 </description>
114                 <see-also>
115                         <ref type="application">DBdel</ref>
116                         <ref type="function">DB</ref>
117                         <ref type="application">DBdeltree</ref>
118                 </see-also>
119         </function>
120  ***/
121
122 static int function_db_read(struct ast_channel *chan, const char *cmd,
123                             char *parse, char *buf, size_t len)
124 {
125         AST_DECLARE_APP_ARGS(args,
126                 AST_APP_ARG(family);
127                 AST_APP_ARG(key);
128         );
129
130         buf[0] = '\0';
131
132         if (ast_strlen_zero(parse)) {
133                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
134                 return -1;
135         }
136
137         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
138
139         if (args.argc < 2) {
140                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
141                 return -1;
142         }
143
144         if (ast_db_get(args.family, args.key, buf, len - 1)) {
145                 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
146         } else {
147                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
148         }
149
150         return 0;
151 }
152
153 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
154                              const char *value)
155 {
156         AST_DECLARE_APP_ARGS(args,
157                 AST_APP_ARG(family);
158                 AST_APP_ARG(key);
159         );
160
161         if (ast_strlen_zero(parse)) {
162                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
163                 return -1;
164         }
165
166         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
167
168         if (args.argc < 2) {
169                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
170                 return -1;
171         }
172
173         if (ast_db_put(args.family, args.key, value)) {
174                 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
175         }
176
177         return 0;
178 }
179
180 static struct ast_custom_function db_function = {
181         .name = "DB",
182         .read = function_db_read,
183         .write = function_db_write,
184 };
185
186 static int function_db_exists(struct ast_channel *chan, const char *cmd,
187                               char *parse, char *buf, size_t len)
188 {
189         AST_DECLARE_APP_ARGS(args,
190                 AST_APP_ARG(family);
191                 AST_APP_ARG(key);
192         );
193
194         buf[0] = '\0';
195
196         if (ast_strlen_zero(parse)) {
197                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
198                 return -1;
199         }
200
201         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
202
203         if (args.argc < 2) {
204                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
205                 return -1;
206         }
207
208         if (ast_db_get(args.family, args.key, buf, len - 1)) {
209                 strcpy(buf, "0");
210         } else {
211                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
212                 strcpy(buf, "1");
213         }
214
215         return 0;
216 }
217
218 static struct ast_custom_function db_exists_function = {
219         .name = "DB_EXISTS",
220         .read = function_db_exists,
221         .read_max = 2,
222 };
223
224 static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
225 {
226         size_t parselen = strlen(parse);
227         struct ast_db_entry *dbe, *orig_dbe;
228         struct ast_str *escape_buf = NULL;
229         const char *last = "";
230
231         /* Remove leading and trailing slashes */
232         while (parse[0] == '/') {
233                 parse++;
234                 parselen--;
235         }
236         while (parse[parselen - 1] == '/') {
237                 parse[--parselen] = '\0';
238         }
239
240         ast_str_reset(*result);
241
242         /* Nothing within the database at that prefix? */
243         if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
244                 return 0;
245         }
246
247         for (; dbe; dbe = dbe->next) {
248                 /* Find the current component */
249                 char *curkey = &dbe->key[parselen + 1], *slash;
250                 if (*curkey == '/') {
251                         curkey++;
252                 }
253                 /* Remove everything after the current component */
254                 if ((slash = strchr(curkey, '/'))) {
255                         *slash = '\0';
256                 }
257
258                 /* Skip duplicates */
259                 if (!strcasecmp(last, curkey)) {
260                         continue;
261                 }
262                 last = curkey;
263
264                 if (orig_dbe != dbe) {
265                         ast_str_append(result, maxlen, ",");
266                 }
267                 ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
268         }
269         ast_db_freetree(orig_dbe);
270         ast_free(escape_buf);
271         return 0;
272 }
273
274 static struct ast_custom_function db_keys_function = {
275         .name = "DB_KEYS",
276         .read2 = function_db_keys,
277 };
278
279 static int function_db_delete(struct ast_channel *chan, const char *cmd,
280                               char *parse, char *buf, size_t len)
281 {
282         AST_DECLARE_APP_ARGS(args,
283                 AST_APP_ARG(family);
284                 AST_APP_ARG(key);
285         );
286
287         buf[0] = '\0';
288
289         if (ast_strlen_zero(parse)) {
290                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
291                 return -1;
292         }
293
294         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
295
296         if (args.argc < 2) {
297                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
298                 return -1;
299         }
300
301         if (ast_db_get(args.family, args.key, buf, len - 1)) {
302                 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
303         } else {
304                 if (ast_db_del(args.family, args.key)) {
305                         ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
306                 }
307         }
308
309         pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
310
311         return 0;
312 }
313
314
315 static struct ast_custom_function db_delete_function = {
316         .name = "DB_DELETE",
317         .read = function_db_delete,
318 };
319
320 static int unload_module(void)
321 {
322         int res = 0;
323
324         res |= ast_custom_function_unregister(&db_function);
325         res |= ast_custom_function_unregister(&db_exists_function);
326         res |= ast_custom_function_unregister(&db_delete_function);
327         res |= ast_custom_function_unregister(&db_keys_function);
328
329         return res;
330 }
331
332 static int load_module(void)
333 {
334         int res = 0;
335
336         res |= ast_custom_function_register(&db_function);
337         res |= ast_custom_function_register(&db_exists_function);
338         res |= ast_custom_function_register(&db_delete_function);
339         res |= ast_custom_function_register(&db_keys_function);
340
341         return res;
342 }
343
344 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");