Sat Aug 6 00:39:29 2011

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 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 40722 $")
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <sys/types.h>
00036 #include <regex.h>
00037 
00038 #include "asterisk/module.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/options.h"
00043 #include "asterisk/utils.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/astdb.h"
00046 
00047 static int function_db_read(struct ast_channel *chan, char *cmd,
00048              char *parse, char *buf, size_t len)
00049 {
00050    AST_DECLARE_APP_ARGS(args,
00051               AST_APP_ARG(family);
00052               AST_APP_ARG(key);
00053    );
00054 
00055    buf[0] = '\0';
00056 
00057    if (ast_strlen_zero(parse)) {
00058       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00059       return -1;
00060    }
00061 
00062    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00063 
00064    if (args.argc < 2) {
00065       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00066       return -1;
00067    }
00068 
00069    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00070       ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", args.family,
00071             args.key);
00072    } else
00073       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00074 
00075    return 0;
00076 }
00077 
00078 static int function_db_write(struct ast_channel *chan, char *cmd, char *parse,
00079               const char *value)
00080 {
00081    AST_DECLARE_APP_ARGS(args,
00082               AST_APP_ARG(family);
00083               AST_APP_ARG(key);
00084    );
00085 
00086    if (ast_strlen_zero(parse)) {
00087       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00088       return -1;
00089    }
00090 
00091    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00092 
00093    if (args.argc < 2) {
00094       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00095       return -1;
00096    }
00097 
00098    if (ast_db_put(args.family, args.key, (char *) value))
00099       ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00100 
00101    return 0;
00102 }
00103 
00104 static struct ast_custom_function db_function = {
00105    .name = "DB",
00106    .synopsis = "Read from or write to the Asterisk database",
00107    .syntax = "DB(<family>/<key>)",
00108    .desc =
00109 "This function will read from or write a value to the Asterisk database.  On a\n"
00110 "read, this function returns the corresponding value from the database, or blank\n"
00111 "if it does not exist.  Reading a database value will also set the variable\n"
00112 "DB_RESULT.  If you wish to find out if an entry exists, use the DB_EXISTS\n"
00113 "function.\n",
00114    .read = function_db_read,
00115    .write = function_db_write,
00116 };
00117 
00118 static int function_db_exists(struct ast_channel *chan, char *cmd,
00119                char *parse, char *buf, size_t len)
00120 {
00121    AST_DECLARE_APP_ARGS(args,
00122               AST_APP_ARG(family);
00123               AST_APP_ARG(key);
00124    );
00125 
00126    buf[0] = '\0';
00127 
00128    if (ast_strlen_zero(parse)) {
00129       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00130       return -1;
00131    }
00132 
00133    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00134 
00135    if (args.argc < 2) {
00136       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00137       return -1;
00138    }
00139 
00140    if (ast_db_get(args.family, args.key, buf, len - 1))
00141       strcpy(buf, "0");
00142    else {
00143       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00144       strcpy(buf, "1");
00145    }
00146 
00147    return 0;
00148 }
00149 
00150 static struct ast_custom_function db_exists_function = {
00151    .name = "DB_EXISTS",
00152    .synopsis = "Check to see if a key exists in the Asterisk database",
00153    .syntax = "DB_EXISTS(<family>/<key>)",
00154    .desc =
00155       "This function will check to see if a key exists in the Asterisk\n"
00156       "database. If it exists, the function will return \"1\". If not,\n"
00157       "it will return \"0\".  Checking for existence of a database key will\n"
00158       "also set the variable DB_RESULT to the key's value if it exists.\n",
00159    .read = function_db_exists,
00160 };
00161 
00162 static int function_db_delete(struct ast_channel *chan, char* cmd,
00163                char *parse, char *buf, size_t len)
00164 {
00165    AST_DECLARE_APP_ARGS(args,
00166               AST_APP_ARG(family);
00167               AST_APP_ARG(key);
00168    );
00169 
00170    buf[0] = '\0';
00171 
00172    if (ast_strlen_zero(parse)) {
00173       ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00174       return -1;
00175    }
00176 
00177    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00178 
00179    if (args.argc < 2) {
00180       ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00181       return -1;
00182    }
00183 
00184    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00185       ast_log(LOG_DEBUG, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
00186    } else {
00187       if (ast_db_del(args.family, args.key)) {
00188          ast_log(LOG_DEBUG, "DB_DELETE: %s/%s could not be deleted from the database\n", 
00189             args.family, args.key);
00190       }
00191    }
00192    pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00193 
00194    return 0;
00195 }
00196 
00197 
00198 static struct ast_custom_function db_delete_function = {
00199    .name = "DB_DELETE",
00200    .synopsis = "Return a value from the database and delete it",
00201    .syntax = "DB_DELETE(<family>/<key>)",
00202    .desc =
00203       "This function will retrieve a value from the Asterisk database\n"
00204       " and then remove that key from the database.  DB_RESULT\n"
00205       "will be set to the key's value if it exists.\n",
00206    .read = function_db_delete,
00207 };
00208 
00209 static int unload_module(void)
00210 {
00211    int res = 0;
00212 
00213    res |= ast_custom_function_unregister(&db_function);
00214    res |= ast_custom_function_unregister(&db_exists_function);
00215    res |= ast_custom_function_unregister(&db_delete_function);
00216 
00217    return res;
00218 }
00219 
00220 static int load_module(void)
00221 {
00222    int res = 0;
00223 
00224    res |= ast_custom_function_register(&db_function);
00225    res |= ast_custom_function_register(&db_exists_function);
00226    res |= ast_custom_function_register(&db_delete_function);
00227 
00228    return res;
00229 }
00230 
00231 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7