[asterisk-commits] branch file/bug5998 - r7480 /team/file/bug5998/apps/app_directed_pickup.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Dec 14 19:55:04 CST 2005


Author: file
Date: Wed Dec 14 19:55:03 2005
New Revision: 7480

URL: http://svn.digium.com/view/asterisk?rev=7480&view=rev
Log:
Commit new directed pickup with multiple pickup capability and more error checking to prevent segfaults/deadlocks

Modified:
    team/file/bug5998/apps/app_directed_pickup.c

Modified: team/file/bug5998/apps/app_directed_pickup.c
URL: http://svn.digium.com/view/asterisk/team/file/bug5998/apps/app_directed_pickup.c?rev=7480&r1=7479&r2=7480&view=diff
==============================================================================
--- team/file/bug5998/apps/app_directed_pickup.c (original)
+++ team/file/bug5998/apps/app_directed_pickup.c Wed Dec 14 19:55:03 2005
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2005, Joshua Colp
  *
- * Joshua Colp <jcolp at asterlink.com>
+ * Joshua Colp <Joshua.Colp at file-radio.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -52,12 +52,38 @@
 
 LOCAL_USER_DECL;
 
+static int perform_pickup(struct ast_channel *chan, struct ast_channel *target)
+{
+	int res = 0;
+
+	ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
+		chan->name);
+	res = ast_answer(chan);
+	if (res) {
+		ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
+		return -1;
+	}
+	res = ast_queue_control(chan, AST_CONTROL_ANSWER);
+	if (res) {
+		ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
+			chan->name);
+		return -1;
+	}
+	res = ast_channel_masquerade(target, chan);
+	if (res) {
+		ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
+		return -1;
+	}
+
+	return 0;
+}
+
 static int pickup_exec(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 	struct localuser *u = NULL;
 	struct ast_channel *origin = NULL, *target = NULL;
-	char *tmp = NULL, *exten = NULL, *context = NULL;
+	char *tmp = NULL, *exten = NULL, *context = NULL, *current_pickup = NULL, *next_pickup = NULL;
 	char workspace[256] = "";
 
 	if (ast_strlen_zero(data)) {
@@ -67,64 +93,50 @@
 
 	LOCAL_USER_ADD(u);
 	
-	/* Get the extension and context if present */
-	exten = data;
-	context = strchr(data, '@');
-	if (context) {
-		*context = '\0';
-		context++;
+	/* Start off at the beginning */
+	current_pickup = ast_strdupa(data);
+	while (current_pickup != NULL) {
+		next_pickup = strchr(current_pickup, '&');
+		if (next_pickup != NULL) {
+			*next_pickup = '\0';
+			next_pickup++;
+		}
+		/* Now parse out the arguments */
+		exten = current_pickup;
+		context = strchr(current_pickup, '@');
+		if (context != NULL) {
+			*context = '\0';
+			context++;
+		}
+		/* Attempt to do the pickup */
+		origin = ast_get_channel_by_exten_locked(exten, context);
+		if (origin != NULL) {
+			/* Only go for an attempt if we have a CDR record, if not just unlock the origin channel */
+			if (origin->cdr != NULL) {
+				ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace, sizeof(workspace), 0);
+				if (tmp != NULL) {
+					target = ast_get_channel_by_name_locked(tmp);
+				}
+			}
+			ast_mutex_unlock(&origin->lock);
+		}
+		/* Only actually do a pickup if certain conditions are met */
+		if (target != NULL) {
+			if ((!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING))) {
+				/* All is well - do the pickup */
+				res = perform_pickup(chan, target);
+				if (res == 0) {
+					/* All went fine! */
+					ast_mutex_unlock(&target->lock);
+					break;
+				}
+				res = 0;
+			}
+			ast_mutex_unlock(&target->lock);
+		}
+		/* Move on to the next one if applicable */
+		current_pickup = next_pickup;
 	}
-
-	/* Find a channel to pickup */
-	origin = ast_get_channel_by_exten_locked(exten, context);
-	if (origin) {
-		ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
-			       sizeof(workspace), 0);
-		if (tmp) {
-			/* We have a possible channel... now we need to find it! */
-			target = ast_get_channel_by_name_locked(tmp);
-		} else {
-			ast_log(LOG_DEBUG, "No target channel found.\n");
-			res = -1;
-		}
-		ast_mutex_unlock(&origin->lock);
-	} else {
-		ast_log(LOG_DEBUG, "No originating channel found.\n");
-	}
-	
-	if (res)
-		goto out;
-
-	if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING))) {
-		ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
-			chan->name);
-		res = ast_answer(chan);
-		if (res) {
-			ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
-			res = -1;
-			goto out;
-		}
-		res = ast_queue_control(chan, AST_CONTROL_ANSWER);
-		if (res) {
-			ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
-				chan->name);
-			res = -1;
-			goto out;
-		}
-		res = ast_channel_masquerade(target, chan);
-		if (res) {
-			ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
-			res = -1;
-			goto out;
-		}
-	} else {
-		ast_log(LOG_DEBUG, "No call pickup possible...\n");
-		res = -1;
-	}
-	/* Done */
- out:
-	if (target) 
-		ast_mutex_unlock(&target->lock);
 	
 	LOCAL_USER_REMOVE(u);
 



More information about the asterisk-commits mailing list