Lock around variables retrieved, and copy the values, if they stay persistent,
[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         ast_channel_lock(chan);
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         snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
195         if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
196                 while_pri = ast_strdupa(while_pri);
197                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
198         }
199         ast_channel_unlock(chan);
200         
201
202         if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
203                 /* Condition Met (clean up helper vars) */
204                 const char *goto_str;
205                 pbx_builtin_setvar_helper(chan, varname, NULL);
206                 pbx_builtin_setvar_helper(chan, my_name, NULL);
207                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
208                 ast_channel_lock(chan);
209                 if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
210                         ast_parseable_goto(chan, goto_str);
211                         pbx_builtin_setvar_helper(chan, end_varname, NULL);
212                 } else {
213                         int pri = find_matching_endwhile(chan);
214                         if (pri > 0) {
215                                 ast_verb(3, "Jumping to priority %d\n", pri);
216                                 chan->priority = pri;
217                         } else {
218                                 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
219                         }
220                 }
221                 ast_channel_unlock(chan);
222                 return res;
223         }
224
225         if (!end && !while_pri) {
226                 char *goto_str;
227                 size = strlen(chan->context) + strlen(chan->exten) + 32;
228                 goto_str = alloca(size);
229                 memset(goto_str, 0, size);
230                 snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority);
231                 pbx_builtin_setvar_helper(chan, varname, goto_str);
232         }
233
234         else if (end && while_pri) {
235                 /* END of loop */
236                 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
237                 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
238                         char *goto_str;
239                         size = strlen(chan->context) + strlen(chan->exten) + 32;
240                         goto_str = alloca(size);
241                         memset(goto_str, 0, size);
242                         snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority+1);
243                         pbx_builtin_setvar_helper(chan, end_varname, goto_str);
244                 }
245                 ast_parseable_goto(chan, while_pri);
246         }
247
248         return res;
249 }
250
251 static int while_start_exec(struct ast_channel *chan, void *data) {
252         return _while_exec(chan, data, 0);
253 }
254
255 static int while_end_exec(struct ast_channel *chan, void *data) {
256         return _while_exec(chan, data, 1);
257 }
258
259 static int while_exit_exec(struct ast_channel *chan, void *data) {
260         return _while_exec(chan, data, 2);
261 }
262
263 static int while_continue_exec(struct ast_channel *chan, void *data)
264 {
265         int x;
266         const char *prefix = "WHILE", *while_pri=NULL;
267
268         for (x = 0; ; x++) {
269                 const char *tmp = get_index(chan, prefix, x);
270                 if (tmp)
271                         while_pri = tmp;
272                 else
273                         break;
274         }
275
276         if (while_pri)
277                 ast_parseable_goto(chan, while_pri);
278
279         return 0;
280 }
281
282 static int unload_module(void)
283 {
284         int res;
285         
286         res = ast_unregister_application(start_app);
287         res |= ast_unregister_application(stop_app);
288         res |= ast_unregister_application(exit_app);
289         res |= ast_unregister_application(continue_app);
290
291         return res;
292 }
293
294 static int load_module(void)
295 {
296         int res;
297
298         res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
299         res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
300         res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
301         res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
302
303         return res;
304 }
305
306 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");