From 767f019264968e4ba4d46ee7fd08b623e452438e Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Tue, 21 Jun 2005 01:16:18 +0000
Subject: [PATCH] add AGI 'RECEIVE TEXT' command (bug #4525)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5950 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channel.c                  | 34 ++++++++++++++++++++++++++++++++++
 include/asterisk/channel.h |  9 +++++++++
 res/res_agi.c              | 26 +++++++++++++++++++++++++-
 3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/channel.c b/channel.c
index b04017b0b0..620f8cf97b 100755
--- a/channel.c
+++ b/channel.c
@@ -1652,6 +1652,40 @@ int ast_recvchar(struct ast_channel *chan, int timeout)
 	}
 }
 
+char *ast_recvtext(struct ast_channel *chan, int timeout)
+{
+	int res,ourto;
+	struct ast_frame *f;
+	char *buf;
+	
+	ourto = timeout;
+	for(;;) {
+		if (ast_check_hangup(chan)) return NULL;
+		res = ast_waitfor(chan,ourto);
+		if (res <= 0) { 
+			/* if timeout */
+			return NULL;
+		}
+		ourto = res;
+		f = ast_read(chan);
+		if (f == NULL) return NULL; /* no frame */
+		if ((f->frametype == AST_FRAME_CONTROL) && 
+			(f->subclass == AST_CONTROL_HANGUP)) return NULL; /* if hangup */
+		if (f->frametype == AST_FRAME_TEXT) {
+			/* if a text frame */
+			buf = (char *)malloc(strlen((char *)f->data));
+			if (buf) {
+				strcpy(buf, (char *)f->data);
+				ast_frfree(f);
+				return(buf);
+		   	} else {
+				return NULL;
+			}
+		}
+		ast_frfree(f);
+	}
+}
+
 int ast_sendtext(struct ast_channel *chan, char *text)
 {
 	int res = 0;
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index a98442f768..0ed9d00cc4 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -737,6 +737,15 @@ int ast_senddigit(struct ast_channel *chan, char digit);
 
 int ast_recvchar(struct ast_channel *chan, int timeout);
 
+/*! Receives a text string from a channel */
+/*! 
+ * \param chan channel to act upon
+ * \param timeout timeout in milliseconds (0 for infinite wait)
+ * \return the received text, or NULL to signify failure.
+ * Read a string of text from a channel
+ */
+char *ast_recvtext(struct ast_channel *chan, int timeout);
+
 /*! Browse channels in use */
 /*! 
  * \param prev where you want to start in the channel list
diff --git a/res/res_agi.c b/res/res_agi.c
index 275ccf6d55..9ae23dca3a 100755
--- a/res/res_agi.c
+++ b/res/res_agi.c
@@ -403,6 +403,23 @@ static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, char *a
 	}
 }
 
+static int handle_recvtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
+{
+	char *buf;
+	
+	if (argc != 3)
+		return RESULT_SHOWUSAGE;
+	buf = ast_recvtext(chan,atoi(argv[2]));
+	if (buf) {
+		fdprintf(agi->fd, "200 result=1 (%s)\n", buf);
+		free(buf);
+		return RESULT_SUCCESS;
+	} else {	
+		fdprintf(agi->fd, "200 result=-1\n");
+		return RESULT_FAILURE;
+	}
+}
+
 static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
 {
 	int res,x;
@@ -1358,6 +1375,12 @@ static char usage_recvchar[] =
 " if one is received, or 0 if the channel does not support text reception.  Returns\n"
 " -1 only on error/hangup.\n";
 
+static char usage_recvtext[] =
+" Usage: RECEIVE CHAR <timeout>\n"
+"	Receives a string of text on a channel. Specify timeout to be the\n"
+" maximum time to wait for input in milliseconds, or 0 for infinite. Most channels\n"
+" do not support the reception of text. Returns -1 for failure or 1 for success, and the string in parentheses.\n";
+
 static char usage_tddmode[] =
 " Usage: TDD MODE <on|off>\n"
 "	Enable/Disable TDD transmission/reception on a channel. Returns 1 if\n"
@@ -1493,7 +1516,8 @@ static agi_command commands[MAX_COMMANDS] = {
 	{ { "get", "variable", NULL }, handle_getvariable, "Gets a channel variable", usage_getvariable },
 	{ { "hangup", NULL }, handle_hangup, "Hangup the current channel", usage_hangup },
 	{ { "noop", NULL }, handle_noop, "Does nothing", usage_noop },
-	{ { "receive", "char", NULL }, handle_recvchar, "Receives text from channels supporting it", usage_recvchar },
+	{ { "receive", "char", NULL }, handle_recvchar, "Receives one character from channels supporting it", usage_recvchar },
+	{ { "receive", "text", NULL }, handle_recvtext, "Receives text from channels supporting it", usage_recvtext },
 	{ { "record", "file", NULL }, handle_recordfile, "Records to a given file", usage_recordfile },
 	{ { "say", "alpha", NULL }, handle_sayalpha, "Says a given character string", usage_sayalpha },
 	{ { "say", "digits", NULL }, handle_saydigits, "Says a given digit string", usage_saydigits },
-- 
GitLab