Advertisement
Guest User

Untitled

a guest
Nov 24th, 2010
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 243.58 KB | None | 0 0
  1. /*
  2.  * Asterisk -- An open source telephony toolkit.
  3.  *
  4.  * Copyright (C) 1999 - 2006, Digium, Inc.
  5.  *
  6.  * Mark Spencer <[email protected]>
  7.  *
  8.  * See http://www.asterisk.org for more information about
  9.  * the Asterisk project. Please do not directly contact
  10.  * any of the maintainers of this project for assistance;
  11.  * the project provides a web site, mailing lists and IRC
  12.  * channels for your use.
  13.  *
  14.  * This program is free software, distributed under the terms of
  15.  * the GNU General Public License Version 2. See the LICENSE file
  16.  * at the top of the source tree.
  17.  */
  18.  
  19. /*! \file
  20.  *
  21.  * \brief True call queues with optional send URL on answer
  22.  *
  23.  * \author Mark Spencer <[email protected]>
  24.  *
  25.  * \arg Config in \ref Config_qu queues.conf
  26.  *
  27.  * \par Development notes
  28.  * \note 2004-11-25: Persistent Dynamic Members added by:
  29.  *             NetNation Communications (www.netnation.com)
  30.  *             Kevin Lindsay <[email protected]>
  31.  *
  32.  *             Each dynamic agent in each queue is now stored in the astdb.
  33.  *             When asterisk is restarted, each agent will be automatically
  34.  *             readded into their recorded queues. This feature can be
  35.  *             configured with the 'persistent_members=<1|0>' setting in the
  36.  *             '[general]' category in queues.conf. The default is on.
  37.  *
  38.  * \note 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
  39.  *
  40.  * \note These features added by David C. Troy <[email protected]>:
  41.  *    - Per-queue holdtime calculation
  42.  *    - Estimated holdtime announcement
  43.  *    - Position announcement
  44.  *    - Abandoned/completed call counters
  45.  *    - Failout timer passed as optional app parameter
  46.  *    - Optional monitoring of calls, started when call is answered
  47.  *
  48.  * Patch Version 1.07 2003-12-24 01
  49.  *
  50.  * Added servicelevel statistic by Michiel Betel <[email protected]>
  51.  * Added Priority jumping code for adding and removing queue members by Jonathan Stanton <[email protected]>
  52.  *
  53.  * Fixed to work with CVS as of 2004-02-25 and released as 1.07a
  54.  * by Matthew Enger <[email protected]>
  55.  *
  56.  * \ingroup applications
  57.  */
  58.  
  59. /*** MODULEINFO
  60.     <depend>res_monitor</depend>
  61.  ***/
  62.  
  63. #include "asterisk.h"
  64.  
  65. ASTERISK_FILE_VERSION(__FILE__, "$Revision: 287387 $")
  66.  
  67. #include <sys/time.h>
  68. #include <sys/signal.h>
  69. #include <netinet/in.h>
  70. #include <ctype.h>
  71.  
  72. #include "asterisk/lock.h"
  73. #include "asterisk/file.h"
  74. #include "asterisk/channel.h"
  75. #include "asterisk/pbx.h"
  76. #include "asterisk/app.h"
  77. #include "asterisk/linkedlists.h"
  78. #include "asterisk/module.h"
  79. #include "asterisk/translate.h"
  80. #include "asterisk/say.h"
  81. #include "asterisk/features.h"
  82. #include "asterisk/musiconhold.h"
  83. #include "asterisk/cli.h"
  84. #include "asterisk/manager.h"
  85. #include "asterisk/config.h"
  86. #include "asterisk/monitor.h"
  87. #include "asterisk/utils.h"
  88. #include "asterisk/causes.h"
  89. #include "asterisk/astdb.h"
  90. #include "asterisk/devicestate.h"
  91. #include "asterisk/stringfields.h"
  92. #include "asterisk/event.h"
  93. #include "asterisk/astobj2.h"
  94. #include "asterisk/strings.h"
  95. #include "asterisk/global_datastores.h"
  96. #include "asterisk/taskprocessor.h"
  97.  
  98. /* Define, to debug reference counts on queues, without debugging reference counts on queue members */
  99. /* #define REF_DEBUG_ONLY_QUEUES */
  100.  
  101. /*!
  102.  * \par Please read before modifying this file.
  103.  * There are three locks which are regularly used
  104.  * throughout this file, the queue list lock, the lock
  105.  * for each individual queue, and the interface list lock.
  106.  * Please be extra careful to always lock in the following order
  107.  * 1) queue list lock
  108.  * 2) individual queue lock
  109.  * 3) interface list lock
  110.  * This order has sort of "evolved" over the lifetime of this
  111.  * application, but it is now in place this way, so please adhere
  112.  * to this order!
  113.  */
  114.  
  115. /*** DOCUMENTATION
  116.     <application name="Queue" language="en_US">
  117.         <synopsis>
  118.             Queue a call for a call queue.
  119.         </synopsis>
  120.         <syntax>
  121.             <parameter name="queuename" required="true" />
  122.             <parameter name="options">
  123.                 <optionlist>
  124.                     <option name="C">
  125.                         <para>Mark all calls as "answered elsewhere" when cancelled.</para>
  126.                     </option>
  127.                     <option name="c">
  128.                         <para>Continue in the dialplan if the callee hangs up.</para>
  129.                     </option>
  130.                     <option name="d">
  131.                         <para>data-quality (modem) call (minimum delay).</para>
  132.                     </option>
  133.                     <option name="h">
  134.                         <para>Allow <emphasis>callee</emphasis> to hang up by pressing <literal>*</literal>.</para>
  135.                     </option>
  136.                     <option name="H">
  137.                         <para>Allow <emphasis>caller</emphasis> to hang up by pressing <literal>*</literal>.</para>
  138.                     </option>
  139.                     <option name="n">
  140.                         <para>No retries on the timeout; will exit this application and
  141.                         go to the next step.</para>
  142.                     </option>
  143.                     <option name="i">
  144.                         <para>Ignore call forward requests from queue members and do nothing
  145.                         when they are requested.</para>
  146.                     </option>
  147.                     <option name="r">
  148.                         <para>Ring instead of playing MOH. Periodic Announcements are still made, if applicable.</para>
  149.                     </option>
  150.                     <option name="t">
  151.                         <para>Allow the <emphasis>called</emphasis> user to transfer the calling user.</para>
  152.                     </option>
  153.                     <option name="T">
  154.                         <para>Allow the <emphasis>calling</emphasis> user to transfer the call.</para>
  155.                     </option>
  156.                     <option name="w">
  157.                         <para>Allow the <emphasis>called</emphasis> user to write the conversation to
  158.                         disk via Monitor.</para>
  159.                     </option>
  160.                     <option name="W">
  161.                         <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
  162.                         disk via Monitor.</para>
  163.                     </option>
  164.                     <option name="k">
  165.                         <para>Allow the <emphasis>called</emphasis> party to enable parking of the call by sending
  166.                         the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  167.                     </option>
  168.                     <option name="K">
  169.                         <para>Allow the <emphasis>calling</emphasis> party to enable parking of the call by sending
  170.                         the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  171.                     </option>
  172.                     <option name="x">
  173.                         <para>Allow the <emphasis>called</emphasis> user to write the conversation
  174.                         to disk via MixMonitor.</para>
  175.                     </option>
  176.                     <option name="X">
  177.                         <para>Allow the <emphasis>calling</emphasis> user to write the conversation to
  178.                         disk via MixMonitor.</para>
  179.                     </option>
  180.                 </optionlist>
  181.             </parameter>
  182.             <parameter name="URL">
  183.                 <para><replaceable>URL</replaceable> will be sent to the called party if the channel supports it.</para>
  184.             </parameter>
  185.             <parameter name="announceoverride" />
  186.             <parameter name="timeout">
  187.                 <para>Will cause the queue to fail out after a specified number of
  188.                 seconds, checked between each <filename>queues.conf</filename> <replaceable>timeout</replaceable> and
  189.                 <replaceable>retry</replaceable> cycle.</para>
  190.             </parameter>
  191.             <parameter name="AGI">
  192.                 <para>Will setup an AGI script to be executed on the calling party's channel once they are
  193.                 connected to a queue member.</para>
  194.             </parameter>
  195.             <parameter name="macro">
  196.                 <para>Will run a macro on the calling party's channel once they are connected to a queue member.</para>
  197.             </parameter>
  198.             <parameter name="gosub">
  199.                 <para>Will run a gosub on the calling party's channel once they are connected to a queue member.</para>
  200.             </parameter>
  201.             <parameter name="rule">
  202.                 <para>Will cause the queue's defaultrule to be overridden by the rule specified.</para>
  203.             </parameter>
  204.         </syntax>
  205.         <description>
  206.             <para>In addition to transferring the call, a call may be parked and then picked
  207.             up by another user.</para>
  208.             <para>This application will return to the dialplan if the queue does not exist, or
  209.             any of the join options cause the caller to not enter the queue.</para>
  210.             <para>This application sets the following channel variable upon completion:</para>
  211.             <variablelist>
  212.                 <variable name="QUEUESTATUS">
  213.                     <para>The status of the call as a text string.</para>
  214.                     <value name="TIMEOUT" />
  215.                     <value name="FULL" />
  216.                     <value name="JOINEMPTY" />
  217.                     <value name="LEAVEEMPTY" />
  218.                     <value name="JOINUNAVAIL" />
  219.                     <value name="LEAVEUNAVAIL" />
  220.                     <value name="CONTINUE" />
  221.                 </variable>
  222.             </variablelist>
  223.         </description>
  224.         <see-also>
  225.             <ref type="application">AddQueueMember</ref>
  226.             <ref type="application">RemoveQueueMember</ref>
  227.             <ref type="application">PauseQueueMember</ref>
  228.             <ref type="application">UnpauseQueueMember</ref>
  229.             <ref type="application">AgentLogin</ref>
  230.             <ref type="function">QUEUE_MEMBER_COUNT</ref>
  231.             <ref type="function">QUEUE_MEMBER_LIST</ref>
  232.             <ref type="function">QUEUE_WAITING_COUNT</ref>
  233.         </see-also>
  234.     </application>
  235.     <application name="AddQueueMember" language="en_US">
  236.         <synopsis>
  237.             Dynamically adds queue members.
  238.         </synopsis>
  239.         <syntax>
  240.             <parameter name="queuename" required="true" />
  241.             <parameter name="interface" />
  242.             <parameter name="penalty" />
  243.             <parameter name="options" />
  244.             <parameter name="membername" />
  245.             <parameter name="stateinterface" />
  246.         </syntax>
  247.         <description>
  248.             <para>Dynamically adds interface to an existing queue. If the interface is
  249.             already in the queue it will return an error.</para>
  250.             <para>This application sets the following channel variable upon completion:</para>
  251.             <variablelist>
  252.                 <variable name="AQMSTATUS">
  253.                     <para>The status of the attempt to add a queue member as a text string.</para>
  254.                     <value name="ADDED" />
  255.                     <value name="MEMBERALREADY" />
  256.                     <value name="NOSUCHQUEUE" />
  257.                 </variable>
  258.             </variablelist>
  259.         </description>
  260.         <see-also>
  261.             <ref type="application">RemoveQueueMember</ref>
  262.             <ref type="application">PauseQueueMember</ref>
  263.             <ref type="application">UnpauseQueueMember</ref>
  264.             <ref type="application">AgentLogin</ref>
  265.         </see-also>
  266.     </application>
  267.     <application name="RemoveQueueMember" language="en_US">
  268.         <synopsis>
  269.             Dynamically removes queue members.
  270.         </synopsis>
  271.         <syntax>
  272.             <parameter name="queuename" required="true" />
  273.             <parameter name="interface" />
  274.             <parameter name="options" />
  275.         </syntax>
  276.         <description>
  277.             <para>If the interface is <emphasis>NOT</emphasis> in the queue it will return an error.</para>
  278.             <para>This application sets the following channel variable upon completion:</para>
  279.             <variablelist>
  280.                 <variable name="RQMSTATUS">
  281.                     <value name="REMOVED" />
  282.                     <value name="NOTINQUEUE" />
  283.                     <value name="NOSUCHQUEUE" />
  284.                 </variable>
  285.             </variablelist>
  286.             <para>Example: RemoveQueueMember(techsupport,SIP/3000)</para>
  287.         </description>
  288.         <see-also>
  289.             <ref type="application">Queue</ref>
  290.             <ref type="application">AddQueueMember</ref>
  291.             <ref type="application">PauseQueueMember</ref>
  292.             <ref type="application">UnpauseQueueMember</ref>
  293.         </see-also>
  294.     </application>
  295.     <application name="PauseQueueMember" language="en_US">
  296.         <synopsis>
  297.             Pauses a queue member.
  298.         </synopsis>
  299.         <syntax>
  300.             <parameter name="queuename" />
  301.             <parameter name="interface" required="true" />
  302.             <parameter name="options" />
  303.             <parameter name="reason">
  304.                 <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
  305.             </parameter>
  306.         </syntax>
  307.         <description>
  308.             <para>Pauses (blocks calls for) a queue member. The given interface will be paused in the given queue.
  309.             This prevents any calls from being sent from the queue to the interface until it is
  310.             unpaused with UnpauseQueueMember or the manager interface.  If no queuename is given,
  311.             the interface is paused in every queue it is a member of. The application will fail if the
  312.             interface is not found.</para>
  313.             <para>This application sets the following channel variable upon completion:</para>
  314.             <variablelist>
  315.                 <variable name="PQMSTATUS">
  316.                     <para>The status of the attempt to pause a queue member as a text string.</para>
  317.                     <value name="PAUSED" />
  318.                     <value name="NOTFOUND" />
  319.                 </variable>
  320.             </variablelist>
  321.             <para>Example: PauseQueueMember(,SIP/3000)</para>
  322.         </description>
  323.         <see-also>
  324.             <ref type="application">UnpauseQueueMember</ref>
  325.         </see-also>
  326.     </application>
  327.     <application name="UnpauseQueueMember" language="en_US">
  328.         <synopsis>
  329.             Unpauses a queue member.       
  330.         </synopsis>
  331.         <syntax>
  332.             <parameter name="queuename" />
  333.             <parameter name="interface" required="true" />
  334.             <parameter name="options" />
  335.             <parameter name="reason">
  336.                 <para>Is used to add extra information to the appropriate queue_log entries and manager events.</para>
  337.             </parameter>
  338.         </syntax>
  339.         <description>
  340.             <para>Unpauses (resumes calls to) a queue member. This is the counterpart to <literal>PauseQueueMember()</literal>
  341.             and operates exactly the same way, except it unpauses instead of pausing the given interface.</para>
  342.             <para>This application sets the following channel variable upon completion:</para>
  343.             <variablelist>
  344.                 <variable name="UPQMSTATUS">
  345.                     <para>The status of the attempt to unpause a queue member as a text string.</para>
  346.                     <value name="UNPAUSED" />
  347.                     <value name="NOTFOUND" />
  348.                 </variable>
  349.             </variablelist>
  350.             <para>Example: UnpauseQueueMember(,SIP/3000)</para>
  351.         </description>
  352.         <see-also>
  353.             <ref type="application">PauseQueueMember</ref>
  354.         </see-also>
  355.     </application>
  356.     <application name="QueueLog" language="en_US">
  357.         <synopsis>
  358.             Writes to the queue_log file.
  359.         </synopsis>
  360.         <syntax>
  361.             <parameter name="queuename" required="true" />
  362.             <parameter name="uniqueid" required="true" />
  363.             <parameter name="agent" required="true" />
  364.             <parameter name="event" required="true" />
  365.             <parameter name="additionalinfo" />
  366.         </syntax>
  367.         <description>
  368.             <para>Allows you to write your own events into the queue log.</para>
  369.             <para>Example: QueueLog(101,${UNIQUEID},${AGENT},WENTONBREAK,600)</para>
  370.         </description>
  371.         <see-also>
  372.             <ref type="application">Queue</ref>
  373.         </see-also>
  374.     </application>
  375.     <function name="QUEUE_VARIABLES" language="en_US">
  376.         <synopsis>
  377.             Return Queue information in variables.
  378.         </synopsis>
  379.         <syntax>
  380.             <parameter name="queuename" required="true">
  381.                 <enumlist>
  382.                     <enum name="QUEUEMAX">
  383.                         <para>Maxmimum number of calls allowed.</para>
  384.                     </enum>
  385.                     <enum name="QUEUESTRATEGY">
  386.                         <para>The strategy of the queue.</para>
  387.                     </enum>
  388.                     <enum name="QUEUECALLS">
  389.                         <para>Number of calls currently in the queue.</para>
  390.                     </enum>
  391.                     <enum name="QUEUEHOLDTIME">
  392.                         <para>Current average hold time.</para>
  393.                     </enum>
  394.                     <enum name="QUEUECOMPLETED">
  395.                         <para>Number of completed calls for the queue.</para>
  396.                     </enum>
  397.                     <enum name="QUEUEABANDONED">
  398.                         <para>Number of abandoned calls.</para>
  399.                     </enum>
  400.                     <enum name="QUEUESRVLEVEL">
  401.                         <para>Queue service level.</para>
  402.                     </enum>
  403.                     <enum name="QUEUESRVLEVELPERF">
  404.                         <para>Current service level performance.</para>
  405.                     </enum>
  406.                 </enumlist>
  407.             </parameter>
  408.         </syntax>
  409.         <description>
  410.             <para>Makes the following queue variables available.</para>
  411.             <para>Returns <literal>0</literal> if queue is found and setqueuevar is defined, <literal>-1</literal> otherwise.</para>
  412.         </description>
  413.     </function>
  414.     <function name="QUEUE_MEMBER" language="en_US">
  415.         <synopsis>
  416.             Count number of members answering a queue.
  417.         </synopsis>
  418.         <syntax>
  419.             <parameter name="queuename" required="true" />
  420.             <parameter name="option" required="true">
  421.                 <enumlist>
  422.                     <enum name="logged">
  423.                         <para>Returns the number of logged-in members for the specified queue.</para>
  424.                     </enum>
  425.                     <enum name="free">
  426.                         <para>Returns the number of logged-in members for the specified queue available to take a call.</para>
  427.                     </enum>
  428.                     <enum name="count">
  429.                         <para>Returns the total number of members for the specified queue.</para>
  430.                     </enum>
  431.                 </enumlist>
  432.             </parameter>
  433.         </syntax>
  434.         <description>
  435.             <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
  436.         </description>
  437.     </function>
  438.     <function name="QUEUE_MEMBER_COUNT" language="en_US">
  439.         <synopsis>
  440.             Count number of members answering a queue.
  441.         </synopsis>
  442.         <syntax>
  443.             <parameter name="queuename" required="true" />
  444.         </syntax>
  445.         <description>
  446.             <para>Returns the number of members currently associated with the specified <replaceable>queuename</replaceable>.</para>
  447.             <warning><para>This function has been deprecated in favor of the <literal>QUEUE_MEMBER()</literal> function</para></warning>
  448.         </description>
  449.         <see-also>
  450.             <ref type="function">QUEUE_MEMBER_LIST</ref>
  451.         </see-also>
  452.     </function>
  453.     <function name="QUEUE_WAITING_COUNT" language="en_US">
  454.         <synopsis>
  455.             Count number of calls currently waiting in a queue.
  456.         </synopsis>
  457.         <syntax>
  458.             <parameter name="queuename" />
  459.         </syntax>
  460.         <description>
  461.             <para>Returns the number of callers currently waiting in the specified <replaceable>queuename</replaceable>.</para>
  462.         </description>
  463.     </function>
  464.     <function name="QUEUE_MEMBER_LIST" language="en_US">
  465.         <synopsis>
  466.             Returns a list of interfaces on a queue.
  467.         </synopsis>
  468.         <syntax>
  469.             <parameter name="queuename" required="true" />
  470.         </syntax>
  471.         <description>
  472.             <para>Returns a comma-separated list of members associated with the specified <replaceable>queuename</replaceable>.</para>
  473.         </description>
  474.         <see-also>
  475.             <ref type="function">QUEUE_MEMBER_COUNT</ref>
  476.         </see-also>
  477.     </function>
  478.     <function name="QUEUE_MEMBER_PENALTY" language="en_US">
  479.         <synopsis>
  480.             Gets or sets queue members penalty.
  481.         </synopsis>
  482.         <syntax>
  483.             <parameter name="queuename" required="true" />
  484.             <parameter name="interface" required="true" />
  485.         </syntax>
  486.         <description>
  487.             <para>Gets or sets queue members penalty.</para>
  488.         </description>
  489.     </function>
  490.  
  491.  ***/
  492.  
  493. enum {
  494.     QUEUE_STRATEGY_RINGALL = 0,
  495.     QUEUE_STRATEGY_LEASTRECENT,
  496.     QUEUE_STRATEGY_FEWESTCALLS,
  497.     QUEUE_STRATEGY_RANDOM,
  498.     QUEUE_STRATEGY_RRMEMORY,
  499.     QUEUE_STRATEGY_LINEAR,
  500.     QUEUE_STRATEGY_WRANDOM
  501. };
  502.  
  503. enum queue_reload_mask {
  504.     QUEUE_RELOAD_PARAMETERS = (1 << 0),
  505.     QUEUE_RELOAD_MEMBER = (1 << 1),
  506.     QUEUE_RELOAD_RULES = (1 << 2),
  507.     QUEUE_RESET_STATS = (1 << 3),
  508. };
  509.  
  510. static const struct strategy {
  511.     int strategy;
  512.     const char *name;
  513. } strategies[] = {
  514.     { QUEUE_STRATEGY_RINGALL, "ringall" },
  515.     { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
  516.     { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
  517.     { QUEUE_STRATEGY_RANDOM, "random" },
  518.     { QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
  519.     { QUEUE_STRATEGY_RRMEMORY, "roundrobin" },
  520.     { QUEUE_STRATEGY_LINEAR, "linear" },
  521.     { QUEUE_STRATEGY_WRANDOM, "wrandom"},
  522. };
  523.  
  524. static struct ast_taskprocessor *devicestate_tps;
  525.  
  526. #define DEFAULT_RETRY       5
  527. #define DEFAULT_TIMEOUT     15
  528. #define RECHECK         1       /*!< Recheck every second to see we we're at the top yet */
  529. #define MAX_PERIODIC_ANNOUNCEMENTS 10           /*!< The maximum periodic announcements we can have */
  530. #define DEFAULT_MIN_ANNOUNCE_FREQUENCY 15       /*!< The minimum number of seconds between position announcements \
  531.                                                      The default value of 15 provides backwards compatibility */
  532. #define MAX_QUEUE_BUCKETS 53
  533.  
  534. #define RES_OKAY    0       /*!< Action completed */
  535. #define RES_EXISTS  (-1)        /*!< Entry already exists */
  536. #define RES_OUTOFMEMORY (-2)        /*!< Out of memory */
  537. #define RES_NOSUCHQUEUE (-3)        /*!< No such queue */
  538. #define RES_NOT_DYNAMIC (-4)        /*!< Member is not dynamic */
  539.  
  540. static char *app = "Queue";
  541.  
  542. static char *app_aqm = "AddQueueMember" ;
  543.  
  544. static char *app_rqm = "RemoveQueueMember" ;
  545.  
  546. static char *app_pqm = "PauseQueueMember" ;
  547.  
  548. static char *app_upqm = "UnpauseQueueMember" ;
  549.  
  550. static char *app_ql = "QueueLog" ;
  551.  
  552. /*! \brief Persistent Members astdb family */
  553. static const char *pm_family = "Queue/PersistentMembers";
  554. /* The maximum length of each persistent member queue database entry */
  555. #define PM_MAX_LEN 8192
  556.  
  557. /*! \brief queues.conf [general] option */
  558. static int queue_persistent_members = 0;
  559.  
  560. /*! \brief queues.conf per-queue weight option */
  561. static int use_weight = 0;
  562.  
  563. /*! \brief queues.conf [general] option */
  564. static int autofill_default = 0;
  565.  
  566. /*! \brief queues.conf [general] option */
  567. static int montype_default = 0;
  568.  
  569. /*! \brief queues.conf [general] option */
  570. static int shared_lastcall = 0;
  571.  
  572. /*! \brief Subscription to device state change events */
  573. static struct ast_event_sub *device_state_sub;
  574.  
  575. /*! \brief queues.conf [general] option */
  576. static int update_cdr = 0;
  577.  
  578. enum queue_result {
  579.     QUEUE_UNKNOWN = 0,
  580.     QUEUE_TIMEOUT = 1,
  581.     QUEUE_JOINEMPTY = 2,
  582.     QUEUE_LEAVEEMPTY = 3,
  583.     QUEUE_JOINUNAVAIL = 4,
  584.     QUEUE_LEAVEUNAVAIL = 5,
  585.     QUEUE_FULL = 6,
  586.     QUEUE_CONTINUE = 7,
  587. };
  588.  
  589. const struct {
  590.     enum queue_result id;
  591.     char *text;
  592. } queue_results[] = {
  593.     { QUEUE_UNKNOWN, "UNKNOWN" },
  594.     { QUEUE_TIMEOUT, "TIMEOUT" },
  595.     { QUEUE_JOINEMPTY,"JOINEMPTY" },
  596.     { QUEUE_LEAVEEMPTY, "LEAVEEMPTY" },
  597.     { QUEUE_JOINUNAVAIL, "JOINUNAVAIL" },
  598.     { QUEUE_LEAVEUNAVAIL, "LEAVEUNAVAIL" },
  599.     { QUEUE_FULL, "FULL" },
  600.     { QUEUE_CONTINUE, "CONTINUE" },
  601. };
  602.  
  603. enum queue_timeout_priority {
  604.     TIMEOUT_PRIORITY_APP,
  605.     TIMEOUT_PRIORITY_CONF,
  606. };
  607.  
  608. /*! \brief We define a custom "local user" structure because we
  609.  *  use it not only for keeping track of what is in use but
  610.  *  also for keeping track of who we're dialing.
  611.  *
  612.  *  There are two "links" defined in this structure, q_next and call_next.
  613.  *  q_next links ALL defined callattempt structures into a linked list. call_next is
  614.  *  a link which allows for a subset of the callattempts to be traversed. This subset
  615.  *  is used in wait_for_answer so that irrelevant callattempts are not traversed. This
  616.  *  also is helpful so that queue logs are always accurate in the case where a call to
  617.  *  a member times out, especially if using the ringall strategy.
  618. */
  619.  
  620. struct callattempt {
  621.     struct callattempt *q_next;
  622.     struct callattempt *call_next;
  623.     struct ast_channel *chan;
  624.     char interface[256];
  625.     int stillgoing;
  626.     int metric;
  627.     int oldstatus;
  628.     time_t lastcall;
  629.     struct call_queue *lastqueue;
  630.     struct member *member;
  631. };
  632.  
  633.  
  634. struct queue_ent {
  635.     struct call_queue *parent;             /*!< What queue is our parent */
  636.     char moh[80];                          /*!< Name of musiconhold to be used */
  637.     char announce[80];                     /*!< Announcement to play for member when call is answered */
  638.     char context[AST_MAX_CONTEXT];         /*!< Context when user exits queue */
  639.     char digits[AST_MAX_EXTENSION];        /*!< Digits entered while in queue */
  640.     int valid_digits;                      /*!< Digits entered correspond to valid extension. Exited */
  641.     int pos;                               /*!< Where we are in the queue */
  642.     int prio;                              /*!< Our priority */
  643.     int last_pos_said;                     /*!< Last position we told the user */
  644.     time_t last_periodic_announce_time;    /*!< The last time we played a periodic announcement */
  645.     int last_periodic_announce_sound;      /*!< The last periodic announcement we made */
  646.     time_t last_pos;                       /*!< Last time we told the user their position */
  647.     int opos;                              /*!< Where we started in the queue */
  648.     int handled;                           /*!< Whether our call was handled */
  649.     int pending;                           /*!< Non-zero if we are attempting to call a member */
  650.     int max_penalty;                       /*!< Limit the members that can take this call to this penalty or lower */
  651.     int min_penalty;                       /*!< Limit the members that can take this call to this penalty or higher */
  652.     int linpos;                            /*!< If using linear strategy, what position are we at? */
  653.     int linwrapped;                        /*!< Is the linpos wrapped? */
  654.     time_t start;                          /*!< When we started holding */
  655.     time_t expire;                         /*!< When this entry should expire (time out of queue) */
  656.     int cancel_answered_elsewhere;         /*!< Whether we should force the CAE flag on this call (C) option*/
  657.     struct ast_channel *chan;              /*!< Our channel */
  658.     AST_LIST_HEAD_NOLOCK(,penalty_rule) qe_rules; /*!< Local copy of the queue's penalty rules */
  659.     struct penalty_rule *pr;               /*!< Pointer to the next penalty rule to implement */
  660.     struct queue_ent *next;                /*!< The next queue entry */
  661. };
  662.  
  663. struct member {
  664.     char interface[80];                 /*!< Technology/Location to dial to reach this member*/
  665.     char state_interface[80];           /*!< Technology/Location from which to read devicestate changes */
  666.     char membername[80];                /*!< Member name to use in queue logs */
  667.     int penalty;                        /*!< Are we a last resort? */
  668.     int calls;                          /*!< Number of calls serviced by this member */
  669.     int dynamic;                        /*!< Are we dynamically added? */
  670.     int realtime;                       /*!< Is this member realtime? */
  671.     int status;                         /*!< Status of queue member */
  672.     int paused;                         /*!< Are we paused (not accepting calls)? */
  673.     time_t lastcall;                    /*!< When last successful call was hungup */
  674.     struct call_queue *lastqueue;       /*!< Last queue we received a call */
  675.     unsigned int dead:1;                /*!< Used to detect members deleted in realtime */
  676.     unsigned int delme:1;               /*!< Flag to delete entry on reload */
  677.     char rt_uniqueid[80];               /*!< Unique id of realtime member entry */
  678. };
  679.  
  680. enum empty_conditions {
  681.     QUEUE_EMPTY_PENALTY = (1 << 0),
  682.     QUEUE_EMPTY_PAUSED = (1 << 1),
  683.     QUEUE_EMPTY_INUSE = (1 << 2),
  684.     QUEUE_EMPTY_RINGING = (1 << 3),
  685.     QUEUE_EMPTY_UNAVAILABLE = (1 << 4),
  686.     QUEUE_EMPTY_INVALID = (1 << 5),
  687.     QUEUE_EMPTY_UNKNOWN = (1 << 6),
  688.     QUEUE_EMPTY_WRAPUP = (1 << 7),
  689. };
  690.  
  691. /* values used in multi-bit flags in call_queue */
  692. #define ANNOUNCEHOLDTIME_ALWAYS 1
  693. #define ANNOUNCEHOLDTIME_ONCE 2
  694. #define QUEUE_EVENT_VARIABLES 3
  695.  
  696. struct penalty_rule {
  697.     int time;                           /*!< Number of seconds that need to pass before applying this rule */
  698.     int max_value;                      /*!< The amount specified in the penalty rule for max penalty */
  699.     int min_value;                      /*!< The amount specified in the penalty rule for min penalty */
  700.     int max_relative;                   /*!< Is the max adjustment relative? 1 for relative, 0 for absolute */
  701.     int min_relative;                   /*!< Is the min adjustment relative? 1 for relative, 0 for absolute */
  702.     AST_LIST_ENTRY(penalty_rule) list;  /*!< Next penalty_rule */
  703. };
  704.  
  705. #define ANNOUNCEPOSITION_YES 1 /*!< We announce position */
  706. #define ANNOUNCEPOSITION_NO 2 /*!< We don't announce position */
  707. #define ANNOUNCEPOSITION_MORE_THAN 3 /*!< We say "Currently there are more than <limit>" */
  708. #define ANNOUNCEPOSITION_LIMIT 4 /*!< We not announce position more than <limit> */
  709.  
  710. struct call_queue {
  711.     /*! Queue name */
  712.     char name[80];
  713.     /*! Music on Hold class */
  714.     char moh[80];
  715.     /*! Announcement to play when call is answered */
  716.     char announce[80];
  717.     /*! Exit context */
  718.     char context[AST_MAX_CONTEXT];
  719.     /*! Macro to run upon member connection */
  720.     char membermacro[AST_MAX_CONTEXT];
  721.     /*! Gosub to run upon member connection */
  722.     char membergosub[AST_MAX_CONTEXT];
  723.     /*! Default rule to use if none specified in call to Queue() */
  724.     char defaultrule[AST_MAX_CONTEXT];
  725.     /*! Sound file: "Your call is now first in line" (def. queue-youarenext) */
  726.     char sound_next[80];
  727.     /*! Sound file: "There are currently" (def. queue-thereare) */
  728.     char sound_thereare[80];
  729.     /*! Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting) */
  730.     char sound_calls[80];
  731.     /*! Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
  732.     char sound_holdtime[80];
  733.     /*! Sound file: "minutes." (def. queue-minutes) */
  734.     char sound_minutes[80];
  735.     /*! Sound file: "minute." (def. queue-minute) */
  736.     char sound_minute[80];
  737.     /*! Sound file: "seconds." (def. queue-seconds) */
  738.     char sound_seconds[80];
  739.     /*! Sound file: "Thank you for your patience." (def. queue-thankyou) */
  740.     char sound_thanks[80];
  741.     /*! Sound file: Custom announce for caller, no default */
  742.     char sound_callerannounce[80];
  743.     /*! Sound file: "Hold time" (def. queue-reporthold) */
  744.     char sound_reporthold[80];
  745.     /*! Sound file: "Currently there are more than" (def. queue-quantity1) */
  746.     char queue_quantity1[80];
  747.     /*! Sound file: "callers waiting to speak with a representative" (def. queue-quantity2) */
  748.     char queue_quantity2[80];
  749.     /*! Sound files: Custom announce, no default */
  750.     struct ast_str *sound_periodicannounce[MAX_PERIODIC_ANNOUNCEMENTS];
  751.     unsigned int dead:1;
  752.     unsigned int eventwhencalled:2;
  753.     unsigned int ringinuse:1;
  754.     unsigned int setinterfacevar:1;
  755.     unsigned int setqueuevar:1;
  756.     unsigned int setqueueentryvar:1;
  757.     unsigned int reportholdtime:1;
  758.     unsigned int wrapped:1;
  759.     unsigned int timeoutrestart:1;
  760.     unsigned int announceholdtime:2;
  761.     unsigned int announceposition:3;
  762.     int strategy:4;
  763.     unsigned int maskmemberstatus:1;
  764.     unsigned int realtime:1;
  765.     unsigned int found:1;
  766.     enum empty_conditions joinempty;
  767.     enum empty_conditions leavewhenempty;
  768.     int announcepositionlimit;          /*!< How many positions we announce? */
  769.     int announcefrequency;              /*!< How often to announce their position */
  770.     int minannouncefrequency;           /*!< The minimum number of seconds between position announcements (def. 15) */
  771.     int periodicannouncefrequency;      /*!< How often to play periodic announcement */
  772.     int numperiodicannounce;            /*!< The number of periodic announcements configured */
  773.     int randomperiodicannounce;         /*!< Are periodic announcments randomly chosen */
  774.     int roundingseconds;                /*!< How many seconds do we round to? */
  775.     int holdtime;                       /*!< Current avg holdtime, based on an exponential average */
  776.     int talktime;                       /*!< Current avg talktime, based on the same exponential average */
  777.     int callscompleted;                 /*!< Number of queue calls completed */
  778.     int callsabandoned;                 /*!< Number of queue calls abandoned */
  779.         int callsexitedwithtimeout;         /*!< Number of queue calls exited with timeout */
  780.         int callsexitedwithkey;             /*!< Number of queue calls exited with keys */
  781.         int callsexitedempty;               /*!< Number of queue calls exited empty */
  782.         int callsexitedsyscompat;           /*!< Number of queue calls exited with syscompat */
  783.     int servicelevel;                   /*!< seconds setting for servicelevel*/
  784.     int callscompletedinsl;             /*!< Number of calls answered with servicelevel*/
  785.     char monfmt[8];                     /*!< Format to use when recording calls */
  786.     int montype;                        /*!< Monitor type  Monitor vs. MixMonitor */
  787.     int count;                          /*!< How many entries */
  788.     int maxlen;                         /*!< Max number of entries */
  789.     int wrapuptime;                     /*!< Wrapup Time */
  790.  
  791.     int retry;                          /*!< Retry calling everyone after this amount of time */
  792.     int timeout;                        /*!< How long to wait for an answer */
  793.     int weight;                         /*!< Respective weight */
  794.     int autopause;                      /*!< Auto pause queue members if they fail to answer */
  795.     int timeoutpriority;                /*!< Do we allow a fraction of the timeout to occur for a ring? */
  796.  
  797.     /* Queue strategy things */
  798.     int rrpos;                          /*!< Round Robin - position */
  799.     int memberdelay;                    /*!< Seconds to delay connecting member to caller */
  800.     int autofill;                       /*!< Ignore the head call status and ring an available agent */
  801.    
  802.     struct ao2_container *members;             /*!< Head of the list of members */
  803.     /*!
  804.      * \brief Number of members _logged in_
  805.      * \note There will be members in the members container that are not logged
  806.      *       in, so this can not simply be replaced with ao2_container_count().
  807.      */
  808.     int membercount;
  809.     struct queue_ent *head;             /*!< Head of the list of callers */
  810.     AST_LIST_ENTRY(call_queue) list;    /*!< Next call queue */
  811.     AST_LIST_HEAD_NOLOCK(, penalty_rule) rules; /*!< The list of penalty rules to invoke */
  812. };
  813.  
  814. struct rule_list {
  815.     char name[80];
  816.     AST_LIST_HEAD_NOLOCK(,penalty_rule) rules;
  817.     AST_LIST_ENTRY(rule_list) list;
  818. };
  819.  
  820. AST_LIST_HEAD_STATIC(rule_lists, rule_list);
  821.  
  822. static struct ao2_container *queues;
  823.  
  824. static void update_realtime_members(struct call_queue *q);
  825. static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused);
  826.  
  827. static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
  828. /*! \brief sets the QUEUESTATUS channel variable */
  829. static void set_queue_result(struct ast_channel *chan, enum queue_result res)
  830. {
  831.     int i;
  832.  
  833.     for (i = 0; i < ARRAY_LEN(queue_results); i++) {
  834.         if (queue_results[i].id == res) {
  835.             pbx_builtin_setvar_helper(chan, "QUEUESTATUS", queue_results[i].text);
  836.             return;
  837.         }
  838.     }
  839. }
  840.  
  841. static const char *int2strat(int strategy)
  842. {
  843.     int x;
  844.  
  845.     for (x = 0; x < ARRAY_LEN(strategies); x++) {
  846.         if (strategy == strategies[x].strategy)
  847.             return strategies[x].name;
  848.     }
  849.  
  850.     return "<unknown>";
  851. }
  852.  
  853. static int strat2int(const char *strategy)
  854. {
  855.     int x;
  856.  
  857.     for (x = 0; x < ARRAY_LEN(strategies); x++) {
  858.         if (!strcasecmp(strategy, strategies[x].name))
  859.             return strategies[x].strategy;
  860.     }
  861.  
  862.     return -1;
  863. }
  864.  
  865. static int queue_hash_cb(const void *obj, const int flags)
  866. {
  867.     const struct call_queue *q = obj;
  868.  
  869.     return ast_str_case_hash(q->name);
  870. }
  871.  
  872. static int queue_cmp_cb(void *obj, void *arg, int flags)
  873. {
  874.     struct call_queue *q = obj, *q2 = arg;
  875.     return !strcasecmp(q->name, q2->name) ? CMP_MATCH | CMP_STOP : 0;
  876. }
  877.  
  878. #ifdef REF_DEBUG_ONLY_QUEUES
  879. #define queue_ref(a)    __ao2_ref_debug(a,1,"",__FILE__,__LINE__,__PRETTY_FUNCTION__)
  880. #define queue_unref(a)  __ao2_ref_debug(a,-1,"",__FILE__,__LINE__,__PRETTY_FUNCTION__)
  881. #define queue_t_ref(a,b)    __ao2_ref_debug(a,1,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  882. #define queue_t_unref(a,b)  __ao2_ref_debug(a,-1,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  883. #define queues_t_link(c,q,tag)  __ao2_link_debug(c,q,tag,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  884. #define queues_t_unlink(c,q,tag)    __ao2_unlink_debug(c,q,tag,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  885. #else
  886. #define queue_t_ref(a,b)    queue_ref(a)
  887. #define queue_t_unref(a,b)  queue_unref(a)
  888. #define queues_t_link(c,q,tag)  ao2_t_link(c,q,tag)
  889. #define queues_t_unlink(c,q,tag)    ao2_t_unlink(c,q,tag)
  890. static inline struct call_queue *queue_ref(struct call_queue *q)
  891. {
  892.     ao2_ref(q, 1);
  893.     return q;
  894. }
  895.  
  896. static inline struct call_queue *queue_unref(struct call_queue *q)
  897. {
  898.     ao2_ref(q, -1);
  899.     return q;
  900. }
  901. #endif
  902.  
  903. /*! \brief Set variables of queue */
  904. static void set_queue_variables(struct call_queue *q, struct ast_channel *chan)
  905. {
  906.     char interfacevar[256]="";
  907.     float sl = 0;
  908.  
  909.     if (q->setqueuevar) {
  910.         sl = 0;
  911.         if (q->callscompleted > 0)
  912.             sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
  913.  
  914.         snprintf(interfacevar, sizeof(interfacevar),
  915.             "QUEUENAME=%s,QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
  916.             q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned,  q->servicelevel, sl);
  917.    
  918.         pbx_builtin_setvar_multiple(chan, interfacevar);
  919.     }
  920. }
  921.  
  922. /*! \brief Insert the 'new' entry after the 'prev' entry of queue 'q' */
  923. static inline void insert_entry(struct call_queue *q, struct queue_ent *prev, struct queue_ent *new, int *pos)
  924. {
  925.     struct queue_ent *cur;
  926.  
  927.     if (!q || !new)
  928.         return;
  929.     if (prev) {
  930.         cur = prev->next;
  931.         prev->next = new;
  932.     } else {
  933.         cur = q->head;
  934.         q->head = new;
  935.     }
  936.     new->next = cur;
  937.  
  938.     /* every queue_ent must have a reference to it's parent call_queue, this
  939.      * reference does not go away until the end of the queue_ent's life, meaning
  940.      * that even when the queue_ent leaves the call_queue this ref must remain. */
  941.     queue_ref(q);
  942.     new->parent = q;
  943.     new->pos = ++(*pos);
  944.     new->opos = *pos;
  945. }
  946.  
  947. /*! \brief Check if members are available
  948.  *
  949.  * This function checks to see if members are available to be called. If any member
  950.  * is available, the function immediately returns 0. If no members are available,
  951.  * then -1 is returned.
  952.  */
  953. static int get_member_status(struct call_queue *q, int max_penalty, int min_penalty, enum empty_conditions conditions)
  954. {
  955.     struct member *member;
  956.     struct ao2_iterator mem_iter;
  957.  
  958.     ao2_lock(q);
  959.     mem_iter = ao2_iterator_init(q->members, 0);
  960.     for (; (member = ao2_iterator_next(&mem_iter)); ao2_ref(member, -1)) {
  961.         if ((max_penalty && (member->penalty > max_penalty)) || (min_penalty && (member->penalty < min_penalty))) {
  962.             if (conditions & QUEUE_EMPTY_PENALTY) {
  963.                 ast_debug(4, "%s is unavailable because his penalty is not between %d and %d\n", member->membername, min_penalty, max_penalty);
  964.                 continue;
  965.             }
  966.         }
  967.  
  968.         switch (member->status) {
  969.         case AST_DEVICE_INVALID:
  970.             if (conditions & QUEUE_EMPTY_INVALID) {
  971.                 ast_debug(4, "%s is unavailable because his device state is 'invalid'\n", member->membername);
  972.                 break;
  973.             }
  974.             goto default_case;
  975.         case AST_DEVICE_UNAVAILABLE:
  976.             if (conditions & QUEUE_EMPTY_UNAVAILABLE) {
  977.                 ast_debug(4, "%s is unavailable because his device state is 'unavailable'\n", member->membername);
  978.                 break;
  979.             }
  980.             goto default_case;
  981.         case AST_DEVICE_INUSE:
  982.             if (conditions & QUEUE_EMPTY_INUSE) {
  983.                 ast_debug(4, "%s is unavailable because his device state is 'inuse'\n", member->membername);
  984.                 break;
  985.             }
  986.             goto default_case;
  987.         case AST_DEVICE_RINGING:
  988.             if (conditions & QUEUE_EMPTY_RINGING) {
  989.                 ast_debug(4, "%s is unavailable because his device state is 'ringing'\n", member->membername);
  990.                 break;
  991.             }
  992.         case AST_DEVICE_UNKNOWN:
  993.             if (conditions & QUEUE_EMPTY_UNKNOWN) {
  994.                 ast_debug(4, "%s is unavailable because his device state is 'unknown'\n", member->membername);
  995.                 break;
  996.             }
  997.             /* Fall-through */
  998.         default:
  999.         default_case:
  1000.             if (member->paused && (conditions & QUEUE_EMPTY_PAUSED)) {
  1001.                 ast_debug(4, "%s is unavailable because he is paused'\n", member->membername);
  1002.                 break;
  1003.             } else if ((conditions & QUEUE_EMPTY_WRAPUP) && member->lastcall && q->wrapuptime && (time(NULL) - q->wrapuptime < member->lastcall)) {
  1004.                 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);
  1005.                 break;
  1006.             } else {
  1007.                 ao2_unlock(q);
  1008.                 ao2_ref(member, -1);
  1009.                 ao2_iterator_destroy(&mem_iter);
  1010.                 ast_debug(4, "%s is available.\n", member->membername);
  1011.                 return 0;
  1012.             }
  1013.             break;
  1014.         }
  1015.     }
  1016.     ao2_iterator_destroy(&mem_iter);
  1017.  
  1018.     ao2_unlock(q);
  1019.     return -1;
  1020. }
  1021.  
  1022. struct statechange {
  1023.     AST_LIST_ENTRY(statechange) entry;
  1024.     int state;
  1025.     char dev[0];
  1026. };
  1027.  
  1028. /*! \brief set a member's status based on device state of that member's state_interface.
  1029.  *  
  1030.  * Lock interface list find sc, iterate through each queues queue_member list for member to
  1031.  * update state inside queues
  1032. */
  1033. static int update_status(struct call_queue *q, struct member *m, const int status)
  1034. {
  1035.     m->status = status;
  1036.  
  1037.     if (q->maskmemberstatus)
  1038.         return 0;
  1039.  
  1040.     manager_event(EVENT_FLAG_AGENT, "QueueMemberStatus",
  1041.         "Queue: %s\r\n"
  1042.         "Location: %s\r\n"
  1043.         "MemberName: %s\r\n"
  1044.         "Membership: %s\r\n"
  1045.         "Penalty: %d\r\n"
  1046.         "CallsTaken: %d\r\n"
  1047.         "LastCall: %d\r\n"
  1048.         "Status: %d\r\n"
  1049.         "Paused: %d\r\n",
  1050.         q->name, m->interface, m->membername, m->dynamic ? "dynamic" : m->realtime ? "realtime" : "static",
  1051.         m->penalty, m->calls, (int)m->lastcall, m->status, m->paused
  1052.     );
  1053.  
  1054.     return 0;
  1055. }
  1056.  
  1057. /*! \brief set a member's status based on device state of that member's interface*/
  1058. static int handle_statechange(void *datap)
  1059. {
  1060.     struct statechange *sc = datap;
  1061.     struct ao2_iterator miter, qiter;
  1062.     struct member *m;
  1063.     struct call_queue *q;
  1064.     char interface[80], *slash_pos;
  1065.     int found = 0;
  1066.  
  1067.     qiter = ao2_iterator_init(queues, 0);
  1068.     while ((q = ao2_t_iterator_next(&qiter, "Iterate over queues"))) {
  1069.         ao2_lock(q);
  1070.  
  1071.         miter = ao2_iterator_init(q->members, 0);
  1072.         for (; (m = ao2_iterator_next(&miter)); ao2_ref(m, -1)) {
  1073.             ast_copy_string(interface, m->state_interface, sizeof(interface));
  1074.  
  1075.             if ((slash_pos = strchr(interface, '/')))
  1076.                 if (!strncasecmp(interface, "Local/", 6) && (slash_pos = strchr(slash_pos + 1, '/')))
  1077.                     *slash_pos = '\0';
  1078.  
  1079.             if (!strcasecmp(interface, sc->dev)) {
  1080.                 found = 1;
  1081.                 update_status(q, m, sc->state);
  1082.                 ao2_ref(m, -1);
  1083.                 break;
  1084.             }
  1085.         }
  1086.         ao2_iterator_destroy(&miter);
  1087.  
  1088.         ao2_unlock(q);
  1089.         queue_t_unref(q, "Done with iterator");
  1090.     }
  1091.     ao2_iterator_destroy(&qiter);
  1092.  
  1093.     if (found)
  1094.         ast_debug(1, "Device '%s' changed to state '%d' (%s)\n", sc->dev, sc->state, ast_devstate2str(sc->state));
  1095.     else
  1096.         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));
  1097.  
  1098.     ast_free(sc);
  1099.     return 0;
  1100. }
  1101.  
  1102. static void device_state_cb(const struct ast_event *event, void *unused)
  1103. {
  1104.     enum ast_device_state state;
  1105.     const char *device;
  1106.     struct statechange *sc;
  1107.     size_t datapsize;
  1108.  
  1109.     state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
  1110.     device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
  1111.  
  1112.     if (ast_strlen_zero(device)) {
  1113.         ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
  1114.         return;
  1115.     }
  1116.     datapsize = sizeof(*sc) + strlen(device) + 1;
  1117.     if (!(sc = ast_calloc(1, datapsize))) {
  1118.         ast_log(LOG_ERROR, "failed to calloc a state change struct\n");
  1119.         return;
  1120.     }
  1121.     sc->state = state;
  1122.     strcpy(sc->dev, device);
  1123.     if (ast_taskprocessor_push(devicestate_tps, handle_statechange, sc) < 0) {
  1124.         ast_free(sc);
  1125.     }
  1126. }
  1127.  
  1128. /*! \brief allocate space for new queue member and set fields based on parameters passed */
  1129. static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused, const char *state_interface)
  1130. {
  1131.     struct member *cur;
  1132.    
  1133.     if ((cur = ao2_alloc(sizeof(*cur), NULL))) {
  1134.         cur->penalty = penalty;
  1135.         cur->paused = paused;
  1136.         ast_copy_string(cur->interface, interface, sizeof(cur->interface));
  1137.         if (!ast_strlen_zero(state_interface))
  1138.             ast_copy_string(cur->state_interface, state_interface, sizeof(cur->state_interface));
  1139.         else
  1140.             ast_copy_string(cur->state_interface, interface, sizeof(cur->state_interface));
  1141.         if (!ast_strlen_zero(membername))
  1142.             ast_copy_string(cur->membername, membername, sizeof(cur->membername));
  1143.         else
  1144.             ast_copy_string(cur->membername, interface, sizeof(cur->membername));
  1145.         if (!strchr(cur->interface, '/'))
  1146.             ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
  1147.         cur->status = ast_device_state(cur->state_interface);
  1148.     }
  1149.  
  1150.     return cur;
  1151. }
  1152.  
  1153.  
  1154. static int compress_char(const char c)
  1155. {
  1156.     if (c < 32)
  1157.         return 0;
  1158.     else if (c > 96)
  1159.         return c - 64;
  1160.     else
  1161.         return c - 32;
  1162. }
  1163.  
  1164. static int member_hash_fn(const void *obj, const int flags)
  1165. {
  1166.     const struct member *mem = obj;
  1167.     const char *chname = strchr(mem->interface, '/');
  1168.     int ret = 0, i;
  1169.     if (!chname)
  1170.         chname = mem->interface;
  1171.     for (i = 0; i < 5 && chname[i]; i++)
  1172.         ret += compress_char(chname[i]) << (i * 6);
  1173.     return ret;
  1174. }
  1175.  
  1176. static int member_cmp_fn(void *obj1, void *obj2, int flags)
  1177. {
  1178.     struct member *mem1 = obj1, *mem2 = obj2;
  1179.     return strcasecmp(mem1->interface, mem2->interface) ? 0 : CMP_MATCH | CMP_STOP;
  1180. }
  1181.  
  1182. /*!
  1183.  * \brief Initialize Queue default values.
  1184.  * \note the queue's lock  must be held before executing this function
  1185. */
  1186. static void init_queue(struct call_queue *q)
  1187. {
  1188.     int i;
  1189.     struct penalty_rule *pr_iter;
  1190.  
  1191.     q->dead = 0;
  1192.     q->retry = DEFAULT_RETRY;
  1193.     q->timeout = DEFAULT_TIMEOUT;
  1194.     q->maxlen = 0;
  1195.     q->announcefrequency = 0;
  1196.     q->minannouncefrequency = DEFAULT_MIN_ANNOUNCE_FREQUENCY;
  1197.     q->announceholdtime = 1;
  1198.     q->announcepositionlimit = 10; /* Default 10 positions */
  1199.     q->announceposition = ANNOUNCEPOSITION_YES; /* Default yes */
  1200.     q->roundingseconds = 0; /* Default - don't announce seconds */
  1201.     q->servicelevel = 0;
  1202.     q->ringinuse = 1;
  1203.     q->setinterfacevar = 0;
  1204.     q->setqueuevar = 0;
  1205.     q->setqueueentryvar = 0;
  1206.     q->autofill = autofill_default;
  1207.     q->montype = montype_default;
  1208.     q->monfmt[0] = '\0';
  1209.     q->reportholdtime = 0;
  1210.     q->wrapuptime = 0;
  1211.     q->joinempty = 0;
  1212.     q->leavewhenempty = 0;
  1213.     q->memberdelay = 0;
  1214.     q->maskmemberstatus = 0;
  1215.     q->eventwhencalled = 0;
  1216.     q->weight = 0;
  1217.     q->timeoutrestart = 0;
  1218.     q->periodicannouncefrequency = 0;
  1219.     q->randomperiodicannounce = 0;
  1220.     q->numperiodicannounce = 0;
  1221.     q->timeoutpriority = TIMEOUT_PRIORITY_APP;
  1222.     if (!q->members) {
  1223.         if (q->strategy == QUEUE_STRATEGY_LINEAR)
  1224.             /* linear strategy depends on order, so we have to place all members in a single bucket */
  1225.             q->members = ao2_container_alloc(1, member_hash_fn, member_cmp_fn);
  1226.         else
  1227.             q->members = ao2_container_alloc(37, member_hash_fn, member_cmp_fn);
  1228.     }
  1229.     q->found = 1;
  1230.  
  1231.     ast_copy_string(q->sound_next, "queue-youarenext", sizeof(q->sound_next));
  1232.     ast_copy_string(q->sound_thereare, "queue-thereare", sizeof(q->sound_thereare));
  1233.     ast_copy_string(q->sound_calls, "queue-callswaiting", sizeof(q->sound_calls));
  1234.     ast_copy_string(q->sound_holdtime, "queue-holdtime", sizeof(q->sound_holdtime));
  1235.     ast_copy_string(q->sound_minutes, "queue-minutes", sizeof(q->sound_minutes));
  1236.     ast_copy_string(q->sound_minute, "queue-minute", sizeof(q->sound_minute));
  1237.     ast_copy_string(q->sound_seconds, "queue-seconds", sizeof(q->sound_seconds));
  1238.     ast_copy_string(q->sound_thanks, "queue-thankyou", sizeof(q->sound_thanks));
  1239.     ast_copy_string(q->sound_reporthold, "queue-reporthold", sizeof(q->sound_reporthold));
  1240.     ast_copy_string(q->queue_quantity1, "queue-quantity1", sizeof(q->queue_quantity1));
  1241.     ast_copy_string(q->queue_quantity2, "queue-quantity2", sizeof(q->queue_quantity2));
  1242.  
  1243.     if (!q->sound_periodicannounce[0]) {
  1244.         q->sound_periodicannounce[0] = ast_str_create(32);
  1245.     }
  1246.  
  1247.     if (q->sound_periodicannounce[0]) {
  1248.         ast_str_set(&q->sound_periodicannounce[0], 0, "queue-periodic-announce");
  1249.     }
  1250.  
  1251.     for (i = 1; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
  1252.         if (q->sound_periodicannounce[i])
  1253.             ast_str_set(&q->sound_periodicannounce[i], 0, "%s", "");
  1254.     }
  1255.  
  1256.     while ((pr_iter = AST_LIST_REMOVE_HEAD(&q->rules,list)))
  1257.         ast_free(pr_iter);
  1258. }
  1259.  
  1260. static void clear_queue(struct call_queue *q)
  1261. {
  1262.     q->holdtime = 0;
  1263.     q->callscompleted = 0;
  1264.     q->callsabandoned = 0;
  1265.         q->callsexitedwithtimeout = 0;
  1266.         q->callsexitedwithkey = 0;
  1267.         q->callsexitedempty = 0;
  1268.     q->callsexitedsyscompat = 0;
  1269.     q->callscompletedinsl = 0;
  1270.     q->talktime = 0;
  1271.  
  1272.     if (q->members) {
  1273.         struct member *mem;
  1274.         struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
  1275.         while ((mem = ao2_iterator_next(&mem_iter))) {
  1276.             mem->calls = 0;
  1277.             mem->lastcall = 0;
  1278.             ao2_ref(mem, -1);
  1279.         }
  1280.         ao2_iterator_destroy(&mem_iter);
  1281.     }
  1282. }
  1283.  
  1284. /*!
  1285.  * \brief Change queue penalty by adding rule.
  1286.  *
  1287.  * Check rule for errors with time or fomatting, see if rule is relative to rest
  1288.  * of queue, iterate list of rules to find correct insertion point, insert and return.
  1289.  * \retval -1 on failure
  1290.  * \retval 0 on success
  1291.  * \note Call this with the rule_lists locked
  1292. */
  1293. static int insert_penaltychange (const char *list_name, const char *content, const int linenum)
  1294. {
  1295.     char *timestr, *maxstr, *minstr, *contentdup;
  1296.     struct penalty_rule *rule = NULL, *rule_iter;
  1297.     struct rule_list *rl_iter;
  1298.     int penaltychangetime, inserted = 0;
  1299.  
  1300.     if (!(rule = ast_calloc(1, sizeof(*rule)))) {
  1301.         return -1;
  1302.     }
  1303.  
  1304.     contentdup = ast_strdupa(content);
  1305.    
  1306.     if (!(maxstr = strchr(contentdup, ','))) {
  1307.         ast_log(LOG_WARNING, "Improperly formatted penaltychange rule at line %d. Ignoring.\n", linenum);
  1308.         ast_free(rule);
  1309.         return -1;
  1310.     }
  1311.  
  1312.     *maxstr++ = '\0';
  1313.     timestr = contentdup;
  1314.  
  1315.     if ((penaltychangetime = atoi(timestr)) < 0) {
  1316.         ast_log(LOG_WARNING, "Improper time parameter specified for penaltychange rule at line %d. Ignoring.\n", linenum);
  1317.         ast_free(rule);
  1318.         return -1;
  1319.     }
  1320.  
  1321.     rule->time = penaltychangetime;
  1322.  
  1323.     if ((minstr = strchr(maxstr,',')))
  1324.         *minstr++ = '\0';
  1325.    
  1326.     /* The last check will evaluate true if either no penalty change is indicated for a given rule
  1327.      * OR if a min penalty change is indicated but no max penalty change is */
  1328.     if (*maxstr == '+' || *maxstr == '-' || *maxstr == '\0') {
  1329.         rule->max_relative = 1;
  1330.     }
  1331.  
  1332.     rule->max_value = atoi(maxstr);
  1333.  
  1334.     if (!ast_strlen_zero(minstr)) {
  1335.         if (*minstr == '+' || *minstr == '-')
  1336.             rule->min_relative = 1;
  1337.         rule->min_value = atoi(minstr);
  1338.     } else /*there was no minimum specified, so assume this means no change*/
  1339.         rule->min_relative = 1;
  1340.  
  1341.     /*We have the rule made, now we need to insert it where it belongs*/
  1342.     AST_LIST_TRAVERSE(&rule_lists, rl_iter, list){
  1343.         if (strcasecmp(rl_iter->name, list_name))
  1344.             continue;
  1345.  
  1346.         AST_LIST_TRAVERSE_SAFE_BEGIN(&rl_iter->rules, rule_iter, list) {
  1347.             if (rule->time < rule_iter->time) {
  1348.                 AST_LIST_INSERT_BEFORE_CURRENT(rule, list);
  1349.                 inserted = 1;
  1350.                 break;
  1351.             }
  1352.         }
  1353.         AST_LIST_TRAVERSE_SAFE_END;
  1354.    
  1355.         if (!inserted) {
  1356.             AST_LIST_INSERT_TAIL(&rl_iter->rules, rule, list);
  1357.         }
  1358.     }
  1359.  
  1360.     return 0;
  1361. }
  1362.  
  1363. static void parse_empty_options(const char *value, enum empty_conditions *empty, int joinempty)
  1364. {
  1365.     char *value_copy = ast_strdupa(value);
  1366.     char *option = NULL;
  1367.     while ((option = strsep(&value_copy, ","))) {
  1368.         if (!strcasecmp(option, "paused")) {
  1369.             *empty |= QUEUE_EMPTY_PAUSED;
  1370.         } else if (!strcasecmp(option, "penalty")) {
  1371.             *empty |= QUEUE_EMPTY_PENALTY;
  1372.         } else if (!strcasecmp(option, "inuse")) {
  1373.             *empty |= QUEUE_EMPTY_INUSE;
  1374.         } else if (!strcasecmp(option, "ringing")) {
  1375.             *empty |= QUEUE_EMPTY_RINGING;
  1376.         } else if (!strcasecmp(option, "invalid")) {
  1377.             *empty |= QUEUE_EMPTY_INVALID;
  1378.         } else if (!strcasecmp(option, "wrapup")) {
  1379.             *empty |= QUEUE_EMPTY_WRAPUP;
  1380.         } else if (!strcasecmp(option, "unavailable")) {
  1381.             *empty |= QUEUE_EMPTY_UNAVAILABLE;
  1382.         } else if (!strcasecmp(option, "unknown")) {
  1383.             *empty |= QUEUE_EMPTY_UNKNOWN;
  1384.         } else if (!strcasecmp(option, "loose")) {
  1385.             *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID);
  1386.         } else if (!strcasecmp(option, "strict")) {
  1387.             *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED | QUEUE_EMPTY_UNAVAILABLE);
  1388.         } else if ((ast_false(option) && joinempty) || (ast_true(option) && !joinempty)) {
  1389.             *empty = (QUEUE_EMPTY_PENALTY | QUEUE_EMPTY_INVALID | QUEUE_EMPTY_PAUSED);
  1390.         } else if ((ast_false(option) && !joinempty) || (ast_true(option) && joinempty)) {
  1391.             *empty = 0;
  1392.         } else {
  1393.             ast_log(LOG_WARNING, "Unknown option %s for '%s'\n", option, joinempty ? "joinempty" : "leavewhenempty");
  1394.         }
  1395.     }
  1396. }
  1397.  
  1398. /*! \brief Configure a queue parameter.
  1399.  *
  1400.  * The failunknown flag is set for config files (and static realtime) to show
  1401.  * errors for unknown parameters. It is cleared for dynamic realtime to allow
  1402.  *  extra fields in the tables.
  1403.  * \note For error reporting, line number is passed for .conf static configuration,
  1404.  * for Realtime queues, linenum is -1.
  1405. */
  1406. static void queue_set_param(struct call_queue *q, const char *param, const char *val, int linenum, int failunknown)
  1407. {
  1408.     if (!strcasecmp(param, "musicclass") ||
  1409.         !strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
  1410.         ast_copy_string(q->moh, val, sizeof(q->moh));
  1411.     } else if (!strcasecmp(param, "announce")) {
  1412.         ast_copy_string(q->announce, val, sizeof(q->announce));
  1413.     } else if (!strcasecmp(param, "context")) {
  1414.         ast_copy_string(q->context, val, sizeof(q->context));
  1415.     } else if (!strcasecmp(param, "timeout")) {
  1416.         q->timeout = atoi(val);
  1417.         if (q->timeout < 0)
  1418.             q->timeout = DEFAULT_TIMEOUT;
  1419.     } else if (!strcasecmp(param, "ringinuse")) {
  1420.         q->ringinuse = ast_true(val);
  1421.     } else if (!strcasecmp(param, "setinterfacevar")) {
  1422.         q->setinterfacevar = ast_true(val);
  1423.     } else if (!strcasecmp(param, "setqueuevar")) {
  1424.         q->setqueuevar = ast_true(val);
  1425.     } else if (!strcasecmp(param, "setqueueentryvar")) {
  1426.         q->setqueueentryvar = ast_true(val);
  1427.     } else if (!strcasecmp(param, "monitor-format")) {
  1428.         ast_copy_string(q->monfmt, val, sizeof(q->monfmt));
  1429.     } else if (!strcasecmp(param, "membermacro")) {
  1430.         ast_copy_string(q->membermacro, val, sizeof(q->membermacro));
  1431.     } else if (!strcasecmp(param, "membergosub")) {
  1432.         ast_copy_string(q->membergosub, val, sizeof(q->membergosub));
  1433.     } else if (!strcasecmp(param, "queue-youarenext")) {
  1434.         ast_copy_string(q->sound_next, val, sizeof(q->sound_next));
  1435.     } else if (!strcasecmp(param, "queue-thereare")) {
  1436.         ast_copy_string(q->sound_thereare, val, sizeof(q->sound_thereare));
  1437.     } else if (!strcasecmp(param, "queue-callswaiting")) {
  1438.         ast_copy_string(q->sound_calls, val, sizeof(q->sound_calls));
  1439.     } else if (!strcasecmp(param, "queue-quantity1")) {
  1440.         ast_copy_string(q->queue_quantity1, val, sizeof(q->queue_quantity1));
  1441.     } else if (!strcasecmp(param, "queue-quantity2")) {
  1442.         ast_copy_string(q->queue_quantity2, val, sizeof(q->queue_quantity2));
  1443.     } else if (!strcasecmp(param, "queue-holdtime")) {
  1444.         ast_copy_string(q->sound_holdtime, val, sizeof(q->sound_holdtime));
  1445.     } else if (!strcasecmp(param, "queue-minutes")) {
  1446.         ast_copy_string(q->sound_minutes, val, sizeof(q->sound_minutes));
  1447.     } else if (!strcasecmp(param, "queue-minute")) {
  1448.         ast_copy_string(q->sound_minute, val, sizeof(q->sound_minute));
  1449.     } else if (!strcasecmp(param, "queue-seconds")) {
  1450.         ast_copy_string(q->sound_seconds, val, sizeof(q->sound_seconds));
  1451.     } else if (!strcasecmp(param, "queue-thankyou")) {
  1452.         ast_copy_string(q->sound_thanks, val, sizeof(q->sound_thanks));
  1453.     } else if (!strcasecmp(param, "queue-callerannounce")) {
  1454.         ast_copy_string(q->sound_callerannounce, val, sizeof(q->sound_callerannounce));
  1455.     } else if (!strcasecmp(param, "queue-reporthold")) {
  1456.         ast_copy_string(q->sound_reporthold, val, sizeof(q->sound_reporthold));
  1457.     } else if (!strcasecmp(param, "announce-frequency")) {
  1458.         q->announcefrequency = atoi(val);
  1459.     } else if (!strcasecmp(param, "min-announce-frequency")) {
  1460.         q->minannouncefrequency = atoi(val);
  1461.         ast_debug(1, "%s=%s for queue '%s'\n", param, val, q->name);
  1462.     } else if (!strcasecmp(param, "announce-round-seconds")) {
  1463.         q->roundingseconds = atoi(val);
  1464.         /* Rounding to any other values just doesn't make sense... */
  1465.         if (!(q->roundingseconds == 0 || q->roundingseconds == 5 || q->roundingseconds == 10
  1466.             || q->roundingseconds == 15 || q->roundingseconds == 20 || q->roundingseconds == 30)) {
  1467.             if (linenum >= 0) {
  1468.                 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
  1469.                     "using 0 instead for queue '%s' at line %d of queues.conf\n",
  1470.                     val, param, q->name, linenum);
  1471.             } else {
  1472.                 ast_log(LOG_WARNING, "'%s' isn't a valid value for %s "
  1473.                     "using 0 instead for queue '%s'\n", val, param, q->name);
  1474.             }
  1475.             q->roundingseconds=0;
  1476.         }
  1477.     } else if (!strcasecmp(param, "announce-holdtime")) {
  1478.         if (!strcasecmp(val, "once"))
  1479.             q->announceholdtime = ANNOUNCEHOLDTIME_ONCE;
  1480.         else if (ast_true(val))
  1481.             q->announceholdtime = ANNOUNCEHOLDTIME_ALWAYS;
  1482.         else
  1483.             q->announceholdtime = 0;
  1484.     } else if (!strcasecmp(param, "announce-position")) {
  1485.         if (!strcasecmp(val, "limit"))
  1486.             q->announceposition = ANNOUNCEPOSITION_LIMIT;
  1487.         else if (!strcasecmp(val, "more"))
  1488.             q->announceposition = ANNOUNCEPOSITION_MORE_THAN;
  1489.         else if (ast_true(val))
  1490.             q->announceposition = ANNOUNCEPOSITION_YES;
  1491.         else
  1492.             q->announceposition = ANNOUNCEPOSITION_NO;
  1493.     } else if (!strcasecmp(param, "announce-position-limit")) {
  1494.         q->announcepositionlimit = atoi(val);
  1495.     } else if (!strcasecmp(param, "periodic-announce")) {
  1496.         if (strchr(val, ',')) {
  1497.             char *s, *buf = ast_strdupa(val);
  1498.             unsigned int i = 0;
  1499.  
  1500.             while ((s = strsep(&buf, ",|"))) {
  1501.                 if (!q->sound_periodicannounce[i])
  1502.                     q->sound_periodicannounce[i] = ast_str_create(16);
  1503.                 ast_str_set(&q->sound_periodicannounce[i], 0, "%s", s);
  1504.                 i++;
  1505.                 if (i == MAX_PERIODIC_ANNOUNCEMENTS)
  1506.                     break;
  1507.             }
  1508.             q->numperiodicannounce = i;
  1509.         } else {
  1510.             ast_str_set(&q->sound_periodicannounce[0], 0, "%s", val);
  1511.             q->numperiodicannounce = 1;
  1512.         }
  1513.     } else if (!strcasecmp(param, "periodic-announce-frequency")) {
  1514.         q->periodicannouncefrequency = atoi(val);
  1515.     } else if (!strcasecmp(param, "random-periodic-announce")) {
  1516.         q->randomperiodicannounce = ast_true(val);
  1517.     } else if (!strcasecmp(param, "retry")) {
  1518.         q->retry = atoi(val);
  1519.         if (q->retry <= 0)
  1520.             q->retry = DEFAULT_RETRY;
  1521.     } else if (!strcasecmp(param, "wrapuptime")) {
  1522.         q->wrapuptime = atoi(val);
  1523.     } else if (!strcasecmp(param, "autofill")) {
  1524.         q->autofill = ast_true(val);
  1525.     } else if (!strcasecmp(param, "monitor-type")) {
  1526.         if (!strcasecmp(val, "mixmonitor"))
  1527.             q->montype = 1;
  1528.     } else if (!strcasecmp(param, "autopause")) {
  1529.         q->autopause = ast_true(val);
  1530.     } else if (!strcasecmp(param, "maxlen")) {
  1531.         q->maxlen = atoi(val);
  1532.         if (q->maxlen < 0)
  1533.             q->maxlen = 0;
  1534.     } else if (!strcasecmp(param, "servicelevel")) {
  1535.         q->servicelevel= atoi(val);
  1536.     } else if (!strcasecmp(param, "strategy")) {
  1537.         int strategy;
  1538.  
  1539.         /* We are a static queue and already have set this, no need to do it again */
  1540.         if (failunknown) {
  1541.             return;
  1542.         }
  1543.         strategy = strat2int(val);
  1544.         if (strategy < 0) {
  1545.             ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
  1546.                 val, q->name);
  1547.             q->strategy = QUEUE_STRATEGY_RINGALL;
  1548.         }
  1549.         if (strategy == q->strategy) {
  1550.             return;
  1551.         }
  1552.         if (strategy == QUEUE_STRATEGY_LINEAR) {
  1553.             ast_log(LOG_WARNING, "Changing to the linear strategy currently requires asterisk to be restarted.\n");
  1554.             return;
  1555.         }
  1556.         q->strategy = strategy;
  1557.     } else if (!strcasecmp(param, "joinempty")) {
  1558.         parse_empty_options(val, &q->joinempty, 1);
  1559.     } else if (!strcasecmp(param, "leavewhenempty")) {
  1560.         parse_empty_options(val, &q->leavewhenempty, 0);
  1561.     } else if (!strcasecmp(param, "eventmemberstatus")) {
  1562.         q->maskmemberstatus = !ast_true(val);
  1563.     } else if (!strcasecmp(param, "eventwhencalled")) {
  1564.         if (!strcasecmp(val, "vars")) {
  1565.             q->eventwhencalled = QUEUE_EVENT_VARIABLES;
  1566.         } else {
  1567.             q->eventwhencalled = ast_true(val) ? 1 : 0;
  1568.         }
  1569.     } else if (!strcasecmp(param, "reportholdtime")) {
  1570.         q->reportholdtime = ast_true(val);
  1571.     } else if (!strcasecmp(param, "memberdelay")) {
  1572.         q->memberdelay = atoi(val);
  1573.     } else if (!strcasecmp(param, "weight")) {
  1574.         q->weight = atoi(val);
  1575.     } else if (!strcasecmp(param, "timeoutrestart")) {
  1576.         q->timeoutrestart = ast_true(val);
  1577.     } else if (!strcasecmp(param, "defaultrule")) {
  1578.         ast_copy_string(q->defaultrule, val, sizeof(q->defaultrule));
  1579.     } else if (!strcasecmp(param, "timeoutpriority")) {
  1580.         if (!strcasecmp(val, "conf")) {
  1581.             q->timeoutpriority = TIMEOUT_PRIORITY_CONF;
  1582.         } else {
  1583.             q->timeoutpriority = TIMEOUT_PRIORITY_APP;
  1584.         }
  1585.     } else if (failunknown) {
  1586.         if (linenum >= 0) {
  1587.             ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queues.conf\n",
  1588.                 q->name, param, linenum);
  1589.         } else {
  1590.             ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s\n", q->name, param);
  1591.         }
  1592.     }
  1593. }
  1594.  
  1595. /*!
  1596.  * \brief Find rt member record to update otherwise create one.
  1597.  *
  1598.  * Search for member in queue, if found update penalty/paused state,
  1599.  * if no member exists create one flag it as a RT member and add to queue member list.
  1600. */
  1601. 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)
  1602. {
  1603.     struct member *m;
  1604.     struct ao2_iterator mem_iter;
  1605.     int penalty = 0;
  1606.     int paused  = 0;
  1607.     int found = 0;
  1608.  
  1609.     if (ast_strlen_zero(rt_uniqueid)) {
  1610.         ast_log(LOG_WARNING, "Realtime field uniqueid is empty for member %s\n", S_OR(membername, "NULL"));
  1611.         return;
  1612.     }
  1613.  
  1614.     if (penalty_str) {
  1615.         penalty = atoi(penalty_str);
  1616.         if (penalty < 0)
  1617.             penalty = 0;
  1618.     }
  1619.  
  1620.     if (paused_str) {
  1621.         paused = atoi(paused_str);
  1622.         if (paused < 0)
  1623.             paused = 0;
  1624.     }
  1625.  
  1626.     /* Find member by realtime uniqueid and update */
  1627.     mem_iter = ao2_iterator_init(q->members, 0);
  1628.     while ((m = ao2_iterator_next(&mem_iter))) {
  1629.         if (!strcasecmp(m->rt_uniqueid, rt_uniqueid)) {
  1630.             m->dead = 0;    /* Do not delete this one. */
  1631.             ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
  1632.             if (paused_str)
  1633.                 m->paused = paused;
  1634.             if (strcasecmp(state_interface, m->state_interface)) {
  1635.                 ast_copy_string(m->state_interface, state_interface, sizeof(m->state_interface));
  1636.             }      
  1637.             m->penalty = penalty;
  1638.             found = 1;
  1639.             ao2_ref(m, -1);
  1640.             break;
  1641.         }
  1642.         ao2_ref(m, -1);
  1643.     }
  1644.     ao2_iterator_destroy(&mem_iter);
  1645.  
  1646.     /* Create a new member */
  1647.     if (!found) {
  1648.         if ((m = create_queue_member(interface, membername, penalty, paused, state_interface))) {
  1649.             m->dead = 0;
  1650.             m->realtime = 1;
  1651.             ast_copy_string(m->rt_uniqueid, rt_uniqueid, sizeof(m->rt_uniqueid));
  1652.             ast_queue_log(q->name, "REALTIME", m->interface, "ADDMEMBER", "%s", "");
  1653.             ao2_link(q->members, m);
  1654.             ao2_ref(m, -1);
  1655.             m = NULL;
  1656.             q->membercount++;
  1657.         }
  1658.     }
  1659. }
  1660.  
  1661. /*! \brief Iterate through queue's member list and delete them */
  1662. static void free_members(struct call_queue *q, int all)
  1663. {
  1664.     /* Free non-dynamic members */
  1665.     struct member *cur;
  1666.     struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
  1667.  
  1668.     while ((cur = ao2_iterator_next(&mem_iter))) {
  1669.         if (all || !cur->dynamic) {
  1670.             ao2_unlink(q->members, cur);
  1671.             q->membercount--;
  1672.         }
  1673.         ao2_ref(cur, -1);
  1674.     }
  1675.     ao2_iterator_destroy(&mem_iter);
  1676. }
  1677.  
  1678. /*! \brief Free queue's member list then its string fields */
  1679. static void destroy_queue(void *obj)
  1680. {
  1681.     struct call_queue *q = obj;
  1682.     int i;
  1683.  
  1684.     free_members(q, 1);
  1685.     for (i = 0; i < MAX_PERIODIC_ANNOUNCEMENTS; i++) {
  1686.         if (q->sound_periodicannounce[i])
  1687.             free(q->sound_periodicannounce[i]);
  1688.     }
  1689.     ao2_ref(q->members, -1);
  1690. }
  1691.  
  1692. static struct call_queue *alloc_queue(const char *queuename)
  1693. {
  1694.     struct call_queue *q;
  1695.  
  1696.     if ((q = ao2_t_alloc(sizeof(*q), destroy_queue, "Allocate queue"))) {
  1697.         ast_copy_string(q->name, queuename, sizeof(q->name));
  1698.     }
  1699.     return q;
  1700. }
  1701.  
  1702. /*!
  1703.  * \brief Reload a single queue via realtime.
  1704.  *
  1705.  * Check for statically defined queue first, check if deleted RT queue,
  1706.  * check for new RT queue, if queue vars are not defined init them with defaults.
  1707.  * reload RT queue vars, set RT queue members dead and reload them, return finished queue.
  1708.  * \retval the queue,
  1709.  * \retval NULL if it doesn't exist.
  1710.  * \note Should be called with the "queues" container locked.
  1711. */
  1712. static struct call_queue *find_queue_by_name_rt(const char *queuename, struct ast_variable *queue_vars, struct ast_config *member_config)
  1713. {
  1714.     struct ast_variable *v;
  1715.     struct call_queue *q, tmpq;
  1716.     struct member *m;
  1717.     struct ao2_iterator mem_iter;
  1718.     char *interface = NULL;
  1719.     const char *tmp_name;
  1720.     char *tmp;
  1721.     char tmpbuf[64];    /* Must be longer than the longest queue param name. */
  1722.  
  1723.     ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
  1724.  
  1725.     /* Static queues override realtime. */
  1726.     if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Check if static queue exists"))) {
  1727.         ao2_lock(q);
  1728.         if (!q->realtime) {
  1729.             if (q->dead) {
  1730.                 ao2_unlock(q);
  1731.                 queue_t_unref(q, "Queue is dead; can't return it");
  1732.                 return NULL;
  1733.             } else {
  1734.                 ast_log(LOG_WARNING, "Static queue '%s' already exists. Not loading from realtime\n", q->name);
  1735.                 ao2_unlock(q);
  1736.                 return q;
  1737.             }
  1738.         }
  1739.     } else if (!member_config)
  1740.         /* Not found in the list, and it's not realtime ... */
  1741.         return NULL;
  1742.  
  1743.     /* Check if queue is defined in realtime. */
  1744.     if (!queue_vars) {
  1745.         /* Delete queue from in-core list if it has been deleted in realtime. */
  1746.         if (q) {
  1747.             /*! \note Hmm, can't seem to distinguish a DB failure from a not
  1748.                found condition... So we might delete an in-core queue
  1749.                in case of DB failure. */
  1750.             ast_debug(1, "Queue %s not found in realtime.\n", queuename);
  1751.  
  1752.             q->dead = 1;
  1753.             /* Delete if unused (else will be deleted when last caller leaves). */
  1754.             queues_t_unlink(queues, q, "Unused; removing from container");
  1755.             ao2_unlock(q);
  1756.             queue_t_unref(q, "Queue is dead; can't return it");
  1757.         }
  1758.         return NULL;
  1759.     }
  1760.  
  1761.     /* Create a new queue if an in-core entry does not exist yet. */
  1762.     if (!q) {
  1763.         struct ast_variable *tmpvar = NULL;
  1764.         if (!(q = alloc_queue(queuename)))
  1765.             return NULL;
  1766.         ao2_lock(q);
  1767.         clear_queue(q);
  1768.         q->realtime = 1;
  1769.         q->membercount = 0;
  1770.         /*Before we initialize the queue, we need to set the strategy, so that linear strategy
  1771.          * will allocate the members properly
  1772.          */
  1773.         for (tmpvar = queue_vars; tmpvar; tmpvar = tmpvar->next) {
  1774.             if (!strcasecmp(tmpvar->name, "strategy")) {
  1775.                 q->strategy = strat2int(tmpvar->value);
  1776.                 if (q->strategy < 0) {
  1777.                     ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
  1778.                     tmpvar->value, q->name);
  1779.                     q->strategy = QUEUE_STRATEGY_RINGALL;
  1780.                 }
  1781.                 break;
  1782.             }
  1783.         }
  1784.         /* We traversed all variables and didn't find a strategy */
  1785.         if (!tmpvar)
  1786.             q->strategy = QUEUE_STRATEGY_RINGALL;
  1787.         queues_t_link(queues, q, "Add queue to container");
  1788.     }
  1789.     init_queue(q);      /* Ensure defaults for all parameters not set explicitly. */
  1790.  
  1791.     memset(tmpbuf, 0, sizeof(tmpbuf));
  1792.     for (v = queue_vars; v; v = v->next) {
  1793.         /* Convert to dashes `-' from underscores `_' as the latter are more SQL friendly. */
  1794.         if ((tmp = strchr(v->name, '_'))) {
  1795.             ast_copy_string(tmpbuf, v->name, sizeof(tmpbuf));
  1796.             tmp_name = tmpbuf;
  1797.             tmp = tmpbuf;
  1798.             while ((tmp = strchr(tmp, '_')))
  1799.                 *tmp++ = '-';
  1800.         } else
  1801.             tmp_name = v->name;
  1802.  
  1803.         /* NULL values don't get returned from realtime; blank values should
  1804.          * still get set.  If someone doesn't want a value to be set, they
  1805.          * should set the realtime column to NULL, not blank. */
  1806.         queue_set_param(q, tmp_name, v->value, -1, 0);
  1807.     }
  1808.  
  1809.     /* Temporarily set realtime members dead so we can detect deleted ones.
  1810.      * Also set the membercount correctly for realtime*/
  1811.     mem_iter = ao2_iterator_init(q->members, 0);
  1812.     while ((m = ao2_iterator_next(&mem_iter))) {
  1813.         q->membercount++;
  1814.         if (m->realtime)
  1815.             m->dead = 1;
  1816.         ao2_ref(m, -1);
  1817.     }
  1818.     ao2_iterator_destroy(&mem_iter);
  1819.  
  1820.     while ((interface = ast_category_browse(member_config, interface))) {
  1821.         rt_handle_member_record(q, interface,
  1822.             ast_variable_retrieve(member_config, interface, "uniqueid"),
  1823.             S_OR(ast_variable_retrieve(member_config, interface, "membername"),interface),
  1824.             ast_variable_retrieve(member_config, interface, "penalty"),
  1825.             ast_variable_retrieve(member_config, interface, "paused"),
  1826.             S_OR(ast_variable_retrieve(member_config, interface, "state_interface"),interface));
  1827.     }
  1828.  
  1829.     /* Delete all realtime members that have been deleted in DB. */
  1830.     mem_iter = ao2_iterator_init(q->members, 0);
  1831.     while ((m = ao2_iterator_next(&mem_iter))) {
  1832.         if (m->dead) {
  1833.             ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
  1834.             ao2_unlink(q->members, m);
  1835.             q->membercount--;
  1836.         }
  1837.         ao2_ref(m, -1);
  1838.     }
  1839.     ao2_iterator_destroy(&mem_iter);
  1840.  
  1841.     ao2_unlock(q);
  1842.  
  1843.     return q;
  1844. }
  1845.  
  1846. static struct call_queue *load_realtime_queue(const char *queuename)
  1847. {
  1848.     struct ast_variable *queue_vars;
  1849.     struct ast_config *member_config = NULL;
  1850.     struct call_queue *q = NULL, tmpq;
  1851.     int prev_weight = 0;
  1852.  
  1853.     /* Find the queue in the in-core list first. */
  1854.     ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
  1855.     q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Look for queue in memory first");
  1856.  
  1857.     if (!q || q->realtime) {
  1858.         /*! \note Load from realtime before taking the "queues" container lock, to avoid blocking all
  1859.            queue operations while waiting for the DB.
  1860.  
  1861.            This will be two separate database transactions, so we might
  1862.            see queue parameters as they were before another process
  1863.            changed the queue and member list as it was after the change.
  1864.            Thus we might see an empty member list when a queue is
  1865.            deleted. In practise, this is unlikely to cause a problem. */
  1866.  
  1867.         queue_vars = ast_load_realtime("queues", "name", queuename, SENTINEL);
  1868.         if (queue_vars) {
  1869.             member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", queuename, SENTINEL);
  1870.             if (!member_config) {
  1871.                 ast_log(LOG_ERROR, "no queue_members defined in your config (extconfig.conf).\n");
  1872.                 ast_variables_destroy(queue_vars);
  1873.                 return NULL;
  1874.             }
  1875.         }
  1876.         if (q) {
  1877.             prev_weight = q->weight ? 1 : 0;
  1878.         }
  1879.  
  1880.         ao2_lock(queues);
  1881.  
  1882.         q = find_queue_by_name_rt(queuename, queue_vars, member_config);
  1883.         if (member_config) {
  1884.             ast_config_destroy(member_config);
  1885.         }
  1886.         if (queue_vars) {
  1887.             ast_variables_destroy(queue_vars);
  1888.         }
  1889.         /* update the use_weight value if the queue's has gained or lost a weight */
  1890.         if (q) {
  1891.             if (!q->weight && prev_weight) {
  1892.                 ast_atomic_fetchadd_int(&use_weight, -1);
  1893.             }
  1894.             if (q->weight && !prev_weight) {
  1895.                 ast_atomic_fetchadd_int(&use_weight, +1);
  1896.             }
  1897.         }
  1898.         /* Other cases will end up with the proper value for use_weight */
  1899.         ao2_unlock(queues);
  1900.  
  1901.     } else {
  1902.         update_realtime_members(q);
  1903.     }
  1904.     return q;
  1905. }
  1906.  
  1907. static int update_realtime_member_field(struct member *mem, const char *queue_name, const char *field, const char *value)
  1908. {
  1909.     int ret = -1;
  1910.  
  1911.     if (ast_strlen_zero(mem->rt_uniqueid))
  1912.         return ret;
  1913.  
  1914.     if ((ast_update_realtime("queue_members", "uniqueid", mem->rt_uniqueid, field, value, SENTINEL)) > 0)
  1915.         ret = 0;
  1916.  
  1917.     return ret;
  1918. }
  1919.  
  1920.  
  1921. static void update_realtime_members(struct call_queue *q)
  1922. {
  1923.     struct ast_config *member_config = NULL;
  1924.     struct member *m;
  1925.     char *interface = NULL;
  1926.     struct ao2_iterator mem_iter;
  1927.  
  1928.     if (!(member_config = ast_load_realtime_multientry("queue_members", "interface LIKE", "%", "queue_name", q->name , SENTINEL))) {
  1929.         /*This queue doesn't have realtime members*/
  1930.         ast_debug(3, "Queue %s has no realtime members defined. No need for update\n", q->name);
  1931.         return;
  1932.     }
  1933.  
  1934.     ao2_lock(queues);
  1935.     ao2_lock(q);
  1936.    
  1937.     /* Temporarily set realtime  members dead so we can detect deleted ones.*/
  1938.     mem_iter = ao2_iterator_init(q->members, 0);
  1939.     while ((m = ao2_iterator_next(&mem_iter))) {
  1940.         if (m->realtime)
  1941.             m->dead = 1;
  1942.         ao2_ref(m, -1);
  1943.     }
  1944.     ao2_iterator_destroy(&mem_iter);
  1945.  
  1946.     while ((interface = ast_category_browse(member_config, interface))) {
  1947.         rt_handle_member_record(q, interface,
  1948.             ast_variable_retrieve(member_config, interface, "uniqueid"),
  1949.             S_OR(ast_variable_retrieve(member_config, interface, "membername"), interface),
  1950.             ast_variable_retrieve(member_config, interface, "penalty"),
  1951.             ast_variable_retrieve(member_config, interface, "paused"),
  1952.             S_OR(ast_variable_retrieve(member_config, interface, "state_interface"), interface));
  1953.     }
  1954.  
  1955.     /* Delete all realtime members that have been deleted in DB. */
  1956.     mem_iter = ao2_iterator_init(q->members, 0);
  1957.     while ((m = ao2_iterator_next(&mem_iter))) {
  1958.         if (m->dead) {
  1959.             ast_queue_log(q->name, "REALTIME", m->interface, "REMOVEMEMBER", "%s", "");
  1960.             ao2_unlink(q->members, m);
  1961.             q->membercount--;
  1962.         }
  1963.         ao2_ref(m, -1);
  1964.     }
  1965.     ao2_iterator_destroy(&mem_iter);
  1966.     ao2_unlock(q);
  1967.     ao2_unlock(queues);
  1968.     ast_config_destroy(member_config);
  1969. }
  1970.  
  1971. static int join_queue(char *queuename, struct queue_ent *qe, enum queue_result *reason)
  1972. {
  1973.     struct call_queue *q;
  1974.     struct queue_ent *cur, *prev = NULL;
  1975.     int res = -1;
  1976.     int pos = 0;
  1977.     int inserted = 0;
  1978.  
  1979.     if (!(q = load_realtime_queue(queuename)))
  1980.         return res;
  1981.  
  1982.     ao2_lock(queues);
  1983.     ao2_lock(q);
  1984.  
  1985.     /* This is our one */
  1986.     if (q->joinempty) {
  1987.         int status = 0;
  1988.         if ((status = get_member_status(q, qe->max_penalty, qe->min_penalty, q->joinempty))) {
  1989.             *reason = QUEUE_JOINEMPTY;
  1990.             ao2_unlock(q);
  1991.             ao2_unlock(queues);
  1992.             return res;
  1993.         }
  1994.     }
  1995.     if (*reason == QUEUE_UNKNOWN && q->maxlen && (q->count >= q->maxlen))
  1996.         *reason = QUEUE_FULL;
  1997.     else if (*reason == QUEUE_UNKNOWN) {
  1998.         /* There's space for us, put us at the right position inside
  1999.          * the queue.
  2000.          * Take into account the priority of the calling user */
  2001.         inserted = 0;
  2002.         prev = NULL;
  2003.         cur = q->head;
  2004.         while (cur) {
  2005.             /* We have higher priority than the current user, enter
  2006.              * before him, after all the other users with priority
  2007.              * higher or equal to our priority. */
  2008.             if ((!inserted) && (qe->prio > cur->prio)) {
  2009.                 insert_entry(q, prev, qe, &pos);
  2010.                 inserted = 1;
  2011.             }
  2012.             cur->pos = ++pos;
  2013.             prev = cur;
  2014.             cur = cur->next;
  2015.         }
  2016.         /* No luck, join at the end of the queue */
  2017.         if (!inserted)
  2018.             insert_entry(q, prev, qe, &pos);
  2019.         ast_copy_string(qe->moh, q->moh, sizeof(qe->moh));
  2020.         ast_copy_string(qe->announce, q->announce, sizeof(qe->announce));
  2021.         ast_copy_string(qe->context, q->context, sizeof(qe->context));
  2022.         q->count++;
  2023.         res = 0;
  2024.         manager_event(EVENT_FLAG_CALL, "Join",
  2025.             "Channel: %s\r\nCallerIDNum: %s\r\nCallerIDName: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\nUniqueid: %s\r\n",
  2026.             qe->chan->name,
  2027.             S_OR(qe->chan->cid.cid_num, "unknown"), /* XXX somewhere else it is <unknown> */
  2028.             S_OR(qe->chan->cid.cid_name, "unknown"),
  2029.             q->name, qe->pos, q->count, qe->chan->uniqueid );
  2030.         ast_debug(1, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
  2031.     }
  2032.     ao2_unlock(q);
  2033.     ao2_unlock(queues);
  2034.  
  2035.     return res;
  2036. }
  2037.  
  2038. static int play_file(struct ast_channel *chan, const char *filename)
  2039. {
  2040.     int res;
  2041.  
  2042.     if (ast_strlen_zero(filename)) {
  2043.         return 0;
  2044.     }
  2045.  
  2046.     if (!ast_fileexists(filename, NULL, chan->language)) {
  2047.         return 0;
  2048.     }
  2049.  
  2050.     ast_stopstream(chan);
  2051.  
  2052.     res = ast_streamfile(chan, filename, chan->language);
  2053.     if (!res)
  2054.         res = ast_waitstream(chan, AST_DIGIT_ANY);
  2055.  
  2056.     ast_stopstream(chan);
  2057.  
  2058.     return res;
  2059. }
  2060.  
  2061. /*!
  2062.  * \brief Check for valid exit from queue via goto
  2063.  * \retval 0 if failure
  2064.  * \retval 1 if successful
  2065. */
  2066. static int valid_exit(struct queue_ent *qe, char digit)
  2067. {
  2068.     int digitlen = strlen(qe->digits);
  2069.  
  2070.     /* Prevent possible buffer overflow */
  2071.     if (digitlen < sizeof(qe->digits) - 2) {
  2072.         qe->digits[digitlen] = digit;
  2073.         qe->digits[digitlen + 1] = '\0';
  2074.     } else {
  2075.         qe->digits[0] = '\0';
  2076.         return 0;
  2077.     }
  2078.  
  2079.     /* If there's no context to goto, short-circuit */
  2080.     if (ast_strlen_zero(qe->context))
  2081.         return 0;
  2082.  
  2083.     /* If the extension is bad, then reset the digits to blank */
  2084.     if (!ast_canmatch_extension(qe->chan, qe->context, qe->digits, 1, qe->chan->cid.cid_num)) {
  2085.         qe->digits[0] = '\0';
  2086.         return 0;
  2087.     }
  2088.  
  2089.     /* We have an exact match */
  2090.     if (!ast_goto_if_exists(qe->chan, qe->context, qe->digits, 1)) {
  2091.         qe->valid_digits = 1;
  2092.         /* Return 1 on a successful goto */
  2093.         return 1;
  2094.     }
  2095.  
  2096.     return 0;
  2097. }
  2098.  
  2099. static int say_position(struct queue_ent *qe, int ringing)
  2100. {
  2101.     int res = 0, avgholdmins, avgholdsecs, announceposition = 0;
  2102.     int say_thanks = 1;
  2103.     time_t now;
  2104.  
  2105.     /* Let minannouncefrequency seconds pass between the start of each position announcement */
  2106.     time(&now);
  2107.     if ((now - qe->last_pos) < qe->parent->minannouncefrequency)
  2108.         return 0;
  2109.  
  2110.     /* If either our position has changed, or we are over the freq timer, say position */
  2111.     if ((qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency))
  2112.         return 0;
  2113.  
  2114.     if (ringing) {
  2115.         ast_indicate(qe->chan,-1);
  2116.     } else {
  2117.         ast_moh_stop(qe->chan);
  2118.     }
  2119.  
  2120.     if (qe->parent->announceposition == ANNOUNCEPOSITION_YES ||
  2121.         qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN ||
  2122.         (qe->parent->announceposition == ANNOUNCEPOSITION_LIMIT &&
  2123.         qe->pos <= qe->parent->announcepositionlimit))
  2124.             announceposition = 1;
  2125.  
  2126.  
  2127.     if (announceposition == 1) {
  2128.         /* Say we're next, if we are */
  2129.         if (qe->pos == 1) {
  2130.             res = play_file(qe->chan, qe->parent->sound_next);
  2131.             if (res)
  2132.                 goto playout;
  2133.             else
  2134.                 goto posout;
  2135.         } else {
  2136.             if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
  2137.                 /* More than Case*/
  2138.                 res = play_file(qe->chan, qe->parent->queue_quantity1);
  2139.                 if (res)
  2140.                     goto playout;
  2141.                 res = ast_say_number(qe->chan, qe->parent->announcepositionlimit, AST_DIGIT_ANY, qe->chan->language, NULL); /* Needs gender */
  2142.                 if (res)
  2143.                     goto playout;
  2144.             } else {
  2145.                 /* Normal Case */
  2146.                 res = play_file(qe->chan, qe->parent->sound_thereare);
  2147.                 if (res)
  2148.                     goto playout;
  2149.                 res = ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language, NULL); /* Needs gender */
  2150.                 if (res)
  2151.                     goto playout;
  2152.             }
  2153.             if (qe->parent->announceposition == ANNOUNCEPOSITION_MORE_THAN && qe->pos > qe->parent->announcepositionlimit){
  2154.                 /* More than Case*/
  2155.                 res = play_file(qe->chan, qe->parent->queue_quantity2);
  2156.                 if (res)
  2157.                     goto playout;
  2158.             } else {
  2159.                 res = play_file(qe->chan, qe->parent->sound_calls);
  2160.                 if (res)
  2161.                     goto playout;
  2162.             }
  2163.         }
  2164.     }
  2165.     /* Round hold time to nearest minute */
  2166.     avgholdmins = abs(((qe->parent->holdtime + 30) - (now - qe->start)) / 60);
  2167.  
  2168.     /* If they have specified a rounding then round the seconds as well */
  2169.     if (qe->parent->roundingseconds) {
  2170.         avgholdsecs = (abs(((qe->parent->holdtime + 30) - (now - qe->start))) - 60 * avgholdmins) / qe->parent->roundingseconds;
  2171.         avgholdsecs *= qe->parent->roundingseconds;
  2172.     } else {
  2173.         avgholdsecs = 0;
  2174.     }
  2175.  
  2176.     ast_verb(3, "Hold time for %s is %d minute(s) %d seconds\n", qe->parent->name, avgholdmins, avgholdsecs);
  2177.  
  2178.     /* If the hold time is >1 min, if it's enabled, and if it's not
  2179.        supposed to be only once and we have already said it, say it */
  2180.     if ((avgholdmins+avgholdsecs) > 0 && qe->parent->announceholdtime &&
  2181.         ((qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE && !qe->last_pos) ||
  2182.         !(qe->parent->announceholdtime == ANNOUNCEHOLDTIME_ONCE))) {
  2183.         res = play_file(qe->chan, qe->parent->sound_holdtime);
  2184.         if (res)
  2185.             goto playout;
  2186.  
  2187.         if (avgholdmins >= 1) {
  2188.             res = ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, qe->chan->language, NULL);
  2189.             if (res)
  2190.                 goto playout;
  2191.  
  2192.             if (avgholdmins == 1) {
  2193.                 res = play_file(qe->chan, qe->parent->sound_minute);
  2194.                 if (res)
  2195.                     goto playout;
  2196.             } else {
  2197.                 res = play_file(qe->chan, qe->parent->sound_minutes);
  2198.                 if (res)
  2199.                     goto playout;
  2200.             }
  2201.         }
  2202.         if (avgholdsecs >= 1) {
  2203.             res = ast_say_number(qe->chan, avgholdsecs, AST_DIGIT_ANY, qe->chan->language, NULL);
  2204.             if (res)
  2205.                 goto playout;
  2206.  
  2207.             res = play_file(qe->chan, qe->parent->sound_seconds);
  2208.             if (res)
  2209.                 goto playout;
  2210.         }
  2211.     } else if (qe->parent->announceholdtime && !qe->parent->announceposition) {
  2212.         say_thanks = 0;
  2213.     }
  2214.  
  2215. posout:
  2216.     if (qe->parent->announceposition) {
  2217.         ast_verb(3, "Told %s in %s their queue position (which was %d)\n",
  2218.             qe->chan->name, qe->parent->name, qe->pos);
  2219.     }
  2220.     if (say_thanks) {
  2221.         res = play_file(qe->chan, qe->parent->sound_thanks);
  2222.     }
  2223. playout:
  2224.  
  2225.     if ((res > 0 && !valid_exit(qe, res)))
  2226.         res = 0;
  2227.  
  2228.     /* Set our last_pos indicators */
  2229.     qe->last_pos = now;
  2230.     qe->last_pos_said = qe->pos;
  2231.  
  2232.     /* Don't restart music on hold if we're about to exit the caller from the queue */
  2233.     if (!res) {
  2234.         if (ringing) {
  2235.             ast_indicate(qe->chan, AST_CONTROL_RINGING);
  2236.         } else {
  2237.             ast_moh_start(qe->chan, qe->moh, NULL);
  2238.         }
  2239.     }
  2240.     return res;
  2241. }
  2242.  
  2243. static void recalc_holdtime(struct queue_ent *qe, int newholdtime)
  2244. {
  2245.     int oldvalue;
  2246.  
  2247.     /* Calculate holdtime using an exponential average */
  2248.     /* Thanks to SRT for this contribution */
  2249.     /* 2^2 (4) is the filter coefficient; a higher exponent would give old entries more weight */
  2250.  
  2251.     ao2_lock(qe->parent);
  2252.     oldvalue = qe->parent->holdtime;
  2253.     qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newholdtime) >> 2;
  2254.     ao2_unlock(qe->parent);
  2255. }
  2256.  
  2257. /*! \brief Caller leaving queue.
  2258.  *
  2259.  * Search the queue to find the leaving client, if found remove from queue
  2260.  * create manager event, move others up the queue.
  2261. */
  2262. static void leave_queue(struct queue_ent *qe)
  2263. {
  2264.     struct call_queue *q;
  2265.     struct queue_ent *current, *prev = NULL;
  2266.     struct penalty_rule *pr_iter;
  2267.     int pos = 0;
  2268.  
  2269.     if (!(q = qe->parent))
  2270.         return;
  2271.     queue_t_ref(q, "Copy queue pointer from queue entry");
  2272.     ao2_lock(q);
  2273.  
  2274.     prev = NULL;
  2275.     for (current = q->head; current; current = current->next) {
  2276.         if (current == qe) {
  2277.             q->count--;
  2278.  
  2279.             /* Take us out of the queue */
  2280.             manager_event(EVENT_FLAG_CALL, "Leave",
  2281.                 "Channel: %s\r\nQueue: %s\r\nCount: %d\r\nUniqueid: %s\r\n",
  2282.                 qe->chan->name, q->name,  q->count, qe->chan->uniqueid);
  2283.             ast_debug(1, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
  2284.             /* Take us out of the queue */
  2285.             if (prev)
  2286.                 prev->next = current->next;
  2287.             else
  2288.                 q->head = current->next;
  2289.             /* Free penalty rules */
  2290.             while ((pr_iter = AST_LIST_REMOVE_HEAD(&qe->qe_rules, list)))
  2291.                 ast_free(pr_iter);
  2292.         } else {
  2293.             /* Renumber the people after us in the queue based on a new count */
  2294.             current->pos = ++pos;
  2295.             prev = current;
  2296.         }
  2297.     }
  2298.     ao2_unlock(q);
  2299.  
  2300.     /*If the queue is a realtime queue, check to see if it's still defined in real time*/
  2301.     if (q->realtime) {
  2302.         struct ast_variable *var;
  2303.         if (!(var = ast_load_realtime("queues", "name", q->name, SENTINEL))) {
  2304.             q->dead = 1;
  2305.         } else {
  2306.             ast_variables_destroy(var);
  2307.         }
  2308.     }
  2309.  
  2310.     if (q->dead) { 
  2311.         /* It's dead and nobody is in it, so kill it */
  2312.         queues_t_unlink(queues, q, "Queue is now dead; remove it from the container");
  2313.     }
  2314.     /* unref the explicit ref earlier in the function */
  2315.     queue_t_unref(q, "Expire copied reference");
  2316. }
  2317.  
  2318. /*! \brief Hang up a list of outgoing calls */
  2319. static void hangupcalls(struct callattempt *outgoing, struct ast_channel *exception, int cancel_answered_elsewhere)
  2320. {
  2321.     struct callattempt *oo;
  2322.  
  2323.     while (outgoing) {
  2324.         /* If someone else answered the call we should indicate this in the CANCEL */
  2325.         /* Hangup any existing lines we have open */
  2326.         if (outgoing->chan && (outgoing->chan != exception)) {
  2327.             if (exception || cancel_answered_elsewhere)
  2328.                 ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
  2329.             ast_hangup(outgoing->chan);
  2330.         }
  2331.         oo = outgoing;
  2332.         outgoing = outgoing->q_next;
  2333.         if (oo->member)
  2334.             ao2_ref(oo->member, -1);
  2335.         ast_free(oo);
  2336.     }
  2337. }
  2338.  
  2339. /*!
  2340.  * \brief Get the number of members available to accept a call.
  2341.  *
  2342.  * \note The queue passed in should be locked prior to this function call
  2343.  *
  2344.  * \param[in] q The queue for which we are couting the number of available members
  2345.  * \return Return the number of available members in queue q
  2346.  */
  2347. static int num_available_members(struct call_queue *q)
  2348. {
  2349.     struct member *mem;
  2350.     int avl = 0;
  2351.     struct ao2_iterator mem_iter;
  2352.  
  2353.     mem_iter = ao2_iterator_init(q->members, 0);
  2354.     while ((mem = ao2_iterator_next(&mem_iter))) {
  2355.         switch (mem->status) {
  2356.         case AST_DEVICE_INUSE:
  2357.             if (!q->ringinuse)
  2358.                 break;
  2359.             /* else fall through */
  2360.         case AST_DEVICE_NOT_INUSE:
  2361.         case AST_DEVICE_UNKNOWN:
  2362.             if (!mem->paused) {
  2363.                 avl++;
  2364.             }
  2365.             break;
  2366.         }
  2367.         ao2_ref(mem, -1);
  2368.  
  2369.         /* If autofill is not enabled or if the queue's strategy is ringall, then
  2370.          * we really don't care about the number of available members so much as we
  2371.          * do that there is at least one available.
  2372.          *
  2373.          * In fact, we purposely will return from this function stating that only
  2374.          * one member is available if either of those conditions hold. That way,
  2375.          * functions which determine what action to take based on the number of available
  2376.          * members will operate properly. The reasoning is that even if multiple
  2377.          * members are available, only the head caller can actually be serviced.
  2378.          */
  2379.         if ((!q->autofill || q->strategy == QUEUE_STRATEGY_RINGALL) && avl) {
  2380.             break;
  2381.         }
  2382.     }
  2383.     ao2_iterator_destroy(&mem_iter);
  2384.  
  2385.     return avl;
  2386. }
  2387.  
  2388. /* traverse all defined queues which have calls waiting and contain this member
  2389.    return 0 if no other queue has precedence (higher weight) or 1 if found  */
  2390. static int compare_weight(struct call_queue *rq, struct member *member)
  2391. {
  2392.     struct call_queue *q;
  2393.     struct member *mem;
  2394.     int found = 0;
  2395.     struct ao2_iterator queue_iter;
  2396.    
  2397.     /* q's lock and rq's lock already set by try_calling()
  2398.      * to solve deadlock */
  2399.     queue_iter = ao2_iterator_init(queues, 0);
  2400.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  2401.         if (q == rq) { /* don't check myself, could deadlock */
  2402.             queue_t_unref(q, "Done with iterator");
  2403.             continue;
  2404.         }
  2405.         ao2_lock(q);
  2406.         if (q->count && q->members) {
  2407.             if ((mem = ao2_find(q->members, member, OBJ_POINTER))) {
  2408.                 ast_debug(1, "Found matching member %s in queue '%s'\n", mem->interface, q->name);
  2409.                 if (q->weight > rq->weight && q->count >= num_available_members(q)) {
  2410.                     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);
  2411.                     found = 1;
  2412.                 }
  2413.                 ao2_ref(mem, -1);
  2414.             }
  2415.         }
  2416.         ao2_unlock(q);
  2417.         queue_t_unref(q, "Done with iterator");
  2418.         if (found) {
  2419.             break;
  2420.         }
  2421.     }
  2422.     ao2_iterator_destroy(&queue_iter);
  2423.     return found;
  2424. }
  2425.  
  2426. /*! \brief common hangup actions */
  2427. static void do_hang(struct callattempt *o)
  2428. {
  2429.     o->stillgoing = 0;
  2430.     ast_hangup(o->chan);
  2431.     o->chan = NULL;
  2432. }
  2433.  
  2434. /*! \brief convert "\n" to "\nVariable: " ready for manager to use */
  2435. static char *vars2manager(struct ast_channel *chan, char *vars, size_t len)
  2436. {
  2437.     struct ast_str *buf = ast_str_thread_get(&ast_str_thread_global_buf, len + 1);
  2438.     char *tmp;
  2439.  
  2440.     if (pbx_builtin_serialize_variables(chan, &buf)) {
  2441.         int i, j;
  2442.  
  2443.         /* convert "\n" to "\nVariable: " */
  2444.         strcpy(vars, "Variable: ");
  2445.         tmp = ast_str_buffer(buf);
  2446.  
  2447.         for (i = 0, j = 10; (i < len - 1) && (j < len - 1); i++, j++) {
  2448.             vars[j] = tmp[i];
  2449.  
  2450.             if (tmp[i + 1] == '\0')
  2451.                 break;
  2452.             if (tmp[i] == '\n') {
  2453.                 vars[j++] = '\r';
  2454.                 vars[j++] = '\n';
  2455.  
  2456.                 ast_copy_string(&(vars[j]), "Variable: ", len - j);
  2457.                 j += 9;
  2458.             }
  2459.         }
  2460.         if (j > len - 3)
  2461.             j = len - 3;
  2462.         vars[j++] = '\r';
  2463.         vars[j++] = '\n';
  2464.         vars[j] = '\0';
  2465.     } else {
  2466.         /* there are no channel variables; leave it blank */
  2467.         *vars = '\0';
  2468.     }
  2469.     return vars;
  2470. }
  2471.  
  2472. /*!
  2473.  * \brief Part 2 of ring_one
  2474.  *
  2475.  * Does error checking before attempting to request a channel and call a member.
  2476.  * This function is only called from ring_one().
  2477.  * Failure can occur if:
  2478.  * - Agent on call
  2479.  * - Agent is paused
  2480.  * - Wrapup time not expired
  2481.  * - Priority by another queue
  2482.  *
  2483.  * \retval 1 on success to reach a free agent
  2484.  * \retval 0 on failure to get agent.
  2485.  */
  2486. static int ring_entry(struct queue_ent *qe, struct callattempt *tmp, int *busies)
  2487. {
  2488.     int res;
  2489.     int status;
  2490.     char tech[256];
  2491.     char *location;
  2492.     const char *macrocontext, *macroexten;
  2493.  
  2494.     /* on entry here, we know that tmp->chan == NULL */
  2495.     if ((tmp->lastqueue && tmp->lastqueue->wrapuptime && (time(NULL) - tmp->lastcall < tmp->lastqueue->wrapuptime)) ||
  2496.         (!tmp->lastqueue && qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime))) {
  2497.         ast_debug(1, "Wrapuptime not yet expired on queue %s for %s\n",
  2498.                 (tmp->lastqueue ? tmp->lastqueue->name : qe->parent->name), tmp->interface);
  2499.         if (qe->chan->cdr)
  2500.             ast_cdr_busy(qe->chan->cdr);
  2501.         tmp->stillgoing = 0;
  2502.         (*busies)++;
  2503.         return 0;
  2504.     }
  2505.  
  2506.     if (!qe->parent->ringinuse && (tmp->member->status != AST_DEVICE_NOT_INUSE) && (tmp->member->status != AST_DEVICE_UNKNOWN)) {
  2507.         ast_debug(1, "%s in use, can't receive call\n", tmp->interface);
  2508.         if (qe->chan->cdr)
  2509.             ast_cdr_busy(qe->chan->cdr);
  2510.         tmp->stillgoing = 0;
  2511.         return 0;
  2512.     }
  2513.  
  2514.     if (tmp->member->paused) {
  2515.         ast_debug(1, "%s paused, can't receive call\n", tmp->interface);
  2516.         if (qe->chan->cdr)
  2517.             ast_cdr_busy(qe->chan->cdr);
  2518.         tmp->stillgoing = 0;
  2519.         return 0;
  2520.     }
  2521.     if (use_weight && compare_weight(qe->parent,tmp->member)) {
  2522.         ast_debug(1, "Priority queue delaying call to %s:%s\n", qe->parent->name, tmp->interface);
  2523.         if (qe->chan->cdr)
  2524.             ast_cdr_busy(qe->chan->cdr);
  2525.         tmp->stillgoing = 0;
  2526.         (*busies)++;
  2527.         return 0;
  2528.     }
  2529.  
  2530.     ast_copy_string(tech, tmp->interface, sizeof(tech));
  2531.     if ((location = strchr(tech, '/')))
  2532.         *location++ = '\0';
  2533.     else
  2534.         location = "";
  2535.  
  2536.     /* Request the peer */
  2537.     tmp->chan = ast_request(tech, qe->chan->nativeformats, location, &status);
  2538.     if (!tmp->chan) {           /* If we can't, just go on to the next call */
  2539.         if (qe->chan->cdr)
  2540.             ast_cdr_busy(qe->chan->cdr);
  2541.         tmp->stillgoing = 0;   
  2542.  
  2543.         ao2_lock(qe->parent);
  2544.         update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
  2545.         qe->parent->rrpos++;
  2546.         qe->linpos++;
  2547.         ao2_unlock(qe->parent);
  2548.  
  2549.         (*busies)++;
  2550.         return 0;
  2551.     }
  2552.    
  2553.     if (qe->cancel_answered_elsewhere) {
  2554.         ast_set_flag(tmp->chan, AST_FLAG_ANSWERED_ELSEWHERE);
  2555.     }
  2556.     tmp->chan->appl = "AppQueue";
  2557.     tmp->chan->data = "(Outgoing Line)";
  2558.     memset(&tmp->chan->whentohangup, 0, sizeof(tmp->chan->whentohangup));
  2559.     if (tmp->chan->cid.cid_num)
  2560.         ast_free(tmp->chan->cid.cid_num);
  2561.     tmp->chan->cid.cid_num = ast_strdup(qe->chan->cid.cid_num);
  2562.     if (tmp->chan->cid.cid_name)
  2563.         ast_free(tmp->chan->cid.cid_name);
  2564.     tmp->chan->cid.cid_name = ast_strdup(qe->chan->cid.cid_name);
  2565.     if (tmp->chan->cid.cid_ani)
  2566.         ast_free(tmp->chan->cid.cid_ani);
  2567.     tmp->chan->cid.cid_ani = ast_strdup(qe->chan->cid.cid_ani);
  2568.  
  2569.     /* Inherit specially named variables from parent channel */
  2570.     ast_channel_inherit_variables(qe->chan, tmp->chan);
  2571.     ast_channel_datastore_inherit(qe->chan, tmp->chan);
  2572.  
  2573.     /* Presense of ADSI CPE on outgoing channel follows ours */
  2574.     tmp->chan->adsicpe = qe->chan->adsicpe;
  2575.  
  2576.     /* Inherit context and extension */
  2577.     ast_channel_lock(qe->chan);
  2578.     macrocontext = pbx_builtin_getvar_helper(qe->chan, "MACRO_CONTEXT");
  2579.     ast_string_field_set(tmp->chan, dialcontext, ast_strlen_zero(macrocontext) ? qe->chan->context : macrocontext);
  2580.     macroexten = pbx_builtin_getvar_helper(qe->chan, "MACRO_EXTEN");
  2581.     if (!ast_strlen_zero(macroexten))
  2582.         ast_copy_string(tmp->chan->exten, macroexten, sizeof(tmp->chan->exten));
  2583.     else
  2584.         ast_copy_string(tmp->chan->exten, qe->chan->exten, sizeof(tmp->chan->exten));
  2585.     if (ast_cdr_isset_unanswered()) {
  2586.         /* they want to see the unanswered dial attempts! */
  2587.         /* set up the CDR fields on all the CDRs to give sensical information */
  2588.         ast_cdr_setdestchan(tmp->chan->cdr, tmp->chan->name);
  2589.         strcpy(tmp->chan->cdr->clid, qe->chan->cdr->clid);
  2590.         strcpy(tmp->chan->cdr->channel, qe->chan->cdr->channel);
  2591.         strcpy(tmp->chan->cdr->src, qe->chan->cdr->src);
  2592.         strcpy(tmp->chan->cdr->dst, qe->chan->exten);
  2593.         strcpy(tmp->chan->cdr->dcontext, qe->chan->context);
  2594.         strcpy(tmp->chan->cdr->lastapp, qe->chan->cdr->lastapp);
  2595.         strcpy(tmp->chan->cdr->lastdata, qe->chan->cdr->lastdata);
  2596.         tmp->chan->cdr->amaflags = qe->chan->cdr->amaflags;
  2597.         strcpy(tmp->chan->cdr->accountcode, qe->chan->cdr->accountcode);
  2598.         strcpy(tmp->chan->cdr->userfield, qe->chan->cdr->userfield);
  2599.     }
  2600.     ast_channel_unlock(qe->chan);
  2601.  
  2602.     /* Place the call, but don't wait on the answer */
  2603.     if ((res = ast_call(tmp->chan, location, 0))) {
  2604.         /* Again, keep going even if there's an error */
  2605.         ast_debug(1, "ast call on peer returned %d\n", res);
  2606.         ast_verb(3, "Couldn't call %s\n", tmp->interface);
  2607.         do_hang(tmp);
  2608.         (*busies)++;
  2609.         update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
  2610.         return 0;
  2611.     } else if (qe->parent->eventwhencalled) {
  2612.         char vars[2048];
  2613.  
  2614.         manager_event(EVENT_FLAG_AGENT, "AgentCalled",
  2615.                     "Queue: %s\r\n"
  2616.                     "AgentCalled: %s\r\n"
  2617.                     "AgentName: %s\r\n"
  2618.                     "ChannelCalling: %s\r\n"
  2619.                     "DestinationChannel: %s\r\n"
  2620.                     "CallerIDNum: %s\r\n"
  2621.                     "CallerIDName: %s\r\n"
  2622.                     "Context: %s\r\n"
  2623.                     "Extension: %s\r\n"
  2624.                     "Priority: %d\r\n"
  2625.                     "Uniqueid: %s\r\n"
  2626.                     "%s",
  2627.                     qe->parent->name, tmp->interface, tmp->member->membername, qe->chan->name, tmp->chan->name,
  2628.                     tmp->chan->cid.cid_num ? tmp->chan->cid.cid_num : "unknown",
  2629.                     tmp->chan->cid.cid_name ? tmp->chan->cid.cid_name : "unknown",
  2630.                     qe->chan->context, qe->chan->exten, qe->chan->priority, qe->chan->uniqueid,
  2631.                     qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
  2632.         ast_verb(3, "Called %s\n", tmp->interface);
  2633.     }
  2634.  
  2635.     update_status(qe->parent, tmp->member, ast_device_state(tmp->member->state_interface));
  2636.     return 1;
  2637. }
  2638.  
  2639. /*! \brief find the entry with the best metric, or NULL */
  2640. static struct callattempt *find_best(struct callattempt *outgoing)
  2641. {
  2642.     struct callattempt *best = NULL, *cur;
  2643.  
  2644.     for (cur = outgoing; cur; cur = cur->q_next) {
  2645.         if (cur->stillgoing &&                  /* Not already done */
  2646.             !cur->chan &&                   /* Isn't already going */
  2647.             (!best || cur->metric < best->metric)) {        /* We haven't found one yet, or it's better */
  2648.             best = cur;
  2649.         }
  2650.     }
  2651.  
  2652.     return best;
  2653. }
  2654.  
  2655. /*!
  2656.  * \brief Place a call to a queue member.
  2657.  *
  2658.  * Once metrics have been calculated for each member, this function is used
  2659.  * to place a call to the appropriate member (or members). The low-level
  2660.  * channel-handling and error detection is handled in ring_entry
  2661.  *
  2662.  * \retval 1 if a member was called successfully
  2663.  * \retval 0 otherwise
  2664.  */
  2665. static int ring_one(struct queue_ent *qe, struct callattempt *outgoing, int *busies)
  2666. {
  2667.     int ret = 0;
  2668.  
  2669.     while (ret == 0) {
  2670.         struct callattempt *best = find_best(outgoing);
  2671.         if (!best) {
  2672.             ast_debug(1, "Nobody left to try ringing in queue\n");
  2673.             break;
  2674.         }
  2675.         if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
  2676.             struct callattempt *cur;
  2677.             /* Ring everyone who shares this best metric (for ringall) */
  2678.             for (cur = outgoing; cur; cur = cur->q_next) {
  2679.                 if (cur->stillgoing && !cur->chan && cur->metric <= best->metric) {
  2680.                     ast_debug(1, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
  2681.                     ret |= ring_entry(qe, cur, busies);
  2682.                 }
  2683.             }
  2684.         } else {
  2685.             /* Ring just the best channel */
  2686.             ast_debug(1, "Trying '%s' with metric %d\n", best->interface, best->metric);
  2687.             ret = ring_entry(qe, best, busies);
  2688.         }
  2689.        
  2690.         /* If we have timed out, break out */
  2691.         if (qe->expire && (time(NULL) >= qe->expire)) {
  2692.             ast_debug(1, "Queue timed out while ringing members.\n");
  2693.             ret = 0;
  2694.             break;
  2695.         }
  2696.     }
  2697.  
  2698.     return ret;
  2699. }
  2700.  
  2701. /*! \brief Search for best metric and add to Round Robbin queue */
  2702. static int store_next_rr(struct queue_ent *qe, struct callattempt *outgoing)
  2703. {
  2704.     struct callattempt *best = find_best(outgoing);
  2705.  
  2706.     if (best) {
  2707.         /* Ring just the best channel */
  2708.         ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
  2709.         qe->parent->rrpos = best->metric % 1000;
  2710.     } else {
  2711.         /* Just increment rrpos */
  2712.         if (qe->parent->wrapped) {
  2713.             /* No more channels, start over */
  2714.             qe->parent->rrpos = 0;
  2715.         } else {
  2716.             /* Prioritize next entry */
  2717.             qe->parent->rrpos++;
  2718.         }
  2719.     }
  2720.     qe->parent->wrapped = 0;
  2721.  
  2722.     return 0;
  2723. }
  2724.  
  2725. /*! \brief Search for best metric and add to Linear queue */
  2726. static int store_next_lin(struct queue_ent *qe, struct callattempt *outgoing)
  2727. {
  2728.     struct callattempt *best = find_best(outgoing);
  2729.  
  2730.     if (best) {
  2731.         /* Ring just the best channel */
  2732.         ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
  2733.         qe->linpos = best->metric % 1000;
  2734.     } else {
  2735.         /* Just increment rrpos */
  2736.         if (qe->linwrapped) {
  2737.             /* No more channels, start over */
  2738.             qe->linpos = 0;
  2739.         } else {
  2740.             /* Prioritize next entry */
  2741.             qe->linpos++;
  2742.         }
  2743.     }
  2744.     qe->linwrapped = 0;
  2745.  
  2746.     return 0;
  2747. }
  2748.  
  2749. /*! \brief Playback announcement to queued members if peroid has elapsed */
  2750. static int say_periodic_announcement(struct queue_ent *qe, int ringing)
  2751. {
  2752.     int res = 0;
  2753.     time_t now;
  2754.  
  2755.     /* Get the current time */
  2756.     time(&now);
  2757.  
  2758.     /* Check to see if it is time to announce */
  2759.     if ((now - qe->last_periodic_announce_time) < qe->parent->periodicannouncefrequency)
  2760.         return 0;
  2761.  
  2762.     /* Stop the music on hold so we can play our own file */
  2763.     if (ringing)
  2764.         ast_indicate(qe->chan,-1);
  2765.     else
  2766.         ast_moh_stop(qe->chan);
  2767.  
  2768.     ast_verb(3, "Playing periodic announcement\n");
  2769.    
  2770.     if (qe->parent->randomperiodicannounce && qe->parent->numperiodicannounce) {
  2771.         qe->last_periodic_announce_sound = ((unsigned long) ast_random()) % qe->parent->numperiodicannounce;
  2772.     } else if (qe->last_periodic_announce_sound >= qe->parent->numperiodicannounce ||
  2773.         ast_str_strlen(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]) == 0) {
  2774.         qe->last_periodic_announce_sound = 0;
  2775.     }
  2776.    
  2777.     /* play the announcement */
  2778.     res = play_file(qe->chan, ast_str_buffer(qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]));
  2779.  
  2780.     if (res > 0 && !valid_exit(qe, res))
  2781.         res = 0;
  2782.  
  2783.     /* Resume Music on Hold if the caller is going to stay in the queue */
  2784.     if (!res) {
  2785.         if (ringing)
  2786.             ast_indicate(qe->chan, AST_CONTROL_RINGING);
  2787.         else
  2788.             ast_moh_start(qe->chan, qe->moh, NULL);
  2789.     }
  2790.  
  2791.     /* update last_periodic_announce_time */
  2792.     qe->last_periodic_announce_time = now;
  2793.  
  2794.     /* Update the current periodic announcement to the next announcement */
  2795.     if (!qe->parent->randomperiodicannounce) {
  2796.         qe->last_periodic_announce_sound++;
  2797.     }
  2798.    
  2799.     return res;
  2800. }
  2801.  
  2802. /*! \brief Record that a caller gave up on waiting in queue */
  2803. static void record_abandoned(struct queue_ent *qe)
  2804. {
  2805.     ao2_lock(qe->parent);
  2806.     set_queue_variables(qe->parent, qe->chan);
  2807.     manager_event(EVENT_FLAG_AGENT, "QueueCallerAbandon",
  2808.         "Queue: %s\r\n"
  2809.         "Uniqueid: %s\r\n"
  2810.         "Position: %d\r\n"
  2811.         "OriginalPosition: %d\r\n"
  2812.         "HoldTime: %d\r\n",
  2813.         qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
  2814.  
  2815.     qe->parent->callsabandoned++;
  2816.     ao2_unlock(qe->parent);
  2817. }
  2818.  
  2819. static void record_abandoned_old(struct queue_ent *qe, const char *new_event)
  2820. {
  2821.     ao2_lock(qe->parent);
  2822.     set_queue_variables(qe->parent, qe->chan);
  2823.     manager_event(EVENT_FLAG_AGENT, "QueueCallerAbandon",
  2824.         "Queue: %s\r\n"
  2825.         "Uniqueid: %s\r\n"
  2826.         "Position: %d\r\n"
  2827.         "OriginalPosition: %d\r\n"
  2828.         "HoldTime: %d\r\n"
  2829.         "ReplacedBy: %s\r\n"
  2830.         "Deprecated: yes\r\n",
  2831.         qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start),new_event);
  2832.  
  2833.     qe->parent->callsabandoned++;
  2834.     ao2_unlock(qe->parent);
  2835. }
  2836.  
  2837. static void record_exitwithtimeout(struct queue_ent *qe)
  2838. {
  2839.         ao2_lock(qe->parent);
  2840.     set_queue_variables(qe->parent, qe->chan);
  2841.         manager_event(EVENT_FLAG_AGENT, "QueueCallerExitWithTimeout",
  2842.                 "Queue: %s\r\n"
  2843.                 "Uniqueid: %s\r\n"
  2844.                 "Position: %d\r\n"
  2845.                 "OriginalPosition: %d\r\n"
  2846.                 "HoldTime: %d\r\n",
  2847.                 qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
  2848.  
  2849.         qe->parent->callsexitedwithtimeout++;
  2850.     ao2_unlock(qe->parent);
  2851. }
  2852.  
  2853. static void record_exitwithkey(struct queue_ent *qe)
  2854. {
  2855.         ao2_lock(qe->parent);
  2856.     set_queue_variables(qe->parent, qe->chan);
  2857.         manager_event(EVENT_FLAG_AGENT, "QueueCallerExitWithKeys",
  2858.                 "Queue: %s\r\n"
  2859.                 "Uniqueid: %s\r\n"
  2860.                 "Position: %d\r\n"
  2861.                 "OriginalPosition: %d\r\n"
  2862.                 "HoldTime: %d\r\n",
  2863.                 qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
  2864.  
  2865.         qe->parent->callsexitedwithkey++;
  2866.     ao2_unlock(qe->parent);
  2867. }
  2868.  
  2869. static void record_exitempty(struct queue_ent *qe)
  2870. {
  2871.         ao2_lock(qe->parent);
  2872.     set_queue_variables(qe->parent, qe->chan);
  2873.         manager_event(EVENT_FLAG_AGENT, "QueueCallerExitEmpty",
  2874.                 "Queue: %s\r\n"
  2875.                 "Uniqueid: %s\r\n"
  2876.                 "Position: %d\r\n"
  2877.                 "OriginalPosition: %d\r\n"
  2878.                 "HoldTime: %d\r\n",
  2879.                 qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
  2880.  
  2881.         qe->parent->callsexitedempty++;
  2882.     ao2_unlock(qe->parent);
  2883. }
  2884.  
  2885. static void record_exitsyscompat(struct queue_ent *qe)
  2886. {
  2887.         ao2_lock(&qe->parent);
  2888.     set_queue_variables(qe->parent, qe->chan);
  2889.         manager_event(EVENT_FLAG_AGENT, "QueueCallerExitSyscompat",
  2890.                 "Queue: %s\r\n"
  2891.                 "Uniqueid: %s\r\n"
  2892.                 "Position: %d\r\n"
  2893.                 "OriginalPosition: %d\r\n"
  2894.                 "HoldTime: %d\r\n",
  2895.                 qe->parent->name, qe->chan->uniqueid, qe->pos, qe->opos, (int)(time(NULL) - qe->start));
  2896.  
  2897.         qe->parent->callsexitedsyscompat++;
  2898.     ao2_unlock(qe->parent);
  2899. }
  2900.  
  2901. /*! \brief RNA == Ring No Answer. Common code that is executed when we try a queue member and they don't answer. */
  2902. static void rna(int rnatime, struct queue_ent *qe, char *interface, char *membername, int pause)
  2903. {
  2904.     ast_verb(3, "Nobody picked up in %d ms\n", rnatime);
  2905.     if (qe->parent->eventwhencalled) {
  2906.         char vars[2048];
  2907.  
  2908.         manager_event(EVENT_FLAG_AGENT, "AgentRingNoAnswer",
  2909.                         "Queue: %s\r\n"
  2910.                         "Uniqueid: %s\r\n"
  2911.                         "Channel: %s\r\n"
  2912.                         "Member: %s\r\n"
  2913.                         "MemberName: %s\r\n"
  2914.                         "Ringtime: %d\r\n"
  2915.                         "%s",
  2916.                         qe->parent->name,
  2917.                         qe->chan->uniqueid,
  2918.                         qe->chan->name,
  2919.                         interface,
  2920.                         membername,
  2921.                         rnatime,
  2922.                         qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
  2923.     }
  2924.     ast_queue_log(qe->parent->name, qe->chan->uniqueid, membername, "RINGNOANSWER", "%d", rnatime);
  2925.     if (qe->parent->autopause && pause) {
  2926.         if (!set_member_paused(qe->parent->name, interface, "Auto-Pause", 1)) {
  2927.             ast_verb(3, "Auto-Pausing Queue Member %s in queue %s since they failed to answer.\n", interface, qe->parent->name);
  2928.         } else {
  2929.             ast_verb(3, "Failed to pause Queue Member %s in queue %s!\n", interface, qe->parent->name);
  2930.         }
  2931.     }
  2932.     return;
  2933. }
  2934.  
  2935. #define AST_MAX_WATCHERS 256
  2936. /*! \brief Wait for a member to answer the call
  2937.  *
  2938.  * \param[in] qe the queue_ent corresponding to the caller in the queue
  2939.  * \param[in] outgoing the list of callattempts. Relevant ones will have their chan and stillgoing parameters non-zero
  2940.  * \param[in] to the amount of time (in milliseconds) to wait for a response
  2941.  * \param[out] digit if a user presses a digit to exit the queue, this is the digit the caller pressed
  2942.  * \param[in] prebusies number of busy members calculated prior to calling wait_for_answer
  2943.  * \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
  2944.  * \param[in] forwardsallowed used to detect if we should allow call forwarding, based on the 'i' option to Queue()
  2945.  */
  2946. static struct callattempt *wait_for_answer(struct queue_ent *qe, struct callattempt *outgoing, int *to, char *digit, int prebusies, int caller_disconnect, int forwardsallowed)
  2947. {
  2948.     const char *queue = qe->parent->name;
  2949.     struct callattempt *o, *start = NULL, *prev = NULL;
  2950.     int status;
  2951.     int numbusies = prebusies;
  2952.     int numnochan = 0;
  2953.     int stillgoing = 0;
  2954.     int orig = *to;
  2955.     struct ast_frame *f;
  2956.     struct callattempt *peer = NULL;
  2957.     struct ast_channel *winner;
  2958.     struct ast_channel *in = qe->chan;
  2959.     char on[80] = "";
  2960.     char membername[80] = "";
  2961.     long starttime = 0;
  2962.     long endtime = 0;
  2963. #ifdef HAVE_EPOLL
  2964.     struct callattempt *epollo;
  2965. #endif
  2966.  
  2967.     starttime = (long) time(NULL);
  2968. #ifdef HAVE_EPOLL
  2969.     for (epollo = outgoing; epollo; epollo = epollo->q_next) {
  2970.         if (epollo->chan)
  2971.             ast_poll_channel_add(in, epollo->chan);
  2972.     }
  2973. #endif
  2974.    
  2975.     while (*to && !peer) {
  2976.         int numlines, retry, pos = 1;
  2977.         struct ast_channel *watchers[AST_MAX_WATCHERS];
  2978.         watchers[0] = in;
  2979.         start = NULL;
  2980.  
  2981.         for (retry = 0; retry < 2; retry++) {
  2982.             numlines = 0;
  2983.             for (o = outgoing; o; o = o->q_next) { /* Keep track of important channels */
  2984.                 if (o->stillgoing) {    /* Keep track of important channels */
  2985.                     stillgoing = 1;
  2986.                     if (o->chan) {
  2987.                         watchers[pos++] = o->chan;
  2988.                         if (!start)
  2989.                             start = o;
  2990.                         else
  2991.                             prev->call_next = o;
  2992.                         prev = o;
  2993.                     }
  2994.                 }
  2995.                 numlines++;
  2996.             }
  2997.             if (pos > 1 /* found */ || !stillgoing /* nobody listening */ ||
  2998.                 (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) /* ring would not be delivered */)
  2999.                 break;
  3000.             /* On "ringall" strategy we only move to the next penalty level
  3001.                when *all* ringing phones are done in the current penalty level */
  3002.             ring_one(qe, outgoing, &numbusies);
  3003.             /* and retry... */
  3004.         }
  3005.         if (pos == 1 /* not found */) {
  3006.             if (numlines == (numbusies + numnochan)) {
  3007.                 ast_debug(1, "Everyone is busy at this time\n");
  3008.             } else {
  3009.                 ast_debug(3, "No one is answering queue '%s' (%d numlines / %d busies / %d failed channels)\n", queue, numlines, numbusies, numnochan);
  3010.             }
  3011.             *to = 0;
  3012.             return NULL;
  3013.         }
  3014.  
  3015.         /* Poll for events from both the incoming channel as well as any outgoing channels */
  3016.         winner = ast_waitfor_n(watchers, pos, to);
  3017.  
  3018.         /* Service all of the outgoing channels */
  3019.         for (o = start; o; o = o->call_next) {
  3020.             if (o->stillgoing && (o->chan) &&  (o->chan->_state == AST_STATE_UP)) {
  3021.                 if (!peer) {
  3022.                     ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
  3023.                     peer = o;
  3024.                 }
  3025.             } else if (o->chan && (o->chan == winner)) {
  3026.  
  3027.                 ast_copy_string(on, o->member->interface, sizeof(on));
  3028.                 ast_copy_string(membername, o->member->membername, sizeof(membername));
  3029.  
  3030.                 if (!ast_strlen_zero(o->chan->call_forward) && !forwardsallowed) {
  3031.                     ast_verb(3, "Forwarding %s to '%s' prevented.\n", in->name, o->chan->call_forward);
  3032.                     numnochan++;
  3033.                     do_hang(o);
  3034.                     winner = NULL;
  3035.                     continue;
  3036.                 } else if (!ast_strlen_zero(o->chan->call_forward)) {
  3037.                     char tmpchan[256];
  3038.                     char *stuff;
  3039.                     char *tech;
  3040.  
  3041.                     ast_copy_string(tmpchan, o->chan->call_forward, sizeof(tmpchan));
  3042.                     if ((stuff = strchr(tmpchan, '/'))) {
  3043.                         *stuff++ = '\0';
  3044.                         tech = tmpchan;
  3045.                     } else {
  3046.                         snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, o->chan->context);
  3047.                         stuff = tmpchan;
  3048.                         tech = "Local";
  3049.                     }
  3050.                     /* Before processing channel, go ahead and check for forwarding */
  3051.                     ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, o->chan->name);
  3052.                     /* Setup parameters */
  3053.                     o->chan = ast_request(tech, in->nativeformats, stuff, &status);
  3054.                     if (!o->chan) {
  3055.                         ast_log(LOG_NOTICE,
  3056.                             "Forwarding failed to create channel to dial '%s/%s'\n",
  3057.                             tech, stuff);
  3058.                         o->stillgoing = 0;
  3059.                         numnochan++;
  3060.                     } else {
  3061.                         ast_channel_inherit_variables(in, o->chan);
  3062.                         ast_channel_datastore_inherit(in, o->chan);
  3063.                         if (o->chan->cid.cid_num)
  3064.                             ast_free(o->chan->cid.cid_num);
  3065.                         o->chan->cid.cid_num = ast_strdup(in->cid.cid_num);
  3066.  
  3067.                         if (o->chan->cid.cid_name)
  3068.                             ast_free(o->chan->cid.cid_name);
  3069.                         o->chan->cid.cid_name = ast_strdup(in->cid.cid_name);
  3070.  
  3071.                         ast_string_field_set(o->chan, accountcode, in->accountcode);
  3072.                         o->chan->cdrflags = in->cdrflags;
  3073.  
  3074.                         if (in->cid.cid_ani) {
  3075.                             if (o->chan->cid.cid_ani)
  3076.                                 ast_free(o->chan->cid.cid_ani);
  3077.                             o->chan->cid.cid_ani = ast_strdup(in->cid.cid_ani);
  3078.                         }
  3079.                         if (o->chan->cid.cid_rdnis)
  3080.                             ast_free(o->chan->cid.cid_rdnis);
  3081.                         o->chan->cid.cid_rdnis = ast_strdup(S_OR(in->macroexten, in->exten));
  3082.                         if (ast_call(o->chan, stuff, 0)) {
  3083.                             ast_log(LOG_NOTICE, "Forwarding failed to dial '%s/%s'\n",
  3084.                                 tech, stuff);
  3085.                             do_hang(o);
  3086.                             numnochan++;
  3087.                         }
  3088.                     }
  3089.                     /* Hangup the original channel now, in case we needed it */
  3090.                     ast_hangup(winner);
  3091.                     continue;
  3092.                 }
  3093.                 f = ast_read(winner);
  3094.                 if (f) {
  3095.                     if (f->frametype == AST_FRAME_CONTROL) {
  3096.                         switch (f->subclass) {
  3097.                         case AST_CONTROL_ANSWER:
  3098.                             /* This is our guy if someone answered. */
  3099.                             if (!peer) {
  3100.                                 ast_verb(3, "%s answered %s\n", o->chan->name, in->name);
  3101.                                 peer = o;
  3102.                             }
  3103.                             break;
  3104.                         case AST_CONTROL_BUSY:
  3105.                             ast_verb(3, "%s is busy\n", o->chan->name);
  3106.                             if (in->cdr)
  3107.                                 ast_cdr_busy(in->cdr);
  3108.                             do_hang(o);
  3109.                             endtime = (long) time(NULL);
  3110.                             endtime -= starttime;
  3111.                             rna(endtime * 1000, qe, on, membername, 0);
  3112.                             if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
  3113.                                 if (qe->parent->timeoutrestart)
  3114.                                     *to = orig;
  3115.                                 /* Have enough time for a queue member to answer? */
  3116.                                 if (*to > 500) {
  3117.                                     ring_one(qe, outgoing, &numbusies);
  3118.                                     starttime = (long) time(NULL);
  3119.                                 }
  3120.                             }
  3121.                             numbusies++;
  3122.                             break;
  3123.                         case AST_CONTROL_CONGESTION:
  3124.                             ast_verb(3, "%s is circuit-busy\n", o->chan->name);
  3125.                             if (in->cdr)
  3126.                                 ast_cdr_busy(in->cdr);
  3127.                             endtime = (long) time(NULL);
  3128.                             endtime -= starttime;
  3129.                             rna(endtime * 1000, qe, on, membername, 0);
  3130.                             do_hang(o);
  3131.                             if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
  3132.                                 if (qe->parent->timeoutrestart)
  3133.                                     *to = orig;
  3134.                                 if (*to > 500) {
  3135.                                     ring_one(qe, outgoing, &numbusies);
  3136.                                     starttime = (long) time(NULL);
  3137.                                 }
  3138.                             }
  3139.                             numbusies++;
  3140.                             break;
  3141.                         case AST_CONTROL_RINGING:
  3142.                             ast_verb(3, "%s is ringing\n", o->chan->name);
  3143.                             break;
  3144.                         case AST_CONTROL_OFFHOOK:
  3145.                             /* Ignore going off hook */
  3146.                             break;
  3147.                         default:
  3148.                             ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
  3149.                         }
  3150.                     }
  3151.                     ast_frfree(f);
  3152.                 } else { /* ast_read() returned NULL */
  3153.                     endtime = (long) time(NULL) - starttime;
  3154.                     rna(endtime * 1000, qe, on, membername, 1);
  3155.                     do_hang(o);
  3156.                     if (qe->parent->strategy != QUEUE_STRATEGY_RINGALL) {
  3157.                         if (qe->parent->timeoutrestart)
  3158.                             *to = orig;
  3159.                         if (*to > 500) {
  3160.                             ring_one(qe, outgoing, &numbusies);
  3161.                             starttime = (long) time(NULL);
  3162.                         }
  3163.                     }
  3164.                 }
  3165.             }
  3166.         }
  3167.  
  3168.         /* If we received an event from the caller, deal with it. */
  3169.         if (winner == in) {
  3170.             f = ast_read(in);
  3171.             if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
  3172.                 /* Got hung up */
  3173.                 *to = -1;
  3174.                 if (f) {
  3175.                     if (f->data.uint32) {
  3176.                         in->hangupcause = f->data.uint32;
  3177.                     }
  3178.                     ast_frfree(f);
  3179.                 }
  3180.                 return NULL;
  3181.             }
  3182.             if ((f->frametype == AST_FRAME_DTMF) && caller_disconnect && (f->subclass == '*')) {
  3183.                 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
  3184.                 *to = 0;
  3185.                 ast_frfree(f);
  3186.                 return NULL;
  3187.             }
  3188.             if ((f->frametype == AST_FRAME_DTMF) && valid_exit(qe, f->subclass)) {
  3189.                 ast_verb(3, "User pressed digit: %c\n", f->subclass);
  3190.                 *to = 0;
  3191.                 *digit = f->subclass;
  3192.                 ast_frfree(f);
  3193.                 return NULL;
  3194.             }
  3195.             ast_frfree(f);
  3196.         }
  3197.         if (!*to) {
  3198.             for (o = start; o; o = o->call_next)
  3199.                 rna(orig, qe, o->interface, o->member->membername, 1);
  3200.         }
  3201.     }
  3202.  
  3203. #ifdef HAVE_EPOLL
  3204.     for (epollo = outgoing; epollo; epollo = epollo->q_next) {
  3205.         if (epollo->chan)
  3206.             ast_poll_channel_del(in, epollo->chan);
  3207.     }
  3208. #endif
  3209.  
  3210.     return peer;
  3211. }
  3212.  
  3213. /*!
  3214.  * \brief Check if we should start attempting to call queue members.
  3215.  *
  3216.  * A simple process, really. Count the number of members who are available
  3217.  * to take our call and then see if we are in a position in the queue at
  3218.  * which a member could accept our call.
  3219.  *
  3220.  * \param[in] qe The caller who wants to know if it is his turn
  3221.  * \retval 0 It is not our turn
  3222.  * \retval 1 It is our turn
  3223.  */
  3224. static int is_our_turn(struct queue_ent *qe)
  3225. {
  3226.     struct queue_ent *ch;
  3227.     int res;
  3228.     int avl;
  3229.     int idx = 0;
  3230.     /* This needs a lock. How many members are available to be served? */
  3231.     ao2_lock(qe->parent);
  3232.  
  3233.     avl = num_available_members(qe->parent);
  3234.  
  3235.     ch = qe->parent->head;
  3236.  
  3237.     ast_debug(1, "There %s %d available %s.\n", avl != 1 ? "are" : "is", avl, avl != 1 ? "members" : "member");
  3238.  
  3239.     while ((idx < avl) && (ch) && (ch != qe)) {
  3240.         if (!ch->pending)
  3241.             idx++;
  3242.         ch = ch->next;         
  3243.     }
  3244.  
  3245.     ao2_unlock(qe->parent);
  3246.     /* If the queue entry is within avl [the number of available members] calls from the top ...
  3247.      * Autofill and position check added to support autofill=no (as only calls
  3248.      * from the front of the queue are valid when autofill is disabled)
  3249.      */
  3250.     if (ch && idx < avl && (qe->parent->autofill || qe->pos == 1)) {
  3251.         ast_debug(1, "It's our turn (%s).\n", qe->chan->name);
  3252.         res = 1;
  3253.     } else {
  3254.         ast_debug(1, "It's not our turn (%s).\n", qe->chan->name);
  3255.         res = 0;
  3256.     }
  3257.  
  3258.     return res;
  3259. }
  3260.  
  3261. /*!
  3262.  * \brief update rules for queues
  3263.  *
  3264.  * Calculate min/max penalties making sure if relative they stay within bounds.
  3265.  * Update queues penalty and set dialplan vars, goto next list entry.
  3266. */
  3267. static void update_qe_rule(struct queue_ent *qe)
  3268. {
  3269.     int max_penalty = qe->pr->max_relative ? qe->max_penalty + qe->pr->max_value : qe->pr->max_value;
  3270.     int min_penalty = qe->pr->min_relative ? qe->min_penalty + qe->pr->min_value : qe->pr->min_value;
  3271.     char max_penalty_str[20], min_penalty_str[20];
  3272.     /* a relative change to the penalty could put it below 0 */
  3273.     if (max_penalty < 0)
  3274.         max_penalty = 0;
  3275.     if (min_penalty < 0)
  3276.         min_penalty = 0;
  3277.     if (min_penalty > max_penalty)
  3278.         min_penalty = max_penalty;
  3279.     snprintf(max_penalty_str, sizeof(max_penalty_str), "%d", max_penalty);
  3280.     snprintf(min_penalty_str, sizeof(min_penalty_str), "%d", min_penalty);
  3281.     pbx_builtin_setvar_helper(qe->chan, "QUEUE_MAX_PENALTY", max_penalty_str);
  3282.     pbx_builtin_setvar_helper(qe->chan, "QUEUE_MIN_PENALTY", min_penalty_str);
  3283.     qe->max_penalty = max_penalty;
  3284.     qe->min_penalty = min_penalty;
  3285.     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);
  3286.     qe->pr = AST_LIST_NEXT(qe->pr, list);
  3287. }
  3288.  
  3289. /*! \brief The waiting areas for callers who are not actively calling members
  3290.  *
  3291.  * This function is one large loop. This function will return if a caller
  3292.  * either exits the queue or it becomes that caller's turn to attempt calling
  3293.  * queue members. Inside the loop, we service the caller with periodic announcements,
  3294.  * holdtime announcements, etc. as configured in queues.conf
  3295.  *
  3296.  * \retval  0 if the caller's turn has arrived
  3297.  * \retval -1 if the caller should exit the queue.
  3298.  */
  3299. static int wait_our_turn(struct queue_ent *qe, int ringing, enum queue_result *reason)
  3300. {
  3301.     int res = 0;
  3302.  
  3303.     /* This is the holding pen for callers 2 through maxlen */
  3304.     for (;;) {
  3305.  
  3306.         if (is_our_turn(qe))
  3307.             break;
  3308.  
  3309.         /* If we have timed out, break out */
  3310.         if (qe->expire && (time(NULL) >= qe->expire)) {
  3311.             *reason = QUEUE_TIMEOUT;
  3312.             break;
  3313.         }
  3314.  
  3315.         if (qe->parent->leavewhenempty) {
  3316.             int status = 0;
  3317.  
  3318.             if ((status = get_member_status(qe->parent, qe->max_penalty, qe->min_penalty, qe->parent->leavewhenempty))) {
  3319.                 *reason = QUEUE_LEAVEEMPTY;
  3320.                 record_abandoned_old(qe, "QueueCallerExitEmpty");
  3321.                 record_exitempty(qe);
  3322.                 ast_queue_log(qe->parent->name, qe->chan->uniqueid, "NONE", "EXITEMPTY", "%d|%d|%ld", qe->pos, qe->opos, (long) time(NULL) - qe->start);
  3323.                 leave_queue(qe);
  3324.                 break;
  3325.             }
  3326.         }
  3327.  
  3328.         /* Make a position announcement, if enabled */
  3329.         if (qe->parent->announcefrequency &&
  3330.             (res = say_position(qe,ringing)))
  3331.             break;
  3332.  
  3333.         /* If we have timed out, break out */
  3334.         if (qe->expire && (time(NULL) >= qe->expire)) {
  3335.             *reason = QUEUE_TIMEOUT;
  3336.             break;
  3337.         }
  3338.  
  3339.         /* Make a periodic announcement, if enabled */
  3340.         if (qe->parent->periodicannouncefrequency &&
  3341.             (res = say_periodic_announcement(qe,ringing)))
  3342.             break;
  3343.        
  3344.         /* see if we need to move to the next penalty level for this queue */
  3345.         while (qe->pr && ((time(NULL) - qe->start) >= qe->pr->time)) {
  3346.             update_qe_rule(qe);
  3347.         }
  3348.  
  3349.         /* If we have timed out, break out */
  3350.         if (qe->expire && (time(NULL) >= qe->expire)) {
  3351.             *reason = QUEUE_TIMEOUT;
  3352.             break;
  3353.         }
  3354.        
  3355.         /* Wait a second before checking again */
  3356.         if ((res = ast_waitfordigit(qe->chan, RECHECK * 1000))) {
  3357.             if (res > 0 && !valid_exit(qe, res))
  3358.                 res = 0;
  3359.             else
  3360.                 break;
  3361.         }
  3362.        
  3363.         /* If we have timed out, break out */
  3364.         if (qe->expire && (time(NULL) >= qe->expire)) {
  3365.             *reason = QUEUE_TIMEOUT;
  3366.             break;
  3367.         }
  3368.     }
  3369.  
  3370.     return res;
  3371. }
  3372.  
  3373. /*!
  3374.  * \brief update the queue status
  3375.  * \retval Always 0
  3376. */
  3377. static int update_queue(struct call_queue *q, struct member *member, int callcompletedinsl, int newtalktime)
  3378. {
  3379.     int oldtalktime;
  3380.  
  3381.     struct member *mem;
  3382.     struct call_queue *qtmp;
  3383.     struct ao2_iterator queue_iter;
  3384.    
  3385.     if (shared_lastcall) {
  3386.         queue_iter = ao2_iterator_init(queues, 0);
  3387.         while ((qtmp = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  3388.             ao2_lock(qtmp);
  3389.             if ((mem = ao2_find(qtmp->members, member, OBJ_POINTER))) {
  3390.                 time(&mem->lastcall);
  3391.                 mem->calls++;
  3392.                 mem->lastqueue = q;
  3393.                 ao2_ref(mem, -1);
  3394.             }
  3395.             ao2_unlock(qtmp);
  3396.             queue_t_unref(qtmp, "Done with iterator");
  3397.         }
  3398.         ao2_iterator_destroy(&queue_iter);
  3399.     } else {
  3400.         ao2_lock(q);
  3401.         time(&member->lastcall);
  3402.         member->calls++;
  3403.         member->lastqueue = q;
  3404.         ao2_unlock(q);
  3405.     }  
  3406.     ao2_lock(q);
  3407.     q->callscompleted++;
  3408.     if (callcompletedinsl)
  3409.         q->callscompletedinsl++;
  3410.     /* Calculate talktime using the same exponential average as holdtime code*/
  3411.     oldtalktime = q->talktime;
  3412.     q->talktime = (((oldtalktime << 2) - oldtalktime) + newtalktime) >> 2;
  3413.     ao2_unlock(q);
  3414.     return 0;
  3415. }
  3416.  
  3417. /*! \brief Calculate the metric of each member in the outgoing callattempts
  3418.  *
  3419.  * A numeric metric is given to each member depending on the ring strategy used
  3420.  * by the queue. Members with lower metrics will be called before members with
  3421.  * higher metrics
  3422.  * \retval -1 if penalties are exceeded
  3423.  * \retval 0 otherwise
  3424.  */
  3425. static int calc_metric(struct call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct callattempt *tmp)
  3426. {
  3427.     if ((qe->max_penalty && (mem->penalty > qe->max_penalty)) || (qe->min_penalty && (mem->penalty < qe->min_penalty)))
  3428.         return -1;
  3429.  
  3430.     switch (q->strategy) {
  3431.     case QUEUE_STRATEGY_RINGALL:
  3432.         /* Everyone equal, except for penalty */
  3433.         tmp->metric = mem->penalty * 1000000;
  3434.         break;
  3435.     case QUEUE_STRATEGY_LINEAR:
  3436.         if (pos < qe->linpos) {
  3437.             tmp->metric = 1000 + pos;
  3438.         } else {
  3439.             if (pos > qe->linpos)
  3440.                 /* Indicate there is another priority */
  3441.                 qe->linwrapped = 1;
  3442.             tmp->metric = pos;
  3443.         }
  3444.         tmp->metric += mem->penalty * 1000000;
  3445.         break;
  3446.     case QUEUE_STRATEGY_RRMEMORY:
  3447.         if (pos < q->rrpos) {
  3448.             tmp->metric = 1000 + pos;
  3449.         } else {
  3450.             if (pos > q->rrpos)
  3451.                 /* Indicate there is another priority */
  3452.                 q->wrapped = 1;
  3453.             tmp->metric = pos;
  3454.         }
  3455.         tmp->metric += mem->penalty * 1000000;
  3456.         break;
  3457.     case QUEUE_STRATEGY_RANDOM:
  3458.         tmp->metric = ast_random() % 1000;
  3459.         tmp->metric += mem->penalty * 1000000;
  3460.         break;
  3461.     case QUEUE_STRATEGY_WRANDOM:
  3462.         tmp->metric = ast_random() % ((1 + mem->penalty) * 1000);
  3463.         break;
  3464.     case QUEUE_STRATEGY_FEWESTCALLS:
  3465.         tmp->metric = mem->calls;
  3466.         tmp->metric += mem->penalty * 1000000;
  3467.         break;
  3468.     case QUEUE_STRATEGY_LEASTRECENT:
  3469.         if (!mem->lastcall)
  3470.             tmp->metric = 0;
  3471.         else
  3472.             tmp->metric = 1000000 - (time(NULL) - mem->lastcall);
  3473.         tmp->metric += mem->penalty * 1000000;
  3474.         break;
  3475.     default:
  3476.         ast_log(LOG_WARNING, "Can't calculate metric for unknown strategy %d\n", q->strategy);
  3477.         break;
  3478.     }
  3479.     return 0;
  3480. }
  3481.  
  3482. enum agent_complete_reason {
  3483.     CALLER,
  3484.     AGENT,
  3485.     TRANSFER
  3486. };
  3487.  
  3488. /*! \brief Send out AMI message with member call completion status information */
  3489. static void send_agent_complete(const struct queue_ent *qe, const char *queuename,
  3490.     const struct ast_channel *peer, const struct member *member, time_t callstart,
  3491.     char *vars, size_t vars_len, enum agent_complete_reason rsn)
  3492. {
  3493.     const char *reason = NULL;  /* silence dumb compilers */
  3494.  
  3495.     if (!qe->parent->eventwhencalled)
  3496.         return;
  3497.  
  3498.     switch (rsn) {
  3499.     case CALLER:
  3500.         reason = "caller";
  3501.         break;
  3502.     case AGENT:
  3503.         reason = "agent";
  3504.         break;
  3505.     case TRANSFER:
  3506.         reason = "transfer";
  3507.         break;
  3508.     }
  3509.  
  3510.     manager_event(EVENT_FLAG_AGENT, "AgentComplete",
  3511.         "Queue: %s\r\n"
  3512.         "Uniqueid: %s\r\n"
  3513.         "Channel: %s\r\n"
  3514.         "Member: %s\r\n"
  3515.         "MemberName: %s\r\n"
  3516.         "HoldTime: %ld\r\n"
  3517.         "TalkTime: %ld\r\n"
  3518.         "Reason: %s\r\n"
  3519.         "%s",
  3520.         queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
  3521.         (long)(callstart - qe->start), (long)(time(NULL) - callstart), reason,
  3522.         qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, vars_len) : "");
  3523. }
  3524.  
  3525. struct queue_transfer_ds {
  3526.     struct queue_ent *qe;
  3527.     struct member *member;
  3528.     time_t starttime;
  3529.     int callcompletedinsl;
  3530. };
  3531.  
  3532. static void queue_transfer_destroy(void *data)
  3533. {
  3534.     struct queue_transfer_ds *qtds = data;
  3535.     ast_free(qtds);
  3536. }
  3537.  
  3538. /*! \brief a datastore used to help correctly log attended transfers of queue callers
  3539.  */
  3540. static const struct ast_datastore_info queue_transfer_info = {
  3541.     .type = "queue_transfer",
  3542.     .chan_fixup = queue_transfer_fixup,
  3543.     .destroy = queue_transfer_destroy,
  3544. };
  3545.  
  3546. /*! \brief Log an attended transfer when a queue caller channel is masqueraded
  3547.  *
  3548.  * When a caller is masqueraded, we want to log a transfer. Fixup time is the closest we can come to when
  3549.  * the actual transfer occurs. This happens during the masquerade after datastores are moved from old_chan
  3550.  * to new_chan. This is why new_chan is referenced for exten, context, and datastore information.
  3551.  *
  3552.  * At the end of this, we want to remove the datastore so that this fixup function is not called on any
  3553.  * future masquerades of the caller during the current call.
  3554.  */
  3555. static void queue_transfer_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  3556. {
  3557.     struct queue_transfer_ds *qtds = data;
  3558.     struct queue_ent *qe = qtds->qe;
  3559.     struct member *member = qtds->member;
  3560.     time_t callstart = qtds->starttime;
  3561.     int callcompletedinsl = qtds->callcompletedinsl;
  3562.     struct ast_datastore *datastore;
  3563.  
  3564.     ast_queue_log(qe->parent->name, qe->chan->uniqueid, member->membername, "TRANSFER", "%s|%s|%ld|%ld|%d",
  3565.                 new_chan->exten, new_chan->context, (long) (callstart - qe->start),
  3566.                 (long) (time(NULL) - callstart), qe->opos);
  3567.  
  3568.     update_queue(qe->parent, member, callcompletedinsl, (time(NULL) - callstart));
  3569.    
  3570.     /* No need to lock the channels because they are already locked in ast_do_masquerade */
  3571.     if ((datastore = ast_channel_datastore_find(old_chan, &queue_transfer_info, NULL))) {
  3572.         ast_channel_datastore_remove(old_chan, datastore);
  3573.     } else {
  3574.         ast_log(LOG_WARNING, "Can't find the queue_transfer datastore.\n");
  3575.     }
  3576. }
  3577.  
  3578. /*! \brief mechanism to tell if a queue caller was atxferred by a queue member.
  3579.  *
  3580.  * When a caller is atxferred, then the queue_transfer_info datastore
  3581.  * is removed from the channel. If it's still there after the bridge is
  3582.  * broken, then the caller was not atxferred.
  3583.  *
  3584.  * \note Only call this with chan locked
  3585.  */
  3586. static int attended_transfer_occurred(struct ast_channel *chan)
  3587. {
  3588.     return ast_channel_datastore_find(chan, &queue_transfer_info, NULL) ? 0 : 1;
  3589. }
  3590.  
  3591. /*! \brief create a datastore for storing relevant info to log attended transfers in the queue_log
  3592.  */
  3593. static struct ast_datastore *setup_transfer_datastore(struct queue_ent *qe, struct member *member, time_t starttime, int callcompletedinsl)
  3594. {
  3595.     struct ast_datastore *ds;
  3596.     struct queue_transfer_ds *qtds = ast_calloc(1, sizeof(*qtds));
  3597.  
  3598.     if (!qtds) {
  3599.         ast_log(LOG_WARNING, "Memory allocation error!\n");
  3600.         return NULL;
  3601.     }
  3602.  
  3603.     ast_channel_lock(qe->chan);
  3604.     if (!(ds = ast_datastore_alloc(&queue_transfer_info, NULL))) {
  3605.         ast_channel_unlock(qe->chan);
  3606.         ast_log(LOG_WARNING, "Unable to create transfer datastore. queue_log will not show attended transfer\n");
  3607.         return NULL;
  3608.     }
  3609.  
  3610.     qtds->qe = qe;
  3611.     /* This member is refcounted in try_calling, so no need to add it here, too */
  3612.     qtds->member = member;
  3613.     qtds->starttime = starttime;
  3614.     qtds->callcompletedinsl = callcompletedinsl;
  3615.     ds->data = qtds;
  3616.     ast_channel_datastore_add(qe->chan, ds);
  3617.     ast_channel_unlock(qe->chan);
  3618.     return ds;
  3619. }
  3620.  
  3621. struct queue_end_bridge {
  3622.     struct call_queue *q;
  3623.     struct ast_channel *chan;
  3624. };
  3625.  
  3626. static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
  3627. {
  3628.     struct queue_end_bridge *qeb = bconfig->end_bridge_callback_data;
  3629.     ao2_ref(qeb, +1);
  3630.     qeb->chan = originator;
  3631. }
  3632.  
  3633. static void end_bridge_callback(void *data)
  3634. {
  3635.     struct queue_end_bridge *qeb = data;
  3636.     struct call_queue *q = qeb->q;
  3637.     struct ast_channel *chan = qeb->chan;
  3638.  
  3639.     if (ao2_ref(qeb, -1) == 1) {
  3640.         ao2_lock(q);
  3641.         set_queue_variables(q, chan);
  3642.         ao2_unlock(q);
  3643.         /* This unrefs the reference we made in try_calling when we allocated qeb */
  3644.         queue_t_unref(q, "Expire bridge_config reference");
  3645.     }
  3646. }
  3647.  
  3648. /*! \brief A large function which calls members, updates statistics, and bridges the caller and a member
  3649.  *
  3650.  * Here is the process of this function
  3651.  * 1. Process any options passed to the Queue() application. Options here mean the third argument to Queue()
  3652.  * 2. Iterate trough the members of the queue, creating a callattempt corresponding to each member. During this
  3653.  *    iteration, we also check the dialed_interfaces datastore to see if we have already attempted calling this
  3654.  *    member. If we have, we do not create a callattempt. This is in place to prevent call forwarding loops. Also
  3655.  *    during each iteration, we call calc_metric to determine which members should be rung when.
  3656.  * 3. Call ring_one to place a call to the appropriate member(s)
  3657.  * 4. Call wait_for_answer to wait for an answer. If no one answers, return.
  3658.  * 5. Take care of any holdtime announcements, member delays, or other options which occur after a call has been answered.
  3659.  * 6. Start the monitor or mixmonitor if the option is set
  3660.  * 7. Remove the caller from the queue to allow other callers to advance
  3661.  * 8. Bridge the call.
  3662.  * 9. Do any post processing after the call has disconnected.
  3663.  *
  3664.  * \param[in] qe the queue_ent structure which corresponds to the caller attempting to reach members
  3665.  * \param[in] options the options passed as the third parameter to the Queue() application
  3666.  * \param[in] announceoverride filename to play to user when waiting
  3667.  * \param[in] url the url passed as the fourth parameter to the Queue() application
  3668.  * \param[in,out] tries the number of times we have tried calling queue members
  3669.  * \param[out] noption set if the call to Queue() has the 'n' option set.
  3670.  * \param[in] agi the agi passed as the fifth parameter to the Queue() application
  3671.  * \param[in] macro the macro passed as the sixth parameter to the Queue() application
  3672.  * \param[in] gosub the gosub passed as the seventh parameter to the Queue() application
  3673.  * \param[in] ringing 1 if the 'r' option is set, otherwise 0
  3674.  */
  3675. 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)
  3676. {
  3677.     struct member *cur;
  3678.     struct callattempt *outgoing = NULL; /* the list of calls we are building */
  3679.     int to, orig;
  3680.     char oldexten[AST_MAX_EXTENSION]="";
  3681.     char oldcontext[AST_MAX_CONTEXT]="";
  3682.     char queuename[256]="";
  3683.     char interfacevar[256]="";
  3684.     struct ast_channel *peer;
  3685.     struct ast_channel *which;
  3686.     struct callattempt *lpeer;
  3687.     struct member *member;
  3688.     struct ast_app *application;
  3689.     int res = 0, bridge = 0;
  3690.     int numbusies = 0;
  3691.     int x=0;
  3692.     char *announce = NULL;
  3693.     char digit = 0;
  3694.     time_t callstart;
  3695.     time_t now = time(NULL);
  3696.     struct ast_bridge_config bridge_config;
  3697.     char nondataquality = 1;
  3698.     char *agiexec = NULL;
  3699.     char *macroexec = NULL;
  3700.     char *gosubexec = NULL;
  3701.     int ret = 0;
  3702.     const char *monitorfilename;
  3703.     const char *monitor_exec;
  3704.     const char *monitor_options;
  3705.     char tmpid[256], tmpid2[256];
  3706.     char meid[1024], meid2[1024];
  3707.     char mixmonargs[1512];
  3708.     struct ast_app *mixmonapp = NULL;
  3709.     char *p;
  3710.     char vars[2048];
  3711.     int forwardsallowed = 1;
  3712.     int callcompletedinsl;
  3713.     struct ao2_iterator memi;
  3714.     struct ast_datastore *datastore, *transfer_ds;
  3715.     struct queue_end_bridge *queue_end_bridge = NULL;
  3716.     const int need_weight = use_weight;
  3717.  
  3718.     ast_channel_lock(qe->chan);
  3719.     datastore = ast_channel_datastore_find(qe->chan, &dialed_interface_info, NULL);
  3720.     ast_channel_unlock(qe->chan);
  3721.  
  3722.     memset(&bridge_config, 0, sizeof(bridge_config));
  3723.     tmpid[0] = 0;
  3724.     meid[0] = 0;
  3725.     time(&now);
  3726.  
  3727.     /* If we've already exceeded our timeout, then just stop
  3728.      * This should be extremely rare. queue_exec will take care
  3729.      * of removing the caller and reporting the timeout as the reason.
  3730.      */
  3731.     if (qe->expire && now >= qe->expire) {
  3732.         res = 0;
  3733.         goto out;
  3734.     }
  3735.        
  3736.     for (; options && *options; options++)
  3737.         switch (*options) {
  3738.         case 't':
  3739.             ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_REDIRECT);
  3740.             break;
  3741.         case 'T':
  3742.             ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_REDIRECT);
  3743.             break;
  3744.         case 'w':
  3745.             ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_AUTOMON);
  3746.             break;
  3747.         case 'W':
  3748.             ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMON);
  3749.             break;
  3750.         case 'c':
  3751.             ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_NO_H_EXTEN);
  3752.             break;
  3753.         case 'd':
  3754.             nondataquality = 0;
  3755.             break;
  3756.         case 'h':
  3757.             ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_DISCONNECT);
  3758.             break;
  3759.         case 'H':
  3760.             ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_DISCONNECT);
  3761.             break;
  3762.         case 'k':
  3763.             ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_PARKCALL);
  3764.             break;
  3765.         case 'K':
  3766.             ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_PARKCALL);
  3767.             break;
  3768.         case 'n':
  3769.             if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY || qe->parent->strategy == QUEUE_STRATEGY_LINEAR)
  3770.                 (*tries)++;
  3771.             else
  3772.                 *tries = qe->parent->membercount;
  3773.             *noption = 1;
  3774.             break;
  3775.         case 'i':
  3776.             forwardsallowed = 0;
  3777.             break;
  3778.         case 'x':
  3779.             ast_set_flag(&(bridge_config.features_callee), AST_FEATURE_AUTOMIXMON);
  3780.             break;
  3781.         case 'X':
  3782.             ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMIXMON);
  3783.             break;
  3784.         case 'C':
  3785.             qe->cancel_answered_elsewhere = 1;
  3786.             break;
  3787.  
  3788.         }
  3789.  
  3790.     /* if the calling channel has the ANSWERED_ELSEWHERE flag set, make sure this is inherited.
  3791.         (this is mainly to support chan_local)
  3792.     */
  3793.     if (ast_test_flag(qe->chan, AST_FLAG_ANSWERED_ELSEWHERE)) {
  3794.         qe->cancel_answered_elsewhere = 1;
  3795.     }
  3796.  
  3797.     /* Hold the lock while we setup the outgoing calls */
  3798.     if (need_weight)
  3799.         ao2_lock(queues);
  3800.     ao2_lock(qe->parent);
  3801.     ast_debug(1, "%s is trying to call a queue member.\n",
  3802.                             qe->chan->name);
  3803.     ast_copy_string(queuename, qe->parent->name, sizeof(queuename));
  3804.     if (!ast_strlen_zero(qe->announce))
  3805.         announce = qe->announce;
  3806.     if (!ast_strlen_zero(announceoverride))
  3807.         announce = announceoverride;
  3808.  
  3809.     memi = ao2_iterator_init(qe->parent->members, 0);
  3810.     while ((cur = ao2_iterator_next(&memi))) {
  3811.         struct callattempt *tmp = ast_calloc(1, sizeof(*tmp));
  3812.         struct ast_dialed_interface *di;
  3813.         AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
  3814.         if (!tmp) {
  3815.             ao2_ref(cur, -1);
  3816.             ao2_unlock(qe->parent);
  3817.             ao2_iterator_destroy(&memi);
  3818.             if (need_weight)
  3819.                 ao2_unlock(queues);
  3820.             goto out;
  3821.         }
  3822.         if (!datastore) {
  3823.             if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
  3824.                 ao2_ref(cur, -1);
  3825.                 ao2_unlock(qe->parent);
  3826.                 ao2_iterator_destroy(&memi);
  3827.                 if (need_weight)
  3828.                     ao2_unlock(queues);
  3829.                 free(tmp);
  3830.                 goto out;
  3831.             }
  3832.             datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  3833.             if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
  3834.                 ao2_ref(cur, -1);
  3835.                 ao2_unlock(&qe->parent);
  3836.                 ao2_iterator_destroy(&memi);
  3837.                 if (need_weight)
  3838.                     ao2_unlock(queues);
  3839.                 free(tmp);
  3840.                 goto out;
  3841.             }
  3842.             datastore->data = dialed_interfaces;
  3843.             AST_LIST_HEAD_INIT(dialed_interfaces);
  3844.  
  3845.             ast_channel_lock(qe->chan);
  3846.             ast_channel_datastore_add(qe->chan, datastore);
  3847.             ast_channel_unlock(qe->chan);
  3848.         } else
  3849.             dialed_interfaces = datastore->data;
  3850.  
  3851.         AST_LIST_LOCK(dialed_interfaces);
  3852.         AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
  3853.             if (!strcasecmp(cur->interface, di->interface)) {
  3854.                 ast_debug(1, "Skipping dialing interface '%s' since it has already been dialed\n",
  3855.                     di->interface);
  3856.                 break;
  3857.             }
  3858.         }
  3859.         AST_LIST_UNLOCK(dialed_interfaces);
  3860.        
  3861.         if (di) {
  3862.             free(tmp);
  3863.             continue;
  3864.         }
  3865.  
  3866.         /* It is always ok to dial a Local interface.  We only keep track of
  3867.          * which "real" interfaces have been dialed.  The Local channel will
  3868.          * inherit this list so that if it ends up dialing a real interface,
  3869.          * it won't call one that has already been called. */
  3870.         if (strncasecmp(cur->interface, "Local/", 6)) {
  3871.             if (!(di = ast_calloc(1, sizeof(*di) + strlen(cur->interface)))) {
  3872.                 ao2_ref(cur, -1);
  3873.                 ao2_unlock(qe->parent);
  3874.                 ao2_iterator_destroy(&memi);
  3875.                 if (need_weight)
  3876.                     ao2_unlock(queues);
  3877.                 free(tmp);
  3878.                 goto out;
  3879.             }
  3880.             strcpy(di->interface, cur->interface);
  3881.  
  3882.             AST_LIST_LOCK(dialed_interfaces);
  3883.             AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
  3884.             AST_LIST_UNLOCK(dialed_interfaces);
  3885.         }
  3886.  
  3887.         tmp->stillgoing = -1;
  3888.         tmp->member = cur;
  3889.         tmp->oldstatus = cur->status;
  3890.         tmp->lastcall = cur->lastcall;
  3891.         tmp->lastqueue = cur->lastqueue;
  3892.         ast_copy_string(tmp->interface, cur->interface, sizeof(tmp->interface));
  3893.         /* Special case: If we ring everyone, go ahead and ring them, otherwise
  3894.            just calculate their metric for the appropriate strategy */
  3895.         if (!calc_metric(qe->parent, cur, x++, qe, tmp)) {
  3896.             /* Put them in the list of outgoing thingies...  We're ready now.
  3897.                XXX If we're forcibly removed, these outgoing calls won't get
  3898.                hung up XXX */
  3899.             tmp->q_next = outgoing;
  3900.             outgoing = tmp;    
  3901.             /* If this line is up, don't try anybody else */
  3902.             if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
  3903.                 break;
  3904.         } else {
  3905.             ao2_ref(cur, -1);
  3906.             ast_free(tmp);
  3907.         }
  3908.     }
  3909.     ao2_iterator_destroy(&memi);
  3910.  
  3911.     if (qe->parent->timeoutpriority == TIMEOUT_PRIORITY_APP) {
  3912.         /* Application arguments have higher timeout priority (behaviour for <=1.6) */
  3913.         if (qe->expire && (!qe->parent->timeout || (qe->expire - now) <= qe->parent->timeout))
  3914.             to = (qe->expire - now) * 1000;
  3915.         else
  3916.             to = (qe->parent->timeout) ? qe->parent->timeout * 1000 : -1;
  3917.     } else {
  3918.         /* Config timeout is higher priority thatn application timeout */
  3919.         if (qe->expire && qe->expire<=now) {
  3920.             to = 0;
  3921.         } else if (qe->parent->timeout) {
  3922.             to = qe->parent->timeout * 1000;
  3923.         } else {
  3924.             to = -1;
  3925.         }
  3926.     }
  3927.     orig = to;
  3928.     ++qe->pending;
  3929.     ao2_unlock(qe->parent);
  3930.     ring_one(qe, outgoing, &numbusies);
  3931.     if (need_weight)
  3932.         ao2_unlock(queues);
  3933.     lpeer = wait_for_answer(qe, outgoing, &to, &digit, numbusies, ast_test_flag(&(bridge_config.features_caller), AST_FEATURE_DISCONNECT), forwardsallowed);
  3934.     /* The ast_channel_datastore_remove() function could fail here if the
  3935.      * datastore was moved to another channel during a masquerade. If this is
  3936.      * the case, don't free the datastore here because later, when the channel
  3937.      * to which the datastore was moved hangs up, it will attempt to free this
  3938.      * datastore again, causing a crash
  3939.      */
  3940.     ast_channel_lock(qe->chan);
  3941.     if (datastore && !ast_channel_datastore_remove(qe->chan, datastore)) {
  3942.         ast_datastore_free(datastore);
  3943.     }
  3944.     ast_channel_unlock(qe->chan);
  3945.     ao2_lock(qe->parent);
  3946.     if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY) {
  3947.         store_next_rr(qe, outgoing);
  3948.     }
  3949.     if (qe->parent->strategy == QUEUE_STRATEGY_LINEAR) {
  3950.         store_next_lin(qe, outgoing);
  3951.     }
  3952.     ao2_unlock(qe->parent);
  3953.     peer = lpeer ? lpeer->chan : NULL;
  3954.     if (!peer) {
  3955.         qe->pending = 0;
  3956.         if (to) {
  3957.             /* Must gotten hung up */
  3958.             res = -1;
  3959.         } else {
  3960.             /* User exited by pressing a digit */
  3961.             res = digit;
  3962.         }
  3963.         if (res == -1)
  3964.             ast_debug(1, "%s: Nobody answered.\n", qe->chan->name);
  3965.         if (ast_cdr_isset_unanswered()) {
  3966.             /* channel contains the name of one of the outgoing channels
  3967.                in its CDR; zero out this CDR to avoid a dual-posting */
  3968.             struct callattempt *o;
  3969.             for (o = outgoing; o; o = o->q_next) {
  3970.                 if (!o->chan) {
  3971.                     continue;
  3972.                 }
  3973.                 if (strcmp(o->chan->cdr->dstchannel, qe->chan->cdr->dstchannel) == 0) {
  3974.                     ast_set_flag(o->chan->cdr, AST_CDR_FLAG_POST_DISABLED);
  3975.                     break;
  3976.                 }
  3977.             }
  3978.         }
  3979.     } else { /* peer is valid */
  3980.         /* Ah ha!  Someone answered within the desired timeframe.  Of course after this
  3981.            we will always return with -1 so that it is hung up properly after the
  3982.            conversation.  */
  3983.         if (!strcmp(qe->chan->tech->type, "DAHDI"))
  3984.             ast_channel_setoption(qe->chan, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);
  3985.         if (!strcmp(peer->tech->type, "DAHDI"))
  3986.             ast_channel_setoption(peer, AST_OPTION_TONE_VERIFY, &nondataquality, sizeof(nondataquality), 0);
  3987.         /* Update parameters for the queue */
  3988.         time(&now);
  3989.         recalc_holdtime(qe, (now - qe->start));
  3990.         ao2_lock(qe->parent);
  3991.         callcompletedinsl = ((now - qe->start) <= qe->parent->servicelevel);
  3992.         ao2_unlock(qe->parent);
  3993.         member = lpeer->member;
  3994.         /* Increment the refcount for this member, since we're going to be using it for awhile in here. */
  3995.         ao2_ref(member, 1);
  3996.         hangupcalls(outgoing, peer, qe->cancel_answered_elsewhere);
  3997.         outgoing = NULL;
  3998.         if (announce || qe->parent->reportholdtime || qe->parent->memberdelay) {
  3999.             int res2;
  4000.  
  4001.             res2 = ast_autoservice_start(qe->chan);
  4002.             if (!res2) {
  4003.                 if (qe->parent->memberdelay) {
  4004.                     ast_log(LOG_NOTICE, "Delaying member connect for %d seconds\n", qe->parent->memberdelay);
  4005.                     res2 |= ast_safe_sleep(peer, qe->parent->memberdelay * 1000);
  4006.                 }
  4007.                 if (!res2 && announce) {
  4008.                     play_file(peer, announce);
  4009.                 }
  4010.                 if (!res2 && qe->parent->reportholdtime) {
  4011.                     if (!play_file(peer, qe->parent->sound_reporthold)) {
  4012.                         int holdtime, holdtimesecs;
  4013.  
  4014.                         time(&now);
  4015.                         holdtime = abs((now - qe->start) / 60);
  4016.                         holdtimesecs = abs((now - qe->start) % 60);
  4017.                         if (holdtime > 0) {
  4018.                             ast_say_number(peer, holdtime, AST_DIGIT_ANY, peer->language, NULL);
  4019.                             play_file(peer, qe->parent->sound_minutes);
  4020.                         }
  4021.                         if (holdtimesecs > 1) {
  4022.                             ast_say_number(peer, holdtimesecs, AST_DIGIT_ANY, peer->language, NULL);
  4023.                             play_file(peer, qe->parent->sound_seconds);
  4024.                         }
  4025.                     }
  4026.                 }
  4027.             }
  4028.             res2 |= ast_autoservice_stop(qe->chan);
  4029.             if (ast_check_hangup(peer)) {
  4030.                 /* Agent must have hung up */
  4031.                 ast_log(LOG_WARNING, "Agent on %s hungup on the customer.\n", peer->name);
  4032.                 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "AGENTDUMP", "%s", "");
  4033.                 if (qe->parent->eventwhencalled)
  4034.                     manager_event(EVENT_FLAG_AGENT, "AgentDump",
  4035.                             "Queue: %s\r\n"
  4036.                             "Uniqueid: %s\r\n"
  4037.                             "Channel: %s\r\n"
  4038.                             "Member: %s\r\n"
  4039.                             "MemberName: %s\r\n"
  4040.                             "%s",
  4041.                             queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
  4042.                             qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
  4043.                 ast_hangup(peer);
  4044.                 ao2_ref(member, -1);
  4045.                 goto out;
  4046.             } else if (res2) {
  4047.                 /* Caller must have hung up just before being connected*/
  4048.                 ast_log(LOG_NOTICE, "Caller was about to talk to agent on %s but the caller hungup.\n", peer->name);
  4049.                 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "ABANDON", "%d|%d|%ld", qe->pos, qe->opos, (long) time(NULL) - qe->start);
  4050.                 record_abandoned(qe);
  4051.                 ast_hangup(peer);
  4052.                 ao2_ref(member, -1);
  4053.                 return -1;
  4054.             }
  4055.         }
  4056.         /* Stop music on hold */
  4057.         if (ringing)
  4058.             ast_indicate(qe->chan,-1);
  4059.         else
  4060.             ast_moh_stop(qe->chan);
  4061.         /* If appropriate, log that we have a destination channel */
  4062.         if (qe->chan->cdr)
  4063.             ast_cdr_setdestchan(qe->chan->cdr, peer->name);
  4064.         /* Make sure channels are compatible */
  4065.         res = ast_channel_make_compatible(qe->chan, peer);
  4066.         if (res < 0) {
  4067.             ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "SYSCOMPAT", "%s", "");
  4068.             ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
  4069.             record_abandoned_old(qe, "QueueCallerExitSyscompat");
  4070.             record_exitsyscompat(qe);
  4071.             ast_cdr_failed(qe->chan->cdr);
  4072.             ast_hangup(peer);
  4073.             ao2_ref(member, -1);
  4074.             return -1;
  4075.         }
  4076.  
  4077.         /* Play announcement to the caller telling it's his turn if defined */
  4078.         if (!ast_strlen_zero(qe->parent->sound_callerannounce)) {
  4079.             if (play_file(qe->chan, qe->parent->sound_callerannounce))
  4080.                 ast_log(LOG_WARNING, "Announcement file '%s' is unavailable, continuing anyway...\n", qe->parent->sound_callerannounce);
  4081.         }
  4082.  
  4083.         ao2_lock(qe->parent);
  4084.         /* if setinterfacevar is defined, make member variables available to the channel */
  4085.         /* use  pbx_builtin_setvar to set a load of variables with one call */
  4086.         if (qe->parent->setinterfacevar) {
  4087.             snprintf(interfacevar, sizeof(interfacevar), "MEMBERINTERFACE=%s,MEMBERNAME=%s,MEMBERCALLS=%d,MEMBERLASTCALL=%ld,MEMBERPENALTY=%d,MEMBERDYNAMIC=%d,MEMBERREALTIME=%d",
  4088.                 member->interface, member->membername, member->calls, (long)member->lastcall, member->penalty, member->dynamic, member->realtime);
  4089.             pbx_builtin_setvar_multiple(qe->chan, interfacevar);
  4090.             pbx_builtin_setvar_multiple(peer, interfacevar);
  4091.         }
  4092.        
  4093.         /* if setqueueentryvar is defined, make queue entry (i.e. the caller) variables available to the channel */
  4094.         /* use  pbx_builtin_setvar to set a load of variables with one call */
  4095.         if (qe->parent->setqueueentryvar) {
  4096.             snprintf(interfacevar, sizeof(interfacevar), "QEHOLDTIME=%ld,QEORIGINALPOS=%d",
  4097.                 (long) time(NULL) - qe->start, qe->opos);
  4098.             pbx_builtin_setvar_multiple(qe->chan, interfacevar);
  4099.             pbx_builtin_setvar_multiple(peer, interfacevar);
  4100.         }
  4101.    
  4102.         /* try to set queue variables if configured to do so*/
  4103.         set_queue_variables(qe->parent, qe->chan);
  4104.         set_queue_variables(qe->parent, peer);
  4105.         ao2_unlock(qe->parent);
  4106.        
  4107.         ast_channel_lock(qe->chan);
  4108.         if ((monitorfilename = pbx_builtin_getvar_helper(qe->chan, "MONITOR_FILENAME"))) {
  4109.                 monitorfilename = ast_strdupa(monitorfilename);
  4110.         }
  4111.         ast_channel_unlock(qe->chan);
  4112.         /* Begin Monitoring */
  4113.         if (qe->parent->monfmt && *qe->parent->monfmt) {
  4114.             if (!qe->parent->montype) {
  4115.                 const char *monexec, *monargs;
  4116.                 ast_debug(1, "Starting Monitor as requested.\n");
  4117.                 ast_channel_lock(qe->chan);
  4118.                 if ((monexec = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC")) || (monargs = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC_ARGS"))) {
  4119.                     which = qe->chan;
  4120.                     monexec = monexec ? ast_strdupa(monexec) : NULL;
  4121.                 }
  4122.                 else
  4123.                     which = peer;
  4124.                 ast_channel_unlock(qe->chan);
  4125.                 if (ast_monitor_start) {
  4126.                     if (monitorfilename) {
  4127.                         ast_monitor_start(which, qe->parent->monfmt, monitorfilename, 1, X_REC_IN | X_REC_OUT);
  4128.                     } else if (qe->chan->cdr && ast_monitor_start) {
  4129.                         ast_monitor_start(which, qe->parent->monfmt, qe->chan->cdr->uniqueid, 1, X_REC_IN | X_REC_OUT);
  4130.                     } else if (ast_monitor_start) {
  4131.                         /* Last ditch effort -- no CDR, make up something */
  4132.                         snprintf(tmpid, sizeof(tmpid), "chan-%lx", ast_random());
  4133.                         ast_monitor_start(which, qe->parent->monfmt, tmpid, 1, X_REC_IN | X_REC_OUT);
  4134.                     }
  4135.                 }
  4136.                 if (!ast_strlen_zero(monexec) && ast_monitor_setjoinfiles) {
  4137.                     ast_monitor_setjoinfiles(which, 1);
  4138.                 }
  4139.             } else {
  4140.                 mixmonapp = pbx_findapp("MixMonitor");
  4141.                
  4142.                 if (mixmonapp) {
  4143.                     ast_debug(1, "Starting MixMonitor as requested.\n");
  4144.                     if (!monitorfilename) {
  4145.                         if (qe->chan->cdr)
  4146.                             ast_copy_string(tmpid, qe->chan->cdr->uniqueid, sizeof(tmpid));
  4147.                         else
  4148.                             snprintf(tmpid, sizeof(tmpid), "chan-%lx", ast_random());
  4149.                     } else {
  4150.                         const char *m = monitorfilename;
  4151.                         for (p = tmpid2; p < tmpid2 + sizeof(tmpid2) - 1; p++, m++) {
  4152.                             switch (*m) {
  4153.                             case '^':
  4154.                                 if (*(m + 1) == '{')
  4155.                                     *p = '$';
  4156.                                 break;
  4157.                             case ',':
  4158.                                 *p++ = '\\';
  4159.                                 /* Fall through */
  4160.                             default:
  4161.                                 *p = *m;
  4162.                             }
  4163.                             if (*m == '\0')
  4164.                                 break;
  4165.                         }
  4166.                         if (p == tmpid2 + sizeof(tmpid2))
  4167.                             tmpid2[sizeof(tmpid2) - 1] = '\0';
  4168.  
  4169.                         pbx_substitute_variables_helper(qe->chan, tmpid2, tmpid, sizeof(tmpid) - 1);
  4170.                     }
  4171.  
  4172.                     ast_channel_lock(qe->chan);
  4173.                     if ((monitor_exec = pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC"))) {
  4174.                             monitor_exec = ast_strdupa(monitor_exec);
  4175.                     }
  4176.                     if ((monitor_options = pbx_builtin_getvar_helper(qe->chan, "MONITOR_OPTIONS"))) {
  4177.                             monitor_options = ast_strdupa(monitor_options);
  4178.                     } else {
  4179.                         monitor_options = "";
  4180.                     }
  4181.                     ast_channel_unlock(qe->chan);
  4182.  
  4183.                     if (monitor_exec) {
  4184.                         const char *m = monitor_exec;
  4185.                         for (p = meid2; p < meid2 + sizeof(meid2) - 1; p++, m++) {
  4186.                             switch (*m) {
  4187.                             case '^':
  4188.                                 if (*(m + 1) == '{')
  4189.                                     *p = '$';
  4190.                                 break;
  4191.                             case ',':
  4192.                                 *p++ = '\\';
  4193.                                 /* Fall through */
  4194.                             default:
  4195.                                 *p = *m;
  4196.                             }
  4197.                             if (*m == '\0')
  4198.                                 break;
  4199.                         }
  4200.                         if (p == meid2 + sizeof(meid2))
  4201.                             meid2[sizeof(meid2) - 1] = '\0';
  4202.  
  4203.                         pbx_substitute_variables_helper(qe->chan, meid2, meid, sizeof(meid) - 1);
  4204.                     }
  4205.    
  4206.                     snprintf(tmpid2, sizeof(tmpid2), "%s.%s", tmpid, qe->parent->monfmt);
  4207.  
  4208.                     if (!ast_strlen_zero(monitor_exec))
  4209.                         snprintf(mixmonargs, sizeof(mixmonargs), "%s,b%s,%s", tmpid2, monitor_options, monitor_exec);
  4210.                     else
  4211.                         snprintf(mixmonargs, sizeof(mixmonargs), "%s,b%s", tmpid2, monitor_options);
  4212.                    
  4213.                     ast_debug(1, "Arguments being passed to MixMonitor: %s\n", mixmonargs);
  4214.                     /* We purposely lock the CDR so that pbx_exec does not update the application data */
  4215.                     if (qe->chan->cdr)
  4216.                         ast_set_flag(qe->chan->cdr, AST_CDR_FLAG_LOCKED);
  4217.                     ret = pbx_exec(qe->chan, mixmonapp, mixmonargs);
  4218.                     if (qe->chan->cdr)
  4219.                         ast_clear_flag(qe->chan->cdr, AST_CDR_FLAG_LOCKED);
  4220.  
  4221.                 } else {
  4222.                     ast_log(LOG_WARNING, "Asked to run MixMonitor on this call, but cannot find the MixMonitor app!\n");
  4223.                 }
  4224.             }
  4225.         }
  4226.         /* Drop out of the queue at this point, to prepare for next caller */
  4227.         leave_queue(qe);           
  4228.         if (!ast_strlen_zero(url) && ast_channel_supports_html(peer)) {
  4229.             ast_debug(1, "app_queue: sendurl=%s.\n", url);
  4230.             ast_channel_sendurl(peer, url);
  4231.         }
  4232.        
  4233.         /* run a macro for this connection if defined. The macro simply returns, no action is taken on the result */
  4234.         /* use macro from dialplan if passed as a option, otherwise use the default queue macro */
  4235.         if (!ast_strlen_zero(macro)) {
  4236.                 macroexec = ast_strdupa(macro);
  4237.         } else {
  4238.             if (qe->parent->membermacro)
  4239.                 macroexec = ast_strdupa(qe->parent->membermacro);
  4240.         }
  4241.  
  4242.         if (!ast_strlen_zero(macroexec)) {
  4243.             ast_debug(1, "app_queue: macro=%s.\n", macroexec);
  4244.            
  4245.             res = ast_autoservice_start(qe->chan);
  4246.             if (res) {
  4247.                 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
  4248.                 res = -1;
  4249.             }
  4250.            
  4251.             application = pbx_findapp("Macro");
  4252.  
  4253.             if (application) {
  4254.                 res = pbx_exec(peer, application, macroexec);
  4255.                 ast_debug(1, "Macro exited with status %d\n", res);
  4256.                 res = 0;
  4257.             } else {
  4258.                 ast_log(LOG_ERROR, "Could not find application Macro\n");
  4259.                 res = -1;
  4260.             }
  4261.  
  4262.             if (ast_autoservice_stop(qe->chan) < 0) {
  4263.                 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
  4264.                 res = -1;
  4265.             }
  4266.         }
  4267.  
  4268.         /* run a gosub for this connection if defined. The gosub simply returns, no action is taken on the result */
  4269.         /* use gosub from dialplan if passed as a option, otherwise use the default queue gosub */
  4270.         if (!ast_strlen_zero(gosub)) {
  4271.                 gosubexec = ast_strdupa(gosub);
  4272.         } else {
  4273.             if (qe->parent->membergosub)
  4274.                 gosubexec = ast_strdupa(qe->parent->membergosub);
  4275.         }
  4276.  
  4277.         if (!ast_strlen_zero(gosubexec)) {
  4278.             ast_debug(1, "app_queue: gosub=%s.\n", gosubexec);
  4279.            
  4280.             res = ast_autoservice_start(qe->chan);
  4281.             if (res) {
  4282.                 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
  4283.                 res = -1;
  4284.             }
  4285.            
  4286.             application = pbx_findapp("Gosub");
  4287.            
  4288.             if (application) {
  4289.                 char *gosub_args, *gosub_argstart;
  4290.  
  4291.                 /* Set where we came from */
  4292.                 ast_copy_string(peer->context, "app_queue_gosub_virtual_context", sizeof(peer->context));
  4293.                 ast_copy_string(peer->exten, "s", sizeof(peer->exten));
  4294.                 peer->priority = 0;
  4295.  
  4296.                 gosub_argstart = strchr(gosubexec, ',');
  4297.                 if (gosub_argstart) {
  4298.                     *gosub_argstart = 0;
  4299.                     if (asprintf(&gosub_args, "%s,s,1(%s)", gosubexec, gosub_argstart + 1) < 0) {
  4300.                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
  4301.                         gosub_args = NULL;
  4302.                     }
  4303.                     *gosub_argstart = ',';
  4304.                 } else {
  4305.                     if (asprintf(&gosub_args, "%s,s,1", gosubexec) < 0) {
  4306.                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
  4307.                         gosub_args = NULL;
  4308.                     }
  4309.                 }
  4310.                 if (gosub_args) {
  4311.                     res = pbx_exec(peer, application, gosub_args);
  4312.                     if (!res) {
  4313.                         struct ast_pbx_args args;
  4314.                         memset(&args, 0, sizeof(args));
  4315.                         args.no_hangup_chan = 1;
  4316.                         ast_pbx_run_args(peer, &args);
  4317.                     }
  4318.                     ast_free(gosub_args);
  4319.                     ast_debug(1, "Gosub exited with status %d\n", res);
  4320.                 } else {
  4321.                     ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
  4322.                 }
  4323.             } else {
  4324.                 ast_log(LOG_ERROR, "Could not find application Gosub\n");
  4325.                 res = -1;
  4326.             }
  4327.        
  4328.             if (ast_autoservice_stop(qe->chan) < 0) {
  4329.                 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
  4330.                 res = -1;
  4331.             }
  4332.         }
  4333.  
  4334.         if (!ast_strlen_zero(agi)) {
  4335.             ast_debug(1, "app_queue: agi=%s.\n", agi);
  4336.             application = pbx_findapp("agi");
  4337.             if (application) {
  4338.                 agiexec = ast_strdupa(agi);
  4339.                 ret = pbx_exec(qe->chan, application, agiexec);
  4340.             } else
  4341.                 ast_log(LOG_WARNING, "Asked to execute an AGI on this channel, but could not find application (agi)!\n");
  4342.         }
  4343.         qe->handled++;
  4344.         ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "CONNECT", "%ld|%s|%ld", (long) time(NULL) - qe->start, peer->uniqueid,
  4345.                                                     (long)(orig - to > 0 ? (orig - to) / 1000 : 0));
  4346.         if (update_cdr && qe->chan->cdr)
  4347.             ast_copy_string(qe->chan->cdr->dstchannel, member->membername, sizeof(qe->chan->cdr->dstchannel));
  4348.         if (qe->parent->eventwhencalled)
  4349.             manager_event(EVENT_FLAG_AGENT, "AgentConnect",
  4350.                     "Queue: %s\r\n"
  4351.                     "Uniqueid: %s\r\n"
  4352.                     "Channel: %s\r\n"
  4353.                     "Member: %s\r\n"
  4354.                     "MemberName: %s\r\n"
  4355.                     "Holdtime: %ld\r\n"
  4356.                     "BridgedChannel: %s\r\n"
  4357.                     "Ringtime: %ld\r\n"
  4358.                     "%s",
  4359.                     queuename, qe->chan->uniqueid, peer->name, member->interface, member->membername,
  4360.                     (long) time(NULL) - qe->start, peer->uniqueid, (long)(orig - to > 0 ? (orig - to) / 1000 : 0),
  4361.                     qe->parent->eventwhencalled == QUEUE_EVENT_VARIABLES ? vars2manager(qe->chan, vars, sizeof(vars)) : "");
  4362.         ast_copy_string(oldcontext, qe->chan->context, sizeof(oldcontext));
  4363.         ast_copy_string(oldexten, qe->chan->exten, sizeof(oldexten));
  4364.    
  4365.         if ((queue_end_bridge = ao2_alloc(sizeof(*queue_end_bridge), NULL))) {
  4366.             queue_end_bridge->q = qe->parent;
  4367.             queue_end_bridge->chan = qe->chan;
  4368.             bridge_config.end_bridge_callback = end_bridge_callback;
  4369.             bridge_config.end_bridge_callback_data = queue_end_bridge;
  4370.             bridge_config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
  4371.             /* Since queue_end_bridge can survive beyond the life of this call to Queue, we need
  4372.              * to make sure to increase the refcount of this queue so it cannot be freed until we
  4373.              * are done with it. We remove this reference in end_bridge_callback.
  4374.              */
  4375.             queue_t_ref(qe->parent, "For bridge_config reference");
  4376.         }
  4377.  
  4378.         time(&callstart);
  4379.         transfer_ds = setup_transfer_datastore(qe, member, callstart, callcompletedinsl);
  4380.         bridge = ast_bridge_call(qe->chan,peer, &bridge_config);
  4381.  
  4382.         /* If the queue member did an attended transfer, then the TRANSFER already was logged in the queue_log
  4383.          * when the masquerade occurred. These other "ending" queue_log messages are unnecessary, except for
  4384.          * the AgentComplete manager event
  4385.          */
  4386.         ast_channel_lock(qe->chan);
  4387.         if (!attended_transfer_occurred(qe->chan)) {
  4388.             struct ast_datastore *tds;
  4389.  
  4390.             /* detect a blind transfer */
  4391.             if (!(qe->chan->_softhangup | peer->_softhangup) && (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten))) {
  4392.                 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "TRANSFER", "%s|%s|%ld|%ld|%d",
  4393.                     qe->chan->exten, qe->chan->context, (long) (callstart - qe->start),
  4394.                     (long) (time(NULL) - callstart), qe->opos);
  4395.                 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), TRANSFER);
  4396.             } else if (ast_check_hangup(qe->chan)) {
  4397.                 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "COMPLETECALLER", "%ld|%ld|%d",
  4398.                     (long) (callstart - qe->start), (long) (time(NULL) - callstart), qe->opos);
  4399.                 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), CALLER);
  4400.             } else {
  4401.                 ast_queue_log(queuename, qe->chan->uniqueid, member->membername, "COMPLETEAGENT", "%ld|%ld|%d",
  4402.                     (long) (callstart - qe->start), (long) (time(NULL) - callstart), qe->opos);
  4403.                 send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), AGENT);
  4404.             }
  4405.             if ((tds = ast_channel_datastore_find(qe->chan, &queue_transfer_info, NULL))) {
  4406.                 ast_channel_datastore_remove(qe->chan, tds);
  4407.             }
  4408.             update_queue(qe->parent, member, callcompletedinsl, (time(NULL) - callstart));
  4409.         } else {
  4410.             /* We already logged the TRANSFER on the queue_log, but we still need to send the AgentComplete event */
  4411.             send_agent_complete(qe, queuename, peer, member, callstart, vars, sizeof(vars), TRANSFER);
  4412.         }
  4413.  
  4414.         if (transfer_ds) {
  4415.             ast_datastore_free(transfer_ds);
  4416.         }
  4417.         ast_channel_unlock(qe->chan);
  4418.         ast_hangup(peer);
  4419.         res = bridge ? bridge : 1;
  4420.         ao2_ref(member, -1);
  4421.     }
  4422. out:
  4423.     hangupcalls(outgoing, NULL, qe->cancel_answered_elsewhere);
  4424.  
  4425.     return res;
  4426. }
  4427.  
  4428. static int wait_a_bit(struct queue_ent *qe)
  4429. {
  4430.     /* Don't need to hold the lock while we setup the outgoing calls */
  4431.     int retrywait = qe->parent->retry * 1000;
  4432.  
  4433.     int res = ast_waitfordigit(qe->chan, retrywait);
  4434.     if (res > 0 && !valid_exit(qe, res))
  4435.         res = 0;
  4436.  
  4437.     return res;
  4438. }
  4439.  
  4440. static struct member *interface_exists(struct call_queue *q, const char *interface)
  4441. {
  4442.     struct member *mem;
  4443.     struct ao2_iterator mem_iter;
  4444.  
  4445.     if (!q)
  4446.         return NULL;
  4447.  
  4448.     mem_iter = ao2_iterator_init(q->members, 0);
  4449.     while ((mem = ao2_iterator_next(&mem_iter))) {
  4450.         if (!strcasecmp(interface, mem->interface)) {
  4451.             ao2_iterator_destroy(&mem_iter);
  4452.             return mem;
  4453.         }
  4454.         ao2_ref(mem, -1);
  4455.     }
  4456.     ao2_iterator_destroy(&mem_iter);
  4457.  
  4458.     return NULL;
  4459. }
  4460.  
  4461.  
  4462. /*! \brief Dump all members in a specific queue to the database
  4463.  *
  4464.  * <pm_family>/<queuename> = <interface>;<penalty>;<paused>;<state_interface>[|...]
  4465.  */
  4466. static void dump_queue_members(struct call_queue *pm_queue)
  4467. {
  4468.     struct member *cur_member;
  4469.     char value[PM_MAX_LEN];
  4470.     int value_len = 0;
  4471.     int res;
  4472.     struct ao2_iterator mem_iter;
  4473.  
  4474.     memset(value, 0, sizeof(value));
  4475.  
  4476.     if (!pm_queue)
  4477.         return;
  4478.  
  4479.     mem_iter = ao2_iterator_init(pm_queue->members, 0);
  4480.     while ((cur_member = ao2_iterator_next(&mem_iter))) {
  4481.         if (!cur_member->dynamic) {
  4482.             ao2_ref(cur_member, -1);
  4483.             continue;
  4484.         }
  4485.  
  4486.         res = snprintf(value + value_len, sizeof(value) - value_len, "%s%s;%d;%d;%s;%s",
  4487.             value_len ? "|" : "", cur_member->interface, cur_member->penalty, cur_member->paused, cur_member->membername, cur_member->state_interface);
  4488.  
  4489.         ao2_ref(cur_member, -1);
  4490.  
  4491.         if (res != strlen(value + value_len)) {
  4492.             ast_log(LOG_WARNING, "Could not create persistent member string, out of space\n");
  4493.             break;
  4494.         }
  4495.         value_len += res;
  4496.     }
  4497.     ao2_iterator_destroy(&mem_iter);
  4498.    
  4499.     if (value_len && !cur_member) {
  4500.         if (ast_db_put(pm_family, pm_queue->name, value))
  4501.             ast_log(LOG_WARNING, "failed to create persistent dynamic entry!\n");
  4502.     } else
  4503.         /* Delete the entry if the queue is empty or there is an error */
  4504.         ast_db_del(pm_family, pm_queue->name);
  4505. }
  4506.  
  4507. /*! \brief Remove member from queue
  4508.  * \retval RES_NOT_DYNAMIC when they aren't a RT member
  4509.  * \retval RES_NOSUCHQUEUE queue does not exist
  4510.  * \retval RES_OKAY removed member from queue
  4511.  * \retval RES_EXISTS queue exists but no members
  4512. */
  4513. static int remove_from_queue(const char *queuename, const char *interface)
  4514. {
  4515.     struct call_queue *q, tmpq;
  4516.     struct member *mem, tmpmem;
  4517.     int res = RES_NOSUCHQUEUE;
  4518.  
  4519.     ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
  4520.     ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
  4521.     if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Temporary reference for interface removal"))) {
  4522.         ao2_lock(queues);
  4523.         ao2_lock(q);
  4524.         if ((mem = ao2_find(q->members, &tmpmem, OBJ_POINTER))) {
  4525.             /* XXX future changes should beware of this assumption!! */
  4526.             if (!mem->dynamic) {
  4527.                 ao2_ref(mem, -1);
  4528.                 ao2_unlock(q);
  4529.                 queue_t_unref(q, "Interface wasn't dynamic, expiring temporary reference");
  4530.                 ao2_unlock(queues);
  4531.                 return RES_NOT_DYNAMIC;
  4532.             }
  4533.             q->membercount--;
  4534.             manager_event(EVENT_FLAG_AGENT, "QueueMemberRemoved",
  4535.                 "Queue: %s\r\n"
  4536.                 "Location: %s\r\n"
  4537.                 "MemberName: %s\r\n",
  4538.                 q->name, mem->interface, mem->membername);
  4539.             ao2_unlink(q->members, mem);
  4540.             ao2_ref(mem, -1);
  4541.  
  4542.             if (queue_persistent_members)
  4543.                 dump_queue_members(q);
  4544.            
  4545.             res = RES_OKAY;
  4546.         } else {
  4547.             res = RES_EXISTS;
  4548.         }
  4549.         ao2_unlock(q);
  4550.         ao2_unlock(queues);
  4551.         queue_t_unref(q, "Expiring temporary reference");
  4552.     }
  4553.  
  4554.     return res;
  4555. }
  4556.  
  4557. /*! \brief Add member to queue
  4558.  * \retval RES_NOT_DYNAMIC when they aren't a RT member
  4559.  * \retval RES_NOSUCHQUEUE queue does not exist
  4560.  * \retval RES_OKAY added member from queue
  4561.  * \retval RES_EXISTS queue exists but no members
  4562.  * \retval RES_OUT_OF_MEMORY queue exists but not enough memory to create member
  4563. */
  4564. static int add_to_queue(const char *queuename, const char *interface, const char *membername, int penalty, int paused, int dump, const char *state_interface)
  4565. {
  4566.     struct call_queue *q;
  4567.     struct member *new_member, *old_member;
  4568.     int res = RES_NOSUCHQUEUE;
  4569.  
  4570.     /*! \note Ensure the appropriate realtime queue is loaded.  Note that this
  4571.      * short-circuits if the queue is already in memory. */
  4572.     if (!(q = load_realtime_queue(queuename)))
  4573.         return res;
  4574.  
  4575.     ao2_lock(queues);
  4576.  
  4577.     ao2_lock(q);
  4578.     if ((old_member = interface_exists(q, interface)) == NULL) {
  4579.         if ((new_member = create_queue_member(interface, membername, penalty, paused, state_interface))) {
  4580.             new_member->dynamic = 1;
  4581.             ao2_link(q->members, new_member);
  4582.             q->membercount++;
  4583.             manager_event(EVENT_FLAG_AGENT, "QueueMemberAdded",
  4584.                 "Queue: %s\r\n"
  4585.                 "Location: %s\r\n"
  4586.                 "MemberName: %s\r\n"
  4587.                 "Membership: %s\r\n"
  4588.                 "Penalty: %d\r\n"
  4589.                 "CallsTaken: %d\r\n"
  4590.                 "LastCall: %d\r\n"
  4591.                 "Status: %d\r\n"
  4592.                 "Paused: %d\r\n",
  4593.                 q->name, new_member->interface, new_member->membername,
  4594.                 "dynamic",
  4595.                 new_member->penalty, new_member->calls, (int) new_member->lastcall,
  4596.                 new_member->status, new_member->paused);
  4597.            
  4598.             ao2_ref(new_member, -1);
  4599.             new_member = NULL;
  4600.  
  4601.             if (dump)
  4602.                 dump_queue_members(q);
  4603.            
  4604.             res = RES_OKAY;
  4605.         } else {
  4606.             res = RES_OUTOFMEMORY;
  4607.         }
  4608.     } else {
  4609.         ao2_ref(old_member, -1);
  4610.         res = RES_EXISTS;
  4611.     }
  4612.     ao2_unlock(q);
  4613.     ao2_unlock(queues);
  4614.     queue_t_unref(q, "Expiring temporary reference");
  4615.  
  4616.     return res;
  4617. }
  4618.  
  4619. static int set_member_paused(const char *queuename, const char *interface, const char *reason, int paused)
  4620. {
  4621.     int found = 0;
  4622.     struct call_queue *q;
  4623.     struct member *mem;
  4624.     struct ao2_iterator queue_iter;
  4625.     int failed;
  4626.  
  4627.     /* Special event for when all queues are paused - individual events still generated */
  4628.     /* XXX In all other cases, we use the membername, but since this affects all queues, we cannot */
  4629.     if (ast_strlen_zero(queuename))
  4630.         ast_queue_log("NONE", "NONE", interface, (paused ? "PAUSEALL" : "UNPAUSEALL"), "%s", "");
  4631.  
  4632.     queue_iter = ao2_iterator_init(queues, 0);
  4633.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate over queues"))) {
  4634.         ao2_lock(q);
  4635.         if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename)) {
  4636.             if ((mem = interface_exists(q, interface))) {
  4637.                 if (mem->paused == paused) {
  4638.                     ast_debug(1, "%spausing already-%spaused queue member %s:%s\n", (paused ? "" : "un"), (paused ? "" : "un"), q->name, interface);
  4639.                 }
  4640.  
  4641.                 failed = 0;
  4642.                 if (mem->realtime) {
  4643.                     failed = update_realtime_member_field(mem, q->name, "paused", paused ? "1" : "0");
  4644.                 }
  4645.            
  4646.                 if (failed) {
  4647.                     ast_log(LOG_WARNING, "Failed %spausing realtime queue member %s:%s\n", (paused ? "" : "un"), q->name, interface);
  4648.                     ao2_ref(mem, -1);
  4649.                     ao2_unlock(q);
  4650.                     queue_t_unref(q, "Done with iterator");
  4651.                     continue;
  4652.                 }  
  4653.                 found++;
  4654.                 mem->paused = paused;
  4655.  
  4656.                 if (queue_persistent_members)
  4657.                     dump_queue_members(q);
  4658.  
  4659.                 ast_queue_log(q->name, "NONE", mem->membername, (paused ? "PAUSE" : "UNPAUSE"), "%s", S_OR(reason, ""));
  4660.                
  4661.                 if (!ast_strlen_zero(reason)) {
  4662.                     manager_event(EVENT_FLAG_AGENT, "QueueMemberPaused",
  4663.                         "Queue: %s\r\n"
  4664.                         "Location: %s\r\n"
  4665.                         "MemberName: %s\r\n"
  4666.                         "Paused: %d\r\n"
  4667.                         "Reason: %s\r\n",
  4668.                             q->name, mem->interface, mem->membername, paused, reason);
  4669.                 } else {
  4670.                     manager_event(EVENT_FLAG_AGENT, "QueueMemberPaused",
  4671.                         "Queue: %s\r\n"
  4672.                         "Location: %s\r\n"
  4673.                         "MemberName: %s\r\n"
  4674.                         "Paused: %d\r\n",
  4675.                             q->name, mem->interface, mem->membername, paused);
  4676.                 }
  4677.                 ao2_ref(mem, -1);
  4678.             }
  4679.         }
  4680.        
  4681.         if (!ast_strlen_zero(queuename) && !strcasecmp(queuename, q->name)) {
  4682.             ao2_unlock(q);
  4683.             queue_t_unref(q, "Done with iterator");
  4684.             break;
  4685.         }
  4686.        
  4687.         ao2_unlock(q);
  4688.         queue_t_unref(q, "Done with iterator");
  4689.     }
  4690.     ao2_iterator_destroy(&queue_iter);
  4691.  
  4692.     return found ? RESULT_SUCCESS : RESULT_FAILURE;
  4693. }
  4694.  
  4695. /* \brief Sets members penalty, if queuename=NULL we set member penalty in all the queues. */
  4696. static int set_member_penalty(char *queuename, char *interface, int penalty)
  4697. {
  4698.     int foundinterface = 0, foundqueue = 0;
  4699.     struct call_queue *q;
  4700.     struct member *mem;
  4701.     struct ao2_iterator queue_iter;
  4702.  
  4703.     if (penalty < 0) {
  4704.         ast_log(LOG_ERROR, "Invalid penalty (%d)\n", penalty);
  4705.         return RESULT_FAILURE;
  4706.     }
  4707.  
  4708.     queue_iter = ao2_iterator_init(queues, 0);
  4709.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  4710.         ao2_lock(q);
  4711.         if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename)) {
  4712.             foundqueue++;
  4713.             if ((mem = interface_exists(q, interface))) {
  4714.                 foundinterface++;
  4715.                 mem->penalty = penalty;
  4716.                
  4717.                 ast_queue_log(q->name, "NONE", interface, "PENALTY", "%d", penalty);
  4718.                 manager_event(EVENT_FLAG_AGENT, "QueueMemberPenalty",
  4719.                     "Queue: %s\r\n"
  4720.                     "Location: %s\r\n"
  4721.                     "Penalty: %d\r\n",
  4722.                     q->name, mem->interface, penalty);
  4723.                 ao2_ref(mem, -1);
  4724.             }
  4725.         }
  4726.         ao2_unlock(q);
  4727.         queue_t_unref(q, "Done with iterator");
  4728.     }
  4729.     ao2_iterator_destroy(&queue_iter);
  4730.  
  4731.     if (foundinterface) {
  4732.         return RESULT_SUCCESS;
  4733.     } else if (!foundqueue) {
  4734.         ast_log (LOG_ERROR, "Invalid queuename\n");
  4735.     } else {
  4736.         ast_log (LOG_ERROR, "Invalid interface\n");
  4737.     }  
  4738.  
  4739.     return RESULT_FAILURE;
  4740. }
  4741.  
  4742. /* \brief Gets members penalty.
  4743.  * \return Return the members penalty or RESULT_FAILURE on error.
  4744. */
  4745. static int get_member_penalty(char *queuename, char *interface)
  4746. {
  4747.     int foundqueue = 0, penalty;
  4748.     struct call_queue *q, tmpq;
  4749.     struct member *mem;
  4750.    
  4751.     ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
  4752.     if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Search for queue"))) {
  4753.         foundqueue = 1;
  4754.         ao2_lock(q);
  4755.         if ((mem = interface_exists(q, interface))) {
  4756.             penalty = mem->penalty;
  4757.             ao2_ref(mem, -1);
  4758.             ao2_unlock(q);
  4759.             queue_t_unref(q, "Search complete");
  4760.             return penalty;
  4761.         }
  4762.         ao2_unlock(q);
  4763.         queue_t_unref(q, "Search complete");
  4764.     }
  4765.  
  4766.     /* some useful debuging */
  4767.     if (foundqueue)
  4768.         ast_log (LOG_ERROR, "Invalid queuename\n");
  4769.     else
  4770.         ast_log (LOG_ERROR, "Invalid interface\n");
  4771.  
  4772.     return RESULT_FAILURE;
  4773. }
  4774.  
  4775. /*! \brief Reload dynamic queue members persisted into the astdb */
  4776. static void reload_queue_members(void)
  4777. {
  4778.     char *cur_ptr;
  4779.     const char *queue_name;
  4780.     char *member;
  4781.     char *interface;
  4782.     char *membername = NULL;
  4783.     char *state_interface;
  4784.     char *penalty_tok;
  4785.     int penalty = 0;
  4786.     char *paused_tok;
  4787.     int paused = 0;
  4788.     struct ast_db_entry *db_tree;
  4789.     struct ast_db_entry *entry;
  4790.     struct call_queue *cur_queue;
  4791.     char queue_data[PM_MAX_LEN];
  4792.  
  4793.     ao2_lock(queues);
  4794.  
  4795.     /* Each key in 'pm_family' is the name of a queue */
  4796.     db_tree = ast_db_gettree(pm_family, NULL);
  4797.     for (entry = db_tree; entry; entry = entry->next) {
  4798.  
  4799.         queue_name = entry->key + strlen(pm_family) + 2;
  4800.  
  4801.         {
  4802.             struct call_queue tmpq;
  4803.             ast_copy_string(tmpq.name, queue_name, sizeof(tmpq.name));
  4804.             cur_queue = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Reload queue members");
  4805.         }  
  4806.  
  4807.         if (!cur_queue)
  4808.             cur_queue = load_realtime_queue(queue_name);
  4809.  
  4810.         if (!cur_queue) {
  4811.             /* If the queue no longer exists, remove it from the
  4812.              * database */
  4813.             ast_log(LOG_WARNING, "Error loading persistent queue: '%s': it does not exist\n", queue_name);
  4814.             ast_db_del(pm_family, queue_name);
  4815.             continue;
  4816.         }
  4817.  
  4818.         if (ast_db_get(pm_family, queue_name, queue_data, PM_MAX_LEN)) {
  4819.             queue_t_unref(cur_queue, "Expire reload reference");
  4820.             continue;
  4821.         }
  4822.  
  4823.         cur_ptr = queue_data;
  4824.         while ((member = strsep(&cur_ptr, ",|"))) {
  4825.             if (ast_strlen_zero(member))
  4826.                 continue;
  4827.  
  4828.             interface = strsep(&member, ";");
  4829.             penalty_tok = strsep(&member, ";");
  4830.             paused_tok = strsep(&member, ";");
  4831.             membername = strsep(&member, ";");
  4832.             state_interface = strsep(&member, ";");
  4833.  
  4834.             if (!penalty_tok) {
  4835.                 ast_log(LOG_WARNING, "Error parsing persistent member string for '%s' (penalty)\n", queue_name);
  4836.                 break;
  4837.             }
  4838.             penalty = strtol(penalty_tok, NULL, 10);
  4839.             if (errno == ERANGE) {
  4840.                 ast_log(LOG_WARNING, "Error converting penalty: %s: Out of range.\n", penalty_tok);
  4841.                 break;
  4842.             }
  4843.            
  4844.             if (!paused_tok) {
  4845.                 ast_log(LOG_WARNING, "Error parsing persistent member string for '%s' (paused)\n", queue_name);
  4846.                 break;
  4847.             }
  4848.             paused = strtol(paused_tok, NULL, 10);
  4849.             if ((errno == ERANGE) || paused < 0 || paused > 1) {
  4850.                 ast_log(LOG_WARNING, "Error converting paused: %s: Expected 0 or 1.\n", paused_tok);
  4851.                 break;
  4852.             }
  4853.  
  4854.             ast_debug(1, "Reload Members: Queue: %s  Member: %s  Name: %s  Penalty: %d  Paused: %d\n", queue_name, interface, membername, penalty, paused);
  4855.            
  4856.             if (add_to_queue(queue_name, interface, membername, penalty, paused, 0, state_interface) == RES_OUTOFMEMORY) {
  4857.                 ast_log(LOG_ERROR, "Out of Memory when reloading persistent queue member\n");
  4858.                 break;
  4859.             }
  4860.         }
  4861.         queue_t_unref(cur_queue, "Expire reload reference");
  4862.     }
  4863.  
  4864.     ao2_unlock(queues);
  4865.     if (db_tree) {
  4866.         ast_log(LOG_NOTICE, "Queue members successfully reloaded from database.\n");
  4867.         ast_db_freetree(db_tree);
  4868.     }
  4869. }
  4870.  
  4871. /*! \brief PauseQueueMember application */
  4872. static int pqm_exec(struct ast_channel *chan, void *data)
  4873. {
  4874.     char *parse;
  4875.     AST_DECLARE_APP_ARGS(args,
  4876.         AST_APP_ARG(queuename);
  4877.         AST_APP_ARG(interface);
  4878.         AST_APP_ARG(options);
  4879.         AST_APP_ARG(reason);
  4880.     );
  4881.  
  4882.     if (ast_strlen_zero(data)) {
  4883.         ast_log(LOG_WARNING, "PauseQueueMember requires an argument ([queuename],interface[,options][,reason])\n");
  4884.         return -1;
  4885.     }
  4886.  
  4887.     parse = ast_strdupa(data);
  4888.  
  4889.     AST_STANDARD_APP_ARGS(args, parse);
  4890.  
  4891.     if (ast_strlen_zero(args.interface)) {
  4892.         ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename],interface[,options[,reason]])\n");
  4893.         return -1;
  4894.     }
  4895.  
  4896.     if (set_member_paused(args.queuename, args.interface, args.reason, 1)) {
  4897.         ast_log(LOG_WARNING, "Attempt to pause interface %s, not found\n", args.interface);
  4898.         pbx_builtin_setvar_helper(chan, "PQMSTATUS", "NOTFOUND");
  4899.         return 0;
  4900.     }
  4901.  
  4902.     pbx_builtin_setvar_helper(chan, "PQMSTATUS", "PAUSED");
  4903.  
  4904.     return 0;
  4905. }
  4906.  
  4907. /*! \brief UnPauseQueueMember application */
  4908. static int upqm_exec(struct ast_channel *chan, void *data)
  4909. {
  4910.     char *parse;
  4911.     AST_DECLARE_APP_ARGS(args,
  4912.         AST_APP_ARG(queuename);
  4913.         AST_APP_ARG(interface);
  4914.         AST_APP_ARG(options);
  4915.         AST_APP_ARG(reason);
  4916.     );
  4917.  
  4918.     if (ast_strlen_zero(data)) {
  4919.         ast_log(LOG_WARNING, "UnpauseQueueMember requires an argument ([queuename],interface[,options[,reason]])\n");
  4920.         return -1;
  4921.     }
  4922.  
  4923.     parse = ast_strdupa(data);
  4924.  
  4925.     AST_STANDARD_APP_ARGS(args, parse);
  4926.  
  4927.     if (ast_strlen_zero(args.interface)) {
  4928.         ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename],interface[,options[,reason]])\n");
  4929.         return -1;
  4930.     }
  4931.  
  4932.     if (set_member_paused(args.queuename, args.interface, args.reason, 0)) {
  4933.         ast_log(LOG_WARNING, "Attempt to unpause interface %s, not found\n", args.interface);
  4934.         pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "NOTFOUND");
  4935.         return 0;
  4936.     }
  4937.  
  4938.     pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "UNPAUSED");
  4939.  
  4940.     return 0;
  4941. }
  4942.  
  4943. /*! \brief RemoveQueueMember application */
  4944. static int rqm_exec(struct ast_channel *chan, void *data)
  4945. {
  4946.     int res=-1;
  4947.     char *parse, *temppos = NULL;
  4948.     AST_DECLARE_APP_ARGS(args,
  4949.         AST_APP_ARG(queuename);
  4950.         AST_APP_ARG(interface);
  4951.         AST_APP_ARG(options);
  4952.     );
  4953.  
  4954.  
  4955.     if (ast_strlen_zero(data)) {
  4956.         ast_log(LOG_WARNING, "RemoveQueueMember requires an argument (queuename[,interface[,options]])\n");
  4957.         return -1;
  4958.     }
  4959.  
  4960.     parse = ast_strdupa(data);
  4961.  
  4962.     AST_STANDARD_APP_ARGS(args, parse);
  4963.  
  4964.     if (ast_strlen_zero(args.interface)) {
  4965.         args.interface = ast_strdupa(chan->name);
  4966.         temppos = strrchr(args.interface, '-');
  4967.         if (temppos)
  4968.             *temppos = '\0';
  4969.     }
  4970.  
  4971.     ast_debug(1, "queue: %s, member: %s\n", args.queuename, args.interface);
  4972.  
  4973.     switch (remove_from_queue(args.queuename, args.interface)) {
  4974.     case RES_OKAY:
  4975.         ast_queue_log(args.queuename, chan->uniqueid, args.interface, "REMOVEMEMBER", "%s", "");
  4976.         ast_log(LOG_NOTICE, "Removed interface '%s' from queue '%s'\n", args.interface, args.queuename);
  4977.         pbx_builtin_setvar_helper(chan, "RQMSTATUS", "REMOVED");
  4978.         res = 0;
  4979.         break;
  4980.     case RES_EXISTS:
  4981.         ast_debug(1, "Unable to remove interface '%s' from queue '%s': Not there\n", args.interface, args.queuename);
  4982.         pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTINQUEUE");
  4983.         res = 0;
  4984.         break;
  4985.     case RES_NOSUCHQUEUE:
  4986.         ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': No such queue\n", args.queuename);
  4987.         pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOSUCHQUEUE");
  4988.         res = 0;
  4989.         break;
  4990.     case RES_NOT_DYNAMIC:
  4991.         ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': '%s' is not a dynamic member\n", args.queuename, args.interface);
  4992.         pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTDYNAMIC");
  4993.         res = 0;
  4994.         break;
  4995.     }
  4996.  
  4997.     return res;
  4998. }
  4999.  
  5000. /*! \brief AddQueueMember application */
  5001. static int aqm_exec(struct ast_channel *chan, void *data)
  5002. {
  5003.     int res=-1;
  5004.     char *parse, *temppos = NULL;
  5005.     AST_DECLARE_APP_ARGS(args,
  5006.         AST_APP_ARG(queuename);
  5007.         AST_APP_ARG(interface);
  5008.         AST_APP_ARG(penalty);
  5009.         AST_APP_ARG(options);
  5010.         AST_APP_ARG(membername);
  5011.         AST_APP_ARG(state_interface);
  5012.     );
  5013.     int penalty = 0;
  5014.  
  5015.     if (ast_strlen_zero(data)) {
  5016.         ast_log(LOG_WARNING, "AddQueueMember requires an argument (queuename[,interface[,penalty[,options[,membername[,stateinterface]]]]])\n");
  5017.         return -1;
  5018.     }
  5019.  
  5020.     parse = ast_strdupa(data);
  5021.  
  5022.     AST_STANDARD_APP_ARGS(args, parse);
  5023.  
  5024.     if (ast_strlen_zero(args.interface)) {
  5025.         args.interface = ast_strdupa(chan->name);
  5026.         temppos = strrchr(args.interface, '-');
  5027.         if (temppos)
  5028.             *temppos = '\0';
  5029.     }
  5030.  
  5031.     if (!ast_strlen_zero(args.penalty)) {
  5032.         if ((sscanf(args.penalty, "%30d", &penalty) != 1) || penalty < 0) {
  5033.             ast_log(LOG_WARNING, "Penalty '%s' is invalid, must be an integer >= 0\n", args.penalty);
  5034.             penalty = 0;
  5035.         }
  5036.     }
  5037.  
  5038.     switch (add_to_queue(args.queuename, args.interface, args.membername, penalty, 0, queue_persistent_members, args.state_interface)) {
  5039.     case RES_OKAY:
  5040.         ast_queue_log(args.queuename, chan->uniqueid, args.interface, "ADDMEMBER", "%s", "");
  5041.         ast_log(LOG_NOTICE, "Added interface '%s' to queue '%s'\n", args.interface, args.queuename);
  5042.         pbx_builtin_setvar_helper(chan, "AQMSTATUS", "ADDED");
  5043.         res = 0;
  5044.         break;
  5045.     case RES_EXISTS:
  5046.         ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': Already there\n", args.interface, args.queuename);
  5047.         pbx_builtin_setvar_helper(chan, "AQMSTATUS", "MEMBERALREADY");
  5048.         res = 0;
  5049.         break;
  5050.     case RES_NOSUCHQUEUE:
  5051.         ast_log(LOG_WARNING, "Unable to add interface to queue '%s': No such queue\n", args.queuename);
  5052.         pbx_builtin_setvar_helper(chan, "AQMSTATUS", "NOSUCHQUEUE");
  5053.         res = 0;
  5054.         break;
  5055.     case RES_OUTOFMEMORY:
  5056.         ast_log(LOG_ERROR, "Out of memory adding member %s to queue %s\n", args.interface, args.queuename);
  5057.         break;
  5058.     }
  5059.  
  5060.     return res;
  5061. }
  5062.  
  5063. /*! \brief QueueLog application */
  5064. static int ql_exec(struct ast_channel *chan, void *data)
  5065. {
  5066.     char *parse;
  5067.  
  5068.     AST_DECLARE_APP_ARGS(args,
  5069.         AST_APP_ARG(queuename);
  5070.         AST_APP_ARG(uniqueid);
  5071.         AST_APP_ARG(membername);
  5072.         AST_APP_ARG(event);
  5073.         AST_APP_ARG(params);
  5074.     );
  5075.  
  5076.     if (ast_strlen_zero(data)) {
  5077.         ast_log(LOG_WARNING, "QueueLog requires arguments (queuename,uniqueid,membername,event[,additionalinfo]\n");
  5078.         return -1;
  5079.     }
  5080.  
  5081.     parse = ast_strdupa(data);
  5082.  
  5083.     AST_STANDARD_APP_ARGS(args, parse);
  5084.  
  5085.     if (ast_strlen_zero(args.queuename) || ast_strlen_zero(args.uniqueid)
  5086.         || ast_strlen_zero(args.membername) || ast_strlen_zero(args.event)) {
  5087.         ast_log(LOG_WARNING, "QueueLog requires arguments (queuename,uniqueid,membername,event[,additionalinfo])\n");
  5088.         return -1;
  5089.     }
  5090.  
  5091.     ast_queue_log(args.queuename, args.uniqueid, args.membername, args.event,
  5092.         "%s", args.params ? args.params : "");
  5093.  
  5094.     return 0;
  5095. }
  5096.  
  5097. /*! \brief Copy rule from global list into specified queue */
  5098. static void copy_rules(struct queue_ent *qe, const char *rulename)
  5099. {
  5100.     struct penalty_rule *pr_iter;
  5101.     struct rule_list *rl_iter;
  5102.     const char *tmp = ast_strlen_zero(rulename) ? qe->parent->defaultrule : rulename;
  5103.     AST_LIST_LOCK(&rule_lists);
  5104.     AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
  5105.         if (!strcasecmp(rl_iter->name, tmp))
  5106.             break;
  5107.     }
  5108.     if (rl_iter) {
  5109.         AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
  5110.             struct penalty_rule *new_pr = ast_calloc(1, sizeof(*new_pr));
  5111.             if (!new_pr) {
  5112.                 ast_log(LOG_ERROR, "Memory allocation error when copying penalty rules! Aborting!\n");
  5113.                 AST_LIST_UNLOCK(&rule_lists);
  5114.                 break;
  5115.             }
  5116.             new_pr->time = pr_iter->time;
  5117.             new_pr->max_value = pr_iter->max_value;
  5118.             new_pr->min_value = pr_iter->min_value;
  5119.             new_pr->max_relative = pr_iter->max_relative;
  5120.             new_pr->min_relative = pr_iter->min_relative;
  5121.             AST_LIST_INSERT_TAIL(&qe->qe_rules, new_pr, list);
  5122.         }
  5123.     }
  5124.     AST_LIST_UNLOCK(&rule_lists);
  5125. }
  5126.  
  5127. /*!\brief The starting point for all queue calls
  5128.  *
  5129.  * The process involved here is to
  5130.  * 1. Parse the options specified in the call to Queue()
  5131.  * 2. Join the queue
  5132.  * 3. Wait in a loop until it is our turn to try calling a queue member
  5133.  * 4. Attempt to call a queue member
  5134.  * 5. If 4. did not result in a bridged call, then check for between
  5135.  *    call options such as periodic announcements etc.
  5136.  * 6. Try 4 again unless some condition (such as an expiration time) causes us to
  5137.  *    exit the queue.
  5138.  */
  5139. static int queue_exec(struct ast_channel *chan, void *data)
  5140. {
  5141.     int res=-1;
  5142.     int ringing=0;
  5143.     const char *user_priority;
  5144.     const char *max_penalty_str;
  5145.     const char *min_penalty_str;
  5146.     int prio;
  5147.     int qcontinue = 0;
  5148.     int max_penalty, min_penalty;
  5149.     enum queue_result reason = QUEUE_UNKNOWN;
  5150.     /* whether to exit Queue application after the timeout hits */
  5151.     int tries = 0;
  5152.     int noption = 0;
  5153.     char *parse;
  5154.     int makeannouncement = 0;
  5155.     AST_DECLARE_APP_ARGS(args,
  5156.         AST_APP_ARG(queuename);
  5157.         AST_APP_ARG(options);
  5158.         AST_APP_ARG(url);
  5159.         AST_APP_ARG(announceoverride);
  5160.         AST_APP_ARG(queuetimeoutstr);
  5161.         AST_APP_ARG(agi);
  5162.         AST_APP_ARG(macro);
  5163.         AST_APP_ARG(gosub);
  5164.         AST_APP_ARG(rule);
  5165.     );
  5166.     /* Our queue entry */
  5167.     struct queue_ent qe = { 0 };
  5168.    
  5169.     if (ast_strlen_zero(data)) {
  5170.         ast_log(LOG_WARNING, "Queue requires an argument: queuename[,options[,URL[,announceoverride[,timeout[,agi[,macro[,gosub[,rule]]]]]]]]\n");
  5171.         return -1;
  5172.     }
  5173.    
  5174.     parse = ast_strdupa(data);
  5175.     AST_STANDARD_APP_ARGS(args, parse);
  5176.  
  5177.     /* Setup our queue entry */
  5178.     qe.start = time(NULL);
  5179.  
  5180.     /* set the expire time based on the supplied timeout; */
  5181.     if (!ast_strlen_zero(args.queuetimeoutstr))
  5182.         qe.expire = qe.start + atoi(args.queuetimeoutstr);
  5183.     else
  5184.         qe.expire = 0;
  5185.  
  5186.     /* Get the priority from the variable ${QUEUE_PRIO} */
  5187.     ast_channel_lock(chan);
  5188.     user_priority = pbx_builtin_getvar_helper(chan, "QUEUE_PRIO");
  5189.     if (user_priority) {
  5190.         if (sscanf(user_priority, "%30d", &prio) == 1) {
  5191.             ast_debug(1, "%s: Got priority %d from ${QUEUE_PRIO}.\n", chan->name, prio);
  5192.         } else {
  5193.             ast_log(LOG_WARNING, "${QUEUE_PRIO}: Invalid value (%s), channel %s.\n",
  5194.                 user_priority, chan->name);
  5195.             prio = 0;
  5196.         }
  5197.     } else {
  5198.         ast_debug(3, "NO QUEUE_PRIO variable found. Using default.\n");
  5199.         prio = 0;
  5200.     }
  5201.  
  5202.     /* Get the maximum penalty from the variable ${QUEUE_MAX_PENALTY} */
  5203.  
  5204.     if ((max_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MAX_PENALTY"))) {
  5205.         if (sscanf(max_penalty_str, "%30d", &max_penalty) == 1) {
  5206.             ast_debug(1, "%s: Got max penalty %d from ${QUEUE_MAX_PENALTY}.\n", chan->name, max_penalty);
  5207.         } else {
  5208.             ast_log(LOG_WARNING, "${QUEUE_MAX_PENALTY}: Invalid value (%s), channel %s.\n",
  5209.                 max_penalty_str, chan->name);
  5210.             max_penalty = 0;
  5211.         }
  5212.     } else {
  5213.         max_penalty = 0;
  5214.     }
  5215.  
  5216.     if ((min_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MIN_PENALTY"))) {
  5217.         if (sscanf(min_penalty_str, "%30d", &min_penalty) == 1) {
  5218.             ast_debug(1, "%s: Got min penalty %d from ${QUEUE_MIN_PENALTY}.\n", chan->name, min_penalty);
  5219.         } else {
  5220.             ast_log(LOG_WARNING, "${QUEUE_MIN_PENALTY}: Invalid value (%s), channel %s.\n",
  5221.                 min_penalty_str, chan->name);
  5222.             min_penalty = 0;
  5223.         }
  5224.     } else {
  5225.         min_penalty = 0;
  5226.     }
  5227.     ast_channel_unlock(chan);
  5228.  
  5229.     if (args.options && (strchr(args.options, 'r')))
  5230.         ringing = 1;
  5231.  
  5232.     if (args.options && (strchr(args.options, 'c')))
  5233.         qcontinue = 1;
  5234.  
  5235.     ast_debug(1, "queue: %s, options: %s, url: %s, announce: %s, expires: %ld, priority: %d\n",
  5236.         args.queuename, args.options, args.url, args.announceoverride, (long)qe.expire, prio);
  5237.  
  5238.     qe.chan = chan;
  5239.     qe.prio = prio;
  5240.     qe.max_penalty = max_penalty;
  5241.     qe.min_penalty = min_penalty;
  5242.     qe.last_pos_said = 0;
  5243.     qe.last_pos = 0;
  5244.     qe.last_periodic_announce_time = time(NULL);
  5245.     qe.last_periodic_announce_sound = 0;
  5246.     qe.valid_digits = 0;
  5247.     if (join_queue(args.queuename, &qe, &reason)) {
  5248.         ast_log(LOG_WARNING, "Unable to join queue '%s'\n", args.queuename);
  5249.         set_queue_result(chan, reason);
  5250.         return 0;
  5251.     }
  5252.     ast_queue_log(args.queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", S_OR(args.url, ""),
  5253.         S_OR(chan->cid.cid_num, ""));
  5254.     copy_rules(&qe, args.rule);
  5255.     qe.pr = AST_LIST_FIRST(&qe.qe_rules);
  5256. check_turns:
  5257.     if (ringing) {
  5258.         ast_indicate(chan, AST_CONTROL_RINGING);
  5259.     } else {
  5260.         ast_moh_start(chan, qe.moh, NULL);
  5261.     }
  5262.  
  5263.     /* This is the wait loop for callers 2 through maxlen */
  5264.     res = wait_our_turn(&qe, ringing, &reason);
  5265.     if (res) {
  5266.         goto stop;
  5267.     }
  5268.  
  5269.     makeannouncement = 0;
  5270.  
  5271.     for (;;) {
  5272.         /* This is the wait loop for the head caller*/
  5273.         /* To exit, they may get their call answered; */
  5274.         /* they may dial a digit from the queue context; */
  5275.         /* or, they may timeout. */
  5276.  
  5277.         /* Leave if we have exceeded our queuetimeout */
  5278.         if (qe.expire && (time(NULL) >= qe.expire)) {
  5279.             record_abandoned_old(&qe,"QueueCallerExitWithTimeout"); // Deprecated
  5280.             record_exitwithtimeout(&qe);
  5281.             reason = QUEUE_TIMEOUT;
  5282.             res = 0;
  5283.             ast_queue_log(args.queuename, chan->uniqueid,"NONE", "EXITWITHTIMEOUT", "%d|%d|%ld",
  5284.                 qe.pos, qe.opos, (long) time(NULL) - qe.start);
  5285.             break;
  5286.         }
  5287.  
  5288.         if (makeannouncement) {
  5289.             /* Make a position announcement, if enabled */
  5290.             if (qe.parent->announcefrequency)
  5291.                 if ((res = say_position(&qe,ringing)))
  5292.                     goto stop;
  5293.         }
  5294.         makeannouncement = 1;
  5295.  
  5296.         /* Make a periodic announcement, if enabled */
  5297.         if (qe.parent->periodicannouncefrequency)
  5298.             if ((res = say_periodic_announcement(&qe,ringing)))
  5299.                 goto stop;
  5300.    
  5301.         /* Leave if we have exceeded our queuetimeout */
  5302.         if (qe.expire && (time(NULL) >= qe.expire)) {
  5303.             record_abandoned(&qe); // Deprecated
  5304.             record_exitwithtimeout(&qe);
  5305.             reason = QUEUE_TIMEOUT;
  5306.             res = 0;
  5307.             ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
  5308.             break;
  5309.         }
  5310.  
  5311.         /* see if we need to move to the next penalty level for this queue */
  5312.         while (qe.pr && ((time(NULL) - qe.start) > qe.pr->time)) {
  5313.             update_qe_rule(&qe);
  5314.         }
  5315.  
  5316.         /* Try calling all queue members for 'timeout' seconds */
  5317.         res = try_calling(&qe, args.options, args.announceoverride, args.url, &tries, &noption, args.agi, args.macro, args.gosub, ringing);
  5318.         if (res) {
  5319.             goto stop;
  5320.         }
  5321.  
  5322.         if (qe.parent->leavewhenempty) {
  5323.             int status = 0;
  5324.             if ((status = get_member_status(qe.parent, qe.max_penalty, qe.min_penalty, qe.parent->leavewhenempty))) {
  5325.                 record_abandoned_old(&qe,"QueueCallerExitEmpty");
  5326.                 record_exitempty(&qe);
  5327.                 reason = QUEUE_LEAVEEMPTY;
  5328.                 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITEMPTY", "%d|%d|%ld", qe.pos, qe.opos, (long)(time(NULL) - qe.start));
  5329.                 res = 0;
  5330.                 break;
  5331.             }
  5332.         }
  5333.  
  5334.         /* exit after 'timeout' cycle if 'n' option enabled */
  5335.         if (noption && tries >= qe.parent->membercount) {
  5336.             ast_verb(3, "Exiting on time-out cycle\n");
  5337.             ast_queue_log(args.queuename, chan->uniqueid, "NONE", "QueueCallerExitWithTimeout", "%d", qe.pos);
  5338.             record_abandoned_old(&qe,"EXITWITHTIMEOUT");
  5339.             record_exitwithtimeout(&qe);
  5340.             reason = QUEUE_TIMEOUT;
  5341.             res = 0;
  5342.             break;
  5343.         }
  5344.  
  5345.        
  5346.         /* Leave if we have exceeded our queuetimeout */
  5347.         if (qe.expire && (time(NULL) >= qe.expire)) {
  5348.             record_abandoned_old(&qe,"QueueCallerExitWithTimeout");
  5349.             record_exitwithtimeout(&qe);
  5350.             reason = QUEUE_TIMEOUT;
  5351.             res = 0;
  5352.             ast_queue_log(qe.parent->name, qe.chan->uniqueid,"NONE", "EXITWITHTIMEOUT", "%d|%d|%ld", qe.pos, qe.opos, (long) time(NULL) - qe.start);
  5353.             break;
  5354.         }
  5355.  
  5356.         /* If using dynamic realtime members, we should regenerate the member list for this queue */
  5357.         update_realtime_members(qe.parent);
  5358.         /* OK, we didn't get anybody; wait for 'retry' seconds; may get a digit to exit with */
  5359.         res = wait_a_bit(&qe);
  5360.         if (res)
  5361.             goto stop;
  5362.  
  5363.         /* Since this is a priority queue and
  5364.          * it is not sure that we are still at the head
  5365.          * of the queue, go and check for our turn again.
  5366.          */
  5367.         if (!is_our_turn(&qe)) {
  5368.             ast_debug(1, "Darn priorities, going back in queue (%s)!\n", qe.chan->name);
  5369.             goto check_turns;
  5370.         }
  5371.     }
  5372.  
  5373. stop:
  5374.     if (res) {
  5375.         if (res < 0) {
  5376.             if (!qe.handled) {
  5377.                 record_abandoned(&qe);
  5378.                 ast_queue_log(args.queuename, chan->uniqueid, "NONE", "ABANDON",
  5379.                     "%d|%d|%ld", qe.pos, qe.opos,
  5380.                     (long) time(NULL) - qe.start);
  5381.                 res = -1;
  5382.             } else if (qcontinue) {
  5383.                 reason = QUEUE_CONTINUE;
  5384.                 res = 0;
  5385.             }
  5386.         } else if (qe.valid_digits) {
  5387.             record_abandoned_old(&qe,"QueueCallerExitKey");
  5388.             record_exitwithkey(&qe);
  5389.             ast_queue_log(args.queuename, chan->uniqueid, "NONE", "EXITWITHKEY",
  5390.                 "%s|%d", qe.digits, qe.pos);
  5391.         }
  5392.     }
  5393.  
  5394.     /* Don't allow return code > 0 */
  5395.     if (res >= 0) {
  5396.         res = 0;   
  5397.         if (ringing) {
  5398.             ast_indicate(chan, -1);
  5399.         } else {
  5400.             ast_moh_stop(chan);
  5401.         }          
  5402.         ast_stopstream(chan);
  5403.     }
  5404.  
  5405.     set_queue_variables(qe.parent, qe.chan);
  5406.  
  5407.     leave_queue(&qe);
  5408.     if (reason != QUEUE_UNKNOWN)
  5409.         set_queue_result(chan, reason);
  5410.  
  5411.     if (qe.parent) {
  5412.         /* every queue_ent is given a reference to it's parent call_queue when it joins the queue.
  5413.          * This ref must be taken away right before the queue_ent is destroyed.  In this case
  5414.          * the queue_ent is about to be returned on the stack */
  5415.         qe.parent = queue_unref(qe.parent);
  5416.     }
  5417.  
  5418.     return res;
  5419. }
  5420.  
  5421. /*!
  5422.  * \brief create interface var with all queue details.
  5423.  * \retval 0 on success
  5424.  * \retval -1 on error
  5425. */
  5426. static int queue_function_var(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  5427. {
  5428.     int res = -1;
  5429.     struct call_queue *q, tmpq;
  5430.     char interfacevar[256] = "";
  5431.     float sl = 0;
  5432.  
  5433.     if (ast_strlen_zero(data)) {
  5434.         ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
  5435.         return -1;
  5436.     }
  5437.  
  5438.     ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
  5439.     if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE() function"))) {
  5440.         ao2_lock(q);
  5441.         if (q->setqueuevar) {
  5442.             sl = 0;
  5443.             res = 0;
  5444.  
  5445.             if (q->callscompleted > 0) {
  5446.                 sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
  5447.             }
  5448.  
  5449.             snprintf(interfacevar, sizeof(interfacevar),
  5450.                 "QUEUEMAX=%d,QUEUESTRATEGY=%s,QUEUECALLS=%d,QUEUEHOLDTIME=%d,QUEUETALKTIME=%d,QUEUECOMPLETED=%d,QUEUEABANDONED=%d,QUEUESRVLEVEL=%d,QUEUESRVLEVELPERF=%2.1f",
  5451.                 q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted, q->callsabandoned,  q->servicelevel, sl);
  5452.  
  5453.             pbx_builtin_setvar_multiple(chan, interfacevar);
  5454.         }
  5455.  
  5456.         ao2_unlock(q);
  5457.         queue_t_unref(q, "Done with QUEUE() function");
  5458.     } else {
  5459.         ast_log(LOG_WARNING, "queue %s was not found\n", data);
  5460.     }
  5461.  
  5462.     snprintf(buf, len, "%d", res);
  5463.  
  5464.     return 0;
  5465. }
  5466.  
  5467. /*!
  5468.  * \brief Get number either busy / free or total members of a specific queue
  5469.  * \retval number of members (busy / free / total)
  5470.  * \retval -1 on error
  5471. */
  5472. static int queue_function_qac(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  5473. {
  5474.     int count = 0;
  5475.     struct member *m;
  5476.     struct ao2_iterator mem_iter;
  5477.     struct call_queue *q;
  5478.     char *option;
  5479.  
  5480.     if (ast_strlen_zero(data)) {
  5481.         ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
  5482.         return -1;
  5483.     }
  5484.  
  5485.     if ((option = strchr(data, ',')))
  5486.         *option++ = '\0';
  5487.     else
  5488.         option = "logged";
  5489.     if ((q = load_realtime_queue(data))) {
  5490.         ao2_lock(q);
  5491.         if (!strcasecmp(option, "logged")) {
  5492.             mem_iter = ao2_iterator_init(q->members, 0);
  5493.             while ((m = ao2_iterator_next(&mem_iter))) {
  5494.                 /* Count the agents who are logged in and presently answering calls */
  5495.                 if ((m->status != AST_DEVICE_UNAVAILABLE) && (m->status != AST_DEVICE_INVALID)) {
  5496.                     count++;
  5497.                 }
  5498.                 ao2_ref(m, -1);
  5499.             }
  5500.             ao2_iterator_destroy(&mem_iter);
  5501.         } else if (!strcasecmp(option, "free")) {
  5502.             mem_iter = ao2_iterator_init(q->members, 0);
  5503.             while ((m = ao2_iterator_next(&mem_iter))) {
  5504.                 /* Count the agents who are logged in and presently answering calls */
  5505.                 if ((m->status == AST_DEVICE_NOT_INUSE) && (!m->paused)) {
  5506.                     count++;
  5507.                 }
  5508.                 ao2_ref(m, -1);
  5509.             }
  5510.             ao2_iterator_destroy(&mem_iter);
  5511.         } else /* must be "count" */
  5512.             count = q->membercount;
  5513.         ao2_unlock(q);
  5514.         queue_t_unref(q, "Done with temporary reference in QUEUE_MEMBER()");
  5515.     } else
  5516.         ast_log(LOG_WARNING, "queue %s was not found\n", data);
  5517.  
  5518.     snprintf(buf, len, "%d", count);
  5519.  
  5520.     return 0;
  5521. }
  5522.  
  5523. /*!
  5524.  * \brief Get the total number of members in a specific queue (Deprecated)
  5525.  * \retval number of members
  5526.  * \retval -1 on error
  5527. */
  5528. static int queue_function_qac_dep(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  5529. {
  5530.     int count = 0;
  5531.     struct member *m;
  5532.     struct call_queue *q;
  5533.     struct ao2_iterator mem_iter;
  5534.     static int depflag = 1;
  5535.  
  5536.     if (depflag) {
  5537.         depflag = 0;
  5538.         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");
  5539.     }
  5540.  
  5541.     if (ast_strlen_zero(data)) {
  5542.         ast_log(LOG_ERROR, "%s requires an argument: queuename\n", cmd);
  5543.         return -1;
  5544.     }
  5545.    
  5546.     if ((q = load_realtime_queue(data))) {
  5547.         ao2_lock(q);
  5548.         mem_iter = ao2_iterator_init(q->members, 0);
  5549.         while ((m = ao2_iterator_next(&mem_iter))) {
  5550.             /* Count the agents who are logged in and presently answering calls */
  5551.             if ((m->status != AST_DEVICE_UNAVAILABLE) && (m->status != AST_DEVICE_INVALID)) {
  5552.                 count++;
  5553.             }
  5554.             ao2_ref(m, -1);
  5555.         }
  5556.         ao2_iterator_destroy(&mem_iter);
  5557.         ao2_unlock(q);
  5558.         queue_t_unref(q, "Done with temporary reference in QUEUE_MEMBER_COUNT");
  5559.     } else
  5560.         ast_log(LOG_WARNING, "queue %s was not found\n", data);
  5561.  
  5562.     snprintf(buf, len, "%d", count);
  5563.  
  5564.     return 0;
  5565. }
  5566.  
  5567. /*! \brief Dialplan function QUEUE_WAITING_COUNT() Get number callers waiting in a specific queue */
  5568. static int queue_function_queuewaitingcount(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  5569. {
  5570.     int count = 0;
  5571.     struct call_queue *q, tmpq;
  5572.     struct ast_variable *var = NULL;
  5573.  
  5574.     buf[0] = '\0';
  5575.    
  5576.     if (ast_strlen_zero(data)) {
  5577.         ast_log(LOG_ERROR, "QUEUE_WAITING_COUNT requires an argument: queuename\n");
  5578.         return -1;
  5579.     }
  5580.  
  5581.     ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
  5582.     if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_WAITING_COUNT()"))) {
  5583.         ao2_lock(q);
  5584.         count = q->count;
  5585.         ao2_unlock(q);
  5586.         queue_t_unref(q, "Done with reference in QUEUE_WAITING_COUNT()");
  5587.     } else if ((var = ast_load_realtime("queues", "name", data, SENTINEL))) {
  5588.         /* if the queue is realtime but was not found in memory, this
  5589.          * means that the queue had been deleted from memory since it was
  5590.          * "dead." This means it has a 0 waiting count
  5591.          */
  5592.         count = 0;
  5593.         ast_variables_destroy(var);
  5594.     } else
  5595.         ast_log(LOG_WARNING, "queue %s was not found\n", data);
  5596.  
  5597.     snprintf(buf, len, "%d", count);
  5598.  
  5599.     return 0;
  5600. }
  5601.  
  5602. /*! \brief Dialplan function QUEUE_MEMBER_LIST() Get list of members in a specific queue */
  5603. static int queue_function_queuememberlist(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  5604. {
  5605.     struct call_queue *q, tmpq;
  5606.     struct member *m;
  5607.  
  5608.     /* Ensure an otherwise empty list doesn't return garbage */
  5609.     buf[0] = '\0';
  5610.  
  5611.     if (ast_strlen_zero(data)) {
  5612.         ast_log(LOG_ERROR, "QUEUE_MEMBER_LIST requires an argument: queuename\n");
  5613.         return -1;
  5614.     }
  5615.  
  5616.     ast_copy_string(tmpq.name, data, sizeof(tmpq.name));
  5617.     if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_MEMBER_LIST()"))) {
  5618.         int buflen = 0, count = 0;
  5619.         struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
  5620.  
  5621.         ao2_lock(q);
  5622.         while ((m = ao2_iterator_next(&mem_iter))) {
  5623.             /* strcat() is always faster than printf() */
  5624.             if (count++) {
  5625.                 strncat(buf + buflen, ",", len - buflen - 1);
  5626.                 buflen++;
  5627.             }
  5628.             strncat(buf + buflen, m->interface, len - buflen - 1);
  5629.             buflen += strlen(m->interface);
  5630.             /* Safeguard against overflow (negative length) */
  5631.             if (buflen >= len - 2) {
  5632.                 ao2_ref(m, -1);
  5633.                 ast_log(LOG_WARNING, "Truncating list\n");
  5634.                 break;
  5635.             }
  5636.             ao2_ref(m, -1);
  5637.         }
  5638.         ao2_iterator_destroy(&mem_iter);
  5639.         ao2_unlock(q);
  5640.         queue_t_unref(q, "Done with QUEUE_MEMBER_LIST()");
  5641.     } else
  5642.         ast_log(LOG_WARNING, "queue %s was not found\n", data);
  5643.  
  5644.     /* We should already be terminated, but let's make sure. */
  5645.     buf[len - 1] = '\0';
  5646.  
  5647.     return 0;
  5648. }
  5649.  
  5650. /*! \brief Dialplan function QUEUE_MEMBER_PENALTY() Gets the members penalty. */
  5651. static int queue_function_memberpenalty_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  5652. {
  5653.     int penalty;
  5654.     AST_DECLARE_APP_ARGS(args,
  5655.         AST_APP_ARG(queuename);
  5656.         AST_APP_ARG(interface);
  5657.     );
  5658.     /* Make sure the returned value on error is NULL. */
  5659.     buf[0] = '\0';
  5660.  
  5661.     if (ast_strlen_zero(data)) {
  5662.         ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
  5663.         return -1;
  5664.     }
  5665.  
  5666.     AST_STANDARD_APP_ARGS(args, data);
  5667.  
  5668.     if (args.argc < 2) {
  5669.         ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
  5670.         return -1;
  5671.     }
  5672.  
  5673.     penalty = get_member_penalty (args.queuename, args.interface);
  5674.    
  5675.     if (penalty >= 0) /* remember that buf is already '\0' */
  5676.         snprintf (buf, len, "%d", penalty);
  5677.  
  5678.     return 0;
  5679. }
  5680.  
  5681. /*! \brief Dialplan function QUEUE_MEMBER_PENALTY() Sets the members penalty. */
  5682. static int queue_function_memberpenalty_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  5683. {
  5684.     int penalty;
  5685.     AST_DECLARE_APP_ARGS(args,
  5686.         AST_APP_ARG(queuename);
  5687.         AST_APP_ARG(interface);
  5688.     );
  5689.  
  5690.     if (ast_strlen_zero(data)) {
  5691.         ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
  5692.         return -1;
  5693.     }
  5694.  
  5695.     AST_STANDARD_APP_ARGS(args, data);
  5696.  
  5697.     if (args.argc < 2) {
  5698.         ast_log(LOG_ERROR, "Missing argument. QUEUE_MEMBER_PENALTY(<queuename>,<interface>)\n");
  5699.         return -1;
  5700.     }
  5701.  
  5702.     penalty = atoi(value);
  5703.  
  5704.     if (ast_strlen_zero(args.interface)) {
  5705.         ast_log (LOG_ERROR, "<interface> parameter can't be null\n");
  5706.         return -1;
  5707.     }
  5708.  
  5709.     /* if queuename = NULL then penalty will be set for interface in all the queues. */
  5710.     if (set_member_penalty(args.queuename, args.interface, penalty)) {
  5711.         ast_log(LOG_ERROR, "Invalid interface, queue or penalty\n");
  5712.         return -1;
  5713.     }
  5714.  
  5715.     return 0;
  5716. }
  5717.  
  5718. static struct ast_custom_function queuevar_function = {
  5719.     .name = "QUEUE_VARIABLES",
  5720.     .read = queue_function_var,
  5721. };
  5722.  
  5723. static struct ast_custom_function queuemembercount_function = {
  5724.     .name = "QUEUE_MEMBER",
  5725.     .read = queue_function_qac,
  5726. };
  5727.  
  5728. static struct ast_custom_function queuemembercount_dep = {
  5729.     .name = "QUEUE_MEMBER_COUNT",
  5730.     .read = queue_function_qac_dep,
  5731. };
  5732.  
  5733. static struct ast_custom_function queuewaitingcount_function = {
  5734.     .name = "QUEUE_WAITING_COUNT",
  5735.     .read = queue_function_queuewaitingcount,
  5736. };
  5737.  
  5738. static struct ast_custom_function queuememberlist_function = {
  5739.     .name = "QUEUE_MEMBER_LIST",
  5740.     .read = queue_function_queuememberlist,
  5741. };
  5742.  
  5743. static struct ast_custom_function queuememberpenalty_function = {
  5744.     .name = "QUEUE_MEMBER_PENALTY",
  5745.     .read = queue_function_memberpenalty_read,
  5746.     .write = queue_function_memberpenalty_write,
  5747. };
  5748.  
  5749. /*! \brief Reload the rules defined in queuerules.conf
  5750.  *
  5751.  * \param reload If 1, then only process queuerules.conf if the file
  5752.  * has changed since the last time we inspected it.
  5753.  * \return Always returns AST_MODULE_LOAD_SUCCESS
  5754.  */
  5755. static int reload_queue_rules(int reload)
  5756. {
  5757.     struct ast_config *cfg;
  5758.     struct rule_list *rl_iter, *new_rl;
  5759.     struct penalty_rule *pr_iter;
  5760.     char *rulecat = NULL;
  5761.     struct ast_variable *rulevar = NULL;
  5762.     struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  5763.    
  5764.     if (!(cfg = ast_config_load("queuerules.conf", config_flags))) {
  5765.         ast_log(LOG_NOTICE, "No queuerules.conf file found, queues will not follow penalty rules\n");
  5766.         return AST_MODULE_LOAD_SUCCESS;
  5767.     } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  5768.         ast_log(LOG_NOTICE, "queuerules.conf has not changed since it was last loaded. Not taking any action.\n");
  5769.         return AST_MODULE_LOAD_SUCCESS;
  5770.     } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  5771.         ast_log(LOG_ERROR, "Config file queuerules.conf is in an invalid format.  Aborting.\n");
  5772.         return AST_MODULE_LOAD_SUCCESS;
  5773.     }
  5774.  
  5775.     AST_LIST_LOCK(&rule_lists);
  5776.     while ((rl_iter = AST_LIST_REMOVE_HEAD(&rule_lists, list))) {
  5777.         while ((pr_iter = AST_LIST_REMOVE_HEAD(&rl_iter->rules, list)))
  5778.             ast_free(pr_iter);
  5779.         ast_free(rl_iter);
  5780.     }
  5781.     while ((rulecat = ast_category_browse(cfg, rulecat))) {
  5782.         if (!(new_rl = ast_calloc(1, sizeof(*new_rl)))) {
  5783.             AST_LIST_UNLOCK(&rule_lists);
  5784.             return AST_MODULE_LOAD_FAILURE;
  5785.         } else {
  5786.             ast_copy_string(new_rl->name, rulecat, sizeof(new_rl->name));
  5787.             AST_LIST_INSERT_TAIL(&rule_lists, new_rl, list);
  5788.             for (rulevar = ast_variable_browse(cfg, rulecat); rulevar; rulevar = rulevar->next)
  5789.                 if(!strcasecmp(rulevar->name, "penaltychange"))
  5790.                     insert_penaltychange(new_rl->name, rulevar->value, rulevar->lineno);
  5791.                 else
  5792.                     ast_log(LOG_WARNING, "Don't know how to handle rule type '%s' on line %d\n", rulevar->name, rulevar->lineno);
  5793.         }
  5794.     }
  5795.     AST_LIST_UNLOCK(&rule_lists);
  5796.  
  5797.     ast_config_destroy(cfg);
  5798.  
  5799.     return AST_MODULE_LOAD_SUCCESS;
  5800. }
  5801.  
  5802. /*! Set the global queue parameters as defined in the "general" section of queues.conf */
  5803. static void queue_set_global_params(struct ast_config *cfg)
  5804. {
  5805.     const char *general_val = NULL;
  5806.     queue_persistent_members = 0;
  5807.     if ((general_val = ast_variable_retrieve(cfg, "general", "persistentmembers")))
  5808.         queue_persistent_members = ast_true(general_val);
  5809.     autofill_default = 0;
  5810.     if ((general_val = ast_variable_retrieve(cfg, "general", "autofill")))
  5811.         autofill_default = ast_true(general_val);
  5812.     montype_default = 0;
  5813.     if ((general_val = ast_variable_retrieve(cfg, "general", "monitor-type"))) {
  5814.         if (!strcasecmp(general_val, "mixmonitor"))
  5815.             montype_default = 1;
  5816.     }
  5817.     update_cdr = 0;
  5818.     if ((general_val = ast_variable_retrieve(cfg, "general", "updatecdr")))
  5819.         update_cdr = ast_true(general_val);
  5820.     shared_lastcall = 0;
  5821.     if ((general_val = ast_variable_retrieve(cfg, "general", "shared_lastcall")))
  5822.         shared_lastcall = ast_true(general_val);
  5823. }
  5824.  
  5825. /*! \brief reload information pertaining to a single member
  5826.  *
  5827.  * This function is called when a member = line is encountered in
  5828.  * queues.conf.
  5829.  *
  5830.  * \param memberdata The part after member = in the config file
  5831.  * \param q The queue to which this member belongs
  5832.  */
  5833. static void reload_single_member(const char *memberdata, struct call_queue *q)
  5834. {
  5835.     char *membername, *interface, *state_interface, *tmp;
  5836.     char *parse;
  5837.     struct member *cur, *newm;
  5838.     struct member tmpmem;
  5839.     int penalty;
  5840.     AST_DECLARE_APP_ARGS(args,
  5841.         AST_APP_ARG(interface);
  5842.         AST_APP_ARG(penalty);
  5843.         AST_APP_ARG(membername);
  5844.         AST_APP_ARG(state_interface);
  5845.     );
  5846.  
  5847.     if (ast_strlen_zero(memberdata)) {
  5848.         ast_log(LOG_WARNING, "Empty queue member definition. Moving on!\n");
  5849.         return;
  5850.     }
  5851.  
  5852.     /* Add a new member */
  5853.     parse = ast_strdupa(memberdata);
  5854.                
  5855.     AST_STANDARD_APP_ARGS(args, parse);
  5856.  
  5857.     interface = args.interface;
  5858.     if (!ast_strlen_zero(args.penalty)) {
  5859.         tmp = args.penalty;
  5860.         ast_strip(tmp);
  5861.         penalty = atoi(tmp);
  5862.         if (penalty < 0) {
  5863.             penalty = 0;
  5864.         }
  5865.     } else {
  5866.         penalty = 0;
  5867.     }
  5868.  
  5869.     if (!ast_strlen_zero(args.membername)) {
  5870.         membername = args.membername;
  5871.         ast_strip(membername);
  5872.     } else {
  5873.         membername = interface;
  5874.     }
  5875.  
  5876.     if (!ast_strlen_zero(args.state_interface)) {
  5877.         state_interface = args.state_interface;
  5878.         ast_strip(state_interface);
  5879.     } else {
  5880.         state_interface = interface;
  5881.     }
  5882.  
  5883.     /* Find the old position in the list */
  5884.     ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
  5885.     cur = ao2_find(q->members, &tmpmem, OBJ_POINTER | OBJ_UNLINK);
  5886.     if ((newm = create_queue_member(interface, membername, penalty, cur ? cur->paused : 0, state_interface))) {
  5887.         ao2_link(q->members, newm);
  5888.         ao2_ref(newm, -1);
  5889.     }
  5890.     newm = NULL;
  5891.  
  5892.     if (cur) {
  5893.         ao2_ref(cur, -1);
  5894.     } else {
  5895.         q->membercount++;
  5896.     }
  5897. }
  5898.  
  5899. static int mark_member_dead(void *obj, void *arg, int flags)
  5900. {
  5901.     struct member *member = obj;
  5902.     if (!member->dynamic) {
  5903.         member->delme = 1;
  5904.     }
  5905.     return 0;
  5906. }
  5907.  
  5908. static int kill_dead_members(void *obj, void *arg, int flags)
  5909. {
  5910.     struct member *member = obj;
  5911.     struct call_queue *q = arg;
  5912.  
  5913.     if (!member->delme) {
  5914.         if (member->dynamic) {
  5915.             /* dynamic members were not counted toward the member count
  5916.              * when reloading members from queues.conf, so we do that here
  5917.              */
  5918.             q->membercount++;
  5919.         }
  5920.         member->status = ast_device_state(member->state_interface);
  5921.         return 0;
  5922.     } else {
  5923.         q->membercount--;
  5924.         return CMP_MATCH;
  5925.     }
  5926. }
  5927.  
  5928. /*! \brief Reload information pertaining to a particular queue
  5929.  *
  5930.  * Once we have isolated a queue within reload_queues, we call this. This will either
  5931.  * reload information for the queue or if we're just reloading member information, we'll just
  5932.  * reload that without touching other settings within the queue
  5933.  *
  5934.  * \param cfg The configuration which we are reading
  5935.  * \param mask Tells us what information we need to reload
  5936.  * \param queuename The name of the queue we are reloading information from
  5937.  * \retval void
  5938.  */
  5939. static void reload_single_queue(struct ast_config *cfg, struct ast_flags *mask, const char *queuename)
  5940. {
  5941.     int new;
  5942.     struct call_queue *q = NULL;
  5943.     /*We're defining a queue*/
  5944.     struct call_queue tmpq;
  5945.     const char *tmpvar;
  5946.     const int queue_reload = ast_test_flag(mask, QUEUE_RELOAD_PARAMETERS);
  5947.     const int member_reload = ast_test_flag(mask, QUEUE_RELOAD_MEMBER);
  5948.     int prev_weight = 0;
  5949.     struct ast_variable *var;
  5950.  
  5951.     ast_copy_string(tmpq.name, queuename, sizeof(tmpq.name));
  5952.     if (!(q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find queue for reload"))) {
  5953.         if (queue_reload) {
  5954.             /* Make one then */
  5955.             if (!(q = alloc_queue(queuename))) {
  5956.                 return;
  5957.             }
  5958.         } else {
  5959.             /* Since we're not reloading queues, this means that we found a queue
  5960.              * in the configuration file which we don't know about yet. Just return.
  5961.              */
  5962.             return;
  5963.         }
  5964.         new = 1;
  5965.     } else {
  5966.         new = 0;
  5967.     }
  5968.    
  5969.     if (!new) {
  5970.         ao2_lock(q);
  5971.         prev_weight = q->weight ? 1 : 0;
  5972.     }
  5973.     /* Check if we already found a queue with this name in the config file */
  5974.     if (q->found) {
  5975.         ast_log(LOG_WARNING, "Queue '%s' already defined! Skipping!\n", queuename);
  5976.         if (!new) {
  5977.             /* It should be impossible to *not* hit this case*/
  5978.             ao2_unlock(q);
  5979.         }
  5980.         queue_t_unref(q, "We exist! Expiring temporary pointer");
  5981.         return;
  5982.     }
  5983.     /* Due to the fact that the "linear" strategy will have a different allocation
  5984.      * scheme for queue members, we must devise the queue's strategy before other initializations.
  5985.      * To be specific, the linear strategy needs to function like a linked list, meaning the ao2
  5986.      * container used will have only a single bucket instead of the typical number.
  5987.      */
  5988.     if (queue_reload) {
  5989.         if ((tmpvar = ast_variable_retrieve(cfg, queuename, "strategy"))) {
  5990.             q->strategy = strat2int(tmpvar);
  5991.             if (q->strategy < 0) {
  5992.                 ast_log(LOG_WARNING, "'%s' isn't a valid strategy for queue '%s', using ringall instead\n",
  5993.                 tmpvar, q->name);
  5994.                 q->strategy = QUEUE_STRATEGY_RINGALL;
  5995.             }
  5996.         } else {
  5997.             q->strategy = QUEUE_STRATEGY_RINGALL;
  5998.         }
  5999.         init_queue(q);
  6000.     }
  6001.     if (member_reload) {
  6002.         q->membercount = 0;
  6003.         ao2_callback(q->members, OBJ_NODATA, mark_member_dead, NULL);
  6004.     }
  6005.     for (var = ast_variable_browse(cfg, queuename); var; var = var->next) {
  6006.         if (member_reload && !strcasecmp(var->name, "member")) {
  6007.             reload_single_member(var->value, q);
  6008.         } else if (queue_reload) {
  6009.             queue_set_param(q, var->name, var->value, var->lineno, 1);
  6010.         }
  6011.     }
  6012.     /* At this point, we've determined if the queue has a weight, so update use_weight
  6013.      * as appropriate
  6014.      */
  6015.     if (!q->weight && prev_weight) {
  6016.         ast_atomic_fetchadd_int(&use_weight, -1);
  6017.     }
  6018.     else if (q->weight && !prev_weight) {
  6019.         ast_atomic_fetchadd_int(&use_weight, +1);
  6020.     }
  6021.  
  6022.     /* Free remaining members marked as delme */
  6023.     if (member_reload) {
  6024.         ao2_callback(q->members, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, kill_dead_members, q);
  6025.     }
  6026.  
  6027.     if (new) {
  6028.         queues_t_link(queues, q, "Add queue to container");
  6029.     } else {
  6030.         ao2_unlock(q);
  6031.     }
  6032.     queue_t_unref(q, "Expiring creation reference");
  6033. }
  6034.  
  6035. static int mark_dead_and_unfound(void *obj, void *arg, int flags)
  6036. {
  6037.     struct call_queue *q = obj;
  6038.     char *queuename = arg;
  6039.     if (!q->realtime && (ast_strlen_zero(queuename) || !strcasecmp(queuename, q->name))) {
  6040.         q->dead = 1;
  6041.         q->found = 0;
  6042.     }
  6043.     return 0;
  6044. }
  6045.  
  6046. static int kill_dead_queues(void *obj, void *arg, int flags)
  6047. {
  6048.     struct call_queue *q = obj;
  6049.     char *queuename = arg;
  6050.     if ((ast_strlen_zero(queuename) || !strcasecmp(queuename, q->name)) && q->dead) {
  6051.         return CMP_MATCH;
  6052.     } else {
  6053.         return 0;
  6054.     }
  6055. }
  6056.  
  6057. /*! \brief reload the queues.conf file
  6058.  *
  6059.  * This function reloads the information in the general section of the queues.conf
  6060.  * file and potentially more, depending on the value of mask.
  6061.  *
  6062.  * \param reload 0 if we are calling this the first time, 1 every other time
  6063.  * \param mask Gives flags telling us what information to actually reload
  6064.  * \param queuename If set to a non-zero string, then only reload information from
  6065.  * that particular queue. Otherwise inspect all queues
  6066.  * \retval -1 Failure occurred
  6067.  * \retval 0 All clear!
  6068.  */
  6069. static int reload_queues(int reload, struct ast_flags *mask, const char *queuename)
  6070. {
  6071.     struct ast_config *cfg;
  6072.     char *cat;
  6073.     struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  6074.     const int queue_reload = ast_test_flag(mask, QUEUE_RELOAD_PARAMETERS);
  6075.  
  6076.     if (!(cfg = ast_config_load("queues.conf", config_flags))) {
  6077.         ast_log(LOG_NOTICE, "No call queueing config file (queues.conf), so no call queues\n");
  6078.         return -1;
  6079.     } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  6080.         return 0;
  6081.     } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  6082.         ast_log(LOG_ERROR, "Config file queues.conf is in an invalid format.  Aborting.\n");
  6083.         return -1;
  6084.     }
  6085.  
  6086.     /* We've made it here, so it looks like we're doing operations on all queues. */
  6087.     ao2_lock(queues);
  6088.    
  6089.     /* Mark all queues as dead for the moment if we're reloading queues.
  6090.      * For clarity, we could just be reloading members, in which case we don't want to mess
  6091.      * with the other queue parameters at all*/
  6092.     if (queue_reload) {
  6093.         ao2_callback(queues, OBJ_NODATA, mark_dead_and_unfound, (char *) queuename);
  6094.     }
  6095.  
  6096.     /* Chug through config file */
  6097.     cat = NULL;
  6098.     while ((cat = ast_category_browse(cfg, cat)) ) {
  6099.         if (!strcasecmp(cat, "general") && queue_reload) {
  6100.             queue_set_global_params(cfg);
  6101.             continue;
  6102.         }
  6103.         if (ast_strlen_zero(queuename) || !strcasecmp(cat, queuename))
  6104.             reload_single_queue(cfg, mask, cat);
  6105.     }
  6106.  
  6107.     ast_config_destroy(cfg);
  6108.     /* Unref all the dead queues if we were reloading queues */
  6109.     if (queue_reload) {
  6110.         ao2_callback(queues, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, kill_dead_queues, (char *) queuename);
  6111.     }
  6112.     ao2_unlock(queues);
  6113.     return 0;
  6114. }
  6115.  
  6116. /*! \brief Facilitates resetting statistics for a queue
  6117.  *
  6118.  * This function actually does not reset any statistics, but
  6119.  * rather finds a call_queue struct which corresponds to the
  6120.  * passed-in queue name and passes that structure to the
  6121.  * clear_queue function. If no queuename is passed in, then
  6122.  * all queues will have their statistics reset.
  6123.  *
  6124.  * \param queuename The name of the queue to reset the statistics
  6125.  * for. If this is NULL or zero-length, then this means to reset
  6126.  * the statistics for all queues
  6127.  * \retval void
  6128.  */
  6129. static int clear_stats(const char *queuename)
  6130. {
  6131.     struct call_queue *q;
  6132.     struct ao2_iterator queue_iter = ao2_iterator_init(queues, 0);
  6133.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  6134.         ao2_lock(q);
  6135.         if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename))
  6136.             clear_queue(q);
  6137.         ao2_unlock(q);
  6138.         queue_t_unref(q, "Done with iterator");
  6139.     }
  6140.     ao2_iterator_destroy(&queue_iter);
  6141.     return 0;
  6142. }
  6143.  
  6144. /*! \brief The command center for all reload operations
  6145.  *
  6146.  * Whenever any piece of queue information is to be reloaded, this function
  6147.  * is called. It interprets the flags set in the mask parameter and acts
  6148.  * based on how they are set.
  6149.  *
  6150.  * \param reload True if we are reloading information, false if we are loading
  6151.  * information for the first time.
  6152.  * \param mask A bitmask which tells the handler what actions to take
  6153.  * \param queuename The name of the queue on which we wish to take action
  6154.  * \retval 0 All reloads were successful
  6155.  * \retval non-zero There was a failure
  6156.  */
  6157. static int reload_handler(int reload, struct ast_flags *mask, const char *queuename)
  6158. {
  6159.     int res = 0;
  6160.  
  6161.     if (ast_test_flag(mask, QUEUE_RELOAD_RULES)) {
  6162.         res |= reload_queue_rules(reload);
  6163.     }
  6164.     if (ast_test_flag(mask, QUEUE_RESET_STATS)) {
  6165.         res |= clear_stats(queuename);
  6166.     }
  6167.     if (ast_test_flag(mask, (QUEUE_RELOAD_PARAMETERS | QUEUE_RELOAD_MEMBER))) {
  6168.         res |= reload_queues(reload, mask, queuename);
  6169.     }
  6170.     return res;
  6171. }
  6172.  
  6173. /*! \brief direct ouput to manager or cli with proper terminator */
  6174. static void do_print(struct mansession *s, int fd, const char *str)
  6175. {
  6176.     if (s)
  6177.         astman_append(s, "%s\r\n", str);
  6178.     else
  6179.         ast_cli(fd, "%s\n", str);
  6180. }
  6181.  
  6182. /*!
  6183.  * \brief Show queue(s) status and statistics
  6184.  *
  6185.  * List the queues strategy, calls processed, members logged in,
  6186.  * other queue statistics such as avg hold time.
  6187. */
  6188. static char *__queues_show(struct mansession *s, int fd, int argc, char **argv)
  6189. {
  6190.     struct call_queue *q;
  6191.     struct ast_str *out = ast_str_alloca(240);
  6192.     int found = 0;
  6193.     time_t now = time(NULL);
  6194.     struct ao2_iterator queue_iter;
  6195.     struct ao2_iterator mem_iter;
  6196.  
  6197.     if (argc != 2 && argc != 3)
  6198.         return CLI_SHOWUSAGE;
  6199.  
  6200.     if (argc == 3)  { /* specific queue */
  6201.         if ((q = load_realtime_queue(argv[2]))) {
  6202.             queue_t_unref(q, "Done with temporary pointer");
  6203.         }
  6204.     } else if (ast_check_realtime("queues")) {
  6205.         /* This block is to find any queues which are defined in realtime but
  6206.          * which have not yet been added to the in-core container
  6207.          */
  6208.         struct ast_config *cfg = ast_load_realtime_multientry("queues", "name LIKE", "%", SENTINEL);
  6209.         char *queuename;
  6210.         if (cfg) {
  6211.             for (queuename = ast_category_browse(cfg, NULL); !ast_strlen_zero(queuename); queuename = ast_category_browse(cfg, queuename)) {
  6212.                 if ((q = load_realtime_queue(queuename))) {
  6213.                     queue_t_unref(q, "Done with temporary pointer");
  6214.                 }
  6215.             }
  6216.             ast_config_destroy(cfg);
  6217.         }
  6218.     }
  6219.  
  6220.     queue_iter = ao2_iterator_init(queues, AO2_ITERATOR_DONTLOCK);
  6221.     ao2_lock(queues);
  6222.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  6223.         float sl;
  6224.         struct call_queue *realtime_queue = NULL;
  6225.  
  6226.         ao2_lock(q);
  6227.         /* This check is to make sure we don't print information for realtime
  6228.          * queues which have been deleted from realtime but which have not yet
  6229.          * been deleted from the in-core container
  6230.          */
  6231.         if (q->realtime && !(realtime_queue = load_realtime_queue(q->name))) {
  6232.             ao2_unlock(q);
  6233.             queue_t_unref(q, "Done with iterator");
  6234.             continue;
  6235.         } else if (q->realtime) {
  6236.             queue_t_unref(realtime_queue, "Queue is already in memory");
  6237.         }
  6238.         if (argc == 3 && strcasecmp(q->name, argv[2])) {
  6239.             ao2_unlock(q);
  6240.             queue_t_unref(q, "Done with iterator");
  6241.             continue;
  6242.         }
  6243.         found = 1;
  6244.  
  6245.         ast_str_set(&out, 0, "%s has %d calls (max ", q->name, q->count);
  6246.         if (q->maxlen)
  6247.             ast_str_append(&out, 0, "%d", q->maxlen);
  6248.         else
  6249.             ast_str_append(&out, 0, "unlimited");
  6250.         sl = 0;
  6251.         if (q->callscompleted > 0)
  6252.             sl = 100 * ((float) q->callscompletedinsl / (float) q->callscompleted);
  6253.         ast_str_append(&out, 0, ") in '%s' strategy (%ds holdtime, %ds talktime), W:%d, C:%d, A:%d, SL:%2.1f%% within %ds",
  6254.             int2strat(q->strategy), q->holdtime, q->talktime, q->weight,
  6255.             q->callscompleted, q->callsabandoned,sl,q->servicelevel);
  6256.         do_print(s, fd, ast_str_buffer(out));
  6257.         if (!ao2_container_count(q->members))
  6258.             do_print(s, fd, "   No Members");
  6259.         else {
  6260.             struct member *mem;
  6261.  
  6262.             do_print(s, fd, "   Members: ");
  6263.             mem_iter = ao2_iterator_init(q->members, 0);
  6264.             while ((mem = ao2_iterator_next(&mem_iter))) {
  6265.                 ast_str_set(&out, 0, "      %s", mem->membername);
  6266.                 if (strcasecmp(mem->membername, mem->interface)) {
  6267.                     ast_str_append(&out, 0, " (%s)", mem->interface);
  6268.                 }
  6269.                 if (mem->penalty)
  6270.                     ast_str_append(&out, 0, " with penalty %d", mem->penalty);
  6271.                 ast_str_append(&out, 0, "%s%s%s (%s)",
  6272.                     mem->dynamic ? " (dynamic)" : "",
  6273.                     mem->realtime ? " (realtime)" : "",
  6274.                     mem->paused ? " (paused)" : "",
  6275.                     ast_devstate2str(mem->status));
  6276.                 if (mem->calls)
  6277.                     ast_str_append(&out, 0, " has taken %d calls (last was %ld secs ago)",
  6278.                         mem->calls, (long) (time(NULL) - mem->lastcall));
  6279.                 else
  6280.                     ast_str_append(&out, 0, " has taken no calls yet");
  6281.                 do_print(s, fd, ast_str_buffer(out));
  6282.                 ao2_ref(mem, -1);
  6283.             }
  6284.             ao2_iterator_destroy(&mem_iter);
  6285.         }
  6286.         if (!q->head)
  6287.             do_print(s, fd, "   No Callers");
  6288.         else {
  6289.             struct queue_ent *qe;
  6290.             int pos = 1;
  6291.  
  6292.             do_print(s, fd, "   Callers: ");
  6293.             for (qe = q->head; qe; qe = qe->next) {
  6294.                 ast_str_set(&out, 0, "      %d. %s (wait: %ld:%2.2ld, prio: %d)",
  6295.                     pos++, qe->chan->name, (long) (now - qe->start) / 60,
  6296.                     (long) (now - qe->start) % 60, qe->prio);
  6297.                 do_print(s, fd, ast_str_buffer(out));
  6298.             }
  6299.         }
  6300.         do_print(s, fd, "");    /* blank line between entries */
  6301.         ao2_unlock(q);
  6302.         queue_t_unref(q, "Done with iterator"); /* Unref the iterator's reference */
  6303.     }
  6304.     ao2_iterator_destroy(&queue_iter);
  6305.     ao2_unlock(queues);
  6306.     if (!found) {
  6307.         if (argc == 3)
  6308.             ast_str_set(&out, 0, "No such queue: %s.", argv[2]);
  6309.         else
  6310.             ast_str_set(&out, 0, "No queues.");
  6311.         do_print(s, fd, ast_str_buffer(out));
  6312.     }
  6313.     return CLI_SUCCESS;
  6314. }
  6315.  
  6316. static char *complete_queue(const char *line, const char *word, int pos, int state)
  6317. {
  6318.     struct call_queue *q;
  6319.     char *ret = NULL;
  6320.     int which = 0;
  6321.     int wordlen = strlen(word);
  6322.     struct ao2_iterator queue_iter;
  6323.  
  6324.     queue_iter = ao2_iterator_init(queues, 0);
  6325.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  6326.         if (!strncasecmp(word, q->name, wordlen) && ++which > state) {
  6327.             ret = ast_strdup(q->name);
  6328.             queue_t_unref(q, "Done with iterator");
  6329.             break;
  6330.         }
  6331.         queue_t_unref(q, "Done with iterator");
  6332.     }
  6333.     ao2_iterator_destroy(&queue_iter);
  6334.  
  6335.     return ret;
  6336. }
  6337.  
  6338. static char *complete_queue_show(const char *line, const char *word, int pos, int state)
  6339. {
  6340.     if (pos == 2)
  6341.         return complete_queue(line, word, pos, state);
  6342.     return NULL;
  6343. }
  6344.  
  6345. static char *queue_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6346. {
  6347.     switch ( cmd ) {
  6348.     case CLI_INIT:
  6349.         e->command = "queue show";
  6350.         e->usage =
  6351.             "Usage: queue show\n"
  6352.             "       Provides summary information on a specified queue.\n";
  6353.         return NULL;
  6354.     case CLI_GENERATE:
  6355.         return complete_queue_show(a->line, a->word, a->pos, a->n);
  6356.     }
  6357.  
  6358.     return __queues_show(NULL, a->fd, a->argc, a->argv);
  6359. }
  6360.  
  6361. /*!\brief callback to display queues status in manager
  6362.    \addtogroup Group_AMI
  6363.  */
  6364. static int manager_queues_show(struct mansession *s, const struct message *m)
  6365. {
  6366.     char *a[] = { "queue", "show" };
  6367.  
  6368.     __queues_show(s, -1, 2, a);
  6369.     astman_append(s, "\r\n\r\n");   /* Properly terminate Manager output */
  6370.  
  6371.     return RESULT_SUCCESS;
  6372. }
  6373.  
  6374. static int manager_queue_rule_show(struct mansession *s, const struct message *m)
  6375. {
  6376.     const char *rule = astman_get_header(m, "Rule");
  6377.     struct rule_list *rl_iter;
  6378.     struct penalty_rule *pr_iter;
  6379.  
  6380.     AST_LIST_LOCK(&rule_lists);
  6381.     AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
  6382.         if (ast_strlen_zero(rule) || !strcasecmp(rule, rl_iter->name)) {
  6383.             astman_append(s, "RuleList: %s\r\n", rl_iter->name);
  6384.             AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
  6385.                 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 );
  6386.             }
  6387.             if (!ast_strlen_zero(rule))
  6388.                 break;
  6389.         }
  6390.     }
  6391.     AST_LIST_UNLOCK(&rule_lists);
  6392.  
  6393.     astman_append(s, "\r\n\r\n");
  6394.  
  6395.     return RESULT_SUCCESS;
  6396. }
  6397.  
  6398. /*! \brief Summary of queue info via the AMI */
  6399. static int manager_queues_summary(struct mansession *s, const struct message *m)
  6400. {
  6401.     time_t now;
  6402.     int qmemcount = 0;
  6403.     int qmemavail = 0;
  6404.     int qchancount = 0;
  6405.     int qlongestholdtime = 0;
  6406.     const char *id = astman_get_header(m, "ActionID");
  6407.     const char *queuefilter = astman_get_header(m, "Queue");
  6408.     char idText[256] = "";
  6409.     struct call_queue *q;
  6410.     struct queue_ent *qe;
  6411.     struct member *mem;
  6412.     struct ao2_iterator queue_iter;
  6413.     struct ao2_iterator mem_iter;
  6414.  
  6415.     astman_send_ack(s, m, "Queue summary will follow");
  6416.     time(&now);
  6417.     if (!ast_strlen_zero(id))
  6418.         snprintf(idText, 256, "ActionID: %s\r\n", id);
  6419.     queue_iter = ao2_iterator_init(queues, 0);
  6420.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  6421.         ao2_lock(q);
  6422.  
  6423.         /* List queue properties */
  6424.         if (ast_strlen_zero(queuefilter) || !strcmp(q->name, queuefilter)) {
  6425.             /* Reset the necessary local variables if no queuefilter is set*/
  6426.             qmemcount = 0;
  6427.             qmemavail = 0;
  6428.             qchancount = 0;
  6429.             qlongestholdtime = 0;
  6430.  
  6431.             /* List Queue Members */
  6432.             mem_iter = ao2_iterator_init(q->members, 0);
  6433.             while ((mem = ao2_iterator_next(&mem_iter))) {
  6434.                 if ((mem->status != AST_DEVICE_UNAVAILABLE) && (mem->status != AST_DEVICE_INVALID)) {
  6435.                     ++qmemcount;
  6436.                     if (((mem->status == AST_DEVICE_NOT_INUSE) || (mem->status == AST_DEVICE_UNKNOWN)) && !(mem->paused)) {
  6437.                         ++qmemavail;
  6438.                     }
  6439.                 }
  6440.                 ao2_ref(mem, -1);
  6441.             }
  6442.             ao2_iterator_destroy(&mem_iter);
  6443.             for (qe = q->head; qe; qe = qe->next) {
  6444.                 if ((now - qe->start) > qlongestholdtime) {
  6445.                     qlongestholdtime = now - qe->start;
  6446.                 }
  6447.                 ++qchancount;
  6448.             }
  6449.             astman_append(s, "Event: QueueSummary\r\n"
  6450.                 "Queue: %s\r\n"
  6451.                 "LoggedIn: %d\r\n"
  6452.                 "Available: %d\r\n"
  6453.                 "Callers: %d\r\n"
  6454.                 "HoldTime: %d\r\n"
  6455.                 "TalkTime: %d\r\n"
  6456.                 "LongestHoldTime: %d\r\n"
  6457.                 "%s"
  6458.                 "\r\n",
  6459.                 q->name, qmemcount, qmemavail, qchancount, q->holdtime, q->talktime, qlongestholdtime, idText);
  6460.         }
  6461.         ao2_unlock(q);
  6462.         queue_t_unref(q, "Done with iterator");
  6463.     }
  6464.     ao2_iterator_destroy(&queue_iter);
  6465.     astman_append(s,
  6466.         "Event: QueueSummaryComplete\r\n"
  6467.         "%s"
  6468.         "\r\n", idText);
  6469.  
  6470.     return RESULT_SUCCESS;
  6471. }
  6472.  
  6473. /*! \brief Queue status info via AMI */
  6474. static int manager_queues_status(struct mansession *s, const struct message *m)
  6475. {
  6476.     time_t now;
  6477.     int pos;
  6478.     const char *id = astman_get_header(m,"ActionID");
  6479.     const char *queuefilter = astman_get_header(m,"Queue");
  6480.     const char *memberfilter = astman_get_header(m,"Member");
  6481.     char idText[256] = "";
  6482.     struct call_queue *q;
  6483.     struct queue_ent *qe;
  6484.     float sl = 0;
  6485.     struct member *mem;
  6486.     struct ao2_iterator queue_iter;
  6487.     struct ao2_iterator mem_iter;
  6488.  
  6489.     astman_send_ack(s, m, "Queue status will follow");
  6490.     time(&now);
  6491.     if (!ast_strlen_zero(id))
  6492.         snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
  6493.  
  6494.     queue_iter = ao2_iterator_init(queues, 0);
  6495.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  6496.         ao2_lock(q);
  6497.  
  6498.         /* List queue properties */
  6499.         if (ast_strlen_zero(queuefilter) || !strcmp(q->name, queuefilter)) {
  6500.             sl = ((q->callscompleted > 0) ? 100 * ((float)q->callscompletedinsl / (float)q->callscompleted) : 0);
  6501.             astman_append(s, "Event: QueueParams\r\n"
  6502.                 "Queue: %s\r\n"
  6503.                 "Max: %d\r\n"
  6504.                 "Strategy: %s\r\n"
  6505.                 "Calls: %d\r\n"
  6506.                 "Holdtime: %d\r\n"
  6507.                 "TalkTime: %d\r\n"
  6508.                 "Completed: %d\r\n"
  6509.                 "Abandoned: %d\r\n"
  6510.                 "ServiceLevel: %d\r\n"
  6511.                 "ServicelevelPerf: %2.1f\r\n"
  6512.                 "Weight: %d\r\n"
  6513.                 "%s"
  6514.                 "\r\n",
  6515.                 q->name, q->maxlen, int2strat(q->strategy), q->count, q->holdtime, q->talktime, q->callscompleted,
  6516.                 q->callsabandoned, q->servicelevel, sl, q->weight, idText);
  6517.             /* List Queue Members */
  6518.             mem_iter = ao2_iterator_init(q->members, 0);
  6519.             while ((mem = ao2_iterator_next(&mem_iter))) {
  6520.                 if (ast_strlen_zero(memberfilter) || !strcmp(mem->interface, memberfilter) || !strcmp(mem->membername, memberfilter)) {
  6521.                     astman_append(s, "Event: QueueMember\r\n"
  6522.                         "Queue: %s\r\n"
  6523.                         "Name: %s\r\n"
  6524.                         "Location: %s\r\n"
  6525.                         "Membership: %s\r\n"
  6526.                         "Penalty: %d\r\n"
  6527.                         "CallsTaken: %d\r\n"
  6528.                         "LastCall: %d\r\n"
  6529.                         "Status: %d\r\n"
  6530.                         "Paused: %d\r\n"
  6531.                         "%s"
  6532.                         "\r\n",
  6533.                         q->name, mem->membername, mem->interface, mem->dynamic ? "dynamic" : "static",
  6534.                         mem->penalty, mem->calls, (int)mem->lastcall, mem->status, mem->paused, idText);
  6535.                 }
  6536.                 ao2_ref(mem, -1);
  6537.             }
  6538.             ao2_iterator_destroy(&mem_iter);
  6539.             /* List Queue Entries */
  6540.             pos = 1;
  6541.             for (qe = q->head; qe; qe = qe->next) {
  6542.                 astman_append(s, "Event: QueueEntry\r\n"
  6543.                     "Queue: %s\r\n"
  6544.                     "Position: %d\r\n"
  6545.                     "Channel: %s\r\n"
  6546.                     "Uniqueid: %s\r\n"
  6547.                     "CallerIDNum: %s\r\n"
  6548.                     "CallerIDName: %s\r\n"
  6549.                     "Wait: %ld\r\n"
  6550.                     "%s"
  6551.                     "\r\n",
  6552.                     q->name, pos++, qe->chan->name, qe->chan->uniqueid,
  6553.                     S_OR(qe->chan->cid.cid_num, "unknown"),
  6554.                     S_OR(qe->chan->cid.cid_name, "unknown"),
  6555.                     (long) (now - qe->start), idText);
  6556.             }
  6557.         }
  6558.         ao2_unlock(q);
  6559.         queue_t_unref(q, "Done with iterator");
  6560.     }
  6561.     ao2_iterator_destroy(&queue_iter);
  6562.  
  6563.     astman_append(s,
  6564.         "Event: QueueStatusComplete\r\n"
  6565.         "%s"
  6566.         "\r\n",idText);
  6567.  
  6568.     return RESULT_SUCCESS;
  6569. }
  6570.  
  6571. static int manager_add_queue_member(struct mansession *s, const struct message *m)
  6572. {
  6573.     const char *queuename, *interface, *penalty_s, *paused_s, *membername, *state_interface;
  6574.     int paused, penalty = 0;
  6575.  
  6576.     queuename = astman_get_header(m, "Queue");
  6577.     interface = astman_get_header(m, "Interface");
  6578.     penalty_s = astman_get_header(m, "Penalty");
  6579.     paused_s = astman_get_header(m, "Paused");
  6580.     membername = astman_get_header(m, "MemberName");
  6581.     state_interface = astman_get_header(m, "StateInterface");
  6582.  
  6583.     if (ast_strlen_zero(queuename)) {
  6584.         astman_send_error(s, m, "'Queue' not specified.");
  6585.         return 0;
  6586.     }
  6587.  
  6588.     if (ast_strlen_zero(interface)) {
  6589.         astman_send_error(s, m, "'Interface' not specified.");
  6590.         return 0;
  6591.     }
  6592.  
  6593.     if (ast_strlen_zero(penalty_s))
  6594.         penalty = 0;
  6595.     else if (sscanf(penalty_s, "%30d", &penalty) != 1 || penalty < 0)
  6596.         penalty = 0;
  6597.  
  6598.     if (ast_strlen_zero(paused_s))
  6599.         paused = 0;
  6600.     else
  6601.         paused = abs(ast_true(paused_s));
  6602.  
  6603.     switch (add_to_queue(queuename, interface, membername, penalty, paused, queue_persistent_members, state_interface)) {
  6604.     case RES_OKAY:
  6605.         ast_queue_log(queuename, "MANAGER", interface, "ADDMEMBER", "%s", "");
  6606.         astman_send_ack(s, m, "Added interface to queue");
  6607.         break;
  6608.     case RES_EXISTS:
  6609.         astman_send_error(s, m, "Unable to add interface: Already there");
  6610.         break;
  6611.     case RES_NOSUCHQUEUE:
  6612.         astman_send_error(s, m, "Unable to add interface to queue: No such queue");
  6613.         break;
  6614.     case RES_OUTOFMEMORY:
  6615.         astman_send_error(s, m, "Out of memory");
  6616.         break;
  6617.     }
  6618.  
  6619.     return 0;
  6620. }
  6621.  
  6622. static int manager_remove_queue_member(struct mansession *s, const struct message *m)
  6623. {
  6624.     const char *queuename, *interface;
  6625.  
  6626.     queuename = astman_get_header(m, "Queue");
  6627.     interface = astman_get_header(m, "Interface");
  6628.  
  6629.     if (ast_strlen_zero(queuename) || ast_strlen_zero(interface)) {
  6630.         astman_send_error(s, m, "Need 'Queue' and 'Interface' parameters.");
  6631.         return 0;
  6632.     }
  6633.  
  6634.     switch (remove_from_queue(queuename, interface)) {
  6635.     case RES_OKAY:
  6636.         ast_queue_log(queuename, "MANAGER", interface, "REMOVEMEMBER", "%s", "");
  6637.         astman_send_ack(s, m, "Removed interface from queue");
  6638.         break;
  6639.     case RES_EXISTS:
  6640.         astman_send_error(s, m, "Unable to remove interface: Not there");
  6641.         break;
  6642.     case RES_NOSUCHQUEUE:
  6643.         astman_send_error(s, m, "Unable to remove interface from queue: No such queue");
  6644.         break;
  6645.     case RES_OUTOFMEMORY:
  6646.         astman_send_error(s, m, "Out of memory");
  6647.         break;
  6648.     case RES_NOT_DYNAMIC:
  6649.         astman_send_error(s, m, "Member not dynamic");
  6650.         break;
  6651.     }
  6652.  
  6653.     return 0;
  6654. }
  6655.  
  6656. static int manager_pause_queue_member(struct mansession *s, const struct message *m)
  6657. {
  6658.     const char *queuename, *interface, *paused_s, *reason;
  6659.     int paused;
  6660.  
  6661.     interface = astman_get_header(m, "Interface");
  6662.     paused_s = astman_get_header(m, "Paused");
  6663.     queuename = astman_get_header(m, "Queue");      /* Optional - if not supplied, pause the given Interface in all queues */
  6664.     reason = astman_get_header(m, "Reason");        /* Optional - Only used for logging purposes */
  6665.  
  6666.     if (ast_strlen_zero(interface) || ast_strlen_zero(paused_s)) {
  6667.         astman_send_error(s, m, "Need 'Interface' and 'Paused' parameters.");
  6668.         return 0;
  6669.     }
  6670.  
  6671.     paused = abs(ast_true(paused_s));
  6672.  
  6673.     if (set_member_paused(queuename, interface, reason, paused))
  6674.         astman_send_error(s, m, "Interface not found");
  6675.     else
  6676.         astman_send_ack(s, m, paused ? "Interface paused successfully" : "Interface unpaused successfully");
  6677.     return 0;
  6678. }
  6679.  
  6680. static int manager_queue_log_custom(struct mansession *s, const struct message *m)
  6681. {
  6682.     const char *queuename, *event, *message, *interface, *uniqueid;
  6683.  
  6684.     queuename = astman_get_header(m, "Queue");
  6685.     uniqueid = astman_get_header(m, "UniqueId");
  6686.     interface = astman_get_header(m, "Interface");
  6687.     event = astman_get_header(m, "Event");
  6688.     message = astman_get_header(m, "Message");
  6689.  
  6690.     if (ast_strlen_zero(queuename) || ast_strlen_zero(event)) {
  6691.         astman_send_error(s, m, "Need 'Queue' and 'Event' parameters.");
  6692.         return 0;
  6693.     }
  6694.  
  6695.     ast_queue_log(queuename, S_OR(uniqueid, "NONE"), interface, event, "%s", message);
  6696.     astman_send_ack(s, m, "Event added successfully");
  6697.  
  6698.     return 0;
  6699. }
  6700.  
  6701. static int manager_queue_reload(struct mansession *s, const struct message *m)
  6702. {
  6703.     struct ast_flags mask = {0,};
  6704.     const char *queuename = NULL;
  6705.     int header_found = 0;
  6706.  
  6707.     queuename = astman_get_header(m, "Queue");
  6708.     if (!strcasecmp(S_OR(astman_get_header(m, "Members"), ""), "yes")) {
  6709.         ast_set_flag(&mask, QUEUE_RELOAD_MEMBER);
  6710.         header_found = 1;
  6711.     }
  6712.     if (!strcasecmp(S_OR(astman_get_header(m, "Rules"), ""), "yes")) {
  6713.         ast_set_flag(&mask, QUEUE_RELOAD_RULES);
  6714.         header_found = 1;
  6715.     }
  6716.     if (!strcasecmp(S_OR(astman_get_header(m, "Parameters"), ""), "yes")) {
  6717.         ast_set_flag(&mask, QUEUE_RELOAD_PARAMETERS);
  6718.         header_found = 1;
  6719.     }
  6720.  
  6721.     if (!header_found) {
  6722.         ast_set_flag(&mask, AST_FLAGS_ALL);
  6723.     }
  6724.  
  6725.     if (!reload_handler(1, &mask, queuename)) {
  6726.         astman_send_ack(s, m, "Queue reloaded successfully");
  6727.     } else {
  6728.         astman_send_error(s, m, "Error encountered while reloading queue");
  6729.     }
  6730.     return 0;
  6731. }
  6732.  
  6733. static int manager_queue_reset(struct mansession *s, const struct message *m)
  6734. {
  6735.     const char *queuename = NULL;
  6736.     struct ast_flags mask = {QUEUE_RESET_STATS,};
  6737.    
  6738.     queuename = astman_get_header(m, "Queue");
  6739.  
  6740.     if (!reload_handler(1, &mask, queuename)) {
  6741.         astman_send_ack(s, m, "Queue stats reset successfully");
  6742.     } else {
  6743.         astman_send_error(s, m, "Error encountered while resetting queue stats");
  6744.     }
  6745.     return 0;
  6746. }
  6747.  
  6748. static char *complete_queue_add_member(const char *line, const char *word, int pos, int state)
  6749. {
  6750.     /* 0 - queue; 1 - add; 2 - member; 3 - <interface>; 4 - to; 5 - <queue>; 6 - penalty; 7 - <penalty>; 8 - as; 9 - <membername> */
  6751.     switch (pos) {
  6752.     case 3: /* Don't attempt to complete name of interface (infinite possibilities) */
  6753.         return NULL;
  6754.     case 4: /* only one possible match, "to" */
  6755.         return state == 0 ? ast_strdup("to") : NULL;
  6756.     case 5: /* <queue> */
  6757.         return complete_queue(line, word, pos, state);
  6758.     case 6: /* only one possible match, "penalty" */
  6759.         return state == 0 ? ast_strdup("penalty") : NULL;
  6760.     case 7:
  6761.         if (state < 100) {      /* 0-99 */
  6762.             char *num;
  6763.             if ((num = ast_malloc(3))) {
  6764.                 sprintf(num, "%d", state);
  6765.             }
  6766.             return num;
  6767.         } else {
  6768.             return NULL;
  6769.         }
  6770.     case 8: /* only one possible match, "as" */
  6771.         return state == 0 ? ast_strdup("as") : NULL;
  6772.     case 9: /* Don't attempt to complete name of member (infinite possibilities) */
  6773.         return NULL;
  6774.     default:
  6775.         return NULL;
  6776.     }
  6777. }
  6778.  
  6779. static int manager_queue_member_penalty(struct mansession *s, const struct message *m)
  6780. {
  6781.     const char *queuename, *interface, *penalty_s;
  6782.     int penalty;
  6783.  
  6784.     interface = astman_get_header(m, "Interface");
  6785.     penalty_s = astman_get_header(m, "Penalty");
  6786.     /* Optional - if not supplied, set the penalty value for the given Interface in all queues */
  6787.     queuename = astman_get_header(m, "Queue");
  6788.  
  6789.     if (ast_strlen_zero(interface) || ast_strlen_zero(penalty_s)) {
  6790.         astman_send_error(s, m, "Need 'Interface' and 'Penalty' parameters.");
  6791.         return 0;
  6792.     }
  6793.  
  6794.     penalty = atoi(penalty_s);
  6795.  
  6796.     if (set_member_penalty((char *)queuename, (char *)interface, penalty))
  6797.         astman_send_error(s, m, "Invalid interface, queuename or penalty");
  6798.     else
  6799.         astman_send_ack(s, m, "Interface penalty set successfully");
  6800.  
  6801.     return 0;
  6802. }
  6803.  
  6804. static char *handle_queue_add_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6805. {
  6806.     char *queuename, *interface, *membername = NULL, *state_interface = NULL;
  6807.     int penalty;
  6808.  
  6809.     switch ( cmd ) {
  6810.     case CLI_INIT:
  6811.         e->command = "queue add member";
  6812.         e->usage =
  6813.             "Usage: queue add member <channel> to <queue> [[[penalty <penalty>] as <membername>] state_interface <interface>]\n"
  6814.             "       Add a channel to a queue with optionally:  a penalty, membername and a state_interface\n";
  6815.         return NULL;
  6816.     case CLI_GENERATE:
  6817.         return complete_queue_add_member(a->line, a->word, a->pos, a->n);
  6818.     }
  6819.  
  6820.     if ((a->argc != 6) && (a->argc != 8) && (a->argc != 10) && (a->argc != 12)) {
  6821.         return CLI_SHOWUSAGE;
  6822.     } else if (strcmp(a->argv[4], "to")) {
  6823.         return CLI_SHOWUSAGE;
  6824.     } else if ((a->argc >= 8) && strcmp(a->argv[6], "penalty")) {
  6825.         return CLI_SHOWUSAGE;
  6826.     } else if ((a->argc >= 10) && strcmp(a->argv[8], "as")) {
  6827.         return CLI_SHOWUSAGE;
  6828.     } else if ((a->argc == 12) && strcmp(a->argv[10], "state_interface")) {
  6829.         return CLI_SHOWUSAGE;
  6830.     }
  6831.  
  6832.     queuename = a->argv[5];
  6833.     interface = a->argv[3];
  6834.     if (a->argc >= 8) {
  6835.         if (sscanf(a->argv[7], "%30d", &penalty) == 1) {
  6836.             if (penalty < 0) {
  6837.                 ast_cli(a->fd, "Penalty must be >= 0\n");
  6838.                 penalty = 0;
  6839.             }
  6840.         } else {
  6841.             ast_cli(a->fd, "Penalty must be an integer >= 0\n");
  6842.             penalty = 0;
  6843.         }
  6844.     } else {
  6845.         penalty = 0;
  6846.     }
  6847.  
  6848.     if (a->argc >= 10) {
  6849.         membername = a->argv[9];
  6850.     }
  6851.  
  6852.     if (a->argc >= 12) {
  6853.         state_interface = a->argv[11];
  6854.     }
  6855.  
  6856.     switch (add_to_queue(queuename, interface, membername, penalty, 0, queue_persistent_members, state_interface)) {
  6857.     case RES_OKAY:
  6858.         ast_queue_log(queuename, "CLI", interface, "ADDMEMBER", "%s", "");
  6859.         ast_cli(a->fd, "Added interface '%s' to queue '%s'\n", interface, queuename);
  6860.         return CLI_SUCCESS;
  6861.     case RES_EXISTS:
  6862.         ast_cli(a->fd, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
  6863.         return CLI_FAILURE;
  6864.     case RES_NOSUCHQUEUE:
  6865.         ast_cli(a->fd, "Unable to add interface to queue '%s': No such queue\n", queuename);
  6866.         return CLI_FAILURE;
  6867.     case RES_OUTOFMEMORY:
  6868.         ast_cli(a->fd, "Out of memory\n");
  6869.         return CLI_FAILURE;
  6870.     case RES_NOT_DYNAMIC:
  6871.         ast_cli(a->fd, "Member not dynamic\n");
  6872.         return CLI_FAILURE;
  6873.     default:
  6874.         return CLI_FAILURE;
  6875.     }
  6876. }
  6877.  
  6878. static char *complete_queue_remove_member(const char *line, const char *word, int pos, int state)
  6879. {
  6880.     int which = 0;
  6881.     struct call_queue *q;
  6882.     struct member *m;
  6883.     struct ao2_iterator queue_iter;
  6884.     struct ao2_iterator mem_iter;
  6885.     int wordlen = strlen(word);
  6886.  
  6887.     /* 0 - queue; 1 - remove; 2 - member; 3 - <member>; 4 - from; 5 - <queue> */
  6888.     if (pos > 5 || pos < 3)
  6889.         return NULL;
  6890.     if (pos == 4)   /* only one possible match, 'from' */
  6891.         return (state == 0 ? ast_strdup("from") : NULL);
  6892.  
  6893.     if (pos == 5)   /* No need to duplicate code */
  6894.         return complete_queue(line, word, pos, state);
  6895.  
  6896.     /* here is the case for 3, <member> */
  6897.     queue_iter = ao2_iterator_init(queues, 0);
  6898.     while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
  6899.         ao2_lock(q);
  6900.         mem_iter = ao2_iterator_init(q->members, 0);
  6901.         while ((m = ao2_iterator_next(&mem_iter))) {
  6902.             if (!strncasecmp(word, m->membername, wordlen) && ++which > state) {
  6903.                 char *tmp;
  6904.                 ao2_unlock(q);
  6905.                 tmp = ast_strdup(m->interface);
  6906.                 ao2_ref(m, -1);
  6907.                 queue_t_unref(q, "Done with iterator, returning interface name");
  6908.                 ao2_iterator_destroy(&mem_iter);
  6909.                 ao2_iterator_destroy(&queue_iter);
  6910.                 return tmp;
  6911.             }
  6912.             ao2_ref(m, -1);
  6913.         }
  6914.         ao2_iterator_destroy(&mem_iter);
  6915.         ao2_unlock(q);
  6916.         queue_t_unref(q, "Done with iterator");
  6917.     }
  6918.     ao2_iterator_destroy(&queue_iter);
  6919.  
  6920.     return NULL;
  6921. }
  6922.  
  6923. static char *handle_queue_remove_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6924. {
  6925.     char *queuename, *interface;
  6926.  
  6927.     switch (cmd) {
  6928.     case CLI_INIT:
  6929.         e->command = "queue remove member";
  6930.         e->usage =
  6931.             "Usage: queue remove member <channel> from <queue>\n"
  6932.             "       Remove a specific channel from a queue.\n";
  6933.         return NULL;
  6934.     case CLI_GENERATE:
  6935.         return complete_queue_remove_member(a->line, a->word, a->pos, a->n);
  6936.     }
  6937.  
  6938.     if (a->argc != 6) {
  6939.         return CLI_SHOWUSAGE;
  6940.     } else if (strcmp(a->argv[4], "from")) {
  6941.         return CLI_SHOWUSAGE;
  6942.     }
  6943.  
  6944.     queuename = a->argv[5];
  6945.     interface = a->argv[3];
  6946.  
  6947.     switch (remove_from_queue(queuename, interface)) {
  6948.     case RES_OKAY:
  6949.         ast_queue_log(queuename, "CLI", interface, "REMOVEMEMBER", "%s", "");
  6950.         ast_cli(a->fd, "Removed interface '%s' from queue '%s'\n", interface, queuename);
  6951.         return CLI_SUCCESS;
  6952.     case RES_EXISTS:
  6953.         ast_cli(a->fd, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
  6954.         return CLI_FAILURE;
  6955.     case RES_NOSUCHQUEUE:
  6956.         ast_cli(a->fd, "Unable to remove interface from queue '%s': No such queue\n", queuename);
  6957.         return CLI_FAILURE;
  6958.     case RES_OUTOFMEMORY:
  6959.         ast_cli(a->fd, "Out of memory\n");
  6960.         return CLI_FAILURE;
  6961.     case RES_NOT_DYNAMIC:
  6962.         ast_cli(a->fd, "Unable to remove interface '%s' from queue '%s': Member is not dynamic\n", interface, queuename);
  6963.         return CLI_FAILURE;
  6964.     default:
  6965.         return CLI_FAILURE;
  6966.     }
  6967. }
  6968.  
  6969. static char *complete_queue_pause_member(const char *line, const char *word, int pos, int state)
  6970. {
  6971.     /* 0 - queue; 1 - pause; 2 - member; 3 - <interface>; 4 - queue; 5 - <queue>; 6 - reason; 7 - <reason> */
  6972.     switch (pos) {
  6973.     case 3: /* Don't attempt to complete name of interface (infinite possibilities) */
  6974.         return NULL;
  6975.     case 4: /* only one possible match, "queue" */
  6976.         return state == 0 ? ast_strdup("queue") : NULL;
  6977.     case 5: /* <queue> */
  6978.         return complete_queue(line, word, pos, state);
  6979.     case 6: /* "reason" */
  6980.         return state == 0 ? ast_strdup("reason") : NULL;
  6981.     case 7: /* Can't autocomplete a reason, since it's 100% customizeable */
  6982.         return NULL;
  6983.     default:
  6984.         return NULL;
  6985.     }
  6986. }
  6987.  
  6988. static char *handle_queue_pause_member(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6989. {
  6990.     char *queuename, *interface, *reason;
  6991.     int paused;
  6992.  
  6993.     switch (cmd) {
  6994.     case CLI_INIT:
  6995.         e->command = "queue {pause|unpause} member";
  6996.         e->usage =
  6997.             "Usage: queue {pause|unpause} member <member> [queue <queue> [reason <reason>]]\n"
  6998.             "   Pause or unpause a queue member. Not specifying a particular queue\n"
  6999.             "   will pause or unpause a member across all queues to which the member\n"
  7000.             "   belongs.\n";
  7001.         return NULL;
  7002.     case CLI_GENERATE:
  7003.         return complete_queue_pause_member(a->line, a-> word, a->pos, a->n);
  7004.     }
  7005.  
  7006.     if (a->argc < 4 || a->argc == 5 || a->argc == 7 || a->argc > 8) {
  7007.         return CLI_SHOWUSAGE;
  7008.     } else if (a->argc >= 5 && strcmp(a->argv[4], "queue")) {
  7009.         return CLI_SHOWUSAGE;
  7010.     } else if (a->argc == 8 && strcmp(a->argv[6], "reason")) {
  7011.         return CLI_SHOWUSAGE;
  7012.     }
  7013.  
  7014.  
  7015.     interface = a->argv[3];
  7016.     queuename = a->argc >= 6 ? a->argv[5] : NULL;
  7017.     reason = a->argc == 8 ? a->argv[7] : NULL;
  7018.     paused = !strcasecmp(a->argv[1], "pause");
  7019.  
  7020.     if (set_member_paused(queuename, interface, reason, paused) == RESULT_SUCCESS) {
  7021.         ast_cli(a->fd, "%spaused interface '%s'", paused ? "" : "un", interface);
  7022.         if (!ast_strlen_zero(queuename))
  7023.             ast_cli(a->fd, " in queue '%s'", queuename);
  7024.         if (!ast_strlen_zero(reason))
  7025.             ast_cli(a->fd, " for reason '%s'", reason);
  7026.         ast_cli(a->fd, "\n");
  7027.         return CLI_SUCCESS;
  7028.     } else {
  7029.         ast_cli(a->fd, "Unable to %spause interface '%s'", paused ? "" : "un", interface);
  7030.         if (!ast_strlen_zero(queuename))
  7031.             ast_cli(a->fd, " in queue '%s'", queuename);
  7032.         if (!ast_strlen_zero(reason))
  7033.             ast_cli(a->fd, " for reason '%s'", reason);
  7034.         ast_cli(a->fd, "\n");
  7035.         return CLI_FAILURE;
  7036.     }
  7037. }
  7038.  
  7039. static char *complete_queue_set_member_penalty(const char *line, const char *word, int pos, int state)
  7040. {
  7041.     /* 0 - queue; 1 - set; 2 - penalty; 3 - <penalty>; 4 - on; 5 - <member>; 6 - in; 7 - <queue>;*/
  7042.     switch (pos) {
  7043.     case 4:
  7044.         if (state == 0) {
  7045.             return ast_strdup("on");
  7046.         } else {
  7047.             return NULL;
  7048.         }
  7049.     case 6:
  7050.         if (state == 0) {
  7051.             return ast_strdup("in");
  7052.         } else {
  7053.             return NULL;
  7054.         }
  7055.     case 7:
  7056.         return complete_queue(line, word, pos, state);
  7057.     default:
  7058.         return NULL;
  7059.     }
  7060. }
  7061.  
  7062. static char *handle_queue_set_member_penalty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  7063. {
  7064.     char *queuename = NULL, *interface;
  7065.     int penalty = 0;
  7066.  
  7067.     switch (cmd) {
  7068.     case CLI_INIT:
  7069.         e->command = "queue set penalty";
  7070.         e->usage =
  7071.         "Usage: queue set penalty <penalty> on <interface> [in <queue>]\n"
  7072.         "   Set a member's penalty in the queue specified. If no queue is specified\n"
  7073.         "   then that interface's penalty is set in all queues to which that interface is a member\n";
  7074.         return NULL;
  7075.     case CLI_GENERATE:
  7076.         return complete_queue_set_member_penalty(a->line, a->word, a->pos, a->n);
  7077.     }
  7078.  
  7079.     if (a->argc != 6 && a->argc != 8) {
  7080.         return CLI_SHOWUSAGE;
  7081.     } else if (strcmp(a->argv[4], "on") || (a->argc > 6 && strcmp(a->argv[6], "in"))) {
  7082.         return CLI_SHOWUSAGE;
  7083.     }
  7084.  
  7085.     if (a->argc == 8)
  7086.         queuename = a->argv[7];
  7087.     interface = a->argv[5];
  7088.     penalty = atoi(a->argv[3]);
  7089.  
  7090.     switch (set_member_penalty(queuename, interface, penalty)) {
  7091.     case RESULT_SUCCESS:
  7092.         ast_cli(a->fd, "Set penalty on interface '%s' from queue '%s'\n", interface, queuename);
  7093.         return CLI_SUCCESS;
  7094.     case RESULT_FAILURE:
  7095.         ast_cli(a->fd, "Failed to set penalty on interface '%s' from queue '%s'\n", interface, queuename);
  7096.         return CLI_FAILURE;
  7097.     default:
  7098.         return CLI_FAILURE;
  7099.     }
  7100. }
  7101.  
  7102. static char *complete_queue_rule_show(const char *line, const char *word, int pos, int state)
  7103. {
  7104.     int which = 0;
  7105.     struct rule_list *rl_iter;
  7106.     int wordlen = strlen(word);
  7107.     char *ret = NULL;
  7108.     if (pos != 3) /* Wha? */ {
  7109.         return NULL;
  7110.     }
  7111.  
  7112.     AST_LIST_LOCK(&rule_lists);
  7113.     AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
  7114.         if (!strncasecmp(word, rl_iter->name, wordlen) && ++which > state) {
  7115.             ret = ast_strdup(rl_iter->name);
  7116.             break;
  7117.         }
  7118.     }
  7119.     AST_LIST_UNLOCK(&rule_lists);
  7120.  
  7121.     return ret;
  7122. }
  7123.  
  7124. static char *handle_queue_rule_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  7125. {
  7126.     char *rule;
  7127.     struct rule_list *rl_iter;
  7128.     struct penalty_rule *pr_iter;
  7129.     switch (cmd) {
  7130.     case CLI_INIT:
  7131.         e->command = "queue show rules";
  7132.         e->usage =
  7133.         "Usage: queue show rules [rulename]\n"
  7134.         "   Show the list of rules associated with rulename. If no\n"
  7135.         "   rulename is specified, list all rules defined in queuerules.conf\n";
  7136.         return NULL;
  7137.     case CLI_GENERATE:
  7138.         return complete_queue_rule_show(a->line, a->word, a->pos, a->n);
  7139.     }
  7140.  
  7141.     if (a->argc != 3 && a->argc != 4)
  7142.         return CLI_SHOWUSAGE;
  7143.  
  7144.     rule = a->argc == 4 ? a->argv[3] : "";
  7145.     AST_LIST_LOCK(&rule_lists);
  7146.     AST_LIST_TRAVERSE(&rule_lists, rl_iter, list) {
  7147.         if (ast_strlen_zero(rule) || !strcasecmp(rl_iter->name, rule)) {
  7148.             ast_cli(a->fd, "Rule: %s\n", rl_iter->name);
  7149.             AST_LIST_TRAVERSE(&rl_iter->rules, pr_iter, list) {
  7150.                 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);
  7151.             }
  7152.         }
  7153.     }
  7154.     AST_LIST_UNLOCK(&rule_lists);
  7155.     return CLI_SUCCESS;
  7156. }
  7157.  
  7158. static char *handle_queue_reset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  7159. {
  7160.     struct ast_flags mask = {QUEUE_RESET_STATS,};
  7161.     int i;
  7162.  
  7163.     switch (cmd) {
  7164.         case CLI_INIT:
  7165.             e->command = "queue reset stats";
  7166.             e->usage =
  7167.                 "Usage: queue reset stats [<queuenames>]\n"
  7168.                 "\n"
  7169.                 "Issuing this command will reset statistics for\n"
  7170.                 "<queuenames>, or for all queues if no queue is\n"
  7171.                 "specified.\n";
  7172.             return NULL;
  7173.         case CLI_GENERATE:
  7174.             if (a->pos >= 3) {
  7175.                 return complete_queue(a->line, a->word, a->pos, a->n);
  7176.             } else {
  7177.                 return NULL;
  7178.             }
  7179.     }
  7180.  
  7181.     if (a->argc < 3) {
  7182.         return CLI_SHOWUSAGE;
  7183.     }
  7184.  
  7185.     if (a->argc == 3) {
  7186.         reload_handler(1, &mask, NULL);
  7187.         return CLI_SUCCESS;
  7188.     }
  7189.  
  7190.     for (i = 3; i < a->argc; ++i) {
  7191.         reload_handler(1, &mask, a->argv[i]);
  7192.     }
  7193.  
  7194.     return CLI_SUCCESS;
  7195. }
  7196.  
  7197. static char *handle_queue_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  7198. {
  7199.     struct ast_flags mask = {0,};
  7200.     int i;
  7201.  
  7202.     switch (cmd) {
  7203.         case CLI_INIT:
  7204.             e->command = "queue reload {parameters|members|rules|all}";
  7205.             e->usage =
  7206.                 "Usage: queue reload {parameters|members|rules|all} [<queuenames>]\n"
  7207.                 "Reload queues. If <queuenames> are specified, only reload information pertaining\n"
  7208.                 "to <queuenames>. One of 'parameters,' 'members,' 'rules,' or 'all' must be\n"
  7209.                 "specified in order to know what information to reload. Below is an explanation\n"
  7210.                 "of each of these qualifiers.\n"
  7211.                 "\n"
  7212.                 "\t'members' - reload queue members from queues.conf\n"
  7213.                 "\t'parameters' - reload all queue options except for queue members\n"
  7214.                 "\t'rules' - reload the queuerules.conf file\n"
  7215.                 "\t'all' - reload queue rules, parameters, and members\n"
  7216.                 "\n"
  7217.                 "Note: the 'rules' qualifier here cannot actually be applied to a specific queue.\n"
  7218.                 "Use of the 'rules' qualifier causes queuerules.conf to be reloaded. Even if only\n"
  7219.                 "one queue is specified when using this command, reloading queue rules may cause\n"
  7220.                 "other queues to be affected\n";
  7221.             return NULL;
  7222.         case CLI_GENERATE:
  7223.             if (a->pos >= 3) {
  7224.                 return complete_queue(a->line, a->word, a->pos, a->n);
  7225.             } else {
  7226.                 return NULL;
  7227.             }
  7228.     }
  7229.  
  7230.     if (a->argc < 3)
  7231.         return CLI_SHOWUSAGE;
  7232.  
  7233.     if (!strcasecmp(a->argv[2], "rules")) {
  7234.         ast_set_flag(&mask, QUEUE_RELOAD_RULES);
  7235.     } else if (!strcasecmp(a->argv[2], "members")) {
  7236.         ast_set_flag(&mask, QUEUE_RELOAD_MEMBER);
  7237.     } else if (!strcasecmp(a->argv[2], "parameters")) {
  7238.         ast_set_flag(&mask, QUEUE_RELOAD_PARAMETERS);
  7239.     } else if (!strcasecmp(a->argv[2], "all")) {
  7240.         ast_set_flag(&mask, AST_FLAGS_ALL);
  7241.     }
  7242.  
  7243.     if (a->argc == 3) {
  7244.         reload_handler(1, &mask, NULL);
  7245.         return CLI_SUCCESS;
  7246.     }
  7247.  
  7248.     for (i = 3; i < a->argc; ++i) {
  7249.         reload_handler(1, &mask, a->argv[i]);
  7250.     }
  7251.  
  7252.     return CLI_SUCCESS;
  7253. }
  7254.  
  7255. static const char qpm_cmd_usage[] =
  7256. "Usage: queue pause member <channel> in <queue> reason <reason>\n";
  7257.  
  7258. static const char qum_cmd_usage[] =
  7259. "Usage: queue unpause member <channel> in <queue> reason <reason>\n";
  7260.  
  7261. static const char qsmp_cmd_usage[] =
  7262. "Usage: queue set member penalty <channel> from <queue> <penalty>\n";
  7263.  
  7264. static struct ast_cli_entry cli_queue[] = {
  7265.     AST_CLI_DEFINE(queue_show, "Show status of a specified queue"),
  7266.     AST_CLI_DEFINE(handle_queue_add_member, "Add a channel to a specified queue"),
  7267.     AST_CLI_DEFINE(handle_queue_remove_member, "Removes a channel from a specified queue"),
  7268.     AST_CLI_DEFINE(handle_queue_pause_member, "Pause or unpause a queue member"),
  7269.     AST_CLI_DEFINE(handle_queue_set_member_penalty, "Set penalty for a channel of a specified queue"),
  7270.     AST_CLI_DEFINE(handle_queue_rule_show, "Show the rules defined in queuerules.conf"),
  7271.     AST_CLI_DEFINE(handle_queue_reload, "Reload queues, members, queue rules, or parameters"),
  7272.     AST_CLI_DEFINE(handle_queue_reset, "Reset statistics for a queue"),
  7273. };
  7274.  
  7275. static int unload_module(void)
  7276. {
  7277.     int res;
  7278.     struct ast_context *con;
  7279.     struct ao2_iterator q_iter;
  7280.     struct call_queue *q = NULL;
  7281.  
  7282.     ast_cli_unregister_multiple(cli_queue, ARRAY_LEN(cli_queue));
  7283.     res = ast_manager_unregister("QueueStatus");
  7284.     res |= ast_manager_unregister("Queues");
  7285.     res |= ast_manager_unregister("QueueRule");
  7286.     res |= ast_manager_unregister("QueueSummary");
  7287.     res |= ast_manager_unregister("QueueAdd");
  7288.     res |= ast_manager_unregister("QueueRemove");
  7289.     res |= ast_manager_unregister("QueuePause");
  7290.     res |= ast_manager_unregister("QueueLog");
  7291.     res |= ast_manager_unregister("QueuePenalty");
  7292.     res |= ast_unregister_application(app_aqm);
  7293.     res |= ast_unregister_application(app_rqm);
  7294.     res |= ast_unregister_application(app_pqm);
  7295.     res |= ast_unregister_application(app_upqm);
  7296.     res |= ast_unregister_application(app_ql);
  7297.     res |= ast_unregister_application(app);
  7298.     res |= ast_custom_function_unregister(&queuevar_function);
  7299.     res |= ast_custom_function_unregister(&queuemembercount_function);
  7300.     res |= ast_custom_function_unregister(&queuemembercount_dep);
  7301.     res |= ast_custom_function_unregister(&queuememberlist_function);
  7302.     res |= ast_custom_function_unregister(&queuewaitingcount_function);
  7303.     res |= ast_custom_function_unregister(&queuememberpenalty_function);
  7304.  
  7305.     if (device_state_sub)
  7306.         ast_event_unsubscribe(device_state_sub);
  7307.  
  7308.     if ((con = ast_context_find("app_queue_gosub_virtual_context"))) {
  7309.         ast_context_remove_extension2(con, "s", 1, NULL, 0);
  7310.         ast_context_destroy(con, "app_queue"); /* leave no trace */
  7311.     }
  7312.  
  7313.     q_iter = ao2_iterator_init(queues, 0);
  7314.     while ((q = ao2_t_iterator_next(&q_iter, "Iterate through queues"))) {
  7315.         queues_t_unlink(queues, q, "Remove queue from container due to unload");
  7316.         queue_t_unref(q, "Done with iterator");
  7317.     }
  7318.     ao2_iterator_destroy(&q_iter);
  7319.     ao2_ref(queues, -1);
  7320.     devicestate_tps = ast_taskprocessor_unreference(devicestate_tps);
  7321.     ast_unload_realtime("queue_members");
  7322.     return res;
  7323. }
  7324.  
  7325. static int load_module(void)
  7326. {
  7327.     int res;
  7328.     struct ast_context *con;
  7329.     struct ast_flags mask = {AST_FLAGS_ALL, };
  7330.  
  7331.     queues = ao2_container_alloc(MAX_QUEUE_BUCKETS, queue_hash_cb, queue_cmp_cb);
  7332.  
  7333.     use_weight = 0;
  7334.  
  7335.     if (reload_handler(0, &mask, NULL))
  7336.         return AST_MODULE_LOAD_DECLINE;
  7337.  
  7338.     con = ast_context_find_or_create(NULL, NULL, "app_queue_gosub_virtual_context", "app_queue");
  7339.     if (!con)
  7340.         ast_log(LOG_ERROR, "Queue virtual context 'app_queue_gosub_virtual_context' does not exist and unable to create\n");
  7341.     else
  7342.         ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "app_queue");
  7343.  
  7344.     if (queue_persistent_members)
  7345.         reload_queue_members();
  7346.  
  7347.     ast_cli_register_multiple(cli_queue, ARRAY_LEN(cli_queue));
  7348.     res = ast_register_application_xml(app, queue_exec);
  7349.     res |= ast_register_application_xml(app_aqm, aqm_exec);
  7350.     res |= ast_register_application_xml(app_rqm, rqm_exec);
  7351.     res |= ast_register_application_xml(app_pqm, pqm_exec);
  7352.     res |= ast_register_application_xml(app_upqm, upqm_exec);
  7353.     res |= ast_register_application_xml(app_ql, ql_exec);
  7354.     res |= ast_manager_register("Queues", 0, manager_queues_show, "Queues");
  7355.     res |= ast_manager_register("QueueStatus", 0, manager_queues_status, "Queue Status");
  7356.     res |= ast_manager_register("QueueSummary", 0, manager_queues_summary, "Queue Summary");
  7357.     res |= ast_manager_register("QueueAdd", EVENT_FLAG_AGENT, manager_add_queue_member, "Add interface to queue.");
  7358.     res |= ast_manager_register("QueueRemove", EVENT_FLAG_AGENT, manager_remove_queue_member, "Remove interface from queue.");
  7359.     res |= ast_manager_register("QueuePause", EVENT_FLAG_AGENT, manager_pause_queue_member, "Makes a queue member temporarily unavailable");
  7360.     res |= ast_manager_register("QueueLog", EVENT_FLAG_AGENT, manager_queue_log_custom, "Adds custom entry in queue_log");
  7361.     res |= ast_manager_register("QueuePenalty", EVENT_FLAG_AGENT, manager_queue_member_penalty, "Set the penalty for a queue member");
  7362.     res |= ast_manager_register("QueueRule", 0, manager_queue_rule_show, "Queue Rules");
  7363.     res |= ast_manager_register("QueueReload", 0, manager_queue_reload, "Reload a queue, queues, or any sub-section of a queue or queues");
  7364.     res |= ast_manager_register("QueueReset", 0, manager_queue_reset, "Reset queue statistics");
  7365.     res |= ast_custom_function_register(&queuevar_function);
  7366.     res |= ast_custom_function_register(&queuemembercount_function);
  7367.     res |= ast_custom_function_register(&queuemembercount_dep);
  7368.     res |= ast_custom_function_register(&queuememberlist_function);
  7369.     res |= ast_custom_function_register(&queuewaitingcount_function);
  7370.     res |= ast_custom_function_register(&queuememberpenalty_function);
  7371.  
  7372.     if (!(devicestate_tps = ast_taskprocessor_get("app_queue", 0))) {
  7373.         ast_log(LOG_WARNING, "devicestate taskprocessor reference failed - devicestate notifications will not occur\n");
  7374.     }
  7375.  
  7376.     if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL, AST_EVENT_IE_END))) {
  7377.         res = -1;
  7378.     }
  7379.  
  7380.     ast_realtime_require_field("queue_members", "paused", RQ_INTEGER1, 1, "uniqueid", RQ_UINTEGER2, 5, SENTINEL);
  7381.  
  7382.     return res ? AST_MODULE_LOAD_DECLINE : 0;
  7383. }
  7384.  
  7385. static int reload(void)
  7386. {
  7387.     struct ast_flags mask = {AST_FLAGS_ALL & ~QUEUE_RESET_STATS,};
  7388.     ast_unload_realtime("queue_members");
  7389.     reload_handler(1, &mask, NULL);
  7390.     return 0;
  7391. }
  7392.  
  7393. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "True Call Queueing",
  7394.         .load = load_module,
  7395.         .unload = unload_module,
  7396.         .reload = reload,
  7397.            );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement