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