Correction to commmit 120863, make sure proper destructor function is called as well...
[asterisk-bristuff.git] / funcs / func_db.c
blob13932d27d3c9b89f7032d643eff2d734c92fa951
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>
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <regex.h>
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/options.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/app.h"
45 #include "asterisk/astdb.h"
47 static int function_db_read(struct ast_channel *chan, char *cmd,
48 char *parse, char *buf, size_t len)
50 AST_DECLARE_APP_ARGS(args,
51 AST_APP_ARG(family);
52 AST_APP_ARG(key);
55 buf[0] = '\0';
57 if (ast_strlen_zero(parse)) {
58 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
59 return -1;
62 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
64 if (args.argc < 2) {
65 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
66 return -1;
69 if (ast_db_get(args.family, args.key, buf, len - 1)) {
70 ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", args.family,
71 args.key);
72 } else
73 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
75 return 0;
78 static int function_db_write(struct ast_channel *chan, char *cmd, char *parse,
79 const char *value)
81 AST_DECLARE_APP_ARGS(args,
82 AST_APP_ARG(family);
83 AST_APP_ARG(key);
86 if (ast_strlen_zero(parse)) {
87 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
88 return -1;
91 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
93 if (args.argc < 2) {
94 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
95 return -1;
98 if (ast_db_put(args.family, args.key, (char *) value))
99 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
101 return 0;
104 static struct ast_custom_function db_function = {
105 .name = "DB",
106 .synopsis = "Read from or write to the Asterisk database",
107 .syntax = "DB(<family>/<key>)",
108 .desc =
109 "This function will read from or write a value to the Asterisk database. On a\n"
110 "read, this function returns the corresponding value from the database, or blank\n"
111 "if it does not exist. Reading a database value will also set the variable\n"
112 "DB_RESULT. If you wish to find out if an entry exists, use the DB_EXISTS\n"
113 "function.\n",
114 .read = function_db_read,
115 .write = function_db_write,
118 static int function_db_exists(struct ast_channel *chan, char *cmd,
119 char *parse, char *buf, size_t len)
121 AST_DECLARE_APP_ARGS(args,
122 AST_APP_ARG(family);
123 AST_APP_ARG(key);
126 buf[0] = '\0';
128 if (ast_strlen_zero(parse)) {
129 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
130 return -1;
133 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
135 if (args.argc < 2) {
136 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
137 return -1;
140 if (ast_db_get(args.family, args.key, buf, len - 1))
141 strcpy(buf, "0");
142 else {
143 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
144 strcpy(buf, "1");
147 return 0;
150 static struct ast_custom_function db_exists_function = {
151 .name = "DB_EXISTS",
152 .synopsis = "Check to see if a key exists in the Asterisk database",
153 .syntax = "DB_EXISTS(<family>/<key>)",
154 .desc =
155 "This function will check to see if a key exists in the Asterisk\n"
156 "database. If it exists, the function will return \"1\". If not,\n"
157 "it will return \"0\". Checking for existence of a database key will\n"
158 "also set the variable DB_RESULT to the key's value if it exists.\n",
159 .read = function_db_exists,
162 static int function_db_delete(struct ast_channel *chan, char* cmd,
163 char *parse, char *buf, size_t len)
165 AST_DECLARE_APP_ARGS(args,
166 AST_APP_ARG(family);
167 AST_APP_ARG(key);
170 buf[0] = '\0';
172 if (ast_strlen_zero(parse)) {
173 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
174 return -1;
177 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
179 if (args.argc < 2) {
180 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
181 return -1;
184 if (ast_db_get(args.family, args.key, buf, len - 1)) {
185 ast_log(LOG_DEBUG, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
186 } else {
187 if (ast_db_del(args.family, args.key)) {
188 ast_log(LOG_DEBUG, "DB_DELETE: %s/%s could not be deleted from the database\n",
189 args.family, args.key);
192 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
194 return 0;
198 static struct ast_custom_function db_delete_function = {
199 .name = "DB_DELETE",
200 .synopsis = "Return a value from the database and delete it",
201 .syntax = "DB_DELETE(<family>/<key>)",
202 .desc =
203 "This function will retrieve a value from the Asterisk database\n"
204 " and then remove that key from the database. DB_RESULT\n"
205 "will be set to the key's value if it exists.\n",
206 .read = function_db_delete,
209 static int unload_module(void)
211 int res = 0;
213 res |= ast_custom_function_unregister(&db_function);
214 res |= ast_custom_function_unregister(&db_exists_function);
215 res |= ast_custom_function_unregister(&db_delete_function);
217 return res;
220 static int load_module(void)
222 int res = 0;
224 res |= ast_custom_function_register(&db_function);
225 res |= ast_custom_function_register(&db_exists_function);
226 res |= ast_custom_function_register(&db_delete_function);
228 return res;
231 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");