3125d8ecaa1303383307d1553720f936f1607b9d
[asterisk/asterisk.git] / funcs / func_db.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, 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
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <regex.h>
32
33 #include "asterisk.h"
34
35 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
36
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/options.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/astdb.h"
44
45 static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
46 {
47         char *parse;    
48         AST_DECLARE_APP_ARGS(args,
49                 AST_APP_ARG(family);
50                 AST_APP_ARG(key);
51         );
52
53         if (ast_strlen_zero(data)) {
54                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
55                 buf[0] = '\0';
56                 return buf;
57         }
58
59         parse = ast_strdupa(data);
60         if (!parse) {
61                 ast_log(LOG_ERROR, "Out of memory!\n");
62                 buf[0] = '\0';
63                 return buf;
64         }
65                 
66         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
67         
68         if (args.argc < 2) {
69                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
70                 buf[0] = '\0';
71                 return buf;
72         }
73
74         if (ast_db_get(args.family, args.key, buf, len-1)) {
75                 ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", args.family, args.key);
76         } else
77                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
78
79         
80         return buf;
81 }
82
83 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
84 {
85         char *parse;    
86         AST_DECLARE_APP_ARGS(args,
87                 AST_APP_ARG(family);
88                 AST_APP_ARG(key);
89         );
90
91         if (ast_strlen_zero(data)) {
92                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
93                 return;
94         }
95
96         parse = ast_strdupa(data);
97         if (!parse) { 
98                 ast_log(LOG_ERROR, "Out of memory!\n"); 
99                 return;
100         }
101
102         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
103
104         if (args.argc < 2) {
105                 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
106                 return;
107         }
108
109         if (ast_db_put(args.family, args.key, (char*)value)) {
110                 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
111         }
112 }
113
114 #ifndef BUILTIN_FUNC
115 static
116 #endif
117 struct ast_custom_function db_function = {
118         .name = "DB",
119         .synopsis = "Read or Write from/to the Asterisk database",
120         .syntax = "DB(<family>/<key>)",
121         .desc = "This function will read or write a value from/to the Asterisk database.\n"
122                 "DB(...) will read a value from the database, while DB(...)=value\n"
123                 "will write a value to the database.  On a read, this function\n"
124                 "returns the value from the datase, or NULL if it does not exist.\n"
125                 "On a write, this function will always return NULL.  Reading a database value\n"
126                 "will also set the variable DB_RESULT.\n",
127         .read = function_db_read,
128         .write = function_db_write,
129 };
130
131 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
132 {
133         char *parse;    
134         AST_DECLARE_APP_ARGS(args,
135                 AST_APP_ARG(family);
136                 AST_APP_ARG(key);
137         );
138
139         if (ast_strlen_zero(data)) {
140                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
141                 buf[0] = '\0';
142                 return buf;
143         }
144
145         parse = ast_strdupa(data);
146         if (!parse) {
147                 ast_log(LOG_ERROR, "Out of memory!\n");
148                 buf[0] = '\0';
149                 return buf;
150         }
151                 
152         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
153         
154         if (args.argc < 2) {
155                 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
156                 buf[0] = '\0';
157                 return buf;
158         }
159
160         if (ast_db_get(args.family, args.key, buf, len-1))
161                 ast_copy_string(buf, "0", len); 
162         else {
163                 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
164                 ast_copy_string(buf, "1", len);
165         }
166         
167         return buf;
168 }
169
170 #ifndef BUILTIN_FUNC
171 static
172 #endif
173 struct ast_custom_function db_exists_function = {
174         .name = "DB_EXISTS",
175         .synopsis = "Check to see if a key exists in the Asterisk database",
176         .syntax = "DB_EXISTS(<family>/<key>)",
177         .desc = "This function will check to see if a key exists in the Asterisk\n"
178                 "database. If it exists, the function will return \"1\". If not,\n"
179                 "it will return \"0\".  Checking for existence of a database key will\n"
180                 "also set the variable DB_RESULT to the key's value if it exists.\n",
181         .read = function_db_exists,
182 };