git migration: Refactor the ASTERISK_FILE_VERSION macro
[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_REGISTER_FILE()
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                         <note>
114                                 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
115                                 is set to <literal>no</literal>, this function can only be read from the
116                                 dialplan, and not directly from external protocols. It can, however, be
117                                 executed as a write operation (<literal>DB_DELETE(family, key)=ignored</literal>)</para>
118                         </note>
119                 </description>
120                 <see-also>
121                         <ref type="application">DBdel</ref>
122                         <ref type="function">DB</ref>
123                         <ref type="application">DBdeltree</ref>
124                 </see-also>
125         </function>
126  ***/
127
128 static int function_db_read(struct ast_channel *chan, const char *cmd,
129                             char *parse, char *buf, size_t len)
130 {
131         AST_DECLARE_APP_ARGS(args,
132                 AST_APP_ARG(family);
133                 AST_APP_ARG(key);
134         );
135
136         buf[0] = '\0';
137
138         if (ast_strlen_zero(parse)) {
139                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
140                 return -1;
141         }
142
143         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
144
145         if (args.argc < 2) {
146                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
147                 return -1;
148         }
149
150         if (ast_db_get(args.family, args.key, buf, len - 1)) {
151                 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
152         } else {
153                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
154         }
155
156         return 0;
157 }
158
159 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
160                              const char *value)
161 {
162         AST_DECLARE_APP_ARGS(args,
163                 AST_APP_ARG(family);
164                 AST_APP_ARG(key);
165         );
166
167         if (ast_strlen_zero(parse)) {
168                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
169                 return -1;
170         }
171
172         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
173
174         if (args.argc < 2) {
175                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
176                 return -1;
177         }
178
179         if (ast_db_put(args.family, args.key, value)) {
180                 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
181         }
182
183         return 0;
184 }
185
186 static struct ast_custom_function db_function = {
187         .name = "DB",
188         .read = function_db_read,
189         .write = function_db_write,
190 };
191
192 static int function_db_exists(struct ast_channel *chan, const char *cmd,
193                               char *parse, char *buf, size_t len)
194 {
195         AST_DECLARE_APP_ARGS(args,
196                 AST_APP_ARG(family);
197                 AST_APP_ARG(key);
198         );
199
200         buf[0] = '\0';
201
202         if (ast_strlen_zero(parse)) {
203                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
204                 return -1;
205         }
206
207         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
208
209         if (args.argc < 2) {
210                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
211                 return -1;
212         }
213
214         if (ast_db_get(args.family, args.key, buf, len - 1)) {
215                 strcpy(buf, "0");
216         } else {
217                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
218                 strcpy(buf, "1");
219         }
220
221         return 0;
222 }
223
224 static struct ast_custom_function db_exists_function = {
225         .name = "DB_EXISTS",
226         .read = function_db_exists,
227         .read_max = 2,
228 };
229
230 static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
231 {
232         size_t parselen = strlen(parse);
233         struct ast_db_entry *dbe, *orig_dbe;
234         struct ast_str *escape_buf = NULL;
235         const char *last = "";
236
237         /* Remove leading and trailing slashes */
238         while (parse[0] == '/') {
239                 parse++;
240                 parselen--;
241         }
242         while (parse[parselen - 1] == '/') {
243                 parse[--parselen] = '\0';
244         }
245
246         ast_str_reset(*result);
247
248         /* Nothing within the database at that prefix? */
249         if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
250                 return 0;
251         }
252
253         for (; dbe; dbe = dbe->next) {
254                 /* Find the current component */
255                 char *curkey = &dbe->key[parselen + 1], *slash;
256                 if (*curkey == '/') {
257                         curkey++;
258                 }
259                 /* Remove everything after the current component */
260                 if ((slash = strchr(curkey, '/'))) {
261                         *slash = '\0';
262                 }
263
264                 /* Skip duplicates */
265                 if (!strcasecmp(last, curkey)) {
266                         continue;
267                 }
268                 last = curkey;
269
270                 if (orig_dbe != dbe) {
271                         ast_str_append(result, maxlen, ",");
272                 }
273                 ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
274         }
275         ast_db_freetree(orig_dbe);
276         ast_free(escape_buf);
277         return 0;
278 }
279
280 static struct ast_custom_function db_keys_function = {
281         .name = "DB_KEYS",
282         .read2 = function_db_keys,
283 };
284
285 static int function_db_delete(struct ast_channel *chan, const char *cmd,
286                               char *parse, char *buf, size_t len)
287 {
288         AST_DECLARE_APP_ARGS(args,
289                 AST_APP_ARG(family);
290                 AST_APP_ARG(key);
291         );
292
293         buf[0] = '\0';
294
295         if (ast_strlen_zero(parse)) {
296                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
297                 return -1;
298         }
299
300         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
301
302         if (args.argc < 2) {
303                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
304                 return -1;
305         }
306
307         if (ast_db_get(args.family, args.key, buf, len - 1)) {
308                 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
309         } else {
310                 if (ast_db_del(args.family, args.key)) {
311                         ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
312                 }
313         }
314
315         pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
316
317         return 0;
318 }
319
320 /*!
321  * \brief Wrapper to execute DB_DELETE from a write operation. Allows execution
322  * even if live_dangerously is disabled.
323  */
324 static int function_db_delete_write(struct ast_channel *chan, const char *cmd, char *parse,
325         const char *value)
326 {
327         /* Throwaway to hold the result from the read */
328         char buf[128];
329         return function_db_delete(chan, cmd, parse, buf, sizeof(buf));
330 }
331
332 static struct ast_custom_function db_delete_function = {
333         .name = "DB_DELETE",
334         .read = function_db_delete,
335         .write = function_db_delete_write,
336 };
337
338 static int unload_module(void)
339 {
340         int res = 0;
341
342         res |= ast_custom_function_unregister(&db_function);
343         res |= ast_custom_function_unregister(&db_exists_function);
344         res |= ast_custom_function_unregister(&db_delete_function);
345         res |= ast_custom_function_unregister(&db_keys_function);
346
347         return res;
348 }
349
350 static int load_module(void)
351 {
352         int res = 0;
353
354         res |= ast_custom_function_register_escalating(&db_function, AST_CFE_BOTH);
355         res |= ast_custom_function_register(&db_exists_function);
356         res |= ast_custom_function_register_escalating(&db_delete_function, AST_CFE_READ);
357         res |= ast_custom_function_register(&db_keys_function);
358
359         return res;
360 }
361
362 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");