Index: apps/app_dial.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_dial.c,v
retrieving revision 1.104
diff -u -r1.104 app_dial.c
--- apps/app_dial.c	7 Nov 2004 21:49:43 -0000	1.104
+++ apps/app_dial.c	19 Nov 2004 15:17:47 -0000
@@ -68,7 +68,11 @@
 "             that are assigned to you.\n"
 "      'r' -- indicate ringing to the calling party, pass no audio until answered.\n"
 "      'm' -- provide hold music to the calling party until answered.\n"
-"      'M(x) -- Executes the macro (x) upon connect of the call\n"
+"      'M(x[^arg]) -- Executes the macro (x with ^ delim arg list) upon connect of the call.\n"
+"                     Also, the macro can set the MACRO_RESULT variable to do the following:\n"
+"                     -- ABORT - Hangup both legs of the call.\n"
+"                     -- CONTINUE - Hangup the called party and continue on in the dialplan.\n"
+"                     -- GOTO:[<exten>[@<context>[:<priority>]] | <exten>[:priority]>] - Transfer.\n"
 "      'h' -- allow callee to hang up by hitting *.\n"
 "      'H' -- allow caller to hang up by hitting *.\n"
 "      'C' -- reset call detail record for this call.\n"
@@ -480,7 +484,7 @@
 	char *sdtmfptr;
 	char sdtmfdata[256] = "";
 	char *stack,*var;
-	char *mac = NULL, macroname[256] = "";
+	char *mac = NULL, *macroname = NULL;
 	char status[256]="";
 	char toast[80];
 	int play_to_caller=0,play_to_callee=0;
@@ -488,9 +492,12 @@
 	char *varname;
 	int vartype;
 	char *outbound_group = NULL;
-
+	char *macro_result = NULL, *macro_transfer_exten = NULL, *macro_transfer_context = NULL;
 	int digit = 0;
 	time_t start_time, answer_time, end_time;
+	struct ast_app *app = NULL;
+	int mpri = 0;
+	char *mpri_ptr = NULL;
 
 	if (!data) {
 		ast_log(LOG_WARNING, "Dial requires an argument (technology1/number1&technology2/number2...|optional timeout|options)\n");
@@ -657,7 +664,7 @@
 		/* Get the macroname from the dial option string */
 		if ((mac = strstr(transfer, "M("))) {
 			hasmacro = 1;
-			strncpy(macroname, mac + 2, sizeof(macroname) - 1);
+			macroname = ast_strdupa(mac + 2);
 			while (*mac && (*mac != ')'))
 				*(mac++) = 'X';
 			if (*mac)
@@ -997,8 +1004,6 @@
 			res = 0;
 
 		if (hasmacro && macroname) {
-			void *app = NULL;
-
 			res = ast_autoservice_start(chan);
 			if (res) {
 				ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
@@ -1008,6 +1013,9 @@
 			app = pbx_findapp("Macro");
 
 			if (app && !res) {
+				for(res=0;res<strlen(macroname);res++)
+					if(macroname[res] == '^')
+						macroname[res] = '|';
 				res = pbx_exec(peer, app, macroname, 1);
 				ast_log(LOG_DEBUG, "Macro exited with status %d\n", res);
 				res = 0;
@@ -1019,6 +1027,52 @@
 			if (ast_autoservice_stop(chan) < 0) {
 				ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
 				res = -1;
+			}
+
+			if (!res) {
+				if((macro_result = pbx_builtin_getvar_helper(peer, "MACRO_RESULT"))) {
+					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.
+						*/
+						go_on = 1;
+						res = -1;
+					} else if (!strcasecmp(macro_result, "ABORT")) {
+						/* Hangup both ends unless the caller has the g flag */
+						res = -1;
+					} else if(!strncasecmp(macro_result, "GOTO:",5) && (macro_transfer_exten = ast_strdupa(macro_result + 5))) {
+						/* perform a transfer to a new extension
+						   e.g. 1234 or 1234:1 or 1234@mycon or 1234@mycon:1 
+						*/
+						mpri = 1;
+						if ((macro_transfer_context = strchr(macro_transfer_exten,'@'))) {
+							*macro_transfer_context = '\0';
+							macro_transfer_context++;
+							if((mpri_ptr = strchr(macro_transfer_context,':'))) {
+								*mpri_ptr = '\0';
+								mpri_ptr++;
+								mpri = atoi(mpri_ptr);
+							}
+						} else {
+							macro_transfer_context = chan->context;
+							if((mpri_ptr = strchr(macro_transfer_exten,':'))) {
+								*mpri_ptr = '\0';
+								mpri_ptr++;
+								mpri = atoi(mpri_ptr);
+							}
+						}
+						if(ast_exists_extension(chan, macro_transfer_context, macro_transfer_exten, mpri, chan->cid.cid_num)) {
+							if(strcmp(chan->context,macro_transfer_context))
+								strncpy(chan->context,macro_transfer_context, sizeof(chan->context));
+							strncpy(chan->exten,macro_transfer_exten, sizeof(chan->exten));
+							chan->priority = mpri - 1;
+							go_on = 1;
+							res = -1;
+						}
+
+					}
+				}
 			}
 		}
 
