remove unnecessary checks before calls to ast_strlen_zero
[asterisk/asterisk.git] / apps / app_while.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com>
5  *
6  * Anthony Minessale <anthmct@yahoo.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief While Loop and ExecIf Implementations
22  * 
23  */
24
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/config.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/options.h"
42
43 #define ALL_DONE(u,ret) {LOCAL_USER_REMOVE(u); return ret;}
44
45
46 static char *exec_app = "ExecIf";
47 static char *exec_desc = 
48 "Usage:  ExecIF (<expr>|<app>|<data>)\n"
49 "If <expr> is true, execute and return the result of <app>(<data>).\n"
50 "If <expr> is true, but <app> is not found, then the application\n"
51 "will return a non-zero value.";
52 static char *exec_synopsis = "Conditional exec";
53
54 static char *start_app = "While";
55 static char *start_desc = 
56 "Usage:  While(<expr>)\n"
57 "Start a While Loop.  Execution will return to this point when\n"
58 "EndWhile is called until expr is no longer true.\n";
59
60 static char *start_synopsis = "Start A While Loop";
61
62
63 static char *stop_app = "EndWhile";
64 static char *stop_desc = 
65 "Usage:  EndWhile()\n"
66 "Return to the previous called While\n\n";
67
68 static char *stop_synopsis = "End A While Loop";
69
70 static char *tdesc = "While Loops and Conditional Execution";
71
72
73
74 STANDARD_LOCAL_USER;
75
76 LOCAL_USER_DECL;
77
78 static int execif_exec(struct ast_channel *chan, void *data) {
79         int res=0;
80         struct localuser *u;
81         char *myapp = NULL;
82         char *mydata = NULL;
83         char *expr = NULL;
84         struct ast_app *app = NULL;
85
86         LOCAL_USER_ADD(u);
87
88         expr = ast_strdupa(data);
89         if (!expr) {
90                 ast_log(LOG_ERROR, "Out of memory\n");
91                 LOCAL_USER_REMOVE(u);
92                 return -1;
93         }
94
95         if ((myapp = strchr(expr,'|'))) {
96                 *myapp = '\0';
97                 myapp++;
98                 if ((mydata = strchr(myapp,'|'))) {
99                         *mydata = '\0';
100                         mydata++;
101                 } else
102                         mydata = "";
103
104                 if (ast_true(expr)) { 
105                         if ((app = pbx_findapp(myapp))) {
106                                 res = pbx_exec(chan, app, mydata, 1);
107                         } else {
108                                 ast_log(LOG_WARNING, "Count not find application! (%s)\n", myapp);
109                                 res = -1;
110                         }
111                 }
112         } else {
113                 ast_log(LOG_ERROR,"Invalid Syntax.\n");
114                 res = -1;
115         }
116                 
117         ALL_DONE(u,res);
118 }
119
120 #define VAR_SIZE 64
121
122
123 static char *get_index(struct ast_channel *chan, const char *prefix, int index) {
124         char varname[VAR_SIZE];
125
126         snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
127         return pbx_builtin_getvar_helper(chan, varname);
128 }
129
130 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
131 {
132         struct ast_exten *e;
133         struct ast_include *i;
134         struct ast_context *c2;
135
136         for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
137                 if (ast_extension_match(ast_get_extension_name(e), exten)) {
138                         int needmatch = ast_get_extension_matchcid(e);
139                         if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
140                                 (!needmatch)) {
141                                 /* This is the matching extension we want */
142                                 struct ast_exten *p;
143                                 for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
144                                         if (priority != ast_get_extension_priority(p))
145                                                 continue;
146                                         return p;
147                                 }
148                         }
149                 }
150         }
151
152         /* No match; run through includes */
153         for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
154                 for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
155                         if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
156                                 e = find_matching_priority(c2, exten, priority, callerid);
157                                 if (e)
158                                         return e;
159                         }
160                 }
161         }
162         return NULL;
163 }
164
165 static int find_matching_endwhile(struct ast_channel *chan)
166 {
167         struct ast_context *c;
168         int res=-1;
169
170         if (ast_lock_contexts()) {
171                 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
172                 return -1;
173         }
174
175         for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
176                 struct ast_exten *e;
177
178                 if (!ast_lock_context(c)) {
179                         if (!strcmp(ast_get_context_name(c), chan->context)) {
180                                 /* This is the matching context we want */
181                                 int cur_priority = chan->priority + 1, level=1;
182
183                                 for (e = find_matching_priority(c, chan->exten, cur_priority, chan->cid.cid_num); e; e = find_matching_priority(c, chan->exten, ++cur_priority, chan->cid.cid_num)) {
184                                         if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
185                                                 level++;
186                                         } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
187                                                 level--;
188                                         }
189
190                                         if (level == 0) {
191                                                 res = cur_priority;
192                                                 break;
193                                         }
194                                 }
195                         }
196                         ast_unlock_context(c);
197                         if (res > 0) {
198                                 break;
199                         }
200                 }
201         }
202         ast_unlock_contexts();
203         return res;
204 }
205
206 static int _while_exec(struct ast_channel *chan, void *data, int end)
207 {
208         int res=0;
209         struct localuser *u;
210         char *while_pri = NULL;
211         char *goto_str = NULL, *my_name = NULL;
212         char *condition = NULL, *label = NULL;
213         char varname[VAR_SIZE], end_varname[VAR_SIZE];
214         const char *prefix = "WHILE";
215         size_t size=0;
216         int used_index_i = -1, x=0;
217         char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
218         
219         if (!chan) {
220                 /* huh ? */
221                 return -1;
222         }
223
224         LOCAL_USER_ADD(u);
225
226         /* dont want run away loops if the chan isn't even up
227            this is up for debate since it slows things down a tad ......
228         */
229         if (ast_waitfordigit(chan,1) < 0)
230                 ALL_DONE(u,-1);
231
232
233         for (x=0;;x++) {
234                 if (get_index(chan, prefix, x)) {
235                         used_index_i = x;
236                 } else 
237                         break;
238         }
239         
240         snprintf(used_index, VAR_SIZE, "%d", used_index_i);
241         snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
242         
243         if (!end) {
244                 condition = ast_strdupa((char *) data);
245         }
246
247         size = strlen(chan->context) + strlen(chan->exten) + 32;
248         my_name = alloca(size);
249         memset(my_name, 0, size);
250         snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
251         
252         if (ast_strlen_zero(label)) {
253                 if (end) 
254                         label = used_index;
255                 else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
256                         label = new_index;
257                         pbx_builtin_setvar_helper(chan, my_name, label);
258                 }
259                 
260         }
261         
262         snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
263         while_pri = pbx_builtin_getvar_helper(chan, varname);
264         
265         if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
266                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
267         }
268         
269
270         if (!end && !ast_true(condition)) {
271                 /* Condition Met (clean up helper vars) */
272                 pbx_builtin_setvar_helper(chan, varname, NULL);
273                 pbx_builtin_setvar_helper(chan, my_name, NULL);
274         snprintf(end_varname,VAR_SIZE,"END_%s",varname);
275                 if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
276                         pbx_builtin_setvar_helper(chan, end_varname, NULL);
277                         ast_parseable_goto(chan, goto_str);
278                 } else {
279                         int pri = find_matching_endwhile(chan);
280                         if (pri > 0) {
281                                 if (option_verbose > 2)
282                                         ast_verbose(VERBOSE_PREFIX_3 "Jumping to priority %d\n", pri);
283                                 chan->priority = pri;
284                         } else {
285                                 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
286                         }
287                 }
288                 ALL_DONE(u,res);
289         }
290
291         if (!end && !while_pri) {
292                 size = strlen(chan->context) + strlen(chan->exten) + 32;
293                 goto_str = alloca(size);
294                 memset(goto_str, 0, size);
295                 snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority);
296                 pbx_builtin_setvar_helper(chan, varname, goto_str);
297         } 
298
299         else if (end && while_pri) {
300                 /* END of loop */
301                 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
302                 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
303                         size = strlen(chan->context) + strlen(chan->exten) + 32;
304                         goto_str = alloca(size);
305                         memset(goto_str, 0, size);
306                         snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1);
307                         pbx_builtin_setvar_helper(chan, end_varname, goto_str);
308                 }
309                 ast_parseable_goto(chan, while_pri);
310         }
311         
312
313
314
315         ALL_DONE(u, res);
316 }
317
318 static int while_start_exec(struct ast_channel *chan, void *data) {
319         return _while_exec(chan, data, 0);
320 }
321
322 static int while_end_exec(struct ast_channel *chan, void *data) {
323         return _while_exec(chan, data, 1);
324 }
325
326
327 int unload_module(void)
328 {
329         int res;
330         
331         res = ast_unregister_application(start_app);
332         res |= ast_unregister_application(exec_app);
333         res |= ast_unregister_application(stop_app);
334
335         STANDARD_HANGUP_LOCALUSERS;
336
337         return res;
338 }
339
340 int load_module(void)
341 {
342         int res;
343
344         res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
345         res |= ast_register_application(exec_app, execif_exec, exec_synopsis, exec_desc);
346         res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
347
348         return res;
349 }
350
351 char *description(void)
352 {
353         return tdesc;
354 }
355
356 int usecount(void)
357 {
358         int res;
359         STANDARD_USECOUNT(res);
360         return res;
361 }
362
363 char *key()
364 {
365         return ASTERISK_GPL_KEY;
366 }
367