normalize code preparing for loader changes
[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 *d_descrip =
52 "  DBdel(family/key): This applicaiton will delete a key from the Asterisk\n"
53 "database.\n";
54
55 static char *dt_descrip =
56 "  DBdeltree(family[/keytree]): This application will delete a family or keytree\n"
57 "from the Asterisk database\n";
58
59 static char *d_app = "DBdel";
60 static char *dt_app = "DBdeltree";
61
62 static char *d_synopsis = "Delete a key from the database";
63 static char *dt_synopsis = "Delete a family or keytree from the database";
64
65 LOCAL_USER_DECL;
66
67 static int deltree_exec(struct ast_channel *chan, void *data)
68 {
69         char *argv, *family, *keytree;
70         struct localuser *u;
71
72         LOCAL_USER_ADD(u);
73
74         if (!(argv = ast_strdupa(data))) {
75                 LOCAL_USER_REMOVE(u);
76                 return 0;
77         }
78
79         if (strchr(argv, '/')) {
80                 family = strsep(&argv, "/");
81                 keytree = strsep(&argv, "\0");
82                         if (!family || !keytree) {
83                                 ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
84                                 LOCAL_USER_REMOVE(u);
85                                 return 0;
86                         }
87                 if (ast_strlen_zero(keytree))
88                         keytree = 0;
89         } else {
90                 family = argv;
91                 keytree = 0;
92         }
93
94         if (option_verbose > 2) {
95                 if (keytree)
96                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
97                 else
98                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
99         }
100
101         if (ast_db_deltree(family, keytree)) {
102                 if (option_verbose > 2)
103                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
104         }
105
106         LOCAL_USER_REMOVE(u);
107
108         return 0;
109 }
110
111 static int del_exec(struct ast_channel *chan, void *data)
112 {
113         char *argv, *family, *key;
114         struct localuser *u;
115
116         LOCAL_USER_ADD(u);
117
118         if (!(argv = ast_strdupa(data))) {
119                 LOCAL_USER_REMOVE(u);
120                 return 0;
121         }
122
123         if (strchr(argv, '/')) {
124                 family = strsep(&argv, "/");
125                 key = strsep(&argv, "\0");
126                 if (!family || !key) {
127                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
128                         LOCAL_USER_REMOVE(u);
129                         return 0;
130                 }
131                 if (option_verbose > 2)
132                         ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
133                 if (ast_db_del(family, key)) {
134                         if (option_verbose > 2)
135                                 ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
136                 }
137         } else {
138                 ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
139         }
140
141         LOCAL_USER_REMOVE(u);
142         
143         return 0;
144 }
145
146 int unload_module(void)
147 {
148         int retval;
149
150         retval = ast_unregister_application(dt_app);
151         retval |= ast_unregister_application(d_app);
152
153         STANDARD_HANGUP_LOCALUSERS;
154
155         return retval;
156 }
157
158 int load_module(void)
159 {
160         int retval;
161
162         retval = ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
163         retval |= ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
164         
165         return retval;
166 }
167
168 const char *description(void)
169 {
170         return "Database Access Functions";
171 }
172
173 int usecount(void)
174 {
175         int res;
176         STANDARD_USECOUNT(res);
177         return res;
178 }
179
180 const char *key(void)
181 {
182         return ASTERISK_GPL_KEY;
183 }