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_WARNING, "DB: %s/%s not found in database.\n", family, key);
60 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
68 if (!data || ast_strlen_zero(data)) {
69 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
73 args = ast_strdupa(data);
74 argc = ast_separate_app_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
80 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
84 if (ast_db_put(family, key, (char*)value)) {
85 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
92 struct ast_custom_function db_function = {
94 .synopsis = "Read or Write from/to the Asterisk database",
95 .syntax = "DB(<family>/<key>)",
96 .desc = "This function will read or write a value from/to the Asterisk database."
97 "DB(...) will read a value from the database, while DB(...)=value"
98 "will write a value to the database. On a read, this function"
99 "returns the value from the datase, or NULL if it does not exist."
100 "On a write, this function will always return NULL.",
101 .read = function_db_read,
102 .write = function_db_write,
105 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
113 if (!data || ast_strlen_zero(data)) {
114 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
118 args = ast_strdupa(data);
119 argc = ast_separate_app_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
125 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
129 if (ast_db_get(family, key, buf, len-1))
130 ast_copy_string(buf, "0", len);
132 ast_copy_string(buf, "1", len);
140 struct ast_custom_function db_exists_function = {
142 .synopsis = "Check to see if a key exists in the Asterisk database",
143 .syntax = "DB_EXISTS(<family>/<key>)",
144 .desc = "This function will check to see if a key exists in the Asterisk\n"
145 "database. If it exists, the function will return \"1\". If not,\n"
146 "it will return \"0\".",
147 .read = function_db_exists,