Index: res/res_features.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_features.c,v
retrieving revision 1.43
diff -u -u -r1.43 res_features.c
--- res/res_features.c	4 Mar 2005 06:47:24 -0000	1.43
+++ res/res_features.c	7 Mar 2005 19:00:11 -0000
@@ -17,6 +17,7 @@
 #include <asterisk/channel.h>
 #include <asterisk/pbx.h>
 #include <asterisk/options.h>
+#include <asterisk/causes.h>
 #include <asterisk/module.h>
 #include <asterisk/translate.h>
 #include <asterisk/app.h>
@@ -143,6 +144,9 @@
 	struct ast_channel *peer;
 };
 
+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;
@@ -635,7 +639,7 @@
 		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);
@@ -730,17 +734,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);
@@ -850,6 +854,154 @@
 				ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
 		}
 	}
+}
+
+
+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;
+	int cause = 0;
+	struct ast_channel *chan;
+	struct ast_frame *f;
+	int res = 0, ready = 0;
+	if ((chan = ast_request(type, format, data, &cause))) {
+		ast_set_callerid(chan, cid_num, cid_name, cid_num);
+		if((res = ast_indicate(caller, AST_CONTROL_RINGING))) {
+			/* nm they hung up*/
+			ast_hangup(chan);
+			return NULL;
+		}
+		
+		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);
+			while (!ast_check_hangup(caller) && timeout && (chan->_state != AST_STATE_UP)) {
+				if (disconnect_code) {
+					/* see if the caller dialed any digits while he's waiting for somebody to answer */
+					res = ast_waitfordigit(caller, 100);
+					if (res < 0) {
+						break; /*doh caller is dead*/
+					}
+					if (res) { /* res is a digit !*/
+						dialed_code[x++] = res;
+						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;		
+							break;
+						}
+					}
+				}
+
+				/* give chan 100 ms to have something to say if it's quiet still, loop around to see if caller is still alive*/
+				if((res = ast_waitfor(chan, 100)) < 0) {
+					break; /*doh chan is dead*/
+				}
+
+				/* 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;		
+					break; /*doh! timeout*/
+				}
+				if(!res) { /* chan has nothing to say still ... */
+					continue;
+				}
+				/* else chan is ready to rock and roll */
+				f = ast_read(chan);
+				if (f == NULL) { /*doh! where'd he go?*/
+					state = AST_CONTROL_HANGUP;
+					res = 0;
+					break;
+				}
+				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;
+					} 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 = f->subclass;
+						ast_frfree(f);
+						ready=1;
+						break;
+					} 
+					/* else who cares */
+				}
+				ast_frfree(f);
+			}
+			if(ast_indicate(caller, -1)) {
+				ready = 0;
+			}
+
+		} 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;
+		}
+	}
+	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)
+				ast_cdr_init(chan->cdr, chan);
+		}
+		if (chan->cdr) {
+			char tmp[256];
+			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");
+		ast_hangup(chan);
+		chan = NULL;
+	}
+	return chan;
 }
 
 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
