From bd5b6cea68ba4f484c70455ef6363556976a3d3a Mon Sep 17 00:00:00 2001
From: "Dwayne M. Hubbard" <dwayne.hubbard@gmail.com>
Date: Thu, 27 Sep 2007 23:18:09 +0000
Subject: [PATCH] Merged revisions 84018 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r84018 | dhubbard | 2007-09-27 18:12:25 -0500 (Thu, 27 Sep 2007) | 1 line

if an Agent is redirected, the base channel should actually be redirected.  This was causing multiple issues, especially issue 7706 and BE-160
........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@84019 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_agent.c      | 35 +++++++++++++++++++++++++++++++++++
 include/asterisk/channel.h |  6 ++++++
 main/manager.c             | 18 +++++++++++++++++-
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/channels/chan_agent.c b/channels/chan_agent.c
index 8ca9d76271..438134fbb4 100644
--- a/channels/chan_agent.c
+++ b/channels/chan_agent.c
@@ -231,6 +231,8 @@ static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
 static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
 static void set_agentbycallerid(const char *callerid, const char *agent);
 static char *complete_agent_logoff_cmd(const char *line, const char *word, int pos, int state);
+static struct ast_channel* agent_get_base_channel(struct ast_channel *chan);
+static int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base);
 
 /*! \brief Channel interface description for PBX integration */
 static const struct ast_channel_tech agent_tech = {
@@ -253,6 +255,8 @@ static const struct ast_channel_tech agent_tech = {
 	.indicate = agent_indicate,
 	.fixup = agent_fixup,
 	.bridged_channel = agent_bridgedchannel,
+	.get_base_channel = agent_get_base_channel,
+	.set_base_channel = agent_set_base_channel,
 };
 
 /*!
@@ -698,6 +702,37 @@ static void set_agentbycallerid(const char *callerid, const char *agent)
 	pbx_builtin_setvar_helper(NULL, buf, agent);
 }
 
+struct ast_channel* agent_get_base_channel(struct ast_channel *chan)
+{
+	struct agent_pvt *p = NULL;
+	struct ast_channel *base = NULL;
+	
+	if (!chan || !chan->tech_pvt) {
+		ast_log(LOG_ERROR, "whoa, you need a channel (0x%ld) with a tech_pvt (0x%ld) to get a base channel.\n", (long)chan, (chan)?(long)chan->tech_pvt:(long)NULL);
+	} else {
+		p = chan->tech_pvt;
+		base = p->chan;
+	}
+	return base;
+}
+
+int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base)
+{
+	struct agent_pvt *p = NULL;
+	
+	if (!chan || !base) {
+		ast_log(LOG_ERROR, "whoa, you need a channel (0x%ld) and a base channel (0x%ld) for setting.\n", (long)chan, (long)base);
+		return -1;
+	}
+	p = chan->tech_pvt;
+	if (!p) {
+		ast_log(LOG_ERROR, "whoa, channel %s is missing his tech_pvt structure!!.\n", chan->name);
+		return -1;
+	}
+	p->chan = base;
+	return 0;
+}
+
 static int agent_hangup(struct ast_channel *ast)
 {
 	struct agent_pvt *p = ast->tech_pvt;
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 95465dfd96..8e556d092d 100644
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -314,6 +314,12 @@ struct ast_channel_tech {
 
 	/*! \brief Provide additional write items for CHANNEL() dialplan function */
 	int (* func_channel_write)(struct ast_channel *chan, const char *function, char *data, const char *value);
+
+	/*! \brief Retrieve base channel (agent and local) */
+	struct ast_channel* (* get_base_channel)(struct ast_channel *chan);
+	
+	/*! \brief Set base channel (agent and local) */
+	int (* set_base_channel)(struct ast_channel *chan, struct ast_channel *base);
 };
 
 struct ast_epoll_data;
diff --git a/main/manager.c b/main/manager.c
index 83e9c826d1..ff628242dc 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -1787,7 +1787,7 @@ static int action_redirect(struct mansession *s, const struct message *m)
 	const char *exten = astman_get_header(m, "Exten");
 	const char *context = astman_get_header(m, "Context");
 	const char *priority = astman_get_header(m, "Priority");
-	struct ast_channel *chan, *chan2 = NULL;
+	struct ast_channel *base, *chan, *chan2 = NULL;
 	int pi = 0;
 	int res;
 
@@ -1809,6 +1809,14 @@ static int action_redirect(struct mansession *s, const struct message *m)
 		astman_send_error(s, m, buf);
 		return 0;
 	}
+	if (chan->tech->get_base_channel) {
+		base = chan->tech->get_base_channel(chan);
+		if (base) {
+			ast_mutex_unlock(&chan->lock);
+			chan = base;
+			ast_mutex_lock(&chan->lock);
+		}
+	}
 	if (ast_check_hangup(chan)) {
 		astman_send_error(s, m, "Redirect failed, channel not up.\n");
 		ast_channel_unlock(chan);
@@ -1822,6 +1830,14 @@ static int action_redirect(struct mansession *s, const struct message *m)
 		ast_channel_unlock(chan2);
 		return 0;
 	}
+	if (chan2 && chan2->tech->get_base_channel) {
+		base = chan2->tech->get_base_channel(chan2);
+		if (base) {
+			ast_mutex_unlock(&chan2->lock);
+			chan2 = base;
+			ast_mutex_lock(&chan2->lock);
+		}
+	}
 	res = ast_async_goto(chan, context, exten, pi);
 	if (!res) {
 		if (!ast_strlen_zero(name2)) {
-- 
GitLab