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