Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:47 2009

## Asterisk developer's documentation

# app_db.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2005, Digium, Inc.
    00005  * Copyright (C) 2003, Jefferson Noxon
    00006  *
    00007  * Mark Spencer <markster@digium.com>
    00008  * 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 Database access functions
    00024  *
    00025  * \ingroup applications
    00026  */
    00027
    00028 #include <stdlib.h>
    00029 #include <stdio.h>
    00030 #include <string.h>
    00031 #include <unistd.h>
    00032 #include <sys/types.h>
    00033
    00034 #include "asterisk.h"
    00035
    00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 47780 $")
    00037
    00038 #include "asterisk/options.h"
    00039 #include "asterisk/file.h"
    00040 #include "asterisk/logger.h"
    00041 #include "asterisk/channel.h"
    00042 #include "asterisk/pbx.h"
    00043 #include "asterisk/module.h"
    00044 #include "asterisk/astdb.h"
    00045 #include "asterisk/lock.h"
    00046 #include "asterisk/options.h"
    00047
    00048 static char *tdesc = "Database Access Functions";
    00049
    00050 static char *g_descrip =
    00051 "  DBget(varname=family/key[|options]): This application will retrieve a value\n"
    00052 "from the Asterisk database and store it in the given variable.\n"
    00053 "  Options:\n"
    00054 "    j - Jump to priority n+101 if the requested family/key isn't found.\n"
    00055 "  This application sets the following channel variable upon completion:\n"
    00056 "    DBGETSTATUS - This variable will contain the status of the attempt\n"
    00057 "                  FOUND | NOTFOUND \n"
    00058 "  This application has been deprecated in favor of the DB function.\n";
    00059
    00060 static char *p_descrip =
    00061 "  DBput(family/key=value): This application will store the given value in the\n"
    00062 "specified location in the Asterisk database.\n"
    00063 "  This application has been deprecated in favor of the DB function.\n";
    00064
    00065 static char *d_descrip =
    00066 "  DBdel(family/key): This application will delete a key from the Asterisk\n"
    00067 "database.\n";
    00068
    00069 static char *dt_descrip =
    00070 "  DBdeltree(family[/keytree]): This application will delete a family or keytree\n"
    00071 "from the Asterisk database\n";
    00072
    00073 static char *g_app = "DBget";
    00074 static char *p_app = "DBput";
    00075 static char *d_app = "DBdel";
    00076 static char *dt_app = "DBdeltree";
    00077
    00078 static char *g_synopsis = "Retrieve a value from the database";
    00079 static char *p_synopsis = "Store a value in the database";
    00080 static char *d_synopsis = "Delete a key from the database";
    00081 static char *dt_synopsis = "Delete a family or keytree from the database";
    00082
    00083 STANDARD_LOCAL_USER;
    00084
    00085 LOCAL_USER_DECL;
    00086
    00087 static int deltree_exec(struct ast_channel *chan, void *data)
    00088 {
    00089    char *argv, *family, *keytree;
    00090    struct localuser *u;
    00091
    00092    LOCAL_USER_ADD(u);
    00093
    00094    argv = ast_strdupa(data);
    00095    if (!argv) {
    00096       ast_log(LOG_ERROR, "Memory allocation failed\n");
    00097       LOCAL_USER_REMOVE(u);
    00098       return 0;
    00099    }
    00100
    00101    if (strchr(argv, '/')) {
    00102       family = strsep(&argv, "/");
    00103       keytree = strsep(&argv, "\0");
    00104          if (!family || !keytree) {
    00105             ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
    00106             LOCAL_USER_REMOVE(u);
    00107             return 0;
    00108          }
    00109       if (ast_strlen_zero(keytree))
    00110          keytree = 0;
    00111    } else {
    00112       family = argv;
    00113       keytree = 0;
    00114    }
    00115
    00116    if (option_verbose > 2) {
    00117       if (keytree)
    00118          ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
    00119       else
    00120          ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
    00121    }
    00122
    00123    if (ast_db_deltree(family, keytree)) {
    00124       if (option_verbose > 2)
    00125          ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
    00126    }
    00127
    00128    LOCAL_USER_REMOVE(u);
    00129
    00130    return 0;
    00131 }
    00132
    00133 static int del_exec(struct ast_channel *chan, void *data)
    00134 {
    00135    char *argv, *family, *key;
    00136    struct localuser *u;
    00137
    00138    LOCAL_USER_ADD(u);
    00139
    00140    argv = ast_strdupa(data);
    00141    if (!argv) {
    00142       ast_log (LOG_ERROR, "Memory allocation failed\n");
    00143       LOCAL_USER_REMOVE(u);
    00144       return 0;
    00145    }
    00146
    00147    if (strchr(argv, '/')) {
    00148       family = strsep(&argv, "/");
    00149       key = strsep(&argv, "\0");
    00150       if (!family || !key) {
    00151          ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
    00152          LOCAL_USER_REMOVE(u);
    00153          return 0;
    00154       }
    00155       if (option_verbose > 2)
    00156          ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
    00157       if (ast_db_del(family, key)) {
    00158          if (option_verbose > 2)
    00159             ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
    00160       }
    00161    } else {
    00162       ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
    00163    }
    00164
    00165    LOCAL_USER_REMOVE(u);
    00166
    00167    return 0;
    00168 }
    00169
    00170 static int put_exec(struct ast_channel *chan, void *data)
    00171 {
    00172    char *argv, *value, *family, *key;
    00173    static int dep_warning = 0;
    00174    struct localuser *u;
    00175
    00176    LOCAL_USER_ADD(u);
    00177
    00178    if (!dep_warning) {
    00179       ast_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n");
    00180       dep_warning = 1;
    00181    }
    00182
    00183    argv = ast_strdupa(data);
    00184    if (!argv) {
    00185       ast_log(LOG_ERROR, "Memory allocation failed\n");
    00186       LOCAL_USER_REMOVE(u);
    00187       return 0;
    00188    }
    00189
    00190    if (strchr(argv, '/') && strchr(argv, '=')) {
    00191       family = strsep(&argv, "/");
    00192       key = strsep(&argv, "=");
    00193       value = strsep(&argv, "\0");
    00194       if (!value || !family || !key) {
    00195          ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
    00196          LOCAL_USER_REMOVE(u);
    00197          return 0;
    00198       }
    00199       if (option_verbose > 2)
    00200          ast_verbose(VERBOSE_PREFIX_3 "DBput: family=%s, key=%s, value=%s\n", family, key, value);
    00201       if (ast_db_put(family, key, value)) {
    00202          if (option_verbose > 2)
    00203             ast_verbose(VERBOSE_PREFIX_3 "DBput: Error writing value to database.\n");
    00204       }
    00205
    00206    } else   {
    00207       ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
    00208    }
    00209
    00210    LOCAL_USER_REMOVE(u);
    00211
    00212    return 0;
    00213 }
    00214
    00215 static int get_exec(struct ast_channel *chan, void *data)
    00216 {
    00217    char *argv, *varname, *family, *key, *options = NULL;
    00218    char dbresult[256];
    00219    static int dep_warning = 0;
    00220    int priority_jump = 0;
    00221    struct localuser *u;
    00222
    00223    LOCAL_USER_ADD(u);
    00224
    00225    if (!dep_warning) {
    00226       ast_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n");
    00227       dep_warning = 1;
    00228    }
    00229
    00230    argv = ast_strdupa(data);
    00231    if (!argv) {
    00232       ast_log(LOG_ERROR, "Memory allocation failed\n");
    00233       LOCAL_USER_REMOVE(u);
    00234       return 0;
    00235    }
    00236
    00237    if (strchr(argv, '=') && strchr(argv, '/')) {
    00238       varname = strsep(&argv, "=");
    00239       family = strsep(&argv, "/");
    00240       if (strchr((void *)&argv, '|')) {
    00241          key = strsep(&argv, "|");
    00242          options = strsep(&argv, "\0");
    00243       } else
    00244          key = strsep(&argv, "\0");
    00245
    00246       if (!varname || !family || !key) {
    00247          ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
    00248          LOCAL_USER_REMOVE(u);
    00249          return 0;
    00250       }
    00251
    00252       if (options) {
    00253          if (strchr(options, 'j'))
    00254             priority_jump = 1;
    00255       }
    00256
    00257       if (option_verbose > 2)
    00258          ast_verbose(VERBOSE_PREFIX_3 "DBget: varname=%s, family=%s, key=%s\n", varname, family, key);
    00259       if (!ast_db_get(family, key, dbresult, sizeof (dbresult) - 1)) {
    00260          pbx_builtin_setvar_helper(chan, varname, dbresult);
    00261          if (option_verbose > 2)
    00262             ast_verbose(VERBOSE_PREFIX_3 "DBget: set variable %s to %s\n", varname, dbresult);
    00263          pbx_builtin_setvar_helper(chan, "DBGETSTATUS", "FOUND");
    00264       } else {
    00265          if (option_verbose > 2)
    00266          ast_verbose(VERBOSE_PREFIX_3 "DBget: Value not found in database.\n");
    00267          if (priority_jump || option_priority_jumping) {
    00268             /* Send the call to n+101 priority, where n is the current priority */
    00269             ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
    00270          }
    00271          pbx_builtin_setvar_helper(chan, "DBGETSTATUS", "NOTFOUND");
    00272       }
    00273    } else {
    00274       ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
    00275    }
    00276
    00277    LOCAL_USER_REMOVE(u);
    00278
    00279    return 0;
    00280 }
    00281
    00282 int unload_module(void)
    00283 {
    00284    int retval;
    00285
    00286    retval = ast_unregister_application(dt_app);
    00287    retval |= ast_unregister_application(d_app);
    00288    retval |= ast_unregister_application(p_app);
    00289    retval |= ast_unregister_application(g_app);
    00290
    00291    STANDARD_HANGUP_LOCALUSERS;
    00292
    00293    return retval;
    00294 }
    00295
    00296 int load_module(void)
    00297 {
    00298    int retval;
    00299
    00300    retval = ast_register_application(g_app, get_exec, g_synopsis, g_descrip);
    00301    retval |= ast_register_application(p_app, put_exec, p_synopsis, p_descrip);
    00302    retval |= ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
    00303    retval |= ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
    00304
    00305    return retval;
    00306 }
    00307
    00308 char *description(void)
    00309 {
    00310    return tdesc;
    00311 }
    00312
    00313 int usecount(void)
    00314 {
    00315    int res;
    00316    STANDARD_USECOUNT(res);
    00317    return res;
    00318 }
    00319
    00320 char *key()
    00321 {
    00322    return ASTERISK_GPL_KEY;
    00323 }