2 * Asterisk -- A telephony toolkit for Linux.
4 * Functions for interaction with the Asterisk database
6 * Copyright (C) 2005, Russell Bryant <russelb@clemson.edu>
8 * func_db.c adapted from the old app_db.c, copyright by the following people
9 * Copyright (C) 2005, Mark Spencer <markster@digium.com>
10 * Copyright (C) 2003, Jefferson Noxon <jeff@debian.org>
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License
18 #include <sys/types.h>
21 #include "asterisk/channel.h"
22 #include "asterisk/pbx.h"
23 #include "asterisk/logger.h"
24 #include "asterisk/options.h"
25 #include "asterisk/utils.h"
26 #include "asterisk/app.h"
27 #include "asterisk/astdb.h"
29 static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
37 if (!data || ast_strlen_zero(data)) {
38 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
42 args = ast_strdupa(data);
43 argc = ast_separate_app_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
49 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
53 if (ast_db_get(family, key, buf, len-1)) {
54 ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", family, key);
56 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
62 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
70 if (!data || ast_strlen_zero(data)) {
71 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
75 args = ast_strdupa(data);
76 argc = ast_separate_app_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
82 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
86 if (ast_db_put(family, key, (char*)value)) {
87 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
94 struct ast_custom_function db_function = {
96 .synopsis = "Read or Write from/to the Asterisk database",
97 .syntax = "DB(<family>/<key>)",
98 .desc = "This function will read or write a value from/to the Asterisk database.\n"
99 "DB(...) will read a value from the database, while DB(...)=value\n"
100 "will write a value to the database. On a read, this function\n"
101 "returns the value from the datase, or NULL if it does not exist.\n"
102 "On a write, this function will always return NULL. Reading a database value\n"
103 "will also set the global variable DB_RESULT.\n",
104 .read = function_db_read,
105 .write = function_db_write,
108 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
116 if (!data || ast_strlen_zero(data)) {
117 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
121 args = ast_strdupa(data);
122 argc = ast_separate_app_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
128 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
132 if (ast_db_get(family, key, buf, len-1))
133 ast_copy_string(buf, "0", len);
135 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
136 ast_copy_string(buf, "1", len);
145 struct ast_custom_function db_exists_function = {
147 .synopsis = "Check to see if a key exists in the Asterisk database",
148 .syntax = "DB_EXISTS(<family>/<key>)",
149 .desc = "This function will check to see if a key exists in the Asterisk\n"
150 "database. If it exists, the function will return \"1\". If not,\n"
151 "it will return \"0\". Checking for existance of database value will\n"
152 "also set the global variable DB_RESULT to that value if it exists.\n",
153 .read = function_db_exists,