From 9dba32410fc7a458469ecf56ed25b21050df649e Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Mon, 6 Feb 2017 16:37:59 -0600
Subject: [PATCH] FS-10015: [freeswitch-core] Add variable to allow firing of
 text events #resolve

---
 libs/esl/src/esl_event.c         |  2 +-
 libs/esl/src/include/esl_event.h |  2 +-
 src/include/switch_types.h       |  3 ++-
 src/switch_channel.c             |  1 +
 src/switch_core_media.c          | 31 ++++++++++++++++++++++++++-----
 src/switch_event.c               |  2 +-
 src/switch_ivr_async.c           |  2 +-
 7 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/libs/esl/src/esl_event.c b/libs/esl/src/esl_event.c
index d097f74569..ae01575251 100644
--- a/libs/esl/src/esl_event.c
+++ b/libs/esl/src/esl_event.c
@@ -147,7 +147,7 @@ static const char *EVENT_NAMES[] = {
 	"CALL_SETUP_RESULT",
 	"CALL_DETAIL",
 	"DEVICE_STATE",
-	"REAL_TIME_TEXT",
+	"TEXT",
 	"ALL"
 };
 
diff --git a/libs/esl/src/include/esl_event.h b/libs/esl/src/include/esl_event.h
index 1380f68092..765d0e6150 100644
--- a/libs/esl/src/include/esl_event.h
+++ b/libs/esl/src/include/esl_event.h
@@ -137,7 +137,7 @@ typedef enum {
 	ESL_EVENT_CALL_SETUP_RESULT,
 	ESL_EVENT_CALL_DETAIL,
 	ESL_EVENT_DEVICE_STATE,
-	ESL_EVENT_REAL_TIME_TEXT,
+	ESL_EVENT_TEXT,
 	ESL_EVENT_ALL
 } esl_event_types_t;
 
diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index 0bcb120284..b2b28e91d8 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -1537,6 +1537,7 @@ typedef enum {
 	CF_TEXT_IDLE,
 	CF_TEXT_LINE_BASED,
 	CF_QUEUE_TEXT_EVENTS,
+	CF_FIRE_TEXT_EVENTS,
 	CF_MSRP,
 	CF_MSRPS,
 	CF_WANT_MSRP,
@@ -2058,7 +2059,7 @@ typedef enum {
 	SWITCH_EVENT_CALL_SETUP_RESULT,
 	SWITCH_EVENT_CALL_DETAIL,
 	SWITCH_EVENT_DEVICE_STATE,
-	SWITCH_EVENT_REAL_TIME_TEXT,
+	SWITCH_EVENT_TEXT,
 	SWITCH_EVENT_ALL
 } switch_event_types_t;
 
diff --git a/src/switch_channel.c b/src/switch_channel.c
index 9302751a79..1fa9fcfa50 100644
--- a/src/switch_channel.c
+++ b/src/switch_channel.c
@@ -2644,6 +2644,7 @@ SWITCH_DECLARE(void) switch_channel_event_set_extended_data(switch_channel_t *ch
 		event->event_id == SWITCH_EVENT_MEDIA_BUG_STOP || 
 		event->event_id == SWITCH_EVENT_CHANNEL_HOLD || 
 		event->event_id == SWITCH_EVENT_CHANNEL_UNHOLD || 
+		event->event_id == SWITCH_EVENT_TEXT || 
 		event->event_id == SWITCH_EVENT_CUSTOM) {
 
 		/* Index Variables */
diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index 6287427f71..ca5fce75a7 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -6461,6 +6461,10 @@ static void *SWITCH_THREAD_FUNC text_helper_thread(switch_thread_t *thread, void
 		return NULL;
 	}
 
+	if (switch_channel_var_true(session->channel, "fire_text_events")) {
+		switch_channel_set_flag(session->channel, CF_FIRE_TEXT_EVENTS);
+	}
+
 	cr_frame.data = CR;
 	cr_frame.datalen = 3;
 	
@@ -14206,9 +14210,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_text_frame(switch_core_
 			switch_core_media_bug_prune(session);
 		}
 	}
-
+	
 	if (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK) {		
-		if (switch_channel_test_flag(session->channel, CF_QUEUE_TEXT_EVENTS) && (*frame)->datalen && !switch_test_flag((*frame), SFF_CNG)) {
+		if ((switch_channel_test_flag(session->channel, CF_QUEUE_TEXT_EVENTS) || switch_channel_test_flag(session->channel, CF_FIRE_TEXT_EVENTS)) && 
+			(*frame)->datalen && !switch_test_flag((*frame), SFF_CNG)) {
 			int ok = 1;
 			switch_event_t *event;
 			void *data = (*frame)->data;
@@ -14234,11 +14239,27 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_text_frame(switch_core_
 
 
 			if (ok) {
-				if (switch_event_create(&event, SWITCH_EVENT_MESSAGE) == SWITCH_STATUS_SUCCESS) {
-					switch_channel_event_set_extended_data(session->channel, event);
+				if (switch_event_create(&event, SWITCH_EVENT_TEXT) == SWITCH_STATUS_SUCCESS) {
+					switch_channel_event_set_data(session->channel, event);
 
 					switch_event_add_body(event, "%s", (char *)data);
-					switch_core_session_queue_event(session, &event);
+
+					if (switch_channel_test_flag(session->channel, CF_QUEUE_TEXT_EVENTS)) {
+						switch_event_t *q_event = NULL;
+
+						if (switch_channel_test_flag(session->channel, CF_FIRE_TEXT_EVENTS)) {
+							switch_event_dup(&q_event, event);
+						} else {
+							q_event = event;
+							event = NULL;
+						}
+
+						switch_core_session_queue_event(session, &q_event);
+					}
+					
+					if (switch_channel_test_flag(session->channel, CF_FIRE_TEXT_EVENTS)) {
+						switch_event_fire(&event);
+					}
 				}
 				if (session->text_line_buffer) {
 					switch_buffer_zero(session->text_line_buffer);
diff --git a/src/switch_event.c b/src/switch_event.c
index 138621a758..6f99952d79 100644
--- a/src/switch_event.c
+++ b/src/switch_event.c
@@ -219,7 +219,7 @@ static char *EVENT_NAMES[] = {
 	"CALL_SETUP_RESULT",
 	"CALL_DETAIL",
 	"DEVICE_STATE",
-	"REAL_TIME_TEXT",
+	"TEXT",
 	"ALL"
 };
 
diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c
index 6b8e4d933d..ceacdf88dc 100644
--- a/src/switch_ivr_async.c
+++ b/src/switch_ivr_async.c
@@ -1543,7 +1543,7 @@ static switch_bool_t text_callback(switch_media_bug_t *bug, void *user_data, swi
 				switch_core_session_t *session = switch_core_media_bug_get_session(bug);
 				//switch_channel_t *channel = switch_core_session_get_channel(session);
 
-				if (switch_event_create(&event, SWITCH_EVENT_REAL_TIME_TEXT) == SWITCH_STATUS_SUCCESS) {
+				if (switch_event_create(&event, SWITCH_EVENT_TEXT) == SWITCH_STATUS_SUCCESS) {
 					switch_event_add_body(event, text, SWITCH_VA_NONE);
 					
 					if (switch_true(switch_core_get_variable("fire_text_events"))) {