Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_logic.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 * Portions Copyright (C) 2005, Anthony Minessale II
6 *
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
12 *
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
16 */
17
18/*! \file
19 *
20 * \brief Conditional logic dialplan functions
21 *
22 * \author Anthony Minessale II
23 *
24 * \ingroup functions
25 */
26
27/*** MODULEINFO
28 <support_level>core</support_level>
29 ***/
30
31#include "asterisk.h"
32
33#include "asterisk/module.h"
34#include "asterisk/channel.h"
35#include "asterisk/pbx.h"
36#include "asterisk/utils.h"
37#include "asterisk/app.h"
38
39/*** DOCUMENTATION
40 <function name="ISNULL" language="en_US">
41 <since>
42 <version>1.2.0</version>
43 </since>
44 <synopsis>
45 Check if a value is NULL.
46 </synopsis>
47 <syntax>
48 <parameter name="data" required="true" />
49 </syntax>
50 <description>
51 <para>Returns <literal>1</literal> if NULL or <literal>0</literal> otherwise.</para>
52 </description>
53 </function>
54 <function name="SET" language="en_US">
55 <since>
56 <version>1.2.0</version>
57 </since>
58 <synopsis>
59 SET assigns a value to a channel variable.
60 </synopsis>
61 <syntax argsep="=">
62 <parameter name="varname" required="true" />
63 <parameter name="value" />
64 </syntax>
65 <description>
66 </description>
67 </function>
68 <function name="EXISTS" language="en_US">
69 <since>
70 <version>1.2.0</version>
71 </since>
72 <synopsis>
73 Test the existence of a value.
74 </synopsis>
75 <syntax>
76 <parameter name="data" required="true" />
77 </syntax>
78 <description>
79 <para>Returns <literal>1</literal> if exists, <literal>0</literal> otherwise.</para>
80 </description>
81 </function>
82 <function name="IF" language="en_US">
83 <since>
84 <version>1.2.0</version>
85 </since>
86 <synopsis>
87 Check for an expression.
88 </synopsis>
89 <syntax argsep="?">
90 <parameter name="expression" required="true" />
91 <parameter name="retvalue" argsep=":" required="true">
92 <argument name="true" />
93 <argument name="false" />
94 </parameter>
95 </syntax>
96 <description>
97 <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
98 </description>
99 </function>
100 <function name="IFTIME" language="en_US">
101 <since>
102 <version>1.2.0</version>
103 </since>
104 <synopsis>
105 Temporal Conditional.
106 </synopsis>
107 <syntax argsep="?">
108 <parameter name="timespec" required="true" />
109 <parameter name="retvalue" required="true" argsep=":">
110 <argument name="true" />
111 <argument name="false" />
112 </parameter>
113 </syntax>
114 <description>
115 <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
116 </description>
117 </function>
118 <function name="IMPORT" language="en_US">
119 <since>
120 <version>1.6.0</version>
121 </since>
122 <synopsis>
123 Retrieve the value of a variable from another channel.
124 </synopsis>
125 <syntax>
126 <parameter name="channel" required="true" />
127 <parameter name="variable" required="true" />
128 </syntax>
129 <description>
130 </description>
131 </function>
132 <function name="DELETE" language="en_US">
133 <since>
134 <version>18.21.0</version>
135 <version>20.6.0</version>
136 <version>21.1.0</version>
137 </since>
138 <synopsis>
139 Deletes a specified channel variable.
140 </synopsis>
141 <syntax>
142 <parameter name="varname" required="true">
143 <para>Channel variable name</para>
144 </parameter>
145 </syntax>
146 <description>
147 <para>Delete the channel variable specified in <replaceable>varname</replaceable>.
148 Will succeed if the channel variable exists or not.</para>
149 </description>
150 <see-also>
151 <ref type="function">GLOBAL_DELETE</ref>
152 </see-also>
153 </function>
154 <function name="VARIABLE_EXISTS" language="en_US">
155 <since>
156 <version>18.21.0</version>
157 <version>20.6.0</version>
158 <version>21.1.0</version>
159 </since>
160 <synopsis>
161 Check if a dialplan variable exists or not.
162 </synopsis>
163 <syntax>
164 <parameter name="varname" required="true">
165 <para>Channel variable name</para>
166 </parameter>
167 </syntax>
168 <description>
169 <para>Returns <literal>1</literal> if channel variable exists or <literal>0</literal> otherwise.</para>
170 </description>
171 <see-also>
172 <ref type="function">GLOBAL_EXISTS</ref>
173 </see-also>
174 </function>
175 ***/
176
177static int isnull(struct ast_channel *chan, const char *cmd, char *data,
178 char *buf, size_t len)
179{
180 strcpy(buf, data && *data ? "0" : "1");
181
182 return 0;
183}
184
185static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf,
186 size_t len)
187{
188 strcpy(buf, data && *data ? "1" : "0");
189
190 return 0;
191}
192
193static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf,
194 size_t len)
195{
196 struct ast_timing timing;
197 char *expr;
198 char *iftrue;
199 char *iffalse;
200
201 data = ast_strip_quoted(data, "\"", "\"");
202 expr = strsep(&data, "?");
203 iftrue = strsep(&data, ":");
204 iffalse = data;
205
206 if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
208 "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
209 return -1;
210 }
211
212 if (!ast_build_timing(&timing, expr)) {
213 ast_log(LOG_WARNING, "Invalid Time Spec.\n");
214 ast_destroy_timing(&timing);
215 return -1;
216 }
217
218 if (iftrue)
219 iftrue = ast_strip_quoted(iftrue, "\"", "\"");
220 if (iffalse)
221 iffalse = ast_strip_quoted(iffalse, "\"", "\"");
222
223 ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
224 ast_destroy_timing(&timing);
225
226 return 0;
227}
228
229static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf,
230 size_t len)
231{
233 AST_APP_ARG(expr);
234 AST_APP_ARG(remainder);
235 );
237 AST_APP_ARG(iftrue);
238 AST_APP_ARG(iffalse);
239 );
240 args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
241 then args1.remainder will be NULL, not a pointer to a null string, and
242 then any garbage in args2.iffalse will not be cleared, and you'll crash.
243 -- and if you mod the ast_app_separate_args func instead, you'll really
244 mess things up badly, because the rest of everything depends on null args
245 for non-specified stuff. */
246
247 AST_NONSTANDARD_APP_ARGS(args1, data, '?');
248 AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
249
250 if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
251 ast_debug(1, "<expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
252 return -1;
253 }
254
255 args1.expr = ast_strip(args1.expr);
256 if (args2.iftrue)
257 args2.iftrue = ast_strip(args2.iftrue);
258 if (args2.iffalse)
259 args2.iffalse = ast_strip(args2.iffalse);
260
261 ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
262
263 return 0;
264}
265
266static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf,
267 size_t len)
268{
269 char *varname;
270 char *val;
271
272 varname = strsep(&data, "=");
273 val = data;
274
275 if (ast_strlen_zero(varname) || !val) {
276 ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
277 return -1;
278 }
279
280 varname = ast_strip(varname);
281 val = ast_strip(val);
282 pbx_builtin_setvar_helper(chan, varname, val);
284
285 return 0;
286}
287
288static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
289{
290 if (len > -1) {
291 ast_str_make_space(str, len == 0 ? strlen(data) : len);
292 }
293 return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
294}
295
296static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
297{
299 AST_APP_ARG(channel);
300 AST_APP_ARG(varname);
301 );
303 if (buf) {
304 *buf = '\0';
305 }
306
307 if (!ast_strlen_zero(args.varname)) {
308 struct ast_channel *chan2;
309
310 if ((chan2 = ast_channel_get_by_name(args.channel))) {
311 char *s = ast_alloca(strlen(args.varname) + 4);
312 sprintf(s, "${%s}", args.varname);
313 ast_channel_lock(chan2);
314 if (buf) {
316 } else {
318 }
319 ast_channel_unlock(chan2);
320 chan2 = ast_channel_unref(chan2);
321 }
322 }
323
324 return 0;
325}
326
327static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
328{
329 return import_helper(chan, cmd, data, buf, NULL, len);
330}
331
332static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
333{
334 return import_helper(chan, cmd, data, NULL, str, len);
335}
336
337static int delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
338{
340
341 return 0;
342}
343
344static int variable_exists_read(struct ast_channel *chan, const char *cmd, char *data,
345 char *buf, size_t len)
346{
347 const char *var = pbx_builtin_getvar_helper(chan, data);
348
349 strcpy(buf, var ? "1" : "0");
350
351 return 0;
352}
353
355 .name = "ISNULL",
356 .read = isnull,
357 .read_max = 2,
358};
359
361 .name = "SET",
362 .read = set,
363 .read2 = set2,
364};
365
367 .name = "EXISTS",
368 .read = exists,
369 .read_max = 2,
370};
371
373 .name = "IF",
374 .read = acf_if,
375};
376
378 .name = "IFTIME",
379 .read = iftime,
380};
381
383 .name = "IMPORT",
384 .read = import_read,
385 .read2 = import_read2,
386};
387
389 .name = "DELETE",
390 .write = delete_write,
391};
392
394 .name = "VARIABLE_EXISTS",
395 .read = variable_exists_read,
396};
397
398static int unload_module(void)
399{
400 int res = 0;
401
410
411 return res;
412}
413
414static int load_module(void)
415{
416 int res = 0;
417
426
427 return res;
428}
429
430AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");
const char * str
Definition: app_jack.c:150
#define var
Definition: ast_expr2f.c:605
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define ast_channel_lock(chan)
Definition: channel.h:2970
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1481
#define ast_channel_unlock(chan)
Definition: channel.h:2971
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
Definition: func_logic.c:332
static struct ast_custom_function exists_function
Definition: func_logic.c:366
static struct ast_custom_function variable_exists_function
Definition: func_logic.c:393
static struct ast_custom_function import_function
Definition: func_logic.c:382
static struct ast_custom_function set_function
Definition: func_logic.c:360
static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
Definition: func_logic.c:288
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:296
static struct ast_custom_function if_function
Definition: func_logic.c:372
static struct ast_custom_function delete_function
Definition: func_logic.c:388
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:266
static struct ast_custom_function if_time_function
Definition: func_logic.c:377
static int variable_exists_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:344
static struct ast_custom_function isnull_function
Definition: func_logic.c:354
static int delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_logic.c:337
static int load_module(void)
Definition: func_logic.c:414
static int unload_module(void)
Definition: func_logic.c:398
static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:193
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:185
static int isnull(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:177
static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:229
static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:327
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
int ast_build_timing(struct ast_timing *i, const char *info_in)
Construct a timing bitmap, for use in time-based conditionals.
Definition: extconf.c:3806
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:4000
int ast_destroy_timing(struct ast_timing *i)
Deallocates memory structures associated with a timing bitmap.
Definition: pbx_timing.c:279
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:8297
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define NULL
Definition: resample.c:96
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: utils.c:1818
#define ast_str_make_space(buf, new_len)
Definition: strings.h:828
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:223
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742
Main Channel structure associated with a channel.
const char * data
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Support for dynamic strings.
Definition: strings.h:623
Definition: ast_expr2.c:325
int value
Definition: syslog.c:37
const char * args
Utility functions.