Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.173
diff -u -r1.173 pbx.c
--- pbx.c	19 Nov 2004 05:18:10 -0000	1.173
+++ pbx.c	22 Nov 2004 20:52:42 -0000
@@ -153,36 +153,7 @@
 
 int ast_pbx_outgoing_cdr_failed(void);
 
-static int pbx_builtin_prefix(struct ast_channel *, void *);
-static int pbx_builtin_suffix(struct ast_channel *, void *);
-static int pbx_builtin_stripmsd(struct ast_channel *, void *);
-static int pbx_builtin_answer(struct ast_channel *, void *);
-static int pbx_builtin_goto(struct ast_channel *, void *);
-static int pbx_builtin_hangup(struct ast_channel *, void *);
-static int pbx_builtin_background(struct ast_channel *, void *);
-static int pbx_builtin_dtimeout(struct ast_channel *, void *);
-static int pbx_builtin_rtimeout(struct ast_channel *, void *);
-static int pbx_builtin_atimeout(struct ast_channel *, void *);
-static int pbx_builtin_wait(struct ast_channel *, void *);
-static int pbx_builtin_waitexten(struct ast_channel *, void *);
-static int pbx_builtin_setlanguage(struct ast_channel *, void *);
-static int pbx_builtin_resetcdr(struct ast_channel *, void *);
-static int pbx_builtin_setaccount(struct ast_channel *, void *);
-static int pbx_builtin_setamaflags(struct ast_channel *, void *);
-static int pbx_builtin_ringing(struct ast_channel *, void *);
-static int pbx_builtin_progress(struct ast_channel *, void *);
-static int pbx_builtin_congestion(struct ast_channel *, void *);
-static int pbx_builtin_busy(struct ast_channel *, void *);
-static int pbx_builtin_setglobalvar(struct ast_channel *, void *);
-static int pbx_builtin_noop(struct ast_channel *, void *);
-static int pbx_builtin_gotoif(struct ast_channel *, void *);
-static int pbx_builtin_gotoiftime(struct ast_channel *, void *);
-static int pbx_builtin_saynumber(struct ast_channel *, void *);
-static int pbx_builtin_saydigits(struct ast_channel *, void *);
-static int pbx_builtin_saycharacters(struct ast_channel *, void *);
-static int pbx_builtin_sayphonetic(struct ast_channel *, void *);
-int pbx_builtin_setvar(struct ast_channel *, void *);
-static int pbx_builtin_importvar(struct ast_channel *, void *);
+
 void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
 char *pbx_builtin_getvar_helper(struct ast_channel *chan, char *name);
 
@@ -4662,38 +4633,38 @@
 	} while(f);
 }
 
-static int pbx_builtin_progress(struct ast_channel *chan, void *data)
+int pbx_builtin_progress(struct ast_channel *chan, void *data)
 {
 	ast_indicate(chan, AST_CONTROL_PROGRESS);
 	return 0;
 }
 
-static int pbx_builtin_ringing(struct ast_channel *chan, void *data)
+int pbx_builtin_ringing(struct ast_channel *chan, void *data)
 {
 	ast_indicate(chan, AST_CONTROL_RINGING);
 	return 0;
 }
 
-static int pbx_builtin_busy(struct ast_channel *chan, void *data)
+int pbx_builtin_busy(struct ast_channel *chan, void *data)
 {
 	ast_indicate(chan, AST_CONTROL_BUSY);		
 	wait_for_hangup(chan, data);
 	return -1;
 }
 
-static int pbx_builtin_congestion(struct ast_channel *chan, void *data)
+int pbx_builtin_congestion(struct ast_channel *chan, void *data)
 {
 	ast_indicate(chan, AST_CONTROL_CONGESTION);
 	wait_for_hangup(chan, data);
 	return -1;
 }
 
-static int pbx_builtin_answer(struct ast_channel *chan, void *data)
+int pbx_builtin_answer(struct ast_channel *chan, void *data)
 {
 	return ast_answer(chan);
 }
 
-static int pbx_builtin_setlanguage(struct ast_channel *chan, void *data)
+int pbx_builtin_setlanguage(struct ast_channel *chan, void *data)
 {
 	/* Copy the language as specified */
 	if (data)
@@ -4701,7 +4672,7 @@
 	return 0;
 }
 
