include "logger.h" and errno.h from asterisk.h - usage shows that they
[asterisk/asterisk.git] / apps / app_dial.c
index 2d15fbe..dfaa682 100644 (file)
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <sys/time.h>
 #include <sys/signal.h>
 #include <sys/stat.h>
@@ -42,7 +36,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/lock.h"
 #include "asterisk/file.h"
-#include "asterisk/logger.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
 #include "asterisk/options.h"
@@ -66,7 +59,7 @@ static char *app = "Dial";
 static char *synopsis = "Place a call and connect to the current channel";
 
 static char *descrip =
-"  Dial(Technology/resource[&Tech2/resource2...][|timeout][|options][|URL]):\n"
+"  Dial(Technology/resource[&Tech2/resource2...][,timeout][,options][,URL]):\n"
 "This application will place calls to one or more specified channels. As soon\n"
 "as one of the requested channels answers, the originating channel will be\n"
 "answered, if it has not already been answered. These two channels will then\n"
@@ -82,7 +75,7 @@ static char *descrip =
 "    ANSWEREDTIME - This is the amount of time for actual call.\n"
 "    DIALSTATUS   - This is the status of the call:\n"
 "                   CHANUNAVAIL | CONGESTION | NOANSWER | BUSY | ANSWER | CANCEL\n" 
-"                   DONTCALL | TORTURE\n"
+"                   DONTCALL | TORTURE | INVALIDARGS\n"
 "  For the Privacy and Screening Modes, the DIALSTATUS variable will be set to\n"
 "DONTCALL if the called party chooses to send the calling party to the 'Go Away'\n"
 "script. The DIALSTATUS variable will be set to TORTURE if the called party\n"
@@ -92,10 +85,15 @@ static char *descrip =
 "ends the call.\n"
 "  The optional URL will be sent to the called party if the channel supports it.\n"
 "  If the OUTBOUND_GROUP variable is set, all peer channels created by this\n"
-"application will be put into that group (as in Set(GROUP()=...).\n\n"
+"application will be put into that group (as in Set(GROUP()=...).\n"
+"  If the OUTBOUND_GROUP_ONCE variable is set, all peer channels created by this\n"
+"application will be put into that group (as in Set(GROUP()=...). Unlike OUTBOUND_GROUP,\n"
+"however, the variable will be unset after use.\n\n"
 "  Options:\n"
 "    A(x) - Play an announcement to the called party, using 'x' as the file.\n"
 "    C    - Reset the CDR for this call.\n"
+"    c    - If DIAL cancels this call, always set the flag to tell the channel\n"
+"           driver that the call is answered elsewhere.\n"
 "    d    - Allow the calling user to dial a 1 digit extension while waiting for\n"
 "           a call to be answered. Exit to that extension if it exists in the\n"
 "           current context, or the context defined in the EXITCONTEXT variable,\n"
@@ -104,7 +102,8 @@ static char *descrip =
 "           party has answered, but before the call gets bridged. The 'called'\n"
 "           DTMF string is sent to the called party, and the 'calling' DTMF\n"
 "           string is sent to the calling party. Both parameters can be used\n"
-"           alone.\n"          
+"           alone.\n"
+"    e    - execute the 'h' extension for peer after the call ends\n"
 "    f    - Force the callerid of the *calling* channel to be set as the\n"
 "           extension associated with the channel using a dialplan 'hint'.\n"
 "           For example, some PSTNs do not allow CallerID to be set to anything\n"
@@ -120,7 +119,10 @@ static char *descrip =
 "    H    - Allow the calling party to hang up by hitting the '*' DTMF digit.\n"
 "    i    - Asterisk will ignore any forwarding requests it may receive on this\n"
 "           dial attempt.\n"
-"    j    - Jump to priority n+101 if all of the requested channels were busy.\n"
+"    k    - Allow the called party to enable parking of the call by sending\n"
+"           the DTMF sequence defined for call parking in features.conf.\n"
+"    K    - Allow the calling party to enable parking of the call by sending\n"
+"           the DTMF sequence defined for call parking in features.conf.\n"
 "    L(x[:y][:z]) - Limit the call to 'x' ms. Play a warning when 'y' ms are\n"
 "           left. Repeat the warning every 'z' ms. The following special\n"
 "           variables can be used with this option:\n"
@@ -142,9 +144,7 @@ static char *descrip =
 "           finished executing.\n"
 "           * ABORT        Hangup both legs of the call.\n"
 "           * CONGESTION   Behave as if line congestion was encountered.\n"
-"           * BUSY         Behave as if a busy signal was encountered. This will also\n"
-"                          have the application jump to priority n+101 if the\n"
-"                          'j' option is set.\n"
+"           * BUSY         Behave as if a busy signal was encountered.\n"
 "           * CONTINUE     Hangup the called party and allow the calling party\n"
 "                          to continue dialplan execution at the next priority.\n"
 "           * GOTO:<context>^<exten>^<priority> - Transfer the call to the\n"
@@ -184,20 +184,31 @@ static char *descrip =
 "           DTMF sequence defined in features.conf.\n"
 "    T    - Allow the calling party to transfer the called party by sending the\n"
 "           DTMF sequence defined in features.conf.\n"
+"    U(x[^arg]) - Execute via Gosub the routine 'x' for the *called* channel before connecting\n"
+"           to the calling channel. Arguments can be specified to the Gosub\n"
+"           using '^' as a delimeter. The Gosub routine can set the variable\n"
+"           GOSUB_RESULT to specify the following actions after the Gosub returns.\n" 
+"           * ABORT        Hangup both legs of the call.\n"
+"           * CONGESTION   Behave as if line congestion was encountered.\n"
+"           * BUSY         Behave as if a busy signal was encountered.\n"
+"           * CONTINUE     Hangup the called party and allow the calling party\n"
+"                          to continue dialplan execution at the next priority.\n"
+"           * GOTO:<context>^<exten>^<priority> - Transfer the call to the\n"
+"                          specified priority. Optionally, an extension, or\n"
+"                          extension and priority can be specified.\n"
+"           You cannot use any additional action post answer options in conjunction\n"
+"           with this option. Also, pbx services are not run on the peer (called) channel,\n"
+"           so you will not be able to set timeouts via the TIMEOUT() function in this routine.\n"
 "    w    - Allow the called party to enable recording of the call by sending\n"
 "           the DTMF sequence defined for one-touch recording in features.conf.\n"
 "    W    - Allow the calling party to enable recording of the call by sending\n"
-"           the DTMF sequence defined for one-touch recording in features.conf.\n"
-"    k    - Allow the called party to enable parking of the call by sending\n"
-"           the DTMF sequence defined for call parking in features.conf.\n"
-"    K    - Allow the calling party to enable parking of the call by sending\n"
-"           the DTMF sequence defined for call parking in features.conf.\n";
+"           the DTMF sequence defined for one-touch recording in features.conf.\n";
 
 /* RetryDial App by Anthony Minessale II <anthmct@yahoo.com> Jan/2005 */
 static char *rapp = "RetryDial";
 static char *rsynopsis = "Place a call, retrying on failure allowing optional exit extension.";
 static char *rdescrip =
-"  RetryDial(announce|sleep|retries|dialargs): This application will attempt to\n"
+"  RetryDial(announce,sleep,retries,dialargs): This application will attempt to\n"
 "place a call using the normal Dial application. If no channel can be reached,\n"
 "the 'announce' file will be played. Then, it will wait 'sleep' number of\n"
 "seconds before retying the call. After 'retires' number of attempts, the\n"
@@ -218,7 +229,6 @@ enum {
        OPT_GO_ON =             (1 << 5),
        OPT_CALLEE_HANGUP =     (1 << 6),
        OPT_CALLER_HANGUP =     (1 << 7),
-       OPT_PRIORITY_JUMP =     (1 << 8),
        OPT_DURATION_LIMIT =    (1 << 9),
        OPT_MUSICBACK =         (1 << 10),
        OPT_CALLEE_MACRO =      (1 << 11),
@@ -238,10 +248,13 @@ enum {
        OPT_CALLEE_PARK =       (1 << 25),
        OPT_CALLER_PARK =       (1 << 26),
        OPT_IGNORE_FORWARDING = (1 << 27),
-} dial_exec_option_flags;
+       OPT_CALLEE_GOSUB =      (1 << 28),
+       OPT_CANCEL_ELSEWHERE =  (1 << 29),
+       OPT_PEER_H =            (1 << 30),
+};
 
-#define DIAL_STILLGOING                        (1 << 30)
-#define DIAL_NOFORWARDHTML             (1 << 31)
+#define DIAL_STILLGOING                        (1 << 31)
+#define DIAL_NOFORWARDHTML             ((uint64_t)1 << 32) /* flags are now 64 bits, so keep it up! */
 
 enum {
        OPT_ARG_ANNOUNCE = 0,
@@ -250,67 +263,78 @@ enum {
        OPT_ARG_DURATION_LIMIT,
        OPT_ARG_MUSICBACK,
        OPT_ARG_CALLEE_MACRO,
+       OPT_ARG_CALLEE_GOSUB,
        OPT_ARG_PRIVACY,
        OPT_ARG_DURATION_STOP,
        OPT_ARG_OPERMODE,
        /* note: this entry _MUST_ be the last one in the enum */
        OPT_ARG_ARRAY_SIZE,
-} dial_exec_option_args;
+};
 
