Add ability to pass arbitrary data to the ao2_callback_fn (called from
[asterisk/asterisk.git] / apps / app_queue.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief True call queues with optional send URL on answer
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \arg Config in \ref Config_qu queues.conf
26  *
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>
31  *
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.
37  *
38  * \note 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
39  *
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
47  *
48  * Patch Version 1.07 2003-12-24 01
49  *
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>
52  *
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>
55  *
56  * \ingroup applications
57  */
58
59 /*** MODULEINFO
60         <depend>res_monitor</depend>
61  ***/
62
63 #include "asterisk.h"
64
65 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
66
67 #include <sys/time.h>
68 #include <sys/signal.h>
69 #include <netinet/in.h>
70
71 #include "asterisk/lock.h"
72 #include "asterisk/file.h"
73 #include "asterisk/channel.h"
74 #include "asterisk/pbx.h"
75 #include "asterisk/app.h"
76 #include "asterisk/linkedlists.h"
77 #include "asterisk/module.h"
78 #include "asterisk/translate.h"
79 #include "asterisk/say.h"
80 #include "asterisk/features.h"
81 #include "asterisk/musiconhold.h"
82 #include "asterisk/cli.h"
83 #include "asterisk/manager.h"
84 #include "asterisk/config.h"
85 #include "asterisk/monitor.h"
86 #include "asterisk/utils.h"
87 #include "asterisk/causes.h"
88 #include "asterisk/astdb.h"
89 #include "asterisk/devicestate.h"
90 #include "asterisk/stringfields.h"
91 #include "asterisk/event.h"
92 #include "asterisk/astobj2.h"
93 #include "asterisk/strings.h"
94 #include "asterisk/global_datastores.h"
95 #include "asterisk/taskprocessor.h"
96
97 /*!
98  * \par Please read before modifying this file.
99  * There are three locks which are regularly used
100  * throughout this file, the queue list lock, the lock
101  * for each individual queue, and the interface list lock.
102  * Please be extra careful to always lock in the following order
103  * 1) queue list lock
104  * 2) individual queue lock
105  * 3) interface list lock
106  * This order has sort of "evolved" over the lifetime of this
107  * application, but it is now in place this way, so please adhere
108  * to this order!
109  */
110
111 /*** DOCUMENTATION
112         <application name="Queue" language="en_US">
113                 <synopsis>
114                         Queue a call for a call queue.
115                 </synopsis>
116                 <syntax>
117                         <parameter name="queuename" required="true" />
118                         <parameter name="options">
119                                 <optionlist>
120                                         <option name="c">
121                                                 <para>Continue in the dialplan if the callee hangs up.</para>
122                                         </option>
123                                         <option name="d">
124                                                 <para>data-quality (modem) call (minimum delay).</para>
125                                         </option>
126                                         <option name="h">
127                                                 <para>Allow <emphasis>callee</emphasis> to hang up by pressing <literal>*</literal>.</para>
128                                         </option>
129                                         <option name="H">
130                                                 <para>Allow <emphasis>caller</emphasis> to hang up by pressing <literal>*</literal>.</para>
131                                         </option>
132                                         <option name="n">
133                                                 <para>No retries on the timeout; will exit this application and
134                                                 go to the next step.</para>
135                                         </option>
136                                         <option name="i">
137                                                 <para>Ignore call forward requests from queue members and do nothing
138                                                 when they are requested.</para>
139                                         </option>
140                                         <option name="r">
141                                                 <para>Ring instead of playing MOH. Periodic Announcements are still made, if applicable.</para>
142                                         </option>
143                                         <option name="t">
144                                                 <para>Allow the <emphasis>called</emphasis> user to transfer the calling user.</para>
145                                         </option>
146                                         <option name="T">
147                                                 <para>Allow the <emphasis>calling</emphasis> user to transfer the call.</para>
148                                         </option>
149                                         <option name="w">
150                                                 <para>Allow the <emphasis>called</emphasis> user to write the conversation to
151                                                 disk via Monitor.</para>
152                                         </option>
153                                         <option name="W">
154                                                 <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
155                                                 disk via Monitor.</para>
156                                         </option>
157                                         <option name="k">
158                                                 <para>Allow the <emphasis>called</emphasis> party to enable parking of the call by sending
159                                                 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
160                                         </option>
161                                         <option name="K">
162                                                 <para>Allow the <emphasis>calling</emphasis> party to enable parking of the call by sending
163                                                 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
164                                         </option>
165                                         <option name="x">
166                                                 <para>Allow the <emphasis>called</emphasis> user to write the conversation
167                                                 to disk via MixMonitor.</para>
168                                         </option>
169                                         <option name="X">
170                                                 <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
171                                                 disk via MixMonitor.</para>
172                                         </option>
173                                 </optionlist>
174                         </parameter>
175                         <parameter name="URL">
176                                 <para><replaceable>URL</replaceable> will be sent to the called party if the channel supports it.</para>
177                         </parameter>
178                         <parameter name="announceoverride" />
179                         <parameter name="timeout">
180                                 <para>Will cause the queue to fail out after a specified number of
181                                 seconds, checked between each <filename>queues.conf</filename> <replaceable>timeout</replaceable> and
182                                 <replaceable>retry</replaceable> cycle.</para>
183                         </parameter>
184                         <parameter name="AGI">
185                                 <para>Will setup an AGI script to be executed on the calling party's channel once they are
186                                 connected to a queue member.</para>
187                         </parameter>
188                         <parameter name="macro">
189                                 <para>Will run a macro on the calling party's channel once they are connected to a queue member.</para>
190                         </parameter>
191                         <parameter name="gosub">
192                                 <para>Will run a gosub on the calling party's channel once they are connected to a queue member.</para>
193                         </parameter>
194                         <parameter name="rule">
195                                 <para>Will cause the queue's defaultrule to be overridden by the rule specified.</para>
196                         </parameter>
197                 </syntax>
198                 <description>
199                         <para>In addition to transferring the call, a call may be parked and then picked
200                         up by another user.</para>
201                         <para>This application will return to the dialplan if the queue does not exist, or
202                         any of the join options cause the caller to not enter the queue.</para>
203                         <para>This application sets the following channel variable upon completion:</para>
204                         <variablelist>
205                                 <variable name="QUEUESTATUS">
206                                         <para>The status of the call as a text string.</para>
207                                         <value name="TIMEOUT" />
208                                         <value name="FULL" />
209                                         <value name="JOINEMPTY" />
210                                         <value name="LEAVEEMPTY" />
211                                         <value name="JOINUNAVAIL" />
212                                         <value name="LEAVEUNAVAIL" />
213                                         <value name="CONTINUE" />
214                                 </variable>
215                         </variablelist>
216                 </description>
217                 <see-also>
218                         <ref type="application">AddQueueMember</ref>
219                         <ref type="application">RemoveQueueMember</ref>
220                         <ref type="application">PauseQueueMember</ref>
221                         <ref type="application">UnpauseQueueMember</ref>
222                         <ref type="application">AgentLogin</ref>
223                         <ref type="function">QUEUE_MEMBER_COUNT</ref>
224                         <ref type="function">QUEUE_MEMBER_LIST</ref>
225                         <ref type="function">QUEUE_WAITING_COUNT</ref>
226                 </see-also>
227         </application>
228         <application name="AddQueueMember" language="en_US">
229                 <synopsis>
230                         Dynamically adds queue members.
231                 </synopsis>
232                 <syntax>
233                         <parameter name="queuename" required="true" />
234                         <parameter name="interface" />
235                         <parameter name="penalty" />
236                         <parameter name="options" />
237                         <parameter name="membername" />
238                         <parameter name="stateinterface" />
239                 </syntax>
240                 <description>
241                         <para>Dynamically adds interface to an existing queue. If the interface is
242                         already in the queue it will return an error.</para>
243                         <para>This application sets the following channel variable upon completion:</para>
244                         <variablelist>
245                                 <variable name="AQMSTATUS">
246                                         <para>The status of the attempt to add a queue member as a text string.</para>
247                                         <value name="ADDED" />
248                                         <value name="MEMBERALREADY" />
249                                         <value name="NOSUCHQUEUE" />
250                                 </variable>
251                         </variablelist>
252                 </description>
253                 <see-also>
254                         <ref type="application">RemoveQueueMember</ref>
255                         <ref type="application">PauseQueueMember</ref>
256                         <ref type="application">UnpauseQueueMember</ref>
257                         <ref type="application">AgentLogin</ref>
258                 </see-also>
259         </application>
260         <application name="RemoveQueueMember" language="en_US">
261                 <synopsis>
262                         Dynamically removes queue members.
263                 </synopsis>
264                 <syntax>
265                         <parameter name="queuename" required="true" />
266                         <parameter name="interface" />
267                         <parameter name="options" />
268                 </syntax>
269                 <description>
270                         <para>If the interface is <emphasis>NOT</emphasis> in the queue it will return an error.</para>
271                         <para>This application sets the following channel variable upon completion:</para>
272                         <variablelist>
273                                 <variable name="RQMSTATUS">
274                                         <value name="REMOVED" />
275                                         <value name="NOTINQUEUE" />
276                                         <value name="NOSUCHQUEUE" />
277                                 </variable>
278                         </variablelist>
279                         <para>Example: RemoveQueueMember(techsupport,SIP/3000)</para>
280                 </description>
281                 <see-also>
282                         <ref type="application">Queue</ref>
283                         <ref type="application">AddQueueMember</ref>
284                         <ref type="application">PauseQueueMember</ref>
285                         <ref type="application">UnpauseQueueMember</ref>
286                 </see-also>
287         </application>
288         <application name="PauseQueueMember" language="en_US">
289                 <synopsis>
290                         Pauses a queue member.
291                 </synopsis>
292                 <syntax>
293                         <parameter name="queuename" />
294                         <parameter name="interface" required="true" />
295                         <parameter name="options" />
296                         <parameter name="reason">
297                                 <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
298                         </parameter>
299                 </syntax>
300                 <description>
301                         <para>Pauses (blocks calls for) a queue member. The given interface will be paused in the given queue.
302                         This prevents any calls from being sent from the queue to the interface until it is
303                         unpaused with UnpauseQueueMember or the manager interface.  If no queuename is given,
304                         the interface is paused in every queue it is a member of. The application will fail if the
305                         interface is not found.</para>
306                         <para>This application sets the following channel variable upon completion:</para>
307                         <variablelist>
308                                 <variable name="PQMSTATUS">
309                                         <para>The status of the attempt to pause a queue member as a text string.</para>
310                                         <value name="PAUSED" />
311                                         <value name="NOTFOUND" />
312                                 </variable>
313                         </variablelist>
314                         <para>Example: PauseQueueMember(,SIP/3000)</para>
315                 </description>
316                 <see-also>
317                         <ref type="application">UnpauseQueueMember</ref>
318                 </see-also>
319         </application>
320         <application name="UnpauseQueueMember" language="en_US">
321                 <synopsis>
322                         Unpauses a queue member.                
323                 </synopsis>
324                 <syntax>
325                         <parameter name="queuename" />
326                         <parameter name="interface" required="true" />
327                         <parameter name="options" />
328                         <parameter name="reason">
329                                 <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
330                         </parameter>
331                 </syntax>
332                 <description>
333                         <para>Unpauses (resumes calls to) a queue member. This is the counterpart to <literal>PauseQueueMember()</literal>
334                         and operates exactly the same way, except it unpauses instead of pausing the given interface.</para>
335                         <para>This application sets the following channel variable upon completion:</para>
336                         <variablelist>
337                                 <variable name="UPQMSTATUS">
338                                         <para>The status of the attempt to unpause a queue member as a text string.</para>
339                                         <value name="UNPAUSED" />
340                                         <value name="NOTFOUND" />
341                                 </variable>
342                         </variablelist>
343                         <para>Example: UnpauseQueueMember(,SIP/3000)</para>
344                 </description>
345                 <see-also>
346                         <ref type="application">PauseQueueMember</ref>
347                 </see-also>
348         </application>
349         <application name="QueueLog" language="en_US">
350                 <synopsis>
351                         Writes to the queue_log file.
352                 </synopsis>
353                 <syntax>
354                         <parameter name="queuename" required="true" />
355                         <parameter name="uniqueid" required="true" />
356                         <parameter name="agent" required="true" />
357                         <parameter name="event" required="true" />
358                         <parameter name="additionalinfo" />
359                 </syntax>
360                 <description>
361                         <para>Allows you to write your own events into the queue log.</para>
362                         <para>Example: QueueLog(101,${UNIQUEID},${AGENT},WENTONBREAK,600)</para>
363                 </description>
364                 <see-also>
365                         <ref type="application">Queue</ref>
366                 </see-also>
367         </application>
368         <function name="QUEUE_VARIABLES" language="en_US">
369                 <synopsis>
370                         Return Queue information in variables.
371                 </synopsis>
372                 <syntax>
373                         <parameter name="queuename" required="true">
374                                 <enumlist>
375                                         <enum name="QUEUEMAX">
376                                                 <para>Maxmimum number of calls allowed.</para>
377                                         </enum>
378                                         <enum name="QUEUESTRATEGY">
379                                                 <para>The strategy of the queue.</para>
380                                         </enum>
381                                         <enum name="QUEUECALLS">
382                                                 <para>Number of calls currently in the queue.</para>
383                                         </enum>
384                                         <enum name="QUEUEHOLDTIME">
385                                                 <para>Current average hold time.</para>
386                                         </enum>
387                                         <enum name="QUEUECOMPLETED">
388                                                 <para>Number of completed calls for the queue.</para>
389                                         </enum>
390                                         <enum name="QUEUEABANDONED">
391                                                 <para>Number of abandoned calls.</para>
392                                         </enum>
393                                         <enum name="QUEUESRVLEVEL">
394                                                 <para>Queue service level.</para>
395                                         </enum>
396                                         <enum name="QUEUESRVLEVELPERF">
397                                                 <para>Current service level performance.</para>
398                                         </enum>
399                                 </enumlist>
400                         </parameter>
401                 </syntax>
402                 <description>
403                         <para>Makes the following queue variables available.</para>
404                         <para>Returns <literal>0</literal> if queue is found and setqueuevar is defined, <literal>-1</literal> otherwise.</para>
405                 </description>
406         </function>
407         <function name="QUEUE_MEMBER" language="en_US">
408                 <synopsis>
409                         Count number of members answering a queue.
410                 </synopsis>
411                 <syntax>
412                         <parameter name="queuename" required="true" />
413                         <parameter name="option" required="true">
414                                 <enumlist>
415                                         <enum name="logged">
416                                                 <para>Returns the number of logged-in members for the specified queue.</para>
417                                         </enum>
418                                         <enum name="free">
419                                                 <para>Returns the number of logged-in members for the specified queue available to take a call.</para>
420                                         </enum>
421                                         <enum name="count">
422                                                 <para>Returns the total number of members for the specified queue.</para>
423                                         </enum>
424                                 </enumlist>
425                         </parameter>
426                 </syntax>
427                 <description>
428                         <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
429                 </description>
430         </function>
431         <function name="QUEUE_MEMBER_COUNT" language="en_US">
432                 <synopsis>
433                         Count number of members answering a queue.
434                 </synopsis>
435                 <syntax>
436                         <parameter name="queuename" required="true" />
437                 </syntax>
438                 <description>
439                         <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
440                         <warning><para>This function has been deprecated in favor of the <literal>QUEUE_MEMBER()</literal> function</para></warning>
441                 </description>
442                 <see-also>
443                         <ref type="function">QUEUE_MEMBER_LIST</ref>
444                 </see-also>
445         </function>
446         <function name="QUEUE_WAITING_COUNT" language="en_US">
447                 <synopsis>
448                         Count number of calls currently waiting in a queue.
449                 </synopsis>
450                 <syntax>
451                         <parameter name="queuename" />
452                 </syntax>
453                 <description>
454                         <para>Returns the number of callers currently waiting in the specified <replaceable>queuename</replaceable>.</para>
455                 </description>
456         </function>
457         <function name="QUEUE_MEMBER_LIST" language="en_US">
458                 <synopsis>
459                         Returns a list of interfaces on a queue.
460                 </synopsis>
461                 <syntax>
462                         <parameter name="queuename" required="true" />
463                 </syntax>
464                 <description>
465                         <para>Returns a comma-separated list of members associated with the specified <replaceable>queuename</replaceable>.</para>
466                 </description>
467                 <see-also>
468                         <ref type="function">QUEUE_MEMBER_COUNT</ref>
469                 </see-also>
470         </function>
471         <function name="QUEUE_MEMBER_PENALTY" language="en_US">
472                 <synopsis>
473                         Gets or sets queue members penalty.
474                 </synopsis>
475                 <syntax>
476                         <parameter name="queuename" required="true" />
477                         <parameter name="interface" required="true" />
478                 </syntax>
479                 <description>
480                         <para>Gets or sets queue members penalty.</para>
481                 </description>
482         </function>
483
484  ***/
485
486 enum {
487         QUEUE_STRATEGY_RINGALL = 0,
488         QUEUE_STRATEGY_LEASTRECENT,
489         QUEUE_STRATEGY_FEWESTCALLS,
490         QUEUE_STRATEGY_RANDOM,
491         QUEUE_STRATEGY_RRMEMORY,
492         QUEUE_STRATEGY_LINEAR,
493         QUEUE_STRATEGY_WRANDOM
494 };
495
496 static const struct strategy {
497         int strategy;
498         const char *name;
499 } strategies[] = {
500         { QUEUE_STRATEGY_RINGALL, "ringall" },
501         { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
502         { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
503         { QUEUE_STRATEGY_RANDOM, "random" },
504         { QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
505         { QUEUE_STRATEGY_RRMEMORY, "roundrobin" },
506         { QUEUE_STRATEGY_LINEAR, "linear" },
507         { QUEUE_STRATEGY_WRANDOM, "wrandom"},
508 };
509
510 static struct ast_taskprocessor *devicestate_tps;
511
512 #define DEFAULT_RETRY           5
513 #define DEFAULT_TIMEOUT         15
514 #define RECHECK                 1               /*!< Recheck every second to see we we're at the top yet */
515 #define MAX_PERIODIC_ANNOUNCEMENTS 10           /*!< The maximum periodic announcements we can have */
516 #define DEFAULT_MIN_ANNOUNCE_FREQUENCY 15       /*!< The minimum number of seconds between position announcements \
517                                                      The default value of 15 provides backwards compatibility */
518 #define MAX_QUEUE_BUCKETS 53
519
520 #define RES_OKAY        0               /*!< Action completed */
521 #define RES_EXISTS      (-1)            /*!< Entry already exists */
522 #define RES_OUTOFMEMORY (-2)            /*!< Out of memory */
523 #define RES_NOSUCHQUEUE (-3)            /*!< No such queue */
524 #define RES_NOT_DYNAMIC (-4)            /*!< Member is not dynamic */
525
526 static char *app = "Queue";
527
528 static char *app_aqm = "AddQueueMember" ;
529
530 static char *app_rqm = "RemoveQueueMember" ;
531
532 static char *app_pqm = "PauseQueueMember" ;
533
534 static char *app_upqm = "UnpauseQueueMember" ;
535
536 static char *app_ql = "QueueLog" ;
537
538 /*! \brief Persistent Members astdb family */
539 static const char *pm_family = "Queue/PersistentMembers";
540 /* The maximum length of each persistent member queue database entry */
541 #define PM_MAX_LEN 8192
542
543 /*! \brief queues.conf [general] option */
544 static int queue_keep_stats = 0;
545
546 /*! \brief queues.conf [general] option */
547 static int queue_persistent_members = 0;
548
549 /*! \brief queues.conf per-queue weight option */
550 static int use_weight = 0;
551
552 /*! \brief queues.conf [general] option */
553 static int autofill_default = 0;
554
555 /*! \brief queues.conf [general] option */
556 static int montype_default = 0;
557
558 /*! \brief queues.conf [general] option */
559 static int shared_lastcall = 0;
560
561 /*! \brief Subscription to device state change events */
562 static struct ast_event_sub *device_state_sub;
563
564 /*! \brief queues.conf [general] option */
565 static int update_cdr = 0;
566
567 enum queue_result {
568         QUEUE_UNKNOWN = 0,
569         QUEUE_TIMEOUT = 1,
570         QUEUE_JOINEMPTY = 2,
571         QUEUE_LEAVEEMPTY = 3,
572         QUEUE_JOINUNAVAIL = 4,
573         QUEUE_LEAVEUNAVAIL = 5,
574         QUEUE_FULL = 6,
575         QUEUE_CONTINUE = 7,
576 };
577
578 const struct {
579         enum queue_result id;
580         char *text;
581 } queue_results[] = {
582         { QUEUE_UNKNOWN, "UNKNOWN" },
583         { QUEUE_TIMEOUT, "TIMEOUT" },
584         { QUEUE_JOINEMPTY,"JOINEMPTY" },
585         { QUEUE_LEAVEEMPTY, "LEAVEEMPTY" },
586         { QUEUE_JOINUNAVAIL, "JOINUNAVAIL" },
587         { QUEUE_LEAVEUNAVAIL, "LEAVEUNAVAIL" },
588         { QUEUE_FULL, "FULL" },
589         { QUEUE_CONTINUE, "CONTINUE" },
590 };
591
592 enum queue_timeout_priority {
593         TIMEOUT_PRIORITY_APP,
594         TIMEOUT_PRIORITY_CONF,
595 };
596
597 /*! \brief We define a custom "local user" structure because we
598  *  use it not only for keeping track of what is in use but
599  *  also for keeping track of who we're dialing.
600  *
601  *  There are two "links" defined in this structure, q_next and call_next.
602  *  q_next links ALL defined callattempt structures into a linked list. call_next is
603  *  a link which allows for a subset of the callattempts to be traversed. This subset
604  *  is used in wait_for_answer so that irrelevant callattempts are not traversed. This
605  *  also is helpful so that queue logs are always accurate in the case where a call to 
606  *  a member times out, especially if using the ringall strategy. 
607 */
608
609 struct callattempt {
610         struct callattempt *q_next;
611         struct callattempt *call_next;
612         struct ast_channel *chan;
613         char interface[256];
614         int stillgoing;
615         int metric;
616         int oldstatus;
617         time_t lastcall;
618         struct call_queue *lastqueue;
619         struct member *member;
620 };
621
622
623 struct queue_ent {
624         struct call_queue *parent;             /*!< What queue is our parent */
625         char moh[80];                          /*!< Name of musiconhold to be used */
626         char announce[80];                     /*!< Announcement to play for member when call is answered */
627         char context[AST_MAX_CONTEXT];         /*!< Context when user exits queue */
628         char digits[AST_MAX_EXTENSION];        /*!< Digits entered while in queue */
629         int valid_digits;                      /*!< Digits entered correspond to valid extension. Exited */
630         int pos;                               /*!< Where we are in the queue */
631         int prio;                              /*!< Our priority */
632         int last_pos_said;                     /*!< Last position we told the user */
633         time_t last_periodic_announce_time;    /*!< The last time we played a periodic announcement */
634         int last_periodic_announce_sound;      /*!< The last periodic announcement we made */
635         time_t last_pos;                       /*!< Last time we told the user their position */
636         int opos;                              /*!< Where we started in the queue */
637         int handled;                           /*!< Whether our call was handled */
638         int pending;                           /*!< Non-zero if we are attempting to call a member */
639         int max_penalty;                       /*!< Limit the members that can take this call to this penalty or lower */
640         int min_penalty;                       /*!< Limit the members that can take this call to this penalty or higher */
641         int linpos;                            /*!< If using linear strategy, what position are we at? */
642         int linwrapped;                        /*!< Is the linpos wrapped? */
643         time_t start;                          /*!< When we started holding */
644         time_t expire;                         /*!< When this entry should expire (time out of queue) */
645         struct ast_channel *chan;              /*!< Our channel */
646         AST_LIST_HEAD_NOLOCK(,penalty_rule) qe_rules; /*!< Local copy of the queue's penalty rules */
647         struct penalty_rule *pr;               /*!< Pointer to the next penalty rule to implement */
648         struct queue_ent *next;                /*!< The next queue entry */
649 };
650
651 struct member {
652         char interface[80];                 /*!< Technology/Location to dial to reach this member*/
653         char state_interface[80];           /*!< Technology/Location from which to read devicestate changes */
654         char membername[80];                /*!< Member name to use in queue logs */
655         int penalty;                        /*!< Are we a last resort? */
656         int calls;                          /*!< Number of calls serviced by this member */
657         int dynamic;                        /*!< Are we dynamically added? */
658         int realtime;                       /*!< Is this member realtime? */
659         int status;                         /*!< Status of queue member */
660         int paused;                         /*!< Are we paused (not accepting calls)? */
661         time_t lastcall;                    /*!< When last successful call was hungup */
662         struct call_queue *lastqueue;       /*!< Last queue we received a call */
663         unsigned int dead:1;                /*!< Used to detect members deleted in realtime */
664         unsigned int delme:1;               /*!< Flag to delete entry on reload */
665         char rt_uniqueid[80];               /*!< Unique id of realtime member entry */
666 };
667
668 enum empty_conditions {
669         QUEUE_EMPTY_PENALTY = (1 << 0),
670         QUEUE_EMPTY_PAUSED = (1 << 1),
671         QUEUE_EMPTY_INUSE = (1 << 2),
672         QUEUE_EMPTY_RINGING = (1 << 3),
673         QUEUE_EMPTY_UNAVAILABLE = (1 << 4),
674         QUEUE_EMPTY_INVALID = (1 << 5),
675         QUEUE_EMPTY_UNKNOWN = (1 << 6),
676         QUEUE_EMPTY_WRAPUP = (1 << 7),
677 };
678
679 /* values used in multi-bit flags in call_queue */
680 #define ANNOUNCEHOLDTIME_ALWAYS 1
681 #define ANNOUNCEHOLDTIME_ONCE 2
682 #define QUEUE_EVENT_VARIABLES 3
683
684 struct penalty_rule {
685         int time;                           /*!< Number of seconds that need to pass before applying this rule */
686         int max_value;                      /*!< The amount specified in the penalty rule for max penalty */
687         int min_value;                      /*!< The amount specified in the penalty rule for min penalty */
688         int max_relative;                   /*!< Is the max adjustment relative? 1 for relative, 0 for absolute */
689         int min_relative;                   /*!< Is the min adjustment relative? 1 for relative, 0 for absolute */
690         AST_LIST_ENTRY(penalty_rule) list;  /*!< Next penalty_rule */
691 };
692
693 #define ANNOUNCEPOSITION_YES 1 /*!< We announce position */
694 #define ANNOUNCEPOSITION_NO 2 /*!< We don't announce position */
695 #define ANNOUNCEPOSITION_MORE_THAN 3 /*!< We say "Currently there are more than <limit>" */
696 #define ANNOUNCEPOSITION_LIMIT 4 /*!< We not announce position more than <limit> */
697
698 struct call_queue {
699         AST_DECLARE_STRING_FIELDS(
700                 /*! Queue name */
701                 AST_STRING_FIELD(name);
702                 /*! Music on Hold class */
703                 AST_STRING_FIELD(moh);
704                 /*! Announcement to play when call is answered */
705                 AST_STRING_FIELD(announce);
706                 /*! Exit context */
707                 AST_STRING_FIELD(context);
708                 /*! Macro to run upon member connection */
709                 AST_STRING_FIELD(membermacro);
710                 /*! Gosub to run upon member connection */
711                 AST_STRING_FIELD(membergosub);
712                 /*! Default rule to use if none specified in call to Queue() */
713                 AST_STRING_FIELD(defaultrule);
714                 /*! Sound file: "Your call is now first in line" (def. queue-youarenext) */
715                 AST_STRING_FIELD(sound_next);
716                 /*! Sound file: "There are currently" (def. queue-thereare) */
717                 AST_STRING_FIELD(sound_thereare);
718                 /*! Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting) */
719                 AST_STRING_FIELD(sound_calls);
720                 /*! Sound file: "Currently there are more than" (def. queue-quantity1) */
721                 AST_STRING_FIELD(queue_quantity1);
722                 /*! Sound file: "callers waiting to speak with a representative" (def. queue-quantity2) */
723                 AST_STRING_FIELD(queue_quantity2);
724                 /*! Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
725                 AST_STRING_FIELD(sound_holdtime);
726                 /*! Sound file: "minutes." (def. queue-minutes) */
727                 AST_STRING_FIELD(sound_minutes);
728                 /*! Sound file: "minute." (def. queue-minute) */
729                 AST_STRING_FIELD(sound_minute);
730                 /*! Sound file: "seconds." (def. queue-seconds) */
731                 AST_STRING_FIELD(sound_seconds);
732                 /*! Sound file: "Thank you for your patience." (def. queue-thankyou) */
733                 AST_STRING_FIELD(sound_thanks);
734                 /*! Sound file: Custom announce for caller, no default */
735                 AST_STRING_FIELD(sound_callerannounce);
736                 /*! Sound file: "Hold time" (def. queue-reporthold) */
737                 AST_STRING_FIELD(sound_reporthold);
738         );
739         /*! Sound files: Custom announce, no default */
740         struct ast_str *sound_periodicannounce[MAX_PERIODIC_ANNOUNCEMENTS];
741         unsigned int dead:1;
742         unsigned int eventwhencalled:2;
743         unsigned int ringinuse:1;
744         unsigned int setinterfacevar:1;
745         unsigned int setqueuevar:1;
746         unsigned int setqueueentryvar:1;
747         unsigned int reportholdtime:1;
748         unsigned int wrapped:1;
749         unsigned int timeoutrestart:1;
750         unsigned int announceholdtime:2;
751         unsigned int announceposition:3;
752         int strategy:4;
753         unsigned int maskmemberstatus:1;
754         unsigned int realtime:1;
755         unsigned int found:1;
756         enum empty_conditions joinempty;
757         enum empty_conditions leavewhenempty;
758         int announcepositionlimit;          /*!< How many positions we announce? */
759         int announcefrequency;              /*!< How often to announce their position */
760         int minannouncefrequency;           /*!< The minimum number of seconds between position announcements (def. 15) */
761         int periodicannouncefrequency;      /*!< How often to play periodic announcement */
762         int numperiodicannounce;            /*!< The number of periodic announcements configured */
763         int randomperiodicannounce;         /*!< Are periodic announcments randomly chosen */
764         int roundingseconds;                /*!< How many seconds do we round to? */
765         int holdtime;                       /*!< Current avg holdtime, based on an exponential average */
766         int callscompleted;                 /*!< Number of queue calls completed */
767         int callsabandoned;                 /*!< Number of queue calls abandoned */
768         int servicelevel;                   /*!< seconds setting for servicelevel*/
769         int callscompletedinsl;             /*!< Number of calls answered with servicelevel*/
770         char monfmt[8];                     /*!< Format to use when recording calls */
771         int montype;                        /*!< Monitor type  Monitor vs. MixMonitor */
772         int count;                          /*!< How many entries */
773         int maxlen;                         /*!< Max number of entries */
774         int wrapuptime;                     /*!< Wrapup Time */
775
776         int retry;                          /*!< Retry calling everyone after this amount of time */
777         int timeout;                        /*!< How long to wait for an answer */
778         int weight;                         /*!< Respective weight */
779         int autopause;                      /*!< Auto pause queue members if they fail to answer */
780         int timeoutpriority;                /*!< Do we allow a fraction of the timeout to occur for a ring? */
781
782         /* Queue strategy things */
783         int rrpos;                          /*!< Round Robin - position */
784         int memberdelay;                    /*!< Seconds to delay connecting member to caller */
785         int autofill;                       /*!< Ignore the head call status and ring an available agent */
786         
787         struct ao2_container *members;             /*!< Head of the list of members */
788         /*! 
789          * \brief Number of members _logged in_
790          * \note There will be members in the members container that are not logged
791          *       in, so this can not simply be replaced with ao2_container_count(). 
792          */
793         int membercount;
794         struct queue_ent *head;             /*!< Head of the list of callers */
795         AST_LIST_ENTRY(call_queue) list;    /*!< Next call queue */
796         AST_LIST_HEAD_NOLOCK(, penalty_rule) rules; /*!< The list of penalty rules to invoke */
797 };
798
799 struct rule_list {
800         char name[80];
801         AST_LIST_HEAD_NOLOCK(,penalty_rule) rules;
802         AST_LIST_ENTRY(rule_list) list;
803 };
804
805 AST_LIST_HEAD_STATIC(rule_lists, rule_list);
806
807 static struct ao2_container *queues;
808
809 static void update_realtime_members(struct call_queue *q);
810 static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused);
811
812 static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan); 
813 /*! \brief sets the QUEUESTATUS channel variable */
814 static void set_queue_result(struct ast_channel *chan, enum queue_result res)
815 {
816         int i;
817
818         for (i = 0; i < ARRAY_LEN(queue_results); i++) {
819                 if (queue_results[i].id == res) {
820                         pbx_builtin_setvar_helper(chan, "QUEUESTATUS", queue_results[i].text);
821                         return;
822                 }
823         }
824 }
825
826 static const char *int2strat(int strategy)
827 {
828         int x;
829
830         for (x = 0; x < ARRAY_LEN(strategies); x++) {
831                 if (strategy == strategies[x].strategy)
832                         return strategies[x].name;
833         }
834
835         return "<unknown>";
836 }
837
838 static int strat2int(const char *strategy)
839 {
840         int x;
841
842         for (x = 0; x < ARRAY_LEN(strategies); x++) {
843                 if (!strcasecmp(strategy, strategies[x].name))
844                         return strategies[x].strategy;
845         }
846
847         return -1;
848 }
849
850 static int queue_hash_cb(const void *obj, const int flags)
851 {
852         const struct call_queue *q = obj;
853         return ast_str_hash(q->name);
854 }
855
856 static int queue_cmp_cb(void *obj, void *arg, void *data, int flags)
857 {
858         struct call_queue *q = obj, *q2 = arg;
859         return !strcasecmp(q->name, q2->name) ? CMP_MATCH | CMP_STOP : 0;
860 }
861
862 static inline struct call_queue *queue_ref(struct call_queue *q)
863 {
864         ao2_ref(q, 1);
865         return q;
866 }
867
868 static inline struct call_queue *queue_unref(struct call_queue *q)
869 {
870         ao2_ref(q, -1);
871         return q;
872 }
873
874 /*! \brief Set variables of queue */
875 static void set_queue_variables(struct queue_ent *qe)
876 {
877         char interfacevar[256]="";
878         float sl = 0;
879
880         if (qe->parent->setqueuevar) {
881                 sl = 0;
882                 if (qe->parent->callscompleted > 0) 
883                         sl = 100 * ((float) qe->parent->callscompletedinsl / (float) qe->parent->callscompleted);
884
885                 snprintf(interfacevar, sizeof(interfacevar),
886                         "QUEUENAME=%s,QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
887                         qe->parent->name, qe->parent->maxlen, int2strat(qe->parent->strategy), qe->parent->count, qe->parent->holdtime, qe->parent->callscompleted,
888                         qe->parent->callsabandoned,  qe->parent->servicelevel, sl);
889         
890                 pbx_builtin_setvar_multiple(qe->chan, interfacevar); 
891         }
892 }
893
894 /*! \brief Insert the 'new' entry after the 'prev' entry of queue 'q' */
895 static inline void insert_entry(struct call_queue *q, struct queue_ent *prev, struct queue_ent *new, int *pos)
896 {
897         struct queue_ent *cur;
898
899         if (!q || !new)
900                 return;
901         if (prev) {
902                 cur = prev->next;
903                 prev->next = new;
904         } else {
905                 cur = q->head;
906                 q->head = new;
907         }
908         new->next = cur;
909         new->parent = q;
910         new->pos = ++(*pos);
911         new->opos = *pos;
912 }
913
914 /*! \brief Check if members are available
915  *
916  * This function checks to see if members are available to be called. If any member
917  * is available, the function immediately returns 0. If no members are available,
918  * then -1 is returned.
919  */
920 static int get_member_status(struct call_queue *q, int max_penalty, int min_penalty, enum empty_conditions conditions)
921 {
922         struct member *member;
923         struct ao2_iterator mem_iter;
924
925         ao2_lock(q);
926         mem_iter = ao2_iterator_init(q->members, 0);
927         for (; (member = ao2_iterator_next(&mem_iter)); ao2_ref(member, -1)) {
928                 if ((max_penalty && (member->penalty > max_penalty)) || (min_penalty && (member->penalty < min_penalty))) {
929                         if (conditions & QUEUE_EMPTY_PENALTY) {
930                                 ast_debug(4, "%s is unavailable because his penalty is not between %d and %d\n", member->membername, min_penalty, max_penalty);
931                                 continue;
932                         }
933                 }
934
935                 switch (member->status) {
936                 case AST_DEVICE_INVALID:
937                         if (conditions & QUEUE_EMPTY_INVALID) {
938                                 ast_debug(4, "%s is unavailable because his device state is 'invalid'\n", member->membername);
939                                 break;
940                         }
941                 case AST_DEVICE_UNAVAILABLE:
942                         if (conditions & QUEUE_EMPTY_UNAVAILABLE) {
943                                 ast_debug(4, "%s is unavailable because his device state is 'unavailable'\n", member->membername);
944                                 break;
945                         }
946                 case AST_DEVICE_INUSE:
947                         if (conditions & QUEUE_EMPTY_INUSE) {
948                                 ast_debug(4, "%s is unavailable because his device state is 'inuse'\n", member->membername);
949                                 break;
950                         }
951                 case AST_DEVICE_UNKNOWN:
952                         if (conditions & QUEUE_EMPTY_UNKNOWN) {
953                                 ast_debug(4, "%s is unavailable because his device state is 'unknown'\n", member->membername);
954                                 break;
955                         }
956                 default:
957                         if (member->paused && (conditions & QUEUE_EMPTY_PAUSED)) {
958                                 ast_debug(4, "%s is unavailable because he is paused'\n", member->membername);
959                                 break;
960                         } else if ((conditions & QUEUE_EMPTY_WRAPUP) && member->lastcall && q->wrapuptime && (time(NULL) - q->wrapuptime < member->lastcall)) {
961                                 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);
962                                 break;
963                         } else {
964                                 ao2_unlock(q);
965                                 ao2_ref(member, -1);
966                                 ast_debug(4, "%s is available.\n", member->membername);
967                                 return 0;
968                         }
969                         break;
970                 }
971         }
972
973         ao2_unlock(q);
974         return -1;
975 }
976
977 struct statechange {
978         AST_LIST_ENTRY(statechange) entry;
979         int state;
980         char dev[0];
981 };
982
983 /*! \brief set a member's status based on device state of that member's state_interface.
984  *  
985  * Lock interface list find sc, iterate through each queues queue_member list for member to
986  * update state inside queues
987 */
988 static int update_status(struct call_queue *q, struct member *m, const int status)
989 {
990         m->status = status;
991
992         if (q->maskmemberstatus)
993                 return 0;
994
995         manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
996                 "Queue: %s\r\n"
997                 "Location: %s\r\n"
998                 "MemberName: %s\r\n"
999                 "Membership: %s\r\n"
1000                 "Penalty: %d\r\n"
1001                 "CallsTaken: %d\r\n"
1002                 "LastCall: %d\r\n"
1003                 "Status: %d\r\n"
1004                 "Paused: %d\r\n",
1005                 q->name, m->interface, m->membername, m->dynamic ? "dynamic" : m->realtime ? "realtime" : "static",
1006                 m->penalty, m->calls, (int)m->lastcall, m->status, m->paused
1007         );
1008
1009         return 0;
1010 }
1011
1012 /*! \brief set a member's status based on device state of that member's interface*/
1013 static int handle_statechange(void *datap)
1014 {
1015         struct statechange *sc = datap;
1016         struct ao2_iterator miter, qiter;
1017         struct member *m;
1018         struct call_queue *q;
1019         char interface[80], *slash_pos;
1020         int found = 0;
1021
1022         qiter = ao2_iterator_init(queues, 0);
1023
1024         while ((q = ao2_iterator_next(&qiter))) {
1025                 ao2_lock(q);
1026
1027                 miter = ao2_iterator_init(q->members, 0);
1028                 for (; (m = ao2_iterator_next(&miter)); ao2_ref(m, -1)) {
1029                         ast_copy_string(interface, m->state_interface, sizeof(interface));
1030
1031                         if ((slash_pos = strchr(interface, '/')))
1032                                 if ((slash_pos = strchr(slash_pos + 1, '/')))
1033                                         *slash_pos = '\0';
1034
1035                         if (!strcasecmp(interface, sc->dev)) {
1036                                 found = 1;
1037                                 update_status(q, m, sc->state);
1038                                 ao2_ref(m, -1);
1039                                 break;
1040                         }
1041                 }
1042
1043                 ao2_unlock(q);
1044         }
1045
1046         if (found)
1047                 ast_debug(1, "Device '%s' changed to state '%d' (%s)\n", sc->dev, sc->state, ast_devstate2str(sc->state));
1048         else
1049                 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));
1050
1051         ast_free(sc);
1052         return 0;
1053 }
1054
1055 static void device_state_cb(const struct ast_event *event, void *unused)
1056 {
1057         enum ast_device_state state;
1058         const char *device;
1059         struct statechange *sc;
1060         size_t datapsize;
1061
1062         state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
1063         device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
1064
1065         if (ast_strlen_zero(device)) {
1066                 ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
1067                 return;
1068         }
1069         datapsize = sizeof(*sc) + strlen(device) + 1;
1070         if (!(sc = ast_calloc(1, datapsize))) {
1071                 ast_log(LOG_ERROR, "failed to calloc a state change struct\n");
1072                 return;
1073         }
1074         sc->state = state;
1075         strcpy(sc->dev, device);
1076         if (ast_taskprocessor_push(devicestate_tps, handle_statechange, sc) < 0) {
1077                 ast_free(sc);
1078         }
1079 }
1080
1081 /*! \brief allocate space for new queue member and set fields based on parameters passed */
1082 static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused, const char *state_interface)
1083 {
1084         struct member *cur;
1085         
1086         if ((cur = ao2_alloc(sizeof(*cur), NULL))) {
1087                 cur->penalty = penalty;
1088                 cur->paused = paused;
1089                 ast_copy_string(cur->interface, interface, sizeof(cur->interface));
1090                 if (!ast_strlen_zero(state_interface))
1091                         ast_copy_string(cur->state_interface, state_interface, sizeof(cur->state_interface));
1092                 else
1093                         ast_copy_string(cur->state_interface, interface, sizeof(cur->state_interface));
1094                 if (!ast_strlen_zero(membername))
1095                         ast_copy_string(cur->membername, membername, sizeof(cur->membername));
1096                 else
1097                         ast_copy_string(cur->membername, interface, sizeof(cur->membername));
1098                 if (!strchr(cur->interface, '/'))
1099                         ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
1100                 cur->status = ast_device_state(cur->state_interface);
1101         }
1102
1103         return cur;
1104 }
1105
1106
1107 static int compress_char(const char c)
1108 {
1109         if (c < 32)
1110                 return 0;
1111         else if (c > 96)
1112                 return c - 64;
1113         else
1114                 return c - 32;
1115 }
1116
1117 static int member_hash_fn(const void *obj, const int flags)
1118 {
1119         const struct member *mem = obj;
1120         const char *chname = strchr(mem->interface, '/');
1121         int ret = 0, i;
1122         if (!chname)
1123                 chname = mem->interface;
1124         for (i = 0; i < 5 && chname[i]; i++)
1125                 ret += compress_char(chname[i]) << (i * 6);
1126         return ret;
1127 }
1128
1129 static int member_cmp_fn(void *obj1, void *obj2, void *data, int flags)
1130 {
1131         struct member *mem1 = obj1, *mem2 = obj2;
1132         return strcasecmp(mem1->interface, mem2->interface) ? 0 : CMP_MATCH | CMP_STOP;
1133 }
1134
1135 /*! 
1136  * \brief Initialize Queue default values.
1137  * \note the queue's lock  must be held before executing this function
1138 */
1139 static void init_queue(struct call_queue *q)
1140 {
1141         int i;
1142         struct penalty_rule *pr_iter;
1143
1144         q->dead = 0;
1145         q->retry = DEFAULT_RETRY;
1146         q->timeout = -1;
1147         q->maxlen = 0;
1148         q->announcefrequency = 0;
1149         q->minannouncefrequency = DEFAULT_MIN_ANNOUNCE_FREQUENCY;
1150         q->announceholdtime = 1;
1151         q->announcepositionlimit = 10; /* Default 10 positions */
1152         q->announceposition = ANNOUNCEPOSITION_YES; /* Default yes */
1153         q->roundingseconds = 0; /* Default - don't announce seconds */
1154         q->servicelevel = 0;
1155         q->ringinuse = 1;
1156         q->setinterfacevar = 0;
1157         q->setqueuevar = 0;
1158         q->setqueueentryvar = 0;
1159         q->autofill = autofill_default;
1160         q->montype = montype_default;
1161         q->monfmt[0] = '\0';
1162         q->reportholdtime = 0;
1163         q->wrapuptime = 0;
1164         q->joinempty = 0;
1165         q->leavewhenempty = 0;
1166         q->memberdelay = 0;
1167         q->maskmemberstatus = 0;
1168         q->eventwhencalled = 0;
1169         q->weight = 0;
1170         q->timeoutrestart = 0;
1171         q->periodicannouncefrequency = 0;
1172         q->randomperiodicannounce = 0;
1173         q->numperiodicannounce = 0;
1174         q->timeoutpriority = TIMEOUT_PRIORITY_APP;
1175         if (!q->members) {
1176                 if (q->strategy == QUEUE_STRATEGY_LINEAR)
1177                         /* linear strategy depends on order, so we have to place all members in a single bucket */
1178                         q->members = ao2_container_alloc(1, member_hash_fn, member_cmp_fn);
1179                 else
1180                         q->members = ao2_container_alloc(37, member_hash_fn, member_cmp_fn);
1181         }
1182         q->membercount = 0;
1183         q->found = 1;
1184
1185         ast_string_field_set(q, sound_next, "queue-youarenext");
1186         ast_string_field_set(q, sound_thereare, "queue-thereare");
1187         ast_string_field_set(q, sound_calls, "queue-callswaiting");
1188         ast_string_field_set(q, queue_quantity1, "queue-quantity1");
1189         ast_string_field_set(q, queue_quantity2, "queue-quantity2");
1190         ast_string_field_set(q, sound_holdtime, "queue-holdtime");
1191         ast_string_field_set(q, sound_minutes, "queue-minutes");
1192         ast_string_field_set(q, sound_minute, "queue-minute");
1193         ast_string_field_set(q, sound_seconds, "queue-seconds");
1194         ast_string_field_set(q, sound_thanks, "queue-thankyou");
1195         ast_string_field_set(q, sound_reporthold, "queue-reporthold");
1196
1197         if ((q->sound_periodicannounce[0] = ast_str_create(32)))
1198                 ast_str_set(&q->sound_periodicannounce[0], 0, "queue-periodic-announce");
1199
1200         for (i = 1; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
1201                 if (q->sound_periodicannounce[i])
1202                         ast_str_set(&q->sound_periodicannounce[i], 0, "%s", "");
1203         }
1204
1205         while ((pr_iter = AST_LIST_REMOVE_HEAD(&q->rules,list)))
1206                 ast_free(pr_iter);
1207 }
1208
1209 static void clear_queue(struct call_queue *q)
1210 {
1211         q->holdtime = 0;
1212         q->callscompleted = 0;
1213         q->callsabandoned = 0;
1214         q->callscompletedinsl = 0;
1215         q->wrapuptime = 0;
1216 }
1217
1218 /*! 
1219  * \brief Change queue penalty by adding rule.
1220  *
1221  * Check rule for errors with time or fomatting, see if rule is relative to rest 
1222  * of queue, iterate list of rules to find correct insertion point, insert and return.
1223  * \retval -1 on failure
1224  * \retval 0 on success 
1225  * \note Call this with the rule_lists locked 
1226 */
1227 static int insert_penaltychange (const char *list_name, const char *content, const int linenum)
1228 {
1229         char *timestr, *maxstr, *minstr, *contentdup;
1230         struct penalty_rule *rule = NULL, *rule_iter;
1231         struct rule_list *rl_iter;
1232         int penaltychangetime, inserted = 0;
1233
1234         if (!(rule = ast_calloc(1, sizeof(*rule)))) {
1235                 ast_log(LOG_ERROR, "Cannot allocate memory for penaltychange rule at line %d!\n", linenum);
1236                 return -1;
1237         }
1238
1239         contentdup = ast_strdupa(content);
1240         
1241         if (!(maxstr = strchr(contentdup, ','))) {
1242                 ast_log(LOG_WARNING, "Improperly formatted penaltychange rule at line %d. Ignoring.\n", linenum);
1243                 ast_free(rule);
1244                 return -1;
1245         }
1246
1247         *maxstr++ = '\0';
1248         timestr = contentdup;
1249
1250         if ((penaltychangetime = atoi(timestr)) < 0) {
1251                 ast_log(LOG_WARNING, "Improper time parameter specified for penaltychange rule at line %d. Ignoring.\n", linenum);
1252                 ast_free(rule);
1253                 return -1;
1254         }
1255
1256         rule->time = penaltychangetime;
1257
1258         if ((minstr = strchr(maxstr,',')))
1259                 *minstr++ = '\0';
1260         
1261         /* The last check will evaluate true if either no penalty change is indicated for a given rule
1262          * OR if a min penalty change is indicated but no max penalty change is */
1263         if (*maxstr == '+' || *maxstr == '-' || *maxstr == '\0') {
1264                 rule->max_relative = 1;
1265         }
1266
1267         rule->max_value = atoi(maxstr);
1268
1269         if (!ast_strlen_zero(minstr)) {
1270                 if (*minstr == '+' || *minstr == '-')
1271                         rule->min_relative = 1;
1272                 rule->min_value = atoi(minstr);
1273         } else /*there was no minimum specified, so assume this means no change*/
1274                 rule->min_relative = 1;
1275
1276         /*We have the rule made, now we need to insert it where it belongs*/
1277         AST_LIST_TRAVERSE(&rule_lists, rl_iter, list){
1278                 if (strcasecmp(rl_iter->name, list_name))
1279                         continue;
1280
1281                 AST_LIST_TRAVERSE_SAFE_BEGIN(&rl_iter->rules, rule_iter, list) {
1282                         if (rule->time < rule_iter->time) {
1283                                 AST_LIST_INSERT_BEFORE_CURRENT(rule, list);
1284                                 inserted = 1;
1285                                 break;
1286                         }
1287                 }
1288                 AST_LIST_TRAVERSE_SAFE_END;
1289         
1290                 if (!inserted) {
1291                         AST_LIST_INSERT_TAIL(&rl_iter->rules, rule, list);
1292                 }
1293         }
1294
1295         return 0;
1296 }
1297
1298 static void parse_empty_options(const char *value, enum empty_conditions *empty)
1299 {
1300         char *value_copy = ast_strdupa(value);
1301         char *option = NULL;
1302         while ((option = strsep(&value_copy, ","))) {
1303                 if (!strcasecmp(option, "paused")) {
1304                         *empty |= QUEUE_EMPTY_PAUSED;
1305                 } else if (!strcasecmp(option, "penalty")) {
1306                         *empty |= QUEUE_EMPTY_PENALTY;
1307                 } else if (!strcasecmp(option, "inuse")) {
1308                         *empty |= QUEUE_EMPTY_INUSE;
1309                 } else if (!strcasecmp(option, "ringing")) {
1310                         *empty |= QUEUE_EMPTY_RINGING;
1311                 } else if (!strcasecmp(option, "invalid")) {
1312                         *empty |= QUEUE_EMPTY_INVALID;
1313                 } else if (!strcasecmp(option, "wrapup")) {
1314                         *empty |= QUEUE_EMPTY_WRAPUP;
1315                 } else if (!strcasecmp(option, "unavailable")) {
1316                         *empty |= QUEUE_EMPTY_UNAVAILABLE;
1317                 } else if (!strcasecmp(option, "unknown")) {
1318                         *empty |= QUEUE_EMPTY_UNKNOWN;
1319                 } else if (!strcasecmp(option, "loose")) {
1320                         *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID);
1321                 } else if (!strcasecmp(option, "strict")) {
1322                         *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED | QUEUE_EMPTY_UNAVAILABLE);
1323                 } else if (ast_false(option)) {
1324                         *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED);
1325                 } else if (ast_true(option)) {
1326                         *empty = 0;
1327                 }
1328         }
1329 }
1330
1331 /*! \brief Configure a queue parameter.
1332  * 
1333  * The failunknown flag is set for config files (and static realtime) to show
1334  * errors for unknown parameters. It is cleared for dynamic realtime to allow
1335  *  extra fields in the tables.
1336  * \note For error reporting, line number is passed for .conf static configuration,
1337  * for Realtime queues, linenum is -1.
1338 */
1339 static void queue_set_param(struct call_queue *q, const char *param, const char *val, int linenum, int failunknown)
1340 {
1341         if (!strcasecmp(param, "musicclass") || 
1342                 !strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
1343                 ast_string_field_set(q, moh, val);
1344         } else if (!strcasecmp(param, "announce")) {
1345                 ast_string_field_set(q, announce, val);
1346         } else if (!strcasecmp(param, "context")) {
1347                 ast_string_field_set(q, context, val);
1348         } else if (!strcasecmp(param, "timeout")) {
1349                 q->timeout = atoi(val);
1350                 if (q->timeout < 0)
1351                         q->timeout = DEFAULT_TIMEOUT;
1352         } else if (!strcasecmp(param, "ringinuse")) {
1353                 q->ringinuse = ast_true(val);
1354         } else if (!strcasecmp(param, "setinterfacevar")) {
1355                 q->setinterfacevar = ast_true(val);
1356         } else if (!strcasecmp(param, "setqueuevar")) {
1357                 q->setqueuevar = ast_true(val);
1358         } else if (!strcasecmp(param, "setqueueentryvar")) {
1359                 q->setqueueentryvar = ast_true(val);
1360         } else if (!strcasecmp(param, "monitor-format")) {
1361                 ast_copy_string(q->monfmt, val, sizeof(q->monfmt));
1362         } else if (!strcasecmp(param, "membermacro")) {
1363                 ast_string_field_set(q, membermacro, val);
1364         } else if (!strcasecmp(param, "membergosub")) {
1365                 ast_string_field_set(q, membergosub, val);
1366         } else if (!strcasecmp(param, "queue-youarenext")) {
1367                 ast_string_field_set(q, sound_next, val);
1368         } else if (!strcasecmp(param, "queue-thereare")) {
1369                 ast_string_field_set(q, sound_thereare, val);
1370         } else if (!strcasecmp(param, "queue-callswaiting")) {
1371                 ast_string_field_set(q, sound_calls, val);
1372         } else if (!strcasecmp(param, "queue-quantity1")) {
1373                 ast_string_field_set(q, queue_quantity1, val);
1374         } else if (!strcasecmp(param, "queue-quantity2")) {
1375                 ast_string_field_set(q, queue_quantity2, val);
1376         } else if (!strcasecmp(param, "queue-holdtime")) {
1377                 ast_string_field_set(q, sound_holdtime, val);
1378         } else if (!strcasecmp(param, "queue-minutes")) {
1379                 ast_string_field_set(q, sound_minutes, val);
1380         } else if (!strcasecmp(param, "queue-minute")) {
1381                 ast_string_field_set(q, sound_minute, val);
1382         } else if (!strcasecmp(param, "queue-seconds")) {
1383                 ast_string_field_set(q, sound_seconds, val);
1384         } else if (!strcasecmp(param, "queue-thankyou")) {
1385                 ast_string_field_set(q, sound_thanks, val);
1386         } else if (!strcasecmp(param, "queue-callerannounce")) {
1387                 ast_string_field_set(q, sound_callerannounce, val);
1388         } else if (!strcasecmp(param, "queue-reporthold")) {
1389                 ast_string_field_set(q, sound_reporthold, val);
1390         } else if (!strcasecmp(param, "announce-frequency")) {
1391                 q->announcefrequency = atoi(val);
1392         } else if (!strcasecmp(param, "min-announce-frequency")) {
1393                 q->minannouncefrequency = atoi(val);
1394                 ast_debug(1, "%s=%s for queue '%s'\n", param, val, q->name);
1395         } else if (!strcasecmp(param, "announce-round-seconds")) {
1396                 q->roundingseconds = atoi(val);
1397                 /* Rounding to any other values just doesn't make sense... */
1398                 if (!(q->roundingseconds == 0 || q->roundingseconds == 5 || q->roundingseconds == 10
1399                         || q->roundingseconds == 15 || q->roundingseconds == 20 || q->roundingseconds == 30)) {
1400                         if (linenum >= 0) {
1401                                 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
1402                                         "using 0 instead for queue '%s' at line %d of queues.conf\n",
1403                                         val, param, q->name, linenum);
1404                         } else {
1405                                 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
1406                                         "using 0 instead for queue '%s'\n", val, param, q->name);
1407                         }
1408                         q->roundingseconds=0;
1409                 }
1410         } else if (!strcasecmp(param, "announce-holdtime")) {
1411                 if (!strcasecmp(val, "once"))
1412                         q->announceholdtime = ANNOUNCEHOLDTIME_ONCE;
1413                 else if (ast_true(val))
1414                         q->announceholdtime = ANNOUNCEHOLDTIME_ALWAYS;
1415                 else
1416                         q->announceholdtime = 0;
1417         } else if (!strcasecmp(param, "announce-position")) {
1418                 if (!strcasecmp(val, "limit"))
1419                         q->announceposition = ANNOUNCEPOSITION_LIMIT;
1420                 else if (!strcasecmp(val, "more"))
1421                         q->announceposition = ANNOUNCEPOSITION_MORE_THAN;
1422                 else if (ast_true(val))
1423                         q->announceposition = ANNOUNCEPOSITION_YES;
1424                 else
1425                         q->announceposition = ANNOUNCEPOSITION_NO;
1426         } else if (!strcasecmp(param, "announce-position-limit")) {
1427                 q->announcepositionlimit = atoi(val);
1428         } else if (!strcasecmp(param, "periodic-announce")) {
1429                 if (strchr(val, ',')) {
1430                         char *s, *buf = ast_strdupa(val);
1431                         unsigned int i = 0;
1432
1433                         while ((s = strsep(&buf, ",|"))) {
1434                                 if (!q->sound_periodicannounce[i])
1435                                         q->sound_periodicannounce[i] = ast_str_create(16);
1436                                 ast_str_set(&q->sound_periodicannounce[i], 0, "%s", s);
1437                                 i++;
1438                                 if (i == MAX_PERIODIC_ANNOUNCEMENTS)
1439                                         break;
1440                         }
1441                         q->numperiodicannounce = i;
1442                 } else {
1443                         ast_str_set(&q->sound_periodicannounce[0], 0, "%s", val);
1444                         q->numperiodicannounce = 1;
1445                 }
1446         } else if (!strcasecmp(param, "periodic-announce-frequency")) {
1447                 q->periodicannouncefrequency = atoi(val);
1448         } else if (!strcasecmp(param, "random-periodic-announce")) {
1449                 q->randomperiodicannounce = ast_true(val);
1450         } else if (!strcasecmp(param, "retry")) {
1451                 q->retry = atoi(val);
1452                 if (q->retry <= 0)
1453                         q->retry = DEFAULT_RETRY;
1454         } else if (!strcasecmp(param, "wrapuptime")) {
1455                 q->wrapuptime = atoi(val);
1456         } else if (!strcasecmp(param, "autofill")) {
1457                 q->autofill = ast_true(val);
1458         } else if (!strcasecmp(param, "monitor-type")) {
1459                 if (!strcasecmp(val, "mixmonitor"))
1460                         q->montype = 1;
1461         } else if (!strcasecmp(param, "autopause")) {
1462                 q->autopause = ast_true(val);
1463         } else if (!strcasecmp(param, "maxlen")) {
1464                 q->maxlen = atoi(val);
1465                 if (q->maxlen < 0)
1466                         q->maxlen = 0;
1467         } else if (!strcasecmp(param, "servicelevel")) {
1468                 q->servicelevel= atoi(val);
1469         } else if (!strcasecmp(param, "strategy")) {
1470                 /* We already have set this, no need to do it again */
1471                 return;
1472         } else if (!strcasecmp(param, "joinempty")) {
1473                 parse_empty_options(val, &q->joinempty);
1474         } else if (!strcasecmp(param, "leavewhenempty")) {
1475                 parse_empty_options(val, &q->leavewhenempty);
1476         } else if (!strcasecmp(param, "eventmemberstatus")) {
1477                 q->maskmemberstatus = !ast_true(val);
1478         } else if (!strcasecmp(param, "eventwhencalled")) {
1479                 if (!strcasecmp(val, "vars")) {
1480                         q->eventwhencalled = QUEUE_EVENT_VARIABLES;
1481                 } else {
1482                         q->eventwhencalled = ast_true(val) ? 1 : 0;
1483                 }
1484         } else if (!strcasecmp(param, "reportholdtime")) {
1485                 q->reportholdtime = ast_true(val);
1486         } else if (!strcasecmp(param, "memberdelay")) {
1487                 q->memberdelay = atoi(val);
1488         } else if (!strcasecmp(param, "weight")) {
1489                 q->weight = atoi(val);
1490                 if (q->weight)
1491                         use_weight++;
1492                 /* With Realtime queues, if the last queue using weights is deleted in realtime,
1493                    we will not see any effect on use_weight until next reload. */
1494         } else if (!strcasecmp(param, "timeoutrestart")) {
1495                 q->timeoutrestart = ast_true(val);
1496         } else if (!strcasecmp(param, "defaultrule")) {
1497                 ast_string_field_set(q, defaultrule, val);
1498         } else if (!strcasecmp(param, "timeoutpriority")) {
1499                 if (!strcasecmp(val, "conf")) {
1500                         q->timeoutpriority = TIMEOUT_PRIORITY_CONF;
1501                 } else {
1502                         q->timeoutpriority = TIMEOUT_PRIORITY_APP;
1503                 }
1504         } else if (failunknown) {
1505                 if (linenum >= 0) {
1506                         ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queues.conf\n",
1507                                 q->name, param, linenum);
1508                 } else {
1509                         ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s\n", q->name, param);
1510                 }
1511         }
1512 }
1513
1514 /*!
1515  * \brief Find rt member record to update otherwise create one.
1516  *
1517  * Search for member in queue, if found update penalty/paused state,
1518  * if no memeber exists create one flag it as a RT member and add to queue member list. 
1519 */
1520 static void rt_handle_member_record(struct call_queue *q, char *interface, const char *rt_uniqueid, const char *membername, const char *penalty_str, const char *paused_str, const char* state_interface)
1521 {
1522         struct member *m;
1523         struct ao2_iterator mem_iter;
1524         int penalty = 0;
1525         int paused  = 0;
1526         int found = 0;
1527
1528         if (penalty_str) {
1529                 penalty = atoi(penalty_str);
1530                 if (penalty < 0)
1531                         penalty = 0;
1532         }
1533
1534         if (paused_str) {
1535                 paused = atoi(paused_str);
1536                 if (paused < 0)
1537                         paused = 0;
1538         }
1539
1540         /* Find member by realtime uniqueid and update */
1541         mem_iter = ao2_iterator_init(q->members, 0);
1542         while ((m = ao2_iterator_next(&mem_iter))) {
1543                 if (!strcasecmp(m->rt_uniqueid, rt_uniqueid)) {
1544                         m->dead = 0;    /* Do not delete this one. */
1545                         ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
1546                         if (paused_str)
1547                                 m->paused = paused;
1548                         if (strcasecmp(state_interface, m->state_interface)) {
1549                                 ast_copy_string(m->state_interface, state_interface, sizeof(m->state_interface));
1550                         }          
1551                         m->penalty = penalty;
1552                         found = 1;
1553                         ao2_ref(m, -1);
1554                         break;
1555                 }
1556                 ao2_ref(m, -1);
1557         }
1558
1559         /* Create a new member */
1560         if (!found) {
1561                 if ((m = create_queue_member(interface, membername, penalty, paused, state_interface))) {
1562                         m->dead = 0;
1563                         m->realtime = 1;
1564                         ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
1565                         ast_queue_log(q->name, "REALTIME", m->interface, "ADDMEMBER", "%s", "");
1566                         ao2_link(q->members, m);
1567                         ao2_ref(m, -1);
1568                         m = NULL;
1569                         q->membercount++;
1570                 }
1571         }
1572 }
1573
1574 /*! \brief Iterate through queue's member list and delete them */
1575 static void free_members(struct call_queue *q, int all)
1576 {
1577         /* Free non-dynamic members */
1578         struct member *cur;
1579         struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
1580
1581         while ((cur = ao2_iterator_next(&mem_iter))) {
1582                 if (all || !cur->dynamic) {
1583                         ao2_unlink(q->members, cur);
1584                         q->membercount--;
1585                 }
1586                 ao2_ref(cur, -1);
1587         }
1588 }
1589
1590 /*! \brief Free queue's member list then its string fields */
1591 static void destroy_queue(void *obj)
1592 {
1593         struct call_queue *q = obj;
1594         int i;
1595
1596         free_members(q, 1);
1597         ast_string_field_free_memory(q);
1598         for (i = 0; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
1599                 if (q->sound_periodicannounce[i])
1600                         free(q->sound_periodicannounce[i]);
1601         }
1602         ao2_ref(q->members, -1);
1603 }
1604
1605 static struct call_queue *alloc_queue(const char *queuename)
1606 {
1607         struct call_queue *q;
1608
1609         if ((q = ao2_alloc(sizeof(*q), destroy_queue))) {
1610                 if (ast_string_field_init(q, 64)) {
1611                         free(q);
1612                         return NULL;
1613                 }
1614                 ast_string_field_set(q, name, queuename);
1615         }
1616         return q;
1617 }
1618
1619 /*!
1620  * \brief Reload a single queue via realtime.
1621  *
1622  * Check for statically defined queue first, check if deleted RT queue,
1623  * check for new RT queue, if queue vars are not defined init them with defaults.
1624  * reload RT queue vars, set RT queue members dead and reload them, return finished queue.
1625  * \retval the queue, 
1626  * \retval NULL if it doesn't exist.
1627  * \note Should be called with the "queues" container locked. 
1628 */
1629 static struct call_queue *find_queue_by_name_rt(const char *queuename, struct ast_variable *queue_vars, struct ast_config *member_config)
1630 {
1631         struct ast_variable *v;
1632         struct call_queue *q, tmpq = {
1633                 .name = queuename,      
1634         };
1635         struct member *m;
1636         struct ao2_iterator mem_iter;
1637         char *interface = NULL;
1638         const char *tmp_name;
1639         char *tmp;
1640         char tmpbuf[64];        /* Must be longer than the longest queue param name. */
1641
1642         /* Static queues override realtime. */
1643         if ((q = ao2_find(queues, &tmpq, NULL, OBJ_POINTER))) {
1644                 ao2_lock(q);
1645                 if (!q->realtime) {
1646                         if (q->dead) {
1647                                 ao2_unlock(q);
1648                                 queue_unref(q);
1649                                 return NULL;
1650                         } else {
1651                                 ast_log(LOG_WARNING, "Static queue '%s' already exists. Not loading from realtime\n", q->name);
1652                                 ao2_unlock(q);
1653                                 return q;
1654                         }
1655                 }
1656                 queue_unref(q);
1657         } else if (!member_config)
1658                 /* Not found in the list, and it's not realtime ... */
1659                 return NULL;
1660
1661         /* Check if queue is defined in realtime. */
1662         if (!queue_vars) {
1663                 /* Delete queue from in-core list if it has been deleted in realtime. */
1664                 if (q) {
1665                         /*! \note Hmm, can't seem to distinguish a DB failure from a not
1666                            found condition... So we might delete an in-core queue
1667                            in case of DB failure. */
1668                         ast_debug(1, "Queue %s not found in realtime.\n", queuename);
1669
1670                         q->dead = 1;
1671                         /* Delete if unused (else will be deleted when last caller leaves). */
1672                         ao2_unlink(queues, q);
1673                         ao2_unlock(q);
1674                         queue_unref(q);
1675                 }
1676                 return NULL;
1677         }
1678
1679         /* Create a new queue if an in-core entry does not exist yet. */
1680         if (!q) {
1681                 struct ast_variable *tmpvar = NULL;
1682                 if (!(q = alloc_queue(queuename)))
1683                         return NULL;
1684                 ao2_lock(q);
1685                 clear_queue(q);
1686                 q->realtime = 1;
1687                 /*Before we initialize the queue, we need to set the strategy, so that linear strategy
1688                  * will allocate the members properly
1689                  */
1690                 for (tmpvar = queue_vars; tmpvar; tmpvar = tmpvar->next) {
1691                         if (!strcasecmp(tmpvar->name, "strategy")) {
1692                                 q->strategy = strat2int(tmpvar->value);
1693                                 if (q->strategy < 0) {
1694                                         ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
1695                                         tmpvar->value, q->name);
1696                                         q->strategy = QUEUE_STRATEGY_RINGALL;
1697                                 }
1698                                 break;
1699                         }
1700                 }
1701                 /* We traversed all variables and didn't find a strategy */
1702                 if (!tmpvar)
1703                         q->strategy = QUEUE_STRATEGY_RINGALL;
1704                 ao2_link(queues, q);
1705         }
1706         init_queue(q);          /* Ensure defaults for all parameters not set explicitly. */
1707
1708         memset(tmpbuf, 0, sizeof(tmpbuf));
1709         for (v = queue_vars; v; v = v->next) {
1710                 /* Convert to dashes `-' from underscores `_' as the latter are more SQL friendly. */
1711                 if ((tmp = strchr(v->name, '_'))) {
1712                         ast_copy_string(tmpbuf, v->name, sizeof(tmpbuf));
1713                         tmp_name = tmpbuf;
1714                         tmp = tmpbuf;
1715                         while ((tmp = strchr(tmp, '_')))
1716                                 *tmp++ = '-';
1717                 } else
1718                         tmp_name = v->name;
1719
1720                 if (!ast_strlen_zero(v->value)) {
1721                         /* Don't want to try to set the option if the value is empty */
1722                         queue_set_param(q, tmp_name, v->value, -1, 0);
1723                 }
1724         }
1725
1726         /* Temporarily set realtime members dead so we can detect deleted ones. 
1727          * Also set the membercount correctly for realtime*/
1728         mem_iter = ao2_iterator_init(q->members, 0);
1729         while ((m = ao2_iterator_next(&mem_iter))) {
1730                 q->membercount++;
1731                 if (m->realtime)
1732                         m->dead = 1;
1733                 ao2_ref(m, -1);
1734         }
1735
1736         while ((interface = ast_category_browse(member_config, interface))) {
1737                 rt_handle_member_record(q, interface,
1738                         ast_variable_retrieve(member_config, interface, "uniqueid"),
1739                         S_OR(ast_variable_retrieve(member_config, interface, "membername"),interface),
1740                         ast_variable_retrieve(member_config, interface, "penalty"),
1741                         ast_variable_retrieve(member_config, interface, "paused"),
1742                         S_OR(ast_variable_retrieve(member_config, interface, "state_interface"),interface));
1743         }
1744
1745         /* Delete all realtime members that have been deleted in DB. */
1746         mem_iter = ao2_iterator_init(q->members, 0);
1747         while ((m = ao2_iterator_next(&mem_iter))) {
1748                 if (m->dead) {
1749                         ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
1750                         ao2_unlink(q->members, m);
1751                         q->membercount--;
1752                 }
1753                 ao2_ref(m, -1);
1754         }
1755
1756         ao2_unlock(q);
1757
1758         return q;
1759 }
1760
1761 static struct call_queue *load_realtime_queue(const char *queuename)
1762 {
1763         struct ast_variable *queue_vars;
1764         struct ast_config *member_config = NULL;
1765         struct call_queue *q = NULL, tmpq = {
1766                 .name = queuename,      
1767         };
1768
1769         /* Find the queue in the in-core list first. */
1770         q = ao2_find(queues, &tmpq, NULL, OBJ_POINTER);
1771
1772         if (!q || q->realtime) {
1773                 /*! \note Load from realtime before taking the "queues" container lock, to avoid blocking all
1774                    queue operations while waiting for the DB.
1775
1776                    This will be two separate database transactions, so we might
1777                    see queue parameters as they were before another process
1778                    changed the queue and member list as it was after the change.
1779                    Thus we might see an empty member list when a queue is
1780                    deleted. In practise, this is unlikely to cause a problem. */
1781
1782                 queue_vars = ast_load_realtime("queues", "name", queuename, SENTINEL);
1783                 if (queue_vars) {
1784                         member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", queuename, SENTINEL);
1785                         if (!member_config) {
1786                                 ast_log(LOG_ERROR, "no queue_members defined in your config (extconfig.conf).\n");
1787                                 ast_variables_destroy(queue_vars);
1788                                 return NULL;
1789                         }
1790                 }
1791
1792                 ao2_lock(queues);
1793                 q = find_queue_by_name_rt(queuename, queue_vars, member_config);
1794                 if (member_config)
1795                         ast_config_destroy(member_config);
1796                 if (queue_vars)
1797                         ast_variables_destroy(queue_vars);
1798                 ao2_unlock(queues);
1799
1800         } else {
1801                 update_realtime_members(q);
1802         }
1803         return q;
1804 }
1805
1806 static int update_realtime_member_field(struct member *mem, const char *queue_name, const char *field, const char *value)
1807 {
1808         int ret = -1;
1809
1810         if (ast_strlen_zero(mem->rt_uniqueid))
1811                 return ret;
1812
1813         if ((ast_update_realtime("queue_members", "uniqueid", mem->rt_uniqueid, field, value, SENTINEL)) > 0)
1814                 ret = 0;
1815
1816         return ret;
1817 }
1818
1819
1820 static void update_realtime_members(struct call_queue *q)
1821 {
1822         struct ast_config *member_config = NULL;
1823         struct member *m;
1824         char *interface = NULL;
1825         struct ao2_iterator mem_iter;
1826
1827         if (!(member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", q->name , SENTINEL))) {
1828                 /*This queue doesn't have realtime members*/
1829                 ast_debug(3, "Queue %s has no realtime members defined. No need for update\n", q->name);
1830                 return;
1831         }
1832
1833         ao2_lock(queues);
1834         ao2_lock(q);
1835         
1836         /* Temporarily set realtime  members dead so we can detect deleted ones.*/ 
1837         mem_iter = ao2_iterator_init(q->members, 0);
1838         while ((m = ao2_iterator_next(&mem_iter))) {
1839                 if (m->realtime)
1840                         m->dead = 1;
1841                 ao2_ref(m, -1);
1842         }
1843
1844         while ((interface = ast_category_browse(member_config, interface))) {
1845                 rt_handle_member_record(q, interface,
1846                         ast_variable_retrieve(member_config, interface, "uniqueid"),
1847                         S_OR(ast_variable_retrieve(member_config, interface, "membername"), interface),
1848                         ast_variable_retrieve(member_config, interface, "penalty"),
1849                         ast_variable_retrieve(member_config, interface, "paused"),
1850                         S_OR(ast_variable_retrieve(member_config, interface, "state_interface"), interface));
1851         }
1852
1853         /* Delete all realtime members that have been deleted in DB. */
1854         mem_iter = ao2_iterator_init(q->members, 0);
1855         while ((m = ao2_iterator_next(&mem_iter))) {
1856                 if (m->dead) {
1857                         ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
1858                         ao2_unlink(q->members, m);
1859                         q->membercount--;
1860                 }
1861                 ao2_ref(m, -1);
1862         }
1863         ao2_unlock(q);
1864         ao2_unlock(queues);
1865         ast_config_destroy(member_config);
1866 }
1867
1868 static int join_queue(char *queuename, struct queue_ent *qe, enum queue_result *reason)
1869 {
1870         struct call_queue *q;
1871         struct queue_ent *cur, *prev = NULL;
1872         int res = -1;
1873         int pos = 0;
1874         int inserted = 0;
1875
1876         if (!(q = load_realtime_queue(queuename)))
1877                 return res;
1878
1879         ao2_lock(queues);
1880         ao2_lock(q);
1881
1882         /* This is our one */
1883         if (q->joinempty) {
1884                 int status = 0;
1885                 if ((status = get_member_status(q, qe->max_penalty, qe->min_penalty, q->joinempty))) {
1886                         *reason = QUEUE_JOINEMPTY;
1887                         ao2_unlock(q);
1888                         ao2_unlock(queues);
1889                         return res;
1890                 }
1891         }
1892         if (*reason == QUEUE_UNKNOWN && q->maxlen && (q->count >= q->maxlen))
1893                 *reason = QUEUE_FULL;
1894         else if (*reason == QUEUE_UNKNOWN) {
1895                 /* There's space for us, put us at the right position inside
1896                  * the queue.
1897                  * Take into account the priority of the calling user */
1898                 inserted = 0;
1899                 prev = NULL;
1900                 cur = q->head;
1901                 while (cur) {
1902                         /* We have higher priority than the current user, enter
1903                          * before him, after all the other users with priority
1904                          * higher or equal to our priority. */
1905                         if ((!inserted) && (qe->prio > cur->prio)) {
1906                                 insert_entry(q, prev, qe, &pos);
1907                                 inserted = 1;
1908                         }
1909                         cur->pos = ++pos;
1910                         prev = cur;
1911                         cur = cur->next;
1912                 }
1913                 /* No luck, join at the end of the queue */
1914                 if (!inserted)
1915                         insert_entry(q, prev, qe, &pos);
1916                 ast_copy_string(qe->moh, q->moh, sizeof(qe->moh));
1917                 ast_copy_string(qe->announce, q->announce, sizeof(qe->announce));
1918                 ast_copy_string(qe->context, q->context, sizeof(qe->context));
1919                 q->count++;
1920                 res = 0;
1921                 manager_event(EVENT_FLAG_CALL, "Join",
1922                         "Channel: %s\r\nCallerID: %s\r\nCallerIDName: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\nUniqueid: %s\r\n",
1923                         qe->chan->name,
1924                         S_OR(qe->chan->cid.cid_num, "unknown"), /* XXX somewhere else it is <unknown> */
1925                         S_OR(qe->chan->cid.cid_name, "unknown"),
1926                         q->name, qe->pos, q->count, qe->chan->uniqueid );
1927                 ast_debug(1, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
1928         }
1929         ao2_unlock(q);
1930         ao2_unlock(queues);
1931
1932         return res;
1933 }
1934
1935 static int play_file(struct ast_channel *chan, const char *filename)
1936 {
1937         int res;
1938
1939         ast_stopstream(chan);
1940
1941         res = ast_streamfile(chan, filename, chan->language);
1942         if (!res)
1943                 res = ast_waitstream(chan, AST_DIGIT_ANY);
1944
1945         ast_stopstream(chan);
1946
1947         return res;
1948 }
1949
1950 /*!
1951  * \brief Check for valid exit from queue via goto
1952  * \retval 0 if failure
1953  * \retval 1 if successful
1954 */
1955 static int valid_exit(struct queue_ent *qe, char digit)
1956 {
1957         int digitlen = strlen(qe->digits);
1958
1959         /* Prevent possible buffer overflow */
1960         if (digitlen < sizeof(qe->digits) - 2) {
1961                 qe->digits[digitlen] = digit;
1962                 qe->digits[digitlen + 1] = '\0';
1963         } else {
1964                 qe->digits[0] = '\0';
1965                 return 0;
1966         }
1967
1968         /* If there's no context to goto, short-circuit */
1969         if (ast_strlen_zero(qe->context))
1970                 return 0;
1971
1972         /* If the extension is bad, then reset the digits to blank */
1973         if (!ast_canmatch_extension(qe->chan, qe->context, qe->digits, 1, qe->chan->cid.cid_num)) {
1974                 qe->digits[0] = '\0';
1975                 return 0;
1976         }
1977
1978         /* We have an exact match */
1979         if (!ast_goto_if_exists(qe->chan, qe->context, qe->digits, 1)) {
1980                 qe->valid_digits = 1;
1981                 /* Return 1 on a successful goto */
1982                 return 1;
1983         }
1984
1985         return 0;
1986 }
1987
1988 static int say_position(struct queue_ent *qe, int ringing)
1989 {
1990         int res = 0, avgholdmins, avgholdsecs, announceposition = 0;
1991         time_t now;
1992
1993         /* Let minannouncefrequency seconds pass between the start of each position announcement */
1994         time(&now);
1995         if ((now - qe->last_pos) < qe->parent->minannouncefrequency)
1996                 return 0;
1997
1998         /* If either our position has changed, or we are over the freq timer, say position */
1999         if ((qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency))
2000                 return 0;
2001
2002         if (ringing) {
2003                 ast_indicate(qe->chan,-1);
2004         } else {
2005                 ast_moh_stop(qe->chan);
2006         }
2007
2008         if (qe->parent->announceposition == ANNOUNCEPOSITION_YES ||
2009                 qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN ||
2010                 (qe->parent->announceposition == ANNOUNCEPOSITION_LIMIT &&
2011                 qe->pos <= qe->parent->announcepositionlimit))
2012                         announceposition = 1;
2013
2014
2015         if (announceposition == 1) {
2016                 /* Say we're next, if we are */
2017                 if (qe->pos == 1) {
2018                         res = play_file(qe->chan, qe->parent->sound_next);
2019                         if (res)
2020                                 goto playout;
2021                         else
2022                                 goto posout;
2023                 } else {
2024                         if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
2025                                 /* More than Case*/
2026                                 res = play_file(qe->chan, qe->parent->queue_quantity1);
2027                                 if (res)
2028                                         goto playout;
2029                                 res = ast_say_number(qe->chan, qe->parent->announcepositionlimit, AST_DIGIT_ANY, qe->chan->language, NULL); /* Needs gender */
2030                                 if (res)
2031                                         goto playout;
2032                         } else {
2033                                 /* Normal Case */
2034                                 res = play_file(qe->chan, qe->parent->sound_thereare);
2035                                 if (res)
2036                                         goto playout;
2037                                 res = ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language, NULL); /* Needs gender */
2038                                 if (res)
2039                                         goto playout;
2040                         }
2041                         if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
2042                                 /* More than Case*/
2043                                 res = play_file(qe->chan, qe->parent->queue_quantity2);
2044                                 if (res)
2045                                         goto playout;
2046                         } else {
2047                                 res = play_file(qe->chan, qe->parent->sound_calls);
2048                                 if (res)
2049                                         goto playout;
2050                         }
2051                 }
2052         }
2053         /* Round hold time to nearest minute */
2054         avgholdmins = abs(((qe->parent->holdtime + 30) - (now - qe->start)) / 60);
2055
2056         /* If they have specified a rounding then round the seconds as well */
2057         if (qe->parent->roundingseconds) {
2058                 avgholdsecs = (abs(((qe->parent->holdtime + 30) - (now - qe->start))) - 60 * avgholdmins) / qe->parent->roundingseconds;
2059                 avgholdsecs *= qe->parent->roundingseconds;
2060         } else {
2061                 avgholdsecs = 0;
2062         }
2063
2064         ast_verb(3, "Hold time for %s is %d minute(s) %d seconds\n", qe->parent->name, avgholdmins, avgholdsecs);
2065
2066         /* If the hold time is >1 min, if it's enabled, and if it's not
2067            supposed to be only once and we have already said it, say it */
2068     if ((avgholdmins+avgholdsecs) > 0 && qe->parent->announceholdtime &&
2069         ((qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE && !qe->last_pos) ||
2070         !(qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE))) {
2071                 res = play_file(qe->chan, qe->parent->sound_holdtime);
2072                 if (res)
2073                         goto playout;
2074
2075                 if (avgholdmins > 1) {
2076                         res = ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, qe->chan->language, NULL);
2077                         if (res)
2078                                 goto playout;
2079
2080                         if (avgholdmins == 1) {
2081                                 res = play_file(qe->chan, qe->parent->sound_minute);
2082                                 if (res)
2083                                         goto playout;
2084                         } else {
2085                                 res = play_file(qe->chan, qe->parent->sound_minutes);
2086                                 if (res)
2087                                         goto playout;
2088                         }
2089                 }
2090                 if (avgholdsecs > 1) {
2091                         res = ast_say_number(qe->chan, avgholdmins > 1 ? avgholdsecs : avgholdmins * 60 + avgholdsecs, AST_DIGIT_ANY, qe->chan->language, NULL);
2092                         if (res)
2093                                 goto playout;
2094
2095                         res = play_file(qe->chan, qe->parent->sound_seconds);
2096                         if (res)
2097                                 goto playout;
2098                 }
2099
2100         }
2101
2102 posout:
2103         if (announceposition == 1){
2104                 if (qe->parent->announceposition) {
2105                         ast_verb(3, "Told %s in %s their queue position (which was %d)\n",
2106                                 qe->chan->name, qe->parent->name, qe->pos);
2107                 }
2108                 res = play_file(qe->chan, qe->parent->sound_thanks);
2109         }
2110 playout:
2111         if ((res > 0 && !valid_exit(qe, res)) || res < 0)
2112                 res = 0;
2113
2114         /* Set our last_pos indicators */
2115         qe->last_pos = now;
2116         qe->last_pos_said = qe->pos;
2117
2118         /* Don't restart music on hold if we're about to exit the caller from the queue */
2119         if (!res) {
2120                 if (ringing) {
2121                         ast_indicate(qe->chan, AST_CONTROL_RINGING);
2122                 } else {
2123                         ast_moh_start(qe->chan, qe->moh, NULL);
2124                 }
2125         }
2126         return res;
2127 }
2128
2129 static void recalc_holdtime(struct queue_ent *qe, int newholdtime)
2130 {
2131         int oldvalue;
2132
2133         /* Calculate holdtime using an exponential average */
2134         /* Thanks to SRT for this contribution */
2135         /* 2^2 (4) is the filter coefficient; a higher exponent would give old entries more weight */
2136
2137         ao2_lock(qe->parent);
2138         oldvalue = qe->parent->holdtime;
2139         qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newholdtime) >> 2;
2140         ao2_unlock(qe->parent);
2141 }
2142
2143 /*! \brief Caller leaving queue.
2144  * 
2145  * Search the queue to find the leaving client, if found remove from queue
2146  * create manager event, move others up the queue.
2147 */
2148 static void leave_queue(struct queue_ent *qe)
2149 {
2150         struct call_queue *q;
2151         struct queue_ent *current, *prev = NULL;
2152         struct penalty_rule *pr_iter;
2153         int pos = 0;
2154
2155         if (!(q = qe->parent))
2156                 return;
2157         queue_ref(q);
2158         ao2_lock(q);
2159
2160         prev = NULL;
2161         for (current = q->head; current; current = current->next) {
2162                 if (current == qe) {
2163                         q->count--;
2164
2165                         /* Take us out of the queue */
2166                         manager_event(EVENT_FLAG_CALL, "Leave",
2167                                 "Channel: %s\r\nQueue: %s\r\nCount: %d\r\nUniqueid: %s\r\n",
2168                                 qe->chan->name, q->name,  q->count, qe->chan->uniqueid);
2169                         ast_debug(1, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
2170                         /* Take us out of the queue */
2171                         if (prev)
2172                                 prev->next = current->next;
2173                         else
2174                                 q->head = current->next;
2175                         /* Free penalty rules */
2176                         while ((pr_iter = AST_LIST_REMOVE_HEAD(&qe->qe_rules, list)))
2177                                 ast_free(pr_iter);
2178                 } else {
2179                         /* Renumber the people after us in the queue based on a new count */
2180                         current->pos = ++pos;
2181                         prev = current;
2182                 }
2183         }
2184         ao2_unlock(q);
2185
2186         /*If the queue is a realtime queue, check to see if it's still defined in real time*/
2187         if (q->realtime) {
2188                 if (!ast_load_realtime("queues", "name", q->name, SENTINEL))
2189                         q->dead = 1;
2190         }
2191
2192         if (q->dead) {  
2193                 /* It's dead and nobody is in it, so kill it */
2194                 ao2_unlink(queues, q);
2195                 /* unref the container's reference to the queue */
2196                 queue_unref(q);
2197         }
2198         /* unref the explicit ref earlier in the function */
2199         queue_unref(q);
2200 }
2201
2202 /*! \brief Hang up a list of outgoing calls */
2203 static void hangupcalls(struct callattempt *outgoing, struct ast_channel *exception)
2204 {
2205         struct callattempt *oo;
2206
2207         while (outgoing) {
2208                 /* Hangup any existing lines we have open */
2209                 if (outgoing->chan && (outgoing->chan != exception))
2210                         ast_hangup(outgoing->chan);
2211                 oo = outgoing;
2212                 outgoing = outgoing->q_next;
2213                 if (oo->member)
2214                         ao2_ref(oo->member, -1);
2215                 ast_free(oo);
2216         }
2217 }
2218
2219 /*! 
2220  * \brief traverse all defined queues which have calls waiting and contain this member
2221  * \retval 0 if no other queue has precedence (higher weight) 
2222  * \retval 1 if found  
2223 */
2224 static int compare_weight(struct call_queue *rq, struct member *member)
2225 {
2226         struct call_queue *q;
2227         struct member *mem;
2228         int found = 0;
2229         struct ao2_iterator queue_iter;
2230         
2231         /* q's lock and rq's lock already set by try_calling()
2232          * to solve deadlock */
2233         queue_iter = ao2_iterator_init(queues, 0);
2234         while ((q = ao2_iterator_next(&queue_iter))) {
2235                 if (q == rq) { /* don't check myself, could deadlock */
2236                         queue_unref(q);
2237                         continue;
2238                 }
2239                 ao2_lock(q);
2240                 if (q->count && q->members) {
2241                         if ((mem = ao2_find(q->members, member, NULL, OBJ_POINTER))) {
2242                                 ast_debug(1, "Found matching member %s in queue '%s'\n", mem->interface, q->name);
2243                                 if (q->weight > rq->weight) {
2244                                         ast_debug(1, "Queue '%s' (weight %d, calls %d) is preferred over '%s' (weight %d, calls %d)\n", q->name, q->weight, q->count, rq->name, rq->weight, rq->count);
2245                                         found = 1;
2246                                 }
2247                                 ao2_ref(mem, -1);
2248                         }
2249                 }
2250                 ao2_unlock(q);
2251                 if (found) {
2252                         queue_unref(q);
2253                         break;
2254                 }
2255                 queue_unref(q);
2256         }
2257         return found;
2258 }
2259
2260 /*! \brief common hangup actions */
2261 static void do_hang(struct callattempt *o)
2262 {
2263         o->stillgoing = 0;
2264         ast_hangup(o->chan);
2265         o->chan = NULL;
2266 }
2267
2268 /*! \brief convert "\n" to "\nVariable: " ready for manager to use */
2269 static char *vars2manager(struct ast_channel *chan, char *vars, size_t len)
2270 {
2271         struct ast_str *buf = ast_str_alloca(len + 1);
2272         char *tmp;
2273
2274         if (pbx_builtin_serialize_variables(chan, &buf)) {
2275                 int i, j;
2276
2277                 /* convert "\n" to "\nVariable: " */
2278                 strcpy(vars, "Variable: ");
2279                 tmp = buf->str;
2280
2281                 for (i = 0, j = 10; (i < len - 1) && (j < len - 1); i++, j++) {
2282                         vars[j] = tmp[i];
2283
2284                         if (tmp[i + 1] == '\0')
2285                                 break;
2286                         if (tmp[i] == '\n') {
2287                                 vars[j++] = '\r';
2288                                 vars[j++] = '\n';
2289
2290                                 ast_copy_string(&(vars[j]), "Variable: ", len - j);
2291                                 j += 9;
2292                         }
2293                 }
2294                 if (j > len - 3)
2295                         j = len - 3;
2296                 vars[j++] = '\r';
2297                 vars[j++] = '\n';
2298                 vars[j] = '\0';
2299         } else {
2300                 /* there are no channel variables; leave it blank */
2301                 *vars = '\0';
2302         }
2303         return vars;
2304 }
2305
2306 /*! 
2307  * \brief Part 2 of ring_one
2308  *
2309  * Does error checking before attempting to request a channel and call a member. 
2310  * This function is only called from ring_one(). 
2311  * Failure can occur if:
2312  * - Agent on call
2313  * - Agent is paused
2314  * - Wrapup time not expired
2315  * - Priority by another queue
2316  *
2317  * \retval 1 on success to reach a free agent
2318  * \retval 0 on failure to get agent.
2319  */
2320 static int ring_entry(struct queue_ent *qe, struct callattempt *tmp, int *busies)
2321 {
2322         int res;
2323         int status;
2324         char tech[256];
2325         char *location;
2326         const char *macrocontext, *macroexten;
2327
2328         /* on entry here, we know that tmp->chan == NULL */
2329         if ((tmp->lastqueue && tmp->lastqueue->wrapuptime && (time(NULL) - tmp->lastcall < tmp->lastqueue->wrapuptime)) ||
2330                 (!tmp->lastqueue && qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime))) {
2331                 ast_debug(1, "Wrapuptime not yet expired on queue %s for %s\n", 
2332                                 (tmp->lastqueue ? tmp->lastqueue->name : qe->parent->name), tmp->interface);
2333                 if (qe->chan->cdr)
2334                         ast_cdr_busy(qe->chan->cdr);
2335                 tmp->stillgoing = 0;
2336                 (*busies)++;
2337                 return 0;
2338         }
2339
2340         if (!qe->parent->ringinuse && (tmp->member->status != AST_DEVICE_NOT_INUSE) && (tmp->member->status != AST_DEVICE_UNKNOWN)) {
2341                 ast_debug(1, "%s in use, can't receive call\n", tmp->interface);
2342                 if (qe->chan->cdr)
2343                         ast_cdr_busy(qe->chan->cdr);
2344                 tmp->stillgoing = 0;
2345                 return 0;
2346         }
2347
2348         if (tmp->member->paused) {
2349                 ast_debug(1, "%s paused, can't receive call\n", tmp->interface);
2350                 if (qe->chan->cdr)
2351                         ast_cdr_busy(qe->chan->cdr);
2352                 tmp->stillgoing = 0;
2353                 return 0;
2354         }
2355         if (use_weight && compare_weight(qe->parent,tmp->member)) {
2356                 ast_debug(1, "Priority queue delaying call to %s:%s\n", qe->parent->name, tmp->interface);
2357                 if (qe->chan->cdr)
2358                         ast_cdr_busy(qe->chan->cdr);
2359                 tmp->stillgoing = 0;
2360                 (*busies)++;
2361                 return 0;
2362         }
2363
2364         ast_copy_string(tech, tmp->interface, sizeof(tech));
2365         if ((location = strchr(tech, '/')))
2366                 *location++ = '\0';
2367         else
2368                 location = "";
2369
2370         /* Request the peer */
2371         tmp->chan = ast_request(tech, qe->chan->nativeformats, location, &status);
2372         if (!tmp->chan) {                       /* If we can't, just go on to the next call */
2373                 if (qe->chan->cdr)
2374                         ast_cdr_busy(qe->chan->cdr);
2375                 tmp->stillgoing = 0;    
2376
2377                 ao2_lock(qe->parent);
2378                 update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
2379                 qe->parent->rrpos++;
2380                 qe->linpos++;
2381                 ao2_unlock(qe->parent);
2382
2383                 (*busies)++;
2384                 return 0;
2385         }
2386         
2387         tmp->chan->appl = "AppQueue";
2388         tmp->chan->data = "(Outgoing Line)";
2389         memset(&tmp->chan->whentohangup, 0, sizeof(tmp->chan->whentohangup));
2390         if (tmp->chan->cid.cid_num)
2391                 ast_free(tmp->chan->cid.cid_num);
2392         tmp->chan->cid.cid_num = ast_strdup(qe->chan->cid.cid_num);
2393         if (tmp->chan->cid.cid_name)
2394                 ast_free(tmp->chan->cid.cid_name);
2395         tmp->chan->cid.cid_name = ast_strdup(qe->chan->cid.cid_name);
2396         if (tmp->chan->cid.cid_ani)
2397                 ast_free(tmp->chan->cid.cid_ani);
2398         tmp->chan->cid.cid_ani = ast_strdup(qe->chan->cid.cid_ani);
2399
2400         /* Inherit specially named variables from parent channel */
2401         ast_channel_inherit_variables(qe->chan, tmp->chan);
2402
2403         /* Presense of ADSI CPE on outgoing channel follows ours */
2404         tmp->chan->adsicpe = qe->chan->adsicpe;
2405
2406         /* Inherit context and extension */
2407         ast_channel_lock(qe->chan);
2408         macrocontext = pbx_builtin_getvar_helper(qe->chan, "MACRO_CONTEXT");
2409         ast_string_field_set(tmp->chan, dialcontext, ast_strlen_zero(macrocontext) ? qe->chan->context : macrocontext);
2410         macroexten = pbx_builtin_getvar_helper(qe->chan, "MACRO_EXTEN");
2411         if (!ast_strlen_zero(macroexten))
2412                 ast_copy_string(tmp->chan->exten, macroexten, sizeof(tmp->chan->exten));
2413         else
2414                 ast_copy_string(tmp->chan->exten, qe->chan->exten, sizeof(tmp->chan->exten));
2415         ast_channel_unlock(qe->chan);
2416
2417         /* Place the call, but don't wait on the answer */
2418         if ((res = ast_call(tmp->chan, location, 0))) {
2419                 /* Again, keep going even if there's an error */
2420                 ast_debug(1, "ast call on peer returned %d\n", res);
2421                 ast_verb(3, "Couldn't call %s\n", tmp->interface);
2422                 do_hang(tmp);
2423                 (*busies)++;
2424                 update_status(qe->parent, tmp->member, ast_device_state(tmp->member->interface));
2425                 return 0;
2426         } else if (qe->parent->eventwhencalled) {
2427                 char vars[2048];
2428
2429                 manager_event(EVENT_FLAG_AGENT, "AgentCalled",
2430                                         "Queue: %s\r\n"
2431                                         "AgentCalled: %s\r\n"
2432                                         "AgentName: %s\r\n"
2433                                         "ChannelCalling: %s\r\n"
2434                                         "DestinationChannel: %s\r\n"
2435                                         "CallerIDNum: %s\r\n"
2436                                         "CallerIDName: %s\r\n"
2437                                         "Context: %s\r\n"
2438                                         "Extension: %s\r\n"
2439                                         "Priority: %d\r\n"
2440                                         "Uniqueid: %s\r\n"
2441                                         "%s",
2442                                         qe->parent->name, tmp->interface, tmp->member->membername, qe->chan->name, tmp->chan->name,
2443                                         tmp->chan->cid.cid_num ? tmp->chan->cid.cid_num : "unknown",
2444                                         tmp->chan->cid.cid_name ? tmp->chan->cid.cid_name : "unknown",
2445                                         qe->chan->context, qe->chan->exten, qe->chan->priority, qe->chan->uniqueid,
2446                                         qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
2447                 ast_verb(3, "Called %s\n", tmp->interface);
2448         }
2449
2450         update_status(qe->parent, tmp->member, ast_device_state(tmp->member->interface));
2451         return 1;
2452 }
2453
2454 /*! \brief find the entry with the best metric, or NULL */
2455 static struct callattempt *find_best(struct callattempt *outgoing)
2456 {
2457         struct callattempt *best = NULL, *cur;
2458
2459         for (cur = outgoing; cur; cur = cur->q_next) {
2460                 if (cur->stillgoing &&                                  /* Not already done */
2461                         !cur->chan &&                                   /* Isn't already going */
2462                         (!best || cur->metric < best->metric)) {                /* We haven't found one yet, or it's better */
2463                         best = cur;
2464                 }
2465         }
2466
2467         return best;
2468 }
2469
2470 /*! 
2471  * \brief Place a call to a queue member.
2472  *
2473  * Once metrics have been calculated for each member, this function is used
2474  * to place a call to the appropriate member (or members). The low-level
2475  * channel-handling and error detection is handled in ring_entry
2476  *
2477  * \retval 1 if a member was called successfully
2478  * \retval 0 otherwise
2479  */
2480 static int ring_one(struct queue_ent *qe, struct callattempt *outgoing, int *busies)
2481 {
2482         int ret = 0;
2483
2484         while (ret == 0) {
2485                 struct callattempt *best = find_best(outgoing);
2486                 if (!best) {
2487                         ast_debug(1, "Nobody left to try ringing in queue\n");
2488                         break;
2489                 }
2490                 if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
2491                         struct callattempt *cur;
2492                         /* Ring everyone who shares this best metric (for ringall) */
2493                         for (cur = outgoing; cur; cur = cur->q_next) {
2494                                 if (cur->stillgoing && !cur->chan && cur->metric <= best->metric) {
2495                                         ast_debug(1, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
2496                                         ret |= ring_entry(qe, cur, busies);
2497                                 }
2498                         }
2499                 } else {
2500                         /* Ring just the best channel */
2501                         ast_debug(1, "Trying '%s' with metric %d\n", best->interface, best->metric);
2502                         ret = ring_entry(qe, best, busies);
2503                 }
2504                 
2505                 /* If we have timed out, break out */
2506                 if (qe->expire && (time(NULL) >= qe->expire)) {
2507                         ast_debug(1, "Queue timed out while ringing members.\n");
2508                         ret = 0;
2509                         break;
2510                 }
2511         }
2512
2513         return ret;
2514 }
2515
2516 /*! \brief Search for best metric and add to Round Robbin queue */
2517 static int store_next_rr(struct queue_ent *qe, struct callattempt *outgoing)
2518 {
2519         struct callattempt *best = find_best(outgoing);
2520
2521         if (best) {
2522                 /* Ring just the best channel */
2523                 ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
2524                 qe->parent->rrpos = best->metric % 1000;
2525         } else {
2526                 /* Just increment rrpos */
2527                 if (qe->parent->wrapped) {
2528                         /* No more channels, start over */
2529                         qe->parent->rrpos = 0;
2530                 } else {
2531                         /* Prioritize next entry */
2532                         qe->parent->rrpos++;
2533                 }
2534         }
2535         qe->parent->wrapped = 0;
2536
2537         return 0;
2538 }
2539
2540 /*! \brief Search for best metric and add to Linear queue */
2541 static int store_next_lin(struct queue_ent *qe, struct callattempt *outgoing)
2542 {
2543         struct callattempt *best = find_best(outgoing);
2544
2545         if (best) {
2546                 /* Ring just the best channel */
2547                 ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
2548                 qe->linpos = best->metric % 1000;
2549         } else {
2550                 /* Just increment rrpos */
2551                 if (qe->linwrapped) {
2552                         /* No more channels, start over */
2553                         qe->linpos = 0;
2554                 } else {
2555                         /* Prioritize next entry */
2556                         qe->linpos++;
2557                 }
2558         }
2559         qe->linwrapped = 0;
2560
2561         return 0;
2562 }
2563
2564 /*! \brief Playback announcement to queued members if peroid has elapsed */
2565 static int say_periodic_announcement(struct queue_ent *qe, int ringing)
2566 {
2567         int res = 0;
2568         time_t now;
2569
2570         /* Get the current time */
2571         time(&now);
2572
2573         /* Check to see if it is time to announce */
2574         if ((now - qe->last_periodic_announce_time) < qe->parent->periodicannouncefrequency)
2575                 return 0;
2576
2577         /* Stop the music on hold so we can play our own file */
2578         if (ringing)
2579                 ast_indicate(qe->chan,-1);
2580         else
2581                 ast_moh_stop(qe->chan);
2582
2583         ast_verb(3, "Playing periodic announcement\n");
2584         
2585         if (qe->parent->randomperiodicannounce) {
2586                 qe->last_periodic_announce_sound = ((unsigned long) ast_random()) % qe->parent->numperiodicannounce;
2587         } else if (qe->last_periodic_announce_sound >= qe->parent->numperiodicannounce || 
2588                 ast_strlen_zero(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]->str)) {
2589                 qe->last_periodic_announce_sound = 0;
2590         }
2591         
2592         /* play the announcement */
2593         res = play_file(qe->chan, qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]->str);
2594
2595         if ((res > 0 && !valid_exit(qe, res)) || res < 0)
2596                 res = 0;
2597
2598         /* Resume Music on Hold if the caller is going to stay in the queue */
2599         if (!res) {
2600                 if (ringing)
2601                         ast_indicate(qe->chan, AST_CONTROL_RINGING);
2602                 else
2603                         ast_moh_start(qe->chan, qe->moh, NULL);
2604         }
2605
2606         /* update last_periodic_announce_time */
2607         qe->last_periodic_announce_time = now;
2608
2609         /* Update the current periodic announcement to the next announcement */
2610         if (!qe->parent->randomperiodicannounce) {
2611                 qe->last_periodic_announce_sound++;
2612         }
2613         
2614         return res;
2615 }
2616
2617 /*! \brief Record that a caller gave up on waiting in queue */
2618 static void record_abandoned(struct queue_ent *qe)
2619 {
2620         ao2_lock(qe->parent);
2621         set_queue_variables(qe);
2622         manager_event(EVENT_FLAG_AGENT, "QueueCallerAbandon",
2623                 "Queue: %s\r\n"
2624                 "Uniqueid: %s\r\n"
2625                 "Position: %d\r\n"
2626                 "OriginalPosition: %d\r\n"
2627                 "HoldTime: %d\r\n",
2628                 qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
2629
2630         qe->parent->callsabandoned++;
2631         ao2_unlock(qe->parent);
2632 }
2633
2634 /*! \brief RNA == Ring No Answer. Common code that is executed when we try a queue member and they don't answer. */
2635 static void rna(int rnatime, struct queue_ent *qe, char *interface, char *membername)
2636 {
2637         ast_verb(3, "Nobody picked up in %d ms\n", rnatime);
2638         if (qe->parent->eventwhencalled) {
2639                 char vars[2048];
2640
2641                 manager_event(EVENT_FLAG_AGENT, "AgentRingNoAnswer",
2642                                                 "Queue: %s\r\n"
2643                                                 "Uniqueid: %s\r\n"
2644                                                 "Channel: %s\r\n"
2645                                                 "Member: %s\r\n"
2646                                                 "MemberName: %s\r\n"
2647                                                 "Ringtime: %d\r\n"
2648                                                 "%s",
2649                                                 qe->parent->name,
2650                                                 qe->chan->uniqueid,
2651                                                 qe->chan->name,
2652                                                 interface,
2653                                                 membername,
2654                                                 rnatime,
2655                                                 qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
2656         }
2657         ast_queue_log(qe->parent->name, qe->chan->uniqueid, membername, "RINGNOANSWER", "%d", rnatime);
2658         if (qe->parent->autopause) {
2659                 if (!set_member_paused(qe->parent->name, interface, "Auto-Pause", 1)) {
2660                         ast_verb(3, "Auto-Pausing Queue Member %s in queue %s since they failed to answer.\n", interface, qe->parent->name);
2661                 } else {
2662                         ast_verb(3, "Failed to pause Queue Member %s in queue %s!\n", interface, qe->parent->name);
2663                 }
2664         }
2665         return;
2666 }
2667
2668 #define AST_MAX_WATCHERS 256
2669 /*! \brief Wait for a member to answer the call
2670  *
2671  * \param[in] qe the queue_ent corresponding to the caller in the queue
2672  * \param[in] outgoing the list of callattempts. Relevant ones will have their chan and stillgoing parameters non-zero
2673  * \param[in] to the amount of time (in milliseconds) to wait for a response
2674  * \param[out] digit if a user presses a digit to exit the queue, this is the digit the caller pressed
2675  * \param[in] prebusies number of busy members calculated prior to calling wait_for_answer
2676  * \param[in] caller_disconnect if the 'H' option is used when calling Queue(), this is used to detect if the caller pressed * to disconnect the call
2677  * \param[in] forwardsallowed used to detect if we should allow call forwarding, based on the 'i' option to Queue()
2678  */
2679 static struct callattempt *wait_for_answer(struct queue_ent *qe, struct callattempt *outgoing, int *to, char *digit, int prebusies, int caller_disconnect, int forwardsallowed)
2680 {
2681         const char *queue = qe->parent->name;
2682         struct callattempt *o, *start = NULL, *prev = NULL;
2683         int status;
2684         int numbusies = prebusies;
2685         int numnochan = 0;
2686         int stillgoing = 0;
2687         int orig = *to;
2688         struct ast_frame *f;
2689         struct callattempt *peer = NULL;
2690         struct ast_channel *winner;
2691         struct ast_channel *in = qe->chan;
2692         char on[80] = "";
2693         char membername[80] = "";
2694         long starttime = 0;
2695         long endtime = 0;
2696 #ifdef HAVE_EPOLL
2697         struct callattempt *epollo;
2698 #endif
2699
2700         starttime = (long) time(NULL);
2701 #ifdef HAVE_EPOLL
2702         for (epollo = outgoing; epollo; epollo = epollo->q_next) {
2703                 if (epollo->chan)
2704                         ast_poll_channel_add(in, epollo->chan);
2705         }
2706 #endif
2707         
2708         while (*to && !peer) {
2709                 int numlines, retry, pos = 1;
2710                 struct ast_channel *watchers[AST_MAX_WATCHERS];
2711                 watchers[0] = in;
2712                 start = NULL;
2713
2714                 for (retry = 0; retry < 2; retry++) {
2715                         numlines = 0;
2716                         for (o = outgoing; o; o = o->q_next) { /* Keep track of important channels */
2717                                 if (o->stillgoing) {    /* Keep track of important channels */
2718                                         stillgoing = 1;
2719                                         if (o->chan) {
2720                                                 watchers[pos++] = o->chan;
2721                                                 if (!start)
2722                                                         start = o;
2723                                                 else
2724                                                         prev->call_next = o;
2725                                                 prev = o;
2726                                         }
2727                                 }
2728                                 numlines++;
2729                         }
2730                         if (pos > 1 /* found */ || !stillgoing /* nobody listening */ ||
2731                                 (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) /* ring would not be delivered */)
2732                                 break;
2733                         /* On "ringall" strategy we only move to the next penalty level
2734                            when *all* ringing phones are done in the current penalty level */
2735                         ring_one(qe, outgoing, &numbusies);
2736                         /* and retry... */
2737                 }
2738                 if (pos == 1 /* not found */) {
2739                         if (numlines == (numbusies + numnochan)) {
2740                                 ast_debug(1, "Everyone is busy at this time\n");
2741                         } else {
2742                                 ast_log(LOG_NOTICE, "No one is answering queue '%s' (%d/%d/%d)\n", queue, numlines, numbusies, numnochan);
2743                         }
2744                         *to = 0;
2745                         return NULL;
2746                 }
2747                 winner = ast_waitfor_n(watchers, pos, to);
2748                 for (o = start; o; o = o->call_next) {
2749                         if (o->stillgoing && (o->chan) &&  (o->chan->_state == AST_STATE_UP)) {
2750                                 if (!peer) {
2751                                         ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
2752                                         peer = o;
2753                                 }
2754                         } else if (o->chan && (o->chan == winner)) {
2755
2756                                 ast_copy_string(on, o->member->interface, sizeof(on));
2757                                 ast_copy_string(membername, o->member->membername, sizeof(membername));
2758
2759                                 if (!ast_strlen_zero(o->chan->call_forward) && !forwardsallowed) {
2760                                         ast_verb(3, "Forwarding %s to '%s' prevented.\n", in->name, o->chan->call_forward);
2761                                         numnochan++;
2762                                         do_hang(o);
2763                                         winner = NULL;
2764                                         continue;
2765                                 } else if (!ast_strlen_zero(o->chan->call_forward)) {
2766                                         char tmpchan[256];
2767                                         char *stuff;
2768                                         char *tech;
2769
2770                                         ast_copy_string(tmpchan, o->chan->call_forward, sizeof(tmpchan));
2771                                         if ((stuff = strchr(tmpchan, '/'))) {
2772                                                 *stuff++ = '\0';
2773                                                 tech = tmpchan;
2774                                         } else {
2775                                                 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, o->chan->context);
2776                                                 stuff = tmpchan;
2777                                                 tech = "Local";
2778                                         }
2779                                         /* Before processing channel, go ahead and check for forwarding */
2780                                         ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, o->chan->name);
2781                                         /* Setup parameters */
2782                                         o->chan = ast_request(tech, in->nativeformats, stuff, &status);
2783                                         if (!o->chan) {
2784                                                 ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s'\n", tech, stuff);
2785                                                 o->stillgoing = 0;
2786                                                 numnochan++;
2787                                         } else {
2788                                                 ast_channel_inherit_variables(in, o->chan);
2789                                                 ast_channel_datastore_inherit(in, o->chan);
2790                                                 if (o->chan->cid.cid_num)
2791                                                         ast_free(o->chan->cid.cid_num);
2792                                                 o->chan->cid.cid_num = ast_strdup(in->cid.cid_num);
2793
2794                                                 if (o->chan->cid.cid_name)
2795                                                         ast_free(o->chan->cid.cid_name);
2796                                                 o->chan->cid.cid_name = ast_strdup(in->cid.cid_name);
2797
2798                                                 ast_string_field_set(o->chan, accountcode, in->accountcode);
2799                                                 o->chan->cdrflags = in->cdrflags;
2800
2801                                                 if (in->cid.cid_ani) {
2802                                                         if (o->chan->cid.cid_ani)
2803                                                                 ast_free(o->chan->cid.cid_ani);
2804                                                         o->chan->cid.cid_ani = ast_strdup(in->cid.cid_ani);
2805                                                 }
2806                                                 if (o->chan->cid.cid_rdnis)
2807                                                         ast_free(o->chan->cid.cid_rdnis);
2808                                                 o->chan->cid.cid_rdnis = ast_strdup(S_OR(in->macroexten, in->exten));
2809                                                 if (ast_call(o->chan, tmpchan, 0)) {
2810                                                         ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
2811                                                         do_hang(o);
2812                                                         numnochan++;
2813                                                 }
2814                                         }
2815                                         /* Hangup the original channel now, in case we needed it */
2816                                         ast_hangup(winner);
2817                                         continue;
2818                                 }
2819                                 f = ast_read(winner);
2820                                 if (f) {
2821                                         if (f->frametype == AST_FRAME_CONTROL) {
2822                                                 switch (f->subclass) {
2823                                                 case AST_CONTROL_ANSWER:
2824                                                         /* This is our guy if someone answered. */
2825                                                         if (!peer) {
2826                                                                 ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
2827                                                                 peer = o;
2828                                                         }
2829                                                         break;
2830                                                 case AST_CONTROL_BUSY:
2831                                                         ast_verb(3, "%s is busy\n", o->chan->name);
2832                                                         if (in->cdr)
2833                                                                 ast_cdr_busy(in->cdr);
2834                                                         do_hang(o);
2835                                                         endtime = (long) time(NULL);
2836                                                         endtime -= starttime;
2837                                                         rna(endtime*1000, qe, on, membername);
2838                                                         if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
2839                                                                 if (qe->parent->timeoutrestart)
2840                                                                         *to = orig;
2841                                                                 ring_one(qe, outgoing, &numbusies);
2842                                                         }
2843                                                         numbusies++;
2844                                                         break;
2845                                                 case AST_CONTROL_CONGESTION:
2846                                                         ast_verb(3, "%s is circuit-busy\n", o->chan->name);
2847                                                         if (in->cdr)
2848                                                                 ast_cdr_busy(in->cdr);
2849                                                         endtime = (long) time(NULL);
2850                                                         endtime -= starttime;
2851                                                         rna(endtime*1000, qe, on, membername);
2852                                                         do_hang(o);
2853                                                         if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
2854                                                                 if (qe->parent->timeoutrestart)
2855                                                                         *to = orig;
2856                                                                 ring_one(qe, outgoing, &numbusies);
2857                                                         }
2858                                                         numbusies++;
2859                                                         break;
2860                                                 case AST_CONTROL_RINGING:
2861                                                         ast_verb(3, "%s is ringing\n", o->chan->name);
2862                                                         break;
2863                                                 case AST_CONTROL_OFFHOOK:
2864                                                         /* Ignore going off hook */
2865                                                         break;
2866                                                 default:
2867                                                         ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
2868                                                 }
2869                                         }
2870                                         ast_frfree(f);
2871                                 } else {
2872                                         endtime = (long) time(NULL) - starttime;
2873                                         rna(endtime * 1000, qe, on, membername);
2874                                         do_hang(o);
2875                                         if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
2876                                                 if (qe->parent->timeoutrestart)
2877                                                         *to = orig;
2878                                                 ring_one(qe, outgoing, &numbusies);
2879                                         }
2880                                 }
2881                         }
2882                 }
2883                 if (winner == in) {
2884                         f = ast_read(in);
2885                         if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
2886                                 /* Got hung up */
2887                                 *to = -1;
2888                                 if (f) {
2889                                         if (f->data.uint32) {
2890                                            &n