Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / funcs / func_db.c
blob1c15e5df87533487f1df82d89e3284c46282ac58
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005-2006, Russell Bryant <russelb@clemson.edu>
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>
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.
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.
21 /*! \file
23 * \brief Functions for interaction with the Asterisk database
25 * \author Russell Bryant <russelb@clemson.edu>
27 * \ingroup functions
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <regex.h>
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"
43 static int function_db_read(struct ast_channel *chan, const char *cmd,
44 char *parse, char *buf, size_t len)
46 AST_DECLARE_APP_ARGS(args,
47 AST_APP_ARG(family);
48 AST_APP_ARG(key);
51 buf[0] = '\0';
53 if (ast_strlen_zero(parse)) {
54 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
55 return -1;
58 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
60 if (args.argc < 2) {
61 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
62 return -1;
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);
70 return 0;
73 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
74 const char *value)
76 AST_DECLARE_APP_ARGS(args,
77 AST_APP_ARG(family);
78 AST_APP_ARG(key);
81 if (ast_strlen_zero(parse)) {
82 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
83 return -1;
86 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
88 if (args.argc < 2) {
89 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
90 return -1;
93 if (ast_db_put(args.family, args.key, (char *) value))
94 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
96 return 0;
99 static struct ast_custom_function db_function = {
100 .name = "DB",
101 .synopsis = "Read from or write to the Asterisk database",
102 .syntax = "DB(<family>/<key>)",
103 .desc =
104 "This function will read from or write a value to the Asterisk database. On a\n"
105 "read, this function returns the corresponding value from the database, or blank\n"
106 "if it does not exist. Reading a database value will also set the variable\n"
107 "DB_RESULT. If you wish to find out if an entry exists, use the DB_EXISTS\n"
108 "function.\n",
109 .read = function_db_read,
110 .write = function_db_write,
113 static int function_db_exists(struct ast_channel *chan, const char *cmd,
114 char *parse, char *buf, size_t len)
116 AST_DECLARE_APP_ARGS(args,
117 AST_APP_ARG(family);
118 AST_APP_ARG(key);
121 buf[0] = '\0';
123 if (ast_strlen_zero(parse)) {
124 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
125 return -1;
128 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
130 if (args.argc < 2) {
131 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
132 return -1;
135 if (ast_db_get(args.family, args.key, buf, len - 1))
136 strcpy(buf, "0");
137 else {
138 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
139 strcpy(buf, "1");
142 return 0;
145 static struct ast_custom_function db_exists_function = {
146 .name = "DB_EXISTS",
147 .synopsis = "Check to see if a key exists in the Asterisk database",
148 .syntax = "DB_EXISTS(<family>/<key>)",
149 .desc =
150 "This function will check to see if a key exists in the Asterisk\n"
151 "database. If it exists, the function will return \"1\". If not,\n"
152 "it will return \"0\". Checking for existence of a database key will\n"
153 "also set the variable DB_RESULT to the key's value if it exists.\n",
154 .read = function_db_exists,
157 static int function_db_delete(struct ast_channel *chan, const char *cmd,
158 char *parse, char *buf, size_t len)
160 AST_DECLARE_APP_ARGS(args,
161 AST_APP_ARG(family);
162 AST_APP_ARG(key);
165 buf[0] = '\0';
167 if (ast_strlen_zero(parse)) {
168 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
169 return -1;
172 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
174 if (args.argc < 2) {
175 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
176 return -1;
179 if (ast_db_get(args.family, args.key, buf, len - 1)) {
180 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
181 } else {
182 if (ast_db_del(args.family, args.key)) {
183 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
186 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
188 return 0;
192 static struct ast_custom_function db_delete_function = {
193 .name = "DB_DELETE",
194 .synopsis = "Return a value from the database and delete it",
195 .syntax = "DB_DELETE(<family>/<key>)",
196 .desc =
197 "This function will retrieve a value from the Asterisk database\n"
198 " and then remove that key from the database. DB_RESULT\n"
199 "will be set to the key's value if it exists.\n",
200 .read = function_db_delete,
203 static int unload_module(void)
205 int res = 0;
207 res |= ast_custom_function_unregister(&db_function);
208 res |= ast_custom_function_unregister(&db_exists_function);
209 res |= ast_custom_function_unregister(&db_delete_function);
211 return res;
214 static int load_module(void)
216 int res = 0;
218 res |= ast_custom_function_register(&db_function);
219 res |= ast_custom_function_register(&db_exists_function);
220 res |= ast_custom_function_register(&db_delete_function);
222 return res;
225 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");