Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


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"

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. More...
 
static int function_db_exists (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
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
 

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

static void __reg_module ( void  )
static

Definition at line 292 of file func_db.c.

static void __unreg_module ( void  )
static

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

219 {
221  AST_APP_ARG(family);
222  AST_APP_ARG(key);
223  );
224 
225  buf[0] = '\0';
226 
227  if (ast_strlen_zero(parse)) {
228  ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
229  return -1;
230  }
231 
233 
234  if (args.argc < 2) {
235  ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
236  return -1;
237  }
238 
239  if (ast_db_get(args.family, args.key, buf, len - 1)) {
240  ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
241  } else {
242  if (ast_db_del(args.family, args.key)) {
243  ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
244  }
245  }
246 
247  pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
248 
249  return 0;
250 }
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
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...
Definition: pbx.c:10546
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: db.c:365
char * key
Definition: func_cut.c:85
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
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 256 of file func_db.c.

References function_db_delete().

258 {
259  /* Throwaway to hold the result from the read */
260  char buf[128];
261  return function_db_delete(chan, cmd, parse, buf, sizeof(buf));
262 }
static int function_db_delete(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:217
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
static int function_db_exists ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

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

181 {
183  AST_APP_ARG(family);
184  AST_APP_ARG(key);
185  );
186 
187  buf[0] = '\0';
188 
189  if (ast_strlen_zero(parse)) {
190  ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
191  return -1;
192  }
193 
195 
196  if (args.argc < 2) {
197  ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
198  return -1;
199  }
200 
201  if (ast_db_get(args.family, args.key, buf, len - 1)) {
202  strcpy(buf, "0");
203  } else {
204  pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
205  strcpy(buf, "1");
206  }
207 
208  return 0;
209 }
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
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...
Definition: pbx.c:10546
char * key
Definition: func_cut.c:85
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
static int function_db_read ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

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

117 {
119  AST_APP_ARG(family);
120  AST_APP_ARG(key);
121  );
122 
123  buf[0] = '\0';
124 
125  if (ast_strlen_zero(parse)) {
126  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
127  return -1;
128  }
129 
131 
132  if (args.argc < 2) {
133  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
134  return -1;
135  }
136 
137  if (ast_db_get(args.family, args.key, buf, len - 1)) {
138  ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
139  } else {
140  pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
141  }
142 
143  return 0;
144 }
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
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...
Definition: pbx.c:10546
char * key
Definition: func_cut.c:85
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
static int function_db_write ( struct ast_channel chan,
const char *  cmd,
char *  parse,
const char *  value 
)
static

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

148 {
150  AST_APP_ARG(family);
151  AST_APP_ARG(key);
152  );
153 
154  if (ast_strlen_zero(parse)) {
155  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
156  return -1;
157  }
158 
160 
161  if (args.argc < 2) {
162  ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
163  return -1;
164  }
165 
166  if (ast_db_put(args.family, args.key, value)) {
167  ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
168  }
169 
170  return 0;
171 }
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
int value
Definition: syslog.c:39
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct @350 args
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
char * key
Definition: func_cut.c:85
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
static int load_module ( void  )
static

Definition at line 281 of file func_db.c.

References AST_CFE_BOTH, AST_CFE_READ, ast_custom_function_register, and ast_custom_function_register_escalating.

282 {
283  int res = 0;
284 
288 
289  return res;
290 }
static struct ast_custom_function db_delete_function
Definition: func_db.c:264
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1173
static struct ast_custom_function db_exists_function
Definition: func_db.c:211
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static struct ast_custom_function db_function
Definition: func_db.c:173
static int unload_module ( void  )
static

Definition at line 270 of file func_db.c.

References ast_custom_function_unregister().

271 {
272  int res = 0;
273 
277 
278  return res;
279 }
static struct ast_custom_function db_delete_function
Definition: func_db.c:264
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ast_custom_function db_exists_function
Definition: func_db.c:211
static struct ast_custom_function db_function
Definition: func_db.c:173

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

Definition at line 292 of file func_db.c.

Definition at line 292 of file func_db.c.

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:217
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:256

Definition at line 264 of file func_db.c.

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:179

Definition at line 211 of file func_db.c.

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:146
static int function_db_read(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_db.c:115

Definition at line 173 of file func_db.c.