revert my pass through the tree to remove checks of the result of ast_strdupa
[asterisk/asterisk.git] / apps / app_db.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  * Copyright (C) 2003, Jefferson Noxon
6  *
7  * Mark Spencer <markster@digium.com>
8  * 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 Database access functions
24  *
25  * \author Mark Spencer <markster@digium.com>
26  * \author Jefferson Noxon <jeff@debian.org>
27  *
28  * \ingroup applications
29  */
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include "asterisk/options.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/astdb.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/options.h"
50
51 static char *tdesc = "Database Access Functions";
52
53 static char *d_descrip =
54 "  DBdel(family/key): This applicaiton will delete a key from the Asterisk\n"
55 "database.\n";
56
57 static char *dt_descrip =
58 "  DBdeltree(family[/keytree]): This application will delete a family or keytree\n"
59 "from the Asterisk database\n";
60
61 static char *d_app = "DBdel";
62 static char *dt_app = "DBdeltree";
63
64 static char *d_synopsis = "Delete a key from the database";
65 static char *dt_synopsis = "Delete a family or keytree from the database";
66
67 STANDARD_LOCAL_USER;
68
69 LOCAL_USER_DECL;
70
71 static int deltree_exec(struct ast_channel *chan, void *data)
72 {
73         char *argv, *family, *keytree;
74         struct localuser *u;
75
76         LOCAL_USER_ADD(u);
77
78         argv = ast_strdupa(data);
79         if (!argv) {
80                 ast_log(LOG_ERROR, "Memory allocation failed\n");
81                 LOCAL_USER_REMOVE(u);
82                 return 0;
83         }
84
85         if (strchr(argv, '/')) {
86                 family = strsep(&argv, "/");
87                 keytree = strsep(&argv, "\0");
88                         if (!family || !keytree) {
89                                 ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
90                                 LOCAL_USER_REMOVE(u);
91                                 return 0;
92                         }
93                 if (ast_strlen_zero(keytree))
94                         keytree = 0;
95         } else {
96                 family = argv;
97                 keytree = 0;
98         }
99
100         if (option_verbose > 2) {
101                 if (keytree)
102                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
103                 else
104                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
105         }
106
107         if (ast_db_deltree(family, keytree)) {
108                 if (option_verbose > 2)
109                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
110         }
111
112         LOCAL_USER_REMOVE(u);
113
114         return 0;
115 }
116
117 static int del_exec(struct ast_channel *chan, void *data)
118 {
119         char *argv, *family, *key;
120         struct localuser *u;
121
122         LOCAL_USER_ADD(u);
123
124         argv = ast_strdupa(data);
125         if (!argv) {
126                 ast_log (LOG_ERROR, "Memory allocation failed\n");
127                 LOCAL_USER_REMOVE(u);
128                 return 0;
129         }
130
131         if (strchr(argv, '/')) {
132                 family = strsep(&argv, "/");
133                 key = strsep(&argv, "\0");
134                 if (!family || !key) {
135                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
136                         LOCAL_USER_REMOVE(u);
137                         return 0;
138                 }
139                 if (option_verbose > 2)
140                         ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
141                 if (ast_db_del(family, key)) {
142                         if (option_verbose > 2)
143                                 ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
144                 }
145         } else {
146                 ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
147         }
148
149         LOCAL_USER_REMOVE(u);
150         
151         return 0;
152 }
153
154 int unload_module(void)
155 {
156         int retval;
157
158         retval = ast_unregister_application(dt_app);
159         retval |= ast_unregister_application(d_app);
160
161         STANDARD_HANGUP_LOCALUSERS;
162
163         return retval;
164 }
165
166 int load_module(void)
167 {
168         int retval;
169
170         retval = ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
171         retval |= ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
172         
173         return retval;
174 }
175
176 char *description(void)
177 {
178         return tdesc;
179 }
180
181 int usecount(void)
182 {
183         int res;
184         STANDARD_USECOUNT(res);
185         return res;
186 }
187
188 char *key()
189 {
190         return ASTERISK_GPL_KEY;
191 }