ef644f43f68536c11d6e350c5ed6583019c9283e
[asterisk/asterisk.git] / funcs / func_logic.c
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 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include "asterisk/module.h"
32 #include "asterisk/channel.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/utils.h"
35 #include "asterisk/app.h"
36
37 /*** DOCUMENTATION
38         <function name="ISNULL" language="en_US">
39                 <synopsis>
40                         Check if a value is NULL.
41                 </synopsis>
42                 <syntax>
43                         <parameter name="data" required="true" />
44                 </syntax>
45                 <description>
46                         <para>Returns <literal>1</literal> if NULL or <literal>0</literal> otherwise.</para>
47                 </description>
48         </function>
49         <function name="SET" language="en_US">
50                 <synopsis>
51                         SET assigns a value to a channel variable.
52                 </synopsis>
53                 <syntax argsep="=">
54                         <parameter name="varname" required="true" />
55                         <parameter name="value" />
56                 </syntax>
57                 <description>
58                 </description>
59         </function>
60         <function name="EXISTS" language="en_US">
61                 <synopsis>
62                         Test the existence of a value.
63                 </synopsis>
64                 <syntax>
65                         <parameter name="data" required="true" />
66                 </syntax>
67                 <description>
68                         <para>Returns <literal>1</literal> if exists, <literal>0</literal> otherwise.</para>
69                 </description>
70         </function>
71         <function name="IF" language="en_US">
72                 <synopsis>
73                         Check for an expresion.
74                 </synopsis>
75                 <syntax argsep="?">
76                         <parameter name="expresion" required="true" />
77                         <parameter name="retvalue" argsep=":" required="true">
78                                 <argument name="true" />
79                                 <argument name="false" />
80                         </parameter>
81                 </syntax>
82                 <description>
83                         <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
84                 </description>  
85         </function>
86         <function name="IFTIME" language="en_US">
87                 <synopsis>
88                         Temporal Conditional.
89                 </synopsis>
90                 <syntax argsep="?">
91                         <parameter name="timespec" required="true" />
92                         <parameter name="retvalue" required="true" argsep=":">
93                                 <argument name="true" />
94                                 <argument name="false" />
95                         </parameter>
96                 </syntax>
97                 <description>
98                         <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
99                 </description>
100         </function>
101         <function name="IMPORT" language="en_US">
102                 <synopsis>
103                         Retrieve the value of a variable from another channel.
104                 </synopsis>
105                 <syntax>
106                         <parameter name="channel" required="true" />
107                         <parameter name="variable" required="true" />
108                 </syntax>
109                 <description>
110                 </description>
111         </function>
112  ***/
113
114 static int isnull(struct ast_channel *chan, const char *cmd, char *data,
115                   char *buf, size_t len)
116 {
117         strcpy(buf, data && *data ? "0" : "1");
118
119         return 0;
120 }
121
122 static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf,
123                   size_t len)
124 {
125         strcpy(buf, data && *data ? "1" : "0");
126
127         return 0;
128 }
129
130 static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf,
131                   size_t len)
132 {
133         struct ast_timing timing;
134         char *expr;
135         char *iftrue;
136         char *iffalse;
137
138         data = ast_strip_quoted(data, "\"", "\"");
139         expr = strsep(&data, "?");
140         iftrue = strsep(&data, ":");
141         iffalse = data;
142
143         if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
144                 ast_log(LOG_WARNING,
145                                 "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
146                 return -1;
147         }
148
149         if (!ast_build_timing(&timing, expr)) {
150                 ast_log(LOG_WARNING, "Invalid Time Spec.\n");
151                 return -1;
152         }
153
154         if (iftrue)
155                 iftrue = ast_strip_quoted(iftrue, "\"", "\"");
156         if (iffalse)
157                 iffalse = ast_strip_quoted(iffalse, "\"", "\"");
158
159         ast_copy_string(buf, ast_check_timing(&timing) ? iftrue : iffalse, len);
160
161         return 0;
162 }
163
164 static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf,
165                   size_t len)
166 {
167         AST_DECLARE_APP_ARGS(args1,
168                 AST_APP_ARG(expr);
169                 AST_APP_ARG(remainder);
170         );
171         AST_DECLARE_APP_ARGS(args2,
172                 AST_APP_ARG(iftrue);
173                 AST_APP_ARG(iffalse);
174         );
175         args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
176                                                                                         then args1.remainder will be NULL, not a pointer to a null string, and
177                                                                                         then any garbage in args2.iffalse will not be cleared, and you'll crash.
178                                                                                     -- and if you mod the ast_app_separate_args func instead, you'll really
179                                                                                         mess things up badly, because the rest of everything depends on null args
180                                                                                         for non-specified stuff. */
181         
182         AST_NONSTANDARD_APP_ARGS(args1, data, '?');
183         AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
184
185         if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
186                 ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>])  (expr must be non-null, and either <true> or <false> must be non-null)\n");
187                 ast_log(LOG_WARNING, "      In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
188                 return -1;
189         }
190
191         args1.expr = ast_strip(args1.expr);
192         if (args2.iftrue)
193                 args2.iftrue = ast_strip(args2.iftrue);
194         if (args2.iffalse)
195                 args2.iffalse = ast_strip(args2.iffalse);
196
197         ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
198
199         return 0;
200 }
201
202 static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf,
203                size_t len)
204 {
205         char *varname;
206         char *val;
207
208         varname = strsep(&data, "=");
209         val = data;
210
211         if (ast_strlen_zero(varname) || !val) {
212                 ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
213                 return -1;
214         }
215
216         varname = ast_strip(varname);
217         val = ast_strip(val);
218         pbx_builtin_setvar_helper(chan, varname, val);
219         ast_copy_string(buf, val, len);
220
221         return 0;
222 }
223
224 static int acf_import(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
225 {
226         AST_DECLARE_APP_ARGS(args,
227                 AST_APP_ARG(channel);
228                 AST_APP_ARG(varname);
229         );
230         AST_STANDARD_APP_ARGS(args, data);
231         buf[0] = 0;
232         if (!ast_strlen_zero(args.varname)) {
233                 struct ast_channel *chan2 = ast_get_channel_by_name_locked(args.channel);
234                 if (chan2) {
235                         char *s = alloca(strlen(args.varname) + 4);
236                         if (s) {
237                                 sprintf(s, "${%s}", args.varname);
238                                 pbx_substitute_variables_helper(chan2, s, buf, len);
239                         }
240                         ast_channel_unlock(chan2);
241                 }
242         }
243         return 0;
244 }
245
246 static struct ast_custom_function isnull_function = {
247         .name = "ISNULL",
248         .read = isnull,
249 };
250
251 static struct ast_custom_function set_function = {
252         .name = "SET",
253         .read = set,
254 };
255
256 static struct ast_custom_function exists_function = {
257         .name = "EXISTS",
258         .read = exists,
259 };
260
261 static struct ast_custom_function if_function = {
262         .name = "IF",
263         .read = acf_if,
264 };
265
266 static struct ast_custom_function if_time_function = {
267         .name = "IFTIME",
268         .read = iftime,
269 };
270
271 static struct ast_custom_function import_function = {
272         .name = "IMPORT",
273         .read = acf_import,
274 };
275
276 static int unload_module(void)
277 {
278         int res = 0;
279
280         res |= ast_custom_function_unregister(&isnull_function);
281         res |= ast_custom_function_unregister(&set_function);
282         res |= ast_custom_function_unregister(&exists_function);
283         res |= ast_custom_function_unregister(&if_function);
284         res |= ast_custom_function_unregister(&if_time_function);
285         res |= ast_custom_function_unregister(&import_function);
286
287         return res;
288 }
289
290 static int load_module(void)
291 {
292         int res = 0;
293
294         res |= ast_custom_function_register(&isnull_function);
295         res |= ast_custom_function_register(&set_function);
296         res |= ast_custom_function_register(&exists_function);
297         res |= ast_custom_function_register(&if_function);
298         res |= ast_custom_function_register(&if_time_function);
299         res |= ast_custom_function_register(&import_function);
300
301         return res;
302 }
303
304 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");