Don't do SIP contact/route DNS if we're not using the result.
[asterisk/asterisk.git] / apps / app_while.c
index 5cdb87d..90d3d58 100644 (file)
 
 /*! \file
  *
- * \brief While Loop and ExecIf Implementations
+ * \brief While Loop Implementation
  *
  * \author Anthony Minessale <anthmct@yahoo.com>
  * 
  * \ingroup applications
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include "asterisk/file.h"
-#include "asterisk/logger.h"
-#include "asterisk/channel.h"
-#include "asterisk/utils.h"
-#include "asterisk/config.h"
 #include "asterisk/pbx.h"
 #include "asterisk/module.h"
-#include "asterisk/lock.h"
-#include "asterisk/options.h"
-
-#define ALL_DONE(u,ret) {LOCAL_USER_REMOVE(u); return ret;}
-
+#include "asterisk/channel.h"
 
-static char *exec_app = "ExecIf";
-static char *exec_desc = 
-"Usage:  ExecIF (<expr>|<app>|<data>)\n"
-"If <expr> is true, execute and return the result of <app>(<data>).\n"
-"If <expr> is true, but <app> is not found, then the application\n"
-"will return a non-zero value.\n";
-static char *exec_synopsis = "Conditional exec";
+/*** DOCUMENTATION
+       <application name="While" language="en_US">
+               <synopsis>
+                       Start a while loop.
+               </synopsis>
+               <syntax>
+                       <parameter name="expr" required="true" />
+               </syntax>
+               <description>
+                       <para>Start a While Loop.  Execution will return to this point when
+                       <literal>EndWhile()</literal> is called until expr is no longer true.</para>
+               </description>
+               <see-also>
+                       <ref type="application">EndWhile</ref>
+                       <ref type="application">ExitWhile</ref>
+                       <ref type="application">ContinueWhile</ref>
+               </see-also>
+       </application>
+       <application name="EndWhile" language="en_US">
+               <synopsis>
+                       End a while loop.
+               </synopsis>
+               <syntax />
+               <description>
+                       <para>Return to the previous called <literal>While()</literal>.</para>
+               </description>
+               <see-also>
+                       <ref type="application">While</ref>
+                       <ref type="application">ExitWhile</ref>
+                       <ref type="application">ContinueWhile</ref>
+               </see-also>
+       </application>
+       <application name="ExitWhile" language="en_US">
+               <synopsis>
+                       End a While loop.
+               </synopsis>
+               <syntax />
+               <description>
+                       <para>Exits a <literal>While()</literal> loop, whether or not the conditional has been satisfied.</para>
+               </description>
+               <see-also>
+                       <ref type="application">While</ref>
+                       <ref type="application">EndWhile</ref>
+                       <ref type="application">ContinueWhile</ref>
+               </see-also>
+       </application>
+       <application name="ContinueWhile" language="en_US">
+               <synopsis>
+                       Restart a While loop.
+               </synopsis>
+               <syntax />
+               <description>
+                       <para>Returns to the top of the while loop and re-evaluates the conditional.</para>
+               </description>
+               <see-also>
+                       <ref type="application">While</ref>
+                       <ref type="application">EndWhile</ref>
+                       <ref type="application">ExitWhile</ref>
+               </see-also>
+       </application>
+ ***/
 
 static char *start_app = "While";
-static char *start_desc = 
-"Usage:  While(<expr>)\n"
-"Start a While Loop.  Execution will return to this point when\n"
-"EndWhile is called until expr is no longer true.\n";
-
-static char *start_synopsis = "Start a while loop";
-
-
 static char *stop_app = "EndWhile";
-static char *stop_desc = 
-"Usage:  EndWhile()\n"
-"Return to the previous called While\n";
-
-static char *stop_synopsis = "End a while loop";
-
 static char *exit_app = "ExitWhile";
-static char *exit_desc =
-"Usage:  ExitWhile()\n"
-"Exits a While loop, whether or not the conditional has been satisfied.\n";
-static char *exit_synopsis = "End a While loop";
-
 static char *continue_app = "ContinueWhile";
