[Freeswitch-svn] [commit] r10380 - in freeswitch/trunk/src: . include

FreeSWITCH SVN anthm at freeswitch.org
Thu Nov 13 12:23:59 PST 2008


Author: anthm
Date: Thu Nov 13 15:23:59 2008
New Revision: 10380

Log:
rewrite switch_play_and_get_digits MODAPP-166

Modified:
   freeswitch/trunk/src/include/switch_types.h
   freeswitch/trunk/src/switch_ivr_play_say.c

Modified: freeswitch/trunk/src/include/switch_types.h
==============================================================================
--- freeswitch/trunk/src/include/switch_types.h	(original)
+++ freeswitch/trunk/src/include/switch_types.h	Thu Nov 13 15:23:59 2008
@@ -626,6 +626,7 @@
 	SWITCH_STATUS_UNLOAD,
 	SWITCH_STATUS_NOUNLOAD,
 	SWITCH_STATUS_IGNORE,
+	SWITCH_STATUS_TOO_SMALL,
 	SWITCH_STATUS_NOT_INITALIZED
 } switch_status_t;
 

Modified: freeswitch/trunk/src/switch_ivr_play_say.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_play_say.c	(original)
+++ freeswitch/trunk/src/switch_ivr_play_say.c	Thu Nov 13 15:23:59 2008
@@ -1380,7 +1380,10 @@
 												uint32_t max_digits,
 												const char *prompt_audio_file,
 												const char *var_name,
-												char *digit_buffer, switch_size_t digit_buffer_length, uint32_t timeout, const char *valid_terminators)
+												char *digit_buffer, 
+												switch_size_t digit_buffer_length, 
+												uint32_t timeout, 
+												const char *valid_terminators)
 {
 	switch_channel_t *channel;
 	switch_input_args_t args = { 0 };
@@ -1421,6 +1424,11 @@
 		status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &terminator, timeout, 0, 0);
 	}
 
+	len = strlen(digit_buffer);
+	if ((min_digits && len < min_digits)) {
+		status = SWITCH_STATUS_TOO_SMALL;
+	}
+
 	if (status == SWITCH_STATUS_SUCCESS) {
 		switch_channel_set_variable(channel, SWITCH_READ_RESULT_VARIABLE, "success");
 	} else if (status == SWITCH_STATUS_TIMEOUT) {
@@ -1440,7 +1448,6 @@
 
 }
 
