func_db.c File Reference

Functions for interaction with the Asterisk database. More...

#include "asterisk.h"
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/astdb.h"

Include dependency graph for func_db.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int function_db_delete (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static int function_db_delete_write (struct ast_channel *chan, const char *cmd, char *parse, const char *value)
 Wrapper to execute DB_DELETE from a write operation. Allows execution even if live_dangerously is disabled.
static int function_db_exists (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static int function_db_keys (struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
static int function_db_read (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static int function_db_write (struct ast_channel *chan, const char *cmd, char *parse, const char *value)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Database (astdb) related dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function db_delete_function
static struct ast_custom_function db_exists_function
static struct ast_custom_function db_function
static struct ast_custom_function db_keys_function


Detailed Description

Functions for interaction with the Asterisk database.

Author:
Russell Bryant <russelb@clemson.edu>

Definition in file func_db.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 362 of file func_db.c.

static void __unreg_module ( void   )  [static]

Definition at line 362 of file func_db.c.

static int function_db_delete ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 285 of file func_db.c.

References args, AST_APP_ARG, ast_db_del(), ast_db_get(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero, LOG_WARNING, and pbx_builtin_setvar_helper().

Referenced by function_db_delete_write().

00287 {
00288    AST_DECLARE_APP_ARGS(args,
00289       AST_APP_ARG(family);
00290       AST_APP_ARG(key);
00291    );
00292 
00293    buf[0] = '\0';
00294 
00295    if (ast_strlen_zero(parse)) {
00296       ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00297       return -1;
00298    }
00299 
00300    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00301 
00302    if (args.argc < 2) {
00303       ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00304       return -1;
00305    }
00306 
00307    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00308       ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
00309    } else {
00310       if (ast_db_del(args.family, args.key)) {
00311          ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
00312       }
00313    }
00314 
00315    pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00316 
00317    return 0;
00318 }

static int function_db_delete_write ( struct ast_channel chan,
const char *  cmd,
char *  parse,
const char *  value 
) [static]

Wrapper to execute DB_DELETE from a write operation. Allows execution even if live_dangerously is disabled.

Definition at line 324 of file func_db.c.

References buf, and function_db_delete().

00326 {
00327    /* Throwaway to hold the result from the read */
00328    char buf[128];
00329    return function_db_delete(chan, cmd, parse, buf, sizeof(buf));
00330 }

static int function_db_exists ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 192 of file func_db.c.

References args, AST_APP_ARG, ast_db_get(), AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero, LOG_WARNING, and pbx_builtin_setvar_helper().

00194 {
00195    AST_DECLARE_APP_ARGS(args,
00196       AST_APP_ARG(family);
00197       AST_APP_ARG(key);
00198    );
00199 
00200    buf[0] = '\0';
00201 
00202    if (ast_strlen_zero(parse)) {
00203       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00204       return -1;
00205    }
00206 
00207    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00208 
00209    if (args.argc < 2) {
00210       ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00211       return -1;
00212    }
00213 
00214    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00215       strcpy(buf, "0");
00216    } else {
00217       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00218       strcpy(buf, "1");
00219    }
00220 
00221    return 0;
00222 }

static int function_db_keys ( struct ast_channel chan,
const char *  cmd,
char *  parse,
struct ast_str **  result,
ssize_t  maxlen 
) [static]

Definition at line 230 of file func_db.c.

References ast_db_freetree(), ast_db_gettree(), ast_free, ast_str_append(), ast_str_append_escapecommas(), ast_str_reset(), escape_buf, ast_db_entry::key, last, ast_db_entry::next, and NULL.

00231 {
00232    size_t parselen = strlen(parse);
00233    struct ast_db_entry *dbe, *orig_dbe;
00234    struct ast_str *escape_buf = NULL;
00235    const char *last = "";
00236 
00237    /* Remove leading and trailing slashes */
00238    while (parse[0] == '/') {
00239       parse++;
00240       parselen--;
00241    }
00242    while (parse[parselen - 1] == '/') {
00243       parse[--parselen] = '\0';
00244    }
00245 
00246    ast_str_reset(*result);
00247 
00248    /* Nothing within the database at that prefix? */
00249    if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
00250       return 0;
00251    }
00252 
00253    for (; dbe; dbe = dbe->next) {
00254       /* Find the current component */
00255       char *curkey = &dbe->key[parselen + 1], *slash;
00256       if (*curkey == '/') {
00257          curkey++;
00258       }
00259       /* Remove everything after the current component */
00260       if ((slash = strchr(curkey, '/'))) {
00261          *slash = '\0';
00262       }
00263 
00264       /* Skip duplicates */
00265       if (!strcasecmp(last, curkey)) {
00266          continue;
00267       }
00268       last = curkey;
00269 
00270       if (orig_dbe != dbe) {
00271          ast_str_append(result, maxlen, ",");
00272       }
00273       ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
00274    }
00275    ast_db_freetree(orig_dbe);
00276    ast_free(escape_buf);
00277    return 0;
00278 }

static int function_db_read ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 128 of file func_db.c.

References args, AST_APP_ARG, ast_db_get(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero, LOG_WARNING, and pbx_builtin_setvar_helper().

00130 {
00131    AST_DECLARE_APP_ARGS(args,
00132       AST_APP_ARG(family);
00133       AST_APP_ARG(key);
00134    );
00135 
00136    buf[0] = '\0';
00137 
00138    if (ast_strlen_zero(parse)) {
00139       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00140       return -1;
00141    }
00142 
00143    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00144 
00145    if (args.argc < 2) {
00146       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00147       return -1;
00148    }
00149 
00150    if (ast_db_get(args.family, args.key, buf, len - 1)) {
00151       ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
00152    } else {
00153       pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00154    }
00155 
00156    return 0;
00157 }

static int function_db_write ( struct ast_channel chan,
const char *  cmd,
char *  parse,
const char *  value 
) [static]

Definition at line 159 of file func_db.c.

References args, AST_APP_ARG, ast_db_put(), AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strlen_zero, and LOG_WARNING.

00161 {
00162    AST_DECLARE_APP_ARGS(args,
00163       AST_APP_ARG(family);
00164       AST_APP_ARG(key);
00165    );
00166 
00167    if (ast_strlen_zero(parse)) {
00168       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00169       return -1;
00170    }
00171 
00172    AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00173 
00174    if (args.argc < 2) {
00175       ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00176       return -1;
00177    }
00178 
00179    if (ast_db_put(args.family, args.key, value)) {
00180       ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00181    }
00182 
00183    return 0;
00184 }

static int load_module ( void   )  [static]

static int unload_module ( void   )  [static]

Definition at line 338 of file func_db.c.

References ast_custom_function_unregister().

00339 {
00340    int res = 0;
00341 
00342    res |= ast_custom_function_unregister(&db_function);
00343    res |= ast_custom_function_unregister(&db_exists_function);
00344    res |= ast_custom_function_unregister(&db_delete_function);
00345    res |= ast_custom_function_unregister(&db_keys_function);
00346 
00347    return res;
00348 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Database (astdb) related dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, } [static]

Definition at line 362 of file func_db.c.

Definition at line 362 of file func_db.c.

Initial value:

 {
   .name = "DB_DELETE",
   .read = function_db_delete,
   .write = function_db_delete_write,
}

Definition at line 332 of file func_db.c.

Initial value:

 {
   .name = "DB_EXISTS",
   .read = function_db_exists,
   .read_max = 2,
}

Definition at line 224 of file func_db.c.

Initial value:

 {
   .name = "DB",
   .read = function_db_read,
   .write = function_db_write,
}

Definition at line 186 of file func_db.c.

Initial value:

 {
   .name = "DB_KEYS",
   .read2 = function_db_keys,
}

Definition at line 280 of file func_db.c.


Generated on Thu Apr 16 06:31:15 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6