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