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)
53 if (ast_strlen_zero(data)) {
54 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
58 args = ast_strdupa(data);
59 argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
65 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
69 if (ast_db_get(family, key, buf, len-1)) {
70 ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", family, key);
72 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
78 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
86 if (ast_strlen_zero(data)) {
87 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
91 args = ast_strdupa(data);
92 argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
98 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
102 if (ast_db_put(family, key, (char*)value)) {
103 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
110 struct ast_custom_function db_function = {
112 .synopsis = "Read or Write from/to the Asterisk database",
113 .syntax = "DB(<family>/<key>)",
114 .desc = "This function will read or write a value from/to the Asterisk database.\n"
115 "DB(...) will read a value from the database, while DB(...)=value\n"
116 "will write a value to the database. On a read, this function\n"
117 "returns the value from the datase, or NULL if it does not exist.\n"
118 "On a write, this function will always return NULL. Reading a database value\n"
119 "will also set the variable DB_RESULT.\n",
120 .read = function_db_read,
121 .write = function_db_write,
124 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
132 if (ast_strlen_zero(data)) {
133 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
137 args = ast_strdupa(data);
138 argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
144 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
148 if (ast_db_get(family, key, buf, len-1))
149 ast_copy_string(buf, "0", len);
151 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
152 ast_copy_string(buf, "1", len);
161 struct ast_custom_function db_exists_function = {
163 .synopsis = "Check to see if a key exists in the Asterisk database",
164 .syntax = "DB_EXISTS(<family>/<key>)",
165 .desc = "This function will check to see if a key exists in the Asterisk\n"
166 "database. If it exists, the function will return \"1\". If not,\n"
167 "it will return \"0\". Checking for existence of a database key will\n"
168 "also set the variable DB_RESULT to the key's value if it exists.\n",
169 .read = function_db_exists,