Add timezone to the possible fields in a timespec.
[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                 ast_destroy_timing(&timing);
152                 return -1;
153         }
154
155         if (iftrue)
156                 iftrue = ast_strip_quoted(iftrue, "\"", "\"");
157         if (iffalse)
158                 iffalse = ast_strip_quoted(iffalse, "\"", "\"");
159
160         ast_copy_string(buf, ast_check_timing(&timing) ? iftrue : iffalse, len);
161         ast_destroy_timing(&timing);
162
163         return 0;
164 }
165
166 static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf,
167                   size_t len)
168 {
169         AST_DECLARE_APP_ARGS(args1,
170                 AST_APP_ARG(expr);
171                 AST_APP_ARG(remainder);
172         );
173         AST_DECLARE_APP_ARGS(args2,
174                 AST_APP_ARG(iftrue);
175                 AST_APP_ARG(iffalse);
176         );
177         args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
178                                                                                         then args1.remainder will be NULL, not a pointer to a null string, and
179                                                                                         then any garbage in args2.iffalse will not be cleared, and you'll crash.
180                                                                                     -- and if you mod the ast_app_separate_args func instead, you'll really
181                                                                                         mess things up badly, because the rest of everything depends on null args
182                                                                                         for non-specified stuff. */
183         
184         AST_NONSTANDARD_APP_ARGS(args1, data, '?');
185         AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
186
187         if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
188                 ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>])  (expr must be non-null, and either <true> or <false> must be non-null)\n");
189                 ast_log(LOG_WARNING, "      In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
190                 return -1;
191         }
192
193         args1.expr = ast_strip(args1.expr);
194         if (args2.iftrue)
195                 args2.iftrue = ast_strip(args2.iftrue);
196         if (args2.iffalse)
197                 args2.iffalse = ast_strip(args2.iffalse);
198
199         ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
200
201         return 0;
202 }
203
204 static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf,
205                size_t len)
206 {
207         char *varname;
208         char *val;
209
210         varname = strsep(&data, "=");
211         val = data;
212
213         if (ast_strlen_zero(varname) || !val) {
214                 ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
215                 return -1;
216         }
217
218         varname = ast_strip(varname);
219         val = ast_strip(val);
220         pbx_builtin_setvar_helper(chan, varname, val);
221         ast_copy_string(buf, val, len);
222
223         return 0;
224 }
225
226 static int acf_import(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
227 {
228         AST_DECLARE_APP_ARGS(args,
229                 AST_APP_ARG(channel);
230                 AST_APP_ARG(varname);
231         );
232         AST_STANDARD_APP_ARGS(args, data);
233         buf[0] = 0;
234         if (!ast_strlen_zero(args.varname)) {
235                 struct ast_channel *chan2 = ast_get_channel_by_name_locked(args.channel);
236                 if (chan2) {
237                         char *s = alloca(strlen(args.varname) + 4);
238                         if (s) {
239                                 sprintf(s, "${%s}", args.varname);
240                                 pbx_substitute_variables_helper(chan2, s, buf, len);
241                         }
242                         ast_channel_unlock(chan2);
243                 }
244         }
245         return 0;
246 }
247
248 static struct ast_custom_function isnull_function = {
249         .name = "ISNULL",
250         .read = isnull,
251 };
252
253 static struct ast_custom_function set_function = {
254         .name = "SET",
255         .read = set,
256 };
257
258 static struct ast_custom_function exists_function = {
259         .name = "EXISTS",
260         .read = exists,
261 };
262
263 static struct ast_custom_function if_function = {
264         .name = "IF",
265         .read = acf_if,
266 };
267
268 static struct ast_custom_function if_time_function = {
269         .name = "IFTIME",
270         .read = iftime,
271 };
272
273 static struct ast_custom_function import_function = {
274         .name = "IMPORT",
275         .read = acf_import,
276 };
277
278 static int unload_module(void)
279 {
280         int res = 0;
281
282         res |= ast_custom_function_unregister(&isnull_function);
283         res |= ast_custom_function_unregister(&set_function);
284         res |= ast_custom_function_unregister(&exists_function);
285         res |= ast_custom_function_unregister(&if_function);
286         res |= ast_custom_function_unregister(&if_time_function);
287         res |= ast_custom_function_unregister(&import_function);
288
289         return res;
290 }
291
292 static int load_module(void)
293 {
294         int res = 0;
295
296         res |= ast_custom_function_register(&isnull_function);
297         res |= ast_custom_function_register(&set_function);
298         res |= ast_custom_function_register(&exists_function);
299         res |= ast_custom_function_register(&if_function);
300         res |= ast_custom_function_register(&if_time_function);
301         res |= ast_custom_function_register(&import_function);
302
303         return res;
304 }
305
306 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");