145 static int function_db_read(
struct ast_channel *chan,
const char *cmd,
146 char *parse,
char *buf,
size_t len)
155 if (ast_strlen_zero(parse)) {
156 ast_log(LOG_WARNING,
"DB requires an argument, DB(<family>/<key>)\n");
163 ast_log(LOG_WARNING,
"DB requires an argument, DB(<family>/<key>)\n");
167 if (
ast_db_get(args.family, args.key, buf, len - 1)) {
168 ast_debug(1,
"DB: %s/%s not found in database.\n", args.family, args.key);
176 static int function_db_write(
struct ast_channel *chan,
const char *cmd,
char *parse,
184 if (ast_strlen_zero(parse)) {
185 ast_log(LOG_WARNING,
"DB requires an argument, DB(<family>/<key>)=<value>\n");
192 ast_log(LOG_WARNING,
"DB requires an argument, DB(<family>/<key>)=value\n");
199 if (ast_strlen_zero(args.key) || args.key[0] ==
'/' || args.key[strlen(args.key) - 1] ==
'/' || strstr(args.key,
"//")) {
200 ast_log(LOG_WARNING,
"DB: key '%s' seems malformed\n", args.key);
202 if (
ast_db_put(args.family, args.key, value)) {
203 ast_log(LOG_WARNING,
"DB: Error writing value to database.\n");
211 .read = function_db_read,
212 .write = function_db_write,
215 static int function_db_exists(
struct ast_channel *chan,
const char *cmd,
216 char *parse,
char *buf,
size_t len)
225 if (ast_strlen_zero(parse)) {
226 ast_log(LOG_WARNING,
"DB_EXISTS requires an argument, DB(<family>/<key>)\n");
233 ast_log(LOG_WARNING,
"DB_EXISTS requires an argument, DB(<family>/<key>)\n");
237 if (
ast_db_get(args.family, args.key, buf, len - 1)) {
249 .read = function_db_exists,
253 static int function_db_keys(
struct ast_channel *chan,
const char *cmd,
char *parse,
struct ast_str **result, ssize_t maxlen)
255 size_t parselen = strlen(parse);
257 struct ast_str *escape_buf = NULL;
258 const char *last =
"";
261 while (parse[0] ==
'/') {
265 while (parse[parselen - 1] ==
'/') {
266 parse[--parselen] =
'\0';
276 for (; dbe; dbe = dbe->next) {
278 char *curkey = &dbe->key[parselen + 1], *slash;
279 if (*curkey ==
'/') {
283 if ((slash = strchr(curkey,
'/'))) {
288 if (!strcasecmp(last, curkey)) {
293 if (orig_dbe != dbe) {
299 ast_free(escape_buf);
305 .read2 = function_db_keys,
308 static int function_db_keycount(
struct ast_channel *chan,
const char *cmd,
char *parse,
char *buf,
size_t len)
310 size_t parselen = strlen(parse);
312 const char *last =
"";
316 while (parse[0] ==
'/') {
320 while (parse[parselen - 1] ==
'/') {
321 parse[--parselen] =
'\0';
326 snprintf(buf, len,
"%d", keycount);
330 for (; dbe; dbe = dbe->next) {
332 char *curkey = &dbe->key[parselen + 1], *slash;
333 if (*curkey ==
'/') {
337 if ((slash = strchr(curkey,
'/'))) {
342 if (!strcasecmp(last, curkey)) {
350 snprintf(buf, len,
"%d", keycount);
355 .
name =
"DB_KEYCOUNT",
356 .read = function_db_keycount,
359 static int function_db_delete(
struct ast_channel *chan,
const char *cmd,
360 char *parse,
char *buf,
size_t len)
369 if (ast_strlen_zero(parse)) {
370 ast_log(LOG_WARNING,
"DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
377 ast_log(LOG_WARNING,
"DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
381 if (
ast_db_get(args.family, args.key, buf, len - 1)) {
382 ast_debug(1,
"DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
385 ast_debug(1,
"DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
403 return function_db_delete(chan, cmd, parse, buf,
sizeof(buf));
408 .read = function_db_delete,
412 static int unload_module(
void)
425 static int load_module(
void)
438 AST_MODULE_INFO_STANDARD(
ASTERISK_GPL_KEY,
"Database (astdb) related dialplan functions");
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
char * ast_str_append_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string, with escaping of commas...
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
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 dis...
#define ast_debug(level,...)
Log a DEBUG message.
Core PBX routines and definitions.
Support for dynamic strings.
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
Persistent data storage (akin to *doze registry)
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_custom_function_register(acf)
Register a custom function.
#define AST_APP_ARG(name)
Define an application argument.