-static char *continue_desc =
-"Usage:  ContinueWhile()\n"
-"Returns to the top of the while loop and re-evaluates the conditional.\n";
-static char *continue_synopsis = "Restart a While loop";
-
-static char *tdesc = "While Loops and Conditional Execution";
-
-LOCAL_USER_DECL;
-
-static int execif_exec(struct ast_channel *chan, void *data) {
-       int res=0;
-       struct localuser *u;
-       char *myapp = NULL;
-       char *mydata = NULL;
-       char *expr = NULL;
-       struct ast_app *app = NULL;
-
-       LOCAL_USER_ADD(u);
-
-       if (!(expr = ast_strdupa(data))) {
-               LOCAL_USER_REMOVE(u);
-               return -1;
-       }
-
-       if ((myapp = strchr(expr,'|'))) {
-               *myapp = '\0';
-               myapp++;
-               if ((mydata = strchr(myapp,'|'))) {
-                       *mydata = '\0';
-                       mydata++;
-               } else
-                       mydata = "";
-
-               if (ast_true(expr)) { 
-                       if ((app = pbx_findapp(myapp))) {
-                               res = pbx_exec(chan, app, mydata);
-                       } else {
-                               ast_log(LOG_WARNING, "Count not find application! (%s)\n", myapp);
-                               res = -1;
-                       }
-               }
-       } else {
-               ast_log(LOG_ERROR,"Invalid Syntax.\n");
-               res = -1;
-       }
-               
-       ALL_DONE(u,res);
-}
 
 #define VAR_SIZE 64
 
 
-static const char *get_index(struct ast_channel *chan, const char *prefix, int index) {
+static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) {
        char varname[VAR_SIZE];
 
-       snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
+       snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx);
        return pbx_builtin_getvar_helper(chan, varname);
 }
 
@@ -177,7 +154,7 @@ static int find_matching_endwhile(struct ast_channel *chan)
        struct ast_context *c;
        int res=-1;
 
-       if (ast_lock_contexts()) {
+       if (ast_rdlock_contexts()) {
                ast_log(LOG_ERROR, "Failed to lock contexts list\n");
                return -1;
        }
@@ -185,12 +162,16 @@ static int find_matching_endwhile(struct ast_channel *chan)
        for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
                struct ast_exten *e;
 
-               if (!ast_lock_context(c)) {
-                       if (!strcmp(ast_get_context_name(c), chan->context)) {
+               if (!ast_rdlock_context(c)) {
+                       if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) {
                                /* This is the matching context we want */
-                               int cur_priority = chan->priority + 1, level=1;
+                               int cur_priority = ast_channel_priority(chan) + 1, level=1;
 
-                               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)) {
+                               for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority,
+                                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
+                                       e;
+                                       e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority,
+                                               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                                        if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
                                                level++;
                                        } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
@@ -213,10 +194,9 @@ static int find_matching_endwhile(struct ast_channel *chan)
        return res;
 }
 
-static int _while_exec(struct ast_channel *chan, void *data, int end)
+static int _while_exec(struct ast_channel *chan, const char *data, int end)
 {
        int res=0;
-       struct localuser *u;
        const char *while_pri = NULL;
        char *my_name = NULL;
        const char *condition = NULL, *label = NULL;
@@ -231,14 +211,16 @@ static int _while_exec(struct ast_channel *chan, void *data, int end)
                return -1;
        }
 
-       LOCAL_USER_ADD(u);
-
-       /* dont want run away loops if the chan isn't even up
+#if 0
+       /* don't want run away loops if the chan isn't even up
           this is up for debate since it slows things down a tad ......
+
+          Debate is over... this prevents While/EndWhile from working
+          within the "h" extension.  Not good.
        */
        if (ast_waitfordigit(chan,1) < 0)
-               ALL_DONE(u,-1);
-
+               return -1;
+#endif
 
        for (x=0;;x++) {
                if (get_index(chan, prefix, x)) {
@@ -250,97 +232,91 @@ static int _while_exec(struct ast_channel *chan, void *data, int end)
        snprintf(used_index, VAR_SIZE, "%d", used_index_i);
        snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
        
-       if (!end) {
-               condition = ast_strdupa((char *) data);
-       }
+       if (!end)
+               condition = ast_strdupa(data);
 
-       size = strlen(chan->context) + strlen(chan->exten) + 32;
-       my_name = alloca(size);
+       size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
+       my_name = ast_alloca(size);
        memset(my_name, 0, size);
-       snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
+       snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
        
-       if (ast_strlen_zero(label)) {
-               if (end) 
-                       label = used_index;
-               else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
-                       label = new_index;
-                       pbx_builtin_setvar_helper(chan, my_name, label);
-               }
-               
+       ast_channel_lock(chan);
+       if (end) {
+               label = used_index;
+       } else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
+               label = new_index;
+               pbx_builtin_setvar_helper(chan, my_name, label);
        }
-       
        snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
-       while_pri = pbx_builtin_getvar_helper(chan, varname);
-       
        if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
+               while_pri = ast_strdupa(while_pri);
                snprintf(end_varname,VAR_SIZE,"END_%s",varname);
        }
+       ast_channel_unlock(chan);
        
 
-       if ((!end && !ast_true(condition)) || (end == 2)) {
+       if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
                /* Condition Met (clean up helper vars) */
                const char *goto_str;
                pbx_builtin_setvar_helper(chan, varname, NULL);
                pbx_builtin_setvar_helper(chan, my_name, NULL);
-        snprintf(end_varname,VAR_SIZE,"END_%s",varname);
-               if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
-                       pbx_builtin_setvar_helper(chan, end_varname, NULL);
+               snprintf(end_varname,VAR_SIZE,"END_%s",varname);
+               ast_channel_lock(chan);
+               if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
                        ast_parseable_goto(chan, goto_str);
+                       pbx_builtin_setvar_helper(chan, end_varname, NULL);
                } else {
                        int pri = find_matching_endwhile(chan);
                        if (pri > 0) {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Jumping to priority %d\n", pri);
-                               chan->priority = pri;
+                               ast_verb(3, "Jumping to priority %d\n", pri);
+                               ast_channel_priority_set(chan, pri);
                        } else {
-                               ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
+                               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));
                        }
                }
