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