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