Mon May 14 2012 03:42:40

Asterisk developer's documentation


features.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Routines implementing call features as call pickup, parking and transfer
00022  *
00023  * \author Mark Spencer <markster@digium.com> 
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 356963 $")
00033 
00034 #include "asterisk/_private.h"
00035 
00036 #include <pthread.h>
00037 #include <signal.h>
00038 #include <sys/time.h>
00039 #include <sys/signal.h>
00040 #include <netinet/in.h>
00041 
00042 #include "asterisk/lock.h"
00043 #include "asterisk/file.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/pbx.h"
00046 #include "asterisk/causes.h"
00047 #include "asterisk/module.h"
00048 #include "asterisk/translate.h"
00049 #include "asterisk/app.h"
00050 #include "asterisk/say.h"
00051 #include "asterisk/features.h"
00052 #include "asterisk/musiconhold.h"
00053 #include "asterisk/config.h"
00054 #include "asterisk/cli.h"
00055 #include "asterisk/manager.h"
00056 #include "asterisk/utils.h"
00057 #include "asterisk/adsi.h"
00058 #include "asterisk/devicestate.h"
00059 #include "asterisk/monitor.h"
00060 #include "asterisk/audiohook.h"
00061 #include "asterisk/global_datastores.h"
00062 #include "asterisk/astobj2.h"
00063 #include "asterisk/cel.h"
00064 #include "asterisk/test.h"
00065 
00066 /*
00067  * Party A - transferee
00068  * Party B - transferer
00069  * Party C - target of transfer
00070  *
00071  * DTMF attended transfer works within the channel bridge.
00072  * Unfortunately, when either party A or B in the channel bridge
00073  * hangs up, that channel is not completely hung up until the
00074  * transfer completes.  This is a real problem depending upon
00075  * the channel technology involved.
00076  *
00077  * For chan_dahdi, the channel is crippled until the hangup is
00078  * complete.  Either the channel is not useable (analog) or the
00079  * protocol disconnect messages are held up (PRI/BRI/SS7) and
00080  * the media is not released.
00081  *
00082  * For chan_sip, a call limit of one is going to block that
00083  * endpoint from any further calls until the hangup is complete.
00084  *
00085  * For party A this is a minor problem.  The party A channel
00086  * will only be in this condition while party B is dialing and
00087  * when party B and C are conferring.  The conversation between
00088  * party B and C is expected to be a short one.  Party B is
00089  * either asking a question of party C or announcing party A.
00090  * Also party A does not have much incentive to hangup at this
00091  * point.
00092  *
00093  * For party B this can be a major problem during a blonde
00094  * transfer.  (A blonde transfer is our term for an attended
00095  * transfer that is converted into a blind transfer. :))  Party
00096  * B could be the operator.  When party B hangs up, he assumes
00097  * that he is out of the original call entirely.  The party B
00098  * channel will be in this condition while party C is ringing,
00099  * while attempting to recall party B, and while waiting between
00100  * call attempts.
00101  *
00102  * WARNING:
00103  * The ATXFER_NULL_TECH conditional is a hack to fix the
00104  * problem.  It will replace the party B channel technology with
00105  * a NULL channel driver.  The consequences of this code is that
00106  * the 'h' extension will not be able to access any channel
00107  * technology specific information like SIP statistics for the
00108  * call.
00109  *
00110  * Uncomment the ATXFER_NULL_TECH define below to replace the
00111  * party B channel technology in the channel bridge to complete
00112  * hanging up the channel technology.
00113  */
00114 //#define ATXFER_NULL_TECH 1
00115 
00116 /*** DOCUMENTATION
00117    <application name="Bridge" language="en_US">
00118       <synopsis>
00119          Bridge two channels.
00120       </synopsis>
00121       <syntax>
00122          <parameter name="channel" required="true">
00123             <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
00124          </parameter>
00125          <parameter name="options">
00126             <optionlist>
00127                <option name="p">
00128                   <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
00129                </option>
00130                <option name="h">
00131                   <para>Allow the called party to hang up by sending the
00132                   <replaceable>*</replaceable> DTMF digit.</para>
00133                </option>
00134                <option name="H">
00135                   <para>Allow the calling party to hang up by pressing the
00136                   <replaceable>*</replaceable> DTMF digit.</para>
00137                </option>
00138                <option name="k">
00139                   <para>Allow the called party to enable parking of the call by sending
00140                   the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
00141                </option>
00142                <option name="K">
00143                   <para>Allow the calling party to enable parking of the call by sending
00144                    the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
00145                </option>
00146                <option name="L(x[:y][:z])">
00147                   <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
00148                   when <replaceable>y</replaceable> ms are left. Repeat the warning every
00149                   <replaceable>z</replaceable> ms. The following special variables can be
00150                   used with this option:</para>
00151                   <variablelist>
00152                      <variable name="LIMIT_PLAYAUDIO_CALLER">
00153                         <para>Play sounds to the caller. yes|no (default yes)</para>
00154                      </variable>
00155                      <variable name="LIMIT_PLAYAUDIO_CALLEE">   
00156                         <para>Play sounds to the callee. yes|no</para>
00157                      </variable>
00158                      <variable name="LIMIT_TIMEOUT_FILE">
00159                         <para>File to play when time is up.</para>
00160                      </variable>
00161                      <variable name="LIMIT_CONNECT_FILE">
00162                         <para>File to play when call begins.</para>
00163                      </variable>
00164                      <variable name="LIMIT_WARNING_FILE">
00165                         <para>File to play as warning if <replaceable>y</replaceable> is
00166                         defined. The default is to say the time remaining.</para>
00167                      </variable>
00168                   </variablelist>
00169                </option>
00170                <option name="S(x)">
00171                   <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
00172                </option>
00173                <option name="t">
00174                   <para>Allow the called party to transfer the calling party by sending the
00175                   DTMF sequence defined in <filename>features.conf</filename>.</para>
00176                </option>
00177                <option name="T">
00178                   <para>Allow the calling party to transfer the called party by sending the
00179                   DTMF sequence defined in <filename>features.conf</filename>.</para>
00180                </option>
00181                <option name="w">
00182                   <para>Allow the called party to enable recording of the call by sending
00183                   the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
00184                </option>
00185                <option name="W">
00186                   <para>Allow the calling party to enable recording of the call by sending
00187                   the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
00188                </option>
00189                <option name="x">
00190                   <para>Cause the called party to be hung up after the bridge, instead of being
00191                   restarted in the dialplan.</para>
00192                </option>
00193             </optionlist>
00194          </parameter>
00195       </syntax>
00196       <description>
00197          <para>Allows the ability to bridge two channels via the dialplan.</para>
00198          <para>This application sets the following channel variable upon completion:</para>
00199          <variablelist>
00200             <variable name="BRIDGERESULT">
00201                <para>The result of the bridge attempt as a text string.</para>
00202                <value name="SUCCESS" />
00203                <value name="FAILURE" />
00204                <value name="LOOP" />
00205                <value name="NONEXISTENT" />
00206                <value name="INCOMPATIBLE" />
00207             </variable>
00208          </variablelist>
00209       </description>
00210    </application>
00211    <application name="ParkedCall" language="en_US">
00212       <synopsis>
00213          Retrieve a parked call.
00214       </synopsis>
00215       <syntax>
00216          <parameter name="exten">
00217             <para>Parking space extension to retrieve a parked call.
00218             If not provided then the first available parked call in the
00219             parking lot will be retrieved.</para>
00220          </parameter>
00221          <parameter name="parking_lot_name">
00222             <para>Specify from which parking lot to retrieve a parked call.</para>
00223             <para>The parking lot used is selected in the following order:</para>
00224             <para>1) parking_lot_name option</para>
00225             <para>2) <variable>PARKINGLOT</variable> variable</para>
00226             <para>3) <literal>CHANNEL(parkinglot)</literal> function
00227             (Possibly preset by the channel driver.)</para>
00228             <para>4) Default parking lot.</para>
00229          </parameter>
00230       </syntax>
00231       <description>
00232          <para>Used to retrieve a parked call from a parking lot.</para>
00233          <note>
00234             <para>Parking lots automatically create and manage dialplan extensions in
00235             the parking lot context.  You do not need to explicitly use this
00236             application in your dialplan.  Instead, all you should do is include the
00237             parking lot context in your dialplan.</para>
00238          </note>
00239       </description>
00240       <see-also>
00241          <ref type="application">Park</ref>
00242          <ref type="application">ParkAndAnnounce</ref>
00243       </see-also>
00244    </application>
00245    <application name="Park" language="en_US">
00246       <synopsis>
00247          Park yourself.
00248       </synopsis>
00249       <syntax>
00250          <parameter name="timeout">
00251             <para>A custom parking timeout for this parked call. Value in milliseconds.</para>
00252          </parameter>
00253          <parameter name="return_context">
00254             <para>The context to return the call to after it times out.</para>
00255          </parameter>
00256          <parameter name="return_exten">
00257             <para>The extension to return the call to after it times out.</para>
00258          </parameter>
00259          <parameter name="return_priority">
00260             <para>The priority to return the call to after it times out.</para>
00261          </parameter>
00262          <parameter name="options">
00263             <para>A list of options for this parked call.</para>
00264             <optionlist>
00265                <option name="r">
00266                   <para>Send ringing instead of MOH to the parked call.</para>
00267                </option>
00268                <option name="R">
00269                   <para>Randomize the selection of a parking space.</para>
00270                </option>
00271                <option name="s">
00272                   <para>Silence announcement of the parking space number.</para>
00273                </option>
00274             </optionlist>
00275          </parameter>
00276          <parameter name="parking_lot_name">
00277             <para>Specify in which parking lot to park a call.</para>
00278             <para>The parking lot used is selected in the following order:</para>
00279             <para>1) parking_lot_name option</para>
00280             <para>2) <variable>PARKINGLOT</variable> variable</para>
00281             <para>3) <literal>CHANNEL(parkinglot)</literal> function
00282             (Possibly preset by the channel driver.)</para>
00283             <para>4) Default parking lot.</para>
00284          </parameter>
00285       </syntax>
00286       <description>
00287          <para>Used to park yourself (typically in combination with a supervised
00288          transfer to know the parking space).</para>
00289          <para>If you set the <variable>PARKINGEXTEN</variable> variable to a
00290          parking space extension in the parking lot, Park() will attempt to park the call
00291          on that extension.  If the extension is already is in use then execution
00292          will continue at the next priority.</para>
00293          <para>If the <literal>parkeddynamic</literal> option is enabled in <filename>features.conf</filename>
00294          the following variables can be used to dynamically create new parking lots.</para>
00295          <para>If you set the <variable>PARKINGDYNAMIC</variable> variable and this parking lot
00296          exists then it will be used as a template for the newly created dynamic lot.  Otherwise,
00297          the default parking lot will be used.</para>
00298          <para>If you set the <variable>PARKINGDYNCONTEXT</variable> variable then the newly created dynamic
00299          parking lot will use this context.</para>
00300          <para>If you set the <variable>PARKINGDYNEXTEN</variable> variable then the newly created dynamic
00301          parking lot will use this extension to access the parking lot.</para>
00302          <para>If you set the <variable>PARKINGDYNPOS</variable> variable then the newly created dynamic parking lot
00303          will use those parking postitions.</para>
00304          <note>
00305             <para>This application must be used as the first extension priority
00306             to be recognized as a parking access extension.  DTMF transfers
00307             and some channel drivers need this distinction to operate properly.
00308             The parking access extension in this case is treated like a dialplan
00309             hint.</para>
00310          </note>
00311          <note>
00312             <para>Parking lots automatically create and manage dialplan extensions in
00313             the parking lot context.  You do not need to explicitly use this
00314             application in your dialplan.  Instead, all you should do is include the
00315             parking lot context in your dialplan.</para>
00316          </note>
00317       </description>
00318       <see-also>
00319          <ref type="application">ParkAndAnnounce</ref>
00320          <ref type="application">ParkedCall</ref>
00321       </see-also>
00322    </application>
00323    <manager name="ParkedCalls" language="en_US">
00324       <synopsis>
00325          List parked calls.
00326       </synopsis>
00327       <syntax>
00328          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00329       </syntax>
00330       <description>
00331          <para>List parked calls.</para>
00332       </description>
00333    </manager>
00334    <manager name="Park" language="en_US">
00335       <synopsis>
00336          Park a channel.
00337       </synopsis>
00338       <syntax>
00339          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00340          <parameter name="Channel" required="true">
00341             <para>Channel name to park.</para>
00342          </parameter>
00343          <parameter name="Channel2" required="true">
00344             <para>Channel to return to if timeout.</para>
00345          </parameter>
00346          <parameter name="Timeout">
00347             <para>Number of milliseconds to wait before callback.</para>
00348          </parameter>
00349          <parameter name="Parkinglot">
00350             <para>Specify in which parking lot to park the channel.</para>
00351          </parameter>
00352       </syntax>
00353       <description>
00354          <para>Park a channel.</para>
00355       </description>
00356    </manager>
00357    <manager name="Bridge" language="en_US">
00358       <synopsis>
00359          Bridge two channels already in the PBX.
00360       </synopsis>
00361       <syntax>
00362          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00363          <parameter name="Channel1" required="true">
00364             <para>Channel to Bridge to Channel2.</para>
00365          </parameter>
00366          <parameter name="Channel2" required="true">
00367             <para>Channel to Bridge to Channel1.</para>
00368          </parameter>
00369          <parameter name="Tone">
00370             <para>Play courtesy tone to Channel 2.</para>
00371             <enumlist>
00372                <enum name="yes" />
00373                <enum name="no" />
00374             </enumlist>
00375          </parameter>
00376       </syntax>
00377       <description>
00378          <para>Bridge together two channels already in the PBX.</para>
00379       </description>
00380    </manager>
00381  ***/
00382 
00383 #define DEFAULT_PARK_TIME                    45000 /*!< ms */
00384 #define DEFAULT_PARK_EXTENSION                  "700"
00385 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT          3000  /*!< ms */
00386 #define DEFAULT_FEATURE_DIGIT_TIMEOUT           1000  /*!< ms */
00387 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000 /*!< ms */
00388 #define DEFAULT_ATXFER_DROP_CALL             0     /*!< Do not drop call. */
00389 #define DEFAULT_ATXFER_LOOP_DELAY               10000 /*!< ms */
00390 #define DEFAULT_ATXFER_CALLBACK_RETRIES            2
00391 
00392 #define AST_MAX_WATCHERS 256
00393 #define MAX_DIAL_FEATURE_OPTIONS 30
00394 
00395 struct feature_group_exten {
00396    AST_LIST_ENTRY(feature_group_exten) entry;
00397    AST_DECLARE_STRING_FIELDS(
00398       AST_STRING_FIELD(exten);
00399    );
00400    struct ast_call_feature *feature;
00401 };
00402 
00403 struct feature_group {
00404    AST_LIST_ENTRY(feature_group) entry;
00405    AST_DECLARE_STRING_FIELDS(
00406       AST_STRING_FIELD(gname);
00407    );
00408    AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
00409 };
00410 
00411 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
00412 
00413 typedef enum {
00414    FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
00415    FEATURE_INTERPRET_DO,     /* Used by feature_interpret */
00416    FEATURE_INTERPRET_CHECK,  /* Used by feature_check */
00417 } feature_interpret_op;
00418 
00419 static const char *parkedcall = "ParkedCall";
00420 
00421 static char pickup_ext[AST_MAX_EXTENSION];                 /*!< Call pickup extension */
00422 
00423 /*! Parking lot access ramp dialplan usage entry. */
00424 struct parking_dp_ramp {
00425    /*! Next node in the parking lot spaces dialplan list. */
00426    AST_LIST_ENTRY(parking_dp_ramp) node;
00427    /*! TRUE if the parking lot access extension is exclusive. */
00428    unsigned int exclusive:1;
00429    /*! Parking lot access extension */
00430    char exten[1];
00431 };
00432 
00433 /*! Parking lot dialplan access ramp map */
00434 AST_LIST_HEAD_NOLOCK(parking_dp_ramp_map, parking_dp_ramp);
00435 
00436 /*! Parking lot spaces dialplan usage entry. */
00437 struct parking_dp_spaces {
00438    /*! Next node in the parking lot spaces dialplan list. */
00439    AST_LIST_ENTRY(parking_dp_spaces) node;
00440    /*! First parking space */
00441    int start;
00442    /*! Last parking space */
00443    int stop;
00444 };
00445 
00446 /*! Parking lot dialplan context space map */
00447 AST_LIST_HEAD_NOLOCK(parking_dp_space_map, parking_dp_spaces);
00448 
00449 /*! Parking lot context dialplan usage entry. */
00450 struct parking_dp_context {
00451    /*! Next node in the parking lot contexts dialplan list. */
00452    AST_LIST_ENTRY(parking_dp_context) node;
00453    /*! Parking access extensions defined in this context. */
00454    struct parking_dp_ramp_map access_extens;
00455    /*! Parking spaces defined in this context. */
00456    struct parking_dp_space_map spaces;
00457    /*! Parking hints defined in this context. */
00458    struct parking_dp_space_map hints;
00459    /*! Parking lot context name */
00460    char context[1];
00461 };
00462 
00463 /*! Parking lot dialplan usage map. */
00464 AST_LIST_HEAD_NOLOCK(parking_dp_map, parking_dp_context);
00465 
00466 /*!
00467  * \brief Description of one parked call, added to a list while active, then removed.
00468  * The list belongs to a parkinglot.
00469  */
00470 struct parkeduser {
00471    struct ast_channel *chan;                   /*!< Parked channel */
00472    struct timeval start;                       /*!< Time the park started */
00473    int parkingnum;                             /*!< Parking lot space used */
00474    char parkingexten[AST_MAX_EXTENSION];       /*!< If set beforehand, parking extension used for this call */
00475    char context[AST_MAX_CONTEXT];              /*!< Where to go if our parking time expires */
00476    char exten[AST_MAX_EXTENSION];
00477    int priority;
00478    int parkingtime;                            /*!< Maximum length in parking lot before return */
00479    /*! Method to entertain the caller when parked: AST_CONTROL_RINGING, AST_CONTROL_HOLD, or 0(none) */
00480    enum ast_control_frame_type hold_method;
00481    unsigned int notquiteyet:1;
00482    unsigned int options_specified:1;
00483    char peername[AST_CHANNEL_NAME];
00484    unsigned char moh_trys;
00485    /*! Parking lot this entry belongs to.  Holds a parking lot reference. */
00486    struct ast_parkinglot *parkinglot;
00487    AST_LIST_ENTRY(parkeduser) list;
00488 };
00489 
00490 /*! Parking lot configuration options. */
00491 struct parkinglot_cfg {
00492    /*! Music class used for parking */
00493    char mohclass[MAX_MUSICCLASS];
00494    /*! Extension to park calls in this parking lot. */
00495    char parkext[AST_MAX_EXTENSION];
00496    /*! Context for which parking is made accessible */
00497    char parking_con[AST_MAX_EXTENSION];
00498    /*! First available extension for parking */
00499    int parking_start;
00500    /*! Last available extension for parking */
00501    int parking_stop;
00502    /*! Default parking time in ms. */
00503    int parkingtime;
00504    /*!
00505     * \brief Enable DTMF based transfers on bridge when picking up parked calls.
00506     *
00507     * \details
00508     * none(0)
00509     * AST_FEATURE_FLAG_BYCALLEE
00510     * AST_FEATURE_FLAG_BYCALLER
00511     * AST_FEATURE_FLAG_BYBOTH
00512     */
00513    int parkedcalltransfers;
00514    /*!
00515     * \brief Enable DTMF based parking on bridge when picking up parked calls.
00516     *
00517     * \details
00518     * none(0)
00519     * AST_FEATURE_FLAG_BYCALLEE
00520     * AST_FEATURE_FLAG_BYCALLER
00521     * AST_FEATURE_FLAG_BYBOTH
00522     */
00523    int parkedcallreparking;
00524    /*!
00525     * \brief Enable DTMF based hangup on a bridge when pickup up parked calls.
00526     *
00527     * \details
00528     * none(0)
00529     * AST_FEATURE_FLAG_BYCALLEE
00530     * AST_FEATURE_FLAG_BYCALLER
00531     * AST_FEATURE_FLAG_BYBOTH
00532     */
00533    int parkedcallhangup;
00534    /*!
00535     * \brief Enable DTMF based recording on a bridge when picking up parked calls.
00536     *
00537     * \details
00538     * none(0)
00539     * AST_FEATURE_FLAG_BYCALLEE
00540     * AST_FEATURE_FLAG_BYCALLER
00541     * AST_FEATURE_FLAG_BYBOTH
00542     */
00543    int parkedcallrecording;
00544 
00545    /*! TRUE if findslot is set to next */
00546    unsigned int parkfindnext:1;
00547    /*! TRUE if the parking lot is exclusively accessed by parkext */
00548    unsigned int parkext_exclusive:1;
00549    /*! Add parking hints automatically */
00550    unsigned int parkaddhints:1;
00551    /*! TRUE if configuration is invalid and the parking lot should not be used. */
00552    unsigned int is_invalid:1;
00553 };
00554 
00555 /*! \brief Structure for parking lots which are put in a container. */
00556 struct ast_parkinglot {
00557    /*! Name of the parking lot. */
00558    char name[AST_MAX_CONTEXT];
00559    /*! Parking lot user configuration. */
00560    struct parkinglot_cfg cfg;
00561 
00562    /*! Parking space to start next park search. */
00563    int next_parking_space;
00564 
00565    /*! That which bears the_mark shall be deleted if parking lot empty! (Used during reloads.) */
00566    unsigned int the_mark:1;
00567    /*! TRUE if the parking lot is disabled. */
00568    unsigned int disabled:1;
00569 
00570    /*! List of active parkings in this parkinglot */
00571    AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
00572 };
00573 
00574 /*! \brief The configured parking lots container. Always at least one  - the default parking lot */
00575 static struct ao2_container *parkinglots;
00576 
00577 /*!
00578  * \brief Default parking lot.
00579  * \note Holds a parkinglot reference.
00580  * \note Will not be NULL while running.
00581  */
00582 static struct ast_parkinglot *default_parkinglot;
00583 
00584 /*! Force a config reload to reload regardless of config file timestamp. */
00585 static int force_reload_load;
00586 
00587 static int parkedplay = 0;                                 /*!< Who to play courtesytone to when someone picks up a parked call. */
00588 static int parkeddynamic = 0;                              /*!< Enable creation of parkinglots dynamically */
00589 static char courtesytone[256];                             /*!< Courtesy tone used to pickup parked calls and on-touch-record */
00590 static char xfersound[256];                                /*!< Call transfer sound */
00591 static char xferfailsound[256];                            /*!< Call transfer failure sound */
00592 static char pickupsound[256];                              /*!< Pickup sound */
00593 static char pickupfailsound[256];                          /*!< Pickup failure sound */
00594 
00595 /*!
00596  * \brief Context for parking dialback to parker.
00597  * \note The need for the context is a KLUDGE.
00598  *
00599  * \todo Might be able to eliminate the parking_con_dial context
00600  * kludge by running app_dial directly in its own thread to
00601  * simulate a PBX.
00602  */
00603 static char parking_con_dial[] = "park-dial";
00604 
00605 /*! Ensure that features.conf reloads on one thread at a time. */
00606 AST_MUTEX_DEFINE_STATIC(features_reload_lock);
00607 
00608 static int adsipark;
00609 
00610 static int transferdigittimeout;
00611 static int featuredigittimeout;
00612 static int comebacktoorigin = 1;
00613 
00614 static int atxfernoanswertimeout;
00615 static unsigned int atxferdropcall;
00616 static unsigned int atxferloopdelay;
00617 static unsigned int atxfercallbackretries;
00618 
00619 static char *registrar = "features";         /*!< Registrar for operations */
00620 
00621 /*! PARK_APP_NAME application arguments */
00622 AST_DEFINE_APP_ARGS_TYPE(park_app_args,
00623    AST_APP_ARG(timeout);      /*!< Time in ms to remain in the parking lot. */
00624    AST_APP_ARG(return_con);   /*!< Context to return parked call if timeout. */
00625    AST_APP_ARG(return_ext);   /*!< Exten to return parked call if timeout. */
00626    AST_APP_ARG(return_pri);   /*!< Priority to return parked call if timeout. */
00627    AST_APP_ARG(options);      /*!< Parking option flags. */
00628    AST_APP_ARG(pl_name);      /*!< Parking lot name to use if present. */
00629    AST_APP_ARG(dummy);        /*!< Place to put any remaining args string. */
00630    );
00631 
00632 /* module and CLI command definitions */
00633 static const char *parkcall = "Park";
00634 
00635 static struct ast_app *monitor_app = NULL;
00636 static int monitor_ok = 1;
00637 
00638 static struct ast_app *mixmonitor_app = NULL;
00639 static int mixmonitor_ok = 1;
00640 
00641 static struct ast_app *stopmixmonitor_app = NULL;
00642 static int stopmixmonitor_ok = 1;
00643 
00644 static pthread_t parking_thread;
00645 struct ast_dial_features {
00646    struct ast_flags features_caller;
00647    struct ast_flags features_callee;
00648    int is_caller;
00649 };
00650 
00651 #if defined(ATXFER_NULL_TECH)
00652 /*!
00653  * \internal
00654  * \brief Set the channel technology to the kill technology.
00655  *
00656  * \param chan Channel to change technology.
00657  *
00658  * \return Nothing
00659  */
00660 static void set_kill_chan_tech(struct ast_channel *chan)
00661 {
00662    int idx;
00663 
00664    ast_channel_lock(chan);
00665 
00666    /* Hangup the channel's physical side */
00667    if (chan->tech->hangup) {
00668       chan->tech->hangup(chan);
00669    }
00670    if (chan->tech_pvt) {
00671       ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
00672          chan->name);
00673       ast_free(chan->tech_pvt);
00674       chan->tech_pvt = NULL;
00675    }
00676 
00677    /* Install the kill technology and wake up anyone waiting on it. */
00678    chan->tech = &ast_kill_tech;
00679    for (idx = 0; idx < AST_MAX_FDS; ++idx) {
00680       switch (idx) {
00681       case AST_ALERT_FD:
00682       case AST_TIMING_FD:
00683       case AST_GENERATOR_FD:
00684          /* Don't clear these fd's. */
00685          break;
00686       default:
00687          ast_channel_set_fd(chan, idx, -1);
00688          break;
00689       }
00690    }
00691    ast_queue_frame(chan, &ast_null_frame);
00692 
00693    ast_channel_unlock(chan);
00694 }
00695 #endif   /* defined(ATXFER_NULL_TECH) */
00696 
00697 #if defined(ATXFER_NULL_TECH)
00698 /*!
00699  * \internal
00700  * \brief Set the channel name to something unique.
00701  *
00702  * \param chan Channel to change name.
00703  *
00704  * \return Nothing
00705  */
00706 static void set_new_chan_name(struct ast_channel *chan)
00707 {
00708    static int seq_num_last;
00709    int seq_num;
00710    int len;
00711    char *chan_name;
00712    char dummy[1];
00713 
00714    /* Create the new channel name string. */
00715    ast_channel_lock(chan);
00716    seq_num = ast_atomic_fetchadd_int(&seq_num_last, +1);
00717    len = snprintf(dummy, sizeof(dummy), "%s<XFER_%x>", chan->name, seq_num) + 1;
00718    chan_name = alloca(len);
00719    snprintf(chan_name, len, "%s<XFER_%x>", chan->name, seq_num);
00720    ast_channel_unlock(chan);
00721 
00722    ast_change_name(chan, chan_name);
00723 }
00724 #endif   /* defined(ATXFER_NULL_TECH) */
00725 
00726 static void *dial_features_duplicate(void *data)
00727 {
00728    struct ast_dial_features *df = data, *df_copy;
00729  
00730    if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
00731       return NULL;
00732    }
00733  
00734    memcpy(df_copy, df, sizeof(*df));
00735  
00736    return df_copy;
00737 }
00738 
00739 static void dial_features_destroy(void *data)
00740 {
00741    struct ast_dial_features *df = data;
00742    if (df) {
00743       ast_free(df);
00744    }
00745 }
00746 
00747 static const struct ast_datastore_info dial_features_info = {
00748    .type = "dial-features",
00749    .destroy = dial_features_destroy,
00750    .duplicate = dial_features_duplicate,
00751 };
00752  
00753 /* Forward declarations */
00754 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
00755 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
00756 static struct ast_parkinglot *find_parkinglot(const char *name);
00757 static struct ast_parkinglot *create_parkinglot(const char *name);
00758 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
00759 static int parkinglot_activate(struct ast_parkinglot *parkinglot);
00760 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile);
00761 
00762 /*!
00763  * \internal
00764  * \brief Get the parking extension if it exists.
00765  *
00766  * \param exten_str Parking extension to see if exists.
00767  * \param chan Channel to autoservice while looking for exten.  (Could be NULL)
00768  * \param context Parking context to look in for exten.
00769  *
00770  * \retval exten on success.
00771  * \retval NULL on error or exten does not exist.
00772  */
00773 static struct ast_exten *get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
00774 {
00775    struct ast_exten *exten;
00776    struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
00777    const char *app_at_exten;
00778 
00779    ast_debug(4, "Checking if %s@%s is a parking exten\n", exten_str, context);
00780    exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
00781       E_MATCH);
00782    if (!exten) {
00783       return NULL;
00784    }
00785 
00786    app_at_exten = ast_get_extension_app(exten);
00787    if (!app_at_exten || strcasecmp(parkcall, app_at_exten)) {
00788       return NULL;
00789    }
00790 
00791    return exten;
00792 }
00793 
00794 int ast_parking_ext_valid(const char *exten_str, struct ast_channel *chan, const char *context)
00795 {
00796    return get_parking_exten(exten_str, chan, context) ? 1 : 0;
00797 }
00798 
00799 const char *ast_pickup_ext(void)
00800 {
00801    return pickup_ext;
00802 }
00803 
00804 struct ast_bridge_thread_obj 
00805 {
00806    struct ast_bridge_config bconfig;
00807    struct ast_channel *chan;
00808    struct ast_channel *peer;
00809    unsigned int return_to_pbx:1;
00810 };
00811 
00812 static int parkinglot_hash_cb(const void *obj, const int flags)
00813 {
00814    const struct ast_parkinglot *parkinglot = obj;
00815 
00816    return ast_str_case_hash(parkinglot->name);
00817 }
00818 
00819 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
00820 {
00821    struct ast_parkinglot *parkinglot = obj;
00822    struct ast_parkinglot *parkinglot2 = arg;
00823 
00824    return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
00825 }
00826 
00827 /*!
00828  * \brief store context, extension and priority 
00829  * \param chan, context, ext, pri
00830  */
00831 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
00832 {
00833    ast_copy_string(chan->context, context, sizeof(chan->context));
00834    ast_copy_string(chan->exten, ext, sizeof(chan->exten));
00835    chan->priority = pri;
00836 }
00837 
00838 /*!
00839  * \brief Check goto on transfer
00840  * \param chan
00841  *
00842  * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
00843  * When found make sure the types are compatible. Check if channel is valid
00844  * if so start the new channel else hangup the call. 
00845  */
00846 static void check_goto_on_transfer(struct ast_channel *chan) 
00847 {
00848    struct ast_channel *xferchan;
00849    const char *val;
00850    char *goto_on_transfer;
00851    char *x;
00852 
00853    ast_channel_lock(chan);
00854    val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
00855    if (ast_strlen_zero(val)) {
00856       ast_channel_unlock(chan);
00857       return;
00858    }
00859    goto_on_transfer = ast_strdupa(val);
00860    ast_channel_unlock(chan);
00861 
00862    ast_debug(1, "Attempting GOTO_ON_BLINDXFR=%s for %s.\n", val, chan->name);
00863 
00864    xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", chan->linkedid, 0,
00865       "%s", chan->name);
00866    if (!xferchan) {
00867       return;
00868    }
00869 
00870    /* Make formats okay */
00871    xferchan->readformat = chan->readformat;
00872    xferchan->writeformat = chan->writeformat;
00873 
00874    if (ast_channel_masquerade(xferchan, chan)) {
00875       /* Failed to setup masquerade. */
00876       ast_hangup(xferchan);
00877       return;
00878    }
00879 
00880    for (x = goto_on_transfer; *x; ++x) {
00881       if (*x == '^') {
00882          *x = ',';
00883       }
00884    }
00885    ast_parseable_goto(xferchan, goto_on_transfer);
00886    xferchan->_state = AST_STATE_UP;
00887    ast_clear_flag(xferchan, AST_FLAGS_ALL);  
00888    ast_channel_clear_softhangup(xferchan, AST_SOFTHANGUP_ALL);
00889 
00890    if (ast_do_masquerade(xferchan) || ast_pbx_start(xferchan)) {
00891       /* Failed to do masquerade or could not start PBX. */
00892       ast_hangup(xferchan);
00893    }
00894 }
00895 
00896 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
00897    const char *caller_name, struct ast_channel *requestor,
00898    struct ast_channel *transferee, const char *type, format_t format, void *data,
00899    int timeout, int *outstate, const char *language);
00900 
00901 /*!
00902  * \brief bridge the call 
00903  * \param data thread bridge.
00904  *
00905  * Set Last Data for respective channels, reset cdr for channels
00906  * bridge call, check if we're going back to dialplan
00907  * if not hangup both legs of the call
00908  */
00909 static void *bridge_call_thread(void *data)
00910 {
00911    struct ast_bridge_thread_obj *tobj = data;
00912    int res;
00913 
00914    tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00915    tobj->chan->data = tobj->peer->name;
00916    tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00917    tobj->peer->data = tobj->chan->name;
00918 
00919    ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
00920 
00921    if (tobj->return_to_pbx) {
00922       if (!ast_check_hangup(tobj->peer)) {
00923          ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
00924          res = ast_pbx_start(tobj->peer);
00925          if (res != AST_PBX_SUCCESS)
00926             ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
00927       } else
00928          ast_hangup(tobj->peer);
00929       if (!ast_check_hangup(tobj->chan)) {
00930          ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
00931          res = ast_pbx_start(tobj->chan);
00932          if (res != AST_PBX_SUCCESS)
00933             ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
00934       } else
00935          ast_hangup(tobj->chan);
00936    } else {
00937       ast_hangup(tobj->chan);
00938       ast_hangup(tobj->peer);
00939    }
00940 
00941    ast_free(tobj);
00942 
00943    return NULL;
00944 }
00945 
00946 /*!
00947  * \brief create thread for the parked call
00948  * \param data
00949  *
00950  * Create thread and attributes, call bridge_call_thread
00951  */
00952 static void bridge_call_thread_launch(void *data) 
00953 {
00954    pthread_t thread;
00955    pthread_attr_t attr;
00956    struct sched_param sched;
00957 
00958    pthread_attr_init(&attr);
00959    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
00960    ast_pthread_create(&thread, &attr, bridge_call_thread, data);
00961    pthread_attr_destroy(&attr);
00962    memset(&sched, 0, sizeof(sched));
00963    pthread_setschedparam(thread, SCHED_RR, &sched);
00964 }
00965 
00966 /*!
00967  * \brief Announce call parking by ADSI
00968  * \param chan .
00969  * \param parkingexten .
00970  * Create message to show for ADSI, display message.
00971  * \retval 0 on success.
00972  * \retval -1 on failure.
00973  */
00974 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
00975 {
00976    int res;
00977    int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
00978    char tmp[256];
00979    char *message[5] = {NULL, NULL, NULL, NULL, NULL};
00980 
00981    snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
00982    message[0] = tmp;
00983    res = ast_adsi_load_session(chan, NULL, 0, 1);
00984    if (res == -1)
00985       return res;
00986    return ast_adsi_print(chan, message, justify, 1);
00987 }
00988 
00989 /*!
00990  * \brief Find parking lot name from channel
00991  * \note Channel needs to be locked while the returned string is in use.
00992  */
00993 static const char *findparkinglotname(struct ast_channel *chan)
00994 {
00995    const char *name;
00996 
00997    /* The channel variable overrides everything */
00998    name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
00999    if (!name && !ast_strlen_zero(chan->parkinglot)) {
01000       /* Use the channel's parking lot. */
01001       name = chan->parkinglot;
01002    }
01003    return name;
01004 }
01005 
01006 /*! \brief Notify metermaids that we've changed an extension */
01007 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
01008 {
01009    ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'", 
01010       exten, context, ast_devstate2str(state));
01011 
01012    ast_devstate_changed(state, "park:%s@%s", exten, context);
01013 }
01014 
01015 /*! \brief metermaids callback from devicestate.c */
01016 static enum ast_device_state metermaidstate(const char *data)
01017 {
01018    char *context;
01019    char *exten;
01020 
01021    context = ast_strdupa(data);
01022 
01023    exten = strsep(&context, "@");
01024    if (!context)
01025       return AST_DEVICE_INVALID;
01026    
01027    ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
01028 
01029    if (!ast_exists_extension(NULL, context, exten, 1, NULL))
01030       return AST_DEVICE_NOT_INUSE;
01031 
01032    return AST_DEVICE_INUSE;
01033 }
01034 
01035 /*! Options to pass to park_call_full */
01036 enum ast_park_call_options {
01037    /*! Provide ringing to the parked caller instead of music on hold */
01038    AST_PARK_OPT_RINGING =   (1 << 0),
01039    /*! Randomly choose a parking spot for the caller instead of choosing
01040     *  the first one that is available. */
01041    AST_PARK_OPT_RANDOMIZE = (1 << 1),
01042    /*! Do not announce the parking number */
01043    AST_PARK_OPT_SILENCE = (1 << 2),
01044 };
01045 
01046 /*! Optional additional parking options when parking a call. */
01047 struct ast_park_call_args {
01048    /*! How long to wait in the parking lot before the call gets sent back
01049     *  to the specified return extension (or a best guess at where it came
01050     *  from if not explicitly specified). */
01051    int timeout;
01052    /*! An output parameter to store the parking space where the parked caller
01053     *  was placed. */
01054    int *extout;
01055    const char *orig_chan_name;
01056    const char *return_con;
01057    const char *return_ext;
01058    int return_pri;
01059    uint32_t flags;
01060    /*! Parked user that has already obtained a parking space */
01061    struct parkeduser *pu;
01062    /*! \brief Parkinglot to be parked in */
01063    struct ast_parkinglot *parkinglot;
01064 };
01065 
01066 /*!
01067  * \internal
01068  * \brief Create a dynamic parking lot.
01069  *
01070  * \param name Dynamic parking lot name to create.
01071  * \param chan Channel to get dynamic parking lot parameters.
01072  *
01073  * \retval parkinglot on success.
01074  * \retval NULL on error.
01075  */
01076 static struct ast_parkinglot *create_dynamic_parkinglot(const char *name, struct ast_channel *chan)
01077 {
01078    const char *dyn_context;
01079    const char *dyn_exten;
01080    const char *dyn_range;
01081    const char *template_name;
01082    struct ast_parkinglot *template_parkinglot = NULL;
01083    struct ast_parkinglot *parkinglot;
01084    int dyn_start;
01085    int dyn_end;
01086 
01087    ast_channel_lock(chan);
01088    template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
01089    dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
01090    dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
01091    dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
01092    ast_channel_unlock(chan);
01093 
01094    if (!ast_strlen_zero(template_name)) {
01095       template_parkinglot = find_parkinglot(template_name);
01096       if (!template_parkinglot) {
01097          ast_debug(1, "PARKINGDYNAMIC lot %s does not exist.\n",
01098             template_name);
01099       } else if (template_parkinglot->cfg.is_invalid) {
01100          ast_debug(1, "PARKINGDYNAMIC lot %s has invalid config.\n",
01101             template_name);
01102          parkinglot_unref(template_parkinglot);
01103          template_parkinglot = NULL;
01104       }
01105    }
01106    if (!template_parkinglot) {
01107       template_parkinglot = parkinglot_addref(default_parkinglot);
01108       ast_debug(1, "Using default parking lot for template\n");
01109    }
01110 
01111    parkinglot = copy_parkinglot(name, template_parkinglot);
01112    if (!parkinglot) {
01113       ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
01114    } else {
01115       /* Configure the dynamic parking lot. */
01116       if (!ast_strlen_zero(dyn_context)) {
01117          ast_copy_string(parkinglot->cfg.parking_con, dyn_context,
01118             sizeof(parkinglot->cfg.parking_con));
01119       }
01120       if (!ast_strlen_zero(dyn_exten)) {
01121          ast_copy_string(parkinglot->cfg.parkext, dyn_exten,
01122             sizeof(parkinglot->cfg.parkext));
01123       }
01124       if (!ast_strlen_zero(dyn_range)) {
01125          if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
01126             ast_log(LOG_WARNING,
01127                "Format for parking positions is a-b, where a and b are numbers\n");
01128          } else if (dyn_end < dyn_start || dyn_start <= 0 || dyn_end <= 0) {
01129             ast_log(LOG_WARNING,
01130                "Format for parking positions is a-b, where a <= b\n");
01131          } else {
01132             parkinglot->cfg.parking_start = dyn_start;
01133             parkinglot->cfg.parking_stop = dyn_end;
01134          }
01135       }
01136 
01137       /*
01138        * Sanity check for dynamic parking lot configuration.
01139        *
01140        * XXX It may be desirable to instead check if the dynamic
01141        * parking lot overlaps any existing lots like what is done for
01142        * a reload.
01143        */
01144       if (!strcmp(parkinglot->cfg.parking_con, template_parkinglot->cfg.parking_con)) {
01145          if (!strcmp(parkinglot->cfg.parkext, template_parkinglot->cfg.parkext)
01146             && parkinglot->cfg.parkext_exclusive) {
01147             ast_log(LOG_WARNING,
01148                "Parking lot '%s' conflicts with template parking lot '%s'!\n"
01149                "Change either PARKINGDYNCONTEXT or PARKINGDYNEXTEN.\n",
01150                parkinglot->name, template_parkinglot->name);
01151          }
01152          if ((template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_start
01153                && parkinglot->cfg.parking_start <= template_parkinglot->cfg.parking_stop)
01154             || (template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_stop
01155                && parkinglot->cfg.parking_stop <= template_parkinglot->cfg.parking_stop)
01156             || (parkinglot->cfg.parking_start < template_parkinglot->cfg.parking_start
01157                && template_parkinglot->cfg.parking_stop < parkinglot->cfg.parking_stop)) {
01158             ast_log(LOG_WARNING,
01159                "Parking lot '%s' parking spaces overlap template parking lot '%s'!\n"
01160                "Change PARKINGDYNPOS.\n",
01161                parkinglot->name, template_parkinglot->name);
01162          }
01163       }
01164 
01165       parkinglot_activate(parkinglot);
01166       ao2_link(parkinglots, parkinglot);
01167    }
01168    parkinglot_unref(template_parkinglot);
01169 
01170    return parkinglot;
01171 }
01172 
01173 /*!
01174  * \internal
01175  * \brief Abort parking a call that has not completed parking yet.
01176  *
01177  * \param pu Parked user item to clean up.
01178  *
01179  * \note The parking lot parkings list is locked on entry.
01180  *
01181  * \return Nothing
01182  */
01183 static void park_space_abort(struct parkeduser *pu)
01184 {
01185    struct ast_parkinglot *parkinglot;
01186 
01187    parkinglot = pu->parkinglot;
01188 
01189    /* Put back the parking space just allocated. */
01190    --parkinglot->next_parking_space;
01191 
01192    AST_LIST_REMOVE(&parkinglot->parkings, pu, list);
01193 
01194    AST_LIST_UNLOCK(&parkinglot->parkings);
01195    parkinglot_unref(parkinglot);
01196    ast_free(pu);
01197 }
01198 
01199 /*!
01200  * \internal
01201  * \brief Reserve a parking space in a parking lot for a call being parked.
01202  *
01203  * \param park_me Channel being parked.
01204  * \param parker Channel parking the call.
01205  * \param args Optional additional parking options when parking a call.
01206  *
01207  * \return Parked call descriptor or NULL if failed.
01208  * \note The parking lot list is locked if successful.
01209  */
01210 static struct parkeduser *park_space_reserve(struct ast_channel *park_me, struct ast_channel *parker, struct ast_park_call_args *args)
01211 {
01212    struct parkeduser *pu;
01213    int i;
01214    int parking_space = -1;
01215    const char *parkinglotname;
01216    const char *parkingexten;
01217    struct parkeduser *cur;
01218    struct ast_parkinglot *parkinglot = NULL;
01219 
01220    if (args->parkinglot) {
01221       parkinglot = parkinglot_addref(args->parkinglot);
01222       parkinglotname = parkinglot->name;
01223    } else {
01224       if (parker) {
01225          parkinglotname = findparkinglotname(parker);
01226       } else { /* parker was NULL, check park_me (ParkAndAnnounce / res_agi) */
01227          parkinglotname = findparkinglotname(park_me);
01228       }
01229       if (!ast_strlen_zero(parkinglotname)) {
01230          parkinglot = find_parkinglot(parkinglotname);
01231       } else {
01232          /* Parking lot is not specified, so use the default parking lot. */
01233          ast_debug(4, "This could be an indication channel driver needs updating, using default lot.\n");
01234          parkinglot = parkinglot_addref(default_parkinglot);
01235       }
01236    }
01237 
01238    /* Dynamically create parkinglot */
01239    if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
01240       parkinglot = create_dynamic_parkinglot(parkinglotname, park_me);
01241    }
01242 
01243    if (!parkinglot) {
01244       ast_log(LOG_WARNING, "Parking lot not available to park %s.\n", park_me->name);
01245       return NULL;
01246    }
01247 
01248    ast_debug(1, "Parking lot: %s\n", parkinglot->name);
01249    if (parkinglot->disabled || parkinglot->cfg.is_invalid) {
01250       ast_log(LOG_WARNING, "Parking lot %s is not in a useable state.\n",
01251          parkinglot->name);
01252       parkinglot_unref(parkinglot);
01253       return NULL;
01254    }
01255 
01256    /* Allocate memory for parking data */
01257    if (!(pu = ast_calloc(1, sizeof(*pu)))) {
01258       parkinglot_unref(parkinglot);
01259       return NULL;
01260    }
01261 
01262    /* Lock parking list */
01263    AST_LIST_LOCK(&parkinglot->parkings);
01264 
01265    /* Check for channel variable PARKINGEXTEN */
01266    parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(park_me, "PARKINGEXTEN"), ""));
01267    if (!ast_strlen_zero(parkingexten)) {
01268       /*!
01269        * \note The API forces us to specify a numeric parking slot, even
01270        * though the architecture would tend to support non-numeric extensions
01271        * (as are possible with SIP, for example).  Hence, we enforce that
01272        * limitation here.  If extout was not numeric, we could permit
01273        * arbitrary non-numeric extensions.
01274        */
01275       if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space <= 0) {
01276          ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n",
01277             parkingexten);
01278          AST_LIST_UNLOCK(&parkinglot->parkings);
01279          parkinglot_unref(parkinglot);
01280          ast_free(pu);
01281          return NULL;
01282       }
01283 
01284       if (parking_space < parkinglot->cfg.parking_start
01285          || parkinglot->cfg.parking_stop < parking_space) {
01286          /*
01287           * Cannot allow park because parking lots are not setup for
01288           * spaces outside of the lot.  (Things like dialplan hints don't
01289           * exist for outside lot space.)
01290           */
01291          ast_log(LOG_WARNING, "PARKINGEXTEN=%d is not in %s (%d-%d).\n",
01292             parking_space, parkinglot->name, parkinglot->cfg.parking_start,
01293             parkinglot->cfg.parking_stop);
01294          AST_LIST_UNLOCK(&parkinglot->parkings);
01295          parkinglot_unref(parkinglot);
01296          ast_free(pu);
01297          return NULL;
01298       }
01299 
01300       /* Check if requested parking space is in use. */
01301       AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
01302          if (cur->parkingnum == parking_space) {
01303             ast_log(LOG_WARNING, "PARKINGEXTEN=%d is already in use in %s\n",
01304                parking_space, parkinglot->name);
01305             AST_LIST_UNLOCK(&parkinglot->parkings);
01306             parkinglot_unref(parkinglot);
01307             ast_free(pu);
01308             return NULL;
01309          }
01310       }
01311    } else {
01312       /* PARKINGEXTEN is empty, so find a usable extension in the lot to park the call */
01313       int start; /* The first slot we look in the parkinglot. It can be randomized. */
01314       int start_checked = 0; /* flag raised once the first slot is checked */
01315 
01316       /* If using randomize mode, set start to random position on parking range */
01317       if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
01318          start = ast_random() % (parkinglot->cfg.parking_stop - parkinglot->cfg.parking_start + 1);
01319          start += parkinglot->cfg.parking_start;
01320       } else if (parkinglot->cfg.parkfindnext
01321          && parkinglot->cfg.parking_start <= parkinglot->next_parking_space
01322          && parkinglot->next_parking_space <= parkinglot->cfg.parking_stop) {
01323          /* Start looking with the next parking space in the lot. */
01324          start = parkinglot->next_parking_space;
01325       } else {
01326          /* Otherwise, just set it to the start position. */
01327          start = parkinglot->cfg.parking_start;
01328       }
01329 
01330       /* free parking extension linear search: O(n^2) */
01331       for (i = start; ; i++) {
01332          /* If we are past the end, wrap around to the first parking slot*/
01333          if (i == parkinglot->cfg.parking_stop + 1) {
01334             i = parkinglot->cfg.parking_start;
01335          }
01336 
01337          if (i == start) {
01338             /* At this point, if start_checked, we've exhausted all the possible slots. */
01339             if (start_checked) {
01340                break;
01341             } else {
01342                start_checked = 1;
01343             }
01344          }
01345 
01346          /* Search the list of parked calls already in use for i. If we find it, it's in use. */
01347          AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
01348             if (cur->parkingnum == i) {
01349                break;
01350             }
01351          }
01352          if (!cur) {
01353             /* We found a parking space. */
01354             parking_space = i;
01355             break;
01356          }
01357       }
01358       if (parking_space == -1) {
01359          /* We did not find a parking space.  Lot is full. */
01360          ast_log(LOG_WARNING, "No more parking spaces in %s\n", parkinglot->name);
01361          AST_LIST_UNLOCK(&parkinglot->parkings);
01362          parkinglot_unref(parkinglot);
01363          ast_free(pu);
01364          return NULL;
01365       }
01366    }
01367 
01368    /* Prepare for next parking space search. */
01369    parkinglot->next_parking_space = parking_space + 1;
01370 
01371    snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
01372    pu->notquiteyet = 1;
01373    pu->parkingnum = parking_space;
01374    pu->parkinglot = parkinglot;
01375    AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
01376 
01377    return pu;
01378 }
01379 
01380 /* Park a call */
01381 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
01382 {
01383    struct parkeduser *pu = args->pu;
01384    const char *event_from;    /*!< Channel name that is parking the call. */
01385    char app_data[AST_MAX_EXTENSION + AST_MAX_CONTEXT];
01386 
01387    if (pu == NULL) {
01388       args->pu = pu = park_space_reserve(chan, peer, args);
01389       if (pu == NULL) {
01390          return -1;
01391       }
01392    }
01393 
01394    chan->appl = "Parked Call";
01395    chan->data = NULL;
01396 
01397    pu->chan = chan;
01398 
01399    /* Put the parked channel on hold if we have two different channels */
01400    if (chan != peer) {
01401       if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
01402          pu->hold_method = AST_CONTROL_RINGING;
01403          ast_indicate(chan, AST_CONTROL_RINGING);
01404       } else {
01405          pu->hold_method = AST_CONTROL_HOLD;
01406          ast_indicate_data(chan, AST_CONTROL_HOLD,
01407             S_OR(pu->parkinglot->cfg.mohclass, NULL),
01408             !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
01409       }
01410    }
01411    
01412    pu->start = ast_tvnow();
01413    pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->cfg.parkingtime;
01414    if (args->extout)
01415       *(args->extout) = pu->parkingnum;
01416 
01417    if (peer) {
01418       event_from = S_OR(args->orig_chan_name, peer->name);
01419 
01420       /*
01421        * This is so ugly that it hurts, but implementing
01422        * get_base_channel() on local channels could have ugly side
01423        * effects.  We could have
01424        * transferer<->local,1<->local,2<->parking and we need the
01425        * callback name to be that of transferer.  Since local,1/2 have
01426        * the same name we can be tricky and just grab the bridged
01427        * channel from the other side of the local.
01428        */
01429       if (!strcasecmp(peer->tech->type, "Local")) {
01430          struct ast_channel *tmpchan, *base_peer;
01431          char other_side[AST_CHANNEL_NAME];
01432          char *c;
01433 
01434          ast_copy_string(other_side, event_from, sizeof(other_side));
01435          if ((c = strrchr(other_side, ';'))) {
01436             *++c = '1';
01437          }
01438          if ((tmpchan = ast_channel_get_by_name(other_side))) {
01439             ast_channel_lock(tmpchan);
01440             if ((base_peer = ast_bridged_channel(tmpchan))) {
01441                ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
01442             }
01443             ast_channel_unlock(tmpchan);
01444             tmpchan = ast_channel_unref(tmpchan);
01445          }
01446       } else {
01447          ast_copy_string(pu->peername, event_from, sizeof(pu->peername));
01448       }
01449    } else {
01450       event_from = S_OR(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"), chan->name);
01451    }
01452 
01453    /*
01454     * Remember what had been dialed, so that if the parking
01455     * expires, we try to come back to the same place
01456     */
01457    pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
01458 
01459    /*
01460     * If extension has options specified, they override all other
01461     * possibilities such as the returntoorigin flag and transferred
01462     * context.  Information on extension options is lost here, so
01463     * we set a flag
01464     */
01465    ast_copy_string(pu->context, 
01466       S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)), 
01467       sizeof(pu->context));
01468    ast_copy_string(pu->exten, 
01469       S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)), 
01470       sizeof(pu->exten));
01471    pu->priority = args->return_pri ? args->return_pri : 
01472       (chan->macropriority ? chan->macropriority : chan->priority);
01473 
01474    /*
01475     * If parking a channel directly, don't quite yet get parking
01476     * running on it.  All parking lot entries are put into the
01477     * parking lot with notquiteyet on.
01478     */
01479    if (peer != chan) {
01480       pu->notquiteyet = 0;
01481    }
01482 
01483    /* Wake up the (presumably select()ing) thread */
01484    pthread_kill(parking_thread, SIGURG);
01485    ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n",
01486       chan->name, pu->parkingnum, pu->parkinglot->name,
01487       pu->context, pu->exten, pu->priority, (pu->parkingtime / 1000));
01488 
01489    ast_cel_report_event(chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
01490 
01491    ast_manager_event(chan, EVENT_FLAG_CALL, "ParkedCall",
01492       "Exten: %s\r\n"
01493       "Channel: %s\r\n"
01494       "Parkinglot: %s\r\n"
01495       "From: %s\r\n"
01496       "Timeout: %ld\r\n"
01497       "CallerIDNum: %s\r\n"
01498       "CallerIDName: %s\r\n"
01499       "ConnectedLineNum: %s\r\n"
01500       "ConnectedLineName: %s\r\n"
01501       "Uniqueid: %s\r\n",
01502       pu->parkingexten, chan->name, pu->parkinglot->name, event_from,
01503       (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
01504       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, "<unknown>"),
01505       S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, "<unknown>"),
01506       S_COR(chan->connected.id.number.valid, chan->connected.id.number.str, "<unknown>"),
01507       S_COR(chan->connected.id.name.valid, chan->connected.id.name.str, "<unknown>"),
01508       chan->uniqueid
01509       );
01510    ast_debug(4, "peer->name: %s\n", peer ? peer->name : "-No peer-");
01511    ast_debug(4, "args->orig_chan_name: %s\n", args->orig_chan_name ? args->orig_chan_name : "-none-");
01512    ast_debug(4, "pu->peername: %s\n", pu->peername);
01513    ast_debug(4, "AMI ParkedCall Channel: %s\n", chan->name);
01514    ast_debug(4, "AMI ParkedCall From: %s\n", event_from);
01515 
01516    if (peer && adsipark && ast_adsi_available(peer)) {
01517       adsi_announce_park(peer, pu->parkingexten);  /* Only supports parking numbers */
01518       ast_adsi_unload_session(peer);
01519    }
01520 
01521    snprintf(app_data, sizeof(app_data), "%s,%s", pu->parkingexten,
01522       pu->parkinglot->name);
01523    if (ast_add_extension(pu->parkinglot->cfg.parking_con, 1, pu->parkingexten, 1,
01524       NULL, NULL, parkedcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
01525       ast_log(LOG_ERROR, "Could not create parked call exten: %s@%s\n",
01526          pu->parkingexten, pu->parkinglot->cfg.parking_con);
01527    } else {
01528       notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con, AST_DEVICE_INUSE);
01529    }
01530 
01531    AST_LIST_UNLOCK(&pu->parkinglot->parkings);
01532 
01533    /* Only say number if it's a number and the channel hasn't been masqueraded away */
01534    if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE)
01535       && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
01536       /*
01537        * If a channel is masqueraded into peer while playing back the
01538        * parking space number do not continue playing it back.  This
01539        * is the case if an attended transfer occurs.
01540        */
01541       ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
01542       /* Tell the peer channel the number of the parking space */
01543       ast_say_digits(peer, pu->parkingnum, "", peer->language);
01544       ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
01545    }
01546    if (peer == chan) { /* pu->notquiteyet = 1 */
01547       /* Wake up parking thread if we're really done */
01548       pu->hold_method = AST_CONTROL_HOLD;
01549       ast_indicate_data(chan, AST_CONTROL_HOLD,
01550          S_OR(pu->parkinglot->cfg.mohclass, NULL),
01551          !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
01552       pu->notquiteyet = 0;
01553       pthread_kill(parking_thread, SIGURG);
01554    }
01555    return 0;
01556 }
01557 
01558 int ast_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
01559 {
01560    int res;
01561    char *parse;
01562    const char *app_data;
01563    struct ast_exten *exten;
01564    struct park_app_args app_args;
01565    struct ast_park_call_args args = {
01566       .timeout = timeout,
01567       .extout = extout,
01568    };
01569 
01570    if (!park_exten || !park_context) {
01571       return park_call_full(park_me, parker, &args);
01572    }
01573 
01574    /*
01575     * Determiine if the specified park extension has an exclusive
01576     * parking lot to use.
01577     */
01578    if (parker && parker != park_me) {
01579       ast_autoservice_start(park_me);
01580    }
01581    exten = get_parking_exten(park_exten, parker, park_context);
01582    if (exten) {
01583       app_data = ast_get_extension_app_data(exten);
01584       if (!app_data) {
01585          app_data = "";
01586       }
01587       parse = ast_strdupa(app_data);
01588       AST_STANDARD_APP_ARGS(app_args, parse);
01589    
01590       if (!ast_strlen_zero(app_args.pl_name)) {
01591          /* Find the specified exclusive parking lot */
01592          args.parkinglot = find_parkinglot(app_args.pl_name);
01593          if (!args.parkinglot && parkeddynamic) {
01594             args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
01595          }
01596       }
01597    }
01598    if (parker && parker != park_me) {
01599       ast_autoservice_stop(park_me);
01600    }
01601 
01602    res = park_call_full(park_me, parker, &args);
01603    if (args.parkinglot) {
01604       parkinglot_unref(args.parkinglot);
01605    }
01606    return res;
01607 }
01608 
01609 int ast_park_call(struct ast_channel *park_me, struct ast_channel *parker, int timeout, const char *park_exten, int *extout)
01610 {
01611    struct ast_park_call_args args = {
01612       .timeout = timeout,
01613       .extout = extout,
01614    };
01615 
01616    return park_call_full(park_me, parker, &args);
01617 }
01618 
01619 /*!
01620  * \brief Park call via masqueraded channel and announce parking spot on peer channel.
01621  *
01622  * \param rchan the real channel to be parked
01623  * \param peer the channel to have the parking read to.
01624  * \param args Additional parking options when parking a call.
01625  *
01626  * \retval 0 on success.
01627  * \retval -1 on failure.
01628  */
01629 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
01630 {
01631    struct ast_channel *chan;
01632 
01633    /* Make a new, channel that we'll use to masquerade in the real one */
01634    chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten,
01635       rchan->context, rchan->linkedid, rchan->amaflags, "Parked/%s", rchan->name);
01636    if (!chan) {
01637       ast_log(LOG_WARNING, "Unable to create parked channel\n");
01638       if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
01639          if (peer == rchan) {
01640             /* Only have one channel to worry about. */
01641             ast_stream_and_wait(peer, "pbx-parkingfailed", "");
01642          } else if (peer) {
01643             /* Have two different channels to worry about. */
01644             play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
01645          }
01646       }
01647       return -1;
01648    }
01649 
01650    args->pu = park_space_reserve(rchan, peer, args);
01651    if (!args->pu) {
01652       ast_hangup(chan);
01653       if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
01654          if (peer == rchan) {
01655             /* Only have one channel to worry about. */
01656             ast_stream_and_wait(peer, "pbx-parkingfailed", "");
01657          } else if (peer) {
01658             /* Have two different channels to worry about. */
01659             play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
01660          }
01661       }
01662       return -1;
01663    }
01664 
01665    /* Make formats okay */
01666    chan->readformat = rchan->readformat;
01667    chan->writeformat = rchan->writeformat;
01668 
01669    if (ast_channel_masquerade(chan, rchan)) {
01670       park_space_abort(args->pu);
01671       args->pu = NULL;
01672       ast_hangup(chan);
01673       if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
01674          if (peer == rchan) {
01675             /* Only have one channel to worry about. */
01676             ast_stream_and_wait(peer, "pbx-parkingfailed", "");
01677          } else if (peer) {
01678             /* Have two different channels to worry about. */
01679             play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
01680          }
01681       }
01682       return -1;
01683    }
01684 
01685    /* Setup the extensions and such */
01686    set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
01687 
01688    /* Setup the macro extension and such */
01689    ast_copy_string(chan->macrocontext,rchan->macrocontext,sizeof(chan->macrocontext));
01690    ast_copy_string(chan->macroexten,rchan->macroexten,sizeof(chan->macroexten));
01691    chan->macropriority = rchan->macropriority;
01692 
01693    /* Manually do the masquerade to make sure it is complete. */
01694    ast_do_masquerade(chan);
01695 
01696    if (peer == rchan) {
01697       peer = chan;
01698    }
01699 
01700    /* parking space reserved, return code check unnecessary */
01701    park_call_full(chan, peer, args);
01702 
01703    return 0;
01704 }
01705 
01706 int ast_masq_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
01707 {
01708    int res;
01709    char *parse;
01710    const char *app_data;
01711    struct ast_exten *exten;
01712    struct park_app_args app_args;
01713    struct ast_park_call_args args = {
01714       .timeout = timeout,
01715       .extout = extout,
01716    };
01717 
01718    if (parker) {
01719       args.orig_chan_name = ast_strdupa(parker->name);
01720    }
01721    if (!park_exten || !park_context) {
01722       return masq_park_call(park_me, parker, &args);
01723    }
01724 
01725    /*
01726     * Determiine if the specified park extension has an exclusive
01727     * parking lot to use.
01728     */
01729    if (parker && parker != park_me) {
01730       ast_autoservice_start(park_me);
01731    }
01732    exten = get_parking_exten(park_exten, parker, park_context);
01733    if (exten) {
01734       app_data = ast_get_extension_app_data(exten);
01735       if (!app_data) {
01736          app_data = "";
01737       }
01738       parse = ast_strdupa(app_data);
01739       AST_STANDARD_APP_ARGS(app_args, parse);
01740    
01741       if (!ast_strlen_zero(app_args.pl_name)) {
01742          /* Find the specified exclusive parking lot */
01743          args.parkinglot = find_parkinglot(app_args.pl_name);
01744          if (!args.parkinglot && parkeddynamic) {
01745             args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
01746          }
01747       }
01748    }
01749    if (parker && parker != park_me) {
01750       ast_autoservice_stop(park_me);
01751    }
01752 
01753    res = masq_park_call(park_me, parker, &args);
01754    if (args.parkinglot) {
01755       parkinglot_unref(args.parkinglot);
01756    }
01757    return res;
01758 }
01759 
01760 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
01761 {
01762    struct ast_park_call_args args = {
01763       .timeout = timeout,
01764       .extout = extout,
01765    };
01766 
01767    if (peer) {
01768       args.orig_chan_name = ast_strdupa(peer->name);
01769    }
01770    return masq_park_call(rchan, peer, &args);
01771 }
01772 
01773 static int finishup(struct ast_channel *chan)
01774 {
01775    ast_indicate(chan, AST_CONTROL_UNHOLD);
01776 
01777    return ast_autoservice_stop(chan);
01778 }
01779 
01780 /*!
01781  * \internal
01782  * \brief Builtin transfer park call helper.
01783  *
01784  * \param park_me Channel to be parked.
01785  * \param parker Channel parking the call.
01786  * \param park_exten Parking lot dialplan access ramp extension.
01787  *
01788  * \note Assumes park_me is on hold and in autoservice.
01789  *
01790  * \retval -1 on successful park.
01791  * \retval -1 on park_me hangup.
01792  * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
01793  */
01794 static int xfer_park_call_helper(struct ast_channel *park_me, struct ast_channel *parker, struct ast_exten *park_exten)
01795 {
01796    char *parse;
01797    const char *app_data;
01798    const char *pl_name;
01799    struct ast_park_call_args args = { 0, };
01800    struct park_app_args app_args;
01801    int res;
01802 
01803    app_data = ast_get_extension_app_data(park_exten);
01804    if (!app_data) {
01805       app_data = "";
01806    }
01807    parse = ast_strdupa(app_data);
01808    AST_STANDARD_APP_ARGS(app_args, parse);
01809 
01810    /* Find the parking lot */
01811    if (!ast_strlen_zero(app_args.pl_name)) {
01812       pl_name = app_args.pl_name;
01813    } else {
01814       pl_name = findparkinglotname(parker);
01815    }
01816    if (ast_strlen_zero(pl_name)) {
01817       /* Parking lot is not specified, so use the default parking lot. */
01818       args.parkinglot = parkinglot_addref(default_parkinglot);
01819    } else {
01820       args.parkinglot = find_parkinglot(pl_name);
01821       if (!args.parkinglot && parkeddynamic) {
01822          args.parkinglot = create_dynamic_parkinglot(pl_name, park_me);
01823       }
01824    }
01825 
01826    if (args.parkinglot) {
01827       /* Park the call */
01828       res = finishup(park_me);
01829       if (res) {
01830          /* park_me hungup on us. */
01831          parkinglot_unref(args.parkinglot);
01832          return -1;
01833       }
01834       res = masq_park_call(park_me, parker, &args);
01835       parkinglot_unref(args.parkinglot);
01836    } else {
01837       /* Parking failed because parking lot does not exist. */
01838       if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
01839          ast_stream_and_wait(parker, "pbx-parkingfailed", "");
01840       }
01841       finishup(park_me);
01842       res = -1;
01843    }
01844 
01845    return res ? AST_FEATURE_RETURN_SUCCESS : -1;
01846 }
01847 
01848 /*!
01849  * \brief set caller and callee according to the direction
01850  * \param caller, callee, peer, chan, sense
01851  *
01852  * Detect who triggered feature and set callee/caller variables accordingly
01853  */
01854 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
01855    struct ast_channel *peer, struct ast_channel *chan, int sense)
01856 {
01857    if (sense == FEATURE_SENSE_PEER) {
01858       *caller = peer;
01859       *callee = chan;
01860    } else {
01861       *callee = peer;
01862       *caller = chan;
01863    }
01864 }
01865 
01866 /*!
01867  * \brief support routing for one touch call parking
01868  * \param chan channel parking call
01869  * \param peer channel to be parked
01870  * \param config unsed
01871  * \param code unused
01872  * \param sense feature options
01873  * \param data unused
01874  *
01875  * \retval -1 on successful park.
01876  * \retval -1 on chan hangup.
01877  * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
01878  */
01879 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
01880 {
01881    struct ast_channel *parker;
01882    struct ast_channel *parkee;
01883    struct ast_park_call_args args = { 0, };
01884 
01885    /*
01886     * We used to set chan's exten and priority to "s" and 1 here,
01887     * but this generates (in some cases) an invalid extension, and
01888     * if "s" exists, could errantly cause execution of extensions
01889     * you don't expect.  It makes more sense to let nature take its
01890     * course when chan finishes, and let the pbx do its thing and
01891     * hang up when the park is over.
01892     */
01893 
01894    /* Answer if call is not up */
01895    if (chan->_state != AST_STATE_UP) {
01896       /*
01897        * XXX Why are we doing this?  Both of the channels should be up
01898        * since you cannot do DTMF features unless you are bridged.
01899        */
01900       if (ast_answer(chan)) {
01901          return -1;
01902       }
01903 
01904       /* Sleep to allow VoIP streams to settle down */
01905       if (ast_safe_sleep(chan, 1000)) {
01906          return -1;
01907       }
01908    }
01909 
01910    /* one direction used to call park_call.... */
01911    set_peers(&parker, &parkee, peer, chan, sense);
01912    return masq_park_call(parkee, parker, &args) ? AST_FEATURE_RETURN_SUCCESS : -1;
01913 }
01914 
01915 /*!
01916  * \internal
01917  * \brief Play file to specified channel.
01918  *
01919  * \param play_to Channel to play audiofile to.
01920  * \param other Channel to put in autoservice while playing file.
01921  * \param msg Descriptive name of message type being played.
01922  * \param audiofile Audio file to play.
01923  *
01924  * \retval 0 on success.
01925  * \retval -1 on error. (Couldn't play file, a channel hung up,...)
01926  */
01927 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile)
01928 {
01929    /* Put other channel in autoservice. */
01930    if (ast_autoservice_start(other)) {
01931       return -1;
01932    }
01933    ast_autoservice_ignore(other, AST_FRAME_DTMF_BEGIN);
01934    ast_autoservice_ignore(other, AST_FRAME_DTMF_END);
01935    if (ast_stream_and_wait(play_to, audiofile, "")) {
01936       ast_log(LOG_WARNING, "Failed to play %s '%s'!\n", msg, audiofile);
01937       ast_autoservice_stop(other);
01938       return -1;
01939    }
01940    if (ast_autoservice_stop(other)) {
01941       return -1;
01942    }
01943    return 0;
01944 }
01945 
01946 /*!
01947  * \internal
01948  * \brief Play file to specified channels.
01949  *
01950  * \param left Channel on left to play file.
01951  * \param right Channel on right to play file.
01952  * \param which Play file on indicated channels: which < 0 play left, which == 0 play both, which > 0 play right
01953  * \param msg Descriptive name of message type being played.
01954  * \param audiofile Audio file to play to channels.
01955  *
01956  * \note Plays file to the indicated channels in turn so please
01957  * don't use this for very long messages.
01958  *
01959  * \retval 0 on success.
01960  * \retval -1 on error. (Couldn't play file, channel hung up,...)
01961  */
01962 static int play_message_to_chans(struct ast_channel *left, struct ast_channel *right, int which, const char *msg, const char *audiofile)
01963 {
01964    /* First play the file to the left channel if requested. */
01965    if (which <= 0 && play_message_on_chan(left, right, msg, audiofile)) {
01966       return -1;
01967    }
01968 
01969    /* Then play the file to the right channel if requested. */
01970    if (which >= 0 && play_message_on_chan(right, left, msg, audiofile)) {
01971       return -1;
01972    }
01973 
01974    return 0;
01975 }
01976 
01977 /*!
01978  * \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
01979  * other channel during the message, so please don't use this for very long messages
01980  */
01981 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
01982 {
01983    return play_message_to_chans(caller_chan, callee_chan, 0, "automon message",
01984       audiofile);
01985 }
01986 
01987 /*!
01988  * \brief Monitor a channel by DTMF
01989  * \param chan channel requesting monitor
01990  * \param peer channel to be monitored
01991  * \param config
01992  * \param code
01993  * \param sense feature options
01994  *
01995  * \param data
01996  * Check monitor app enabled, setup channels, both caller/callee chans not null
01997  * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
01998  * \retval AST_FEATURE_RETURN_SUCCESS on success.
01999  * \retval -1 on error.
02000  */
02001 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02002 {
02003    char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
02004    int x = 0;
02005    size_t len;
02006    struct ast_channel *caller_chan, *callee_chan;
02007    const char *automon_message_start = NULL;
02008    const char *automon_message_stop = NULL;
02009 
02010    if (!monitor_ok) {
02011       ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
02012       return -1;
02013    }
02014 
02015    if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
02016       monitor_ok = 0;
02017       ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
02018       return -1;
02019    }
02020 
02021    set_peers(&caller_chan, &callee_chan, peer, chan, sense);
02022    if (caller_chan) {   /* Find extra messages */
02023       automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
02024       automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
02025    }
02026 
02027    if (!ast_strlen_zero(courtesytone)) {  /* Play courtesy tone if configured */
02028       if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
02029          return -1;
02030       }
02031    }
02032    
02033    if (callee_chan->monitor) {
02034       ast_verb(4, "User hit '%s' to stop recording call.\n", code);
02035       if (!ast_strlen_zero(automon_message_stop)) {
02036          play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
02037       }
02038       callee_chan->monitor->stop(callee_chan, 1);
02039       return AST_FEATURE_RETURN_SUCCESS;
02040    }
02041 
02042    if (caller_chan && callee_chan) {
02043       const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
02044       const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
02045       const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
02046 
02047       if (!touch_format)
02048          touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
02049 
02050       if (!touch_monitor)
02051          touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
02052    
02053       if (!touch_monitor_prefix)
02054          touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
02055    
02056       if (touch_monitor) {
02057          len = strlen(touch_monitor) + 50;
02058          args = alloca(len);
02059          touch_filename = alloca(len);
02060          snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
02061          snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
02062       } else {
02063          caller_chan_id = ast_strdupa(S_COR(caller_chan->caller.id.number.valid,
02064             caller_chan->caller.id.number.str, caller_chan->name));
02065          callee_chan_id = ast_strdupa(S_COR(callee_chan->caller.id.number.valid,
02066             callee_chan->caller.id.number.str, callee_chan->name));
02067          len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
02068          args = alloca(len);
02069          touch_filename = alloca(len);
02070          snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
02071          snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
02072       }
02073 
02074       for(x = 0; x < strlen(args); x++) {
02075          if (args[x] == '/')
02076             args[x] = '-';
02077       }
02078       
02079       ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
02080 
02081       pbx_exec(callee_chan, monitor_app, args);
02082       pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
02083       pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
02084 
02085       if (!ast_strlen_zero(automon_message_start)) {  /* Play start message for both channels */
02086          play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
02087       }
02088    
02089       return AST_FEATURE_RETURN_SUCCESS;
02090    }
02091    
02092    ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");  
02093    return -1;
02094 }
02095 
02096 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02097 {
02098    char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
02099    int x = 0;
02100    size_t len;
02101    struct ast_channel *caller_chan, *callee_chan;
02102    const char *mixmonitor_spy_type = "MixMonitor";
02103    int count = 0;
02104 
02105    if (!mixmonitor_ok) {
02106       ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
02107       return -1;
02108    }
02109 
02110    if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
02111       mixmonitor_ok = 0;
02112       ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
02113       return -1;
02114    }
02115 
02116    set_peers(&caller_chan, &callee_chan, peer, chan, sense);
02117 
02118    if (!ast_strlen_zero(courtesytone)) {
02119       if (ast_autoservice_start(callee_chan))
02120          return -1;
02121       ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
02122       if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
02123          ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
02124          ast_autoservice_stop(callee_chan);
02125          return -1;
02126       }
02127       if (ast_autoservice_stop(callee_chan))
02128          return -1;
02129    }
02130 
02131    ast_channel_lock(callee_chan);
02132    count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
02133    ast_channel_unlock(callee_chan);
02134 
02135    /* This means a mixmonitor is attached to the channel, running or not is unknown. */
02136    if (count > 0) {
02137       
02138       ast_verb(3, "User hit '%s' to stop recording call.\n", code);
02139 
02140       /* Make sure they are running */
02141       ast_channel_lock(callee_chan);
02142       count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
02143       ast_channel_unlock(callee_chan);
02144       if (count > 0) {
02145          if (!stopmixmonitor_ok) {
02146             ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
02147             return -1;
02148          }
02149          if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
02150             stopmixmonitor_ok = 0;
02151             ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
02152             return -1;
02153          } else {
02154             pbx_exec(callee_chan, stopmixmonitor_app, "");
02155             return AST_FEATURE_RETURN_SUCCESS;
02156          }
02157       }
02158       
02159       ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n"); 
02160    }        
02161 
02162    if (caller_chan && callee_chan) {
02163       const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
02164       const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
02165 
02166       if (!touch_format)
02167          touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
02168 
02169       if (!touch_monitor)
02170          touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
02171 
02172       if (touch_monitor) {
02173          len = strlen(touch_monitor) + 50;
02174          args = alloca(len);
02175          touch_filename = alloca(len);
02176          snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
02177          snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
02178       } else {
02179          caller_chan_id = ast_strdupa(S_COR(caller_chan->caller.id.number.valid,
02180             caller_chan->caller.id.number.str, caller_chan->name));
02181          callee_chan_id = ast_strdupa(S_COR(callee_chan->caller.id.number.valid,
02182             callee_chan->caller.id.number.str, callee_chan->name));
02183          len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
02184          args = alloca(len);
02185          touch_filename = alloca(len);
02186          snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
02187          snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
02188       }
02189 
02190       for( x = 0; x < strlen(args); x++) {
02191          if (args[x] == '/')
02192             args[x] = '-';
02193       }
02194 
02195       ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
02196 
02197       pbx_exec(callee_chan, mixmonitor_app, args);
02198       pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
02199       pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
02200       return AST_FEATURE_RETURN_SUCCESS;
02201    
02202    }
02203 
02204    ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
02205    return -1;
02206 
02207 }
02208 
02209 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02210 {
02211    ast_verb(4, "User hit '%s' to disconnect call.\n", code);
02212    return AST_FEATURE_RETURN_HANGUP;
02213 }
02214 
02215 /*!
02216  * \brief Find the context for the transfer
02217  * \param transferer
02218  * \param transferee
02219  * 
02220  * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
02221  * \return a context string
02222  */
02223 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
02224 {
02225    const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
02226    if (ast_strlen_zero(s)) {
02227       s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
02228    }
02229    if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
02230       s = transferer->macrocontext;
02231    }
02232    if (ast_strlen_zero(s)) {
02233       s = transferer->context;
02234    }
02235    return s;  
02236 }
02237 
02238 /*!
02239  * \brief Blind transfer user to another extension
02240  * \param chan channel to be transfered
02241  * \param peer channel initiated blind transfer
02242  * \param config
02243  * \param code
02244  * \param data
02245  * \param sense  feature options
02246  * 
02247  * Place chan on hold, check if transferred to parkinglot extension,
02248  * otherwise check extension exists and transfer caller.
02249  * \retval AST_FEATURE_RETURN_SUCCESS.
02250  * \retval -1 on failure.
02251  */
02252 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02253 {
02254    struct ast_channel *transferer;
02255    struct ast_channel *transferee;
02256    struct ast_exten *park_exten;
02257    const char *transferer_real_context;
02258    char xferto[256] = "";
02259    int res;
02260 
02261    ast_debug(1, "Executing Blind Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
02262    set_peers(&transferer, &transferee, peer, chan, sense);
02263    transferer_real_context = real_ctx(transferer, transferee);
02264 
02265    /* Start autoservice on transferee while we talk to the transferer */
02266    ast_autoservice_start(transferee);
02267    ast_indicate(transferee, AST_CONTROL_HOLD);
02268 
02269    /* Transfer */
02270    res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
02271    if (res < 0) {
02272       finishup(transferee);
02273       return -1; /* error ? */
02274    }
02275    if (res > 0) { /* If they've typed a digit already, handle it */
02276       xferto[0] = (char) res;
02277    }
02278 
02279    res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
02280    if (res < 0) {  /* hangup or error, (would be 0 for invalid and 1 for valid) */
02281       finishup(transferee);
02282       return -1;
02283    }
02284    if (res == 0) {
02285       if (xferto[0]) {
02286          ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
02287             xferto, transferer_real_context);
02288       } else {
02289          /* Does anyone care about this case? */
02290          ast_log(LOG_WARNING, "No digits dialed.\n");
02291       }
02292       ast_stream_and_wait(transferer, "pbx-invalid", "");
02293       finishup(transferee);
02294       return AST_FEATURE_RETURN_SUCCESS;
02295    }
02296 
02297    park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
02298    if (park_exten) {
02299       /* We are transfering the transferee to a parking lot. */
02300       return xfer_park_call_helper(transferee, transferer, park_exten);
02301    }
02302 
02303    /* Do blind transfer. */
02304    ast_verb(3, "Blind transferring %s to '%s' (context %s) priority 1\n",
02305       transferee->name, xferto, transferer_real_context);
02306    ast_cel_report_event(transferer, AST_CEL_BLINDTRANSFER, NULL, xferto, transferee);
02307    pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
02308    pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
02309    finishup(transferee);
02310    ast_channel_lock(transferer);
02311    if (!transferer->cdr) { /* this code should never get called (in a perfect world) */
02312       transferer->cdr = ast_cdr_alloc();
02313       if (transferer->cdr) {
02314          ast_cdr_init(transferer->cdr, transferer); /* initialize our channel's cdr */
02315          ast_cdr_start(transferer->cdr);
02316       }
02317    }
02318    ast_channel_unlock(transferer);
02319    if (transferer->cdr) {
02320       struct ast_cdr *swap = transferer->cdr;
02321 
02322       ast_debug(1,
02323          "transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
02324          transferer->name, transferee->name, transferer->cdr->lastapp,
02325          transferer->cdr->lastdata, transferer->cdr->channel,
02326          transferer->cdr->dstchannel);
02327       ast_debug(1, "TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
02328          transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel,
02329          transferee->cdr->dstchannel);
02330       ast_debug(1, "transferer_real_context=%s; xferto=%s\n",
02331          transferer_real_context, xferto);
02332       /* swap cdrs-- it will save us some time & work */
02333       transferer->cdr = transferee->cdr;
02334       transferee->cdr = swap;
02335    }
02336    if (!transferee->pbx) {
02337       /* Doh!  Use our handy async_goto functions */
02338       ast_debug(1, "About to ast_async_goto %s.\n", transferee->name);
02339       if (ast_async_goto(transferee, transferer_real_context, xferto, 1)) {
02340          ast_log(LOG_WARNING, "Async goto failed :-(\n");
02341       }
02342 
02343       /* The transferee is masqueraded and the original bridged channels can be hungup. */
02344       res = -1;
02345    } else {
02346       /* Set the transferee's new extension, since it exists, using transferer context */
02347       ast_debug(1, "About to explicit goto %s, it has a PBX.\n", transferee->name);
02348       ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
02349       set_c_e_p(transferee, transferer_real_context, xferto, 0);
02350 
02351       /*
02352        * Break the bridge.  The transferee needs to resume executing
02353        * dialplan at the xferto location.
02354        */
02355       res = AST_FEATURE_RETURN_SUCCESSBREAK;
02356    }
02357    check_goto_on_transfer(transferer);
02358    return res;
02359 }
02360 
02361 /*!
02362  * \brief make channels compatible
02363  * \param c
02364  * \param newchan
02365  * \retval 0 on success.
02366  * \retval -1 on failure.
02367  */
02368 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
02369 {
02370    if (ast_channel_make_compatible(c, newchan) < 0) {
02371       ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
02372          c->name, newchan->name);
02373       ast_hangup(newchan);
02374       return -1;
02375    }
02376    return 0;
02377 }
02378 
02379 /*!
02380  * \internal
02381  * \brief Builtin attended transfer failed cleanup.
02382  * \since 1.10
02383  *
02384  * \param transferee Party A in the transfer.
02385  * \param transferer Party B in the transfer.
02386  * \param connected_line Saved connected line info about party A.
02387  *
02388  * \note The connected_line data is freed.
02389  *
02390  * \return Nothing
02391  */
02392 static void atxfer_fail_cleanup(struct ast_channel *transferee, struct ast_channel *transferer, struct ast_party_connected_line *connected_line)
02393 {
02394    finishup(transferee);
02395 
02396    /*
02397     * Restore party B connected line info about party A.
02398     *
02399     * Party B was the caller to party C and is the last known mode
02400     * for party B.
02401     */
02402    if (ast_channel_connected_line_macro(transferee, transferer, connected_line, 1, 0)) {
02403       ast_channel_update_connected_line(transferer, connected_line, NULL);
02404    }
02405    ast_party_connected_line_free(connected_line);
02406 }
02407 
02408 /*!
02409  * \brief Attended transfer
02410  * \param chan transfered user
02411  * \param peer person transfering call
02412  * \param config
02413  * \param code
02414  * \param sense feature options
02415  *
02416  * \param data
02417  * Get extension to transfer to, if you cannot generate channel (or find extension)
02418  * return to host channel. After called channel answered wait for hangup of transferer,
02419  * bridge call between transfer peer (taking them off hold) to attended transfer channel.
02420  *
02421  * \return -1 on failure
02422  */
02423 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02424 {
02425    struct ast_channel *transferer;/* Party B */
02426    struct ast_channel *transferee;/* Party A */
02427    struct ast_exten *park_exten;
02428    const char *transferer_real_context;
02429    char xferto[256] = "";
02430    int res;
02431    int outstate=0;
02432    struct ast_channel *newchan;
02433    struct ast_channel *xferchan;
02434    struct ast_bridge_thread_obj *tobj;
02435    struct ast_bridge_config bconfig;
02436    int l;
02437    struct ast_party_connected_line connected_line;
02438    struct ast_datastore *features_datastore;
02439    struct ast_dial_features *dialfeatures = NULL;
02440    char *transferer_tech;
02441    char *transferer_name;
02442    char *transferer_name_orig;
02443    char *dash;
02444 
02445    ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
02446    set_peers(&transferer, &transferee, peer, chan, sense);
02447    transferer_real_context = real_ctx(transferer, transferee);
02448 
02449    /* Start autoservice on transferee while we talk to the transferer */
02450    ast_autoservice_start(transferee);
02451    ast_indicate(transferee, AST_CONTROL_HOLD);
02452 
02453    /* Transfer */
02454    res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
02455    if (res < 0) {
02456       finishup(transferee);
02457       return -1;
02458    }
02459    if (res > 0) { /* If they've typed a digit already, handle it */
02460       xferto[0] = (char) res;
02461    }
02462 
02463    /* this is specific of atxfer */
02464    res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
02465    if (res < 0) {  /* hangup or error, (would be 0 for invalid and 1 for valid) */
02466       finishup(transferee);
02467       return -1;
02468    }
02469    l = strlen(xferto);
02470    if (res == 0) {
02471       if (l) {
02472          ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
02473             xferto, transferer_real_context);
02474       } else {
02475          /* Does anyone care about this case? */
02476          ast_log(LOG_WARNING, "No digits dialed for atxfer.\n");
02477       }
02478       ast_stream_and_wait(transferer, "pbx-invalid", "");
02479       finishup(transferee);
02480       return AST_FEATURE_RETURN_SUCCESS;
02481    }
02482 
02483    park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
02484    if (park_exten) {
02485       /* We are transfering the transferee to a parking lot. */
02486       return xfer_park_call_helper(transferee, transferer, park_exten);
02487    }
02488 
02489    /* Append context to dialed transfer number. */
02490    snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);
02491 
02492    /* If we are performing an attended transfer and we have two channels involved then
02493       copy sound file information to play upon attended transfer completion */
02494    if (transferee) {
02495       const char *chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
02496       const char *chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
02497 
02498       if (!ast_strlen_zero(chan1_attended_sound)) {
02499          pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
02500       }
02501       if (!ast_strlen_zero(chan2_attended_sound)) {
02502          pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
02503       }
02504    }
02505 
02506    /* Extract redial transferer information from the channel name. */
02507    transferer_name_orig = ast_strdupa(transferer->name);
02508    transferer_name = ast_strdupa(transferer_name_orig);
02509    transferer_tech = strsep(&transferer_name, "/");
02510    dash = strrchr(transferer_name, '-');
02511    if (dash) {
02512       /* Trim off channel name sequence/serial number. */
02513       *dash = '\0';
02514    }
02515 
02516    /* Stop autoservice so we can monitor all parties involved in the transfer. */
02517    if (ast_autoservice_stop(transferee) < 0) {
02518       ast_indicate(transferee, AST_CONTROL_UNHOLD);
02519       return -1;
02520    }
02521 
02522    /* Save connected line info for party B about party A in case transfer fails. */
02523    ast_party_connected_line_init(&connected_line);
02524    ast_channel_lock(transferer);
02525    ast_party_connected_line_copy(&connected_line, &transferer->connected);
02526    ast_channel_unlock(transferer);
02527    connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
02528 
02529    /* Dial party C */
02530    newchan = feature_request_and_dial(transferer, transferer_name_orig, transferer,
02531       transferee, "Local", ast_best_codec(transferer->nativeformats), xferto,
02532       atxfernoanswertimeout, &outstate, transferer->language);
02533    ast_debug(2, "Dial party C result: newchan:%d, outstate:%d\n", !!newchan, outstate);
02534 
02535    if (!ast_check_hangup(transferer)) {
02536       int hangup_dont = 0;
02537 
02538       /* Transferer (party B) is up */
02539       ast_debug(1, "Actually doing an attended transfer.\n");
02540 
02541       /* Start autoservice on transferee while the transferer deals with party C. */
02542       ast_autoservice_start(transferee);
02543 
02544       ast_indicate(transferer, -1);
02545       if (!newchan) {
02546          /* any reason besides user requested cancel and busy triggers the failed sound */
02547          switch (outstate) {
02548          case AST_CONTROL_UNHOLD:/* Caller requested cancel or party C answer timeout. */
02549          case AST_CONTROL_BUSY:
02550          case AST_CONTROL_CONGESTION:
02551             if (ast_stream_and_wait(transferer, xfersound, "")) {
02552                ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
02553             }
02554             break;
02555          default:
02556             if (ast_stream_and_wait(transferer, xferfailsound, "")) {
02557                ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
02558             }
02559             break;
02560          }
02561          atxfer_fail_cleanup(transferee, transferer, &connected_line);
02562          return AST_FEATURE_RETURN_SUCCESS;
02563       }
02564 
02565       if (check_compat(transferer, newchan)) {
02566          if (ast_stream_and_wait(transferer, xferfailsound, "")) {
02567             ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
02568          }
02569          atxfer_fail_cleanup(transferee, transferer, &connected_line);
02570          return AST_FEATURE_RETURN_SUCCESS;
02571       }
02572       memset(&bconfig,0,sizeof(struct ast_bridge_config));
02573       ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
02574       ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
02575 
02576       /* ast_bridge_call clears AST_FLAG_BRIDGE_HANGUP_DONT, but we don't
02577          want that to happen here because we're also in another bridge already
02578        */
02579       if (ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT)) {
02580          hangup_dont = 1;
02581       }
02582       /* Let party B and party C talk as long as they want. */
02583       ast_bridge_call(transferer, newchan, &bconfig);
02584       if (hangup_dont) {
02585          ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
02586       }
02587 
02588       if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
02589          ast_hangup(newchan);
02590          if (ast_stream_and_wait(transferer, xfersound, "")) {
02591             ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
02592          }
02593          atxfer_fail_cleanup(transferee, transferer, &connected_line);
02594          return AST_FEATURE_RETURN_SUCCESS;
02595       }
02596 
02597       /* Transferer (party B) is confirmed hung up at this point. */
02598       if (check_compat(transferee, newchan)) {
02599          finishup(transferee);
02600          ast_party_connected_line_free(&connected_line);
02601          return -1;
02602       }
02603 
02604       ast_indicate(transferee, AST_CONTROL_UNHOLD);
02605       if ((ast_autoservice_stop(transferee) < 0)
02606          || (ast_waitfordigit(transferee, 100) < 0)
02607          || (ast_waitfordigit(newchan, 100) < 0)
02608          || ast_check_hangup(transferee)
02609          || ast_check_hangup(newchan)) {
02610          ast_hangup(newchan);
02611          ast_party_connected_line_free(&connected_line);
02612          return -1;
02613       }
02614    } else if (!ast_check_hangup(transferee)) {
02615       /* Transferer (party B) has hung up at this point.  Doing blonde transfer. */
02616       ast_debug(1, "Actually doing a blonde transfer.\n");
02617 
02618       if (!newchan && !atxferdropcall) {
02619          /* Party C is not available, try to call party B back. */
02620          unsigned int tries = 0;
02621 
02622          if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
02623             ast_log(LOG_WARNING,
02624                "Transferer channel name: '%s' cannot be used for callback.\n",
02625                transferer_name_orig);
02626             ast_indicate(transferee, AST_CONTROL_UNHOLD);
02627             ast_party_connected_line_free(&connected_line);
02628             return -1;
02629          }
02630 
02631          tries = 0;
02632          for (;;) {
02633             /* Try to get party B back. */
02634             ast_debug(1, "We're trying to callback %s/%s\n",
02635                transferer_tech, transferer_name);
02636             newchan = feature_request_and_dial(transferer, transferer_name_orig,
02637                transferee, transferee, transferer_tech,
02638                ast_best_codec(transferee->nativeformats), transferer_name,
02639                atxfernoanswertimeout, &outstate, transferer->language);
02640             ast_debug(2, "Dial party B result: newchan:%d, outstate:%d\n",
02641                !!newchan, outstate);
02642             if (newchan || ast_check_hangup(transferee)) {
02643                break;
02644             }
02645 
02646             ++tries;
02647             if (atxfercallbackretries <= tries) {
02648                /* No more callback tries remaining. */
02649                break;
02650             }
02651 
02652             if (atxferloopdelay) {
02653                /* Transfer failed, sleeping */
02654                ast_debug(1, "Sleeping for %d ms before retrying atxfer.\n",
02655                   atxferloopdelay);
02656                ast_safe_sleep(transferee, atxferloopdelay);
02657                if (ast_check_hangup(transferee)) {
02658                   ast_party_connected_line_free(&connected_line);
02659                   return -1;
02660                }
02661             }
02662 
02663             /* Retry dialing party C. */
02664             ast_debug(1, "We're retrying to call %s/%s\n", "Local", xferto);
02665             newchan = feature_request_and_dial(transferer, transferer_name_orig,
02666                transferer, transferee, "Local",
02667                ast_best_codec(transferee->nativeformats), xferto,
02668                atxfernoanswertimeout, &outstate, transferer->language);
02669             ast_debug(2, "Redial party C result: newchan:%d, outstate:%d\n",
02670                !!newchan, outstate);
02671             if (newchan || ast_check_hangup(transferee)) {
02672                break;
02673             }
02674          }
02675       }
02676       ast_indicate(transferee, AST_CONTROL_UNHOLD);
02677       if (!newchan) {
02678          /* No party C or could not callback party B. */
02679          ast_party_connected_line_free(&connected_line);
02680          return -1;
02681       }
02682 
02683       /* newchan is up, we should prepare transferee and bridge them */
02684       if (ast_check_hangup(newchan)) {
02685          ast_hangup(newchan);
02686          ast_party_connected_line_free(&connected_line);
02687          return -1;
02688       }
02689       if (check_compat(transferee, newchan)) {
02690          ast_party_connected_line_free(&connected_line);
02691          return -1;
02692       }
02693    } else {
02694       /*
02695        * Both the transferer and transferee have hungup.  If newchan
02696        * is up, hang it up as it has no one to talk to.
02697        */
02698       ast_debug(1, "Everyone is hungup.\n");
02699       if (newchan) {
02700          ast_hangup(newchan);
02701       }
02702       ast_party_connected_line_free(&connected_line);
02703       return -1;
02704    }
02705 
02706    /* Initiate the channel transfer of party A to party C (or recalled party B). */
02707    ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
02708 
02709    xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
02710    if (!xferchan) {
02711       ast_hangup(newchan);
02712       ast_party_connected_line_free(&connected_line);
02713       return -1;
02714    }
02715 
02716    /* Give party A a momentary ringback tone during transfer. */
02717    xferchan->visible_indication = AST_CONTROL_RINGING;
02718 
02719    /* Make formats okay */
02720    xferchan->readformat = transferee->readformat;
02721    xferchan->writeformat = transferee->writeformat;
02722 
02723    ast_channel_masquerade(xferchan, transferee);
02724    ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
02725    xferchan->_state = AST_STATE_UP;
02726    ast_clear_flag(xferchan, AST_FLAGS_ALL);
02727 
02728    /* Do the masquerade manually to make sure that is is completed. */
02729    ast_do_masquerade(xferchan);
02730 
02731    newchan->_state = AST_STATE_UP;
02732    ast_clear_flag(newchan, AST_FLAGS_ALL);
02733    tobj = ast_calloc(1, sizeof(*tobj));
02734    if (!tobj) {
02735       ast_hangup(xferchan);
02736       ast_hangup(newchan);
02737       ast_party_connected_line_free(&connected_line);
02738       return -1;
02739    }
02740 
02741    ast_channel_lock(newchan);
02742    if ((features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL))) {
02743       dialfeatures = features_datastore->data;
02744    }
02745    ast_channel_unlock(newchan);
02746 
02747    if (dialfeatures) {
02748       /* newchan should always be the callee and shows up as callee in dialfeatures, but for some reason
02749          I don't currently understand, the abilities of newchan seem to be stored on the caller side */
02750       ast_copy_flags(&(config->features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
02751       dialfeatures = NULL;
02752    }
02753 
02754    ast_channel_lock(xferchan);
02755    if ((features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL))) {
02756       dialfeatures = features_datastore->data;
02757    }
02758    ast_channel_unlock(xferchan);
02759 
02760    if (dialfeatures) {
02761       ast_copy_flags(&(config->features_caller), &(dialfeatures->features_caller), AST_FLAGS_ALL);
02762    }
02763 
02764    tobj->chan = newchan;
02765    tobj->peer = xferchan;
02766    tobj->bconfig = *config;
02767 
02768    if (tobj->bconfig.end_bridge_callback_data_fixup) {
02769       tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
02770    }
02771 
02772    /*
02773     * xferchan is transferee, and newchan is the transfer target
02774     * So...in a transfer, who is the caller and who is the callee?
02775     *
02776     * When the call is originally made, it is clear who is caller and callee.
02777     * When a transfer occurs, it is my humble opinion that the transferee becomes
02778     * the caller, and the transfer target is the callee.
02779     *
02780     * The problem is that these macros were set with the intention of the original
02781     * caller and callee taking those roles.  A transfer can totally mess things up,
02782     * to be technical.  What sucks even more is that you can't effectively change
02783     * the macros in the dialplan during the call from the transferer to the transfer
02784     * target because the transferee is stuck with whatever role he originally had.
02785     *
02786     * I think the answer here is just to make sure that it is well documented that
02787     * during a transfer, the transferee is the "caller" and the transfer target
02788     * is the "callee."
02789     *
02790     * This means that if party B calls party A, and party B transfers party A to
02791     * party C, then A has switched roles for the call.  Now party A will have the
02792     * caller macro called on his channel instead of the callee macro.
02793     *
02794     * Luckily, the method by which the party B to party C bridge is
02795     * launched above ensures that the transferee is the "chan" on
02796     * the bridge and the transfer target is the "peer," so my idea
02797     * for the roles post-transfer does not require extensive code
02798     * changes.
02799     */
02800 
02801    /* Transfer party C connected line to party A */
02802    ast_channel_lock(transferer);
02803    /*
02804     * Due to a limitation regarding when callerID is set on a Local channel,
02805     * we use the transferer's connected line information here.
02806     */
02807    ast_party_connected_line_copy(&connected_line, &transferer->connected);
02808    ast_channel_unlock(transferer);
02809    connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
02810    if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
02811       ast_channel_update_connected_line(xferchan, &connected_line, NULL);
02812    }
02813 
02814    /* Transfer party A connected line to party C */
02815    ast_channel_lock(xferchan);
02816    ast_connected_line_copy_from_caller(&connected_line, &xferchan->caller);
02817    ast_channel_unlock(xferchan);
02818    connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
02819    if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
02820       ast_channel_update_connected_line(newchan, &connected_line, NULL);
02821    }
02822 
02823    if (ast_stream_and_wait(newchan, xfersound, ""))
02824       ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
02825    bridge_call_thread_launch(tobj);
02826 
02827    ast_party_connected_line_free(&connected_line);
02828    return -1;/* The transferee is masqueraded and the original bridged channels can be hungup. */
02829 }
02830 
02831 /* add atxfer and automon as undefined so you can only use em if you configure them */
02832 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
02833 
02834 AST_RWLOCK_DEFINE_STATIC(features_lock);
02835 
02836 static struct ast_call_feature builtin_features[] = {
02837    { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02838    { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02839    { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02840    { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02841    { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02842    { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02843 };
02844 
02845 
02846 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
02847 
02848 /*! \brief register new feature into feature_list*/
02849 void ast_register_feature(struct ast_call_feature *feature)
02850 {
02851    if (!feature) {
02852       ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
02853       return;
02854    }
02855   
02856    AST_RWLIST_WRLOCK(&feature_list);
02857    AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
02858    AST_RWLIST_UNLOCK(&feature_list);
02859 
02860    ast_verb(2, "Registered Feature '%s'\n",feature->sname);
02861 }
02862 
02863 /*! 
02864  * \brief Add new feature group
02865  * \param fgname feature group name.
02866  *
02867  * Add new feature group to the feature group list insert at head of list.
02868  * \note This function MUST be called while feature_groups is locked.
02869  */
02870 static struct feature_group *register_group(const char *fgname)
02871 {
02872    struct feature_group *fg;
02873 
02874    if (!fgname) {
02875       ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
02876       return NULL;
02877    }
02878 
02879    if (!(fg = ast_calloc_with_stringfields(1, struct feature_group, 128))) {
02880       return NULL;
02881    }
02882 
02883    ast_string_field_set(fg, gname, fgname);
02884 
02885    AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
02886 
02887    ast_verb(2, "Registered group '%s'\n", fg->gname);
02888 
02889    return fg;
02890 }
02891 
02892 /*! 
02893  * \brief Add feature to group
02894  * \param fg feature group
02895  * \param exten
02896  * \param feature feature to add.
02897  *
02898  * Check fg and feature specified, add feature to list
02899  * \note This function MUST be called while feature_groups is locked. 
02900  */
02901 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
02902 {
02903    struct feature_group_exten *fge;
02904 
02905    if (!fg) {
02906       ast_log(LOG_NOTICE, "You didn't pass a group!\n");
02907       return;
02908    }
02909 
02910    if (!feature) {
02911       ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
02912       return;
02913    }
02914 
02915    if (!(fge = ast_calloc_with_stringfields(1, struct feature_group_exten, 128))) {
02916       return;
02917    }
02918 
02919    ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
02920 
02921    fge->feature = feature;
02922 
02923    AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
02924 
02925    ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
02926                feature->sname, fg->gname, fge->exten);
02927 }
02928 
02929 void ast_unregister_feature(struct ast_call_feature *feature)
02930 {
02931    if (!feature) {
02932       return;
02933    }
02934 
02935    AST_RWLIST_WRLOCK(&feature_list);
02936    AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
02937    AST_RWLIST_UNLOCK(&feature_list);
02938 
02939    ast_free(feature);
02940 }
02941 
02942 /*! \brief Remove all features in the list */
02943 static void ast_unregister_features(void)
02944 {
02945    struct ast_call_feature *feature;
02946 
02947    AST_RWLIST_WRLOCK(&feature_list);
02948    while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
02949       ast_free(feature);
02950    }
02951    AST_RWLIST_UNLOCK(&feature_list);
02952 }
02953 
02954 /*! \brief find a call feature by name */
02955 static struct ast_call_feature *find_dynamic_feature(const char *name)
02956 {
02957    struct ast_call_feature *tmp;
02958 
02959    AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
02960       if (!strcasecmp(tmp->sname, name)) {
02961          break;
02962       }
02963    }
02964 
02965    return tmp;
02966 }
02967 
02968 /*! \brief Remove all feature groups in the list */
02969 static void ast_unregister_groups(void)
02970 {
02971    struct feature_group *fg;
02972    struct feature_group_exten *fge;
02973 
02974    AST_RWLIST_WRLOCK(&feature_groups);
02975    while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
02976       while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
02977          ast_string_field_free_memory(fge);
02978          ast_free(fge);
02979       }
02980 
02981       ast_string_field_free_memory(fg);
02982       ast_free(fg);
02983    }
02984    AST_RWLIST_UNLOCK(&feature_groups);
02985 }
02986 
02987 /*! 
02988  * \brief Find a group by name 
02989  * \param name feature name
02990  * \retval feature group on success.
02991  * \retval NULL on failure.
02992  */
02993 static struct feature_group *find_group(const char *name)
02994 {
02995    struct feature_group *fg = NULL;
02996 
02997    AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
02998       if (!strcasecmp(fg->gname, name))
02999          break;
03000    }
03001 
03002    return fg;
03003 }
03004 
03005 void ast_rdlock_call_features(void)
03006 {
03007    ast_rwlock_rdlock(&features_lock);
03008 }
03009 
03010 void ast_unlock_call_features(void)
03011 {
03012    ast_rwlock_unlock(&features_lock);
03013 }
03014 
03015 struct ast_call_feature *ast_find_call_feature(const char *name)
03016 {
03017    int x;
03018    for (x = 0; x < FEATURES_COUNT; x++) {
03019       if (!strcasecmp(name, builtin_features[x].sname))
03020          return &builtin_features[x];
03021    }
03022    return NULL;
03023 }
03024 
03025 /*!
03026  * \brief exec an app by feature 
03027  * \param chan,peer,config,code,sense,data
03028  *
03029  * Find a feature, determine which channel activated
03030  * \retval AST_FEATURE_RETURN_NO_HANGUP_PEER
03031  * \retval -1 error.
03032  * \retval -2 when an application cannot be found.
03033  */
03034 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
03035 {
03036    struct ast_app *app;
03037    struct ast_call_feature *feature = data;
03038    struct ast_channel *work, *idle;
03039    int res;
03040 
03041    if (!feature) { /* shouldn't ever happen! */
03042       ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
03043       return -1; 
03044    }
03045 
03046    if (sense == FEATURE_SENSE_CHAN) {
03047       if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
03048          return AST_FEATURE_RETURN_KEEPTRYING;
03049       if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
03050          work = chan;
03051          idle = peer;
03052       } else {
03053          work = peer;
03054          idle = chan;
03055       }
03056    } else {
03057       if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
03058          return AST_FEATURE_RETURN_KEEPTRYING;
03059       if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
03060          work = peer;
03061          idle = chan;
03062       } else {
03063          work = chan;
03064          idle = peer;
03065       }
03066    }
03067 
03068    if (!(app = pbx_findapp(feature->app))) {
03069       ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
03070       return -2;
03071    }
03072 
03073    ast_autoservice_start(idle);
03074    ast_autoservice_ignore(idle, AST_FRAME_DTMF_END);
03075    
03076    if(work && idle) {
03077       pbx_builtin_setvar_helper(work, "DYNAMIC_PEERNAME", idle->name);
03078       pbx_builtin_setvar_helper(idle, "DYNAMIC_PEERNAME", work->name);
03079       pbx_builtin_setvar_helper(work, "DYNAMIC_FEATURENAME", feature->sname);
03080       pbx_builtin_setvar_helper(idle, "DYNAMIC_FEATURENAME", feature->sname);
03081    }
03082 
03083    if (!ast_strlen_zero(feature->moh_class))
03084       ast_moh_start(idle, feature->moh_class, NULL);
03085 
03086    res = pbx_exec(work, app, feature->app_args);
03087 
03088    if (!ast_strlen_zero(feature->moh_class))
03089       ast_moh_stop(idle);
03090 
03091    ast_autoservice_stop(idle);
03092 
03093    if (res) {
03094       return AST_FEATURE_RETURN_SUCCESSBREAK;
03095    }
03096    return AST_FEATURE_RETURN_SUCCESS;  /*! \todo XXX should probably return res */
03097 }
03098 
03099 static void unmap_features(void)
03100 {
03101    int x;
03102 
03103    ast_rwlock_wrlock(&features_lock);
03104    for (x = 0; x < FEATURES_COUNT; x++)
03105       strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
03106    ast_rwlock_unlock(&features_lock);
03107 }
03108 
03109 static int remap_feature(const char *name, const char *value)
03110 {
03111    int x, res = -1;
03112 
03113    ast_rwlock_wrlock(&features_lock);
03114    for (x = 0; x < FEATURES_COUNT; x++) {
03115       if (strcasecmp(builtin_features[x].sname, name))
03116          continue;
03117 
03118       ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
03119       res = 0;
03120       break;
03121    }
03122    ast_rwlock_unlock(&features_lock);
03123 
03124    return res;
03125 }
03126 
03127 /*!
03128  * \brief Helper function for feature_interpret and ast_feature_detect
03129  * \param chan,peer,config,code,sense,dynamic_features_buf,features,operation,feature
03130  *
03131  * Lock features list, browse for code, unlock list
03132  * If a feature is found and the operation variable is set, that feature's
03133  * operation is executed.  The first feature found is copied to the feature parameter.
03134  * \retval res on success.
03135  * \retval -1 on failure.
03136  */
03137 static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel *peer,
03138    struct ast_bridge_config *config, const char *code, int sense, char *dynamic_features_buf,
03139    struct ast_flags *features, feature_interpret_op operation, struct ast_call_feature *feature)
03140 {
03141    int x;
03142    struct feature_group *fg = NULL;
03143    struct feature_group_exten *fge;
03144    struct ast_call_feature *tmpfeature;
03145    char *tmp, *tok;
03146    int res = AST_FEATURE_RETURN_PASSDIGITS;
03147    int feature_detected = 0;
03148 
03149    if (!(peer && chan && config) && operation == FEATURE_INTERPRET_DO) {
03150       return -1; /* can not run feature operation */
03151    }
03152 
03153    ast_rwlock_rdlock(&features_lock);
03154    for (x = 0; x < FEATURES_COUNT; x++) {
03155       if ((ast_test_flag(features, builtin_features[x].feature_mask)) &&
03156           !ast_strlen_zero(builtin_features[x].exten)) {
03157          /* Feature is up for consideration */
03158          if (!strcmp(builtin_features[x].exten, code)) {
03159             ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
03160             if (operation == FEATURE_INTERPRET_CHECK) {
03161                res = AST_FEATURE_RETURN_SUCCESS; /* We found something */
03162             } else if (operation == FEATURE_INTERPRET_DO) {
03163                res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
03164             }
03165             if (feature) {
03166                memcpy(feature, &builtin_features[x], sizeof(feature));
03167             }
03168             feature_detected = 1;
03169             break;
03170          } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
03171             if (res == AST_FEATURE_RETURN_PASSDIGITS) {
03172                res = AST_FEATURE_RETURN_STOREDIGITS;
03173             }
03174          }
03175       }
03176    }
03177    ast_rwlock_unlock(&features_lock);
03178 
03179    if (ast_strlen_zero(dynamic_features_buf) || feature_detected) {
03180       return res;
03181    }
03182 
03183    tmp = dynamic_features_buf;
03184 
03185    while ((tok = strsep(&tmp, "#"))) {
03186       AST_RWLIST_RDLOCK(&feature_groups);
03187 
03188       fg = find_group(tok);
03189 
03190       if (fg) {
03191          AST_LIST_TRAVERSE(&fg->features, fge, entry) {
03192             if (!strcmp(fge->exten, code)) {
03193                if (operation) {
03194                   res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
03195                }
03196                memcpy(feature, fge->feature, sizeof(feature));
03197                if (res != AST_FEATURE_RETURN_KEEPTRYING) {
03198                   AST_RWLIST_UNLOCK(&feature_groups);
03199                   break;
03200                }
03201                res = AST_FEATURE_RETURN_PASSDIGITS;
03202             } else if (!strncmp(fge->exten, code, strlen(code))) {
03203                res = AST_FEATURE_RETURN_STOREDIGITS;
03204             }
03205          }
03206          if (fge) {
03207             break;
03208          }
03209       }
03210 
03211       AST_RWLIST_UNLOCK(&feature_groups);
03212 
03213       AST_RWLIST_RDLOCK(&feature_list);
03214 
03215       if (!(tmpfeature = find_dynamic_feature(tok))) {
03216          AST_RWLIST_UNLOCK(&feature_list);
03217          continue;
03218       }
03219 
03220       /* Feature is up for consideration */
03221       if (!strcmp(tmpfeature->exten, code)) {
03222          ast_verb(3, " Feature Found: %s exten: %s\n",tmpfeature->sname, tok);
03223          if (operation == FEATURE_INTERPRET_CHECK) {
03224             res = AST_FEATURE_RETURN_SUCCESS; /* We found something */
03225          } else if (operation == FEATURE_INTERPRET_DO) {
03226             res = tmpfeature->operation(chan, peer, config, code, sense, tmpfeature);
03227          }
03228          if (feature) {
03229             memcpy(feature, tmpfeature, sizeof(feature));
03230          }
03231          if (res != AST_FEATURE_RETURN_KEEPTRYING) {
03232             AST_RWLIST_UNLOCK(&feature_list);
03233             break;
03234          }
03235          res = AST_FEATURE_RETURN_PASSDIGITS;
03236       } else if (!strncmp(tmpfeature->exten, code, strlen(code)))
03237          res = AST_FEATURE_RETURN_STOREDIGITS;
03238 
03239       AST_RWLIST_UNLOCK(&feature_list);
03240    }
03241 
03242    return res;
03243 }
03244 
03245 /*!
03246  * \brief Check the dynamic features
03247  * \param chan,peer,config,code,sense
03248  *
03249  * \retval res on success.
03250  * \retval -1 on failure.
03251  */
03252 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense) {
03253 
03254    char dynamic_features_buf[128];
03255    const char *peer_dynamic_features, *chan_dynamic_features;
03256    struct ast_flags features;
03257    struct ast_call_feature feature;
03258    if (sense == FEATURE_SENSE_CHAN) {
03259       ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
03260    }
03261    else {
03262       ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
03263    }
03264 
03265    ast_channel_lock(peer);
03266    peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
03267    ast_channel_unlock(peer);
03268 
03269    ast_channel_lock(chan);
03270    chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
03271    ast_channel_unlock(chan);
03272 
03273    snprintf(dynamic_features_buf, sizeof(dynamic_features_buf), "%s%s%s", S_OR(chan_dynamic_features, ""), chan_dynamic_features && peer_dynamic_features ? "#" : "", S_OR(peer_dynamic_features,""));
03274 
03275    ast_debug(3, "Feature interpret: chan=%s, peer=%s, code=%s, sense=%d, features=%d, dynamic=%s\n", chan->name, peer->name, code, sense, features.flags, dynamic_features_buf);
03276 
03277    return feature_interpret_helper(chan, peer, config, code, sense, dynamic_features_buf, &features, FEATURE_INTERPRET_DO, &feature);
03278 }
03279 
03280 
03281 int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
03282 
03283    return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, FEATURE_INTERPRET_DETECT, feature);
03284 }
03285 
03286 /*! \brief Check if a feature exists */
03287 static int feature_check(struct ast_channel *chan, struct ast_flags *features, char *code) {
03288    char *chan_dynamic_features;
03289    ast_channel_lock(chan);
03290    chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
03291    ast_channel_unlock(chan);
03292 
03293    return feature_interpret_helper(chan, NULL, NULL, code, 0, chan_dynamic_features, features, FEATURE_INTERPRET_CHECK, NULL);
03294 }
03295 
03296 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
03297 {
03298    int x;
03299 
03300    ast_clear_flag(config, AST_FLAGS_ALL);
03301 
03302    ast_rwlock_rdlock(&features_lock);
03303    for (x = 0; x < FEATURES_COUNT; x++) {
03304       if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
03305          continue;
03306 
03307       if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
03308          ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
03309 
03310       if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
03311          ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
03312    }
03313    ast_rwlock_unlock(&features_lock);
03314 
03315    if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
03316       const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
03317 
03318       if (dynamic_features) {
03319          char *tmp = ast_strdupa(dynamic_features);
03320          char *tok;
03321          struct ast_call_feature *feature;
03322 
03323          /* while we have a feature */
03324          while ((tok = strsep(&tmp, "#"))) {
03325             struct feature_group *fg;
03326 
03327             AST_RWLIST_RDLOCK(&feature_groups);
03328             AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
03329                struct feature_group_exten *fge;
03330 
03331                AST_LIST_TRAVERSE(&fg->features, fge, entry) {
03332                   if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLER)) {
03333                      ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
03334                   }
03335                   if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLEE)) {
03336                      ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
03337                   }
03338                }
03339             }
03340             AST_RWLIST_UNLOCK(&feature_groups);
03341 
03342             AST_RWLIST_RDLOCK(&feature_list);
03343             if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
03344                if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER)) {
03345                   ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
03346                }
03347                if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE)) {
03348                   ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
03349                }
03350             }
03351             AST_RWLIST_UNLOCK(&feature_list);
03352          }
03353       }
03354    }
03355 }
03356 
03357 /*!
03358  * \internal
03359  * \brief Get feature and dial.
03360  *
03361  * \param caller Channel to represent as the calling channel for the dialed channel.
03362  * \param caller_name Original caller channel name.
03363  * \param requestor Channel to say is requesting the dial (usually the caller).
03364  * \param transferee Channel that the dialed channel will be transferred to.
03365  * \param type Channel technology type to dial.
03366  * \param format Codec formats for dialed channel.
03367  * \param data Dialed channel extra parameters for ast_request() and ast_call().
03368  * \param timeout Time limit for dialed channel to answer in ms. Must be greater than zero.
03369  * \param outstate Status of dialed channel if unsuccessful.
03370  * \param language Language of the caller.
03371  *
03372  * \note
03373  * outstate can be:
03374  * 0, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION,
03375  * AST_CONTROL_ANSWER, or AST_CONTROL_UNHOLD.  If
03376  * AST_CONTROL_UNHOLD then the caller channel cancelled the
03377  * transfer or the dialed channel did not answer before the
03378  * timeout.
03379  *
03380  * \details
03381  * Request channel, set channel variables, initiate call,
03382  * check if they want to disconnect, go into loop, check if timeout has elapsed,
03383  * check if person to be transfered hung up, check for answer break loop,
03384  * set cdr return channel.
03385  *
03386  * \retval Channel Connected channel for transfer.
03387  * \retval NULL on failure to get third party connected.
03388  *
03389  * \note This is similar to __ast_request_and_dial() in channel.c
03390  */
03391 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
03392    const char *caller_name, struct ast_channel *requestor,
03393    struct ast_channel *transferee, const char *type, format_t format, void *data,
03394    int timeout, int *outstate, const char *language)
03395 {
03396    int state = 0;
03397    int cause = 0;
03398    int to;
03399    int caller_hungup;
03400    int transferee_hungup;
03401    struct ast_channel *chan;
03402    struct ast_channel *monitor_chans[3];
03403    struct ast_channel *active_channel;
03404    int res;
03405    int ready = 0;
03406    struct timeval started;
03407    int x, len = 0;
03408    char *disconnect_code = NULL, *dialed_code = NULL;
03409    struct ast_frame *f;
03410    AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
03411 
03412    caller_hungup = ast_check_hangup(caller);
03413 
03414    if (!(chan = ast_request(type, format, requestor, data, &cause))) {
03415       ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
03416       switch (cause) {
03417       case AST_CAUSE_BUSY:
03418          state = AST_CONTROL_BUSY;
03419          break;
03420       case AST_CAUSE_CONGESTION:
03421          state = AST_CONTROL_CONGESTION;
03422          break;
03423       default:
03424          state = 0;
03425          break;
03426       }
03427       goto done;
03428    }
03429 
03430    ast_string_field_set(chan, language, language);
03431    ast_channel_inherit_variables(caller, chan);
03432    pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller_name);
03433 
03434    ast_channel_lock(chan);
03435    ast_connected_line_copy_from_caller(&chan->connected, &requestor->caller);
03436    ast_channel_unlock(chan);
03437 
03438    if (ast_call(chan, data, timeout)) {
03439       ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
03440       switch (chan->hangupcause) {
03441       case AST_CAUSE_BUSY:
03442          state = AST_CONTROL_BUSY;
03443          break;
03444       case AST_CAUSE_CONGESTION:
03445          state = AST_CONTROL_CONGESTION;
03446          break;
03447       default:
03448          state = 0;
03449          break;
03450       }
03451       goto done;
03452    }
03453 
03454    /* support dialing of the featuremap disconnect code while performing an attended tranfer */
03455    ast_rwlock_rdlock(&features_lock);
03456    for (x = 0; x < FEATURES_COUNT; x++) {
03457       if (strcasecmp(builtin_features[x].sname, "disconnect"))
03458          continue;
03459 
03460       disconnect_code = builtin_features[x].exten;
03461       len = strlen(disconnect_code) + 1;
03462       dialed_code = alloca(len);
03463       memset(dialed_code, 0, len);
03464       break;
03465    }
03466    ast_rwlock_unlock(&features_lock);
03467    x = 0;
03468    started = ast_tvnow();
03469    to = timeout;
03470    AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
03471 
03472    ast_poll_channel_add(caller, chan);
03473 
03474    transferee_hungup = 0;
03475    while (!ast_check_hangup(transferee) && (chan->_state != AST_STATE_UP)) {
03476       int num_chans = 0;
03477 
03478       monitor_chans[num_chans++] = transferee;
03479       monitor_chans[num_chans++] = chan;
03480       if (!caller_hungup) {
03481          if (ast_check_hangup(caller)) {
03482             caller_hungup = 1;
03483 
03484 #if defined(ATXFER_NULL_TECH)
03485             /* Change caller's name to ensure that it will remain unique. */
03486             set_new_chan_name(caller);
03487 
03488             /*
03489              * Get rid of caller's physical technology so it is free for
03490              * other calls.
03491              */
03492             set_kill_chan_tech(caller);
03493 #endif   /* defined(ATXFER_NULL_TECH) */
03494          } else {
03495             /* caller is not hungup so monitor it. */
03496             monitor_chans[num_chans++] = caller;
03497          }
03498       }
03499 
03500       /* see if the timeout has been violated */
03501       if (ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
03502          state = AST_CONTROL_UNHOLD;
03503          ast_log(LOG_NOTICE, "We exceeded our AT-timeout for %s\n", chan->name);
03504          break; /*doh! timeout*/
03505       }
03506 
03507       active_channel = ast_waitfor_n(monitor_chans, num_chans, &to);
03508       if (!active_channel)
03509          continue;
03510 
03511       f = NULL;
03512       if (transferee == active_channel) {
03513          struct ast_frame *dup_f;
03514 
03515          f = ast_read(transferee);
03516          if (f == NULL) { /*doh! where'd he go?*/
03517             transferee_hungup = 1;
03518             state = 0;
03519             break;
03520          }
03521          if (ast_is_deferrable_frame(f)) {
03522             dup_f = ast_frisolate(f);
03523             if (dup_f) {
03524                if (dup_f == f) {
03525                   f = NULL;
03526                }
03527                AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
03528             }
03529          }
03530       } else if (chan == active_channel) {
03531          if (!ast_strlen_zero(chan->call_forward)) {
03532             state = 0;
03533             ast_autoservice_start(transferee);
03534             chan = ast_call_forward(caller, chan, NULL, format, NULL, &state);
03535             ast_autoservice_stop(transferee);
03536             if (!chan) {
03537                break;
03538             }
03539             continue;
03540          }
03541          f = ast_read(chan);
03542          if (f == NULL) { /*doh! where'd he go?*/
03543             switch (chan->hangupcause) {
03544             case AST_CAUSE_BUSY:
03545                state = AST_CONTROL_BUSY;
03546                break;
03547             case AST_CAUSE_CONGESTION:
03548                state = AST_CONTROL_CONGESTION;
03549                break;
03550             default:
03551                state = 0;
03552                break;
03553             }
03554             break;
03555          }
03556 
03557          if (f->frametype == AST_FRAME_CONTROL) {
03558             if (f->subclass.integer == AST_CONTROL_RINGING) {
03559                ast_verb(3, "%s is ringing\n", chan->name);
03560                ast_indicate(caller, AST_CONTROL_RINGING);
03561             } else if (f->subclass.integer == AST_CONTROL_BUSY) {
03562                state = f->subclass.integer;
03563                ast_verb(3, "%s is busy\n", chan->name);
03564                ast_indicate(caller, AST_CONTROL_BUSY);
03565                ast_frfree(f);
03566                break;
03567             } else if (f->subclass.integer == AST_CONTROL_INCOMPLETE) {
03568                ast_verb(3, "%s dialed incomplete extension %s; ignoring\n", chan->name, chan->exten);
03569             } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
03570                state = f->subclass.integer;
03571                ast_verb(3, "%s is congested\n", chan->name);
03572                ast_indicate(caller, AST_CONTROL_CONGESTION);
03573                ast_frfree(f);
03574                break;
03575             } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
03576                /* This is what we are hoping for */
03577                state = f->subclass.integer;
03578                ast_frfree(f);
03579                ready=1;
03580                break;
03581             } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
03582                if (caller_hungup) {
03583                   struct ast_party_connected_line connected;
03584 
03585                   /* Just save it for the transfer. */
03586                   ast_party_connected_line_set_init(&connected, &caller->connected);
03587                   res = ast_connected_line_parse_data(f->data.ptr, f->datalen,
03588                      &connected);
03589                   if (!res) {
03590                      ast_channel_set_connected_line(caller, &connected, NULL);
03591                   }
03592                   ast_party_connected_line_free(&connected);
03593                } else {
03594                   ast_autoservice_start(transferee);
03595                   if (ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
03596                      ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE,
03597                         f->data.ptr, f->datalen);
03598                   }
03599                   ast_autoservice_stop(transferee);
03600                }
03601             } else if (f->subclass.integer == AST_CONTROL_REDIRECTING) {
03602                if (!caller_hungup) {
03603                   ast_autoservice_start(transferee);
03604                   if (ast_channel_redirecting_macro(chan, caller, f, 1, 1)) {
03605                      ast_indicate_data(caller, AST_CONTROL_REDIRECTING,
03606                         f->data.ptr, f->datalen);
03607                   }
03608                   ast_autoservice_stop(transferee);
03609                }
03610             } else if (f->subclass.integer != -1
03611                && f->subclass.integer != AST_CONTROL_PROGRESS
03612                && f->subclass.integer != AST_CONTROL_PROCEEDING) {
03613                ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
03614             }
03615             /* else who cares */
03616          } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
03617             ast_write(caller, f);
03618          }
03619       } else if (caller == active_channel) {
03620          f = ast_read(caller);
03621          if (f) {
03622             if (f->frametype == AST_FRAME_DTMF) {
03623                dialed_code[x++] = f->subclass.integer;
03624                dialed_code[x] = '\0';
03625                if (strlen(dialed_code) == len) {
03626                   x = 0;
03627                } else if (x && strncmp(dialed_code, disconnect_code, x)) {
03628                   x = 0;
03629                   dialed_code[x] = '\0';
03630                }
03631                if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
03632                   /* Caller Canceled the call */
03633                   state = AST_CONTROL_UNHOLD;
03634                   ast_frfree(f);
03635                   break;
03636                }
03637             } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
03638                ast_write(chan, f);
03639             }
03640          }
03641       }
03642       if (f)
03643          ast_frfree(f);
03644    } /* end while */
03645 
03646    ast_poll_channel_del(caller, chan);
03647 
03648    /*
03649     * We need to free all the deferred frames, but we only need to
03650     * queue the deferred frames if no hangup was received.
03651     */
03652    ast_channel_lock(transferee);
03653    transferee_hungup = (transferee_hungup || ast_check_hangup(transferee));
03654    while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
03655       if (!transferee_hungup) {
03656          ast_queue_frame_head(transferee, f);
03657       }
03658       ast_frfree(f);
03659    }
03660    ast_channel_unlock(transferee);
03661 
03662 done:
03663    ast_indicate(caller, -1);
03664    if (chan && (ready || chan->_state == AST_STATE_UP)) {
03665       state = AST_CONTROL_ANSWER;
03666    } else if (chan) {
03667       ast_hangup(chan);
03668       chan = NULL;
03669    }
03670 
03671    if (outstate)
03672       *outstate = state;
03673 
03674    return chan;
03675 }
03676 
03677 void ast_channel_log(char *title, struct ast_channel *chan);
03678 
03679 void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
03680 {
03681        ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long)chan);
03682        ast_log(LOG_NOTICE, "CHAN: name: %s;  appl: %s; data: %s; contxt: %s;  exten: %s; pri: %d;\n",
03683                        chan->name, chan->appl, chan->data, chan->context, chan->exten, chan->priority);
03684        ast_log(LOG_NOTICE, "CHAN: acctcode: %s;  dialcontext: %s; amaflags: %x; maccontxt: %s;  macexten: %s; macpri: %d;\n",
03685                        chan->accountcode, chan->dialcontext, chan->amaflags, chan->macrocontext, chan->macroexten, chan->macropriority);
03686        ast_log(LOG_NOTICE, "CHAN: masq: %p;  masqr: %p; _bridge: %p; uniqueID: %s; linkedID:%s\n",
03687                        chan->masq, chan->masqr,
03688                        chan->_bridge, chan->uniqueid, chan->linkedid);
03689        if (chan->masqr)
03690                ast_log(LOG_NOTICE, "CHAN: masquerading as: %s;  cdr: %p;\n",
03691                                chan->masqr->name, chan->masqr->cdr);
03692        if (chan->_bridge)
03693                ast_log(LOG_NOTICE, "CHAN: Bridged to %s\n", chan->_bridge->name);
03694 
03695    ast_log(LOG_NOTICE, "===== done ====\n");
03696 }
03697 
03698 /*!
03699  * \brief return the first unlocked cdr in a possible chain
03700  */
03701 static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
03702 {
03703    struct ast_cdr *cdr_orig = cdr;
03704    while (cdr) {
03705       if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
03706          return cdr;
03707       cdr = cdr->next;
03708    }
03709    return cdr_orig; /* everybody LOCKED or some other weirdness, like a NULL */
03710 }
03711 
03712 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
03713 {
03714    const char *feature;
03715 
03716    if (ast_strlen_zero(features)) {
03717       return;
03718    }
03719 
03720    for (feature = features; *feature; feature++) {
03721       switch (*feature) {
03722       case 'T' :
03723       case 't' :
03724          ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
03725          break;
03726       case 'K' :
03727       case 'k' :
03728          ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
03729          break;
03730       case 'H' :
03731       case 'h' :
03732          ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
03733          break;
03734       case 'W' :
03735       case 'w' :
03736          ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
03737          break;
03738       default :
03739          ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
03740       }
03741    }
03742 }
03743 
03744 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
03745 {
03746    struct ast_datastore *ds_callee_features = NULL, *ds_caller_features = NULL;
03747    struct ast_dial_features *callee_features = NULL, *caller_features = NULL;
03748 
03749    ast_channel_lock(caller);
03750    ds_caller_features = ast_channel_datastore_find(caller, &dial_features_info, NULL);
03751    ast_channel_unlock(caller);
03752    if (!ds_caller_features) {
03753       if (!(ds_caller_features = ast_datastore_alloc(&dial_features_info, NULL))) {
03754          ast_log(LOG_WARNING, "Unable to create channel datastore for caller features. Aborting!\n");
03755          return;
03756       }
03757       if (!(caller_features = ast_calloc(1, sizeof(*caller_features)))) {
03758          ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
03759          ast_datastore_free(ds_caller_features);
03760          return;
03761       }
03762       ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
03763       caller_features->is_caller = 1;
03764       ast_copy_flags(&(caller_features->features_callee), &(config->features_callee), AST_FLAGS_ALL);
03765       ast_copy_flags(&(caller_features->features_caller), &(config->features_caller), AST_FLAGS_ALL);
03766       ds_caller_features->data = caller_features;
03767       ast_channel_lock(caller);
03768       ast_channel_datastore_add(caller, ds_caller_features);
03769       ast_channel_unlock(caller);
03770    } else {
03771       /* If we don't return here, then when we do a builtin_atxfer we will copy the disconnect
03772        * flags over from the atxfer to the caller */
03773       return;
03774    }
03775 
03776    ast_channel_lock(callee);
03777    ds_callee_features = ast_channel_datastore_find(callee, &dial_features_info, NULL);
03778    ast_channel_unlock(callee);
03779    if (!ds_callee_features) {
03780       if (!(ds_callee_features = ast_datastore_alloc(&dial_features_info, NULL))) {
03781          ast_log(LOG_WARNING, "Unable to create channel datastore for callee features. Aborting!\n");
03782          return;
03783       }
03784       if (!(callee_features = ast_calloc(1, sizeof(*callee_features)))) {
03785          ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
03786          ast_datastore_free(ds_callee_features);
03787          return;
03788       }
03789       ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
03790       callee_features->is_caller = 0;
03791       ast_copy_flags(&(callee_features->features_callee), &(config->features_caller), AST_FLAGS_ALL);
03792       ast_copy_flags(&(callee_features->features_caller), &(config->features_callee), AST_FLAGS_ALL);
03793       ds_callee_features->data = callee_features;
03794       ast_channel_lock(callee);
03795       ast_channel_datastore_add(callee, ds_callee_features);
03796       ast_channel_unlock(callee);
03797    }
03798 
03799    return;
03800 }
03801 
03802 static void clear_dialed_interfaces(struct ast_channel *chan)
03803 {
03804    struct ast_datastore *di_datastore;
03805 
03806    ast_channel_lock(chan);
03807    if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
03808       if (option_debug) {
03809          ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", chan->name);
03810       }
03811       if (!ast_channel_datastore_remove(chan, di_datastore)) {
03812          ast_datastore_free(di_datastore);
03813       }
03814    }
03815    ast_channel_unlock(chan);
03816 }
03817 
03818 /*!
03819  * \brief bridge the call and set CDR
03820  *
03821  * \param chan The bridge considers this channel the caller.
03822  * \param peer The bridge considers this channel the callee.
03823  * \param config Configuration for this bridge.
03824  *
03825  * Set start time, check for two channels,check if monitor on
03826  * check for feature activation, create new CDR
03827  * \retval res on success.
03828  * \retval -1 on failure to bridge.
03829  */
03830 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
03831 {
03832    /* Copy voice back and forth between the two channels.  Give the peer
03833       the ability to transfer calls with '#<extension' syntax. */
03834    struct ast_frame *f;
03835    struct ast_channel *who;
03836    char chan_featurecode[FEATURE_MAX_LEN + 1]="";
03837    char peer_featurecode[FEATURE_MAX_LEN + 1]="";
03838    char orig_channame[AST_CHANNEL_NAME];
03839    char orig_peername[AST_CHANNEL_NAME];
03840    int res;
03841    int diff;
03842    int hasfeatures=0;
03843    int hadfeatures=0;
03844    int autoloopflag;
03845    int sendingdtmfdigit = 0;
03846    int we_disabled_peer_cdr = 0;
03847    struct ast_option_header *aoh;
03848    struct ast_cdr *bridge_cdr = NULL;
03849    struct ast_cdr *chan_cdr = chan->cdr; /* the proper chan cdr, if there are forked cdrs */
03850    struct ast_cdr *peer_cdr = peer->cdr; /* the proper chan cdr, if there are forked cdrs */
03851    struct ast_cdr *new_chan_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
03852    struct ast_cdr *new_peer_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
03853    struct ast_silence_generator *silgen = NULL;
03854    const char *h_context;
03855 
03856    pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
03857    pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
03858 
03859    /* Clear any BLINDTRANSFER since the transfer has completed. */
03860    pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
03861    pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", NULL);
03862 
03863    set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
03864    add_features_datastores(chan, peer, config);
03865 
03866    /* This is an interesting case.  One example is if a ringing channel gets redirected to
03867     * an extension that picks up a parked call.  This will make sure that the call taken
03868     * out of parking gets told that the channel it just got bridged to is still ringing. */
03869    if (chan->_state == AST_STATE_RINGING && peer->visible_indication != AST_CONTROL_RINGING) {
03870       ast_indicate(peer, AST_CONTROL_RINGING);
03871    }
03872 
03873    if (monitor_ok) {
03874       const char *monitor_exec;
03875       struct ast_channel *src = NULL;
03876       if (!monitor_app) {
03877          if (!(monitor_app = pbx_findapp("Monitor")))
03878             monitor_ok=0;
03879       }
03880       if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR"))) 
03881          src = chan;
03882       else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
03883          src = peer;
03884       if (monitor_app && src) {
03885          char *tmp = ast_strdupa(monitor_exec);
03886          pbx_exec(src, monitor_app, tmp);
03887       }
03888    }
03889 
03890    set_config_flags(chan, peer, config);
03891 
03892    /* Answer if need be */
03893    if (chan->_state != AST_STATE_UP) {
03894       if (ast_raw_answer(chan, 1)) {
03895          return -1;
03896       }
03897    }
03898 
03899 #ifdef FOR_DEBUG
03900    /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
03901    ast_channel_log("Pre-bridge CHAN Channel info", chan);
03902    ast_channel_log("Pre-bridge PEER Channel info", peer);
03903 #endif
03904    /* two channels are being marked as linked here */
03905    ast_channel_set_linkgroup(chan,peer);
03906 
03907    /* copy the userfield from the B-leg to A-leg if applicable */
03908    if (chan->cdr && peer->cdr && !ast_strlen_zero(peer->cdr->userfield)) {
03909       char tmp[256];
03910 
03911       ast_channel_lock(chan);
03912       if (!ast_strlen_zero(chan->cdr->userfield)) {
03913          snprintf(tmp, sizeof(tmp), "%s;%s", chan->cdr->userfield, peer->cdr->userfield);
03914          ast_cdr_appenduserfield(chan, tmp);
03915       } else {
03916          ast_cdr_setuserfield(chan, peer->cdr->userfield);
03917       }
03918       ast_channel_unlock(chan);
03919       /* Don't delete the CDR; just disable it. */
03920       ast_set_flag(peer->cdr, AST_CDR_FLAG_POST_DISABLED);
03921       we_disabled_peer_cdr = 1;
03922    }
03923    ast_copy_string(orig_channame,chan->name,sizeof(orig_channame));
03924    ast_copy_string(orig_peername,peer->name,sizeof(orig_peername));
03925 
03926    if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
03927       ast_channel_lock_both(chan, peer);
03928       if (chan_cdr) {
03929          ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
03930          ast_cdr_update(chan);
03931          bridge_cdr = ast_cdr_dup_unique_swap(chan_cdr);
03932          /* rip any forked CDR's off of the chan_cdr and attach
03933           * them to the bridge_cdr instead */
03934          bridge_cdr->next = chan_cdr->next;
03935          chan_cdr->next = NULL;
03936          ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
03937          ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
03938          if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
03939             ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
03940          }
03941          ast_cdr_setaccount(peer, chan->accountcode);
03942       } else {
03943          /* better yet, in a xfer situation, find out why the chan cdr got zapped (pun unintentional) */
03944          bridge_cdr = ast_cdr_alloc(); /* this should be really, really rare/impossible? */
03945          ast_copy_string(bridge_cdr->channel, chan->name, sizeof(bridge_cdr->channel));
03946          ast_copy_string(bridge_cdr->dstchannel, peer->name, sizeof(bridge_cdr->dstchannel));
03947          ast_copy_string(bridge_cdr->uniqueid, chan->uniqueid, sizeof(bridge_cdr->uniqueid));
03948          ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
03949          ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
03950          ast_cdr_setcid(bridge_cdr, chan);
03951          bridge_cdr->disposition = (chan->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NULL;
03952          bridge_cdr->amaflags = chan->amaflags ? chan->amaflags :  ast_default_amaflags;
03953          ast_copy_string(bridge_cdr->accountcode, chan->accountcode, sizeof(bridge_cdr->accountcode));
03954          /* Destination information */
03955          ast_copy_string(bridge_cdr->dst, chan->exten, sizeof(bridge_cdr->dst));
03956          ast_copy_string(bridge_cdr->dcontext, chan->context, sizeof(bridge_cdr->dcontext));
03957          if (peer_cdr) {
03958             bridge_cdr->start = peer_cdr->start;
03959             ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
03960          } else {
03961             ast_cdr_start(bridge_cdr);
03962          }
03963       }
03964       ast_channel_unlock(chan);
03965       ast_channel_unlock(peer);
03966 
03967       ast_debug(4,"bridge answer set, chan answer set\n");
03968       /* peer_cdr->answer will be set when a macro runs on the peer;
03969          in that case, the bridge answer will be delayed while the
03970          macro plays on the peer channel. The peer answered the call
03971          before the macro started playing. To the phone system,
03972          this is billable time for the call, even tho the caller
03973          hears nothing but ringing while the macro does its thing. */
03974 
03975       /* Another case where the peer cdr's time will be set, is when
03976          A self-parks by pickup up phone and dialing 700, then B
03977          picks up A by dialing its parking slot; there may be more 
03978          practical paths that get the same result, tho... in which
03979          case you get the previous answer time from the Park... which
03980          is before the bridge's start time, so I added in the 
03981          tvcmp check to the if below */
03982 
03983       if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
03984          ast_cdr_setanswer(bridge_cdr, peer_cdr->answer);
03985          ast_cdr_setdisposition(bridge_cdr, peer_cdr->disposition);
03986          if (chan_cdr) {
03987             ast_cdr_setanswer(chan_cdr, peer_cdr->answer);
03988             ast_cdr_setdisposition(chan_cdr, peer_cdr->disposition);
03989          }
03990       } else {
03991          ast_cdr_answer(bridge_cdr);
03992          if (chan_cdr) {
03993             ast_cdr_answer(chan_cdr); /* for the sake of cli status checks */
03994          }
03995       }
03996       if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
03997          if (chan_cdr) {
03998             ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
03999          }
04000          if (peer_cdr) {
04001             ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
04002          }
04003       }
04004       /* the DIALED flag may be set if a dialed channel is transfered
04005        * and then bridged to another channel.  In order for the
04006        * bridge CDR to be written, the DIALED flag must not be
04007        * present. */
04008       ast_clear_flag(bridge_cdr, AST_CDR_FLAG_DIALED);
04009    }
04010    ast_cel_report_event(chan, AST_CEL_BRIDGE_START, NULL, NULL, peer);
04011 
04012    /* If we are bridging a call, stop worrying about forwarding loops. We presume that if
04013     * a call is being bridged, that the humans in charge know what they're doing. If they
04014     * don't, well, what can we do about that? */
04015    clear_dialed_interfaces(chan);
04016    clear_dialed_interfaces(peer);
04017 
04018    for (;;) {
04019       struct ast_channel *other; /* used later */
04020    
04021       res = ast_channel_bridge(chan, peer, config, &f, &who);
04022 
04023       if (ast_test_flag(chan, AST_FLAG_ZOMBIE)
04024          || ast_test_flag(peer, AST_FLAG_ZOMBIE)) {
04025          /* Zombies are present time to leave! */
04026          res = -1;
04027          if (f) {
04028             ast_frfree(f);
04029          }
04030          goto before_you_go;
04031       }
04032 
04033       /* When frame is not set, we are probably involved in a situation
04034          where we've timed out.
04035          When frame is set, we'll come this code twice; once for DTMF_BEGIN
04036          and also for DTMF_END. If we flow into the following 'if' for both, then 
04037          our wait times are cut in half, as both will subtract from the
04038          feature_timer. Not good!
04039       */
04040       if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
04041          /* Update feature timer for next pass */
04042          diff = ast_tvdiff_ms(ast_tvnow(), config->feature_start_time);
04043          if (res == AST_BRIDGE_RETRY) {
04044             /* The feature fully timed out but has not been updated. Skip
04045              * the potential round error from the diff calculation and
04046              * explicitly set to expired. */
04047             config->feature_timer = -1;
04048          } else {
04049             config->feature_timer -= diff;
04050          }
04051 
04052          if (hasfeatures) {
04053             if (config->feature_timer <= 0) {
04054                /* Not *really* out of time, just out of time for
04055                   digits to come in for features. */
04056                ast_debug(1, "Timed out for feature!\n");
04057                if (!ast_strlen_zero(peer_featurecode)) {
04058                   ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
04059                   memset(peer_featurecode, 0, sizeof(peer_featurecode));
04060                }
04061                if (!ast_strlen_zero(chan_featurecode)) {
04062                   ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
04063                   memset(chan_featurecode, 0, sizeof(chan_featurecode));
04064                }
04065                if (f)
04066                   ast_frfree(f);
04067                hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
04068                if (!hasfeatures) {
04069                   /* No more digits expected - reset the timer */
04070                   config->feature_timer = 0;
04071                }
04072                hadfeatures = hasfeatures;
04073                /* Continue as we were */
04074                continue;
04075             } else if (!f) {
04076                /* The bridge returned without a frame and there is a feature in progress.
04077                 * However, we don't think the feature has quite yet timed out, so just
04078                 * go back into the bridge. */
04079                continue;
04080             }
04081          } else {
04082             if (config->feature_timer <=0) {
04083                /* We ran out of time */
04084                config->feature_timer = 0;
04085                who = chan;
04086                if (f)
04087                   ast_frfree(f);
04088                f = NULL;
04089                res = 0;
04090             }
04091          }
04092       }
04093       if (res < 0) {
04094          if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer)) {
04095             ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
04096          }
04097          goto before_you_go;
04098       }
04099       
04100       if (!f || (f->frametype == AST_FRAME_CONTROL &&
04101             (f->subclass.integer == AST_CONTROL_HANGUP || f->subclass.integer == AST_CONTROL_BUSY ||
04102                f->subclass.integer == AST_CONTROL_CONGESTION))) {
04103          /*
04104           * If the bridge was broken for a hangup that isn't real,
04105           * then don't run the h extension, because the channel isn't
04106           * really hung up. This should really only happen with AST_SOFTHANGUP_ASYNCGOTO,
04107           * but it doesn't hurt to check AST_SOFTHANGUP_UNBRIDGE either.
04108           */
04109          ast_channel_lock(chan);
04110          if (chan->_softhangup & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE)) {
04111             ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
04112          }
04113          ast_channel_unlock(chan);
04114          res = -1;
04115          break;
04116       }
04117       /* many things should be sent to the 'other' channel */
04118       other = (who == chan) ? peer : chan;
04119       if (f->frametype == AST_FRAME_CONTROL) {
04120          switch (f->subclass.integer) {
04121          case AST_CONTROL_RINGING:
04122          case AST_CONTROL_FLASH:
04123          case -1:
04124             ast_indicate(other, f->subclass.integer);
04125             break;
04126          case AST_CONTROL_CONNECTED_LINE:
04127             if (!ast_channel_connected_line_macro(who, other, f, who != chan, 1)) {
04128                break;
04129             }
04130             ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
04131             break;
04132          case AST_CONTROL_REDIRECTING:
04133             if (!ast_channel_redirecting_macro(who, other, f, who != chan, 1)) {
04134                break;
04135             }
04136             ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
04137             break;
04138          case AST_CONTROL_AOC:
04139          case AST_CONTROL_HOLD:
04140          case AST_CONTROL_UNHOLD:
04141             ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
04142             break;
04143          case AST_CONTROL_OPTION:
04144             aoh = f->data.ptr;
04145             /* Forward option Requests, but only ones we know are safe
04146              * These are ONLY sent by chan_iax2 and I'm not convinced that
04147              * they are useful. I haven't deleted them entirely because I
04148              * just am not sure of the ramifications of removing them. */
04149             if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
04150                   switch (ntohs(aoh->option)) {
04151                case AST_OPTION_TONE_VERIFY:
04152                case AST_OPTION_TDD:
04153                case AST_OPTION_RELAXDTMF:
04154                case AST_OPTION_AUDIO_MODE:
04155                case AST_OPTION_DIGIT_DETECT:
04156                case AST_OPTION_FAX_DETECT:
04157                   ast_channel_setoption(other, ntohs(aoh->option), aoh->data, 
04158                      f->datalen - sizeof(struct ast_option_header), 0);
04159                }
04160             }
04161             break;
04162          }
04163       } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
04164          struct ast_flags *cfg;
04165          char dtmfcode[2] = { f->subclass.integer, };
04166          size_t featurelen;
04167 
04168          if (who == chan) {
04169             featurelen = strlen(chan_featurecode);
04170             cfg = &(config->features_caller);
04171          } else {
04172             featurelen = strlen(peer_featurecode);
04173             cfg = &(config->features_callee);
04174          }
04175          /* Take a peek if this (possibly) matches a feature. If not, just pass this
04176           * DTMF along untouched. If this is not the first digit of a multi-digit code
04177           * then we need to fall through and stream the characters if it matches */
04178          if (featurelen == 0
04179             && feature_check(chan, cfg, &dtmfcode[0]) == AST_FEATURE_RETURN_PASSDIGITS) {
04180             if (option_debug > 3) {
04181                ast_log(LOG_DEBUG, "Passing DTMF through, since it is not a feature code\n");
04182             }
04183             ast_write(other, f);
04184             sendingdtmfdigit = 1;
04185          } else {
04186             /* If ast_opt_transmit_silence is set, then we need to make sure we are
04187              * transmitting something while we hold on to the DTMF waiting for a
04188              * feature. */
04189             if (!silgen && ast_opt_transmit_silence) {
04190                silgen = ast_channel_start_silence_generator(other);
04191             }
04192             if (option_debug > 3) {
04193                ast_log(LOG_DEBUG, "Not passing DTMF through, since it may be a feature code\n");
04194             }
04195          }
04196       } else if (f->frametype == AST_FRAME_DTMF_END) {
04197          char *featurecode;
04198          int sense;
04199 
04200          hadfeatures = hasfeatures;
04201          /* This cannot overrun because the longest feature is one shorter than our buffer */
04202          if (who == chan) {
04203             sense = FEATURE_SENSE_CHAN;
04204             featurecode = chan_featurecode;
04205          } else  {
04206             sense = FEATURE_SENSE_PEER;
04207             featurecode = peer_featurecode;
04208          }
04209 
04210          if (sendingdtmfdigit == 1) {
04211             /* We let the BEGIN go through happily, so let's not bother with the END,
04212              * since we already know it's not something we bother with */
04213             ast_write(other, f);
04214             sendingdtmfdigit = 0;
04215          } else {
04216             /*! append the event to featurecode. we rely on the string being zero-filled, and
04217              * not overflowing it. 
04218              * \todo XXX how do we guarantee the latter ?
04219              */
04220             featurecode[strlen(featurecode)] = f->subclass.integer;
04221             /* Get rid of the frame before we start doing "stuff" with the channels */
04222             ast_frfree(f);
04223             f = NULL;
04224             if (silgen) {
04225                ast_channel_stop_silence_generator(other, silgen);
04226                silgen = NULL;
04227             }
04228             config->feature_timer = 0;
04229             res = feature_interpret(chan, peer, config, featurecode, sense);
04230             switch(res) {
04231             case AST_FEATURE_RETURN_PASSDIGITS:
04232                ast_dtmf_stream(other, who, featurecode, 0, 0);
04233                /* Fall through */
04234             case AST_FEATURE_RETURN_SUCCESS:
04235                memset(featurecode, 0, sizeof(chan_featurecode));
04236                break;
04237             }
04238             if (res >= AST_FEATURE_RETURN_PASSDIGITS) {
04239                res = 0;
04240             } else {
04241                break;
04242             }
04243             hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
04244             if (hadfeatures && !hasfeatures) {
04245                /* Feature completed or timed out */
04246                config->feature_timer = 0;
04247             } else if (hasfeatures) {
04248                if (config->timelimit) {
04249                   /* No warning next time - we are waiting for feature code */
04250                   ast_set_flag(config, AST_FEATURE_WARNING_ACTIVE);
04251                }
04252                config->feature_start_time = ast_tvnow();
04253                config->feature_timer = featuredigittimeout;
04254                ast_debug(1, "Set feature timer to %ld ms\n", config->feature_timer);
04255             }
04256          }
04257       }
04258       if (f)
04259          ast_frfree(f);
04260    }
04261    ast_cel_report_event(chan, AST_CEL_BRIDGE_END, NULL, NULL, peer);
04262 
04263 before_you_go:
04264    /* Just in case something weird happened and we didn't clean up the silence generator... */
04265    if (silgen) {
04266       ast_channel_stop_silence_generator(who == chan ? peer : chan, silgen);
04267       silgen = NULL;
04268    }
04269 
04270    if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT)) {
04271       ast_clear_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT); /* its job is done */
04272       if (bridge_cdr) {
04273          ast_cdr_discard(bridge_cdr);
04274          /* QUESTION: should we copy bridge_cdr fields to the peer before we throw it away? */
04275       }
04276       return res; /* if we shouldn't do the h-exten, we shouldn't do the bridge cdr, either! */
04277    }
04278 
04279    if (config->end_bridge_callback) {
04280       config->end_bridge_callback(config->end_bridge_callback_data);
04281    }
04282 
04283    /* run the hangup exten on the chan object IFF it was NOT involved in a parking situation 
04284     * if it were, then chan belongs to a different thread now, and might have been hung up long
04285      * ago.
04286     */
04287    if (ast_test_flag(&config->features_caller, AST_FEATURE_NO_H_EXTEN)) {
04288       h_context = NULL;
04289    } else if (ast_exists_extension(chan, chan->context, "h", 1,
04290       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
04291       h_context = chan->context;
04292    } else if (!ast_strlen_zero(chan->macrocontext)
04293       && ast_exists_extension(chan, chan->macrocontext, "h", 1,
04294          S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
04295       h_context = chan->macrocontext;
04296    } else {
04297       h_context = NULL;
04298    }
04299    if (h_context) {
04300       struct ast_cdr *swapper = NULL;
04301       char savelastapp[AST_MAX_EXTENSION];
04302       char savelastdata[AST_MAX_EXTENSION];
04303       char save_context[AST_MAX_CONTEXT];
04304       char save_exten[AST_MAX_EXTENSION];
04305       int  save_prio;
04306       int  found = 0;   /* set if we find at least one match */
04307       int  spawn_error = 0;
04308 
04309       /*
04310        * Make sure that the channel is marked as hungup since we are
04311        * going to run the "h" exten on it.
04312        */
04313       ast_softhangup(chan, AST_SOFTHANGUP_APPUNLOAD);
04314 
04315       autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
04316       ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
04317       if (bridge_cdr && ast_opt_end_cdr_before_h_exten) {
04318          ast_cdr_end(bridge_cdr);
04319       }
04320 
04321       /* swap the bridge cdr and the chan cdr for a moment, and let the endbridge
04322          dialplan code operate on it */
04323       ast_channel_lock(chan);
04324       if (bridge_cdr) {
04325          swapper = chan->cdr;
04326          ast_copy_string(savelastapp, bridge_cdr->lastapp, sizeof(bridge_cdr->lastapp));
04327          ast_copy_string(savelastdata, bridge_cdr->lastdata, sizeof(bridge_cdr->lastdata));
04328          chan->cdr = bridge_cdr;
04329       }
04330       ast_copy_string(save_context, chan->context, sizeof(save_context));
04331       ast_copy_string(save_exten, chan->exten, sizeof(save_exten));
04332       save_prio = chan->priority;
04333       if (h_context != chan->context) {
04334          ast_copy_string(chan->context, h_context, sizeof(chan->context));
04335       }
04336       ast_copy_string(chan->exten, "h", sizeof(chan->exten));
04337       chan->priority = 1;
04338       ast_channel_unlock(chan);
04339 
04340       while ((spawn_error = ast_spawn_extension(chan, chan->context, chan->exten,
04341          chan->priority,
04342          S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
04343          &found, 1)) == 0) {
04344          chan->priority++;
04345       }
04346       if (found && spawn_error) {
04347          /* Something bad happened, or a hangup has been requested. */
04348          ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
04349          ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
04350       }
04351 
04352       /* swap it back */
04353       ast_channel_lock(chan);
04354       ast_copy_string(chan->context, save_context, sizeof(chan->context));
04355       ast_copy_string(chan->exten, save_exten, sizeof(chan->exten));
04356       chan->priority = save_prio;
04357       if (bridge_cdr) {
04358          if (chan->cdr == bridge_cdr) {
04359             chan->cdr = swapper;
04360          } else {
04361             bridge_cdr = NULL;
04362          }
04363       }
04364       /* An "h" exten has been run, so indicate that one has been run. */
04365       ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN);
04366       ast_channel_unlock(chan);
04367 
04368       /* protect the lastapp/lastdata against the effects of the hangup/dialplan code */
04369       if (bridge_cdr) {
04370          ast_copy_string(bridge_cdr->lastapp, savelastapp, sizeof(bridge_cdr->lastapp));
04371          ast_copy_string(bridge_cdr->lastdata, savelastdata, sizeof(bridge_cdr->lastdata));
04372       }
04373       ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
04374    }
04375    
04376    /* obey the NoCDR() wishes. -- move the DISABLED flag to the bridge CDR if it was set on the channel during the bridge... */
04377    new_chan_cdr = pick_unlocked_cdr(chan->cdr); /* the proper chan cdr, if there are forked cdrs */
04378    /* If the channel CDR has been modified during the call, record the changes in the bridge cdr */
04379    if (new_chan_cdr && bridge_cdr) {
04380       ast_cdr_copy_vars(bridge_cdr, new_chan_cdr);
04381       ast_copy_string(bridge_cdr->userfield, new_chan_cdr->userfield, sizeof(bridge_cdr->userfield));
04382       bridge_cdr->amaflags = new_chan_cdr->amaflags;
04383       ast_copy_string(bridge_cdr->accountcode, new_chan_cdr->accountcode, sizeof(bridge_cdr->accountcode));
04384       if (ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED)) {
04385          ast_set_flag(bridge_cdr, AST_CDR_FLAG_POST_DISABLED);
04386       }
04387    }
04388 
04389    /* we can post the bridge CDR at this point */
04390    if (bridge_cdr) {
04391       ast_cdr_end(bridge_cdr);
04392       ast_cdr_detach(bridge_cdr);
04393    }
04394    
04395    /* do a specialized reset on the beginning channel
04396       CDR's, if they still exist, so as not to mess up
04397       issues in future bridges;
04398       
04399       Here are the rules of the game:
04400       1. The chan and peer channel pointers will not change
04401          during the life of the bridge.
04402       2. But, in transfers, the channel names will change.
04403          between the time the bridge is started, and the
04404          time the channel ends. 
04405          Usually, when a channel changes names, it will
04406          also change CDR pointers.
04407       3. Usually, only one of the two channels (chan or peer)
04408          will change names.
04409       4. Usually, if a channel changes names during a bridge,
04410          it is because of a transfer. Usually, in these situations,
04411          it is normal to see 2 bridges running simultaneously, and
04412          it is not unusual to see the two channels that change
04413          swapped between bridges.
04414       5. After a bridge occurs, we have 2 or 3 channels' CDRs
04415          to attend to; if the chan or peer changed names,
04416          we have the before and after attached CDR's.
04417    */
04418 
04419    if (new_chan_cdr) {
04420       struct ast_channel *chan_ptr = NULL;
04421 
04422       if (strcasecmp(orig_channame, chan->name) != 0) { 
04423          /* old channel */
04424          if ((chan_ptr = ast_channel_get_by_name(orig_channame))) {
04425             ast_channel_lock(chan_ptr);
04426             if (!ast_bridged_channel(chan_ptr)) {
04427                struct ast_cdr *cur;
04428                for (cur = chan_ptr->cdr; cur; cur = cur->next) {
04429                   if (cur == chan_cdr) {
04430                      break;
04431                   }
04432                }
04433                if (cur) {
04434                   ast_cdr_specialized_reset(chan_cdr, 0);
04435                }
04436             }
04437             ast_channel_unlock(chan_ptr);
04438             chan_ptr = ast_channel_unref(chan_ptr);
04439          }
04440          /* new channel */
04441          ast_cdr_specialized_reset(new_chan_cdr, 0);
04442       } else {
04443          ast_cdr_specialized_reset(chan->cdr, 0); /* nothing changed, reset the chan cdr  */
04444       }
04445    }
04446 
04447    {
04448       struct ast_channel *chan_ptr = NULL;
04449       new_peer_cdr = pick_unlocked_cdr(peer->cdr); /* the proper chan cdr, if there are forked cdrs */
04450       if (new_chan_cdr && ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED) && new_peer_cdr && !ast_test_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED))
04451          ast_set_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED); /* DISABLED is viral-- it will propagate across a bridge */
04452       if (strcasecmp(orig_peername, peer->name) != 0) { 
04453          /* old channel */
04454          if ((chan_ptr = ast_channel_get_by_name(orig_peername))) {
04455             ast_channel_lock(chan_ptr);
04456             if (!ast_bridged_channel(chan_ptr)) {
04457                struct ast_cdr *cur;
04458                for (cur = chan_ptr->cdr; cur; cur = cur->next) {
04459                   if (cur == peer_cdr) {
04460                      break;
04461                   }
04462                }
04463                if (cur) {
04464                   ast_cdr_specialized_reset(peer_cdr, 0);
04465                }
04466             }
04467             ast_channel_unlock(chan_ptr);
04468             chan_ptr = ast_channel_unref(chan_ptr);
04469          }
04470          /* new channel */
04471          if (new_peer_cdr) {
04472             ast_cdr_specialized_reset(new_peer_cdr, 0);
04473          }
04474       } else {
04475          if (we_disabled_peer_cdr) {
04476             ast_clear_flag(peer->cdr, AST_CDR_FLAG_POST_DISABLED);
04477          }
04478          ast_cdr_specialized_reset(peer->cdr, 0); /* nothing changed, reset the peer cdr  */
04479       }
04480    }
04481    
04482    return res;
04483 }
04484 
04485 /*! \brief Output parking event to manager */
04486 static void post_manager_event(const char *s, struct parkeduser *pu)
04487 {
04488    manager_event(EVENT_FLAG_CALL, s,
04489       "Exten: %s\r\n"
04490       "Channel: %s\r\n"
04491       "Parkinglot: %s\r\n"
04492       "CallerIDNum: %s\r\n"
04493       "CallerIDName: %s\r\n"
04494       "ConnectedLineNum: %s\r\n"
04495       "ConnectedLineName: %s\r\n"
04496       "UniqueID: %s\r\n",
04497       pu->parkingexten, 
04498       pu->chan->name,
04499       pu->parkinglot->name,
04500       S_COR(pu->chan->caller.id.number.valid, pu->chan->caller.id.number.str, "<unknown>"),
04501       S_COR(pu->chan->caller.id.name.valid, pu->chan->caller.id.name.str, "<unknown>"),
04502       S_COR(pu->chan->connected.id.number.valid, pu->chan->connected.id.number.str, "<unknown>"),
04503       S_COR(pu->chan->connected.id.name.valid, pu->chan->connected.id.name.str, "<unknown>"),
04504       pu->chan->uniqueid
04505       );
04506 }
04507 
04508 static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_flags *features_caller, char *options, size_t len)
04509 {
04510    int i = 0;
04511    enum {
04512       OPT_CALLEE_REDIRECT   = 't',
04513       OPT_CALLER_REDIRECT   = 'T',
04514       OPT_CALLEE_AUTOMON    = 'w',
04515       OPT_CALLER_AUTOMON    = 'W',
04516       OPT_CALLEE_DISCONNECT = 'h',
04517       OPT_CALLER_DISCONNECT = 'H',
04518       OPT_CALLEE_PARKCALL   = 'k',
04519       OPT_CALLER_PARKCALL   = 'K',
04520    };
04521 
04522    memset(options, 0, len);
04523    if (ast_test_flag(features_caller, AST_FEATURE_REDIRECT) && i < len) {
04524       options[i++] = OPT_CALLER_REDIRECT;
04525    }
04526    if (ast_test_flag(features_caller, AST_FEATURE_AUTOMON) && i < len) {
04527       options[i++] = OPT_CALLER_AUTOMON;
04528    }
04529    if (ast_test_flag(features_caller, AST_FEATURE_DISCONNECT) && i < len) {
04530       options[i++] = OPT_CALLER_DISCONNECT;
04531    }
04532    if (ast_test_flag(features_caller, AST_FEATURE_PARKCALL) && i < len) {
04533       options[i++] = OPT_CALLER_PARKCALL;
04534    }
04535 
04536    if (ast_test_flag(features_callee, AST_FEATURE_REDIRECT) && i < len) {
04537       options[i++] = OPT_CALLEE_REDIRECT;
04538    }
04539    if (ast_test_flag(features_callee, AST_FEATURE_AUTOMON) && i < len) {
04540       options[i++] = OPT_CALLEE_AUTOMON;
04541    }
04542    if (ast_test_flag(features_callee, AST_FEATURE_DISCONNECT) && i < len) {
04543       options[i++] = OPT_CALLEE_DISCONNECT;
04544    }
04545    if (ast_test_flag(features_callee, AST_FEATURE_PARKCALL) && i < len) {
04546       options[i++] = OPT_CALLEE_PARKCALL;
04547    }
04548 
04549    return options;
04550 }
04551 
04552 /*!
04553  * \internal
04554  * \brief Run management on a parked call.
04555  *
04556  * \note The parkinglot parkings list is locked on entry.
04557  *
04558  * \retval TRUE if the parking completed.
04559  */
04560 static int manage_parked_call(struct parkeduser *pu, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
04561 {
04562    struct ast_channel *chan = pu->chan;   /* shorthand */
04563    int tms;        /* timeout for this item */
04564    int x;          /* fd index in channel */
04565    int parking_complete = 0;
04566 
04567    tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
04568    if (tms > pu->parkingtime) {
04569       /*
04570        * Call has been parked too long.
04571        * Stop entertaining the caller.
04572        */
04573       switch (pu->hold_method) {
04574       case AST_CONTROL_HOLD:
04575          ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
04576          break;
04577       case AST_CONTROL_RINGING:
04578          ast_indicate(pu->chan, -1);
04579          break;
04580       default:
04581          break;
04582       }
04583       pu->hold_method = 0;
04584 
04585       /* Get chan, exten from derived kludge */
04586       if (pu->peername[0]) {
04587          char *peername;
04588          char *dash;
04589          char *peername_flat; /* using something like DAHDI/52 for an extension name is NOT a good idea */
04590          int i;
04591 
04592          peername = ast_strdupa(pu->peername);
04593          dash = strrchr(peername, '-');
04594          if (dash) {
04595             *dash = '\0';
04596          }
04597 
04598          peername_flat = ast_strdupa(peername);
04599          for (i = 0; peername_flat[i]; i++) {
04600             if (peername_flat[i] == '/') {
04601                peername_flat[i] = '_';
04602             }
04603          }
04604 
04605          if (!ast_context_find_or_create(NULL, NULL, parking_con_dial, registrar)) {
04606             ast_log(LOG_ERROR,
04607                "Parking dial context '%s' does not exist and unable to create\n",
04608                parking_con_dial);
04609          } else {
04610             char returnexten[AST_MAX_EXTENSION];
04611             struct ast_datastore *features_datastore;
04612             struct ast_dial_features *dialfeatures;
04613 
04614             if (!strncmp(peername, "Parked/", 7)) {
04615                peername += 7;
04616             }
04617 
04618             ast_channel_lock(chan);
04619             features_datastore = ast_channel_datastore_find(chan, &dial_features_info,
04620                NULL);
04621             if (features_datastore && (dialfeatures = features_datastore->data)) {
04622                char buf[MAX_DIAL_FEATURE_OPTIONS] = {0,};
04623 
04624                snprintf(returnexten, sizeof(returnexten), "%s,30,%s", peername,
04625                   callback_dialoptions(&(dialfeatures->features_callee),
04626                      &(dialfeatures->features_caller), buf, sizeof(buf)));
04627             } else { /* Existing default */
04628                ast_log(LOG_NOTICE, "Dial features not found on %s, using default!\n",
04629                   chan->name);
04630                snprintf(returnexten, sizeof(returnexten), "%s,30,t", peername);
04631             }
04632             ast_channel_unlock(chan);
04633 
04634             if (ast_add_extension(parking_con_dial, 1, peername_flat, 1, NULL, NULL,
04635                "Dial", ast_strdup(returnexten), ast_free_ptr, registrar)) {
04636                ast_log(LOG_ERROR,
04637                   "Could not create parking return dial exten: %s@%s\n",
04638                   peername_flat, parking_con_dial);
04639             }
04640          }
04641          if (pu->options_specified) {
04642             /*
04643              * Park() was called with overriding return arguments, respect
04644              * those arguments.
04645              */
04646             set_c_e_p(chan, pu->context, pu->exten, pu->priority);
04647          } else if (comebacktoorigin) {
04648             set_c_e_p(chan, parking_con_dial, peername_flat, 1);
04649          } else {
04650             char parkingslot[AST_MAX_EXTENSION];
04651 
04652             snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
04653             pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parkingslot);
04654             set_c_e_p(chan, "parkedcallstimeout", peername_flat, 1);
04655          }
04656       } else {
04657          /*
04658           * They've been waiting too long, send them back to where they
04659           * came.  Theoretically they should have their original
04660           * extensions and such, but we copy to be on the safe side.
04661           */
04662          set_c_e_p(chan, pu->context, pu->exten, pu->priority);
04663       }
04664       post_manager_event("ParkedCallTimeOut", pu);
04665       ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "ParkedCallTimeOut", NULL);
04666 
04667       ast_verb(2, "Timeout for %s parked on %d (%s). Returning to %s,%s,%d\n",
04668          pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->chan->context,
04669          pu->chan->exten, pu->chan->priority);
04670 
04671       /* Start up the PBX, or hang them up */
04672       if (ast_pbx_start(chan))  {
04673          ast_log(LOG_WARNING,
04674             "Unable to restart the PBX for user on '%s', hanging them up...\n",
04675             pu->chan->name);
04676          ast_hangup(chan);
04677       }
04678 
04679       /* And take them out of the parking lot */
04680       parking_complete = 1;
04681    } else { /* still within parking time, process descriptors */
04682       for (x = 0; x < AST_MAX_FDS; x++) {
04683          struct ast_frame *f;
04684          int y;
04685 
04686          if (chan->fds[x] == -1) {
04687             continue;   /* nothing on this descriptor */
04688          }
04689 
04690          for (y = 0; y < nfds; y++) {
04691             if (pfds[y].fd == chan->fds[x]) {
04692                /* Found poll record! */
04693                break;
04694             }
04695          }
04696          if (y == nfds) {
04697             /* Not found */
04698             continue;
04699          }
04700 
04701          if (!(pfds[y].revents & (POLLIN | POLLERR | POLLPRI))) {
04702             /* Next x */
04703             continue;
04704          }
04705 
04706          if (pfds[y].revents & POLLPRI) {
04707             ast_set_flag(chan, AST_FLAG_EXCEPTION);
04708          } else {
04709             ast_clear_flag(chan, AST_FLAG_EXCEPTION);
04710          }
04711          chan->fdno = x;
04712 
04713          /* See if they need servicing */
04714          f = ast_read(pu->chan);
04715          /* Hangup? */
04716          if (!f || (f->frametype == AST_FRAME_CONTROL
04717             && f->subclass.integer == AST_CONTROL_HANGUP)) {
04718             if (f) {
04719                ast_frfree(f);
04720             }
04721             post_manager_event("ParkedCallGiveUp", pu);
04722             ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "ParkedCallGiveUp",
04723                NULL);
04724 
04725             /* There's a problem, hang them up */
04726             ast_verb(2, "%s got tired of being parked\n", chan->name);
04727             ast_hangup(chan);
04728 
04729             /* And take them out of the parking lot */
04730             parking_complete = 1;
04731             break;
04732          } else {
04733             /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
04734             ast_frfree(f);
04735             if (pu->hold_method == AST_CONTROL_HOLD
04736                && pu->moh_trys < 3
04737                && !chan->generatordata) {
04738                ast_debug(1,
04739                   "MOH on parked call stopped by outside source.  Restarting on channel %s.\n",
04740                   chan->name);
04741                ast_indicate_data(chan, AST_CONTROL_HOLD,
04742                   S_OR(pu->parkinglot->cfg.mohclass, NULL),
04743                   (!ast_strlen_zero(pu->parkinglot->cfg.mohclass)
04744                      ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0));
04745                pu->moh_trys++;
04746             }
04747             goto std;   /* XXX Ick: jumping into an else statement??? XXX */
04748          }
04749       } /* End for */
04750       if (x >= AST_MAX_FDS) {
04751 std:     for (x = 0; x < AST_MAX_FDS; x++) { /* mark fds for next round */
04752             if (chan->fds[x] > -1) {
04753                void *tmp = ast_realloc(*new_pfds,
04754                   (*new_nfds + 1) * sizeof(struct pollfd));
04755 
04756                if (!tmp) {
04757                   continue;
04758                }
04759                *new_pfds = tmp;
04760                (*new_pfds)[*new_nfds].fd = chan->fds[x];
04761                (*new_pfds)[*new_nfds].events = POLLIN | POLLERR | POLLPRI;
04762                (*new_pfds)[*new_nfds].revents = 0;
04763                (*new_nfds)++;
04764             }
04765          }
04766          /* Keep track of our shortest wait */
04767          if (tms < *ms || *ms < 0) {
04768             *ms = tms;
04769          }
04770       }
04771    }
04772 
04773    return parking_complete;
04774 }
04775 
04776 /*! \brief Run management on parkinglots, called once per parkinglot */
04777 static void manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
04778 {
04779    struct parkeduser *pu;
04780    struct ast_context *con;
04781 
04782    /* Lock parkings list */
04783    AST_LIST_LOCK(&curlot->parkings);
04784    AST_LIST_TRAVERSE_SAFE_BEGIN(&curlot->parkings, pu, list) {
04785       if (pu->notquiteyet) { /* Pretend this one isn't here yet */
04786          continue;
04787       }
04788       if (manage_parked_call(pu, pfds, nfds, new_pfds, new_nfds, ms)) {
04789          /* Parking is complete for this call so remove it from the parking lot. */
04790          con = ast_context_find(pu->parkinglot->cfg.parking_con);
04791          if (con) {
04792             if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
04793                ast_log(LOG_WARNING,
04794                   "Whoa, failed to remove the parking extension %s@%s!\n",
04795                   pu->parkingexten, pu->parkinglot->cfg.parking_con);
04796             }
04797             notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con,
04798                AST_DEVICE_NOT_INUSE);
04799          } else {
04800             ast_log(LOG_WARNING,
04801                "Whoa, parking lot '%s' context '%s' does not exist.\n",
04802                pu->parkinglot->name, pu->parkinglot->cfg.parking_con);
04803          }
04804          AST_LIST_REMOVE_CURRENT(list);
04805          parkinglot_unref(pu->parkinglot);
04806          ast_free(pu);
04807       }
04808    }
04809    AST_LIST_TRAVERSE_SAFE_END;
04810    AST_LIST_UNLOCK(&curlot->parkings);
04811 }
04812 
04813 /*! 
04814  * \brief Take care of parked calls and unpark them if needed 
04815  * \param ignore unused var.
04816  * 
04817  * Start inf loop, lock parking lot, check if any parked channels have gone above timeout
04818  * if so, remove channel from parking lot and return it to the extension that parked it.
04819  * Check if parked channel decided to hangup, wait until next FD via select().
04820  */
04821 static void *do_parking_thread(void *ignore)
04822 {
04823    struct pollfd *pfds = NULL, *new_pfds = NULL;
04824    int nfds = 0, new_nfds = 0;
04825 
04826    for (;;) {
04827       struct ao2_iterator iter;
04828       struct ast_parkinglot *curlot;
04829       int ms = -1;   /* poll2 timeout, uninitialized */
04830 
04831       iter = ao2_iterator_init(parkinglots, 0);
04832       while ((curlot = ao2_iterator_next(&iter))) {
04833          manage_parkinglot(curlot, pfds, nfds, &new_pfds, &new_nfds, &ms);
04834          ao2_ref(curlot, -1);
04835       }
04836       ao2_iterator_destroy(&iter);
04837 
04838       /* Recycle */
04839       ast_free(pfds);
04840       pfds = new_pfds;
04841       nfds = new_nfds;
04842       new_pfds = NULL;
04843       new_nfds = 0;
04844 
04845       /* Wait for something to happen */
04846       ast_poll(pfds, nfds, ms);
04847       pthread_testcancel();
04848    }
04849    /* If this WERE reached, we'd need to free(pfds) */
04850    return NULL;   /* Never reached */
04851 }
04852 
04853 /*! \brief Find parkinglot by name */
04854 static struct ast_parkinglot *find_parkinglot(const char *name)
04855 {
04856    struct ast_parkinglot *parkinglot;
04857 
04858    if (ast_strlen_zero(name)) {
04859       return NULL;
04860    }
04861 
04862    parkinglot = ao2_find(parkinglots, (void *) name, 0);
04863    if (parkinglot) {
04864       ast_debug(1, "Found Parking lot: %s\n", parkinglot->name);
04865    }
04866 
04867    return parkinglot;
04868 }
04869 
04870 /*! \brief Copy parkinglot and store it with new name */
04871 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot)
04872 {
04873    struct ast_parkinglot *copylot;
04874 
04875    if ((copylot = find_parkinglot(name))) { /* Parkinglot with that name already exists */
04876       ao2_ref(copylot, -1);
04877       return NULL;
04878    }
04879 
04880    copylot = create_parkinglot(name);
04881    if (!copylot) {
04882       return NULL;
04883    }
04884 
04885    ast_debug(1, "Building parking lot %s\n", name);
04886 
04887    /* Copy the source parking lot configuration. */
04888    copylot->cfg = parkinglot->cfg;
04889 
04890    return copylot;
04891 }
04892 
04893 AST_APP_OPTIONS(park_call_options, BEGIN_OPTIONS
04894    AST_APP_OPTION('r', AST_PARK_OPT_RINGING),
04895    AST_APP_OPTION('R', AST_PARK_OPT_RANDOMIZE),
04896    AST_APP_OPTION('s', AST_PARK_OPT_SILENCE),
04897 END_OPTIONS );
04898 
04899 /*! \brief Park a call */
04900 static int park_call_exec(struct ast_channel *chan, const char *data)
04901 {
04902    struct ast_park_call_args args = { 0, };
04903    struct ast_flags flags = { 0 };
04904    char orig_exten[AST_MAX_EXTENSION];
04905    int orig_priority;
04906    int res;
04907    const char *pl_name;
04908    char *parse;
04909    struct park_app_args app_args;
04910 
04911    /*
04912     * Cache the original channel name because we are going to
04913     * masquerade the channel.  Prefer the BLINDTRANSFER channel
04914     * name over this channel name.  BLINDTRANSFER could be set if
04915     * the parking access extension did not get detected and we are
04916     * executing the Park application from the dialplan.
04917     *
04918     * The orig_chan_name is used to return the call to the
04919     * originator on parking timeout.
04920     */
04921    args.orig_chan_name = ast_strdupa(S_OR(
04922       pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"), chan->name));
04923 
04924    /* Answer if call is not up */
04925    if (chan->_state != AST_STATE_UP) {
04926       if (ast_answer(chan)) {
04927          return -1;
04928       }
04929 
04930       /* Sleep to allow VoIP streams to settle down */
04931       if (ast_safe_sleep(chan, 1000)) {
04932          return -1;
04933       }
04934    }
04935 
04936    /* Process the dialplan application options. */
04937    parse = ast_strdupa(data);
04938    AST_STANDARD_APP_ARGS(app_args, parse);
04939 
04940    if (!ast_strlen_zero(app_args.timeout)) {
04941       if (sscanf(app_args.timeout, "%30d", &args.timeout) != 1) {
04942          ast_log(LOG_WARNING, "Invalid timeout '%s' provided\n", app_args.timeout);
04943          args.timeout = 0;
04944       }
04945    }
04946    if (!ast_strlen_zero(app_args.return_con)) {
04947       args.return_con = app_args.return_con;
04948    }
04949    if (!ast_strlen_zero(app_args.return_ext)) {
04950       args.return_ext = app_args.return_ext;
04951    }
04952    if (!ast_strlen_zero(app_args.return_pri)) {
04953       if (sscanf(app_args.return_pri, "%30d", &args.return_pri) != 1) {
04954          ast_log(LOG_WARNING, "Invalid priority '%s' specified\n", app_args.return_pri);
04955          args.return_pri = 0;
04956       }
04957    }
04958 
04959    ast_app_parse_options(park_call_options, &flags, NULL, app_args.options);
04960    args.flags = flags.flags;
04961 
04962    /*
04963     * Setup the exten/priority to be s/1 since we don't know where
04964     * this call should return.
04965     */
04966    ast_copy_string(orig_exten, chan->exten, sizeof(orig_exten));
04967    orig_priority = chan->priority;
04968    strcpy(chan->exten, "s");
04969    chan->priority = 1;
04970 
04971    /* Park the call */
04972    if (!ast_strlen_zero(app_args.pl_name)) {
04973       pl_name = app_args.pl_name;
04974    } else {
04975       pl_name = findparkinglotname(chan);
04976    }
04977    if (ast_strlen_zero(pl_name)) {
04978       /* Parking lot is not specified, so use the default parking lot. */
04979       args.parkinglot = parkinglot_addref(default_parkinglot);
04980    } else {
04981       args.parkinglot = find_parkinglot(pl_name);
04982       if (!args.parkinglot && parkeddynamic) {
04983          args.parkinglot = create_dynamic_parkinglot(pl_name, chan);
04984       }
04985    }
04986    if (args.parkinglot) {
04987       res = masq_park_call(chan, chan, &args);
04988       parkinglot_unref(args.parkinglot);
04989    } else {
04990       /* Parking failed because the parking lot does not exist. */
04991       if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
04992          ast_stream_and_wait(chan, "pbx-parkingfailed", "");
04993       }
04994       res = -1;
04995    }
04996    if (res) {
04997       /* Park failed, try to continue in the dialplan. */
04998       ast_copy_string(chan->exten, orig_exten, sizeof(chan->exten));
04999       chan->priority = orig_priority;
05000       res = 0;
05001    } else {
05002       /* Park succeeded. */
05003       res = -1;
05004    }
05005 
05006    return res;
05007 }
05008 
05009 /*! \brief Pickup parked call */
05010 static int parked_call_exec(struct ast_channel *chan, const char *data)
05011 {
05012    int res = 0;
05013    struct ast_channel *peer = NULL;
05014    struct parkeduser *pu;
05015    struct ast_context *con;
05016    char *parse;
05017    const char *pl_name;
05018    int park = 0;
05019    struct ast_bridge_config config;
05020    struct ast_parkinglot *parkinglot;
05021    AST_DECLARE_APP_ARGS(app_args,
05022       AST_APP_ARG(pl_space);  /*!< Parking lot space to retrieve if present. */
05023       AST_APP_ARG(pl_name);   /*!< Parking lot name to use if present. */
05024       AST_APP_ARG(dummy);     /*!< Place to put any remaining args string. */
05025    );
05026 
05027    parse = ast_strdupa(data);
05028    AST_STANDARD_APP_ARGS(app_args, parse);
05029 
05030    if (!ast_strlen_zero(app_args.pl_space)) {
05031       if (sscanf(app_args.pl_space, "%30u", &park) != 1) {
05032          ast_log(LOG_WARNING, "Specified parking extension not a number: %s\n",
05033             app_args.pl_space);
05034          park = -1;
05035       }
05036    }
05037 
05038    if (!ast_strlen_zero(app_args.pl_name)) {
05039       pl_name = app_args.pl_name;
05040    } else {
05041       pl_name = findparkinglotname(chan);
05042    }
05043    if (ast_strlen_zero(pl_name)) {
05044       /* Parking lot is not specified, so use the default parking lot. */
05045       parkinglot = parkinglot_addref(default_parkinglot);
05046    } else {
05047       parkinglot = find_parkinglot(pl_name);
05048       if (!parkinglot) {
05049          /* It helps to answer the channel if not already up. :) */
05050          if (chan->_state != AST_STATE_UP) {
05051             ast_answer(chan);
05052          }
05053          if (ast_stream_and_wait(chan, "pbx-invalidpark", "")) {
05054             ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n",
05055                "pbx-invalidpark", chan->name);
05056          }
05057          ast_log(LOG_WARNING,
05058             "Channel %s tried to retrieve parked call from unknown parking lot '%s'\n",
05059             chan->name, pl_name);
05060          return -1;
05061       }
05062    }
05063 
05064    AST_LIST_LOCK(&parkinglot->parkings);
05065    AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot->parkings, pu, list) {
05066       if ((ast_strlen_zero(app_args.pl_space) || pu->parkingnum == park)
05067          && !pu->notquiteyet && !pu->chan->pbx) {
05068          /* The parking space has a call and can be picked up now. */
05069          AST_LIST_REMOVE_CURRENT(list);
05070          break;
05071       }
05072    }
05073    AST_LIST_TRAVERSE_SAFE_END;
05074    if (pu) {
05075       /* Found a parked call to pickup. */
05076       peer = pu->chan;
05077       con = ast_context_find(parkinglot->cfg.parking_con);
05078       if (con) {
05079          if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
05080             ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
05081          } else {
05082             notify_metermaids(pu->parkingexten, parkinglot->cfg.parking_con, AST_DEVICE_NOT_INUSE);
05083          }
05084       } else {
05085          ast_log(LOG_WARNING, "Whoa, no parking context?\n");
05086       }
05087 
05088       ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "UnParkedCall", chan);
05089       ast_manager_event(pu->chan, EVENT_FLAG_CALL, "UnParkedCall",
05090          "Exten: %s\r\n"
05091          "Channel: %s\r\n"
05092          "Parkinglot: %s\r\n"
05093          "From: %s\r\n"
05094          "CallerIDNum: %s\r\n"
05095          "CallerIDName: %s\r\n"
05096          "ConnectedLineNum: %s\r\n"
05097          "ConnectedLineName: %s\r\n"
05098          "Uniqueid: %s\r\n",
05099          pu->parkingexten, pu->chan->name, pu->parkinglot->name, chan->name,
05100          S_COR(pu->chan->caller.id.number.valid, pu->chan->caller.id.number.str, "<unknown>"),
05101          S_COR(pu->chan->caller.id.name.valid, pu->chan->caller.id.name.str, "<unknown>"),
05102          S_COR(pu->chan->connected.id.number.valid, pu->chan->connected.id.number.str, "<unknown>"),
05103          S_COR(pu->chan->connected.id.name.valid, pu->chan->connected.id.name.str, "<unknown>"),
05104          pu->chan->uniqueid
05105          );
05106 
05107       /* Stop entertaining the caller. */
05108       switch (pu->hold_method) {
05109       case AST_CONTROL_HOLD:
05110          ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
05111          break;
05112       case AST_CONTROL_RINGING:
05113          ast_indicate(pu->chan, -1);
05114          break;
05115       default:
05116          break;
05117       }
05118       pu->hold_method = 0;
05119 
05120       parkinglot_unref(pu->parkinglot);
05121       ast_free(pu);
05122    }
05123    AST_LIST_UNLOCK(&parkinglot->parkings);
05124 
05125    if (peer) {
05126       /* Update connected line between retrieving call and parked call. */
05127       struct ast_party_connected_line connected;
05128 
05129       ast_party_connected_line_init(&connected);
05130 
05131       /* Send our caller-id to peer. */
05132       ast_channel_lock(chan);
05133       ast_connected_line_copy_from_caller(&connected, &chan->caller);
05134       ast_channel_unlock(chan);
05135       connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
05136       if (ast_channel_connected_line_macro(chan, peer, &connected, 0, 0)) {
05137          ast_channel_update_connected_line(peer, &connected, NULL);
05138       }
05139 
05140       /*
05141        * Get caller-id from peer.
05142        *
05143        * Update the retrieving call before it is answered if possible
05144        * for best results.  Some phones do not support updating the
05145        * connected line information after connection.
05146        */
05147       ast_channel_lock(peer);
05148       ast_connected_line_copy_from_caller(&connected, &peer->caller);
05149       ast_channel_unlock(peer);
05150       connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
05151       if (ast_channel_connected_line_macro(peer, chan, &connected, 1, 0)) {
05152          ast_channel_update_connected_line(chan, &connected, NULL);
05153       }
05154 
05155       ast_party_connected_line_free(&connected);
05156    }
05157 
05158    /* JK02: it helps to answer the channel if not already up */
05159    if (chan->_state != AST_STATE_UP) {
05160       ast_answer(chan);
05161    }
05162 
05163    if (peer) {
05164       struct ast_datastore *features_datastore;
05165       struct ast_dial_features *dialfeatures = NULL;
05166 
05167       /* Play a courtesy to the source(s) configured to prefix the bridge connecting */
05168       if (!ast_strlen_zero(courtesytone)) {
05169          static const char msg[] = "courtesy tone";
05170 
05171          switch (parkedplay) {
05172          case 0:/* Courtesy tone to pickup chan */
05173             res = play_message_to_chans(chan, peer, -1, msg, courtesytone);
05174             break;
05175          case 1:/* Courtesy tone to parked chan */
05176             res = play_message_to_chans(chan, peer, 1, msg, courtesytone);
05177             break;
05178          case 2:/* Courtesy tone to both chans */
05179             res = play_message_to_chans(chan, peer, 0, msg, courtesytone);
05180             break;
05181          default:
05182             res = 0;
05183             break;
05184          }
05185          if (res) {
05186             ast_hangup(peer);
05187             parkinglot_unref(parkinglot);
05188             return -1;
05189          }
05190       }
05191 
05192       res = ast_channel_make_compatible(chan, peer);
05193       if (res < 0) {
05194          ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
05195          ast_hangup(peer);
05196          parkinglot_unref(parkinglot);
05197          return -1;
05198       }
05199       /* This runs sorta backwards, since we give the incoming channel control, as if it
05200          were the person called. */
05201       ast_verb(3, "Channel %s connected to parked call %d\n", chan->name, park);
05202 
05203       pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
05204       ast_cdr_setdestchan(chan->cdr, peer->name);
05205       memset(&config, 0, sizeof(struct ast_bridge_config));
05206 
05207       /* Get datastore for peer and apply it's features to the callee side of the bridge config */
05208       ast_channel_lock(peer);
05209       if ((features_datastore = ast_channel_datastore_find(peer, &dial_features_info, NULL))) {
05210          dialfeatures = features_datastore->data;
05211       }
05212 
05213       /*
05214        * When the datastores for both caller and callee are created,
05215        * both the callee and caller channels use the features_caller
05216        * flag variable to represent themselves.  With that said, the
05217        * config.features_callee flags should be copied from the
05218        * datastore's caller feature flags regardless if peer was a
05219        * callee or caller.
05220        */
05221       if (dialfeatures) {
05222          ast_copy_flags(&(config.features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
05223       }
05224       ast_channel_unlock(peer);
05225 
05226       if ((parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
05227          ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
05228       }
05229       if ((parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
05230          ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
05231       }
05232       if ((parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
05233          ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
05234       }
05235       if ((parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
05236          ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
05237       }
05238       if ((parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
05239          ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
05240       }
05241       if ((parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
05242          ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
05243       }
05244       if ((parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
05245          ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
05246       }
05247       if ((parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
05248          ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
05249       }
05250 
05251       res = ast_bridge_call(chan, peer, &config);
05252 
05253       pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
05254       ast_cdr_setdestchan(chan->cdr, peer->name);
05255 
05256       /* Simulate the PBX hanging up */
05257       ast_hangup(peer);
05258    } else {
05259       if (ast_stream_and_wait(chan, "pbx-invalidpark", "")) {
05260          ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark",
05261             chan->name);
05262       }
05263       ast_verb(3, "Channel %s tried to retrieve nonexistent parked call %d\n",
05264          chan->name, park);
05265    }
05266 
05267    parkinglot_unref(parkinglot);
05268    return -1;
05269 }
05270 
05271 /*!
05272  * \brief Unreference parkinglot object.
05273  */
05274 static void parkinglot_unref(struct ast_parkinglot *parkinglot) 
05275 {
05276    ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name,
05277       ao2_ref(parkinglot, 0) - 1);
05278    ao2_ref(parkinglot, -1);
05279 }
05280 
05281 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot)
05282 {
05283    int refcount;
05284 
05285    refcount = ao2_ref(parkinglot, +1);
05286    ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount + 1);
05287    return parkinglot;
05288 }
05289 
05290 /*! \brief Destroy a parking lot */
05291 static void parkinglot_destroy(void *obj)
05292 {
05293    struct ast_parkinglot *doomed = obj;
05294 
05295    /*
05296     * No need to destroy parked calls here because any parked call
05297     * holds a parking lot reference.  Therefore the parkings list
05298     * must be empty.
05299     */
05300    ast_assert(AST_LIST_EMPTY(&doomed->parkings));
05301    AST_LIST_HEAD_DESTROY(&doomed->parkings);
05302 }
05303 
05304 /*! \brief Allocate parking lot structure */
05305 static struct ast_parkinglot *create_parkinglot(const char *name)
05306 {
05307    struct ast_parkinglot *newlot;
05308 
05309    if (ast_strlen_zero(name)) { /* No name specified */
05310       return NULL;
05311    }
05312 
05313    newlot = ao2_alloc(sizeof(*newlot), parkinglot_destroy);
05314    if (!newlot)
05315       return NULL;
05316    
05317    ast_copy_string(newlot->name, name, sizeof(newlot->name));
05318    newlot->cfg.is_invalid = 1;/* No config is set yet. */
05319    AST_LIST_HEAD_INIT(&newlot->parkings);
05320 
05321    return newlot;
05322 }
05323 
05324 /*! 
05325  * \brief Add parking hints for all defined parking spaces.
05326  * \param context Dialplan context to add the hints.
05327  * \param start Starting space in parkinglot.
05328  * \param stop Ending space in parkinglot.
05329  */
05330 static void park_add_hints(const char *context, int start, int stop)
05331 {
05332    int numext;
05333    char device[AST_MAX_EXTENSION];
05334    char exten[10];
05335 
05336    for (numext = start; numext <= stop; numext++) {
05337       snprintf(exten, sizeof(exten), "%d", numext);
05338       snprintf(device, sizeof(device), "park:%s@%s", exten, context);
05339       ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
05340    }
05341 }
05342 
05343 /*! Default configuration for default parking lot. */
05344 static const struct parkinglot_cfg parkinglot_cfg_default_default = {
05345    .mohclass = "default",
05346    .parkext = DEFAULT_PARK_EXTENSION,
05347    .parking_con = "parkedcalls",
05348    .parking_start = 701,
05349    .parking_stop = 750,
05350    .parkingtime = DEFAULT_PARK_TIME,
05351 };
05352 
05353 /*! Default configuration for normal parking lots. */
05354 static const struct parkinglot_cfg parkinglot_cfg_default = {
05355    .parkext = DEFAULT_PARK_EXTENSION,
05356    .parkingtime = DEFAULT_PARK_TIME,
05357 };
05358 
05359 /*!
05360  * \internal
05361  * \brief Set parking lot feature flag configuration value.
05362  *
05363  * \param pl_name Parking lot name for diagnostic messages.
05364  * \param param Parameter value to set.
05365  * \param var Current configuration variable item.
05366  *
05367  * \return Nothing
05368  */
05369 static void parkinglot_feature_flag_cfg(const char *pl_name, int *param, struct ast_variable *var)
05370 {
05371    ast_debug(1, "Setting parking lot %s %s to %s\n", pl_name, var->name, var->value);
05372    if (!strcasecmp(var->value, "both")) {
05373       *param = AST_FEATURE_FLAG_BYBOTH;
05374    } else if (!strcasecmp(var->value, "caller")) {
05375       *param = AST_FEATURE_FLAG_BYCALLER;
05376    } else if (!strcasecmp(var->value, "callee")) {
05377       *param = AST_FEATURE_FLAG_BYCALLEE;
05378    }
05379 }
05380 
05381 /*!
05382  * \internal
05383  * \brief Read parking lot configuration.
05384  *
05385  * \param pl_name Parking lot name for diagnostic messages.
05386  * \param cfg Parking lot config to update that is already initialized with defaults.
05387  * \param var Config variable list.
05388  *
05389  * \retval 0 on success.
05390  * \retval -1 on error.
05391  */
05392 static int parkinglot_config_read(const char *pl_name, struct parkinglot_cfg *cfg, struct ast_variable *var)
05393 {
05394    int error = 0;
05395 
05396    while (var) {
05397       if (!strcasecmp(var->name, "context")) {
05398          ast_copy_string(cfg->parking_con, var->value, sizeof(cfg->parking_con));
05399       } else if (!strcasecmp(var->name, "parkext")) {
05400          ast_copy_string(cfg->parkext, var->value, sizeof(cfg->parkext));
05401       } else if (!strcasecmp(var->name, "parkext_exclusive")) {
05402          cfg->parkext_exclusive = ast_true(var->value);
05403       } else if (!strcasecmp(var->name, "parkinghints")) {
05404          cfg->parkaddhints = ast_true(var->value);
05405       } else if (!strcasecmp(var->name, "parkedmusicclass")) {
05406          ast_copy_string(cfg->mohclass, var->value, sizeof(cfg->mohclass));
05407       } else if (!strcasecmp(var->name, "parkingtime")) {
05408          int parkingtime = 0;
05409 
05410          if ((sscanf(var->value, "%30d", &parkingtime) != 1) || parkingtime < 1) {
05411             ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
05412             error = -1;
05413          } else {
05414             cfg->parkingtime = parkingtime * 1000;
05415          }
05416       } else if (!strcasecmp(var->name, "parkpos")) {
05417          int start = 0;
05418          int end = 0;
05419 
05420          if (sscanf(var->value, "%30d-%30d", &start, &end) != 2) {
05421             ast_log(LOG_WARNING,
05422                "Format for parking positions is a-b, where a and b are numbers at line %d of %s\n",
05423                var->lineno, var->file);
05424             error = -1;
05425          } else if (end < start || start <= 0 || end <= 0) {
05426             ast_log(LOG_WARNING, "Parking range is invalid. Must be a <= b, at line %d of %s\n",
05427                var->lineno, var->file);
05428             error = -1;
05429          } else {
05430             cfg->parking_start = start;
05431             cfg->parking_stop = end;
05432          }
05433       } else if (!strcasecmp(var->name, "findslot")) {
05434          cfg->parkfindnext = (!strcasecmp(var->value, "next"));
05435       } else if (!strcasecmp(var->name, "parkedcalltransfers")) {
05436          parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcalltransfers, var);
05437       } else if (!strcasecmp(var->name, "parkedcallreparking")) {
05438          parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallreparking, var);
05439       } else if (!strcasecmp(var->name, "parkedcallhangup")) {
05440          parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallhangup, var);
05441       } else if (!strcasecmp(var->name, "parkedcallrecording")) {
05442          parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallrecording, var);
05443       }
05444       var = var->next;
05445    }
05446 
05447    /* Check for configuration errors */
05448    if (ast_strlen_zero(cfg->parking_con)) {
05449       ast_log(LOG_WARNING, "Parking lot %s needs context\n", pl_name);
05450       error = -1;
05451    }
05452    if (ast_strlen_zero(cfg->parkext)) {
05453       ast_log(LOG_WARNING, "Parking lot %s needs parkext\n", pl_name);
05454       error = -1;
05455    }
05456    if (!cfg->parking_start) {
05457       ast_log(LOG_WARNING, "Parking lot %s needs parkpos\n", pl_name);
05458       error = -1;
05459    }
05460    if (error) {
05461       cfg->is_invalid = 1;
05462    }
05463 
05464    return error;
05465 }
05466 
05467 /*!
05468  * \internal
05469  * \brief Activate the given parkinglot.
05470  *
05471  * \param parkinglot Parking lot to activate.
05472  *
05473  * \details
05474  * Insert into the dialplan the context, parking lot access
05475  * extension, and optional dialplan hints.
05476  *
05477  * \retval 0 on success.
05478  * \retval -1 on error.
05479  */
05480 static int parkinglot_activate(struct ast_parkinglot *parkinglot)
05481 {
05482    int disabled = 0;
05483    char app_data[5 + AST_MAX_CONTEXT];
05484 
05485    /* Create Park option list.  Must match with struct park_app_args options. */
05486    if (parkinglot->cfg.parkext_exclusive) {
05487       /* Specify the parking lot this parking extension parks calls. */
05488       snprintf(app_data, sizeof(app_data), ",,,,,%s", parkinglot->name);
05489    } else {
05490       /* The dialplan must specify which parking lot to use. */
05491       app_data[0] = '\0';
05492    }
05493 
05494    /* Create context */
05495    if (!ast_context_find_or_create(NULL, NULL, parkinglot->cfg.parking_con, registrar)) {
05496       ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n",
05497          parkinglot->cfg.parking_con);
05498       disabled = 1;
05499 
05500    /* Add a parking extension into the context */
05501    } else if (ast_add_extension(parkinglot->cfg.parking_con, 1, parkinglot->cfg.parkext,
05502       1, NULL, NULL, parkcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
05503       ast_log(LOG_ERROR, "Could not create parking lot %s access exten %s@%s\n",
05504          parkinglot->name, parkinglot->cfg.parkext, parkinglot->cfg.parking_con);
05505       disabled = 1;
05506    } else {
05507       /* Add parking hints */
05508       if (parkinglot->cfg.parkaddhints) {
05509          park_add_hints(parkinglot->cfg.parking_con, parkinglot->cfg.parking_start,
05510             parkinglot->cfg.parking_stop);
05511       }
05512 
05513       /*
05514        * XXX Not sure why we should need to notify the metermaids for
05515        * this exten.  It was originally done for the default parking
05516        * lot entry exten only but should be done for all entry extens
05517        * if we do it for one.
05518        */
05519       /* Notify metermaids about parking lot entry exten state. */
05520       notify_metermaids(parkinglot->cfg.parkext, parkinglot->cfg.parking_con,
05521          AST_DEVICE_INUSE);
05522    }
05523 
05524    parkinglot->disabled = disabled;
05525    return disabled ? -1 : 0;
05526 }
05527 
05528 /*! \brief Build parkinglot from configuration and chain it in if it doesn't already exist */
05529 static struct ast_parkinglot *build_parkinglot(const char *pl_name, struct ast_variable *var)
05530 {
05531    struct ast_parkinglot *parkinglot;
05532    const struct parkinglot_cfg *cfg_defaults;
05533    struct parkinglot_cfg new_cfg;
05534    int cfg_error;
05535    int oldparkinglot = 0;
05536 
05537    parkinglot = find_parkinglot(pl_name);
05538    if (parkinglot) {
05539       oldparkinglot = 1;
05540    } else {
05541       parkinglot = create_parkinglot(pl_name);
05542       if (!parkinglot) {
05543          return NULL;
05544       }
05545    }
05546    if (!strcmp(parkinglot->name, DEFAULT_PARKINGLOT)) {
05547       cfg_defaults = &parkinglot_cfg_default_default;
05548    } else {
05549       cfg_defaults = &parkinglot_cfg_default;
05550    }
05551    new_cfg = *cfg_defaults;
05552 
05553    ast_debug(1, "Building parking lot %s\n", parkinglot->name);
05554 
05555    ao2_lock(parkinglot);
05556 
05557    /* Do some config stuff */
05558    cfg_error = parkinglot_config_read(parkinglot->name, &new_cfg, var);
05559    if (oldparkinglot) {
05560       if (cfg_error) {
05561          /* Bad configuration read.  Keep using the original config. */
05562          ast_log(LOG_WARNING, "Changes to parking lot %s are discarded.\n",
05563             parkinglot->name);
05564          cfg_error = 0;
05565       } else if (!AST_LIST_EMPTY(&parkinglot->parkings)
05566          && memcmp(&new_cfg, &parkinglot->cfg, sizeof(parkinglot->cfg))) {
05567          /* Try reloading later when parking lot is empty. */
05568          ast_log(LOG_WARNING,
05569             "Parking lot %s has parked calls.  Parking lot changes discarded.\n",
05570             parkinglot->name);
05571          force_reload_load = 1;
05572       } else {
05573          /* Accept the new config */
05574          parkinglot->cfg = new_cfg;
05575       }
05576    } else {
05577       /* Load the initial parking lot config. */
05578       parkinglot->cfg = new_cfg;
05579    }
05580    parkinglot->the_mark = 0;
05581 
05582    ao2_unlock(parkinglot);
05583 
05584    if (cfg_error) {
05585       /* Only new parking lots could have config errors here. */
05586       ast_log(LOG_WARNING, "New parking lot %s is discarded.\n", parkinglot->name);
05587       parkinglot_unref(parkinglot);
05588       return NULL;
05589    }
05590 
05591    /* Move it into the list, if it wasn't already there */
05592    if (!oldparkinglot) {
05593       ao2_link(parkinglots, parkinglot);
05594    }
05595    parkinglot_unref(parkinglot);
05596 
05597    return parkinglot;
05598 }
05599 
05600 /*!
05601  * \internal
05602  * \brief Process an applicationmap section config line.
05603  *
05604  * \param var Config variable line.
05605  *
05606  * \return Nothing
05607  */
05608 static void process_applicationmap_line(struct ast_variable *var)
05609 {
05610    char *tmp_val = ast_strdupa(var->value);
05611    char *activateon;
05612    struct ast_call_feature *feature;
05613    AST_DECLARE_APP_ARGS(args,
05614       AST_APP_ARG(exten);
05615       AST_APP_ARG(activatedby);
05616       AST_APP_ARG(app);
05617       AST_APP_ARG(app_args);
05618       AST_APP_ARG(moh_class);
05619    );
05620 
05621    AST_STANDARD_APP_ARGS(args, tmp_val);
05622    if (strchr(args.app, '(')) {
05623       /* New syntax */
05624       args.moh_class = args.app_args;
05625       args.app_args = strchr(args.app, '(');
05626       *args.app_args++ = '\0';
05627       if (args.app_args[strlen(args.app_args) - 1] == ')') {
05628          args.app_args[strlen(args.app_args) - 1] = '\0';
05629       }
05630    }
05631 
05632    activateon = strsep(&args.activatedby, "/");
05633 
05634    /*! \todo XXX var_name or app_args ? */
05635    if (ast_strlen_zero(args.app)
05636       || ast_strlen_zero(args.exten)
05637       || ast_strlen_zero(activateon)
05638       || ast_strlen_zero(var->name)) {
05639       ast_log(LOG_NOTICE,
05640          "Please check the feature Mapping Syntax, either extension, name, or app aren't provided %s %s %s %s\n",
05641          args.app, args.exten, activateon, var->name);
05642       return;
05643    }
05644 
05645    AST_RWLIST_RDLOCK(&feature_list);
05646    if (find_dynamic_feature(var->name)) {
05647       AST_RWLIST_UNLOCK(&feature_list);
05648       ast_log(LOG_WARNING, "Dynamic Feature '%s' specified more than once!\n",
05649          var->name);
05650       return;
05651    }
05652    AST_RWLIST_UNLOCK(&feature_list);
05653 
05654    if (!(feature = ast_calloc(1, sizeof(*feature)))) {
05655       return;
05656    }
05657 
05658    ast_copy_string(feature->sname, var->name, FEATURE_SNAME_LEN);
05659    ast_copy_string(feature->app, args.app, FEATURE_APP_LEN);
05660    ast_copy_string(feature->exten, args.exten, FEATURE_EXTEN_LEN);
05661 
05662    if (args.app_args) {
05663       ast_copy_string(feature->app_args, args.app_args, FEATURE_APP_ARGS_LEN);
05664    }
05665 
05666    if (args.moh_class) {
05667       ast_copy_string(feature->moh_class, args.moh_class, FEATURE_MOH_LEN);
05668    }
05669 
05670    ast_copy_string(feature->exten, args.exten, sizeof(feature->exten));
05671    feature->operation = feature_exec_app;
05672    ast_set_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF);
05673 
05674    /* Allow caller and callee to be specified for backwards compatability */
05675    if (!strcasecmp(activateon, "self") || !strcasecmp(activateon, "caller")) {
05676       ast_set_flag(feature, AST_FEATURE_FLAG_ONSELF);
05677    } else if (!strcasecmp(activateon, "peer") || !strcasecmp(activateon, "callee")) {
05678       ast_set_flag(feature, AST_FEATURE_FLAG_ONPEER);
05679    } else {
05680       ast_log(LOG_NOTICE, "Invalid 'ActivateOn' specification for feature '%s',"
05681          " must be 'self', or 'peer'\n", var->name);
05682       return;
05683    }
05684 
05685    if (ast_strlen_zero(args.activatedby)) {
05686       ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
05687    } else if (!strcasecmp(args.activatedby, "caller")) {
05688       ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLER);
05689    } else if (!strcasecmp(args.activatedby, "callee")) {
05690       ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLEE);
05691    } else if (!strcasecmp(args.activatedby, "both")) {
05692       ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
05693    } else {
05694       ast_log(LOG_NOTICE, "Invalid 'ActivatedBy' specification for feature '%s',"
05695          " must be 'caller', or 'callee', or 'both'\n", var->name);
05696       return;
05697    }
05698 
05699    ast_register_feature(feature);
05700 
05701    ast_verb(2, "Mapping Feature '%s' to app '%s(%s)' with code '%s'\n",
05702       var->name, args.app, args.app_args, args.exten);
05703 }
05704 
05705 static int process_config(struct ast_config *cfg)
05706 {
05707    int i;
05708    struct ast_variable *var = NULL;
05709    struct feature_group *fg = NULL;
05710    char *ctg; 
05711    static const char * const categories[] = { 
05712       /* Categories in features.conf that are not
05713        * to be parsed as group categories
05714        */
05715       "general",
05716       "featuremap",
05717       "applicationmap"
05718    };
05719 
05720    /* Set general features global defaults. */
05721    featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
05722 
05723    /* Set global call pickup defaults. */
05724    strcpy(pickup_ext, "*8");
05725    pickupsound[0] = '\0';
05726    pickupfailsound[0] = '\0';
05727 
05728    /* Set global call transfer defaults. */
05729    strcpy(xfersound, "beep");
05730    strcpy(xferfailsound, "beeperr");
05731    transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
05732    atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
05733    atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
05734    atxferdropcall = DEFAULT_ATXFER_DROP_CALL;
05735    atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
05736 
05737    /* Set global call parking defaults. */
05738    comebacktoorigin = 1;
05739    courtesytone[0] = '\0';
05740    parkedplay = 0;
05741    adsipark = 0;
05742    parkeddynamic = 0;
05743 
05744    var = ast_variable_browse(cfg, "general");
05745    build_parkinglot(DEFAULT_PARKINGLOT, var);
05746    for (; var; var = var->next) {
05747       if (!strcasecmp(var->name, "parkeddynamic")) {
05748          parkeddynamic = ast_true(var->value);
05749       } else if (!strcasecmp(var->name, "adsipark")) {
05750          adsipark = ast_true(var->value);
05751       } else if (!strcasecmp(var->name, "transferdigittimeout")) {
05752          if ((sscanf(var->value, "%30d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
05753             ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
05754             transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
05755          } else {
05756             transferdigittimeout = transferdigittimeout * 1000;
05757          }
05758       } else if (!strcasecmp(var->name, "featuredigittimeout")) {
05759          if ((sscanf(var->value, "%30d", &featuredigittimeout) != 1) || (featuredigittimeout < 1)) {
05760             ast_log(LOG_WARNING, "%s is not a valid featuredigittimeout\n", var->value);
05761             featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
05762          }
05763       } else if (!strcasecmp(var->name, "atxfernoanswertimeout")) {
05764          if ((sscanf(var->value, "%30d", &atxfernoanswertimeout) != 1) || (atxfernoanswertimeout < 1)) {
05765             ast_log(LOG_WARNING, "%s is not a valid atxfernoanswertimeout\n", var->value);
05766             atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
05767          } else {
05768             atxfernoanswertimeout = atxfernoanswertimeout * 1000;
05769          }
05770       } else if (!strcasecmp(var->name, "atxferloopdelay")) {
05771          if ((sscanf(var->value, "%30u", &atxferloopdelay) != 1)) {
05772             ast_log(LOG_WARNING, "%s is not a valid atxferloopdelay\n", var->value);
05773             atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
05774          } else {
05775             atxferloopdelay *= 1000;
05776          }
05777       } else if (!strcasecmp(var->name, "atxferdropcall")) {
05778          atxferdropcall = ast_true(var->value);
05779       } else if (!strcasecmp(var->name, "atxfercallbackretries")) {
05780          if ((sscanf(var->value, "%30u", &atxfercallbackretries) != 1)) {
05781             ast_log(LOG_WARNING, "%s is not a valid atxfercallbackretries\n", var->value);
05782             atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
05783          }
05784       } else if (!strcasecmp(var->name, "courtesytone")) {
05785          ast_copy_string(courtesytone, var->value, sizeof(courtesytone));
05786       }  else if (!strcasecmp(var->name, "parkedplay")) {
05787          if (!strcasecmp(var->value, "both")) {
05788             parkedplay = 2;
05789          } else if (!strcasecmp(var->value, "parked")) {
05790             parkedplay = 1;
05791          } else {
05792             parkedplay = 0;
05793          }
05794       } else if (!strcasecmp(var->name, "xfersound")) {
05795          ast_copy_string(xfersound, var->value, sizeof(xfersound));
05796       } else if (!strcasecmp(var->name, "xferfailsound")) {
05797          ast_copy_string(xferfailsound, var->value, sizeof(xferfailsound));
05798       } else if (!strcasecmp(var->name, "pickupexten")) {
05799          ast_copy_string(pickup_ext, var->value, sizeof(pickup_ext));
05800       } else if (!strcasecmp(var->name, "pickupsound")) {
05801          ast_copy_string(pickupsound, var->value, sizeof(pickupsound));
05802       } else if (!strcasecmp(var->name, "pickupfailsound")) {
05803          ast_copy_string(pickupfailsound, var->value, sizeof(pickupfailsound));
05804       } else if (!strcasecmp(var->name, "comebacktoorigin")) {
05805          comebacktoorigin = ast_true(var->value);
05806       }
05807    }
05808 
05809    unmap_features();
05810    for (var = ast_variable_browse(cfg, "featuremap"); var; var = var->next) {
05811       if (remap_feature(var->name, var->value)) {
05812          ast_log(LOG_NOTICE, "Unknown feature '%s'\n", var->name);
05813       }
05814    }
05815 
05816    /* Map a key combination to an application */
05817    ast_unregister_features();
05818    for (var = ast_variable_browse(cfg, "applicationmap"); var; var = var->next) {
05819       process_applicationmap_line(var);
05820    }
05821 
05822    ast_unregister_groups();
05823    AST_RWLIST_WRLOCK(&feature_groups);
05824 
05825    ctg = NULL;
05826    while ((ctg = ast_category_browse(cfg, ctg))) {
05827       /* Is this a parkinglot definition ? */
05828       if (!strncasecmp(ctg, "parkinglot_", strlen("parkinglot_"))) {
05829          ast_debug(2, "Found configuration section %s, assume parking context\n", ctg);
05830          if (!build_parkinglot(ctg, ast_variable_browse(cfg, ctg))) {
05831             ast_log(LOG_ERROR, "Could not build parking lot %s. Configuration error.\n", ctg);
05832          } else {
05833             ast_debug(1, "Configured parking context %s\n", ctg);
05834          }
05835          continue;   
05836       }
05837 
05838       /* No, check if it's a group */
05839       for (i = 0; i < ARRAY_LEN(categories); i++) {
05840          if (!strcasecmp(categories[i], ctg)) {
05841             break;
05842          }
05843       }
05844       if (i < ARRAY_LEN(categories)) {
05845          continue;
05846       }
05847 
05848       if (!(fg = register_group(ctg))) {
05849          continue;
05850       }
05851 
05852       for (var = ast_variable_browse(cfg, ctg); var; var = var->next) {
05853          struct ast_call_feature *feature;
05854 
05855          AST_RWLIST_RDLOCK(&feature_list);
05856          if (!(feature = find_dynamic_feature(var->name)) && 
05857              !(feature = ast_find_call_feature(var->name))) {
05858             AST_RWLIST_UNLOCK(&feature_list);
05859             ast_log(LOG_WARNING, "Feature '%s' was not found.\n", var->name);
05860             continue;
05861          }
05862          AST_RWLIST_UNLOCK(&feature_list);
05863 
05864          register_group_feature(fg, var->value, feature);
05865       }
05866    }
05867 
05868    AST_RWLIST_UNLOCK(&feature_groups);
05869 
05870    return 0;
05871 }
05872 
05873 /*!
05874  * \internal
05875  * \brief Destroy the given dialplan usage context.
05876  *
05877  * \param doomed Parking lot usage context to destroy.
05878  *
05879  * \return Nothing
05880  */
05881 static void destroy_dialplan_usage_context(struct parking_dp_context *doomed)
05882 {
05883    struct parking_dp_ramp *ramp;
05884    struct parking_dp_spaces *spaces;
05885 
05886    while ((ramp = AST_LIST_REMOVE_HEAD(&doomed->access_extens, node))) {
05887       ast_free(ramp);
05888    }
05889    while ((spaces = AST_LIST_REMOVE_HEAD(&doomed->spaces, node))) {
05890       ast_free(spaces);
05891    }
05892    while ((spaces = AST_LIST_REMOVE_HEAD(&doomed->hints, node))) {
05893       ast_free(spaces);
05894    }
05895    ast_free(doomed);
05896 }
05897 
05898 /*!
05899  * \internal
05900  * \brief Destroy the given dialplan usage map.
05901  *
05902  * \param doomed Parking lot usage map to destroy.
05903  *
05904  * \return Nothing
05905  */
05906 static void destroy_dialplan_usage_map(struct parking_dp_map *doomed)
05907 {
05908    struct parking_dp_context *item;
05909 
05910    while ((item = AST_LIST_REMOVE_HEAD(doomed, node))) {
05911       destroy_dialplan_usage_context(item);
05912    }
05913 }
05914 
05915 /*!
05916  * \internal
05917  * \brief Create a new parking lot ramp dialplan usage node.
05918  *
05919  * \param exten Parking lot access ramp extension.
05920  * \param exclusive TRUE if the parking lot access ramp extension is exclusive.
05921  *
05922  * \retval New usage ramp node on success.
05923  * \retval NULL on error.
05924  */
05925 static struct parking_dp_ramp *build_dialplan_useage_ramp(const char *exten, int exclusive)
05926 {
05927    struct parking_dp_ramp *ramp_node;
05928 
05929    ramp_node = ast_calloc(1, sizeof(*ramp_node) + strlen(exten));
05930    if (!ramp_node) {
05931       return NULL;
05932    }
05933    ramp_node->exclusive = exclusive;
05934    strcpy(ramp_node->exten, exten);
05935    return ramp_node;
05936 }
05937 
05938 /*!
05939  * \internal
05940  * \brief Add parking lot access ramp to the context ramp usage map.
05941  *
05942  * \param ramp_map Current parking lot context ramp usage map.
05943  * \param exten Parking lot access ramp extension to add.
05944  * \param exclusive TRUE if the parking lot access ramp extension is exclusive.
05945  * \param lot Parking lot supplying reference data.
05946  * \param complain TRUE if to complain of parking lot ramp conflicts.
05947  *
05948  * \retval 0 on success.  The ramp_map is updated.
05949  * \retval -1 on failure.
05950  */
05951 static int usage_context_add_ramp(struct parking_dp_ramp_map *ramp_map, const char *exten, int exclusive, struct ast_parkinglot *lot, int complain)
05952 {
05953    struct parking_dp_ramp *cur_ramp;
05954    struct parking_dp_ramp *new_ramp;
05955    int cmp;
05956 
05957    /* Make sure that exclusive is only 0 or 1 */
05958    if (exclusive) {
05959       exclusive = 1;
05960    }
05961 
05962    AST_LIST_TRAVERSE_SAFE_BEGIN(ramp_map, cur_ramp, node) {
05963       cmp = strcmp(exten, cur_ramp->exten);
05964       if (cmp > 0) {
05965          /* The parking lot ramp goes after this node. */
05966          continue;
05967       }
05968       if (cmp == 0) {
05969          /* The ramp is already in the map. */
05970          if (complain && (cur_ramp->exclusive || exclusive)) {
05971             ast_log(LOG_WARNING,
05972                "Parking lot '%s' parkext %s@%s used by another parking lot.\n",
05973                lot->name, exten, lot->cfg.parking_con);
05974          }
05975          return 0;
05976       }
05977       /* The new parking lot ramp goes before this node. */
05978       new_ramp = build_dialplan_useage_ramp(exten, exclusive);
05979       if (!new_ramp) {
05980          return -1;
05981       }
05982       AST_LIST_INSERT_BEFORE_CURRENT(new_ramp, node);
05983       return 0;
05984    }
05985    AST_LIST_TRAVERSE_SAFE_END;
05986 
05987    /* New parking lot access ramp goes on the end. */
05988    new_ramp = build_dialplan_useage_ramp(exten, exclusive);
05989    if (!new_ramp) {
05990       return -1;
05991    }
05992    AST_LIST_INSERT_TAIL(ramp_map, new_ramp, node);
05993    return 0;
05994 }
05995 
05996 /*!
05997  * \internal
05998  * \brief Create a new parking lot spaces dialplan usage node.
05999  *
06000  * \param start First parking lot space to add.
06001  * \param stop Last parking lot space to add.
06002  *
06003  * \retval New usage ramp node on success.
06004  * \retval NULL on error.
06005  */
06006 static struct parking_dp_spaces *build_dialplan_useage_spaces(int start, int stop)
06007 {
06008    struct parking_dp_spaces *spaces_node;
06009 
06010    spaces_node = ast_calloc(1, sizeof(*spaces_node));
06011    if (!spaces_node) {
06012       return NULL;
06013    }
06014    spaces_node->start = start;
06015    spaces_node->stop = stop;
06016    return spaces_node;
06017 }
06018 
06019 /*!
06020  * \internal
06021  * \brief Add parking lot spaces to the context space usage map.
06022  *
06023  * \param space_map Current parking lot context space usage map.
06024  * \param start First parking lot space to add.
06025  * \param stop Last parking lot space to add.
06026  * \param lot Parking lot supplying reference data.
06027  * \param complain TRUE if to complain of parking lot spaces conflicts.
06028  *
06029  * \retval 0 on success.  The space_map is updated.
06030  * \retval -1 on failure.
06031  */
06032 static int usage_context_add_spaces(struct parking_dp_space_map *space_map, int start, int stop, struct ast_parkinglot *lot, int complain)
06033 {
06034    struct parking_dp_spaces *cur_node;
06035    struct parking_dp_spaces *expand_node;
06036    struct parking_dp_spaces *new_node;
06037 
06038    expand_node = NULL;
06039    AST_LIST_TRAVERSE_SAFE_BEGIN(space_map, cur_node, node) {
06040       /* NOTE: stop + 1 to combine immediately adjacent nodes into one. */
06041       if (expand_node) {
06042          /* The previous node is expanding to possibly eat following nodes. */
06043          if (expand_node->stop + 1 < cur_node->start) {
06044             /* Current node is completely after expanding node. */
06045             return 0;
06046          }
06047 
06048          if (complain
06049             && ((cur_node->start <= start && start <= cur_node->stop)
06050                || (cur_node->start <= stop && stop <= cur_node->stop)
06051                || (start < cur_node->start && cur_node->stop < stop))) {
06052             /* Only complain once per range add. */
06053             complain = 0;
06054             ast_log(LOG_WARNING,
06055                "Parking lot '%s' parkpos %d-%d@%s overlaps another parking lot.\n",
06056                lot->name, start, stop, lot->cfg.parking_con);
06057          }
06058 
06059          /* Current node is eaten by the expanding node. */
06060          if (expand_node->stop < cur_node->stop) {
06061             expand_node->stop = cur_node->stop;
06062          }
06063          AST_LIST_REMOVE_CURRENT(node);
06064          ast_free(cur_node);
06065          continue;
06066       }
06067 
06068       if (cur_node->stop + 1 < start) {
06069          /* New range is completely after current node. */
06070          continue;
06071       }
06072       if (stop + 1 < cur_node->start) {
06073          /* New range is completely before current node. */
06074          new_node = build_dialplan_useage_spaces(start, stop);
06075          if (!new_node) {
06076             return -1;
06077          }
06078          AST_LIST_INSERT_BEFORE_CURRENT(new_node, node);
06079          return 0;
06080       }
06081 
06082       if (complain
06083          && ((cur_node->start <= start && start <= cur_node->stop)
06084             || (cur_node->start <= stop && stop <= cur_node->stop)
06085             || (start < cur_node->start && cur_node->stop < stop))) {
06086          /* Only complain once per range add. */
06087          complain = 0;
06088          ast_log(LOG_WARNING,
06089             "Parking lot '%s' parkpos %d-%d@%s overlaps another parking lot.\n",
06090             lot->name, start, stop, lot->cfg.parking_con);
06091       }
06092 
06093       /* Current node range overlaps or is immediately adjacent to new range. */
06094       if (start < cur_node->start) {
06095          /* Expand the current node in the front. */
06096          cur_node->start = start;
06097       }
06098       if (stop <= cur_node->stop) {
06099          /* Current node is not expanding in the rear. */
06100          return 0;
06101       }
06102       cur_node->stop = stop;
06103       expand_node = cur_node;
06104    }
06105    AST_LIST_TRAVERSE_SAFE_END;
06106 
06107    if (expand_node) {
06108       /*
06109        * The previous node expanded and either ate all following nodes
06110        * or it was the last node.
06111        */
06112       return 0;
06113    }
06114 
06115    /* New range goes on the end. */
06116    new_node = build_dialplan_useage_spaces(start, stop);
06117    if (!new_node) {
06118       return -1;
06119    }
06120    AST_LIST_INSERT_TAIL(space_map, new_node, node);
06121    return 0;
06122 }
06123 
06124 /*!
06125  * \internal
06126  * \brief Add parking lot spaces to the context dialplan usage node.
06127  *
06128  * \param ctx_node Usage node to add parking lot spaces.
06129  * \param lot Parking lot to add data to ctx_node.
06130  * \param complain TRUE if to complain of parking lot ramp and spaces conflicts.
06131  *
06132  * \retval 0 on success.
06133  * \retval -1 on error.
06134  */
06135 static int dialplan_usage_add_parkinglot_data(struct parking_dp_context *ctx_node, struct ast_parkinglot *lot, int complain)
06136 {
06137    if (usage_context_add_ramp(&ctx_node->access_extens, lot->cfg.parkext,
06138       lot->cfg.parkext_exclusive, lot, complain)) {
06139       return -1;
06140    }
06141    if (usage_context_add_spaces(&ctx_node->spaces, lot->cfg.parking_start,
06142       lot->cfg.parking_stop, lot, complain)) {
06143       return -1;
06144    }
06145    if (lot->cfg.parkaddhints
06146       && usage_context_add_spaces(&ctx_node->hints, lot->cfg.parking_start,
06147          lot->cfg.parking_stop, lot, 0)) {
06148       return -1;
06149    }
06150    return 0;
06151 }
06152 
06153 /*!
06154  * \internal
06155  * \brief Create a new parking lot context dialplan usage node.
06156  *
06157  * \param lot Parking lot to create a new dialplan usage from.
06158  *
06159  * \retval New usage context node on success.
06160  * \retval NULL on error.
06161  */
06162 static struct parking_dp_context *build_dialplan_useage_context(struct ast_parkinglot *lot)
06163 {
06164    struct parking_dp_context *ctx_node;
06165 
06166    ctx_node = ast_calloc(1, sizeof(*ctx_node) + strlen(lot->cfg.parking_con));
06167    if (!ctx_node) {
06168       return NULL;
06169    }
06170    if (dialplan_usage_add_parkinglot_data(ctx_node, lot, 0)) {
06171       destroy_dialplan_usage_context(ctx_node);
06172       return NULL;
06173    }
06174    strcpy(ctx_node->context, lot->cfg.parking_con);
06175    return ctx_node;
06176 }
06177 
06178 /*!
06179  * \internal
06180  * \brief Add the given parking lot dialplan usage to the dialplan usage map.
06181  *
06182  * \param usage_map Parking lot usage map to add the given parking lot.
06183  * \param lot Parking lot to add dialplan usage.
06184  * \param complain TRUE if to complain of parking lot ramp and spaces conflicts.
06185  *
06186  * \retval 0 on success.
06187  * \retval -1 on error.
06188  */
06189 static int dialplan_usage_add_parkinglot(struct parking_dp_map *usage_map, struct ast_parkinglot *lot, int complain)
06190 {
06191    struct parking_dp_context *cur_ctx;
06192    struct parking_dp_context *new_ctx;
06193    int cmp;
06194 
06195    AST_LIST_TRAVERSE_SAFE_BEGIN(usage_map, cur_ctx, node) {
06196       cmp = strcmp(lot->cfg.parking_con, cur_ctx->context);
06197       if (cmp > 0) {
06198          /* The parking lot context goes after this node. */
06199          continue;
06200       }
06201       if (cmp == 0) {
06202          /* This is the node we will add parking lot spaces to the map. */
06203          return dialplan_usage_add_parkinglot_data(cur_ctx, lot, complain);
06204       }
06205       /* The new parking lot context goes before this node. */
06206       new_ctx = build_dialplan_useage_context(lot);
06207       if (!new_ctx) {
06208          return -1;
06209       }
06210       AST_LIST_INSERT_BEFORE_CURRENT(new_ctx, node);
06211       return 0;
06212    }
06213    AST_LIST_TRAVERSE_SAFE_END;
06214 
06215    /* New parking lot context goes on the end. */
06216    new_ctx = build_dialplan_useage_context(lot);
06217    if (!new_ctx) {
06218       return -1;
06219    }
06220    AST_LIST_INSERT_TAIL(usage_map, new_ctx, node);
06221    return 0;
06222 }
06223 
06224 /*!
06225  * \internal
06226  * \brief Build the dialplan usage map of the current parking lot container.
06227  *
06228  * \param usage_map Parking lot usage map.  Must already be initialized.
06229  * \param complain TRUE if to complain of parking lot ramp and spaces conflicts.
06230  *
06231  * \retval 0 on success.  The usage_map is filled in.
06232  * \retval -1 on failure.  Built usage_map is incomplete.
06233  */
06234 static int build_dialplan_useage_map(struct parking_dp_map *usage_map, int complain)
06235 {
06236    int status = 0;
06237    struct ao2_iterator iter;
06238    struct ast_parkinglot *curlot;
06239 
06240    /* For all parking lots */
06241    iter = ao2_iterator_init(parkinglots, 0);
06242    for (; (curlot = ao2_iterator_next(&iter)); ao2_ref(curlot, -1)) {
06243       /* Add the parking lot to the map. */
06244       if (dialplan_usage_add_parkinglot(usage_map, curlot, complain)) {
06245          ao2_ref(curlot, -1);
06246          status = -1;
06247          break;
06248       }
06249    }
06250    ao2_iterator_destroy(&iter);
06251 
06252    return status;
06253 }
06254 
06255 /*!
06256  * \internal
06257  * \brief Remove the given extension if it exists.
06258  *
06259  * \param context Dialplan database context name.
06260  * \param exten Extension to remove.
06261  * \param priority Extension priority to remove.
06262  *
06263  * \return Nothing
06264  */
06265 static void remove_exten_if_exist(const char *context, const char *exten, int priority)
06266 {
06267    struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
06268 
06269    if (pbx_find_extension(NULL, NULL, &q, context, exten, priority, NULL, NULL,
06270       E_MATCH)) {
06271       ast_debug(1, "Removing unneeded parking lot exten: %s@%s priority:%d\n",
06272          context, exten, priority);
06273       ast_context_remove_extension(context, exten, priority, registrar);
06274    }
06275 }
06276 
06277 /*!
06278  * \internal
06279  * \brief Remove unused parking lot access ramp items.
06280  *
06281  * \param context Dialplan database context name.
06282  * \param old_ramps Before configuration reload access ramp usage map.
06283  * \param new_ramps After configuration reload access ramp usage map.
06284  *
06285  * \details
06286  * Remove access ramp items that were in the old context but not in the
06287  * new context.
06288  *
06289  * \return Nothing
06290  */
06291 static void remove_dead_ramp_usage(const char *context, struct parking_dp_ramp_map *old_ramps, struct parking_dp_ramp_map *new_ramps)
06292 {
06293    struct parking_dp_ramp *old_ramp;
06294    struct parking_dp_ramp *new_ramp;
06295    int cmp;
06296 
06297    old_ramp = AST_LIST_FIRST(old_ramps);
06298    new_ramp = AST_LIST_FIRST(new_ramps);
06299 
06300    while (new_ramp) {
06301       if (!old_ramp) {
06302          /* No old ramps left, so no dead ramps can remain. */
06303          return;
06304       }
06305       cmp = strcmp(old_ramp->exten, new_ramp->exten);
06306       if (cmp < 0) {
06307          /* New map does not have old ramp. */
06308          remove_exten_if_exist(context, old_ramp->exten, 1);
06309          old_ramp = AST_LIST_NEXT(old_ramp, node);
06310          continue;
06311       }
06312       if (cmp == 0) {
06313          /* Old and new map have this ramp. */
06314          old_ramp = AST_LIST_NEXT(old_ramp, node);
06315       } else {
06316          /* Old map does not have new ramp. */
06317       }
06318       new_ramp = AST_LIST_NEXT(new_ramp, node);
06319    }
06320 
06321    /* Any old ramps left must be dead. */
06322    for (; old_ramp; old_ramp = AST_LIST_NEXT(old_ramp, node)) {
06323       remove_exten_if_exist(context, old_ramp->exten, 1);
06324    }
06325 }
06326 
06327 /*!
06328  * \internal
06329  * \brief Destroy the given parking space.
06330  *
06331  * \param context Dialplan database context name.
06332  * \param space Parking space.
06333  *
06334  * \return Nothing
06335  */
06336 static void destroy_space(const char *context, int space)
06337 {
06338    char exten[AST_MAX_EXTENSION];
06339 
06340    /* Destroy priorities of the parking space that we registered. */
06341    snprintf(exten, sizeof(exten), "%d", space);
06342    remove_exten_if_exist(context, exten, PRIORITY_HINT);
06343    remove_exten_if_exist(context, exten, 1);
06344 }
06345 
06346 /*!
06347  * \internal
06348  * \brief Remove unused parking lot space items.
06349  *
06350  * \param context Dialplan database context name.
06351  * \param old_spaces Before configuration reload parking space usage map.
06352  * \param new_spaces After configuration reload parking space usage map.
06353  * \param destroy_space Function to destroy parking space item.
06354  *
06355  * \details
06356  * Remove parking space items that were in the old context but
06357  * not in the new context.
06358  *
06359  * \return Nothing
06360  */
06361 static void remove_dead_spaces_usage(const char *context,
06362    struct parking_dp_space_map *old_spaces, struct parking_dp_space_map *new_spaces,
06363    void (*destroy_space)(const char *context, int space))
06364 {
06365    struct parking_dp_spaces *old_range;
06366    struct parking_dp_spaces *new_range;
06367    int space;/*!< Current position in the current old range. */
06368    int stop;
06369 
06370    old_range = AST_LIST_FIRST(old_spaces);
06371    new_range = AST_LIST_FIRST(new_spaces);
06372    space = -1;
06373 
06374    while (old_range) {
06375       if (space < old_range->start) {
06376          space = old_range->start;
06377       }
06378       if (new_range) {
06379          if (space < new_range->start) {
06380             /* Current position in old range starts before new range. */
06381             if (old_range->stop < new_range->start) {
06382                /* Old range ends before new range. */
06383                stop = old_range->stop;
06384                old_range = AST_LIST_NEXT(old_range, node);
06385             } else {
06386                /* Tail of old range overlaps new range. */
06387                stop = new_range->start - 1;
06388             }
06389          } else if (/* new_range->start <= space && */ space <= new_range->stop) {
06390             /* Current position in old range overlaps new range. */
06391             if (old_range->stop <= new_range->stop) {
06392                /* Old range ends at or before new range. */
06393                old_range = AST_LIST_NEXT(old_range, node);
06394             } else {
06395                /* Old range extends beyond end of new range. */
06396                space = new_range->stop + 1;
06397                new_range = AST_LIST_NEXT(new_range, node);
06398             }
06399             continue;
06400          } else /* if (new_range->stop < space) */ {
06401             /* Current position in old range starts after new range. */
06402             new_range = AST_LIST_NEXT(new_range, node);
06403             continue;
06404          }
06405       } else {
06406          /* No more new ranges.  All remaining old spaces are dead. */
06407          stop = old_range->stop;
06408          old_range = AST_LIST_NEXT(old_range, node);
06409       }
06410 
06411       /* Destroy dead parking spaces. */
06412       for (; space <= stop; ++space) {
06413          destroy_space(context, space);
06414       }
06415    }
06416 }
06417 
06418 /*!
06419  * \internal
06420  * \brief Remove unused parking lot context items.
06421  *
06422  * \param context Dialplan database context name.
06423  * \param old_ctx Before configuration reload context usage map.
06424  * \param new_ctx After configuration reload context usage map.
06425  *
06426  * \details
06427  * Remove context usage items that were in the old context but not in the
06428  * new context.
06429  *
06430  * \return Nothing
06431  */
06432 static void remove_dead_context_usage(const char *context, struct parking_dp_context *old_ctx, struct parking_dp_context *new_ctx)
06433 {
06434    remove_dead_ramp_usage(context, &old_ctx->access_extens, &new_ctx->access_extens);
06435    remove_dead_spaces_usage(context, &old_ctx->spaces, &new_ctx->spaces, destroy_space);
06436 #if 0
06437    /* I don't think we should destroy hints if the parking space still exists. */
06438    remove_dead_spaces_usage(context, &old_ctx->hints, &new_ctx->hints, destroy_space_hint);
06439 #endif
06440 }
06441 
06442 /*!
06443  * \internal
06444  * \brief Remove unused parking lot dialplan items.
06445  *
06446  * \param old_map Before configuration reload dialplan usage map.
06447  * \param new_map After configuration reload dialplan usage map.
06448  *
06449  * \details
06450  * Remove dialplan items that were in the old map but not in the
06451  * new map.
06452  *
06453  * \return Nothing
06454  */
06455 static void remove_dead_dialplan_useage(struct parking_dp_map *old_map, struct parking_dp_map *new_map)
06456 {
06457    struct parking_dp_context *old_ctx;
06458    struct parking_dp_context *new_ctx;
06459    struct ast_context *con;
06460    int cmp;
06461 
06462    old_ctx = AST_LIST_FIRST(old_map);
06463    new_ctx = AST_LIST_FIRST(new_map);
06464 
06465    while (new_ctx) {
06466       if (!old_ctx) {
06467          /* No old contexts left, so no dead stuff can remain. */
06468          return;
06469       }
06470       cmp = strcmp(old_ctx->context, new_ctx->context);
06471       if (cmp < 0) {
06472          /* New map does not have old map context. */
06473          con = ast_context_find(old_ctx->context);
06474          if (con) {
06475             ast_context_destroy(con, registrar);
06476          }
06477          old_ctx = AST_LIST_NEXT(old_ctx, node);
06478          continue;
06479       }
06480       if (cmp == 0) {
06481          /* Old and new map have this context. */
06482          remove_dead_context_usage(old_ctx->context, old_ctx, new_ctx);
06483          old_ctx = AST_LIST_NEXT(old_ctx, node);
06484       } else {
06485          /* Old map does not have new map context. */
06486       }
06487       new_ctx = AST_LIST_NEXT(new_ctx, node);
06488    }
06489 
06490    /* Any old contexts left must be dead. */
06491    for (; old_ctx; old_ctx = AST_LIST_NEXT(old_ctx, node)) {
06492       con = ast_context_find(old_ctx->context);
06493       if (con) {
06494          ast_context_destroy(con, registrar);
06495       }
06496    }
06497 }
06498 
06499 static int parkinglot_markall_cb(void *obj, void *arg, int flags)
06500 {
06501    struct ast_parkinglot *parkinglot = obj;
06502 
06503    parkinglot->the_mark = 1;
06504    return 0;
06505 }
06506 
06507 static int parkinglot_is_marked_cb(void *obj, void *arg, int flags)
06508 {
06509    struct ast_parkinglot *parkinglot = obj;
06510 
06511    if (parkinglot->the_mark) {
06512       if (AST_LIST_EMPTY(&parkinglot->parkings)) {
06513          /* This parking lot can actually be deleted. */
06514          return CMP_MATCH;
06515       }
06516       /* Try reloading later when parking lot is empty. */
06517       ast_log(LOG_WARNING,
06518          "Parking lot %s has parked calls.  Could not remove.\n",
06519          parkinglot->name);
06520       parkinglot->disabled = 1;
06521       force_reload_load = 1;
06522    }
06523 
06524    return 0;
06525 }
06526 
06527 static int parkinglot_activate_cb(void *obj, void *arg, int flags)
06528 {
06529    struct ast_parkinglot *parkinglot = obj;
06530 
06531    if (parkinglot->the_mark) {
06532       /*
06533        * Don't activate a parking lot that still bears the_mark since
06534        * it is effectively deleted.
06535        */
06536       return 0;
06537    }
06538 
06539    if (parkinglot_activate(parkinglot)) {
06540       /*
06541        * The parking lot failed to activate.  Allow reloading later to
06542        * see if that fixes it.
06543        */
06544       force_reload_load = 1;
06545       ast_log(LOG_WARNING, "Parking lot %s not open for business.\n", parkinglot->name);
06546    } else {
06547       ast_debug(1, "Parking lot %s now open for business. (parkpos %d-%d)\n",
06548          parkinglot->name, parkinglot->cfg.parking_start,
06549          parkinglot->cfg.parking_stop);
06550    }
06551 
06552    return 0;
06553 }
06554 
06555 static int load_config(int reload)
06556 {
06557    struct ast_flags config_flags = {
06558       reload && !force_reload_load ? CONFIG_FLAG_FILEUNCHANGED : 0 };
06559    struct ast_config *cfg;
06560    struct parking_dp_map old_usage_map = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
06561    struct parking_dp_map new_usage_map = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
06562 
06563    /* We are reloading now and have already determined if we will force the reload. */
06564    force_reload_load = 0;
06565 
06566    if (!default_parkinglot) {
06567       /* Must create the default default parking lot */
06568       default_parkinglot = build_parkinglot(DEFAULT_PARKINGLOT, NULL);
06569       if (!default_parkinglot) {
06570          ast_log(LOG_ERROR, "Configuration of default default parking lot failed.\n");
06571          return -1;
06572       }
06573       ast_debug(1, "Configuration of default default parking lot done.\n");
06574       parkinglot_addref(default_parkinglot);
06575    }
06576 
06577    cfg = ast_config_load2("features.conf", "features", config_flags);
06578    if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
06579       /* No sense in asking for reload trouble if nothing changed. */
06580       ast_debug(1, "features.conf did not change.\n");
06581       return 0;
06582    }
06583    if (cfg == CONFIG_STATUS_FILEMISSING
06584       || cfg == CONFIG_STATUS_FILEINVALID) {
06585       ast_log(LOG_WARNING, "Could not load features.conf\n");
06586       return 0;
06587    }
06588 
06589    /* Save current parking lot dialplan needs. */
06590    if (build_dialplan_useage_map(&old_usage_map, 0)) {
06591       destroy_dialplan_usage_map(&old_usage_map);
06592 
06593       /* Allow reloading later to see if conditions have improved. */
06594       force_reload_load = 1;
06595       return -1;
06596    }
06597 
06598    ao2_t_callback(parkinglots, OBJ_NODATA, parkinglot_markall_cb, NULL,
06599       "callback to mark all parking lots");
06600    process_config(cfg);
06601    ast_config_destroy(cfg);
06602    ao2_t_callback(parkinglots, OBJ_NODATA | OBJ_UNLINK, parkinglot_is_marked_cb, NULL,
06603       "callback to remove marked parking lots");
06604 
06605    /* Save updated parking lot dialplan needs. */
06606    if (build_dialplan_useage_map(&new_usage_map, 1)) {
06607       /*
06608        * Yuck, if this failure caused any parking lot dialplan items
06609        * to be lost, they will likely remain lost until Asterisk is
06610        * restarted.
06611        */
06612       destroy_dialplan_usage_map(&old_usage_map);
06613       destroy_dialplan_usage_map(&new_usage_map);
06614       return -1;
06615    }
06616 
06617    /* Remove no longer needed parking lot dialplan usage. */
06618    remove_dead_dialplan_useage(&old_usage_map, &new_usage_map);
06619 
06620    destroy_dialplan_usage_map(&old_usage_map);
06621    destroy_dialplan_usage_map(&new_usage_map);
06622 
06623    ao2_t_callback(parkinglots, OBJ_NODATA, parkinglot_activate_cb, NULL,
06624       "callback to activate all parking lots");
06625 
06626    return 0;
06627 }
06628 
06629 /*!
06630  * \brief CLI command to list configured features
06631  * \param e
06632  * \param cmd
06633  * \param a
06634  *
06635  * \retval CLI_SUCCESS on success.
06636  * \retval NULL when tab completion is used.
06637  */
06638 static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06639 {
06640    int i;
06641    struct ast_call_feature *feature;
06642    struct ao2_iterator iter;
06643    struct ast_parkinglot *curlot;
06644 #define HFS_FORMAT "%-25s %-7s %-7s\n"
06645 
06646    switch (cmd) {
06647    
06648    case CLI_INIT:
06649       e->command = "features show";
06650       e->usage =
06651          "Usage: features show\n"
06652          "       Lists configured features\n";
06653       return NULL;
06654    case CLI_GENERATE:
06655       return NULL;
06656    }
06657 
06658    ast_cli(a->fd, HFS_FORMAT, "Builtin Feature", "Default", "Current");
06659    ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
06660 
06661    ast_cli(a->fd, HFS_FORMAT, "Pickup", "*8", ast_pickup_ext());          /* default hardcoded above, so we'll hardcode it here */
06662 
06663    ast_rwlock_rdlock(&features_lock);
06664    for (i = 0; i < FEATURES_COUNT; i++)
06665       ast_cli(a->fd, HFS_FORMAT, builtin_features[i].fname, builtin_features[i].default_exten, builtin_features[i].exten);
06666    ast_rwlock_unlock(&features_lock);
06667 
06668    ast_cli(a->fd, "\n");
06669    ast_cli(a->fd, HFS_FORMAT, "Dynamic Feature", "Default", "Current");
06670    ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
06671    if (AST_RWLIST_EMPTY(&feature_list)) {
06672       ast_cli(a->fd, "(none)\n");
06673    } else {
06674       AST_RWLIST_RDLOCK(&feature_list);
06675       AST_RWLIST_TRAVERSE(&feature_list, feature, feature_entry) {
06676          ast_cli(a->fd, HFS_FORMAT, feature->sname, "no def", feature->exten);
06677       }
06678       AST_RWLIST_UNLOCK(&feature_list);
06679    }
06680 
06681    ast_cli(a->fd, "\nFeature Groups:\n");
06682    ast_cli(a->fd, "---------------\n");
06683    if (AST_RWLIST_EMPTY(&feature_groups)) {
06684       ast_cli(a->fd, "(none)\n");
06685    } else {
06686       struct feature_group *fg;
06687       struct feature_group_exten *fge;
06688 
06689       AST_RWLIST_RDLOCK(&feature_groups);
06690       AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
06691          ast_cli(a->fd, "===> Group: %s\n", fg->gname);
06692          AST_LIST_TRAVERSE(&fg->features, fge, entry) {
06693             ast_cli(a->fd, "===> --> %s (%s)\n", fge->feature->sname, fge->exten);
06694          }
06695       }
06696       AST_RWLIST_UNLOCK(&feature_groups);
06697    }
06698 
06699    iter = ao2_iterator_init(parkinglots, 0);
06700    while ((curlot = ao2_iterator_next(&iter))) {
06701       ast_cli(a->fd, "\nCall parking (Parking lot: %s)\n", curlot->name);
06702       ast_cli(a->fd, "------------\n");
06703       ast_cli(a->fd,"%-22s:      %s\n", "Parking extension", curlot->cfg.parkext);
06704       ast_cli(a->fd,"%-22s:      %s\n", "Parking context", curlot->cfg.parking_con);
06705       ast_cli(a->fd,"%-22s:      %d-%d\n", "Parked call extensions",
06706          curlot->cfg.parking_start, curlot->cfg.parking_stop);
06707       ast_cli(a->fd,"%-22s:      %d ms\n", "Parkingtime", curlot->cfg.parkingtime);
06708       ast_cli(a->fd,"%-22s:      %s\n", "MusicOnHold class", curlot->cfg.mohclass);
06709       ast_cli(a->fd,"%-22s:      %s\n", "Enabled", AST_CLI_YESNO(!curlot->disabled));
06710       ast_cli(a->fd,"\n");
06711       ao2_ref(curlot, -1);
06712    }
06713    ao2_iterator_destroy(&iter);
06714 
06715    return CLI_SUCCESS;
06716 }
06717 
06718 int ast_features_reload(void)
06719 {
06720    struct ast_context *con;
06721    int res;
06722 
06723    ast_mutex_lock(&features_reload_lock);/* Searialize reloading features.conf */
06724 
06725    /*
06726     * Always destroy the parking_con_dial context to remove buildup
06727     * of recalled extensions in the context.  At worst, the parked
06728     * call gets hungup attempting to run an invalid extension when
06729     * we are trying to callback the parker or the preset return
06730     * extension.  This is a small window of opportunity on an
06731     * execution chain that is not expected to happen very often.
06732     */
06733    con = ast_context_find(parking_con_dial);
06734    if (con) {
06735       ast_context_destroy(con, registrar);
06736    }
06737 
06738    res = load_config(1);
06739    ast_mutex_unlock(&features_reload_lock);
06740 
06741    return res;
06742 }
06743 
06744 static char *handle_features_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06745 {
06746    switch (cmd) { 
06747    case CLI_INIT:
06748       e->command = "features reload";
06749       e->usage =
06750          "Usage: features reload\n"
06751          "       Reloads configured call features from features.conf\n";
06752       return NULL;
06753    case CLI_GENERATE:
06754       return NULL;
06755    }
06756    ast_features_reload();
06757 
06758    return CLI_SUCCESS;
06759 }
06760 
06761 /*!
06762  * \brief Actual bridge
06763  * \param chan
06764  * \param tmpchan
06765  * 
06766  * Stop hold music, lock both channels, masq channels,
06767  * after bridge return channel to next priority.
06768  */
06769 static void do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
06770 {
06771    ast_moh_stop(chan);
06772    ast_channel_lock_both(chan, tmpchan);
06773    ast_setstate(tmpchan, chan->_state);
06774    tmpchan->readformat = chan->readformat;
06775    tmpchan->writeformat = chan->writeformat;
06776    ast_channel_unlock(chan);
06777    ast_channel_unlock(tmpchan);
06778 
06779    ast_channel_masquerade(tmpchan, chan);
06780 
06781    /* must be done without any channel locks held */
06782    ast_do_masquerade(tmpchan);
06783 
06784    /* when returning from bridge, the channel will continue at the next priority */
06785    ast_explicit_goto(tmpchan, chan->context, chan->exten, chan->priority + 1);
06786 }
06787 
06788 /*!
06789  * \brief Bridge channels together
06790  * \param s
06791  * \param m
06792  * 
06793  * Make sure valid channels were specified, 
06794  * send errors if any of the channels could not be found/locked, answer channels if needed,
06795  * create the placeholder channels and grab the other channels 
06796  * make the channels compatible, send error if we fail doing so 
06797  * setup the bridge thread object and start the bridge.
06798  * 
06799  * \retval 0 on success or on incorrect use.
06800  * \retval 1 on failure to bridge channels.
06801  */
06802 static int action_bridge(struct mansession *s, const struct message *m)
06803 {
06804    const char *channela = astman_get_header(m, "Channel1");
06805    const char *channelb = astman_get_header(m, "Channel2");
06806    const char *playtone = astman_get_header(m, "Tone");
06807    struct ast_channel *chana = NULL, *chanb = NULL, *chans[2];
06808    struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
06809    struct ast_bridge_thread_obj *tobj = NULL;
06810 
06811    /* make sure valid channels were specified */
06812    if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
06813       astman_send_error(s, m, "Missing channel parameter in request");
06814       return 0;
06815    }
06816 
06817    /* Start with chana */
06818    chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
06819 
06820    /* send errors if any of the channels could not be found/locked */
06821    if (!chana) {
06822       char buf[256];
06823       snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
06824       astman_send_error(s, m, buf);
06825       return 0;
06826    }
06827 
06828    /* Answer the channels if needed */
06829    if (chana->_state != AST_STATE_UP)
06830       ast_answer(chana);
06831 
06832    /* create the placeholder channels and grab the other channels */
06833    if (!(tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
06834       NULL, NULL, chana->linkedid, 0, "Bridge/%s", chana->name))) {
06835       astman_send_error(s, m, "Unable to create temporary channel!");
06836       chana = ast_channel_unref(chana);
06837       return 1;
06838    }
06839 
06840    do_bridge_masquerade(chana, tmpchana);
06841 
06842    chana = ast_channel_unref(chana);
06843 
06844    /* now do chanb */
06845    chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
06846    /* send errors if any of the channels could not be found/locked */
06847    if (!chanb) {
06848       char buf[256];
06849       snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
06850       ast_hangup(tmpchana);
06851       astman_send_error(s, m, buf);
06852       return 0;
06853    }
06854 
06855    /* Answer the channels if needed */
06856    if (chanb->_state != AST_STATE_UP)
06857       ast_answer(chanb);
06858 
06859    /* create the placeholder channels and grab the other channels */
06860    if (!(tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
06861       NULL, NULL, chanb->linkedid, 0, "Bridge/%s", chanb->name))) {
06862       astman_send_error(s, m, "Unable to create temporary channels!");
06863       ast_hangup(tmpchana);
06864       chanb = ast_channel_unref(chanb);
06865       return 1;
06866    }
06867 
06868    do_bridge_masquerade(chanb, tmpchanb);
06869 
06870    chanb = ast_channel_unref(chanb);
06871 
06872    /* make the channels compatible, send error if we fail doing so */
06873    if (ast_channel_make_compatible(tmpchana, tmpchanb)) {
06874       ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", tmpchana->name, tmpchanb->name);
06875       astman_send_error(s, m, "Could not make channels compatible for manager bridge");
06876       ast_hangup(tmpchana);
06877       ast_hangup(tmpchanb);
06878       return 1;
06879    }
06880 
06881    /* setup the bridge thread object and start the bridge */
06882    if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
06883       ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno));
06884       astman_send_error(s, m, "Unable to spawn a new bridge thread");
06885       ast_hangup(tmpchana);
06886       ast_hangup(tmpchanb);
06887       return 1;
06888    }
06889 
06890    tobj->chan = tmpchana;
06891    tobj->peer = tmpchanb;
06892    tobj->return_to_pbx = 1;
06893 
06894    if (ast_true(playtone)) {
06895       if (!ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, tmpchanb->language)) {
06896          if (ast_waitstream(tmpchanb, "") < 0)
06897             ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", tmpchanb->name);
06898       }
06899    }
06900 
06901    chans[0] = tmpchana;
06902    chans[1] = tmpchanb;
06903 
06904    ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeAction", 2, chans,
06905             "Response: Success\r\n"
06906             "Channel1: %s\r\n"
06907             "Channel2: %s\r\n", tmpchana->name, tmpchanb->name);
06908 
06909    bridge_call_thread_launch(tobj);
06910 
06911    astman_send_ack(s, m, "Launched bridge thread with success");
06912 
06913    return 0;
06914 }
06915 
06916 /*!
06917  * \brief CLI command to list parked calls
06918  * \param e 
06919  * \param cmd
06920  * \param a
06921  *  
06922  * Check right usage, lock parking lot, display parked calls, unlock parking lot list.
06923  * \retval CLI_SUCCESS on success.
06924  * \retval CLI_SHOWUSAGE on incorrect number of arguments.
06925  * \retval NULL when tab completion is used.
06926  */
06927 static char *handle_parkedcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06928 {
06929    struct parkeduser *cur;
06930    int numparked = 0;
06931    struct ao2_iterator iter;
06932    struct ast_parkinglot *curlot;
06933 
06934    switch (cmd) {
06935    case CLI_INIT:
06936       e->command = "parkedcalls show";
06937       e->usage =
06938          "Usage: parkedcalls show\n"
06939          "       List currently parked calls\n";
06940       return NULL;
06941    case CLI_GENERATE:
06942       return NULL;
06943    }
06944 
06945    if (a->argc > e->args)
06946       return CLI_SHOWUSAGE;
06947 
06948    ast_cli(a->fd, "%-10s %-25s (%-15s %-12s %4s) %s\n", "Num", "Channel",
06949       "Context", "Extension", "Pri", "Timeout");
06950 
06951    iter = ao2_iterator_init(parkinglots, 0);
06952    while ((curlot = ao2_iterator_next(&iter))) {
06953       int lotparked = 0;
06954 
06955       /* subtract ref for iterator and for configured parking lot */
06956       ast_cli(a->fd, "*** Parking lot: %s (%d)\n", curlot->name,
06957          ao2_ref(curlot, 0) - 2 - (curlot == default_parkinglot));
06958 
06959       AST_LIST_LOCK(&curlot->parkings);
06960       AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
06961          ast_cli(a->fd, "%-10.10s %-25s (%-15s %-12s %4d) %6lds\n",
06962             cur->parkingexten, cur->chan->name, cur->context, cur->exten,
06963             cur->priority,
06964             (long) (cur->start.tv_sec + (cur->parkingtime / 1000) - time(NULL)));
06965          ++lotparked;
06966       }
06967       AST_LIST_UNLOCK(&curlot->parkings);
06968       if (lotparked) {
06969          numparked += lotparked;
06970          ast_cli(a->fd, "   %d parked call%s in parking lot %s\n", lotparked,
06971             ESS(lotparked), curlot->name);
06972       }
06973 
06974       ao2_ref(curlot, -1);
06975    }
06976    ao2_iterator_destroy(&iter);
06977 
06978    ast_cli(a->fd, "---\n%d parked call%s in total.\n", numparked, ESS(numparked));
06979 
06980    return CLI_SUCCESS;
06981 }
06982 
06983 static struct ast_cli_entry cli_features[] = {
06984    AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
06985    AST_CLI_DEFINE(handle_features_reload, "Reloads configured features"),
06986    AST_CLI_DEFINE(handle_parkedcalls, "List currently parked calls"),
06987 };
06988 
06989 /*! 
06990  * \brief Dump parking lot status
06991  * \param s
06992  * \param m
06993  * 
06994  * Lock parking lot, iterate list and append parked calls status, unlock parking lot.
06995  * \return Always RESULT_SUCCESS 
06996  */
06997 static int manager_parking_status(struct mansession *s, const struct message *m)
06998 {
06999    struct parkeduser *cur;
07000    const char *id = astman_get_header(m, "ActionID");
07001    char idText[256] = "";
07002    struct ao2_iterator iter;
07003    struct ast_parkinglot *curlot;
07004    int numparked = 0;
07005 
07006    if (!ast_strlen_zero(id))
07007       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
07008 
07009    astman_send_ack(s, m, "Parked calls will follow");
07010 
07011    iter = ao2_iterator_init(parkinglots, 0);
07012    while ((curlot = ao2_iterator_next(&iter))) {
07013       AST_LIST_LOCK(&curlot->parkings);
07014       AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
07015          astman_append(s, "Event: ParkedCall\r\n"
07016             "Parkinglot: %s\r\n"
07017             "Exten: %d\r\n"
07018             "Channel: %s\r\n"
07019             "From: %s\r\n"
07020             "Timeout: %ld\r\n"
07021             "CallerIDNum: %s\r\n"
07022             "CallerIDName: %s\r\n"
07023             "ConnectedLineNum: %s\r\n"
07024             "ConnectedLineName: %s\r\n"
07025             "%s"
07026             "\r\n",
07027             curlot->name,
07028             cur->parkingnum, cur->chan->name, cur->peername,
07029             (long) cur->start.tv_sec + (long) (cur->parkingtime / 1000) - (long) time(NULL),
07030             S_COR(cur->chan->caller.id.number.valid, cur->chan->caller.id.number.str, ""),   /* XXX in other places it is <unknown> */
07031             S_COR(cur->chan->caller.id.name.valid, cur->chan->caller.id.name.str, ""),
07032             S_COR(cur->chan->connected.id.number.valid, cur->chan->connected.id.number.str, ""),   /* XXX in other places it is <unknown> */
07033             S_COR(cur->chan->connected.id.name.valid, cur->chan->connected.id.name.str, ""),
07034             idText);
07035          ++numparked;
07036       }
07037       AST_LIST_UNLOCK(&curlot->parkings);
07038       ao2_ref(curlot, -1);
07039    }
07040    ao2_iterator_destroy(&iter);
07041 
07042    astman_append(s,
07043       "Event: ParkedCallsComplete\r\n"
07044       "Total: %d\r\n"
07045       "%s"
07046       "\r\n",
07047       numparked, idText);
07048 
07049    return RESULT_SUCCESS;
07050 }
07051 
07052 /*!
07053  * \brief Create manager event for parked calls
07054  * \param s
07055  * \param m
07056  *
07057  * Get channels involved in park, create event.
07058  * \return Always 0
07059  *
07060  * \note ADSI is not compatible with this AMI action for the
07061  * same reason ch2 can no longer announce the parking space.
07062  */
07063 static int manager_park(struct mansession *s, const struct message *m)
07064 {
07065    const char *channel = astman_get_header(m, "Channel");
07066    const char *channel2 = astman_get_header(m, "Channel2");
07067    const char *timeout = astman_get_header(m, "Timeout");
07068    const char *parkinglotname = astman_get_header(m, "Parkinglot");
07069    char buf[BUFSIZ];
07070    int res = 0;
07071    struct ast_channel *ch1, *ch2;
07072    struct ast_park_call_args args = {
07073          /*
07074           * Don't say anything to ch2 since AMI is a third party parking
07075           * a call and we will likely crash if we do.
07076           *
07077           * XXX When the AMI action was originally implemented, the
07078           * parking space was announced to ch2.  Unfortunately, grabbing
07079           * the ch2 lock and holding it while the announcement is played
07080           * was not really a good thing to do to begin with since it
07081           * could hold up the system.  Also holding the lock is no longer
07082           * possible with a masquerade.
07083           *
07084           * Restoring the announcement to ch2 is not easily doable for
07085           * the following reasons:
07086           *
07087           * 1) The AMI manager is not the thread processing ch2.
07088           *
07089           * 2) ch2 could be the same as ch1, bridged to ch1, or some
07090           * random uninvolved channel.
07091           */
07092          .flags = AST_PARK_OPT_SILENCE,
07093       };
07094 
07095    if (ast_strlen_zero(channel)) {
07096       astman_send_error(s, m, "Channel not specified");
07097       return 0;
07098    }
07099 
07100    if (ast_strlen_zero(channel2)) {
07101       astman_send_error(s, m, "Channel2 not specified");
07102       return 0;
07103    }
07104 
07105    if (!ast_strlen_zero(timeout)) {
07106       if (sscanf(timeout, "%30d", &args.timeout) != 1) {
07107          astman_send_error(s, m, "Invalid timeout value.");
07108          return 0;
07109       }
07110    }
07111 
07112    if (!(ch1 = ast_channel_get_by_name(channel))) {
07113       snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
07114       astman_send_error(s, m, buf);
07115       return 0;
07116    }
07117 
07118    if (!(ch2 = ast_channel_get_by_name(channel2))) {
07119       snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel2);
07120       astman_send_error(s, m, buf);
07121       ast_channel_unref(ch1);
07122       return 0;
07123    }
07124 
07125    if (!ast_strlen_zero(parkinglotname)) {
07126       args.parkinglot = find_parkinglot(parkinglotname);
07127    }
07128 
07129    res = masq_park_call(ch1, ch2, &args);
07130    if (!res) {
07131       ast_softhangup(ch2, AST_SOFTHANGUP_EXPLICIT);
07132       astman_send_ack(s, m, "Park successful");
07133    } else {
07134       astman_send_error(s, m, "Park failure");
07135    }
07136 
07137    if (args.parkinglot) {
07138       parkinglot_unref(args.parkinglot);
07139    }
07140    ch1 = ast_channel_unref(ch1);
07141    ch2 = ast_channel_unref(ch2);
07142 
07143    return 0;
07144 }
07145 
07146 /*!
07147  * The presence of this datastore on the channel indicates that
07148  * someone is attemting to pickup or has picked up the channel.
07149  * The purpose is to prevent a race between two channels
07150  * attempting to pickup the same channel.
07151  */
07152 static const struct ast_datastore_info pickup_active = {
07153    .type = "pickup-active",
07154 };
07155 
07156 int ast_can_pickup(struct ast_channel *chan)
07157 {
07158    if (!chan->pbx && !chan->masq && !ast_test_flag(chan, AST_FLAG_ZOMBIE)
07159       && (chan->_state == AST_STATE_RINGING
07160          || chan->_state == AST_STATE_RING
07161          /*
07162           * Check the down state as well because some SIP devices do not
07163           * give 180 ringing when they can just give 183 session progress
07164           * instead.  Issue 14005.  (Some ISDN switches as well for that
07165           * matter.)
07166           */
07167          || chan->_state == AST_STATE_DOWN)
07168       && !ast_channel_datastore_find(chan, &pickup_active, NULL)) {
07169       return 1;
07170    }
07171    return 0;
07172 }
07173 
07174 static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
07175 {
07176    struct ast_channel *target = obj;/*!< Potential pickup target */
07177    struct ast_channel *chan = data;/*!< Channel wanting to pickup call */
07178 
07179    ast_channel_lock(target);
07180    if (chan != target && (chan->pickupgroup & target->callgroup)
07181       && ast_can_pickup(target)) {
07182       /* Return with the channel still locked on purpose */
07183       return CMP_MATCH | CMP_STOP;
07184    }
07185    ast_channel_unlock(target);
07186 
07187    return 0;
07188 }
07189 
07190 /*!
07191  * \brief Pickup a call
07192  * \param chan channel that initiated pickup.
07193  *
07194  * Walk list of channels, checking it is not itself, channel is pbx one,
07195  * check that the callgroup for both channels are the same and the channel is ringing.
07196  * Answer calling channel, flag channel as answered on queue, masq channels together.
07197  */
07198 int ast_pickup_call(struct ast_channel *chan)
07199 {
07200    struct ast_channel *target;/*!< Potential pickup target */
07201    int res = -1;
07202    ast_debug(1, "pickup attempt by %s\n", chan->name);
07203 
07204    /* The found channel is already locked. */
07205    target = ast_channel_callback(find_channel_by_group, NULL, chan, 0);
07206    if (target) {
07207       ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", target->name, chan->name);
07208 
07209       res = ast_do_pickup(chan, target);
07210       ast_channel_unlock(target);
07211       if (!res) {
07212          if (!ast_strlen_zero(pickupsound)) {
07213             pbx_builtin_setvar_helper(target, "BRIDGE_PLAY_SOUND", pickupsound);
07214          }
07215       } else {
07216          ast_log(LOG_WARNING, "pickup %s failed by %s\n", target->name, chan->name);
07217       }
07218       target = ast_channel_unref(target);
07219    }
07220 
07221    if (res < 0) {
07222       ast_debug(1, "No call pickup possible... for %s\n", chan->name);
07223       if (!ast_strlen_zero(pickupfailsound)) {
07224          ast_answer(chan);
07225          ast_stream_and_wait(chan, pickupfailsound, "");
07226       }
07227    }
07228 
07229    return res;
07230 }
07231 
07232 int ast_do_pickup(struct ast_channel *chan, struct ast_channel *target)
07233 {
07234    struct ast_party_connected_line connected_caller;
07235    struct ast_channel *chans[2] = { chan, target };
07236    struct ast_datastore *ds_pickup;
07237    const char *chan_name;/*!< A masquerade changes channel names. */
07238    const char *target_name;/*!< A masquerade changes channel names. */
07239    int res = -1;
07240 
07241    target_name = ast_strdupa(target->name);
07242    ast_debug(1, "Call pickup on '%s' by '%s'\n", target_name, chan->name);
07243 
07244    /* Mark the target to block any call pickup race. */
07245    ds_pickup = ast_datastore_alloc(&pickup_active, NULL);
07246    if (!ds_pickup) {
07247       ast_log(LOG_WARNING,
07248          "Unable to create channel datastore on '%s' for call pickup\n", target_name);
07249       return -1;
07250    }
07251    ast_channel_datastore_add(target, ds_pickup);
07252 
07253    ast_party_connected_line_init(&connected_caller);
07254    ast_party_connected_line_copy(&connected_caller, &target->connected);
07255    ast_channel_unlock(target);/* The pickup race is avoided so we do not need the lock anymore. */
07256    connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
07257    if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
07258       ast_channel_update_connected_line(chan, &connected_caller, NULL);
07259    }
07260    ast_party_connected_line_free(&connected_caller);
07261 
07262    ast_channel_lock(chan);
07263    chan_name = ast_strdupa(chan->name);
07264    ast_connected_line_copy_from_caller(&connected_caller, &chan->caller);
07265    ast_channel_unlock(chan);
07266    connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
07267    ast_channel_queue_connected_line_update(chan, &connected_caller, NULL);
07268    ast_party_connected_line_free(&connected_caller);
07269 
07270    ast_cel_report_event(target, AST_CEL_PICKUP, NULL, NULL, chan);
07271 
07272    if (ast_answer(chan)) {
07273       ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan_name);
07274       goto pickup_failed;
07275    }
07276 
07277    if (ast_queue_control(chan, AST_CONTROL_ANSWER)) {
07278       ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan_name);
07279       goto pickup_failed;
07280    }
07281    
07282    /* setting this flag to generate a reason header in the cancel message to the ringing channel */
07283    ast_set_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE);
07284 
07285    if (ast_channel_masquerade(target, chan)) {
07286       ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan_name,
07287          target_name);
07288       goto pickup_failed;
07289    }
07290 
07291    /* If you want UniqueIDs, set channelvars in manager.conf to CHANNEL(uniqueid) */
07292    ast_manager_event_multichan(EVENT_FLAG_CALL, "Pickup", 2, chans,
07293       "Channel: %s\r\n"
07294       "TargetChannel: %s\r\n",
07295       chan_name, target_name);
07296 
07297    /* Do the masquerade manually to make sure that it is completed. */
07298    ast_do_masquerade(target);
07299    res = 0;
07300 
07301 pickup_failed:
07302    ast_channel_lock(target);
07303    if (!ast_channel_datastore_remove(target, ds_pickup)) {
07304       ast_datastore_free(ds_pickup);
07305    }
07306 
07307    return res;
07308 }
07309 
07310 static char *app_bridge = "Bridge";
07311 
07312 enum {
07313    BRIDGE_OPT_PLAYTONE = (1 << 0),
07314    OPT_CALLEE_HANGUP =  (1 << 1),
07315    OPT_CALLER_HANGUP =  (1 << 2),
07316    OPT_DURATION_LIMIT = (1 << 3),
07317    OPT_DURATION_STOP =  (1 << 4),
07318    OPT_CALLEE_TRANSFER = (1 << 5),
07319    OPT_CALLER_TRANSFER = (1 << 6),
07320    OPT_CALLEE_MONITOR = (1 << 7),
07321    OPT_CALLER_MONITOR = (1 << 8),
07322    OPT_CALLEE_PARK = (1 << 9),
07323    OPT_CALLER_PARK = (1 << 10),
07324    OPT_CALLEE_KILL = (1 << 11),
07325 };
07326  
07327 enum {
07328    OPT_ARG_DURATION_LIMIT = 0,
07329    OPT_ARG_DURATION_STOP,
07330    /* note: this entry _MUST_ be the last one in the enum */
07331    OPT_ARG_ARRAY_SIZE,
07332 };
07333 
07334 AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
07335    AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
07336    AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
07337    AST_APP_OPTION('H', OPT_CALLER_HANGUP),
07338    AST_APP_OPTION('k', OPT_CALLEE_PARK),
07339    AST_APP_OPTION('K', OPT_CALLER_PARK),
07340    AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
07341    AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
07342    AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
07343    AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
07344    AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
07345    AST_APP_OPTION('W', OPT_CALLER_MONITOR),
07346    AST_APP_OPTION('x', OPT_CALLEE_KILL),
07347 END_OPTIONS );
07348 
07349 int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
07350    char *parse, struct timeval *calldurationlimit)
07351 {
07352    char *stringp = ast_strdupa(parse);
07353    char *limit_str, *warning_str, *warnfreq_str;
07354    const char *var;
07355    int play_to_caller = 0, play_to_callee = 0;
07356    int delta;
07357 
07358    limit_str = strsep(&stringp, ":");
07359    warning_str = strsep(&stringp, ":");
07360    warnfreq_str = strsep(&stringp, ":");
07361 
07362    config->timelimit = atol(limit_str);
07363    if (warning_str)
07364       config->play_warning = atol(warning_str);
07365    if (warnfreq_str)
07366       config->warning_freq = atol(warnfreq_str);
07367 
07368    if (!config->timelimit) {
07369       ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
07370       config->timelimit = config->play_warning = config->warning_freq = 0;
07371       config->warning_sound = NULL;
07372       return -1; /* error */
07373    } else if ( (delta = config->play_warning - config->timelimit) > 0) {
07374       int w = config->warning_freq;
07375 
07376       /*
07377        * If the first warning is requested _after_ the entire call
07378        * would end, and no warning frequency is requested, then turn
07379        * off the warning. If a warning frequency is requested, reduce
07380        * the 'first warning' time by that frequency until it falls
07381        * within the call's total time limit.
07382        *
07383        * Graphically:
07384        *                timelim->|    delta        |<-playwarning
07385        *      0__________________|_________________|
07386        *                       | w  |    |    |    |
07387        *
07388        * so the number of intervals to cut is 1+(delta-1)/w
07389        */
07390       if (w == 0) {
07391          config->play_warning = 0;
07392       } else {
07393          config->play_warning -= w * ( 1 + (delta-1)/w );
07394          if (config->play_warning < 1)
07395             config->play_warning = config->warning_freq = 0;
07396       }
07397    }
07398    
07399    ast_channel_lock(chan);
07400 
07401    var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
07402    play_to_caller = var ? ast_true(var) : 1;
07403 
07404    var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
07405    play_to_callee = var ? ast_true(var) : 0;
07406 
07407    if (!play_to_caller && !play_to_callee)
07408       play_to_caller = 1;
07409 
07410    var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
07411    config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
07412 
07413    /* The code looking at config wants a NULL, not just "", to decide
07414     * that the message should not be played, so we replace "" with NULL.
07415     * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
07416     * not found.
07417     */
07418 
07419    var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
07420    config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
07421 
07422    var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
07423    config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
07424 
07425    ast_channel_unlock(chan);
07426 
07427    /* undo effect of S(x) in case they are both used */
07428    calldurationlimit->tv_sec = 0;
07429    calldurationlimit->tv_usec = 0;
07430 
07431    /* more efficient to do it like S(x) does since no advanced opts */
07432    if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
07433       calldurationlimit->tv_sec = config->timelimit / 1000;
07434       calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
07435       ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
07436          calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
07437       config->timelimit = play_to_caller = play_to_callee =
07438       config->play_warning = config->warning_freq = 0;
07439    } else {
07440       ast_verb(4, "Limit Data for this call:\n");
07441       ast_verb(4, "timelimit      = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
07442       ast_verb(4, "play_warning   = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
07443       ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
07444       ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
07445       ast_verb(4, "warning_freq   = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
07446       ast_verb(4, "start_sound    = %s\n", S_OR(config->start_sound, ""));
07447       ast_verb(4, "warning_sound  = %s\n", config->warning_sound);
07448       ast_verb(4, "end_sound      = %s\n", S_OR(config->end_sound, ""));
07449    }
07450    if (play_to_caller)
07451       ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
07452    if (play_to_callee)
07453       ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
07454    return 0;
07455 }
07456 
07457 
07458 /*!
07459  * \brief Bridge channels
07460  * \param chan
07461  * \param data channel to bridge with.
07462  * 
07463  * Split data, check we aren't bridging with ourself, check valid channel,
07464  * answer call if not already, check compatible channels, setup bridge config
07465  * now bridge call, if transfered party hangs up return to PBX extension.
07466  */
07467 static int bridge_exec(struct ast_channel *chan, const char *data)
07468 {
07469    struct ast_channel *current_dest_chan, *final_dest_chan, *chans[2];
07470    char *tmp_data  = NULL;
07471    struct ast_flags opts = { 0, };
07472    struct ast_bridge_config bconfig = { { 0, }, };
07473    char *opt_args[OPT_ARG_ARRAY_SIZE];
07474    struct timeval calldurationlimit = { 0, };
07475 
07476    AST_DECLARE_APP_ARGS(args,
07477       AST_APP_ARG(dest_chan);
07478       AST_APP_ARG(options);
07479    );
07480    
07481    if (ast_strlen_zero(data)) {
07482       ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
07483       return -1;
07484    }
07485 
07486    tmp_data = ast_strdupa(data);
07487    AST_STANDARD_APP_ARGS(args, tmp_data);
07488    if (!ast_strlen_zero(args.options))
07489       ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
07490 
07491    /* avoid bridge with ourselves */
07492    if (!strcmp(chan->name, args.dest_chan)) {
07493       ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", chan->name);
07494       ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07495                "Response: Failed\r\n"
07496                "Reason: Unable to bridge channel to itself\r\n"
07497                "Channel1: %s\r\n"
07498                "Channel2: %s\r\n",
07499                chan->name, args.dest_chan);
07500       pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
07501       return 0;
07502    }
07503 
07504    /* make sure we have a valid end point */
07505    if (!(current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
07506          strlen(args.dest_chan)))) {
07507       ast_log(LOG_WARNING, "Bridge failed because channel %s does not exists or we "
07508          "cannot get its lock\n", args.dest_chan);
07509       ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07510                "Response: Failed\r\n"
07511                "Reason: Cannot grab end point\r\n"
07512                "Channel1: %s\r\n"
07513                "Channel2: %s\r\n", chan->name, args.dest_chan);
07514       pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
07515       return 0;
07516    }
07517 
07518    /* answer the channel if needed */
07519    if (current_dest_chan->_state != AST_STATE_UP) {
07520       ast_answer(current_dest_chan);
07521    }
07522 
07523    /* try to allocate a place holder where current_dest_chan will be placed */
07524    if (!(final_dest_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
07525       NULL, NULL, current_dest_chan->linkedid, 0, "Bridge/%s", current_dest_chan->name))) {
07526       ast_log(LOG_WARNING, "Cannot create placeholder channel for chan %s\n", args.dest_chan);
07527       ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07528                "Response: Failed\r\n"
07529                "Reason: cannot create placeholder\r\n"
07530                "Channel1: %s\r\n"
07531                "Channel2: %s\r\n", chan->name, args.dest_chan);
07532    }
07533 
07534    do_bridge_masquerade(current_dest_chan, final_dest_chan);
07535 
07536    chans[0] = current_dest_chan;
07537    chans[1] = final_dest_chan;
07538 
07539    /* now current_dest_chan is a ZOMBIE and with softhangup set to 1 and final_dest_chan is our end point */
07540    /* try to make compatible, send error if we fail */
07541    if (ast_channel_make_compatible(chan, final_dest_chan) < 0) {
07542       ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, final_dest_chan->name);
07543       ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeExec", 2, chans,
07544                "Response: Failed\r\n"
07545                "Reason: Could not make channels compatible for bridge\r\n"
07546                "Channel1: %s\r\n"
07547                "Channel2: %s\r\n", chan->name, final_dest_chan->name);
07548       ast_hangup(final_dest_chan); /* may be we should return this channel to the PBX? */
07549       pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE");
07550       current_dest_chan = ast_channel_unref(current_dest_chan);
07551       return 0;
07552    }
07553 
07554    /* Report that the bridge will be successfull */
07555    ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeExec", 2, chans,
07556             "Response: Success\r\n"
07557             "Channel1: %s\r\n"
07558             "Channel2: %s\r\n", chan->name, final_dest_chan->name);
07559 
07560    /* we have 2 valid channels to bridge, now it is just a matter of setting up the bridge config and starting the bridge */  
07561    if (ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE) && !ast_strlen_zero(xfersound)) {
07562       if (!ast_streamfile(final_dest_chan, xfersound, final_dest_chan->language)) {
07563          if (ast_waitstream(final_dest_chan, "") < 0)
07564             ast_log(LOG_WARNING, "Failed to play courtesy tone on %s\n", final_dest_chan->name);
07565       }
07566    }
07567    
07568    current_dest_chan = ast_channel_unref(current_dest_chan);
07569    
07570    if (ast_test_flag(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
07571       if (ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
07572          goto done;
07573    }
07574 
07575    if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
07576       ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
07577    if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
07578       ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
07579    if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
07580       ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
07581    if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
07582       ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
07583    if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
07584       ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
07585    if (ast_test_flag(&opts, OPT_CALLER_MONITOR)) 
07586       ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
07587    if (ast_test_flag(&opts, OPT_CALLEE_PARK))
07588       ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
07589    if (ast_test_flag(&opts, OPT_CALLER_PARK))
07590       ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
07591 
07592    ast_bridge_call(chan, final_dest_chan, &bconfig);
07593 
07594    /* the bridge has ended, set BRIDGERESULT to SUCCESS. If the other channel has not been hung up, return it to the PBX */
07595    pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
07596    if (!ast_check_hangup(final_dest_chan) && !ast_test_flag(&opts, OPT_CALLEE_KILL)) {
07597       ast_debug(1, "starting new PBX in %s,%s,%d for chan %s\n", 
07598          final_dest_chan->context, final_dest_chan->exten, 
07599          final_dest_chan->priority, final_dest_chan->name);
07600 
07601       if (ast_pbx_start(final_dest_chan) != AST_PBX_SUCCESS) {
07602          ast_log(LOG_WARNING, "FAILED continuing PBX on dest chan %s\n", final_dest_chan->name);
07603          ast_hangup(final_dest_chan);
07604       } else
07605          ast_debug(1, "SUCCESS continuing PBX on chan %s\n", final_dest_chan->name);
07606    } else {
07607       ast_debug(1, "hangup chan %s since the other endpoint has hung up or the x flag was passed\n", final_dest_chan->name);
07608       ast_hangup(final_dest_chan);
07609    }
07610 done:
07611    if (bconfig.warning_sound) {
07612       ast_free((char *)bconfig.warning_sound);
07613    }
07614    if (bconfig.end_sound) {
07615       ast_free((char *)bconfig.end_sound);
07616    }
07617    if (bconfig.start_sound) {
07618       ast_free((char *)bconfig.start_sound);
07619    }
07620 
07621    return 0;
07622 }
07623 
07624 #if defined(TEST_FRAMEWORK)
07625 /*!
07626  * \internal
07627  * \brief Convert parking spaces map list to a comma separated string.
07628  *
07629  * \param str String buffer to fill.
07630  * \param spaces Parking spaces map list to convert.
07631  *
07632  * \return Nothing
07633  */
07634 static void create_spaces_str(struct ast_str **str, struct parking_dp_space_map *spaces)
07635 {
07636    const char *comma;
07637    struct parking_dp_spaces *cur;
07638 
07639    ast_str_reset(*str);
07640    comma = "";
07641    AST_LIST_TRAVERSE(spaces, cur, node) {
07642       if (cur->start == cur->stop) {
07643          ast_str_append(str, 0, "%s%d", comma, cur->start);
07644       } else {
07645          ast_str_append(str, 0, "%s%d-%d", comma, cur->start, cur->stop);
07646       }
07647       comma = ",";
07648    }
07649 }
07650 #endif   /* defined(TEST_FRAMEWORK) */
07651 
07652 #if defined(TEST_FRAMEWORK)
07653 /*!
07654  * \internal
07655  * \brief Compare parking spaces map to what is expected.
07656  *
07657  * \param test Unit test context.
07658  * \param spaces Parking spaces map list to check.
07659  * \param expected String to compare with.
07660  * \param what What is being compared.
07661  *
07662  * \retval 0 successful compare.
07663  * \retval nonzero if failed to compare.
07664  */
07665 static int check_spaces(struct ast_test *test, struct parking_dp_space_map *spaces, const char *expected, const char *what)
07666 {
07667    int cmp;
07668    struct ast_str *str = ast_str_alloca(1024);
07669 
07670    create_spaces_str(&str, spaces);
07671    cmp = strcmp(expected, ast_str_buffer(str));
07672    if (cmp) {
07673       ast_test_status_update(test,
07674          "Unexpected parking space map for %s. Expect:'%s' Got:'%s'\n",
07675          what, expected, ast_str_buffer(str));
07676    }
07677    return cmp;
07678 }
07679 #endif   /* defined(TEST_FRAMEWORK) */
07680 
07681 #if defined(TEST_FRAMEWORK)
07682 /*!
07683  * \internal
07684  * \brief Add a dead space to the dead spaces list.
07685  *
07686  * \param context Dead spaces list ptr pretending to be a context name ptr.
07687  * \param space Dead space to add to the list.
07688  *
07689  * \return Nothing
07690  */
07691 static void test_add_dead_space(const char *context, int space)
07692 {
07693    struct parking_dp_space_map *dead_spaces = (struct parking_dp_space_map *) context;
07694 
07695    usage_context_add_spaces(dead_spaces, space, space, NULL, 0);
07696 }
07697 #endif   /* defined(TEST_FRAMEWORK) */
07698 
07699 #if defined(TEST_FRAMEWORK)
07700 struct test_map {
07701    const char *ramp;
07702    int start;
07703    int stop;
07704    const char *expect;
07705 };
07706 
07707 /*!
07708  * \internal
07709  * \brief Build a parking lot dialplan usage test map from a table.
07710  *
07711  * \param test Unit test context.
07712  * \param lot Parking lot to use to build test usage map.
07713  * \param table_name Name of passed in table.
07714  * \param table Usage information to put in the usage map.
07715  * \param num_entries Number of entries in the table.
07716  *
07717  * \retval Created context node on success.
07718  * \retval NULL on error.
07719  */
07720 static struct parking_dp_context *test_build_maps(struct ast_test *test,
07721    struct ast_parkinglot *lot, const char *table_name, const struct test_map *table,
07722    size_t num_entries)
07723 {
07724    struct parking_dp_context *ctx_node;
07725    int cur_index = 0;
07726    char what[40];
07727 
07728    snprintf(what, sizeof(what), "%s[%d]", table_name, cur_index);
07729    ast_copy_string(lot->cfg.parkext, table->ramp, sizeof(lot->cfg.parkext));
07730    lot->cfg.parking_start = table->start;
07731    lot->cfg.parking_stop = table->stop;
07732    ctx_node = build_dialplan_useage_context(lot);
07733    if (!ctx_node) {
07734       ast_test_status_update(test, "Failed to create parking lot context map for %s\n",
07735          what);
07736       return NULL;
07737    }
07738    if (check_spaces(test, &ctx_node->spaces, table->expect, what)) {
07739       destroy_dialplan_usage_context(ctx_node);
07740       return NULL;
07741    }
07742    while (--num_entries) {
07743       ++cur_index;
07744       ++table;
07745       snprintf(what, sizeof(what), "%s[%d]", table_name, cur_index);
07746       ast_copy_string(lot->cfg.parkext, table->ramp, sizeof(lot->cfg.parkext));
07747       lot->cfg.parking_start = table->start;
07748       lot->cfg.parking_stop = table->stop;
07749       if (dialplan_usage_add_parkinglot_data(ctx_node, lot, 1)) {
07750          ast_test_status_update(test, "Failed to add parking lot data for %s\n", what);
07751          destroy_dialplan_usage_context(ctx_node);
07752          return NULL;
07753       }
07754       if (check_spaces(test, &ctx_node->spaces, table->expect, what)) {
07755          destroy_dialplan_usage_context(ctx_node);
07756          return NULL;
07757       }
07758    }
07759    return ctx_node;
07760 }
07761 
07762 static const struct test_map test_old_ctx[] = {
07763    /* The following order of building ctx is important to test adding items to the lists. */
07764    { "702", 14, 15, "14-15" },
07765    { "700", 10, 11, "10-11,14-15" },
07766    { "701", 18, 19, "10-11,14-15,18-19" },
07767    { "703", 12, 13, "10-15,18-19" },
07768    { "704", 16, 17, "10-19" },
07769 
07770    /* Parking ramp and space conflicts are intended with these lines. */
07771    { "704", 9, 19, "9-19" },
07772    { "704", 9, 20, "9-20" },
07773    { "704", 8, 21, "8-21" },
07774 
07775    /* Add more spaces to ctx to test removing dead parking spaces. */
07776    { "705", 23, 25, "8-21,23-25" },
07777    { "706", 28, 31, "8-21,23-25,28-31" },
07778    { "707", 33, 34, "8-21,23-25,28-31,33-34" },
07779    { "708", 38, 40, "8-21,23-25,28-31,33-34,38-40" },
07780    { "709", 42, 43, "8-21,23-25,28-31,33-34,38-40,42-43" },
07781 };
07782 
07783 static const struct test_map test_new_ctx[] = {
07784    { "702", 4, 5, "4-5" },
07785    { "704", 24, 26, "4-5,24-26" },
07786    { "709", 29, 30, "4-5,24-26,29-30" },
07787    { "710", 32, 35, "4-5,24-26,29-30,32-35" },
07788    { "711", 37, 39, "4-5,24-26,29-30,32-35,37-39" },
07789 };
07790 #endif   /* defined(TEST_FRAMEWORK) */
07791 
07792 #if defined(TEST_FRAMEWORK)
07793 /*!
07794  * \internal
07795  * \brief Test parking dialplan usage map code.
07796  *
07797  * \param test Unit test context.
07798  *
07799  * \retval 0 on success.
07800  * \retval -1 on error.
07801  */
07802 static int test_dialplan_usage_map(struct ast_test *test)
07803 {
07804    struct parking_dp_context *old_ctx;
07805    struct parking_dp_context *new_ctx;
07806    struct ast_parkinglot *lot;
07807    struct parking_dp_spaces *spaces;
07808    struct parking_dp_space_map dead_spaces = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
07809    int res;
07810 
07811    ast_test_status_update(test, "Test parking dialplan usage map code\n");
07812 
07813    lot = create_parkinglot("test_lot");
07814    if (!lot) {
07815       return -1;
07816    }
07817    ast_copy_string(lot->cfg.parking_con, "test-ctx", sizeof(lot->cfg.parking_con));
07818    lot->cfg.parkext_exclusive = 1;
07819 
07820    ast_test_status_update(test,
07821       "Build old_ctx map\n");
07822    ast_log(LOG_NOTICE, "6 Ramp and space conflict warnings are expected.\n");
07823    old_ctx = test_build_maps(test, lot, "test_old_ctx", test_old_ctx,
07824       ARRAY_LEN(test_old_ctx));
07825    if (!old_ctx) {
07826       ao2_ref(lot, -1);
07827       return -1;
07828    }
07829 
07830    ast_test_status_update(test, "Build new_ctx map\n");
07831    new_ctx = test_build_maps(test, lot, "test_new_ctx", test_new_ctx,
07832       ARRAY_LEN(test_new_ctx));
07833    if (!new_ctx) {
07834       res = -1;
07835       goto fail_old_ctx;
07836    }
07837 
07838    ast_test_status_update(test, "Test removing dead parking spaces\n");
07839    remove_dead_spaces_usage((void *) &dead_spaces, &old_ctx->spaces,
07840       &new_ctx->spaces, test_add_dead_space);
07841    if (check_spaces(test, &dead_spaces, "8-21,23,28,31,40,42-43", "dead_spaces")) {
07842       res = -1;
07843       goto fail_dead_spaces;
07844    }
07845 
07846    res = 0;
07847 
07848 fail_dead_spaces:
07849    while ((spaces = AST_LIST_REMOVE_HEAD(&dead_spaces, node))) {
07850       ast_free(spaces);
07851    }
07852    destroy_dialplan_usage_context(new_ctx);
07853 
07854 fail_old_ctx:
07855    destroy_dialplan_usage_context(old_ctx);
07856    ao2_ref(lot, -1);
07857    return res;
07858 }
07859 #endif   /* defined(TEST_FRAMEWORK) */
07860 
07861 #if defined(TEST_FRAMEWORK)
07862 static int fake_fixup(struct ast_channel *clonechan, struct ast_channel *original)
07863 {
07864    return 0;
07865 }
07866 #endif   /* defined(TEST_FRAMEWORK) */
07867 
07868 #if defined(TEST_FRAMEWORK)
07869 static struct ast_channel *create_test_channel(const struct ast_channel_tech *fake_tech)
07870 {
07871    struct ast_channel *test_channel1;
07872 
07873    if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
07874       NULL, NULL, 0, 0, "TestChannel1"))) {
07875       ast_log(LOG_WARNING, "Whoa, test channel creation failed.\n");
07876       return NULL;
07877    }
07878 
07879    /* normally this is done in the channel driver */
07880    test_channel1->nativeformats = AST_FORMAT_GSM;
07881    test_channel1->writeformat = AST_FORMAT_GSM;
07882    test_channel1->rawwriteformat = AST_FORMAT_GSM;
07883    test_channel1->readformat = AST_FORMAT_GSM;
07884    test_channel1->rawreadformat = AST_FORMAT_GSM;
07885    test_channel1->tech = fake_tech;
07886 
07887    return test_channel1;
07888 }
07889 #endif   /* defined(TEST_FRAMEWORK) */
07890 
07891 #if defined(TEST_FRAMEWORK)
07892 static int unpark_test_channel(struct ast_channel *toremove, struct ast_park_call_args *args)
07893 {
07894    struct ast_context *con;
07895    struct parkeduser *pu_toremove;
07896    int res = 0;
07897 
07898    args->pu->notquiteyet = 1; /* go ahead and stop processing the test parking */
07899 
07900    AST_LIST_LOCK(&args->pu->parkinglot->parkings);
07901    AST_LIST_TRAVERSE_SAFE_BEGIN(&args->pu->parkinglot->parkings, pu_toremove, list) {
07902       if (pu_toremove == args->pu) {
07903          AST_LIST_REMOVE_CURRENT(list);
07904          break;
07905       }
07906    }
07907    AST_LIST_TRAVERSE_SAFE_END;
07908    AST_LIST_UNLOCK(&args->pu->parkinglot->parkings);
07909 
07910    if (!pu_toremove) {
07911       ast_log(LOG_WARNING, "Whoa, could not find parking test call!\n");
07912       return -1;
07913    }
07914 
07915    con = ast_context_find(args->pu->parkinglot->cfg.parking_con);
07916    if (con) {
07917       if (ast_context_remove_extension2(con, args->pu->parkingexten, 1, NULL, 0)) {
07918          ast_log(LOG_WARNING, "Whoa, failed to remove the parking extension!\n");
07919          res = -1;
07920       } else {
07921          notify_metermaids(args->pu->parkingexten,
07922             pu_toremove->parkinglot->cfg.parking_con, AST_DEVICE_NOT_INUSE);
07923       }
07924    } else {
07925       ast_log(LOG_WARNING, "Whoa, no parking context?\n");
07926       res = -1;
07927    }
07928 
07929    parkinglot_unref(pu_toremove->parkinglot);
07930    ast_free(pu_toremove);
07931    args->pu = NULL;
07932 
07933    if (!res && toremove) {
07934       ast_hangup(toremove);
07935    }
07936    return res;
07937 }
07938 #endif   /* defined(TEST_FRAMEWORK) */
07939 
07940 #if defined(TEST_FRAMEWORK)
07941 AST_TEST_DEFINE(features_test)
07942 {
07943    struct ast_channel *test_channel1 = NULL;
07944    struct ast_channel *parked_chan = NULL;
07945    struct ast_parkinglot *dynlot;
07946    struct ast_park_call_args args = {
07947       .timeout = DEFAULT_PARK_TIME,
07948    };
07949 
07950    int res = 0;
07951 
07952    static const struct ast_channel_tech fake_tech = {
07953       .fixup = fake_fixup, /* silence warning from masquerade */
07954    };
07955 
07956    static const char unique_lot_1[] = "myuniquetestparkinglot314";
07957    static const char unique_lot_2[] = "myuniquetestparkinglot3141592654";
07958    static const char unique_context_1[] = "myuniquetestcontext314";
07959    static const char unique_context_2[] = "myuniquetestcontext3141592654";
07960    static const char parkinglot_parkext[] = "750";
07961    static const char parkinglot_range[] = "751-760";
07962 
07963    switch (cmd) {
07964    case TEST_INIT:
07965       info->name = "features_test";
07966       info->category = "/main/features/";
07967       info->summary = "Features unit test";
07968       info->description =
07969          "Tests whether parking respects PARKINGLOT settings";
07970       return AST_TEST_NOT_RUN;
07971    case TEST_EXECUTE:
07972       break;
07973    }
07974 
07975    if (test_dialplan_usage_map(test)) {
07976       res = -1;
07977       goto exit_features_test;
07978    }
07979 
07980    /* changing a config option is a bad practice, but must be done in this case */
07981    parkeddynamic = 1;
07982 
07983    ast_test_status_update(test, "Test parking functionality with defaults\n");
07984    if (!(test_channel1 = create_test_channel(&fake_tech))) {
07985       res = -1;
07986       goto exit_features_test;
07987    }
07988    if (park_call_full(test_channel1, NULL, &args)) {
07989       res = -1;
07990       goto exit_features_test;
07991    }
07992    if (unpark_test_channel(test_channel1, &args)) {
07993       res = -1;
07994       goto exit_features_test;
07995    }
07996 
07997 
07998    ast_test_status_update(test, "Check that certain parking options are respected\n");
07999    if (!(test_channel1 = create_test_channel(&fake_tech))) {
08000       res = -1;
08001       goto exit_features_test;
08002    }
08003    pbx_builtin_setvar_helper(test_channel1, "PARKINGLOT", unique_lot_1);
08004    pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNCONTEXT", unique_context_1);
08005    pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNEXTEN", parkinglot_parkext);
08006    pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNPOS", parkinglot_range);
08007    if (park_call_full(test_channel1, NULL, &args)) {
08008       res = -1;
08009       goto exit_features_test;
08010    }
08011    /* grab newly created parking lot for destruction in the end */
08012    dynlot = args.pu->parkinglot;
08013    if (args.pu->parkingnum != 751
08014       || strcmp(dynlot->name, unique_lot_1)
08015       || strcmp(dynlot->cfg.parking_con, unique_context_1)
08016       || strcmp(dynlot->cfg.parkext, parkinglot_parkext)
08017       || dynlot->cfg.parking_start != 751
08018       || dynlot->cfg.parking_stop != 760) {
08019       ast_test_status_update(test, "Parking settings were not respected\n");
08020       ast_test_status_update(test, "Dyn-name:%s\n", dynlot->name);
08021       ast_test_status_update(test, "Dyn-context:%s\n", dynlot->cfg.parking_con);
08022       ast_test_status_update(test, "Dyn-parkext:%s\n", dynlot->cfg.parkext);
08023       ast_test_status_update(test, "Dyn-parkpos:%d-%d\n", dynlot->cfg.parking_start,
08024          dynlot->cfg.parking_stop);
08025       ast_test_status_update(test, "Parked in space:%d\n", args.pu->parkingnum);
08026       if (!unpark_test_channel(test_channel1, &args)) {
08027          test_channel1 = NULL;
08028       }
08029       res = -1;
08030       goto exit_features_test;
08031    } else {
08032       ast_test_status_update(test, "Parking settings for non-masquerading park verified\n");
08033    }
08034    if (unpark_test_channel(test_channel1, &args)) {
08035       res = -1;
08036       goto exit_features_test;
08037    }
08038 
08039 
08040    ast_test_status_update(test, "Check #2 that certain parking options are respected\n");
08041    if (!(test_channel1 = create_test_channel(&fake_tech))) {
08042       res = -1;
08043       goto exit_features_test;
08044    }
08045    pbx_builtin_setvar_helper(test_channel1, "PARKINGLOT", unique_lot_2);
08046    pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNCONTEXT", unique_context_2);
08047    pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNEXTEN", parkinglot_parkext);
08048    pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNPOS", parkinglot_range);
08049    if (masq_park_call(test_channel1, NULL, &args)) {
08050       res = -1;
08051       goto exit_features_test;
08052    }
08053    /* hangup zombie channel */
08054    ast_hangup(test_channel1);
08055    test_channel1 = NULL;
08056 
08057    dynlot = args.pu->parkinglot;
08058    if (args.pu->parkingnum != 751
08059       || strcmp(dynlot->name, unique_lot_2)
08060       || strcmp(dynlot->cfg.parking_con, unique_context_2)
08061       || strcmp(dynlot->cfg.parkext, parkinglot_parkext)
08062       || dynlot->cfg.parking_start != 751
08063       || dynlot->cfg.parking_stop != 760) {
08064       ast_test_status_update(test, "Parking settings were not respected\n");
08065       ast_test_status_update(test, "Dyn-name:%s\n", dynlot->name);
08066       ast_test_status_update(test, "Dyn-context:%s\n", dynlot->cfg.parking_con);
08067       ast_test_status_update(test, "Dyn-parkext:%s\n", dynlot->cfg.parkext);
08068       ast_test_status_update(test, "Dyn-parkpos:%d-%d\n", dynlot->cfg.parking_start,
08069          dynlot->cfg.parking_stop);
08070       ast_test_status_update(test, "Parked in space:%d\n", args.pu->parkingnum);
08071       res = -1;
08072    } else {
08073       ast_test_status_update(test, "Parking settings for masquerading park verified\n");
08074    }
08075 
08076    /* find the real channel */
08077    parked_chan = ast_channel_get_by_name("TestChannel1");
08078    if (unpark_test_channel(parked_chan, &args)) {
08079       if (parked_chan) {
08080          ast_hangup(parked_chan);
08081       }
08082       res = -1;
08083    }
08084 
08085 
08086 exit_features_test:
08087 
08088    if (test_channel1) {
08089       ast_hangup(test_channel1);
08090    }
08091 
08092    force_reload_load = 1;
08093    ast_features_reload();
08094    return res ? AST_TEST_FAIL : AST_TEST_PASS;
08095 }
08096 #endif   /* defined(TEST_FRAMEWORK) */
08097 
08098 int ast_features_init(void)
08099 {
08100    int res;
08101 
08102    parkinglots = ao2_container_alloc(7, parkinglot_hash_cb, parkinglot_cmp_cb);
08103    if (!parkinglots) {
08104       return -1;
08105    }
08106 
08107    res = load_config(0);
08108    if (res) {
08109       return res;
08110    }
08111    ast_cli_register_multiple(cli_features, ARRAY_LEN(cli_features));
08112    ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
08113    ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
08114    res = ast_register_application2(parkedcall, parked_call_exec, NULL, NULL, NULL);
08115    if (!res)
08116       res = ast_register_application2(parkcall, park_call_exec, NULL, NULL, NULL);
08117    if (!res) {
08118       ast_manager_register_xml("ParkedCalls", 0, manager_parking_status);
08119       ast_manager_register_xml("Park", EVENT_FLAG_CALL, manager_park);
08120       ast_manager_register_xml("Bridge", EVENT_FLAG_CALL, action_bridge);
08121    }
08122 
08123    res |= ast_devstate_prov_add("Park", metermaidstate);
08124 #if defined(TEST_FRAMEWORK)
08125    res |= AST_TEST_REGISTER(features_test);
08126 #endif   /* defined(TEST_FRAMEWORK) */
08127 
08128    return res;
08129 }