the last round of file version tags
[asterisk/asterisk.git] / apps / app_while.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * While Loop and ExecIf Implementations
5  * 
6  * Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com>
7  *
8  * Anthony Minessale <anthmct@yahoo.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #include "asterisk.h"
19
20 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
21
22 #include "asterisk/file.h"
23 #include "asterisk/logger.h"
24 #include "asterisk/channel.h"
25 #include "asterisk/utils.h"
26 #include "asterisk/config.h"
27 #include "asterisk/pbx.h"
28 #include "asterisk/module.h"
29 #include "asterisk/lock.h"
30
31 #define ALL_DONE(u,ret) {LOCAL_USER_REMOVE(u); return ret;}
32
33
34 static char *exec_app = "ExecIf";
35 static char *exec_desc = 
36 "Usage:  ExecIF (<expr>|<app>|<data>)\n"
37 "If <expr> is true, execute and return the result of <app>(<data>).\n"
38 "If <expr> is true, but <app> is not found, then the application\n"
39 "will return a non-zero value.";
40 static char *exec_synopsis = "Conditional exec";
41
42 static char *start_app = "While";
43 static char *start_desc = 
44 "Usage:  While(<expr>)\n"
45 "Start a While Loop.  Execution will return to this point when\n"
46 "EndWhile is called until expr is no longer true.\n";
47
48 static char *start_synopsis = "Start A While Loop";
49
50
51 static char *stop_app = "EndWhile";
52 static char *stop_desc = 
53 "Usage:  EndWhile()\n"
54 "Return to the previous called While\n\n";
55
56 static char *stop_synopsis = "End A While Loop";
57
58 static char *tdesc = "While Loops and Conditional Execution";
59
60
61
62 STANDARD_LOCAL_USER;
63
64 LOCAL_USER_DECL;
65
66 static int execif_exec(struct ast_channel *chan, void *data) {
67         int res=0;
68         struct localuser *u;
69         char *myapp = NULL;
70         char *mydata = NULL;
71         char *expr = NULL;
72         struct ast_app *app = NULL;
73
74         LOCAL_USER_ADD(u);
75         expr = ast_strdupa((char *) data);
76         if ((myapp = strchr(expr,'|'))) {
77                 *myapp = '\0';
78                 myapp++;
79                 if ((mydata = strchr(myapp,'|'))) {
80                         *mydata = '\0';
81                         mydata++;
82                 } else
83                         mydata = "";
84
85                 if (ast_true(expr)) { 
86                         if ((app = pbx_findapp(myapp))) {
87                                 res = pbx_exec(chan, app, mydata, 1);
88                         } else {
89                                 ast_log(LOG_WARNING, "Count not find application! (%s)\n", myapp);
90                                 res = -1;
91                         }
92                 }
93         } else {
94                 ast_log(LOG_ERROR,"Invalid Syntax.\n");
95                 res = -1;
96         }
97                 
98         ALL_DONE(u,res);
99 }
100
101 #define VAR_SIZE 64
102
103
104 static char *get_index(struct ast_channel *chan, const char *prefix, int index) {
105         char varname[VAR_SIZE];
106
107         snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
108         return pbx_builtin_getvar_helper(chan, varname);
109 }
110
111
112 static int _while_exec(struct ast_channel *chan, void *data, int end)
113 {
114         int res=0;
115         struct localuser *u;
116         char *while_pri = NULL;
117         char *goto_str = NULL, *my_name = NULL;
118         char *condition = NULL, *label = NULL;
119         char varname[VAR_SIZE], end_varname[VAR_SIZE];
120         const char *prefix = "WHILE";
121         size_t size=0;
122         int used_index_i = -1, x=0;
123         char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
124         
125         if (!chan) {
126                 /* huh ? */
127                 return -1;
128         }
129
130         LOCAL_USER_ADD(u);
131
132         /* dont want run away loops if the chan isn't even up
133            this is up for debate since it slows things down a tad ......
134         */
135         if (ast_waitfordigit(chan,1) < 0)
136                 ALL_DONE(u,-1);
137
138
139         for (x=0;;x++) {
140                 if (get_index(chan, prefix, x)) {
141                         used_index_i = x;
142                 } else 
143                         break;
144         }
145         
146         snprintf(used_index, VAR_SIZE, "%d", used_index_i);
147         snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
148         
149         if (!end) {
150                 condition = ast_strdupa((char *) data);
151         }
152
153         size = strlen(chan->context) + strlen(chan->exten) + 32;
154         my_name = alloca(size);
155         memset(my_name, 0, size);
156         snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
157         
158         if (!label || ast_strlen_zero(label)) {
159                 if (end) 
160                         label = used_index;
161                 else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
162                         label = new_index;
163                         pbx_builtin_setvar_helper(chan, my_name, label);
164                 }
165                 
166         }
167         
168         snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
169         while_pri = pbx_builtin_getvar_helper(chan, varname);
170         
171         if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
172                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
173         }
174         
175
176         if (!end && !ast_true(condition)) {
177                 /* Condition Met (clean up helper vars) */
178                 pbx_builtin_setvar_helper(chan, varname, NULL);
179                 pbx_builtin_setvar_helper(chan, my_name, NULL);
180         snprintf(end_varname,VAR_SIZE,"END_%s",varname);
181                 if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
182                         pbx_builtin_setvar_helper(chan, end_varname, NULL);
183                         ast_parseable_goto(chan, goto_str);
184                 }
185                 ALL_DONE(u,res);
186         }
187
188         if (!end && !while_pri) {
189                 size = strlen(chan->context) + strlen(chan->exten) + 32;
190                 goto_str = alloca(size);
191                 memset(goto_str, 0, size);
192                 snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority);
193                 pbx_builtin_setvar_helper(chan, varname, goto_str);
194         } 
195
196         else if (end && while_pri) {
197                 /* END of loop */
198                 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
199                 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
200                         size = strlen(chan->context) + strlen(chan->exten) + 32;
201                         goto_str = alloca(size);
202                         memset(goto_str, 0, size);
203                         snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1);
204                         pbx_builtin_setvar_helper(chan, end_varname, goto_str);
205                 }
206                 ast_parseable_goto(chan, while_pri);
207         }
208         
209
210
211
212         ALL_DONE(u, res);
213 }
214
215 static int while_start_exec(struct ast_channel *chan, void *data) {
216         return _while_exec(chan, data, 0);
217 }
218
219 static int while_end_exec(struct ast_channel *chan, void *data) {
220         return _while_exec(chan, data, 1);
221 }
222
223
224 int unload_module(void)
225 {
226         STANDARD_HANGUP_LOCALUSERS;
227         ast_unregister_application(start_app);
228         ast_unregister_application(exec_app);
229         return ast_unregister_application(stop_app);
230 }
231
232 int load_module(void)
233 {
234         ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
235         ast_register_application(exec_app, execif_exec, exec_synopsis, exec_desc);
236         return ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
237 }
238
239 char *description(void)
240 {
241         return tdesc;
242 }
243
244 int usecount(void)
245 {
246         int res;
247         STANDARD_USECOUNT(res);
248         return res;
249 }
250
251 char *key()
252 {
253         return ASTERISK_GPL_KEY;
254 }
255