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