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