-static int pbx_builtin_resetcdr(struct ast_channel *chan, void *data)
+int pbx_builtin_resetcdr(struct ast_channel *chan, void *data)
 {
 	int flags = 0;
 	/* Reset the CDR as specified */
@@ -4716,7 +4687,7 @@
 	return 0;
 }
 
-static int pbx_builtin_setaccount(struct ast_channel *chan, void *data)
+int pbx_builtin_setaccount(struct ast_channel *chan, void *data)
 {
 	/* Copy the account code  as specified */
 	if (data)
@@ -4726,7 +4697,7 @@
 	return 0;
 }
 
-static int pbx_builtin_setamaflags(struct ast_channel *chan, void *data)
+int pbx_builtin_setamaflags(struct ast_channel *chan, void *data)
 {
 	/* Copy the AMA Flags as specified */
 	if (data)
@@ -4736,13 +4707,13 @@
 	return 0;
 }
 
-static int pbx_builtin_hangup(struct ast_channel *chan, void *data)
+int pbx_builtin_hangup(struct ast_channel *chan, void *data)
 {
 	/* Just return non-zero and it will hang up */
 	return -1;
 }
 
-static int pbx_builtin_stripmsd(struct ast_channel *chan, void *data)
+int pbx_builtin_stripmsd(struct ast_channel *chan, void *data)
 {
 	char newexten[AST_MAX_EXTENSION] = "";
 
@@ -4757,7 +4728,7 @@
 	return 0;
 }
 
-static int pbx_builtin_prefix(struct ast_channel *chan, void *data)
+int pbx_builtin_prefix(struct ast_channel *chan, void *data)
 {
 	char newexten[AST_MAX_EXTENSION] = "";
 
@@ -4772,7 +4743,7 @@
 	return 0;
 }
 
-static int pbx_builtin_suffix(struct ast_channel *chan, void *data)
+int pbx_builtin_suffix(struct ast_channel *chan, void *data)
 {
 	char newexten[AST_MAX_EXTENSION] = "";
 
@@ -4787,7 +4758,7 @@
 	return 0;
 }
 
-static int pbx_builtin_gotoiftime(struct ast_channel *chan, void *data)
+int pbx_builtin_gotoiftime(struct ast_channel *chan, void *data)
 {
 	int res=0;
 	char *s, *ts;
@@ -4811,7 +4782,7 @@
 	return res;
 }
 
-static int pbx_builtin_wait(struct ast_channel *chan, void *data)
+int pbx_builtin_wait(struct ast_channel *chan, void *data)
 {
 	int ms;
 
@@ -4823,7 +4794,7 @@
 	return 0;
 }
 
-static int pbx_builtin_waitexten(struct ast_channel *chan, void *data)
+int pbx_builtin_waitexten(struct ast_channel *chan, void *data)
 {
 	int ms;
 	int res;
@@ -4849,7 +4820,7 @@
 	return res;
 }
 
-static int pbx_builtin_background(struct ast_channel *chan, void *data)
+int pbx_builtin_background(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 	int option_skip = 0;
@@ -4904,7 +4875,7 @@
 	return res;
 }
 
-static int pbx_builtin_atimeout(struct ast_channel *chan, void *data)
+int pbx_builtin_atimeout(struct ast_channel *chan, void *data)
 {
 	int x = atoi((char *) data);
 
@@ -4915,7 +4886,7 @@
 	return 0;
 }
 
-static int pbx_builtin_rtimeout(struct ast_channel *chan, void *data)
+int pbx_builtin_rtimeout(struct ast_channel *chan, void *data)
 {
 	/* Set the timeout for how long to wait between digits */
 	chan->pbx->rtimeout = atoi((char *)data);
@@ -4924,7 +4895,7 @@
 	return 0;
 }
 
-static int pbx_builtin_dtimeout(struct ast_channel *chan, void *data)
+int pbx_builtin_dtimeout(struct ast_channel *chan, void *data)
 {
 	/* Set the timeout for how long to wait between digits */
 	chan->pbx->dtimeout = atoi((char *)data);
@@ -4933,7 +4904,29 @@
 	return 0;
 }
 