-AST_APP_OPTIONS(dial_exec_options, {
+AST_APP_OPTIONS(dial_exec_options, BEGIN_OPTIONS
        AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
        AST_APP_OPTION('C', OPT_RESETCDR),
+       AST_APP_OPTION('c', OPT_CANCEL_ELSEWHERE),
        AST_APP_OPTION('d', OPT_DTMF_EXIT),
        AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
+       AST_APP_OPTION('e', OPT_PEER_H),
        AST_APP_OPTION('f', OPT_FORCECLID),
        AST_APP_OPTION('g', OPT_GO_ON),
        AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
        AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
        AST_APP_OPTION('H', OPT_CALLER_HANGUP),
        AST_APP_OPTION('i', OPT_IGNORE_FORWARDING),
-       AST_APP_OPTION('j', OPT_PRIORITY_JUMP),
+       AST_APP_OPTION('k', OPT_CALLEE_PARK),
+       AST_APP_OPTION('K', OPT_CALLER_PARK),
+       AST_APP_OPTION('k', OPT_CALLEE_PARK),
+       AST_APP_OPTION('K', OPT_CALLER_PARK),
        AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
        AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
        AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
        AST_APP_OPTION('n', OPT_SCREEN_NOINTRO),
        AST_APP_OPTION('N', OPT_SCREEN_NOCLID),
-       AST_APP_OPTION_ARG('O', OPT_OPERMODE,OPT_ARG_OPERMODE),
        AST_APP_OPTION('o', OPT_ORIGINAL_CLID),
+       AST_APP_OPTION_ARG('O', OPT_OPERMODE,OPT_ARG_OPERMODE),
        AST_APP_OPTION('p', OPT_SCREENING),
        AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
        AST_APP_OPTION('r', OPT_RINGBACK),
        AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
        AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
        AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
+       AST_APP_OPTION_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
        AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
        AST_APP_OPTION('W', OPT_CALLER_MONITOR),
-       AST_APP_OPTION('k', OPT_CALLEE_PARK),
-       AST_APP_OPTION('K', OPT_CALLER_PARK),
-});
+END_OPTIONS );
 
-/* We define a custom "local user" structure because we
-   use it not only for keeping track of what is in use but
-   also for keeping track of who we're dialing. */
+#define CAN_EARLY_BRIDGE(flags) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
+       OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
+       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK | OPT_CALLER_PARK))
 
-struct dial_localuser {
+/*
+ * The list of active channels
+ */
+struct chanlist {
+       struct chanlist *next;
        struct ast_channel *chan;
-       unsigned int flags;
+       uint64_t flags;
        int forwards;
-       struct dial_localuser *next;
 };
 
 
-static void hanguptree(struct dial_localuser *outgoing, struct ast_channel *exception)
+static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception, int answered_elsewhere)
 {
        /* Hang up a tree of stuff */
-       struct dial_localuser *oo;
+       struct chanlist *oo;
        while (outgoing) {
                /* Hangup any existing lines we have open */
-               if (outgoing->chan && (outgoing->chan != exception))
+               if (outgoing->chan && (outgoing->chan != exception)) {
+                       if (answered_elsewhere)
+                               ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
                        ast_hangup(outgoing->chan);
+               }
                oo = outgoing;
                outgoing=outgoing->next;
-               free(oo);
+               ast_free(oo);
        }
 }
 
