From 51cab9e7cc4f901c5e18558e5fa64d70057ed5b5 Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Sat, 8 Jan 2005 17:23:29 +0000
Subject: [PATCH] Make queue support channel variable inheritance (bug #3274)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@4709 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_dial.c            | 41 ++----------------------------------
 apps/app_queue.c           |  5 +++++
 channel.c                  | 43 ++++++++++++++++++++++++++++++++++++++
 include/asterisk/channel.h | 14 +++++++++++++
 4 files changed, 64 insertions(+), 39 deletions(-)

diff --git a/apps/app_dial.c b/apps/app_dial.c
index 05f2dcfee6..6604ca18aa 100755
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -488,9 +488,6 @@ static int dial_exec(struct ast_channel *chan, void *data)
 	char *newnum;
 	char *l;
 	char *url=NULL; /* JDG */
-	struct ast_var_t *current;
-	struct varshead *headp, *newheadp;
-	struct ast_var_t *newvar;
 	unsigned int calldurationlimit=0;
 	char *cdl;
 	time_t now;
@@ -511,9 +508,7 @@ static int dial_exec(struct ast_channel *chan, void *data)
 	char toast[80];
 	int play_to_caller=0,play_to_callee=0;
 	int playargs=0, sentringing=0, moh=0;
-	char *varname;
 	char *mohclass = NULL;
-	int vartype;
 	char *outbound_group = NULL;
 	char *macro_result = NULL, *macro_transfer_dest = NULL;
 	int digit = 0;
@@ -853,40 +848,8 @@ static int dial_exec(struct ast_channel *chan, void *data)
 			}
 		}
 
-		/* Contitionally copy channel variables to the newly created channel */
-		headp = &chan->varshead;
-		AST_LIST_TRAVERSE(headp, current, entries) {
-			varname = ast_var_full_name(current);
-			vartype = 0;
-			if (varname) {
-				if (varname[0] == '_') {
-					vartype = 1;
-					if (varname[1] == '_')
-						vartype = 2;
-				}
-			}
-			if (vartype == 1) {
-				newvar = ast_var_assign((char*)&(varname[1]), 
-												ast_var_value(current));
-				newheadp = &tmp->chan->varshead;
-				AST_LIST_INSERT_HEAD(newheadp, newvar, entries);
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Copying soft-transferable variable %s.\n", 
-												ast_var_name(newvar));
-			} else if (vartype == 2) {
-				newvar = ast_var_assign(ast_var_full_name(current), 
-												ast_var_value(current));
-				newheadp = &tmp->chan->varshead;
-				AST_LIST_INSERT_HEAD(newheadp, newvar, entries);
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Copying hard-transferable variable %s.\n", 
-												ast_var_name(newvar));
-			} else {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Not copying variable %s.\n", 
-												ast_var_name(current));
-			}
-		}
+		/* Inherit specially named variables from parent channel */
+		ast_channel_inherit_variables(chan, tmp->chan);
 
 		tmp->chan->appl = "AppDial";
 		tmp->chan->data = "(Outgoing Line)";
diff --git a/apps/app_queue.c b/apps/app_queue.c
index 3ff353454a..b78f947e02 100755
--- a/apps/app_queue.c
+++ b/apps/app_queue.c
@@ -823,8 +823,13 @@ static int ring_entry(struct queue_ent *qe, struct localuser *tmp)
 		tmp->chan->cid.cid_name = strdup(qe->chan->cid.cid_name);
 	if (qe->chan->cid.cid_ani)
 		tmp->chan->cid.cid_ani = strdup(qe->chan->cid.cid_ani);
+
+	/* Inherit specially named variables from parent channel */
+	ast_channel_inherit_variables(qe->chan, tmp->chan);
+
 	/* Presense of ADSI CPE on outgoing channel follows ours */
 	tmp->chan->adsicpe = qe->chan->adsicpe;
+
 	/* Place the call, but don't wait on the answer */
 	res = ast_call(tmp->chan, location, 0);
 	if (res) {
diff --git a/channel.c b/channel.c
index efd6486a17..bc54a8bc57 100755
--- a/channel.c
+++ b/channel.c
@@ -2243,6 +2243,49 @@ void ast_change_name(struct ast_channel *chan, char *newname)
 	manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
 }
 
+void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
+{
+	struct ast_var_t *current, *newvar;
+	char *varname;
+
+	AST_LIST_TRAVERSE(&parent->varshead, current, entries) {
+		int vartype = 0;
+
+		varname = ast_var_full_name(current);
+		if (!varname)
+			continue;
+
+		if (varname[0] == '_') {
+			vartype = 1;
+			if (varname[1] == '_')
+				vartype = 2;
+		}
+
+		switch (vartype) {
+		case 1:
+			newvar = ast_var_assign(&varname[1], ast_var_value(current));
+			if (newvar) {
+				AST_LIST_INSERT_HEAD(&child->varshead, newvar, entries);
+				if (option_debug)
+					ast_log(LOG_DEBUG, "Copying soft-transferable variable %s.\n", ast_var_name(newvar));
+			}
+			break;
+		case 2:
+			newvar = ast_var_assign(ast_var_full_name(current), ast_var_value(current));
+			if (newvar) {
+				AST_LIST_INSERT_HEAD(&child->varshead, newvar, entries);
+				if (option_debug)
+					ast_log(LOG_DEBUG, "Copying hard-transferable variable %s.\n", ast_var_name(newvar));
+			}
+			break;
+		default:
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Not copying variable %s.\n", ast_var_name(current));
+			break;
+		}
+	}
+}
+
 /* Clone channel variables from 'clone' channel into 'original' channel
    All variables except those related to app_groupcount are cloned
    Variables are actually _removed_ from 'clone' channel, presumably
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 908ff55851..765e7ff4df 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -782,6 +782,20 @@ int ast_do_masquerade(struct ast_channel *chan);
 /* Find bridged channel */
 struct ast_channel *ast_bridged_channel(struct ast_channel *chan);
 
+/*!
+  \brief Inherits channel variable from parent to child channel
+  \param parent Parent channel
+  \param child Child channel
+
+  Scans all channel variables in the parent channel, looking for those
+  that should be copied into the child channel.
+  Variables whose names begin with a single '_' are copied into the
+  child channel with the prefix removed.
+  Variables whose names begin with '__' are copied into the child
+  channel with their names unchanged.
+*/
+void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child);
+
 /* Misc. functions below */
 
 /* Helper function for migrating select to poll */
-- 
GitLab