Sat Mar 10 01:54:18 2012

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

Generated on Sat Mar 10 01:54:18 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7