@@ -425,10 +449,11 @@ static void senddialendevent(const struct ast_channel *src, const char *dialstat
  * XXX this code is highly suspicious, as it essentially overwrites
  * the outgoing channel without properly deleting it.
  */
-static void do_forward(struct dial_localuser *o,
-       struct cause_args *num, struct ast_flags *peerflags, int single)
+static void do_forward(struct chanlist *o,
+       struct cause_args *num, struct ast_flags64 *peerflags, int single)
 {
        char tmpchan[256];
+       struct ast_channel *original = o->chan;
        struct ast_channel *c = o->chan; /* the winner */
        struct ast_channel *in = num->chan; /* the input channel */
        char *stuff;
@@ -448,37 +473,36 @@ static void do_forward(struct dial_localuser *o,
        /* Before processing channel, go ahead and check for forwarding */
        o->forwards++;
        if (o->forwards < AST_MAX_FORWARDS) {
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
+               ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
                /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
-               if (ast_test_flag(peerflags, OPT_IGNORE_FORWARDING)) {
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
+               if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
+                       ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
                        c = o->chan = NULL;
                        cause = AST_CAUSE_BUSY;
                } else {
                        /* Setup parameters */
                        c = o->chan = ast_request(tech, in->nativeformats, stuff, &cause);
-                       if (!c)
-                               ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
-                       else
+                       if (c) {
+                               if (single)
+                                       ast_channel_make_compatible(o->chan, in);
                                ast_channel_inherit_variables(in, o->chan);
+                       } else
+                               ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
                }
        } else {
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Too many forwards from %s\n", c->name);
+               ast_verb(3, "Too many forwards from %s\n", c->name);
                cause = AST_CAUSE_CONGESTION;
                c = o->chan = NULL;
        }
        if (!c) {
-               ast_clear_flag(o, DIAL_STILLGOING);     
+               ast_clear_flag64(o, DIAL_STILLGOING);   
                handle_cause(cause, num);
        } else {
                char *new_cid_num, *new_cid_name;
                struct ast_channel *src;
 
                ast_rtp_make_compatible(c, in, single);
-               if (ast_test_flag(o, OPT_FORCECLID)) {
+               if (ast_test_flag64(o, OPT_FORCECLID)) {
                        new_cid_num = ast_strdup(S_OR(in->macroexten, in->exten));
                        new_cid_name = NULL; /* XXX no name ? */
                        src = c;        /* XXX possible bug in previous code, which used 'winner' ? it may have changed */
@@ -498,19 +522,19 @@ static void do_forward(struct dial_localuser *o,
                S_REPLACE(c->cid.cid_rdnis, ast_strdup(S_OR(in->macroexten, in->exten)));
                if (ast_call(c, tmpchan, 0)) {
                        ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
-                       ast_clear_flag(o, DIAL_STILLGOING);     
-                       ast_hangup(c);
+                       ast_clear_flag64(o, DIAL_STILLGOING);   
+                       ast_hangup(original);
                        c = o->chan = NULL;
                        num->nochan++;
                } else {
                        senddialevent(in, c);
                        /* After calling, set callerid to extension */
-                       if (!ast_test_flag(peerflags, OPT_ORIGINAL_CLID)) {
-                               char cidname[AST_MAX_EXTENSION];
+                       if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
+                               char cidname[AST_MAX_EXTENSION] = "";
                                ast_set_callerid(c, S_OR(in->macroexten, in->exten), get_cid_name(cidname, sizeof(cidname), in), NULL);
                        }
                        /* Hangup the original channel now, in case we needed it */
-                       ast_hangup(c);
+                       ast_hangup(original);
                }
        }
 }
@@ -525,16 +549,19 @@ struct privacy_args {
 };
 
 static struct ast_channel *wait_for_answer(struct ast_channel *in,
-       struct dial_localuser *outgoing, int *to, struct ast_flags *peerflags,
+       struct chanlist *outgoing, int *to, struct ast_flags64 *peerflags,
        struct privacy_args *pa,
-       const struct cause_args *num_in, int priority_jump, int *result)
+       const struct cause_args *num_in, int *result)
 {
        struct cause_args num = *num_in;
        int prestart = num.busy + num.congestion + num.nochan;
        int orig = *to;
        struct ast_channel *peer = NULL;
        /* single is set if only one destination is enabled */
-       int single = outgoing && !outgoing->next && !ast_test_flag(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
+       int single = outgoing && !outgoing->next && !ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
+#ifdef HAVE_EPOLL
+       struct chanlist *epollo;
+#endif
        
        if (single) {
                /* Turn off hold music, etc */
@@ -542,10 +569,14 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                /* If we are calling a single channel, make them compatible for in-band tone purpose */
                ast_channel_make_compatible(outgoing->chan, in);
        }
-       
+
+#ifdef HAVE_EPOLL
+       for (epollo = outgoing; epollo; epollo = epollo->next)
+               ast_poll_channel_add(in, epollo->chan);
+#endif 
        
        while (*to && !peer) {
-               struct dial_localuser *o;
+               struct chanlist *o;
                int pos = 0;    /* how many channels do we handle */
                int numlines = prestart;
                struct ast_channel *winner;
@@ -554,25 +585,21 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                watchers[pos++] = in;
                for (o = outgoing; o; o = o->next) {
                        /* Keep track of important channels */
-                       if (ast_test_flag(o, DIAL_STILLGOING) && o->chan)
+                       if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
                                watchers[pos++] = o->chan;
                        numlines++;
                }
                if (pos == 1) { /* only the input channel is available */
                        if (numlines == (num.busy + num.congestion + num.nochan)) {
-                               if (option_verbose > 2)
-                                       ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
+                               ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
                                if (num.busy)
                                        strcpy(pa->status, "BUSY");     
                                else if (num.congestion)
                                        strcpy(pa->status, "CONGESTION");
                                else if (num.nochan)
                                        strcpy(pa->status, "CHANUNAVAIL");
-                               if (ast_opt_priority_jumping || priority_jump)
-                                       ast_goto_if_exists(in, in->context, in->exten, in->priority + 101);
                        } else {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
+                               ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
                        }
                        *to = 0;
                        return NULL;
@@ -584,12 +611,11 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
 
                        if (c == NULL)
                                continue;
-                       if (ast_test_flag(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
+                       if (ast_test_flag64(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
                                if (!peer) {
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "%s answered %s\n", c->name, in->name);
+                                       ast_verb(3, "%s answered %s\n", c->name, in->name);
                                        peer = c;
-                                       ast_copy_flags(peerflags, o,
+                                       ast_copy_flags64(peerflags, o,
                                                       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
                                                       OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
                                                       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
@@ -612,7 +638,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                in->hangupcause = c->hangupcause;
                                ast_hangup(c);
                                c = o->chan = NULL;
-                               ast_clear_flag(o, DIAL_STILLGOING);
+                               ast_clear_flag64(o, DIAL_STILLGOING);
                                handle_cause(in->hangupcause, &num);
                                continue;
                        }
@@ -621,10 +647,9 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                case AST_CONTROL_ANSWER:
                                        /* This is our guy if someone answered. */
                                        if (!peer) {
-                                               if (option_verbose > 2)
-                                                       ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", c->name, in->name);
+                                               ast_verb(3, "%s answered %s\n", c->name, in->name);
                                                peer = c;
-                                               ast_copy_flags(peerflags, o,
+                                               ast_copy_flags64(peerflags, o,
                                                               OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
                                                               OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
                                                               OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
@@ -632,72 +657,65 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                                               DIAL_NOFORWARDHTML);
                                                ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
                                                ast_copy_string(c->exten, "", sizeof(c->exten));
-                                               /* Setup early bridge if appropriate */
-                                               ast_channel_early_bridge(in, peer);
+                                               if (CAN_EARLY_BRIDGE(peerflags))
+                                                       /* Setup early bridge if appropriate */
+                                                       ast_channel_early_bridge(in, peer);
                                        }
                                        /* If call has been answered, then the eventual hangup is likely to be normal hangup */
                                        in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
                                        c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
                                        break;
                                case AST_CONTROL_BUSY:
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", c->name);
+                                       ast_verb(3, "%s is busy\n", c->name);
                                        in->hangupcause = c->hangupcause;
                                        ast_hangup(c);
                                        c = o->chan = NULL;
-                                       ast_clear_flag(o, DIAL_STILLGOING);     
+                                       ast_clear_flag64(o, DIAL_STILLGOING);   
                                        handle_cause(AST_CAUSE_BUSY, &num);
                                        break;
                                case AST_CONTROL_CONGESTION:
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", c->name);
+                                       ast_verb(3, "%s is circuit-busy\n", c->name);
                                        in->hangupcause = c->hangupcause;
                                        ast_hangup(c);
                                        c = o->chan = NULL;
-                                       ast_clear_flag(o, DIAL_STILLGOING);
+                                       ast_clear_flag64(o, DIAL_STILLGOING);
                                        handle_cause(AST_CAUSE_CONGESTION, &num);
                                        break;
                                case AST_CONTROL_RINGING:
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", c->name);
+                                       ast_verb(3, "%s is ringing\n", c->name);
                                        /* Setup early media if appropriate */
-                                       if (single)
+                                       if (single && CAN_EARLY_BRIDGE(peerflags))
                                                ast_channel_early_bridge(in, c);
-                                       if (!(pa->sentringing) && !ast_test_flag(outgoing, OPT_MUSICBACK)) {
+                                       if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK)) {
                                                ast_indicate(in, AST_CONTROL_RINGING);
                                                pa->sentringing++;
                                        }
                                        break;
                                case AST_CONTROL_PROGRESS:
-                                       if (option_verbose > 2)
-                                               ast_verbose (VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", c->name, in->name);
+                                       ast_verb(3, "%s is making progress passing it to %s\n", c->name, in->name);
                                        /* Setup early media if appropriate */
-                                       if (single)
+                                       if (single && CAN_EARLY_BRIDGE(peerflags))
                                                ast_channel_early_bridge(in, c);
-                                       if (!ast_test_flag(outgoing, OPT_RINGBACK))
+                                       if (!ast_test_flag64(outgoing, OPT_RINGBACK))
                                                ast_indicate(in, AST_CONTROL_PROGRESS);
                                        break;
                                case AST_CONTROL_VIDUPDATE:
-                                       if (option_verbose > 2)
-                                               ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", c->name, in->name);
+                                       ast_verb(3, "%s requested a video update, passing it to %s\n", c->name, in->name);
                                        ast_indicate(in, AST_CONTROL_VIDUPDATE);
                                        break;
                                case AST_CONTROL_PROCEEDING:
-                                       if (option_verbose > 2)
-                                               ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", c->name, in->name);
-                                       if (single)
+                                       ast_verb(3, "%s is proceeding passing it to %s\n", c->name, in->name);
+                                       if (single && CAN_EARLY_BRIDGE(peerflags))
                                                ast_channel_early_bridge(in, c);
-                                       if (!ast_test_flag(outgoing, OPT_RINGBACK))
+                                       if (!ast_test_flag64(outgoing, OPT_RINGBACK))
                                                ast_indicate(in, AST_CONTROL_PROCEEDING);
                                        break;
                                case AST_CONTROL_HOLD:
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", c->name);
+                                       ast_verb(3, "Call on %s placed on hold\n", c->name);
                                        ast_indicate(in, AST_CONTROL_HOLD);
                                        break;
                                case AST_CONTROL_UNHOLD:
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", c->name);
+                                       ast_verb(3, "Call on %s left from hold\n", c->name);
                                        ast_indicate(in, AST_CONTROL_UNHOLD);
                                        break;
                                case AST_CONTROL_OFFHOOK:
@@ -705,29 +723,27 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                        /* Ignore going off hook and flash */
                                        break;
                                case -1:
-                                       if (!ast_test_flag(outgoing, OPT_RINGBACK | OPT_MUSICBACK)) {
-                                               if (option_verbose > 2)
-                                                       ast_verbose(VERBOSE_PREFIX_3 "%s stopped sounds\n", c->name);
+                                       if (!ast_test_flag64(outgoing, OPT_RINGBACK | OPT_MUSICBACK)) {
+                                               ast_verb(3, "%s stopped sounds\n", c->name);
                                                ast_indicate(in, -1);
                                                pa->sentringing = 0;
                                        }
                                        break;
                                default:
-                                       if (option_debug)
-                                               ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
+                                       ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
                                }
                        } else if (single) {
                                /* XXX are we sure the logic is correct ? or we should just switch on f->frametype ? */
-                               if (f->frametype == AST_FRAME_VOICE && !ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
+                               if (f->frametype == AST_FRAME_VOICE && !ast_test_flag64(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
                                        if (ast_write(in, f)) 
                                                ast_log(LOG_WARNING, "Unable to forward voice frame\n");
-                               } else if (f->frametype == AST_FRAME_IMAGE && !ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
+                               } else if (f->frametype == AST_FRAME_IMAGE && !ast_test_flag64(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
                                        if (ast_write(in, f))
                                                ast_log(LOG_WARNING, "Unable to forward image\n");
-                               } else if (f->frametype == AST_FRAME_TEXT && !ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
+                               } else if (f->frametype == AST_FRAME_TEXT && !ast_test_flag64(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
                                        if (ast_write(in, f))
                                                ast_log(LOG_WARNING, "Unable to send text\n");
-                               } else if (f->frametype == AST_FRAME_HTML && !ast_test_flag(outgoing, DIAL_NOFORWARDHTML)) {
+                               } else if (f->frametype == AST_FRAME_HTML && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)) {
                                        if (ast_channel_sendhtml(in, f->subclass, f->data, f->datalen) == -1)
                                                ast_log(LOG_WARNING, "Unable to send URL\n");
                                }
@@ -746,6 +762,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                /* Got hung up */
                                *to = -1;
                                strcpy(pa->status, "CANCEL");
+                               ast_cdr_noanswer(in->cdr);
                                if (f)
                                        ast_frfree(f);
                                return NULL;
@@ -753,12 +770,12 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
 
                        /* now f is guaranteed non-NULL */
                        if (f->frametype == AST_FRAME_DTMF) {
-                               if (ast_test_flag(peerflags, OPT_DTMF_EXIT)) {
+                               if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
                                        const char *context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
                                        if (onedigit_goto(in, context, (char) f->subclass, 1)) {
-                                               if (option_verbose > 2)
-                                                       ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
+                                               ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
                                                *to=0;
+                                               ast_cdr_noanswer(in->cdr);
                                                *result = f->subclass;
                                                strcpy(pa->status, "CANCEL");
                                                ast_frfree(f);
@@ -766,41 +783,49 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                        }
                                }
 
-                               if (ast_test_flag(peerflags, OPT_CALLER_HANGUP) && 
+                               if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) && 
                                                  (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
+                                       ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
                                        *to=0;
                                        strcpy(pa->status, "CANCEL");
+                                       ast_cdr_noanswer(in->cdr);
                                        ast_frfree(f);
                                        return NULL;
                                }
                        }
 
                        /* Forward HTML stuff */
-                       if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag(outgoing, DIAL_NOFORWARDHTML)) 
+                       if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)) 
                                if(ast_channel_sendhtml(outgoing->chan, f->subclass, f->data, f->datalen) == -1)
                                        ast_log(LOG_WARNING, "Unable to send URL\n");
                        
 
-                       if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF)))  {
+                       if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF_BEGIN) || (f->frametype == AST_FRAME_DTMF_END)))  {
                                if (ast_write(outgoing->chan, f))
-                                       ast_log(LOG_WARNING, "Unable to forward voice\n");
+                                       ast_log(LOG_WARNING, "Unable to forward voice or dtmf\n");
                        }
                        if (single && (f->frametype == AST_FRAME_CONTROL) && 
                                ((f->subclass == AST_CONTROL_HOLD) || 
                                 (f->subclass == AST_CONTROL_UNHOLD) || 
                                 (f->subclass == AST_CONTROL_VIDUPDATE))) {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
+                               ast_verb(3, "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
                                ast_indicate_data(outgoing->chan, f->subclass, f->data, f->datalen);
                        }
                        ast_frfree(f);
                }
-               if (!*to && (option_verbose > 2))
-                       ast_verbose(VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
+               if (!*to)
+                       ast_verb(3, "Nobody picked up in %d ms\n", orig);
+               if (!*to || ast_check_hangup(in)) {
+                       ast_cdr_noanswer(in->cdr);
+               }
+               
        }
 
+#ifdef HAVE_EPOLL
+       for (epollo = outgoing; epollo; epollo = epollo->next)
+               ast_poll_channel_del(in, epollo->chan);
+#endif
+
        return peer;
 }
 
@@ -808,18 +833,18 @@ static void replace_macro_delimiter(char *s)
 {
        for (; *s; s++)
                if (*s == '^')
-                       *s = '|';
+                       *s = ',';
 }
 
 
 /* returns true if there is a valid privacy reply */
-static int valid_priv_reply(struct ast_flags *opts, int res)
+static int valid_priv_reply(struct ast_flags64 *opts, int res)
 {
        if (res < '1')
                return 0;
-       if (ast_test_flag(opts, OPT_PRIVACY) && res <= '5')
+       if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
                return 1;
-       if (ast_test_flag(opts, OPT_SCREENING) && res <= '4')
+       if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
                return 1;
        return 0;
 }
@@ -899,21 +924,20 @@ static int do_timelimit(struct ast_channel *chan, struct ast_bridge_config *conf
        /* more efficient to do it like S(x) does since no advanced opts */
        if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
                *calldurationlimit = config->timelimit / 1000;
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Setting call duration limit to %d seconds.\n",
+               ast_verb(3, "Setting call duration limit to %d seconds.\n",
                                *calldurationlimit);
                config->timelimit = play_to_caller = play_to_callee =
                config->play_warning = config->warning_freq = 0;
-       } else if (option_verbose > 2) {
-               ast_verbose(VERBOSE_PREFIX_3 "Limit Data for this call:\n");
-               ast_verbose(VERBOSE_PREFIX_4 "timelimit      = %ld\n", config->timelimit);
-               ast_verbose(VERBOSE_PREFIX_4 "play_warning   = %ld\n", config->play_warning);
-               ast_verbose(VERBOSE_PREFIX_4 "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
-               ast_verbose(VERBOSE_PREFIX_4 "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
-               ast_verbose(VERBOSE_PREFIX_4 "warning_freq   = %ld\n", config->warning_freq);
-               ast_verbose(VERBOSE_PREFIX_4 "start_sound    = %s\n", S_OR(config->start_sound, ""));
-               ast_verbose(VERBOSE_PREFIX_4 "warning_sound  = %s\n", config->warning_sound);
-               ast_verbose(VERBOSE_PREFIX_4 "end_sound      = %s\n", S_OR(config->end_sound, ""));
+       } else {
+               ast_verb(3, "Limit Data for this call:\n");
+               ast_verb(4, "timelimit      = %ld\n", config->timelimit);
+               ast_verb(4, "play_warning   = %ld\n", config->play_warning);
+               ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
+               ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
+               ast_verb(4, "warning_freq   = %ld\n", config->warning_freq);
+               ast_verb(4, "start_sound    = %s\n", S_OR(config->start_sound, ""));
+               ast_verb(4, "warning_sound  = %s\n", config->warning_sound);
+               ast_verb(4, "end_sound      = %s\n", S_OR(config->end_sound, ""));
        }
         if (play_to_caller)
                 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
@@ -923,7 +947,7 @@ static int do_timelimit(struct ast_channel *chan, struct ast_bridge_config *conf
 }
 
 static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
-    struct ast_flags *opts, char **opt_args, struct privacy_args *pa)
+    struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
 {
 
        int res2;
@@ -937,10 +961,13 @@ static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
           target extension was picked up. We are going to have to kill some
           time and make the caller believe the peer hasn't picked up yet */
 
-       if (ast_test_flag(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
+       if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
+               char *original_moh = ast_strdupa(chan->musicclass);
                ast_indicate(chan, -1);
+               ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
                ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
-       } else if (ast_test_flag(opts, OPT_RINGBACK)) {
+               ast_string_field_set(chan, musicclass, original_moh);
+       } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
                ast_indicate(chan, AST_CONTROL_RINGING);
                pa->sentringing++;
        }
@@ -965,9 +992,9 @@ static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
                /* now get input from the called party, as to their choice */
                if( !res2 ) {
                        /* XXX can we have both, or they are mutually exclusive ? */
-                       if( ast_test_flag(opts, OPT_PRIVACY) )
+                       if( ast_test_flag64(opts, OPT_PRIVACY) )
                                res2 = ast_play_and_wait(peer,"priv-callee-options");
-                       if( ast_test_flag(opts, OPT_SCREENING) )
+                       if( ast_test_flag64(opts, OPT_SCREENING) )
                                res2 = ast_play_and_wait(peer,"screen-callee-options");
                }
                /*! \page DialPrivacy Dial Privacy scripts
@@ -992,20 +1019,19 @@ static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
                res2 = ast_play_and_wait(peer, "vm-sorry");
        }
 
-       if (ast_test_flag(opts, OPT_MUSICBACK)) {
+       if (ast_test_flag64(opts, OPT_MUSICBACK)) {
                ast_moh_stop(chan);
-       } else if (ast_test_flag(opts, OPT_RINGBACK)) {
+       } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
                ast_indicate(chan, -1);
                pa->sentringing=0;
        }
        ast_autoservice_stop(chan);
-       if(ast_test_flag(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
+       if(ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
                /* map keypresses to various things, the index is res2 - '1' */
                static const char *_val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
                static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
                int i = res2 - '1';
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "--Set privacy database entry %s/%s to %s\n",
+               ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
                                             opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
                ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
        }
@@ -1023,7 +1049,7 @@ static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
                break;
        case '5':
                /* XXX should we set status to DENY ? */
-               if( ast_test_flag(opts, OPT_PRIVACY) )
+               if( ast_test_flag64(opts, OPT_PRIVACY) )
                        break;
                /* if not privacy, then 5 is the same as "default" case */
        default:        /* bad input or -1 if failure to start autoservice */
@@ -1040,12 +1066,12 @@ static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
        if (res2 == '1') {      /* the only case where we actually connect */
                /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll 
                   just clog things up, and it's not useful information, not being tied to a CID */
-               if( strncmp(pa->privcid,"NOCALLERID",10) == 0 || ast_test_flag(opts, OPT_SCREEN_NOINTRO) ) {
+               if( strncmp(pa->privcid,"NOCALLERID",10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO) ) {
                        ast_filedelete(pa->privintro, NULL);
                        if( ast_fileexists(pa->privintro, NULL, NULL ) > 0 )
                                ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
-                       else if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Successfully deleted %s intro file\n", pa->privintro);
+                       else
+                               ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
                }
                return 0;       /* the good exit path */
        } else {
@@ -1054,18 +1080,120 @@ static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
        }
 }
 
-static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags *peerflags)
+/*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
+static int setup_privacy_args(struct privacy_args *pa,
+       struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
+{
+       char callerid[60];
+       int res;
+       char *l;
+
+       if (!ast_strlen_zero(chan->cid.cid_num)) {
+               l = ast_strdupa(chan->cid.cid_num);
+               ast_shrink_phone_number(l);
+               if (ast_test_flag64(opts, OPT_PRIVACY) ) {
+                       ast_verb(3, "Privacy DB is '%s', clid is '%s'\n",
+                                            opt_args[OPT_ARG_PRIVACY], l);
+                       pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
+               } else {
+                       ast_verb(3,  "Privacy Screening, clid is '%s'\n", l);
+                       pa->privdb_val = AST_PRIVACY_UNKNOWN;
+               }
+       } else {
+               char *tnam, *tn2;
+
+               tnam = ast_strdupa(chan->name);
+               /* clean the channel name so slashes don't try to end up in disk file name */
+               for (tn2 = tnam; *tn2; tn2++) {
+                       if (*tn2=='/')  /* any other chars to be afraid of? */
+                               *tn2 = '=';
+               }
+               ast_verb(3, "Privacy-- callerid is empty\n");
+
+               snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
+               l = callerid;
+               pa->privdb_val = AST_PRIVACY_UNKNOWN;
+       }
+       
+       ast_copy_string(pa->privcid,l,sizeof(pa->privcid));
+
+       if( strncmp(pa->privcid,"NOCALLERID",10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCLID) ) { /* if callerid is set, and ast_test_flag64(&opts, OPT_SCREEN_NOCLID) is set also */  
+               if (option_verbose > 2)
+                       ast_verbose( VERBOSE_PREFIX_3  "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
+               pa->privdb_val = AST_PRIVACY_ALLOW;
+       } else if (ast_test_flag64(opts, OPT_SCREEN_NOCLID) && strncmp(pa->privcid,"NOCALLERID",10) == 0 ) {
+               if (option_verbose > 2)
+                       ast_verbose( VERBOSE_PREFIX_3  "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
+       }
+       
+       if (pa->privdb_val == AST_PRIVACY_DENY ) {
+               ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
+               if (option_verbose > 2)
+                       ast_verbose( VERBOSE_PREFIX_3  "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
+               return 0;
+       } else if (pa->privdb_val == AST_PRIVACY_KILL ) {
+               ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
+               return 0; /* Is this right? */
+       } else if (pa->privdb_val == AST_PRIVACY_TORTURE ) {
+               ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
+               return 0; /* is this right??? */
+       } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN ) {
+               /* Get the user's intro, store it in priv-callerintros/$CID, 
+                  unless it is already there-- this should be done before the 
+                  call is actually dialed  */
+
+               /* make sure the priv-callerintros dir actually exists */
+               snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
+               if ((res = ast_mkdir(pa->privintro, 0755))) {
+                       ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
+                       return -1;
+               }
+
+               snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
+               if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
+                       /* the DELUX version of this code would allow this caller the
+                          option to hear and retape their previously recorded intro.
+                       */
+               } else {
+                       int duration; /* for feedback from play_and_wait */
+                       /* the file doesn't exist yet. Let the caller submit his
+                          vocal intro for posterity */
+                       /* priv-recordintro script:
+
+                          "At the tone, please say your name:"
+
+                       */
+                       ast_answer(chan);
+                       res = ast_play_and_record(chan, "priv-recordintro", pa->privintro, 4, "gsm", &duration, 128, 2000, 0);  /* NOTE: I've reduced the total time to 4 sec */
+                                                                       /* don't think we'll need a lock removed, we took care of
+                                                                          conflicts by naming the pa.privintro file */
+                       if (res == -1) {
+                               /* Delete the file regardless since they hung up during recording */
+                               ast_filedelete(pa->privintro, NULL);
+                               if (ast_fileexists(pa->privintro,NULL,NULL ) > 0 )
+                                       ast_log(LOG_NOTICE,"privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
+                               else
+                                       ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
+                               return -1;
+                       }
+                       if (!ast_streamfile(chan, "vm-dialout", chan->language) )
+                               ast_waitstream(chan, "");
+               }
+       }
+       return 1;       /* success */
+}
+
+static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags64 *peerflags, int *continue_exec)
 {
        int res = -1;   /* default: error */
-       struct ast_module_user *u;
        char *rest, *cur;       /* scan the list of destinations */
-       struct dial_localuser *outgoing = NULL; /* list of destinations */
+       struct chanlist *outgoing = NULL;       /* list of destinations */
        struct ast_channel *peer;
        int to; /* timeout */
        struct cause_args num = { chan, 0, 0, 0 };
        int cause;
-       char numsubst[AST_MAX_EXTENSION];
-       char cidname[AST_MAX_EXTENSION];
+       char numsubst[256];
+       char cidname[AST_MAX_EXTENSION] = "";
 
        struct ast_bridge_config config;
        unsigned int calldurationlimit = 0;
@@ -1073,6 +1201,7 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
        struct privacy_args pa = {
                .sentringing = 0,
                .privdb_val = 0,
+               .status = "INVALIDARGS",
        };
        int sentringing = 0, moh = 0;
        const char *outbound_group = NULL;
@@ -1086,183 +1215,87 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                             AST_APP_ARG(options);
                             AST_APP_ARG(url);
        );
-       struct ast_flags opts = { 0, };
+       struct ast_flags64 opts = { 0, };
        char *opt_args[OPT_ARG_ARRAY_SIZE];
 
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
+               pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
                return -1;
        }
 
-       u = ast_module_user_add(chan);  /* XXX is this the right place ? */
-
        parse = ast_strdupa(data);
-
+       
        AST_STANDARD_APP_ARGS(args, parse);
 
        memset(&config,0,sizeof(struct ast_bridge_config));
 
        if (!ast_strlen_zero(args.options) &&
-                       ast_app_parse_options(dial_exec_options, &opts, opt_args, args.options))
+                       ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
+               pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
                goto done;
+       }
 
        if (ast_strlen_zero(args.peers)) {
                ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
+               pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
                goto done;
        }
 
-       if (ast_test_flag(&opts, OPT_OPERMODE)) {
+       if (ast_test_flag64(&opts, OPT_OPERMODE)) {
                opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Setting operator services mode to %d.\n", opermode);
+               ast_verb(3, "Setting operator services mode to %d.\n", opermode);
        }
        
-       if (ast_test_flag(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
+       if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
                calldurationlimit = atoi(opt_args[OPT_ARG_DURATION_STOP]);
                if (!calldurationlimit) {
                        ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
+                       pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
                        goto done;
                }
-               if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "Setting call duration limit to %d seconds.\n", calldurationlimit);
+               ast_verb(3, "Setting call duration limit to %d seconds.\n", calldurationlimit);
        }
 
-       if (ast_test_flag(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
+       if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
                dtmfcalling = opt_args[OPT_ARG_SENDDTMF];
                dtmfcalled = strsep(&dtmfcalling, ":");
        }
 
-       if (ast_test_flag(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
+       if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
                if (do_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
                        goto done;
        }
 
-       if (ast_test_flag(&opts, OPT_RESETCDR) && chan->cdr)
+       if (ast_test_flag64(&opts, OPT_RESETCDR) && chan->cdr)
                ast_cdr_reset(chan->cdr, NULL);
-       if (ast_test_flag(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
+       if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
                opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
-       if (ast_test_flag(&opts, OPT_PRIVACY) || ast_test_flag(&opts, OPT_SCREENING)) {
-               char callerid[60];
-               char *l = chan->cid.cid_num;    /* XXX watch out, we are overwriting it */
-               if (!ast_strlen_zero(l)) {
-                       ast_shrink_phone_number(l);
-                       if( ast_test_flag(&opts, OPT_PRIVACY) ) {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3  "Privacy DB is '%s', clid is '%s'\n",
-                                                    opt_args[OPT_ARG_PRIVACY], l);
-                               pa.privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
-                       }
-                       else {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3  "Privacy Screening, clid is '%s'\n", l);
-                               pa.privdb_val = AST_PRIVACY_UNKNOWN;
-                       }
-               } else {
-                       char *tnam, *tn2;
-
-                       tnam = ast_strdupa(chan->name);
-                       /* clean the channel name so slashes don't try to end up in disk file name */
-                       for(tn2 = tnam; *tn2; tn2++) {
-                               if( *tn2=='/')
-                                       *tn2 = '=';  /* any other chars to be afraid of? */
-                       }
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3  "Privacy-- callerid is empty\n");
-
-                       snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
-                       l = callerid;
-                       pa.privdb_val = AST_PRIVACY_UNKNOWN;
-               }
-               
-               ast_copy_string(pa.privcid,l,sizeof(pa.privcid));
 
-               if( strncmp(pa.privcid,"NOCALLERID",10) != 0 && ast_test_flag(&opts, OPT_SCREEN_NOCLID) ) { /* if callerid is set, and ast_test_flag(&opts, OPT_SCREEN_NOCLID) is set also */  
-                       if (option_verbose > 2)
-                               ast_verbose( VERBOSE_PREFIX_3  "CallerID set (%s); N option set; Screening should be off\n", pa.privcid);
-                       pa.privdb_val = AST_PRIVACY_ALLOW;
-               }
-               else if(ast_test_flag(&opts, OPT_SCREEN_NOCLID) && strncmp(pa.privcid,"NOCALLERID",10) == 0 ) {
-                       if (option_verbose > 2)
-                               ast_verbose( VERBOSE_PREFIX_3  "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa.privdb_val);
-               }
-               
-               if(pa.privdb_val == AST_PRIVACY_DENY ) {
-                       ast_copy_string(pa.status, "NOANSWER", sizeof(pa.status));
-                       if (option_verbose > 2)
-                               ast_verbose( VERBOSE_PREFIX_3  "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
-                       res=0;
+       if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
+               res = setup_privacy_args(&pa, &opts, opt_args, chan);
+               if (res <= 0)
                        goto out;
-               }
-               else if(pa.privdb_val == AST_PRIVACY_KILL ) {
-                       ast_copy_string(pa.status, "DONTCALL", sizeof(pa.status));
-                       if (ast_opt_priority_jumping || ast_test_flag(&opts, OPT_PRIORITY_JUMP)) {
-                               ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 201);
-                       }
-                       res = 0;
-                       goto out; /* Is this right? */
-               }
-               else if(pa.privdb_val == AST_PRIVACY_TORTURE ) {
-                       ast_copy_string(pa.status, "TORTURE", sizeof(pa.status));
-                       if (ast_opt_priority_jumping || ast_test_flag(&opts, OPT_PRIORITY_JUMP)) {
-                               ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 301);
-                       }
-                       res = 0;
-                       goto out; /* is this right??? */
-               }
-               else if(pa.privdb_val == AST_PRIVACY_UNKNOWN ) {
-                       /* Get the user's intro, store it in priv-callerintros/$CID, 
-                          unless it is already there-- this should be done before the 
-                          call is actually dialed  */
-
-                       /* make sure the priv-callerintros dir actually exists */
-                       snprintf(pa.privintro, sizeof(pa.privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
-                       if (mkdir(pa.privintro, 0755) && errno != EEXIST) {
-                               ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(errno));
-                               res = -1;
-                               goto out;
-                       }
-
-                       snprintf(pa.privintro,sizeof(pa.privintro), "priv-callerintros/%s", pa.privcid);
-                       if( ast_fileexists(pa.privintro,NULL,NULL ) > 0 && strncmp(pa.privcid,"NOCALLERID",10) != 0) {
-                               /* the DELUX version of this code would allow this caller the
-                                  option to hear and retape their previously recorded intro.
-                               */
-                       }
-                       else {
-                               int duration; /* for feedback from play_and_wait */
-                               /* the file doesn't exist yet. Let the caller submit his
-                                  vocal intro for posterity */
-                               /* priv-recordintro script:
-
-                                  "At the tone, please say your name:"
-
-                               */
-                               res = ast_play_and_record(chan, "priv-recordintro", pa.privintro, 4, "gsm", &duration, 128, 2000, 0);  /* NOTE: I've reduced the total time to 4 sec */
-                                                                               /* don't think we'll need a lock removed, we took care of
-                                                                                  conflicts by naming the pa.privintro file */
-                               if (res == -1) {
-                                       /* Delete the file regardless since they hung up during recording */
-                                        ast_filedelete(pa.privintro, NULL);
-                                        if( ast_fileexists(pa.privintro,NULL,NULL ) > 0 )
-                                                ast_log(LOG_NOTICE,"privacy: ast_filedelete didn't do its job on %s\n", pa.privintro);
-                                        else if (option_verbose > 2)
-                                                ast_verbose( VERBOSE_PREFIX_3 "Successfully deleted %s intro file\n", pa.privintro);
-                                       goto out;
-                               }
-                                if( !ast_streamfile(chan, "vm-dialout", chan->language) )
-                                        ast_waitstream(chan, "");
-                       }
-               }
+               res = -1;       /* reset default */
        }
 
+       if (continue_exec)
+               *continue_exec = 0;
+       
        /* If a channel group has been specified, get it for use when we create peer channels */
-       outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP");
-
-       ast_copy_flags(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING);
+       if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
+               outbound_group = ast_strdupa(outbound_group);
+               pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
+       } else {
+               outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP");
+       }
+           
+       ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING);
        /* loop through the list of dial destinations */
        rest = args.peers;
        while ((cur = strsep(&rest, "&")) ) {
-               struct dial_localuser *tmp;
+               struct chanlist *tmp;
+               struct ast_channel *tc; /* channel for this destination */
                /* Get a technology/[device:]number pair */
                char *number = cur;
                char *tech = strsep(&number, "/");
@@ -1273,138 +1306,143 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                if (!(tmp = ast_calloc(1, sizeof(*tmp))))
                        goto out;
                if (opts.flags) {
-                       ast_copy_flags(tmp, &opts,
+                       ast_copy_flags64(tmp, &opts,
+                                      OPT_CANCEL_ELSEWHERE |
                                       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
                                       OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
                                       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
                                       OPT_CALLEE_PARK | OPT_CALLER_PARK |
                                       OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID);
-                       ast_set2_flag(tmp, args.url, DIAL_NOFORWARDHTML);       
+                       ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);     
                }
                ast_copy_string(numsubst, number, sizeof(numsubst));
                /* Request the peer */
-               tmp->chan = ast_request(tech, chan->nativeformats, numsubst, &cause);
-               if (!tmp->chan) {
+               tc = ast_request(tech, chan->nativeformats, numsubst, &cause);
+               if (!tc) {
                        /* If we can't, just go on to the next call */
-                       ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n", tech, cause, ast_cause2str(cause));
+                       ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
+                                   tech, cause, ast_cause2str(cause));
                        handle_cause(cause, &num);
                        if (!rest)      /* we are on the last destination */
                                chan->hangupcause = cause;
+                       ast_free(tmp);
                        continue;
                }
-               pbx_builtin_setvar_helper(tmp->chan, "DIALEDPEERNUMBER", numsubst);
-               if (!ast_strlen_zero(tmp->chan->call_forward)) {
+               pbx_builtin_setvar_helper(tc, "DIALEDPEERNUMBER", numsubst);
+               if (!ast_strlen_zero(tc->call_forward)) {
                        char tmpchan[256];
                        char *stuff;
                        char *tech;
-                       ast_copy_string(tmpchan, tmp->chan->call_forward, sizeof(tmpchan));
+                       ast_copy_string(tmpchan, tc->call_forward, sizeof(tmpchan));
                        if ((stuff = strchr(tmpchan, '/'))) {
                                *stuff++ = '\0';
                                tech = tmpchan;
                        } else {
-                               snprintf(tmpchan, sizeof(tmpchan), "%s@%s", tmp->chan->call_forward, tmp->chan->context);
+                               snprintf(tmpchan, sizeof(tmpchan), "%s@%s", tc->call_forward, tc->context);
                                stuff = tmpchan;
                                tech = "Local";
                        }
                        tmp->forwards++;
                        if (tmp->forwards < AST_MAX_FORWARDS) {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Now forwarding %s to '%s/%s' (thanks to %s)\n", chan->name, tech, stuff, tmp->chan->name);
-                               ast_hangup(tmp->chan);
-                               /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
-                               if (ast_test_flag(&opts, OPT_IGNORE_FORWARDING)) {
-                                       tmp->chan = NULL;
+                               ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n",
+                                       chan->name, tech, stuff, tc->name);
+                               ast_hangup(tc);
+                               /* If we have been told to ignore forwards, just set this channel to null
+                                * and continue processing extensions normally */
+                               if (ast_test_flag64(&opts, OPT_IGNORE_FORWARDING)) {
+                                       tc = NULL;
                                        cause = AST_CAUSE_BUSY;
-                                       if (option_verbose > 2)
-                                               ast_verbose(VERBOSE_PREFIX_3 "Forwarding %s to '%s/%s' prevented.\n", chan->name, tech, stuff);
+                                       ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n",
+                                                       chan->name, tech, stuff);
                                } else {
-                                       tmp->chan = ast_request(tech, chan->nativeformats, stuff, &cause);
+                                       tc = ast_request(tech, chan->nativeformats, stuff, &cause);
                                }
-                               if (!tmp->chan)
+                               if (!tc)
                                        ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
                                else
-                                       ast_channel_inherit_variables(chan, tmp->chan);
+                                       ast_channel_inherit_variables(chan, tc);
                        } else {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Too many forwards from %s\n", tmp->chan->name);
-                               ast_hangup(tmp->chan);
-                               tmp->chan = NULL;
+                               ast_verb(3, "Too many forwards from %s\n", tc->name);
+                               ast_hangup(tc);
+                               tc = NULL;
                                cause = AST_CAUSE_CONGESTION;
                        }
-                       if (!tmp->chan) {
+                       if (!tc) {
                                handle_cause(cause, &num);
+                               ast_free(tmp);
                                continue;
                        }
                }
 
                /* Setup outgoing SDP to match incoming one */
-               ast_rtp_make_compatible(tmp->chan, chan, !outgoing && !rest);
+               ast_rtp_make_compatible(tc, chan, !outgoing && !rest);
                
                /* Inherit specially named variables from parent channel */
-               ast_channel_inherit_variables(chan, tmp->chan);
+               ast_channel_inherit_variables(chan, tc);
 
-               tmp->chan->appl = "AppDial";
-               tmp->chan->data = "(Outgoing Line)";
-               tmp->chan->whentohangup = 0;
+               tc->appl = "AppDial";
+               tc->data = "(Outgoing Line)";
+               tc->whentohangup = 0;
 
-               S_REPLACE(tmp->chan->cid.cid_num, ast_strdup(chan->cid.cid_num));
-               S_REPLACE(tmp->chan->cid.cid_name, ast_strdup(chan->cid.cid_name));
-               S_REPLACE(tmp->chan->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
-               S_REPLACE(tmp->chan->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
+               S_REPLACE(tc->cid.cid_num, ast_strdup(chan->cid.cid_num));
+               S_REPLACE(tc->cid.cid_name, ast_strdup(chan->cid.cid_name));
+               S_REPLACE(tc->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
+               S_REPLACE(tc->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
                
                /* Copy language from incoming to outgoing */
-               ast_string_field_set(tmp->chan, language, chan->language);
-               ast_string_field_set(tmp->chan, accountcode, chan->accountcode);
-               tmp->chan->cdrflags = chan->cdrflags;
-               if (ast_strlen_zero(tmp->chan->musicclass))
-                       ast_string_field_set(tmp->chan, musicclass, chan->musicclass);
-               /* Pass callingpres setting */
-               tmp->chan->cid.cid_pres = chan->cid.cid_pres;
-               /* Pass type of number */
-               tmp->chan->cid.cid_ton = chan->cid.cid_ton;
-               /* Pass type of tns */
-               tmp->chan->cid.cid_tns = chan->cid.cid_tns;
-               /* Presense of ADSI CPE on outgoing channel follows ours */
-               tmp->chan->adsicpe = chan->adsicpe;
-               /* Pass the transfer capability */
-               tmp->chan->transfercapability = chan->transfercapability;
+               ast_string_field_set(tc, language, chan->language);
+               ast_string_field_set(tc, accountcode, chan->accountcode);
+               tc->cdrflags = chan->cdrflags;
+               if (ast_strlen_zero(tc->musicclass))
+                       ast_string_field_set(tc, musicclass, chan->musicclass);
+               /* Pass callingpres, type of number, tns, ADSI CPE, transfer capability */
+               tc->cid.cid_pres = chan->cid.cid_pres;
+               tc->cid.cid_ton = chan->cid.cid_ton;
+               tc->cid.cid_tns = chan->cid.cid_tns;
+               tc->cid.cid_ani2 = chan->cid.cid_ani2;
+               tc->adsicpe = chan->adsicpe;
+               tc->transfercapability = chan->transfercapability;
 
                /* If we have an outbound group, set this peer channel to it */
                if (outbound_group)
-                       ast_app_group_set_channel(tmp->chan, outbound_group);
+                       ast_app_group_set_channel(tc, outbound_group);
 
                /* Inherit context and extension */
-               ast_copy_string(tmp->chan->dialcontext, chan->context, sizeof(tmp->chan->dialcontext));
-               ast_copy_string(tmp->chan->exten, chan->exten, sizeof(tmp->chan->exten));
+               if (!ast_strlen_zero(chan->macrocontext))
+                       ast_copy_string(tc->dialcontext, chan->macrocontext, sizeof(tc->dialcontext));
+               else
+                       ast_copy_string(tc->dialcontext, chan->context, sizeof(tc->dialcontext));
+               if (!ast_strlen_zero(chan->macroexten))
+                       ast_copy_string(tc->exten, chan->macroexten, sizeof(tc->exten));
+               else
+                       ast_copy_string(tc->exten, chan->exten, sizeof(tc->exten));
 
-               /* Place the call, but don't wait on the answer */
-               res = ast_call(tmp->chan, numsubst, 0);
+               res = ast_call(tc, numsubst, 0);        /* Place the call, but don't wait on the answer */
 
                /* Save the info in cdr's that we called them */
                if (chan->cdr)
-                       ast_cdr_setdestchan(chan->cdr, tmp->chan->name);
+                       ast_cdr_setdestchan(chan->cdr, tc->name);
 
                /* check the results of ast_call */
                if (res) {
                        /* Again, keep going even if there's an error */
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst);
-                       ast_hangup(tmp->chan);
-                       tmp->chan = NULL;
+                       ast_debug(1, "ast call on peer returned %d\n", res);
+                       ast_verb(3, "Couldn't call %s\n", numsubst);
+                       ast_hangup(tc);
+                       tc = NULL;
+                       ast_free(tmp);
                        continue;
                } else {
-                       senddialevent(chan, tmp->chan);
-                       if (option_verbose > 2)
-                               ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
-                       if (!ast_test_flag(peerflags, OPT_ORIGINAL_CLID))
-                               ast_set_callerid(tmp->chan, S_OR(chan->macroexten, chan->exten), get_cid_name(cidname, sizeof(cidname), chan), NULL);
+                       senddialevent(chan, tc);
+                       ast_verb(3, "Called %s\n", numsubst);
+                       if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID))
+                               ast_set_callerid(tc, S_OR(chan->macroexten, chan->exten), get_cid_name(cidname, sizeof(cidname), chan), NULL);
                }
                /* Put them in the list of outgoing thingies...  We're ready now. 
                   XXX If we're forcibly removed, these outgoing calls won't get
                   hung up XXX */
-               ast_set_flag(tmp, DIAL_STILLGOING);     
+               ast_set_flag64(tmp, DIAL_STILLGOING);   
+               tmp->chan = tc;
                tmp->next = outgoing;
                outgoing = tmp;
                /* If this line is up, don't try anybody else */
@@ -1427,17 +1465,25 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
        } else {
                /* Our status will at least be NOANSWER */
                strcpy(pa.status, "NOANSWER");
-               if (ast_test_flag(outgoing, OPT_MUSICBACK)) {
+               if (ast_test_flag64(outgoing, OPT_MUSICBACK)) {
                        moh = 1;
-                       ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
-               } else if (ast_test_flag(outgoing, OPT_RINGBACK)) {
+                       if (!ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
+                               char *original_moh = ast_strdupa(chan->musicclass);
+                               ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
+                               ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
+                               ast_string_field_set(chan, musicclass, original_moh);
+                       } else {
+                               ast_moh_start(chan, NULL, NULL);
+                       }
+                       ast_indicate(chan, AST_CONTROL_PROGRESS);
+               } else if (ast_test_flag64(outgoing, OPT_RINGBACK)) {
                        ast_indicate(chan, AST_CONTROL_RINGING);
                        sentringing++;
                }
        }
 
        time(&start_time);
-       peer = wait_for_answer(chan, outgoing, &to, peerflags, &pa, &num, ast_test_flag(&opts, OPT_PRIORITY_JUMP), &result);
+       peer = wait_for_answer(chan, outgoing, &to, peerflags, &pa, &num, &result);
        
        if (!peer) {
                if (result) {
@@ -1457,7 +1503,7 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                /* Ah ha!  Someone answered within the desired timeframe.  Of course after this
                   we will always return with -1 so that it is hung up properly after the 
                   conversation.  */
-               hanguptree(outgoing, peer);
+               hanguptree(outgoing, peer, 1);
                outgoing = NULL;
                /* If appropriate, log that we have a destination channel */
                if (chan->cdr)
@@ -1470,17 +1516,16 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                        number = numsubst;
                pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
                if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "app_dial: sendurl=%s.\n", args.url);
+                       ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
                        ast_channel_sendurl( peer, args.url );
                }
-               if ( (ast_test_flag(&opts, OPT_PRIVACY) || ast_test_flag(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
+               if ( (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
                        if (do_privacy(chan, peer, &opts, opt_args, &pa)) {
                                res = 0;
                                goto out;
                        }
                }
-               if (!ast_test_flag(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
+               if (!ast_test_flag64(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
                        res = 0;
                } else {
                        int digit = 0;
@@ -1495,24 +1540,26 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                        /* Ok, done. stop autoservice */
                        res = ast_autoservice_stop(chan);
                        if (digit > 0 && !res)
-                               res = ast_senddigit(chan, digit); 
+                               res = ast_senddigit(chan, digit, 0); 
                        else
                                res = digit;
 
                }
 
-               if (chan && peer && ast_test_flag(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
+               if (chan && peer && ast_test_flag64(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
                        replace_macro_delimiter(opt_args[OPT_ARG_GOTO]);
                        ast_parseable_goto(chan, opt_args[OPT_ARG_GOTO]);
                        ast_parseable_goto(peer, opt_args[OPT_ARG_GOTO]);
                        peer->priority++;
                        ast_pbx_start(peer);
-                       hanguptree(outgoing, NULL);
+                       hanguptree(outgoing, NULL, ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0);
+                       if (continue_exec)
+                               *continue_exec = 1;
                        res = 0;
                        goto done;
                }
 
-               if (ast_test_flag(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
+               if (ast_test_flag64(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
                        struct ast_app *theapp;
                        const char *macro_result;
 
@@ -1527,8 +1574,7 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                        if (theapp && !res) {   /* XXX why check res here ? */
                                replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
                                res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "Macro exited with status %d\n", res);
+                               ast_debug(1, "Macro exited with status %d\n", res);
                                res = 0;
                        } else {
                                ast_log(LOG_ERROR, "Could not find application Macro\n");
@@ -1545,23 +1591,18 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
 
                                if (!strcasecmp(macro_result, "BUSY")) {
                                        ast_copy_string(pa.status, macro_result, sizeof(pa.status));
-                                       if (ast_opt_priority_jumping || ast_test_flag(&opts, OPT_PRIORITY_JUMP)) {
-                                               if (!ast_goto_if_exists(chan, NULL, NULL, chan->priority + 101)) {
-                                                       ast_set_flag(peerflags, OPT_GO_ON);
-                                               }
-                                       } else
-                                               ast_set_flag(peerflags, OPT_GO_ON);
+                                       ast_set_flag64(peerflags, OPT_GO_ON);
                                        res = -1;
                                } else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
                                        ast_copy_string(pa.status, macro_result, sizeof(pa.status));
-                                       ast_set_flag(peerflags, OPT_GO_ON);     
+                                       ast_set_flag64(peerflags, OPT_GO_ON);   
                                        res = -1;
                                } else if (!strcasecmp(macro_result, "CONTINUE")) {
                                        /* hangup peer and keep chan alive assuming the macro has changed 
                                           the context / exten / priority or perhaps 
                                           the next priority in the current exten is desired.
                                        */
-                                       ast_set_flag(peerflags, OPT_GO_ON);     
+                                       ast_set_flag64(peerflags, OPT_GO_ON);   
                                        res = -1;
                                } else if (!strcasecmp(macro_result, "ABORT")) {
                                        /* Hangup both ends unless the caller has the g flag */
@@ -1572,7 +1613,90 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                                        if (strchr(macro_transfer_dest, '^')) { /* context^exten^priority*/
                                                replace_macro_delimiter(macro_transfer_dest);
                                                if (!ast_parseable_goto(chan, macro_transfer_dest))
-                                                       ast_set_flag(peerflags, OPT_GO_ON);
+                                                       ast_set_flag64(peerflags, OPT_GO_ON);
+                                       }
+                               }
+                       }
+               }
+
+               if (ast_test_flag64(&opts, OPT_CALLEE_GOSUB) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GOSUB])) {
+                       struct ast_app *theapp;
+                       const char *gosub_result;
+                       char *gosub_args, *gosub_argstart;
+
+                       res = ast_autoservice_start(chan);
+                       if (res) {
+                               ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
+                               res = -1;
+                       }
+
+                       theapp = pbx_findapp("Gosub");
+
+                       if (theapp && !res) {   /* XXX why check res here ? */
+                               replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GOSUB]);
+
+                               /* Set where we came from */
+                               ast_copy_string(peer->context, "app_dial_gosub_virtual_context", sizeof(peer->context));
+                               ast_copy_string(peer->exten, "s", sizeof(peer->exten));
+                               peer->priority = 0;
+
+                               gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], '|');
+                               if (gosub_argstart) {
+                                       *gosub_argstart = 0;
+                                       asprintf(&gosub_args, "%s|s|1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], gosub_argstart + 1);
+                                       *gosub_argstart = '|';
+                               } else {
+                                       asprintf(&gosub_args, "%s|s|1", opt_args[OPT_ARG_CALLEE_GOSUB]);
+                               }
+
+                               if (gosub_args) {
+                                       res = pbx_exec(peer, theapp, gosub_args);
+                                       ast_pbx_run(peer);
+                                       free(gosub_args);
+                                       if (option_debug)
+                                               ast_log(LOG_DEBUG, "Gosub exited with status %d\n", res);
+                               } else
+                                       ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
+                               
+                               res = 0;
+                       } else {
+                               ast_log(LOG_ERROR, "Could not find application Gosub\n");
+                               res = -1;
+                       }
+
+                       if (ast_autoservice_stop(chan) < 0) {
+                               ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
+                               res = -1;
+                       }
+
+                       if (!res && (gosub_result = pbx_builtin_getvar_helper(peer, "GOSUB_RESULT"))) {
+                               char *gosub_transfer_dest;
+
+                               if (!strcasecmp(gosub_result, "BUSY")) {
+                                       ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
+                                       ast_set_flag64(peerflags, OPT_GO_ON);
+                                       res = -1;
+                               } else if (!strcasecmp(gosub_result, "CONGESTION") || !strcasecmp(gosub_result, "CHANUNAVAIL")) {
+                                       ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
+                                       ast_set_flag64(peerflags, OPT_GO_ON);   
+                                       res = -1;
+                               } else if (!strcasecmp(gosub_result, "CONTINUE")) {
+                                       /* hangup peer and keep chan alive assuming the macro has changed 
+                                          the context / exten / priority or perhaps 
+                                          the next priority in the current exten is desired.
+                                       */
+                                       ast_set_flag64(peerflags, OPT_GO_ON);   
+                                       res = -1;
+                               } else if (!strcasecmp(gosub_result, "ABORT")) {
+                                       /* Hangup both ends unless the caller has the g flag */
+                                       res = -1;
+                               } else if (!strncasecmp(gosub_result, "GOTO:", 5) && (gosub_transfer_dest = ast_strdupa(gosub_result + 5))) {
+                                       res = -1;
+                                       /* perform a transfer to a new extension */
+                                       if (strchr(gosub_transfer_dest, '^')) { /* context^exten^priority*/
+                                               replace_macro_delimiter(gosub_transfer_dest);
+                                               if (!ast_parseable_goto(chan, gosub_transfer_dest))
+                                                       ast_set_flag64(peerflags, OPT_GO_ON);
                                        }
                                }
                        }
@@ -1580,17 +1704,17 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
 
                if (!res) {
                        if (calldurationlimit > 0) {
-                               chan->whentohangup = time(NULL) + calldurationlimit;
+                               peer->whentohangup = time(NULL) + calldurationlimit;
                        }
                        if (!ast_strlen_zero(dtmfcalled)) { 
                                if (option_verbose > 2)
                                        ast_verbose(VERBOSE_PREFIX_3 "Sending DTMF '%s' to the called party.\n", dtmfcalled);
-                               res = ast_dtmf_stream(peer,chan,dtmfcalled,250);
+                               res = ast_dtmf_stream(peer,chan,dtmfcalled,250,0);
                        }
                        if (!ast_strlen_zero(dtmfcalling)) {
                                if (option_verbose > 2)
                                        ast_verbose(VERBOSE_PREFIX_3 "Sending DTMF '%s' to the calling party.\n", dtmfcalling);
-                               res = ast_dtmf_stream(chan,peer,dtmfcalling,250);
+                               res = ast_dtmf_stream(chan,peer,dtmfcalling,250,0);
                        }
                }
                
@@ -1598,21 +1722,21 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                        res = -1;
                        end_time = time(NULL);
                } else {
-                       if (ast_test_flag(peerflags, OPT_CALLEE_TRANSFER))
+                       if (ast_test_flag64(peerflags, OPT_CALLEE_TRANSFER))
                                ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
-                       if (ast_test_flag(peerflags, OPT_CALLER_TRANSFER))
+                       if (ast_test_flag64(peerflags, OPT_CALLER_TRANSFER))
                                ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
-                       if (ast_test_flag(peerflags, OPT_CALLEE_HANGUP))
+                       if (ast_test_flag64(peerflags, OPT_CALLEE_HANGUP))
                                ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
-                       if (ast_test_flag(peerflags, OPT_CALLER_HANGUP))
+                       if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP))
                                ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
-                       if (ast_test_flag(peerflags, OPT_CALLEE_MONITOR))
+                       if (ast_test_flag64(peerflags, OPT_CALLEE_MONITOR))
                                ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
-                       if (ast_test_flag(peerflags, OPT_CALLER_MONITOR)) 
+                       if (ast_test_flag64(peerflags, OPT_CALLER_MONITOR)) 
                                ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
-                       if (ast_test_flag(peerflags, OPT_CALLEE_PARK))
+                       if (ast_test_flag64(peerflags, OPT_CALLEE_PARK))
                                ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
-                       if (ast_test_flag(peerflags, OPT_CALLER_PARK))
+                       if (ast_test_flag64(peerflags, OPT_CALLER_PARK))
                                ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
 
                        if (moh) {
@@ -1650,9 +1774,35 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
 
                snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
                pbx_builtin_setvar_helper(chan, "DIALEDTIME", toast);
-
+               
+               
+               if (ast_test_flag64(&opts, OPT_PEER_H)) {
+                       ast_log(LOG_NOTICE,"PEER context: %s; PEER exten: %s;  PEER priority: %d\n", 
+                                       peer->context, peer->exten, peer->priority);
+               }
+               
+               strcpy(peer->context, chan->context);
+
+               if (ast_test_flag64(&opts, OPT_PEER_H) && ast_exists_extension(peer, peer->context, "h", 1, peer->cid.cid_num)) {
+                       int autoloopflag;
+                       int found;
+                       strcpy(peer->exten, "h");
+                       peer->priority = 1;
+                       autoloopflag = ast_test_flag(peer, AST_FLAG_IN_AUTOLOOP);       /* save value to restore at the end */
+                       ast_set_flag(peer, AST_FLAG_IN_AUTOLOOP);
+                       
+                       while ((res = ast_spawn_extension(peer, peer->context, peer->exten, peer->priority, peer->cid.cid_num, &found,1))) {
+                               peer->priority++;
+                       }
+                       if (found && res) {
+                               /* Something bad happened, or a hangup has been requested. */
+                               ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
+                               ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
+                       }
+                       ast_set2_flag(peer, autoloopflag, AST_FLAG_IN_AUTOLOOP);  /* set it back the way it was */
+               }
                if (res != AST_PBX_NO_HANGUP_PEER) {
-                       if (!chan->_softhangup)
+                       if (!ast_check_hangup(chan))
                                chan->hangupcause = peer->hangupcause;
                        ast_hangup(peer);
                }
@@ -1666,67 +1816,59 @@ out:
                ast_indicate(chan, -1);
        }
        ast_channel_early_bridge(chan, NULL);
-       hanguptree(outgoing, NULL);
+       hanguptree(outgoing, NULL, 0);  /* In this case, there's no answer anywhere */
        pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
        senddialendevent(chan, pa.status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "Exiting with DIALSTATUS=%s.\n", pa.status);
+       ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
        
-       if ((ast_test_flag(peerflags, OPT_GO_ON)) && (!chan->_softhangup) && (res != AST_PBX_KEEPALIVE))
+       if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_KEEPALIVE)) {
+               if (calldurationlimit)
+                       chan->whentohangup = 0;
                res = 0;
+       }
 
 done:
-       ast_module_user_remove(u);      /* XXX probably not the right place for this. */
        return res;
 }
 
 static int dial_exec(struct ast_channel *chan, void *data)
 {
-       struct ast_flags peerflags;
+       struct ast_flags64 peerflags;
+
        memset(&peerflags, 0, sizeof(peerflags));
-       return dial_exec_full(chan, data, &peerflags);
+
+       return dial_exec_full(chan, data, &peerflags, NULL);
 }
 
 static int retrydial_exec(struct ast_channel *chan, void *data)
 {
-       char *announce = NULL, *dialdata = NULL;
+       char *parse;
        const char *context = NULL;
        int sleep = 0, loops = 0, res = -1;
-       struct ast_module_user *u;
-       struct ast_flags peerflags;
-       
+       struct ast_flags64 peerflags = { 0, };
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(announce);
+               AST_APP_ARG(sleep);
+               AST_APP_ARG(retries);
+               AST_APP_ARG(dialdata);
+       );
+
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
                return -1;
        }       
 
-       u = ast_module_user_add(chan);
+       parse = ast_strdupa(data);
+       AST_STANDARD_APP_ARGS(args, parse);
 
-       announce = ast_strdupa(data);
+       if ((sleep = atoi(args.sleep))) {
+               sleep *= 1000;
+       }
 
-       memset(&peerflags, 0, sizeof(peerflags));
+       loops = atoi(args.retries);
 
-       if ((dialdata = strchr(announce, '|'))) {
-               *dialdata++ = '\0';
-               if ((sleep = atoi(dialdata))) {
-                       sleep *= 1000;
-               } else {
-                       ast_log(LOG_ERROR, "%s requires the numerical argument <sleep>\n",rapp);
-                       goto done;
-               }
-               if ((dialdata = strchr(dialdata, '|'))) {
-                       *dialdata++ = '\0';
-                       if (!(loops = atoi(dialdata))) {
-                               ast_log(LOG_ERROR, "%s requires the numerical argument <loops>\n",rapp);
-                               goto done;
-                       }
-               }
-       }
-       
-       if ((dialdata = strchr(dialdata, '|'))) {
-               *dialdata++ = '\0';
-       } else {
-               ast_log(LOG_ERROR, "%s requires more arguments\n",rapp);
+       if (!args.dialdata) {
+               ast_log(LOG_ERROR, "%s requires a 4th argument (dialdata)\n", rapp);
                goto done;
        }
                
@@ -1740,26 +1882,42 @@ static int retrydial_exec(struct ast_channel *chan, void *data)
 
        res = 0;
        while (loops) {
+               int continue_exec;
+
                chan->data = "Retrying";
                if (ast_test_flag(chan, AST_FLAG_MOH))
                        ast_moh_stop(chan);
 
-               if ((res = dial_exec_full(chan, dialdata, &peerflags)) == 0) {
-                       if (ast_test_flag(&peerflags, OPT_DTMF_EXIT)) {
-                               if (!(res = ast_streamfile(chan, announce, chan->language)))
-                                       res = ast_waitstream(chan, AST_DIGIT_ANY);
+               res = dial_exec_full(chan, args.dialdata, &peerflags, &continue_exec);
+               if (continue_exec)
+                       break;
+
+               if (res == 0) {
+                       if (ast_test_flag64(&peerflags, OPT_DTMF_EXIT)) {
+                               if (!ast_strlen_zero(args.announce)) {
+                                       if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
+                                               if(!(res = ast_streamfile(chan, args.announce, chan->language)))                                                                
+                                                       ast_waitstream(chan, AST_DIGIT_ANY);
+                                       } else
+                                               ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
+                               }
                                if (!res && sleep) {
                                        if (!ast_test_flag(chan, AST_FLAG_MOH))
                                                ast_moh_start(chan, NULL, NULL);
                                        res = ast_waitfordigit(chan, sleep);
                                }
                        } else {
-                               if (!(res = ast_streamfile(chan, announce, chan->language)))
-                                       res = ast_waitstream(chan, "");
+                               if (!ast_strlen_zero(args.announce)) {
+                                       if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
+                                               if (!(res = ast_streamfile(chan, args.announce, chan->language)))
+                                                       res = ast_waitstream(chan, "");
+                                       } else
+                                               ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
+                               }
                                if (sleep) {
                                        if (!ast_test_flag(chan, AST_FLAG_MOH))
                                                ast_moh_start(chan, NULL, NULL);
-                                       if (!res) 
+                                       if (!res)
                                                res = ast_waitfordigit(chan, sleep);
                                }
                        }
@@ -1777,29 +1935,41 @@ static int retrydial_exec(struct ast_channel *chan, void *data)
        }
        if (loops == 0)
                res = 0;
-       
+       else if (res == 1)
+               res = 0;
+
        if (ast_test_flag(chan, AST_FLAG_MOH))
                ast_moh_stop(chan);
-done:
-       ast_module_user_remove(u);
+ done:
        return res;
 }
 
 static int unload_module(void)
 {
        int res;
+       struct ast_context *con;
 
        res = ast_unregister_application(app);
        res |= ast_unregister_application(rapp);
 
-       ast_module_user_hangup_all();
-       
+       if ((con = ast_context_find("app_dial_gosub_virtual_context")))
+               ast_context_remove_extension2(con, "s", 1, NULL);
+
        return res;
 }
 
 static int load_module(void)
 {
        int res;
+       struct ast_context *con;
+
+       con = ast_context_find("app_dial_gosub_virtual_context");
+       if (!con)
+               con = ast_context_create(NULL, "app_dial_gosub_virtual_context", "app_dial");
+       if (!con)
+               ast_log(LOG_ERROR, "Dial virtual context 'app_dial_gosub_virtual_context' does not exist and unable to create\n");
+       else
+               ast_add_extension2(con, 1, "s", 1, NULL, NULL, "KeepAlive", ast_strdup(""), ast_free_ptr, "app_dial");
 
        res = ast_register_application(app, dial_exec, synopsis, descrip);
        res |= ast_register_application(rapp, retrydial_exec, rsynopsis, rdescrip);