2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 * Portions Copyright (C) 2005, Anthony Minessale II
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.
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.
20 * \brief Conditional logic dialplan functions
22 * \author Anthony Minessale II
27 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
32 #include <sys/types.h>
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/app.h"
41 static int isnull(struct ast_channel
*chan
, char *cmd
, char *data
,
42 char *buf
, size_t len
)
44 strcpy(buf
, data
&& *data
? "0" : "1");
49 static int exists(struct ast_channel
*chan
, char *cmd
, char *data
, char *buf
,
52 strcpy(buf
, data
&& *data
? "1" : "0");
57 static int iftime(struct ast_channel
*chan
, char *cmd
, char *data
, char *buf
,
60 struct ast_timing timing
;
65 data
= ast_strip_quoted(data
, "\"", "\"");
66 expr
= strsep(&data
, "?");
67 iftrue
= strsep(&data
, ":");
70 if (ast_strlen_zero(expr
) || !(iftrue
|| iffalse
)) {
72 "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
76 if (!ast_build_timing(&timing
, expr
)) {
77 ast_log(LOG_WARNING
, "Invalid Time Spec.\n");
82 iftrue
= ast_strip_quoted(iftrue
, "\"", "\"");
84 iffalse
= ast_strip_quoted(iffalse
, "\"", "\"");
86 ast_copy_string(buf
, ast_check_timing(&timing
) ? iftrue
: iffalse
, len
);
91 static int acf_if(struct ast_channel
*chan
, char *cmd
, char *data
, char *buf
,
94 AST_DECLARE_APP_ARGS(args1
,
96 AST_APP_ARG(remainder
);
98 AST_DECLARE_APP_ARGS(args2
,
100 AST_APP_ARG(iffalse
);
102 args2
.iftrue
= args2
.iffalse
= NULL
; /* you have to set these, because if there is nothing after the '?',
103 then args1.remainder will be NULL, not a pointer to a null string, and
104 then any garbage in args2.iffalse will not be cleared, and you'll crash.
105 -- and if you mod the ast_app_separate_args func instead, you'll really
106 mess things up badly, because the rest of everything depends on null args
107 for non-specified stuff. */
109 AST_NONSTANDARD_APP_ARGS(args1
, data
, '?');
110 AST_NONSTANDARD_APP_ARGS(args2
, args1
.remainder
, ':');
112 if (ast_strlen_zero(args1
.expr
) || !(args2
.iftrue
|| args2
.iffalse
)) {
113 ast_log(LOG_WARNING
, "Syntax IF(<expr>?[<true>][:<false>]) (expr must be non-null, and either <true> or <false> must be non-null)\n");
114 ast_log(LOG_WARNING
, " In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1
.expr
, args2
.iftrue
, args2
.iffalse
);
118 args1
.expr
= ast_strip(args1
.expr
);
120 args2
.iftrue
= ast_strip(args2
.iftrue
);
122 args2
.iffalse
= ast_strip(args2
.iffalse
);
124 ast_copy_string(buf
, pbx_checkcondition(args1
.expr
) ? (S_OR(args2
.iftrue
, "")) : (S_OR(args2
.iffalse
, "")), len
);
129 static int set(struct ast_channel
*chan
, char *cmd
, char *data
, char *buf
,
135 varname
= strsep(&data
, "=");
138 if (ast_strlen_zero(varname
) || !val
) {
139 ast_log(LOG_WARNING
, "Syntax SET(<varname>=[<value>])\n");
143 varname
= ast_strip(varname
);
144 val
= ast_strip(val
);
145 pbx_builtin_setvar_helper(chan
, varname
, val
);
146 ast_copy_string(buf
, val
, len
);
151 static struct ast_custom_function isnull_function
= {
153 .synopsis
= "NULL Test: Returns 1 if NULL or 0 otherwise",
154 .syntax
= "ISNULL(<data>)",
158 static struct ast_custom_function set_function
= {
160 .synopsis
= "SET assigns a value to a channel variable",
161 .syntax
= "SET(<varname>=[<value>])",
165 static struct ast_custom_function exists_function
= {
167 .synopsis
= "Existence Test: Returns 1 if exists, 0 otherwise",
168 .syntax
= "EXISTS(<data>)",
172 static struct ast_custom_function if_function
= {
175 "Conditional: Returns the data following '?' if true else the data following ':'",
176 .syntax
= "IF(<expr>?[<true>][:<false>])",
180 static struct ast_custom_function if_time_function
= {
183 "Temporal Conditional: Returns the data following '?' if true else the data following ':'",
184 .syntax
= "IFTIME(<timespec>?[<true>][:<false>])",
188 static int unload_module(void)
192 res
|= ast_custom_function_unregister(&isnull_function
);
193 res
|= ast_custom_function_unregister(&set_function
);
194 res
|= ast_custom_function_unregister(&exists_function
);
195 res
|= ast_custom_function_unregister(&if_function
);
196 res
|= ast_custom_function_unregister(&if_time_function
);
201 static int load_module(void)
205 res
|= ast_custom_function_register(&isnull_function
);
206 res
|= ast_custom_function_register(&set_function
);
207 res
|= ast_custom_function_register(&exists_function
);
208 res
|= ast_custom_function_register(&if_function
);
209 res
|= ast_custom_function_register(&if_time_function
);
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Logical dialplan functions");