Sat Mar 26 11:08:28 2011

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 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 294571 $")
00029 
00030 #include "asterisk/_private.h"
00031 
00032 #include <pthread.h>
00033 #include <signal.h>
00034 #include <sys/time.h>
00035 #include <sys/signal.h>
00036 #include <netinet/in.h>
00037 
00038 #include "asterisk/lock.h"
00039 #include "asterisk/file.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/causes.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/translate.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/say.h"
00047 #include "asterisk/features.h"
00048 #include "asterisk/musiconhold.h"
00049 #include "asterisk/config.h"
00050 #include "asterisk/cli.h"
00051 #include "asterisk/manager.h"
00052 #include "asterisk/utils.h"
00053 #include "asterisk/adsi.h"
00054 #include "asterisk/devicestate.h"
00055 #include "asterisk/monitor.h"
00056 #include "asterisk/audiohook.h"
00057 #include "asterisk/global_datastores.h"
00058 #include "asterisk/astobj2.h"
00059 
00060 /*** DOCUMENTATION
00061    <application name="Bridge" language="en_US">
00062       <synopsis>
00063          Bridge two channels.
00064       </synopsis>
00065       <syntax>
00066          <parameter name="channel" required="true">
00067             <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
00068          </parameter>
00069          <parameter name="options">
00070             <optionlist>
00071                <option name="p">
00072                   <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
00073                </option>
00074             </optionlist>
00075          </parameter>
00076       </syntax>
00077       <description>
00078          <para>Allows the ability to bridge two channels via the dialplan.</para>
00079          <para>This application sets the following channel variable upon completion:</para>
00080          <variablelist>
00081             <variable name="BRIDGERESULT">
00082                <para>The result of the bridge attempt as a text string.</para>
00083                <value name="SUCCESS" />
00084                <value name="FAILURE" />
00085                <value name="LOOP" />
00086                <value name="NONEXISTENT" />
00087                <value name="INCOMPATIBLE" />
00088             </variable>
00089          </variablelist>
00090       </description>
00091    </application>
00092    <application name="ParkedCall" language="en_US">
00093       <synopsis>
00094          Answer a parked call.
00095       </synopsis>
00096       <syntax>
00097          <parameter name="exten" required="true" />
00098       </syntax>
00099       <description>
00100          <para>Used to connect to a parked call. This application is always
00101          registered internally and does not need to be explicitly added
00102          into the dialplan, although you should include the <literal>parkedcalls</literal>
00103          context. If no extension is provided, then the first available
00104          parked call will be acquired.</para>
00105       </description>
00106       <see-also>
00107          <ref type="application">Park</ref>
00108          <ref type="application">ParkAndAnnounce</ref>
00109       </see-also>
00110    </application>
00111    <application name="Park" language="en_US">
00112       <synopsis>
00113          Park yourself.
00114       </synopsis>
00115       <syntax>
00116          <parameter name="timeout">
00117             <para>A custom parking timeout for this parked call.</para>
00118          </parameter>
00119          <parameter name="return_context">
00120             <para>The context to return the call to after it times out.</para>
00121          </parameter>
00122          <parameter name="return_exten">
00123             <para>The extension to return the call to after it times out.</para>
00124          </parameter>
00125          <parameter name="return_priority">
00126             <para>The priority to return the call to after it times out.</para>
00127          </parameter>
00128          <parameter name="options">
00129             <para>A list of options for this parked call.</para>
00130             <optionlist>
00131                <option name="r">
00132                   <para>Send ringing instead of MOH to the parked call.</para>
00133                </option>
00134                <option name="R">
00135                   <para>Randomize the selection of a parking space.</para>
00136                </option>
00137                <option name="s">
00138                   <para>Silence announcement of the parking space number.</para>
00139                </option>
00140             </optionlist>
00141          </parameter>
00142       </syntax>
00143       <description>
00144          <para>Used to park yourself (typically in combination with a supervised
00145          transfer to know the parking space). This application is always
00146          registered internally and does not need to be explicitly added
00147          into the dialplan, although you should include the <literal>parkedcalls</literal>
00148          context (or the context specified in <filename>features.conf</filename>).</para>
00149          <para>If you set the <variable>PARKINGLOT</variable> variable, the call will be parked
00150          in the specifed parking context. Note setting this variable overrides the <variable>
00151          PARKINGLOT</variable> set by the <literal>CHANNEL</literal> function.</para>
00152          <para>If you set the <variable>PARKINGEXTEN</variable> variable to an extension in your
00153          parking context, Park() will park the call on that extension, unless
00154          it already exists. In that case, execution will continue at next priority.</para>
00155       </description>
00156       <see-also>
00157          <ref type="application">ParkAndAnnounce</ref>
00158          <ref type="application">ParkedCall</ref>
00159       </see-also>
00160    </application>
00161  ***/
00162 
00163 #define DEFAULT_PARK_TIME 45000
00164 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
00165 #define DEFAULT_FEATURE_DIGIT_TIMEOUT 1000
00166 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000
00167 #define DEFAULT_PARKINGLOT "default"         /*!< Default parking lot */
00168 #define DEFAULT_ATXFER_DROP_CALL 0
00169 #define DEFAULT_ATXFER_LOOP_DELAY 10000
00170 #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
00171 
00172 #define AST_MAX_WATCHERS 256
00173 #define MAX_DIAL_FEATURE_OPTIONS 30
00174 
00175 struct feature_group_exten {
00176    AST_LIST_ENTRY(feature_group_exten) entry;
00177    AST_DECLARE_STRING_FIELDS(
00178       AST_STRING_FIELD(exten);
00179    );
00180    struct ast_call_feature *feature;
00181 };
00182 
00183 struct feature_group {
00184    AST_LIST_ENTRY(feature_group) entry;
00185    AST_DECLARE_STRING_FIELDS(
00186       AST_STRING_FIELD(gname);
00187    );
00188    AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
00189 };
00190 
00191 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
00192 
00193 static char *parkedcall = "ParkedCall";
00194 
00195 static char pickup_ext[AST_MAX_EXTENSION];                 /*!< Call pickup extension */
00196 
00197 /*! \brief Description of one parked call, added to a list while active, then removed.
00198    The list belongs to a parkinglot 
00199 */
00200 struct parkeduser {
00201    struct ast_channel *chan;                   /*!< Parking channel */
00202    struct timeval start;                       /*!< Time the parking started */
00203    int parkingnum;                             /*!< Parking lot */
00204    char parkingexten[AST_MAX_EXTENSION];       /*!< If set beforehand, parking extension used for this call */
00205    char context[AST_MAX_CONTEXT];              /*!< Where to go if our parking time expires */
00206    char exten[AST_MAX_EXTENSION];
00207    int priority;
00208    int parkingtime;                            /*!< Maximum length in parking lot before return */
00209    unsigned int notquiteyet:1;
00210    unsigned int options_specified:1;
00211    char peername[1024];
00212    unsigned char moh_trys;
00213    struct ast_parkinglot *parkinglot;
00214    AST_LIST_ENTRY(parkeduser) list;
00215 };
00216 
00217 /*! \brief Structure for parking lots which are put in a container. */
00218 struct ast_parkinglot {
00219    char name[AST_MAX_CONTEXT];
00220    char parking_con[AST_MAX_EXTENSION];      /*!< Context for which parking is made accessible */
00221    char parking_con_dial[AST_MAX_EXTENSION]; /*!< Context for dialback for parking (KLUDGE) */
00222    int parking_start;            /*!< First available extension for parking */
00223    int parking_stop;          /*!< Last available extension for parking */
00224    int parking_offset;
00225    int parkfindnext;
00226    int parkingtime;           /*!< Default parking time */
00227    char mohclass[MAX_MUSICCLASS];                  /*!< Music class used for parking */
00228    int parkaddhints;                               /*!< Add parking hints automatically */
00229    int parkedcalltransfers;                        /*!< Enable DTMF based transfers on bridge when picking up parked calls */
00230    int parkedcallreparking;                        /*!< Enable DTMF based parking on bridge when picking up parked calls */
00231    int parkedcallhangup;                           /*!< Enable DTMF based hangup on a bridge when pickup up parked calls */
00232    int parkedcallrecording;                        /*!< Enable DTMF based recording on a bridge when picking up parked calls */
00233    AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings; /*!< List of active parkings in this parkinglot */
00234 };
00235 
00236 /*! \brief The list of parking lots configured. Always at least one  - the default parking lot */
00237 static struct ao2_container *parkinglots;
00238  
00239 struct ast_parkinglot *default_parkinglot;
00240 char parking_ext[AST_MAX_EXTENSION];            /*!< Extension you type to park the call */
00241 
00242 static char courtesytone[256];                             /*!< Courtesy tone */
00243 static int parkedplay = 0;                                 /*!< Who to play the courtesy tone to */
00244 static char xfersound[256];                                /*!< Call transfer sound */
00245 static char xferfailsound[256];                            /*!< Call transfer failure sound */
00246 static char pickupsound[256];                              /*!< Pickup sound */
00247 static char pickupfailsound[256];                          /*!< Pickup failure sound */
00248 
00249 static int adsipark;
00250 
00251 static int transferdigittimeout;
00252 static int featuredigittimeout;
00253 static int comebacktoorigin = 1;
00254 
00255 static int atxfernoanswertimeout;
00256 static unsigned int atxferdropcall;
00257 static unsigned int atxferloopdelay;
00258 static unsigned int atxfercallbackretries;
00259 
00260 static char *registrar = "features";         /*!< Registrar for operations */
00261 
00262 /* module and CLI command definitions */
00263 static char *parkcall = PARK_APP_NAME;
00264 
00265 static struct ast_app *monitor_app = NULL;
00266 static int monitor_ok = 1;
00267 
00268 static struct ast_app *mixmonitor_app = NULL;
00269 static int mixmonitor_ok = 1;
00270 
00271 static struct ast_app *stopmixmonitor_app = NULL;
00272 static int stopmixmonitor_ok = 1;
00273 
00274 static pthread_t parking_thread;
00275 struct ast_dial_features {
00276    struct ast_flags features_caller;
00277    struct ast_flags features_callee;
00278    int is_caller;
00279 };
00280 
00281 static void *dial_features_duplicate(void *data)
00282 {
00283    struct ast_dial_features *df = data, *df_copy;
00284  
00285    if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
00286       return NULL;
00287    }
00288  
00289    memcpy(df_copy, df, sizeof(*df));
00290  
00291    return df_copy;
00292  }
00293  
00294  static void dial_features_destroy(void *data)
00295  {
00296    struct ast_dial_features *df = data;
00297    if (df) {
00298       ast_free(df);
00299    }
00300  }
00301  
00302  const struct ast_datastore_info dial_features_info = {
00303    .type = "dial-features",
00304    .destroy = dial_features_destroy,
00305    .duplicate = dial_features_duplicate,
00306  };
00307  
00308 /* Forward declarations */
00309 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
00310 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
00311 static void parkinglot_destroy(void *obj);
00312 int manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, const int nfds, struct pollfd **new_pfds, int *new_nfds, int *fs);
00313 struct ast_parkinglot *find_parkinglot(const char *name);
00314 
00315 
00316 const char *ast_parking_ext(void)
00317 {
00318    return parking_ext;
00319 }
00320 
00321 const char *ast_pickup_ext(void)
00322 {
00323    return pickup_ext;
00324 }
00325 
00326 struct ast_bridge_thread_obj 
00327 {
00328    struct ast_bridge_config bconfig;
00329    struct ast_channel *chan;
00330    struct ast_channel *peer;
00331    unsigned int return_to_pbx:1;
00332 };
00333 
00334 static int parkinglot_hash_cb(const void *obj, const int flags)
00335 {
00336    const struct ast_parkinglot *parkinglot = obj;
00337 
00338    return ast_str_case_hash(parkinglot->name);
00339 }
00340 
00341 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
00342 {
00343    struct ast_parkinglot *parkinglot = obj, *parkinglot2 = arg;
00344 
00345    return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
00346 }
00347 
00348 /*!
00349  * \brief store context, extension and priority 
00350  * \param chan, context, ext, pri
00351 */
00352 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
00353 {
00354    ast_copy_string(chan->context, context, sizeof(chan->context));
00355    ast_copy_string(chan->exten, ext, sizeof(chan->exten));
00356    chan->priority = pri;
00357 }
00358 
00359 /*!
00360  * \brief Check goto on transfer
00361  * \param chan
00362  *
00363  * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
00364  * When found make sure the types are compatible. Check if channel is valid
00365  * if so start the new channel else hangup the call. 
00366 */
00367 static void check_goto_on_transfer(struct ast_channel *chan) 
00368 {
00369    struct ast_channel *xferchan;
00370    const char *val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
00371    char *x, *goto_on_transfer;
00372    struct ast_frame *f;
00373 
00374    if (ast_strlen_zero(val))
00375       return;
00376 
00377    goto_on_transfer = ast_strdupa(val);
00378 
00379    if (!(xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "%s", chan->name)))
00380       return;
00381 
00382    for (x = goto_on_transfer; x && *x; x++) {
00383       if (*x == '^')
00384          *x = '|';
00385    }
00386    /* Make formats okay */
00387    xferchan->readformat = chan->readformat;
00388    xferchan->writeformat = chan->writeformat;
00389    ast_channel_masquerade(xferchan, chan);
00390    ast_parseable_goto(xferchan, goto_on_transfer);
00391    xferchan->_state = AST_STATE_UP;
00392    ast_clear_flag(xferchan, AST_FLAGS_ALL);  
00393    xferchan->_softhangup = 0;
00394    if ((f = ast_read(xferchan))) {
00395       ast_frfree(f);
00396       f = NULL;
00397       ast_pbx_start(xferchan);
00398    } else {
00399       ast_hangup(xferchan);
00400    }
00401 }
00402 
00403 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate, const char *language);
00404 
00405 /*!
00406  * \brief bridge the call 
00407  * \param data thread bridge.
00408  *
00409  * Set Last Data for respective channels, reset cdr for channels
00410  * bridge call, check if we're going back to dialplan
00411  * if not hangup both legs of the call
00412 */
00413 static void *bridge_call_thread(void *data)
00414 {
00415    struct ast_bridge_thread_obj *tobj = data;
00416    int res;
00417 
00418    tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00419    tobj->chan->data = tobj->peer->name;
00420    tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00421    tobj->peer->data = tobj->chan->name;
00422 
00423    ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
00424 
00425    if (tobj->return_to_pbx) {
00426       if (!ast_check_hangup(tobj->peer)) {
00427          ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
00428          res = ast_pbx_start(tobj->peer);
00429          if (res != AST_PBX_SUCCESS)
00430             ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
00431       } else
00432          ast_hangup(tobj->peer);
00433       if (!ast_check_hangup(tobj->chan)) {
00434          ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
00435          res = ast_pbx_start(tobj->chan);
00436          if (res != AST_PBX_SUCCESS)
00437             ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
00438       } else
00439          ast_hangup(tobj->chan);
00440    } else {
00441       ast_hangup(tobj->chan);
00442       ast_hangup(tobj->peer);
00443    }
00444 
00445    ast_free(tobj);
00446 
00447    return NULL;
00448 }
00449 
00450 /*!
00451  * \brief create thread for the parked call
00452  * \param data
00453  *
00454  * Create thread and attributes, call bridge_call_thread
00455 */
00456 static void bridge_call_thread_launch(void *data) 
00457 {
00458    pthread_t thread;
00459    pthread_attr_t attr;
00460    struct sched_param sched;
00461 
00462    pthread_attr_init(&attr);
00463    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
00464    ast_pthread_create(&thread, &attr, bridge_call_thread, data);
00465    pthread_attr_destroy(&attr);
00466    memset(&sched, 0, sizeof(sched));
00467    pthread_setschedparam(thread, SCHED_RR, &sched);
00468 }
00469 
00470 /*!
00471  * \brief Announce call parking by ADSI
00472  * \param chan .
00473  * \param parkingexten .
00474  * Create message to show for ADSI, display message.
00475  * \retval 0 on success.
00476  * \retval -1 on failure.
00477 */
00478 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
00479 {
00480    int res;
00481    int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
00482    char tmp[256];
00483    char *message[5] = {NULL, NULL, NULL, NULL, NULL};
00484 
00485    snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
00486    message[0] = tmp;
00487    res = ast_adsi_load_session(chan, NULL, 0, 1);
00488    if (res == -1)
00489       return res;
00490    return ast_adsi_print(chan, message, justify, 1);
00491 }
00492 
00493 /*! \brief Find parking lot name from channel */
00494 static const char *findparkinglotname(struct ast_channel *chan)
00495 {
00496    const char *temp, *parkinglot = NULL;
00497 
00498    /* Check if the channel has a parking lot */
00499    if (!ast_strlen_zero(chan->parkinglot))
00500       parkinglot = chan->parkinglot;
00501 
00502    /* Channel variables override everything */
00503 
00504    if ((temp  = pbx_builtin_getvar_helper(chan, "PARKINGLOT")))
00505       return temp;
00506 
00507    return parkinglot;
00508 }
00509 
00510 /*! \brief Notify metermaids that we've changed an extension */
00511 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
00512 {
00513    ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'", 
00514       exten, context, ast_devstate2str(state));
00515 
00516    ast_devstate_changed(state, "park:%s@%s", exten, context);
00517 }
00518 
00519 /*! \brief metermaids callback from devicestate.c */
00520 static enum ast_device_state metermaidstate(const char *data)
00521 {
00522    char *context;
00523    char *exten;
00524 
00525    context = ast_strdupa(data);
00526 
00527    exten = strsep(&context, "@");
00528    if (!context)
00529       return AST_DEVICE_INVALID;
00530    
00531    ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
00532 
00533    if (!ast_exists_extension(NULL, context, exten, 1, NULL))
00534       return AST_DEVICE_NOT_INUSE;
00535 
00536    return AST_DEVICE_INUSE;
00537 }
00538 
00539 /*! Options to pass to park_call_full */
00540 enum ast_park_call_options {
00541    /*! Provide ringing to the parked caller instead of music on hold */
00542    AST_PARK_OPT_RINGING =   (1 << 0),
00543    /*! Randomly choose a parking spot for the caller instead of choosing
00544     *  the first one that is available. */
00545    AST_PARK_OPT_RANDOMIZE = (1 << 1),
00546    /*! Do not announce the parking number */
00547    AST_PARK_OPT_SILENCE = (1 << 2),
00548 };
00549 
00550 struct ast_park_call_args {
00551    /*! How long to wait in the parking lot before the call gets sent back
00552     *  to the specified return extension (or a best guess at where it came
00553     *  from if not explicitly specified). */
00554    int timeout;
00555    /*! An output parameter to store the parking space where the parked caller
00556     *  was placed. */
00557    int *extout;
00558    const char *orig_chan_name;
00559    const char *return_con;
00560    const char *return_ext;
00561    int return_pri;
00562    uint32_t flags;
00563    /*! Parked user that has already obtained a parking space */
00564    struct parkeduser *pu;
00565 };
00566 
00567 static struct parkeduser *park_space_reserve(struct ast_channel *chan,
00568  struct ast_channel *peer, struct ast_park_call_args *args)
00569 {
00570    struct parkeduser *pu;
00571    int i, parking_space = -1, parking_range;
00572    const char *parkinglotname = NULL;
00573    const char *parkingexten;
00574    struct ast_parkinglot *parkinglot = NULL;
00575    
00576    if (peer)
00577       parkinglotname = findparkinglotname(peer);
00578    else /* peer was NULL, check chan (ParkAndAnnounce / res_agi) */
00579       parkinglotname = findparkinglotname(chan);
00580 
00581    if (parkinglotname) {
00582       if (option_debug)
00583          ast_log(LOG_DEBUG, "Found chanvar Parkinglot: %s\n", parkinglotname);
00584       parkinglot = find_parkinglot(parkinglotname);   
00585    }
00586    if (!parkinglot) {
00587       parkinglot = parkinglot_addref(default_parkinglot);
00588    }
00589 
00590    if (option_debug)
00591       ast_log(LOG_DEBUG, "Parkinglot: %s\n", parkinglot->name);
00592 
00593    /* Allocate memory for parking data */
00594    if (!(pu = ast_calloc(1, sizeof(*pu)))) {
00595       parkinglot_unref(parkinglot);
00596       return NULL;
00597    }
00598 
00599    /* Lock parking list */
00600    AST_LIST_LOCK(&parkinglot->parkings);
00601    /* Check for channel variable PARKINGEXTEN */
00602    ast_channel_lock(chan);
00603    parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGEXTEN"), ""));
00604    ast_channel_unlock(chan);
00605    if (!ast_strlen_zero(parkingexten)) {
00606       /*!\note The API forces us to specify a numeric parking slot, even
00607        * though the architecture would tend to support non-numeric extensions
00608        * (as are possible with SIP, for example).  Hence, we enforce that
00609        * limitation here.  If extout was not numeric, we could permit
00610        * arbitrary non-numeric extensions.
00611        */
00612         if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space < 0) {
00613          AST_LIST_UNLOCK(&parkinglot->parkings);
00614          parkinglot_unref(parkinglot);
00615             free(pu);
00616             ast_log(LOG_WARNING, "PARKINGEXTEN does not indicate a valid parking slot: '%s'.\n", parkingexten);
00617             return NULL;
00618         }
00619         snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
00620 
00621       if (ast_exists_extension(NULL, parkinglot->parking_con, pu->parkingexten, 1, NULL)) {
00622          ast_log(LOG_WARNING, "Requested parking extension already exists: %s@%s\n", parkingexten, parkinglot->parking_con);
00623          AST_LIST_UNLOCK(&parkinglot->parkings);
00624          parkinglot_unref(parkinglot);
00625          ast_free(pu);
00626          return NULL;
00627       }
00628    } else {
00629       int start;
00630       struct parkeduser *cur = NULL;
00631 
00632       /* Select parking space within range */
00633       parking_range = parkinglot->parking_stop - parkinglot->parking_start + 1;
00634 
00635       if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
00636          start = ast_random() % (parkinglot->parking_stop - parkinglot->parking_start + 1);
00637       } else {
00638          start = parkinglot->parking_start;
00639       }
00640 
00641       for (i = start; 1; i++) {
00642          if (i == parkinglot->parking_stop + 1) {
00643             i = parkinglot->parking_start - 1;
00644             break;
00645          }
00646 
00647          AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
00648             if (cur->parkingnum == i) {
00649                break;
00650             }
00651          }
00652          if (!cur) {
00653             parking_space = i;
00654             break;
00655          }
00656       }
00657 
00658       if (i == start - 1 && cur) {
00659          ast_log(LOG_WARNING, "No more parking spaces\n");
00660          ast_free(pu);
00661          AST_LIST_UNLOCK(&parkinglot->parkings);
00662          parkinglot_unref(parkinglot);
00663          return NULL;
00664       }
00665       /* Set pointer for next parking */
00666       if (parkinglot->parkfindnext) 
00667          parkinglot->parking_offset = parking_space - parkinglot->parking_start + 1;
00668       snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
00669    }
00670 
00671    pu->notquiteyet = 1;
00672    pu->parkingnum = parking_space;
00673    pu->parkinglot = parkinglot_addref(parkinglot);
00674    AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
00675    parkinglot_unref(parkinglot);
00676 
00677    return pu;
00678 }
00679 
00680 /* Park a call */
00681 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
00682 {
00683    struct ast_context *con;
00684    int parkingnum_copy;
00685    struct parkeduser *pu = args->pu;
00686    const char *event_from;
00687 
00688    if (pu == NULL)
00689       pu = park_space_reserve(chan, peer, args);
00690    if (pu == NULL)
00691       return 1; /* Continue execution if possible */
00692 
00693    snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", pu->parkingnum);
00694    
00695    chan->appl = "Parked Call";
00696    chan->data = NULL; 
00697 
00698    pu->chan = chan;
00699    
00700    /* Put the parked channel on hold if we have two different channels */
00701    if (chan != peer) {
00702       if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
00703          ast_indicate(pu->chan, AST_CONTROL_RINGING);
00704       } else {
00705          ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
00706             S_OR(pu->parkinglot->mohclass, NULL),
00707             !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
00708       }
00709    }
00710    
00711    pu->start = ast_tvnow();
00712    pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->parkingtime;
00713    parkingnum_copy = pu->parkingnum;
00714    if (args->extout)
00715       *(args->extout) = pu->parkingnum;
00716 
00717    if (peer) { 
00718       /* This is so ugly that it hurts, but implementing get_base_channel() on local channels
00719          could have ugly side effects.  We could have transferer<->local,1<->local,2<->parking
00720          and we need the callback name to be that of transferer.  Since local,1/2 have the same
00721          name we can be tricky and just grab the bridged channel from the other side of the local
00722       */
00723       if (!strcasecmp(peer->tech->type, "Local")) {
00724          struct ast_channel *tmpchan, *base_peer;
00725          char other_side[AST_CHANNEL_NAME];
00726          char *c;
00727          ast_copy_string(other_side, S_OR(args->orig_chan_name, peer->name), sizeof(other_side));
00728          if ((c = strrchr(other_side, ';'))) {
00729             *++c = '1';
00730          }
00731          if ((tmpchan = ast_get_channel_by_name_locked(other_side))) {
00732             if ((base_peer = ast_bridged_channel(tmpchan))) {
00733                ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
00734             }
00735             ast_channel_unlock(tmpchan);
00736          }
00737       } else {
00738          ast_copy_string(pu->peername, S_OR(args->orig_chan_name, peer->name), sizeof(pu->peername));
00739       }
00740    }
00741 
00742    /* Remember what had been dialed, so that if the parking
00743       expires, we try to come back to the same place */
00744 
00745    pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
00746 
00747    /* If extension has options specified, they override all other possibilities
00748    such as the returntoorigin flag and transferred context. Information on
00749    extension options is lost here, so we set a flag */
00750 
00751    ast_copy_string(pu->context, 
00752       S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)), 
00753       sizeof(pu->context));
00754    ast_copy_string(pu->exten, 
00755       S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)), 
00756       sizeof(pu->exten));
00757    pu->priority = args->return_pri ? args->return_pri : 
00758       (chan->macropriority ? chan->macropriority : chan->priority);
00759 
00760    /* If parking a channel directly, don't quiet yet get parking running on it.
00761     * All parking lot entries are put into the parking lot with notquiteyet on. */
00762    if (peer != chan) 
00763       pu->notquiteyet = 0;
00764 
00765    /* Wake up the (presumably select()ing) thread */
00766    pthread_kill(parking_thread, SIGURG);
00767    ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n", pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->context, pu->exten, pu->priority, (pu->parkingtime/1000));
00768 
00769    if (peer) {
00770       event_from = peer->name;
00771    } else {
00772       event_from = pbx_builtin_getvar_helper(chan, "BLINDTRANSFER");
00773    }
00774 
00775    manager_event(EVENT_FLAG_CALL, "ParkedCall",
00776       "Exten: %s\r\n"
00777       "Channel: %s\r\n"
00778       "Parkinglot: %s\r\n"
00779       "From: %s\r\n"
00780       "Timeout: %ld\r\n"
00781       "CallerIDNum: %s\r\n"
00782       "CallerIDName: %s\r\n"
00783       "Uniqueid: %s\r\n",
00784       pu->parkingexten, pu->chan->name, pu->parkinglot->name, event_from ? event_from : "",
00785       (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
00786       S_OR(pu->chan->cid.cid_num, "<unknown>"),
00787       S_OR(pu->chan->cid.cid_name, "<unknown>"),
00788       pu->chan->uniqueid
00789       );
00790 
00791    if (peer && adsipark && ast_adsi_available(peer)) {
00792       adsi_announce_park(peer, pu->parkingexten);  /* Only supports parking numbers */
00793       ast_adsi_unload_session(peer);
00794    }
00795 
00796    con = ast_context_find_or_create(NULL, NULL, pu->parkinglot->parking_con, registrar);
00797    if (!con)   /* Still no context? Bad */
00798       ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", pu->parkinglot->parking_con);
00799    if (con) {
00800       if (!ast_add_extension2(con, 1, pu->parkingexten, 1, NULL, NULL, parkedcall, ast_strdup(pu->parkingexten), ast_free_ptr, registrar))
00801          notify_metermaids(pu->parkingexten, pu->parkinglot->parking_con, AST_DEVICE_INUSE);
00802    }
00803 
00804    AST_LIST_UNLOCK(&pu->parkinglot->parkings);
00805 
00806    /* Only say number if it's a number and the channel hasn't been masqueraded away */
00807    if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE) && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
00808       /* If a channel is masqueraded into peer while playing back the parking slot number do not continue playing it back. This is the case if an attended transfer occurs. */
00809       ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
00810       /* Tell the peer channel the number of the parking space */
00811       ast_say_digits(peer, pu->parkingnum, "", peer->language);
00812       ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
00813    }
00814    if (peer == chan) { /* pu->notquiteyet = 1 */
00815       /* Wake up parking thread if we're really done */
00816       ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
00817          S_OR(pu->parkinglot->mohclass, NULL),
00818          !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
00819       pu->notquiteyet = 0;
00820       pthread_kill(parking_thread, SIGURG);
00821    }
00822    return 0;
00823 }
00824 
00825 /*! \brief Park a call */
00826 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
00827 {
00828    struct ast_park_call_args args = {
00829       .timeout = timeout,
00830       .extout = extout,
00831    };
00832 
00833    return park_call_full(chan, peer, &args);
00834 }
00835 
00836 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout, int play_announcement, struct ast_park_call_args *args)
00837 {
00838    struct ast_channel *chan;
00839    struct ast_frame *f;
00840    int park_status;
00841    struct ast_park_call_args park_args = {0,};
00842 
00843    if (!args) {
00844       args = &park_args;
00845       args->timeout = timeout;
00846       args->extout = extout;
00847    }
00848 
00849    if ((args->pu = park_space_reserve(rchan, peer, args)) == NULL) {
00850       if (peer)
00851          ast_stream_and_wait(peer, "beeperr", "");
00852       return AST_FEATURE_RETURN_PARKFAILED;
00853    }
00854 
00855    /* Make a new, fake channel that we'll use to masquerade in the real one */
00856    if (!(chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten, rchan->context, rchan->amaflags, "Parked/%s",rchan->name))) {
00857       ast_log(LOG_WARNING, "Unable to create parked channel\n");
00858       return -1;
00859    }
00860 
00861    /* Make formats okay */
00862    chan->readformat = rchan->readformat;
00863    chan->writeformat = rchan->writeformat;
00864    ast_channel_masquerade(chan, rchan);
00865 
00866    /* Setup the extensions and such */
00867    set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
00868 
00869    /* Setup the macro extension and such */
00870    ast_copy_string(chan->macrocontext,rchan->macrocontext,sizeof(chan->macrocontext));
00871    ast_copy_string(chan->macroexten,rchan->macroexten,sizeof(chan->macroexten));
00872    chan->macropriority = rchan->macropriority;
00873 
00874    /* Make the masq execute */
00875    if ((f = ast_read(chan)))
00876       ast_frfree(f);
00877 
00878    if (peer == rchan) {
00879       peer = chan;
00880    }
00881 
00882    if (peer && (!play_announcement && args == &park_args)) {
00883       args->orig_chan_name = ast_strdupa(peer->name);
00884    }
00885 
00886    park_status = park_call_full(chan, peer, args);
00887    if (park_status == 1) {
00888    /* would be nice to play "invalid parking extension" */
00889       ast_hangup(chan);
00890       return -1;
00891    }
00892 
00893    return 0;
00894 }
00895 
00896 /* Park call via masqueraded channel */
00897 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
00898 {
00899    return masq_park_call(rchan, peer, timeout, extout, 0, NULL);
00900 }
00901 
00902 static int masq_park_call_announce_args(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
00903 {
00904    return masq_park_call(rchan, peer, 0, NULL, 1, args);
00905 }
00906 
00907 static int masq_park_call_announce(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
00908 {
00909    return masq_park_call(rchan, peer, timeout, extout, 1, NULL);
00910 }
00911 
00912 #define FEATURE_SENSE_CHAN (1 << 0)
00913 #define FEATURE_SENSE_PEER (1 << 1)
00914 
00915 /*! 
00916  * \brief set caller and callee according to the direction 
00917  * \param caller, callee, peer, chan, sense
00918  *
00919  * Detect who triggered feature and set callee/caller variables accordingly
00920 */
00921 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
00922    struct ast_channel *peer, struct ast_channel *chan, int sense)
00923 {
00924    if (sense == FEATURE_SENSE_PEER) {
00925       *caller = peer;
00926       *callee = chan;
00927    } else {
00928       *callee = peer;
00929       *caller = chan;
00930    }
00931 }
00932 
00933 /*! 
00934  * \brief support routing for one touch call parking
00935  * \param chan channel parking call
00936  * \param peer channel to be parked
00937  * \param config unsed
00938  * \param code unused
00939  * \param sense feature options
00940  *
00941  * \param data
00942  * Setup channel, set return exten,priority to 's,1'
00943  * answer chan, sleep chan, park call
00944 */
00945 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
00946 {
00947    struct ast_channel *parker;
00948    struct ast_channel *parkee;
00949    int res = 0;
00950 
00951    set_peers(&parker, &parkee, peer, chan, sense);
00952    /* we used to set chan's exten and priority to "s" and 1
00953       here, but this generates (in some cases) an invalid
00954       extension, and if "s" exists, could errantly
00955       cause execution of extensions you don't expect. It
00956       makes more sense to let nature take its course
00957       when chan finishes, and let the pbx do its thing
00958       and hang up when the park is over.
00959    */
00960    if (chan->_state != AST_STATE_UP)
00961       res = ast_answer(chan);
00962    if (!res)
00963       res = ast_safe_sleep(chan, 1000);
00964 
00965    if (!res) { /* one direction used to call park_call.... */
00966       res = masq_park_call_announce(parkee, parker, 0, NULL);
00967       /* PBX should hangup zombie channel if a masquerade actually occurred (res=0) */
00968    }
00969 
00970    return res;
00971 }
00972 
00973 /*! \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
00974    other channel during the message, so please don't use this for very long messages
00975  */
00976 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
00977 {
00978    /* First play for caller, put other channel on auto service */
00979    if (ast_autoservice_start(callee_chan))
00980       return -1;
00981    ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
00982    if (ast_stream_and_wait(caller_chan, audiofile, "")) {
00983       ast_log(LOG_WARNING, "Failed to play automon message!\n");
00984       ast_autoservice_stop(callee_chan);
00985       return -1;
00986    }
00987    if (ast_autoservice_stop(callee_chan))
00988       return -1;
00989    /* Then play for callee, put other channel on auto service */
00990    if (ast_autoservice_start(caller_chan))
00991       return -1;
00992    ast_autoservice_ignore(caller_chan, AST_FRAME_DTMF_END);
00993    if (ast_stream_and_wait(callee_chan, audiofile, "")) {
00994       ast_log(LOG_WARNING, "Failed to play automon message !\n");
00995       ast_autoservice_stop(caller_chan);
00996       return -1;
00997    }
00998    if (ast_autoservice_stop(caller_chan))
00999       return -1;
01000    return(0);
01001 }
01002 
01003 /*!
01004  * \brief Monitor a channel by DTMF
01005  * \param chan channel requesting monitor
01006  * \param peer channel to be monitored
01007  * \param config
01008  * \param code
01009  * \param sense feature options
01010  *
01011  * \param data
01012  * Check monitor app enabled, setup channels, both caller/callee chans not null
01013  * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
01014  * \retval AST_FEATURE_RETURN_SUCCESS on success.
01015  * \retval -1 on error.
01016 */
01017 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01018 {
01019    char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
01020    int x = 0;
01021    size_t len;
01022    struct ast_channel *caller_chan, *callee_chan;
01023    const char *automon_message_start = NULL;
01024    const char *automon_message_stop = NULL;
01025 
01026    if (!monitor_ok) {
01027       ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
01028       return -1;
01029    }
01030 
01031    if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
01032       monitor_ok = 0;
01033       ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
01034       return -1;
01035    }
01036 
01037    set_peers(&caller_chan, &callee_chan, peer, chan, sense);
01038    if (caller_chan) {   /* Find extra messages */
01039       automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
01040       automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
01041    }
01042 
01043    if (!ast_strlen_zero(courtesytone)) {  /* Play courtesy tone if configured */
01044       if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
01045          return -1;
01046       }
01047    }
01048    
01049    if (callee_chan->monitor) {
01050       ast_verb(4, "User hit '%s' to stop recording call.\n", code);
01051       if (!ast_strlen_zero(automon_message_stop)) {
01052          play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
01053       }
01054       callee_chan->monitor->stop(callee_chan, 1);
01055       return AST_FEATURE_RETURN_SUCCESS;
01056    }
01057 
01058    if (caller_chan && callee_chan) {
01059       const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
01060       const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
01061       const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
01062 
01063       if (!touch_format)
01064          touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
01065 
01066       if (!touch_monitor)
01067          touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
01068    
01069       if (!touch_monitor_prefix)
01070          touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
01071    
01072       if (touch_monitor) {
01073          len = strlen(touch_monitor) + 50;
01074          args = alloca(len);
01075          touch_filename = alloca(len);
01076          snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
01077          snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
01078       } else {
01079          caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
01080          callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
01081          len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
01082          args = alloca(len);
01083          touch_filename = alloca(len);
01084          snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
01085          snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
01086       }
01087 
01088       for(x = 0; x < strlen(args); x++) {
01089          if (args[x] == '/')
01090             args[x] = '-';
01091       }
01092       
01093       ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
01094 
01095       pbx_exec(callee_chan, monitor_app, args);
01096       pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
01097       pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
01098 
01099       if (!ast_strlen_zero(automon_message_start)) {  /* Play start message for both channels */
01100          play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
01101       }
01102    
01103       return AST_FEATURE_RETURN_SUCCESS;
01104    }
01105    
01106    ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");  
01107    return -1;
01108 }
01109 
01110 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01111 {
01112    char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
01113    int x = 0;
01114    size_t len;
01115    struct ast_channel *caller_chan, *callee_chan;
01116    const char *mixmonitor_spy_type = "MixMonitor";
01117    int count = 0;
01118 
01119    if (!mixmonitor_ok) {
01120       ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
01121       return -1;
01122    }
01123 
01124    if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
01125       mixmonitor_ok = 0;
01126       ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
01127       return -1;
01128    }
01129 
01130    set_peers(&caller_chan, &callee_chan, peer, chan, sense);
01131 
01132    if (!ast_strlen_zero(courtesytone)) {
01133       if (ast_autoservice_start(callee_chan))
01134          return -1;
01135       ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
01136       if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
01137          ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
01138          ast_autoservice_stop(callee_chan);
01139          return -1;
01140       }
01141       if (ast_autoservice_stop(callee_chan))
01142          return -1;
01143    }
01144 
01145    ast_channel_lock(callee_chan);
01146    count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
01147    ast_channel_unlock(callee_chan);
01148 
01149    /* This means a mixmonitor is attached to the channel, running or not is unknown. */
01150    if (count > 0) {
01151       
01152       ast_verb(3, "User hit '%s' to stop recording call.\n", code);
01153 
01154       /* Make sure they are running */
01155       ast_channel_lock(callee_chan);
01156       count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
01157       ast_channel_unlock(callee_chan);
01158       if (count > 0) {
01159          if (!stopmixmonitor_ok) {
01160             ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
01161             return -1;
01162          }
01163          if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
01164             stopmixmonitor_ok = 0;
01165             ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
01166             return -1;
01167          } else {
01168             pbx_exec(callee_chan, stopmixmonitor_app, "");
01169             return AST_FEATURE_RETURN_SUCCESS;
01170          }
01171       }
01172       
01173       ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n"); 
01174    }        
01175 
01176    if (caller_chan && callee_chan) {
01177       const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
01178       const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
01179 
01180       if (!touch_format)
01181          touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
01182 
01183       if (!touch_monitor)
01184          touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
01185 
01186       if (touch_monitor) {
01187          len = strlen(touch_monitor) + 50;
01188          args = alloca(len);
01189          touch_filename = alloca(len);
01190          snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
01191          snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
01192       } else {
01193          caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
01194          callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
01195          len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
01196          args = alloca(len);
01197          touch_filename = alloca(len);
01198          snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
01199          snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
01200       }
01201 
01202       for( x = 0; x < strlen(args); x++) {
01203          if (args[x] == '/')
01204             args[x] = '-';
01205       }
01206 
01207       ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
01208 
01209       pbx_exec(callee_chan, mixmonitor_app, args);
01210       pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
01211       pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
01212       return AST_FEATURE_RETURN_SUCCESS;
01213    
01214    }
01215 
01216    ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
01217    return -1;
01218 
01219 }
01220 
01221 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01222 {
01223    ast_verb(4, "User hit '%s' to disconnect call.\n", code);
01224    return AST_FEATURE_RETURN_HANGUP;
01225 }
01226 
01227 static int finishup(struct ast_channel *chan)
01228 {
01229    ast_indicate(chan, AST_CONTROL_UNHOLD);
01230 
01231    return ast_autoservice_stop(chan);
01232 }
01233 
01234 /*!
01235  * \brief Find the context for the transfer
01236  * \param transferer
01237  * \param transferee
01238  * 
01239  * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
01240  * \return a context string
01241 */
01242 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
01243 {
01244    const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
01245    if (ast_strlen_zero(s)) {
01246       s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
01247    }
01248    if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
01249       s = transferer->macrocontext;
01250    }
01251    if (ast_strlen_zero(s)) {
01252       s = transferer->context;
01253    }
01254    return s;  
01255 }
01256 
01257 /*!
01258  * \brief Blind transfer user to another extension
01259  * \param chan channel to be transfered
01260  * \param peer channel initiated blind transfer
01261  * \param config
01262  * \param code
01263  * \param data
01264  * \param sense  feature options
01265  * 
01266  * Place chan on hold, check if transferred to parkinglot extension,
01267  * otherwise check extension exists and transfer caller.
01268  * \retval AST_FEATURE_RETURN_SUCCESS.
01269  * \retval -1 on failure.
01270 */
01271 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01272 {
01273    struct ast_channel *transferer;
01274    struct ast_channel *transferee;
01275    const char *transferer_real_context;
01276    char xferto[256];
01277    int res, parkstatus = 0;
01278 
01279    set_peers(&transferer, &transferee, peer, chan, sense);
01280    transferer_real_context = real_ctx(transferer, transferee);
01281    /* Start autoservice on chan while we talk to the originator */
01282    ast_autoservice_start(transferee);
01283    ast_autoservice_ignore(transferee, AST_FRAME_DTMF_END);
01284    ast_indicate(transferee, AST_CONTROL_HOLD);
01285 
01286    memset(xferto, 0, sizeof(xferto));
01287 
01288    /* Transfer */
01289    res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
01290    if (res < 0) {
01291       finishup(transferee);
01292       return -1; /* error ? */
01293    }
01294    if (res > 0)   /* If they've typed a digit already, handle it */
01295       xferto[0] = (char) res;
01296 
01297    ast_stopstream(transferer);
01298    res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
01299    if (res < 0) {  /* hangup, would be 0 for invalid and 1 for valid */
01300       finishup(transferee);
01301       return res;
01302    }
01303    if (!strcmp(xferto, ast_parking_ext())) {
01304       res = finishup(transferee);
01305       if (res)
01306          res = -1;
01307       else if (!(parkstatus = masq_park_call_announce(transferee, transferer, 0, NULL))) {   /* success */
01308          /* We return non-zero, but tell the PBX not to hang the channel when
01309             the thread dies -- We have to be careful now though.  We are responsible for 
01310             hanging up the channel, else it will never be hung up! */
01311 
01312          return 0;
01313       } else {
01314          ast_log(LOG_WARNING, "Unable to park call %s, parkstatus = %d\n", transferee->name, parkstatus);
01315       }
01316       /*! \todo XXX Maybe we should have another message here instead of invalid extension XXX */
01317    } else if (ast_exists_extension(transferee, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
01318       pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
01319       pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
01320       res=finishup(transferee);
01321       if (!transferer->cdr) { /* this code should never get called (in a perfect world) */
01322          transferer->cdr=ast_cdr_alloc();
01323          if (transferer->cdr) {
01324             ast_cdr_init(transferer->cdr, transferer); /* initialize our channel's cdr */
01325             ast_cdr_start(transferer->cdr);
01326          }
01327       }
01328       if (transferer->cdr) {
01329          struct ast_cdr *swap = transferer->cdr;
01330          ast_log(LOG_DEBUG,"transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
01331                transferer->name, transferee->name, transferer->cdr->lastapp, transferer->cdr->lastdata, 
01332                transferer->cdr->channel, transferer->cdr->dstchannel);
01333          ast_log(LOG_DEBUG,"TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
01334                transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel, transferee->cdr->dstchannel);
01335          ast_log(LOG_DEBUG,"transferer_real_context=%s; xferto=%s\n", transferer_real_context, xferto);
01336          /* swap cdrs-- it will save us some time & work */
01337          transferer->cdr = transferee->cdr;
01338          transferee->cdr = swap;
01339       }
01340       if (!transferee->pbx) {
01341          /* Doh!  Use our handy async_goto functions */
01342          ast_verb(3, "Transferring %s to '%s' (context %s) priority 1\n"
01343                         ,transferee->name, xferto, transferer_real_context);
01344          if (ast_async_goto(transferee, transferer_real_context, xferto, 1))
01345             ast_log(LOG_WARNING, "Async goto failed :-(\n");
01346       } else {
01347          /* Set the channel's new extension, since it exists, using transferer context */
01348          ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
01349          ast_log(LOG_DEBUG,"ABOUT TO AST_ASYNC_GOTO, have a pbx... set HANGUP_DONT on chan=%s\n", transferee->name);
01350          set_c_e_p(transferee, transferer_real_context, xferto, 0);
01351       }
01352       check_goto_on_transfer(transferer);
01353       return res;
01354    } else {
01355       ast_verb(3, "Unable to find extension '%s' in context '%s'\n", xferto, transferer_real_context);
01356    }
01357    if (parkstatus != AST_FEATURE_RETURN_PARKFAILED && ast_stream_and_wait(transferer, xferfailsound, AST_DIGIT_ANY) < 0) {
01358       finishup(transferee);
01359       return -1;
01360    }
01361    ast_stopstream(transferer);
01362    res = finishup(transferee);
01363    if (res) {
01364       ast_verb(2, "Hungup during autoservice stop on '%s'\n", transferee->name);
01365       return res;
01366    }
01367    return AST_FEATURE_RETURN_SUCCESS;
01368 }
01369 
01370 /*!
01371  * \brief make channels compatible
01372  * \param c
01373  * \param newchan
01374  * \retval 0 on success.
01375  * \retval -1 on failure.
01376 */
01377 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
01378 {
01379    if (ast_channel_make_compatible(c, newchan) < 0) {
01380       ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
01381          c->name, newchan->name);
01382       ast_hangup(newchan);
01383       return -1;
01384    }
01385    return 0;
01386 }
01387 
01388 /*!
01389  * \brief Attended transfer
01390  * \param chan transfered user
01391  * \param peer person transfering call
01392  * \param config
01393  * \param code
01394  * \param sense feature options
01395  * 
01396  * \param data
01397  * Get extension to transfer to, if you cannot generate channel (or find extension) 
01398  * return to host channel. After called channel answered wait for hangup of transferer,
01399  * bridge call between transfer peer (taking them off hold) to attended transfer channel.
01400  *
01401  * \return -1 on failure
01402 */
01403 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01404 {
01405    struct ast_channel *transferer;
01406    struct ast_channel *transferee;
01407    const char *transferer_real_context;
01408    char xferto[256] = "";
01409    int res;
01410    int outstate=0;
01411    struct ast_channel *newchan;
01412    struct ast_channel *xferchan;
01413    struct ast_bridge_thread_obj *tobj;
01414    struct ast_bridge_config bconfig;
01415    struct ast_frame *f;
01416    int l;
01417    struct ast_datastore *features_datastore;
01418    struct ast_dial_features *dialfeatures = NULL;
01419 
01420    ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
01421    set_peers(&transferer, &transferee, peer, chan, sense);
01422    transferer_real_context = real_ctx(transferer, transferee);
01423    /* Start autoservice on chan while we talk to the originator */
01424    ast_autoservice_start(transferee);
01425    ast_autoservice_ignore(transferee, AST_FRAME_DTMF_END);
01426    ast_indicate(transferee, AST_CONTROL_HOLD);
01427    
01428    /* Transfer */
01429    res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
01430    if (res < 0) {
01431       finishup(transferee);
01432       return res;
01433    }
01434    if (res > 0) /* If they've typed a digit already, handle it */
01435       xferto[0] = (char) res;
01436 
01437    /* this is specific of atxfer */
01438    res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
01439    if (res < 0) {  /* hangup, would be 0 for invalid and 1 for valid */
01440       finishup(transferee);
01441       return res;
01442    }
01443    if (res == 0) {
01444       ast_log(LOG_WARNING, "Did not read data.\n");
01445       finishup(transferee);
01446       if (ast_stream_and_wait(transferer, "beeperr", ""))
01447          return -1;
01448       return AST_FEATURE_RETURN_SUCCESS;
01449    }
01450 
01451    /* valid extension, res == 1 */
01452    if (!ast_exists_extension(transferer, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
01453       ast_log(LOG_WARNING, "Extension %s does not exist in context %s\n",xferto,transferer_real_context);
01454       finishup(transferee);
01455       if (ast_stream_and_wait(transferer, "beeperr", ""))
01456          return -1;
01457       return AST_FEATURE_RETURN_SUCCESS;
01458    }
01459 
01460    /* If we are attended transfering to parking, just use builtin_parkcall instead of trying to track all of
01461     * the different variables for handling this properly with a builtin_atxfer */
01462    if (!strcmp(xferto, ast_parking_ext())) {
01463       finishup(transferee);
01464       return builtin_parkcall(chan, peer, config, code, sense, data);
01465    }
01466 
01467    l = strlen(xferto);
01468    snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);   /* append context */
01469 
01470    /* If we are performing an attended transfer and we have two channels involved then
01471       copy sound file information to play upon attended transfer completion */
01472    if (transferee) {
01473       const char *chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
01474       const char *chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
01475 
01476       if (!ast_strlen_zero(chan1_attended_sound)) {
01477          pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
01478       }
01479       if (!ast_strlen_zero(chan2_attended_sound)) {
01480          pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
01481       }
01482    }
01483 
01484    newchan = feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
01485       xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1, transferer->language);
01486 
01487    if (!ast_check_hangup(transferer)) {
01488       int hangup_dont = 0;
01489 
01490       /* Transferer is up - old behaviour */
01491       ast_indicate(transferer, -1);
01492       if (!newchan) {
01493          finishup(transferee);
01494          /* any reason besides user requested cancel and busy triggers the failed sound */
01495          if (outstate != AST_CONTROL_UNHOLD && outstate != AST_CONTROL_BUSY &&
01496             ast_stream_and_wait(transferer, xferfailsound, ""))
01497             return -1;
01498          if (ast_stream_and_wait(transferer, xfersound, ""))
01499             ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01500          return AST_FEATURE_RETURN_SUCCESS;
01501       }
01502 
01503       if (check_compat(transferer, newchan)) {
01504          /* we do mean transferee here, NOT transferer */
01505          finishup(transferee);
01506          return -1;
01507       }
01508       memset(&bconfig,0,sizeof(struct ast_bridge_config));
01509       ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
01510       ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
01511 
01512       /* ast_bridge_call clears AST_FLAG_BRIDGE_HANGUP_DONT, but we don't
01513          want that to happen here because we're also in another bridge already
01514        */
01515       if (ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT)) {
01516          hangup_dont = 1;
01517       }
01518       res = ast_bridge_call(transferer, newchan, &bconfig);
01519       if (hangup_dont) {
01520          ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
01521       }
01522 
01523       if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
01524          ast_hangup(newchan);
01525          if (ast_stream_and_wait(transferer, xfersound, ""))
01526             ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01527          finishup(transferee);
01528          transferer->_softhangup = 0;
01529          return AST_FEATURE_RETURN_SUCCESS;
01530       }
01531       if (check_compat(transferee, newchan)) {
01532          finishup(transferee);
01533          return -1;
01534       }
01535       ast_indicate(transferee, AST_CONTROL_UNHOLD);
01536 
01537       if ((ast_autoservice_stop(transferee) < 0)
01538        || (ast_waitfordigit(transferee, 100) < 0)
01539        || (ast_waitfordigit(newchan, 100) < 0)
01540        || ast_check_hangup(transferee)
01541        || ast_check_hangup(newchan)) {
01542          ast_hangup(newchan);
01543          return -1;
01544       }
01545       xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Transfered/%s", transferee->name);
01546       if (!xferchan) {
01547          ast_hangup(newchan);
01548          return -1;
01549       }
01550       /* Make formats okay */
01551       xferchan->visible_indication = transferer->visible_indication;
01552       xferchan->readformat = transferee->readformat;
01553       xferchan->writeformat = transferee->writeformat;
01554       ast_channel_masquerade(xferchan, transferee);
01555       ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
01556       xferchan->_state = AST_STATE_UP;
01557       ast_clear_flag(xferchan, AST_FLAGS_ALL);
01558       xferchan->_softhangup = 0;
01559       if ((f = ast_read(xferchan)))
01560          ast_frfree(f);
01561       newchan->_state = AST_STATE_UP;
01562       ast_clear_flag(newchan, AST_FLAGS_ALL);
01563       newchan->_softhangup = 0;
01564       if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
01565          ast_hangup(xferchan);
01566          ast_hangup(newchan);
01567          return -1;
01568       }
01569 
01570       ast_channel_lock(newchan);
01571       if ((features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL))) {
01572             dialfeatures = features_datastore->data;
01573       }
01574       ast_channel_unlock(newchan);
01575 
01576       if (dialfeatures) {
01577          /* newchan should always be the callee and shows up as callee in dialfeatures, but for some reason
01578             I don't currently understand, the abilities of newchan seem to be stored on the caller side */
01579          ast_copy_flags(&(config->features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
01580          dialfeatures = NULL;
01581       }
01582 
01583       ast_channel_lock(xferchan);
01584       if ((features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL))) {
01585          dialfeatures = features_datastore->data;
01586       }
01587       ast_channel_unlock(xferchan);
01588     
01589       if (dialfeatures) {
01590          ast_copy_flags(&(config->features_caller), &(dialfeatures->features_caller), AST_FLAGS_ALL);
01591       }
01592     
01593       tobj->chan = newchan;
01594       tobj->peer = xferchan;
01595       tobj->bconfig = *config;
01596 
01597       if (tobj->bconfig.end_bridge_callback_data_fixup) {
01598          tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
01599       }
01600 
01601       if (ast_stream_and_wait(newchan, xfersound, ""))
01602          ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01603       bridge_call_thread_launch(tobj);
01604       return -1;      /* XXX meaning the channel is bridged ? */
01605    } else if (!ast_check_hangup(transferee)) {
01606       /* act as blind transfer */
01607       if (ast_autoservice_stop(transferee) < 0) {
01608          ast_hangup(newchan);
01609          return -1;
01610       }
01611 
01612       if (!newchan) {
01613          unsigned int tries = 0;
01614          char *transferer_tech, *transferer_name = ast_strdupa(transferer->name);
01615 
01616          transferer_tech = strsep(&transferer_name, "/");
01617          transferer_name = strsep(&transferer_name, "-");
01618 
01619          if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
01620             ast_log(LOG_WARNING, "Transferer has invalid channel name: '%s'\n", transferer->name);
01621             if (ast_stream_and_wait(transferee, "beeperr", ""))
01622                return -1;
01623             return AST_FEATURE_RETURN_SUCCESS;
01624          }
01625 
01626          ast_log(LOG_NOTICE, "We're trying to call %s/%s\n", transferer_tech, transferer_name);
01627          newchan = feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
01628             transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0, transferer->language);
01629          while (!newchan && !atxferdropcall && tries < atxfercallbackretries) {
01630             /* Trying to transfer again */
01631             ast_autoservice_start(transferee);
01632             ast_autoservice_ignore(transferee, AST_FRAME_DTMF_END);
01633             ast_indicate(transferee, AST_CONTROL_HOLD);
01634 
01635             newchan = feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
01636                xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1, transferer->language);
01637             if (ast_autoservice_stop(transferee) < 0) {
01638                if (newchan)
01639                   ast_hangup(newchan);
01640                return -1;
01641             }
01642             if (!newchan) {
01643                /* Transfer failed, sleeping */
01644                ast_debug(1, "Sleeping for %d ms before callback.\n", atxferloopdelay);
01645                ast_safe_sleep(transferee, atxferloopdelay);
01646                ast_debug(1, "Trying to callback...\n");
01647                newchan = feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
01648                   transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0, transferer->language);
01649             }
01650             tries++;
01651          }
01652       }
01653       if (!newchan)
01654          return -1;
01655 
01656       /* newchan is up, we should prepare transferee and bridge them */
01657       if (check_compat(transferee, newchan)) {
01658          finishup(transferee);
01659          return -1;
01660       }
01661       ast_indicate(transferee, AST_CONTROL_UNHOLD);
01662 
01663       if ((ast_waitfordigit(transferee, 100) < 0)
01664          || (ast_waitfordigit(newchan, 100) < 0)
01665          || ast_check_hangup(transferee)
01666          || ast_check_hangup(newchan)) {
01667          ast_hangup(newchan);
01668          return -1;
01669       }
01670 
01671       xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Transfered/%s", transferee->name);
01672       if (!xferchan) {
01673          ast_hangup(newchan);
01674          return -1;
01675       }
01676       /* Make formats okay */
01677       xferchan->visible_indication = transferer->visible_indication;
01678       xferchan->readformat = transferee->readformat;
01679       xferchan->writeformat = transferee->writeformat;
01680       ast_channel_masquerade(xferchan, transferee);
01681       ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
01682       xferchan->_state = AST_STATE_UP;
01683       ast_clear_flag(xferchan, AST_FLAGS_ALL);
01684       xferchan->_softhangup = 0;
01685       if ((f = ast_read(xferchan)))
01686          ast_frfree(f);
01687       newchan->_state = AST_STATE_UP;
01688       ast_clear_flag(newchan, AST_FLAGS_ALL);
01689       newchan->_softhangup = 0;
01690       if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
01691          ast_hangup(xferchan);
01692          ast_hangup(newchan);
01693          return -1;
01694       }
01695       tobj->chan = newchan;
01696       tobj->peer = xferchan;
01697       tobj->bconfig = *config;
01698 
01699       if (tobj->bconfig.end_bridge_callback_data_fixup) {
01700          tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
01701       }
01702 
01703       if (ast_stream_and_wait(newchan, xfersound, ""))
01704          ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01705       bridge_call_thread_launch(tobj);
01706       return -1;      /* XXX meaning the channel is bridged ? */
01707    } else {
01708       /* Transferee hung up */
01709       finishup(transferee);
01710       /* At this point both the transferer transferee have hungup,
01711        * so if newchan is up, hang it up as it has no one to talk to */
01712       if (newchan) {
01713          ast_hangup(newchan);
01714       }
01715       return -1;
01716    }
01717 }
01718 
01719 /* add atxfer and automon as undefined so you can only use em if you configure them */
01720 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
01721 
01722 AST_RWLOCK_DEFINE_STATIC(features_lock);
01723 
01724 static struct ast_call_feature builtin_features[] = 
01725 {
01726    { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01727    { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01728    { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01729    { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01730    { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01731    { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01732 };
01733 
01734 
01735 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
01736 
01737 /*! \brief register new feature into feature_list*/
01738 void ast_register_feature(struct ast_call_feature *feature)
01739 {
01740    if (!feature) {
01741       ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
01742       return;
01743    }
01744   
01745    AST_RWLIST_WRLOCK(&feature_list);
01746    AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
01747    AST_RWLIST_UNLOCK(&feature_list);
01748 
01749    ast_verb(2, "Registered Feature '%s'\n",feature->sname);
01750 }
01751 
01752 /*! 
01753  * \brief Add new feature group
01754  * \param fgname feature group name.
01755  *
01756  * Add new feature group to the feature group list insert at head of list.
01757  * \note This function MUST be called while feature_groups is locked.
01758 */
01759 static struct feature_group *register_group(const char *fgname)
01760 {
01761    struct feature_group *fg;
01762 
01763    if (!fgname) {
01764       ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
01765       return NULL;
01766    }
01767 
01768    if (!(fg = ast_calloc(1, sizeof(*fg)))) {
01769       return NULL;
01770    }
01771 
01772    if (ast_string_field_init(fg, 128)) {
01773       ast_free(fg);
01774       return NULL;
01775    }
01776 
01777    ast_string_field_set(fg, gname, fgname);
01778 
01779    AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
01780 
01781    ast_verb(2, "Registered group '%s'\n", fg->gname);
01782 
01783    return fg;
01784 }
01785 
01786 /*! 
01787  * \brief Add feature to group
01788  * \param fg feature group
01789  * \param exten
01790  * \param feature feature to add.
01791  *
01792  * Check fg and feature specified, add feature to list
01793  * \note This function MUST be called while feature_groups is locked. 
01794 */
01795 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
01796 {
01797    struct feature_group_exten *fge;
01798 
01799    if (!fg) {
01800       ast_log(LOG_NOTICE, "You didn't pass a group!\n");
01801       return;
01802    }
01803 
01804    if (!feature) {
01805       ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
01806       return;
01807    }
01808 
01809    if (!(fge = ast_calloc(1, sizeof(*fge)))) {
01810       return;
01811    }
01812 
01813    if (ast_string_field_init(fge, 128)) {
01814       ast_free(fge);
01815       return;
01816    }
01817 
01818    ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
01819 
01820    fge->feature = feature;
01821 
01822    AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
01823 
01824    ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
01825                feature->sname, fg->gname, fge->exten);
01826 }
01827 
01828 void ast_unregister_feature(struct ast_call_feature *feature)
01829 {
01830    if (!feature) {
01831       return;
01832    }
01833 
01834    AST_RWLIST_WRLOCK(&feature_list);
01835    AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
01836    AST_RWLIST_UNLOCK(&feature_list);
01837 
01838    ast_free(feature);
01839 }
01840 
01841 /*! \brief Remove all features in the list */
01842 static void ast_unregister_features(void)
01843 {
01844    struct ast_call_feature *feature;
01845 
01846    AST_RWLIST_WRLOCK(&feature_list);
01847    while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
01848       ast_free(feature);
01849    }
01850    AST_RWLIST_UNLOCK(&feature_list);
01851 }
01852 
01853 /*! \brief find a call feature by name */
01854 static struct ast_call_feature *find_dynamic_feature(const char *name)
01855 {
01856    struct ast_call_feature *tmp;
01857 
01858    AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
01859       if (!strcasecmp(tmp->sname, name)) {
01860          break;
01861       }
01862    }
01863 
01864    return tmp;
01865 }
01866 
01867 /*! \brief Remove all feature groups in the list */
01868 static void ast_unregister_groups(void)
01869 {
01870    struct feature_group *fg;
01871    struct feature_group_exten *fge;
01872 
01873    AST_RWLIST_WRLOCK(&feature_groups);
01874    while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
01875       while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
01876          ast_string_field_free_memory(fge);
01877          ast_free(fge);
01878       }
01879 
01880       ast_string_field_free_memory(fg);
01881       ast_free(fg);
01882    }
01883    AST_RWLIST_UNLOCK(&feature_groups);
01884 }
01885 
01886 /*! 
01887  * \brief Find a group by name 
01888  * \param name feature name
01889  * \retval feature group on success.
01890  * \retval NULL on failure.
01891 */
01892 static struct feature_group *find_group(const char *name)
01893 {
01894    struct feature_group *fg = NULL;
01895 
01896    AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
01897       if (!strcasecmp(fg->gname, name))
01898          break;
01899    }
01900 
01901    return fg;
01902 }
01903 
01904 void ast_rdlock_call_features(void)
01905 {
01906    ast_rwlock_rdlock(&features_lock);
01907 }
01908 
01909 void ast_unlock_call_features(void)
01910 {
01911    ast_rwlock_unlock(&features_lock);
01912 }
01913 
01914 struct ast_call_feature *ast_find_call_feature(const char *name)
01915 {
01916    int x;
01917    for (x = 0; x < FEATURES_COUNT; x++) {
01918       if (!strcasecmp(name, builtin_features[x].sname))
01919          return &builtin_features[x];
01920    }
01921    return NULL;
01922 }
01923 
01924 /*!
01925  * \brief exec an app by feature 
01926  * \param chan,peer,config,code,sense,data
01927  *
01928  * Find a feature, determine which channel activated
01929  * \retval AST_FEATURE_RETURN_NO_HANGUP_PEER
01930  * \retval -1 error.
01931  * \retval -2 when an application cannot be found.
01932 */
01933 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01934 {
01935    struct ast_app *app;
01936    struct ast_call_feature *feature = data;
01937    struct ast_channel *work, *idle;
01938    int res;
01939 
01940    if (!feature) { /* shouldn't ever happen! */
01941       ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
01942       return -1; 
01943    }
01944 
01945    if (sense == FEATURE_SENSE_CHAN) {
01946       if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
01947          return AST_FEATURE_RETURN_KEEPTRYING;
01948       if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
01949          work = chan;
01950          idle = peer;
01951       } else {
01952          work = peer;
01953          idle = chan;
01954       }
01955    } else {
01956       if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
01957          return AST_FEATURE_RETURN_KEEPTRYING;
01958       if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
01959          work = peer;
01960          idle = chan;
01961       } else {
01962          work = chan;
01963          idle = peer;
01964       }
01965    }
01966 
01967    if (!(app = pbx_findapp(feature->app))) {
01968       ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
01969       return -2;
01970    }
01971 
01972    ast_autoservice_start(idle);
01973    ast_autoservice_ignore(idle, AST_FRAME_DTMF_END);
01974    
01975    if (!ast_strlen_zero(feature->moh_class))
01976       ast_moh_start(idle, feature->moh_class, NULL);
01977 
01978    res = pbx_exec(work, app, feature->app_args);
01979 
01980    if (!ast_strlen_zero(feature->moh_class))
01981       ast_moh_stop(idle);
01982 
01983    ast_autoservice_stop(idle);
01984 
01985    if (res) {
01986       return AST_FEATURE_RETURN_SUCCESSBREAK;
01987    }
01988    return AST_FEATURE_RETURN_SUCCESS;  /*! \todo XXX should probably return res */
01989 }
01990 
01991 static void unmap_features(void)
01992 {
01993    int x;
01994 
01995    ast_rwlock_wrlock(&features_lock);
01996    for (x = 0; x < FEATURES_COUNT; x++)
01997       strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
01998    ast_rwlock_unlock(&features_lock);
01999 }
02000 
02001 static int remap_feature(const char *name, const char *value)
02002 {
02003    int x, res = -1;
02004 
02005    ast_rwlock_wrlock(&features_lock);
02006    for (x = 0; x < FEATURES_COUNT; x++) {
02007       if (strcasecmp(builtin_features[x].sname, name))
02008          continue;
02009 
02010       ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
02011       res = 0;
02012       break;
02013    }
02014    ast_rwlock_unlock(&features_lock);
02015 
02016    return res;
02017 }
02018 
02019 /*!
02020  * \brief Check the dynamic features
02021  * \param chan,peer,config,code,sense
02022  *
02023  * Lock features list, browse for code, unlock list
02024  * \retval res on success.
02025  * \retval -1 on failure.
02026 */
02027 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense)
02028 {
02029    int x;
02030    struct ast_flags features;
02031    struct ast_call_feature *feature = NULL;
02032    struct feature_group *fg = NULL;
02033    struct feature_group_exten *fge;
02034    const char *peer_dynamic_features, *chan_dynamic_features;
02035    char dynamic_features_buf[128];
02036    char *tmp, *tok;
02037    int res = AST_FEATURE_RETURN_PASSDIGITS;
02038    int feature_detected = 0;
02039 
02040    if (sense == FEATURE_SENSE_CHAN) {
02041       ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
02042    }
02043    else {
02044       ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
02045    }
02046 
02047    ast_channel_lock(peer);
02048    peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
02049    ast_channel_unlock(peer);
02050 
02051    ast_channel_lock(chan);
02052    chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
02053    ast_channel_unlock(chan);
02054 
02055    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,""));
02056 
02057    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);
02058 
02059    ast_rwlock_rdlock(&features_lock);
02060    for (x = 0; x < FEATURES_COUNT; x++) {
02061       if ((ast_test_flag(&features, builtin_features[x].feature_mask)) &&
02062           !ast_strlen_zero(builtin_features[x].exten)) {
02063          /* Feature is up for consideration */
02064          if (!strcmp(builtin_features[x].exten, code)) {
02065             ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
02066             res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
02067             feature_detected = 1;
02068             break;
02069          } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
02070             if (res == AST_FEATURE_RETURN_PASSDIGITS)
02071                res = AST_FEATURE_RETURN_STOREDIGITS;
02072          }
02073       }
02074    }
02075    ast_rwlock_unlock(&features_lock);
02076 
02077    if (ast_strlen_zero(dynamic_features_buf) || feature_detected)
02078       return res;
02079 
02080    tmp = dynamic_features_buf;
02081 
02082    while ((tok = strsep(&tmp, "#"))) {
02083       AST_RWLIST_RDLOCK(&feature_groups);
02084 
02085       fg = find_group(tok);
02086 
02087       if (fg) {
02088          AST_LIST_TRAVERSE(&fg->features, fge, entry) {
02089             if (!strcmp(fge->exten, code)) {
02090                res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
02091                memcpy(feature, fge->feature, sizeof(feature));
02092                if (res != AST_FEATURE_RETURN_KEEPTRYING) {
02093                   AST_RWLIST_UNLOCK(&feature_groups);
02094                   break;
02095                }
02096                res = AST_FEATURE_RETURN_PASSDIGITS;
02097             } else if (!strncmp(fge->exten, code, strlen(code))) {
02098                res = AST_FEATURE_RETURN_STOREDIGITS;
02099             }
02100          }
02101          if (fge) {
02102             break;
02103          }
02104       }
02105 
02106       AST_RWLIST_UNLOCK(&feature_groups);
02107 
02108       AST_RWLIST_RDLOCK(&feature_list);
02109 
02110       if (!(feature = find_dynamic_feature(tok))) {
02111          AST_RWLIST_UNLOCK(&feature_list);
02112          continue;
02113       }
02114          
02115       /* Feature is up for consideration */
02116       if (!strcmp(feature->exten, code)) {
02117          ast_verb(3, " Feature Found: %s exten: %s\n",feature->sname, tok);
02118          res = feature->operation(chan, peer, config, code, sense, feature);
02119          if (res != AST_FEATURE_RETURN_KEEPTRYING) {
02120             AST_RWLIST_UNLOCK(&feature_list);
02121             break;
02122          }
02123          res = AST_FEATURE_RETURN_PASSDIGITS;
02124       } else if (!strncmp(feature->exten, code, strlen(code)))
02125          res = AST_FEATURE_RETURN_STOREDIGITS;
02126 
02127       AST_RWLIST_UNLOCK(&feature_list);
02128    }
02129    
02130    return res;
02131 }
02132 
02133 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
02134 {
02135    int x;
02136    
02137    ast_clear_flag(config, AST_FLAGS_ALL);
02138 
02139    ast_rwlock_rdlock(&features_lock);
02140    for (x = 0; x < FEATURES_COUNT; x++) {
02141       if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
02142          continue;
02143 
02144       if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
02145          ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
02146 
02147       if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
02148          ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
02149    }
02150    ast_rwlock_unlock(&features_lock);
02151    
02152    if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
02153       const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
02154 
02155       if (dynamic_features) {
02156          char *tmp = ast_strdupa(dynamic_features);
02157          char *tok;
02158          struct ast_call_feature *feature;
02159 
02160          /* while we have a feature */
02161          while ((tok = strsep(&tmp, "#"))) {
02162             struct feature_group *fg;
02163 
02164             AST_RWLIST_RDLOCK(&feature_groups);
02165             AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
02166                struct feature_group_exten *fge;
02167 
02168                AST_LIST_TRAVERSE(&fg->features, fge, entry) {
02169                   if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLER)) {
02170                      ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
02171                   }
02172                   if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLEE)) {
02173                      ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
02174                   }
02175                }
02176             }
02177             AST_RWLIST_UNLOCK(&feature_groups);
02178 
02179             AST_RWLIST_RDLOCK(&feature_list);
02180             if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
02181                if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER)) {
02182                   ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
02183                }
02184                if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE)) {
02185                   ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
02186                }
02187             }
02188             AST_RWLIST_UNLOCK(&feature_list);
02189          }
02190       }
02191    }
02192 }
02193 
02194 /*! 
02195  * \brief Get feature and dial
02196  * \param caller,transferee,type,format,data,timeout,outstate,cid_num,cid_name,igncallerstate
02197  *
02198  * Request channel, set channel variables, initiate call,check if they want to disconnect
02199  * go into loop, check if timeout has elapsed, check if person to be transfered hung up,
02200  * check for answer break loop, set cdr return channel.
02201  *
02202  * \todo XXX Check - this is very similar to the code in channel.c 
02203  * \return always a channel
02204 */
02205 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate, const char *language)
02206 {
02207    int state = 0;
02208    int cause = 0;
02209    int to;
02210    struct ast_channel *chan;
02211    struct ast_channel *monitor_chans[2];
02212    struct ast_channel *active_channel;
02213    int res = 0, ready = 0;
02214    struct timeval started;
02215    int x, len = 0;
02216    char *disconnect_code = NULL, *dialed_code = NULL;
02217 
02218    if (!(chan = ast_request(type, format, data, &cause))) {
02219       ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
02220       switch(cause) {
02221       case AST_CAUSE_BUSY:
02222          state = AST_CONTROL_BUSY;
02223          break;
02224       case AST_CAUSE_CONGESTION:
02225          state = AST_CONTROL_CONGESTION;
02226          break;
02227       }
02228       goto done;
02229    }
02230 
02231    ast_set_callerid(chan, cid_num, cid_name, cid_num);
02232    ast_string_field_set(chan, language, language);
02233    ast_channel_inherit_variables(caller, chan); 
02234    pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller->name);
02235       
02236    if (ast_call(chan, data, timeout)) {
02237       ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
02238       goto done;
02239    }
02240    
02241    ast_indicate(caller, AST_CONTROL_RINGING);
02242    /* support dialing of the featuremap disconnect code while performing an attended tranfer */
02243    ast_rwlock_rdlock(&features_lock);
02244    for (x = 0; x < FEATURES_COUNT; x++) {
02245       if (strcasecmp(builtin_features[x].sname, "disconnect"))
02246          continue;
02247 
02248       disconnect_code = builtin_features[x].exten;
02249       len = strlen(disconnect_code) + 1;
02250       dialed_code = alloca(len);
02251       memset(dialed_code, 0, len);
02252       break;
02253    }
02254    ast_rwlock_unlock(&features_lock);
02255    x = 0;
02256    started = ast_tvnow();
02257    to = timeout;
02258 
02259    ast_poll_channel_add(caller, chan);
02260 
02261    while (!((transferee && ast_check_hangup(transferee)) && (!igncallerstate && ast_check_hangup(caller))) && timeout && (chan->_state != AST_STATE_UP)) {
02262       struct ast_frame *f = NULL;
02263 
02264       monitor_chans[0] = caller;
02265       monitor_chans[1] = chan;
02266       active_channel = ast_waitfor_n(monitor_chans, 2, &to);
02267 
02268       /* see if the timeout has been violated */
02269       if(ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
02270          state = AST_CONTROL_UNHOLD;
02271          ast_log(LOG_NOTICE, "We exceeded our AT-timeout\n");
02272          break; /*doh! timeout*/
02273       }
02274 
02275       if (!active_channel)
02276          continue;
02277 
02278       if (chan && (chan == active_channel)){
02279          if (!ast_strlen_zero(chan->call_forward)) {
02280             if (!(chan = ast_call_forward(caller, chan, NULL, format, NULL, outstate))) {
02281                return NULL;
02282             }
02283             continue;
02284          }
02285          f = ast_read(chan);
02286          if (f == NULL) { /*doh! where'd he go?*/
02287             state = AST_CONTROL_HANGUP;
02288             res = 0;
02289             break;
02290          }
02291          
02292          if (f->frametype == AST_FRAME_CONTROL || f->frametype == AST_FRAME_DTMF || f->frametype == AST_FRAME_TEXT) {
02293             if (f->subclass == AST_CONTROL_RINGING) {
02294                state = f->subclass;
02295                ast_verb(3, "%s is ringing\n", chan->name);
02296                ast_indicate(caller, AST_CONTROL_RINGING);
02297             } else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
02298                state = f->subclass;
02299                ast_verb(3, "%s is busy\n", chan->name);
02300                ast_indicate(caller, AST_CONTROL_BUSY);
02301                ast_frfree(f);
02302                f = NULL;
02303                break;
02304             } else if (f->subclass == AST_CONTROL_ANSWER) {
02305                /* This is what we are hoping for */
02306                state = f->subclass;
02307                ast_frfree(f);
02308                f = NULL;
02309                ready=1;
02310                break;
02311             } else if (f->subclass != -1 && f->subclass != AST_CONTROL_PROGRESS) {
02312                ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass);
02313             }
02314             /* else who cares */
02315          } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
02316             ast_write(caller, f);
02317          }
02318 
02319       } else if (caller && (active_channel == caller)) {
02320          f = ast_read(caller);
02321          if (f == NULL) { /*doh! where'd he go?*/
02322             if (!igncallerstate) {
02323                if (ast_check_hangup(caller) && !ast_check_hangup(chan)) {
02324                   /* make this a blind transfer */
02325                   ready = 1;
02326                   break;
02327                }
02328                state = AST_CONTROL_HANGUP;
02329                res = 0;
02330                break;
02331             }
02332          } else {
02333          
02334             if (f->frametype == AST_FRAME_DTMF) {
02335                dialed_code[x++] = f->subclass;
02336                dialed_code[x] = '\0';
02337                if (strlen(dialed_code) == len) {
02338                   x = 0;
02339                } else if (x && strncmp(dialed_code, disconnect_code, x)) {
02340                   x = 0;
02341                   dialed_code[x] = '\0';
02342                }
02343                if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
02344                   /* Caller Canceled the call */
02345                   state = AST_CONTROL_UNHOLD;
02346                   ast_frfree(f);
02347                   f = NULL;
02348                   break;
02349                }
02350             } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
02351                ast_write(chan, f);
02352             }
02353          }
02354       }
02355       if (f)
02356          ast_frfree(f);
02357    } /* end while */
02358 
02359    ast_poll_channel_del(caller, chan);
02360       
02361 done:
02362    ast_indicate(caller, -1);
02363    if (chan && ready) {
02364       if (chan->_state == AST_STATE_UP) 
02365          state = AST_CONTROL_ANSWER;
02366       res = 0;
02367    } else if (chan) {
02368       res = -1;
02369       ast_hangup(chan);
02370       chan = NULL;
02371    } else {
02372       res = -1;
02373    }
02374    
02375    if (outstate)
02376       *outstate = state;
02377 
02378    return chan;
02379 }
02380 
02381 /*!
02382  * \brief return the first unlocked cdr in a possible chain
02383 */
02384 static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
02385 {
02386    struct ast_cdr *cdr_orig = cdr;
02387    while (cdr) {
02388       if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
02389          return cdr;
02390       cdr = cdr->next;
02391    }
02392    return cdr_orig; /* everybody LOCKED or some other weirdness, like a NULL */
02393 }
02394 
02395 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
02396 {
02397    const char *feature;
02398 
02399    if (ast_strlen_zero(features)) {
02400       return;
02401    }
02402 
02403    for (feature = features; *feature; feature++) {
02404       switch (*feature) {
02405       case 'T' :
02406       case 't' :
02407          ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
02408          break;
02409       case 'K' :
02410       case 'k' :
02411          ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
02412          break;
02413       case 'H' :
02414       case 'h' :
02415          ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
02416          break;
02417       case 'W' :
02418       case 'w' :
02419          ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
02420          break;
02421       default :
02422          ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
02423       }
02424    }
02425 }
02426 
02427 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
02428 {
02429    struct ast_datastore *ds_callee_features = NULL, *ds_caller_features = NULL;
02430    struct ast_dial_features *callee_features = NULL, *caller_features = NULL;
02431 
02432    ast_channel_lock(caller);
02433    ds_caller_features = ast_channel_datastore_find(caller, &dial_features_info, NULL);
02434    ast_channel_unlock(caller);
02435    if (!ds_caller_features) {
02436       if (!(ds_caller_features = ast_datastore_alloc(&dial_features_info, NULL))) {
02437          ast_log(LOG_WARNING, "Unable to create channel datastore for caller features. Aborting!\n");
02438          return;
02439       }
02440       if (!(caller_features = ast_calloc(1, sizeof(*caller_features)))) {
02441          ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
02442          ast_datastore_free(ds_caller_features);
02443          return;
02444       }
02445       ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
02446       caller_features->is_caller = 1;
02447       ast_copy_flags(&(caller_features->features_callee), &(config->features_callee), AST_FLAGS_ALL);
02448       ast_copy_flags(&(caller_features->features_caller), &(config->features_caller), AST_FLAGS_ALL);
02449       ds_caller_features->data = caller_features;
02450       ast_channel_lock(caller);
02451       ast_channel_datastore_add(caller, ds_caller_features);
02452       ast_channel_unlock(caller);
02453    } else {
02454       /* If we don't return here, then when we do a builtin_atxfer we will copy the disconnect
02455        * flags over from the atxfer to the caller */
02456       return;
02457    }
02458 
02459    ast_channel_lock(callee);
02460    ds_callee_features = ast_channel_datastore_find(callee, &dial_features_info, NULL);
02461    ast_channel_unlock(callee);
02462    if (!ds_callee_features) {
02463       if (!(ds_callee_features = ast_datastore_alloc(&dial_features_info, NULL))) {
02464          ast_log(LOG_WARNING, "Unable to create channel datastore for callee features. Aborting!\n");
02465          return;
02466       }
02467       if (!(callee_features = ast_calloc(1, sizeof(*callee_features)))) {
02468          ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
02469          ast_datastore_free(ds_callee_features);
02470          return;
02471       }
02472       ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
02473       callee_features->is_caller = 0;
02474       ast_copy_flags(&(callee_features->features_callee), &(config->features_caller), AST_FLAGS_ALL);
02475       ast_copy_flags(&(callee_features->features_caller), &(config->features_callee), AST_FLAGS_ALL);
02476       ds_callee_features->data = callee_features;
02477       ast_channel_lock(callee);
02478       ast_channel_datastore_add(callee, ds_callee_features);
02479       ast_channel_unlock(callee);
02480    }
02481 
02482    return;
02483 }
02484 
02485 /*!
02486  * \brief bridge the call and set CDR
02487  * \param chan,peer,config
02488  * 
02489  * Set start time, check for two channels,check if monitor on
02490  * check for feature activation, create new CDR
02491  * \retval res on success.
02492  * \retval -1 on failure to bridge.
02493 */
02494 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
02495 {
02496    /* Copy voice back and forth between the two channels.  Give the peer
02497       the ability to transfer calls with '#<extension' syntax. */
02498    struct ast_frame *f;
02499    struct ast_channel *who;
02500    char chan_featurecode[FEATURE_MAX_LEN + 1]="";
02501    char peer_featurecode[FEATURE_MAX_LEN + 1]="";
02502    char orig_channame[AST_MAX_EXTENSION];
02503    char orig_peername[AST_MAX_EXTENSION];
02504    int res;
02505    int diff;
02506    int hasfeatures=0;
02507    int hadfeatures=0;
02508    int autoloopflag;
02509    struct ast_option_header *aoh;
02510    struct ast_bridge_config backup_config;
02511    struct ast_cdr *bridge_cdr = NULL;
02512    struct ast_cdr *orig_peer_cdr = NULL;
02513    struct ast_cdr *chan_cdr = chan->cdr; /* the proper chan cdr, if there are forked cdrs */
02514    struct ast_cdr *peer_cdr = peer->cdr; /* the proper chan cdr, if there are forked cdrs */
02515    struct ast_cdr *new_chan_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
02516    struct ast_cdr *new_peer_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
02517 
02518    memset(&backup_config, 0, sizeof(backup_config));
02519 
02520    config->start_time = ast_tvnow();
02521 
02522    if (chan && peer) {
02523       pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
02524       pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
02525    } else if (chan) {
02526       pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
02527    }
02528 
02529    set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
02530    add_features_datastores(chan, peer, config);
02531 
02532    /* This is an interesting case.  One example is if a ringing channel gets redirected to
02533     * an extension that picks up a parked call.  This will make sure that the call taken
02534     * out of parking gets told that the channel it just got bridged to is still ringing. */
02535    if (chan->_state == AST_STATE_RINGING && peer->visible_indication != AST_CONTROL_RINGING) {
02536       ast_indicate(peer, AST_CONTROL_RINGING);
02537    }
02538 
02539    if (monitor_ok) {
02540       const char *monitor_exec;
02541       struct ast_channel *src = NULL;
02542       if (!monitor_app) { 
02543          if (!(monitor_app = pbx_findapp("Monitor")))
02544             monitor_ok=0;
02545       }
02546       if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR"))) 
02547          src = chan;
02548       else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
02549          src = peer;
02550       if (monitor_app && src) {
02551          char *tmp = ast_strdupa(monitor_exec);
02552          pbx_exec(src, monitor_app, tmp);
02553       }
02554    }
02555 
02556    set_config_flags(chan, peer, config);
02557    config->firstpass = 1;
02558 
02559    /* Answer if need be */
02560    if (chan->_state != AST_STATE_UP) {
02561       if (ast_raw_answer(chan, 1)) {
02562          return -1;
02563       }
02564    }
02565 
02566    ast_copy_string(orig_channame,chan->name,sizeof(orig_channame));
02567    ast_copy_string(orig_peername,peer->name,sizeof(orig_peername));
02568    orig_peer_cdr = peer_cdr;
02569    
02570    if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
02571       
02572       if (chan_cdr) {
02573          ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
02574          ast_cdr_update(chan);
02575          bridge_cdr = ast_cdr_dup(chan_cdr);
02576          /* rip any forked CDR's off of the chan_cdr and attach
02577           * them to the bridge_cdr instead */
02578          bridge_cdr->next = chan_cdr->next;
02579          chan_cdr->next = NULL;
02580          ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
02581          ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
02582          if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
02583             ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
02584          }
02585          ast_cdr_setaccount(peer, chan->accountcode);
02586 
02587       } else {
02588          /* better yet, in a xfer situation, find out why the chan cdr got zapped (pun unintentional) */
02589          bridge_cdr = ast_cdr_alloc(); /* this should be really, really rare/impossible? */
02590          ast_copy_string(bridge_cdr->channel, chan->name, sizeof(bridge_cdr->channel));
02591          ast_copy_string(bridge_cdr->dstchannel, peer->name, sizeof(bridge_cdr->dstchannel));
02592          ast_copy_string(bridge_cdr->uniqueid, chan->uniqueid, sizeof(bridge_cdr->uniqueid));
02593          ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
02594          ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
02595          ast_cdr_setcid(bridge_cdr, chan);
02596          bridge_cdr->disposition = (chan->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NULL;
02597          bridge_cdr->amaflags = chan->amaflags ? chan->amaflags :  ast_default_amaflags;
02598          ast_copy_string(bridge_cdr->accountcode, chan->accountcode, sizeof(bridge_cdr->accountcode));
02599          /* Destination information */
02600          ast_copy_string(bridge_cdr->dst, chan->exten, sizeof(bridge_cdr->dst));
02601          ast_copy_string(bridge_cdr->dcontext, chan->context, sizeof(bridge_cdr->dcontext));
02602          if (peer_cdr) {
02603             bridge_cdr->start = peer_cdr->start;
02604             ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
02605          } else {
02606             ast_cdr_start(bridge_cdr);
02607          }
02608       }
02609       ast_debug(4,"bridge answer set, chan answer set\n");
02610       /* peer_cdr->answer will be set when a macro runs on the peer;
02611          in that case, the bridge answer will be delayed while the
02612          macro plays on the peer channel. The peer answered the call
02613          before the macro started playing. To the phone system,
02614          this is billable time for the call, even tho the caller
02615          hears nothing but ringing while the macro does its thing. */
02616 
02617       /* Another case where the peer cdr's time will be set, is when
02618          A self-parks by pickup up phone and dialing 700, then B
02619          picks up A by dialing its parking slot; there may be more 
02620          practical paths that get the same result, tho... in which
02621          case you get the previous answer time from the Park... which
02622          is before the bridge's start time, so I added in the 
02623          tvcmp check to the if below */
02624 
02625       if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
02626          ast_cdr_setanswer(bridge_cdr, peer_cdr->answer);
02627          ast_cdr_setdisposition(bridge_cdr, peer_cdr->disposition);
02628          if (chan_cdr) {
02629             ast_cdr_setanswer(chan_cdr, peer_cdr->answer);
02630             ast_cdr_setdisposition(chan_cdr, peer_cdr->disposition);
02631          }
02632       } else {
02633          ast_cdr_answer(bridge_cdr);
02634          if (chan_cdr) {
02635             ast_cdr_answer(chan_cdr); /* for the sake of cli status checks */
02636          }
02637       }
02638       if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
02639          if (chan_cdr) {
02640             ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
02641          }
02642          if (peer_cdr) {
02643             ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
02644          }
02645       }
02646       /* the DIALED flag may be set if a dialed channel is transfered
02647        * and then bridged to another channel.  In order for the
02648        * bridge CDR to be written, the DIALED flag must not be
02649        * present. */
02650       ast_clear_flag(bridge_cdr, AST_CDR_FLAG_DIALED);
02651    }
02652    for (;;) {
02653       struct ast_channel *other; /* used later */
02654    
02655       res = ast_channel_bridge(chan, peer, config, &f, &who);
02656       
02657       /* When frame is not set, we are probably involved in a situation
02658          where we've timed out.
02659          When frame is set, we'll come this code twice; once for DTMF_BEGIN
02660          and also for DTMF_END. If we flow into the following 'if' for both, then 
02661          our wait times are cut in half, as both will subtract from the
02662          feature_timer. Not good!
02663       */
02664       if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
02665          /* Update time limit for next pass */
02666          diff = ast_tvdiff_ms(ast_tvnow(), config->start_time);
02667          if (res == AST_BRIDGE_RETRY) {
02668             /* The feature fully timed out but has not been updated. Skip
02669              * the potential round error from the diff calculation and
02670              * explicitly set to expired. */
02671             config->feature_timer = -1;
02672          } else {
02673             config->feature_timer -= diff;
02674          }
02675 
02676          if (hasfeatures) {
02677             /* Running on backup config, meaning a feature might be being
02678                activated, but that's no excuse to keep things going 
02679                indefinitely! */
02680             if (backup_config.feature_timer && ((backup_config.feature_timer -= diff) <= 0)) {
02681                ast_debug(1, "Timed out, realtime this time!\n");
02682                config->feature_timer = 0;
02683                who = chan;
02684                if (f)
02685                   ast_frfree(f);
02686                f = NULL;
02687                res = 0;
02688             } else if (config->feature_timer <= 0) {
02689                /* Not *really* out of time, just out of time for
02690                   digits to come in for features. */
02691                ast_debug(1, "Timed out for feature!\n");
02692                if (!ast_strlen_zero(peer_featurecode)) {
02693                   ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
02694                   memset(peer_featurecode, 0, sizeof(peer_featurecode));
02695                }
02696                if (!ast_strlen_zero(chan_featurecode)) {
02697                   ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
02698                   memset(chan_featurecode, 0, sizeof(chan_featurecode));
02699                }
02700                if (f)
02701                   ast_frfree(f);
02702                hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
02703                if (!hasfeatures) {
02704                   /* Restore original (possibly time modified) bridge config */
02705                   memcpy(config, &backup_config, sizeof(struct ast_bridge_config));
02706                   memset(&backup_config, 0, sizeof(backup_config));
02707                }
02708                hadfeatures = hasfeatures;
02709                /* Continue as we were */
02710                continue;
02711             } else if (!f) {
02712                /* The bridge returned without a frame and there is a feature in progress.
02713                 * However, we don't think the feature has quite yet timed out, so just
02714                 * go back into the bridge. */
02715                continue;
02716             }
02717          } else {
02718             if (config->feature_timer <=0) {
02719                /* We ran out of time */
02720                config->feature_timer = 0;
02721                who = chan;
02722                if (f)
02723                   ast_frfree(f);
02724                f = NULL;
02725                res = 0;
02726             }
02727          }
02728       }
02729       if (res < 0) {
02730          if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer))
02731             ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
02732          goto before_you_go;
02733       }
02734       
02735       if (!f || (f->frametype == AST_FRAME_CONTROL &&
02736             (f->subclass == AST_CONTROL_HANGUP || f->subclass == AST_CONTROL_BUSY || 
02737                f->subclass == AST_CONTROL_CONGESTION))) {
02738          res = -1;
02739          break;
02740       }
02741       /* many things should be sent to the 'other' channel */
02742       other = (who == chan) ? peer : chan;
02743       if (f->frametype == AST_FRAME_CONTROL) {
02744          switch (f->subclass) {
02745          case AST_CONTROL_RINGING:
02746          case AST_CONTROL_FLASH:
02747          case -1:
02748             ast_indicate(other, f->subclass);
02749             break;
02750          case AST_CONTROL_HOLD:
02751          case AST_CONTROL_UNHOLD:
02752             ast_indicate_data(other, f->subclass, f->data.ptr, f->datalen);
02753             break;
02754          case AST_CONTROL_OPTION:
02755             aoh = f->data.ptr;
02756             /* Forward option Requests */
02757             if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
02758                ast_channel_setoption(other, ntohs(aoh->option), aoh->data, 
02759                   f->datalen - sizeof(struct ast_option_header), 0);
02760             }
02761             break;
02762          }
02763       } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
02764          /* eat it */
02765       } else if (f->frametype == AST_FRAME_DTMF) {
02766          char *featurecode;
02767          int sense;
02768 
02769          hadfeatures = hasfeatures;
02770          /* This cannot overrun because the longest feature is one shorter than our buffer */
02771          if (who == chan) {
02772             sense = FEATURE_SENSE_CHAN;
02773             featurecode = chan_featurecode;
02774          } else  {
02775             sense = FEATURE_SENSE_PEER;
02776             featurecode = peer_featurecode;
02777          }
02778          /*! append the event to featurecode. we rely on the string being zero-filled, and
02779           * not overflowing it. 
02780           * \todo XXX how do we guarantee the latter ?
02781           */
02782          featurecode[strlen(featurecode)] = f->subclass;
02783          /* Get rid of the frame before we start doing "stuff" with the channels */
02784          ast_frfree(f);
02785          f = NULL;
02786          config->feature_timer = backup_config.feature_timer;
02787          res = feature_interpret(chan, peer, config, featurecode, sense);
02788          switch(res) {
02789          case AST_FEATURE_RETURN_PASSDIGITS:
02790             ast_dtmf_stream(other, who, featurecode, 0, 0);
02791             /* Fall through */
02792          case AST_FEATURE_RETURN_SUCCESS:
02793             memset(featurecode, 0, sizeof(chan_featurecode));
02794             break;
02795          }
02796          if (res >= AST_FEATURE_RETURN_PASSDIGITS) {
02797             res = 0;
02798          } else 
02799             break;
02800          hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
02801          if (hadfeatures && !hasfeatures) {
02802             /* Restore backup */
02803             memcpy(config, &backup_config, sizeof(struct ast_bridge_config));
02804             memset(&backup_config, 0, sizeof(struct ast_bridge_config));
02805          } else if (hasfeatures) {
02806             if (!hadfeatures) {
02807                /* Backup configuration */
02808                memcpy(&backup_config, config, sizeof(struct ast_bridge_config));
02809                /* Setup temporary config options */
02810                config->play_warning = 0;
02811                ast_clear_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
02812                ast_clear_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
02813                config->warning_freq = 0;
02814                config->warning_sound = NULL;
02815                config->end_sound = NULL;
02816                config->start_sound = NULL;
02817                config->firstpass = 0;
02818             }
02819             config->start_time = ast_tvnow();
02820             config->feature_timer = featuredigittimeout;
02821             ast_debug(1, "Set time limit to %ld\n", config->feature_timer);
02822          }
02823       }
02824       if (f)
02825          ast_frfree(f);
02826 
02827    }
02828    before_you_go:
02829 
02830    if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT)) {
02831       ast_clear_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT); /* its job is done */
02832       if (bridge_cdr) {
02833          ast_cdr_discard(bridge_cdr);
02834          /* QUESTION: should we copy bridge_cdr fields to the peer before we throw it away? */
02835       }
02836       return res; /* if we shouldn't do the h-exten, we shouldn't do the bridge cdr, either! */
02837    }
02838 
02839    if (config->end_bridge_callback) {
02840       config->end_bridge_callback(config->end_bridge_callback_data);
02841    }
02842 
02843    /* run the hangup exten on the chan object IFF it was NOT involved in a parking situation 
02844     * if it were, then chan belongs to a different thread now, and might have been hung up long
02845      * ago.
02846     */
02847    if (!ast_test_flag(&(config->features_caller),AST_FEATURE_NO_H_EXTEN) &&
02848       ast_exists_extension(chan, chan->context, "h", 1, chan->cid.cid_num)) {
02849       struct ast_cdr *swapper = NULL;
02850       char savelastapp[AST_MAX_EXTENSION];
02851       char savelastdata[AST_MAX_EXTENSION];
02852       char save_exten[AST_MAX_EXTENSION];
02853       int  save_prio;
02854       int  found = 0;   /* set if we find at least one match */
02855       int  spawn_error = 0;
02856       
02857       autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
02858       ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
02859       if (bridge_cdr && ast_opt_end_cdr_before_h_exten) {
02860          ast_cdr_end(bridge_cdr);
02861       }
02862       /* swap the bridge cdr and the chan cdr for a moment, and let the endbridge
02863          dialplan code operate on it */
02864       ast_channel_lock(chan);
02865       if (bridge_cdr) {
02866          swapper = chan->cdr;
02867          ast_copy_string(savelastapp, bridge_cdr->lastapp, sizeof(bridge_cdr->lastapp));
02868          ast_copy_string(savelastdata, bridge_cdr->lastdata, sizeof(bridge_cdr->lastdata));
02869          chan->cdr = bridge_cdr;
02870       }
02871       ast_copy_string(save_exten, chan->exten, sizeof(save_exten));
02872       save_prio = chan->priority;
02873       ast_copy_string(chan->exten, "h", sizeof(chan->exten));
02874       chan->priority = 1;
02875       ast_channel_unlock(chan);
02876       while ((spawn_error = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num, &found, 1)) == 0) {
02877          chan->priority++;
02878       }
02879       if (spawn_error && (!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num) || ast_check_hangup(chan))) {
02880          /* if the extension doesn't exist or a hangup occurred, this isn't really a spawn error */
02881          spawn_error = 0;
02882       }
02883       if (found && spawn_error) {
02884          /* Something bad happened, or a hangup has been requested. */
02885          ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
02886          ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
02887       }
02888       /* swap it back */
02889       ast_channel_lock(chan);
02890       ast_copy_string(chan->exten, save_exten, sizeof(chan->exten));
02891       chan->priority = save_prio;
02892       if (bridge_cdr) {
02893          if (chan->cdr == bridge_cdr) {
02894             chan->cdr = swapper;
02895          } else {
02896             bridge_cdr = NULL;
02897          }
02898       }
02899       if (!spawn_error) {
02900          ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN);
02901       }
02902       ast_channel_unlock(chan);
02903       /* protect the lastapp/lastdata against the effects of the hangup/dialplan code */
02904       if (bridge_cdr) {
02905          ast_copy_string(bridge_cdr->lastapp, savelastapp, sizeof(bridge_cdr->lastapp));
02906          ast_copy_string(bridge_cdr->lastdata, savelastdata, sizeof(bridge_cdr->lastdata));
02907       }
02908       ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
02909    }
02910    
02911    /* obey the NoCDR() wishes. -- move the DISABLED flag to the bridge CDR if it was set on the channel during the bridge... */
02912    new_chan_cdr = pick_unlocked_cdr(chan->cdr); /* the proper chan cdr, if there are forked cdrs */
02913    if (bridge_cdr && new_chan_cdr && ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED))
02914       ast_set_flag(bridge_cdr, AST_CDR_FLAG_POST_DISABLED);
02915 
02916    /* we can post the bridge CDR at this point */
02917    if (bridge_cdr) {
02918       ast_cdr_end(bridge_cdr);
02919       ast_cdr_detach(bridge_cdr);
02920    }
02921    
02922    /* do a specialized reset on the beginning channel
02923       CDR's, if they still exist, so as not to mess up
02924       issues in future bridges;
02925       
02926       Here are the rules of the game:
02927       1. The chan and peer channel pointers will not change
02928          during the life of the bridge.
02929       2. But, in transfers, the channel names will change.
02930          between the time the bridge is started, and the
02931          time the channel ends. 
02932          Usually, when a channel changes names, it will
02933          also change CDR pointers.
02934       3. Usually, only one of the two channels (chan or peer)
02935          will change names.
02936       4. Usually, if a channel changes names during a bridge,
02937          it is because of a transfer. Usually, in these situations,
02938          it is normal to see 2 bridges running simultaneously, and
02939          it is not unusual to see the two channels that change
02940          swapped between bridges.
02941       5. After a bridge occurs, we have 2 or 3 channels' CDRs
02942          to attend to; if the chan or peer changed names,
02943          we have the before and after attached CDR's.
02944    */
02945    
02946    if (new_chan_cdr) {
02947       struct ast_channel *chan_ptr = NULL;
02948  
02949       if (strcasecmp(orig_channame, chan->name) != 0) { 
02950          /* old channel */
02951          chan_ptr = ast_get_channel_by_name_locked(orig_channame);
02952          if (chan_ptr) {
02953             if (!ast_bridged_channel(chan_ptr)) {
02954                struct ast_cdr *cur;
02955                for (cur = chan_ptr->cdr; cur; cur = cur->next) {
02956                   if (cur == chan_cdr) {
02957                      break;
02958                   }
02959                }
02960                if (cur)
02961                   ast_cdr_specialized_reset(chan_cdr,0);
02962             }
02963             ast_channel_unlock(chan_ptr);
02964          }
02965          /* new channel */
02966          ast_cdr_specialized_reset(new_chan_cdr,0);
02967       } else {
02968          ast_cdr_specialized_reset(chan->cdr,0); /* nothing changed, reset the chan cdr  */
02969       }
02970    }
02971    
02972    {
02973       struct ast_channel *chan_ptr = NULL;
02974       new_peer_cdr = pick_unlocked_cdr(peer->cdr); /* the proper chan cdr, if there are forked cdrs */
02975       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))
02976          ast_set_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED); /* DISABLED is viral-- it will propagate across a bridge */
02977       if (strcasecmp(orig_peername, peer->name) != 0) { 
02978          /* old channel */
02979          chan_ptr = ast_get_channel_by_name_locked(orig_peername);
02980          if (chan_ptr) {
02981             if (!ast_bridged_channel(chan_ptr)) {
02982                struct ast_cdr *cur;
02983                for (cur = chan_ptr->cdr; cur; cur = cur->next) {
02984                   if (cur == peer_cdr) {
02985                      break;
02986                   }
02987                }
02988                if (cur)
02989                   ast_cdr_specialized_reset(peer_cdr,0);
02990             }
02991             ast_channel_unlock(chan_ptr);
02992          }
02993          /* new channel */
02994          if (new_peer_cdr) {
02995             ast_cdr_specialized_reset(new_peer_cdr, 0);
02996          }
02997       } else {
02998          ast_cdr_specialized_reset(peer->cdr, 0); /* nothing changed, reset the peer cdr  */
02999       }
03000    }
03001    
03002    return res;
03003 }
03004 
03005 /*! \brief Output parking event to manager */
03006 static void post_manager_event(const char *s, struct parkeduser *pu)
03007 {
03008    manager_event(EVENT_FLAG_CALL, s,
03009       "Exten: %s\r\n"
03010       "Channel: %s\r\n"
03011       "Parkinglot: %s\r\n"
03012       "CallerIDNum: %s\r\n"
03013       "CallerIDName: %s\r\n"
03014       "UniqueID: %s\r\n\r\n",
03015       pu->parkingexten, 
03016       pu->chan->name,
03017       pu->parkinglot->name,
03018       S_OR(pu->chan->cid.cid_num, "<unknown>"),
03019       S_OR(pu->chan->cid.cid_name, "<unknown>"),
03020       pu->chan->uniqueid
03021       );
03022 }
03023 
03024 static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_flags *features_caller, char *options, size_t len)
03025 {
03026    int i = 0;
03027    enum {
03028       OPT_CALLEE_REDIRECT   = 't',
03029       OPT_CALLER_REDIRECT   = 'T',
03030       OPT_CALLEE_AUTOMON    = 'w',
03031       OPT_CALLER_AUTOMON    = 'W',
03032       OPT_CALLEE_DISCONNECT = 'h',
03033       OPT_CALLER_DISCONNECT = 'H',
03034       OPT_CALLEE_PARKCALL   = 'k',
03035       OPT_CALLER_PARKCALL   = 'K',
03036    };
03037 
03038    memset(options, 0, len);
03039    if (ast_test_flag(features_caller, AST_FEATURE_REDIRECT) && i < len) {
03040       options[i++] = OPT_CALLER_REDIRECT;
03041    }
03042    if (ast_test_flag(features_caller, AST_FEATURE_AUTOMON) && i < len) {
03043       options[i++] = OPT_CALLER_AUTOMON;
03044    }
03045    if (ast_test_flag(features_caller, AST_FEATURE_DISCONNECT) && i < len) {
03046       options[i++] = OPT_CALLER_DISCONNECT;
03047    }
03048    if (ast_test_flag(features_caller, AST_FEATURE_PARKCALL) && i < len) {
03049       options[i++] = OPT_CALLER_PARKCALL;
03050    }
03051 
03052    if (ast_test_flag(features_callee, AST_FEATURE_REDIRECT) && i < len) {
03053       options[i++] = OPT_CALLEE_REDIRECT;
03054    }
03055    if (ast_test_flag(features_callee, AST_FEATURE_AUTOMON) && i < len) {
03056       options[i++] = OPT_CALLEE_AUTOMON;
03057    }
03058    if (ast_test_flag(features_callee, AST_FEATURE_DISCONNECT) && i < len) {
03059       options[i++] = OPT_CALLEE_DISCONNECT;
03060    }
03061    if (ast_test_flag(features_callee, AST_FEATURE_PARKCALL) && i < len) {
03062       options[i++] = OPT_CALLEE_PARKCALL;
03063    }
03064 
03065    return options;
03066 }
03067 
03068 /*! \brief Run management on parkinglots, called once per parkinglot */
03069 int manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, const int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
03070 {
03071    struct parkeduser *pu;
03072    int res = 0;
03073    char parkingslot[AST_MAX_EXTENSION];
03074 
03075    /* Lock parking list */
03076    AST_LIST_LOCK(&curlot->parkings);
03077    AST_LIST_TRAVERSE_SAFE_BEGIN(&curlot->parkings, pu, list) {
03078       struct ast_channel *chan = pu->chan;   /* shorthand */
03079       int tms;        /* timeout for this item */
03080       int x;          /* fd index in channel */
03081       struct ast_context *con;
03082 
03083       if (pu->notquiteyet) { /* Pretend this one isn't here yet */
03084          continue;
03085       }
03086       tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
03087       if (tms > pu->parkingtime) {
03088          /* Stop music on hold */
03089          ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
03090          /* Get chan, exten from derived kludge */
03091          if (pu->peername[0]) {
03092             char *peername = ast_strdupa(pu->peername);
03093             char *dash = strrchr(peername, '-');
03094             char peername_flat[AST_MAX_EXTENSION]; /* using something like DAHDI/52 for an extension name is NOT a good idea */
03095             int i;
03096 
03097             if (dash) {
03098                *dash = '\0';
03099             }
03100             ast_copy_string(peername_flat, peername, sizeof(peername_flat));
03101             for (i = 0; peername_flat[i] && i < AST_MAX_EXTENSION; i++) {
03102                if (peername_flat[i] == '/') {
03103                   peername_flat[i] = '0';
03104                }
03105             }
03106             con = ast_context_find_or_create(NULL, NULL, pu->parkinglot->parking_con_dial, registrar);
03107             if (!con) {
03108                ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", pu->parkinglot->parking_con_dial);
03109             }
03110             if (con) {
03111                char returnexten[AST_MAX_EXTENSION];
03112                struct ast_datastore *features_datastore;
03113                struct ast_dial_features *dialfeatures = NULL;
03114 
03115                ast_channel_lock(chan);
03116 
03117                if ((features_datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL)))
03118                   dialfeatures = features_datastore->data;
03119 
03120                ast_channel_unlock(chan);
03121 
03122                if (!strncmp(peername, "Parked/", 7)) {
03123                   peername += 7;
03124                }
03125 
03126                if (dialfeatures) {
03127                   char buf[MAX_DIAL_FEATURE_OPTIONS] = {0,};
03128                   snprintf(returnexten, sizeof(returnexten), "%s,30,%s", peername, callback_dialoptions(&(dialfeatures->features_callee), &(dialfeatures->features_caller), buf, sizeof(buf)));
03129                } else { /* Existing default */
03130                   ast_log(LOG_WARNING, "Dialfeatures not found on %s, using default!\n", chan->name);
03131                   snprintf(returnexten, sizeof(returnexten), "%s,30,t", peername);
03132                }
03133 
03134                ast_add_extension2(con, 1, peername_flat, 1, NULL, NULL, "Dial", ast_strdup(returnexten), ast_free_ptr, registrar);
03135             }
03136             if (pu->options_specified == 1) {
03137                /* Park() was called with overriding return arguments, respect those arguments */
03138                set_c_e_p(chan, pu->context, pu->exten, pu->priority);
03139             } else {
03140                if (comebacktoorigin) {
03141                   set_c_e_p(chan, pu->parkinglot->parking_con_dial, peername_flat, 1);
03142                } else {
03143                   ast_log(LOG_WARNING, "now going to parkedcallstimeout,s,1 | ps is %d\n",pu->parkingnum);
03144                   snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
03145                   pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parkingslot);
03146                   set_c_e_p(chan, "parkedcallstimeout", peername_flat, 1);
03147                }
03148             }
03149          } else {
03150             /* They've been waiting too long, send them back to where they came.  Theoretically they
03151                should have their original extensions and such, but we copy to be on the safe side */
03152             set_c_e_p(chan, pu->context, pu->exten, pu->priority);
03153          }
03154          post_manager_event("ParkedCallTimeOut", pu);
03155 
03156          ast_verb(2, "Timeout for %s parked on %d (%s). Returning to %s,%s,%d\n", pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->chan->context, pu->chan->exten, pu->chan->priority);
03157          /* Start up the PBX, or hang them up */
03158          if (ast_pbx_start(chan))  {
03159             ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
03160             ast_hangup(chan);
03161          }
03162          /* And take them out of the parking lot */
03163          con = ast_context_find(pu->parkinglot->parking_con);
03164          if (con) {
03165             if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0))
03166                ast_log(LOG_WARNING, "Whoa, failed to remove the parking extension!\n");
03167             else
03168                notify_metermaids(pu->parkingexten, curlot->parking_con, AST_DEVICE_NOT_INUSE);
03169          } else
03170             ast_log(LOG_WARNING, "Whoa, no parking context?\n");
03171          AST_LIST_REMOVE_CURRENT(list);
03172          free(pu);
03173       } else { /* still within parking time, process descriptors */
03174          for (x = 0; x < AST_MAX_FDS; x++) {
03175             struct ast_frame *f;
03176             int y;
03177 
03178             if (chan->fds[x] == -1) {
03179                continue;   /* nothing on this descriptor */
03180             }
03181 
03182             for (y = 0; y < nfds; y++) {
03183                if (pfds[y].fd == chan->fds[x]) {
03184                   /* Found poll record! */
03185                   break;
03186                }
03187             }
03188             if (y == nfds) {
03189                /* Not found */
03190                continue;
03191             }
03192 
03193             if (!(pfds[y].revents & (POLLIN | POLLERR))) {
03194                /* Next x */
03195                continue;
03196             }
03197 
03198             if (pfds[y].revents & POLLERR) {
03199                ast_set_flag(chan, AST_FLAG_EXCEPTION);
03200             } else {
03201                ast_clear_flag(chan, AST_FLAG_EXCEPTION);
03202             }
03203             chan->fdno = x;
03204 
03205             /* See if they need servicing */
03206             f = ast_read(pu->chan);
03207             /* Hangup? */
03208             if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass ==  AST_CONTROL_HANGUP))) {
03209                if (f)
03210                   ast_frfree(f);
03211                post_manager_event("ParkedCallGiveUp", pu);
03212 
03213                /* There's a problem, hang them up*/
03214                ast_verb(2, "%s got tired of being parked\n", chan->name);
03215                ast_hangup(chan);
03216                /* And take them out of the parking lot */
03217                con = ast_context_find(curlot->parking_con);
03218                if (con) {
03219                   if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0))
03220                      ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
03221                   else
03222                      notify_metermaids(pu->parkingexten, curlot->parking_con, AST_DEVICE_NOT_INUSE);
03223                } else
03224                   ast_log(LOG_WARNING, "Whoa, no parking context for parking lot %s?\n", curlot->name);
03225                AST_LIST_REMOVE_CURRENT(list);
03226                free(pu);
03227                break;
03228             } else {
03229                /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
03230                ast_frfree(f);
03231                if (pu->moh_trys < 3 && !chan->generatordata) {
03232                   ast_debug(1, "MOH on parked call stopped by outside source.  Restarting on channel %s.\n", chan->name);
03233                   ast_indicate_data(chan, AST_CONTROL_HOLD, 
03234                      S_OR(curlot->mohclass, NULL),
03235                      (!ast_strlen_zero(curlot->mohclass) ? strlen(curlot->mohclass) + 1 : 0));
03236                   pu->moh_trys++;
03237                }
03238                goto std;   /* XXX Ick: jumping into an else statement??? XXX */
03239             }
03240          } /* End for */
03241          if (x >= AST_MAX_FDS) {
03242 std:        for (x = 0; x < AST_MAX_FDS; x++) { /* mark fds for next round */
03243                if (chan->fds[x] > -1) {
03244                   void *tmp = ast_realloc(*new_pfds, (*new_nfds + 1) * sizeof(struct pollfd));
03245                   if (!tmp) {
03246                      continue;
03247                   }
03248                   *new_pfds = tmp;
03249                   (*new_pfds)[*new_nfds].fd = chan->fds[x];
03250                   (*new_pfds)[*new_nfds].events = POLLIN | POLLERR;
03251                   (*new_pfds)[*new_nfds].revents = 0;
03252                   (*new_nfds)++;
03253                }
03254             }
03255             /* Keep track of our shortest wait */
03256             if (tms < *ms || *ms < 0) {
03257                *ms = tms;
03258             }
03259          }
03260       }
03261    }
03262    AST_LIST_TRAVERSE_SAFE_END;
03263    AST_LIST_UNLOCK(&curlot->parkings);
03264 
03265    return res;
03266 }
03267 
03268 /*! 
03269  * \brief Take care of parked calls and unpark them if needed 
03270  * \param ignore unused var.
03271  * 
03272  * Start inf loop, lock parking lot, check if any parked channels have gone above timeout
03273  * if so, remove channel from parking lot and return it to the extension that parked it.
03274  * Check if parked channel decided to hangup, wait until next FD via select().
03275 */
03276 static void *do_parking_thread(void *ignore)
03277 {
03278    struct pollfd *pfds = NULL, *new_pfds = NULL;
03279    int nfds = 0, new_nfds = 0;
03280 
03281    for (;;) {
03282       struct ao2_iterator iter;
03283       struct ast_parkinglot *curlot;
03284       int ms = -1;   /* poll2 timeout, uninitialized */
03285       iter = ao2_iterator_init(parkinglots, 0);
03286 
03287       while ((curlot = ao2_iterator_next(&iter))) {
03288          manage_parkinglot(curlot, pfds, nfds, &new_pfds, &new_nfds, &ms);
03289          ao2_ref(curlot, -1);
03290       }
03291       ao2_iterator_destroy(&iter);
03292 
03293       /* Recycle */
03294       ast_free(pfds);
03295       pfds = new_pfds;
03296       nfds = new_nfds;
03297       new_pfds = NULL;
03298       new_nfds = 0;
03299 
03300       /* Wait for something to happen */
03301       ast_poll(pfds, nfds, ms);
03302       pthread_testcancel();
03303    }
03304    /* If this WERE reached, we'd need to free(pfds) */
03305    return NULL;   /* Never reached */
03306 }
03307 
03308 /*! \brief Find parkinglot by name */
03309 struct ast_parkinglot *find_parkinglot(const char *name)
03310 {
03311    struct ast_parkinglot *parkinglot = NULL;
03312    struct ast_parkinglot tmp_parkinglot;
03313    
03314    if (ast_strlen_zero(name))
03315       return NULL;
03316 
03317    ast_copy_string(tmp_parkinglot.name, name, sizeof(tmp_parkinglot.name));
03318 
03319    parkinglot = ao2_find(parkinglots, &tmp_parkinglot, OBJ_POINTER);
03320 
03321    if (parkinglot && option_debug)
03322       ast_log(LOG_DEBUG, "Found Parkinglot: %s\n", parkinglot->name);
03323 
03324    return parkinglot;
03325 }
03326 
03327 AST_APP_OPTIONS(park_call_options, BEGIN_OPTIONS
03328    AST_APP_OPTION('r', AST_PARK_OPT_RINGING),
03329    AST_APP_OPTION('R', AST_PARK_OPT_RANDOMIZE),
03330    AST_APP_OPTION('s', AST_PARK_OPT_SILENCE),
03331 END_OPTIONS );
03332 
03333 /*! \brief Park a call */
03334 static int park_call_exec(struct ast_channel *chan, void *data)
03335 {
03336    /* Cache the original channel name in case we get masqueraded in the middle
03337     * of a park--it is still theoretically possible for a transfer to happen before
03338     * we get here, but it is _really_ unlikely */
03339    char *orig_chan_name = ast_strdupa(chan->name);
03340    char orig_exten[AST_MAX_EXTENSION];
03341    int orig_priority = chan->priority;
03342 
03343    /* Data is unused at the moment but could contain a parking
03344       lot context eventually */
03345    int res = 0;
03346 
03347    char *parse = NULL;
03348    AST_DECLARE_APP_ARGS(app_args,
03349       AST_APP_ARG(timeout);
03350       AST_APP_ARG(return_con);
03351       AST_APP_ARG(return_ext);
03352       AST_APP_ARG(return_pri);
03353       AST_APP_ARG(options);
03354    );
03355 
03356    parse = ast_strdupa(data);
03357    AST_STANDARD_APP_ARGS(app_args, parse);
03358 
03359    ast_copy_string(orig_exten, chan->exten, sizeof(orig_exten));
03360 
03361    /* Setup the exten/priority to be s/1 since we don't know
03362       where this call should return */
03363    strcpy(chan->exten, "s");
03364    chan->priority = 1;
03365 
03366    /* Answer if call is not up */
03367    if (chan->_state != AST_STATE_UP)
03368       res = ast_answer(chan);
03369 
03370    /* Sleep to allow VoIP streams to settle down */
03371    if (!res)
03372       res = ast_safe_sleep(chan, 1000);
03373 
03374    /* Park the call */
03375    if (!res) {
03376       struct ast_park_call_args args = {
03377          .orig_chan_name = orig_chan_name,
03378       };
03379       struct ast_flags flags = { 0 };
03380 
03381       if (parse) {
03382          if (!ast_strlen_zero(app_args.timeout)) {
03383             if (sscanf(app_args.timeout, "%30d", &args.timeout) != 1) {
03384                ast_log(LOG_WARNING, "Invalid timeout '%s' provided\n", app_args.timeout);
03385                args.timeout = 0;
03386             }
03387          }
03388          if (!ast_strlen_zero(app_args.return_con)) {
03389             args.return_con = app_args.return_con;
03390          }
03391          if (!ast_strlen_zero(app_args.return_ext)) {
03392             args.return_ext = app_args.return_ext;
03393          }
03394          if (!ast_strlen_zero(app_args.return_pri)) {
03395             if (sscanf(app_args.return_pri, "%30d", &args.return_pri) != 1) {
03396                ast_log(LOG_WARNING, "Invalid priority '%s' specified\n", app_args.return_pri);
03397                args.return_pri = 0;
03398             }
03399          }
03400       }
03401 
03402       ast_app_parse_options(park_call_options, &flags, NULL, app_args.options);
03403       args.flags = flags.flags;
03404 
03405       res = masq_park_call_announce_args(chan, chan, &args);
03406       /* Continue on in the dialplan */
03407       if (res == 1) {
03408          ast_copy_string(chan->exten, orig_exten, sizeof(chan->exten));
03409          chan->priority = orig_priority;
03410          res = 0;
03411       } else if (!res) {
03412          res = 1;
03413       }
03414    }
03415 
03416    return res;
03417 }
03418 
03419 /*! \brief Pickup parked call */
03420 static int park_exec_full(struct ast_channel *chan, void *data, struct ast_parkinglot *parkinglot)
03421 {
03422    int res = 0;
03423    struct ast_channel *peer=NULL;
03424    struct parkeduser *pu;
03425    struct ast_context *con;
03426    int park = 0;
03427    struct ast_bridge_config config;
03428 
03429    if (data)
03430       park = atoi((char *)data);
03431 
03432    parkinglot = find_parkinglot(findparkinglotname(chan));  
03433    if (!parkinglot)
03434       parkinglot = default_parkinglot;
03435 
03436    AST_LIST_LOCK(&parkinglot->parkings);
03437    AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot->parkings, pu, list) {
03438       if (!pu->notquiteyet && (!data || pu->parkingnum == park)) {
03439          if (pu->chan->pbx) { /* do not allow call to be picked up until the PBX thread is finished */
03440             AST_LIST_UNLOCK(&parkinglot->parkings);
03441             return -1;
03442          }
03443          AST_LIST_REMOVE_CURRENT(list);
03444          break;
03445       }
03446    }
03447    AST_LIST_TRAVERSE_SAFE_END;
03448    AST_LIST_UNLOCK(&parkinglot->parkings);
03449 
03450    if (pu) {
03451       peer = pu->chan;
03452       con = ast_context_find(parkinglot->parking_con);
03453       if (con) {
03454          if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0))
03455             ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
03456          else
03457             notify_metermaids(pu->parkingexten, parkinglot->parking_con, AST_DEVICE_NOT_INUSE);
03458       } else
03459          ast_log(LOG_WARNING, "Whoa, no parking context?\n");
03460 
03461       manager_event(EVENT_FLAG_CALL, "UnParkedCall",
03462          "Exten: %s\r\n"
03463          "Channel: %s\r\n"
03464          "From: %s\r\n"
03465          "CallerIDNum: %s\r\n"
03466          "CallerIDName: %s\r\n",
03467          pu->parkingexten, pu->chan->name, chan->name,
03468          S_OR(pu->chan->cid.cid_num, "<unknown>"),
03469          S_OR(pu->chan->cid.cid_name, "<unknown>")
03470          );
03471 
03472       ast_free(pu);
03473    }
03474    /* JK02: it helps to answer the channel if not already up */
03475    if (chan->_state != AST_STATE_UP)
03476       ast_answer(chan);
03477 
03478    //XXX Why do we unlock here ?
03479    // uncomment it for now, till my setup with debug_threads and detect_deadlocks starts to complain
03480    //ASTOBJ_UNLOCK(parkinglot);
03481 
03482    if (peer) {
03483       struct ast_datastore *features_datastore;
03484       struct ast_dial_features *dialfeatures = NULL;
03485 
03486       /* Play a courtesy to the source(s) configured to prefix the bridge connecting */
03487 
03488       if (!ast_strlen_zero(courtesytone)) {
03489          int error = 0;
03490          ast_indicate(peer, AST_CONTROL_UNHOLD);
03491          if (parkedplay == 0) {
03492             error = ast_stream_and_wait(chan, courtesytone, "");
03493          } else if (parkedplay == 1) {
03494             error = ast_stream_and_wait(peer, courtesytone, "");
03495          } else if (parkedplay == 2) {
03496             if (!ast_streamfile(chan, courtesytone, chan->language) &&
03497                   !ast_streamfile(peer, courtesytone, chan->language)) {
03498                /*! \todo XXX we would like to wait on both! */
03499                res = ast_waitstream(chan, "");
03500                if (res >= 0)
03501                   res = ast_waitstream(peer, "");
03502                if (res < 0)
03503                   error = 1;
03504             }
03505          }
03506          if (error) {
03507             ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
03508             ast_hangup(peer);
03509             return -1;
03510          }
03511       } else
03512          ast_indicate(peer, AST_CONTROL_UNHOLD);
03513 
03514       res = ast_channel_make_compatible(chan, peer);
03515       if (res < 0) {
03516          ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
03517          ast_hangup(peer);
03518          return -1;
03519       }
03520       /* This runs sorta backwards, since we give the incoming channel control, as if it
03521          were the person called. */
03522       ast_verb(3, "Channel %s connected to parked call %d\n", chan->name, park);
03523 
03524       pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
03525       ast_cdr_setdestchan(chan->cdr, peer->name);
03526       memset(&config, 0, sizeof(struct ast_bridge_config));
03527 
03528       /* Get datastore for peer and apply it's features to the callee side of the bridge config */
03529       ast_channel_lock(peer);
03530       if ((features_datastore = ast_channel_datastore_find(peer, &dial_features_info, NULL))) {
03531          dialfeatures = features_datastore->data;
03532       }
03533       ast_channel_unlock(peer);
03534 
03535       /* When the datastores for both caller and callee are created, both the callee and caller channels
03536        * use the features_caller flag variable to represent themselves. With that said, the config.features_callee
03537        * flags should be copied from the datastore's caller feature flags regardless if peer was a callee
03538        * or caller. */
03539       if (dialfeatures) {
03540          ast_copy_flags(&(config.features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
03541       }
03542 
03543       if ((parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
03544          ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
03545       }
03546       if ((parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
03547          ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
03548       }
03549       if ((parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
03550          ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
03551       }
03552       if ((parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
03553          ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
03554       }
03555       if ((parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
03556          ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
03557       }
03558       if ((parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
03559          ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
03560       }
03561       if ((parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
03562          ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
03563       }
03564       if ((parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
03565          ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
03566       }
03567 
03568       res = ast_bridge_call(chan, peer, &config);
03569 
03570       pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
03571       ast_cdr_setdestchan(chan->cdr, peer->name);
03572 
03573       /* Simulate the PBX hanging up */
03574       ast_hangup(peer);
03575       return -1;
03576    } else {
03577       /*! \todo XXX Play a message XXX */
03578       if (ast_stream_and_wait(chan, "pbx-invalidpark", ""))
03579          ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
03580       ast_verb(3, "Channel %s tried to talk to nonexistent parked call %d\n", chan->name, park);
03581       res = -1;
03582    }
03583 
03584    return -1;
03585 }
03586 
03587 static int park_exec(struct ast_channel *chan, void *data) 
03588 {
03589    return park_exec_full(chan, data, default_parkinglot);
03590 }
03591 
03592 /*! \brief Unreference parkinglot object. If no more references,
03593    then go ahead and delete it */
03594 static void parkinglot_unref(struct ast_parkinglot *parkinglot) 
03595 {
03596    int refcount = ao2_ref(parkinglot, -1);
03597    if (option_debug > 2)
03598       ast_log(LOG_DEBUG, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount - 1);
03599 }
03600 
03601 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot)
03602 {
03603    int refcount = ao2_ref(parkinglot, +1);
03604    if (option_debug > 2)
03605       ast_log(LOG_DEBUG, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount + 1);
03606    return parkinglot;
03607 }
03608 
03609 /*! \brief Allocate parking lot structure */
03610 static struct ast_parkinglot *create_parkinglot(char *name)
03611 {
03612    struct ast_parkinglot *newlot = (struct ast_parkinglot *) NULL;
03613 
03614    if (!name)
03615       return NULL;
03616 
03617    newlot = ao2_alloc(sizeof(*newlot), parkinglot_destroy);
03618    if (!newlot)
03619       return NULL;
03620    
03621    ast_copy_string(newlot->name, name, sizeof(newlot->name));
03622    AST_LIST_HEAD_INIT(&newlot->parkings);
03623 
03624    return newlot;
03625 }
03626 
03627 /*! \brief Destroy a parking lot */
03628 static void parkinglot_destroy(void *obj)
03629 {
03630    struct ast_parkinglot *ruin = obj;
03631    struct ast_context *con;
03632    con = ast_context_find(ruin->parking_con);
03633    if (con)
03634       ast_context_destroy(con, registrar);
03635    ao2_unlink(parkinglots, ruin);
03636 }
03637 
03638 /*! \brief Build parkinglot from configuration and chain it in */
03639 static struct ast_parkinglot *build_parkinglot(char *name, struct ast_variable *var)
03640 {
03641    struct ast_parkinglot *parkinglot;
03642    struct ast_context *con = NULL;
03643 
03644    struct ast_variable *confvar = var;
03645    int error = 0;
03646    int start = 0, end = 0;
03647    int oldparkinglot = 0;
03648 
03649    parkinglot = find_parkinglot(name);
03650    if (parkinglot)
03651       oldparkinglot = 1;
03652    else
03653       parkinglot = create_parkinglot(name);
03654 
03655    if (!parkinglot)
03656       return NULL;
03657 
03658    ao2_lock(parkinglot);
03659 
03660    if (option_debug)
03661       ast_log(LOG_DEBUG, "Building parking lot %s\n", name);
03662    
03663    /* Do some config stuff */
03664    while(confvar) {
03665       if (!strcasecmp(confvar->name, "context")) {
03666          ast_copy_string(parkinglot->parking_con, confvar->value, sizeof(parkinglot->parking_con));
03667       } else if (!strcasecmp(confvar->name, "parkingtime")) {
03668          if ((sscanf(confvar->value, "%30d", &parkinglot->parkingtime) != 1) || (parkinglot->parkingtime < 1)) {
03669             ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", confvar->value);
03670             parkinglot->parkingtime = DEFAULT_PARK_TIME;
03671          } else
03672             parkinglot->parkingtime = parkinglot->parkingtime * 1000;
03673       } else if (!strcasecmp(confvar->name, "parkpos")) {
03674          if (sscanf(confvar->value, "%30d-%30d", &start, &end) != 2) {
03675             ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of parking.conf\n", confvar->lineno);
03676             error = 1;
03677          } else {
03678             parkinglot->parking_start = start;
03679             parkinglot->parking_stop = end;
03680          }
03681       } else if (!strcasecmp(confvar->name, "findslot")) {
03682          parkinglot->parkfindnext = (!strcasecmp(confvar->value, "next"));
03683       } else if (!strcasecmp(confvar->name, "parkedcalltransfers") ||
03684             !strcasecmp(confvar->name, "parkedcallreparking") ||
03685             !strcasecmp(confvar->name, "parkedcallhangup") ||
03686             !strcasecmp(confvar->name, "parkedcallrecording")) {
03687          if (!strcasecmp(confvar->value, "both")) {
03688             parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYBOTH;
03689          } else if (!strcasecmp(confvar->value, "caller")) {
03690             parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLER;
03691          } else if (!strcasecmp(confvar->value, "callee")) {
03692             parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLEE;
03693          }
03694       }
03695       confvar = confvar->next;
03696    }
03697    /* make sure parkingtime is set if not specified */
03698    if (parkinglot->parkingtime == 0) {
03699       parkinglot->parkingtime = DEFAULT_PARK_TIME;
03700    }
03701 
03702    if (!var) { /* Default parking lot */
03703       ast_copy_string(parkinglot->parking_con, "parkedcalls", sizeof(parkinglot->parking_con));
03704       ast_copy_string(parkinglot->parking_con_dial, "park-dial", sizeof(parkinglot->parking_con_dial));
03705       ast_copy_string(parkinglot->mohclass, "default", sizeof(parkinglot->mohclass));
03706    }
03707 
03708    /* Check for errors */
03709    if (ast_strlen_zero(parkinglot->parking_con)) {
03710       ast_log(LOG_WARNING, "Parking lot %s lacks context\n", name);
03711       error = 1;
03712    }
03713 
03714    /* Create context */
03715    if (!error && !(con = ast_context_find_or_create(NULL, NULL, parkinglot->parking_con, registrar))) {
03716       ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parkinglot->parking_con);
03717       error = 1;
03718    }
03719 
03720    /* Add a parking extension into the context */
03721    if (!error && !oldparkinglot) {
03722       if (!ast_strlen_zero(ast_parking_ext())) {
03723          if (ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, strdup(""), ast_free_ptr, registrar) == -1)
03724             error = 1;
03725       }
03726    }
03727 
03728    ao2_unlock(parkinglot);
03729 
03730    if (error) {
03731       ast_log(LOG_WARNING, "Parking %s not open for business. Configuration error.\n", name);
03732       parkinglot_destroy(parkinglot);
03733       return NULL;
03734    }
03735    if (option_debug)
03736       ast_log(LOG_DEBUG, "Parking %s now open for business. (start exten %d end %d)\n", name, start, end);
03737 
03738 
03739    /* Move it into the list, if it wasn't already there */
03740    if (!oldparkinglot) {
03741       ao2_link(parkinglots, parkinglot);
03742    }
03743    parkinglot_unref(parkinglot);
03744 
03745    return parkinglot;
03746 }
03747 
03748 
03749 /*! 
03750  * \brief Add parking hints for all defined parking lots 
03751  * \param context
03752  * \param start starting parkinglot number
03753  * \param stop ending parkinglot number
03754 */
03755 static void park_add_hints(char *context, int start, int stop)
03756 {
03757    int numext;
03758    char device[AST_MAX_EXTENSION];
03759    char exten[10];
03760 
03761    for (numext = start; numext <= stop; numext++) {
03762       snprintf(exten, sizeof(exten), "%d", numext);
03763       snprintf(device, sizeof(device), "park:%s@%s", exten, context);
03764       ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
03765    }
03766 }
03767 
03768 static int load_config(void) 
03769 {
03770    int start = 0, end = 0;
03771    int res;
03772    int i;
03773    struct ast_context *con = NULL;
03774    struct ast_config *cfg = NULL;
03775    struct ast_variable *var = NULL;
03776    struct feature_group *fg = NULL;
03777    struct ast_flags config_flags = { 0 };
03778    char old_parking_ext[AST_MAX_EXTENSION];
03779    char old_parking_con[AST_MAX_EXTENSION] = "";
03780    char *ctg; 
03781    static const char *categories[] = { 
03782       /* Categories in features.conf that are not
03783        * to be parsed as group categories
03784        */
03785       "general",
03786       "featuremap",
03787       "applicationmap"
03788    };
03789 
03790    if (default_parkinglot) {
03791       strcpy(old_parking_con, default_parkinglot->parking_con);
03792       strcpy(old_parking_ext, parking_ext);
03793    } else {
03794       default_parkinglot = build_parkinglot(DEFAULT_PARKINGLOT, NULL);
03795       if (default_parkinglot) {
03796          ao2_lock(default_parkinglot);
03797          default_parkinglot->parking_start = 701;
03798          default_parkinglot->parking_stop = 750;
03799          default_parkinglot->parking_offset = 0;
03800          default_parkinglot->parkfindnext = 0;
03801          default_parkinglot->parkingtime = DEFAULT_PARK_TIME;
03802          ao2_unlock(default_parkinglot);
03803       }
03804    }
03805    if (default_parkinglot) {
03806       if (option_debug)
03807          ast_log(LOG_DEBUG, "Configuration of default parkinglot done.\n");
03808    } else {
03809       ast_log(LOG_ERROR, "Configuration of default parkinglot failed.\n");
03810       return -1;
03811    }
03812    
03813 
03814    /* Reset to defaults */
03815    strcpy(parking_ext, "700");
03816    strcpy(pickup_ext, "*8");
03817    courtesytone[0] = '\0';
03818    strcpy(xfersound, "beep");
03819    strcpy(xferfailsound, "pbx-invalid");
03820    pickupsound[0] = '\0';
03821    pickupfailsound[0] = '\0';
03822    adsipark = 0;
03823    comebacktoorigin = 1;
03824 
03825    default_parkinglot->parkaddhints = 0;
03826    default_parkinglot->parkedcalltransfers = 0;
03827    default_parkinglot->parkedcallreparking = 0;
03828    default_parkinglot->parkedcallrecording = 0;
03829    default_parkinglot->parkedcallhangup = 0;
03830 
03831    transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
03832    featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
03833    atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
03834    atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
03835    atxferdropcall = DEFAULT_ATXFER_DROP_CALL;
03836    atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
03837 
03838    cfg = ast_config_load2("features.conf", "features", config_flags);
03839    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
03840       ast_log(LOG_WARNING,"Could not load features.conf\n");
03841       return 0;
03842    }
03843    for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
03844       if (!strcasecmp(var->name, "parkext")) {
03845          ast_copy_string(parking_ext, var->value, sizeof(parking_ext));
03846       } else if (!strcasecmp(var->name, "context")) {
03847          ast_copy_string(default_parkinglot->parking_con, var->value, sizeof(default_parkinglot->parking_con));
03848       } else if (!strcasecmp(var->name, "parkingtime")) {
03849          if ((sscanf(var->value, "%30d", &default_parkinglot->parkingtime) != 1) || (default_parkinglot->parkingtime < 1)) {
03850             ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
03851             default_parkinglot->parkingtime = DEFAULT_PARK_TIME;
03852          } else
03853             default_parkinglot->parkingtime = default_parkinglot->parkingtime * 1000;
03854       } else if (!strcasecmp(var->name, "parkpos")) {
03855          if (sscanf(var->value, "%30d-%30d", &start, &end) != 2) {
03856             ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of features.conf\n", var->lineno);
03857          } else if (default_parkinglot) {
03858             default_parkinglot->parking_start = start;
03859             default_parkinglot->parking_stop = end;
03860          } else {
03861             ast_log(LOG_WARNING, "No default parking lot!\n");
03862          }
03863       } else if (!strcasecmp(var->name, "findslot")) {
03864          default_parkinglot->parkfindnext = (!strcasecmp(var->value, "next"));
03865       } else if (!strcasecmp(var->name, "parkinghints")) {
03866          default_parkinglot->parkaddhints = ast_true(var->value);
03867       } else if (!strcasecmp(var->name, "parkedcalltransfers")) {
03868          if (!strcasecmp(var->value, "both"))
03869             default_parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYBOTH;
03870          else if (!strcasecmp(var->value, "caller"))
03871             default_parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLER;
03872          else if (!strcasecmp(var->value, "callee"))
03873             default_parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLEE;
03874       } else if (!strcasecmp(var->name, "parkedcallreparking")) {
03875          if (!strcasecmp(var->value, "both"))
03876             default_parkinglot->parkedcallreparking = AST_FEATURE_FLAG_BYBOTH;
03877          else if (!strcasecmp(var->value, "caller"))
03878             default_parkinglot->parkedcallreparking = AST_FEATURE_FLAG_BYCALLER;
03879          else if (!strcasecmp(var->value, "callee"))
03880             default_parkinglot->parkedcallreparking = AST_FEATURE_FLAG_BYCALLEE;
03881       } else if (!strcasecmp(var->name, "parkedcallhangup")) {
03882          if (!strcasecmp(var->value, "both"))
03883             default_parkinglot->parkedcallhangup = AST_FEATURE_FLAG_BYBOTH;
03884          else if (!strcasecmp(var->value, "caller"))
03885             default_parkinglot->parkedcallhangup = AST_FEATURE_FLAG_BYCALLER;
03886          else if (!strcasecmp(var->value, "callee"))
03887             default_parkinglot->parkedcallhangup = AST_FEATURE_FLAG_BYCALLEE;
03888       } else if (!strcasecmp(var->name, "parkedcallrecording")) {
03889          if (!strcasecmp(var->value, "both"))
03890             default_parkinglot->parkedcallrecording = AST_FEATURE_FLAG_BYBOTH;
03891          else if (!strcasecmp(var->value, "caller"))
03892             default_parkinglot->parkedcallrecording = AST_FEATURE_FLAG_BYCALLER;
03893          else if (!strcasecmp(var->value, "callee"))
03894             default_parkinglot->parkedcallrecording = AST_FEATURE_FLAG_BYCALLEE;
03895       } else if (!strcasecmp(var->name, "adsipark")) {
03896          adsipark = ast_true(var->value);
03897       } else if (!strcasecmp(var->name, "transferdigittimeout")) {
03898          if ((sscanf(var->value, "%30d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
03899             ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
03900             transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
03901          } else
03902             transferdigittimeout = transferdigittimeout * 1000;
03903       } else if (!strcasecmp(var->name, "featuredigittimeout")) {
03904          if ((sscanf(var->value, "%30d", &featuredigittimeout) != 1) || (featuredigittimeout < 1)) {
03905             ast_log(LOG_WARNING, "%s is not a valid featuredigittimeout\n", var->value);
03906             featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
03907          }
03908       } else if (!strcasecmp(var->name, "atxfernoanswertimeout")) {
03909          if ((sscanf(var->value, "%30d", &atxfernoanswertimeout) != 1) || (atxfernoanswertimeout < 1)) {
03910             ast_log(LOG_WARNING, "%s is not a valid atxfernoanswertimeout\n", var->value);
03911             atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
03912          } else
03913             atxfernoanswertimeout = atxfernoanswertimeout * 1000;
03914       } else if (!strcasecmp(var->name, "atxferloopdelay")) {
03915          if ((sscanf(var->value, "%30u", &atxferloopdelay) != 1)) {
03916             ast_log(LOG_WARNING, "%s is not a valid atxferloopdelay\n", var->value);
03917             atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
03918          } else 
03919             atxferloopdelay *= 1000;
03920       } else if (!strcasecmp(var->name, "atxferdropcall")) {
03921          atxferdropcall = ast_true(var->value);
03922       } else if (!strcasecmp(var->name, "atxfercallbackretries")) {
03923          if ((sscanf(var->value, "%30u", &atxferloopdelay) != 1)) {
03924             ast_log(LOG_WARNING, "%s is not a valid atxfercallbackretries\n", var->value);
03925             atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
03926          }
03927       } else if (!strcasecmp(var->name, "courtesytone")) {
03928          ast_copy_string(courtesytone, var->value, sizeof(courtesytone));
03929       }  else if (!strcasecmp(var->name, "parkedplay")) {
03930          if (!strcasecmp(var->value, "both"))
03931             parkedplay = 2;
03932          else if (!strcasecmp(var->value, "parked"))
03933             parkedplay = 1;
03934          else
03935             parkedplay = 0;
03936       } else if (!strcasecmp(var->name, "xfersound")) {
03937          ast_copy_string(xfersound, var->value, sizeof(xfersound));
03938       } else if (!strcasecmp(var->name, "xferfailsound")) {
03939          ast_copy_string(xferfailsound, var->value, sizeof(xferfailsound));
03940       } else if (!strcasecmp(var->name, "pickupexten")) {
03941          ast_copy_string(pickup_ext, var->value, sizeof(pickup_ext));
03942       } else if (!strcasecmp(var->name, "pickupsound")) {
03943          ast_copy_string(pickupsound, var->value, sizeof(pickupsound));
03944       } else if (!strcasecmp(var->name, "pickupfailsound")) {
03945          ast_copy_string(pickupfailsound, var->value, sizeof(pickupfailsound));
03946       } else if (!strcasecmp(var->name, "comebacktoorigin")) {
03947          comebacktoorigin = ast_true(var->value);
03948       } else if (!strcasecmp(var->name, "parkedmusicclass")) {
03949          ast_copy_string(default_parkinglot->mohclass, var->value, sizeof(default_parkinglot->mohclass));
03950       }
03951    }
03952 
03953    unmap_features();
03954    for (var = ast_variable_browse(cfg, "featuremap"); var; var = var->next) {
03955       if (remap_feature(var->name, var->value))
03956          ast_log(LOG_NOTICE, "Unknown feature '%s'\n", var->name);
03957    }
03958 
03959    /* Map a key combination to an application*/
03960    ast_unregister_features();
03961    for (var = ast_variable_browse(cfg, "applicationmap"); var; var = var->next) {
03962       char *tmp_val = ast_strdupa(var->value);
03963       char *activateon; 
03964       struct ast_call_feature *feature;
03965       AST_DECLARE_APP_ARGS(args,
03966          AST_APP_ARG(exten);
03967          AST_APP_ARG(activatedby);
03968          AST_APP_ARG(app);
03969          AST_APP_ARG(app_args);
03970          AST_APP_ARG(moh_class);
03971       );
03972 
03973       AST_STANDARD_APP_ARGS(args, tmp_val);
03974       if (strchr(args.app, '(')) {
03975          /* New syntax */
03976          args.moh_class = args.app_args;
03977          args.app_args = strchr(args.app, '(');
03978          *args.app_args++ = '\0';
03979          if (args.app_args[strlen(args.app_args) - 1] == ')') {
03980             args.app_args[strlen(args.app_args) - 1] = '\0';
03981          }
03982       }
03983 
03984       activateon = strsep(&args.activatedby, "/"); 
03985 
03986       /*! \todo XXX var_name or app_args ? */
03987       if (ast_strlen_zero(args.app) || ast_strlen_zero(args.exten) || ast_strlen_zero(activateon) || ast_strlen_zero(var->name)) {
03988          ast_log(LOG_NOTICE, "Please check the feature Mapping Syntax, either extension, name, or app aren't provided %s %s %s %s\n",
03989             args.app, args.exten, activateon, var->name);
03990          continue;
03991       }
03992 
03993       AST_RWLIST_RDLOCK(&feature_list);
03994       if ((feature = find_dynamic_feature(var->name))) {
03995          AST_RWLIST_UNLOCK(&feature_list);
03996          ast_log(LOG_WARNING, "Dynamic Feature '%s' specified more than once!\n", var->name);
03997          continue;
03998       }
03999       AST_RWLIST_UNLOCK(&feature_list);
04000             
04001       if (!(feature = ast_calloc(1, sizeof(*feature)))) {
04002          continue;
04003       }
04004 
04005       ast_copy_string(feature->sname, var->name, FEATURE_SNAME_LEN);
04006       ast_copy_string(feature->app, args.app, FEATURE_APP_LEN);
04007       ast_copy_string(feature->exten, args.exten, FEATURE_EXTEN_LEN);
04008       
04009       if (args.app_args) {
04010          ast_copy_string(feature->app_args, args.app_args, FEATURE_APP_ARGS_LEN);
04011       }
04012 
04013       if (args.moh_class) {
04014          ast_copy_string(feature->moh_class, args.moh_class, FEATURE_MOH_LEN);
04015       }
04016 
04017       ast_copy_string(feature->exten, args.exten, sizeof(feature->exten));
04018       feature->operation = feature_exec_app;
04019       ast_set_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF);
04020 
04021       /* Allow caller and calle to be specified for backwards compatability */
04022       if (!strcasecmp(activateon, "self") || !strcasecmp(activateon, "caller"))
04023          ast_set_flag(feature, AST_FEATURE_FLAG_ONSELF);
04024       else if (!strcasecmp(activateon, "peer") || !strcasecmp(activateon, "callee"))
04025          ast_set_flag(feature, AST_FEATURE_FLAG_ONPEER);
04026       else {
04027          ast_log(LOG_NOTICE, "Invalid 'ActivateOn' specification for feature '%s',"
04028             " must be 'self', or 'peer'\n", var->name);
04029          continue;
04030       }
04031 
04032       if (ast_strlen_zero(args.activatedby))
04033          ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
04034       else if (!strcasecmp(args.activatedby, "caller"))
04035          ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLER);
04036       else if (!strcasecmp(args.activatedby, "callee"))
04037          ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLEE);
04038       else if (!strcasecmp(args.activatedby, "both"))
04039          ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
04040       else {
04041          ast_log(LOG_NOTICE, "Invalid 'ActivatedBy' specification for feature '%s',"
04042             " must be 'caller', or 'callee', or 'both'\n", var->name);
04043          continue;
04044       }
04045 
04046       ast_register_feature(feature);
04047 
04048       ast_verb(2, "Mapping Feature '%s' to app '%s(%s)' with code '%s'\n", var->name, args.app, args.app_args, args.exten);
04049    }
04050 
04051    ast_unregister_groups();
04052    AST_RWLIST_WRLOCK(&feature_groups);
04053 
04054    ctg = NULL;
04055    while ((ctg = ast_category_browse(cfg, ctg))) {
04056       /* Is this a parkinglot definition ? */
04057       if (!strncasecmp(ctg, "parkinglot_", strlen("parkinglot_"))) {
04058          ast_debug(2, "Found configuration section %s, assume parking context\n", ctg);
04059          if(!build_parkinglot(ctg, ast_variable_browse(cfg, ctg)))
04060             ast_log(LOG_ERROR, "Could not build parking lot %s. Configuration error.\n", ctg);
04061          else
04062             ast_debug(1, "Configured parking context %s\n", ctg);
04063          continue;   
04064       }
04065       /* No, check if it's a group */
04066       for (i = 0; i < ARRAY_LEN(categories); i++) {
04067          if (!strcasecmp(categories[i], ctg))
04068             break;
04069       }
04070 
04071       if (i < ARRAY_LEN(categories)) 
04072          continue;
04073 
04074       if (!(fg = register_group(ctg)))
04075          continue;
04076 
04077       for (var = ast_variable_browse(cfg, ctg); var; var = var->next) {
04078          struct ast_call_feature *feature;
04079 
04080          AST_RWLIST_RDLOCK(&feature_list);
04081          if (!(feature = find_dynamic_feature(var->name)) && 
04082              !(feature = ast_find_call_feature(var->name))) {
04083             AST_RWLIST_UNLOCK(&feature_list);
04084             ast_log(LOG_WARNING, "Feature '%s' was not found.\n", var->name);
04085             continue;
04086          }
04087          AST_RWLIST_UNLOCK(&feature_list);
04088 
04089          register_group_feature(fg, var->value, feature);
04090       }
04091    }
04092 
04093    AST_RWLIST_UNLOCK(&feature_groups);
04094 
04095    ast_config_destroy(cfg);
04096 
04097    /* Remove the old parking extension */
04098    if (!ast_strlen_zero(old_parking_con) && (con = ast_context_find(old_parking_con))) {
04099       if(ast_context_remove_extension2(con, old_parking_ext, 1, registrar, 0))
04100             notify_metermaids(old_parking_ext, old_parking_con, AST_DEVICE_NOT_INUSE);
04101       ast_debug(1, "Removed old parking extension %s@%s\n", old_parking_ext, old_parking_con);
04102    }
04103    
04104    if (!(con = ast_context_find_or_create(NULL, NULL, default_parkinglot->parking_con, registrar))) {
04105       ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", default_parkinglot->parking_con);
04106       return -1;
04107    }
04108    res = ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, NULL, NULL, registrar);
04109    if (default_parkinglot->parkaddhints)
04110       park_add_hints(default_parkinglot->parking_con, default_parkinglot->parking_start, default_parkinglot->parking_stop);
04111    if (!res)
04112       notify_metermaids(ast_parking_ext(), default_parkinglot->parking_con, AST_DEVICE_INUSE);
04113    return res;
04114 
04115 }
04116 
04117 /*!
04118  * \brief CLI command to list configured features
04119  * \param e
04120  * \param cmd
04121  * \param a
04122  *
04123  * \retval CLI_SUCCESS on success.
04124  * \retval NULL when tab completion is used.
04125  */
04126 static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04127 {
04128    int i;
04129    struct ast_call_feature *feature;
04130    struct ao2_iterator iter;
04131    struct ast_parkinglot *curlot;
04132 #define HFS_FORMAT "%-25s %-7s %-7s\n"
04133 
04134    switch (cmd) {
04135    
04136    case CLI_INIT:
04137       e->command = "features show";
04138       e->usage =
04139          "Usage: features show\n"
04140          "       Lists configured features\n";
04141       return NULL;
04142    case CLI_GENERATE:
04143       return NULL;
04144    }
04145 
04146    ast_cli(a->fd, HFS_FORMAT, "Builtin Feature", "Default", "Current");
04147    ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
04148 
04149    ast_cli(a->fd, HFS_FORMAT, "Pickup", "*8", ast_pickup_ext());          /* default hardcoded above, so we'll hardcode it here */
04150 
04151    ast_rwlock_rdlock(&features_lock);
04152    for (i = 0; i < FEATURES_COUNT; i++)
04153       ast_cli(a->fd, HFS_FORMAT, builtin_features[i].fname, builtin_features[i].default_exten, builtin_features[i].exten);
04154    ast_rwlock_unlock(&features_lock);
04155 
04156    ast_cli(a->fd, "\n");
04157    ast_cli(a->fd, HFS_FORMAT, "Dynamic Feature", "Default", "Current");
04158    ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
04159    if (AST_RWLIST_EMPTY(&feature_list)) {
04160       ast_cli(a->fd, "(none)\n");
04161    } else {
04162       AST_RWLIST_RDLOCK(&feature_list);
04163       AST_RWLIST_TRAVERSE(&feature_list, feature, feature_entry) {
04164          ast_cli(a->fd, HFS_FORMAT, feature->sname, "no def", feature->exten);
04165       }
04166       AST_RWLIST_UNLOCK(&feature_list);
04167    }
04168 
04169    ast_cli(a->fd, "\nFeature Groups:\n");
04170    ast_cli(a->fd, "---------------\n");
04171    if (AST_RWLIST_EMPTY(&feature_groups)) {
04172       ast_cli(a->fd, "(none)\n");
04173    } else {
04174       struct feature_group *fg;
04175       struct feature_group_exten *fge;
04176 
04177       AST_RWLIST_RDLOCK(&feature_groups);
04178       AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
04179          ast_cli(a->fd, "===> Group: %s\n", fg->gname);
04180          AST_LIST_TRAVERSE(&fg->features, fge, entry) {
04181             ast_cli(a->fd, "===> --> %s (%s)\n", fge->feature->sname, fge->exten);
04182          }
04183       }
04184       AST_RWLIST_UNLOCK(&feature_groups);
04185    }
04186 
04187    iter = ao2_iterator_init(parkinglots, 0);
04188    while ((curlot = ao2_iterator_next(&iter))) {
04189       ast_cli(a->fd, "\nCall parking (Parking lot: %s)\n", curlot->name);
04190       ast_cli(a->fd, "------------\n");
04191       ast_cli(a->fd,"%-22s:      %s\n", "Parking extension", parking_ext);
04192       ast_cli(a->fd,"%-22s:      %s\n", "Parking context", curlot->parking_con);
04193       ast_cli(a->fd,"%-22s:      %d-%d\n", "Parked call extensions", curlot->parking_start, curlot->parking_stop);
04194       ast_cli(a->fd,"\n");
04195       ao2_ref(curlot, -1);
04196    }
04197    ao2_iterator_destroy(&iter);
04198 
04199    return CLI_SUCCESS;
04200 }
04201 
04202 int ast_features_reload(void)
04203 {
04204    int res;
04205    /* Release parking lot list */
04206    //ASTOBJ_CONTAINER_MARKALL(&parkinglots);
04207    // TODO: I don't think any marking is necessary
04208 
04209    /* Reload configuration */
04210    res = load_config();
04211    
04212    //ASTOBJ_CONTAINER_PRUNE_MARKED(&parkinglots, parkinglot_destroy);
04213    return res;
04214 }
04215 
04216 static char *handle_features_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04217 {
04218    switch (cmd) { 
04219    case CLI_INIT:
04220       e->command = "features reload";
04221       e->usage =
04222          "Usage: features reload\n"
04223          "       Reloads configured call features from features.conf\n";
04224       return NULL;
04225    case CLI_GENERATE:
04226       return NULL;
04227    }
04228    ast_features_reload();
04229 
04230    return CLI_SUCCESS;
04231 }
04232 
04233 static char mandescr_bridge[] =
04234 "Description: Bridge together two channels already in the PBX\n"
04235 "Variables: ( Headers marked with * are required )\n"
04236 "   *Channel1: Channel to Bridge to Channel2\n"
04237 "   *Channel2: Channel to Bridge to Channel1\n"
04238 "        Tone: (Yes|No) Play courtesy tone to Channel 2\n"
04239 "\n";
04240 
04241 /*!
04242  * \brief Actual bridge
04243  * \param chan
04244  * \param tmpchan
04245  * 
04246  * Stop hold music, lock both channels, masq channels,
04247  * after bridge return channel to next priority.
04248 */
04249 static void do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
04250 {
04251    ast_moh_stop(chan);
04252    ast_channel_lock(chan);
04253    ast_setstate(tmpchan, chan->_state);
04254    tmpchan->readformat = chan->readformat;
04255    tmpchan->writeformat = chan->writeformat;
04256    ast_channel_masquerade(tmpchan, chan);
04257    ast_channel_lock(tmpchan);
04258    ast_do_masquerade(tmpchan);
04259    /* when returning from bridge, the channel will continue at the next priority */
04260    ast_explicit_goto(tmpchan, chan->context, chan->exten, chan->priority + 1);
04261    ast_channel_unlock(tmpchan);
04262    ast_channel_unlock(chan);
04263 }
04264 
04265 /*!
04266  * \brief Bridge channels together
04267  * \param s
04268  * \param m
04269  * 
04270  * Make sure valid channels were specified, 
04271  * send errors if any of the channels could not be found/locked, answer channels if needed,
04272  * create the placeholder channels and grab the other channels 
04273  * make the channels compatible, send error if we fail doing so 
04274  * setup the bridge thread object and start the bridge.
04275  * 
04276  * \retval 0 on success or on incorrect use.
04277  * \retval 1 on failure to bridge channels.
04278 */
04279 static int action_bridge(struct mansession *s, const struct message *m)
04280 {
04281    const char *channela = astman_get_header(m, "Channel1");
04282    const char *channelb = astman_get_header(m, "Channel2");
04283    const char *playtone = astman_get_header(m, "Tone");
04284    struct ast_channel *chana = NULL, *chanb = NULL;
04285    struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
04286    struct ast_bridge_thread_obj *tobj = NULL;
04287 
04288    /* make sure valid channels were specified */
04289    if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
04290       astman_send_error(s, m, "Missing channel parameter in request");
04291       return 0;
04292    }
04293 
04294    /* The same code must be executed for chana and chanb.  To avoid a
04295     * theoretical deadlock, this code is separated so both chana and chanb will
04296     * not hold locks at the same time. */
04297 
04298    /* Start with chana */
04299    chana = ast_get_channel_by_name_prefix_locked(channela, strlen(channela));
04300 
04301    /* send errors if any of the channels could not be found/locked */
04302    if (!chana) {
04303       char buf[256];
04304       snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
04305       astman_send_error(s, m, buf);
04306       return 0;
04307    }
04308 
04309    /* Answer the channels if needed */
04310    if (chana->_state != AST_STATE_UP)
04311       ast_answer(chana);
04312 
04313    /* create the placeholder channels and grab the other channels */
04314    if (!(tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
04315       NULL, NULL, 0, "Bridge/%s", chana->name))) {
04316       astman_send_error(s, m, "Unable to create temporary channel!");
04317       ast_channel_unlock(chana);
04318       return 1;
04319    }
04320 
04321    do_bridge_masquerade(chana, tmpchana);
04322    ast_channel_unlock(chana);
04323    chana = NULL;
04324 
04325    /* now do chanb */
04326    chanb = ast_get_channel_by_name_prefix_locked(channelb, strlen(channelb));
04327    /* send errors if any of the channels could not be found/locked */
04328    if (!chanb) {
04329       char buf[256];
04330       snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
04331       ast_hangup(tmpchana);
04332       astman_send_error(s, m, buf);
04333       return 0;
04334    }
04335 
04336    /* Answer the channels if needed */
04337    if (chanb->_state != AST_STATE_UP)
04338       ast_answer(chanb);
04339 
04340    /* create the placeholder channels and grab the other channels */
04341    if (!(tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
04342       NULL, NULL, 0, "Bridge/%s", chanb->name))) {
04343       astman_send_error(s, m, "Unable to create temporary channels!");
04344       ast_hangup(tmpchana);
04345       ast_channel_unlock(chanb);
04346       return 1;
04347    }
04348    do_bridge_masquerade(chanb, tmpchanb);
04349    ast_channel_unlock(chanb);
04350    chanb = NULL;
04351 
04352    /* make the channels compatible, send error if we fail doing so */
04353    if (ast_channel_make_compatible(tmpchana, tmpchanb)) {
04354       ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", tmpchana->name, tmpchanb->name);
04355       astman_send_error(s, m, "Could not make channels compatible for manager bridge");
04356       ast_hangup(tmpchana);
04357       ast_hangup(tmpchanb);
04358       return 1;
04359    }
04360 
04361    /* setup the bridge thread object and start the bridge */
04362    if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
04363       ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno));
04364       astman_send_error(s, m, "Unable to spawn a new bridge thread");
04365       ast_hangup(tmpchana);
04366       ast_hangup(tmpchanb);
04367       return 1;
04368    }
04369 
04370    tobj->chan = tmpchana;
04371    tobj->peer = tmpchanb;
04372    tobj->return_to_pbx = 1;
04373 
04374    if (ast_true(playtone)) {
04375       if (!ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, tmpchanb->language)) {
04376          if (ast_waitstream(tmpchanb, "") < 0)
04377             ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", tmpchanb->name);
04378       }
04379    }
04380 
04381    bridge_call_thread_launch(tobj);
04382 
04383    astman_send_ack(s, m, "Launched bridge thread with success");
04384 
04385    return 0;
04386 }
04387 
04388 /*!
04389  * \brief CLI command to list parked calls
04390  * \param e 
04391  * \param cmd
04392  * \param a
04393  *  
04394  * Check right usage, lock parking lot, display parked calls, unlock parking lot list.
04395  * \retval CLI_SUCCESS on success.
04396  * \retval CLI_SHOWUSAGE on incorrect number of arguments.
04397  * \retval NULL when tab completion is used.
04398 */
04399 static char *handle_parkedcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04400 {
04401    struct parkeduser *cur;
04402    int numparked = 0;
04403    struct ao2_iterator iter;
04404    struct ast_parkinglot *curlot;
04405 
04406    switch (cmd) {
04407    case CLI_INIT:
04408       e->command = "parkedcalls show";
04409       e->usage =
04410          "Usage: parkedcalls show\n"
04411          "       List currently parked calls\n";
04412       return NULL;
04413    case CLI_GENERATE:
04414       return NULL;
04415    }
04416 
04417    if (a->argc > e->args)
04418       return CLI_SHOWUSAGE;
04419 
04420    ast_cli(a->fd, "%4s %25s (%-15s %-12s %-4s) %-6s \n", "Num", "Channel"
04421       , "Context", "Extension", "Pri", "Timeout");
04422 
04423    iter = ao2_iterator_init(parkinglots, 0);
04424    while ((curlot = ao2_iterator_next(&iter))) {
04425       int lotparked = 0;
04426       ast_cli(a->fd, "*** Parking lot: %s\n", curlot->name);
04427 
04428       AST_LIST_LOCK(&curlot->parkings);
04429       AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
04430          ast_cli(a->fd, "%-10.10s %25s (%-15s %-12s %-4d) %6lds\n"
04431             ,cur->parkingexten, cur->chan->name, cur->context, cur->exten
04432             ,cur->priority,
04433             (long)(cur->start.tv_sec + (cur->parkingtime/1000) - time(NULL)) );
04434          numparked++;
04435          numparked += lotparked;
04436       }
04437       AST_LIST_UNLOCK(&curlot->parkings);
04438       if (lotparked)
04439          ast_cli(a->fd, "   %d parked call%s in parking lot %s\n", lotparked, ESS(lotparked), curlot->name);
04440 
04441       ao2_ref(curlot, -1);
04442    }
04443 
04444    ast_cli(a->fd, "---\n%d parked call%s in total.\n", numparked, ESS(numparked));
04445 
04446    return CLI_SUCCESS;
04447 }
04448 
04449 static struct ast_cli_entry cli_features[] = {
04450    AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
04451    AST_CLI_DEFINE(handle_features_reload, "Reloads configured features"),
04452    AST_CLI_DEFINE(handle_parkedcalls, "List currently parked calls"),
04453 };
04454 
04455 /*! 
04456  * \brief Dump parking lot status
04457  * \param s
04458  * \param m
04459  * 
04460  * Lock parking lot, iterate list and append parked calls status, unlock parking lot.
04461  * \return Always RESULT_SUCCESS 
04462 */
04463 static int manager_parking_status(struct mansession *s, const struct message *m)
04464 {
04465    struct parkeduser *cur;
04466    const char *id = astman_get_header(m, "ActionID");
04467    char idText[256] = "";
04468    struct ao2_iterator iter;
04469    struct ast_parkinglot *curlot;
04470 
04471    if (!ast_strlen_zero(id))
04472       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
04473 
04474    astman_send_ack(s, m, "Parked calls will follow");
04475 
04476    iter = ao2_iterator_init(parkinglots, 0);
04477    while ((curlot = ao2_iterator_next(&iter))) {
04478 
04479       AST_LIST_LOCK(&curlot->parkings);
04480       AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
04481          astman_append(s, "Event: ParkedCall\r\n"
04482             "Exten: %d\r\n"
04483             "Channel: %s\r\n"
04484             "From: %s\r\n"
04485             "Timeout: %ld\r\n"
04486             "CallerIDNum: %s\r\n"
04487             "CallerIDName: %s\r\n"
04488             "%s"
04489             "\r\n",
04490             cur->parkingnum, cur->chan->name, cur->peername,
04491             (long) cur->start.tv_sec + (long) (cur->parkingtime / 1000) - (long) time(NULL),
04492             S_OR(cur->chan->cid.cid_num, ""),   /* XXX in other places it is <unknown> */
04493             S_OR(cur->chan->cid.cid_name, ""),
04494             idText);
04495       }
04496       AST_LIST_UNLOCK(&curlot->parkings);
04497       ao2_ref(curlot, -1);
04498    }
04499 
04500    astman_append(s,
04501       "Event: ParkedCallsComplete\r\n"
04502       "%s"
04503       "\r\n",idText);
04504 
04505 
04506    return RESULT_SUCCESS;
04507 }
04508 
04509 static char mandescr_park[] =
04510 "Description: Park a channel.\n"
04511 "Variables: (Names marked with * are required)\n"
04512 "  *Channel: Channel name to park\n"
04513 "  *Channel2: Channel to announce park info to (and return to if timeout)\n"
04514 "  Timeout: Number of milliseconds to wait before callback.\n";  
04515 
04516 /*!
04517  * \brief Create manager event for parked calls
04518  * \param s
04519  * \param m
04520  *
04521  * Get channels involved in park, create event.
04522  * \return Always 0
04523 */
04524 static int manager_park(struct mansession *s, const struct message *m)
04525 {
04526    const char *channel = astman_get_header(m, "Channel");
04527    const char *channel2 = astman_get_header(m, "Channel2");
04528    const char *timeout = astman_get_header(m, "Timeout");
04529    char buf[BUFSIZ];
04530    int to = 0;
04531    int res = 0;
04532    int parkExt = 0;
04533    struct ast_channel *ch1, *ch2;
04534 
04535    if (ast_strlen_zero(channel)) {
04536       astman_send_error(s, m, "Channel not specified");
04537       return 0;
04538    }
04539 
04540    if (ast_strlen_zero(channel2)) {
04541       astman_send_error(s, m, "Channel2 not specified");
04542       return 0;
04543    }
04544 
04545    ch1 = ast_get_channel_by_name_locked(channel);
04546    if (!ch1) {
04547       snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
04548       astman_send_error(s, m, buf);
04549       return 0;
04550    }
04551 
04552    ch2 = ast_get_channel_by_name_locked(channel2);
04553    if (!ch2) {
04554       snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel2);
04555       astman_send_error(s, m, buf);
04556       ast_channel_unlock(ch1);
04557       return 0;
04558    }
04559 
04560    if (!ast_strlen_zero(timeout)) {
04561       sscanf(timeout, "%30d", &to);
04562    }
04563 
04564    res = ast_masq_park_call(ch1, ch2, to, &parkExt);
04565    if (!res) {
04566       ast_softhangup(ch2, AST_SOFTHANGUP_EXPLICIT);
04567       astman_send_ack(s, m, "Park successful");
04568    } else {
04569       astman_send_error(s, m, "Park failure");
04570    }
04571 
04572    ast_channel_unlock(ch1);
04573    ast_channel_unlock(ch2);
04574 
04575    return 0;
04576 }
04577 
04578 static int find_channel_by_group(struct ast_channel *c, void *data) {
04579    struct ast_channel *chan = data;
04580 
04581    return !c->pbx &&
04582       /* Accessing 'chan' here is safe without locking, because there is no way for
04583          the channel do disappear from under us at this point.  pickupgroup *could*
04584          change while we're here, but that isn't a problem. */
04585       (c != chan) &&
04586       (chan->pickupgroup & c->callgroup) &&
04587       ((c->_state == AST_STATE_RINGING) || (c->_state == AST_STATE_RING));
04588 }
04589 
04590 /*!
04591  * \brief Pickup a call
04592  * \param chan channel that initiated pickup.
04593  *
04594  * Walk list of channels, checking it is not itself, channel is pbx one,
04595  * check that the callgroup for both channels are the same and the channel is ringing.
04596  * Answer calling channel, flag channel as answered on queue, masq channels together.
04597 */
04598 int ast_pickup_call(struct ast_channel *chan)
04599 {
04600    struct ast_channel *cur = ast_channel_search_locked(find_channel_by_group, chan);
04601 
04602    if (cur) {
04603       int res = -1;
04604       ast_debug(1, "Call pickup on chan '%s' by '%s'\n",cur->name, chan->name);
04605       res = ast_answer(chan);
04606       if (res)
04607          ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
04608       res = ast_queue_control(chan, AST_CONTROL_ANSWER);
04609       if (res)
04610          ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
04611       res = ast_channel_masquerade(cur, chan);
04612       if (res)
04613          ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, cur->name);     /* Done */
04614       if (!ast_strlen_zero(pickupsound)) {
04615          ast_stream_and_wait(cur, pickupsound, "");
04616       }
04617       ast_channel_unlock(cur);
04618       return res;
04619    } else   {
04620       ast_debug(1, "No call pickup possible...\n");
04621       if (!ast_strlen_zero(pickupfailsound)) {
04622          ast_stream_and_wait(chan, pickupfailsound, "");
04623       }
04624    }
04625    return -1;
04626 }
04627 
04628 static char *app_bridge = "Bridge";
04629 
04630 enum {
04631    BRIDGE_OPT_PLAYTONE = (1 << 0),
04632 };
04633 
04634 AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
04635    AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE)
04636 END_OPTIONS );
04637 
04638 /*!
04639  * \brief Bridge channels
04640  * \param chan
04641  * \param data channel to bridge with.
04642  * 
04643  * Split data, check we aren't bridging with ourself, check valid channel,
04644  * answer call if not already, check compatible channels, setup bridge config
04645  * now bridge call, if transfered party hangs up return to PBX extension.
04646 */
04647 static int bridge_exec(struct ast_channel *chan, void *data)
04648 {
04649    struct ast_channel *current_dest_chan, *final_dest_chan;
04650    char *tmp_data  = NULL;
04651    struct ast_flags opts = { 0, };
04652    struct ast_bridge_config bconfig = { { 0, }, };
04653 
04654    AST_DECLARE_APP_ARGS(args,
04655       AST_APP_ARG(dest_chan);
04656       AST_APP_ARG(options);
04657    );
04658    
04659    if (ast_strlen_zero(data)) {
04660       ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
04661       return -1;
04662    }
04663 
04664    tmp_data = ast_strdupa(data);
04665    AST_STANDARD_APP_ARGS(args, tmp_data);
04666    if (!ast_strlen_zero(args.options))
04667       ast_app_parse_options(bridge_exec_options, &opts, NULL, args.options);
04668 
04669    /* avoid bridge with ourselves */
04670    if (!strcmp(chan->name, args.dest_chan)) {
04671       ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", chan->name);
04672       manager_event(EVENT_FLAG_CALL, "BridgeExec",
04673                "Response: Failed\r\n"
04674                "Reason: Unable to bridge channel to itself\r\n"
04675                "Channel1: %s\r\n"
04676                "Channel2: %s\r\n",
04677                chan->name, args.dest_chan);
04678       pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
04679       return 0;
04680    }
04681 
04682    /* make sure we have a valid end point */
04683    if (!(current_dest_chan = ast_get_channel_by_name_prefix_locked(args.dest_chan, 
04684       strlen(args.dest_chan)))) {
04685       ast_log(LOG_WARNING, "Bridge failed because channel %s does not exists or we "
04686          "cannot get its lock\n", args.dest_chan);
04687       manager_event(EVENT_FLAG_CALL, "BridgeExec",
04688                "Response: Failed\r\n"
04689                "Reason: Cannot grab end point\r\n"
04690                "Channel1: %s\r\n"
04691                "Channel2: %s\r\n", chan->name, args.dest_chan);
04692       pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
04693       return 0;
04694    }
04695 
04696    /* answer the channel if needed */
04697    if (current_dest_chan->_state != AST_STATE_UP)
04698       ast_answer(current_dest_chan);
04699 
04700    /* try to allocate a place holder where current_dest_chan will be placed */
04701    if (!(final_dest_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, 
04702       NULL, NULL, 0, "Bridge/%s", current_dest_chan->name))) {
04703       ast_log(LOG_WARNING, "Cannot create placeholder channel for chan %s\n", args.dest_chan);
04704       manager_event(EVENT_FLAG_CALL, "BridgeExec",
04705                "Response: Failed\r\n"
04706                "Reason: cannot create placeholder\r\n"
04707                "Channel1: %s\r\n"
04708                "Channel2: %s\r\n", chan->name, args.dest_chan);
04709    }
04710    do_bridge_masquerade(current_dest_chan, final_dest_chan);
04711 
04712    ast_channel_unlock(current_dest_chan);
04713 
04714    /* now current_dest_chan is a ZOMBIE and with softhangup set to 1 and final_dest_chan is our end point */
04715    /* try to make compatible, send error if we fail */
04716    if (ast_channel_make_compatible(chan, final_dest_chan) < 0) {
04717       ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, final_dest_chan->name);
04718       manager_event(EVENT_FLAG_CALL, "BridgeExec",
04719                "Response: Failed\r\n"
04720                "Reason: Could not make channels compatible for bridge\r\n"
04721                "Channel1: %s\r\n"
04722                "Channel2: %s\r\n", chan->name, final_dest_chan->name);
04723       ast_hangup(final_dest_chan); /* may be we should return this channel to the PBX? */
04724       pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE");
04725       return 0;
04726    }
04727 
04728    /* Report that the bridge will be successfull */
04729    manager_event(EVENT_FLAG_CALL, "BridgeExec",
04730             "Response: Success\r\n"
04731             "Channel1: %s\r\n"
04732             "Channel2: %s\r\n", chan->name, final_dest_chan->name);
04733 
04734    /* we have 2 valid channels to bridge, now it is just a matter of setting up the bridge config and starting the bridge */  
04735    if (ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE) && !ast_strlen_zero(xfersound)) {
04736       if (!ast_streamfile(final_dest_chan, xfersound, final_dest_chan->language)) {
04737          if (ast_waitstream(final_dest_chan, "") < 0)
04738             ast_log(LOG_WARNING, "Failed to play courtesy tone on %s\n", final_dest_chan->name);
04739       }
04740    }
04741    
04742    /* do the bridge */
04743    ast_bridge_call(chan, final_dest_chan, &bconfig);
04744 
04745    /* the bridge has ended, set BRIDGERESULT to SUCCESS. If the other channel has not been hung up, return it to the PBX */
04746    pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
04747    if (!ast_check_hangup(final_dest_chan)) {
04748       ast_debug(1, "starting new PBX in %s,%s,%d for chan %s\n", 
04749          final_dest_chan->context, final_dest_chan->exten, 
04750          final_dest_chan->priority, final_dest_chan->name);
04751 
04752       if (ast_pbx_start(final_dest_chan) != AST_PBX_SUCCESS) {
04753          ast_log(LOG_WARNING, "FAILED continuing PBX on dest chan %s\n", final_dest_chan->name);
04754          ast_hangup(final_dest_chan);
04755       } else
04756          ast_debug(1, "SUCCESS continuing PBX on chan %s\n", final_dest_chan->name);
04757    } else {
04758       ast_debug(1, "hangup chan %s since the other endpoint has hung up\n", final_dest_chan->name);
04759       ast_hangup(final_dest_chan);
04760    }
04761 
04762    return 0;
04763 }
04764 
04765 int ast_features_init(void)
04766 {
04767    int res;
04768 
04769    ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
04770 
04771    parkinglots = ao2_container_alloc(7, parkinglot_hash_cb, parkinglot_cmp_cb);
04772 
04773    if ((res = load_config()))
04774       return res;
04775    ast_cli_register_multiple(cli_features, ARRAY_LEN(cli_features));
04776    ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
04777    res = ast_register_application2(parkedcall, park_exec, NULL, NULL, NULL);
04778    if (!res)
04779       res = ast_register_application2(parkcall, park_call_exec, NULL, NULL, NULL);
04780    if (!res) {
04781       ast_manager_register("ParkedCalls", 0, manager_parking_status, "List parked calls");
04782       ast_manager_register2("Park", EVENT_FLAG_CALL, manager_park, "Park a channel", mandescr_park); 
04783       ast_manager_register2("Bridge", EVENT_FLAG_CALL, action_bridge, "Bridge two channels already in the PBX", mandescr_bridge);
04784    }
04785 
04786    res |= ast_devstate_prov_add("Park", metermaidstate);
04787 
04788    return res;
04789 }

Generated on Sat Mar 26 11:08:30 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.9