-
 SWITCH_DECLARE(switch_status_t) switch_play_and_get_digits(switch_core_session_t *session,
 														   uint32_t min_digits,
 														   uint32_t max_digits,
@@ -1448,114 +1455,42 @@
 														   uint32_t timeout,
 														   char *valid_terminators,
 														   char *prompt_audio_file,
-														   char *bad_input_audio_file, void *digit_buffer, uint32_t digit_buffer_length,
+														   char *bad_input_audio_file, 
+														   void *digit_buffer, 
+														   uint32_t digit_buffer_length,
 														   char *digits_regex)
 {
+	switch_channel_t *channel = switch_core_session_get_channel(session);
 
-	char terminator;			                                          /* used to hold terminator recieved from */
-	switch_channel_t *channel = switch_core_session_get_channel(session); /* the channel contained in session */
-	switch_status_t status;		                                          /* used to recieve state out of called functions */
-
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
-					  "switch_play_and_get_digits(session, %d, %d, %d, %d, %s, %s, %s, digit_buffer, %d, %s)\n",
-					  min_digits, max_digits, max_tries, timeout, valid_terminators, prompt_audio_file,
-					  bad_input_audio_file, digit_buffer_length, digits_regex);
-
-	/* Answer the channel if it hasn't already been answered */
-	switch_channel_pre_answer(channel);
-
-	/* Start pestering the user for input */
-	for (; switch_channel_ready(channel) && max_tries > 0; max_tries--) {
-		switch_input_args_t args = { 0 };
-		/* make the buffer so fresh and so clean clean */
+	while(switch_channel_ready(channel) && max_tries) {
+		switch_status_t status;
+		
 		memset(digit_buffer, 0, digit_buffer_length);
-
-		args.buf = digit_buffer;
-		args.buflen = digit_buffer_length;
-		/* Play the file */
-		status = switch_ivr_play_file(session, NULL, prompt_audio_file, &args);
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "play gave up %s\n", (char *) digit_buffer);
-
-		/* Make sure we made it out alive */
-		if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
-			goto done;
-		}
-		/* we only get one digit out of playback, see if thats all we needed and what we got */
-		if (max_digits == 1 && status == SWITCH_STATUS_BREAK) {
-			/* Check the digit if we have a regex */
-			if (digits_regex != NULL) {
-				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking regex [%s] on [%s]\n", digits_regex, (char *) digit_buffer);
-
-				/* Make sure the digit is allowed */
-				if (switch_regex_match(digit_buffer, digits_regex) == SWITCH_STATUS_SUCCESS) {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Match found!\n");
-					/* jobs done */
-					break;
-				} else {
-					/* See if a bad input prompt was specified, if so, play it */
-					if (strlen(bad_input_audio_file) > 0) {
-						status = switch_ivr_play_file(session, NULL, bad_input_audio_file, NULL);
-
-						/* Make sure we made it out alive */
-						if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
-							goto done;
-						}
-					}
+		switch_channel_flush_dtmf(channel);
+		status = switch_ivr_read(session, min_digits, max_digits, prompt_audio_file, NULL, 
+												 digit_buffer, digit_buffer_length, timeout, valid_terminators);
+		if (status == SWITCH_STATUS_SUCCESS) {
+			if (!switch_strlen_zero((char *)digit_buffer)) {
+				if (switch_strlen_zero(digits_regex)) {
+					return SWITCH_STATUS_SUCCESS;
 				}
-			} else {
-				/* jobs done */
-				break;
-			}
-		}
-
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Calling more digits try %d\n", max_tries);
-
-		/* Try to grab some more digits for the timeout period */
-		status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &terminator, timeout, 0, 0);
-		if (status == SWITCH_STATUS_TIMEOUT) {
-			status = SWITCH_STATUS_SUCCESS;
-		}
-		
-		/* Make sure we made it out alive */
-		if (status != SWITCH_STATUS_SUCCESS) {
-			/* Bail */
-			goto done;
-		}
-		/* see if we got enough */
-		if (min_digits <= strlen(digit_buffer)) {
-			/* See if we need to test a regex */
-			if (digits_regex != NULL) {
-				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking regex [%s] on [%s]\n", digits_regex, (char *) digit_buffer);
-				/* Test the regex */
 				if (switch_regex_match(digit_buffer, digits_regex) == SWITCH_STATUS_SUCCESS) {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Match found!\n");
-					/* Jobs done */
 					return SWITCH_STATUS_SUCCESS;
-				} else {
-					/* See if a bad input prompt was specified, if so, play it */
-					if (strlen(bad_input_audio_file) > 0) {
-						status = switch_ivr_play_file(session, NULL, bad_input_audio_file, NULL);
-
-						/* Make sure we made it out alive */
-						if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
-							goto done;
-						}
-					}
 				}
-			} else {
-				/* Jobs done */
-				return SWITCH_STATUS_SUCCESS;
 			}
 		}
+
+		if (!switch_channel_ready(channel)) {
+			break;
+		}
+
+		switch_ivr_play_file(session, NULL, bad_input_audio_file, NULL);
+		max_tries--;
 	}
 
-  done:
-	/* if we got here, we got no digits or lost the channel */
-	digit_buffer = "\0";
 	return SWITCH_STATUS_FALSE;
 }
 
-
 SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text_handle(switch_core_session_t *session,
 															 switch_speech_handle_t *sh,
 															 switch_codec_t *codec, switch_timer_t *timer, char *text, switch_input_args_t *args)



More information about the Freeswitch-svn mailing list