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