Mon May 14 04:42:56 2007

Asterisk developer's documentation


func_db.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005-2006, Russell Bryant <russelb@clemson.edu> 
00005  *
00006  * func_db.c adapted from the old app_db.c, copyright by the following people 
00007  * Copyright (C) 2005, Mark Spencer <markster@digium.com>
00008  * Copyright (C) 2003, Jefferson Noxon <jeff@debian.org>
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  *
00023  * \brief Functions for interaction with the Asterisk database
00024  *
00025  * \author Russell Bryant <russelb@clemson.edu>
00026  *
00027  * \ingroup functions
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <sys/types.h>
00038 #include <regex.h>
00039 
00040 #include "asterisk/module.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/logger.h"
00044 #include "asterisk/options.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/app.h"
00047 #include "asterisk/astdb.h"
00048 
00049 static int function_db_read(struct ast_channel *chan, char *cmd,
00050              char *parse, char *buf, size_t len)
00051 {
00052    AST_DECLARE_APP_ARGS(args,
00053               AST_APP_ARG(family);
00054               AST_APP_ARG(key);
00055    );
00056 
00057    buf[0] = '\0';
00058 
00059    if (ast_strlen_zero(parse)) {
00060       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00061       return -1;
00062    }
00063 
00064    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00065 
00066    if (args.argc < 2) {
00067       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00068       return -1;
00069    }
00070 
00071    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00072       ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", args.family,
00073             args.key);
00074    } else
00075       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00076 
00077    return 0;
00078 }
00079 
00080 static int function_db_write(struct ast_channel *chan, char *cmd, char *parse,
00081               const char *value)
00082 {
00083    AST_DECLARE_APP_ARGS(args,
00084               AST_APP_ARG(family);
00085               AST_APP_ARG(key);
00086    );
00087 
00088    if (ast_strlen_zero(parse)) {
00089       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00090       return -1;
00091    }
00092 
00093    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00094 
00095    if (args.argc < 2) {
00096       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00097       return -1;
00098    }
00099 
00100    if (ast_db_put(args.family, args.key, (char *) value))
00101       ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00102 
00103    return 0;
00104 }
00105 
00106 static struct ast_custom_function db_function = {
00107    .name = "DB",
00108    .synopsis = "Read from or write to the Asterisk database",
00109    .syntax = "DB(<family>/<key>)",
00110    .desc =
00111 "This function will read from or write a value to the Asterisk database.  On a\n"
00112 "read, this function returns the corresponding value from the database, or blank\n"
00113 "if it does not exist.  Reading a database value will also set the variable\n"
00114 "DB_RESULT.  If you wish to find out if an entry exists, use the DB_EXISTS\n"
00115 "function.\n",
00116    .read = function_db_read,
00117    .write = function_db_write,
00118 };
00119 
00120 static int function_db_exists(struct ast_channel *chan, char *cmd,
00121                char *parse, char *buf, size_t len)
00122 {
00123    AST_DECLARE_APP_ARGS(args,
00124               AST_APP_ARG(family);
00125               AST_APP_ARG(key);
00126    );
00127 
00128    buf[0] = '\0';
00129 
00130    if (ast_strlen_zero(parse)) {
00131       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00132       return -1;
00133    }
00134 
00135    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00136 
00137    if (args.argc < 2) {
00138       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00139       return -1;
00140    }
00141 
00142    if (ast_db_get(args.family, args.key, buf, len - 1))
00143       strcpy(buf, "0");
00144    else {
00145       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00146       strcpy(buf, "1");
00147    }
00148 
00149    return 0;
00150 }
00151 
00152 static struct ast_custom_function db_exists_function = {
00153    .name = "DB_EXISTS",
00154    .synopsis = "Check to see if a key exists in the Asterisk database",
00155    .syntax = "DB_EXISTS(<family>/<key>)",
00156    .desc =
00157       "This function will check to see if a key exists in the Asterisk\n"
00158       "database. If it exists, the function will return \"1\". If not,\n"
00159       "it will return \"0\".  Checking for existence of a database key will\n"
00160       "also set the variable DB_RESULT to the key's value if it exists.\n",
00161    .read = function_db_exists,
00162 };
00163 
00164 static int function_db_delete(struct ast_channel *chan, char* cmd,
00165                char *parse, char *buf, size_t len)
00166 {
00167    AST_DECLARE_APP_ARGS(args,
00168               AST_APP_ARG(family);
00169               AST_APP_ARG(key);
00170    );
00171 
00172    buf[0] = '\0';
00173 
00174    if (ast_strlen_zero(parse)) {
00175       ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00176       return -1;
00177    }
00178 
00179    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00180 
00181    if (args.argc < 2) {
00182       ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00183       return -1;
00184    }
00185 
00186    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00187       ast_log(LOG_DEBUG, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
00188    } else {
00189       if (ast_db_del(args.family, args.key)) {
00190          ast_log(LOG_DEBUG, "DB_DELETE: %s/%s could not be deleted from the database\n", 
00191             args.family, args.key);
00192       }
00193    }
00194    pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00195 
00196    return 0;
00197 }
00198 
00199 
00200 static struct ast_custom_function db_delete_function = {
00201    .name = "DB_DELETE",
00202    .synopsis = "Return a value from the database and delete it",
00203    .syntax = "DB_DELETE(<family>/<key>)",
00204    .desc =
00205       "This function will retrieve a value from the Asterisk database\n"
00206       " and then remove that key from the database.  DB_RESULT\n"
00207       "will be set to the key's value if it exists.\n",
00208    .read = function_db_delete,
00209 };
00210 
00211 static int unload_module(void)
00212 {
00213    int res = 0;
00214 
00215    res |= ast_custom_function_unregister(&db_function);
00216    res |= ast_custom_function_unregister(&db_exists_function);
00217    res |= ast_custom_function_unregister(&db_delete_function);
00218 
00219    return res;
00220 }
00221 
00222 static int load_module(void)
00223 {
00224    int res = 0;
00225 
00226    res |= ast_custom_function_register(&db_function);
00227    res |= ast_custom_function_register(&db_exists_function);
00228    res |= ast_custom_function_register(&db_delete_function);
00229 
00230    return res;
00231 }
00232 
00233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");

Generated on Mon May 14 04:42:56 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1