-static int pbx_builtin_goto(struct ast_channel *chan, void *data)
+int pbx_explicit_goto(struct ast_channel *chan, char* context, char *exten, int priority) 
+{
+	if(chan) {
+		if(ast_exists_extension(chan, 
+								context ? context : chan->context,
+								exten ? exten : chan->exten,
+								priority ? priority : chan->priority,
+								chan->cid.cid_num)) {
+			if(context)
+				strncpy(chan->context, context, sizeof(chan->context));
+			if(exten)
+				strncpy(chan->exten, exten, sizeof(chan->exten));
+			if(priority)
+				chan->priority = (priority - 1);
+			return 0;
+		}
+		return -1;
+	}
+
+	return -2;
+}
+
+int pbx_builtin_goto(struct ast_channel *chan, void *data)
 {
 	char *s;
 	char *exten, *pri, *context;
@@ -4979,14 +4972,19 @@
 			mode = 0;
 	} 
 	/* At this point we have a priority and maybe an extension and a context */
-	if (mode)
-		chan->priority += mode * ipri - 1;
-	else
-		chan->priority = ipri - 1;
-	if (exten && strcasecmp(exten, "BYEXTENSION"))
-		strncpy(chan->exten, exten, sizeof(chan->exten)-1);
-	if (context)
-		strncpy(chan->context, context, sizeof(chan->context)-1);
+
+	if (exten && !strcasecmp(exten, "BYEXTENSION"))
+		exten = NULL;
+
+	if (mode) {
+		ipri = chan->priority + (ipri * mode);
+	}
+
+	if (pbx_explicit_goto(chan, context, exten, ipri)) {
+		ast_log(LOG_WARNING, "Error: nonexistant extension %s encountered in context %s\n", exten, context);
+		return -1;
+	}
+
 	if (option_verbose > 2)
 		ast_verbose( VERBOSE_PREFIX_3 "Goto (%s,%s,%d)\n", chan->context,chan->exten, chan->priority+1);
 	ast_cdr_update(chan);
@@ -5132,7 +5130,7 @@
 	return(0);
 }
 
-static int pbx_builtin_setglobalvar(struct ast_channel *chan, void *data)
+int pbx_builtin_setglobalvar(struct ast_channel *chan, void *data)
 {
 	char *name;
 	char *value;
@@ -5152,7 +5150,7 @@
 	return(0);
 }
 
-static int pbx_builtin_noop(struct ast_channel *chan, void *data)
+int pbx_builtin_noop(struct ast_channel *chan, void *data)
 {
 	return 0;
 }
@@ -5173,7 +5171,7 @@
 	return condition ? atoi(condition) : 0;
 }
 
