Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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. More...
 
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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const 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 russe.nosp@m.lb@c.nosp@m.lemso.nosp@m.n.ed.nosp@m.u

Definition in file func_db.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 360 of file func_db.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 360 of file func_db.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 360 of file func_db.c.

◆ function_db_delete()

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

Definition at line 283 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().

285 {
287  AST_APP_ARG(family);
288  AST_APP_ARG(key);
289  );
290 
291  buf[0] = '\0';
292 
293  if (ast_strlen_zero(parse)) {
294  ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
295  return -1;
296  }
297 
299 
300  if (args.argc < 2) {
301  ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
302  return -1;
303  }
304 
305  if (ast_db_get(args.family, args.key, buf, len - 1)) {
306  ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
307  } else {
308  if (ast_db_del(args.family, args.key)) {
309  ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
310  }
311  }
312 
313  pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
314 
315  return 0;
316 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
const char * args
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
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...
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:429
char * key
Definition: func_cut.c:83
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ function_db_delete_write()

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 322 of file func_db.c.

References buf, and function_db_delete().

324 {
325  /* Throwaway to hold the result from the read */
326  char buf[128];
327  return function_db_delete(chan, cmd, parse, buf, sizeof(buf));
328 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int function_db_delete(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:283
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872

◆ function_db_exists()

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

Definition at line 190 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().

192 {
194  AST_APP_ARG(family);
195  AST_APP_ARG(key);
196  );
197 
198  buf[0] = '\0';
199 
200  if (ast_strlen_zero(parse)) {
201  ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
202  return -1;
203  }
204 
206 
207  if (args.argc < 2) {
208  ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
209  return -1;
210  }
211 
212  if (ast_db_get(args.family, args.key, buf, len - 1)) {
213  strcpy(buf, "0");
214  } else {
215  pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
216  strcpy(buf, "1");
217  }
218 
219  return 0;
220 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
const char * args
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
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...
char * key
Definition: func_cut.c:83
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ function_db_keys()

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 228 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.

229 {
230  size_t parselen = strlen(parse);
231  struct ast_db_entry *dbe, *orig_dbe;
232  struct ast_str *escape_buf = NULL;
233  const char *last = "";
234 
235  /* Remove leading and trailing slashes */
236  while (parse[0] == '/') {
237  parse++;
238  parselen--;
239  }
240  while (parse[parselen - 1] == '/') {
241  parse[--parselen] = '\0';
242  }
243 
244  ast_str_reset(*result);
245 
246  /* Nothing within the database at that prefix? */
247  if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
248  return 0;
249  }
250 
251  for (; dbe; dbe = dbe->next) {
252  /* Find the current component */
253  char *curkey = &dbe->key[parselen + 1], *slash;
254  if (*curkey == '/') {
255  curkey++;
256  }
257  /* Remove everything after the current component */
258  if ((slash = strchr(curkey, '/'))) {
259  *slash = '\0';
260  }
261 
262  /* Skip duplicates */
263  if (!strcasecmp(last, curkey)) {
264  continue;
265  }
266  last = curkey;
267 
268  if (orig_dbe != dbe) {
269  ast_str_append(result, maxlen, ",");
270  }
271  ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
272  }
273  ast_db_freetree(orig_dbe);
274  ast_free(escape_buf);
275  return 0;
276 }
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: main/db.c:598
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...
Definition: strings.h:1028
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define NULL
Definition: resample.c:96
struct ast_db_entry * next
Definition: astdb.h:32
struct sla_ringing_trunk * last
Definition: app_meetme.c:1092
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition: main/db.c:531
Definition: astdb.h:31
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
static struct ast_threadstorage escape_buf
Definition: cdr_mysql.c:71
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:653
char * key
Definition: astdb.h:33

◆ function_db_read()

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

Definition at line 126 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().

128 {
130  AST_APP_ARG(family);
131  AST_APP_ARG(key);
132  );
133 
134  buf[0] = '\0';
135 
136  if (ast_strlen_zero(parse)) {
137  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
138  return -1;
139  }
140 
142 
143  if (args.argc < 2) {
144  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
145  return -1;
146  }
147 
148  if (ast_db_get(args.family, args.key, buf, len - 1)) {
149  ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
150  } else {
151  pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
152  }
153 
154  return 0;
155 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
const char * args
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
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...
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ function_db_write()

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

Definition at line 157 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.

159 {
161  AST_APP_ARG(family);
162  AST_APP_ARG(key);
163  );
164 
165  if (ast_strlen_zero(parse)) {
166  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
167  return -1;
168  }
169 
171 
172  if (args.argc < 2) {
173  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
174  return -1;
175  }
176 
177  if (ast_db_put(args.family, args.key, value)) {
178  ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
179  }
180 
181  return 0;
182 }
#define LOG_WARNING
Definition: logger.h:274
const char * args
int value
Definition: syslog.c:37
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ load_module()

static int load_module ( void  )
static

Definition at line 348 of file func_db.c.

References AST_CFE_BOTH, AST_CFE_READ, ast_custom_function_register, and ast_custom_function_register_escalating.

349 {
350  int res = 0;
351 
356 
357  return res;
358 }
static struct ast_custom_function db_delete_function
Definition: func_db.c:330
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1517
static struct ast_custom_function db_keys_function
Definition: func_db.c:278
static struct ast_custom_function db_exists_function
Definition: func_db.c:222
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508
static struct ast_custom_function db_function
Definition: func_db.c:184

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 336 of file func_db.c.

References ast_custom_function_unregister().

337 {
338  int res = 0;
339 
344 
345  return res;
346 }
static struct ast_custom_function db_delete_function
Definition: func_db.c:330
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function db_keys_function
Definition: func_db.c:278
static struct ast_custom_function db_exists_function
Definition: func_db.c:222
static struct ast_custom_function db_function
Definition: func_db.c:184

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 360 of file func_db.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 360 of file func_db.c.

◆ db_delete_function

struct ast_custom_function db_delete_function
static
Initial value:
= {
.name = "DB_DELETE",
}
static int function_db_delete(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:283
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...
Definition: func_db.c:322

Definition at line 330 of file func_db.c.

◆ db_exists_function

struct ast_custom_function db_exists_function
static
Initial value:
= {
.name = "DB_EXISTS",
.read_max = 2,
}
static int function_db_exists(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:190

Definition at line 222 of file func_db.c.

◆ db_function

struct ast_custom_function db_function
static
Initial value:
= {
.name = "DB",
}
static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse, const char *value)
Definition: func_db.c:157
static int function_db_read(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:126

Definition at line 184 of file func_db.c.

◆ db_keys_function

struct ast_custom_function db_keys_function
static
Initial value:
= {
.name = "DB_KEYS",
.read2 = function_db_keys,
}
static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
Definition: func_db.c:228

Definition at line 278 of file func_db.c.