Add MASTER_CHANNEL() dialplan function, as well as a useful usage.
[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 /*** DOCUMENTATION
44         <function name="DB" language="en_US">
45                 <synopsis>
46                         Read from or write to the Asterisk database.
47                 </synopsis>
48                 <syntax argsep="/">
49                         <parameter name="family" required="true" />
50                         <parameter name="key" required="true" />
51                 </syntax>
52                 <description>
53                         <para>This function will read from or write a value to the Asterisk database.  On a
54                         read, this function returns the corresponding value from the database, or blank
55                         if it does not exist.  Reading a database value will also set the variable
56                         DB_RESULT.  If you wish to find out if an entry exists, use the DB_EXISTS
57                         function.</para>
58                 </description>
59                 <see-also>
60                         <ref type="application">DBdel</ref>
61                         <ref type="function">DB_DELETE</ref>
62                         <ref type="application">DBdeltree</ref>
63                         <ref type="function">DB_EXISTS</ref>
64                 </see-also>
65         </function>
66         <function name="DB_EXISTS" language="en_US">
67                 <synopsis>
68                         Check to see if a key exists in the Asterisk database.
69                 </synopsis>
70                 <syntax argsep="/">
71                         <parameter name="family" required="true" />
72                         <parameter name="key" required="true" />
73                 </syntax>
74                 <description>
75                         <para>This function will check to see if a key exists in the Asterisk
76                         database. If it exists, the function will return <literal>1</literal>. If not,
77                         it will return <literal>0</literal>.  Checking for existence of a database key will
78                         also set the variable DB_RESULT to the key's value if it exists.</para>
79                 </description>
80                 <see-also>
81                         <ref type="function">DB</ref>
82                 </see-also>
83         </function>
84         <function name="DB_DELETE" language="en_US">
85                 <synopsis>
86                         Return a value from the database and delete it.
87                 </synopsis>
88                 <syntax argsep="/">
89                         <parameter name="family" required="true" />
90                         <parameter name="key" required="true" />
91                 </syntax>
92                 <description>
93                         <para>This function will retrieve a value from the Asterisk database
94                         and then remove that key from the database. <variable>DB_RESULT</variable>
95                         will be set to the key's value if it exists.</para>
96                 </description>
97                 <see-also>
98                         <ref type="application">DBdel</ref>
99                         <ref type="function">DB</ref>
100                         <ref type="application">DBdeltree</ref>
101                 </see-also>
102         </function>
103  ***/
104
105 static int function_db_read(struct ast_channel *chan, const char *cmd,
106                             char *parse, char *buf, size_t len)
107 {
108         AST_DECLARE_APP_ARGS(args,
109                 AST_APP_ARG(family);
110                 AST_APP_ARG(key);
111         );
112
113         buf[0] = '\0';
114
115         if (ast_strlen_zero(parse)) {
116                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
117                 return -1;
118         }
119
120         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
121
122         if (args.argc < 2) {
123                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
124                 return -1;
125         }
126
127         if (ast_db_get(args.family, args.key, buf, len - 1)) {
128                 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
129         } else {
130                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
131         }
132
133         return 0;
134 }
135
136 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
137                              const char *value)
138 {
139         AST_DECLARE_APP_ARGS(args,
140                 AST_APP_ARG(family);
141                 AST_APP_ARG(key);
142         );
143
144         if (ast_strlen_zero(parse)) {
145                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
146                 return -1;
147         }
148
149         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
150
151         if (args.argc < 2) {
152                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
153                 return -1;
154         }
155
156         if (ast_db_put(args.family, args.key, value)) {
157                 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
158         }
159
160         return 0;
161 }
162
163 static struct ast_custom_function db_function = {
164         .name = "DB",
165         .read = function_db_read,
166         .write = function_db_write,
167 };
168
169 static int function_db_exists(struct ast_channel *chan, const char *cmd,
170                               char *parse, char *buf, size_t len)
171 {
172         AST_DECLARE_APP_ARGS(args,
173                 AST_APP_ARG(family);
174                 AST_APP_ARG(key);
175         );
176
177         buf[0] = '\0';
178
179         if (ast_strlen_zero(parse)) {
180                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
181                 return -1;
182         }
183
184         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
185
186         if (args.argc < 2) {
187                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
188                 return -1;
189         }
190
191         if (ast_db_get(args.family, args.key, buf, len - 1)) {
192                 strcpy(buf, "0");
193         } else {
194                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
195                 strcpy(buf, "1");
196         }
197
198         return 0;
199 }
200
201 static struct ast_custom_function db_exists_function = {
202         .name = "DB_EXISTS",
203         .read = function_db_exists,
204         .read_max = 2,
205 };
206
207 static int function_db_delete(struct ast_channel *chan, const char *cmd,
208                               char *parse, char *buf, size_t len)
209 {
210         AST_DECLARE_APP_ARGS(args,
211                 AST_APP_ARG(family);
212                 AST_APP_ARG(key);
213         );
214
215         buf[0] = '\0';
216
217         if (ast_strlen_zero(parse)) {
218                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
219                 return -1;
220         }
221
222         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
223
224         if (args.argc < 2) {
225                 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
226                 return -1;
227         }
228
229         if (ast_db_get(args.family, args.key, buf, len - 1)) {
230                 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
231         } else {
232                 if (ast_db_del(args.family, args.key)) {
233                         ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
234                 }
235         }
236
237         pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
238
239         return 0;
240 }
241
242
243 static struct ast_custom_function db_delete_function = {
244         .name = "DB_DELETE",
245         .read = function_db_delete,
246 };
247
248 static int unload_module(void)
249 {
250         int res = 0;
251
252         res |= ast_custom_function_unregister(&db_function);
253         res |= ast_custom_function_unregister(&db_exists_function);
254         res |= ast_custom_function_unregister(&db_delete_function);
255
256         return res;
257 }
258
259 static int load_module(void)
260 {
261         int res = 0;
262
263         res |= ast_custom_function_register(&db_function);
264         res |= ast_custom_function_register(&db_exists_function);
265         res |= ast_custom_function_register(&db_delete_function);
266
267         return res;
268 }
269
270 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");