Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## 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, 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  */
    00026
    00027 #include <stdlib.h>
    00028 #include <string.h>
    00029 #include <sys/types.h>
    00030 #include <regex.h>
    00031
    00032 #include "asterisk.h"
    00033
    00034 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 37143 $") */
    00035
    00036 #include "asterisk/channel.h"
    00037 #include "asterisk/pbx.h"
    00038 #include "asterisk/logger.h"
    00039 #include "asterisk/options.h"
    00040 #include "asterisk/utils.h"
    00041 #include "asterisk/app.h"
    00042 #include "asterisk/astdb.h"
    00043
    00044 static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00045 {
    00046    int argc;
    00047    char *args;
    00048    char *argv[2];
    00049    char *family;
    00050    char *key;
    00051
    00052    if (ast_strlen_zero(data)) {
    00053       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
    00054       return buf;
    00055    }
    00056
    00057    args = ast_strdupa(data);
    00058    argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
    00059
    00060    if (argc > 1) {
    00061       family = argv[0];
    00062       key = argv[1];
    00063    } else {
    00064       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
    00065       return buf;
    00066    }
    00067
    00068    if (ast_db_get(family, key, buf, len-1)) {
    00069       ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", family, key);
    00070    } else
    00071       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
    00072
    00073
    00074    return buf;
    00075 }
    00076
    00077 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
    00078 {
    00079    int argc;
    00080    char *args;
    00081    char *argv[2];
    00082    char *family;
    00083    char *key;
    00084
    00085    if (ast_strlen_zero(data)) {
    00086       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
    00087       return;
    00088    }
    00089
    00090    args = ast_strdupa(data);
    00091    argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
    00092
    00093    if (argc > 1) {
    00094       family = argv[0];
    00095       key = argv[1];
    00096    } else {
    00097       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
    00098       return;
    00099    }
    00100
    00101    if (ast_db_put(family, key, (char*)value)) {
    00102       ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
    00103    }
    00104 }
    00105
    00106 #ifndef BUILTIN_FUNC
    00107 static
    00108 #endif
    00109 struct ast_custom_function db_function = {
    00110    .name = "DB",
    00111    .synopsis = "Read from or write to the Asterisk database",
    00112    .syntax = "DB(<family>/<key>)",
    00113    .desc =
    00114 "This function will read from or write a value to the Asterisk database.  On a\n"
    00115 "read, this function returns the corresponding value from the database, or blank\n"
    00116 "if it does not exist.  Reading a database value will also set the variable\n"
    00117 "DB_RESULT.  If you wish to find out if an entry exists, use the DB_EXISTS\n"
    00118 "function.\n",
    00119    .read = function_db_read,
    00120    .write = function_db_write,
    00121 };
    00122
    00123 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00124 {
    00125    int argc;
    00126    char *args;
    00127    char *argv[2];
    00128    char *family;
    00129    char *key;
    00130
    00131    if (ast_strlen_zero(data)) {
    00132       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
    00133       return buf;
    00134    }
    00135
    00136    args = ast_strdupa(data);
    00137    argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
    00138
    00139    if (argc > 1) {
    00140       family = argv[0];
    00141       key = argv[1];
    00142    } else {
    00143       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
    00144       return buf;
    00145    }
    00146
    00147    if (ast_db_get(family, key, buf, len-1))
    00148       ast_copy_string(buf, "0", len);
    00149    else {
    00150       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
    00151       ast_copy_string(buf, "1", len);
    00152    }
    00153
    00154    return buf;
    00155 }
    00156
    00157 #ifndef BUILTIN_FUNC
    00158 static
    00159 #endif
    00160 struct ast_custom_function db_exists_function = {
    00161    .name = "DB_EXISTS",
    00162    .synopsis = "Check to see if a key exists in the Asterisk database",
    00163    .syntax = "DB_EXISTS(<family>/<key>)",
    00164    .desc = "This function will check to see if a key exists in the Asterisk\n"
    00165       "database. If it exists, the function will return \"1\". If not,\n"
    00166       "it will return \"0\".  Checking for existence of a database key will\n"
    00167       "also set the variable DB_RESULT to the key's value if it exists.\n",
    00168    .read = function_db_exists,
    00169 };