- spaces to tabs
[asterisk/asterisk.git] / funcs / func_db.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005-2006, Russell Bryant <russelb@clemson.edu> 
5  *
6  * func_db.c adapted from the old app_db.c, copyright by the following people 
7  * Copyright (C) 2005, Mark Spencer <markster@digium.com>
8  * Copyright (C) 2003, 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 Functions for interaction with the Asterisk database
24  *
25  * \author Russell Bryant <russelb@clemson.edu>
26  *
27  * \ingroup functions
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <regex.h>
35
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
41 #include "asterisk/astdb.h"
42
43 static int function_db_read(struct ast_channel *chan, const char *cmd,
44                             char *parse, char *buf, size_t len)
45 {
46         AST_DECLARE_APP_ARGS(args,
47                 AST_APP_ARG(family);
48                 AST_APP_ARG(key);
49         );
50
51         buf[0] = '\0';
52
53         if (ast_strlen_zero(parse)) {
54                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
55                 return -1;
56         }
57
58         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
59
60         if (args.argc < 2) {
61                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
62                 return -1;
63         }
64
65         if (ast_db_get(args.family, args.key, buf, len - 1)) {
66                 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
67         } else {
68                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
69         }
70
71         return 0;
72 }
73
74 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
75                              const char *value)
76 {
77         AST_DECLARE_APP_ARGS(args,
78                 AST_APP_ARG(family);
79                 AST_APP_ARG(key);
80         );
81
82         if (ast_strlen_zero(parse)) {
83                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
84                 return -1;
85         }
86
87         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
88
89         if (args.argc < 2) {
90                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
91                 return -1;
92         }
93
94         if (ast_db_put(args.family, args.key, value)) {
95                 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
96         }
97
98         return 0;
99 }
100
101 static struct ast_custom_function db_function = {
102         .name = "DB",
103         .synopsis = "Read from or write to the Asterisk database",
104         .syntax = "DB(<family>/<key>)",
105         .desc =
106 "This function will read from or write a value to the Asterisk database.  On a\n"
107 "read, this function returns the corresponding value from the database, or blank\n"
108 "if it does not exist.  Reading a database value will also set the variable\n"
109 "DB_RESULT.  If you wish to find out if an entry exists, use the DB_EXISTS\n"
110 "function.\n",
111         .read = function_db_read,
112         .write = function_db_write,
113 };
114
115 static int function_db_exists(struct ast_channel *chan, const char *cmd,
116                               char *parse, char *buf, size_t len)
117 {
118         AST_DECLARE_APP_ARGS(args,
119                 AST_APP_ARG(family);
120                 AST_APP_ARG(key);
121         );
122
123         buf[0] = '\0';
124
125         if (ast_strlen_zero(parse)) {
126                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
127                 return -1;
128         }
129
130         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
131
132         if (args.argc < 2) {
133                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
134                 return -1;
135         }
136
137         if (ast_db_get(args.family, args.key, buf, len - 1)) {
138                 strcpy(buf, "0");
139         } else {
140                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
141                 strcpy(buf, "1");
142         }
143
144         return 0;
145 }
146
147 static struct ast_custom_function db_exists_function = {
148         .name = "DB_EXISTS",
149         .synopsis = "Check to see if a key exists in the Asterisk database",
150         .syntax = "DB_EXISTS(<family>/<key>)",
151         .desc =
152                 "This function will check to see if a key exists in the Asterisk\n"
153                 "database. If it exists, the function will return \"1\". If not,\n"
154                 "it will return \"0\".  Checking for existence of a database key will\n"
155                 "also set the variable DB_RESULT to the key's value if it exists.\n",
156         .read = function_db_exists,
157 };
158
159 static int function_db_delete(struct ast_channel *chan, const char *cmd,
160                               char *parse, char *buf, size_t len)
161 {
162         AST_DECLARE_APP_ARGS(args,
163                 AST_APP_ARG(family);
164                 AST_APP_ARG(key);
165         );
166
167         buf[0] = '\0';
168
169         if (ast_strlen_zero(parse)) {
170                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
171                 return -1;
172         }
173
174         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
175
176         if (args.argc < 2) {
177                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
178                 return -1;
179         }
180
181         if (ast_db_get(args.family, args.key, buf, len - 1)) {
182                 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
183         } else {
184                 if (ast_db_del(args.family, args.key)) {
185                         ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
186                 }
187         }
188
189         pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
190
191         return 0;
192 }
193
194
195 static struct ast_custom_function db_delete_function = {
196         .name = "DB_DELETE",
197         .synopsis = "Return a value from the database and delete it",
198         .syntax = "DB_DELETE(<family>/<key>)",
199         .desc =
200                 "This function will retrieve a value from the Asterisk database\n"
201                 " and then remove that key from the database.  DB_RESULT\n"
202                 "will be set to the key's value if it exists.\n",
203         .read = function_db_delete,
204 };
205
206 static int unload_module(void)
207 {
208         int res = 0;
209
210         res |= ast_custom_function_unregister(&db_function);
211         res |= ast_custom_function_unregister(&db_exists_function);
212         res |= ast_custom_function_unregister(&db_delete_function);
213
214         return res;
215 }
216
217 static int load_module(void)
218 {
219         int res = 0;
220
221         res |= ast_custom_function_register(&db_function);
222         res |= ast_custom_function_register(&db_exists_function);
223         res |= ast_custom_function_register(&db_delete_function);
224
225         return res;
226 }
227
228 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");