-               ALL_DONE(u,res);
+               ast_channel_unlock(chan);
+               return res;
        }
 
        if (!end && !while_pri) {
                char *goto_str;
-               size = strlen(chan->context) + strlen(chan->exten) + 32;
-               goto_str = alloca(size);
+               size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
+               goto_str = ast_alloca(size);
                memset(goto_str, 0, size);
-               snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority);
+               snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
                pbx_builtin_setvar_helper(chan, varname, goto_str);
-       } 
+       }
 
        else if (end && while_pri) {
                /* END of loop */
                snprintf(end_varname, VAR_SIZE, "END_%s", varname);
                if (! pbx_builtin_getvar_helper(chan, end_varname)) {
                        char *goto_str;
-                       size = strlen(chan->context) + strlen(chan->exten) + 32;
-                       goto_str = alloca(size);
+                       size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
+                       goto_str = ast_alloca(size);
                        memset(goto_str, 0, size);
-                       snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1);
+                       snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1);
                        pbx_builtin_setvar_helper(chan, end_varname, goto_str);
                }
                ast_parseable_goto(chan, while_pri);
        }
-       
-
-
 
-       ALL_DONE(u, res);
+       return res;
 }
 
-static int while_start_exec(struct ast_channel *chan, void *data) {
+static int while_start_exec(struct ast_channel *chan, const char *data) {
        return _while_exec(chan, data, 0);
 }
 
-static int while_end_exec(struct ast_channel *chan, void *data) {
+static int while_end_exec(struct ast_channel *chan, const char *data) {
        return _while_exec(chan, data, 1);
 }
 
-static int while_exit_exec(struct ast_channel *chan, void *data) {
+static int while_exit_exec(struct ast_channel *chan, const char *data) {
        return _while_exec(chan, data, 2);
 }
 
-static int while_continue_exec(struct ast_channel *chan, void *data)
+static int while_continue_exec(struct ast_channel *chan, const char *data)
 {
        int x;
        const char *prefix = "WHILE", *while_pri=NULL;
@@ -359,42 +335,28 @@ static int while_continue_exec(struct ast_channel *chan, void *data)
        return 0;
 }
 
-static int unload_module(void *mod)
+static int unload_module(void)
 {
        int res;
        
        res = ast_unregister_application(start_app);
-       res |= ast_unregister_application(exec_app);
        res |= ast_unregister_application(stop_app);
        res |= ast_unregister_application(exit_app);
        res |= ast_unregister_application(continue_app);
 
-       STANDARD_HANGUP_LOCALUSERS;
-
        return res;
 }
 
-static int load_module(void *mod)
+static int load_module(void)
 {
        int res;
 
-       res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
-       res |= ast_register_application(exec_app, execif_exec, exec_synopsis, exec_desc);
-       res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
-       res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
-       res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
+       res = ast_register_application_xml(start_app, while_start_exec);
+       res |= ast_register_application_xml(stop_app, while_end_exec);
+       res |= ast_register_application_xml(exit_app, while_exit_exec);
+       res |= ast_register_application_xml(continue_app, while_continue_exec);
 
        return res;
 }
 
-static const char *description(void)
-{
-       return tdesc;
-}
-
-static const char *key(void)
-{
-       return ASTERISK_GPL_KEY;
-}
-
-STD_MOD1;
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");