From 0cc02feae975d4f0ff54cee6e3fb042c9d137a87 Mon Sep 17 00:00:00 2001
From: Joshua Colp <jcolp@digium.com>
Date: Mon, 22 May 2006 16:08:23 +0000
Subject: [PATCH] Add PICKUPMARK support to app_directed_pickup (issue #7104
 reported by thaeger)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@29463 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_directed_pickup.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/apps/app_directed_pickup.c b/apps/app_directed_pickup.c
index e75e34a265..dcb792443f 100644
--- a/apps/app_directed_pickup.c
+++ b/apps/app_directed_pickup.c
@@ -20,7 +20,7 @@
  *
  * \brief Directed Call Pickup Support
  *
- * \author Joshua Colp <jcolp@asterlink.com>
+ * \author Joshua Colp <jcolp@digium.com>
  *
  * \ingroup applications
  */
@@ -42,12 +42,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/lock.h"
 #include "asterisk/app.h"
 
+#define PICKUPMARK "PICKUPMARK"
+
 static const char *app = "Pickup";
 static const char *synopsis = "Directed Call Pickup";
 static const char *descrip =
 "  Pickup(extension[@context][&extension2@context...]): This application can pickup any ringing channel\n"
 "that is calling the specified extension. If no context is specified, the current\n"
-"context will be used.\n";
+"context will be used. If you use the special string \"PICKUPMARK\" for the context parameter, for example\n"
+"10@PICKUPMARK, this application tries to find a channel which has defined a channel variable with the same context\n"
+"as \"extension\".";
 
 LOCAL_USER_DECL;
 
@@ -58,6 +62,7 @@ static int pickup_exec(struct ast_channel *chan, void *data)
 	struct ast_channel *origin = NULL, *target = NULL;
 	char *tmp = NULL, *exten = NULL, *context = NULL, *rest=data;
 	char workspace[256] = "";
+	const char *tmp2 = NULL;
 
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "Pickup requires an argument (extension) !\n");
@@ -77,8 +82,21 @@ static int pickup_exec(struct ast_channel *chan, void *data)
 		if (context)
 			*context++ = '\0';
 
-		/* Find a channel to pickup */
-		origin = ast_get_channel_by_exten_locked(exten, context);
+		/* If the context is the pickup mark, iterate through all channels finding the right origin one */
+		if (!strcmp(context, PICKUPMARK)) {
+			while ((origin = ast_channel_walk_locked(origin))) {
+				if (origin) {
+					tmp2 = pbx_builtin_getvar_helper(origin, PICKUPMARK);
+					if (tmp2 && !strcmp(tmp2, exten))
+						break;
+					ast_mutex_unlock(&origin->lock);
+				}
+			}
+		} else {
+			/* Use the classic mode of searching */
+			origin = ast_get_channel_by_exten_locked(exten, context);
+		}
+
 		if (origin) {
 			ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
 					sizeof(workspace), 0, 0);
-- 
GitLab