SIP channel name uniqueness
[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) ? S_OR(iftrue, "") : S_OR(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 set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
227 {
228         if (len > -1) {
229                 ast_str_make_space(str, len == 0 ? strlen(data) : len);
230         }
231         return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
232 }
233
234 static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
235 {
236         AST_DECLARE_APP_ARGS(args,
237                 AST_APP_ARG(channel);
238                 AST_APP_ARG(varname);
239         );
240         AST_STANDARD_APP_ARGS(args, data);
241         if (buf) {
242                 *buf = '\0';
243         }
244
245         if (!ast_strlen_zero(args.varname)) {
246                 struct ast_channel *chan2;
247
248                 if ((chan2 = ast_channel_get_by_name(args.channel))) {
249                         char *s = alloca(strlen(args.varname) + 4);
250                         if (s) {
251                                 sprintf(s, "${%s}", args.varname);
252                                 ast_channel_lock(chan2);
253                                 if (buf) {
254                                         pbx_substitute_variables_helper(chan2, s, buf, len);
255                                 } else {
256                                         ast_str_substitute_variables(str, len, chan2, s);
257                                 }
258                                 ast_channel_unlock(chan2);
259                         }
260                         chan2 = ast_channel_unref(chan2);
261                 }
262         }
263
264         return 0;
265 }
266
267 static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
268 {
269         return import_helper(chan, cmd, data, buf, NULL, len);
270 }
271
272 static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
273 {
274         return import_helper(chan, cmd, data, NULL, str, len);
275 }
276
277 static struct ast_custom_function isnull_function = {
278         .name = "ISNULL",
279         .read = isnull,
280         .read_max = 2,
281 };
282
283 static struct ast_custom_function set_function = {
284         .name = "SET",
285         .read = set,
286         .read2 = set2,
287 };
288
289 static struct ast_custom_function exists_function = {
290         .name = "EXISTS",
291         .read = exists,
292         .read_max = 2,
293 };
294
295 static struct ast_custom_function if_function = {
296         .name = "IF",
297         .read = acf_if,
298 };
299
300 static struct ast_custom_function if_time_function = {
301         .name = "IFTIME",
302         .read = iftime,
303 };
304
305 static struct ast_custom_function import_function = {
306         .name = "IMPORT",
307         .read = import_read,
308         .read2 = import_read2,
309 };
310
311 static int unload_module(void)
312 {
313         int res = 0;
314
315         res |= ast_custom_function_unregister(&isnull_function);
316         res |= ast_custom_function_unregister(&set_function);
317         res |= ast_custom_function_unregister(&exists_function);
318         res |= ast_custom_function_unregister(&if_function);
319         res |= ast_custom_function_unregister(&if_time_function);
320         res |= ast_custom_function_unregister(&import_function);
321
322         return res;
323 }
324
325 static int load_module(void)
326 {
327         int res = 0;
328
329         res |= ast_custom_function_register(&isnull_function);
330         res |= ast_custom_function_register(&set_function);
331         res |= ast_custom_function_register(&exists_function);
332         res |= ast_custom_function_register(&if_function);
333         res |= ast_custom_function_register(&if_time_function);
334         res |= ast_custom_function_register(&import_function);
335
336         return res;
337 }
338
339 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");