Index: res/res_features.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_features.c,v
retrieving revision 1.54
diff -u -u -r1.54 res_features.c
--- res/res_features.c	16 May 2005 00:43:16 -0000	1.54
+++ res/res_features.c	24 May 2005 18:12:17 -0000
@@ -28,6 +28,7 @@
 #include "asterisk/manager.h"
 #include "asterisk/utils.h"
 #include "asterisk/adsi.h"
+#include "asterisk/causes.h"
 #include <pthread.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -191,6 +192,9 @@
 	}
 }
 
+static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *caller, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name);
+
+
 static void *ast_bridge_call_thread(void *data) 
 {
 	struct ast_bridge_thread_obj *tobj = data;
@@ -692,7 +696,15 @@
 		cid_name = transferer->cid.cid_name;
 		if (ast_exists_extension(transferer, transferer_real_context,xferto, 1, cid_num)) {
 			snprintf(dialstr, sizeof(dialstr), "%s@%s/n", xferto, transferer_real_context);
-			if ((newchan = ast_request_and_dial("Local", ast_best_codec(transferer->nativeformats), dialstr,30000, &outstate, cid_num, cid_name))) {
+			if ( (newchan = ast_feature_request_and_dial(transferer, 
+														 "Local",
+														 ast_best_codec(transferer->nativeformats),
+														 dialstr,
+														 30000,
+														 &outstate,
+														 cid_num,
+														 cid_name))) {
+
 				res = ast_channel_make_compatible(transferer, newchan);
 				if (res < 0) {
 					ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", transferer->name, newchan->name);
@@ -702,7 +714,8 @@
 				memset(&bconfig,0,sizeof(struct ast_bridge_config));
 				ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
 				ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
-				res = ast_bridge_call(transferer,newchan,&bconfig);
+				res = ast_bridge_call(transferer, newchan, &bconfig);
+				
 				if (newchan->_softhangup || newchan->_state != AST_STATE_UP) {
 					ast_hangup(newchan);
 					if (f) {
@@ -787,17 +800,17 @@
 				return -1;
 				
 			} else {
-				ast_log(LOG_WARNING, "Unable to create channel Local/%s do you have chan_local?\n",dialstr);
 				ast_moh_stop(transferee);
 				ast_autoservice_stop(transferee);
 				ast_indicate(transferee, AST_CONTROL_UNHOLD);
-				if (!ast_strlen_zero(xferfailsound)) {
+				/* any reason besides user requested cancel and busy triggers the failed sound */
+				if (outstate != AST_CONTROL_UNHOLD && outstate != AST_CONTROL_BUSY && !ast_strlen_zero(xferfailsound)) {
 					res = ast_streamfile(transferer, xferfailsound, transferer->language);
 					if (!res && (ast_waitstream(transferer, "") < 0)) {
 						return -1;
 					}
 				}
-				return -1;
+				return FEATURE_RETURN_SUCCESS;
 			}
 		} else {
 			ast_log(LOG_WARNING, "Extension %s does not exist in context %s\n",xferto,transferer_real_context);
@@ -909,6 +922,156 @@
 	}
 }
 
+
+static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *caller, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name)
+{
+	int state = 0, cause = 0, to = 0, res = 0, ready = 0;
+	struct ast_channel *chan, *active_channel, *monitor_channels[2];
+	struct ast_frame *f = NULL;
+
+	if ((chan = ast_request(type, format, data, &cause))) {
+		ast_set_callerid(chan, cid_num, cid_name, cid_num);
+		if (!ast_call(chan, data, 0)) {
+			struct timeval started, ended;
+			int elapsed,x,len=0;
+			char *disconnect_code = NULL, *dialed_code = NULL;
+
+			/* support dialing of the featuremap disconnect code while performing an attended tranfer */
+			for (x=0; x<FEATURES_COUNT; x++) {
+				if (!strcasecmp(builtin_features[x].sname, "disconnect")) {
+					disconnect_code = builtin_features[x].exten;
+					len = strlen(disconnect_code) + 1;
+					dialed_code = alloca(len);
+					memset(dialed_code, 0, len);
+				}
+			}
+			x = 0;
+			gettimeofday(&started, NULL);
+			to = timeout;
+
+			while (!ast_check_hangup(caller) && timeout && (chan->_state != AST_STATE_UP)) {
+				monitor_channels[0] = caller;
+				monitor_channels[1] = chan;
+				res = 100;
+				active_channel = ast_waitfor_n(monitor_channels, 2, &res);
+				
+				/* see if the timeout has been violated */
+				gettimeofday(&ended,NULL);
+				elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000));
+				if(elapsed > timeout) {
+					state = AST_CONTROL_UNHOLD;
+					ast_log(LOG_NOTICE, "We exceeded our AT-timeout\n");
+					break; /*doh! timeout*/
+				}
+
+				if(chan && (chan == active_channel)){
+					if(!(f = ast_read(chan))) {
+						state = AST_CONTROL_HANGUP;
+						res = 0;
+						break;
+					} else {
+						if (f->frametype == AST_FRAME_CONTROL || f->frametype == AST_FRAME_DTMF || f->frametype == AST_FRAME_TEXT) {
+							if (f->subclass == AST_CONTROL_RINGING) {
+								state = f->subclass;
+								if (option_verbose > 2)
+									ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", chan->name);
+								
+								ast_indicate(caller, AST_CONTROL_RINGING);
+							} else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
+								state = f->subclass;
+								ast_frfree(f);
+								break;
+							} else if (f->subclass == AST_CONTROL_ANSWER) {
+								/* This is what we are hoping for */
+								state = AST_CONTROL_ANSWER;
+								ast_frfree(f);
+								ready = 1;
+								break;
+							} else {
+								ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass);
+							}
+						}
+						ast_frfree(f);
+					}
+						
+				} else if (caller && (active_channel == caller)){
+					if(!(f = ast_read(caller))) {
+						state = AST_CONTROL_HANGUP;
+						res = 0;
+						break;
+					} else {
+						if (f->frametype == AST_FRAME_DTMF){	
+							dialed_code[x++] = f->subclass;
+							dialed_code[x] = '\0';
+							if (strlen(dialed_code) == len) {
+								x = 0;
+							}
+							if (!strcmp(dialed_code, disconnect_code)) {
+								/* Caller Canceled the call */
+								state = AST_CONTROL_UNHOLD;
+								ast_frfree(f);
+								break;
+							}
+						}
+						ast_frfree(f);
+					}
+				}
+			}
+		} else {
+			ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
+		}
+	} else {
+		ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
+		switch(cause) {
+		case AST_CAUSE_BUSY:
+			state = AST_CONTROL_BUSY;
+			break;
+		case AST_CAUSE_CONGESTION:
+			state = AST_CONTROL_CONGESTION;
+			break;
+		}
+	}
+	
+	ast_indicate(caller, AST_CONTROL_UNHOLD);
+	if (chan && ready) {
+		if (chan->_state == AST_STATE_UP) 
+			state = AST_CONTROL_ANSWER;
+		res = 0;
+	} else if(chan) {
+		res = -1;
+		ast_hangup(chan);
+		chan = NULL;
+	} else {
+		res = -1;
+	}
+	
+	if (outstate)
+		*outstate = state;
+
+	if (chan && res <= 0) {
+		if (!chan->cdr) {
+			chan->cdr = ast_cdr_alloc();
+		}
+		if (chan->cdr) {
+			char tmp[256];
+			ast_cdr_init(chan->cdr, chan);
+			snprintf(tmp, 256, "%s/%s", type, (char *)data);
+			ast_cdr_setapp(chan->cdr,"Dial",tmp);
+			ast_cdr_update(chan);
+			ast_cdr_start(chan->cdr);
+			ast_cdr_end(chan->cdr);
+			/* If the cause wasn't handled properly */
+			if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
+				ast_cdr_failed(chan->cdr);
+		} else {
+			ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
+		}
+	}
+	
+	return chan;
+}
+
+
 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
 {
 	/* Copy voice back and forth between the two channels.  Give the peer
@@ -1406,7 +1569,7 @@
 		}
  
 		ast_moh_stop(peer);
-		ast_indicate(peer, AST_CONTROL_UNHOLD);
+		ast_indicate(peer, -1);
 		res = ast_channel_make_compatible(chan, peer);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
