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