2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief True call queues with optional send URL on answer
23 * \author Mark Spencer <markster@digium.com>
25 * \arg Config in \ref Config_qu queues.conf
27 * \par Development notes
28 * \note 2004-11-25: Persistent Dynamic Members added by:
29 * NetNation Communications (www.netnation.com)
30 * Kevin Lindsay <kevinl@netnation.com>
32 * Each dynamic agent in each queue is now stored in the astdb.
33 * When asterisk is restarted, each agent will be automatically
34 * readded into their recorded queues. This feature can be
35 * configured with the 'persistent_members=<1|0>' setting in the
36 * '[general]' category in queues.conf. The default is on.
38 * \note 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
40 * \note These features added by David C. Troy <dave@toad.net>:
41 * - Per-queue holdtime calculation
42 * - Estimated holdtime announcement
43 * - Position announcement
44 * - Abandoned/completed call counters
45 * - Failout timer passed as optional app parameter
46 * - Optional monitoring of calls, started when call is answered
48 * Patch Version 1.07 2003-12-24 01
50 * Added servicelevel statistic by Michiel Betel <michiel@betel.nl>
51 * Added Priority jumping code for adding and removing queue members by Jonathan Stanton <asterisk@doilooklikeicare.com>
53 * Fixed to work with CVS as of 2004-02-25 and released as 1.07a
54 * by Matthew Enger <m.enger@xi.com.au>
56 * \ingroup applications
60 <use type="module">res_monitor</use>
61 <support_level>core</support_level>
66 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
69 #include <sys/signal.h>
70 #include <netinet/in.h>
73 #include "asterisk/lock.h"
74 #include "asterisk/file.h"
75 #include "asterisk/channel.h"
76 #include "asterisk/pbx.h"
77 #include "asterisk/app.h"
78 #include "asterisk/linkedlists.h"
79 #include "asterisk/module.h"
80 #include "asterisk/translate.h"
81 #include "asterisk/say.h"
82 #include "asterisk/features.h"
83 #include "asterisk/musiconhold.h"
84 #include "asterisk/cli.h"
85 #include "asterisk/manager.h"
86 #include "asterisk/config.h"
87 #include "asterisk/monitor.h"
88 #include "asterisk/utils.h"
89 #include "asterisk/causes.h"
90 #include "asterisk/astdb.h"
91 #include "asterisk/devicestate.h"
92 #include "asterisk/stringfields.h"
93 #include "asterisk/event.h"
94 #include "asterisk/astobj2.h"
95 #include "asterisk/strings.h"
96 #include "asterisk/global_datastores.h"
97 #include "asterisk/taskprocessor.h"
98 #include "asterisk/aoc.h"
99 #include "asterisk/callerid.h"
100 #include "asterisk/cel.h"
101 #include "asterisk/data.h"
103 /* Define, to debug reference counts on queues, without debugging reference counts on queue members */
104 /* #define REF_DEBUG_ONLY_QUEUES */
107 * \par Please read before modifying this file.
108 * There are three locks which are regularly used
109 * throughout this file, the queue list lock, the lock
110 * for each individual queue, and the interface list lock.
111 * Please be extra careful to always lock in the following order
113 * 2) individual queue lock
114 * 3) interface list lock
115 * This order has sort of "evolved" over the lifetime of this
116 * application, but it is now in place this way, so please adhere
121 <application name="Queue" language="en_US">
123 Queue a call for a call queue.
126 <parameter name="queuename" required="true" />
127 <parameter name="options">
130 <para>Mark all calls as "answered elsewhere" when cancelled.</para>
133 <para>Continue in the dialplan if the callee hangs up.</para>
136 <para>data-quality (modem) call (minimum delay).</para>
138 <option name="F" argsep="^">
139 <argument name="context" required="false" />
140 <argument name="exten" required="false" />
141 <argument name="priority" required="true" />
142 <para>When the caller hangs up, transfer the <emphasis>called member</emphasis>
143 to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
145 <para>Any channel variables you want the called channel to inherit from the caller channel must be
146 prefixed with one or two underbars ('_').</para>
150 <para>When the caller hangs up, transfer the <emphasis>called member</emphasis> to the next priority of
151 the current extension and <emphasis>start</emphasis> execution at that location.</para>
153 <para>Any channel variables you want the called channel to inherit from the caller channel must be
154 prefixed with one or two underbars ('_').</para>
157 <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
161 <para>Allow <emphasis>callee</emphasis> to hang up by pressing <literal>*</literal>.</para>
164 <para>Allow <emphasis>caller</emphasis> to hang up by pressing <literal>*</literal>.</para>
167 <para>No retries on the timeout; will exit this application and
168 go to the next step.</para>
171 <para>Ignore call forward requests from queue members and do nothing
172 when they are requested.</para>
175 <para>Asterisk will ignore any connected line update requests or any redirecting party
176 update requests it may receive on this dial attempt.</para>
179 <para>Ring instead of playing MOH. Periodic Announcements are still made, if applicable.</para>
182 <para>Ring instead of playing MOH when a member channel is actually ringing.</para>
185 <para>Allow the <emphasis>called</emphasis> user to transfer the calling user.</para>
188 <para>Allow the <emphasis>calling</emphasis> user to transfer the call.</para>
191 <para>Allow the <emphasis>called</emphasis> user to write the conversation to
192 disk via Monitor.</para>
195 <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
196 disk via Monitor.</para>
199 <para>Allow the <emphasis>called</emphasis> party to enable parking of the call by sending
200 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
203 <para>Allow the <emphasis>calling</emphasis> party to enable parking of the call by sending
204 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
207 <para>Allow the <emphasis>called</emphasis> user to write the conversation
208 to disk via MixMonitor.</para>
211 <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
212 disk via MixMonitor.</para>
216 <parameter name="URL">
217 <para><replaceable>URL</replaceable> will be sent to the called party if the channel supports it.</para>
219 <parameter name="announceoverride" />
220 <parameter name="timeout">
221 <para>Will cause the queue to fail out after a specified number of
222 seconds, checked between each <filename>queues.conf</filename> <replaceable>timeout</replaceable> and
223 <replaceable>retry</replaceable> cycle.</para>
225 <parameter name="AGI">
226 <para>Will setup an AGI script to be executed on the calling party's channel once they are
227 connected to a queue member.</para>
229 <parameter name="macro">
230 <para>Will run a macro on the calling party's channel once they are connected to a queue member.</para>
232 <parameter name="gosub">
233 <para>Will run a gosub on the calling party's channel once they are connected to a queue member.</para>
235 <parameter name="rule">
236 <para>Will cause the queue's defaultrule to be overridden by the rule specified.</para>
238 <parameter name="position">
239 <para>Attempt to enter the caller into the queue at the numerical position specified. <literal>1</literal>
240 would attempt to enter the caller at the head of the queue, and <literal>3</literal> would attempt to place
241 the caller third in the queue.</para>
245 <para>In addition to transferring the call, a call may be parked and then picked
246 up by another user.</para>
247 <para>This application will return to the dialplan if the queue does not exist, or
248 any of the join options cause the caller to not enter the queue.</para>
249 <para>This application does not automatically answer and should be preceeded
250 by an application such as Answer(), Progress(), or Ringing().</para>
251 <para>This application sets the following channel variable upon completion:</para>
253 <variable name="QUEUESTATUS">
254 <para>The status of the call as a text string.</para>
255 <value name="TIMEOUT" />
256 <value name="FULL" />
257 <value name="JOINEMPTY" />
258 <value name="LEAVEEMPTY" />
259 <value name="JOINUNAVAIL" />
260 <value name="LEAVEUNAVAIL" />
261 <value name="CONTINUE" />
266 <ref type="application">Queue</ref>
267 <ref type="application">QueueLog</ref>
268 <ref type="application">AddQueueMember</ref>
269 <ref type="application">RemoveQueueMember</ref>
270 <ref type="application">PauseQueueMember</ref>
271 <ref type="application">UnpauseQueueMember</ref>
272 <ref type="function">QUEUE_VARIABLES</ref>
273 <ref type="function">QUEUE_MEMBER</ref>
274 <ref type="function">QUEUE_MEMBER_COUNT</ref>
275 <ref type="function">QUEUE_EXISTS</ref>
276 <ref type="function">QUEUE_WAITING_COUNT</ref>
277 <ref type="function">QUEUE_MEMBER_LIST</ref>
278 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
281 <application name="AddQueueMember" language="en_US">
283 Dynamically adds queue members.
286 <parameter name="queuename" required="true" />
287 <parameter name="interface" />
288 <parameter name="penalty" />
289 <parameter name="options" />
290 <parameter name="membername" />
291 <parameter name="stateinterface" />
294 <para>Dynamically adds interface to an existing queue. If the interface is
295 already in the queue it will return an error.</para>
296 <para>This application sets the following channel variable upon completion:</para>
298 <variable name="AQMSTATUS">
299 <para>The status of the attempt to add a queue member as a text string.</para>
300 <value name="ADDED" />
301 <value name="MEMBERALREADY" />
302 <value name="NOSUCHQUEUE" />
307 <ref type="application">Queue</ref>
308 <ref type="application">QueueLog</ref>
309 <ref type="application">AddQueueMember</ref>
310 <ref type="application">RemoveQueueMember</ref>
311 <ref type="application">PauseQueueMember</ref>
312 <ref type="application">UnpauseQueueMember</ref>
313 <ref type="function">QUEUE_VARIABLES</ref>
314 <ref type="function">QUEUE_MEMBER</ref>
315 <ref type="function">QUEUE_MEMBER_COUNT</ref>
316 <ref type="function">QUEUE_EXISTS</ref>
317 <ref type="function">QUEUE_WAITING_COUNT</ref>
318 <ref type="function">QUEUE_MEMBER_LIST</ref>
319 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
322 <application name="RemoveQueueMember" language="en_US">
324 Dynamically removes queue members.
327 <parameter name="queuename" required="true" />
328 <parameter name="interface" />
331 <para>If the interface is <emphasis>NOT</emphasis> in the queue it will return an error.</para>
332 <para>This application sets the following channel variable upon completion:</para>
334 <variable name="RQMSTATUS">
335 <value name="REMOVED" />
336 <value name="NOTINQUEUE" />
337 <value name="NOSUCHQUEUE" />
338 <value name="NOTDYNAMIC" />
341 <para>Example: RemoveQueueMember(techsupport,SIP/3000)</para>
344 <ref type="application">Queue</ref>
345 <ref type="application">QueueLog</ref>
346 <ref type="application">AddQueueMember</ref>
347 <ref type="application">RemoveQueueMember</ref>
348 <ref type="application">PauseQueueMember</ref>
349 <ref type="application">UnpauseQueueMember</ref>
350 <ref type="function">QUEUE_VARIABLES</ref>
351 <ref type="function">QUEUE_MEMBER</ref>
352 <ref type="function">QUEUE_MEMBER_COUNT</ref>
353 <ref type="function">QUEUE_EXISTS</ref>
354 <ref type="function">QUEUE_WAITING_COUNT</ref>
355 <ref type="function">QUEUE_MEMBER_LIST</ref>
356 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
359 <application name="PauseQueueMember" language="en_US">
361 Pauses a queue member.
364 <parameter name="queuename" />
365 <parameter name="interface" required="true" />
366 <parameter name="options" />
367 <parameter name="reason">
368 <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
372 <para>Pauses (blocks calls for) a queue member. The given interface will be paused in the given queue.
373 This prevents any calls from being sent from the queue to the interface until it is
374 unpaused with UnpauseQueueMember or the manager interface. If no queuename is given,
375 the interface is paused in every queue it is a member of. The application will fail if the
376 interface is not found.</para>
377 <para>This application sets the following channel variable upon completion:</para>
379 <variable name="PQMSTATUS">
380 <para>The status of the attempt to pause a queue member as a text string.</para>
381 <value name="PAUSED" />
382 <value name="NOTFOUND" />
385 <para>Example: PauseQueueMember(,SIP/3000)</para>
388 <ref type="application">Queue</ref>
389 <ref type="application">QueueLog</ref>
390 <ref type="application">AddQueueMember</ref>
391 <ref type="application">RemoveQueueMember</ref>
392 <ref type="application">PauseQueueMember</ref>
393 <ref type="application">UnpauseQueueMember</ref>
394 <ref type="function">QUEUE_VARIABLES</ref>
395 <ref type="function">QUEUE_MEMBER</ref>
396 <ref type="function">QUEUE_MEMBER_COUNT</ref>
397 <ref type="function">QUEUE_EXISTS</ref>
398 <ref type="function">QUEUE_WAITING_COUNT</ref>
399 <ref type="function">QUEUE_MEMBER_LIST</ref>
400 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
403 <application name="UnpauseQueueMember" language="en_US">
405 Unpauses a queue member.
408 <parameter name="queuename" />
409 <parameter name="interface" required="true" />
410 <parameter name="options" />
411 <parameter name="reason">
412 <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
416 <para>Unpauses (resumes calls to) a queue member. This is the counterpart to <literal>PauseQueueMember()</literal>
417 and operates exactly the same way, except it unpauses instead of pausing the given interface.</para>
418 <para>This application sets the following channel variable upon completion:</para>
420 <variable name="UPQMSTATUS">
421 <para>The status of the attempt to unpause a queue member as a text string.</para>
422 <value name="UNPAUSED" />
423 <value name="NOTFOUND" />
426 <para>Example: UnpauseQueueMember(,SIP/3000)</para>
429 <ref type="application">Queue</ref>
430 <ref type="application">QueueLog</ref>
431 <ref type="application">AddQueueMember</ref>
432 <ref type="application">RemoveQueueMember</ref>
433 <ref type="application">PauseQueueMember</ref>
434 <ref type="application">UnpauseQueueMember</ref>
435 <ref type="function">QUEUE_VARIABLES</ref>
436 <ref type="function">QUEUE_MEMBER</ref>
437 <ref type="function">QUEUE_MEMBER_COUNT</ref>
438 <ref type="function">QUEUE_EXISTS</ref>
439 <ref type="function">QUEUE_WAITING_COUNT</ref>
440 <ref type="function">QUEUE_MEMBER_LIST</ref>
441 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
444 <application name="QueueLog" language="en_US">
446 Writes to the queue_log file.
449 <parameter name="queuename" required="true" />
450 <parameter name="uniqueid" required="true" />
451 <parameter name="agent" required="true" />
452 <parameter name="event" required="true" />
453 <parameter name="additionalinfo" />
456 <para>Allows you to write your own events into the queue log.</para>
457 <para>Example: QueueLog(101,${UNIQUEID},${AGENT},WENTONBREAK,600)</para>
460 <ref type="application">Queue</ref>
461 <ref type="application">QueueLog</ref>
462 <ref type="application">AddQueueMember</ref>
463 <ref type="application">RemoveQueueMember</ref>
464 <ref type="application">PauseQueueMember</ref>
465 <ref type="application">UnpauseQueueMember</ref>
466 <ref type="function">QUEUE_VARIABLES</ref>
467 <ref type="function">QUEUE_MEMBER</ref>
468 <ref type="function">QUEUE_MEMBER_COUNT</ref>
469 <ref type="function">QUEUE_EXISTS</ref>
470 <ref type="function">QUEUE_WAITING_COUNT</ref>
471 <ref type="function">QUEUE_MEMBER_LIST</ref>
472 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
475 <function name="QUEUE_VARIABLES" language="en_US">
477 Return Queue information in variables.
480 <parameter name="queuename" required="true">
482 <enum name="QUEUEMAX">
483 <para>Maxmimum number of calls allowed.</para>
485 <enum name="QUEUESTRATEGY">
486 <para>The strategy of the queue.</para>
488 <enum name="QUEUECALLS">
489 <para>Number of calls currently in the queue.</para>
491 <enum name="QUEUEHOLDTIME">
492 <para>Current average hold time.</para>
494 <enum name="QUEUECOMPLETED">
495 <para>Number of completed calls for the queue.</para>
497 <enum name="QUEUEABANDONED">
498 <para>Number of abandoned calls.</para>
500 <enum name="QUEUESRVLEVEL">
501 <para>Queue service level.</para>
503 <enum name="QUEUESRVLEVELPERF">
504 <para>Current service level performance.</para>
510 <para>Makes the following queue variables available.</para>
511 <para>Returns <literal>0</literal> if queue is found and setqueuevar is defined, <literal>-1</literal> otherwise.</para>
514 <ref type="application">Queue</ref>
515 <ref type="application">QueueLog</ref>
516 <ref type="application">AddQueueMember</ref>
517 <ref type="application">RemoveQueueMember</ref>
518 <ref type="application">PauseQueueMember</ref>
519 <ref type="application">UnpauseQueueMember</ref>
520 <ref type="function">QUEUE_VARIABLES</ref>
521 <ref type="function">QUEUE_MEMBER</ref>
522 <ref type="function">QUEUE_MEMBER_COUNT</ref>
523 <ref type="function">QUEUE_EXISTS</ref>
524 <ref type="function">QUEUE_WAITING_COUNT</ref>
525 <ref type="function">QUEUE_MEMBER_LIST</ref>
526 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
529 <function name="QUEUE_MEMBER" language="en_US">
531 Count number of members answering a queue.
534 <parameter name="queuename" required="true" />
535 <parameter name="option" required="true">
538 <para>Returns the number of logged-in members for the specified queue.</para>
541 <para>Returns the number of logged-in members for the specified queue that either can take calls or are currently wrapping up after a previous call.</para>
544 <para>Returns the number of logged-in members for the specified queue that are immediately available to answer a call.</para>
547 <para>Returns the total number of members for the specified queue.</para>
549 <enum name="penalty">
550 <para>Gets or sets queue member penalty.</para>
553 <para>Gets or sets queue member paused status.</para>
555 <enum name="ringinuse">
556 <para>Gets or sets queue member ringinuse.</para>
560 <parameter name="interface" required="false" />
563 <para>Allows access to queue counts [R] and member information [R/W].</para>
565 <replaceable>queuename</replaceable> is required for all operations
566 <replaceable>interface</replaceable> is required for all member operations.
570 <ref type="application">Queue</ref>
571 <ref type="application">QueueLog</ref>
572 <ref type="application">AddQueueMember</ref>
573 <ref type="application">RemoveQueueMember</ref>
574 <ref type="application">PauseQueueMember</ref>
575 <ref type="application">UnpauseQueueMember</ref>
576 <ref type="function">QUEUE_VARIABLES</ref>
577 <ref type="function">QUEUE_MEMBER</ref>
578 <ref type="function">QUEUE_MEMBER_COUNT</ref>
579 <ref type="function">QUEUE_EXISTS</ref>
580 <ref type="function">QUEUE_WAITING_COUNT</ref>
581 <ref type="function">QUEUE_MEMBER_LIST</ref>
582 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
585 <function name="QUEUE_MEMBER_COUNT" language="en_US">
587 Count number of members answering a queue.
590 <parameter name="queuename" required="true" />
593 <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
594 <warning><para>This function has been deprecated in favor of the <literal>QUEUE_MEMBER()</literal> function</para></warning>
597 <ref type="application">Queue</ref>
598 <ref type="application">QueueLog</ref>
599 <ref type="application">AddQueueMember</ref>
600 <ref type="application">RemoveQueueMember</ref>
601 <ref type="application">PauseQueueMember</ref>
602 <ref type="application">UnpauseQueueMember</ref>
603 <ref type="function">QUEUE_VARIABLES</ref>
604 <ref type="function">QUEUE_MEMBER</ref>
605 <ref type="function">QUEUE_MEMBER_COUNT</ref>
606 <ref type="function">QUEUE_EXISTS</ref>
607 <ref type="function">QUEUE_WAITING_COUNT</ref>
608 <ref type="function">QUEUE_MEMBER_LIST</ref>
609 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
612 <function name="QUEUE_EXISTS" language="en_US">
614 Check if a named queue exists on this server
617 <parameter name="queuename" />
620 <para>Returns 1 if the specified queue exists, 0 if it does not</para>
623 <ref type="application">Queue</ref>
624 <ref type="application">QueueLog</ref>
625 <ref type="application">AddQueueMember</ref>
626 <ref type="application">RemoveQueueMember</ref>
627 <ref type="application">PauseQueueMember</ref>
628 <ref type="application">UnpauseQueueMember</ref>
629 <ref type="function">QUEUE_VARIABLES</ref>
630 <ref type="function">QUEUE_MEMBER</ref>
631 <ref type="function">QUEUE_MEMBER_COUNT</ref>
632 <ref type="function">QUEUE_EXISTS</ref>
633 <ref type="function">QUEUE_WAITING_COUNT</ref>
634 <ref type="function">QUEUE_MEMBER_LIST</ref>
635 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
638 <function name="QUEUE_WAITING_COUNT" language="en_US">
640 Count number of calls currently waiting in a queue.
643 <parameter name="queuename" />
646 <para>Returns the number of callers currently waiting in the specified <replaceable>queuename</replaceable>.</para>
649 <ref type="application">Queue</ref>
650 <ref type="application">QueueLog</ref>
651 <ref type="application">AddQueueMember</ref>
652 <ref type="application">RemoveQueueMember</ref>
653 <ref type="application">PauseQueueMember</ref>
654 <ref type="application">UnpauseQueueMember</ref>
655 <ref type="function">QUEUE_VARIABLES</ref>
656 <ref type="function">QUEUE_MEMBER</ref>
657 <ref type="function">QUEUE_MEMBER_COUNT</ref>
658 <ref type="function">QUEUE_EXISTS</ref>
659 <ref type="function">QUEUE_WAITING_COUNT</ref>
660 <ref type="function">QUEUE_MEMBER_LIST</ref>
661 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
664 <function name="QUEUE_MEMBER_LIST" language="en_US">
666 Returns a list of interfaces on a queue.
669 <parameter name="queuename" required="true" />
672 <para>Returns a comma-separated list of members associated with the specified <replaceable>queuename</replaceable>.</para>
675 <ref type="application">Queue</ref>
676 <ref type="application">QueueLog</ref>
677 <ref type="application">AddQueueMember</ref>
678 <ref type="application">RemoveQueueMember</ref>
679 <ref type="application">PauseQueueMember</ref>
680 <ref type="application">UnpauseQueueMember</ref>
681 <ref type="function">QUEUE_VARIABLES</ref>
682 <ref type="function">QUEUE_MEMBER</ref>
683 <ref type="function">QUEUE_MEMBER_COUNT</ref>
684 <ref type="function">QUEUE_EXISTS</ref>
685 <ref type="function">QUEUE_WAITING_COUNT</ref>
686 <ref type="function">QUEUE_MEMBER_LIST</ref>
687 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
690 <function name="QUEUE_MEMBER_PENALTY" language="en_US">
692 Gets or sets queue members penalty.
695 <parameter name="queuename" required="true" />
696 <parameter name="interface" required="true" />
699 <para>Gets or sets queue members penalty.</para>
700 <warning><para>This function has been deprecated in favor of the <literal>QUEUE_MEMBER()</literal> function</para></warning>
703 <ref type="application">Queue</ref>
704 <ref type="application">QueueLog</ref>
705 <ref type="application">AddQueueMember</ref>
706 <ref type="application">RemoveQueueMember</ref>
707 <ref type="application">PauseQueueMember</ref>
708 <ref type="application">UnpauseQueueMember</ref>
709 <ref type="function">QUEUE_VARIABLES</ref>
710 <ref type="function">QUEUE_MEMBER</ref>
711 <ref type="function">QUEUE_MEMBER_COUNT</ref>
712 <ref type="function">QUEUE_EXISTS</ref>
713 <ref type="function">QUEUE_WAITING_COUNT</ref>
714 <ref type="function">QUEUE_MEMBER_LIST</ref>
715 <ref type="function">QUEUE_MEMBER_PENALTY</ref>
718 <manager name="Queues" language="en_US">
727 <manager name="QueueStatus" language="en_US">
732 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
733 <parameter name="Queue" />
734 <parameter name="Member" />
739 <manager name="QueueSummary" language="en_US">
744 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
745 <parameter name="Queue" />
750 <manager name="QueueAdd" language="en_US">
752 Add interface to queue.
755 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
756 <parameter name="Queue" required="true" />
757 <parameter name="Interface" required="true" />
758 <parameter name="Penalty" />
759 <parameter name="Paused" />
760 <parameter name="MemberName" />
761 <parameter name="StateInterface" />
766 <manager name="QueueRemove" language="en_US">
768 Remove interface from queue.
771 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
772 <parameter name="Queue" required="true" />
773 <parameter name="Interface" required="true" />
778 <manager name="QueuePause" language="en_US">
780 Makes a queue member temporarily unavailable.
783 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
784 <parameter name="Interface" required="true" />
785 <parameter name="Paused" required="true" />
786 <parameter name="Queue" />
787 <parameter name="Reason" />
792 <manager name="QueueLog" language="en_US">
794 Adds custom entry in queue_log.
797 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
798 <parameter name="Queue" required="true" />
799 <parameter name="Event" required="true" />
800 <parameter name="Uniqueid" />
801 <parameter name="Interface" />
802 <parameter name="Message" />
807 <manager name="QueuePenalty" language="en_US">
809 Set the penalty for a queue member.
812 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
813 <parameter name="Interface" required="true" />
814 <parameter name="Penalty" required="true" />
815 <parameter name="Queue" />
821 <manager name="QueueMemberRingInUse" language="en_US">
823 Set the ringinuse value for a queue member.
826 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
827 <parameter name="Interface" required="true" />
828 <parameter name="RingInUse" required="true" />
829 <parameter name="Queue" />
835 <manager name="QueueRule" language="en_US">
840 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
841 <parameter name="Rule" />
846 <manager name="QueueReload" language="en_US">
848 Reload a queue, queues, or any sub-section of a queue or queues.
851 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
852 <parameter name="Queue" />
853 <parameter name="Members">
859 <parameter name="Rules">
865 <parameter name="Parameters">
875 <manager name="QueueReset" language="en_US">
877 Reset queue statistics.
880 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
881 <parameter name="Queue" />
889 OPT_MARK_AS_ANSWERED = (1 << 0),
890 OPT_GO_ON = (1 << 1),
891 OPT_DATA_QUALITY = (1 << 2),
892 OPT_CALLEE_GO_ON = (1 << 3),
893 OPT_CALLEE_HANGUP = (1 << 4),
894 OPT_CALLER_HANGUP = (1 << 5),
895 OPT_IGNORE_CALL_FW = (1 << 6),
896 OPT_IGNORE_CONNECTEDLINE = (1 << 7),
897 OPT_CALLEE_PARK = (1 << 8),
898 OPT_CALLER_PARK = (1 << 9),
899 OPT_NO_RETRY = (1 << 10),
900 OPT_RINGING = (1 << 11),
901 OPT_RING_WHEN_RINGING = (1 << 12),
902 OPT_CALLEE_TRANSFER = (1 << 13),
903 OPT_CALLER_TRANSFER = (1 << 14),
904 OPT_CALLEE_AUTOMIXMON = (1 << 15),
905 OPT_CALLER_AUTOMIXMON = (1 << 16),
906 OPT_CALLEE_AUTOMON = (1 << 17),
907 OPT_CALLER_AUTOMON = (1 << 18),
911 OPT_ARG_CALLEE_GO_ON = 0,
912 /* note: this entry _MUST_ be the last one in the enum */
916 AST_APP_OPTIONS(queue_exec_options, BEGIN_OPTIONS
917 AST_APP_OPTION('C', OPT_MARK_AS_ANSWERED),
918 AST_APP_OPTION('c', OPT_GO_ON),
919 AST_APP_OPTION('d', OPT_DATA_QUALITY),
920 AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
921 AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
922 AST_APP_OPTION('H', OPT_CALLER_HANGUP),
923 AST_APP_OPTION('i', OPT_IGNORE_CALL_FW),
924 AST_APP_OPTION('I', OPT_IGNORE_CONNECTEDLINE),
925 AST_APP_OPTION('k', OPT_CALLEE_PARK),
926 AST_APP_OPTION('K', OPT_CALLER_PARK),
927 AST_APP_OPTION('n', OPT_NO_RETRY),
928 AST_APP_OPTION('r', OPT_RINGING),
929 AST_APP_OPTION('R', OPT_RING_WHEN_RINGING),
930 AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
931 AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
932 AST_APP_OPTION('x', OPT_CALLEE_AUTOMIXMON),
933 AST_APP_OPTION('X', OPT_CALLER_AUTOMIXMON),
934 AST_APP_OPTION('w', OPT_CALLEE_AUTOMON),
935 AST_APP_OPTION('W', OPT_CALLER_AUTOMON),
939 QUEUE_STRATEGY_RINGALL = 0,
940 QUEUE_STRATEGY_LEASTRECENT,
941 QUEUE_STRATEGY_FEWESTCALLS,
942 QUEUE_STRATEGY_RANDOM,
943 QUEUE_STRATEGY_RRMEMORY,
944 QUEUE_STRATEGY_LINEAR,
945 QUEUE_STRATEGY_WRANDOM,
946 QUEUE_STRATEGY_RRORDERED,
950 QUEUE_AUTOPAUSE_OFF = 0,
955 enum queue_reload_mask {
956 QUEUE_RELOAD_PARAMETERS = (1 << 0),
957 QUEUE_RELOAD_MEMBER = (1 << 1),
958 QUEUE_RELOAD_RULES = (1 << 2),
959 QUEUE_RESET_STATS = (1 << 3),
962 static const struct strategy {
966 { QUEUE_STRATEGY_RINGALL, "ringall" },
967 { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
968 { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
969 { QUEUE_STRATEGY_RANDOM, "random" },
970 { QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
971 { QUEUE_STRATEGY_RRMEMORY, "roundrobin" },
972 { QUEUE_STRATEGY_LINEAR, "linear" },
973 { QUEUE_STRATEGY_WRANDOM, "wrandom"},
974 { QUEUE_STRATEGY_RRORDERED, "rrordered"},
977 static const struct autopause {
980 } autopausesmodes [] = {
981 { QUEUE_AUTOPAUSE_OFF,"no" },
982 { QUEUE_AUTOPAUSE_ON, "yes" },
983 { QUEUE_AUTOPAUSE_ALL,"all" },
987 static struct ast_taskprocessor *devicestate_tps;
989 #define DEFAULT_RETRY 5
990 #define DEFAULT_TIMEOUT 15
991 #define RECHECK 1 /*!< Recheck every second to see we we're at the top yet */
992 #define MAX_PERIODIC_ANNOUNCEMENTS 10 /*!< The maximum periodic announcements we can have */
993 #define DEFAULT_MIN_ANNOUNCE_FREQUENCY 15 /*!< The minimum number of seconds between position announcements \
994 The default value of 15 provides backwards compatibility */
995 #define MAX_QUEUE_BUCKETS 53
997 #define RES_OKAY 0 /*!< Action completed */
998 #define RES_EXISTS (-1) /*!< Entry already exists */
999 #define RES_OUTOFMEMORY (-2) /*!< Out of memory */
1000 #define RES_NOSUCHQUEUE (-3) /*!< No such queue */
1001 #define RES_NOT_DYNAMIC (-4) /*!< Member is not dynamic */
1003 static char *app = "Queue";
1005 static char *app_aqm = "AddQueueMember" ;
1007 static char *app_rqm = "RemoveQueueMember" ;
1009 static char *app_pqm = "PauseQueueMember" ;
1011 static char *app_upqm = "UnpauseQueueMember" ;
1013 static char *app_ql = "QueueLog" ;
1015 /*! \brief Persistent Members astdb family */
1016 static const char * const pm_family = "Queue/PersistentMembers";
1017 /* The maximum length of each persistent member queue database entry */
1018 #define PM_MAX_LEN 8192
1020 /*! \brief queues.conf [general] option */
1021 static int queue_persistent_members = 0;
1023 /*! \brief queues.conf per-queue weight option */
1024 static int use_weight = 0;
1026 /*! \brief queues.conf [general] option */
1027 static int autofill_default = 1;
1029 /*! \brief queues.conf [general] option */
1030 static int montype_default = 0;
1032 /*! \brief queues.conf [general] option */
1033 static int shared_lastcall = 1;
1035 /*! \brief Subscription to device state change events */
1036 static struct ast_event_sub *device_state_sub;
1038 /*! \brief queues.conf [general] option */
1039 static int update_cdr = 0;
1041 /*! \brief queues.conf [general] option */
1042 static int negative_penalty_invalid = 0;
1044 /*! \brief queues.conf [general] option */
1045 static int log_membername_as_agent = 0;
1047 /*! \brief queues.conf [general] option */
1048 static int check_state_unknown = 0;
1050 /*! \brief name of the ringinuse field in the realtime database */
1051 static char *realtime_ringinuse_field;
1056 QUEUE_JOINEMPTY = 2,
1057 QUEUE_LEAVEEMPTY = 3,
1058 QUEUE_JOINUNAVAIL = 4,
1059 QUEUE_LEAVEUNAVAIL = 5,
1064 static const struct {
1065 enum queue_result id;
1067 } queue_results[] = {
1068 { QUEUE_UNKNOWN, "UNKNOWN" },
1069 { QUEUE_TIMEOUT, "TIMEOUT" },
1070 { QUEUE_JOINEMPTY,"JOINEMPTY" },
1071 { QUEUE_LEAVEEMPTY, "LEAVEEMPTY" },
1072 { QUEUE_JOINUNAVAIL, "JOINUNAVAIL" },
1073 { QUEUE_LEAVEUNAVAIL, "LEAVEUNAVAIL" },
1074 { QUEUE_FULL, "FULL" },
1075 { QUEUE_CONTINUE, "CONTINUE" },
1078 enum queue_timeout_priority {
1079 TIMEOUT_PRIORITY_APP,
1080 TIMEOUT_PRIORITY_CONF,
1083 /*! \brief We define a custom "local user" structure because we
1084 * use it not only for keeping track of what is in use but
1085 * also for keeping track of who we're dialing.
1087 * There are two "links" defined in this structure, q_next and call_next.
1088 * q_next links ALL defined callattempt structures into a linked list. call_next is
1089 * a link which allows for a subset of the callattempts to be traversed. This subset
1090 * is used in wait_for_answer so that irrelevant callattempts are not traversed. This
1091 * also is helpful so that queue logs are always accurate in the case where a call to
1092 * a member times out, especially if using the ringall strategy.
1095 struct callattempt {
1096 struct callattempt *q_next;
1097 struct callattempt *call_next;
1098 struct ast_channel *chan;
1099 char interface[256]; /*!< An Asterisk dial string (not a channel name) */
1102 struct call_queue *lastqueue;
1103 struct member *member;
1104 /*! Saved connected party info from an AST_CONTROL_CONNECTED_LINE. */
1105 struct ast_party_connected_line connected;
1106 /*! TRUE if an AST_CONTROL_CONNECTED_LINE update was saved to the connected element. */
1107 unsigned int pending_connected_update:1;
1108 /*! TRUE if the connected line update is blocked. */
1109 unsigned int block_connected_update:1;
1110 /*! TRUE if caller id is not available for connected line */
1111 unsigned int dial_callerid_absent:1;
1112 /*! TRUE if the call is still active */
1113 unsigned int stillgoing:1;
1114 struct ast_aoc_decoded *aoc_s_rate_list;
1119 struct call_queue *parent; /*!< What queue is our parent */
1120 char moh[MAX_MUSICCLASS]; /*!< Name of musiconhold to be used */
1121 char announce[PATH_MAX]; /*!< Announcement to play for member when call is answered */
1122 char context[AST_MAX_CONTEXT]; /*!< Context when user exits queue */
1123 char digits[AST_MAX_EXTENSION]; /*!< Digits entered while in queue */
1124 int valid_digits; /*!< Digits entered correspond to valid extension. Exited */
1125 int pos; /*!< Where we are in the queue */
1126 int prio; /*!< Our priority */
1127 int last_pos_said; /*!< Last position we told the user */
1128 int ring_when_ringing; /*!< Should we only use ring indication when a channel is ringing? */
1129 time_t last_periodic_announce_time; /*!< The last time we played a periodic announcement */
1130 int last_periodic_announce_sound; /*!< The last periodic announcement we made */
1131 time_t last_pos; /*!< Last time we told the user their position */
1132 int opos; /*!< Where we started in the queue */
1133 int handled; /*!< Whether our call was handled */
1134 int pending; /*!< Non-zero if we are attempting to call a member */
1135 int max_penalty; /*!< Limit the members that can take this call to this penalty or lower */
1136 int min_penalty; /*!< Limit the members that can take this call to this penalty or higher */
1137 int linpos; /*!< If using linear strategy, what position are we at? */
1138 int linwrapped; /*!< Is the linpos wrapped? */
1139 time_t start; /*!< When we started holding */
1140 time_t expire; /*!< When this entry should expire (time out of queue) */
1141 int cancel_answered_elsewhere; /*!< Whether we should force the CAE flag on this call (C) option*/
1142 struct ast_channel *chan; /*!< Our channel */
1143 AST_LIST_HEAD_NOLOCK(,penalty_rule) qe_rules; /*!< Local copy of the queue's penalty rules */
1144 struct penalty_rule *pr; /*!< Pointer to the next penalty rule to implement */
1145 struct queue_ent *next; /*!< The next queue entry */
1149 char interface[AST_CHANNEL_NAME]; /*!< Technology/Location to dial to reach this member*/
1150 char state_exten[AST_MAX_EXTENSION]; /*!< Extension to get state from (if using hint) */
1151 char state_context[AST_MAX_CONTEXT]; /*!< Context to use when getting state (if using hint) */
1152 char state_interface[AST_CHANNEL_NAME]; /*!< Technology/Location from which to read devicestate changes */
1153 char membername[80]; /*!< Member name to use in queue logs */
1154 int penalty; /*!< Are we a last resort? */
1155 int calls; /*!< Number of calls serviced by this member */
1156 int dynamic; /*!< Are we dynamically added? */
1157 int realtime; /*!< Is this member realtime? */
1158 int status; /*!< Status of queue member */
1159 int paused; /*!< Are we paused (not accepting calls)? */
1160 time_t lastcall; /*!< When last successful call was hungup */
1161 struct call_queue *lastqueue; /*!< Last queue we received a call */
1162 unsigned int dead:1; /*!< Used to detect members deleted in realtime */
1163 unsigned int delme:1; /*!< Flag to delete entry on reload */
1164 char rt_uniqueid[80]; /*!< Unique id of realtime member entry */
1165 unsigned int ringinuse:1; /*!< Flag to ring queue members even if their status is 'inuse' */
1168 enum empty_conditions {
1169 QUEUE_EMPTY_PENALTY = (1 << 0),
1170 QUEUE_EMPTY_PAUSED = (1 << 1),
1171 QUEUE_EMPTY_INUSE = (1 << 2),
1172 QUEUE_EMPTY_RINGING = (1 << 3),
1173 QUEUE_EMPTY_UNAVAILABLE = (1 << 4),
1174 QUEUE_EMPTY_INVALID = (1 << 5),
1175 QUEUE_EMPTY_UNKNOWN = (1 << 6),
1176 QUEUE_EMPTY_WRAPUP = (1 << 7),
1179 enum member_properties {
1181 MEMBER_RINGINUSE = 1,
1184 /* values used in multi-bit flags in call_queue */
1185 #define ANNOUNCEHOLDTIME_ALWAYS 1
1186 #define ANNOUNCEHOLDTIME_ONCE 2
1187 #define QUEUE_EVENT_VARIABLES 3
1189 struct penalty_rule {
1190 int time; /*!< Number of seconds that need to pass before applying this rule */
1191 int max_value; /*!< The amount specified in the penalty rule for max penalty */
1192 int min_value; /*!< The amount specified in the penalty rule for min penalty */
1193 int max_relative; /*!< Is the max adjustment relative? 1 for relative, 0 for absolute */
1194 int min_relative; /*!< Is the min adjustment relative? 1 for relative, 0 for absolute */
1195 AST_LIST_ENTRY(penalty_rule) list; /*!< Next penalty_rule */
1198 #define ANNOUNCEPOSITION_YES 1 /*!< We announce position */
1199 #define ANNOUNCEPOSITION_NO 2 /*!< We don't announce position */
1200 #define ANNOUNCEPOSITION_MORE_THAN 3 /*!< We say "Currently there are more than <limit>" */
1201 #define ANNOUNCEPOSITION_LIMIT 4 /*!< We not announce position more than <limit> */
1204 AST_DECLARE_STRING_FIELDS(
1206 AST_STRING_FIELD(name);
1207 /*! Music on Hold class */
1208 AST_STRING_FIELD(moh);
1209 /*! Announcement to play when call is answered */
1210 AST_STRING_FIELD(announce);
1212 AST_STRING_FIELD(context);
1213 /*! Macro to run upon member connection */
1214 AST_STRING_FIELD(membermacro);
1215 /*! Gosub to run upon member connection */
1216 AST_STRING_FIELD(membergosub);
1217 /*! Default rule to use if none specified in call to Queue() */
1218 AST_STRING_FIELD(defaultrule);
1219 /*! Sound file: "Your call is now first in line" (def. queue-youarenext) */
1220 AST_STRING_FIELD(sound_next);
1221 /*! Sound file: "There are currently" (def. queue-thereare) */
1222 AST_STRING_FIELD(sound_thereare);
1223 /*! Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting) */
1224 AST_STRING_FIELD(sound_calls);
1225 /*! Sound file: "Currently there are more than" (def. queue-quantity1) */
1226 AST_STRING_FIELD(queue_quantity1);
1227 /*! Sound file: "callers waiting to speak with a representative" (def. queue-quantity2) */
1228 AST_STRING_FIELD(queue_quantity2);
1229 /*! Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
1230 AST_STRING_FIELD(sound_holdtime);
1231 /*! Sound file: "minutes." (def. queue-minutes) */
1232 AST_STRING_FIELD(sound_minutes);
1233 /*! Sound file: "minute." (def. queue-minute) */
1234 AST_STRING_FIELD(sound_minute);
1235 /*! Sound file: "seconds." (def. queue-seconds) */
1236 AST_STRING_FIELD(sound_seconds);
1237 /*! Sound file: "Thank you for your patience." (def. queue-thankyou) */
1238 AST_STRING_FIELD(sound_thanks);
1239 /*! Sound file: Custom announce for caller, no default */
1240 AST_STRING_FIELD(sound_callerannounce);
1241 /*! Sound file: "Hold time" (def. queue-reporthold) */
1242 AST_STRING_FIELD(sound_reporthold);
1244 /*! Sound files: Custom announce, no default */
1245 struct ast_str *sound_periodicannounce[MAX_PERIODIC_ANNOUNCEMENTS];
1246 unsigned int dead:1;
1247 unsigned int eventwhencalled:2;
1248 unsigned int ringinuse:1;
1249 unsigned int setinterfacevar:1;
1250 unsigned int setqueuevar:1;
1251 unsigned int setqueueentryvar:1;
1252 unsigned int reportholdtime:1;
1253 unsigned int wrapped:1;
1254 unsigned int timeoutrestart:1;
1255 unsigned int announceholdtime:2;
1256 unsigned int announceposition:3;
1258 unsigned int maskmemberstatus:1;
1259 unsigned int realtime:1;
1260 unsigned int found:1;
1261 unsigned int relativeperiodicannounce:1;
1262 unsigned int autopausebusy:1;
1263 unsigned int autopauseunavail:1;
1264 enum empty_conditions joinempty;
1265 enum empty_conditions leavewhenempty;
1266 int announcepositionlimit; /*!< How many positions we announce? */
1267 int announcefrequency; /*!< How often to announce their position */
1268 int minannouncefrequency; /*!< The minimum number of seconds between position announcements (def. 15) */
1269 int periodicannouncefrequency; /*!< How often to play periodic announcement */
1270 int numperiodicannounce; /*!< The number of periodic announcements configured */
1271 int randomperiodicannounce; /*!< Are periodic announcments randomly chosen */
1272 int roundingseconds; /*!< How many seconds do we round to? */
1273 int holdtime; /*!< Current avg holdtime, based on an exponential average */
1274 int talktime; /*!< Current avg talktime, based on the same exponential average */
1275 int callscompleted; /*!< Number of queue calls completed */
1276 int callsabandoned; /*!< Number of queue calls abandoned */
1277 int servicelevel; /*!< seconds setting for servicelevel*/
1278 int callscompletedinsl; /*!< Number of calls answered with servicelevel*/
1279 char monfmt[8]; /*!< Format to use when recording calls */
1280 int montype; /*!< Monitor type Monitor vs. MixMonitor */
1281 int count; /*!< How many entries */
1282 int maxlen; /*!< Max number of entries */
1283 int wrapuptime; /*!< Wrapup Time */
1284 int penaltymemberslimit; /*!< Disregard penalty when queue has fewer than this many members */
1286 int retry; /*!< Retry calling everyone after this amount of time */
1287 int timeout; /*!< How long to wait for an answer */
1288 int weight; /*!< Respective weight */
1289 int autopause; /*!< Auto pause queue members if they fail to answer */
1290 int autopausedelay; /*!< Delay auto pause for autopausedelay seconds since last call */
1291 int timeoutpriority; /*!< Do we allow a fraction of the timeout to occur for a ring? */
1293 /* Queue strategy things */
1294 int rrpos; /*!< Round Robin - position */
1295 int memberdelay; /*!< Seconds to delay connecting member to caller */
1296 int autofill; /*!< Ignore the head call status and ring an available agent */
1298 struct ao2_container *members; /*!< Head of the list of members */
1299 struct queue_ent *head; /*!< Head of the list of callers */
1300 AST_LIST_ENTRY(call_queue) list; /*!< Next call queue */
1301 AST_LIST_HEAD_NOLOCK(, penalty_rule) rules; /*!< The list of penalty rules to invoke */
1306 AST_LIST_HEAD_NOLOCK(,penalty_rule) rules;
1307 AST_LIST_ENTRY(rule_list) list;
1310 static AST_LIST_HEAD_STATIC(rule_lists, rule_list);
1312 static struct ao2_container *queues;
1314 static void update_realtime_members(struct call_queue *q);
1315 static struct member *interface_exists(struct call_queue *q, const char *interface);
1316 static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused);
1318 static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
1320 static struct member *find_member_by_queuename_and_interface(const char *queuename, const char *interface);
1321 /*! \brief sets the QUEUESTATUS channel variable */
1322 static void set_queue_result(struct ast_channel *chan, enum queue_result res)
1326 for (i = 0; i < ARRAY_LEN(queue_results); i++) {
1327 if (queue_results[i].id == res) {
1328 pbx_builtin_setvar_helper(chan, "QUEUESTATUS", queue_results[i].text);
1334 static const char *int2strat(int strategy)
1338 for (x = 0; x < ARRAY_LEN(strategies); x++) {
1339 if (strategy == strategies[x].strategy) {
1340 return strategies[x].name;
1347 static int strat2int(const char *strategy)
1351 for (x = 0; x < ARRAY_LEN(strategies); x++) {
1352 if (!strcasecmp(strategy, strategies[x].name)) {
1353 return strategies[x].strategy;
1360 static int autopause2int(const char *autopause)
1363 /*This 'double check' that default value is OFF */
1364 if (ast_strlen_zero(autopause)) {
1365 return QUEUE_AUTOPAUSE_OFF;
1368 /*This 'double check' is to ensure old values works */
1369 if(ast_true(autopause)) {
1370 return QUEUE_AUTOPAUSE_ON;
1373 for (x = 0; x < ARRAY_LEN(autopausesmodes); x++) {
1374 if (!strcasecmp(autopause, autopausesmodes[x].name)) {
1375 return autopausesmodes[x].autopause;
1379 /*This 'double check' that default value is OFF */
1380 return QUEUE_AUTOPAUSE_OFF;
1383 static int queue_hash_cb(const void *obj, const int flags)
1385 const struct call_queue *q = obj;
1387 return ast_str_case_hash(q->name);
1390 static int queue_cmp_cb(void *obj, void *arg, int flags)
1392 struct call_queue *q = obj, *q2 = arg;
1393 return !strcasecmp(q->name, q2->name) ? CMP_MATCH | CMP_STOP : 0;
1396 #ifdef REF_DEBUG_ONLY_QUEUES
1397 #define queue_ref(q) _queue_ref(q, "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1398 #define queue_unref(q) _queue_unref(q, "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1399 #define queue_t_ref(q, tag) _queue_ref(q, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1400 #define queue_t_unref(q, tag) _queue_unref(q, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1401 #define queues_t_link(c, q, tag) __ao2_link_debug(c, q, 0, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1402 #define queues_t_unlink(c, q, tag) __ao2_unlink_debug(c, q, 0, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1404 static inline struct call_queue *_queue_ref(struct call_queue *q, const char *tag, const char *file, int line, const char *filename)
1406 __ao2_ref_debug(q, 1, tag, file, line, filename);
1410 static inline struct call_queue *_queue_unref(struct call_queue *q, const char *tag, const char *file, int line, const char *filename)
1412 __ao2_ref_debug(q, -1, tag, file, line, filename);
1418 #define queue_t_ref(q, tag) queue_ref(q)
1419 #define queue_t_unref(q, tag) queue_unref(q)
1420 #define queues_t_link(c, q, tag) ao2_t_link(c, q, tag)
1421 #define queues_t_unlink(c, q, tag) ao2_t_unlink(c, q, tag)
1423 static inline struct call_queue *queue_ref(struct call_queue *q)
1429 static inline struct call_queue *queue_unref(struct call_queue *q)
1436 /*! \brief Set variables of queue */
1437 static void set_queue_variables(struct call_queue *q, struct ast_channel *chan)
1439 char interfacevar[256]="";
1444 if (q->setqueuevar) {
1446 if (q->callscompleted > 0) {
1447 sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
1450 snprintf(interfacevar, sizeof(interfacevar),
1451 "QUEUENAME=%s,QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
1452 q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned, q->servicelevel, sl);
1456 pbx_builtin_setvar_multiple(chan, interfacevar);
1462 /*! \brief Insert the 'new' entry after the 'prev' entry of queue 'q' */
1463 static inline void insert_entry(struct call_queue *q, struct queue_ent *prev, struct queue_ent *new, int *pos)
1465 struct queue_ent *cur;
1478 /* every queue_ent must have a reference to it's parent call_queue, this
1479 * reference does not go away until the end of the queue_ent's life, meaning
1480 * that even when the queue_ent leaves the call_queue this ref must remain. */
1483 new->pos = ++(*pos);
1487 /*! \brief Check if members are available
1489 * This function checks to see if members are available to be called. If any member
1490 * is available, the function immediately returns 0. If no members are available,
1491 * then -1 is returned.
1493 static int get_member_status(struct call_queue *q, int max_penalty, int min_penalty, enum empty_conditions conditions)
1495 struct member *member;
1496 struct ao2_iterator mem_iter;
1499 mem_iter = ao2_iterator_init(q->members, 0);
1500 for (; (member = ao2_iterator_next(&mem_iter)); ao2_ref(member, -1)) {
1501 if ((max_penalty && (member->penalty > max_penalty)) || (min_penalty && (member->penalty < min_penalty))) {
1502 if (conditions & QUEUE_EMPTY_PENALTY) {
1503 ast_debug(4, "%s is unavailable because his penalty is not between %d and %d\n", member->membername, min_penalty, max_penalty);
1508 switch (member->status) {
1509 case AST_DEVICE_INVALID:
1510 if (conditions & QUEUE_EMPTY_INVALID) {
1511 ast_debug(4, "%s is unavailable because his device state is 'invalid'\n", member->membername);
1515 case AST_DEVICE_UNAVAILABLE:
1516 if (conditions & QUEUE_EMPTY_UNAVAILABLE) {
1517 ast_debug(4, "%s is unavailable because his device state is 'unavailable'\n", member->membername);
1521 case AST_DEVICE_INUSE:
1522 if (conditions & QUEUE_EMPTY_INUSE) {
1523 ast_debug(4, "%s is unavailable because his device state is 'inuse'\n", member->membername);
1527 case AST_DEVICE_RINGING:
1528 if (conditions & QUEUE_EMPTY_RINGING) {
1529 ast_debug(4, "%s is unavailable because his device state is 'ringing'\n", member->membername);
1533 case AST_DEVICE_UNKNOWN:
1534 if (conditions & QUEUE_EMPTY_UNKNOWN) {
1535 ast_debug(4, "%s is unavailable because his device state is 'unknown'\n", member->membername);
1541 if (member->paused && (conditions & QUEUE_EMPTY_PAUSED)) {
1542 ast_debug(4, "%s is unavailable because he is paused'\n", member->membername);
1544 } else if ((conditions & QUEUE_EMPTY_WRAPUP) && member->lastcall && q->wrapuptime && (time(NULL) - q->wrapuptime < member->lastcall)) {
1545 ast_debug(4, "%s is unavailable because it has only been %d seconds since his last call (wrapup time is %d)\n", member->membername, (int) (time(NULL) - member->lastcall), q->wrapuptime);
1548 ao2_ref(member, -1);
1549 ao2_iterator_destroy(&mem_iter);
1551 ast_debug(4, "%s is available.\n", member->membername);
1557 ao2_iterator_destroy(&mem_iter);
1563 struct statechange {
1564 AST_LIST_ENTRY(statechange) entry;
1569 /*! \brief set a member's status based on device state of that member's state_interface.
1571 * Lock interface list find sc, iterate through each queues queue_member list for member to
1572 * update state inside queues
1574 static int update_status(struct call_queue *q, struct member *m, const int status)
1578 if (q->maskmemberstatus) {
1583 <managerEventInstance>
1584 <synopsis>Raised when a Queue member's status has changed.</synopsis>
1586 <parameter name="Queue">
1587 <para>The name of the queue.</para>
1589 <parameter name="Location">
1590 <para>The queue member's channel technology or location.</para>
1592 <parameter name="MemberName">
1593 <para>The name of the queue member.</para>
1595 <parameter name="StateInterface">
1596 <para>Channel technology or location from which to read device state changes.</para>
1598 <parameter name="Membership">
1600 <enum name="dynamic"/>
1601 <enum name="realtime"/>
1602 <enum name="static"/>
1605 <parameter name="Penalty">
1606 <para>The penalty associated with the queue member.</para>
1608 <parameter name="CallsTaken">
1609 <para>The number of calls this queue member has serviced.</para>
1611 <parameter name="LastCall">
1612 <para>The time this member last took call, expressed in seconds since 00:00, Jan 1, 1970 UTC.</para>
1614 <parameter name="Status">
1615 <para>The numeric device state status of the queue member.</para>
1617 <enum name="0"><para>AST_DEVICE_UNKNOWN</para></enum>
1618 <enum name="1"><para>AST_DEVICE_NOT_INUSE</para></enum>
1619 <enum name="2"><para>AST_DEVICE_INUSE</para></enum>
1620 <enum name="3"><para>AST_DEVICE_BUSY</para></enum>
1621 <enum name="4"><para>AST_DEVICE_INVALID</para></enum>
1622 <enum name="5"><para>AST_DEVICE_UNAVAILABLE</para></enum>
1623 <enum name="6"><para>AST_DEVICE_RINGING</para></enum>
1624 <enum name="7"><para>AST_DEVICE_RINGINUSE</para></enum>
1625 <enum name="8"><para>AST_DEVICE_ONHOLD</para></enum>
1628 <parameter name="Paused">
1635 </managerEventInstance>
1637 manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
1640 "MemberName: %s\r\n"
1641 "StateInterface: %s\r\n"
1642 "Membership: %s\r\n"
1644 "CallsTaken: %d\r\n"
1648 q->name, m->interface, m->membername, m->state_interface, m->dynamic ? "dynamic" : m->realtime ? "realtime" : "static",
1649 m->penalty, m->calls, (int)m->lastcall, m->status, m->paused
1656 * \internal \brief Determine if a queue member is available
1657 * \retval 1 if the member is available
1658 * \retval 0 if the member is not available
1660 static int is_member_available(struct member *mem)
1664 switch (mem->status) {
1665 case AST_DEVICE_INVALID:
1666 case AST_DEVICE_UNAVAILABLE:
1668 case AST_DEVICE_INUSE:
1669 case AST_DEVICE_BUSY:
1670 case AST_DEVICE_RINGING:
1671 case AST_DEVICE_RINGINUSE:
1672 case AST_DEVICE_ONHOLD:
1673 if (!mem->ringinuse) {
1676 /* else fall through */
1677 case AST_DEVICE_NOT_INUSE:
1678 case AST_DEVICE_UNKNOWN:
1688 /*! \brief set a member's status based on device state of that member's interface*/
1689 static int handle_statechange(void *datap)
1691 struct statechange *sc = datap;
1692 struct ao2_iterator miter, qiter;
1694 struct call_queue *q;
1695 char interface[80], *slash_pos;
1696 int found = 0; /* Found this member in any queue */
1697 int found_member; /* Found this member in this queue */
1698 int avail = 0; /* Found an available member in this queue */
1700 qiter = ao2_iterator_init(queues, 0);
1701 while ((q = ao2_t_iterator_next(&qiter, "Iterate over queues"))) {
1706 miter = ao2_iterator_init(q->members, 0);
1707 for (; (m = ao2_iterator_next(&miter)); ao2_ref(m, -1)) {
1708 if (!found_member) {
1709 ast_copy_string(interface, m->state_interface, sizeof(interface));
1711 if ((slash_pos = strchr(interface, '/'))) {
1712 if (!strncasecmp(interface, "Local/", 6) && (slash_pos = strchr(slash_pos + 1, '/'))) {
1717 if (!strcasecmp(interface, sc->dev)) {
1719 update_status(q, m, sc->state);
1723 /* check every member until we find one NOT_INUSE */
1725 avail = is_member_available(m);
1727 if (avail && found_member) {
1728 /* early exit as we've found an available member and the member of interest */
1737 ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Queue:%s_avail", q->name);
1739 ast_devstate_changed(AST_DEVICE_INUSE, "Queue:%s_avail", q->name);
1743 ao2_iterator_destroy(&miter);
1746 queue_t_unref(q, "Done with iterator");
1748 ao2_iterator_destroy(&qiter);
1751 ast_debug(1, "Device '%s' changed to state '%d' (%s)\n", sc->dev, sc->state, ast_devstate2str(sc->state));
1753 ast_debug(3, "Device '%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n", sc->dev, sc->state, ast_devstate2str(sc->state));
1760 static void device_state_cb(const struct ast_event *event, void *unused)
1762 enum ast_device_state state;
1764 struct statechange *sc;
1767 state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
1768 device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
1770 if (ast_strlen_zero(device)) {
1771 ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
1774 datapsize = sizeof(*sc) + strlen(device) + 1;
1775 if (!(sc = ast_calloc(1, datapsize))) {
1776 ast_log(LOG_ERROR, "failed to calloc a state change struct\n");
1780 strcpy(sc->dev, device);
1781 if (ast_taskprocessor_push(devicestate_tps, handle_statechange, sc) < 0) {
1786 /*! \brief Helper function which converts from extension state to device state values */
1787 static int extensionstate2devicestate(int state)
1790 case AST_EXTENSION_NOT_INUSE:
1791 state = AST_DEVICE_NOT_INUSE;
1793 case AST_EXTENSION_INUSE:
1794 state = AST_DEVICE_INUSE;
1796 case AST_EXTENSION_BUSY:
1797 state = AST_DEVICE_BUSY;
1799 case AST_EXTENSION_RINGING:
1800 state = AST_DEVICE_RINGING;
1802 case AST_EXTENSION_ONHOLD:
1803 state = AST_DEVICE_ONHOLD;
1805 case AST_EXTENSION_UNAVAILABLE:
1806 state = AST_DEVICE_UNAVAILABLE;
1808 case AST_EXTENSION_REMOVED:
1809 case AST_EXTENSION_DEACTIVATED:
1811 state = AST_DEVICE_INVALID;
1818 static int extension_state_cb(char *context, char *exten, struct ast_state_cb_info *info, void *data)
1820 struct ao2_iterator miter, qiter;
1822 struct call_queue *q;
1823 int state = info->exten_state;
1824 int found = 0, device_state = extensionstate2devicestate(state);
1826 /* only interested in extension state updates involving device states */
1827 if (info->reason != AST_HINT_UPDATE_DEVICE) {
1831 qiter = ao2_iterator_init(queues, 0);
1832 while ((q = ao2_t_iterator_next(&qiter, "Iterate through queues"))) {
1835 miter = ao2_iterator_init(q->members, 0);
1836 for (; (m = ao2_iterator_next(&miter)); ao2_ref(m, -1)) {
1837 if (!strcmp(m->state_context, context) && !strcmp(m->state_exten, exten)) {
1838 update_status(q, m, device_state);
1844 ao2_iterator_destroy(&miter);
1847 queue_t_unref(q, "Done with iterator");
1849 ao2_iterator_destroy(&qiter);
1852 ast_debug(1, "Extension '%s@%s' changed to state '%d' (%s)\n", exten, context, device_state, ast_devstate2str(device_state));
1854 ast_debug(3, "Extension '%s@%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n",
1855 exten, context, device_state, ast_devstate2str(device_state));
1861 /*! \brief Return the current state of a member */
1862 static int get_queue_member_status(struct member *cur)
1864 return ast_strlen_zero(cur->state_exten) ? ast_device_state(cur->state_interface) : extensionstate2devicestate(ast_extension_state(NULL, cur->state_context, cur->state_exten));
1867 /*! \brief allocate space for new queue member and set fields based on parameters passed */
1868 static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused, const char *state_interface, int ringinuse)
1872 if ((cur = ao2_alloc(sizeof(*cur), NULL))) {
1873 cur->ringinuse = ringinuse;
1874 cur->penalty = penalty;
1875 cur->paused = paused;
1876 ast_copy_string(cur->interface, interface, sizeof(cur->interface));
1877 if (!ast_strlen_zero(state_interface)) {
1878 ast_copy_string(cur->state_interface, state_interface, sizeof(cur->state_interface));
1880 ast_copy_string(cur->state_interface, interface, sizeof(cur->state_interface));
1882 if (!ast_strlen_zero(membername)) {
1883 ast_copy_string(cur->membername, membername, sizeof(cur->membername));
1885 ast_copy_string(cur->membername, interface, sizeof(cur->membername));
1887 if (!strchr(cur->interface, '/')) {
1888 ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
1890 if (!strncmp(cur->state_interface, "hint:", 5)) {
1891 char *tmp = ast_strdupa(cur->state_interface), *context = tmp;
1892 char *exten = strsep(&context, "@") + 5;
1894 ast_copy_string(cur->state_exten, exten, sizeof(cur->state_exten));
1895 ast_copy_string(cur->state_context, S_OR(context, "default"), sizeof(cur->state_context));
1897 cur->status = get_queue_member_status(cur);
1904 static int compress_char(const char c)
1908 } else if (c > 96) {
1914 static int member_hash_fn(const void *obj, const int flags)
1916 const struct member *mem = obj;
1917 const char *interface = (flags & OBJ_KEY) ? obj : mem->interface;
1918 const char *chname = strchr(interface, '/');
1924 for (i = 0; i < 5 && chname[i]; i++) {
1925 ret += compress_char(chname[i]) << (i * 6);
1930 static int member_cmp_fn(void *obj1, void *obj2, int flags)
1932 struct member *mem1 = obj1;
1933 struct member *mem2 = obj2;
1934 const char *interface = (flags & OBJ_KEY) ? obj2 : mem2->interface;
1936 return strcasecmp(mem1->interface, interface) ? 0 : CMP_MATCH | CMP_STOP;
1940 * \brief Initialize Queue default values.
1941 * \note the queue's lock must be held before executing this function
1943 static void init_queue(struct call_queue *q)
1946 struct penalty_rule *pr_iter;
1949 q->retry = DEFAULT_RETRY;
1950 q->timeout = DEFAULT_TIMEOUT;
1952 q->announcefrequency = 0;
1953 q->minannouncefrequency = DEFAULT_MIN_ANNOUNCE_FREQUENCY;
1954 q->announceholdtime = 1;
1955 q->announcepositionlimit = 10; /* Default 10 positions */
1956 q->announceposition = ANNOUNCEPOSITION_YES; /* Default yes */
1957 q->roundingseconds = 0; /* Default - don't announce seconds */
1958 q->servicelevel = 0;
1960 q->setinterfacevar = 0;
1962 q->setqueueentryvar = 0;
1963 q->autofill = autofill_default;
1964 q->montype = montype_default;
1965 q->monfmt[0] = '\0';
1966 q->reportholdtime = 0;
1968 q->penaltymemberslimit = 0;
1970 q->leavewhenempty = 0;
1972 q->maskmemberstatus = 0;
1973 q->eventwhencalled = 0;
1975 q->timeoutrestart = 0;
1976 q->periodicannouncefrequency = 0;
1977 q->randomperiodicannounce = 0;
1978 q->numperiodicannounce = 0;
1979 q->autopause = QUEUE_AUTOPAUSE_OFF;
1980 q->timeoutpriority = TIMEOUT_PRIORITY_APP;
1981 q->autopausedelay = 0;
1983 if (q->strategy == QUEUE_STRATEGY_LINEAR || q->strategy == QUEUE_STRATEGY_RRORDERED) {
1984 /* linear strategy depends on order, so we have to place all members in a single bucket */
1985 q->members = ao2_container_alloc(1, member_hash_fn, member_cmp_fn);
1987 q->members = ao2_container_alloc(37, member_hash_fn, member_cmp_fn);
1992 ast_string_field_set(q, sound_next, "queue-youarenext");
1993 ast_string_field_set(q, sound_thereare, "queue-thereare");
1994 ast_string_field_set(q, sound_calls, "queue-callswaiting");
1995 ast_string_field_set(q, queue_quantity1, "queue-quantity1");
1996 ast_string_field_set(q, queue_quantity2, "queue-quantity2");
1997 ast_string_field_set(q, sound_holdtime, "queue-holdtime");
1998 ast_string_field_set(q, sound_minutes, "queue-minutes");
1999 ast_string_field_set(q, sound_minute, "queue-minute");
2000 ast_string_field_set(q, sound_seconds, "queue-seconds");
2001 ast_string_field_set(q, sound_thanks, "queue-thankyou");
2002 ast_string_field_set(q, sound_reporthold, "queue-reporthold");
2004 if (!q->sound_periodicannounce[0]) {
2005 q->sound_periodicannounce[0] = ast_str_create(32);
2008 if (q->sound_periodicannounce[0]) {
2009 ast_str_set(&q->sound_periodicannounce[0], 0, "queue-periodic-announce");
2012 for (i = 1; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
2013 if (q->sound_periodicannounce[i]) {
2014 ast_str_set(&q->sound_periodicannounce[i], 0, "%s", "");
2018 while ((pr_iter = AST_LIST_REMOVE_HEAD(&q->rules,list))) {
2023 static void clear_queue(struct call_queue *q)
2026 q->callscompleted = 0;
2027 q->callsabandoned = 0;
2028 q->callscompletedinsl = 0;
2033 struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
2034 while ((mem = ao2_iterator_next(&mem_iter))) {
2039 ao2_iterator_destroy(&mem_iter);
2044 * \brief Change queue penalty by adding rule.
2046 * Check rule for errors with time or fomatting, see if rule is relative to rest
2047 * of queue, iterate list of rules to find correct insertion point, insert and return.
2048 * \retval -1 on failure
2049 * \retval 0 on success
2050 * \note Call this with the rule_lists locked
2052 static int insert_penaltychange(const char *list_name, const char *content, const int linenum)
2054 char *timestr, *maxstr, *minstr, *contentdup;
2055 struct penalty_rule *rule = NULL, *rule_iter;
2056 struct rule_list *rl_iter;
2057 int penaltychangetime, inserted = 0;
2059 if (!(rule = ast_calloc(1, sizeof(*rule)))) {
2063 contentdup = ast_strdupa(content);
2065 if (!(maxstr = strchr(contentdup, ','))) {
2066 ast_log(LOG_WARNING, "Improperly formatted penaltychange rule at line %d. Ignoring.\n", linenum);
2072 timestr = contentdup;
2074 if ((penaltychangetime = atoi(timestr)) < 0) {
2075 ast_log(LOG_WARNING, "Improper time parameter specified for penaltychange rule at line %d. Ignoring.\n", linenum);
2080 rule->time = penaltychangetime;
2082 if ((minstr = strchr(maxstr,','))) {
2086 /* The last check will evaluate true if either no penalty change is indicated for a given rule
2087 * OR if a min penalty change is indicated but no max penalty change is */
2088 if (*maxstr == '+' || *maxstr == '-' || *maxstr == '\0') {
2089 rule->max_relative = 1;
2092 rule->max_value = atoi(maxstr);
2094 if (!ast_strlen_zero(minstr)) {
2095 if (*minstr == '+' || *minstr == '-') {
2096 rule->min_relative = 1;
2098 rule->min_value = atoi(minstr);
2099 } else { /*there was no minimum specified, so assume this means no change*/
2100 rule->min_relative = 1;
2103 /*We have the rule made, now we need to insert it where it belongs*/
2104 AST_LIST_TRAVERSE(&rule_lists, rl_iter, list){
2105 if (strcasecmp(rl_iter->name, list_name)) {
2109 AST_LIST_TRAVERSE_SAFE_BEGIN(&rl_iter->rules, rule_iter, list) {
2110 if (rule->time < rule_iter->time) {
2111 AST_LIST_INSERT_BEFORE_CURRENT(rule, list);
2116 AST_LIST_TRAVERSE_SAFE_END;
2119 AST_LIST_INSERT_TAIL(&rl_iter->rules, rule, list);
2127 ast_log(LOG_WARNING, "Unknown rule list name %s; ignoring.\n", list_name);
2134 static void parse_empty_options(const char *value, enum empty_conditions *empty, int joinempty)
2136 char *value_copy = ast_strdupa(value);
2137 char *option = NULL;
2138 while ((option = strsep(&value_copy, ","))) {
2139 if (!strcasecmp(option, "paused")) {
2140 *empty |= QUEUE_EMPTY_PAUSED;
2141 } else if (!strcasecmp(option, "penalty")) {
2142 *empty |= QUEUE_EMPTY_PENALTY;
2143 } else if (!strcasecmp(option, "inuse")) {
2144 *empty |= QUEUE_EMPTY_INUSE;
2145 } else if (!strcasecmp(option, "ringing")) {
2146 *empty |= QUEUE_EMPTY_RINGING;
2147 } else if (!strcasecmp(option, "invalid")) {
2148 *empty |= QUEUE_EMPTY_INVALID;
2149 } else if (!strcasecmp(option, "wrapup")) {
2150 *empty |= QUEUE_EMPTY_WRAPUP;
2151 } else if (!strcasecmp(option, "unavailable")) {
2152 *empty |= QUEUE_EMPTY_UNAVAILABLE;
2153 } else if (!strcasecmp(option, "unknown")) {
2154 *empty |= QUEUE_EMPTY_UNKNOWN;
2155 } else if (!strcasecmp(option, "loose")) {
2156 *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID);
2157 } else if (!strcasecmp(option, "strict")) {
2158 *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED | QUEUE_EMPTY_UNAVAILABLE);
2159 } else if ((ast_false(option) && joinempty) || (ast_true(option) && !joinempty)) {
2160 *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED);
2161 } else if ((ast_false(option) && !joinempty) || (ast_true(option) && joinempty)) {
2164 ast_log(LOG_WARNING, "Unknown option %s for '%s'\n", option, joinempty ? "joinempty" : "leavewhenempty");
2169 /*! \brief Configure a queue parameter.
2171 * The failunknown flag is set for config files (and static realtime) to show
2172 * errors for unknown parameters. It is cleared for dynamic realtime to allow
2173 * extra fields in the tables.
2174 * \note For error reporting, line number is passed for .conf static configuration,
2175 * for Realtime queues, linenum is -1.
2177 static void queue_set_param(struct call_queue *q, const char *param, const char *val, int linenum, int failunknown)
2179 if (!strcasecmp(param, "musicclass") ||
2180 !strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
2181 ast_string_field_set(q, moh, val);
2182 } else if (!strcasecmp(param, "announce")) {
2183 ast_string_field_set(q, announce, val);
2184 } else if (!strcasecmp(param, "context")) {
2185 ast_string_field_set(q, context, val);
2186 } else if (!strcasecmp(param, "timeout")) {
2187 q->timeout = atoi(val);
2188 if (q->timeout < 0) {
2189 q->timeout = DEFAULT_TIMEOUT;
2191 } else if (!strcasecmp(param, "ringinuse")) {
2192 q->ringinuse = ast_true(val);
2193 } else if (!strcasecmp(param, "setinterfacevar")) {
2194 q->setinterfacevar = ast_true(val);
2195 } else if (!strcasecmp(param, "setqueuevar")) {
2196 q->setqueuevar = ast_true(val);
2197 } else if (!strcasecmp(param, "setqueueentryvar")) {
2198 q->setqueueentryvar = ast_true(val);
2199 } else if (!strcasecmp(param, "monitor-format")) {
2200 ast_copy_string(q->monfmt, val, sizeof(q->monfmt));
2201 } else if (!strcasecmp(param, "membermacro")) {
2202 ast_string_field_set(q, membermacro, val);
2203 } else if (!strcasecmp(param, "membergosub")) {
2204 ast_string_field_set(q, membergosub, val);
2205 } else if (!strcasecmp(param, "queue-youarenext")) {
2206 ast_string_field_set(q, sound_next, val);
2207 } else if (!strcasecmp(param, "queue-thereare")) {
2208 ast_string_field_set(q, sound_thereare, val);
2209 } else if (!strcasecmp(param, "queue-callswaiting")) {
2210 ast_string_field_set(q, sound_calls, val);
2211 } else if (!strcasecmp(param, "queue-quantity1")) {
2212 ast_string_field_set(q, queue_quantity1, val);
2213 } else if (!strcasecmp(param, "queue-quantity2")) {
2214 ast_string_field_set(q, queue_quantity2, val);
2215 } else if (!strcasecmp(param, "queue-holdtime")) {
2216 ast_string_field_set(q, sound_holdtime, val);
2217 } else if (!strcasecmp(param, "queue-minutes")) {
2218 ast_string_field_set(q, sound_minutes, val);
2219 } else if (!strcasecmp(param, "queue-minute")) {
2220 ast_string_field_set(q, sound_minute, val);
2221 } else if (!strcasecmp(param, "queue-seconds")) {
2222 ast_string_field_set(q, sound_seconds, val);
2223 } else if (!strcasecmp(param, "queue-thankyou")) {
2224 ast_string_field_set(q, sound_thanks, val);
2225 } else if (!strcasecmp(param, "queue-callerannounce")) {
2226 ast_string_field_set(q, sound_callerannounce, val);
2227 } else if (!strcasecmp(param, "queue-reporthold")) {
2228 ast_string_field_set(q, sound_reporthold, val);
2229 } else if (!strcasecmp(param, "announce-frequency")) {
2230 q->announcefrequency = atoi(val);
2231 } else if (!strcasecmp(param, "min-announce-frequency")) {
2232 q->minannouncefrequency = atoi(val);
2233 ast_debug(1, "%s=%s for queue '%s'\n", param, val, q->name);
2234 } else if (!strcasecmp(param, "announce-round-seconds")) {
2235 q->roundingseconds = atoi(val);
2236 /* Rounding to any other values just doesn't make sense... */
2237 if (!(q->roundingseconds == 0 || q->roundingseconds == 5 || q->roundingseconds == 10
2238 || q->roundingseconds == 15 || q->roundingseconds == 20 || q->roundingseconds == 30)) {
2240 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
2241 "using 0 instead for queue '%s' at line %d of queues.conf\n",
2242 val, param, q->name, linenum);
2244 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
2245 "using 0 instead for queue '%s'\n", val, param, q->name);
2247 q->roundingseconds=0;
2249 } else if (!strcasecmp(param, "announce-holdtime")) {
2250 if (!strcasecmp(val, "once")) {
2251 q->announceholdtime = ANNOUNCEHOLDTIME_ONCE;
2252 } else if (ast_true(val)) {
2253 q->announceholdtime = ANNOUNCEHOLDTIME_ALWAYS;
2255 q->announceholdtime = 0;
2257 } else if (!strcasecmp(param, "announce-position")) {
2258 if (!strcasecmp(val, "limit")) {
2259 q->announceposition = ANNOUNCEPOSITION_LIMIT;
2260 } else if (!strcasecmp(val, "more")) {
2261 q->announceposition = ANNOUNCEPOSITION_MORE_THAN;
2262 } else if (ast_true(val)) {
2263 q->announceposition = ANNOUNCEPOSITION_YES;
2265 q->announceposition = ANNOUNCEPOSITION_NO;
2267 } else if (!strcasecmp(param, "announce-position-limit")) {
2268 q->announcepositionlimit = atoi(val);
2269 } else if (!strcasecmp(param, "periodic-announce")) {
2270 if (strchr(val, ',')) {
2271 char *s, *buf = ast_strdupa(val);
2274 while ((s = strsep(&buf, ",|"))) {
2275 if (!q->sound_periodicannounce[i]) {
2276 q->sound_periodicannounce[i] = ast_str_create(16);
2278 ast_str_set(&q->sound_periodicannounce[i], 0, "%s", s);
2280 if (i == MAX_PERIODIC_ANNOUNCEMENTS) {
2284 q->numperiodicannounce = i;
2286 ast_str_set(&q->sound_periodicannounce[0], 0, "%s", val);
2287 q->numperiodicannounce = 1;
2289 } else if (!strcasecmp(param, "periodic-announce-frequency")) {
2290 q->periodicannouncefrequency = atoi(val);
2291 } else if (!strcasecmp(param, "relative-periodic-announce")) {
2292 q->relativeperiodicannounce = ast_true(val);
2293 } else if (!strcasecmp(param, "random-periodic-announce")) {
2294 q->randomperiodicannounce = ast_true(val);
2295 } else if (!strcasecmp(param, "retry")) {
2296 q->retry = atoi(val);
2297 if (q->retry <= 0) {
2298 q->retry = DEFAULT_RETRY;
2300 } else if (!strcasecmp(param, "wrapuptime")) {
2301 q->wrapuptime = atoi(val);
2302 } else if (!strcasecmp(param, "penaltymemberslimit")) {
2303 if ((sscanf(val, "%10d", &q->penaltymemberslimit) != 1)) {
2304 q->penaltymemberslimit = 0;
2306 } else if (!strcasecmp(param, "autofill")) {
2307 q->autofill = ast_true(val);
2308 } else if (!strcasecmp(param, "monitor-type")) {
2309 if (!strcasecmp(val, "mixmonitor")) {
2312 } else if (!strcasecmp(param, "autopause")) {
2313 q->autopause = autopause2int(val);
2314 } else if (!strcasecmp(param, "autopausedelay")) {
2315 q->autopausedelay = atoi(val);
2316 } else if (!strcasecmp(param, "autopausebusy")) {
2317 q->autopausebusy = ast_true(val);
2318 } else if (!strcasecmp(param, "autopauseunavail")) {
2319 q->autopauseunavail = ast_true(val);
2320 } else if (!strcasecmp(param, "maxlen")) {
2321 q->maxlen = atoi(val);
2322 if (q->maxlen < 0) {
2325 } else if (!strcasecmp(param, "servicelevel")) {
2326 q->servicelevel= atoi(val);
2327 } else if (!strcasecmp(param, "strategy")) {
2330 /* We are a static queue and already have set this, no need to do it again */
2334 strategy = strat2int(val);
2336 ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
2338 q->strategy = QUEUE_STRATEGY_RINGALL;
2340 if (strategy == q->strategy) {
2343 if (strategy == QUEUE_STRATEGY_LINEAR) {
2344 ast_log(LOG_WARNING, "Changing to the linear strategy currently requires asterisk to be restarted.\n");
2347 q->strategy = strategy;
2348 } else if (!strcasecmp(param, "joinempty")) {
2349 parse_empty_options(val, &q->joinempty, 1);
2350 } else if (!strcasecmp(param, "leavewhenempty")) {
2351 parse_empty_options(val, &q->leavewhenempty, 0);
2352 } else if (!strcasecmp(param, "eventmemberstatus")) {
2353 q->maskmemberstatus = !ast_true(val);
2354 } else if (!strcasecmp(param, "eventwhencalled")) {
2355 if (!strcasecmp(val, "vars")) {
2356 q->eventwhencalled = QUEUE_EVENT_VARIABLES;
2358 q->eventwhencalled = ast_true(val) ? 1 : 0;
2360 } else if (!strcasecmp(param, "reportholdtime")) {
2361 q->reportholdtime = ast_true(val);
2362 } else if (!strcasecmp(param, "memberdelay")) {
2363 q->memberdelay = atoi(val);
2364 } else if (!strcasecmp(param, "weight")) {
2365 q->weight = atoi(val);
2366 } else if (!strcasecmp(param, "timeoutrestart")) {
2367 q->timeoutrestart = ast_true(val);
2368 } else if (!strcasecmp(param, "defaultrule")) {
2369 ast_string_field_set(q, defaultrule, val);
2370 } else if (!strcasecmp(param, "timeoutpriority")) {
2371 if (!strcasecmp(val, "conf")) {
2372 q->timeoutpriority = TIMEOUT_PRIORITY_CONF;
2374 q->timeoutpriority = TIMEOUT_PRIORITY_APP;
2376 } else if (failunknown) {
2378 ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queues.conf\n",
2379 q->name, param, linenum);
2381 ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s\n", q->name, param);
2387 * \brief Find rt member record to update otherwise create one.
2389 * Search for member in queue, if found update penalty/paused state,
2390 * if no member exists create one flag it as a RT member and add to queue member list.
2392 static void rt_handle_member_record(struct call_queue *q, char *interface, struct ast_config *member_config)
2395 struct ao2_iterator mem_iter;
2399 int ringinuse = q->ringinuse;
2401 const char *config_val;
2402 const char *rt_uniqueid = ast_variable_retrieve(member_config, interface, "uniqueid");
2403 const char *membername = S_OR(ast_variable_retrieve(member_config, interface, "membername"), interface);
2404 const char *state_interface = S_OR(ast_variable_retrieve(member_config, interface, "state_interface"), interface);
2405 const char *penalty_str = ast_variable_retrieve(member_config, interface, "penalty");
2406 const char *paused_str = ast_variable_retrieve(member_config, interface, "paused");
2408 if (ast_strlen_zero(rt_uniqueid)) {
2409 ast_log(LOG_WARNING, "Realtime field uniqueid is empty for member %s\n", S_OR(membername, "NULL"));
2414 penalty = atoi(penalty_str);
2415 if ((penalty < 0) && negative_penalty_invalid) {
2417 } else if (penalty < 0) {
2423 paused = atoi(paused_str);
2429 if ((config_val = ast_variable_retrieve(member_config, interface, realtime_ringinuse_field))) {
2430 if (ast_true(config_val)) {
2432 } else if (ast_false(config_val)) {
2435 ast_log(LOG_WARNING, "Invalid value of '%s' field for %s in queue '%s'\n", realtime_ringinuse_field, interface, q->name);
2439 /* Find member by realtime uniqueid and update */
2440 mem_iter = ao2_iterator_init(q->members, 0);
2441 while ((m = ao2_iterator_next(&mem_iter))) {
2442 if (!strcasecmp(m->rt_uniqueid, rt_uniqueid)) {
2443 m->dead = 0; /* Do not delete this one. */
2444 ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
2448 if (strcasecmp(state_interface, m->state_interface)) {
2449 ast_copy_string(m->state_interface, state_interface, sizeof(m->state_interface));
2451 m->penalty = penalty;
2452 m->ringinuse = ringinuse;
2459 ao2_iterator_destroy(&mem_iter);
2461 /* Create a new member */
2463 if ((m = create_queue_member(interface, membername, penalty, paused, state_interface, ringinuse))) {
2466 ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
2467 if (!log_membername_as_agent) {
2468 ast_queue_log(q->name, "REALTIME", m->interface, "ADDMEMBER", "%s", paused ? "PAUSED" : "");
2470 ast_queue_log(q->name, "REALTIME", m->membername, "ADDMEMBER", "%s", paused ? "PAUSED" : "");
2472 ao2_link(q->members, m);
2479 /*! \brief Iterate through queue's member list and delete them */
2480 static void free_members(struct call_queue *q, int all)
2482 /* Free non-dynamic members */
2484 struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
2486 while ((cur = ao2_iterator_next(&mem_iter))) {
2487 if (all || !cur->dynamic) {
2488 ao2_unlink(q->members, cur);
2492 ao2_iterator_destroy(&mem_iter);
2495 /*! \brief Free queue's member list then its string fields */
2496 static void destroy_queue(void *obj)
2498 struct call_queue *q = obj;
2502 ast_string_field_free_memory(q);
2503 for (i = 0; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
2504 if (q->sound_periodicannounce[i]) {
2505 free(q->sound_periodicannounce[i]);
2508 ao2_ref(q->members, -1);
2511 static struct call_queue *alloc_queue(const char *queuename)
2513 struct call_queue *q;
2515 if ((q = ao2_t_alloc(sizeof(*q), destroy_queue, "Allocate queue"))) {
2516 if (ast_string_field_init(q, 64)) {
2517 queue_t_unref(q, "String field allocation failed");
2520 ast_string_field_set(q, name, queuename);
2526 * \brief Reload a single queue via realtime.
2528 * Check for statically defined queue first, check if deleted RT queue,
2529 * check for new RT queue, if queue vars are not defined init them with defaults.
2530 * reload RT queue vars, set RT queue members dead and reload them, return finished queue.
2531 * \retval the queue,
2532 * \retval NULL if it doesn't exist.
2533 * \note Should be called with the "queues" container locked.
2535 static struct call_queue *find_queue_by_name_rt(const char *queuename, struct ast_variable *queue_vars, struct ast_config *member_config)
2537 struct ast_variable *v;
2538 struct call_queue *q, tmpq = {
2542 struct ao2_iterator mem_iter;
2543 char *interface = NULL;
2544 const char *tmp_name;
2546 char tmpbuf[64]; /* Must be longer than the longest queue param name. */
2548 /* Static queues override realtime. */
2549 if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Check if static queue exists"))) {
2554 queue_t_unref(q, "Queue is dead; can't return it");
2557 ast_log(LOG_WARNING, "Static queue '%s' already exists. Not loading from realtime\n", q->name);
2561 } else if (!member_config) {
2562 /* Not found in the list, and it's not realtime ... */
2565 /* Check if queue is defined in realtime. */
2567 /* Delete queue from in-core list if it has been deleted in realtime. */
2569 /*! \note Hmm, can't seem to distinguish a DB failure from a not
2570 found condition... So we might delete an in-core queue
2571 in case of DB failure. */
2572 ast_debug(1, "Queue %s not found in realtime.\n", queuename);
2575 /* Delete if unused (else will be deleted when last caller leaves). */
2576 queues_t_unlink(queues, q, "Unused; removing from container");
2578 queue_t_unref(q, "Queue is dead; can't return it");
2583 /* Create a new queue if an in-core entry does not exist yet. */
2585 struct ast_variable *tmpvar = NULL;
2586 if (!(q = alloc_queue(queuename))) {
2592 /*Before we initialize the queue, we need to set the strategy, so that linear strategy
2593 * will allocate the members properly
2595 for (tmpvar = queue_vars; tmpvar; tmpvar = tmpvar->next) {
2596 if (!strcasecmp(tmpvar->name, "strategy")) {
2597 q->strategy = strat2int(tmpvar->value);
2598 if (q->strategy < 0) {
2599 ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
2600 tmpvar->value, q->name);
2601 q->strategy = QUEUE_STRATEGY_RINGALL;
2606 /* We traversed all variables and didn't find a strategy */
2608 q->strategy = QUEUE_STRATEGY_RINGALL;
2610 queues_t_link(queues, q, "Add queue to container");
2612 init_queue(q); /* Ensure defaults for all parameters not set explicitly. */
2614 memset(tmpbuf, 0, sizeof(tmpbuf));
2615 for (v = queue_vars; v; v = v->next) {
2616 /* Convert to dashes `-' from underscores `_' as the latter are more SQL friendly. */
2617 if (strchr(v->name, '_')) {
2618 ast_copy_string(tmpbuf, v->name, sizeof(tmpbuf));
2621 while ((tmp = strchr(tmp, '_'))) {
2628 /* NULL values don't get returned from realtime; blank values should
2629 * still get set. If someone doesn't want a value to be set, they
2630 * should set the realtime column to NULL, not blank. */
2631 queue_set_param(q, tmp_name, v->value, -1, 0);
2634 /* Temporarily set realtime members dead so we can detect deleted ones. */
2635 mem_iter = ao2_iterator_init(q->members, 0);
2636 while ((m = ao2_iterator_next(&mem_iter))) {
2642 ao2_iterator_destroy(&mem_iter);
2644 while ((interface = ast_category_browse(member_config, interface))) {
2645 rt_handle_member_record(q, interface, member_config);
2648 /* Delete all realtime members that have been deleted in DB. */
2649 mem_iter = ao2_iterator_init(q->members, 0);
2650 while ((m = ao2_iterator_next(&mem_iter))) {
2652 if (ast_strlen_zero(m->membername) || !log_membername_as_agent) {
2653 ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
2655 ast_queue_log(q->name, "REALTIME", m->membername, "REMOVEMEMBER", "%s", "");
2657 ao2_unlink(q->members, m);
2661 ao2_iterator_destroy(&mem_iter);
2673 * \brief Returns reference to the named queue. If the queue is realtime, it will load the queue as well.
2674 * \param queuename - name of the desired queue
2677 * \retval NULL if it doesn't exist
2679 static struct call_queue *find_load_queue_rt_friendly(const char *queuename)
2681 struct ast_variable *queue_vars;
2682 struct ast_config *member_config = NULL;
2683 struct call_queue *q = NULL, tmpq = {
2686 int prev_weight = 0;
2688 /* Find the queue in the in-core list first. */
2689 q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Look for queue in memory first");
2691 if (!q || q->realtime) {
2692 /*! \note Load from realtime before taking the "queues" container lock, to avoid blocking all
2693 queue operations while waiting for the DB.
2695 This will be two separate database transactions, so we might
2696 see queue parameters as they were before another process
2697 changed the queue and member list as it was after the change.
2698 Thus we might see an empty member list when a queue is
2699 deleted. In practise, this is unlikely to cause a problem. */
2701 queue_vars = ast_load_realtime("queues", "name", queuename, SENTINEL);
2703 member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", queuename, SENTINEL);
2704 if (!member_config) {
2705 ast_debug(1, "No queue_members defined in config extconfig.conf\n");
2706 member_config = ast_config_new();
2710 prev_weight = q->weight ? 1 : 0;
2711 queue_t_unref(q, "Need to find realtime queue");
2714 q = find_queue_by_name_rt(queuename, queue_vars, member_config);
2715 ast_config_destroy(member_config);
2716 ast_variables_destroy(queue_vars);
2718 /* update the use_weight value if the queue's has gained or lost a weight */
2720 if (!q->weight && prev_weight) {
2721 ast_atomic_fetchadd_int(&use_weight, -1);
2723 if (q->weight && !prev_weight) {
2724 ast_atomic_fetchadd_int(&use_weight, +1);
2727 /* Other cases will end up with the proper value for use_weight */
2729 update_realtime_members(q);
2734 static int update_realtime_member_field(struct member *mem, const char *queue_name, const char *field, const char *value)
2738 if (ast_strlen_zero(mem->rt_uniqueid)) {
2742 if ((ast_update_realtime("queue_members", "uniqueid", mem->rt_uniqueid, field, value, SENTINEL)) > 0) {
2750 static void update_realtime_members(struct call_queue *q)
2752 struct ast_config *member_config = NULL;
2754 char *interface = NULL;
2755 struct ao2_iterator mem_iter;
2757 if (!(member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", q->name , SENTINEL))) {
2758 /* This queue doesn't have realtime members. If the queue still has any realtime
2759 * members in memory, they need to be removed.
2762 mem_iter = ao2_iterator_init(q->members, 0);
2763 while ((m = ao2_iterator_next(&mem_iter))) {
2765 ao2_unlink(q->members, m);
2769 ast_debug(3, "Queue %s has no realtime members defined. No need for update\n", q->name);
2776 /* Temporarily set realtime members dead so we can detect deleted ones.*/
2777 mem_iter = ao2_iterator_init(q->members, 0);
2778 while ((m = ao2_iterator_next(&mem_iter))) {
2784 ao2_iterator_destroy(&mem_iter);
2786 while ((interface = ast_category_browse(member_config, interface))) {
2787 rt_handle_member_record(q, interface, member_config);
2790 /* Delete all realtime members that have been deleted in DB. */
2791 mem_iter = ao2_iterator_init(q->members, 0);
2792 while ((m = ao2_iterator_next(&mem_iter))) {
2794 if (ast_strlen_zero(m->membername) || !log_membername_as_agent) {
2795 ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
2797 ast_queue_log(q->name, "REALTIME", m->membername, "REMOVEMEMBER", "%s", "");
2799 ao2_unlink(q->members, m);
2803 ao2_iterator_destroy(&mem_iter);
2805 ast_config_destroy(member_config);
2808 static int join_queue(char *queuename, struct queue_ent *qe, enum queue_result *reason, int position)
2810 struct call_queue *q;
2811 struct queue_ent *cur, *prev = NULL;
2816 if (!(q = find_load_queue_rt_friendly(queuename))) {
2821 /* This is our one */
2824 if ((status = get_member_status(q, qe->max_penalty, qe->min_penalty, q->joinempty))) {
2825 *reason = QUEUE_JOINEMPTY;
2827 queue_t_unref(q, "Done with realtime queue");
2831 if (*reason == QUEUE_UNKNOWN && q->maxlen && (q->count >= q->maxlen)) {
2832 *reason = QUEUE_FULL;
2833 } else if (*reason == QUEUE_UNKNOWN) {
2834 /* There's space for us, put us at the right position inside
2836 * Take into account the priority of the calling user */
2841 /* We have higher priority than the current user, enter
2842 * before him, after all the other users with priority
2843 * higher or equal to our priority. */
2844 if ((!inserted) && (qe->prio > cur->prio)) {
2845 insert_entry(q, prev, qe, &pos);
2848 /* <= is necessary for the position comparison because it may not be possible to enter
2849 * at our desired position since higher-priority callers may have taken the position we want
2851 if (!inserted && (qe->prio >= cur->prio) && position && (position <= pos + 1)) {
2852 insert_entry(q, prev, qe, &pos);
2854 /*pos is incremented inside insert_entry, so don't need to add 1 here*/
2855 if (position < pos) {
2856 ast_log(LOG_NOTICE, "Asked to be inserted at position %d but forced into position %d due to higher priority callers\n", position, pos);
2863 /* No luck, join at the end of the queue */
2865 insert_entry(q, prev, qe, &pos);
2867 ast_copy_string(qe->moh, q->moh, sizeof(qe->moh));
2868 ast_copy_string(qe->announce, q->announce, sizeof(qe->announce));
2869 ast_copy_string(qe->context, q->context, sizeof(qe->context));
2871 if (q->count == 1) {
2872 ast_devstate_changed(AST_DEVICE_RINGING, "Queue:%s", q->name);
2877 <managerEventInstance>
2878 <synopsis>Raised when a channel joins a Queue.</synopsis>
2880 <xi:include xpointer="xpointer(/docs/managerEvent[@name='QueueMemberStatus']/managerEventInstance/syntax/parameter[@name='Queue'])" />
2881 <parameter name="Position">
2882 <para>This channel's current position in the queue.</para>
2884 <parameter name="Count">
2885 <para>The total number of channels in the queue.</para>
2889 <ref type="managerEvent">Leave</ref>
2890 <ref type="application">Queue</ref>
2892 </managerEventInstance>
2894 ast_manager_event(qe->chan, EVENT_FLAG_CALL, "Join",
2896 "CallerIDNum: %s\r\n"
2897 "CallerIDName: %s\r\n"
2898 "ConnectedLineNum: %s\r\n"
2899 "ConnectedLineName: %s\r\n"
2904 ast_channel_name(qe->chan),
2905 S_COR(ast_channel_caller(qe->chan)->id.number.valid, ast_channel_caller(qe->chan)->id.number.str, "unknown"),/* XXX somewhere else it is <unknown> */
2906 S_COR(ast_channel_caller(qe->chan)->id.name.valid, ast_channel_caller(qe->chan)->id.name.str, "unknown"),
2907 S_COR(ast_channel_connected(qe->chan)->id.number.valid, ast_channel_connected(qe->chan)->id.number.str, "unknown"),/* XXX somewhere else it is <unknown> */
2908 S_COR(ast_channel_connected(qe->chan)->id.name.valid, ast_channel_connected(qe->chan)->id.name.str, "unknown"),
2909 q->name, qe->pos, q->count, ast_channel_uniqueid(qe->chan));
2910 ast_debug(1, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, ast_channel_name(qe->chan), qe->pos );
2913 queue_t_unref(q, "Done with realtime queue");
2918 static int play_file(struct ast_channel *chan, const char *filename)
2922 if (ast_strlen_zero(filename)) {
2926 if (!ast_fileexists(filename, NULL, ast_channel_language(chan))) {
2930 ast_stopstream(chan);
2932 res = ast_streamfile(chan, filename, ast_channel_language(chan));
2934 res = ast_waitstream(chan, AST_DIGIT_ANY);
2937 ast_stopstream(chan);
2943 * \brief Check for valid exit from queue via goto
2944 * \retval 0 if failure
2945 * \retval 1 if successful
2947 static int valid_exit(struct queue_ent *qe, char digit)
2949 int digitlen = strlen(qe->digits);
2951 /* Prevent possible buffer overflow */
2952 if (digitlen < sizeof(qe->digits) - 2) {
2953 qe->digits[digitlen] = digit;
2954 qe->digits[digitlen + 1] = '\0';
2956 qe->digits[0] = '\0';
2960 /* If there's no context to goto, short-circuit */
2961 if (ast_strlen_zero(qe->context)) {
2965 /* If the extension is bad, then reset the digits to blank */
2966 if (!ast_canmatch_extension(qe->chan, qe->context, qe->digits, 1,
2967 S_COR(ast_channel_caller(qe->chan)->id.number.valid, ast_channel_caller(qe->chan)->id.number.str, NULL))) {
2968 qe->digits[0] = '\0';
2972 /* We have an exact match */
2973 if (!ast_goto_if_exists(qe->chan, qe->context, qe->digits, 1)) {
2974 qe->valid_digits = 1;
2975 /* Return 1 on a successful goto */
2982 static int say_position(struct queue_ent *qe, int ringing)
2984 int res = 0, avgholdmins, avgholdsecs, announceposition = 0;
2988 /* Let minannouncefrequency seconds pass between the start of each position announcement */
2990 if ((now - qe->last_pos) < qe->parent->minannouncefrequency) {
2994 /* If either our position has changed, or we are over the freq timer, say position */
2995 if ((qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency)) {
3000 ast_indicate(qe->chan,-1);
3002 ast_moh_stop(qe->chan);
3005 if (qe->parent->announceposition == ANNOUNCEPOSITION_YES ||
3006 qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN ||
3007 (qe->parent->announceposition == ANNOUNCEPOSITION_LIMIT &&
3008 qe->pos <= qe->parent->announcepositionlimit)) {
3009 announceposition = 1;
3013 if (announceposition == 1) {
3014 /* Say we're next, if we are */
3016 res = play_file(qe->chan, qe->parent->sound_next);
3022 if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
3024 res = play_file(qe->chan, qe->parent->queue_quantity1);
3028 res = ast_say_number(qe->chan, qe->parent->announcepositionlimit, AST_DIGIT_ANY, ast_channel_language(qe->chan), NULL); /* Needs gender */
3034 res = play_file(qe->chan, qe->parent->sound_thereare);
3038 res = ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, ast_channel_language(qe->chan), NULL); /* Needs gender */
3043 if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
3045 res = play_file(qe->chan, qe->parent->queue_quantity2);
3050 res = play_file(qe->chan, qe->parent->sound_calls);
3057 /* Round hold time to nearest minute */
3058 avgholdmins = abs(((qe->parent->holdtime + 30) - (now - qe->start)) / 60);
3060 /* If they have specified a rounding then round the seconds as well */
3061 if (qe->parent->roundingseconds) {
3062 avgholdsecs = (abs(((qe->parent->holdtime + 30) - (now - qe->start))) - 60 * avgholdmins) / qe->parent->roundingseconds;
3063 avgholdsecs *= qe->parent->roundingseconds;
3068 ast_verb(3, "Hold time for %s is %d minute(s) %d seconds\n", qe->parent->name, avgholdmins, avgholdsecs);
3070 /* If the hold time is >1 min, if it's enabled, and if it's not
3071 supposed to be only once and we have already said it, say it */
3072 if ((avgholdmins+avgholdsecs) > 0 && qe->parent->announceholdtime &&
3073 ((qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE && !qe->last_pos) ||
3074 !(qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE))) {
3075 res = play_file(qe->chan, qe->parent->sound_holdtime);
3080 if (avgholdmins >= 1) {
3081 res = ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, ast_channel_language(qe->chan), NULL);
3086 if (avgholdmins == 1) {
3087 res = play_file(qe->chan, qe->parent->sound_minute);
3092 res = play_file(qe->chan, qe->parent->sound_minutes);
3098 if (avgholdsecs >= 1) {
3099 res = ast_say_number(qe->chan, avgholdsecs, AST_DIGIT_ANY, ast_channel_language(qe->chan), NULL);
3104 res = play_file(qe->chan, qe->parent->sound_seconds);
3109 } else if (qe->parent->announceholdtime && !qe->parent->announceposition) {
3114 if (qe->parent->announceposition) {
3115 ast_verb(3, "Told %s in %s their queue position (which was %d)\n",
3116 ast_channel_name(qe->chan), qe->parent->name, qe->pos);
3119 res = play_file(qe->chan, qe->parent->sound_thanks);
3123 if ((res > 0 && !valid_exit(qe, res))) {
3127 /* Set our last_pos indicators */
3129 qe->last_pos_said = qe->pos;
3131 /* Don't restart music on hold if we're about to exit the caller from the queue */
3134 ast_indicate(qe->chan, AST_CONTROL_RINGING);
3136 ast_moh_start(qe->chan, qe->moh, NULL);
3142 static void recalc_holdtime(struct queue_ent *qe, int newholdtime)
3146 /* Calculate holdtime using an exponential average */
3147 /* Thanks to SRT for this contribution */
3148 /* 2^2 (4) is the filter coefficient; a higher exponent would give old entries more weight */
3150 ao2_lock(qe->parent);
3151 oldvalue = qe->parent->holdtime;
3152 qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newholdtime) >> 2;
3153 ao2_unlock(qe->parent);
3156 /*! \brief Caller leaving queue.
3158 * Search the queue to find the leaving client, if found remove from queue
3159 * create manager event, move others up the queue.
3161 static void leave_queue(struct queue_ent *qe)
3163 struct call_queue *q;
3164 struct queue_ent *current, *prev = NULL;
3165 struct penalty_rule *pr_iter;
3168 if (!(q = qe->parent)) {
3171 queue_t_ref(q, "Copy queue pointer from queue entry");
3175 for (current = q->head; current; current = current->next) {
3176 if (current == qe) {
3180 ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Queue:%s", q->name);
3183 /* Take us out of the queue */
3185 <managerEventInstance>
3186 <synopsis>Raised when a channel leaves a Queue.</synopsis>
3188 <xi:include xpointer="xpointer(/docs/managerEvent[@name='QueueMemberStatus']/managerEventInstance/syntax/parameter[@name='Queue'])" />
3189 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Join']/managerEventInstance/syntax/parameter[@name='Count'])" />
3190 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Join']/managerEventInstance/syntax/parameter[@name='Position'])" />
3193 <ref type="managerEvent">Join</ref>
3195 </managerEventInstance>
3197 ast_manager_event(qe->chan, EVENT_FLAG_CALL, "Leave",
3198 "Channel: %s\r\nQueue: %s\r\nCount: %d\r\nPosition: %d\r\nUniqueid: %s\r\n",
3199 ast_channel_name(qe->chan), q->name, q->count, qe->pos, ast_channel_uniqueid(qe->chan));
3200 ast_debug(1, "Queue '%s' Leave, Channel '%s'\n", q->name, ast_channel_name(qe->chan));
3201 /* Take us out of the queue */
3203 prev->next = current->next;
3205 q->head = current->next;
3207 /* Free penalty rules */
3208 while ((pr_iter = AST_LIST_REMOVE_HEAD(&qe->qe_rules, list))) {
3211 snprintf(posstr, sizeof(posstr), "%d", qe->pos);
3212 pbx_builtin_setvar_helper(qe->chan, "QUEUEPOSITION", posstr);
3214 /* Renumber the people after us in the queue based on a new count */
3215 current->pos = ++pos;
3221 /*If the queue is a realtime queue, check to see if it's still defined in real time*/
3223 struct ast_variable *var;
3224 if (!(var = ast_load_realtime("queues", "name", q->name, SENTINEL))) {
3227 ast_variables_destroy(var);
3232 /* It's dead and nobody is in it, so kill it */
3233 queues_t_unlink(queues, q, "Queue is now dead; remove it from the container");
3235 /* unref the explicit ref earlier in the function */
3236 queue_t_unref(q, "Expire copied reference");
3241 * \brief Destroy the given callattempt structure and free it.
3244 * \param doomed callattempt structure to destroy.
3248 static void callattempt_free(struct callattempt *doomed)
3250 if (doomed->member) {
3251 ao2_ref(doomed->member, -1);
3253 ast_party_connected_line_free(&doomed->connected);
3257 /*! \brief Hang up a list of outgoing calls */
3258 static void hangupcalls(struct callattempt *outgoing, struct ast_channel *exception, int cancel_answered_elsewhere)
3260 struct callattempt *oo;
3263 /* If someone else answered the call we should indicate this in the CANCEL */
3264 /* Hangup any existing lines we have open */
3265 if (outgoing->chan && (outgoing->chan != exception)) {
3266 if (exception || cancel_answered_elsewhere) {
3267 ast_channel_hangupcause_set(outgoing->chan, AST_CAUSE_ANSWERED_ELSEWHERE);
3269 ast_hangup(outgoing->chan);
3272 outgoing = outgoing->q_next;
3273 ast_aoc_destroy_decoded(oo->aoc_s_rate_list);
3274 callattempt_free(oo);
3279 * \brief Get the number of members available to accept a call.
3281 * \note The queue passed in should be locked prior to this function call
3283 * \param[in] q The queue for which we are couting the number of available members
3284 * \return Return the number of available members in queue q
3286 static int num_available_members(struct call_queue *q)
3290 struct ao2_iterator mem_iter;
3292 mem_iter = ao2_iterator_init(q->members, 0);
3293 while ((mem = ao2_iterator_next(&mem_iter))) {
3295 avl += is_member_available(mem);
3298 /* If autofill is not enabled or if the queue's strategy is ringall, then