--- asterisk-1.2.14/apps/app_queue.c	2006-10-03 21:14:13.000000000 +0100
+++ ../asterisk-1.2.14/apps/app_queue.c	2007-01-21 02:44:55.000000000 +0000
@@ -230,6 +237,9 @@
 /*! \brief queues.conf per-queue weight option */
 static int use_weight = 0;
 
+/*! \brief queues.conf [general] option */
+static int autofill_default = 0;
+
 enum queue_result {
 	QUEUE_UNKNOWN = 0,
 	QUEUE_TIMEOUT = 1,
@@ -365,6 +376,8 @@
 	/* Queue strategy things */
 	int rrpos;			/*!< Round Robin - position */
 	int memberdelay;		/*!< Seconds to delay connecting member to caller */
+	int autofill;			/*!< Ignore the head call status and ring an available agent */
+
 
 	struct member *members;		/*!< Head of the list of members */
 	struct queue_ent *head;		/*!< Head of the list of callers */
@@ -625,6 +639,7 @@
 	ast_copy_string(q->sound_lessthan, "queue-less-than", sizeof(q->sound_lessthan));
 	ast_copy_string(q->sound_reporthold, "queue-reporthold", sizeof(q->sound_reporthold));
 	ast_copy_string(q->sound_periodicannounce, "queue-periodic-announce", sizeof(q->sound_periodicannounce));
+	q->autofill = autofill_default;
 }
 
 static void clear_queue(struct call_queue *q)
@@ -766,6 +783,8 @@
 		ast_copy_string(q->sound_thanks, val, sizeof(q->sound_thanks));
 	} else if (!strcasecmp(param, "queue-reporthold")) {
 		ast_copy_string(q->sound_reporthold, val, sizeof(q->sound_reporthold));
+	} else if (!strcasecmp(param, "autofill")) {
+		q->autofill = ast_true(val);
 	} else if (!strcasecmp(param, "announce-frequency")) {
 		q->announcefrequency = atoi(val);
 	} else if (!strcasecmp(param, "announce-round-seconds")) {
@@ -2010,20 +2037,67 @@
 static int is_our_turn(struct queue_ent *qe)
 {
 	struct queue_ent *ch;
+	struct member *cur;
+	int avl = 0;
+	int idx = 0;
 	int res;
 
-	/* Atomically read the parent head -- does not need a lock */
-	ch = qe->parent->head;
-	/* If we are now at the top of the head, break out */
-	if (ch == qe) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
-		res = 1;
+	if (!qe->parent->autofill) {
+		/* Atomically read the parent head -- does not need a lock */
+		ch = qe->parent->head;
+		/* If we are now at the top of the head, break out */
+		if (ch == qe) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
+			res = 1;
+		} else {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
+			res = 0;
+		}
 	} else {
+		/* This needs a lock. How many members are available to be served? */
+		ast_mutex_lock(&qe->parent->lock);
+
+		ch = qe->parent->head;
+
+		if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Even though there are %d available members, the strategy is ringall so only the head call is allowed in\n", avl);
+			avl = 1;
+		} else {
+			for (cur = qe->parent->members; cur; cur = cur->next) {
+				switch (cur->status) {
+				case AST_DEVICE_NOT_INUSE:
+				case AST_DEVICE_UNKNOWN:
+					avl++;
+					break;
+				}
+			}
+		}
+
 		if (option_debug)
-			ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
-		res = 0;
+			ast_log(LOG_DEBUG, "There are %d available members.\n", avl);
+
+		while ((idx < avl) && (ch) && (ch != qe)) {
+			idx++;
+			ch = ch->next;
+		}
+
+		/* If the queue entry is within avl [the number of available members] calls from the top ... */
+		if (ch && idx <= avl) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
+			res = 1;
+		} else {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
+			res = 0;
+		}
+
+		ast_mutex_unlock(&qe->parent->lock);
 	}
+
 	return res;
 }
 
