on this pass, only remove duplicate log messages
[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         if (!(argv = ast_strdupa(data))) {
79                 LOCAL_USER_REMOVE(u);
80                 return 0;
81         }
82
83         if (strchr(argv, '/')) {
84                 family = strsep(&argv, "/");
85                 keytree = strsep(&argv, "\0");
86                         if (!family || !keytree) {
87                                 ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
88                                 LOCAL_USER_REMOVE(u);
89                                 return 0;
90                         }
91                 if (ast_strlen_zero(keytree))
92                         keytree = 0;
93         } else {
94                 family = argv;
95                 keytree = 0;
96         }
97
98         if (option_verbose > 2) {
99                 if (keytree)
100                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
101                 else
102                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
103         }
104
105         if (ast_db_deltree(family, keytree)) {
106                 if (option_verbose > 2)
107                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
108         }
109
110         LOCAL_USER_REMOVE(u);
111
112         return 0;
113 }
114
115 static int del_exec(struct ast_channel *chan, void *data)
116 {
117         char *argv, *family, *key;
118         struct localuser *u;
119
120         LOCAL_USER_ADD(u);
121
122         if (!(argv = ast_strdupa(data))) {
123                 LOCAL_USER_REMOVE(u);
124                 return 0;
125         }
126
127         if (strchr(argv, '/')) {
128                 family = strsep(&argv, "/");
129                 key = strsep(&argv, "\0");
130                 if (!family || !key) {
131                         ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
132                         LOCAL_USER_REMOVE(u);
133                         return 0;
134                 }
135                 if (option_verbose > 2)
136                         ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
137                 if (ast_db_del(family, key)) {
138                         if (option_verbose > 2)
139                                 ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
140                 }
141         } else {
142                 ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
143         }
144
145         LOCAL_USER_REMOVE(u);
146         
147         return 0;
148 }
149
150 int unload_module(void)
151 {
152         int retval;
153
154         retval = ast_unregister_application(dt_app);
155         retval |= ast_unregister_application(d_app);
156
157         STANDARD_HANGUP_LOCALUSERS;
158
159         return retval;
160 }
161
162 int load_module(void)
163 {
164         int retval;
165
166         retval = ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
167         retval |= ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
168         
169         return retval;
170 }
171
172 char *description(void)
173 {
174         return tdesc;
175 }
176
177 int usecount(void)
178 {
179         int res;
180         STANDARD_USECOUNT(res);
181         return res;
182 }
183
184 char *key()
185 {
186         return ASTERISK_GPL_KEY;
187 }