Convert app_db to function variables (bug #4201)
[asterisk/asterisk.git] / apps / app_db.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Database access functions
5  *
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  * Copyright (C) 2003, Jefferson Noxon
8  *
9  * Mark Spencer <markster@digium.com>
10  * Jefferson Noxon <jeff@debian.org>
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License
14  */
15
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include "asterisk/options.h"
21 #include "asterisk/file.h"
22 #include "asterisk/logger.h"
23 #include "asterisk/channel.h"
24 #include "asterisk/pbx.h"
25 #include "asterisk/module.h"
26 #include "asterisk/astdb.h"
27 #include "asterisk/lock.h"
28
29 static char *tdesc = "Database access functions for Asterisk extension logic";
30
31 static char *g_descrip =
32         "  DBget(varname=family/key): Retrieves a value from the Asterisk\n"
33         "database and stores it in the given variable.  Always returns 0.  If the\n"
34         "requested key is not found, jumps to priority n+101 if available.\n";
35
36 static char *p_descrip =
37         "  DBput(family/key=value): Stores the given value in the Asterisk\n"
38         "database.  Always returns 0.\n";
39
40 static char *d_descrip =
41         "  DBdel(family/key): Deletes a key from the Asterisk database.  Always\n"
42         "returns 0.\n";
43
44 static char *dt_descrip =
45         "  DBdeltree(family[/keytree]): Deletes a family or keytree from the Asterisk\n"
46         "database.  Always returns 0.\n";
47
48 static char *g_app = "DBget";
49 static char *p_app = "DBput";
50 static char *d_app = "DBdel";
51 static char *dt_app = "DBdeltree";
52
53 static char *g_synopsis = "Retrieve a value from the database";
54 static char *p_synopsis = "Store a value in the database";
55 static char *d_synopsis = "Delete a key from the database";
56 static char *dt_synopsis = "Delete a family or keytree from the database";
57
58 STANDARD_LOCAL_USER;
59
60 LOCAL_USER_DECL;
61
62 static int deltree_exec(struct ast_channel *chan, void *data)
63 {
64         int arglen;
65         char *argv, *family, *keytree;
66
67         arglen = strlen(data);
68         argv = alloca(arglen + 1);
69         if (!argv) {    /* Why would this fail? */
70                 ast_log(LOG_DEBUG, "Memory allocation failed\n");
71                 return 0;
72         }
73         memcpy(argv, data, arglen + 1);
74
75         if (strchr(argv, '/')) {
76                 family = strsep(&argv, "/");
77                 keytree = strsep(&argv, "\0");
78                         if (!family || !keytree) {
79                                 ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
80                                 return 0;
81                         }
82                 if (!strlen(keytree))
83                         keytree = 0;
84         } else {
85                 family = argv;
86                 keytree = 0;
87         }
88
89         if (option_verbose > 2) {
90                 if (keytree)
91                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
92                 else
93                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
94         }
95
96         if (ast_db_deltree(family, keytree)) {
97                 if (option_verbose > 2)
98                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
99         }
100
101         return 0;
102 }
103
104 static int del_exec(struct ast_channel *chan, void *data)
105 {
106         int arglen;
107         char *argv, *family, *key;
108
109         arglen = strlen(data);
110         argv = alloca(arglen + 1);
111         if (!argv) {    /* Why would this fail? */
112                 ast_log (LOG_DEBUG, "Memory allocation failed\n");
113                 return 0;
114         }
115         memcpy(argv, data, arglen + 1);
116
117         if (strchr(argv, '/')) {
118                 family = strsep(&argv, "/");
119                 key = strsep(&argv, "\0");
120                 if (!family || !key) {
121                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
122                         return 0;
123                 }
124                 if (option_verbose > 2)
125                         ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
126                 if (ast_db_del(family, key)) {
127                         if (option_verbose > 2)
128                                 ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
129                 }
130         } else {
131                 ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
132         }
133         return 0;
134 }
135
136 static int put_exec(struct ast_channel *chan, void *data)
137 {
138         int arglen;
139         char *argv, *value, *family, *key;
140         static int dep_warning = 0;
141
142         if (!dep_warning) {
143                 ast_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n");
144                 dep_warning = 1;
145         }
146         
147         arglen = strlen(data);
148         argv = alloca(arglen + 1);
149         if (!argv) {    /* Why would this fail? */
150                 ast_log(LOG_DEBUG, "Memory allocation failed\n");
151                 return 0;
152         }
153         memcpy(argv, data, arglen + 1);
154
155         if (strchr(argv, '/') && strchr(argv, '=')) {
156                 family = strsep(&argv, "/");
157                 key = strsep(&argv, "=");
158                 value = strsep(&argv, "\0");
159                 if (!value || !family || !key) {
160                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
161                         return 0;
162                 }
163                 if (option_verbose > 2)
164                         ast_verbose(VERBOSE_PREFIX_3 "DBput: family=%s, key=%s, value=%s\n", family, key, value);
165                 if (ast_db_put(family, key, value)) {
166                         if (option_verbose > 2)
167                                 ast_verbose(VERBOSE_PREFIX_3 "DBput: Error writing value to database.\n");
168                 }
169
170         } else  {
171                 ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
172         }
173         return 0;
174 }
175
176 static int get_exec(struct ast_channel *chan, void *data)
177 {
178         int arglen;
179         char *argv, *varname, *family, *key;
180         char dbresult[256];
181         static int dep_warning = 0;
182
183         if (!dep_warning) {
184                 ast_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n");
185                 dep_warning = 1;
186         }
187         
188         arglen = strlen(data);
189         argv = alloca(arglen + 1);
190         if (!argv) {    /* Why would this fail? */
191                 ast_log(LOG_DEBUG, "Memory allocation failed\n");
192                 return 0;
193         }
194         memcpy(argv, data, arglen + 1);
195
196         if (strchr(argv, '=') && strchr(argv, '/')) {
197                 varname = strsep(&argv, "=");
198                 family = strsep(&argv, "/");
199                 key = strsep(&argv, "\0");
200                 if (!varname || !family || !key) {
201                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
202                         return 0;
203                 }
204                 if (option_verbose > 2)
205                         ast_verbose(VERBOSE_PREFIX_3 "DBget: varname=%s, family=%s, key=%s\n", varname, family, key);
206                 if (!ast_db_get(family, key, dbresult, sizeof (dbresult) - 1)) {
207                         pbx_builtin_setvar_helper(chan, varname, dbresult);
208                         if (option_verbose > 2)
209                                 ast_verbose(VERBOSE_PREFIX_3 "DBget: set variable %s to %s\n", varname, dbresult);
210                 } else {
211                         if (option_verbose > 2)
212                         ast_verbose(VERBOSE_PREFIX_3 "DBget: Value not found in database.\n");
213                         /* Send the call to n+101 priority, where n is the current priority */
214                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
215                                 chan->priority += 100;
216                 }
217
218         } else {
219                 ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
220         }
221         return 0;
222 }
223
224 int unload_module(void)
225 {
226         int retval;
227
228         STANDARD_HANGUP_LOCALUSERS;
229         retval = ast_unregister_application(dt_app);
230         retval |= ast_unregister_application(d_app);
231         retval |= ast_unregister_application(p_app);
232         retval |= ast_unregister_application(g_app);
233
234         return retval;
235 }
236
237 int load_module(void)
238 {
239         int retval;
240
241         retval = ast_register_application(g_app, get_exec, g_synopsis, g_descrip);
242         if (!retval)
243                 retval = ast_register_application(p_app, put_exec, p_synopsis, p_descrip);
244         if (!retval)
245                 retval = ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
246         if (!retval)
247                 retval = ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
248         return retval;
249 }
250
251 char *description(void)
252 {
253         return tdesc;
254 }
255
256 int usecount(void)
257 {
258         int res;
259         STANDARD_USECOUNT(res);
260         return res;
261 }
262
263 char *key()
264 {
265         return ASTERISK_GPL_KEY;
266 }