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