Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
func_logic.c File Reference

Conditional logic dialplan functions. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
Include dependency graph for func_logic.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int acf_if (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int exists (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int iftime (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int import_helper (struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
 
static int import_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int import_read2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
 
static int isnull (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int load_module (void)
 
static int set (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int set2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Logical 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 exists_function
 
static struct ast_custom_function if_function
 
static struct ast_custom_function if_time_function
 
static struct ast_custom_function import_function
 
static struct ast_custom_function isnull_function
 
static struct ast_custom_function set_function
 

Detailed Description

Conditional logic dialplan functions.

Author
Anthony Minessale II

Definition in file func_logic.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 339 of file func_logic.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 339 of file func_logic.c.

◆ acf_if()

static int acf_if ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 168 of file func_logic.c.

References AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_log, AST_NONSTANDARD_APP_ARGS, ast_strip(), ast_strlen_zero, LOG_WARNING, NULL, pbx_checkcondition(), and S_OR.

170 {
171  AST_DECLARE_APP_ARGS(args1,
172  AST_APP_ARG(expr);
173  AST_APP_ARG(remainder);
174  );
175  AST_DECLARE_APP_ARGS(args2,
176  AST_APP_ARG(iftrue);
177  AST_APP_ARG(iffalse);
178  );
179  args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
180  then args1.remainder will be NULL, not a pointer to a null string, and
181  then any garbage in args2.iffalse will not be cleared, and you'll crash.
182  -- and if you mod the ast_app_separate_args func instead, you'll really
183  mess things up badly, because the rest of everything depends on null args
184  for non-specified stuff. */
185 
186  AST_NONSTANDARD_APP_ARGS(args1, data, '?');
187  AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
188 
189  if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
190  ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>]) (expr must be non-null, and either <true> or <false> must be non-null)\n");
191  ast_log(LOG_WARNING, " In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
192  return -1;
193  }
194 
195  args1.expr = ast_strip(args1.expr);
196  if (args2.iftrue)
197  args2.iftrue = ast_strip(args2.iftrue);
198  if (args2.iffalse)
199  args2.iffalse = ast_strip(args2.iffalse);
200 
201  ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
202 
203  return 0;
204 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:8321
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
#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)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
#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.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 339 of file func_logic.c.

◆ exists()

static int exists ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 124 of file func_logic.c.

Referenced by ast_mwi_mailbox_update(), AST_TEST_DEFINE(), chan_pjsip_cng_tone_detected(), and socket_process_helper().

126 {
127  strcpy(buf, data && *data ? "1" : "0");
128 
129  return 0;
130 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66

◆ iftime()

static int iftime ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 132 of file func_logic.c.

References ast_build_timing(), ast_check_timing(), ast_copy_string(), ast_destroy_timing(), ast_log, ast_strip_quoted(), ast_strlen_zero, LOG_WARNING, S_OR, and strsep().

134 {
135  struct ast_timing timing;
136  char *expr;
137  char *iftrue;
138  char *iffalse;
139 
140  data = ast_strip_quoted(data, "\"", "\"");
141  expr = strsep(&data, "?");
142  iftrue = strsep(&data, ":");
143  iffalse = data;
144 
145  if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
147  "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
148  return -1;
149  }
150 
151  if (!ast_build_timing(&timing, expr)) {
152  ast_log(LOG_WARNING, "Invalid Time Spec.\n");
153  ast_destroy_timing(&timing);
154  return -1;
155  }
156 
157  if (iftrue)
158  iftrue = ast_strip_quoted(iftrue, "\"", "\"");
159  if (iffalse)
160  iffalse = ast_strip_quoted(iffalse, "\"", "\"");
161 
162  ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
163  ast_destroy_timing(&timing);
164 
165  return 0;
166 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
#define ast_strlen_zero(foo)
Definition: strings.h:52
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: main/utils.c:1639
#define ast_log
Definition: astobj2.c:42
int ast_check_timing(const struct ast_timing *i)
Evaluate a pre-constructed bitmap as to whether the current time falls within the range specified...
Definition: extconf.c:4002
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int ast_destroy_timing(struct ast_timing *i)
Deallocates memory structures associated with a timing bitmap.
Definition: pbx_timing.c:285
int ast_build_timing(struct ast_timing *i, const char *info)
Construct a timing bitmap, for use in time-based conditionals.
Definition: extconf.c:3808
char * strsep(char **str, const char *delims)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79

◆ import_helper()

static int import_helper ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 236 of file func_logic.c.

References args, ast_alloca, AST_APP_ARG, ast_channel_get_by_name(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, AST_DECLARE_APP_ARGS, AST_STANDARD_APP_ARGS, ast_str_substitute_variables(), ast_strlen_zero, and pbx_substitute_variables_helper().

Referenced by import_read(), and import_read2().

237 {
240  AST_APP_ARG(varname);
241  );
243  if (buf) {
244  *buf = '\0';
245  }
246 
247  if (!ast_strlen_zero(args.varname)) {
248  struct ast_channel *chan2;
249 
250  if ((chan2 = ast_channel_get_by_name(args.channel))) {
251  char *s = ast_alloca(strlen(args.varname) + 4);
252  sprintf(s, "${%s}", args.varname);
253  ast_channel_lock(chan2);
254  if (buf) {
256  } else {
257  ast_str_substitute_variables(str, len, chan2, s);
258  }
259  ast_channel_unlock(chan2);
260  chan2 = ast_channel_unref(chan2);
261  }
262  }
263 
264  return 0;
265 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
Definition: muted.c:95
const char * args
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
#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.

◆ import_read()

static int import_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 267 of file func_logic.c.

References import_helper(), and NULL.

268 {
269  return import_helper(chan, cmd, data, buf, NULL, len);
270 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define NULL
Definition: resample.c:96
const char * data
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
Definition: func_logic.c:236

◆ import_read2()

static int import_read2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 272 of file func_logic.c.

References import_helper(), and NULL.

273 {
274  return import_helper(chan, cmd, data, NULL, str, len);
275 }
#define NULL
Definition: resample.c:96
const char * data
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
Definition: func_logic.c:236

◆ isnull()

static int isnull ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 116 of file func_logic.c.

118 {
119  strcpy(buf, data && *data ? "0" : "1");
120 
121  return 0;
122 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * data

◆ load_module()

static int load_module ( void  )
static

Definition at line 325 of file func_logic.c.

References ast_custom_function_register.

326 {
327  int res = 0;
328 
335 
336  return res;
337 }
static struct ast_custom_function set_function
Definition: func_logic.c:283
static struct ast_custom_function isnull_function
Definition: func_logic.c:277
static struct ast_custom_function exists_function
Definition: func_logic.c:289
static struct ast_custom_function if_time_function
Definition: func_logic.c:300
static struct ast_custom_function import_function
Definition: func_logic.c:305
static struct ast_custom_function if_function
Definition: func_logic.c:295
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ set()

static int set ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 206 of file func_logic.c.

References ast_copy_string(), ast_log, ast_strip(), ast_strlen_zero, buf, ast_channel::data, len(), LOG_WARNING, pbx_builtin_setvar_helper(), and strsep().

208 {
209  char *varname;
210  char *val;
211 
212  varname = strsep(&data, "=");
213  val = data;
214 
215  if (ast_strlen_zero(varname) || !val) {
216  ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
217  return -1;
218  }
219 
220  varname = ast_strip(varname);
221  val = ast_strip(val);
222  pbx_builtin_setvar_helper(chan, varname, val);
223  ast_copy_string(buf, val, len);
224 
225  return 0;
226 }
Definition: ast_expr2.c:325
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
const char * data
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
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 * strsep(char **str, const char *delims)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401

◆ set2()

static int set2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 228 of file func_logic.c.

References ast_str_buffer(), ast_str_make_space, ast_str_size(), and ast_channel::data.

229 {
230  if (len > -1) {
231  ast_str_make_space(str, len == 0 ? strlen(data) : len);
232  }
233  return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
234 }
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:699
#define ast_str_make_space(buf, new_len)
Definition: strings.h:780
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
const char * data
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 311 of file func_logic.c.

References ast_custom_function_unregister().

312 {
313  int res = 0;
314 
321 
322  return res;
323 }
static struct ast_custom_function set_function
Definition: func_logic.c:283
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function isnull_function
Definition: func_logic.c:277
static struct ast_custom_function exists_function
Definition: func_logic.c:289
static struct ast_custom_function if_time_function
Definition: func_logic.c:300
static struct ast_custom_function import_function
Definition: func_logic.c:305
static struct ast_custom_function if_function
Definition: func_logic.c:295

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Logical 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 339 of file func_logic.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 339 of file func_logic.c.

◆ exists_function

struct ast_custom_function exists_function
static
Initial value:
= {
.name = "EXISTS",
.read = exists,
.read_max = 2,
}
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:124

Definition at line 289 of file func_logic.c.

◆ if_function

struct ast_custom_function if_function
static
Initial value:
= {
.name = "IF",
.read = acf_if,
}
static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:168

Definition at line 295 of file func_logic.c.

◆ if_time_function

struct ast_custom_function if_time_function
static
Initial value:
= {
.name = "IFTIME",
.read = iftime,
}
static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:132

Definition at line 300 of file func_logic.c.

◆ import_function

struct ast_custom_function import_function
static
Initial value:
= {
.name = "IMPORT",
.read = import_read,
.read2 = import_read2,
}
static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
Definition: func_logic.c:272
static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:267

Definition at line 305 of file func_logic.c.

◆ isnull_function

struct ast_custom_function isnull_function
static
Initial value:
= {
.name = "ISNULL",
.read = isnull,
.read_max = 2,
}
static int isnull(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:116

Definition at line 277 of file func_logic.c.

◆ set_function

struct ast_custom_function set_function
static
Initial value:
= {
.name = "SET",
.read = set,
.read2 = set2,
}
static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
Definition: func_logic.c:228

Definition at line 283 of file func_logic.c.