pbx: Create pbx_include.c for management of 'struct ast_include'.
[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 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_REGISTER_FILE()
35
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39
40 /*** DOCUMENTATION
41         <application name="While" language="en_US">
42                 <synopsis>
43                         Start a while loop.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="expr" required="true" />
47                 </syntax>
48                 <description>
49                         <para>Start a While Loop.  Execution will return to this point when
50                         <literal>EndWhile()</literal> is called until expr is no longer true.</para>
51                 </description>
52                 <see-also>
53                         <ref type="application">EndWhile</ref>
54                         <ref type="application">ExitWhile</ref>
55                         <ref type="application">ContinueWhile</ref>
56                 </see-also>
57         </application>
58         <application name="EndWhile" language="en_US">
59                 <synopsis>
60                         End a while loop.
61                 </synopsis>
62                 <syntax />
63                 <description>
64                         <para>Return to the previous called <literal>While()</literal>.</para>
65                 </description>
66                 <see-also>
67                         <ref type="application">While</ref>
68                         <ref type="application">ExitWhile</ref>
69                         <ref type="application">ContinueWhile</ref>
70                 </see-also>
71         </application>
72         <application name="ExitWhile" language="en_US">
73                 <synopsis>
74                         End a While loop.
75                 </synopsis>
76                 <syntax />
77                 <description>
78                         <para>Exits a <literal>While()</literal> loop, whether or not the conditional has been satisfied.</para>
79                 </description>
80                 <see-also>
81                         <ref type="application">While</ref>
82                         <ref type="application">EndWhile</ref>
83                         <ref type="application">ContinueWhile</ref>
84                 </see-also>
85         </application>
86         <application name="ContinueWhile" language="en_US">
87                 <synopsis>
88                         Restart a While loop.
89                 </synopsis>
90                 <syntax />
91                 <description>
92                         <para>Returns to the top of the while loop and re-evaluates the conditional.</para>
93                 </description>
94                 <see-also>
95                         <ref type="application">While</ref>
96                         <ref type="application">EndWhile</ref>
97                         <ref type="application">ExitWhile</ref>
98                 </see-also>
99         </application>
100  ***/
101
102 static char *start_app = "While";
103 static char *stop_app = "EndWhile";
104 static char *exit_app = "ExitWhile";
105 static char *continue_app = "ContinueWhile";
106
107 #define VAR_SIZE 64
108
109
110 static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) {
111         char varname[VAR_SIZE];
112
113         snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx);
114         return pbx_builtin_getvar_helper(chan, varname);
115 }
116
117 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
118 {
119         struct ast_exten *e;
120         struct ast_context *c2;
121         int idx;
122
123         for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
124                 if (ast_extension_match(ast_get_extension_name(e), exten)) {
125                         int needmatch = ast_get_extension_matchcid(e);
126                         if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
127                                 (!needmatch)) {
128                                 /* This is the matching extension we want */
129                                 struct ast_exten *p;
130                                 for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
131                                         if (priority != ast_get_extension_priority(p))
132                                                 continue;
133                                         return p;
134                                 }
135                         }
136                 }
137         }
138
139         /* No match; run through includes */
140         for (idx = 0; idx < ast_context_includes_count(c); idx++) {
141                 const struct ast_include *i = ast_context_includes_get(c, idx);
142
143                 for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
144                         if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
145                                 e = find_matching_priority(c2, exten, priority, callerid);
146                                 if (e)
147                                         return e;
148                         }
149                 }
150         }
151         return NULL;
152 }
153
154 static int find_matching_endwhile(struct ast_channel *chan)
155 {
156         struct ast_context *c;
157         int res=-1;
158
159         if (ast_rdlock_contexts()) {
160                 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
161                 return -1;
162         }
163
164         for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
165                 struct ast_exten *e;
166
167                 if (!ast_rdlock_context(c)) {
168                         if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) {
169                                 /* This is the matching context we want */
170                                 int cur_priority = ast_channel_priority(chan) + 1, level=1;
171
172                                 for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority,
173                                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
174                                         e;
175                                         e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority,
176                                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
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, const char *data, int end)
200 {
201         int res=0;
202         const char *while_pri = NULL;
203         char *my_name = NULL;
204         const char *condition = NULL, *label = NULL;
205         char varname[VAR_SIZE], end_varname[VAR_SIZE];
206         const char *prefix = "WHILE";
207         size_t size=0;
208         int used_index_i = -1, x=0;
209         char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
210
211         if (!chan) {
212                 /* huh ? */
213                 return -1;
214         }
215
216 #if 0
217         /* don't want run away loops if the chan isn't even up
218            this is up for debate since it slows things down a tad ......
219
220            Debate is over... this prevents While/EndWhile from working
221            within the "h" extension.  Not good.
222         */
223         if (ast_waitfordigit(chan,1) < 0)
224                 return -1;
225 #endif
226
227         for (x=0;;x++) {
228                 if (get_index(chan, prefix, x)) {
229                         used_index_i = x;
230                 } else 
231                         break;
232         }
233         
234         snprintf(used_index, VAR_SIZE, "%d", used_index_i);
235         snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
236         
237         if (!end)
238                 condition = ast_strdupa(data);
239
240         size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
241         my_name = ast_alloca(size);
242         memset(my_name, 0, size);
243         snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
244         
245         ast_channel_lock(chan);
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         snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
253         if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
254                 while_pri = ast_strdupa(while_pri);
255                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
256         }
257         ast_channel_unlock(chan);
258         
259
260         if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
261                 /* Condition Met (clean up helper vars) */
262                 const char *goto_str;
263                 pbx_builtin_setvar_helper(chan, varname, NULL);
264                 pbx_builtin_setvar_helper(chan, my_name, NULL);
265                 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
266                 ast_channel_lock(chan);
267                 if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
268                         ast_parseable_goto(chan, goto_str);
269                         pbx_builtin_setvar_helper(chan, end_varname, NULL);
270                 } else {
271                         int pri = find_matching_endwhile(chan);
272                         if (pri > 0) {
273                                 ast_verb(3, "Jumping to priority %d\n", pri);
274                                 ast_channel_priority_set(chan, pri);
275                         } else {
276                                 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
277                         }
278                 }
279                 ast_channel_unlock(chan);
280                 return res;
281         }
282
283         if (!end && !while_pri) {
284                 char *goto_str;
285                 size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
286                 goto_str = ast_alloca(size);
287                 memset(goto_str, 0, size);
288                 snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
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                         char *goto_str;
297                         size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
298                         goto_str = ast_alloca(size);
299                         memset(goto_str, 0, size);
300                         snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1);
301                         pbx_builtin_setvar_helper(chan, end_varname, goto_str);
302                 }
303                 ast_parseable_goto(chan, while_pri);
304         }
305
306         return res;
307 }
308
309 static int while_start_exec(struct ast_channel *chan, const char *data) {
310         return _while_exec(chan, data, 0);
311 }
312
313 static int while_end_exec(struct ast_channel *chan, const char *data) {
314         return _while_exec(chan, data, 1);
315 }
316
317 static int while_exit_exec(struct ast_channel *chan, const char *data) {
318         return _while_exec(chan, data, 2);
319 }
320
321 static int while_continue_exec(struct ast_channel *chan, const char *data)
322 {
323         int x;
324         const char *prefix = "WHILE", *while_pri=NULL;
325
326         for (x = 0; ; x++) {
327                 const char *tmp = get_index(chan, prefix, x);
328                 if (tmp)
329                         while_pri = tmp;
330                 else
331                         break;
332         }
333
334         if (while_pri)
335                 ast_parseable_goto(chan, while_pri);
336
337         return 0;
338 }
339
340 static int unload_module(void)
341 {
342         int res;
343         
344         res = ast_unregister_application(start_app);
345         res |= ast_unregister_application(stop_app);
346         res |= ast_unregister_application(exit_app);
347         res |= ast_unregister_application(continue_app);
348
349         return res;
350 }
351
352 static int load_module(void)
353 {
354         int res;
355
356         res = ast_register_application_xml(start_app, while_start_exec);
357         res |= ast_register_application_xml(stop_app, while_end_exec);
358         res |= ast_register_application_xml(exit_app, while_exit_exec);
359         res |= ast_register_application_xml(continue_app, while_continue_exec);
360
361         return res;
362 }
363
364 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");