function formatting and header order cleanup
[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
141         arglen = strlen(data);
142         argv = alloca(arglen + 1);
143         if (!argv) {    /* Why would this fail? */
144                 ast_log(LOG_DEBUG, "Memory allocation failed\n");
145                 return 0;
146         }
147         memcpy(argv, data, arglen + 1);
148
149         if (strchr(argv, '/') && strchr(argv, '=')) {
150                 family = strsep(&argv, "/");
151                 key = strsep(&argv, "=");
152                 value = strsep(&argv, "\0");
153                 if (!value || !family || !key) {
154                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
155                         return 0;
156                 }
157                 if (option_verbose > 2)
158                         ast_verbose(VERBOSE_PREFIX_3 "DBput: family=%s, key=%s, value=%s\n", family, key, value);
159                 if (ast_db_put(family, key, value)) {
160                         if (option_verbose > 2)
161                                 ast_verbose(VERBOSE_PREFIX_3 "DBput: Error writing value to database.\n");
162                 }
163
164         } else  {
165                 ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
166         }
167         return 0;
168 }
169
170 static int get_exec(struct ast_channel *chan, void *data)
171 {
172         int arglen;
173         char *argv, *varname, *family, *key;
174         char dbresult[256];
175
176         arglen = strlen(data);
177         argv = alloca(arglen + 1);
178         if (!argv) {    /* Why would this fail? */
179                 ast_log(LOG_DEBUG, "Memory allocation failed\n");
180                 return 0;
181         }
182         memcpy(argv, data, arglen + 1);
183
184         if (strchr(argv, '=') && strchr(argv, '/')) {
185                 varname = strsep(&argv, "=");
186                 family = strsep(&argv, "/");
187                 key = strsep(&argv, "\0");
188                 if (!varname || !family || !key) {
189                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
190                         return 0;
191                 }
192                 if (option_verbose > 2)
193                         ast_verbose(VERBOSE_PREFIX_3 "DBget: varname=%s, family=%s, key=%s\n", varname, family, key);
194                 if (!ast_db_get(family, key, dbresult, sizeof (dbresult) - 1)) {
195                         pbx_builtin_setvar_helper(chan, varname, dbresult);
196                         if (option_verbose > 2)
197                                 ast_verbose(VERBOSE_PREFIX_3 "DBget: set variable %s to %s\n", varname, dbresult);
198                 } else {
199                         if (option_verbose > 2)
200                         ast_verbose(VERBOSE_PREFIX_3 "DBget: Value not found in database.\n");
201                         /* Send the call to n+101 priority, where n is the current priority */
202                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
203                                 chan->priority += 100;
204                 }
205
206         } else {
207                 ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
208         }
209         return 0;
210 }
211
212 int unload_module(void)
213 {
214         int retval;
215
216         STANDARD_HANGUP_LOCALUSERS;
217         retval = ast_unregister_application(dt_app);
218         retval |= ast_unregister_application(d_app);
219         retval |= ast_unregister_application(p_app);
220         retval |= ast_unregister_application(g_app);
221
222         return retval;
223 }
224
225 int load_module(void)
226 {
227         int retval;
228
229         retval = ast_register_application(g_app, get_exec, g_synopsis, g_descrip);
230         if (!retval)
231                 retval = ast_register_application(p_app, put_exec, p_synopsis, p_descrip);
232         if (!retval)
233                 retval = ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
234         if (!retval)
235                 retval = ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
236         return retval;
237 }
238
239 char *description(void)
240 {
241         return tdesc;
242 }
243
244 int usecount(void)
245 {
246         int res;
247         STANDARD_USECOUNT(res);
248         return res;
249 }
250
251 char *key()
252 {
253         return ASTERISK_GPL_KEY;
254 }