Wed Aug 11 01:05:00 2010

Asterisk developer's documentation


app_queue.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief True call queues with optional send URL on answer
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \arg Config in \ref Config_qu queues.conf
00026  *
00027  * \par Development notes
00028  * \note 2004-11-25: Persistent Dynamic Members added by:
00029  *             NetNation Communications (www.netnation.com)
00030  *             Kevin Lindsay <kevinl@netnation.com>
00031  *
00032  *             Each dynamic agent in each queue is now stored in the astdb.
00033  *             When asterisk is restarted, each agent will be automatically
00034  *             readded into their recorded queues. This feature can be
00035  *             configured with the 'persistent_members=<1|0>' setting in the
00036  *             '[general]' category in queues.conf. The default is on.
00037  *
00038  * \note 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
00039  *
00040  * \note These features added by David C. Troy <dave@toad.net>:
00041  *    - Per-queue holdtime calculation
00042  *    - Estimated holdtime announcement
00043  *    - Position announcement
00044  *    - Abandoned/completed call counters
00045  *    - Failout timer passed as optional app parameter
00046  *    - Optional monitoring of calls, started when call is answered
00047  *
00048  * Patch Version 1.07 2003-12-24 01
00049  *
00050  * Added servicelevel statistic by Michiel Betel <michiel@betel.nl>
00051  * Added Priority jumping code for adding and removing queue members by Jonathan Stanton <asterisk@doilooklikeicare.com>
00052  *
00053  * Fixed to work with CVS as of 2004-02-25 and released as 1.07a
00054  * by Matthew Enger <m.enger@xi.com.au>
00055  *
00056  * \ingroup applications
00057  */
00058 
00059 /*** MODULEINFO
00060    <depend>res_monitor</depend>
00061  ***/
00062 
00063 #include "asterisk.h"
00064 
00065 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 277489 $")
00066 
00067 #include <sys/time.h>
00068 #include <sys/signal.h>
00069 #include <netinet/in.h>
00070 #include <ctype.h>
00071 
00072 #include "asterisk/lock.h"
00073 #include "asterisk/file.h"
00074 #include "asterisk/channel.h"
00075 #include "asterisk/pbx.h"
00076 #include "asterisk/app.h"
00077 #include "asterisk/linkedlists.h"
00078 #include "asterisk/module.h"
00079 #include "asterisk/translate.h"
00080 #include "asterisk/say.h"
00081 #include "asterisk/features.h"
00082 #include "asterisk/musiconhold.h"
00083 #include "asterisk/cli.h"
00084 #include "asterisk/manager.h"
00085 #include "asterisk/config.h"
00086 #include "asterisk/monitor.h"
00087 #include "asterisk/utils.h"
00088 #include "asterisk/causes.h"
00089 #include "asterisk/astdb.h"
00090 #include "asterisk/devicestate.h"
00091 #include "asterisk/stringfields.h"
00092 #include "asterisk/event.h"
00093 #include "asterisk/astobj2.h"
00094 #include "asterisk/strings.h"
00095 #include "asterisk/global_datastores.h"
00096 #include "asterisk/taskprocessor.h"
00097 
00098 /* Define, to debug reference counts on queues, without debugging reference counts on queue members */
00099 /* #define REF_DEBUG_ONLY_QUEUES */
00100 
00101 /*!
00102  * \par Please read before modifying this file.
00103  * There are three locks which are regularly used
00104  * throughout this file, the queue list lock, the lock
00105  * for each individual queue, and the interface list lock.
00106  * Please be extra careful to always lock in the following order
00107  * 1) queue list lock
00108  * 2) individual queue lock
00109  * 3) interface list lock
00110  * This order has sort of "evolved" over the lifetime of this
00111  * application, but it is now in place this way, so please adhere
00112  * to this order!
00113  */
00114 
00115 /*** DOCUMENTATION
00116    <application name="Queue" language="en_US">
00117       <synopsis>
00118          Queue a call for a call queue.
00119       </synopsis>
00120       <syntax>
00121          <parameter name="queuename" required="true" />
00122          <parameter name="options">
00123             <optionlist>
00124                <option name="C">
00125                   <para>Mark all calls as "answered elsewhere" when cancelled.</para>
00126                </option>
00127                <option name="c">
00128                   <para>Continue in the dialplan if the callee hangs up.</para>
00129                </option>
00130                <option name="d">
00131                   <para>data-quality (modem) call (minimum delay).</para>
00132                </option>
00133                <option name="h">
00134                   <para>Allow <emphasis>callee</emphasis> to hang up by pressing <literal>*</literal>.</para>
00135                </option>
00136                <option name="H">
00137                   <para>Allow <emphasis>caller</emphasis> to hang up by pressing <literal>*</literal>.</para>
00138                </option>
00139                <option name="n">
00140                   <para>No retries on the timeout; will exit this application and
00141                   go to the next step.</para>
00142                </option>
00143                <option name="i">
00144                   <para>Ignore call forward requests from queue members and do nothing
00145                   when they are requested.</para>
00146                </option>
00147                <option name="r">
00148                   <para>Ring instead of playing MOH. Periodic Announcements are still made, if applicable.</para>
00149                </option>
00150                <option name="t">
00151                   <para>Allow the <emphasis>called</emphasis> user to transfer the calling user.</para>
00152                </option>
00153                <option name="T">
00154                   <para>Allow the <emphasis>calling</emphasis> user to transfer the call.</para>
00155                </option>
00156                <option name="w">
00157                   <para>Allow the <emphasis>called</emphasis> user to write the conversation to
00158                   disk via Monitor.</para>
00159                </option>
00160                <option name="W">
00161                   <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
00162                   disk via Monitor.</para>
00163                </option>
00164                <option name="k">
00165                   <para>Allow the <emphasis>called</emphasis> party to enable parking of the call by sending
00166                   the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
00167                </option>
00168                <option name="K">
00169                   <para>Allow the <emphasis>calling</emphasis> party to enable parking of the call by sending
00170                   the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
00171                </option>
00172                <option name="x">
00173                   <para>Allow the <emphasis>called</emphasis> user to write the conversation
00174                   to disk via MixMonitor.</para>
00175                </option>
00176                <option name="X">
00177                   <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
00178                   disk via MixMonitor.</para>
00179                </option>
00180             </optionlist>
00181          </parameter>
00182          <parameter name="URL">
00183             <para><replaceable>URL</replaceable> will be sent to the called party if the channel supports it.</para>
00184          </parameter>
00185          <parameter name="announceoverride" />
00186          <parameter name="timeout">
00187             <para>Will cause the queue to fail out after a specified number of
00188             seconds, checked between each <filename>queues.conf</filename> <replaceable>timeout</replaceable> and
00189             <replaceable>retry</replaceable> cycle.</para>
00190          </parameter>
00191          <parameter name="AGI">
00192             <para>Will setup an AGI script to be executed on the calling party's channel once they are
00193             connected to a queue member.</para>
00194          </parameter>
00195          <parameter name="macro">
00196             <para>Will run a macro on the calling party's channel once they are connected to a queue member.</para>
00197          </parameter>
00198          <parameter name="gosub">
00199             <para>Will run a gosub on the calling party's channel once they are connected to a queue member.</para>
00200          </parameter>
00201          <parameter name="rule">
00202             <para>Will cause the queue's defaultrule to be overridden by the rule specified.</para>
00203          </parameter>
00204       </syntax>
00205       <description>
00206          <para>In addition to transferring the call, a call may be parked and then picked
00207          up by another user.</para>
00208          <para>This application will return to the dialplan if the queue does not exist, or
00209          any of the join options cause the caller to not enter the queue.</para>
00210          <para>This application sets the following channel variable upon completion:</para>
00211          <variablelist>
00212             <variable name="QUEUESTATUS">
00213                <para>The status of the call as a text string.</para>
00214                <value name="TIMEOUT" />
00215                <value name="FULL" />
00216                <value name="JOINEMPTY" />
00217                <value name="LEAVEEMPTY" />
00218                <value name="JOINUNAVAIL" />
00219                <value name="LEAVEUNAVAIL" />
00220                <value name="CONTINUE" />
00221             </variable>
00222          </variablelist>
00223       </description>
00224       <see-also>
00225          <ref type="application">AddQueueMember</ref>
00226          <ref type="application">RemoveQueueMember</ref>
00227          <ref type="application">PauseQueueMember</ref>
00228          <ref type="application">UnpauseQueueMember</ref>
00229          <ref type="application">AgentLogin</ref>
00230          <ref type="function">QUEUE_MEMBER_COUNT</ref>
00231          <ref type="function">QUEUE_MEMBER_LIST</ref>
00232          <ref type="function">QUEUE_WAITING_COUNT</ref>
00233       </see-also>
00234    </application>
00235    <application name="AddQueueMember" language="en_US">
00236       <synopsis>
00237          Dynamically adds queue members.
00238       </synopsis>
00239       <syntax>
00240          <parameter name="queuename" required="true" />
00241          <parameter name="interface" />
00242          <parameter name="penalty" />
00243          <parameter name="options" />
00244          <parameter name="membername" />
00245          <parameter name="stateinterface" />
00246       </syntax>
00247       <description>
00248          <para>Dynamically adds interface to an existing queue. If the interface is
00249          already in the queue it will return an error.</para>
00250          <para>This application sets the following channel variable upon completion:</para>
00251          <variablelist>
00252             <variable name="AQMSTATUS">
00253                <para>The status of the attempt to add a queue member as a text string.</para>
00254                <value name="ADDED" />
00255                <value name="MEMBERALREADY" />
00256                <value name="NOSUCHQUEUE" />
00257             </variable>
00258          </variablelist>
00259       </description>
00260       <see-also>
00261          <ref type="application">RemoveQueueMember</ref>
00262          <ref type="application">PauseQueueMember</ref>
00263          <ref type="application">UnpauseQueueMember</ref>
00264          <ref type="application">AgentLogin</ref>
00265       </see-also>
00266    </application>
00267    <application name="RemoveQueueMember" language="en_US">
00268       <synopsis>
00269          Dynamically removes queue members.
00270       </synopsis>
00271       <syntax>
00272          <parameter name="queuename" required="true" />
00273          <parameter name="interface" />
00274          <parameter name="options" />
00275       </syntax>
00276       <description>
00277          <para>If the interface is <emphasis>NOT</emphasis> in the queue it will return an error.</para>
00278          <para>This application sets the following channel variable upon completion:</para>
00279          <variablelist>
00280             <variable name="RQMSTATUS">
00281                <value name="REMOVED" />
00282                <value name="NOTINQUEUE" />
00283                <value name="NOSUCHQUEUE" />
00284             </variable>
00285          </variablelist>
00286          <para>Example: RemoveQueueMember(techsupport,SIP/3000)</para>
00287       </description>
00288       <see-also>
00289          <ref type="application">Queue</ref>
00290          <ref type="application">AddQueueMember</ref>
00291          <ref type="application">PauseQueueMember</ref>
00292          <ref type="application">UnpauseQueueMember</ref>
00293       </see-also>
00294    </application>
00295    <application name="PauseQueueMember" language="en_US">
00296       <synopsis>
00297          Pauses a queue member.
00298       </synopsis>
00299       <syntax>
00300          <parameter name="queuename" />
00301          <parameter name="interface" required="true" />
00302          <parameter name="options" />
00303          <parameter name="reason">
00304             <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
00305          </parameter>
00306       </syntax>
00307       <description>
00308          <para>Pauses (blocks calls for) a queue member. The given interface will be paused in the given queue.
00309          This prevents any calls from being sent from the queue to the interface until it is
00310          unpaused with UnpauseQueueMember or the manager interface.  If no queuename is given,
00311          the interface is paused in every queue it is a member of. The application will fail if the
00312          interface is not found.</para>
00313          <para>This application sets the following channel variable upon completion:</para>
00314          <variablelist>
00315             <variable name="PQMSTATUS">
00316                <para>The status of the attempt to pause a queue member as a text string.</para>
00317                <value name="PAUSED" />
00318                <value name="NOTFOUND" />
00319             </variable>
00320          </variablelist>
00321          <para>Example: PauseQueueMember(,SIP/3000)</para>
00322       </description>
00323       <see-also>
00324          <ref type="application">UnpauseQueueMember</ref>
00325       </see-also>
00326    </application>
00327    <application name="UnpauseQueueMember" language="en_US">
00328       <synopsis>
00329          Unpauses a queue member.      
00330       </synopsis>
00331       <syntax>
00332          <parameter name="queuename" />
00333          <parameter name="interface" required="true" />
00334          <parameter name="options" />
00335          <parameter name="reason">
00336             <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
00337          </parameter>
00338       </syntax>
00339       <description>
00340          <para>Unpauses (resumes calls to) a queue member. This is the counterpart to <literal>PauseQueueMember()</literal>
00341          and operates exactly the same way, except it unpauses instead of pausing the given interface.</para>
00342          <para>This application sets the following channel variable upon completion:</para>
00343          <variablelist>
00344             <variable name="UPQMSTATUS">
00345                <para>The status of the attempt to unpause a queue member as a text string.</para>
00346                <value name="UNPAUSED" />
00347                <value name="NOTFOUND" />
00348             </variable>
00349          </variablelist>
00350          <para>Example: UnpauseQueueMember(,SIP/3000)</para>
00351       </description>
00352       <see-also>
00353          <ref type="application">PauseQueueMember</ref>
00354       </see-also>
00355    </application>
00356    <application name="QueueLog" language="en_US">
00357       <synopsis>
00358          Writes to the queue_log file.
00359       </synopsis>
00360       <syntax>
00361          <parameter name="queuename" required="true" />
00362          <parameter name="uniqueid" required="true" />
00363          <parameter name="agent" required="true" />
00364          <parameter name="event" required="true" />
00365          <parameter name="additionalinfo" />
00366       </syntax>
00367       <description>
00368          <para>Allows you to write your own events into the queue log.</para>
00369          <para>Example: QueueLog(101,${UNIQUEID},${AGENT},WENTONBREAK,600)</para>
00370       </description>
00371       <see-also>
00372          <ref type="application">Queue</ref>
00373       </see-also>
00374    </application>
00375    <function name="QUEUE_VARIABLES" language="en_US">
00376       <synopsis>
00377          Return Queue information in variables.
00378       </synopsis>
00379       <syntax>
00380          <parameter name="queuename" required="true">
00381             <enumlist>
00382                <enum name="QUEUEMAX">
00383                   <para>Maxmimum number of calls allowed.</para>
00384                </enum>
00385                <enum name="QUEUESTRATEGY">
00386                   <para>The strategy of the queue.</para>
00387                </enum>
00388                <enum name="QUEUECALLS">
00389                   <para>Number of calls currently in the queue.</para>
00390                </enum>
00391                <enum name="QUEUEHOLDTIME">
00392                   <para>Current average hold time.</para>
00393                </enum>
00394                <enum name="QUEUECOMPLETED">
00395                   <para>Number of completed calls for the queue.</para>
00396                </enum>
00397                <enum name="QUEUEABANDONED">
00398                   <para>Number of abandoned calls.</para>
00399                </enum>
00400                <enum name="QUEUESRVLEVEL">
00401                   <para>Queue service level.</para>
00402                </enum>
00403                <enum name="QUEUESRVLEVELPERF">
00404                   <para>Current service level performance.</para>
00405                </enum>
00406             </enumlist>
00407          </parameter>
00408       </syntax>
00409       <description>
00410          <para>Makes the following queue variables available.</para>
00411          <para>Returns <literal>0</literal> if queue is found and setqueuevar is defined, <literal>-1</literal> otherwise.</para>
00412       </description>
00413    </function>
00414    <function name="QUEUE_MEMBER" language="en_US">
00415       <synopsis>
00416          Count number of members answering a queue.
00417       </synopsis>
00418       <syntax>
00419          <parameter name="queuename" required="true" />
00420          <parameter name="option" required="true">
00421             <enumlist>
00422                <enum name="logged">
00423                   <para>Returns the number of logged-in members for the specified queue.</para>
00424                </enum>
00425                <enum name="free">
00426                   <para>Returns the number of logged-in members for the specified queue available to take a call.</para>
00427                </enum>
00428                <enum name="count">
00429                   <para>Returns the total number of members for the specified queue.</para>
00430                </enum>
00431             </enumlist>
00432          </parameter>
00433       </syntax>
00434       <description>
00435          <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
00436       </description>
00437    </function>
00438    <function name="QUEUE_MEMBER_COUNT" language="en_US">
00439       <synopsis>
00440          Count number of members answering a queue.
00441       </synopsis>
00442       <syntax>
00443          <parameter name="queuename" required="true" />
00444       </syntax>
00445       <description>
00446          <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
00447          <warning><para>This function has been deprecated in favor of the <literal>QUEUE_MEMBER()</literal> function</para></warning>
00448       </description>
00449       <see-also>
00450          <ref type="function">QUEUE_MEMBER_LIST</ref>
00451       </see-also>
00452    </function>
00453    <function name="QUEUE_WAITING_COUNT" language="en_US">
00454       <synopsis>
00455          Count number of calls currently waiting in a queue.
00456       </synopsis>
00457       <syntax>
00458          <parameter name="queuename" />
00459       </syntax>
00460       <description>
00461          <para>Returns the number of callers currently waiting in the specified <replaceable>queuename</replaceable>.</para>
00462       </description>
00463    </function>
00464    <function name="QUEUE_MEMBER_LIST" language="en_US">
00465       <synopsis>
00466          Returns a list of interfaces on a queue.
00467       </synopsis>
00468       <syntax>
00469          <parameter name="queuename" required="true" />
00470       </syntax>
00471       <description>
00472          <para>Returns a comma-separated list of members associated with the specified <replaceable>queuename</replaceable>.</para>
00473       </description>
00474       <see-also>
00475          <ref type="function">QUEUE_MEMBER_COUNT</ref>
00476       </see-also>
00477    </function>
00478    <function name="QUEUE_MEMBER_PENALTY" language="en_US">
00479       <synopsis>
00480          Gets or sets queue members penalty.
00481       </synopsis>
00482       <syntax>
00483          <parameter name="queuename" required="true" />
00484          <parameter name="interface" required="true" />
00485       </syntax>
00486       <description>
00487          <para>Gets or sets queue members penalty.</para>
00488       </description>
00489    </function>
00490 
00491  ***/
00492 
00493 enum {
00494    QUEUE_STRATEGY_RINGALL = 0,
00495    QUEUE_STRATEGY_LEASTRECENT,
00496    QUEUE_STRATEGY_FEWESTCALLS,
00497    QUEUE_STRATEGY_RANDOM,
00498    QUEUE_STRATEGY_RRMEMORY,
00499    QUEUE_STRATEGY_LINEAR,
00500    QUEUE_STRATEGY_WRANDOM
00501 };
00502 
00503 enum queue_reload_mask {
00504    QUEUE_RELOAD_PARAMETERS = (1 << 0),
00505    QUEUE_RELOAD_MEMBER = (1 << 1),
00506    QUEUE_RELOAD_RULES = (1 << 2),
00507    QUEUE_RESET_STATS = (1 << 3),
00508 };
00509 
00510 static const struct strategy {
00511    int strategy;
00512    const char *name;
00513 } strategies[] = {
00514    { QUEUE_STRATEGY_RINGALL, "ringall" },
00515    { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
00516    { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
00517    { QUEUE_STRATEGY_RANDOM, "random" },
00518    { QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
00519    { QUEUE_STRATEGY_RRMEMORY, "roundrobin" },
00520    { QUEUE_STRATEGY_LINEAR, "linear" },
00521    { QUEUE_STRATEGY_WRANDOM, "wrandom"},
00522 };
00523 
00524 static struct ast_taskprocessor *devicestate_tps;
00525 
00526 #define DEFAULT_RETRY      5
00527 #define DEFAULT_TIMEOUT    15
00528 #define RECHECK         1     /*!< Recheck every second to see we we're at the top yet */
00529 #define MAX_PERIODIC_ANNOUNCEMENTS 10           /*!< The maximum periodic announcements we can have */
00530 #define DEFAULT_MIN_ANNOUNCE_FREQUENCY 15       /*!< The minimum number of seconds between position announcements \
00531                                                      The default value of 15 provides backwards compatibility */
00532 #define MAX_QUEUE_BUCKETS 53
00533 
00534 #define  RES_OKAY 0     /*!< Action completed */
00535 #define  RES_EXISTS  (-1)     /*!< Entry already exists */
00536 #define  RES_OUTOFMEMORY   (-2)     /*!< Out of memory */
00537 #define  RES_NOSUCHQUEUE   (-3)     /*!< No such queue */
00538 #define RES_NOT_DYNAMIC (-4)     /*!< Member is not dynamic */
00539 
00540 static char *app = "Queue";
00541 
00542 static char *app_aqm = "AddQueueMember" ;
00543 
00544 static char *app_rqm = "RemoveQueueMember" ;
00545 
00546 static char *app_pqm = "PauseQueueMember" ;
00547 
00548 static char *app_upqm = "UnpauseQueueMember" ;
00549 
00550 static char *app_ql = "QueueLog" ;
00551 
00552 /*! \brief Persistent Members astdb family */
00553 static const char *pm_family = "Queue/PersistentMembers";
00554 /* The maximum length of each persistent member queue database entry */
00555 #define PM_MAX_LEN 8192
00556 
00557 /*! \brief queues.conf [general] option */
00558 static int queue_persistent_members = 0;
00559 
00560 /*! \brief queues.conf per-queue weight option */
00561 static int use_weight = 0;
00562 
00563 /*! \brief queues.conf [general] option */
00564 static int autofill_default = 0;
00565 
00566 /*! \brief queues.conf [general] option */
00567 static int montype_default = 0;
00568 
00569 /*! \brief queues.conf [general] option */
00570 static int shared_lastcall = 0;
00571 
00572 /*! \brief Subscription to device state change events */
00573 static struct ast_event_sub *device_state_sub;
00574 
00575 /*! \brief queues.conf [general] option */
00576 static int update_cdr = 0;
00577 
00578 enum queue_result {
00579    QUEUE_UNKNOWN = 0,
00580    QUEUE_TIMEOUT = 1,
00581    QUEUE_JOINEMPTY = 2,
00582    QUEUE_LEAVEEMPTY = 3,
00583    QUEUE_JOINUNAVAIL = 4,
00584    QUEUE_LEAVEUNAVAIL = 5,
00585    QUEUE_FULL = 6,
00586    QUEUE_CONTINUE = 7,
00587 };
00588 
00589 const struct {
00590    enum queue_result id;
00591    char *text;
00592 } queue_results[] = {
00593    { QUEUE_UNKNOWN, "UNKNOWN" },
00594    { QUEUE_TIMEOUT, "TIMEOUT" },
00595    { QUEUE_JOINEMPTY,"JOINEMPTY" },
00596    { QUEUE_LEAVEEMPTY, "LEAVEEMPTY" },
00597    { QUEUE_JOINUNAVAIL, "JOINUNAVAIL" },
00598    { QUEUE_LEAVEUNAVAIL, "LEAVEUNAVAIL" },
00599    { QUEUE_FULL, "FULL" },
00600    { QUEUE_CONTINUE, "CONTINUE" },
00601 };
00602 
00603 enum queue_timeout_priority {
00604    TIMEOUT_PRIORITY_APP,
00605    TIMEOUT_PRIORITY_CONF,
00606 };
00607 
00608 /*! \brief We define a custom "local user" structure because we
00609  *  use it not only for keeping track of what is in use but
00610  *  also for keeping track of who we're dialing.
00611  *
00612  *  There are two "links" defined in this structure, q_next and call_next.
00613  *  q_next links ALL defined callattempt structures into a linked list. call_next is
00614  *  a link which allows for a subset of the callattempts to be traversed. This subset
00615  *  is used in wait_for_answer so that irrelevant callattempts are not traversed. This
00616  *  also is helpful so that queue logs are always accurate in the case where a call to 
00617  *  a member times out, especially if using the ringall strategy. 
00618 */
00619 
00620 struct callattempt {
00621    struct callattempt *q_next;
00622    struct callattempt *call_next;
00623    struct ast_channel *chan;
00624    char interface[256];
00625    int stillgoing;
00626    int metric;
00627    int oldstatus;
00628    time_t lastcall;
00629    struct call_queue *lastqueue;
00630    struct member *member;
00631 };
00632 
00633 
00634 struct queue_ent {
00635    struct call_queue *parent;             /*!< What queue is our parent */
00636    char moh[80];                          /*!< Name of musiconhold to be used */
00637    char announce[80];                     /*!< Announcement to play for member when call is answered */
00638    char context[AST_MAX_CONTEXT];         /*!< Context when user exits queue */
00639    char digits[AST_MAX_EXTENSION];        /*!< Digits entered while in queue */
00640    int valid_digits;                      /*!< Digits entered correspond to valid extension. Exited */
00641    int pos;                               /*!< Where we are in the queue */
00642    int prio;                              /*!< Our priority */
00643    int last_pos_said;                     /*!< Last position we told the user */
00644    time_t last_periodic_announce_time;    /*!< The last time we played a periodic announcement */
00645    int last_periodic_announce_sound;      /*!< The last periodic announcement we made */
00646    time_t last_pos;                       /*!< Last time we told the user their position */
00647    int opos;                              /*!< Where we started in the queue */
00648    int handled;                           /*!< Whether our call was handled */
00649    int pending;                           /*!< Non-zero if we are attempting to call a member */
00650    int max_penalty;                       /*!< Limit the members that can take this call to this penalty or lower */
00651    int min_penalty;                       /*!< Limit the members that can take this call to this penalty or higher */
00652    int linpos;                            /*!< If using linear strategy, what position are we at? */
00653    int linwrapped;                        /*!< Is the linpos wrapped? */
00654    time_t start;                          /*!< When we started holding */
00655    time_t expire;                         /*!< When this entry should expire (time out of queue) */
00656    int cancel_answered_elsewhere;          /*!< Whether we should force the CAE flag on this call (C) option*/
00657    struct ast_channel *chan;              /*!< Our channel */
00658    AST_LIST_HEAD_NOLOCK(,penalty_rule) qe_rules; /*!< Local copy of the queue's penalty rules */
00659    struct penalty_rule *pr;               /*!< Pointer to the next penalty rule to implement */
00660    struct queue_ent *next;                /*!< The next queue entry */
00661 };
00662 
00663 struct member {
00664    char interface[80];                 /*!< Technology/Location to dial to reach this member*/
00665    char state_interface[80];           /*!< Technology/Location from which to read devicestate changes */
00666    char membername[80];                /*!< Member name to use in queue logs */
00667    int penalty;                        /*!< Are we a last resort? */
00668    int calls;                          /*!< Number of calls serviced by this member */
00669    int dynamic;                        /*!< Are we dynamically added? */
00670    int realtime;                       /*!< Is this member realtime? */
00671    int status;                         /*!< Status of queue member */
00672    int paused;                         /*!< Are we paused (not accepting calls)? */
00673    time_t lastcall;                    /*!< When last successful call was hungup */
00674    struct call_queue *lastqueue;     /*!< Last queue we received a call */
00675    unsigned int dead:1;                /*!< Used to detect members deleted in realtime */
00676    unsigned int delme:1;               /*!< Flag to delete entry on reload */
00677    char rt_uniqueid[80];               /*!< Unique id of realtime member entry */
00678 };
00679 
00680 enum empty_conditions {
00681    QUEUE_EMPTY_PENALTY = (1 << 0),
00682    QUEUE_EMPTY_PAUSED = (1 << 1),
00683    QUEUE_EMPTY_INUSE = (1 << 2),
00684    QUEUE_EMPTY_RINGING = (1 << 3),
00685    QUEUE_EMPTY_UNAVAILABLE = (1 << 4),
00686    QUEUE_EMPTY_INVALID = (1 << 5),
00687    QUEUE_EMPTY_UNKNOWN = (1 << 6),
00688    QUEUE_EMPTY_WRAPUP = (1 << 7),
00689 };
00690 
00691 /* values used in multi-bit flags in call_queue */
00692 #define ANNOUNCEHOLDTIME_ALWAYS 1
00693 #define ANNOUNCEHOLDTIME_ONCE 2
00694 #define QUEUE_EVENT_VARIABLES 3
00695 
00696 struct penalty_rule {
00697    int time;                           /*!< Number of seconds that need to pass before applying this rule */
00698    int max_value;                      /*!< The amount specified in the penalty rule for max penalty */
00699    int min_value;                      /*!< The amount specified in the penalty rule for min penalty */
00700    int max_relative;                   /*!< Is the max adjustment relative? 1 for relative, 0 for absolute */
00701    int min_relative;                   /*!< Is the min adjustment relative? 1 for relative, 0 for absolute */
00702    AST_LIST_ENTRY(penalty_rule) list;  /*!< Next penalty_rule */
00703 };
00704 
00705 #define ANNOUNCEPOSITION_YES 1 /*!< We announce position */
00706 #define ANNOUNCEPOSITION_NO 2 /*!< We don't announce position */
00707 #define ANNOUNCEPOSITION_MORE_THAN 3 /*!< We say "Currently there are more than <limit>" */
00708 #define ANNOUNCEPOSITION_LIMIT 4 /*!< We not announce position more than <limit> */
00709 
00710 struct call_queue {
00711    /*! Queue name */
00712    char name[80];
00713    /*! Music on Hold class */
00714    char moh[80];
00715    /*! Announcement to play when call is answered */
00716    char announce[80];
00717    /*! Exit context */
00718    char context[AST_MAX_CONTEXT];
00719    /*! Macro to run upon member connection */
00720    char membermacro[AST_MAX_CONTEXT];
00721    /*! Gosub to run upon member connection */
00722    char membergosub[AST_MAX_CONTEXT];
00723    /*! Default rule to use if none specified in call to Queue() */
00724    char defaultrule[AST_MAX_CONTEXT];
00725    /*! Sound file: "Your call is now first in line" (def. queue-youarenext) */
00726    char sound_next[80];
00727    /*! Sound file: "There are currently" (def. queue-thereare) */
00728    char sound_thereare[80];
00729    /*! Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting) */
00730    char sound_calls[80];
00731    /*! Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
00732    char sound_holdtime[80];
00733    /*! Sound file: "minutes." (def. queue-minutes) */
00734    char sound_minutes[80];
00735    /*! Sound file: "minute." (def. queue-minute) */
00736    char sound_minute[80];
00737    /*! Sound file: "seconds." (def. queue-seconds) */
00738    char sound_seconds[80];
00739    /*! Sound file: "Thank you for your patience." (def. queue-thankyou) */
00740    char sound_thanks[80];
00741    /*! Sound file: Custom announce for caller, no default */
00742    char sound_callerannounce[80];
00743    /*! Sound file: "Hold time" (def. queue-reporthold) */
00744    char sound_reporthold[80];
00745    /*! Sound file: "Currently there are more than" (def. queue-quantity1) */
00746    char queue_quantity1[80];
00747    /*! Sound file: "callers waiting to speak with a representative" (def. queue-quantity2) */
00748    char queue_quantity2[80];
00749    /*! Sound files: Custom announce, no default */
00750    struct ast_str *sound_periodicannounce[MAX_PERIODIC_ANNOUNCEMENTS];
00751    unsigned int dead:1;
00752    unsigned int eventwhencalled:2;
00753    unsigned int ringinuse:1;
00754    unsigned int setinterfacevar:1;
00755    unsigned int setqueuevar:1;
00756    unsigned int setqueueentryvar:1;
00757    unsigned int reportholdtime:1;
00758    unsigned int wrapped:1;
00759    unsigned int timeoutrestart:1;
00760    unsigned int announceholdtime:2;
00761    unsigned int announceposition:3;
00762    int strategy:4;
00763    unsigned int maskmemberstatus:1;
00764    unsigned int realtime:1;
00765    unsigned int found:1;
00766    enum empty_conditions joinempty;
00767    enum empty_conditions leavewhenempty;
00768    int announcepositionlimit;          /*!< How many positions we announce? */
00769    int announcefrequency;              /*!< How often to announce their position */
00770    int minannouncefrequency;           /*!< The minimum number of seconds between position announcements (def. 15) */
00771    int periodicannouncefrequency;      /*!< How often to play periodic announcement */
00772    int numperiodicannounce;            /*!< The number of periodic announcements configured */
00773    int randomperiodicannounce;         /*!< Are periodic announcments randomly chosen */
00774    int roundingseconds;                /*!< How many seconds do we round to? */
00775    int holdtime;                       /*!< Current avg holdtime, based on an exponential average */
00776    int talktime;                       /*!< Current avg talktime, based on the same exponential average */
00777    int callscompleted;                 /*!< Number of queue calls completed */
00778    int callsabandoned;                 /*!< Number of queue calls abandoned */
00779    int servicelevel;                   /*!< seconds setting for servicelevel*/
00780    int callscompletedinsl;             /*!< Number of calls answered with servicelevel*/
00781    char monfmt[8];                     /*!< Format to use when recording calls */
00782    int montype;                        /*!< Monitor type  Monitor vs. MixMonitor */
00783    int count;                          /*!< How many entries */
00784    int maxlen;                         /*!< Max number of entries */
00785    int wrapuptime;                     /*!< Wrapup Time */
00786 
00787    int retry;                          /*!< Retry calling everyone after this amount of time */
00788    int timeout;                        /*!< How long to wait for an answer */
00789    int weight;                         /*!< Respective weight */
00790    int autopause;                      /*!< Auto pause queue members if they fail to answer */
00791    int timeoutpriority;                /*!< Do we allow a fraction of the timeout to occur for a ring? */
00792 
00793    /* Queue strategy things */
00794    int rrpos;                          /*!< Round Robin - position */
00795    int memberdelay;                    /*!< Seconds to delay connecting member to caller */
00796    int autofill;                       /*!< Ignore the head call status and ring an available agent */
00797    
00798    struct ao2_container *members;             /*!< Head of the list of members */
00799    /*! 
00800     * \brief Number of members _logged in_
00801     * \note There will be members in the members container that are not logged
00802     *       in, so this can not simply be replaced with ao2_container_count(). 
00803     */
00804    int membercount;
00805    struct queue_ent *head;             /*!< Head of the list of callers */
00806    AST_LIST_ENTRY(call_queue) list;    /*!< Next call queue */
00807    AST_LIST_HEAD_NOLOCK(, penalty_rule) rules; /*!< The list of penalty rules to invoke */
00808 };
00809 
00810 struct rule_list {
00811    char name[80];
00812    AST_LIST_HEAD_NOLOCK(,penalty_rule) rules;
00813    AST_LIST_ENTRY(rule_list) list;
00814 };
00815 
00816 AST_LIST_HEAD_STATIC(rule_lists, rule_list);
00817 
00818 static struct ao2_container *queues;
00819 
00820 static void update_realtime_members(struct call_queue *q);
00821 static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused);
00822 
00823 static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan); 
00824 /*! \brief sets the QUEUESTATUS channel variable */
00825 static void set_queue_result(struct ast_channel *chan, enum queue_result res)
00826 {
00827    int i;
00828 
00829    for (i = 0; i < ARRAY_LEN(queue_results); i++) {
00830       if (queue_results[i].id == res) {
00831          pbx_builtin_setvar_helper(chan, "QUEUESTATUS", queue_results[i].text);
00832          return;
00833       }
00834    }
00835 }
00836 
00837 static const char *int2strat(int strategy)
00838 {
00839    int x;
00840 
00841    for (x = 0; x < ARRAY_LEN(strategies); x++) {
00842       if (strategy == strategies[x].strategy)
00843          return strategies[x].name;
00844    }
00845 
00846    return "<unknown>";
00847 }
00848 
00849 static int strat2int(const char *strategy)
00850 {
00851    int x;
00852 
00853    for (x = 0; x < ARRAY_LEN(strategies); x++) {
00854       if (!strcasecmp(strategy, strategies[x].name))
00855          return strategies[x].strategy;
00856    }
00857 
00858    return -1;
00859 }
00860 
00861 static int queue_hash_cb(const void *obj, const int flags)
00862 {
00863    const struct call_queue *q = obj;
00864 
00865    return ast_str_case_hash(q->name);
00866 }
00867 
00868 static int queue_cmp_cb(void *obj, void *arg, int flags)
00869 {
00870    struct call_queue *q = obj, *q2 = arg;
00871    return !strcasecmp(q->name, q2->name) ? CMP_MATCH | CMP_STOP : 0;
00872 }
00873 
00874 #ifdef REF_DEBUG_ONLY_QUEUES
00875 #define queue_ref(a) __ao2_ref_debug(a,1,"",__FILE__,__LINE__,__PRETTY_FUNCTION__)
00876 #define queue_unref(a)  __ao2_ref_debug(a,-1,"",__FILE__,__LINE__,__PRETTY_FUNCTION__)
00877 #define queue_t_ref(a,b)   __ao2_ref_debug(a,1,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00878 #define queue_t_unref(a,b) __ao2_ref_debug(a,-1,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00879 #define queues_t_link(c,q,tag)   __ao2_link_debug(c,q,tag,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00880 #define queues_t_unlink(c,q,tag) __ao2_unlink_debug(c,q,tag,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00881 #else
00882 #define queue_t_ref(a,b)   queue_ref(a)
00883 #define queue_t_unref(a,b) queue_unref(a)
00884 #define queues_t_link(c,q,tag)   ao2_t_link(c,q,tag)
00885 #define queues_t_unlink(c,q,tag) ao2_t_unlink(c,q,tag)
00886 static inline struct call_queue *queue_ref(struct call_queue *q)
00887 {
00888    ao2_ref(q, 1);
00889    return q;
00890 }
00891 
00892 static inline struct call_queue *queue_unref(struct call_queue *q)
00893 {
00894    ao2_ref(q, -1);
00895    return q;
00896 }
00897 #endif
00898 
00899 /*! \brief Set variables of queue */
00900 static void set_queue_variables(struct call_queue *q, struct ast_channel *chan)
00901 {
00902    char interfacevar[256]="";
00903    float sl = 0;
00904 
00905    if (q->setqueuevar) {
00906       sl = 0;
00907       if (q->callscompleted > 0) 
00908          sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
00909 
00910       snprintf(interfacevar, sizeof(interfacevar),
00911          "QUEUENAME=%s,QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
00912          q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned,  q->servicelevel, sl);
00913    
00914       pbx_builtin_setvar_multiple(chan, interfacevar); 
00915    }
00916 }
00917 
00918 /*! \brief Insert the 'new' entry after the 'prev' entry of queue 'q' */
00919 static inline void insert_entry(struct call_queue *q, struct queue_ent *prev, struct queue_ent *new, int *pos)
00920 {
00921    struct queue_ent *cur;
00922 
00923    if (!q || !new)
00924       return;
00925    if (prev) {
00926       cur = prev->next;
00927       prev->next = new;
00928    } else {
00929       cur = q->head;
00930       q->head = new;
00931    }
00932    new->next = cur;
00933 
00934    /* every queue_ent must have a reference to it's parent call_queue, this
00935     * reference does not go away until the end of the queue_ent's life, meaning
00936     * that even when the queue_ent leaves the call_queue this ref must remain. */
00937    queue_ref(q);
00938    new->parent = q;
00939    new->pos = ++(*pos);
00940    new->opos = *pos;
00941 }
00942 
00943 /*! \brief Check if members are available
00944  *
00945  * This function checks to see if members are available to be called. If any member
00946  * is available, the function immediately returns 0. If no members are available,
00947  * then -1 is returned.
00948  */
00949 static int get_member_status(struct call_queue *q, int max_penalty, int min_penalty, enum empty_conditions conditions)
00950 {
00951    struct member *member;
00952    struct ao2_iterator mem_iter;
00953 
00954    ao2_lock(q);
00955    mem_iter = ao2_iterator_init(q->members, 0);
00956    for (; (member = ao2_iterator_next(&mem_iter)); ao2_ref(member, -1)) {
00957       if ((max_penalty && (member->penalty > max_penalty)) || (min_penalty && (member->penalty < min_penalty))) {
00958          if (conditions & QUEUE_EMPTY_PENALTY) {
00959             ast_debug(4, "%s is unavailable because his penalty is not between %d and %d\n", member->membername, min_penalty, max_penalty);
00960             continue;
00961          }
00962       }
00963 
00964       switch (member->status) {
00965       case AST_DEVICE_INVALID:
00966          if (conditions & QUEUE_EMPTY_INVALID) {
00967             ast_debug(4, "%s is unavailable because his device state is 'invalid'\n", member->membername);
00968             break;
00969          }
00970          goto default_case;
00971       case AST_DEVICE_UNAVAILABLE:
00972          if (conditions & QUEUE_EMPTY_UNAVAILABLE) {
00973             ast_debug(4, "%s is unavailable because his device state is 'unavailable'\n", member->membername);
00974             break;
00975          }
00976          goto default_case;
00977       case AST_DEVICE_INUSE:
00978          if (conditions & QUEUE_EMPTY_INUSE) {
00979             ast_debug(4, "%s is unavailable because his device state is 'inuse'\n", member->membername);
00980             break;
00981          }
00982          goto default_case;
00983       case AST_DEVICE_RINGING:
00984          if (conditions & QUEUE_EMPTY_RINGING) {
00985             ast_debug(4, "%s is unavailable because his device state is 'ringing'\n", member->membername);
00986             break;
00987          }
00988       case AST_DEVICE_UNKNOWN:
00989          if (conditions & QUEUE_EMPTY_UNKNOWN) {
00990             ast_debug(4, "%s is unavailable because his device state is 'unknown'\n", member->membername);
00991             break;
00992          }
00993          /* Fall-through */
00994       default:
00995       default_case:
00996          if (member->paused && (conditions & QUEUE_EMPTY_PAUSED)) {
00997             ast_debug(4, "%s is unavailable because he is paused'\n", member->membername);
00998             break;
00999          } else if ((conditions & QUEUE_EMPTY_WRAPUP) && member->lastcall && q->wrapuptime && (time(NULL) - q->wrapuptime < member->lastcall)) {
01000             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);
01001             break;
01002          } else {
01003             ao2_unlock(q);
01004             ao2_ref(member, -1);
01005             ao2_iterator_destroy(&mem_iter);
01006             ast_debug(4, "%s is available.\n", member->membername);
01007             return 0;
01008          }
01009          break;
01010       }
01011    }
01012    ao2_iterator_destroy(&mem_iter);
01013 
01014    ao2_unlock(q);
01015    return -1;
01016 }
01017 
01018 struct statechange {
01019    AST_LIST_ENTRY(statechange) entry;
01020    int state;
01021    char dev[0];
01022 };
01023 
01024 /*! \brief set a member's status based on device state of that member's state_interface.
01025  *  
01026  * Lock interface list find sc, iterate through each queues queue_member list for member to
01027  * update state inside queues
01028 */
01029 static int update_status(struct call_queue *q, struct member *m, const int status)
01030 {
01031    m->status = status;
01032 
01033    if (q->maskmemberstatus)
01034       return 0;
01035 
01036    manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
01037       "Queue: %s\r\n"
01038       "Location: %s\r\n"
01039       "MemberName: %s\r\n"
01040       "Membership: %s\r\n"
01041       "Penalty: %d\r\n"
01042       "CallsTaken: %d\r\n"
01043       "LastCall: %d\r\n"
01044       "Status: %d\r\n"
01045       "Paused: %d\r\n",
01046       q->name, m->interface, m->membername, m->dynamic ? "dynamic" : m->realtime ? "realtime" : "static",
01047       m->penalty, m->calls, (int)m->lastcall, m->status, m->paused
01048    );
01049 
01050    return 0;
01051 }
01052 
01053 /*! \brief set a member's status based on device state of that member's interface*/
01054 static int handle_statechange(void *datap)
01055 {
01056    struct statechange *sc = datap;
01057    struct ao2_iterator miter, qiter;
01058    struct member *m;
01059    struct call_queue *q;
01060    char interface[80], *slash_pos;
01061    int found = 0;
01062 
01063    qiter = ao2_iterator_init(queues, 0);
01064    while ((q = ao2_t_iterator_next(&qiter, "Iterate over queues"))) {
01065       ao2_lock(q);
01066 
01067       miter = ao2_iterator_init(q->members, 0);
01068       for (; (m = ao2_iterator_next(&miter)); ao2_ref(m, -1)) {
01069          ast_copy_string(interface, m->state_interface, sizeof(interface));
01070 
01071          if ((slash_pos = strchr(interface, '/')))
01072             if (!strncasecmp(interface, "Local/", 6) && (slash_pos = strchr(slash_pos + 1, '/')))
01073                *slash_pos = '\0';
01074 
01075          if (!strcasecmp(interface, sc->dev)) {
01076             found = 1;
01077             update_status(q, m, sc->state);
01078             ao2_ref(m, -1);
01079             break;
01080          }
01081       }
01082       ao2_iterator_destroy(&miter);
01083 
01084       ao2_unlock(q);
01085       queue_t_unref(q, "Done with iterator");
01086    }
01087    ao2_iterator_destroy(&qiter);
01088 
01089    if (found)
01090       ast_debug(1, "Device '%s' changed to state '%d' (%s)\n", sc->dev, sc->state, ast_devstate2str(sc->state));
01091    else
01092       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));
01093 
01094    ast_free(sc);
01095    return 0;
01096 }
01097 
01098 static void device_state_cb(const struct ast_event *event, void *unused)
01099 {
01100    enum ast_device_state state;
01101    const char *device;
01102    struct statechange *sc;
01103    size_t datapsize;
01104 
01105    state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
01106    device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
01107 
01108    if (ast_strlen_zero(device)) {
01109       ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
01110       return;
01111    }
01112    datapsize = sizeof(*sc) + strlen(device) + 1;
01113    if (!(sc = ast_calloc(1, datapsize))) {
01114       ast_log(LOG_ERROR, "failed to calloc a state change struct\n");
01115       return;
01116    }
01117    sc->state = state;
01118    strcpy(sc->dev, device);
01119    if (ast_taskprocessor_push(devicestate_tps, handle_statechange, sc) < 0) {
01120       ast_free(sc);
01121    }
01122 }
01123 
01124 /*! \brief allocate space for new queue member and set fields based on parameters passed */
01125 static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused, const char *state_interface)
01126 {
01127    struct member *cur;
01128    
01129    if ((cur = ao2_alloc(sizeof(*cur), NULL))) {
01130       cur->penalty = penalty;
01131       cur->paused = paused;
01132       ast_copy_string(cur->interface, interface, sizeof(cur->interface));
01133       if (!ast_strlen_zero(state_interface))
01134          ast_copy_string(cur->state_interface, state_interface, sizeof(cur->state_interface));
01135       else
01136          ast_copy_string(cur->state_interface, interface, sizeof(cur->state_interface));
01137       if (!ast_strlen_zero(membername))
01138          ast_copy_string(cur->membername, membername, sizeof(cur->membername));
01139       else
01140          ast_copy_string(cur->membername, interface, sizeof(cur->membername));
01141       if (!strchr(cur->interface, '/'))
01142          ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
01143       cur->status = ast_device_state(cur->state_interface);
01144    }
01145 
01146    return cur;
01147 }
01148 
01149 
01150 static int compress_char(const char c)
01151 {
01152    if (c < 32)
01153       return 0;
01154    else if (c > 96)
01155       return c - 64;
01156    else
01157       return c - 32;
01158 }
01159 
01160 static int member_hash_fn(const void *obj, const int flags)
01161 {
01162    const struct member *mem = obj;
01163    const char *chname = strchr(mem->interface, '/');
01164    int ret = 0, i;
01165    if (!chname)
01166       chname = mem->interface;
01167    for (i = 0; i < 5 && chname[i]; i++)
01168       ret += compress_char(chname[i]) << (i * 6);
01169    return ret;
01170 }
01171 
01172 static int member_cmp_fn(void *obj1, void *obj2, int flags)
01173 {
01174    struct member *mem1 = obj1, *mem2 = obj2;
01175    return strcasecmp(mem1->interface, mem2->interface) ? 0 : CMP_MATCH | CMP_STOP;
01176 }
01177 
01178 /*! 
01179  * \brief Initialize Queue default values.
01180  * \note the queue's lock  must be held before executing this function
01181 */
01182 static void init_queue(struct call_queue *q)
01183 {
01184    int i;
01185    struct penalty_rule *pr_iter;
01186 
01187    q->dead = 0;
01188    q->retry = DEFAULT_RETRY;
01189    q->timeout = DEFAULT_TIMEOUT;
01190    q->maxlen = 0;
01191    q->announcefrequency = 0;
01192    q->minannouncefrequency = DEFAULT_MIN_ANNOUNCE_FREQUENCY;
01193    q->announceholdtime = 1;
01194    q->announcepositionlimit = 10; /* Default 10 positions */
01195    q->announceposition = ANNOUNCEPOSITION_YES; /* Default yes */
01196    q->roundingseconds = 0; /* Default - don't announce seconds */
01197    q->servicelevel = 0;
01198    q->ringinuse = 1;
01199    q->setinterfacevar = 0;
01200    q->setqueuevar = 0;
01201    q->setqueueentryvar = 0;
01202    q->autofill = autofill_default;
01203    q->montype = montype_default;
01204    q->monfmt[0] = '\0';
01205    q->reportholdtime = 0;
01206    q->wrapuptime = 0;
01207    q->joinempty = 0;
01208    q->leavewhenempty = 0;
01209    q->memberdelay = 0;
01210    q->maskmemberstatus = 0;
01211    q->eventwhencalled = 0;
01212    q->weight = 0;
01213    q->timeoutrestart = 0;
01214    q->periodicannouncefrequency = 0;
01215    q->randomperiodicannounce = 0;
01216    q->numperiodicannounce = 0;
01217    q->timeoutpriority = TIMEOUT_PRIORITY_APP;
01218    if (!q->members) {
01219       if (q->strategy == QUEUE_STRATEGY_LINEAR)
01220          /* linear strategy depends on order, so we have to place all members in a single bucket */
01221          q->members = ao2_container_alloc(1, member_hash_fn, member_cmp_fn);
01222       else
01223          q->members = ao2_container_alloc(37, member_hash_fn, member_cmp_fn);
01224    }
01225    q->found = 1;
01226 
01227    ast_copy_string(q->sound_next, "queue-youarenext", sizeof(q->sound_next));
01228    ast_copy_string(q->sound_thereare, "queue-thereare", sizeof(q->sound_thereare));
01229    ast_copy_string(q->sound_calls, "queue-callswaiting", sizeof(q->sound_calls));
01230    ast_copy_string(q->sound_holdtime, "queue-holdtime", sizeof(q->sound_holdtime));
01231    ast_copy_string(q->sound_minutes, "queue-minutes", sizeof(q->sound_minutes));
01232    ast_copy_string(q->sound_minute, "queue-minute", sizeof(q->sound_minute));
01233    ast_copy_string(q->sound_seconds, "queue-seconds", sizeof(q->sound_seconds));
01234    ast_copy_string(q->sound_thanks, "queue-thankyou", sizeof(q->sound_thanks));
01235    ast_copy_string(q->sound_reporthold, "queue-reporthold", sizeof(q->sound_reporthold));
01236    ast_copy_string(q->queue_quantity1, "queue-quantity1", sizeof(q->queue_quantity1));
01237    ast_copy_string(q->queue_quantity2, "queue-quantity2", sizeof(q->queue_quantity2));
01238 
01239    if (!q->sound_periodicannounce[0]) {
01240       q->sound_periodicannounce[0] = ast_str_create(32);
01241    }
01242 
01243    if (q->sound_periodicannounce[0]) {
01244       ast_str_set(&q->sound_periodicannounce[0], 0, "queue-periodic-announce");
01245    }
01246 
01247    for (i = 1; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
01248       if (q->sound_periodicannounce[i])
01249          ast_str_set(&q->sound_periodicannounce[i], 0, "%s", "");
01250    }
01251 
01252    while ((pr_iter = AST_LIST_REMOVE_HEAD(&q->rules,list)))
01253       ast_free(pr_iter);
01254 }
01255 
01256 static void clear_queue(struct call_queue *q)
01257 {
01258    q->holdtime = 0;
01259    q->callscompleted = 0;
01260    q->callsabandoned = 0;
01261    q->callscompletedinsl = 0;
01262    q->talktime = 0;
01263 
01264    if (q->members) {
01265       struct member *mem;
01266       struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
01267       while ((mem = ao2_iterator_next(&mem_iter))) {
01268          mem->calls = 0;
01269          mem->lastcall = 0;
01270          ao2_ref(mem, -1);
01271       }
01272       ao2_iterator_destroy(&mem_iter);
01273    }
01274 }
01275 
01276 /*! 
01277  * \brief Change queue penalty by adding rule.
01278  *
01279  * Check rule for errors with time or fomatting, see if rule is relative to rest 
01280  * of queue, iterate list of rules to find correct insertion point, insert and return.
01281  * \retval -1 on failure
01282  * \retval 0 on success 
01283  * \note Call this with the rule_lists locked 
01284 */
01285 static int insert_penaltychange (const char *list_name, const char *content, const int linenum)
01286 {
01287    char *timestr, *maxstr, *minstr, *contentdup;
01288    struct penalty_rule *rule = NULL, *rule_iter;
01289    struct rule_list *rl_iter;
01290    int penaltychangetime, inserted = 0;
01291 
01292    if (!(rule = ast_calloc(1, sizeof(*rule)))) {
01293       return -1;
01294    }
01295 
01296    contentdup = ast_strdupa(content);
01297    
01298    if (!(maxstr = strchr(contentdup, ','))) {
01299       ast_log(LOG_WARNING, "Improperly formatted penaltychange rule at line %d. Ignoring.\n", linenum);
01300       ast_free(rule);
01301       return -1;
01302    }
01303 
01304    *maxstr++ = '\0';
01305    timestr = contentdup;
01306 
01307    if ((penaltychangetime = atoi(timestr)) < 0) {
01308       ast_log(LOG_WARNING, "Improper time parameter specified for penaltychange rule at line %d. Ignoring.\n", linenum);
01309       ast_free(rule);
01310       return -1;
01311    }
01312 
01313    rule->time = penaltychangetime;
01314 
01315    if ((minstr = strchr(maxstr,',')))
01316       *minstr++ = '\0';
01317    
01318    /* The last check will evaluate true if either no penalty change is indicated for a given rule
01319     * OR if a min penalty change is indicated but no max penalty change is */
01320    if (*maxstr == '+' || *maxstr == '-' || *maxstr == '\0') {
01321       rule->max_relative = 1;
01322    }
01323 
01324    rule->max_value = atoi(maxstr);
01325 
01326    if (!ast_strlen_zero(minstr)) {
01327       if (*minstr == '+' || *minstr == '-')
01328          rule->min_relative = 1;
01329       rule->min_value = atoi(minstr);
01330    } else /*there was no minimum specified, so assume this means no change*/
01331       rule->min_relative = 1;
01332 
01333    /*We have the rule made, now we need to insert it where it belongs*/
01334    AST_LIST_TRAVERSE(&rule_lists, rl_iter, list){
01335       if (strcasecmp(rl_iter->name, list_name))
01336          continue;
01337 
01338       AST_LIST_TRAVERSE_SAFE_BEGIN(&rl_iter->rules, rule_iter, list) {
01339          if (rule->time < rule_iter->time) {
01340             AST_LIST_INSERT_BEFORE_CURRENT(rule, list);
01341             inserted = 1;
01342             break;
01343          }
01344       }
01345       AST_LIST_TRAVERSE_SAFE_END;
01346    
01347       if (!inserted) {
01348          AST_LIST_INSERT_TAIL(&rl_iter->rules, rule, list);
01349       }
01350    }
01351 
01352    return 0;
01353 }
01354 
01355 static void parse_empty_options(const char *value, enum empty_conditions *empty, int joinempty)
01356 {
01357    char *value_copy = ast_strdupa(value);
01358    char *option = NULL;
01359    while ((option = strsep(&value_copy, ","))) {
01360       if (!strcasecmp(option, "paused")) {
01361          *empty |= QUEUE_EMPTY_PAUSED;
01362       } else if (!strcasecmp(option, "penalty")) {
01363          *empty |= QUEUE_EMPTY_PENALTY;
01364       } else if (!strcasecmp(option, "inuse")) {
01365          *empty |= QUEUE_EMPTY_INUSE;
01366       } else if (!strcasecmp(option, "ringing")) {
01367          *empty |= QUEUE_EMPTY_RINGING;
01368       } else if (!strcasecmp(option, "invalid")) {
01369          *empty |= QUEUE_EMPTY_INVALID;
01370       } else if (!strcasecmp(option, "wrapup")) {
01371          *empty |= QUEUE_EMPTY_WRAPUP;
01372       } else if (!strcasecmp(option, "unavailable")) {
01373          *empty |= QUEUE_EMPTY_UNAVAILABLE;
01374       } else if (!strcasecmp(option, "unknown")) {
01375          *empty |= QUEUE_EMPTY_UNKNOWN;
01376       } else if (!strcasecmp(option, "loose")) {
01377          *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID);
01378       } else if (!strcasecmp(option, "strict")) {
01379          *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED | QUEUE_EMPTY_UNAVAILABLE);
01380       } else if ((ast_false(option) && joinempty) || (ast_true(option) && !joinempty)) {
01381          *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED);
01382       } else if ((ast_false(option) && !joinempty) || (ast_true(option) && joinempty)) {
01383          *empty = 0;
01384       } else {
01385          ast_log(LOG_WARNING, "Unknown option %s for '%s'\n", option, joinempty ? "joinempty" : "leavewhenempty");
01386       }
01387    }
01388 }
01389 
01390 /*! \brief Configure a queue parameter.
01391  * 
01392  * The failunknown flag is set for config files (and static realtime) to show
01393  * errors for unknown parameters. It is cleared for dynamic realtime to allow
01394  *  extra fields in the tables.
01395  * \note For error reporting, line number is passed for .conf static configuration,
01396  * for Realtime queues, linenum is -1.
01397 */
01398 static void queue_set_param(struct call_queue *q, const char *param, const char *val, int linenum, int failunknown)
01399 {
01400    if (!strcasecmp(param, "musicclass") || 
01401       !strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
01402       ast_copy_string(q->moh, val, sizeof(q->moh));
01403    } else if (!strcasecmp(param, "announce")) {
01404       ast_copy_string(q->announce, val, sizeof(q->announce));
01405    } else if (!strcasecmp(param, "context")) {
01406       ast_copy_string(q->context, val, sizeof(q->context));
01407    } else if (!strcasecmp(param, "timeout")) {
01408       q->timeout = atoi(val);
01409       if (q->timeout < 0)
01410          q->timeout = DEFAULT_TIMEOUT;
01411    } else if (!strcasecmp(param, "ringinuse")) {
01412       q->ringinuse = ast_true(val);
01413    } else if (!strcasecmp(param, "setinterfacevar")) {
01414       q->setinterfacevar = ast_true(val);
01415    } else if (!strcasecmp(param, "setqueuevar")) {
01416       q->setqueuevar = ast_true(val);
01417    } else if (!strcasecmp(param, "setqueueentryvar")) {
01418       q->setqueueentryvar = ast_true(val);
01419    } else if (!strcasecmp(param, "monitor-format")) {
01420       ast_copy_string(q->monfmt, val, sizeof(q->monfmt));
01421    } else if (!strcasecmp(param, "membermacro")) {
01422       ast_copy_string(q->membermacro, val, sizeof(q->membermacro));
01423    } else if (!strcasecmp(param, "membergosub")) {
01424       ast_copy_string(q->membergosub, val, sizeof(q->membergosub));
01425    } else if (!strcasecmp(param, "queue-youarenext")) {
01426       ast_copy_string(q->sound_next, val, sizeof(q->sound_next));
01427    } else if (!strcasecmp(param, "queue-thereare")) {
01428       ast_copy_string(q->sound_thereare, val, sizeof(q->sound_thereare));
01429    } else if (!strcasecmp(param, "queue-callswaiting")) {
01430       ast_copy_string(q->sound_calls, val, sizeof(q->sound_calls));
01431    } else if (!strcasecmp(param, "queue-quantity1")) {
01432       ast_copy_string(q->queue_quantity1, val, sizeof(q->queue_quantity1));
01433    } else if (!strcasecmp(param, "queue-quantity2")) {
01434       ast_copy_string(q->queue_quantity2, val, sizeof(q->queue_quantity2));
01435    } else if (!strcasecmp(param, "queue-holdtime")) {
01436       ast_copy_string(q->sound_holdtime, val, sizeof(q->sound_holdtime));
01437    } else if (!strcasecmp(param, "queue-minutes")) {
01438       ast_copy_string(q->sound_minutes, val, sizeof(q->sound_minutes));
01439    } else if (!strcasecmp(param, "queue-minute")) {
01440       ast_copy_string(q->sound_minute, val, sizeof(q->sound_minute));
01441    } else if (!strcasecmp(param, "queue-seconds")) {
01442       ast_copy_string(q->sound_seconds, val, sizeof(q->sound_seconds));
01443    } else if (!strcasecmp(param, "queue-thankyou")) {
01444       ast_copy_string(q->sound_thanks, val, sizeof(q->sound_thanks));
01445    } else if (!strcasecmp(param, "queue-callerannounce")) {
01446       ast_copy_string(q->sound_callerannounce, val, sizeof(q->sound_callerannounce));
01447    } else if (!strcasecmp(param, "queue-reporthold")) {
01448       ast_copy_string(q->sound_reporthold, val, sizeof(q->sound_reporthold));
01449    } else if (!strcasecmp(param, "announce-frequency")) {
01450       q->announcefrequency = atoi(val);
01451    } else if (!strcasecmp(param, "min-announce-frequency")) {
01452       q->minannouncefrequency = atoi(val);
01453       ast_debug(1, "%s=%s for queue '%s'\n", param, val, q->name);
01454    } else if (!strcasecmp(param, "announce-round-seconds")) {
01455       q->roundingseconds = atoi(val);
01456       /* Rounding to any other values just doesn't make sense... */
01457       if (!(q->roundingseconds == 0 || q->roundingseconds == 5 || q->roundingseconds == 10
01458          || q->roundingseconds == 15 || q->roundingseconds == 20 || q->roundingseconds == 30)) {
01459          if (linenum >= 0) {
01460             ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
01461                "using 0 instead for queue '%s' at line %d of queues.conf\n",
01462                val, param, q->name, linenum);
01463          } else {
01464             ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
01465                "using 0 instead for queue '%s'\n", val, param, q->name);
01466          }
01467          q->roundingseconds=0;
01468       }
01469    } else if (!strcasecmp(param, "announce-holdtime")) {
01470       if (!strcasecmp(val, "once"))
01471          q->announceholdtime = ANNOUNCEHOLDTIME_ONCE;
01472       else if (ast_true(val))
01473          q->announceholdtime = ANNOUNCEHOLDTIME_ALWAYS;
01474       else
01475          q->announceholdtime = 0;
01476    } else if (!strcasecmp(param, "announce-position")) {
01477       if (!strcasecmp(val, "limit"))
01478          q->announceposition = ANNOUNCEPOSITION_LIMIT;
01479       else if (!strcasecmp(val, "more"))
01480          q->announceposition = ANNOUNCEPOSITION_MORE_THAN;
01481       else if (ast_true(val))
01482          q->announceposition = ANNOUNCEPOSITION_YES;
01483       else
01484          q->announceposition = ANNOUNCEPOSITION_NO;
01485    } else if (!strcasecmp(param, "announce-position-limit")) {
01486       q->announcepositionlimit = atoi(val);
01487    } else if (!strcasecmp(param, "periodic-announce")) {
01488       if (strchr(val, ',')) {
01489          char *s, *buf = ast_strdupa(val);
01490          unsigned int i = 0;
01491 
01492          while ((s = strsep(&buf, ",|"))) {
01493             if (!q->sound_periodicannounce[i])
01494                q->sound_periodicannounce[i] = ast_str_create(16);
01495             ast_str_set(&q->sound_periodicannounce[i], 0, "%s", s);
01496             i++;
01497             if (i == MAX_PERIODIC_ANNOUNCEMENTS)
01498                break;
01499          }
01500          q->numperiodicannounce = i;
01501       } else {
01502          ast_str_set(&q->sound_periodicannounce[0], 0, "%s", val);
01503          q->numperiodicannounce = 1;
01504       }
01505    } else if (!strcasecmp(param, "periodic-announce-frequency")) {
01506       q->periodicannouncefrequency = atoi(val);
01507    } else if (!strcasecmp(param, "random-periodic-announce")) {
01508       q->randomperiodicannounce = ast_true(val);
01509    } else if (!strcasecmp(param, "retry")) {
01510       q->retry = atoi(val);
01511       if (q->retry <= 0)
01512          q->retry = DEFAULT_RETRY;
01513    } else if (!strcasecmp(param, "wrapuptime")) {
01514       q->wrapuptime = atoi(val);
01515    } else if (!strcasecmp(param, "autofill")) {
01516       q->autofill = ast_true(val);
01517    } else if (!strcasecmp(param, "monitor-type")) {
01518       if (!strcasecmp(val, "mixmonitor"))
01519          q->montype = 1;
01520    } else if (!strcasecmp(param, "autopause")) {
01521       q->autopause = ast_true(val);
01522    } else if (!strcasecmp(param, "maxlen")) {
01523       q->maxlen = atoi(val);
01524       if (q->maxlen < 0)
01525          q->maxlen = 0;
01526    } else if (!strcasecmp(param, "servicelevel")) {
01527       q->servicelevel= atoi(val);
01528    } else if (!strcasecmp(param, "strategy")) {
01529       int strategy;
01530 
01531       /* We are a static queue and already have set this, no need to do it again */
01532       if (failunknown) {
01533          return;
01534       }
01535       strategy = strat2int(val);
01536       if (strategy < 0) {
01537          ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
01538             val, q->name);
01539          q->strategy = QUEUE_STRATEGY_RINGALL;
01540       }
01541       if (strategy == q->strategy) {
01542          return;
01543       }
01544       if (strategy == QUEUE_STRATEGY_LINEAR) {
01545          ast_log(LOG_WARNING, "Changing to the linear strategy currently requires asterisk to be restarted.\n");
01546          return;
01547       }
01548       q->strategy = strategy;
01549    } else if (!strcasecmp(param, "joinempty")) {
01550       parse_empty_options(val, &q->joinempty, 1);
01551    } else if (!strcasecmp(param, "leavewhenempty")) {
01552       parse_empty_options(val, &q->leavewhenempty, 0);
01553    } else if (!strcasecmp(param, "eventmemberstatus")) {
01554       q->maskmemberstatus = !ast_true(val);
01555    } else if (!strcasecmp(param, "eventwhencalled")) {
01556       if (!strcasecmp(val, "vars")) {
01557          q->eventwhencalled = QUEUE_EVENT_VARIABLES;
01558       } else {
01559          q->eventwhencalled = ast_true(val) ? 1 : 0;
01560       }
01561    } else if (!strcasecmp(param, "reportholdtime")) {
01562       q->reportholdtime = ast_true(val);
01563    } else if (!strcasecmp(param, "memberdelay")) {
01564       q->memberdelay = atoi(val);
01565    } else if (!strcasecmp(param, "weight")) {
01566       q->weight = atoi(val);
01567    } else if (!strcasecmp(param, "timeoutrestart")) {
01568       q->timeoutrestart = ast_true(val);
01569    } else if (!strcasecmp(param, "defaultrule")) {
01570       ast_copy_string(q->defaultrule, val, sizeof(q->defaultrule));
01571    } else if (!strcasecmp(param, "timeoutpriority")) {
01572       if (!strcasecmp(val, "conf")) {
01573          q->timeoutpriority = TIMEOUT_PRIORITY_CONF;
01574       } else {
01575          q->timeoutpriority = TIMEOUT_PRIORITY_APP;
01576       }
01577    } else if (failunknown) {
01578       if (linenum >= 0) {
01579          ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queues.conf\n",
01580             q->name, param, linenum);
01581       } else {
01582          ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s\n", q->name, param);
01583       }
01584    }
01585 }
01586 
01587 /*!
01588  * \brief Find rt member record to update otherwise create one.
01589  *
01590  * Search for member in queue, if found update penalty/paused state,
01591  * if no member exists create one flag it as a RT member and add to queue member list. 
01592 */
01593 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)
01594 {
01595    struct member *m;
01596    struct ao2_iterator mem_iter;
01597    int penalty = 0;
01598    int paused  = 0;
01599    int found = 0;
01600 
01601    if (ast_strlen_zero(rt_uniqueid)) {
01602       ast_log(LOG_WARNING, "Realtime field uniqueid is empty for member %s\n", S_OR(membername, "NULL"));
01603       return;
01604    }
01605 
01606    if (penalty_str) {
01607       penalty = atoi(penalty_str);
01608       if (penalty < 0)
01609          penalty = 0;
01610    }
01611 
01612    if (paused_str) {
01613       paused = atoi(paused_str);
01614       if (paused < 0)
01615          paused = 0;
01616    }
01617 
01618    /* Find member by realtime uniqueid and update */
01619    mem_iter = ao2_iterator_init(q->members, 0);
01620    while ((m = ao2_iterator_next(&mem_iter))) {
01621       if (!strcasecmp(m->rt_uniqueid, rt_uniqueid)) {
01622          m->dead = 0;   /* Do not delete this one. */
01623          ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
01624          if (paused_str)
01625             m->paused = paused;
01626          if (strcasecmp(state_interface, m->state_interface)) {
01627             ast_copy_string(m->state_interface, state_interface, sizeof(m->state_interface));
01628          }     
01629          m->penalty = penalty;
01630          found = 1;
01631          ao2_ref(m, -1);
01632          break;
01633       }
01634       ao2_ref(m, -1);
01635    }
01636    ao2_iterator_destroy(&mem_iter);
01637 
01638    /* Create a new member */
01639    if (!found) {
01640       if ((m = create_queue_member(interface, membername, penalty, paused, state_interface))) {
01641          m->dead = 0;
01642          m->realtime = 1;
01643          ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
01644          ast_queue_log(q->name, "REALTIME", m->interface, "ADDMEMBER", "%s", "");
01645          ao2_link(q->members, m);
01646          ao2_ref(m, -1);
01647          m = NULL;
01648          q->membercount++;
01649       }
01650    }
01651 }
01652 
01653 /*! \brief Iterate through queue's member list and delete them */
01654 static void free_members(struct call_queue *q, int all)
01655 {
01656    /* Free non-dynamic members */
01657    struct member *cur;
01658    struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
01659 
01660    while ((cur = ao2_iterator_next(&mem_iter))) {
01661       if (all || !cur->dynamic) {
01662          ao2_unlink(q->members, cur);
01663          q->membercount--;
01664       }
01665       ao2_ref(cur, -1);
01666    }
01667    ao2_iterator_destroy(&mem_iter);
01668 }
01669 
01670 /*! \brief Free queue's member list then its string fields */
01671 static void destroy_queue(void *obj)
01672 {
01673    struct call_queue *q = obj;
01674    int i;
01675 
01676    free_members(q, 1);
01677    for (i = 0; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
01678       if (q->sound_periodicannounce[i])
01679          free(q->sound_periodicannounce[i]);
01680    }
01681    ao2_ref(q->members, -1);
01682 }
01683 
01684 static struct call_queue *alloc_queue(const char *queuename)
01685 {
01686    struct call_queue *q;
01687 
01688    if ((q = ao2_t_alloc(sizeof(*q), destroy_queue, "Allocate queue"))) {
01689       ast_copy_string(q->name, queuename, sizeof(q->name));
01690    }
01691    return q;
01692 }
01693 
01694 /*!
01695  * \brief Reload a single queue via realtime.
01696  *
01697  * Check for statically defined queue first, check if deleted RT queue,
01698  * check for new RT queue, if queue vars are not defined init them with defaults.
01699  * reload RT queue vars, set RT queue members dead and reload them, return finished queue.
01700  * \retval the queue, 
01701  * \retval NULL if it doesn't exist.
01702  * \note Should be called with the "queues" container locked. 
01703 */
01704 static struct call_queue *find_queue_by_name_rt(const char *queuename, struct ast_variable *queue_vars, struct ast_config *member_config)
01705 {
01706    struct ast_variable *v;
01707    struct call_queue *q, tmpq;
01708    struct member *m;
01709    struct ao2_iterator mem_iter;
01710    char *interface = NULL;
01711    const char *tmp_name;
01712    char *tmp;
01713    char tmpbuf[64];  /* Must be longer than the longest queue param name. */
01714 
01715    ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
01716 
01717    /* Static queues override realtime. */
01718    if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Check if static queue exists"))) {
01719       ao2_lock(q);
01720       if (!q->realtime) {
01721          if (q->dead) {
01722             ao2_unlock(q);
01723             queue_t_unref(q, "Queue is dead; can't return it");
01724             return NULL;
01725          } else {
01726             ast_log(LOG_WARNING, "Static queue '%s' already exists. Not loading from realtime\n", q->name);
01727             ao2_unlock(q);
01728             return q;
01729          }
01730       }
01731    } else if (!member_config)
01732       /* Not found in the list, and it's not realtime ... */
01733       return NULL;
01734 
01735    /* Check if queue is defined in realtime. */
01736    if (!queue_vars) {
01737       /* Delete queue from in-core list if it has been deleted in realtime. */
01738       if (q) {
01739          /*! \note Hmm, can't seem to distinguish a DB failure from a not
01740             found condition... So we might delete an in-core queue
01741             in case of DB failure. */
01742          ast_debug(1, "Queue %s not found in realtime.\n", queuename);
01743 
01744          q->dead = 1;
01745          /* Delete if unused (else will be deleted when last caller leaves). */
01746          queues_t_unlink(queues, q, "Unused; removing from container");
01747          ao2_unlock(q);
01748          queue_t_unref(q, "Queue is dead; can't return it");
01749       }
01750       return NULL;
01751    }
01752 
01753    /* Create a new queue if an in-core entry does not exist yet. */
01754    if (!q) {
01755       struct ast_variable *tmpvar = NULL;
01756       if (!(q = alloc_queue(queuename)))
01757          return NULL;
01758       ao2_lock(q);
01759       clear_queue(q);
01760       q->realtime = 1;
01761       q->membercount = 0;
01762       /*Before we initialize the queue, we need to set the strategy, so that linear strategy
01763        * will allocate the members properly
01764        */
01765       for (tmpvar = queue_vars; tmpvar; tmpvar = tmpvar->next) {
01766          if (!strcasecmp(tmpvar->name, "strategy")) {
01767             q->strategy = strat2int(tmpvar->value);
01768             if (q->strategy < 0) {
01769                ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
01770                tmpvar->value, q->name);
01771                q->strategy = QUEUE_STRATEGY_RINGALL;
01772             }
01773             break;
01774          }
01775       }
01776       /* We traversed all variables and didn't find a strategy */
01777       if (!tmpvar)
01778          q->strategy = QUEUE_STRATEGY_RINGALL;
01779       queues_t_link(queues, q, "Add queue to container");
01780    }
01781    init_queue(q);    /* Ensure defaults for all parameters not set explicitly. */
01782 
01783    memset(tmpbuf, 0, sizeof(tmpbuf));
01784    for (v = queue_vars; v; v = v->next) {
01785       /* Convert to dashes `-' from underscores `_' as the latter are more SQL friendly. */
01786       if ((tmp = strchr(v->name, '_'))) {
01787          ast_copy_string(tmpbuf, v->name, sizeof(tmpbuf));
01788          tmp_name = tmpbuf;
01789          tmp = tmpbuf;
01790          while ((tmp = strchr(tmp, '_')))
01791             *tmp++ = '-';
01792       } else
01793          tmp_name = v->name;
01794 
01795       if (!ast_strlen_zero(v->value)) {
01796          /* Don't want to try to set the option if the value is empty */
01797          queue_set_param(q, tmp_name, v->value, -1, 0);
01798       }
01799    }
01800 
01801    /* Temporarily set realtime members dead so we can detect deleted ones. 
01802     * Also set the membercount correctly for realtime*/
01803    mem_iter = ao2_iterator_init(q->members, 0);
01804    while ((m = ao2_iterator_next(&mem_iter))) {
01805       q->membercount++;
01806       if (m->realtime)
01807          m->dead = 1;
01808       ao2_ref(m, -1);
01809    }
01810    ao2_iterator_destroy(&mem_iter);
01811 
01812    while ((interface = ast_category_browse(member_config, interface))) {
01813       rt_handle_member_record(q, interface,
01814          ast_variable_retrieve(member_config, interface, "uniqueid"),
01815          S_OR(ast_variable_retrieve(member_config, interface, "membername"),interface),
01816          ast_variable_retrieve(member_config, interface, "penalty"),
01817          ast_variable_retrieve(member_config, interface, "paused"),
01818          S_OR(ast_variable_retrieve(member_config, interface, "state_interface"),interface));
01819    }
01820 
01821    /* Delete all realtime members that have been deleted in DB. */
01822    mem_iter = ao2_iterator_init(q->members, 0);
01823    while ((m = ao2_iterator_next(&mem_iter))) {
01824       if (m->dead) {
01825          ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
01826          ao2_unlink(q->members, m);
01827          q->membercount--;
01828       }
01829       ao2_ref(m, -1);
01830    }
01831    ao2_iterator_destroy(&mem_iter);
01832 
01833    ao2_unlock(q);
01834 
01835    return q;
01836 }
01837 
01838 static struct call_queue *load_realtime_queue(const char *queuename)
01839 {
01840    struct ast_variable *queue_vars;
01841    struct ast_config *member_config = NULL;
01842    struct call_queue *q = NULL, tmpq;
01843    int prev_weight = 0;
01844 
01845    /* Find the queue in the in-core list first. */
01846    ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
01847    q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Look for queue in memory first");
01848 
01849    if (!q || q->realtime) {
01850       /*! \note Load from realtime before taking the "queues" container lock, to avoid blocking all
01851          queue operations while waiting for the DB.
01852 
01853          This will be two separate database transactions, so we might
01854          see queue parameters as they were before another process
01855          changed the queue and member list as it was after the change.
01856          Thus we might see an empty member list when a queue is
01857          deleted. In practise, this is unlikely to cause a problem. */
01858 
01859       queue_vars = ast_load_realtime("queues", "name", queuename, SENTINEL);
01860       if (queue_vars) {
01861          member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", queuename, SENTINEL);
01862          if (!member_config) {
01863             ast_log(LOG_ERROR, "no queue_members defined in your config (extconfig.conf).\n");
01864             ast_variables_destroy(queue_vars);
01865             return NULL;
01866          }
01867       }
01868       if (q) {
01869          prev_weight = q->weight ? 1 : 0;
01870       }
01871 
01872       ao2_lock(queues);
01873 
01874       q = find_queue_by_name_rt(queuename, queue_vars, member_config);
01875       if (member_config) {
01876          ast_config_destroy(member_config);
01877       }
01878       if (queue_vars) {
01879          ast_variables_destroy(queue_vars);
01880       }
01881       /* update the use_weight value if the queue's has gained or lost a weight */ 
01882       if (q) {
01883          if (!q->weight && prev_weight) {
01884             ast_atomic_fetchadd_int(&use_weight, -1);
01885          }
01886          if (q->weight && !prev_weight) {
01887             ast_atomic_fetchadd_int(&use_weight, +1);
01888          }
01889       }
01890       /* Other cases will end up with the proper value for use_weight */
01891       ao2_unlock(queues);
01892 
01893    } else {
01894       update_realtime_members(q);
01895    }
01896    return q;
01897 }
01898 
01899 static int update_realtime_member_field(struct member *mem, const char *queue_name, const char *field, const char *value)
01900 {
01901    int ret = -1;
01902 
01903    if (ast_strlen_zero(mem->rt_uniqueid))
01904       return ret;
01905 
01906    if ((ast_update_realtime("queue_members", "uniqueid", mem->rt_uniqueid, field, value, SENTINEL)) > 0)
01907       ret = 0;
01908 
01909    return ret;
01910 }
01911 
01912 
01913 static void update_realtime_members(struct call_queue *q)
01914 {
01915    struct ast_config *member_config = NULL;
01916    struct member *m;
01917    char *interface = NULL;
01918    struct ao2_iterator mem_iter;
01919 
01920    if (!(member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", q->name , SENTINEL))) {
01921       /*This queue doesn't have realtime members*/
01922       ast_debug(3, "Queue %s has no realtime members defined. No need for update\n", q->name);
01923       return;
01924    }
01925 
01926    ao2_lock(queues);
01927    ao2_lock(q);
01928    
01929    /* Temporarily set realtime  members dead so we can detect deleted ones.*/ 
01930    mem_iter = ao2_iterator_init(q->members, 0);
01931    while ((m = ao2_iterator_next(&mem_iter))) {
01932       if (m->realtime)
01933          m->dead = 1;
01934       ao2_ref(m, -1);
01935    }
01936    ao2_iterator_destroy(&mem_iter);
01937 
01938    while ((interface = ast_category_browse(member_config, interface))) {
01939       rt_handle_member_record(q, interface,
01940          ast_variable_retrieve(member_config, interface, "uniqueid"),
01941          S_OR(ast_variable_retrieve(member_config, interface, "membername"), interface),
01942          ast_variable_retrieve(member_config, interface, "penalty"),
01943          ast_variable_retrieve(member_config, interface, "paused"),
01944          S_OR(ast_variable_retrieve(member_config, interface, "state_interface"), interface));
01945    }
01946 
01947    /* Delete all realtime members that have been deleted in DB. */
01948    mem_iter = ao2_iterator_init(q->members, 0);
01949    while ((m = ao2_iterator_next(&mem_iter))) {
01950       if (m->dead) {
01951          ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
01952          ao2_unlink(q->members, m);
01953          q->membercount--;
01954       }
01955       ao2_ref(m, -1);
01956    }
01957    ao2_iterator_destroy(&mem_iter);
01958    ao2_unlock(q);
01959    ao2_unlock(queues);
01960    ast_config_destroy(member_config);
01961 }
01962 
01963 static int join_queue(char *queuename, struct queue_ent *qe, enum queue_result *reason)
01964 {
01965    struct call_queue *q;
01966    struct queue_ent *cur, *prev = NULL;
01967    int res = -1;
01968    int pos = 0;
01969    int inserted = 0;
01970 
01971    if (!(q = load_realtime_queue(queuename)))
01972       return res;
01973 
01974    ao2_lock(queues);
01975    ao2_lock(q);
01976 
01977    /* This is our one */
01978    if (q->joinempty) {
01979       int status = 0;
01980       if ((status = get_member_status(q, qe->max_penalty, qe->min_penalty, q->joinempty))) {
01981          *reason = QUEUE_JOINEMPTY;
01982          ao2_unlock(q);
01983          ao2_unlock(queues);
01984          return res;
01985       }
01986    }
01987    if (*reason == QUEUE_UNKNOWN && q->maxlen && (q->count >= q->maxlen))
01988       *reason = QUEUE_FULL;
01989    else if (*reason == QUEUE_UNKNOWN) {
01990       /* There's space for us, put us at the right position inside
01991        * the queue.
01992        * Take into account the priority of the calling user */
01993       inserted = 0;
01994       prev = NULL;
01995       cur = q->head;
01996       while (cur) {
01997          /* We have higher priority than the current user, enter
01998           * before him, after all the other users with priority
01999           * higher or equal to our priority. */
02000          if ((!inserted) && (qe->prio > cur->prio)) {
02001             insert_entry(q, prev, qe, &pos);
02002             inserted = 1;
02003          }
02004          cur->pos = ++pos;
02005          prev = cur;
02006          cur = cur->next;
02007       }
02008       /* No luck, join at the end of the queue */
02009       if (!inserted)
02010          insert_entry(q, prev, qe, &pos);
02011       ast_copy_string(qe->moh, q->moh, sizeof(qe->moh));
02012       ast_copy_string(qe->announce, q->announce, sizeof(qe->announce));
02013       ast_copy_string(qe->context, q->context, sizeof(qe->context));
02014       q->count++;
02015       res = 0;
02016       manager_event(EVENT_FLAG_CALL, "Join",
02017          "Channel: %s\r\nCallerIDNum: %s\r\nCallerIDName: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\nUniqueid: %s\r\n",
02018          qe->chan->name,
02019          S_OR(qe->chan->cid.cid_num, "unknown"), /* XXX somewhere else it is <unknown> */
02020          S_OR(qe->chan->cid.cid_name, "unknown"),
02021          q->name, qe->pos, q->count, qe->chan->uniqueid );
02022       ast_debug(1, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
02023    }
02024    ao2_unlock(q);
02025    ao2_unlock(queues);
02026 
02027    return res;
02028 }
02029 
02030 static int play_file(struct ast_channel *chan, const char *filename)
02031 {
02032    int res;
02033 
02034    if (ast_strlen_zero(filename)) {
02035       return 0;
02036    }
02037 
02038    if (!ast_fileexists(filename, NULL, chan->language)) {
02039       return 0;
02040    }
02041 
02042    ast_stopstream(chan);
02043 
02044    res = ast_streamfile(chan, filename, chan->language);
02045    if (!res)
02046       res = ast_waitstream(chan, AST_DIGIT_ANY);
02047 
02048    ast_stopstream(chan);
02049 
02050    return res;
02051 }
02052 
02053 /*!
02054  * \brief Check for valid exit from queue via goto
02055  * \retval 0 if failure
02056  * \retval 1 if successful
02057 */
02058 static int valid_exit(struct queue_ent *qe, char digit)
02059 {
02060    int digitlen = strlen(qe->digits);
02061 
02062    /* Prevent possible buffer overflow */
02063    if (digitlen < sizeof(qe->digits) - 2) {
02064       qe->digits[digitlen] = digit;
02065       qe->digits[digitlen + 1] = '\0';
02066    } else {
02067       qe->digits[0] = '\0';
02068       return 0;
02069    }
02070 
02071    /* If there's no context to goto, short-circuit */
02072    if (ast_strlen_zero(qe->context))
02073       return 0;
02074 
02075    /* If the extension is bad, then reset the digits to blank */
02076    if (!ast_canmatch_extension(qe->chan, qe->context, qe->digits, 1, qe->chan->cid.cid_num)) {
02077       qe->digits[0] = '\0';
02078       return 0;
02079    }
02080 
02081    /* We have an exact match */
02082    if (!ast_goto_if_exists(qe->chan, qe->context, qe->digits, 1)) {
02083       qe->valid_digits = 1;
02084       /* Return 1 on a successful goto */
02085       return 1;
02086    }
02087 
02088    return 0;
02089 }
02090 
02091 static int say_position(struct queue_ent *qe, int ringing)
02092 {
02093    int res = 0, avgholdmins, avgholdsecs, announceposition = 0;
02094    int say_thanks = 1;
02095    time_t now;
02096 
02097    /* Let minannouncefrequency seconds pass between the start of each position announcement */
02098    time(&now);
02099    if ((now - qe->last_pos) < qe->parent->minannouncefrequency)
02100       return 0;
02101 
02102    /* If either our position has changed, or we are over the freq timer, say position */
02103    if ((qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency))
02104       return 0;
02105 
02106    if (ringing) {
02107       ast_indicate(qe->chan,-1);
02108    } else {
02109       ast_moh_stop(qe->chan);
02110    }
02111 
02112    if (qe->parent->announceposition == ANNOUNCEPOSITION_YES ||
02113       qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN ||
02114       (qe->parent->announceposition == ANNOUNCEPOSITION_LIMIT &&
02115       qe->pos <= qe->parent->announcepositionlimit))
02116          announceposition = 1;
02117 
02118 
02119    if (announceposition == 1) {
02120       /* Say we're next, if we are */
02121       if (qe->pos == 1) {
02122          res = play_file(qe->chan, qe->parent->sound_next);
02123          if (res)
02124             goto playout;
02125          else
02126             goto posout;
02127       } else {
02128          if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
02129             /* More than Case*/
02130             res = play_file(qe->chan, qe->parent->queue_quantity1);
02131             if (res)
02132                goto playout;
02133             res = ast_say_number(qe->chan, qe->parent->announcepositionlimit, AST_DIGIT_ANY, qe->chan->language, NULL); /* Needs gender */
02134             if (res)
02135                goto playout;
02136          } else {
02137             /* Normal Case */
02138             res = play_file(qe->chan, qe->parent->sound_thereare);
02139             if (res)
02140                goto playout;
02141             res = ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language, NULL); /* Needs gender */
02142             if (res)
02143                goto playout;
02144          }
02145          if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
02146             /* More than Case*/
02147             res = play_file(qe->chan, qe->parent->queue_quantity2);
02148             if (res)
02149                goto playout;
02150          } else {
02151             res = play_file(qe->chan, qe->parent->sound_calls);
02152             if (res)
02153                goto playout;
02154          }
02155       }
02156    }
02157    /* Round hold time to nearest minute */
02158    avgholdmins = abs(((qe->parent->holdtime + 30) - (now - qe->start)) / 60);
02159 
02160    /* If they have specified a rounding then round the seconds as well */
02161    if (qe->parent->roundingseconds) {
02162       avgholdsecs = (abs(((qe->parent->holdtime + 30) - (now - qe->start))) - 60 * avgholdmins) / qe->parent->roundingseconds;
02163       avgholdsecs *= qe->parent->roundingseconds;
02164    } else {
02165       avgholdsecs = 0;
02166    }
02167 
02168    ast_verb(3, "Hold time for %s is %d minute(s) %d seconds\n", qe->parent->name, avgholdmins, avgholdsecs);
02169 
02170    /* If the hold time is >1 min, if it's enabled, and if it's not
02171       supposed to be only once and we have already said it, say it */
02172     if ((avgholdmins+avgholdsecs) > 0 && qe->parent->announceholdtime &&
02173         ((qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE && !qe->last_pos) ||
02174         !(qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE))) {
02175       res = play_file(qe->chan, qe->parent->sound_holdtime);
02176       if (res)
02177          goto playout;
02178 
02179       if (avgholdmins >= 1) {
02180          res = ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, qe->chan->language, NULL);
02181          if (res)
02182             goto playout;
02183 
02184          if (avgholdmins == 1) {
02185             res = play_file(qe->chan, qe->parent->sound_minute);
02186             if (res)
02187                goto playout;
02188          } else {
02189             res = play_file(qe->chan, qe->parent->sound_minutes);
02190             if (res)
02191                goto playout;
02192          }
02193       }
02194       if (avgholdsecs >= 1) {
02195          res = ast_say_number(qe->chan, avgholdsecs, AST_DIGIT_ANY, qe->chan->language, NULL);
02196          if (res)
02197             goto playout;
02198 
02199          res = play_file(qe->chan, qe->parent->sound_seconds);
02200          if (res)
02201             goto playout;
02202       }
02203    } else if (qe->parent->announceholdtime && !qe->parent->announceposition) {
02204       say_thanks = 0;
02205    }
02206 
02207 posout:
02208    if (qe->parent->announceposition) {
02209       ast_verb(3, "Told %s in %s their queue position (which was %d)\n",
02210          qe->chan->name, qe->parent->name, qe->pos);
02211    }
02212    if (say_thanks) {
02213       res = play_file(qe->chan, qe->parent->sound_thanks);
02214    }
02215 playout:
02216 
02217    if ((res > 0 && !valid_exit(qe, res)))
02218       res = 0;
02219 
02220    /* Set our last_pos indicators */
02221    qe->last_pos = now;
02222    qe->last_pos_said = qe->pos;
02223 
02224    /* Don't restart music on hold if we're about to exit the caller from the queue */
02225    if (!res) {
02226       if (ringing) {
02227          ast_indicate(qe->chan, AST_CONTROL_RINGING);
02228       } else {
02229          ast_moh_start(qe->chan, qe->moh, NULL);
02230       }
02231    }
02232    return res;
02233 }
02234 
02235 static void recalc_holdtime(struct queue_ent *qe, int newholdtime)
02236 {
02237    int oldvalue;
02238 
02239    /* Calculate holdtime using an exponential average */
02240    /* Thanks to SRT for this contribution */
02241    /* 2^2 (4) is the filter coefficient; a higher exponent would give old entries more weight */
02242 
02243    ao2_lock(qe->parent);
02244    oldvalue = qe->parent->holdtime;
02245    qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newholdtime) >> 2;
02246    ao2_unlock(qe->parent);
02247 }
02248 
02249 /*! \brief Caller leaving queue.
02250  * 
02251  * Search the queue to find the leaving client, if found remove from queue
02252  * create manager event, move others up the queue.
02253 */
02254 static void leave_queue(struct queue_ent *qe)
02255 {
02256    struct call_queue *q;
02257    struct queue_ent *current, *prev = NULL;
02258    struct penalty_rule *pr_iter;
02259    int pos = 0;
02260 
02261    if (!(q = qe->parent))
02262       return;
02263    queue_t_ref(q, "Copy queue pointer from queue entry");
02264    ao2_lock(q);
02265 
02266    prev = NULL;
02267    for (current = q->head; current; current = current->next) {
02268       if (current == qe) {
02269          q->count--;
02270 
02271          /* Take us out of the queue */
02272          manager_event(EVENT_FLAG_CALL, "Leave",
02273             "Channel: %s\r\nQueue: %s\r\nCount: %d\r\nUniqueid: %s\r\n",
02274             qe->chan->name, q->name,  q->count, qe->chan->uniqueid);
02275          ast_debug(1, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
02276          /* Take us out of the queue */
02277          if (prev)
02278             prev->next = current->next;
02279          else
02280             q->head = current->next;
02281          /* Free penalty rules */
02282          while ((pr_iter = AST_LIST_REMOVE_HEAD(&qe->qe_rules, list)))
02283             ast_free(pr_iter);
02284       } else {
02285          /* Renumber the people after us in the queue based on a new count */
02286          current->pos = ++pos;
02287          prev = current;
02288       }
02289    }
02290    ao2_unlock(q);
02291 
02292    /*If the queue is a realtime queue, check to see if it's still defined in real time*/
02293    if (q->realtime) {
02294       struct ast_variable *var;
02295       if (!(var = ast_load_realtime("queues", "name", q->name, SENTINEL))) {
02296          q->dead = 1;
02297       } else {
02298          ast_variables_destroy(var);
02299       }
02300    }
02301 
02302    if (q->dead) { 
02303       /* It's dead and nobody is in it, so kill it */
02304       queues_t_unlink(queues, q, "Queue is now dead; remove it from the container");
02305    }
02306    /* unref the explicit ref earlier in the function */
02307    queue_t_unref(q, "Expire copied reference");
02308 }
02309 
02310 /*! \brief Hang up a list of outgoing calls */
02311 static void hangupcalls(struct callattempt *outgoing, struct ast_channel *exception, int cancel_answered_elsewhere)
02312 {
02313    struct callattempt *oo;
02314 
02315    while (outgoing) {
02316       /* If someone else answered the call we should indicate this in the CANCEL */
02317       /* Hangup any existing lines we have open */
02318       if (outgoing->chan && (outgoing->chan != exception)) {
02319          if (exception || cancel_answered_elsewhere)
02320             ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
02321          ast_hangup(outgoing->chan);
02322       }
02323       oo = outgoing;
02324       outgoing = outgoing->q_next;
02325       if (oo->member)
02326          ao2_ref(oo->member, -1);
02327       ast_free(oo);
02328    }
02329 }
02330 
02331 /*!
02332  * \brief Get the number of members available to accept a call.
02333  *
02334  * \note The queue passed in should be locked prior to this function call
02335  *
02336  * \param[in] q The queue for which we are couting the number of available members
02337  * \return Return the number of available members in queue q
02338  */
02339 static int num_available_members(struct call_queue *q)
02340 {
02341    struct member *mem;
02342    int avl = 0;
02343    struct ao2_iterator mem_iter;
02344 
02345    mem_iter = ao2_iterator_init(q->members, 0);
02346    while ((mem = ao2_iterator_next(&mem_iter))) {
02347       switch (mem->status) {
02348       case AST_DEVICE_INUSE:
02349          if (!q->ringinuse)
02350             break;
02351          /* else fall through */
02352       case AST_DEVICE_NOT_INUSE:
02353       case AST_DEVICE_UNKNOWN:
02354          if (!mem->paused) {
02355             avl++;
02356          }
02357          break;
02358       }
02359       ao2_ref(mem, -1);
02360 
02361       /* If autofill is not enabled or if the queue's strategy is ringall, then
02362        * we really don't care about the number of available members so much as we
02363        * do that there is at least one available.
02364        *
02365        * In fact, we purposely will return from this function stating that only
02366        * one member is available if either of those conditions hold. That way,
02367        * functions which determine what action to take based on the number of available
02368        * members will operate properly. The reasoning is that even if multiple
02369        * members are available, only the head caller can actually be serviced.
02370        */
02371       if ((!q->autofill || q->strategy == QUEUE_STRATEGY_RINGALL) && avl) {
02372          break;
02373       }
02374    }
02375    ao2_iterator_destroy(&mem_iter);
02376 
02377    return avl;
02378 }
02379 
02380 /* traverse all defined queues which have calls waiting and contain this member
02381    return 0 if no other queue has precedence (higher weight) or 1 if found  */
02382 static int compare_weight(struct call_queue *rq, struct member *member)
02383 {
02384    struct call_queue *q;
02385    struct member *mem;
02386    int found = 0;
02387    struct ao2_iterator queue_iter;
02388    
02389    /* q's lock and rq's lock already set by try_calling()
02390     * to solve deadlock */
02391    queue_iter = ao2_iterator_init(queues, 0);
02392    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
02393       if (q == rq) { /* don't check myself, could deadlock */
02394          queue_t_unref(q, "Done with iterator");
02395          continue;
02396       }
02397       ao2_lock(q);
02398       if (q->count && q->members) {
02399          if ((mem = ao2_find(q->members, member, OBJ_POINTER))) {
02400             ast_debug(1, "Found matching member %s in queue '%s'\n", mem->interface, q->name);
02401             if (q->weight > rq->weight && q->count >= num_available_members(q)) {
02402                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);
02403                found = 1;
02404             }
02405             ao2_ref(mem, -1);
02406          }
02407       }
02408       ao2_unlock(q);
02409       queue_t_unref(q, "Done with iterator");
02410       if (found) {
02411          break;
02412       }
02413    }
02414    ao2_iterator_destroy(&queue_iter);
02415    return found;
02416 }
02417 
02418 /*! \brief common hangup actions */
02419 static void do_hang(struct callattempt *o)
02420 {
02421    o->stillgoing = 0;
02422    ast_hangup(o->chan);
02423    o->chan = NULL;
02424 }
02425 
02426 /*! \brief convert "\n" to "\nVariable: " ready for manager to use */
02427 static char *vars2manager(struct ast_channel *chan, char *vars, size_t len)
02428 {
02429    struct ast_str *buf = ast_str_thread_get(&ast_str_thread_global_buf, len + 1);
02430    char *tmp;
02431 
02432    if (pbx_builtin_serialize_variables(chan, &buf)) {
02433       int i, j;
02434 
02435       /* convert "\n" to "\nVariable: " */
02436       strcpy(vars, "Variable: ");
02437       tmp = ast_str_buffer(buf);
02438 
02439       for (i = 0, j = 10; (i < len - 1) && (j < len - 1); i++, j++) {
02440          vars[j] = tmp[i];
02441 
02442          if (tmp[i + 1] == '\0')
02443             break;
02444          if (tmp[i] == '\n') {
02445             vars[j++] = '\r';
02446             vars[j++] = '\n';
02447 
02448             ast_copy_string(&(vars[j]), "Variable: ", len - j);
02449             j += 9;
02450          }
02451       }
02452       if (j > len - 3)
02453          j = len - 3;
02454       vars[j++] = '\r';
02455       vars[j++] = '\n';
02456       vars[j] = '\0';
02457    } else {
02458       /* there are no channel variables; leave it blank */
02459       *vars = '\0';
02460    }
02461    return vars;
02462 }
02463 
02464 /*! 
02465  * \brief Part 2 of ring_one
02466  *
02467  * Does error checking before attempting to request a channel and call a member. 
02468  * This function is only called from ring_one(). 
02469  * Failure can occur if:
02470  * - Agent on call
02471  * - Agent is paused
02472  * - Wrapup time not expired
02473  * - Priority by another queue
02474  *
02475  * \retval 1 on success to reach a free agent
02476  * \retval 0 on failure to get agent.
02477  */
02478 static int ring_entry(struct queue_ent *qe, struct callattempt *tmp, int *busies)
02479 {
02480    int res;
02481    int status;
02482    char tech[256];
02483    char *location;
02484    const char *macrocontext, *macroexten;
02485 
02486    /* on entry here, we know that tmp->chan == NULL */
02487    if ((tmp->lastqueue && tmp->lastqueue->wrapuptime && (time(NULL) - tmp->lastcall < tmp->lastqueue->wrapuptime)) ||
02488       (!tmp->lastqueue && qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime))) {
02489       ast_debug(1, "Wrapuptime not yet expired on queue %s for %s\n", 
02490             (tmp->lastqueue ? tmp->lastqueue->name : qe->parent->name), tmp->interface);
02491       if (qe->chan->cdr)
02492          ast_cdr_busy(qe->chan->cdr);
02493       tmp->stillgoing = 0;
02494       (*busies)++;
02495       return 0;
02496    }
02497 
02498    if (!qe->parent->ringinuse && (tmp->member->status != AST_DEVICE_NOT_INUSE) && (tmp->member->status != AST_DEVICE_UNKNOWN)) {
02499       ast_debug(1, "%s in use, can't receive call\n", tmp->interface);
02500       if (qe->chan->cdr)
02501          ast_cdr_busy(qe->chan->cdr);
02502       tmp->stillgoing = 0;
02503       return 0;
02504    }
02505 
02506    if (tmp->member->paused) {
02507       ast_debug(1, "%s paused, can't receive call\n", tmp->interface);
02508       if (qe->chan->cdr)
02509          ast_cdr_busy(qe->chan->cdr);
02510       tmp->stillgoing = 0;
02511       return 0;
02512    }
02513    if (use_weight && compare_weight(qe->parent,tmp->member)) {
02514       ast_debug(1, "Priority queue delaying call to %s:%s\n", qe->parent->name, tmp->interface);
02515       if (qe->chan->cdr)
02516          ast_cdr_busy(qe->chan->cdr);
02517       tmp->stillgoing = 0;
02518       (*busies)++;
02519       return 0;
02520    }
02521 
02522    ast_copy_string(tech, tmp->interface, sizeof(tech));
02523    if ((location = strchr(tech, '/')))
02524       *location++ = '\0';
02525    else
02526       location = "";
02527 
02528    /* Request the peer */
02529    tmp->chan = ast_request(tech, qe->chan->nativeformats, location, &status);
02530    if (!tmp->chan) {       /* If we can't, just go on to the next call */
02531       if (qe->chan->cdr)
02532          ast_cdr_busy(qe->chan->cdr);
02533       tmp->stillgoing = 0; 
02534 
02535       ao2_lock(qe->parent);
02536       update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
02537       qe->parent->rrpos++;
02538       qe->linpos++;
02539       ao2_unlock(qe->parent);
02540 
02541       (*busies)++;
02542       return 0;
02543    }
02544    
02545    if (qe->cancel_answered_elsewhere) {
02546       ast_set_flag(tmp->chan, AST_FLAG_ANSWERED_ELSEWHERE);
02547    }
02548    tmp->chan->appl = "AppQueue";
02549    tmp->chan->data = "(Outgoing Line)";
02550    memset(&tmp->chan->whentohangup, 0, sizeof(tmp->chan->whentohangup));
02551    if (tmp->chan->cid.cid_num)
02552       ast_free(tmp->chan->cid.cid_num);
02553    tmp->chan->cid.cid_num = ast_strdup(qe->chan->cid.cid_num);
02554    if (tmp->chan->cid.cid_name)
02555       ast_free(tmp->chan->cid.cid_name);
02556    tmp->chan->cid.cid_name = ast_strdup(qe->chan->cid.cid_name);
02557    if (tmp->chan->cid.cid_ani)
02558       ast_free(tmp->chan->cid.cid_ani);
02559    tmp->chan->cid.cid_ani = ast_strdup(qe->chan->cid.cid_ani);
02560 
02561    /* Inherit specially named variables from parent channel */
02562    ast_channel_inherit_variables(qe->chan, tmp->chan);
02563    ast_channel_datastore_inherit(qe->chan, tmp->chan);
02564 
02565    /* Presense of ADSI CPE on outgoing channel follows ours */
02566    tmp->chan->adsicpe = qe->chan->adsicpe;
02567 
02568    /* Inherit context and extension */
02569    ast_channel_lock(qe->chan);
02570    macrocontext = pbx_builtin_getvar_helper(qe->chan, "MACRO_CONTEXT");
02571    ast_string_field_set(tmp->chan, dialcontext, ast_strlen_zero(macrocontext) ? qe->chan->context : macrocontext);
02572    macroexten = pbx_builtin_getvar_helper(qe->chan, "MACRO_EXTEN");
02573    if (!ast_strlen_zero(macroexten))
02574       ast_copy_string(tmp->chan->exten, macroexten, sizeof(tmp->chan->exten));
02575    else
02576       ast_copy_string(tmp->chan->exten, qe->chan->exten, sizeof(tmp->chan->exten));
02577    if (ast_cdr_isset_unanswered()) {
02578       /* they want to see the unanswered dial attempts! */
02579       /* set up the CDR fields on all the CDRs to give sensical information */
02580       ast_cdr_setdestchan(tmp->chan->cdr, tmp->chan->name);
02581       strcpy(tmp->chan->cdr->clid, qe->chan->cdr->clid);
02582       strcpy(tmp->chan->cdr->channel, qe->chan->cdr->channel);
02583       strcpy(tmp->chan->cdr->src, qe->chan->cdr->src);
02584       strcpy(tmp->chan->cdr->dst, qe->chan->exten);
02585       strcpy(tmp->chan->cdr->dcontext, qe->chan->context);
02586       strcpy(tmp->chan->cdr->lastapp, qe->chan->cdr->lastapp);
02587       strcpy(tmp->chan->cdr->lastdata, qe->chan->cdr->lastdata);
02588       tmp->chan->cdr->amaflags = qe->chan->cdr->amaflags;
02589       strcpy(tmp->chan->cdr->accountcode, qe->chan->cdr->accountcode);
02590       strcpy(tmp->chan->cdr->userfield, qe->chan->cdr->userfield);
02591    }
02592    ast_channel_unlock(qe->chan);
02593 
02594    /* Place the call, but don't wait on the answer */
02595    if ((res = ast_call(tmp->chan, location, 0))) {
02596       /* Again, keep going even if there's an error */
02597       ast_debug(1, "ast call on peer returned %d\n", res);
02598       ast_verb(3, "Couldn't call %s\n", tmp->interface);
02599       do_hang(tmp);
02600       (*busies)++;
02601       update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
02602       return 0;
02603    } else if (qe->parent->eventwhencalled) {
02604       char vars[2048];
02605 
02606       manager_event(EVENT_FLAG_AGENT, "AgentCalled",
02607                "Queue: %s\r\n"
02608                "AgentCalled: %s\r\n"
02609                "AgentName: %s\r\n"
02610                "ChannelCalling: %s\r\n"
02611                "DestinationChannel: %s\r\n"
02612                "CallerIDNum: %s\r\n"
02613                "CallerIDName: %s\r\n"
02614                "Context: %s\r\n"
02615                "Extension: %s\r\n"
02616                "Priority: %d\r\n"
02617                "Uniqueid: %s\r\n"
02618                "%s",
02619                qe->parent->name, tmp->interface, tmp->member->membername, qe->chan->name, tmp->chan->name,
02620                tmp->chan->cid.cid_num ? tmp->chan->cid.cid_num : "unknown",
02621                tmp->chan->cid.cid_name ? tmp->chan->cid.cid_name : "unknown",
02622                qe->chan->context, qe->chan->exten, qe->chan->priority, qe->chan->uniqueid,
02623                qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
02624       ast_verb(3, "Called %s\n", tmp->interface);
02625    }
02626 
02627    update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
02628    return 1;
02629 }
02630 
02631 /*! \brief find the entry with the best metric, or NULL */
02632 static struct callattempt *find_best(struct callattempt *outgoing)
02633 {
02634    struct callattempt *best = NULL, *cur;
02635 
02636    for (cur = outgoing; cur; cur = cur->q_next) {
02637       if (cur->stillgoing &&              /* Not already done */
02638          !cur->chan &&              /* Isn't already going */
02639          (!best || cur->metric < best->metric)) {     /* We haven't found one yet, or it's better */
02640          best = cur;
02641       }
02642    }
02643 
02644    return best;
02645 }
02646 
02647 /*! 
02648  * \brief Place a call to a queue member.
02649  *
02650  * Once metrics have been calculated for each member, this function is used
02651  * to place a call to the appropriate member (or members). The low-level
02652  * channel-handling and error detection is handled in ring_entry
02653  *
02654  * \retval 1 if a member was called successfully
02655  * \retval 0 otherwise
02656  */
02657 static int ring_one(struct queue_ent *qe, struct callattempt *outgoing, int *busies)
02658 {
02659    int ret = 0;
02660 
02661    while (ret == 0) {
02662       struct callattempt *best = find_best(outgoing);
02663       if (!best) {
02664          ast_debug(1, "Nobody left to try ringing in queue\n");
02665          break;
02666       }
02667       if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
02668          struct callattempt *cur;
02669          /* Ring everyone who shares this best metric (for ringall) */
02670          for (cur = outgoing; cur; cur = cur->q_next) {
02671             if (cur->stillgoing && !cur->chan && cur->metric <= best->metric) {
02672                ast_debug(1, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
02673                ret |= ring_entry(qe, cur, busies);
02674             }
02675          }
02676       } else {
02677          /* Ring just the best channel */
02678          ast_debug(1, "Trying '%s' with metric %d\n", best->interface, best->metric);
02679          ret = ring_entry(qe, best, busies);
02680       }
02681       
02682       /* If we have timed out, break out */
02683       if (qe->expire && (time(NULL) >= qe->expire)) {
02684          ast_debug(1, "Queue timed out while ringing members.\n");
02685          ret = 0;
02686          break;
02687       }
02688    }
02689 
02690    return ret;
02691 }
02692 
02693 /*! \brief Search for best metric and add to Round Robbin queue */
02694 static int store_next_rr(struct queue_ent *qe, struct callattempt *outgoing)
02695 {
02696    struct callattempt *best = find_best(outgoing);
02697 
02698    if (best) {
02699       /* Ring just the best channel */
02700       ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
02701       qe->parent->rrpos = best->metric % 1000;
02702    } else {
02703       /* Just increment rrpos */
02704       if (qe->parent->wrapped) {
02705          /* No more channels, start over */
02706          qe->parent->rrpos = 0;
02707       } else {
02708          /* Prioritize next entry */
02709          qe->parent->rrpos++;
02710       }
02711    }
02712    qe->parent->wrapped = 0;
02713 
02714    return 0;
02715 }
02716 
02717 /*! \brief Search for best metric and add to Linear queue */
02718 static int store_next_lin(struct queue_ent *qe, struct callattempt *outgoing)
02719 {
02720    struct callattempt *best = find_best(outgoing);
02721 
02722    if (best) {
02723       /* Ring just the best channel */
02724       ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
02725       qe->linpos = best->metric % 1000;
02726    } else {
02727       /* Just increment rrpos */
02728       if (qe->linwrapped) {
02729          /* No more channels, start over */
02730          qe->linpos = 0;
02731       } else {
02732          /* Prioritize next entry */
02733          qe->linpos++;
02734       }
02735    }
02736    qe->linwrapped = 0;
02737 
02738    return 0;
02739 }
02740 
02741 /*! \brief Playback announcement to queued members if peroid has elapsed */
02742 static int say_periodic_announcement(struct queue_ent *qe, int ringing)
02743 {
02744    int res = 0;
02745    time_t now;
02746 
02747    /* Get the current time */
02748    time(&now);
02749 
02750    /* Check to see if it is time to announce */
02751    if ((now - qe->last_periodic_announce_time) < qe->parent->periodicannouncefrequency)
02752       return 0;
02753 
02754    /* Stop the music on hold so we can play our own file */
02755    if (ringing)
02756       ast_indicate(qe->chan,-1);
02757    else
02758       ast_moh_stop(qe->chan);
02759 
02760    ast_verb(3, "Playing periodic announcement\n");
02761    
02762    if (qe->parent->randomperiodicannounce && qe->parent->numperiodicannounce) {
02763       qe->last_periodic_announce_sound = ((unsigned long) ast_random()) % qe->parent->numperiodicannounce;
02764    } else if (qe->last_periodic_announce_sound >= qe->parent->numperiodicannounce || 
02765       ast_str_strlen(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]) == 0) {
02766       qe->last_periodic_announce_sound = 0;
02767    }
02768    
02769    /* play the announcement */
02770    res = play_file(qe->chan, ast_str_buffer(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]));
02771 
02772    if (res > 0 && !valid_exit(qe, res))
02773       res = 0;
02774 
02775    /* Resume Music on Hold if the caller is going to stay in the queue */
02776    if (!res) {
02777       if (ringing)
02778          ast_indicate(qe->chan, AST_CONTROL_RINGING);
02779       else
02780          ast_moh_start(qe->chan, qe->moh, NULL);
02781    }
02782 
02783    /* update last_periodic_announce_time */
02784    qe->last_periodic_announce_time = now;
02785 
02786    /* Update the current periodic announcement to the next announcement */
02787    if (!qe->parent->randomperiodicannounce) {
02788       qe->last_periodic_announce_sound++;
02789    }
02790    
02791    return res;
02792 }
02793 
02794 /*! \brief Record that a caller gave up on waiting in queue */
02795 static void record_abandoned(struct queue_ent *qe)
02796 {
02797    ao2_lock(qe->parent);
02798    set_queue_variables(qe->parent, qe->chan);
02799    manager_event(EVENT_FLAG_AGENT, "QueueCallerAbandon",
02800       "Queue: %s\r\n"
02801       "Uniqueid: %s\r\n"
02802       "Position: %d\r\n"
02803       "OriginalPosition: %d\r\n"
02804       "HoldTime: %d\r\n",
02805       qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
02806 
02807    qe->parent->callsabandoned++;
02808    ao2_unlock(qe->parent);
02809 }
02810 
02811 /*! \brief RNA == Ring No Answer. Common code that is executed when we try a queue member and they don't answer. */
02812 static void rna(int rnatime, struct queue_ent *qe, char *interface, char *membername, int pause)
02813 {
02814    ast_verb(3, "Nobody picked up in %d ms\n", rnatime);
02815    if (qe->parent->eventwhencalled) {
02816       char vars[2048];
02817 
02818       manager_event(EVENT_FLAG_AGENT, "AgentRingNoAnswer",
02819                   "Queue: %s\r\n"
02820                   "Uniqueid: %s\r\n"
02821                   "Channel: %s\r\n"
02822                   "Member: %s\r\n"
02823                   "MemberName: %s\r\n"
02824                   "Ringtime: %d\r\n"
02825                   "%s",
02826                   qe->parent->name,
02827                   qe->chan->uniqueid,
02828                   qe->chan->name,
02829                   interface,
02830                   membername,
02831                   rnatime,
02832                   qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
02833    }
02834    ast_queue_log(qe->parent->name, qe->chan->uniqueid, membername, "RINGNOANSWER", "%d", rnatime);
02835    if (qe->parent->autopause && pause) {
02836       if (!set_member_paused(qe->parent->name, interface, "Auto-Pause", 1)) {
02837          ast_verb(3, "Auto-Pausing Queue Member %s in queue %s since they failed to answer.\n", interface, qe->parent->name);
02838       } else {
02839          ast_verb(3, "Failed to pause Queue Member %s in queue %s!\n", interface, qe->parent->name);
02840       }
02841    }
02842    return;
02843 }
02844 
02845 #define AST_MAX_WATCHERS 256
02846 /*! \brief Wait for a member to answer the call
02847  *
02848  * \param[in] qe the queue_ent corresponding to the caller in the queue
02849  * \param[in] outgoing the list of callattempts. Relevant ones will have their chan and stillgoing parameters non-zero
02850  * \param[in] to the amount of time (in milliseconds) to wait for a response
02851  * \param[out] digit if a user presses a digit to exit the queue, this is the digit the caller pressed
02852  * \param[in] prebusies number of busy members calculated prior to calling wait_for_answer
02853  * \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
02854  * \param[in] forwardsallowed used to detect if we should allow call forwarding, based on the 'i' option to Queue()
02855  */
02856 static struct callattempt *wait_for_answer(struct queue_ent *qe, struct callattempt *outgoing, int *to, char *digit, int prebusies, int caller_disconnect, int forwardsallowed)
02857 {
02858    const char *queue = qe->parent->name;
02859    struct callattempt *o, *start = NULL, *prev = NULL;
02860    int status;
02861    int numbusies = prebusies;
02862    int numnochan = 0;
02863    int stillgoing = 0;
02864    int orig = *to;
02865    struct ast_frame *f;
02866    struct callattempt *peer = NULL;
02867    struct ast_channel *winner;
02868    struct ast_channel *in = qe->chan;
02869    char on[80] = "";
02870    char membername[80] = "";
02871    long starttime = 0;
02872    long endtime = 0;
02873 #ifdef HAVE_EPOLL
02874    struct callattempt *epollo;
02875 #endif
02876 
02877    starttime = (long) time(NULL);
02878 #ifdef HAVE_EPOLL
02879    for (epollo = outgoing; epollo; epollo = epollo->q_next) {
02880       if (epollo->chan)
02881          ast_poll_channel_add(in, epollo->chan);
02882    }
02883 #endif
02884    
02885    while (*to && !peer) {
02886       int numlines, retry, pos = 1;
02887       struct ast_channel *watchers[AST_MAX_WATCHERS];
02888       watchers[0] = in;
02889       start = NULL;
02890 
02891       for (retry = 0; retry < 2; retry++) {
02892          numlines = 0;
02893          for (o = outgoing; o; o = o->q_next) { /* Keep track of important channels */
02894             if (o->stillgoing) { /* Keep track of important channels */
02895                stillgoing = 1;
02896                if (o->chan) {
02897                   watchers[pos++] = o->chan;
02898                   if (!start)
02899                      start = o;
02900                   else
02901                      prev->call_next = o;
02902                   prev = o;
02903                }
02904             }
02905             numlines++;
02906          }
02907          if (pos > 1 /* found */ || !stillgoing /* nobody listening */ ||
02908             (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) /* ring would not be delivered */)
02909             break;
02910          /* On "ringall" strategy we only move to the next penalty level
02911             when *all* ringing phones are done in the current penalty level */
02912          ring_one(qe, outgoing, &numbusies);
02913          /* and retry... */
02914       }
02915       if (pos == 1 /* not found */) {
02916          if (numlines == (numbusies + numnochan)) {
02917             ast_debug(1, "Everyone is busy at this time\n");
02918          } else {
02919             ast_debug(3, "No one is answering queue '%s' (%d numlines / %d busies / %d failed channels)\n", queue, numlines, numbusies, numnochan);
02920          }
02921          *to = 0;
02922          return NULL;
02923       }
02924 
02925       /* Poll for events from both the incoming channel as well as any outgoing channels */
02926       winner = ast_waitfor_n(watchers, pos, to);
02927 
02928       /* Service all of the outgoing channels */
02929       for (o = start; o; o = o->call_next) {
02930          if (o->stillgoing && (o->chan) &&  (o->chan->_state == AST_STATE_UP)) {
02931             if (!peer) {
02932                ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
02933                peer = o;
02934             }
02935          } else if (o->chan && (o->chan == winner)) {
02936 
02937             ast_copy_string(on, o->member->interface, sizeof(on));
02938             ast_copy_string(membername, o->member->membername, sizeof(membername));
02939 
02940             if (!ast_strlen_zero(o->chan->call_forward) && !forwardsallowed) {
02941                ast_verb(3, "Forwarding %s to '%s' prevented.\n", in->name, o->chan->call_forward);
02942                numnochan++;
02943                do_hang(o);
02944                winner = NULL;
02945                continue;
02946             } else if (!ast_strlen_zero(o->chan->call_forward)) {
02947                char tmpchan[256];
02948                char *stuff;
02949                char *tech;
02950 
02951                ast_copy_string(tmpchan, o->chan->call_forward, sizeof(tmpchan));
02952                if ((stuff = strchr(tmpchan, '/'))) {
02953                   *stuff++ = '\0';
02954                   tech = tmpchan;
02955                } else {
02956                   snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, o->chan->context);
02957                   stuff = tmpchan;
02958                   tech = "Local";
02959                }
02960                /* Before processing channel, go ahead and check for forwarding */
02961                ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, o->chan->name);
02962                /* Setup parameters */
02963                o->chan = ast_request(tech, in->nativeformats, stuff, &status);
02964                if (!o->chan) {
02965                   ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s'\n", tech, stuff);
02966                   o->stillgoing = 0;
02967                   numnochan++;
02968                } else {
02969                   ast_channel_inherit_variables(in, o->chan);
02970                   ast_channel_datastore_inherit(in, o->chan);
02971                   if (o->chan->cid.cid_num)
02972                      ast_free(o->chan->cid.cid_num);
02973                   o->chan->cid.cid_num = ast_strdup(in->cid.cid_num);
02974 
02975                   if (o->chan->cid.cid_name)
02976                      ast_free(o->chan->cid.cid_name);
02977                   o->chan->cid.cid_name = ast_strdup(in->cid.cid_name);
02978 
02979                   ast_string_field_set(o->chan, accountcode, in->accountcode);
02980                   o->chan->cdrflags = in->cdrflags;
02981 
02982                   if (in->cid.cid_ani) {
02983                      if (o->chan->cid.cid_ani)
02984                         ast_free(o->chan->cid.cid_ani);
02985                      o->chan->cid.cid_ani = ast_strdup(in->cid.cid_ani);
02986                   }
02987                   if (o->chan->cid.cid_rdnis)
02988                      ast_free(o->chan->cid.cid_rdnis);
02989                   o->chan->cid.cid_rdnis = ast_strdup(S_OR(in->macroexten, in->exten));
02990                   if (ast_call(o->chan, tmpchan, 0)) {
02991                      ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
02992                      do_hang(o);
02993                      numnochan++;
02994                   }
02995                }
02996                /* Hangup the original channel now, in case we needed it */
02997                ast_hangup(winner);
02998                continue;
02999             }
03000             f = ast_read(winner);
03001             if (f) {
03002                if (f->frametype == AST_FRAME_CONTROL) {
03003                   switch (f->subclass) {
03004                   case AST_CONTROL_ANSWER:
03005                      /* This is our guy if someone answered. */
03006                      if (!peer) {
03007                         ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
03008                         peer = o;
03009                      }
03010                      break;
03011                   case AST_CONTROL_BUSY:
03012                      ast_verb(3, "%s is busy\n", o->chan->name);
03013                      if (in->cdr)
03014                         ast_cdr_busy(in->cdr);
03015                      do_hang(o);
03016                      endtime = (long) time(NULL);
03017                      endtime -= starttime;
03018                      rna(endtime * 1000, qe, on, membername, 0);
03019                      if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
03020                         if (qe->parent->timeoutrestart)
03021                            *to = orig;
03022                         /* Have enough time for a queue member to answer? */
03023                         if (*to > 500) {
03024                            ring_one(qe, outgoing, &numbusies);
03025                            starttime = (long) time(NULL);
03026                         }
03027                      }
03028                      numbusies++;
03029                      break;
03030                   case AST_CONTROL_CONGESTION:
03031                      ast_verb(3, "%s is circuit-busy\n", o->chan->name);
03032                      if (in->cdr)
03033                         ast_cdr_busy(in->cdr);
03034                      endtime = (long) time(NULL);
03035                      endtime -= starttime;
03036                      rna(endtime * 1000, qe, on, membername, 0);
03037                      do_hang(o);
03038                      if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
03039                         if (qe->parent->timeoutrestart)
03040                            *to = orig;
03041                         if (*to > 500) {
03042                            ring_one(qe, outgoing, &numbusies);
03043                            starttime = (long) time(NULL);
03044                         }
03045                      }
03046                      numbusies++;
03047                      break;
03048                   case AST_CONTROL_RINGING:
03049                      ast_verb(3, "%s is ringing\n", o->chan->name);
03050                      break;
03051                   case AST_CONTROL_OFFHOOK:
03052                      /* Ignore going off hook */
03053                      break;
03054                   default:
03055                      ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
03056                   }
03057                }
03058                ast_frfree(f);
03059             } else { /* ast_read() returned NULL */
03060                endtime = (long) time(NULL) - starttime;
03061                rna(endtime * 1000, qe, on, membername, 1);
03062                do_hang(o);
03063                if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
03064                   if (qe->parent->timeoutrestart)
03065                      *to = orig;
03066                   if (*to > 500) {
03067                      ring_one(qe, outgoing, &numbusies);
03068                      starttime = (long) time(NULL);
03069                   }
03070                }
03071             }
03072          }
03073       }
03074 
03075       /* If we received an event from the caller, deal with it. */
03076       if (winner == in) {
03077          f = ast_read(in);
03078          if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
03079             /* Got hung up */
03080             *to = -1;
03081             if (f) {
03082                if (f->data.uint32) {
03083                   in->hangupcause = f->data.uint32;
03084                }
03085                ast_frfree(f);
03086             }
03087             return NULL;
03088          }
03089          if ((f->frametype == AST_FRAME_DTMF) && caller_disconnect && (f->subclass == '*')) {
03090             ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
03091             *to = 0;
03092             ast_frfree(f);
03093             return NULL;
03094          }
03095          if ((f->frametype == AST_FRAME_DTMF) && valid_exit(qe, f->subclass)) {
03096             ast_verb(3, "User pressed digit: %c\n", f->subclass);
03097             *to = 0;
03098             *digit = f->subclass;
03099             ast_frfree(f);
03100             return NULL;
03101          }
03102          ast_frfree(f);
03103       }
03104       if (!*to) {
03105          for (o = start; o; o = o->call_next)
03106             rna(orig, qe, o->interface, o->member->membername, 1);
03107       }
03108    }
03109 
03110 #ifdef HAVE_EPOLL
03111    for (epollo = outgoing; epollo; epollo = epollo->q_next) {
03112       if (epollo->chan)
03113          ast_poll_channel_del(in, epollo->chan);
03114    }
03115 #endif
03116 
03117    return peer;
03118 }
03119 
03120 /*! 
03121  * \brief Check if we should start attempting to call queue members.
03122  *
03123  * A simple process, really. Count the number of members who are available
03124  * to take our call and then see if we are in a position in the queue at
03125  * which a member could accept our call.
03126  *
03127  * \param[in] qe The caller who wants to know if it is his turn
03128  * \retval 0 It is not our turn
03129  * \retval 1 It is our turn
03130  */
03131 static int is_our_turn(struct queue_ent *qe)
03132 {
03133    struct queue_ent *ch;
03134    int res;
03135    int avl;
03136    int idx = 0;
03137    /* This needs a lock. How many members are available to be served? */
03138    ao2_lock(qe->parent);
03139 
03140    avl = num_available_members(qe->parent);
03141 
03142    ch = qe->parent->head;
03143 
03144    ast_debug(1, "There %s %d available %s.\n", avl != 1 ? "are" : "is", avl, avl != 1 ? "members" : "member");
03145 
03146    while ((idx < avl) && (ch) && (ch != qe)) {
03147       if (!ch->pending)
03148          idx++;
03149       ch = ch->next;       
03150    }
03151 
03152    ao2_unlock(qe->parent);
03153    /* If the queue entry is within avl [the number of available members] calls from the top ... 
03154     * Autofill and position check added to support autofill=no (as only calls
03155     * from the front of the queue are valid when autofill is disabled)
03156     */
03157    if (ch && idx < avl && (qe->parent->autofill || qe->pos == 1)) {
03158       ast_debug(1, "It's our turn (%s).\n", qe->chan->name);
03159       res = 1;
03160    } else {
03161       ast_debug(1, "It's not our turn (%s).\n", qe->chan->name);
03162       res = 0;
03163    }
03164 
03165    return res;
03166 }
03167 
03168 /*!
03169  * \brief update rules for queues
03170  *
03171  * Calculate min/max penalties making sure if relative they stay within bounds.
03172  * Update queues penalty and set dialplan vars, goto next list entry.
03173 */
03174 static void update_qe_rule(struct queue_ent *qe)
03175 {
03176    int max_penalty = qe->pr->max_relative ? qe->max_penalty + qe->pr->max_value : qe->pr->max_value;
03177    int min_penalty = qe->pr->min_relative ? qe->min_penalty + qe->pr->min_value : qe->pr->min_value;
03178    char max_penalty_str[20], min_penalty_str[20]; 
03179    /* a relative change to the penalty could put it below 0 */
03180    if (max_penalty < 0)
03181       max_penalty = 0;
03182    if (min_penalty < 0)
03183       min_penalty = 0;
03184    if (min_penalty > max_penalty)
03185       min_penalty = max_penalty;
03186    snprintf(max_penalty_str, sizeof(max_penalty_str), "%d", max_penalty);
03187    snprintf(min_penalty_str, sizeof(min_penalty_str), "%d", min_penalty);
03188    pbx_builtin_setvar_helper(qe->chan, "QUEUE_MAX_PENALTY", max_penalty_str);
03189    pbx_builtin_setvar_helper(qe->chan, "QUEUE_MIN_PENALTY", min_penalty_str);
03190    qe->max_penalty = max_penalty;
03191    qe->min_penalty = min_penalty;
03192    ast_debug(3, "Setting max penalty to %d and min penalty to %d for caller %s since %d seconds have elapsed\n", qe->max_penalty, qe->min_penalty, qe->chan->name, qe->pr->time);
03193    qe->pr = AST_LIST_NEXT(qe->pr, list);
03194 }
03195 
03196 /*! \brief The waiting areas for callers who are not actively calling members
03197  *
03198  * This function is one large loop. This function will return if a caller
03199  * either exits the queue or it becomes that caller's turn to attempt calling
03200  * queue members. Inside the loop, we service the caller with periodic announcements,
03201  * holdtime announcements, etc. as configured in queues.conf
03202  *
03203  * \retval  0 if the caller's turn has arrived
03204  * \retval -1 if the caller should exit the queue.
03205  */
03206 static int wait_our_turn(struct queue_ent *qe, int ringing, enum queue_result *reason)
03207 {
03208    int res = 0;
03209 
03210    /* This is the holding pen for callers 2 through maxlen */
03211    for (;;) {
03212 
03213       if (is_our_turn(qe))
03214          break;
03215 
03216       /* If we have timed out, break out */
03217       if (qe->expire && (time(NULL) >= qe->expire)) {
03218          *reason = QUEUE_TIMEOUT;
03219          break;
03220       }
03221 
03222       if (qe->parent->leavewhenempty) {
03223          int status = 0;
03224 
03225          if ((status = get_member_status(qe->parent, qe->max_penalty, qe->min_penalty, qe->parent->leavewhenempty))) {
03226             *reason = QUEUE_LEAVEEMPTY;
03227             ast_queue_log(qe->parent->name, qe->chan->uniqueid, "NONE", "EXITEMPTY", "%d|%d|%ld", qe->pos, qe->opos, (long) time(NULL) - qe->start);
03228             leave_queue(qe);
03229             break;
03230          }
03231       }
03232 
03233       /* Make a position announcement, if enabled */
03234       if (qe->parent->announcefrequency &&
03235          (res = say_position(qe,ringing)))
03236          break;
03237 
03238       /* If we have timed out, break out */
03239       if (qe->expire && (time(NULL) >= qe->expire)) {
03240          *reason = QUEUE_TIMEOUT;
03241          break;
03242       }
03243 
03244       /* Make a periodic announcement, if enabled */
03245       if (qe->parent->periodicannouncefrequency &&
03246          (res = say_periodic_announcement(qe,ringing)))
03247          break;
03248       
03249       /* see if we need to move to the next penalty level for this queue */
03250       while (qe->pr && ((time(NULL) - qe->start) >= qe->pr->time)) {
03251          update_qe_rule(qe);
03252       }
03253 
03254       /* If we have timed out, break out */
03255       if (qe->expire && (time(NULL) >= qe->expire)) {
03256          *reason = QUEUE_TIMEOUT;
03257          break;
03258       }
03259       
03260       /* Wait a second before checking again */
03261       if ((res = ast_waitfordigit(qe->chan, RECHECK * 1000))) {
03262          if (res > 0 && !valid_exit(qe, res))
03263             res = 0;
03264          else
03265             break;
03266       }
03267       
03268       /* If we have timed out, break out */
03269       if (qe->expire && (time(NULL) >= qe->expire)) {
03270          *reason = QUEUE_TIMEOUT;
03271          break;
03272       }
03273    }
03274 
03275    return res;
03276 }
03277 
03278 /*!
03279  * \brief update the queue status
03280  * \retval Always 0
03281 */
03282 static int update_queue(struct call_queue *q, struct member *member, int callcompletedinsl, int newtalktime)
03283 {
03284    int oldtalktime;
03285 
03286    struct member *mem;
03287    struct call_queue *qtmp;
03288    struct ao2_iterator queue_iter;  
03289    
03290    if (shared_lastcall) {
03291       queue_iter = ao2_iterator_init(queues, 0);
03292       while ((qtmp = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
03293          ao2_lock(qtmp);
03294          if ((mem = ao2_find(qtmp->members, member, OBJ_POINTER))) {
03295             time(&mem->lastcall);
03296             mem->calls++;
03297             mem->lastqueue = q;
03298             ao2_ref(mem, -1);
03299          }
03300          ao2_unlock(qtmp);
03301          queue_t_unref(qtmp, "Done with iterator");
03302       }
03303       ao2_iterator_destroy(&queue_iter);
03304    } else {
03305       ao2_lock(q);
03306       time(&member->lastcall);
03307       member->calls++;
03308       member->lastqueue = q;
03309       ao2_unlock(q);
03310    }  
03311    ao2_lock(q);
03312    q->callscompleted++;
03313    if (callcompletedinsl)
03314       q->callscompletedinsl++;
03315    /* Calculate talktime using the same exponential average as holdtime code*/
03316    oldtalktime = q->talktime;
03317    q->talktime = (((oldtalktime << 2) - oldtalktime) + newtalktime) >> 2;
03318    ao2_unlock(q);
03319    return 0;
03320 }
03321 
03322 /*! \brief Calculate the metric of each member in the outgoing callattempts
03323  *
03324  * A numeric metric is given to each member depending on the ring strategy used
03325  * by the queue. Members with lower metrics will be called before members with
03326  * higher metrics
03327  * \retval -1 if penalties are exceeded
03328  * \retval 0 otherwise
03329  */
03330 static int calc_metric(struct call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct callattempt *tmp)
03331 {
03332    if ((qe->max_penalty && (mem->penalty > qe->max_penalty)) || (qe->min_penalty && (mem->penalty < qe->min_penalty)))
03333       return -1;
03334 
03335    switch (q->strategy) {
03336    case QUEUE_STRATEGY_RINGALL:
03337       /* Everyone equal, except for penalty */
03338       tmp->metric = mem->penalty * 1000000;
03339       break;
03340    case QUEUE_STRATEGY_LINEAR:
03341       if (pos < qe->linpos) {
03342          tmp->metric = 1000 + pos;
03343       } else {
03344          if (pos > qe->linpos)
03345             /* Indicate there is another priority */
03346             qe->linwrapped = 1;
03347          tmp->metric = pos;
03348       }
03349       tmp->metric += mem->penalty * 1000000;
03350       break;
03351    case QUEUE_STRATEGY_RRMEMORY:
03352       if (pos < q->rrpos) {
03353          tmp->metric = 1000 + pos;
03354       } else {
03355          if (pos > q->rrpos)
03356             /* Indicate there is another priority */
03357             q->wrapped = 1;
03358          tmp->metric = pos;
03359       }
03360       tmp->metric += mem->penalty * 1000000;
03361       break;
03362    case QUEUE_STRATEGY_RANDOM:
03363       tmp->metric = ast_random() % 1000;
03364       tmp->metric += mem->penalty * 1000000;
03365       break;
03366    case QUEUE_STRATEGY_WRANDOM:
03367       tmp->metric = ast_random() % ((1 + mem->penalty) * 1000);
03368       break;
03369    case QUEUE_STRATEGY_FEWESTCALLS:
03370       tmp->metric = mem->calls;
03371       tmp->metric += mem->penalty * 1000000;
03372       break;
03373    case QUEUE_STRATEGY_LEASTRECENT:
03374       if (!mem->lastcall)
03375          tmp->metric = 0;
03376       else
03377          tmp->metric = 1000000 - (time(NULL) - mem->lastcall);
03378       tmp->metric += mem->penalty * 1000000;
03379       break;
03380    default:
03381       ast_log(LOG_WARNING, "Can't calculate metric for unknown strategy %d\n", q->strategy);
03382       break;
03383    }
03384    return 0;
03385 }
03386 
03387 enum agent_complete_reason {
03388    CALLER,
03389    AGENT,
03390    TRANSFER
03391 };
03392 
03393 /*! \brief Send out AMI message with member call completion status information */
03394 static void send_agent_complete(const struct queue_ent *qe, const char *queuename,
03395    const struct ast_channel *peer, const struct member *member, time_t callstart,
03396    char *vars, size_t vars_len, enum agent_complete_reason rsn)
03397 {
03398    const char *reason = NULL; /* silence dumb compilers */
03399 
03400    if (!qe->parent->eventwhencalled)
03401       return;
03402 
03403    switch (rsn) {
03404    case CALLER:
03405       reason = "caller";
03406       break;
03407    case AGENT:
03408       reason = "agent";
03409       break;
03410    case TRANSFER:
03411       reason = "transfer";
03412       break;
03413    }
03414 
03415    manager_event(EVENT_FLAG_AGENT, "AgentComplete",
03416       "Queue: %s\r\n"
03417       "Uniqueid: %s\r\n"
03418       "Channel: %s\r\n"
03419       "Member: %s\r\n"
03420       "MemberName: %s\r\n"
03421       "HoldTime: %ld\r\n"
03422       "TalkTime: %ld\r\n"
03423       "Reason: %s\r\n"
03424       "%s",
03425       queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
03426       (long)(callstart - qe->start), (long)(time(NULL) - callstart), reason,
03427       qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, vars_len) : "");
03428 }
03429 
03430 struct queue_transfer_ds {
03431    struct queue_ent *qe;
03432    struct member *member;
03433    time_t starttime;
03434    int callcompletedinsl;
03435 };
03436 
03437 static void queue_transfer_destroy(void *data)
03438 {
03439    struct queue_transfer_ds *qtds = data;
03440    ast_free(qtds);
03441 }
03442 
03443 /*! \brief a datastore used to help correctly log attended transfers of queue callers
03444  */
03445 static const struct ast_datastore_info queue_transfer_info = {
03446    .type = "queue_transfer",
03447    .chan_fixup = queue_transfer_fixup,
03448    .destroy = queue_transfer_destroy,
03449 };
03450 
03451 /*! \brief Log an attended transfer when a queue caller channel is masqueraded
03452  *
03453  * When a caller is masqueraded, we want to log a transfer. Fixup time is the closest we can come to when
03454  * the actual transfer occurs. This happens during the masquerade after datastores are moved from old_chan
03455  * to new_chan. This is why new_chan is referenced for exten, context, and datastore information.
03456  *
03457  * At the end of this, we want to remove the datastore so that this fixup function is not called on any
03458  * future masquerades of the caller during the current call.
03459  */
03460 static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
03461 {
03462    struct queue_transfer_ds *qtds = data;
03463    struct queue_ent *qe = qtds->qe;
03464    struct member *member = qtds->member;
03465    time_t callstart = qtds->starttime;
03466    int callcompletedinsl = qtds->callcompletedinsl;
03467    struct ast_datastore *datastore;
03468 
03469    ast_queue_log(qe->parent->name, qe->chan->uniqueid, member->membername, "TRANSFER", "%s|%s|%ld|%ld|%d",
03470             new_chan->exten, new_chan->context, (long) (callstart - qe->start),
03471             (long) (time(NULL) - callstart), qe->opos);
03472 
03473    update_queue(qe->parent, member, callcompletedinsl, (time(NULL) - callstart));
03474    
03475    /* No need to lock the channels because they are already locked in ast_do_masquerade */
03476    if ((datastore = ast_channel_datastore_find(old_chan, &queue_transfer_info, NULL))) {
03477       ast_channel_datastore_remove(old_chan, datastore);
03478    } else {
03479       ast_log(LOG_WARNING, "Can't find the queue_transfer datastore.\n");
03480    }
03481 }
03482 
03483 /*! \brief mechanism to tell if a queue caller was atxferred by a queue member.
03484  *
03485  * When a caller is atxferred, then the queue_transfer_info datastore
03486  * is removed from the channel. If it's still there after the bridge is
03487  * broken, then the caller was not atxferred.
03488  *
03489  * \note Only call this with chan locked
03490  */
03491 static int attended_transfer_occurred(struct ast_channel *chan)
03492 {
03493    return ast_channel_datastore_find(chan, &queue_transfer_info, NULL) ? 0 : 1;
03494 }
03495 
03496 /*! \brief create a datastore for storing relevant info to log attended transfers in the queue_log
03497  */
03498 static struct ast_datastore *setup_transfer_datastore(struct queue_ent *qe, struct member *member, time_t starttime, int callcompletedinsl)
03499 {
03500    struct ast_datastore *ds;
03501    struct queue_transfer_ds *qtds = ast_calloc(1, sizeof(*qtds));
03502 
03503    if (!qtds) {
03504       ast_log(LOG_WARNING, "Memory allocation error!\n");
03505       return NULL;
03506    }
03507 
03508    ast_channel_lock(qe->chan);
03509    if (!(ds = ast_datastore_alloc(&queue_transfer_info, NULL))) {
03510       ast_channel_unlock(qe->chan);
03511       ast_log(LOG_WARNING, "Unable to create transfer datastore. queue_log will not show attended transfer\n");
03512       return NULL;
03513    }
03514 
03515    qtds->qe = qe;
03516    /* This member is refcounted in try_calling, so no need to add it here, too */
03517    qtds->member = member;
03518    qtds->starttime = starttime;
03519    qtds->callcompletedinsl = callcompletedinsl;
03520    ds->data = qtds;
03521    ast_channel_datastore_add(qe->chan, ds);
03522    ast_channel_unlock(qe->chan);
03523    return ds;
03524 }
03525 
03526 struct queue_end_bridge {
03527    struct call_queue *q;
03528    struct ast_channel *chan;
03529 };
03530 
03531 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
03532 {
03533    struct queue_end_bridge *qeb = bconfig->end_bridge_callback_data;
03534    ao2_ref(qeb, +1);
03535    qeb->chan = originator;
03536 }
03537 
03538 static void end_bridge_callback(void *data)
03539 {
03540    struct queue_end_bridge *qeb = data;
03541    struct call_queue *q = qeb->q;
03542    struct ast_channel *chan = qeb->chan;
03543 
03544    if (ao2_ref(qeb, -1) == 1) {
03545       ao2_lock(q);
03546       set_queue_variables(q, chan);
03547       ao2_unlock(q);
03548       /* This unrefs the reference we made in try_calling when we allocated qeb */
03549       queue_t_unref(q, "Expire bridge_config reference");
03550    }
03551 }
03552 
03553 /*! \brief A large function which calls members, updates statistics, and bridges the caller and a member
03554  * 
03555  * Here is the process of this function
03556  * 1. Process any options passed to the Queue() application. Options here mean the third argument to Queue()
03557  * 2. Iterate trough the members of the queue, creating a callattempt corresponding to each member. During this
03558  *    iteration, we also check the dialed_interfaces datastore to see if we have already attempted calling this
03559  *    member. If we have, we do not create a callattempt. This is in place to prevent call forwarding loops. Also
03560  *    during each iteration, we call calc_metric to determine which members should be rung when.
03561  * 3. Call ring_one to place a call to the appropriate member(s)
03562  * 4. Call wait_for_answer to wait for an answer. If no one answers, return.
03563  * 5. Take care of any holdtime announcements, member delays, or other options which occur after a call has been answered.
03564  * 6. Start the monitor or mixmonitor if the option is set
03565  * 7. Remove the caller from the queue to allow other callers to advance
03566  * 8. Bridge the call.
03567  * 9. Do any post processing after the call has disconnected.
03568  *
03569  * \param[in] qe the queue_ent structure which corresponds to the caller attempting to reach members
03570  * \param[in] options the options passed as the third parameter to the Queue() application
03571  * \param[in] announceoverride filename to play to user when waiting 
03572  * \param[in] url the url passed as the fourth parameter to the Queue() application
03573  * \param[in,out] tries the number of times we have tried calling queue members
03574  * \param[out] noption set if the call to Queue() has the 'n' option set.
03575  * \param[in] agi the agi passed as the fifth parameter to the Queue() application
03576  * \param[in] macro the macro passed as the sixth parameter to the Queue() application
03577  * \param[in] gosub the gosub passed as the seventh parameter to the Queue() application
03578  * \param[in] ringing 1 if the 'r' option is set, otherwise 0
03579  */
03580 static int try_calling(struct queue_ent *qe, const char *options, char *announceoverride, const char *url, int *tries, int *noption, const char *agi, const char *macro, const char *gosub, int ringing)
03581 {
03582    struct member *cur;
03583    struct callattempt *outgoing = NULL; /* the list of calls we are building */
03584    int to, orig;
03585    char oldexten[AST_MAX_EXTENSION]="";
03586    char oldcontext[AST_MAX_CONTEXT]="";
03587    char queuename[256]="";
03588    char interfacevar[256]="";
03589    struct ast_channel *peer;
03590    struct ast_channel *which;
03591    struct callattempt *lpeer;
03592    struct member *member;
03593    struct ast_app *application;
03594    int res = 0, bridge = 0;
03595    int numbusies = 0;
03596    int x=0;
03597    char *announce = NULL;
03598    char digit = 0;
03599    time_t callstart;
03600    time_t now = time(NULL);
03601    struct ast_bridge_config bridge_config;
03602    char nondataquality = 1;
03603    char *agiexec = NULL;
03604    char *macroexec = NULL;
03605    char *gosubexec = NULL;
03606    int ret = 0;
03607    const char *monitorfilename;
03608    const char *monitor_exec;
03609    const char *monitor_options;
03610    char tmpid[256], tmpid2[256];
03611    char meid[1024], meid2[1024];
03612    char mixmonargs[1512];
03613    struct ast_app *mixmonapp = NULL;
03614    char *p;
03615    char vars[2048];
03616    int forwardsallowed = 1;
03617    int callcompletedinsl;
03618    struct ao2_iterator memi;
03619    struct ast_datastore *datastore, *transfer_ds;
03620    struct queue_end_bridge *queue_end_bridge = NULL;
03621    const int need_weight = use_weight;
03622 
03623    ast_channel_lock(qe->chan);
03624    datastore = ast_channel_datastore_find(qe->chan, &dialed_interface_info, NULL);
03625    ast_channel_unlock(qe->chan);
03626 
03627    memset(&bridge_config, 0, sizeof(bridge_config));
03628    tmpid[0] = 0;
03629    meid[0] = 0;
03630    time(&now);
03631 
03632    /* If we've already exceeded our timeout, then just stop
03633     * This should be extremely rare. queue_exec will take care
03634     * of removing the caller and reporting the timeout as the reason.
03635     */
03636    if (qe->expire && now >= qe->expire) {
03637       res = 0;
03638       goto out;
03639    }
03640       
03641    for (; options && *options; options++)
03642       switch (*options) {
03643       case 't':
03644          ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_REDIRECT);
03645          break;
03646       case 'T':
03647          ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_REDIRECT);
03648          break;
03649       case 'w':
03650          ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_AUTOMON);
03651          break;
03652       case 'W':
03653          ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMON);
03654          break;
03655       case 'c':
03656          ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_NO_H_EXTEN);
03657          break;
03658       case 'd':
03659          nondataquality = 0;
03660          break;
03661       case 'h':
03662          ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_DISCONNECT);
03663          break;
03664       case 'H':
03665          ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_DISCONNECT);
03666          break;
03667       case 'k':
03668          ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_PARKCALL);
03669          break;
03670       case 'K':
03671          ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_PARKCALL);
03672          break;
03673       case 'n':
03674          if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY || qe->parent->strategy == QUEUE_STRATEGY_LINEAR)
03675             (*tries)++;
03676          else
03677             *tries = qe->parent->membercount;
03678          *noption = 1;
03679          break;
03680       case 'i':
03681          forwardsallowed = 0;
03682          break;
03683       case 'x':
03684          ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_AUTOMIXMON);
03685          break;
03686       case 'X':
03687          ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMIXMON);
03688          break;
03689       case 'C':
03690          qe->cancel_answered_elsewhere = 1;
03691          break;
03692 
03693       }
03694 
03695    /* if the calling channel has the ANSWERED_ELSEWHERE flag set, make sure this is inherited. 
03696       (this is mainly to support chan_local)
03697    */
03698    if (ast_test_flag(qe->chan, AST_FLAG_ANSWERED_ELSEWHERE)) {
03699       qe->cancel_answered_elsewhere = 1;
03700    }
03701 
03702    /* Hold the lock while we setup the outgoing calls */
03703    if (need_weight)
03704       ao2_lock(queues);
03705    ao2_lock(qe->parent);
03706    ast_debug(1, "%s is trying to call a queue member.\n",
03707                      qe->chan->name);
03708    ast_copy_string(queuename, qe->parent->name, sizeof(queuename));
03709    if (!ast_strlen_zero(qe->announce))
03710       announce = qe->announce;
03711    if (!ast_strlen_zero(announceoverride))
03712       announce = announceoverride;
03713 
03714    memi = ao2_iterator_init(qe->parent->members, 0);
03715    while ((cur = ao2_iterator_next(&memi))) {
03716       struct callattempt *tmp = ast_calloc(1, sizeof(*tmp));
03717       struct ast_dialed_interface *di;
03718       AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
03719       if (!tmp) {
03720          ao2_ref(cur, -1);
03721          ao2_unlock(qe->parent);
03722          ao2_iterator_destroy(&memi);
03723          if (need_weight)
03724             ao2_unlock(queues);
03725          goto out;
03726       }
03727       if (!datastore) {
03728          if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
03729             ao2_ref(cur, -1);
03730             ao2_unlock(qe->parent);
03731             ao2_iterator_destroy(&memi);
03732             if (need_weight)
03733                ao2_unlock(queues);
03734             free(tmp);
03735             goto out;
03736          }
03737          datastore->inheritance = DATASTORE_INHERIT_FOREVER;
03738          if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
03739             ao2_ref(cur, -1);
03740             ao2_unlock(&qe->parent);
03741             ao2_iterator_destroy(&memi);
03742             if (need_weight)
03743                ao2_unlock(queues);
03744             free(tmp);
03745             goto out;
03746          }
03747          datastore->data = dialed_interfaces;
03748          AST_LIST_HEAD_INIT(dialed_interfaces);
03749 
03750          ast_channel_lock(qe->chan);
03751          ast_channel_datastore_add(qe->chan, datastore);
03752          ast_channel_unlock(qe->chan);
03753       } else
03754          dialed_interfaces = datastore->data;
03755 
03756       AST_LIST_LOCK(dialed_interfaces);
03757       AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
03758          if (!strcasecmp(cur->interface, di->interface)) {
03759             ast_debug(1, "Skipping dialing interface '%s' since it has already been dialed\n", 
03760                di->interface);
03761             break;
03762          }
03763       }
03764       AST_LIST_UNLOCK(dialed_interfaces);
03765       
03766       if (di) {
03767          free(tmp);
03768          continue;
03769       }
03770 
03771       /* It is always ok to dial a Local interface.  We only keep track of
03772        * which "real" interfaces have been dialed.  The Local channel will
03773        * inherit this list so that if it ends up dialing a real interface,
03774        * it won't call one that has already been called. */
03775       if (strncasecmp(cur->interface, "Local/", 6)) {
03776          if (!(di = ast_calloc(1, sizeof(*di) + strlen(cur->interface)))) {
03777             ao2_ref(cur, -1);
03778             ao2_unlock(qe->parent);
03779             ao2_iterator_destroy(&memi);
03780             if (need_weight)
03781                ao2_unlock(queues);
03782             free(tmp);
03783             goto out;
03784          }
03785          strcpy(di->interface, cur->interface);
03786 
03787          AST_LIST_LOCK(dialed_interfaces);
03788          AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
03789          AST_LIST_UNLOCK(dialed_interfaces);
03790       }
03791 
03792       tmp->stillgoing = -1;
03793       tmp->member = cur;
03794       tmp->oldstatus = cur->status;
03795       tmp->lastcall = cur->lastcall;
03796       tmp->lastqueue = cur->lastqueue;
03797       ast_copy_string(tmp->interface, cur->interface, sizeof(tmp->interface));
03798       /* Special case: If we ring everyone, go ahead and ring them, otherwise
03799          just calculate their metric for the appropriate strategy */
03800       if (!calc_metric(qe->parent, cur, x++, qe, tmp)) {
03801          /* Put them in the list of outgoing thingies...  We're ready now.
03802             XXX If we're forcibly removed, these outgoing calls won't get
03803             hung up XXX */
03804          tmp->q_next = outgoing;
03805          outgoing = tmp;      
03806          /* If this line is up, don't try anybody else */
03807          if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
03808             break;
03809       } else {
03810          ao2_ref(cur, -1);
03811          ast_free(tmp);
03812       }
03813    }
03814    ao2_iterator_destroy(&memi);
03815 
03816    if (qe->parent->timeoutpriority == TIMEOUT_PRIORITY_APP) {
03817       /* Application arguments have higher timeout priority (behaviour for <=1.6) */
03818       if (qe->expire && (!qe->parent->timeout || (qe->expire - now) <= qe->parent->timeout))
03819          to = (qe->expire - now) * 1000;
03820       else
03821          to = (qe->parent->timeout) ? qe->parent->timeout * 1000 : -1;
03822    } else {
03823       /* Config timeout is higher priority thatn application timeout */
03824       if (qe->expire && qe->expire<=now) {
03825          to = 0;
03826       } else if (qe->parent->timeout) {
03827          to = qe->parent->timeout * 1000;
03828       } else {
03829          to = -1;
03830       }
03831    }
03832    orig = to;
03833    ++qe->pending;
03834    ao2_unlock(qe->parent);
03835    ring_one(qe, outgoing, &numbusies);
03836    if (need_weight)
03837       ao2_unlock(queues);
03838    lpeer = wait_for_answer(qe, outgoing, &to, &digit, numbusies, ast_test_flag(&(bridge_config.features_caller), AST_FEATURE_DISCONNECT), forwardsallowed);
03839    /* The ast_channel_datastore_remove() function could fail here if the
03840     * datastore was moved to another channel during a masquerade. If this is
03841     * the case, don't free the datastore here because later, when the channel
03842     * to which the datastore was moved hangs up, it will attempt to free this
03843     * datastore again, causing a crash
03844     */
03845    ast_channel_lock(qe->chan);
03846    if (datastore && !ast_channel_datastore_remove(qe->chan, datastore)) {
03847       ast_datastore_free(datastore);
03848    }
03849    ast_channel_unlock(qe->chan);
03850    ao2_lock(qe->parent);
03851    if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY) {
03852       store_next_rr(qe, outgoing);
03853    }
03854    if (qe->parent->strategy == QUEUE_STRATEGY_LINEAR) {
03855       store_next_lin(qe, outgoing);
03856    }
03857    ao2_unlock(qe->parent);
03858    peer = lpeer ? lpeer->chan : NULL;
03859    if (!peer) {
03860       qe->pending = 0;
03861       if (to) {
03862          /* Must gotten hung up */
03863          res = -1;
03864       } else {
03865          /* User exited by pressing a digit */
03866          res = digit;
03867       }
03868       if (res == -1)
03869          ast_debug(1, "%s: Nobody answered.\n", qe->chan->name);
03870       if (ast_cdr_isset_unanswered()) {
03871          /* channel contains the name of one of the outgoing channels
03872             in its CDR; zero out this CDR to avoid a dual-posting */
03873          struct callattempt *o;
03874          for (o = outgoing; o; o = o->q_next) {
03875             if (!o->chan) {
03876                continue;
03877             }
03878             if (strcmp(o->chan->cdr->dstchannel, qe->chan->cdr->dstchannel) == 0) {
03879                ast_set_flag(o->chan->cdr, AST_CDR_FLAG_POST_DISABLED);
03880                break;
03881             }
03882          }
03883       }
03884    } else { /* peer is valid */
03885       /* Ah ha!  Someone answered within the desired timeframe.  Of course after this
03886          we will always return with -1 so that it is hung up properly after the
03887          conversation.  */
03888       if (!strcmp(qe->chan->tech->type, "DAHDI"))
03889          ast_channel_setoption(qe->chan, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);
03890       if (!strcmp(peer->tech->type, "DAHDI"))
03891          ast_channel_setoption(peer, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);
03892       /* Update parameters for the queue */
03893       time(&now);
03894       recalc_holdtime(qe, (now - qe->start));
03895       ao2_lock(qe->parent);
03896       callcompletedinsl = ((now - qe->start) <= qe->parent->servicelevel);
03897       ao2_unlock(qe->parent);
03898       member = lpeer->member;
03899       /* Increment the refcount for this member, since we're going to be using it for awhile in here. */
03900       ao2_ref(member, 1);
03901       hangupcalls(outgoing, peer, qe->cancel_answered_elsewhere);
03902       outgoing = NULL;
03903       if (announce || qe->parent->reportholdtime || qe->parent->memberdelay) {
03904          int res2;
03905 
03906          res2 = ast_autoservice_start(qe->chan);
03907          if (!res2) {
03908             if (qe->parent->memberdelay) {
03909                ast_log(LOG_NOTICE, "Delaying member connect for %d seconds\n", qe->parent->memberdelay);
03910                res2 |= ast_safe_sleep(peer, qe->parent->memberdelay * 1000);
03911             }
03912             if (!res2 && announce) {
03913                play_file(peer, announce);
03914             }
03915             if (!res2 && qe->parent->reportholdtime) {
03916                if (!play_file(peer, qe->parent->sound_reporthold)) {
03917                   int holdtime, holdtimesecs;
03918 
03919                   time(&now);
03920                   holdtime = abs((now - qe->start) / 60);
03921                   holdtimesecs = abs((now - qe->start) % 60);
03922                   if (holdtime > 0) {
03923                      ast_say_number(peer, holdtime, AST_DIGIT_ANY, peer->language, NULL);
03924                      play_file(peer, qe->parent->sound_minutes);
03925                   }
03926                   if (holdtimesecs > 1) {
03927                      ast_say_number(peer, holdtimesecs, AST_DIGIT_ANY, peer->language, NULL);
03928                      play_file(peer, qe->parent->sound_seconds);
03929                   }
03930                }
03931             }
03932          }
03933          res2 |= ast_autoservice_stop(qe->chan);
03934          if (ast_check_hangup(peer)) {
03935             /* Agent must have hung up */
03936             ast_log(LOG_WARNING, "Agent on %s hungup on the customer.\n", peer->name);
03937             ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "AGENTDUMP", "%s", "");
03938             if (qe->parent->eventwhencalled)
03939                manager_event(EVENT_FLAG_AGENT, "AgentDump",
03940                      "Queue: %s\r\n"
03941                      "Uniqueid: %s\r\n"
03942                      "Channel: %s\r\n"
03943                      "Member: %s\r\n"
03944                      "MemberName: %s\r\n"
03945                      "%s",
03946                      queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
03947                      qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
03948             ast_hangup(peer);
03949             ao2_ref(member, -1);
03950             goto out;
03951          } else if (res2) {
03952             /* Caller must have hung up just before being connected*/
03953             ast_log(LOG_NOTICE, "Caller was about to talk to agent on %s but the caller hungup.\n", peer->name);
03954             ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "ABANDON", "%d|%d|%ld", qe->pos, qe->opos, (long) time(NULL) - qe->start);
03955             record_abandoned(qe);
03956             ast_hangup(peer);
03957             ao2_ref(member, -1);
03958             return -1;
03959          }
03960       }
03961       /* Stop music on hold */
03962       if (ringing)
03963          ast_indicate(qe->chan,-1);
03964       else
03965          ast_moh_stop(qe->chan);
03966       /* If appropriate, log that we have a destination channel */
03967       if (qe->chan->cdr)
03968          ast_cdr_setdestchan(qe->chan->cdr, peer->name);
03969       /* Make sure channels are compatible */
03970       res = ast_channel_make_compatible(qe->chan, peer);
03971       if (res < 0) {
03972          ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "SYSCOMPAT", "%s", "");
03973          ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
03974          record_abandoned(qe);
03975          ast_cdr_failed(qe->chan->cdr);
03976          ast_hangup(peer);
03977          ao2_ref(member, -1);
03978          return -1;
03979       }
03980 
03981       /* Play announcement to the caller telling it's his turn if defined */
03982       if (!ast_strlen_zero(qe->parent->sound_callerannounce)) {
03983          if (play_file(qe->chan, qe->parent->sound_callerannounce))
03984             ast_log(LOG_WARNING, "Announcement file '%s' is unavailable, continuing anyway...\n", qe->parent->sound_callerannounce);
03985       }
03986 
03987       ao2_lock(qe->parent);
03988       /* if setinterfacevar is defined, make member variables available to the channel */
03989       /* use  pbx_builtin_setvar to set a load of variables with one call */
03990       if (qe->parent->setinterfacevar) {
03991          snprintf(interfacevar, sizeof(interfacevar), "MEMBERINTERFACE=%s,MEMBERNAME=%s,MEMBERCALLS=%d,MEMBERLASTCALL=%ld,MEMBERPENALTY=%d,MEMBERDYNAMIC=%d,MEMBERREALTIME=%d",
03992             member->interface, member->membername, member->calls, (long)member->lastcall, member->penalty, member->dynamic, member->realtime);
03993          pbx_builtin_setvar_multiple(qe->chan, interfacevar);
03994          pbx_builtin_setvar_multiple(peer, interfacevar);
03995       }
03996       
03997       /* if setqueueentryvar is defined, make queue entry (i.e. the caller) variables available to the channel */
03998       /* use  pbx_builtin_setvar to set a load of variables with one call */
03999       if (qe->parent->setqueueentryvar) {
04000          snprintf(interfacevar, sizeof(interfacevar), "QEHOLDTIME=%ld,QEORIGINALPOS=%d",
04001             (long) time(NULL) - qe->start, qe->opos);
04002          pbx_builtin_setvar_multiple(qe->chan, interfacevar);
04003          pbx_builtin_setvar_multiple(peer, interfacevar);
04004       }
04005    
04006       /* try to set queue variables if configured to do so*/
04007       set_queue_variables(qe->parent, qe->chan);
04008       set_queue_variables(qe->parent, peer);
04009       ao2_unlock(qe->parent);
04010       
04011       ast_channel_lock(qe->chan);
04012       if ((monitorfilename = pbx_builtin_getvar_helper(qe->chan, "MONITOR_FILENAME"))) {
04013             monitorfilename = ast_strdupa(monitorfilename);
04014       }
04015       ast_channel_unlock(qe->chan);
04016       /* Begin Monitoring */
04017       if (qe->parent->monfmt && *qe->parent->monfmt) {
04018          if (!qe->parent->montype) {
04019             const char *monexec, *monargs;
04020             ast_debug(1, "Starting Monitor as requested.\n");
04021             ast_channel_lock(qe->chan);
04022             if ((monexec = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC")) || (monargs = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC_ARGS"))) {
04023                which = qe->chan;
04024                monexec = monexec ? ast_strdupa(monexec) : NULL;
04025             }
04026             else
04027                which = peer;
04028             ast_channel_unlock(qe->chan);
04029             if (ast_monitor_start) {
04030                if (monitorfilename) {
04031                   ast_monitor_start(which, qe->parent->monfmt, monitorfilename, 1, X_REC_IN | X_REC_OUT);
04032                } else if (qe->chan->cdr && ast_monitor_start) {
04033                   ast_monitor_start(which, qe->parent->monfmt, qe->chan->cdr->uniqueid, 1, X_REC_IN | X_REC_OUT);
04034                } else if (ast_monitor_start) {
04035                   /* Last ditch effort -- no CDR, make up something */
04036                   snprintf(tmpid, sizeof(tmpid), "chan-%lx", ast_random());
04037                   ast_monitor_start(which, qe->parent->monfmt, tmpid, 1, X_REC_IN | X_REC_OUT);
04038                }
04039             }
04040             if (!ast_strlen_zero(monexec) && ast_monitor_setjoinfiles) {
04041                ast_monitor_setjoinfiles(which, 1);
04042             }
04043          } else {
04044             mixmonapp = pbx_findapp("MixMonitor");
04045             
04046             if (mixmonapp) {
04047                ast_debug(1, "Starting MixMonitor as requested.\n");
04048                if (!monitorfilename) {
04049                   if (qe->chan->cdr)
04050                      ast_copy_string(tmpid, qe->chan->cdr->uniqueid, sizeof(tmpid));
04051                   else
04052                      snprintf(tmpid, sizeof(tmpid), "chan-%lx", ast_random());
04053                } else {
04054                   const char *m = monitorfilename;
04055                   for (p = tmpid2; p < tmpid2 + sizeof(tmpid2) - 1; p++, m++) {
04056                      switch (*m) {
04057                      case '^':
04058                         if (*(m + 1) == '{')
04059                            *p = '$';
04060                         break;
04061                      case ',':
04062                         *p++ = '\\';
04063                         /* Fall through */
04064                      default:
04065                         *p = *m;
04066                      }
04067                      if (*m == '\0')
04068                         break;
04069                   }
04070                   if (p == tmpid2 + sizeof(tmpid2))
04071                      tmpid2[sizeof(tmpid2) - 1] = '\0';
04072 
04073                   pbx_substitute_variables_helper(qe->chan, tmpid2, tmpid, sizeof(tmpid) - 1);
04074                }
04075 
04076                ast_channel_lock(qe->chan);
04077                if ((monitor_exec = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC"))) {
04078                      monitor_exec = ast_strdupa(monitor_exec);
04079                }
04080                if ((monitor_options = pbx_builtin_getvar_helper(qe->chan, "MONITOR_OPTIONS"))) {
04081                      monitor_options = ast_strdupa(monitor_options);
04082                } else {
04083                   monitor_options = "";
04084                }
04085                ast_channel_unlock(qe->chan);
04086 
04087                if (monitor_exec) {
04088                   const char *m = monitor_exec;
04089                   for (p = meid2; p < meid2 + sizeof(meid2) - 1; p++, m++) {
04090                      switch (*m) {
04091                      case '^':
04092                         if (*(m + 1) == '{')
04093                            *p = '$';
04094                         break;
04095                      case ',':
04096                         *p++ = '\\';
04097                         /* Fall through */
04098                      default:
04099                         *p = *m;
04100                      }
04101                      if (*m == '\0')
04102                         break;
04103                   }
04104                   if (p == meid2 + sizeof(meid2))
04105                      meid2[sizeof(meid2) - 1] = '\0';
04106 
04107                   pbx_substitute_variables_helper(qe->chan, meid2, meid, sizeof(meid) - 1);
04108                }
04109    
04110                snprintf(tmpid2, sizeof(tmpid2), "%s.%s", tmpid, qe->parent->monfmt);
04111 
04112                if (!ast_strlen_zero(monitor_exec))
04113                   snprintf(mixmonargs, sizeof(mixmonargs), "%s,b%s,%s", tmpid2, monitor_options, monitor_exec);
04114                else
04115                   snprintf(mixmonargs, sizeof(mixmonargs), "%s,b%s", tmpid2, monitor_options);
04116                
04117                ast_debug(1, "Arguments being passed to MixMonitor: %s\n", mixmonargs);
04118                /* We purposely lock the CDR so that pbx_exec does not update the application data */
04119                if (qe->chan->cdr)
04120                   ast_set_flag(qe->chan->cdr, AST_CDR_FLAG_LOCKED);
04121                ret = pbx_exec(qe->chan, mixmonapp, mixmonargs);
04122                if (qe->chan->cdr)
04123                   ast_clear_flag(qe->chan->cdr, AST_CDR_FLAG_LOCKED);
04124 
04125             } else {
04126                ast_log(LOG_WARNING, "Asked to run MixMonitor on this call, but cannot find the MixMonitor app!\n");
04127             }
04128          }
04129       }
04130       /* Drop out of the queue at this point, to prepare for next caller */
04131       leave_queue(qe);        
04132       if (!ast_strlen_zero(url) && ast_channel_supports_html(peer)) {
04133          ast_debug(1, "app_queue: sendurl=%s.\n", url);
04134          ast_channel_sendurl(peer, url);
04135       }
04136       
04137       /* run a macro for this connection if defined. The macro simply returns, no action is taken on the result */
04138       /* use macro from dialplan if passed as a option, otherwise use the default queue macro */
04139       if (!ast_strlen_zero(macro)) {
04140             macroexec = ast_strdupa(macro);
04141       } else {
04142          if (qe->parent->membermacro)
04143             macroexec = ast_strdupa(qe->parent->membermacro);
04144       }
04145 
04146       if (!ast_strlen_zero(macroexec)) {
04147          ast_debug(1, "app_queue: macro=%s.\n", macroexec);
04148          
04149          res = ast_autoservice_start(qe->chan);
04150          if (res) {
04151             ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
04152             res = -1;
04153          }
04154          
04155          application = pbx_findapp("Macro");
04156 
04157          if (application) {
04158             res = pbx_exec(peer, application, macroexec);
04159             ast_debug(1, "Macro exited with status %d\n", res);
04160             res = 0;
04161          } else {
04162             ast_log(LOG_ERROR, "Could not find application Macro\n");
04163             res = -1;
04164          }
04165 
04166          if (ast_autoservice_stop(qe->chan) < 0) {
04167             ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
04168             res = -1;
04169          }
04170       }
04171 
04172       /* run a gosub for this connection if defined. The gosub simply returns, no action is taken on the result */
04173       /* use gosub from dialplan if passed as a option, otherwise use the default queue gosub */
04174       if (!ast_strlen_zero(gosub)) {
04175             gosubexec = ast_strdupa(gosub);
04176       } else {
04177          if (qe->parent->membergosub)
04178             gosubexec = ast_strdupa(qe->parent->membergosub);
04179       }
04180 
04181       if (!ast_strlen_zero(gosubexec)) {
04182          ast_debug(1, "app_queue: gosub=%s.\n", gosubexec);
04183          
04184          res = ast_autoservice_start(qe->chan);
04185          if (res) {
04186             ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
04187             res = -1;
04188          }
04189          
04190          application = pbx_findapp("Gosub");
04191          
04192          if (application) {
04193             char *gosub_args, *gosub_argstart;
04194 
04195             /* Set where we came from */
04196             ast_copy_string(peer->context, "app_queue_gosub_virtual_context", sizeof(peer->context));
04197             ast_copy_string(peer->exten, "s", sizeof(peer->exten));
04198             peer->priority = 0;
04199 
04200             gosub_argstart = strchr(gosubexec, ',');
04201             if (gosub_argstart) {
04202                *gosub_argstart = 0;
04203                if (asprintf(&gosub_args, "%s,s,1(%s)", gosubexec, gosub_argstart + 1) < 0) {
04204                   ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
04205                   gosub_args = NULL;
04206                }
04207                *gosub_argstart = ',';
04208             } else {
04209                if (asprintf(&gosub_args, "%s,s,1", gosubexec) < 0) {
04210                   ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
04211                   gosub_args = NULL;
04212                }
04213             }
04214             if (gosub_args) {
04215                res = pbx_exec(peer, application, gosub_args);
04216                if (!res) {
04217                   struct ast_pbx_args args;
04218                   memset(&args, 0, sizeof(args));
04219                   args.no_hangup_chan = 1;
04220                   ast_pbx_run_args(peer, &args);
04221                }
04222                ast_free(gosub_args);
04223                ast_debug(1, "Gosub exited with status %d\n", res);
04224             } else {
04225                ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
04226             }
04227          } else {
04228             ast_log(LOG_ERROR, "Could not find application Gosub\n");
04229             res = -1;
04230          }
04231       
04232          if (ast_autoservice_stop(qe->chan) < 0) {
04233             ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
04234             res = -1;
04235          }
04236       }
04237 
04238       if (!ast_strlen_zero(agi)) {
04239          ast_debug(1, "app_queue: agi=%s.\n", agi);
04240          application = pbx_findapp("agi");
04241          if (application) {
04242             agiexec = ast_strdupa(agi);
04243             ret = pbx_exec(qe->chan, application, agiexec);
04244          } else
04245             ast_log(LOG_WARNING, "Asked to execute an AGI on this channel, but could not find application (agi)!\n");
04246       }
04247       qe->handled++;
04248       ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "CONNECT", "%ld|%s|%ld", (long) time(NULL) - qe->start, peer->uniqueid,
04249                                        (long)(orig - to > 0 ? (orig - to) / 1000 : 0));
04250       if (update_cdr && qe->chan->cdr) 
04251          ast_copy_string(qe->chan->cdr->dstchannel, member->membername, sizeof(qe->chan->cdr->dstchannel));
04252       if (qe->parent->eventwhencalled)
04253          manager_event(EVENT_FLAG_AGENT, "AgentConnect",
04254                "Queue: %s\r\n"
04255                "Uniqueid: %s\r\n"
04256                "Channel: %s\r\n"
04257                "Member: %s\r\n"
04258                "MemberName: %s\r\n"
04259                "Holdtime: %ld\r\n"
04260                "BridgedChannel: %s\r\n"
04261                "Ringtime: %ld\r\n"
04262                "%s",
04263                queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
04264                (long) time(NULL) - qe->start, peer->uniqueid, (long)(orig - to > 0 ? (orig - to) / 1000 : 0),
04265                qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
04266       ast_copy_string(oldcontext, qe->chan->context, sizeof(oldcontext));
04267       ast_copy_string(oldexten, qe->chan->exten, sizeof(oldexten));
04268    
04269       if ((queue_end_bridge = ao2_alloc(sizeof(*queue_end_bridge), NULL))) {
04270          queue_end_bridge->q = qe->parent;
04271          queue_end_bridge->chan = qe->chan;
04272          bridge_config.end_bridge_callback = end_bridge_callback;
04273          bridge_config.end_bridge_callback_data = queue_end_bridge;
04274          bridge_config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
04275          /* Since queue_end_bridge can survive beyond the life of this call to Queue, we need
04276           * to make sure to increase the refcount of this queue so it cannot be freed until we
04277           * are done with it. We remove this reference in end_bridge_callback.
04278           */
04279          queue_t_ref(qe->parent, "For bridge_config reference");
04280       }
04281 
04282       time(&callstart);
04283       transfer_ds = setup_transfer_datastore(qe, member, callstart, callcompletedinsl);
04284       bridge = ast_bridge_call(qe->chan,peer, &bridge_config);
04285 
04286       /* If the queue member did an attended transfer, then the TRANSFER already was logged in the queue_log
04287        * when the masquerade occurred. These other "ending" queue_log messages are unnecessary, except for
04288        * the AgentComplete manager event
04289        */
04290       ast_channel_lock(qe->chan);
04291       if (!attended_transfer_occurred(qe->chan)) {
04292          struct ast_datastore *tds;
04293 
04294          /* detect a blind transfer */
04295          if (!(qe->chan->_softhangup | peer->_softhangup) && (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten))) {
04296             ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "TRANSFER", "%s|%s|%ld|%ld|%d",
04297                qe->chan->exten, qe->chan->context, (long) (callstart - qe->start),
04298                (long) (time(NULL) - callstart), qe->opos);
04299             send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), TRANSFER);
04300          } else if (ast_check_hangup(qe->chan)) {
04301             ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "COMPLETECALLER", "%ld|%ld|%d",
04302                (long) (callstart - qe->start), (long) (time(NULL) - callstart), qe->opos);
04303             send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), CALLER);
04304          } else {
04305             ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "COMPLETEAGENT", "%ld|%ld|%d",
04306                (long) (callstart - qe->start), (long) (time(NULL) - callstart), qe->opos);
04307             send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), AGENT);
04308          }
04309          if ((tds = ast_channel_datastore_find(qe->chan, &queue_transfer_info, NULL))) {  
04310             ast_channel_datastore_remove(qe->chan, tds);
04311          }
04312          update_queue(qe->parent, member, callcompletedinsl, (time(NULL) - callstart));
04313       } else {
04314          /* We already logged the TRANSFER on the queue_log, but we still need to send the AgentComplete event */
04315          send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), TRANSFER);
04316       }
04317 
04318       if (transfer_ds) {
04319          ast_datastore_free(transfer_ds);
04320       }
04321       ast_channel_unlock(qe->chan);
04322       ast_hangup(peer);
04323       res = bridge ? bridge : 1;
04324       ao2_ref(member, -1);
04325    }
04326 out:
04327    hangupcalls(outgoing, NULL, qe->cancel_answered_elsewhere);
04328 
04329    return res;
04330 }
04331 
04332 static int wait_a_bit(struct queue_ent *qe)
04333 {
04334    /* Don't need to hold the lock while we setup the outgoing calls */
04335    int retrywait = qe->parent->retry * 1000;
04336 
04337    int res = ast_waitfordigit(qe->chan, retrywait);
04338    if (res > 0 && !valid_exit(qe, res))
04339       res = 0;
04340 
04341    return res;
04342 }
04343 
04344 static struct member *interface_exists(struct call_queue *q, const char *interface)
04345 {
04346    struct member *mem;
04347    struct ao2_iterator mem_iter;
04348 
04349    if (!q)
04350       return NULL;
04351 
04352    mem_iter = ao2_iterator_init(q->members, 0);
04353    while ((mem = ao2_iterator_next(&mem_iter))) {
04354       if (!strcasecmp(interface, mem->interface)) {
04355          ao2_iterator_destroy(&mem_iter);
04356          return mem;
04357       }
04358       ao2_ref(mem, -1);
04359    }
04360    ao2_iterator_destroy(&mem_iter);
04361 
04362    return NULL;
04363 }
04364 
04365 
04366 /*! \brief Dump all members in a specific queue to the database
04367  *
04368  * <pm_family>/<queuename> = <interface>;<penalty>;<paused>;<state_interface>[|...]
04369  */
04370 static void dump_queue_members(struct call_queue *pm_queue)
04371 {
04372    struct member *cur_member;
04373    char value[PM_MAX_LEN];
04374    int value_len = 0;
04375    int res;
04376    struct ao2_iterator mem_iter;
04377 
04378    memset(value, 0, sizeof(value));
04379 
04380    if (!pm_queue)
04381       return;
04382 
04383    mem_iter = ao2_iterator_init(pm_queue->members, 0);
04384    while ((cur_member = ao2_iterator_next(&mem_iter))) {
04385       if (!cur_member->dynamic) {
04386          ao2_ref(cur_member, -1);
04387          continue;
04388       }
04389 
04390       res = snprintf(value + value_len, sizeof(value) - value_len, "%s%s;%d;%d;%s;%s",
04391          value_len ? "|" : "", cur_member->interface, cur_member->penalty, cur_member->paused, cur_member->membername, cur_member->state_interface);
04392 
04393       ao2_ref(cur_member, -1);
04394 
04395       if (res != strlen(value + value_len)) {
04396          ast_log(LOG_WARNING, "Could not create persistent member string, out of space\n");
04397          break;
04398       }
04399       value_len += res;
04400    }
04401    ao2_iterator_destroy(&mem_iter);
04402    
04403    if (value_len && !cur_member) {
04404       if (ast_db_put(pm_family, pm_queue->name, value))
04405          ast_log(LOG_WARNING, "failed to create persistent dynamic entry!\n");
04406    } else
04407       /* Delete the entry if the queue is empty or there is an error */
04408       ast_db_del(pm_family, pm_queue->name);
04409 }
04410 
04411 /*! \brief Remove member from queue 
04412  * \retval RES_NOT_DYNAMIC when they aren't a RT member
04413  * \retval RES_NOSUCHQUEUE queue does not exist
04414  * \retval RES_OKAY removed member from queue
04415  * \retval RES_EXISTS queue exists but no members
04416 */
04417 static int remove_from_queue(const char *queuename, const char *interface)
04418 {
04419    struct call_queue *q, tmpq;
04420    struct member *mem, tmpmem;
04421    int res = RES_NOSUCHQUEUE;
04422 
04423    ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
04424    ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
04425    if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Temporary reference for interface removal"))) {
04426       ao2_lock(queues);
04427       ao2_lock(q);
04428       if ((mem = ao2_find(q->members, &tmpmem, OBJ_POINTER))) {
04429          /* XXX future changes should beware of this assumption!! */
04430          if (!mem->dynamic) {
04431             ao2_ref(mem, -1);
04432             ao2_unlock(q);
04433             queue_t_unref(q, "Interface wasn't dynamic, expiring temporary reference");
04434             ao2_unlock(queues);
04435             return RES_NOT_DYNAMIC;
04436          }
04437          q->membercount--;
04438          manager_event(EVENT_FLAG_AGENT, "QueueMemberRemoved",
04439             "Queue: %s\r\n"
04440             "Location: %s\r\n"
04441             "MemberName: %s\r\n",
04442             q->name, mem->interface, mem->membername);
04443          ao2_unlink(q->members, mem);
04444          ao2_ref(mem, -1);
04445 
04446          if (queue_persistent_members)
04447             dump_queue_members(q);
04448          
04449          res = RES_OKAY;
04450       } else {
04451          res = RES_EXISTS;
04452       }
04453       ao2_unlock(q);
04454       ao2_unlock(queues);
04455       queue_t_unref(q, "Expiring temporary reference");
04456    }
04457 
04458    return res;
04459 }
04460 
04461 /*! \brief Add member to queue 
04462  * \retval RES_NOT_DYNAMIC when they aren't a RT member
04463  * \retval RES_NOSUCHQUEUE queue does not exist
04464  * \retval RES_OKAY added member from queue
04465  * \retval RES_EXISTS queue exists but no members
04466  * \retval RES_OUT_OF_MEMORY queue exists but not enough memory to create member
04467 */
04468 static int add_to_queue(const char *queuename, const char *interface, const char *membername, int penalty, int paused, int dump, const char *state_interface)
04469 {
04470    struct call_queue *q;
04471    struct member *new_member, *old_member;
04472    int res = RES_NOSUCHQUEUE;
04473 
04474    /*! \note Ensure the appropriate realtime queue is loaded.  Note that this
04475     * short-circuits if the queue is already in memory. */
04476    if (!(q = load_realtime_queue(queuename)))
04477       return res;
04478 
04479    ao2_lock(queues);
04480 
04481    ao2_lock(q);
04482    if ((old_member = interface_exists(q, interface)) == NULL) {
04483       if ((new_member = create_queue_member(interface, membername, penalty, paused, state_interface))) {
04484          new_member->dynamic = 1;
04485          ao2_link(q->members, new_member);
04486          q->membercount++;
04487          manager_event(EVENT_FLAG_AGENT, "QueueMemberAdded",
04488             "Queue: %s\r\n"
04489             "Location: %s\r\n"
04490             "MemberName: %s\r\n"
04491             "Membership: %s\r\n"
04492             "Penalty: %d\r\n"
04493             "CallsTaken: %d\r\n"
04494             "LastCall: %d\r\n"
04495             "Status: %d\r\n"
04496             "Paused: %d\r\n",
04497             q->name, new_member->interface, new_member->membername,
04498             "dynamic",
04499             new_member->penalty, new_member->calls, (int) new_member->lastcall,
04500             new_member->status, new_member->paused);
04501          
04502          ao2_ref(new_member, -1);
04503          new_member = NULL;
04504 
04505          if (dump)
04506             dump_queue_members(q);
04507          
04508          res = RES_OKAY;
04509       } else {
04510          res = RES_OUTOFMEMORY;
04511       }
04512    } else {
04513       ao2_ref(old_member, -1);
04514       res = RES_EXISTS;
04515    }
04516    ao2_unlock(q);
04517    ao2_unlock(queues);
04518 
04519    return res;
04520 }
04521 
04522 static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused)
04523 {
04524    int found = 0;
04525    struct call_queue *q;
04526    struct member *mem;
04527    struct ao2_iterator queue_iter;
04528    int failed;
04529 
04530    /* Special event for when all queues are paused - individual events still generated */
04531    /* XXX In all other cases, we use the membername, but since this affects all queues, we cannot */
04532    if (ast_strlen_zero(queuename))
04533       ast_queue_log("NONE", "NONE", interface, (paused ? "PAUSEALL" : "UNPAUSEALL"), "%s", "");
04534 
04535    queue_iter = ao2_iterator_init(queues, 0);
04536    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate over queues"))) {
04537       ao2_lock(q);
04538       if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename)) {
04539          if ((mem = interface_exists(q, interface))) {
04540             if (mem->paused == paused) {
04541                ast_debug(1, "%spausing already-%spaused queue member %s:%s\n", (paused ? "" : "un"), (paused ? "" : "un"), q->name, interface);
04542             }
04543 
04544             failed = 0;
04545             if (mem->realtime) {
04546                failed = update_realtime_member_field(mem, q->name, "paused", paused ? "1" : "0");
04547             }
04548          
04549             if (failed) {
04550                ast_log(LOG_WARNING, "Failed %spausing realtime queue member %s:%s\n", (paused ? "" : "un"), q->name, interface);
04551                ao2_ref(mem, -1);
04552                ao2_unlock(q);
04553                queue_t_unref(q, "Done with iterator");
04554                continue;
04555             }  
04556             found++;
04557             mem->paused = paused;
04558 
04559             if (queue_persistent_members)
04560                dump_queue_members(q);
04561 
04562             ast_queue_log(q->name, "NONE", mem->membername, (paused ? "PAUSE" : "UNPAUSE"), "%s", S_OR(reason, ""));
04563             
04564             if (!ast_strlen_zero(reason)) {
04565                manager_event(EVENT_FLAG_AGENT, "QueueMemberPaused",
04566                   "Queue: %s\r\n"
04567                   "Location: %s\r\n"
04568                   "MemberName: %s\r\n"
04569                   "Paused: %d\r\n"
04570                   "Reason: %s\r\n",
04571                      q->name, mem->interface, mem->membername, paused, reason);
04572             } else {
04573                manager_event(EVENT_FLAG_AGENT, "QueueMemberPaused",
04574                   "Queue: %s\r\n"
04575                   "Location: %s\r\n"
04576                   "MemberName: %s\r\n"
04577                   "Paused: %d\r\n",
04578                      q->name, mem->interface, mem->membername, paused);
04579             }
04580             ao2_ref(mem, -1);
04581          }
04582       }
04583       
04584       if (!ast_strlen_zero(queuename) && !strcasecmp(queuename, q->name)) {
04585          ao2_unlock(q);
04586          queue_t_unref(q, "Done with iterator");
04587          break;
04588       }
04589       
04590       ao2_unlock(q);
04591       queue_t_unref(q, "Done with iterator");
04592    }
04593    ao2_iterator_destroy(&queue_iter);
04594 
04595    return found ? RESULT_SUCCESS : RESULT_FAILURE;
04596 }
04597 
04598 /* \brief Sets members penalty, if queuename=NULL we set member penalty in all the queues. */
04599 static int set_member_penalty(char *queuename, char *interface, int penalty)
04600 {
04601    int foundinterface = 0, foundqueue = 0;
04602    struct call_queue *q;
04603    struct member *mem;
04604    struct ao2_iterator queue_iter;
04605 
04606    if (penalty < 0) {
04607       ast_log(LOG_ERROR, "Invalid penalty (%d)\n", penalty);
04608       return RESULT_FAILURE;
04609    }
04610 
04611    queue_iter = ao2_iterator_init(queues, 0);
04612    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
04613       ao2_lock(q);
04614       if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename)) {
04615          foundqueue++;
04616          if ((mem = interface_exists(q, interface))) {
04617             foundinterface++;
04618             mem->penalty = penalty;
04619             
04620             ast_queue_log(q->name, "NONE", interface, "PENALTY", "%d", penalty);
04621             manager_event(EVENT_FLAG_AGENT, "QueueMemberPenalty",
04622                "Queue: %s\r\n"
04623                "Location: %s\r\n"
04624                "Penalty: %d\r\n",
04625                q->name, mem->interface, penalty);
04626             ao2_ref(mem, -1);
04627          }
04628       }
04629       ao2_unlock(q);
04630       queue_t_unref(q, "Done with iterator");
04631    }
04632    ao2_iterator_destroy(&queue_iter);
04633 
04634    if (foundinterface) {
04635       return RESULT_SUCCESS;
04636    } else if (!foundqueue) {
04637       ast_log (LOG_ERROR, "Invalid queuename\n"); 
04638    } else {
04639       ast_log (LOG_ERROR, "Invalid interface\n");
04640    }  
04641 
04642    return RESULT_FAILURE;
04643 }
04644 
04645 /* \brief Gets members penalty. 
04646  * \return Return the members penalty or RESULT_FAILURE on error. 
04647 */
04648 static int get_member_penalty(char *queuename, char *interface)
04649 {
04650    int foundqueue = 0, penalty;
04651    struct call_queue *q, tmpq;
04652    struct member *mem;
04653    
04654    ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
04655    if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Search for queue"))) {
04656       foundqueue = 1;
04657       ao2_lock(q);
04658       if ((mem = interface_exists(q, interface))) {
04659          penalty = mem->penalty;
04660          ao2_ref(mem, -1);
04661          ao2_unlock(q);
04662          queue_t_unref(q, "Search complete");
04663          return penalty;
04664       }
04665       ao2_unlock(q);
04666       queue_t_unref(q, "Search complete");
04667    }
04668 
04669    /* some useful debuging */
04670    if (foundqueue) 
04671       ast_log (LOG_ERROR, "Invalid queuename\n");
04672    else 
04673       ast_log (LOG_ERROR, "Invalid interface\n");
04674 
04675    return RESULT_FAILURE;
04676 }
04677 
04678 /*! \brief Reload dynamic queue members persisted into the astdb */
04679 static void reload_queue_members(void)
04680 {
04681    char *cur_ptr;
04682    const char *queue_name;
04683    char *member;
04684    char *interface;
04685    char *membername = NULL;
04686    char *state_interface;
04687    char *penalty_tok;
04688    int penalty = 0;
04689    char *paused_tok;
04690    int paused = 0;
04691    struct ast_db_entry *db_tree;
04692    struct ast_db_entry *entry;
04693    struct call_queue *cur_queue;
04694    char queue_data[PM_MAX_LEN];
04695 
04696    ao2_lock(queues);
04697 
04698    /* Each key in 'pm_family' is the name of a queue */
04699    db_tree = ast_db_gettree(pm_family, NULL);
04700    for (entry = db_tree; entry; entry = entry->next) {
04701 
04702       queue_name = entry->key + strlen(pm_family) + 2;
04703 
04704       {
04705          struct call_queue tmpq;
04706          ast_copy_string(tmpq.name, queue_name, sizeof(tmpq.name));
04707          cur_queue = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Reload queue members");
04708       }  
04709 
04710       if (!cur_queue)
04711          cur_queue = load_realtime_queue(queue_name);
04712 
04713       if (!cur_queue) {
04714          /* If the queue no longer exists, remove it from the
04715           * database */
04716          ast_log(LOG_WARNING, "Error loading persistent queue: '%s': it does not exist\n", queue_name);
04717          ast_db_del(pm_family, queue_name);
04718          continue;
04719       } 
04720 
04721       if (ast_db_get(pm_family, queue_name, queue_data, PM_MAX_LEN)) {
04722          queue_t_unref(cur_queue, "Expire reload reference");
04723          continue;
04724       }
04725 
04726       cur_ptr = queue_data;
04727       while ((member = strsep(&cur_ptr, ",|"))) {
04728          if (ast_strlen_zero(member))
04729             continue;
04730 
04731          interface = strsep(&member, ";");
04732          penalty_tok = strsep(&member, ";");
04733          paused_tok = strsep(&member, ";");
04734          membername = strsep(&member, ";");
04735          state_interface = strsep(&member, ";");
04736 
04737          if (!penalty_tok) {
04738             ast_log(LOG_WARNING, "Error parsing persistent member string for '%s' (penalty)\n", queue_name);
04739             break;
04740          }
04741          penalty = strtol(penalty_tok, NULL, 10);
04742          if (errno == ERANGE) {
04743             ast_log(LOG_WARNING, "Error converting penalty: %s: Out of range.\n", penalty_tok);
04744             break;
04745          }
04746          
04747          if (!paused_tok) {
04748             ast_log(LOG_WARNING, "Error parsing persistent member string for '%s' (paused)\n", queue_name);
04749             break;
04750          }
04751          paused = strtol(paused_tok, NULL, 10);
04752          if ((errno == ERANGE) || paused < 0 || paused > 1) {
04753             ast_log(LOG_WARNING, "Error converting paused: %s: Expected 0 or 1.\n", paused_tok);
04754             break;
04755          }
04756 
04757          ast_debug(1, "Reload Members: Queue: %s  Member: %s  Name: %s  Penalty: %d  Paused: %d\n", queue_name, interface, membername, penalty, paused);
04758          
04759          if (add_to_queue(queue_name, interface, membername, penalty, paused, 0, state_interface) == RES_OUTOFMEMORY) {
04760             ast_log(LOG_ERROR, "Out of Memory when reloading persistent queue member\n");
04761             break;
04762          }
04763       }
04764       queue_t_unref(cur_queue, "Expire reload reference");
04765    }
04766 
04767    ao2_unlock(queues);
04768    if (db_tree) {
04769       ast_log(LOG_NOTICE, "Queue members successfully reloaded from database.\n");
04770       ast_db_freetree(db_tree);
04771    }
04772 }
04773 
04774 /*! \brief PauseQueueMember application */
04775 static int pqm_exec(struct ast_channel *chan, void *data)
04776 {
04777    char *parse;
04778    AST_DECLARE_APP_ARGS(args,
04779       AST_APP_ARG(queuename);
04780       AST_APP_ARG(interface);
04781       AST_APP_ARG(options);
04782       AST_APP_ARG(reason);
04783    );
04784 
04785    if (ast_strlen_zero(data)) {
04786       ast_log(LOG_WARNING, "PauseQueueMember requires an argument ([queuename],interface[,options][,reason])\n");
04787       return -1;
04788    }
04789 
04790    parse = ast_strdupa(data);
04791 
04792    AST_STANDARD_APP_ARGS(args, parse);
04793 
04794    if (ast_strlen_zero(args.interface)) {
04795       ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename],interface[,options[,reason]])\n");
04796       return -1;
04797    }
04798 
04799    if (set_member_paused(args.queuename, args.interface, args.reason, 1)) {
04800       ast_log(LOG_WARNING, "Attempt to pause interface %s, not found\n", args.interface);
04801       pbx_builtin_setvar_helper(chan, "PQMSTATUS", "NOTFOUND");
04802       return 0;
04803    }
04804 
04805    pbx_builtin_setvar_helper(chan, "PQMSTATUS", "PAUSED");
04806 
04807    return 0;
04808 }
04809 
04810 /*! \brief UnPauseQueueMember application */
04811 static int upqm_exec(struct ast_channel *chan, void *data)
04812 {
04813    char *parse;
04814    AST_DECLARE_APP_ARGS(args,
04815       AST_APP_ARG(queuename);
04816       AST_APP_ARG(interface);
04817       AST_APP_ARG(options);
04818       AST_APP_ARG(reason);
04819    );
04820 
04821    if (ast_strlen_zero(data)) {
04822       ast_log(LOG_WARNING, "UnpauseQueueMember requires an argument ([queuename],interface[,options[,reason]])\n");
04823       return -1;
04824    }
04825 
04826    parse = ast_strdupa(data);
04827 
04828    AST_STANDARD_APP_ARGS(args, parse);
04829 
04830    if (ast_strlen_zero(args.interface)) {
04831       ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename],interface[,options[,reason]])\n");
04832       return -1;
04833    }
04834 
04835    if (set_member_paused(args.queuename, args.interface, args.reason, 0)) {
04836       ast_log(LOG_WARNING, "Attempt to unpause interface %s, not found\n", args.interface);
04837       pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "NOTFOUND");
04838       return 0;
04839    }
04840 
04841    pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "UNPAUSED");
04842 
04843    return 0;
04844 }
04845 
04846 /*! \brief RemoveQueueMember application */
04847 static int rqm_exec(struct ast_channel *chan, void *data)
04848 {
04849    int res=-1;
04850    char *parse, *temppos = NULL;
04851    AST_DECLARE_APP_ARGS(args,
04852       AST_APP_ARG(queuename);
04853       AST_APP_ARG(interface);
04854       AST_APP_ARG(options);
04855    );
04856 
04857 
04858    if (ast_strlen_zero(data)) {
04859       ast_log(LOG_WARNING, "RemoveQueueMember requires an argument (queuename[,interface[,options]])\n");
04860       return -1;
04861    }
04862 
04863    parse = ast_strdupa(data);
04864 
04865    AST_STANDARD_APP_ARGS(args, parse);
04866 
04867    if (ast_strlen_zero(args.interface)) {
04868       args.interface = ast_strdupa(chan->name);
04869       temppos = strrchr(args.interface, '-');
04870       if (temppos)
04871          *temppos = '\0';
04872    }
04873 
04874    ast_debug(1, "queue: %s, member: %s\n", args.queuename, args.interface);
04875 
04876    switch (remove_from_queue(args.queuename, args.interface)) {
04877    case RES_OKAY:
04878       ast_queue_log(args.queuename, chan->uniqueid, args.interface, "REMOVEMEMBER", "%s", "");
04879       ast_log(LOG_NOTICE, "Removed interface '%s' from queue '%s'\n", args.interface, args.queuename);
04880       pbx_builtin_setvar_helper(chan, "RQMSTATUS", "REMOVED");
04881       res = 0;
04882       break;
04883    case RES_EXISTS:
04884       ast_debug(1, "Unable to remove interface '%s' from queue '%s': Not there\n", args.interface, args.queuename);
04885       pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTINQUEUE");
04886       res = 0;
04887       break;
04888    case RES_NOSUCHQUEUE:
04889       ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': No such queue\n", args.queuename);
04890       pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOSUCHQUEUE");
04891       res = 0;
04892       break;
04893    case RES_NOT_DYNAMIC:
04894       ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': '%s' is not a dynamic member\n", args.queuename, args.interface);
04895       pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTDYNAMIC");
04896       res = 0;
04897       break;
04898    }
04899 
04900    return res;
04901 }
04902 
04903 /*! \brief AddQueueMember application */
04904 static int aqm_exec(struct ast_channel *chan, void *data)
04905 {
04906    int res=-1;
04907    char *parse, *temppos = NULL;
04908    AST_DECLARE_APP_ARGS(args,
04909       AST_APP_ARG(queuename);
04910       AST_APP_ARG(interface);
04911       AST_APP_ARG(penalty);
04912       AST_APP_ARG(options);
04913       AST_APP_ARG(membername);
04914       AST_APP_ARG(state_interface);
04915    );
04916    int penalty = 0;
04917 
04918    if (ast_strlen_zero(data)) {
04919       ast_log(LOG_WARNING, "AddQueueMember requires an argument (queuename[,interface[,penalty[,options[,membername[,stateinterface]]]]])\n");
04920       return -1;
04921    }
04922 
04923    parse = ast_strdupa(data);
04924 
04925    AST_STANDARD_APP_ARGS(args, parse);
04926 
04927    if (ast_strlen_zero(args.interface)) {
04928       args.interface = ast_strdupa(chan->name);
04929       temppos = strrchr(args.interface, '-');
04930       if (temppos)
04931          *temppos = '\0';
04932    }
04933 
04934    if (!ast_strlen_zero(args.penalty)) {
04935       if ((sscanf(args.penalty, "%30d", &penalty) != 1) || penalty < 0) {
04936          ast_log(LOG_WARNING, "Penalty '%s' is invalid, must be an integer >= 0\n", args.penalty);
04937          penalty = 0;
04938       }
04939    }
04940 
04941    switch (add_to_queue(args.queuename, args.interface, args.membername, penalty, 0, queue_persistent_members, args.state_interface)) {
04942    case RES_OKAY:
04943       ast_queue_log(args.queuename, chan->uniqueid, args.interface, "ADDMEMBER", "%s", "");
04944       ast_log(LOG_NOTICE, "Added interface '%s' to queue '%s'\n", args.interface, args.queuename);
04945       pbx_builtin_setvar_helper(chan, "AQMSTATUS", "ADDED");
04946       res = 0;
04947       break;
04948    case RES_EXISTS:
04949       ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': Already there\n", args.interface, args.queuename);
04950       pbx_builtin_setvar_helper(chan, "AQMSTATUS", "MEMBERALREADY");
04951       res = 0;
04952       break;
04953    case RES_NOSUCHQUEUE:
04954       ast_log(LOG_WARNING, "Unable to add interface to queue '%s': No such queue\n", args.queuename);
04955       pbx_builtin_setvar_helper(chan, "AQMSTATUS", "NOSUCHQUEUE");
04956       res = 0;
04957       break;
04958    case RES_OUTOFMEMORY:
04959       ast_log(LOG_ERROR, "Out of memory adding member %s to queue %s\n", args.interface, args.queuename);
04960       break;
04961    }
04962 
04963    return res;
04964 }
04965 
04966 /*! \brief QueueLog application */
04967 static int ql_exec(struct ast_channel *chan, void *data)
04968 {
04969    char *parse;
04970 
04971    AST_DECLARE_APP_ARGS(args,
04972       AST_APP_ARG(queuename);
04973       AST_APP_ARG(uniqueid);
04974       AST_APP_ARG(membername);
04975       AST_APP_ARG(event);
04976       AST_APP_ARG(params);
04977    );
04978 
04979    if (ast_strlen_zero(data)) {
04980       ast_log(LOG_WARNING, "QueueLog requires arguments (queuename,uniqueid,membername,event[,additionalinfo]\n");
04981       return -1;
04982    }
04983 
04984    parse = ast_strdupa(data);
04985 
04986    AST_STANDARD_APP_ARGS(args, parse);
04987 
04988    if (ast_strlen_zero(args.queuename) || ast_strlen_zero(args.uniqueid)
04989        || ast_strlen_zero(args.membername) || ast_strlen_zero(args.event)) {
04990       ast_log(LOG_WARNING, "QueueLog requires arguments (queuename,uniqueid,membername,event[,additionalinfo])\n");
04991       return -1;
04992    }
04993 
04994    ast_queue_log(args.queuename, args.uniqueid, args.membername, args.event, 
04995       "%s", args.params ? args.params : "");
04996 
04997    return 0;
04998 }
04999 
05000 /*! \brief Copy rule from global list into specified queue */
05001 static void copy_rules(struct queue_ent *qe, const char *rulename)
05002 {
05003    struct penalty_rule *pr_iter;
05004    struct rule_list *rl_iter;
05005    const char *tmp = ast_strlen_zero(rulename) ? qe->parent->defaultrule : rulename;
05006    AST_LIST_LOCK(&rule_lists);
05007    AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
05008       if (!strcasecmp(rl_iter->name, tmp))
05009          break;
05010    }
05011    if (rl_iter) {
05012       AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
05013          struct penalty_rule *new_pr = ast_calloc(1, sizeof(*new_pr));
05014          if (!new_pr) {
05015             ast_log(LOG_ERROR, "Memory allocation error when copying penalty rules! Aborting!\n");
05016             AST_LIST_UNLOCK(&rule_lists);
05017             break;
05018          }
05019          new_pr->time = pr_iter->time;
05020          new_pr->max_value = pr_iter->max_value;
05021          new_pr->min_value = pr_iter->min_value;
05022          new_pr->max_relative = pr_iter->max_relative;
05023          new_pr->min_relative = pr_iter->min_relative;
05024          AST_LIST_INSERT_TAIL(&qe->qe_rules, new_pr, list);
05025       }
05026    }
05027    AST_LIST_UNLOCK(&rule_lists);
05028 }
05029 
05030 /*!\brief The starting point for all queue calls
05031  *
05032  * The process involved here is to 
05033  * 1. Parse the options specified in the call to Queue()
05034  * 2. Join the queue
05035  * 3. Wait in a loop until it is our turn to try calling a queue member
05036  * 4. Attempt to call a queue member
05037  * 5. If 4. did not result in a bridged call, then check for between
05038  *    call options such as periodic announcements etc.
05039  * 6. Try 4 again unless some condition (such as an expiration time) causes us to 
05040  *    exit the queue.
05041  */
05042 static int queue_exec(struct ast_channel *chan, void *data)
05043 {
05044    int res=-1;
05045    int ringing=0;
05046    const char *user_priority;
05047    const char *max_penalty_str;
05048    const char *min_penalty_str;
05049    int prio;
05050    int qcontinue = 0;
05051    int max_penalty, min_penalty;
05052    enum queue_result reason = QUEUE_UNKNOWN;
05053    /* whether to exit Queue application after the timeout hits */
05054    int tries = 0;
05055    int noption = 0;
05056    char *parse;
05057    int makeannouncement = 0;
05058    AST_DECLARE_APP_ARGS(args,
05059       AST_APP_ARG(queuename);
05060       AST_APP_ARG(options);
05061       AST_APP_ARG(url);
05062       AST_APP_ARG(announceoverride);
05063       AST_APP_ARG(queuetimeoutstr);
05064       AST_APP_ARG(agi);
05065       AST_APP_ARG(macro);
05066       AST_APP_ARG(gosub);
05067       AST_APP_ARG(rule);
05068    );
05069    /* Our queue entry */
05070    struct queue_ent qe = { 0 };
05071    
05072    if (ast_strlen_zero(data)) {
05073       ast_log(LOG_WARNING, "Queue requires an argument: queuename[,options[,URL[,announceoverride[,timeout[,agi[,macro[,gosub[,rule]]]]]]]]\n");
05074       return -1;
05075    }
05076    
05077    parse = ast_strdupa(data);
05078    AST_STANDARD_APP_ARGS(args, parse);
05079 
05080    /* Setup our queue entry */
05081    qe.start = time(NULL);
05082 
05083    /* set the expire time based on the supplied timeout; */
05084    if (!ast_strlen_zero(args.queuetimeoutstr))
05085       qe.expire = qe.start + atoi(args.queuetimeoutstr);
05086    else
05087       qe.expire = 0;
05088 
05089    /* Get the priority from the variable ${QUEUE_PRIO} */
05090    ast_channel_lock(chan);
05091    user_priority = pbx_builtin_getvar_helper(chan, "QUEUE_PRIO");
05092    if (user_priority) {
05093       if (sscanf(user_priority, "%30d", &prio) == 1) {
05094          ast_debug(1, "%s: Got priority %d from ${QUEUE_PRIO}.\n", chan->name, prio);
05095       } else {
05096          ast_log(LOG_WARNING, "${QUEUE_PRIO}: Invalid value (%s), channel %s.\n",
05097             user_priority, chan->name);
05098          prio = 0;
05099       }
05100    } else {
05101       ast_debug(3, "NO QUEUE_PRIO variable found. Using default.\n");
05102       prio = 0;
05103    }
05104 
05105    /* Get the maximum penalty from the variable ${QUEUE_MAX_PENALTY} */
05106 
05107    if ((max_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MAX_PENALTY"))) {
05108       if (sscanf(max_penalty_str, "%30d", &max_penalty) == 1) {
05109          ast_debug(1, "%s: Got max penalty %d from ${QUEUE_MAX_PENALTY}.\n", chan->name, max_penalty);
05110       } else {
05111          ast_log(LOG_WARNING, "${QUEUE_MAX_PENALTY}: Invalid value (%s), channel %s.\n",
05112             max_penalty_str, chan->name);
05113          max_penalty = 0;
05114       }
05115    } else {
05116       max_penalty = 0;
05117    }
05118 
05119    if ((min_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MIN_PENALTY"))) {
05120       if (sscanf(min_penalty_str, "%30d", &min_penalty) == 1) {
05121          ast_debug(1, "%s: Got min penalty %d from ${QUEUE_MIN_PENALTY}.\n", chan->name, min_penalty);
05122       } else {
05123          ast_log(LOG_WARNING, "${QUEUE_MIN_PENALTY}: Invalid value (%s), channel %s.\n",
05124             min_penalty_str, chan->name);
05125          min_penalty = 0;
05126       }
05127    } else {
05128       min_penalty = 0;
05129    }
05130    ast_channel_unlock(chan);
05131 
05132    if (args.options && (strchr(args.options, 'r')))
05133       ringing = 1;
05134 
05135    if (args.options && (strchr(args.options, 'c')))
05136       qcontinue = 1;
05137 
05138    ast_debug(1, "queue: %s, options: %s, url: %s, announce: %s, expires: %ld, priority: %d\n",
05139       args.queuename, args.options, args.url, args.announceoverride, (long)qe.expire, prio);
05140 
05141    qe.chan = chan;
05142    qe.prio = prio;
05143    qe.max_penalty = max_penalty;
05144    qe.min_penalty = min_penalty;
05145    qe.last_pos_said = 0;
05146    qe.last_pos = 0;
05147    qe.last_periodic_announce_time = time(NULL);
05148    qe.last_periodic_announce_sound = 0;
05149    qe.valid_digits = 0;
05150    if (join_queue(args.queuename, &qe, &reason)) {
05151       ast_log(LOG_WARNING, "Unable to join queue '%s'\n", args.queuename);
05152       set_queue_result(chan, reason);
05153       return 0;
05154    }
05155    ast_queue_log(args.queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", S_OR(args.url, ""),
05156       S_OR(chan->cid.cid_num, ""));
05157    copy_rules(&qe, args.rule);
05158    qe.pr = AST_LIST_FIRST(&qe.qe_rules);
05159 check_turns:
05160    if (ringing) {
05161       ast_indicate(chan, AST_CONTROL_RINGING);
05162    } else {
05163       ast_moh_start(chan, qe.moh, NULL);
05164    }
05165 
05166    /* This is the wait loop for callers 2 through maxlen */
05167    res = wait_our_turn(&qe, ringing, &reason);
05168    if (res) {
05169       goto stop;
05170    }
05171 
05172    makeannouncement = 0;
05173 
05174    for (;;) {
05175       /* This is the wait loop for the head caller*/
05176       /* To exit, they may get their call answered; */
05177       /* they may dial a digit from the queue context; */
05178       /* or, they may timeout. */
05179 
05180       /* Leave if we have exceeded our queuetimeout */
05181       if (qe.expire && (time(NULL) >= qe.expire)) {
05182          record_abandoned(&qe);
05183          reason = QUEUE_TIMEOUT;
05184          res = 0;
05185          ast_queue_log(args.queuename, chan->uniqueid,"NONE", "EXITWITHTIMEOUT", "%d|%d|%ld", 
05186             qe.pos, qe.opos, (long) time(NULL) - qe.start);
05187          break;
05188       }
05189 
05190       if (makeannouncement) {
05191          /* Make a position announcement, if enabled */
05192          if (qe.parent->announcefrequency)
05193             if ((res = say_position(&qe,ringing)))
05194                goto stop;
05195       }
05196       makeannouncement = 1;
05197 
05198       /* Make a periodic announcement, if enabled */
05199       if (qe.parent->periodicannouncefrequency)
05200          if ((res = say_periodic_announcement(&qe,ringing)))
05201             goto stop;
05202    
05203       /* Leave if we have exceeded our queuetimeout */
05204       if (qe.expire && (time(NULL) >= qe.expire)) {
05205          record_abandoned(&qe);
05206          reason = QUEUE_TIMEOUT;
05207          res = 0;
05208          ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
05209          break;
05210       }
05211 
05212       /* see if we need to move to the next penalty level for this queue */
05213       while (qe.pr && ((time(NULL) - qe.start) > qe.pr->time)) {
05214          update_qe_rule(&qe);
05215       }
05216 
05217       /* Try calling all queue members for 'timeout' seconds */
05218       res = try_calling(&qe, args.options, args.announceoverride, args.url, &tries, &noption, args.agi, args.macro, args.gosub, ringing);
05219       if (res) {
05220          goto stop;
05221       }
05222 
05223       if (qe.parent->leavewhenempty) {
05224          int status = 0;
05225          if ((status = get_member_status(qe.parent, qe.max_penalty, qe.min_penalty, qe.parent->leavewhenempty))) {
05226             record_abandoned(&qe);
05227             reason = QUEUE_LEAVEEMPTY;
05228             ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITEMPTY", "%d|%d|%ld", qe.pos, qe.opos, (long)(time(NULL) - qe.start));
05229             res = 0;
05230             break;
05231          }
05232       }
05233 
05234       /* exit after 'timeout' cycle if 'n' option enabled */
05235       if (noption && tries >= qe.parent->membercount) {
05236          ast_verb(3, "Exiting on time-out cycle\n");
05237          ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
05238          record_abandoned(&qe);
05239          reason = QUEUE_TIMEOUT;
05240          res = 0;
05241          break;
05242       }
05243 
05244       
05245       /* Leave if we have exceeded our queuetimeout */
05246       if (qe.expire && (time(NULL) >= qe.expire)) {
05247          record_abandoned(&qe);
05248          reason = QUEUE_TIMEOUT;
05249          res = 0;
05250          ast_queue_log(qe.parent->name, qe.chan->uniqueid,"NONE", "EXITWITHTIMEOUT", "%d|%d|%ld", qe.pos, qe.opos, (long) time(NULL) - qe.start);
05251          break;
05252       }
05253 
05254       /* If using dynamic realtime members, we should regenerate the member list for this queue */
05255       update_realtime_members(qe.parent);
05256       /* OK, we didn't get anybody; wait for 'retry' seconds; may get a digit to exit with */
05257       res = wait_a_bit(&qe);
05258       if (res)
05259          goto stop;
05260 
05261       /* Since this is a priority queue and
05262        * it is not sure that we are still at the head
05263        * of the queue, go and check for our turn again.
05264        */
05265       if (!is_our_turn(&qe)) {
05266          ast_debug(1, "Darn priorities, going back in queue (%s)!\n", qe.chan->name);
05267          goto check_turns;
05268       }
05269    }
05270 
05271 stop:
05272    if (res) {
05273       if (res < 0) {
05274          if (!qe.handled) {
05275             record_abandoned(&qe);
05276             ast_queue_log(args.queuename, chan->uniqueid, "NONE", "ABANDON",
05277                "%d|%d|%ld", qe.pos, qe.opos,
05278                (long) time(NULL) - qe.start);
05279             res = -1;
05280          } else if (qcontinue) {
05281             reason = QUEUE_CONTINUE;
05282             res = 0;
05283          }
05284       } else if (qe.valid_digits) {
05285          ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHKEY",
05286             "%s|%d", qe.digits, qe.pos);
05287       }
05288    }
05289 
05290    /* Don't allow return code > 0 */
05291    if (res >= 0) {
05292       res = 0; 
05293       if (ringing) {
05294          ast_indicate(chan, -1);
05295       } else {
05296          ast_moh_stop(chan);
05297       }        
05298       ast_stopstream(chan);
05299    }
05300 
05301    set_queue_variables(qe.parent, qe.chan);
05302 
05303    leave_queue(&qe);
05304    if (reason != QUEUE_UNKNOWN)
05305       set_queue_result(chan, reason);
05306 
05307    if (qe.parent) {
05308       /* every queue_ent is given a reference to it's parent call_queue when it joins the queue.
05309        * This ref must be taken away right before the queue_ent is destroyed.  In this case
05310        * the queue_ent is about to be returned on the stack */
05311       qe.parent = queue_unref(qe.parent);
05312    }
05313 
05314    return res;
05315 }
05316 
05317 /*!
05318  * \brief create interface var with all queue details.
05319  * \retval 0 on success
05320  * \retval -1 on error
05321 */
05322 static int queue_function_var(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05323 {
05324    int res = -1;
05325    struct call_queue *q, tmpq;
05326    char interfacevar[256] = "";
05327    float sl = 0;
05328 
05329    if (ast_strlen_zero(data)) {
05330       ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
05331       return -1;
05332    }
05333 
05334    ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
05335    if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE() function"))) {
05336       ao2_lock(q);
05337       if (q->setqueuevar) {
05338          sl = 0;
05339          res = 0;
05340 
05341          if (q->callscompleted > 0) {
05342             sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
05343          }
05344 
05345          snprintf(interfacevar, sizeof(interfacevar),
05346             "QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
05347             q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned,  q->servicelevel, sl);
05348 
05349          pbx_builtin_setvar_multiple(chan, interfacevar);
05350       }
05351 
05352       ao2_unlock(q);
05353       queue_t_unref(q, "Done with QUEUE() function");
05354    } else {
05355       ast_log(LOG_WARNING, "queue %s was not found\n", data);
05356    }
05357 
05358    snprintf(buf, len, "%d", res);
05359 
05360    return 0;
05361 }
05362 
05363 /*! 
05364  * \brief Get number either busy / free or total members of a specific queue
05365  * \retval number of members (busy / free / total)
05366  * \retval -1 on error
05367 */
05368 static int queue_function_qac(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05369 {
05370    int count = 0;
05371    struct member *m;
05372    struct ao2_iterator mem_iter;
05373    struct call_queue *q;
05374    char *option;
05375 
05376    if (ast_strlen_zero(data)) {
05377       ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
05378       return -1;
05379    }
05380 
05381    if ((option = strchr(data, ',')))
05382       *option++ = '\0';
05383    else
05384       option = "logged";
05385    if ((q = load_realtime_queue(data))) {
05386       ao2_lock(q);
05387       if (!strcasecmp(option, "logged")) {
05388          mem_iter = ao2_iterator_init(q->members, 0);
05389          while ((m = ao2_iterator_next(&mem_iter))) {
05390             /* Count the agents who are logged in and presently answering calls */
05391             if ((m->status != AST_DEVICE_UNAVAILABLE) && (m->status != AST_DEVICE_INVALID)) {
05392                count++;
05393             }
05394             ao2_ref(m, -1);
05395          }
05396          ao2_iterator_destroy(&mem_iter);
05397       } else if (!strcasecmp(option, "free")) {
05398          mem_iter = ao2_iterator_init(q->members, 0);
05399          while ((m = ao2_iterator_next(&mem_iter))) {
05400             /* Count the agents who are logged in and presently answering calls */
05401             if ((m->status == AST_DEVICE_NOT_INUSE) && (!m->paused)) {
05402                count++;
05403             }
05404             ao2_ref(m, -1);
05405          }
05406          ao2_iterator_destroy(&mem_iter);
05407       } else /* must be "count" */
05408          count = q->membercount;
05409       ao2_unlock(q);
05410       queue_t_unref(q, "Done with temporary reference in QUEUE_MEMBER()");
05411    } else
05412       ast_log(LOG_WARNING, "queue %s was not found\n", data);
05413 
05414    snprintf(buf, len, "%d", count);
05415 
05416    return 0;
05417 }
05418 
05419 /*! 
05420  * \brief Get the total number of members in a specific queue (Deprecated)
05421  * \retval number of members 
05422  * \retval -1 on error 
05423 */
05424 static int queue_function_qac_dep(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05425 {
05426    int count = 0;
05427    struct member *m;
05428    struct call_queue *q;
05429    struct ao2_iterator mem_iter;
05430    static int depflag = 1;
05431 
05432    if (depflag) {
05433       depflag = 0;
05434       ast_log(LOG_NOTICE, "The function QUEUE_MEMBER_COUNT has been deprecated in favor of the QUEUE_MEMBER function and will not be in further releases.\n");
05435    }
05436 
05437    if (ast_strlen_zero(data)) {
05438       ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
05439       return -1;
05440    }
05441    
05442    if ((q = load_realtime_queue(data))) {
05443       ao2_lock(q);
05444       mem_iter = ao2_iterator_init(q->members, 0);
05445       while ((m = ao2_iterator_next(&mem_iter))) {
05446          /* Count the agents who are logged in and presently answering calls */
05447          if ((m->status != AST_DEVICE_UNAVAILABLE) && (m->status != AST_DEVICE_INVALID)) {
05448             count++;
05449          }
05450          ao2_ref(m, -1);
05451       }
05452       ao2_iterator_destroy(&mem_iter);
05453       ao2_unlock(q);
05454       queue_t_unref(q, "Done with temporary reference in QUEUE_MEMBER_COUNT");
05455    } else
05456       ast_log(LOG_WARNING, "queue %s was not found\n", data);
05457 
05458    snprintf(buf, len, "%d", count);
05459 
05460    return 0;
05461 }
05462 
05463 /*! \brief Dialplan function QUEUE_WAITING_COUNT() Get number callers waiting in a specific queue */
05464 static int queue_function_queuewaitingcount(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05465 {
05466    int count = 0;
05467    struct call_queue *q, tmpq;
05468    struct ast_variable *var = NULL;
05469 
05470    buf[0] = '\0';
05471    
05472    if (ast_strlen_zero(data)) {
05473       ast_log(LOG_ERROR, "QUEUE_WAITING_COUNT requires an argument: queuename\n");
05474       return -1;
05475    }
05476 
05477    ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
05478    if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_WAITING_COUNT()"))) {
05479       ao2_lock(q);
05480       count = q->count;
05481       ao2_unlock(q);
05482       queue_t_unref(q, "Done with reference in QUEUE_WAITING_COUNT()");
05483    } else if ((var = ast_load_realtime("queues", "name", data, SENTINEL))) {
05484       /* if the queue is realtime but was not found in memory, this
05485        * means that the queue had been deleted from memory since it was 
05486        * "dead." This means it has a 0 waiting count
05487        */
05488       count = 0;
05489       ast_variables_destroy(var);
05490    } else
05491       ast_log(LOG_WARNING, "queue %s was not found\n", data);
05492 
05493    snprintf(buf, len, "%d", count);
05494 
05495    return 0;
05496 }
05497 
05498 /*! \brief Dialplan function QUEUE_MEMBER_LIST() Get list of members in a specific queue */
05499 static int queue_function_queuememberlist(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05500 {
05501    struct call_queue *q, tmpq;
05502    struct member *m;
05503 
05504    /* Ensure an otherwise empty list doesn't return garbage */
05505    buf[0] = '\0';
05506 
05507    if (ast_strlen_zero(data)) {
05508       ast_log(LOG_ERROR, "QUEUE_MEMBER_LIST requires an argument: queuename\n");
05509       return -1;
05510    }
05511 
05512    ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
05513    if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_MEMBER_LIST()"))) {
05514       int buflen = 0, count = 0;
05515       struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
05516 
05517       ao2_lock(q);
05518       while ((m = ao2_iterator_next(&mem_iter))) {
05519          /* strcat() is always faster than printf() */
05520          if (count++) {
05521             strncat(buf + buflen, ",", len - buflen - 1);
05522             buflen++;
05523          }
05524          strncat(buf + buflen, m->interface, len - buflen - 1);
05525          buflen += strlen(m->interface);
05526          /* Safeguard against overflow (negative length) */
05527          if (buflen >= len - 2) {
05528             ao2_ref(m, -1);
05529             ast_log(LOG_WARNING, "Truncating list\n");
05530             break;
05531          }
05532          ao2_ref(m, -1);
05533       }
05534       ao2_iterator_destroy(&mem_iter);
05535       ao2_unlock(q);
05536       queue_t_unref(q, "Done with QUEUE_MEMBER_LIST()");
05537    } else
05538       ast_log(LOG_WARNING, "queue %s was not found\n", data);
05539 
05540    /* We should already be terminated, but let's make sure. */
05541    buf[len - 1] = '\0';
05542 
05543    return 0;
05544 }
05545 
05546 /*! \brief Dialplan function QUEUE_MEMBER_PENALTY() Gets the members penalty. */
05547 static int queue_function_memberpenalty_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
05548 {
05549    int penalty;
05550    AST_DECLARE_APP_ARGS(args,
05551       AST_APP_ARG(queuename);
05552       AST_APP_ARG(interface);
05553    );
05554    /* Make sure the returned value on error is NULL. */
05555    buf[0] = '\0';
05556 
05557    if (ast_strlen_zero(data)) {
05558       ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05559       return -1;
05560    }
05561 
05562    AST_STANDARD_APP_ARGS(args, data);
05563 
05564    if (args.argc < 2) {
05565       ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05566       return -1;
05567    }
05568 
05569    penalty = get_member_penalty (args.queuename, args.interface);
05570    
05571    if (penalty >= 0) /* remember that buf is already '\0' */
05572       snprintf (buf, len, "%d", penalty);
05573 
05574    return 0;
05575 }
05576 
05577 /*! \brief Dialplan function QUEUE_MEMBER_PENALTY() Sets the members penalty. */
05578 static int queue_function_memberpenalty_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
05579 {
05580    int penalty;
05581    AST_DECLARE_APP_ARGS(args,
05582       AST_APP_ARG(queuename);
05583       AST_APP_ARG(interface);
05584    );
05585 
05586    if (ast_strlen_zero(data)) {
05587       ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05588       return -1;
05589    }
05590 
05591    AST_STANDARD_APP_ARGS(args, data);
05592 
05593    if (args.argc < 2) {
05594       ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
05595       return -1;
05596    }
05597 
05598    penalty = atoi(value);
05599 
05600    if (ast_strlen_zero(args.interface)) {
05601       ast_log (LOG_ERROR, "<interface> parameter can't be null\n");
05602       return -1;
05603    }
05604 
05605    /* if queuename = NULL then penalty will be set for interface in all the queues. */
05606    if (set_member_penalty(args.queuename, args.interface, penalty)) {
05607       ast_log(LOG_ERROR, "Invalid interface, queue or penalty\n");
05608       return -1;
05609    }
05610 
05611    return 0;
05612 }
05613 
05614 static struct ast_custom_function queuevar_function = {
05615    .name = "QUEUE_VARIABLES",
05616    .read = queue_function_var,
05617 };
05618 
05619 static struct ast_custom_function queuemembercount_function = {
05620    .name = "QUEUE_MEMBER",
05621    .read = queue_function_qac,
05622 };
05623 
05624 static struct ast_custom_function queuemembercount_dep = {
05625    .name = "QUEUE_MEMBER_COUNT",
05626    .read = queue_function_qac_dep,
05627 };
05628 
05629 static struct ast_custom_function queuewaitingcount_function = {
05630    .name = "QUEUE_WAITING_COUNT",
05631    .read = queue_function_queuewaitingcount,
05632 };
05633 
05634 static struct ast_custom_function queuememberlist_function = {
05635    .name = "QUEUE_MEMBER_LIST",
05636    .read = queue_function_queuememberlist,
05637 };
05638 
05639 static struct ast_custom_function queuememberpenalty_function = {
05640    .name = "QUEUE_MEMBER_PENALTY",
05641    .read = queue_function_memberpenalty_read,
05642    .write = queue_function_memberpenalty_write,
05643 };
05644 
05645 /*! \brief Reload the rules defined in queuerules.conf
05646  *
05647  * \param reload If 1, then only process queuerules.conf if the file
05648  * has changed since the last time we inspected it.
05649  * \return Always returns AST_MODULE_LOAD_SUCCESS
05650  */
05651 static int reload_queue_rules(int reload)
05652 {
05653    struct ast_config *cfg;
05654    struct rule_list *rl_iter, *new_rl;
05655    struct penalty_rule *pr_iter;
05656    char *rulecat = NULL;
05657    struct ast_variable *rulevar = NULL;
05658    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
05659    
05660    if (!(cfg = ast_config_load("queuerules.conf", config_flags))) {
05661       ast_log(LOG_NOTICE, "No queuerules.conf file found, queues will not follow penalty rules\n");
05662       return AST_MODULE_LOAD_SUCCESS;
05663    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
05664       ast_log(LOG_NOTICE, "queuerules.conf has not changed since it was last loaded. Not taking any action.\n");
05665       return AST_MODULE_LOAD_SUCCESS;
05666    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
05667       ast_log(LOG_ERROR, "Config file queuerules.conf is in an invalid format.  Aborting.\n");
05668       return AST_MODULE_LOAD_SUCCESS;
05669    }
05670 
05671    AST_LIST_LOCK(&rule_lists);
05672    while ((rl_iter = AST_LIST_REMOVE_HEAD(&rule_lists, list))) {
05673       while ((pr_iter = AST_LIST_REMOVE_HEAD(&rl_iter->rules, list)))
05674          ast_free(pr_iter);
05675       ast_free(rl_iter);
05676    }
05677    while ((rulecat = ast_category_browse(cfg, rulecat))) {
05678       if (!(new_rl = ast_calloc(1, sizeof(*new_rl)))) {
05679          AST_LIST_UNLOCK(&rule_lists);
05680          return AST_MODULE_LOAD_FAILURE;
05681       } else {
05682          ast_copy_string(new_rl->name, rulecat, sizeof(new_rl->name));
05683          AST_LIST_INSERT_TAIL(&rule_lists, new_rl, list);
05684          for (rulevar = ast_variable_browse(cfg, rulecat); rulevar; rulevar = rulevar->next)
05685             if(!strcasecmp(rulevar->name, "penaltychange"))
05686                insert_penaltychange(new_rl->name, rulevar->value, rulevar->lineno);
05687             else
05688                ast_log(LOG_WARNING, "Don't know how to handle rule type '%s' on line %d\n", rulevar->name, rulevar->lineno);
05689       }
05690    }
05691    AST_LIST_UNLOCK(&rule_lists);
05692 
05693    ast_config_destroy(cfg);
05694 
05695    return AST_MODULE_LOAD_SUCCESS;
05696 }
05697 
05698 /*! Set the global queue parameters as defined in the "general" section of queues.conf */
05699 static void queue_set_global_params(struct ast_config *cfg)
05700 {
05701    const char *general_val = NULL;
05702    queue_persistent_members = 0;
05703    if ((general_val = ast_variable_retrieve(cfg, "general", "persistentmembers")))
05704       queue_persistent_members = ast_true(general_val);
05705    autofill_default = 0;
05706    if ((general_val = ast_variable_retrieve(cfg, "general", "autofill")))
05707       autofill_default = ast_true(general_val);
05708    montype_default = 0;
05709    if ((general_val = ast_variable_retrieve(cfg, "general", "monitor-type"))) {
05710       if (!strcasecmp(general_val, "mixmonitor"))
05711          montype_default = 1;
05712    }
05713    update_cdr = 0;
05714    if ((general_val = ast_variable_retrieve(cfg, "general", "updatecdr")))
05715       update_cdr = ast_true(general_val);
05716    shared_lastcall = 0;
05717    if ((general_val = ast_variable_retrieve(cfg, "general", "shared_lastcall")))
05718       shared_lastcall = ast_true(general_val);
05719 }
05720 
05721 /*! \brief reload information pertaining to a single member
05722  *
05723  * This function is called when a member = line is encountered in
05724  * queues.conf.
05725  *
05726  * \param memberdata The part after member = in the config file
05727  * \param q The queue to which this member belongs
05728  */
05729 static void reload_single_member(const char *memberdata, struct call_queue *q)
05730 {
05731    char *membername, *interface, *state_interface, *tmp;
05732    char *parse;
05733    struct member *cur, *newm;
05734    struct member tmpmem;
05735    int penalty;
05736    AST_DECLARE_APP_ARGS(args,
05737       AST_APP_ARG(interface);
05738       AST_APP_ARG(penalty);
05739       AST_APP_ARG(membername);
05740       AST_APP_ARG(state_interface);
05741    );
05742 
05743    if (ast_strlen_zero(memberdata)) {
05744       ast_log(LOG_WARNING, "Empty queue member definition. Moving on!\n");
05745       return;
05746    }
05747 
05748    /* Add a new member */
05749    parse = ast_strdupa(memberdata);
05750             
05751    AST_STANDARD_APP_ARGS(args, parse);
05752 
05753    interface = args.interface;
05754    if (!ast_strlen_zero(args.penalty)) {
05755       tmp = args.penalty;
05756       ast_strip(tmp);
05757       penalty = atoi(tmp);
05758       if (penalty < 0) {
05759          penalty = 0;
05760       }
05761    } else {
05762       penalty = 0;
05763    }
05764 
05765    if (!ast_strlen_zero(args.membername)) {
05766       membername = args.membername;
05767       ast_strip(membername);
05768    } else {
05769       membername = interface;
05770    }
05771 
05772    if (!ast_strlen_zero(args.state_interface)) {
05773       state_interface = args.state_interface;
05774       ast_strip(state_interface);
05775    } else {
05776       state_interface = interface;
05777    }
05778 
05779    /* Find the old position in the list */
05780    ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
05781    cur = ao2_find(q->members, &tmpmem, OBJ_POINTER | OBJ_UNLINK);
05782    if ((newm = create_queue_member(interface, membername, penalty, cur ? cur->paused : 0, state_interface))) {
05783       ao2_link(q->members, newm);
05784       ao2_ref(newm, -1);
05785    }
05786    newm = NULL;
05787 
05788    if (cur) {
05789       ao2_ref(cur, -1);
05790    } else {
05791       q->membercount++;
05792    }
05793 }
05794 
05795 static int mark_member_dead(void *obj, void *arg, int flags)
05796 {
05797    struct member *member = obj;
05798    if (!member->dynamic) {
05799       member->delme = 1;
05800    }
05801    return 0;
05802 }
05803 
05804 static int kill_dead_members(void *obj, void *arg, int flags)
05805 {
05806    struct member *member = obj;
05807    struct call_queue *q = arg;
05808 
05809    if (!member->delme) {
05810       if (member->dynamic) {
05811          /* dynamic members were not counted toward the member count
05812           * when reloading members from queues.conf, so we do that here
05813           */
05814          q->membercount++;
05815       }
05816       member->status = ast_device_state(member->state_interface);
05817       return 0;
05818    } else {
05819       q->membercount--;
05820       return CMP_MATCH;
05821    }
05822 }
05823 
05824 /*! \brief Reload information pertaining to a particular queue
05825  *
05826  * Once we have isolated a queue within reload_queues, we call this. This will either
05827  * reload information for the queue or if we're just reloading member information, we'll just
05828  * reload that without touching other settings within the queue 
05829  *
05830  * \param cfg The configuration which we are reading
05831  * \param mask Tells us what information we need to reload
05832  * \param queuename The name of the queue we are reloading information from
05833  * \retval void
05834  */
05835 static void reload_single_queue(struct ast_config *cfg, struct ast_flags *mask, const char *queuename)
05836 {
05837    int new;
05838    struct call_queue *q = NULL;
05839    /*We're defining a queue*/
05840    struct call_queue tmpq;
05841    const char *tmpvar;
05842    const int queue_reload = ast_test_flag(mask, QUEUE_RELOAD_PARAMETERS);
05843    const int member_reload = ast_test_flag(mask, QUEUE_RELOAD_MEMBER);
05844    int prev_weight = 0;
05845    struct ast_variable *var;
05846 
05847    ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
05848    if (!(q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find queue for reload"))) {
05849       if (queue_reload) {
05850          /* Make one then */
05851          if (!(q = alloc_queue(queuename))) {
05852             return;
05853          }
05854       } else {
05855          /* Since we're not reloading queues, this means that we found a queue
05856           * in the configuration file which we don't know about yet. Just return.
05857           */
05858          return;
05859       }
05860       new = 1;
05861    } else {
05862       new = 0;
05863    }
05864    
05865    if (!new) {
05866       ao2_lock(q);
05867       prev_weight = q->weight ? 1 : 0;
05868    }
05869    /* Check if we already found a queue with this name in the config file */
05870    if (q->found) {
05871       ast_log(LOG_WARNING, "Queue '%s' already defined! Skipping!\n", queuename);
05872       if (!new) {
05873          /* It should be impossible to *not* hit this case*/
05874          ao2_unlock(q);
05875       }
05876       queue_t_unref(q, "We exist! Expiring temporary pointer");
05877       return;
05878    }
05879    /* Due to the fact that the "linear" strategy will have a different allocation
05880     * scheme for queue members, we must devise the queue's strategy before other initializations.
05881     * To be specific, the linear strategy needs to function like a linked list, meaning the ao2
05882     * container used will have only a single bucket instead of the typical number.
05883     */
05884    if (queue_reload) {
05885       if ((tmpvar = ast_variable_retrieve(cfg, queuename, "strategy"))) {
05886          q->strategy = strat2int(tmpvar);
05887          if (q->strategy < 0) {
05888             ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
05889             tmpvar, q->name);
05890             q->strategy = QUEUE_STRATEGY_RINGALL;
05891          }
05892       } else {
05893          q->strategy = QUEUE_STRATEGY_RINGALL;
05894       }
05895       init_queue(q);
05896    }
05897    if (member_reload) {
05898       q->membercount = 0;
05899       ao2_callback(q->members, OBJ_NODATA, mark_member_dead, NULL);
05900    }
05901    for (var = ast_variable_browse(cfg, queuename); var; var = var->next) {
05902       if (member_reload && !strcasecmp(var->name, "member")) {
05903          reload_single_member(var->value, q);
05904       } else if (queue_reload) {
05905          queue_set_param(q, var->name, var->value, var->lineno, 1);
05906       }
05907    }
05908    /* At this point, we've determined if the queue has a weight, so update use_weight
05909     * as appropriate
05910     */
05911    if (!q->weight && prev_weight) {
05912       ast_atomic_fetchadd_int(&use_weight, -1);
05913    }
05914    else if (q->weight && !prev_weight) {
05915       ast_atomic_fetchadd_int(&use_weight, +1);
05916    }
05917 
05918    /* Free remaining members marked as delme */
05919    if (member_reload) {
05920       ao2_callback(q->members, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, kill_dead_members, q);
05921    }
05922 
05923    if (new) {
05924       queues_t_link(queues, q, "Add queue to container");
05925    } else {
05926       ao2_unlock(q);
05927    }
05928    queue_t_unref(q, "Expiring creation reference");
05929 }
05930 
05931 static int mark_dead_and_unfound(void *obj, void *arg, int flags)
05932 {
05933    struct call_queue *q = obj;
05934    char *queuename = arg;
05935    if (!q->realtime && (ast_strlen_zero(queuename) || !strcasecmp(queuename, q->name))) {
05936       q->dead = 1;
05937       q->found = 0;
05938    }
05939    return 0;
05940 }
05941 
05942 static int kill_dead_queues(void *obj, void *arg, int flags)
05943 {
05944    struct call_queue *q = obj;
05945    char *queuename = arg;
05946    if ((ast_strlen_zero(queuename) || !strcasecmp(queuename, q->name)) && q->dead) {
05947       return CMP_MATCH;
05948    } else {
05949       return 0;
05950    }
05951 }
05952 
05953 /*! \brief reload the queues.conf file
05954  *
05955  * This function reloads the information in the general section of the queues.conf
05956  * file and potentially more, depending on the value of mask.
05957  *
05958  * \param reload 0 if we are calling this the first time, 1 every other time
05959  * \param mask Gives flags telling us what information to actually reload
05960  * \param queuename If set to a non-zero string, then only reload information from
05961  * that particular queue. Otherwise inspect all queues
05962  * \retval -1 Failure occurred 
05963  * \retval 0 All clear!
05964  */
05965 static int reload_queues(int reload, struct ast_flags *mask, const char *queuename)
05966 {
05967    struct ast_config *cfg;
05968    char *cat;
05969    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
05970    const int queue_reload = ast_test_flag(mask, QUEUE_RELOAD_PARAMETERS);
05971 
05972    if (!(cfg = ast_config_load("queues.conf", config_flags))) {
05973       ast_log(LOG_NOTICE, "No call queueing config file (queues.conf), so no call queues\n");
05974       return -1;
05975    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
05976       return 0;
05977    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
05978       ast_log(LOG_ERROR, "Config file queues.conf is in an invalid format.  Aborting.\n");
05979       return -1;
05980    }
05981 
05982    /* We've made it here, so it looks like we're doing operations on all queues. */
05983    ao2_lock(queues);
05984    
05985    /* Mark all queues as dead for the moment if we're reloading queues.
05986     * For clarity, we could just be reloading members, in which case we don't want to mess
05987     * with the other queue parameters at all*/
05988    if (queue_reload) {
05989       ao2_callback(queues, OBJ_NODATA, mark_dead_and_unfound, (char *) queuename);
05990    }
05991 
05992    /* Chug through config file */
05993    cat = NULL;
05994    while ((cat = ast_category_browse(cfg, cat)) ) {
05995       if (!strcasecmp(cat, "general") && queue_reload) {
05996          queue_set_global_params(cfg);
05997          continue;
05998       }
05999       if (ast_strlen_zero(queuename) || !strcasecmp(cat, queuename))
06000          reload_single_queue(cfg, mask, cat);
06001    }
06002 
06003    ast_config_destroy(cfg);
06004    /* Unref all the dead queues if we were reloading queues */
06005    if (queue_reload) {
06006       ao2_callback(queues, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, kill_dead_queues, (char *) queuename);
06007    }
06008    ao2_unlock(queues);
06009    return 0;
06010 }
06011   
06012 /*! \brief Facilitates resetting statistics for a queue
06013  *
06014  * This function actually does not reset any statistics, but
06015  * rather finds a call_queue struct which corresponds to the
06016  * passed-in queue name and passes that structure to the 
06017  * clear_queue function. If no queuename is passed in, then
06018  * all queues will have their statistics reset.
06019  *
06020  * \param queuename The name of the queue to reset the statistics
06021  * for. If this is NULL or zero-length, then this means to reset
06022  * the statistics for all queues
06023  * \retval void
06024  */
06025 static int clear_stats(const char *queuename)
06026 {
06027    struct call_queue *q;
06028    struct ao2_iterator queue_iter = ao2_iterator_init(queues, 0);
06029    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06030       ao2_lock(q);
06031       if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename))
06032          clear_queue(q);
06033       ao2_unlock(q);
06034       queue_t_unref(q, "Done with iterator");
06035    }
06036    ao2_iterator_destroy(&queue_iter);
06037    return 0;
06038 }
06039 
06040 /*! \brief The command center for all reload operations
06041  *
06042  * Whenever any piece of queue information is to be reloaded, this function
06043  * is called. It interprets the flags set in the mask parameter and acts
06044  * based on how they are set.
06045  *
06046  * \param reload True if we are reloading information, false if we are loading
06047  * information for the first time.
06048  * \param mask A bitmask which tells the handler what actions to take
06049  * \param queuename The name of the queue on which we wish to take action
06050  * \retval 0 All reloads were successful
06051  * \retval non-zero There was a failure
06052  */
06053 static int reload_handler(int reload, struct ast_flags *mask, const char *queuename)
06054 {
06055    int res = 0;
06056 
06057    if (ast_test_flag(mask, QUEUE_RELOAD_RULES)) {
06058       res |= reload_queue_rules(reload);
06059    }
06060    if (ast_test_flag(mask, QUEUE_RESET_STATS)) {
06061       res |= clear_stats(queuename);
06062    }
06063    if (ast_test_flag(mask, (QUEUE_RELOAD_PARAMETERS | QUEUE_RELOAD_MEMBER))) {
06064       res |= reload_queues(reload, mask, queuename);
06065    }
06066    return res;
06067 }
06068 
06069 /*! \brief direct ouput to manager or cli with proper terminator */
06070 static void do_print(struct mansession *s, int fd, const char *str)
06071 {
06072    if (s)
06073       astman_append(s, "%s\r\n", str);
06074    else
06075       ast_cli(fd, "%s\n", str);
06076 }
06077 
06078 /*! 
06079  * \brief Show queue(s) status and statistics 
06080  * 
06081  * List the queues strategy, calls processed, members logged in,
06082  * other queue statistics such as avg hold time.
06083 */
06084 static char *__queues_show(struct mansession *s, int fd, int argc, char **argv)
06085 {
06086    struct call_queue *q;
06087    struct ast_str *out = ast_str_alloca(240);
06088    int found = 0;
06089    time_t now = time(NULL);
06090    struct ao2_iterator queue_iter;
06091    struct ao2_iterator mem_iter;
06092 
06093    if (argc != 2 && argc != 3)
06094       return CLI_SHOWUSAGE;
06095 
06096    if (argc == 3) { /* specific queue */
06097       if ((q = load_realtime_queue(argv[2]))) {
06098          queue_t_unref(q, "Done with temporary pointer");
06099       }
06100    } else if (ast_check_realtime("queues")) {
06101       /* This block is to find any queues which are defined in realtime but
06102        * which have not yet been added to the in-core container
06103        */
06104       struct ast_config *cfg = ast_load_realtime_multientry("queues", "name LIKE", "%", SENTINEL);
06105       char *queuename;
06106       if (cfg) {
06107          for (queuename = ast_category_browse(cfg, NULL); !ast_strlen_zero(queuename); queuename = ast_category_browse(cfg, queuename)) {
06108             if ((q = load_realtime_queue(queuename))) {
06109                queue_t_unref(q, "Done with temporary pointer");
06110             }
06111          }
06112          ast_config_destroy(cfg);
06113       }
06114    }
06115 
06116    queue_iter = ao2_iterator_init(queues, AO2_ITERATOR_DONTLOCK);
06117    ao2_lock(queues);
06118    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06119       float sl;
06120       struct call_queue *realtime_queue = NULL;
06121 
06122       ao2_lock(q);
06123       /* This check is to make sure we don't print information for realtime
06124        * queues which have been deleted from realtime but which have not yet
06125        * been deleted from the in-core container
06126        */
06127       if (q->realtime && !(realtime_queue = load_realtime_queue(q->name))) {
06128          ao2_unlock(q);
06129          queue_t_unref(q, "Done with iterator");
06130          continue;
06131       } else if (q->realtime) {
06132          queue_t_unref(realtime_queue, "Queue is already in memory");
06133       }
06134       if (argc == 3 && strcasecmp(q->name, argv[2])) {
06135          ao2_unlock(q);
06136          queue_t_unref(q, "Done with iterator");
06137          continue;
06138       }
06139       found = 1;
06140 
06141       ast_str_set(&out, 0, "%s has %d calls (max ", q->name, q->count);
06142       if (q->maxlen)
06143          ast_str_append(&out, 0, "%d", q->maxlen);
06144       else
06145          ast_str_append(&out, 0, "unlimited");
06146       sl = 0;
06147       if (q->callscompleted > 0)
06148          sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
06149       ast_str_append(&out, 0, ") in '%s' strategy (%ds holdtime, %ds talktime), W:%d, C:%d, A:%d, SL:%2.1f%% within %ds",
06150          int2strat(q->strategy), q->holdtime, q->talktime, q->weight,
06151          q->callscompleted, q->callsabandoned,sl,q->servicelevel);
06152       do_print(s, fd, ast_str_buffer(out));
06153       if (!ao2_container_count(q->members))
06154          do_print(s, fd, "   No Members");
06155       else {
06156          struct member *mem;
06157 
06158          do_print(s, fd, "   Members: ");
06159          mem_iter = ao2_iterator_init(q->members, 0);
06160          while ((mem = ao2_iterator_next(&mem_iter))) {
06161             ast_str_set(&out, 0, "      %s", mem->membername);
06162             if (strcasecmp(mem->membername, mem->interface)) {
06163                ast_str_append(&out, 0, " (%s)", mem->interface);
06164             }
06165             if (mem->penalty)
06166                ast_str_append(&out, 0, " with penalty %d", mem->penalty);
06167             ast_str_append(&out, 0, "%s%s%s (%s)",
06168                mem->dynamic ? " (dynamic)" : "",
06169                mem->realtime ? " (realtime)" : "",
06170                mem->paused ? " (paused)" : "",
06171                ast_devstate2str(mem->status));
06172             if (mem->calls)
06173                ast_str_append(&out, 0, " has taken %d calls (last was %ld secs ago)",
06174                   mem->calls, (long) (time(NULL) - mem->lastcall));
06175             else
06176                ast_str_append(&out, 0, " has taken no calls yet");
06177             do_print(s, fd, ast_str_buffer(out));
06178             ao2_ref(mem, -1);
06179          }
06180          ao2_iterator_destroy(&mem_iter);
06181       }
06182       if (!q->head)
06183          do_print(s, fd, "   No Callers");
06184       else {
06185          struct queue_ent *qe;
06186          int pos = 1;
06187 
06188          do_print(s, fd, "   Callers: ");
06189          for (qe = q->head; qe; qe = qe->next) {
06190             ast_str_set(&out, 0, "      %d. %s (wait: %ld:%2.2ld, prio: %d)",
06191                pos++, qe->chan->name, (long) (now - qe->start) / 60,
06192                (long) (now - qe->start) % 60, qe->prio);
06193             do_print(s, fd, ast_str_buffer(out));
06194          }
06195       }
06196       do_print(s, fd, ""); /* blank line between entries */
06197       ao2_unlock(q);
06198       queue_t_unref(q, "Done with iterator"); /* Unref the iterator's reference */
06199    }
06200    ao2_iterator_destroy(&queue_iter);
06201    ao2_unlock(queues);
06202    if (!found) {
06203       if (argc == 3)
06204          ast_str_set(&out, 0, "No such queue: %s.", argv[2]);
06205       else
06206          ast_str_set(&out, 0, "No queues.");
06207       do_print(s, fd, ast_str_buffer(out));
06208    }
06209    return CLI_SUCCESS;
06210 }
06211 
06212 static char *complete_queue(const char *line, const char *word, int pos, int state)
06213 {
06214    struct call_queue *q;
06215    char *ret = NULL;
06216    int which = 0;
06217    int wordlen = strlen(word);
06218    struct ao2_iterator queue_iter;
06219 
06220    queue_iter = ao2_iterator_init(queues, 0);
06221    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06222       if (!strncasecmp(word, q->name, wordlen) && ++which > state) {
06223          ret = ast_strdup(q->name);
06224          queue_t_unref(q, "Done with iterator");
06225          break;
06226       }
06227       queue_t_unref(q, "Done with iterator");
06228    }
06229    ao2_iterator_destroy(&queue_iter);
06230 
06231    return ret;
06232 }
06233 
06234 static char *complete_queue_show(const char *line, const char *word, int pos, int state)
06235 {
06236    if (pos == 2)
06237       return complete_queue(line, word, pos, state);
06238    return NULL;
06239 }
06240 
06241 static char *queue_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06242 {
06243    switch ( cmd ) {
06244    case CLI_INIT:
06245       e->command = "queue show";
06246       e->usage =
06247          "Usage: queue show\n"
06248          "       Provides summary information on a specified queue.\n";
06249       return NULL;
06250    case CLI_GENERATE:
06251       return complete_queue_show(a->line, a->word, a->pos, a->n); 
06252    }
06253 
06254    return __queues_show(NULL, a->fd, a->argc, a->argv);
06255 }
06256 
06257 /*!\brief callback to display queues status in manager
06258    \addtogroup Group_AMI
06259  */
06260 static int manager_queues_show(struct mansession *s, const struct message *m)
06261 {
06262    char *a[] = { "queue", "show" };
06263 
06264    __queues_show(s, -1, 2, a);
06265    astman_append(s, "\r\n\r\n"); /* Properly terminate Manager output */
06266 
06267    return RESULT_SUCCESS;
06268 }
06269 
06270 static int manager_queue_rule_show(struct mansession *s, const struct message *m)
06271 {
06272    const char *rule = astman_get_header(m, "Rule");
06273    struct rule_list *rl_iter;
06274    struct penalty_rule *pr_iter;
06275 
06276    AST_LIST_LOCK(&rule_lists);
06277    AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
06278       if (ast_strlen_zero(rule) || !strcasecmp(rule, rl_iter->name)) {
06279          astman_append(s, "RuleList: %s\r\n", rl_iter->name);
06280          AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
06281             astman_append(s, "Rule: %d,%s%d,%s%d\r\n", pr_iter->time, pr_iter->max_relative && pr_iter->max_value >= 0 ? "+" : "", pr_iter->max_value, pr_iter->min_relative && pr_iter->min_value >= 0 ? "+" : "", pr_iter->min_value );
06282          }
06283          if (!ast_strlen_zero(rule))
06284             break;
06285       }
06286    }
06287    AST_LIST_UNLOCK(&rule_lists);
06288 
06289    astman_append(s, "\r\n\r\n");
06290 
06291    return RESULT_SUCCESS;
06292 }
06293 
06294 /*! \brief Summary of queue info via the AMI */
06295 static int manager_queues_summary(struct mansession *s, const struct message *m)
06296 {
06297    time_t now;
06298    int qmemcount = 0;
06299    int qmemavail = 0;
06300    int qchancount = 0;
06301    int qlongestholdtime = 0;
06302    const char *id = astman_get_header(m, "ActionID");
06303    const char *queuefilter = astman_get_header(m, "Queue");
06304    char idText[256] = "";
06305    struct call_queue *q;
06306    struct queue_ent *qe;
06307    struct member *mem;
06308    struct ao2_iterator queue_iter;
06309    struct ao2_iterator mem_iter;
06310 
06311    astman_send_ack(s, m, "Queue summary will follow");
06312    time(&now);
06313    if (!ast_strlen_zero(id))
06314       snprintf(idText, 256, "ActionID: %s\r\n", id);
06315    queue_iter = ao2_iterator_init(queues, 0);
06316    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06317       ao2_lock(q);
06318 
06319       /* List queue properties */
06320       if (ast_strlen_zero(queuefilter) || !strcmp(q->name, queuefilter)) {
06321          /* Reset the necessary local variables if no queuefilter is set*/
06322          qmemcount = 0;
06323          qmemavail = 0;
06324          qchancount = 0;
06325          qlongestholdtime = 0;
06326 
06327          /* List Queue Members */
06328          mem_iter = ao2_iterator_init(q->members, 0);
06329          while ((mem = ao2_iterator_next(&mem_iter))) {
06330             if ((mem->status != AST_DEVICE_UNAVAILABLE) && (mem->status != AST_DEVICE_INVALID)) {
06331                ++qmemcount;
06332                if (((mem->status == AST_DEVICE_NOT_INUSE) || (mem->status == AST_DEVICE_UNKNOWN)) && !(mem->paused)) {
06333                   ++qmemavail;
06334                }
06335             }
06336             ao2_ref(mem, -1);
06337          }
06338          ao2_iterator_destroy(&mem_iter);
06339          for (qe = q->head; qe; qe = qe->next) {
06340             if ((now - qe->start) > qlongestholdtime) {
06341                qlongestholdtime = now - qe->start;
06342             }
06343             ++qchancount;
06344          }
06345          astman_append(s, "Event: QueueSummary\r\n"
06346             "Queue: %s\r\n"
06347             "LoggedIn: %d\r\n"
06348             "Available: %d\r\n"
06349             "Callers: %d\r\n" 
06350             "HoldTime: %d\r\n"
06351             "TalkTime: %d\r\n"
06352             "LongestHoldTime: %d\r\n"
06353             "%s"
06354             "\r\n",
06355             q->name, qmemcount, qmemavail, qchancount, q->holdtime, q->talktime, qlongestholdtime, idText);
06356       }
06357       ao2_unlock(q);
06358       queue_t_unref(q, "Done with iterator");
06359    }
06360    ao2_iterator_destroy(&queue_iter);
06361    astman_append(s,
06362       "Event: QueueSummaryComplete\r\n"
06363       "%s"
06364       "\r\n", idText);
06365 
06366    return RESULT_SUCCESS;
06367 }
06368 
06369 /*! \brief Queue status info via AMI */
06370 static int manager_queues_status(struct mansession *s, const struct message *m)
06371 {
06372    time_t now;
06373    int pos;
06374    const char *id = astman_get_header(m,"ActionID");
06375    const char *queuefilter = astman_get_header(m,"Queue");
06376    const char *memberfilter = astman_get_header(m,"Member");
06377    char idText[256] = "";
06378    struct call_queue *q;
06379    struct queue_ent *qe;
06380    float sl = 0;
06381    struct member *mem;
06382    struct ao2_iterator queue_iter;
06383    struct ao2_iterator mem_iter;
06384 
06385    astman_send_ack(s, m, "Queue status will follow");
06386    time(&now);
06387    if (!ast_strlen_zero(id))
06388       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
06389 
06390    queue_iter = ao2_iterator_init(queues, 0);
06391    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06392       ao2_lock(q);
06393 
06394       /* List queue properties */
06395       if (ast_strlen_zero(queuefilter) || !strcmp(q->name, queuefilter)) {
06396          sl = ((q->callscompleted > 0) ? 100 * ((float)q->callscompletedinsl / (float)q->callscompleted) : 0);
06397          astman_append(s, "Event: QueueParams\r\n"
06398             "Queue: %s\r\n"
06399             "Max: %d\r\n"
06400             "Strategy: %s\r\n"
06401             "Calls: %d\r\n"
06402             "Holdtime: %d\r\n"
06403             "TalkTime: %d\r\n"
06404             "Completed: %d\r\n"
06405             "Abandoned: %d\r\n"
06406             "ServiceLevel: %d\r\n"
06407             "ServicelevelPerf: %2.1f\r\n"
06408             "Weight: %d\r\n"
06409             "%s"
06410             "\r\n",
06411             q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted,
06412             q->callsabandoned, q->servicelevel, sl, q->weight, idText);
06413          /* List Queue Members */
06414          mem_iter = ao2_iterator_init(q->members, 0);
06415          while ((mem = ao2_iterator_next(&mem_iter))) {
06416             if (ast_strlen_zero(memberfilter) || !strcmp(mem->interface, memberfilter) || !strcmp(mem->membername, memberfilter)) {
06417                astman_append(s, "Event: QueueMember\r\n"
06418                   "Queue: %s\r\n"
06419                   "Name: %s\r\n"
06420                   "Location: %s\r\n"
06421                   "Membership: %s\r\n"
06422                   "Penalty: %d\r\n"
06423                   "CallsTaken: %d\r\n"
06424                   "LastCall: %d\r\n"
06425                   "Status: %d\r\n"
06426                   "Paused: %d\r\n"
06427                   "%s"
06428                   "\r\n",
06429                   q->name, mem->membername, mem->interface, mem->dynamic ? "dynamic" : "static",
06430                   mem->penalty, mem->calls, (int)mem->lastcall, mem->status, mem->paused, idText);
06431             }
06432             ao2_ref(mem, -1);
06433          }
06434          ao2_iterator_destroy(&mem_iter);
06435          /* List Queue Entries */
06436          pos = 1;
06437          for (qe = q->head; qe; qe = qe->next) {
06438             astman_append(s, "Event: QueueEntry\r\n"
06439                "Queue: %s\r\n"
06440                "Position: %d\r\n"
06441                "Channel: %s\r\n"
06442                "Uniqueid: %s\r\n"
06443                "CallerIDNum: %s\r\n"
06444                "CallerIDName: %s\r\n"
06445                "Wait: %ld\r\n"
06446                "%s"
06447                "\r\n",
06448                q->name, pos++, qe->chan->name, qe->chan->uniqueid,
06449                S_OR(qe->chan->cid.cid_num, "unknown"),
06450                S_OR(qe->chan->cid.cid_name, "unknown"),
06451                (long) (now - qe->start), idText);
06452          }
06453       }
06454       ao2_unlock(q);
06455       queue_t_unref(q, "Done with iterator");
06456    }
06457    ao2_iterator_destroy(&queue_iter);
06458 
06459    astman_append(s,
06460       "Event: QueueStatusComplete\r\n"
06461       "%s"
06462       "\r\n",idText);
06463 
06464    return RESULT_SUCCESS;
06465 }
06466 
06467 static int manager_add_queue_member(struct mansession *s, const struct message *m)
06468 {
06469    const char *queuename, *interface, *penalty_s, *paused_s, *membername, *state_interface;
06470    int paused, penalty = 0;
06471 
06472    queuename = astman_get_header(m, "Queue");
06473    interface = astman_get_header(m, "Interface");
06474    penalty_s = astman_get_header(m, "Penalty");
06475    paused_s = astman_get_header(m, "Paused");
06476    membername = astman_get_header(m, "MemberName");
06477    state_interface = astman_get_header(m, "StateInterface");
06478 
06479    if (ast_strlen_zero(queuename)) {
06480       astman_send_error(s, m, "'Queue' not specified.");
06481       return 0;
06482    }
06483 
06484    if (ast_strlen_zero(interface)) {
06485       astman_send_error(s, m, "'Interface' not specified.");
06486       return 0;
06487    }
06488 
06489    if (ast_strlen_zero(penalty_s))
06490       penalty = 0;
06491    else if (sscanf(penalty_s, "%30d", &penalty) != 1 || penalty < 0)
06492       penalty = 0;
06493 
06494    if (ast_strlen_zero(paused_s))
06495       paused = 0;
06496    else
06497       paused = abs(ast_true(paused_s));
06498 
06499    switch (add_to_queue(queuename, interface, membername, penalty, paused, queue_persistent_members, state_interface)) {
06500    case RES_OKAY:
06501       ast_queue_log(queuename, "MANAGER", interface, "ADDMEMBER", "%s", "");
06502       astman_send_ack(s, m, "Added interface to queue");
06503       break;
06504    case RES_EXISTS:
06505       astman_send_error(s, m, "Unable to add interface: Already there");
06506       break;
06507    case RES_NOSUCHQUEUE:
06508       astman_send_error(s, m, "Unable to add interface to queue: No such queue");
06509       break;
06510    case RES_OUTOFMEMORY:
06511       astman_send_error(s, m, "Out of memory");
06512       break;
06513    }
06514 
06515    return 0;
06516 }
06517 
06518 static int manager_remove_queue_member(struct mansession *s, const struct message *m)
06519 {
06520    const char *queuename, *interface;
06521 
06522    queuename = astman_get_header(m, "Queue");
06523    interface = astman_get_header(m, "Interface");
06524 
06525    if (ast_strlen_zero(queuename) || ast_strlen_zero(interface)) {
06526       astman_send_error(s, m, "Need 'Queue' and 'Interface' parameters.");
06527       return 0;
06528    }
06529 
06530    switch (remove_from_queue(queuename, interface)) {
06531    case RES_OKAY:
06532       ast_queue_log(queuename, "MANAGER", interface, "REMOVEMEMBER", "%s", "");
06533       astman_send_ack(s, m, "Removed interface from queue");
06534       break;
06535    case RES_EXISTS:
06536       astman_send_error(s, m, "Unable to remove interface: Not there");
06537       break;
06538    case RES_NOSUCHQUEUE:
06539       astman_send_error(s, m, "Unable to remove interface from queue: No such queue");
06540       break;
06541    case RES_OUTOFMEMORY:
06542       astman_send_error(s, m, "Out of memory");
06543       break;
06544    case RES_NOT_DYNAMIC:
06545       astman_send_error(s, m, "Member not dynamic");
06546       break;
06547    }
06548 
06549    return 0;
06550 }
06551 
06552 static int manager_pause_queue_member(struct mansession *s, const struct message *m)
06553 {
06554    const char *queuename, *interface, *paused_s, *reason;
06555    int paused;
06556 
06557    interface = astman_get_header(m, "Interface");
06558    paused_s = astman_get_header(m, "Paused");
06559    queuename = astman_get_header(m, "Queue");      /* Optional - if not supplied, pause the given Interface in all queues */
06560    reason = astman_get_header(m, "Reason");        /* Optional - Only used for logging purposes */
06561 
06562    if (ast_strlen_zero(interface) || ast_strlen_zero(paused_s)) {
06563       astman_send_error(s, m, "Need 'Interface' and 'Paused' parameters.");
06564       return 0;
06565    }
06566 
06567    paused = abs(ast_true(paused_s));
06568 
06569    if (set_member_paused(queuename, interface, reason, paused))
06570       astman_send_error(s, m, "Interface not found");
06571    else
06572       astman_send_ack(s, m, paused ? "Interface paused successfully" : "Interface unpaused successfully");
06573    return 0;
06574 }
06575 
06576 static int manager_queue_log_custom(struct mansession *s, const struct message *m)
06577 {
06578    const char *queuename, *event, *message, *interface, *uniqueid;
06579 
06580    queuename = astman_get_header(m, "Queue");
06581    uniqueid = astman_get_header(m, "UniqueId");
06582    interface = astman_get_header(m, "Interface");
06583    event = astman_get_header(m, "Event");
06584    message = astman_get_header(m, "Message");
06585 
06586    if (ast_strlen_zero(queuename) || ast_strlen_zero(event)) {
06587       astman_send_error(s, m, "Need 'Queue' and 'Event' parameters.");
06588       return 0;
06589    }
06590 
06591    ast_queue_log(queuename, S_OR(uniqueid, "NONE"), interface, event, "%s", message);
06592    astman_send_ack(s, m, "Event added successfully");
06593 
06594    return 0;
06595 }
06596 
06597 static int manager_queue_reload(struct mansession *s, const struct message *m)
06598 {
06599    struct ast_flags mask = {0,};
06600    const char *queuename = NULL;
06601    int header_found = 0;
06602 
06603    queuename = astman_get_header(m, "Queue");
06604    if (!strcasecmp(S_OR(astman_get_header(m, "Members"), ""), "yes")) {
06605       ast_set_flag(&mask, QUEUE_RELOAD_MEMBER);
06606       header_found = 1;
06607    }
06608    if (!strcasecmp(S_OR(astman_get_header(m, "Rules"), ""), "yes")) {
06609       ast_set_flag(&mask, QUEUE_RELOAD_RULES);
06610       header_found = 1;
06611    }
06612    if (!strcasecmp(S_OR(astman_get_header(m, "Parameters"), ""), "yes")) {
06613       ast_set_flag(&mask, QUEUE_RELOAD_PARAMETERS);
06614       header_found = 1;
06615    }
06616 
06617    if (!header_found) {
06618       ast_set_flag(&mask, AST_FLAGS_ALL);
06619    }
06620 
06621    if (!reload_handler(1, &mask, queuename)) {
06622       astman_send_ack(s, m, "Queue reloaded successfully");
06623    } else {
06624       astman_send_error(s, m, "Error encountered while reloading queue");
06625    }
06626    return 0;
06627 }
06628 
06629 static int manager_queue_reset(struct mansession *s, const struct message *m)
06630 {
06631    const char *queuename = NULL;
06632    struct ast_flags mask = {QUEUE_RESET_STATS,};
06633    
06634    queuename = astman_get_header(m, "Queue");
06635 
06636    if (!reload_handler(1, &mask, queuename)) {
06637       astman_send_ack(s, m, "Queue stats reset successfully");
06638    } else {
06639       astman_send_error(s, m, "Error encountered while resetting queue stats");
06640    }
06641    return 0;
06642 }
06643 
06644 static char *complete_queue_add_member(const char *line, const char *word, int pos, int state)
06645 {
06646    /* 0 - queue; 1 - add; 2 - member; 3 - <interface>; 4 - to; 5 - <queue>; 6 - penalty; 7 - <penalty>; 8 - as; 9 - <membername> */
06647    switch (pos) {
06648    case 3: /* Don't attempt to complete name of interface (infinite possibilities) */
06649       return NULL;
06650    case 4: /* only one possible match, "to" */
06651       return state == 0 ? ast_strdup("to") : NULL;
06652    case 5: /* <queue> */
06653       return complete_queue(line, word, pos, state);
06654    case 6: /* only one possible match, "penalty" */
06655       return state == 0 ? ast_strdup("penalty") : NULL;
06656    case 7:
06657       if (state < 100) {      /* 0-99 */
06658          char *num;
06659          if ((num = ast_malloc(3))) {
06660             sprintf(num, "%d", state);
06661          }
06662          return num;
06663       } else {
06664          return NULL;
06665       }
06666    case 8: /* only one possible match, "as" */
06667       return state == 0 ? ast_strdup("as") : NULL;
06668    case 9: /* Don't attempt to complete name of member (infinite possibilities) */
06669       return NULL;
06670    default:
06671       return NULL;
06672    }
06673 }
06674 
06675 static int manager_queue_member_penalty(struct mansession *s, const struct message *m)
06676 {
06677    const char *queuename, *interface, *penalty_s;
06678    int penalty;
06679 
06680    interface = astman_get_header(m, "Interface");
06681    penalty_s = astman_get_header(m, "Penalty");
06682    /* Optional - if not supplied, set the penalty value for the given Interface in all queues */
06683    queuename = astman_get_header(m, "Queue");
06684 
06685    if (ast_strlen_zero(interface) || ast_strlen_zero(penalty_s)) {
06686       astman_send_error(s, m, "Need 'Interface' and 'Penalty' parameters.");
06687       return 0;
06688    }
06689  
06690    penalty = atoi(penalty_s);
06691 
06692    if (set_member_penalty((char *)queuename, (char *)interface, penalty))
06693       astman_send_error(s, m, "Invalid interface, queuename or penalty");
06694    else
06695       astman_send_ack(s, m, "Interface penalty set successfully");
06696 
06697    return 0;
06698 }
06699 
06700 static char *handle_queue_add_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06701 {
06702    char *queuename, *interface, *membername = NULL, *state_interface = NULL;
06703    int penalty;
06704 
06705    switch ( cmd ) {
06706    case CLI_INIT:
06707       e->command = "queue add member";
06708       e->usage =
06709          "Usage: queue add member <channel> to <queue> [[[penalty <penalty>] as <membername>] state_interface <interface>]\n"
06710          "       Add a channel to a queue with optionally:  a penalty, membername and a state_interface\n";
06711       return NULL;
06712    case CLI_GENERATE:
06713       return complete_queue_add_member(a->line, a->word, a->pos, a->n);
06714    }
06715 
06716    if ((a->argc != 6) && (a->argc != 8) && (a->argc != 10) && (a->argc != 12)) {
06717       return CLI_SHOWUSAGE;
06718    } else if (strcmp(a->argv[4], "to")) {
06719       return CLI_SHOWUSAGE;
06720    } else if ((a->argc >= 8) && strcmp(a->argv[6], "penalty")) {
06721       return CLI_SHOWUSAGE;
06722    } else if ((a->argc >= 10) && strcmp(a->argv[8], "as")) {
06723       return CLI_SHOWUSAGE;
06724    } else if ((a->argc == 12) && strcmp(a->argv[10], "state_interface")) {
06725       return CLI_SHOWUSAGE;
06726    }
06727 
06728    queuename = a->argv[5];
06729    interface = a->argv[3];
06730    if (a->argc >= 8) {
06731       if (sscanf(a->argv[7], "%30d", &penalty) == 1) {
06732          if (penalty < 0) {
06733             ast_cli(a->fd, "Penalty must be >= 0\n");
06734             penalty = 0;
06735          }
06736       } else {
06737          ast_cli(a->fd, "Penalty must be an integer >= 0\n");
06738          penalty = 0;
06739       }
06740    } else {
06741       penalty = 0;
06742    }
06743 
06744    if (a->argc >= 10) {
06745       membername = a->argv[9];
06746    }
06747 
06748    if (a->argc >= 12) {
06749       state_interface = a->argv[11];
06750    }
06751 
06752    switch (add_to_queue(queuename, interface, membername, penalty, 0, queue_persistent_members, state_interface)) {
06753    case RES_OKAY:
06754       ast_queue_log(queuename, "CLI", interface, "ADDMEMBER", "%s", "");
06755       ast_cli(a->fd, "Added interface '%s' to queue '%s'\n", interface, queuename);
06756       return CLI_SUCCESS;
06757    case RES_EXISTS:
06758       ast_cli(a->fd, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
06759       return CLI_FAILURE;
06760    case RES_NOSUCHQUEUE:
06761       ast_cli(a->fd, "Unable to add interface to queue '%s': No such queue\n", queuename);
06762       return CLI_FAILURE;
06763    case RES_OUTOFMEMORY:
06764       ast_cli(a->fd, "Out of memory\n");
06765       return CLI_FAILURE;
06766    case RES_NOT_DYNAMIC:
06767       ast_cli(a->fd, "Member not dynamic\n");
06768       return CLI_FAILURE;
06769    default:
06770       return CLI_FAILURE;
06771    }
06772 }
06773 
06774 static char *complete_queue_remove_member(const char *line, const char *word, int pos, int state)
06775 {
06776    int which = 0;
06777    struct call_queue *q;
06778    struct member *m;
06779    struct ao2_iterator queue_iter;
06780    struct ao2_iterator mem_iter;
06781    int wordlen = strlen(word);
06782 
06783    /* 0 - queue; 1 - remove; 2 - member; 3 - <member>; 4 - from; 5 - <queue> */
06784    if (pos > 5 || pos < 3)
06785       return NULL;
06786    if (pos == 4)   /* only one possible match, 'from' */
06787       return (state == 0 ? ast_strdup("from") : NULL);
06788 
06789    if (pos == 5)   /* No need to duplicate code */
06790       return complete_queue(line, word, pos, state);
06791 
06792    /* here is the case for 3, <member> */
06793    queue_iter = ao2_iterator_init(queues, 0);
06794    while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
06795       ao2_lock(q);
06796       mem_iter = ao2_iterator_init(q->members, 0);
06797       while ((m = ao2_iterator_next(&mem_iter))) {
06798          if (!strncasecmp(word, m->membername, wordlen) && ++which > state) {
06799             char *tmp;
06800             ao2_unlock(q);
06801             tmp = ast_strdup(m->interface);
06802             ao2_ref(m, -1);
06803             queue_t_unref(q, "Done with iterator, returning interface name");
06804             ao2_iterator_destroy(&mem_iter);
06805             ao2_iterator_destroy(&queue_iter);
06806             return tmp;
06807          }
06808          ao2_ref(m, -1);
06809       }
06810       ao2_iterator_destroy(&mem_iter);
06811       ao2_unlock(q);
06812       queue_t_unref(q, "Done with iterator");
06813    }
06814    ao2_iterator_destroy(&queue_iter);
06815 
06816    return NULL;
06817 }
06818 
06819 static char *handle_queue_remove_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06820 {
06821    char *queuename, *interface;
06822 
06823    switch (cmd) {
06824    case CLI_INIT:
06825       e->command = "queue remove member";
06826       e->usage = 
06827          "Usage: queue remove member <channel> from <queue>\n"
06828          "       Remove a specific channel from a queue.\n";
06829       return NULL;
06830    case CLI_GENERATE:
06831       return complete_queue_remove_member(a->line, a->word, a->pos, a->n);
06832    }
06833 
06834    if (a->argc != 6) {
06835       return CLI_SHOWUSAGE;
06836    } else if (strcmp(a->argv[4], "from")) {
06837       return CLI_SHOWUSAGE;
06838    }
06839 
06840    queuename = a->argv[5];
06841    interface = a->argv[3];
06842 
06843    switch (remove_from_queue(queuename, interface)) {
06844    case RES_OKAY:
06845       ast_queue_log(queuename, "CLI", interface, "REMOVEMEMBER", "%s", "");
06846       ast_cli(a->fd, "Removed interface '%s' from queue '%s'\n", interface, queuename);
06847       return CLI_SUCCESS;
06848    case RES_EXISTS:
06849       ast_cli(a->fd, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
06850       return CLI_FAILURE;
06851    case RES_NOSUCHQUEUE:
06852       ast_cli(a->fd, "Unable to remove interface from queue '%s': No such queue\n", queuename);
06853       return CLI_FAILURE;
06854    case RES_OUTOFMEMORY:
06855       ast_cli(a->fd, "Out of memory\n");
06856       return CLI_FAILURE;
06857    case RES_NOT_DYNAMIC:
06858       ast_cli(a->fd, "Unable to remove interface '%s' from queue '%s': Member is not dynamic\n", interface, queuename);
06859       return CLI_FAILURE;
06860    default:
06861       return CLI_FAILURE;
06862    }
06863 }
06864 
06865 static char *complete_queue_pause_member(const char *line, const char *word, int pos, int state)
06866 {
06867    /* 0 - queue; 1 - pause; 2 - member; 3 - <interface>; 4 - queue; 5 - <queue>; 6 - reason; 7 - <reason> */
06868    switch (pos) {
06869    case 3:  /* Don't attempt to complete name of interface (infinite possibilities) */
06870       return NULL;
06871    case 4:  /* only one possible match, "queue" */
06872       return state == 0 ? ast_strdup("queue") : NULL;
06873    case 5:  /* <queue> */
06874       return complete_queue(line, word, pos, state);
06875    case 6: /* "reason" */
06876       return state == 0 ? ast_strdup("reason") : NULL;
06877    case 7: /* Can't autocomplete a reason, since it's 100% customizeable */
06878       return NULL;
06879    default:
06880       return NULL;
06881    }
06882 }
06883 
06884 static char *handle_queue_pause_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06885 {
06886    char *queuename, *interface, *reason;
06887    int paused;
06888 
06889    switch (cmd) {
06890    case CLI_INIT:
06891       e->command = "queue {pause|unpause} member";
06892       e->usage = 
06893          "Usage: queue {pause|unpause} member <member> [queue <queue> [reason <reason>]]\n"
06894          "  Pause or unpause a queue member. Not specifying a particular queue\n"
06895          "  will pause or unpause a member across all queues to which the member\n"
06896          "  belongs.\n";
06897       return NULL;
06898    case CLI_GENERATE:
06899       return complete_queue_pause_member(a->line, a-> word, a->pos, a->n);
06900    }
06901 
06902    if (a->argc < 4 || a->argc == 5 || a->argc == 7 || a->argc > 8) {
06903       return CLI_SHOWUSAGE;
06904    } else if (a->argc >= 5 && strcmp(a->argv[4], "queue")) {
06905       return CLI_SHOWUSAGE;
06906    } else if (a->argc == 8 && strcmp(a->argv[6], "reason")) {
06907       return CLI_SHOWUSAGE;
06908    }
06909 
06910 
06911    interface = a->argv[3];
06912    queuename = a->argc >= 6 ? a->argv[5] : NULL;
06913    reason = a->argc == 8 ? a->argv[7] : NULL;
06914    paused = !strcasecmp(a->argv[1], "pause");
06915 
06916    if (set_member_paused(queuename, interface, reason, paused) == RESULT_SUCCESS) {
06917       ast_cli(a->fd, "%spaused interface '%s'", paused ? "" : "un", interface);
06918       if (!ast_strlen_zero(queuename))
06919          ast_cli(a->fd, " in queue '%s'", queuename);
06920       if (!ast_strlen_zero(reason))
06921          ast_cli(a->fd, " for reason '%s'", reason);
06922       ast_cli(a->fd, "\n");
06923       return CLI_SUCCESS;
06924    } else {
06925       ast_cli(a->fd, "Unable to %spause interface '%s'", paused ? "" : "un", interface);
06926       if (!ast_strlen_zero(queuename))
06927          ast_cli(a->fd, " in queue '%s'", queuename);
06928       if (!ast_strlen_zero(reason))
06929          ast_cli(a->fd, " for reason '%s'", reason);
06930       ast_cli(a->fd, "\n");
06931       return CLI_FAILURE;
06932    }
06933 }
06934 
06935 static char *complete_queue_set_member_penalty(const char *line, const char *word, int pos, int state)
06936 {
06937    /* 0 - queue; 1 - set; 2 - penalty; 3 - <penalty>; 4 - on; 5 - <member>; 6 - in; 7 - <queue>;*/
06938    switch (pos) {
06939    case 4:
06940       if (state == 0) {
06941          return ast_strdup("on");
06942       } else {
06943          return NULL;
06944       }
06945    case 6:
06946       if (state == 0) {
06947          return ast_strdup("in");
06948       } else {
06949          return NULL;
06950       }
06951    case 7:
06952       return complete_queue(line, word, pos, state);
06953    default:
06954       return NULL;
06955    }
06956 }
06957  
06958 static char *handle_queue_set_member_penalty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06959 {
06960    char *queuename = NULL, *interface;
06961    int penalty = 0;
06962 
06963    switch (cmd) {
06964    case CLI_INIT:
06965       e->command = "queue set penalty";
06966       e->usage = 
06967       "Usage: queue set penalty <penalty> on <interface> [in <queue>]\n"
06968       "  Set a member's penalty in the queue specified. If no queue is specified\n"
06969       "  then that interface's penalty is set in all queues to which that interface is a member\n";
06970       return NULL;
06971    case CLI_GENERATE:
06972       return complete_queue_set_member_penalty(a->line, a->word, a->pos, a->n);
06973    }
06974 
06975    if (a->argc != 6 && a->argc != 8) {
06976       return CLI_SHOWUSAGE;
06977    } else if (strcmp(a->argv[4], "on") || (a->argc > 6 && strcmp(a->argv[6], "in"))) {
06978       return CLI_SHOWUSAGE;
06979    }
06980 
06981    if (a->argc == 8)
06982       queuename = a->argv[7];
06983    interface = a->argv[5];
06984    penalty = atoi(a->argv[3]);
06985 
06986    switch (set_member_penalty(queuename, interface, penalty)) {
06987    case RESULT_SUCCESS:
06988       ast_cli(a->fd, "Set penalty on interface '%s' from queue '%s'\n", interface, queuename);
06989       return CLI_SUCCESS;
06990    case RESULT_FAILURE:
06991       ast_cli(a->fd, "Failed to set penalty on interface '%s' from queue '%s'\n", interface, queuename);
06992       return CLI_FAILURE;
06993    default:
06994       return CLI_FAILURE;
06995    }
06996 }
06997 
06998 static char *complete_queue_rule_show(const char *line, const char *word, int pos, int state) 
06999 {
07000    int which = 0;
07001    struct rule_list *rl_iter;
07002    int wordlen = strlen(word);
07003    char *ret = NULL;
07004    if (pos != 3) /* Wha? */ {
07005       return NULL;
07006    }
07007 
07008    AST_LIST_LOCK(&rule_lists);
07009    AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
07010       if (!strncasecmp(word, rl_iter->name, wordlen) && ++which > state) {
07011          ret = ast_strdup(rl_iter->name);
07012          break;
07013       }
07014    }
07015    AST_LIST_UNLOCK(&rule_lists);
07016 
07017    return ret;
07018 }
07019 
07020 static char *handle_queue_rule_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07021 {
07022    char *rule;
07023    struct rule_list *rl_iter;
07024    struct penalty_rule *pr_iter;
07025    switch (cmd) {
07026    case CLI_INIT:
07027       e->command = "queue show rules";
07028       e->usage =
07029       "Usage: queue show rules [rulename]\n"
07030       "  Show the list of rules associated with rulename. If no\n"
07031       "  rulename is specified, list all rules defined in queuerules.conf\n";
07032       return NULL;
07033    case CLI_GENERATE:
07034       return complete_queue_rule_show(a->line, a->word, a->pos, a->n);
07035    }
07036 
07037    if (a->argc != 3 && a->argc != 4)
07038       return CLI_SHOWUSAGE;
07039 
07040    rule = a->argc == 4 ? a->argv[3] : "";
07041    AST_LIST_LOCK(&rule_lists);
07042    AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
07043       if (ast_strlen_zero(rule) || !strcasecmp(rl_iter->name, rule)) {
07044          ast_cli(a->fd, "Rule: %s\n", rl_iter->name);
07045          AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
07046             ast_cli(a->fd, "\tAfter %d seconds, adjust QUEUE_MAX_PENALTY %s %d and adjust QUEUE_MIN_PENALTY %s %d\n", pr_iter->time, pr_iter->max_relative ? "by" : "to", pr_iter->max_value, pr_iter->min_relative ? "by" : "to", pr_iter->min_value);
07047          }
07048       }
07049    }
07050    AST_LIST_UNLOCK(&rule_lists);
07051    return CLI_SUCCESS; 
07052 }
07053 
07054 static char *handle_queue_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07055 {
07056    struct ast_flags mask = {QUEUE_RESET_STATS,};
07057    int i;
07058 
07059    switch (cmd) {
07060       case CLI_INIT:
07061          e->command = "queue reset stats";
07062          e->usage =
07063             "Usage: queue reset stats [<queuenames>]\n"
07064             "\n"
07065             "Issuing this command will reset statistics for\n"
07066             "<queuenames>, or for all queues if no queue is\n"
07067             "specified.\n";
07068          return NULL;
07069       case CLI_GENERATE:
07070          if (a->pos >= 3) {
07071             return complete_queue(a->line, a->word, a->pos, a->n);
07072          } else {
07073             return NULL;
07074          }
07075    }
07076 
07077    if (a->argc < 3) {
07078       return CLI_SHOWUSAGE;
07079    }
07080 
07081    if (a->argc == 3) {
07082       reload_handler(1, &mask, NULL);
07083       return CLI_SUCCESS;
07084    }
07085 
07086    for (i = 3; i < a->argc; ++i) {
07087       reload_handler(1, &mask, a->argv[i]);
07088    }
07089 
07090    return CLI_SUCCESS;
07091 }
07092 
07093 static char *handle_queue_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07094 {
07095    struct ast_flags mask = {0,};
07096    int i;
07097 
07098    switch (cmd) {
07099       case CLI_INIT:
07100          e->command = "queue reload {parameters|members|rules|all}";
07101          e->usage =
07102             "Usage: queue reload {parameters|members|rules|all} [<queuenames>]\n"
07103             "Reload queues. If <queuenames> are specified, only reload information pertaining\n"
07104             "to <queuenames>. One of 'parameters,' 'members,' 'rules,' or 'all' must be\n"
07105             "specified in order to know what information to reload. Below is an explanation\n"
07106             "of each of these qualifiers.\n"
07107             "\n"
07108             "\t'members' - reload queue members from queues.conf\n"
07109             "\t'parameters' - reload all queue options except for queue members\n"
07110             "\t'rules' - reload the queuerules.conf file\n"
07111             "\t'all' - reload queue rules, parameters, and members\n"
07112             "\n"
07113             "Note: the 'rules' qualifier here cannot actually be applied to a specific queue.\n"
07114             "Use of the 'rules' qualifier causes queuerules.conf to be reloaded. Even if only\n"
07115             "one queue is specified when using this command, reloading queue rules may cause\n"
07116             "other queues to be affected\n";
07117          return NULL;
07118       case CLI_GENERATE:
07119          if (a->pos >= 3) {
07120             return complete_queue(a->line, a->word, a->pos, a->n);
07121          } else {
07122             return NULL;
07123          }
07124    }
07125 
07126    if (a->argc < 3)
07127       return CLI_SHOWUSAGE;
07128 
07129    if (!strcasecmp(a->argv[2], "rules")) {
07130       ast_set_flag(&mask, QUEUE_RELOAD_RULES);
07131    } else if (!strcasecmp(a->argv[2], "members")) {
07132       ast_set_flag(&mask, QUEUE_RELOAD_MEMBER);
07133    } else if (!strcasecmp(a->argv[2], "parameters")) {
07134       ast_set_flag(&mask, QUEUE_RELOAD_PARAMETERS);
07135    } else if (!strcasecmp(a->argv[2], "all")) {
07136       ast_set_flag(&mask, AST_FLAGS_ALL);
07137    }
07138 
07139    if (a->argc == 3) {
07140       reload_handler(1, &mask, NULL);
07141       return CLI_SUCCESS;
07142    }
07143 
07144    for (i = 3; i < a->argc; ++i) {
07145       reload_handler(1, &mask, a->argv[i]);
07146    }
07147 
07148    return CLI_SUCCESS;
07149 }
07150 
07151 static const char qpm_cmd_usage[] = 
07152 "Usage: queue pause member <channel> in <queue> reason <reason>\n";
07153 
07154 static const char qum_cmd_usage[] =
07155 "Usage: queue unpause member <channel> in <queue> reason <reason>\n";
07156 
07157 static const char qsmp_cmd_usage[] =
07158 "Usage: queue set member penalty <channel> from <queue> <penalty>\n";
07159 
07160 static struct ast_cli_entry cli_queue[] = {
07161    AST_CLI_DEFINE(queue_show, "Show status of a specified queue"),
07162    AST_CLI_DEFINE(handle_queue_add_member, "Add a channel to a specified queue"),
07163    AST_CLI_DEFINE(handle_queue_remove_member, "Removes a channel from a specified queue"),
07164    AST_CLI_DEFINE(handle_queue_pause_member, "Pause or unpause a queue member"),
07165    AST_CLI_DEFINE(handle_queue_set_member_penalty, "Set penalty for a channel of a specified queue"),
07166    AST_CLI_DEFINE(handle_queue_rule_show, "Show the rules defined in queuerules.conf"),
07167    AST_CLI_DEFINE(handle_queue_reload, "Reload queues, members, queue rules, or parameters"),
07168    AST_CLI_DEFINE(handle_queue_reset, "Reset statistics for a queue"),
07169 };
07170 
07171 static int unload_module(void)
07172 {
07173    int res;
07174    struct ast_context *con;
07175    struct ao2_iterator q_iter;
07176    struct call_queue *q = NULL;
07177 
07178    ast_cli_unregister_multiple(cli_queue, ARRAY_LEN(cli_queue));
07179    res = ast_manager_unregister("QueueStatus");
07180    res |= ast_manager_unregister("Queues");
07181    res |= ast_manager_unregister("QueueRule");
07182    res |= ast_manager_unregister("QueueSummary");
07183    res |= ast_manager_unregister("QueueAdd");
07184    res |= ast_manager_unregister("QueueRemove");
07185    res |= ast_manager_unregister("QueuePause");
07186    res |= ast_manager_unregister("QueueLog");
07187    res |= ast_manager_unregister("QueuePenalty");
07188    res |= ast_unregister_application(app_aqm);
07189    res |= ast_unregister_application(app_rqm);
07190    res |= ast_unregister_application(app_pqm);
07191    res |= ast_unregister_application(app_upqm);
07192    res |= ast_unregister_application(app_ql);
07193    res |= ast_unregister_application(app);
07194    res |= ast_custom_function_unregister(&queuevar_function);
07195    res |= ast_custom_function_unregister(&queuemembercount_function);
07196    res |= ast_custom_function_unregister(&queuemembercount_dep);
07197    res |= ast_custom_function_unregister(&queuememberlist_function);
07198    res |= ast_custom_function_unregister(&queuewaitingcount_function);
07199    res |= ast_custom_function_unregister(&queuememberpenalty_function);
07200 
07201    if (device_state_sub)
07202       ast_event_unsubscribe(device_state_sub);
07203 
07204    if ((con = ast_context_find("app_queue_gosub_virtual_context"))) {
07205       ast_context_remove_extension2(con, "s", 1, NULL, 0);
07206       ast_context_destroy(con, "app_queue"); /* leave no trace */
07207    }
07208 
07209    q_iter = ao2_iterator_init(queues, 0);
07210    while ((q = ao2_t_iterator_next(&q_iter, "Iterate through queues"))) {
07211       queues_t_unlink(queues, q, "Remove queue from container due to unload");
07212       queue_t_unref(q, "Done with iterator");
07213    }
07214    ao2_iterator_destroy(&q_iter);
07215    ao2_ref(queues, -1);
07216    devicestate_tps = ast_taskprocessor_unreference(devicestate_tps);
07217    ast_unload_realtime("queue_members");
07218    return res;
07219 }
07220 
07221 static int load_module(void)
07222 {
07223    int res;
07224    struct ast_context *con;
07225    struct ast_flags mask = {AST_FLAGS_ALL, };
07226 
07227    queues = ao2_container_alloc(MAX_QUEUE_BUCKETS, queue_hash_cb, queue_cmp_cb);
07228 
07229    use_weight = 0;
07230 
07231    if (reload_handler(0, &mask, NULL))
07232       return AST_MODULE_LOAD_DECLINE;
07233 
07234    con = ast_context_find_or_create(NULL, NULL, "app_queue_gosub_virtual_context", "app_queue");
07235    if (!con)
07236       ast_log(LOG_ERROR, "Queue virtual context 'app_queue_gosub_virtual_context' does not exist and unable to create\n");
07237    else
07238       ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "app_queue");
07239 
07240    if (queue_persistent_members)
07241       reload_queue_members();
07242 
07243    ast_cli_register_multiple(cli_queue, ARRAY_LEN(cli_queue));
07244    res = ast_register_application_xml(app, queue_exec);
07245    res |= ast_register_application_xml(app_aqm, aqm_exec);
07246    res |= ast_register_application_xml(app_rqm, rqm_exec);
07247    res |= ast_register_application_xml(app_pqm, pqm_exec);
07248    res |= ast_register_application_xml(app_upqm, upqm_exec);
07249    res |= ast_register_application_xml(app_ql, ql_exec);
07250    res |= ast_manager_register("Queues", 0, manager_queues_show, "Queues");
07251    res |= ast_manager_register("QueueStatus", 0, manager_queues_status, "Queue Status");
07252    res |= ast_manager_register("QueueSummary", 0, manager_queues_summary, "Queue Summary");
07253    res |= ast_manager_register("QueueAdd", EVENT_FLAG_AGENT, manager_add_queue_member, "Add interface to queue.");
07254    res |= ast_manager_register("QueueRemove", EVENT_FLAG_AGENT, manager_remove_queue_member, "Remove interface from queue.");
07255    res |= ast_manager_register("QueuePause", EVENT_FLAG_AGENT, manager_pause_queue_member, "Makes a queue member temporarily unavailable");
07256    res |= ast_manager_register("QueueLog", EVENT_FLAG_AGENT, manager_queue_log_custom, "Adds custom entry in queue_log");
07257    res |= ast_manager_register("QueuePenalty", EVENT_FLAG_AGENT, manager_queue_member_penalty, "Set the penalty for a queue member"); 
07258    res |= ast_manager_register("QueueRule", 0, manager_queue_rule_show, "Queue Rules");
07259    res |= ast_manager_register("QueueReload", 0, manager_queue_reload, "Reload a queue, queues, or any sub-section of a queue or queues");
07260    res |= ast_manager_register("QueueReset", 0, manager_queue_reset, "Reset queue statistics");
07261    res |= ast_custom_function_register(&queuevar_function);
07262    res |= ast_custom_function_register(&queuemembercount_function);
07263    res |= ast_custom_function_register(&queuemembercount_dep);
07264    res |= ast_custom_function_register(&queuememberlist_function);
07265    res |= ast_custom_function_register(&queuewaitingcount_function);
07266    res |= ast_custom_function_register(&queuememberpenalty_function);
07267 
07268    if (!(devicestate_tps = ast_taskprocessor_get("app_queue", 0))) {
07269       ast_log(LOG_WARNING, "devicestate taskprocessor reference failed - devicestate notifications will not occur\n");
07270    }
07271 
07272    if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL, AST_EVENT_IE_END))) {
07273       res = -1;
07274    }
07275 
07276    ast_realtime_require_field("queue_members", "paused", RQ_INTEGER1, 1, "uniqueid", RQ_UINTEGER2, 5, SENTINEL);
07277 
07278    return res ? AST_MODULE_LOAD_DECLINE : 0;
07279 }
07280 
07281 static int reload(void)
07282 {
07283    struct ast_flags mask = {AST_FLAGS_ALL,};
07284    ast_unload_realtime("queue_members");
07285    reload_handler(1, &mask, NULL);
07286    return 0;
07287 }
07288 
07289 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "True Call Queueing",
07290       .load = load_module,
07291       .unload = unload_module,
07292       .reload = reload,
07293           );
07294 

Generated on Wed Aug 11 01:05:00 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9