Applications no longer need to call ast_module_user_add and ast_module_user_remove...
[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 "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/types.h>
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 /*! \todo XXX Remove this application after 1.4 is relased */
52 static char *d_descrip =
53 "  DBdel(family/key): This application will delete a key from the Asterisk\n"
54 "database.\n"
55 "  This application has been DEPRECATED in favor of the DB_DELETE function.\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
68 static int deltree_exec(struct ast_channel *chan, void *data)
69 {
70         char *argv, *family, *keytree;
71
72         argv = ast_strdupa(data);
73
74         if (strchr(argv, '/')) {
75                 family = strsep(&argv, "/");
76                 keytree = strsep(&argv, "\0");
77                         if (!family || !keytree) {
78                                 ast_debug(1, "Ignoring; Syntax error in argument\n");
79                                 return 0;
80                         }
81                 if (ast_strlen_zero(keytree))
82                         keytree = 0;
83         } else {
84                 family = argv;
85                 keytree = 0;
86         }
87
88         if (option_verbose > 2) {
89                 if (keytree)
90                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
91                 else
92                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
93         }
94
95         if (ast_db_deltree(family, keytree)) {
96                 if (option_verbose > 2)
97                         ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
98         }
99
100         return 0;
101 }
102
103 static int del_exec(struct ast_channel *chan, void *data)
104 {
105         char *argv, *family, *key;
106         static int deprecation_warning = 0;
107
108         if (!deprecation_warning) {
109                 deprecation_warning = 1;
110                 ast_log(LOG_WARNING, "The DBdel application has been deprecated in favor of the DB_DELETE dialplan function!\n");
111         }
112
113         argv = ast_strdupa(data);
114
115         if (strchr(argv, '/')) {
116                 family = strsep(&argv, "/");
117                 key = strsep(&argv, "\0");
118                 if (!family || !key) {
119                         ast_debug(1, "Ignoring; Syntax error in argument\n");
120                         return 0;
121                 }
122                 if (option_verbose > 2)
123                         ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
124                 if (ast_db_del(family, key)) {
125                         if (option_verbose > 2)
126                                 ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
127                 }
128         } else {
129                 ast_debug(1, "Ignoring, no parameters\n");
130         }
131         
132         return 0;
133 }
134
135 static int unload_module(void)
136 {
137         int retval;
138
139         retval = ast_unregister_application(dt_app);
140         retval |= ast_unregister_application(d_app);
141
142         return retval;
143 }
144
145 static int load_module(void)
146 {
147         int retval;
148
149         retval = ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
150         retval |= ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
151         
152         return retval;
153 }
154
155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database Access Functions");