2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, 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.
23 * \brief Functions for interaction with the Asterisk database
25 * \author Russell Bryant <russelb@clemson.edu>
30 #include <sys/types.h>
35 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
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"
45 static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
48 AST_DECLARE_APP_ARGS(args,
53 if (ast_strlen_zero(data)) {
54 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
59 parse = ast_strdupa(data);
61 ast_log(LOG_ERROR, "Out of memory!\n");
66 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
69 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
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);
77 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
83 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
86 AST_DECLARE_APP_ARGS(args,
91 if (ast_strlen_zero(data)) {
92 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
96 parse = ast_strdupa(data);
98 ast_log(LOG_ERROR, "Out of memory!\n");
102 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
105 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
109 if (ast_db_put(args.family, args.key, (char*)value)) {
110 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
117 struct ast_custom_function db_function = {
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,
131 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
134 AST_DECLARE_APP_ARGS(args,
139 if (ast_strlen_zero(data)) {
140 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
145 parse = ast_strdupa(data);
147 ast_log(LOG_ERROR, "Out of memory!\n");
152 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
155 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
160 if (ast_db_get(args.family, args.key, buf, len-1))
161 ast_copy_string(buf, "0", len);
163 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
164 ast_copy_string(buf, "1", len);
173 struct ast_custom_function db_exists_function = {
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,