-static int pbx_builtin_gotoif(struct ast_channel *chan, void *data)
+int pbx_builtin_gotoif(struct ast_channel *chan, void *data)
 {
 	char *condition,*branch1,*branch2,*branch;
 	char *s;
@@ -5190,6 +5188,15 @@
 	condition=strsep(&stringp,"?");
 	branch1=strsep(&stringp,":");
 	branch2=strsep(&stringp,"");
+	if (!branch2 || ast_strlen_zero(branch2)) {
+		branch2 = alloca(32);
+		snprintf(branch2,32,"%d",chan->priority + 1);
+	}
+	if (!branch1 || ast_strlen_zero(branch1)) {
+		branch1 = alloca(32);
+		snprintf(branch1,32,"%d",chan->priority + 2);
+	}
+
 	branch = pbx_checkcondition(condition) ? branch1 : branch2;
 	
 	if ((branch==NULL) || ast_strlen_zero(branch)) {
@@ -5202,7 +5209,7 @@
 	return(rc);
 }           
 
-static int pbx_builtin_saynumber(struct ast_channel *chan, void *data)
+int pbx_builtin_saynumber(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 	char tmp[256];
@@ -5228,7 +5235,7 @@
 	return res = ast_say_number(chan, atoi((char *) tmp), "", chan->language, options);
 }
 
-static int pbx_builtin_saydigits(struct ast_channel *chan, void *data)
+int pbx_builtin_saydigits(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 
@@ -5237,7 +5244,7 @@
 	return res;
 }
 	
-static int pbx_builtin_saycharacters(struct ast_channel *chan, void *data)
+int pbx_builtin_saycharacters(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 
@@ -5246,7 +5253,7 @@
 	return res;
 }
 	
-static int pbx_builtin_sayphonetic(struct ast_channel *chan, void *data)
+int pbx_builtin_sayphonetic(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 
Index: include/asterisk/pbx.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/pbx.h,v
retrieving revision 1.33
diff -u -r1.33 pbx.h
--- include/asterisk/pbx.h	13 Nov 2004 22:44:33 -0000	1.33
+++ include/asterisk/pbx.h	22 Nov 2004 20:52:42 -0000
@@ -572,6 +572,37 @@
 extern void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
 extern void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count);
 
+int pbx_explicit_goto(struct ast_channel *chan, char* context, char *exten, int priority);
+int pbx_builtin_prefix(struct ast_channel *, void *);
+int pbx_builtin_suffix(struct ast_channel *, void *);
+int pbx_builtin_stripmsd(struct ast_channel *, void *);
+int pbx_builtin_answer(struct ast_channel *, void *);
+int pbx_builtin_goto(struct ast_channel *, void *);
+int pbx_builtin_hangup(struct ast_channel *, void *);
+int pbx_builtin_background(struct ast_channel *, void *);
+int pbx_builtin_dtimeout(struct ast_channel *, void *);
+int pbx_builtin_rtimeout(struct ast_channel *, void *);
+int pbx_builtin_atimeout(struct ast_channel *, void *);
+int pbx_builtin_wait(struct ast_channel *, void *);
+int pbx_builtin_waitexten(struct ast_channel *, void *);
+int pbx_builtin_setlanguage(struct ast_channel *, void *);
+int pbx_builtin_resetcdr(struct ast_channel *, void *);
+int pbx_builtin_setaccount(struct ast_channel *, void *);
+int pbx_builtin_setamaflags(struct ast_channel *, void *);
+int pbx_builtin_ringing(struct ast_channel *, void *);
+int pbx_builtin_progress(struct ast_channel *, void *);
+int pbx_builtin_congestion(struct ast_channel *, void *);
+int pbx_builtin_busy(struct ast_channel *, void *);
+int pbx_builtin_setglobalvar(struct ast_channel *, void *);
+int pbx_builtin_noop(struct ast_channel *, void *);
+int pbx_builtin_gotoif(struct ast_channel *, void *);
+int pbx_builtin_gotoiftime(struct ast_channel *, void *);
+int pbx_builtin_saynumber(struct ast_channel *, void *);
+int pbx_builtin_saydigits(struct ast_channel *, void *);
+int pbx_builtin_saycharacters(struct ast_channel *, void *);
+int pbx_builtin_sayphonetic(struct ast_channel *, void *);
+int pbx_builtin_importvar(struct ast_channel *, void *);
+
 int ast_extension_patmatch(const char *pattern, const char *data);
 
 /* Set "autofallthrough" flag, if newval is <0, does not acutally set.  If
Index: apps/app_dial.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_dial.c,v
retrieving revision 1.107
diff -u -r1.107 app_dial.c
--- apps/app_dial.c	21 Nov 2004 20:38:32 -0000	1.107
+++ apps/app_dial.c	22 Nov 2004 20:52:43 -0000
@@ -68,7 +68,13 @@
 "             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"
+"                     -- CONGESTION - Behave as if line congestion was encountered.\n"
+"                     -- BUSY - Behave as if a busy signal was encountered. (n+101)\n"
+"                     -- CONTINUE - Hangup the called party and continue on in the dialplan.\n"
+"                     -- GOTO:<context>^<exten>^<priority> - Transfer the call.\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"
@@ -484,7 +490,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;
@@ -492,9 +498,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");
@@ -661,7 +670,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)
@@ -1001,8 +1010,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");
@@ -1012,6 +1019,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;
@@ -1023,6 +1033,70 @@
 			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, "BUSY")) {
+						strncpy(status, macro_result, sizeof(status) - 1);
+						if (!pbx_explicit_goto(chan, NULL, NULL, chan->priority + 101)) {
+							go_on = 1;
+						}
+						res = -1;
+					}
+					else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
+						strncpy(status, macro_result, sizeof(status) - 1);
+						go_on = 1;
+						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.
+						*/
+						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))) {
+						res = -1;
+						/* perform a transfer to a new extension */
+						if(strchr(macro_transfer_exten,'^')) { /* context^exten^priority*/
+							/* no brainer mode... substitute ^ with | and feed it to builtin goto */
+							for(res=0;res<strlen(macro_transfer_exten);res++)
+								if(macro_transfer_exten[res] == '^')
+									macro_transfer_exten[res] = '|';
+
+							if(!pbx_builtin_goto(chan, macro_transfer_exten))
+								go_on = 1;
+							
+						} else {
+							/* Support anthm's proposed general syntax */
+							/* 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 (!pbx_explicit_goto(chan, macro_transfer_context, macro_transfer_exten, mpri)) 
+								go_on = 1;
+						}
+					}
+				}
 			}
 		}
 
