f194274ff370ec0b78bb500a2b585d23c2977fd1
[asterisk/asterisk.git] / main / features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Routines implementing call features as call pickup, parking and transfer
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/_private.h"
35
36 #include <pthread.h>
37 #include <signal.h>
38 #include <sys/time.h>
39 #include <sys/signal.h>
40 #include <netinet/in.h>
41
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/causes.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/app.h"
50 #include "asterisk/say.h"
51 #include "asterisk/features.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/config.h"
54 #include "asterisk/cli.h"
55 #include "asterisk/manager.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/adsi.h"
58 #include "asterisk/devicestate.h"
59 #include "asterisk/monitor.h"
60 #include "asterisk/audiohook.h"
61 #include "asterisk/global_datastores.h"
62 #include "asterisk/astobj2.h"
63 #include "asterisk/cel.h"
64 #include "asterisk/test.h"
65
66 /*
67  * Party A - transferee
68  * Party B - transferer
69  * Party C - target of transfer
70  *
71  * DTMF attended transfer works within the channel bridge.
72  * Unfortunately, when either party A or B in the channel bridge
73  * hangs up, that channel is not completely hung up until the
74  * transfer completes.  This is a real problem depending upon
75  * the channel technology involved.
76  *
77  * For chan_dahdi, the channel is crippled until the hangup is
78  * complete.  Either the channel is not useable (analog) or the
79  * protocol disconnect messages are held up (PRI/BRI/SS7) and
80  * the media is not released.
81  *
82  * For chan_sip, a call limit of one is going to block that
83  * endpoint from any further calls until the hangup is complete.
84  *
85  * For party A this is a minor problem.  The party A channel
86  * will only be in this condition while party B is dialing and
87  * when party B and C are conferring.  The conversation between
88  * party B and C is expected to be a short one.  Party B is
89  * either asking a question of party C or announcing party A.
90  * Also party A does not have much incentive to hangup at this
91  * point.
92  *
93  * For party B this can be a major problem during a blonde
94  * transfer.  (A blonde transfer is our term for an attended
95  * transfer that is converted into a blind transfer. :))  Party
96  * B could be the operator.  When party B hangs up, he assumes
97  * that he is out of the original call entirely.  The party B
98  * channel will be in this condition while party C is ringing,
99  * while attempting to recall party B, and while waiting between
100  * call attempts.
101  *
102  * WARNING:
103  * The ATXFER_NULL_TECH conditional is a hack to fix the
104  * problem.  It will replace the party B channel technology with
105  * a NULL channel driver.  The consequences of this code is that
106  * the 'h' extension will not be able to access any channel
107  * technology specific information like SIP statistics for the
108  * call.
109  *
110  * Uncomment the ATXFER_NULL_TECH define below to replace the
111  * party B channel technology in the channel bridge to complete
112  * hanging up the channel technology.
113  */
114 //#define ATXFER_NULL_TECH      1
115
116 /*** DOCUMENTATION
117         <application name="Bridge" language="en_US">
118                 <synopsis>
119                         Bridge two channels.
120                 </synopsis>
121                 <syntax>
122                         <parameter name="channel" required="true">
123                                 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
124                         </parameter>
125                         <parameter name="options">
126                                 <optionlist>
127                                         <option name="p">
128                                                 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
129                                         </option>
130                                         <option name="h">
131                                                 <para>Allow the called party to hang up by sending the
132                                                 <replaceable>*</replaceable> DTMF digit.</para>
133                                         </option>
134                                         <option name="H">
135                                                 <para>Allow the calling party to hang up by pressing the
136                                                 <replaceable>*</replaceable> DTMF digit.</para>
137                                         </option>
138                                         <option name="k">
139                                                 <para>Allow the called party to enable parking of the call by sending
140                                                 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
141                                         </option>
142                                         <option name="K">
143                                                 <para>Allow the calling party to enable parking of the call by sending
144                                                  the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
145                                         </option>
146                                         <option name="L(x[:y][:z])">
147                                                 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
148                                                 when <replaceable>y</replaceable> ms are left. Repeat the warning every
149                                                 <replaceable>z</replaceable> ms. The following special variables can be
150                                                 used with this option:</para>
151                                                 <variablelist>
152                                                         <variable name="LIMIT_PLAYAUDIO_CALLER">
153                                                                 <para>Play sounds to the caller. yes|no (default yes)</para>
154                                                         </variable>
155                                                         <variable name="LIMIT_PLAYAUDIO_CALLEE">   
156                                                                 <para>Play sounds to the callee. yes|no</para>
157                                                         </variable>
158                                                         <variable name="LIMIT_TIMEOUT_FILE">
159                                                                 <para>File to play when time is up.</para>
160                                                         </variable>
161                                                         <variable name="LIMIT_CONNECT_FILE">
162                                                                 <para>File to play when call begins.</para>
163                                                         </variable>
164                                                         <variable name="LIMIT_WARNING_FILE">
165                                                                 <para>File to play as warning if <replaceable>y</replaceable> is
166                                                                 defined. The default is to say the time remaining.</para>
167                                                         </variable>
168                                                 </variablelist>
169                                         </option>
170                                         <option name="S(x)">
171                                                 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
172                                         </option>
173                                         <option name="t">
174                                                 <para>Allow the called party to transfer the calling party by sending the
175                                                 DTMF sequence defined in <filename>features.conf</filename>.</para>
176                                         </option>
177                                         <option name="T">
178                                                 <para>Allow the calling party to transfer the called party by sending the
179                                                 DTMF sequence defined in <filename>features.conf</filename>.</para>
180                                         </option>
181                                         <option name="w">
182                                                 <para>Allow the called party to enable recording of the call by sending
183                                                 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
184                                         </option>
185                                         <option name="W">
186                                                 <para>Allow the calling party to enable recording of the call by sending
187                                                 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
188                                         </option>
189                                         <option name="x">
190                                                 <para>Cause the called party to be hung up after the bridge, instead of being
191                                                 restarted in the dialplan.</para>
192                                         </option>
193                                 </optionlist>
194                         </parameter>
195                 </syntax>
196                 <description>
197                         <para>Allows the ability to bridge two channels via the dialplan.</para>
198                         <para>This application sets the following channel variable upon completion:</para>
199                         <variablelist>
200                                 <variable name="BRIDGERESULT">
201                                         <para>The result of the bridge attempt as a text string.</para>
202                                         <value name="SUCCESS" />
203                                         <value name="FAILURE" />
204                                         <value name="LOOP" />
205                                         <value name="NONEXISTENT" />
206                                         <value name="INCOMPATIBLE" />
207                                 </variable>
208                         </variablelist>
209                 </description>
210         </application>
211         <application name="ParkedCall" language="en_US">
212                 <synopsis>
213                         Retrieve a parked call.
214                 </synopsis>
215                 <syntax>
216                         <parameter name="exten">
217                                 <para>Parking space extension to retrieve a parked call.
218                                 If not provided then the first available parked call in the
219                                 parking lot will be retrieved.</para>
220                         </parameter>
221                         <parameter name="parking_lot_name">
222                                 <para>Specify from which parking lot to retrieve a parked call.</para>
223                                 <para>The parking lot used is selected in the following order:</para>
224                                 <para>1) parking_lot_name option</para>
225                                 <para>2) <variable>PARKINGLOT</variable> variable</para>
226                                 <para>3) <literal>CHANNEL(parkinglot)</literal> function
227                                 (Possibly preset by the channel driver.)</para>
228                                 <para>4) Default parking lot.</para>
229                         </parameter>
230                 </syntax>
231                 <description>
232                         <para>Used to retrieve a parked call from a parking lot.</para>
233                         <note>
234                                 <para>Parking lots automatically create and manage dialplan extensions in
235                                 the parking lot context.  You do not need to explicitly use this
236                                 application in your dialplan.  Instead, all you should do is include the
237                                 parking lot context in your dialplan.</para>
238                         </note>
239                 </description>
240                 <see-also>
241                         <ref type="application">Park</ref>
242                         <ref type="application">ParkAndAnnounce</ref>
243                 </see-also>
244         </application>
245         <application name="Park" language="en_US">
246                 <synopsis>
247                         Park yourself.
248                 </synopsis>
249                 <syntax>
250                         <parameter name="timeout">
251                                 <para>A custom parking timeout for this parked call. Value in milliseconds.</para>
252                         </parameter>
253                         <parameter name="return_context">
254                                 <para>The context to return the call to after it times out.</para>
255                         </parameter>
256                         <parameter name="return_exten">
257                                 <para>The extension to return the call to after it times out.</para>
258                         </parameter>
259                         <parameter name="return_priority">
260                                 <para>The priority to return the call to after it times out.</para>
261                         </parameter>
262                         <parameter name="options">
263                                 <para>A list of options for this parked call.</para>
264                                 <optionlist>
265                                         <option name="r">
266                                                 <para>Send ringing instead of MOH to the parked call.</para>
267                                         </option>
268                                         <option name="R">
269                                                 <para>Randomize the selection of a parking space.</para>
270                                         </option>
271                                         <option name="s">
272                                                 <para>Silence announcement of the parking space number.</para>
273                                         </option>
274                                 </optionlist>
275                         </parameter>
276                         <parameter name="parking_lot_name">
277                                 <para>Specify in which parking lot to park a call.</para>
278                                 <para>The parking lot used is selected in the following order:</para>
279                                 <para>1) parking_lot_name option</para>
280                                 <para>2) <variable>PARKINGLOT</variable> variable</para>
281                                 <para>3) <literal>CHANNEL(parkinglot)</literal> function
282                                 (Possibly preset by the channel driver.)</para>
283                                 <para>4) Default parking lot.</para>
284                         </parameter>
285                 </syntax>
286                 <description>
287                         <para>Used to park yourself (typically in combination with a supervised
288                         transfer to know the parking space).</para>
289                         <para>If you set the <variable>PARKINGEXTEN</variable> variable to a
290                         parking space extension in the parking lot, Park() will attempt to park the call
291                         on that extension.  If the extension is already is in use then execution
292                         will continue at the next priority.</para>
293                         <para>If the <literal>parkeddynamic</literal> option is enabled in <filename>features.conf</filename>
294                         the following variables can be used to dynamically create new parking lots.</para>
295                         <para>If you set the <variable>PARKINGDYNAMIC</variable> variable and this parking lot
296                         exists then it will be used as a template for the newly created dynamic lot.  Otherwise,
297                         the default parking lot will be used.</para>
298                         <para>If you set the <variable>PARKINGDYNCONTEXT</variable> variable then the newly created dynamic
299                         parking lot will use this context.</para>
300                         <para>If you set the <variable>PARKINGDYNEXTEN</variable> variable then the newly created dynamic
301                         parking lot will use this extension to access the parking lot.</para>
302                         <para>If you set the <variable>PARKINGDYNPOS</variable> variable then the newly created dynamic parking lot
303                         will use those parking postitions.</para>
304                         <note>
305                                 <para>This application must be used as the first extension priority
306                                 to be recognized as a parking access extension.  DTMF transfers
307                                 and some channel drivers need this distinction to operate properly.
308                                 The parking access extension in this case is treated like a dialplan
309                                 hint.</para>
310                         </note>
311                         <note>
312                                 <para>Parking lots automatically create and manage dialplan extensions in
313                                 the parking lot context.  You do not need to explicitly use this
314                                 application in your dialplan.  Instead, all you should do is include the
315                                 parking lot context in your dialplan.</para>
316                         </note>
317                 </description>
318                 <see-also>
319                         <ref type="application">ParkAndAnnounce</ref>
320                         <ref type="application">ParkedCall</ref>
321                 </see-also>
322         </application>
323         <manager name="ParkedCalls" language="en_US">
324                 <synopsis>
325                         List parked calls.
326                 </synopsis>
327                 <syntax>
328                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
329                 </syntax>
330                 <description>
331                         <para>List parked calls.</para>
332                 </description>
333         </manager>
334         <manager name="Park" language="en_US">
335                 <synopsis>
336                         Park a channel.
337                 </synopsis>
338                 <syntax>
339                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
340                         <parameter name="Channel" required="true">
341                                 <para>Channel name to park.</para>
342                         </parameter>
343                         <parameter name="Channel2" required="true">
344                                 <para>Channel to return to if timeout.</para>
345                         </parameter>
346                         <parameter name="Timeout">
347                                 <para>Number of milliseconds to wait before callback.</para>
348                         </parameter>
349                         <parameter name="Parkinglot">
350                                 <para>Specify in which parking lot to park the channel.</para>
351                         </parameter>
352                 </syntax>
353                 <description>
354                         <para>Park a channel.</para>
355                 </description>
356         </manager>
357         <manager name="Bridge" language="en_US">
358                 <synopsis>
359                         Bridge two channels already in the PBX.
360                 </synopsis>
361                 <syntax>
362                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
363                         <parameter name="Channel1" required="true">
364                                 <para>Channel to Bridge to Channel2.</para>
365                         </parameter>
366                         <parameter name="Channel2" required="true">
367                                 <para>Channel to Bridge to Channel1.</para>
368                         </parameter>
369                         <parameter name="Tone">
370                                 <para>Play courtesy tone to Channel 2.</para>
371                                 <enumlist>
372                                         <enum name="yes" />
373                                         <enum name="no" />
374                                 </enumlist>
375                         </parameter>
376                 </syntax>
377                 <description>
378                         <para>Bridge together two channels already in the PBX.</para>
379                 </description>
380         </manager>
381  ***/
382
383 #define DEFAULT_PARK_TIME                                                       45000   /*!< ms */
384 #define DEFAULT_PARK_EXTENSION                                          "700"
385 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT                          3000    /*!< ms */
386 #define DEFAULT_FEATURE_DIGIT_TIMEOUT                           1000    /*!< ms */
387 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER      15000   /*!< ms */
388 #define DEFAULT_ATXFER_DROP_CALL                                        0               /*!< Do not drop call. */
389 #define DEFAULT_ATXFER_LOOP_DELAY                                       10000   /*!< ms */
390 #define DEFAULT_ATXFER_CALLBACK_RETRIES                         2
391
392 #define AST_MAX_WATCHERS 256
393 #define MAX_DIAL_FEATURE_OPTIONS 30
394
395 struct feature_group_exten {
396         AST_LIST_ENTRY(feature_group_exten) entry;
397         AST_DECLARE_STRING_FIELDS(
398                 AST_STRING_FIELD(exten);
399         );
400         struct ast_call_feature *feature;
401 };
402
403 struct feature_group {
404         AST_LIST_ENTRY(feature_group) entry;
405         AST_DECLARE_STRING_FIELDS(
406                 AST_STRING_FIELD(gname);
407         );
408         AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
409 };
410
411 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
412
413 typedef enum {
414         FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
415         FEATURE_INTERPRET_DO,     /* Used by feature_interpret */
416         FEATURE_INTERPRET_CHECK,  /* Used by feature_check */
417 } feature_interpret_op;
418
419 static char *parkedcall = "ParkedCall";
420
421 static char pickup_ext[AST_MAX_EXTENSION];                 /*!< Call pickup extension */
422
423 /*! Parking lot access ramp dialplan usage entry. */
424 struct parking_dp_ramp {
425         /*! Next node in the parking lot spaces dialplan list. */
426         AST_LIST_ENTRY(parking_dp_ramp) node;
427         /*! TRUE if the parking lot access extension is exclusive. */
428         unsigned int exclusive:1;
429         /*! Parking lot access extension */
430         char exten[1];
431 };
432
433 /*! Parking lot dialplan access ramp map */
434 AST_LIST_HEAD_NOLOCK(parking_dp_ramp_map, parking_dp_ramp);
435
436 /*! Parking lot spaces dialplan usage entry. */
437 struct parking_dp_spaces {
438         /*! Next node in the parking lot spaces dialplan list. */
439         AST_LIST_ENTRY(parking_dp_spaces) node;
440         /*! First parking space */
441         int start;
442         /*! Last parking space */
443         int stop;
444 };
445
446 /*! Parking lot dialplan context space map */
447 AST_LIST_HEAD_NOLOCK(parking_dp_space_map, parking_dp_spaces);
448
449 /*! Parking lot context dialplan usage entry. */
450 struct parking_dp_context {
451         /*! Next node in the parking lot contexts dialplan list. */
452         AST_LIST_ENTRY(parking_dp_context) node;
453         /*! Parking access extensions defined in this context. */
454         struct parking_dp_ramp_map access_extens;
455         /*! Parking spaces defined in this context. */
456         struct parking_dp_space_map spaces;
457         /*! Parking hints defined in this context. */
458         struct parking_dp_space_map hints;
459         /*! Parking lot context name */
460         char context[1];
461 };
462
463 /*! Parking lot dialplan usage map. */
464 AST_LIST_HEAD_NOLOCK(parking_dp_map, parking_dp_context);
465
466 /*!
467  * \brief Description of one parked call, added to a list while active, then removed.
468  * The list belongs to a parkinglot.
469  */
470 struct parkeduser {
471         struct ast_channel *chan;                   /*!< Parked channel */
472         struct timeval start;                       /*!< Time the park started */
473         int parkingnum;                             /*!< Parking lot space used */
474         char parkingexten[AST_MAX_EXTENSION];       /*!< If set beforehand, parking extension used for this call */
475         char context[AST_MAX_CONTEXT];              /*!< Where to go if our parking time expires */
476         char exten[AST_MAX_EXTENSION];
477         int priority;
478         int parkingtime;                            /*!< Maximum length in parking lot before return */
479         /*! Method to entertain the caller when parked: AST_CONTROL_RINGING, AST_CONTROL_HOLD, or 0(none) */
480         enum ast_control_frame_type hold_method;
481         unsigned int notquiteyet:1;
482         unsigned int options_specified:1;
483         char peername[1024];
484         unsigned char moh_trys;
485         /*! Parking lot this entry belongs to.  Holds a parking lot reference. */
486         struct ast_parkinglot *parkinglot;
487         AST_LIST_ENTRY(parkeduser) list;
488 };
489
490 /*! Parking lot configuration options. */
491 struct parkinglot_cfg {
492         /*! Music class used for parking */
493         char mohclass[MAX_MUSICCLASS];
494         /*! Extension to park calls in this parking lot. */
495         char parkext[AST_MAX_EXTENSION];
496         /*! Context for which parking is made accessible */
497         char parking_con[AST_MAX_EXTENSION];
498         /*! First available extension for parking */
499         int parking_start;
500         /*! Last available extension for parking */
501         int parking_stop;
502         /*! Default parking time in ms. */
503         int parkingtime;
504         /*!
505          * \brief Enable DTMF based transfers on bridge when picking up parked calls.
506          *
507          * \details
508          * none(0)
509          * AST_FEATURE_FLAG_BYCALLEE
510          * AST_FEATURE_FLAG_BYCALLER
511          * AST_FEATURE_FLAG_BYBOTH
512          */
513         int parkedcalltransfers;
514         /*!
515          * \brief Enable DTMF based parking on bridge when picking up parked calls.
516          *
517          * \details
518          * none(0)
519          * AST_FEATURE_FLAG_BYCALLEE
520          * AST_FEATURE_FLAG_BYCALLER
521          * AST_FEATURE_FLAG_BYBOTH
522          */
523         int parkedcallreparking;
524         /*!
525          * \brief Enable DTMF based hangup on a bridge when pickup up parked calls.
526          *
527          * \details
528          * none(0)
529          * AST_FEATURE_FLAG_BYCALLEE
530          * AST_FEATURE_FLAG_BYCALLER
531          * AST_FEATURE_FLAG_BYBOTH
532          */
533         int parkedcallhangup;
534         /*!
535          * \brief Enable DTMF based recording on a bridge when picking up parked calls.
536          *
537          * \details
538          * none(0)
539          * AST_FEATURE_FLAG_BYCALLEE
540          * AST_FEATURE_FLAG_BYCALLER
541          * AST_FEATURE_FLAG_BYBOTH
542          */
543         int parkedcallrecording;
544
545         /*! TRUE if findslot is set to next */
546         unsigned int parkfindnext:1;
547         /*! TRUE if the parking lot is exclusively accessed by parkext */
548         unsigned int parkext_exclusive:1;
549         /*! Add parking hints automatically */
550         unsigned int parkaddhints:1;
551         /*! TRUE if configuration is invalid and the parking lot should not be used. */
552         unsigned int is_invalid:1;
553 };
554
555 /*! \brief Structure for parking lots which are put in a container. */
556 struct ast_parkinglot {
557         /*! Name of the parking lot. */
558         char name[AST_MAX_CONTEXT];
559         /*! Parking lot user configuration. */
560         struct parkinglot_cfg cfg;
561
562         /*! Parking space to start next park search. */
563         int next_parking_space;
564
565         /*! That which bears the_mark shall be deleted if parking lot empty! (Used during reloads.) */
566         unsigned int the_mark:1;
567         /*! TRUE if the parking lot is disabled. */
568         unsigned int disabled:1;
569
570         /*! List of active parkings in this parkinglot */
571         AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
572 };
573
574 /*! \brief The configured parking lots container. Always at least one  - the default parking lot */
575 static struct ao2_container *parkinglots;
576
577 /*!
578  * \brief Default parking lot.
579  * \note Holds a parkinglot reference.
580  * \note Will not be NULL while running.
581  */
582 static struct ast_parkinglot *default_parkinglot;
583
584 /*! Force a config reload to reload regardless of config file timestamp. */
585 static int force_reload_load;
586
587 static int parkedplay = 0;                                 /*!< Who to play courtesytone to when someone picks up a parked call. */
588 static int parkeddynamic = 0;                              /*!< Enable creation of parkinglots dynamically */
589 static char courtesytone[256];                             /*!< Courtesy tone used to pickup parked calls and on-touch-record */
590 static char xfersound[256];                                /*!< Call transfer sound */
591 static char xferfailsound[256];                            /*!< Call transfer failure sound */
592 static char pickupsound[256];                              /*!< Pickup sound */
593 static char pickupfailsound[256];                          /*!< Pickup failure sound */
594
595 /*!
596  * \brief Context for parking dialback to parker.
597  * \note The need for the context is a KLUDGE.
598  *
599  * \todo Might be able to eliminate the parking_con_dial context
600  * kludge by running app_dial directly in its own thread to
601  * simulate a PBX.
602  */
603 static char parking_con_dial[] = "park-dial";
604
605 /*! Ensure that features.conf reloads on one thread at a time. */
606 AST_MUTEX_DEFINE_STATIC(features_reload_lock);
607
608 static int adsipark;
609
610 static int transferdigittimeout;
611 static int featuredigittimeout;
612 static int comebacktoorigin = 1;
613
614 static int atxfernoanswertimeout;
615 static unsigned int atxferdropcall;
616 static unsigned int atxferloopdelay;
617 static unsigned int atxfercallbackretries;
618
619 static char *registrar = "features";               /*!< Registrar for operations */
620
621 /*! PARK_APP_NAME application arguments */
622 AST_DEFINE_APP_ARGS_TYPE(park_app_args,
623         AST_APP_ARG(timeout);           /*!< Time in ms to remain in the parking lot. */
624         AST_APP_ARG(return_con);        /*!< Context to return parked call if timeout. */
625         AST_APP_ARG(return_ext);        /*!< Exten to return parked call if timeout. */
626         AST_APP_ARG(return_pri);        /*!< Priority to return parked call if timeout. */
627         AST_APP_ARG(options);           /*!< Parking option flags. */
628         AST_APP_ARG(pl_name);           /*!< Parking lot name to use if present. */
629         AST_APP_ARG(dummy);                     /*!< Place to put any remaining args string. */
630         );
631
632 /* module and CLI command definitions */
633 static char *parkcall = PARK_APP_NAME;
634
635 static struct ast_app *monitor_app = NULL;
636 static int monitor_ok = 1;
637
638 static struct ast_app *mixmonitor_app = NULL;
639 static int mixmonitor_ok = 1;
640
641 static struct ast_app *stopmixmonitor_app = NULL;
642 static int stopmixmonitor_ok = 1;
643
644 static pthread_t parking_thread;
645 struct ast_dial_features {
646         struct ast_flags features_caller;
647         struct ast_flags features_callee;
648         int is_caller;
649 };
650
651 #if defined(ATXFER_NULL_TECH)
652 /*!
653  * \internal
654  * \brief Set the channel technology to the kill technology.
655  *
656  * \param chan Channel to change technology.
657  *
658  * \return Nothing
659  */
660 static void set_kill_chan_tech(struct ast_channel *chan)
661 {
662         int idx;
663
664         ast_channel_lock(chan);
665
666         /* Hangup the channel's physical side */
667         if (chan->tech->hangup) {
668                 chan->tech->hangup(chan);
669         }
670         if (chan->tech_pvt) {
671                 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
672                         chan->name);
673                 ast_free(chan->tech_pvt);
674                 chan->tech_pvt = NULL;
675         }
676
677         /* Install the kill technology and wake up anyone waiting on it. */
678         chan->tech = &ast_kill_tech;
679         for (idx = 0; idx < AST_MAX_FDS; ++idx) {
680                 switch (idx) {
681                 case AST_ALERT_FD:
682                 case AST_TIMING_FD:
683                 case AST_GENERATOR_FD:
684                         /* Don't clear these fd's. */
685                         break;
686                 default:
687                         ast_channel_set_fd(chan, idx, -1);
688                         break;
689                 }
690         }
691         ast_queue_frame(chan, &ast_null_frame);
692
693         ast_channel_unlock(chan);
694 }
695 #endif  /* defined(ATXFER_NULL_TECH) */
696
697 #if defined(ATXFER_NULL_TECH)
698 /*!
699  * \internal
700  * \brief Set the channel name to something unique.
701  *
702  * \param chan Channel to change name.
703  *
704  * \return Nothing
705  */
706 static void set_new_chan_name(struct ast_channel *chan)
707 {
708         static int seq_num_last;
709         int seq_num;
710         int len;
711         char *chan_name;
712         char dummy[1];
713
714         /* Create the new channel name string. */
715         ast_channel_lock(chan);
716         seq_num = ast_atomic_fetchadd_int(&seq_num_last, +1);
717         len = snprintf(dummy, sizeof(dummy), "%s<XFER_%x>", chan->name, seq_num) + 1;
718         chan_name = alloca(len);
719         snprintf(chan_name, len, "%s<XFER_%x>", chan->name, seq_num);
720         ast_channel_unlock(chan);
721
722         ast_change_name(chan, chan_name);
723 }
724 #endif  /* defined(ATXFER_NULL_TECH) */
725
726 static void *dial_features_duplicate(void *data)
727 {
728         struct ast_dial_features *df = data, *df_copy;
729  
730         if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
731                 return NULL;
732         }
733  
734         memcpy(df_copy, df, sizeof(*df));
735  
736         return df_copy;
737 }
738
739 static void dial_features_destroy(void *data)
740 {
741         struct ast_dial_features *df = data;
742         if (df) {
743                 ast_free(df);
744         }
745 }
746
747 static const struct ast_datastore_info dial_features_info = {
748         .type = "dial-features",
749         .destroy = dial_features_destroy,
750         .duplicate = dial_features_duplicate,
751 };
752  
753 /* Forward declarations */
754 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
755 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
756 static struct ast_parkinglot *find_parkinglot(const char *name);
757 static struct ast_parkinglot *create_parkinglot(const char *name);
758 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
759 static int parkinglot_activate(struct ast_parkinglot *parkinglot);
760 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile);
761
762 /*!
763  * \internal
764  * \brief Get the parking extension if it exists.
765  *
766  * \param exten_str Parking extension to see if exists.
767  * \param chan Channel to autoservice while looking for exten.  (Could be NULL)
768  * \param context Parking context to look in for exten.
769  *
770  * \retval exten on success.
771  * \retval NULL on error or exten does not exist.
772  */
773 static struct ast_exten *get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
774 {
775         struct ast_exten *exten;
776         struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
777         const char *app_at_exten;
778
779         exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
780                 E_MATCH);
781         if (!exten) {
782                 return NULL;
783         }
784
785         app_at_exten = ast_get_extension_app(exten);
786         if (!app_at_exten || strcasecmp(PARK_APP_NAME, app_at_exten)) {
787                 return NULL;
788         }
789
790         return exten;
791 }
792
793 int ast_parking_ext_valid(const char *exten_str, struct ast_channel *chan, const char *context)
794 {
795         return get_parking_exten(exten_str, chan, context) ? 1 : 0;
796 }
797
798 const char *ast_pickup_ext(void)
799 {
800         return pickup_ext;
801 }
802
803 struct ast_bridge_thread_obj 
804 {
805         struct ast_bridge_config bconfig;
806         struct ast_channel *chan;
807         struct ast_channel *peer;
808         unsigned int return_to_pbx:1;
809 };
810
811 static int parkinglot_hash_cb(const void *obj, const int flags)
812 {
813         const struct ast_parkinglot *parkinglot = obj;
814
815         return ast_str_case_hash(parkinglot->name);
816 }
817
818 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
819 {
820         struct ast_parkinglot *parkinglot = obj;
821         struct ast_parkinglot *parkinglot2 = arg;
822
823         return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
824 }
825
826 /*!
827  * \brief store context, extension and priority 
828  * \param chan, context, ext, pri
829  */
830 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
831 {
832         ast_copy_string(chan->context, context, sizeof(chan->context));
833         ast_copy_string(chan->exten, ext, sizeof(chan->exten));
834         chan->priority = pri;
835 }
836
837 /*!
838  * \brief Check goto on transfer
839  * \param chan
840  *
841  * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
842  * When found make sure the types are compatible. Check if channel is valid
843  * if so start the new channel else hangup the call. 
844  */
845 static void check_goto_on_transfer(struct ast_channel *chan) 
846 {
847         struct ast_channel *xferchan;
848         const char *val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
849         char *x, *goto_on_transfer;
850         struct ast_frame *f;
851
852         if (ast_strlen_zero(val))
853                 return;
854
855         goto_on_transfer = ast_strdupa(val);
856
857         if (!(xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", chan->linkedid, 0, "%s", chan->name)))
858                 return;
859
860         for (x = goto_on_transfer; x && *x; x++) {
861                 if (*x == '^')
862                         *x = ',';
863         }
864         /* Make formats okay */
865         xferchan->readformat = chan->readformat;
866         xferchan->writeformat = chan->writeformat;
867         ast_channel_masquerade(xferchan, chan);
868         ast_parseable_goto(xferchan, goto_on_transfer);
869         xferchan->_state = AST_STATE_UP;
870         ast_clear_flag(xferchan, AST_FLAGS_ALL);        
871         ast_channel_clear_softhangup(xferchan, AST_SOFTHANGUP_ALL);
872         if ((f = ast_read(xferchan))) {
873                 ast_frfree(f);
874                 f = NULL;
875                 ast_pbx_start(xferchan);
876         } else {
877                 ast_hangup(xferchan);
878         }
879 }
880
881 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
882         const char *caller_name, struct ast_channel *requestor,
883         struct ast_channel *transferee, const char *type, struct ast_format_cap *cap, void *data,
884         int timeout, int *outstate, const char *language);
885
886 /*!
887  * \brief bridge the call 
888  * \param data thread bridge.
889  *
890  * Set Last Data for respective channels, reset cdr for channels
891  * bridge call, check if we're going back to dialplan
892  * if not hangup both legs of the call
893  */
894 static void *bridge_call_thread(void *data)
895 {
896         struct ast_bridge_thread_obj *tobj = data;
897         int res;
898
899         tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
900         tobj->chan->data = tobj->peer->name;
901         tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
902         tobj->peer->data = tobj->chan->name;
903
904         ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
905
906         if (tobj->return_to_pbx) {
907                 if (!ast_check_hangup(tobj->peer)) {
908                         ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
909                         res = ast_pbx_start(tobj->peer);
910                         if (res != AST_PBX_SUCCESS)
911                                 ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
912                 } else
913                         ast_hangup(tobj->peer);
914                 if (!ast_check_hangup(tobj->chan)) {
915                         ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
916                         res = ast_pbx_start(tobj->chan);
917                         if (res != AST_PBX_SUCCESS)
918                                 ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
919                 } else
920                         ast_hangup(tobj->chan);
921         } else {
922                 ast_hangup(tobj->chan);
923                 ast_hangup(tobj->peer);
924         }
925
926         ast_free(tobj);
927
928         return NULL;
929 }
930
931 /*!
932  * \brief create thread for the parked call
933  * \param data
934  *
935  * Create thread and attributes, call bridge_call_thread
936  */
937 static void bridge_call_thread_launch(void *data) 
938 {
939         pthread_t thread;
940         pthread_attr_t attr;
941         struct sched_param sched;
942
943         pthread_attr_init(&attr);
944         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
945         ast_pthread_create(&thread, &attr, bridge_call_thread, data);
946         pthread_attr_destroy(&attr);
947         memset(&sched, 0, sizeof(sched));
948         pthread_setschedparam(thread, SCHED_RR, &sched);
949 }
950
951 /*!
952  * \brief Announce call parking by ADSI
953  * \param chan .
954  * \param parkingexten .
955  * Create message to show for ADSI, display message.
956  * \retval 0 on success.
957  * \retval -1 on failure.
958  */
959 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
960 {
961         int res;
962         int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
963         char tmp[256];
964         char *message[5] = {NULL, NULL, NULL, NULL, NULL};
965
966         snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
967         message[0] = tmp;
968         res = ast_adsi_load_session(chan, NULL, 0, 1);
969         if (res == -1)
970                 return res;
971         return ast_adsi_print(chan, message, justify, 1);
972 }
973
974 /*!
975  * \brief Find parking lot name from channel
976  * \note Channel needs to be locked while the returned string is in use.
977  */
978 static const char *findparkinglotname(struct ast_channel *chan)
979 {
980         const char *name;
981
982         /* The channel variable overrides everything */
983         name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
984         if (!name && !ast_strlen_zero(chan->parkinglot)) {
985                 /* Use the channel's parking lot. */
986                 name = chan->parkinglot;
987         }
988         return name;
989 }
990
991 /*! \brief Notify metermaids that we've changed an extension */
992 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
993 {
994         ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
995                 exten, context, ast_devstate2str(state));
996
997         ast_devstate_changed(state, "park:%s@%s", exten, context);
998 }
999
1000 /*! \brief metermaids callback from devicestate.c */
1001 static enum ast_device_state metermaidstate(const char *data)
1002 {
1003         char *context;
1004         char *exten;
1005
1006         context = ast_strdupa(data);
1007
1008         exten = strsep(&context, "@");
1009         if (!context)
1010                 return AST_DEVICE_INVALID;
1011
1012         ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
1013
1014         if (!ast_exists_extension(NULL, context, exten, 1, NULL))
1015                 return AST_DEVICE_NOT_INUSE;
1016
1017         return AST_DEVICE_INUSE;
1018 }
1019
1020 /*! Options to pass to park_call_full */
1021 enum ast_park_call_options {
1022         /*! Provide ringing to the parked caller instead of music on hold */
1023         AST_PARK_OPT_RINGING =   (1 << 0),
1024         /*! Randomly choose a parking spot for the caller instead of choosing
1025          *  the first one that is available. */
1026         AST_PARK_OPT_RANDOMIZE = (1 << 1),
1027         /*! Do not announce the parking number */
1028         AST_PARK_OPT_SILENCE = (1 << 2),
1029 };
1030
1031 /*! Optional additional parking options when parking a call. */
1032 struct ast_park_call_args {
1033         /*! How long to wait in the parking lot before the call gets sent back
1034          *  to the specified return extension (or a best guess at where it came
1035          *  from if not explicitly specified). */
1036         int timeout;
1037         /*! An output parameter to store the parking space where the parked caller
1038          *  was placed. */
1039         int *extout;
1040         const char *orig_chan_name;
1041         const char *return_con;
1042         const char *return_ext;
1043         int return_pri;
1044         uint32_t flags;
1045         /*! Parked user that has already obtained a parking space */
1046         struct parkeduser *pu;
1047         /*! \brief Parkinglot to be parked in */
1048         struct ast_parkinglot *parkinglot;
1049 };
1050
1051 /*!
1052  * \internal
1053  * \brief Create a dynamic parking lot.
1054  *
1055  * \param name Dynamic parking lot name to create.
1056  * \param chan Channel to get dynamic parking lot parameters.
1057  *
1058  * \retval parkinglot on success.
1059  * \retval NULL on error.
1060  */
1061 static struct ast_parkinglot *create_dynamic_parkinglot(const char *name, struct ast_channel *chan)
1062 {
1063         const char *dyn_context;
1064         const char *dyn_exten;
1065         const char *dyn_range;
1066         const char *template_name;
1067         struct ast_parkinglot *template_parkinglot = NULL;
1068         struct ast_parkinglot *parkinglot;
1069         int dyn_start;
1070         int dyn_end;
1071
1072         ast_channel_lock(chan);
1073         template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
1074         dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
1075         dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
1076         dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
1077         ast_channel_unlock(chan);
1078
1079         if (!ast_strlen_zero(template_name)) {
1080                 template_parkinglot = find_parkinglot(template_name);
1081                 if (!template_parkinglot) {
1082                         ast_debug(1, "PARKINGDYNAMIC lot %s does not exist.\n",
1083                                 template_name);
1084                 } else if (template_parkinglot->cfg.is_invalid) {
1085                         ast_debug(1, "PARKINGDYNAMIC lot %s has invalid config.\n",
1086                                 template_name);
1087                         parkinglot_unref(template_parkinglot);
1088                         template_parkinglot = NULL;
1089                 }
1090         }
1091         if (!template_parkinglot) {
1092                 template_parkinglot = parkinglot_addref(default_parkinglot);
1093                 ast_debug(1, "Using default parking lot for template\n");
1094         }
1095
1096         parkinglot = copy_parkinglot(name, template_parkinglot);
1097         if (!parkinglot) {
1098                 ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
1099         } else {
1100                 /* Configure the dynamic parking lot. */
1101                 if (!ast_strlen_zero(dyn_context)) {
1102                         ast_copy_string(parkinglot->cfg.parking_con, dyn_context,
1103                                 sizeof(parkinglot->cfg.parking_con));
1104                 }
1105                 if (!ast_strlen_zero(dyn_exten)) {
1106                         ast_copy_string(parkinglot->cfg.parkext, dyn_exten,
1107                                 sizeof(parkinglot->cfg.parkext));
1108                 }
1109                 if (!ast_strlen_zero(dyn_range)) {
1110                         if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
1111                                 ast_log(LOG_WARNING,
1112                                         "Format for parking positions is a-b, where a and b are numbers\n");
1113                         } else if (dyn_end < dyn_start || dyn_start <= 0 || dyn_end <= 0) {
1114                                 ast_log(LOG_WARNING,
1115                                         "Format for parking positions is a-b, where a <= b\n");
1116                         } else {
1117                                 parkinglot->cfg.parking_start = dyn_start;
1118                                 parkinglot->cfg.parking_stop = dyn_end;
1119                         }
1120                 }
1121
1122                 /*
1123                  * Sanity check for dynamic parking lot configuration.
1124                  *
1125                  * XXX It may be desirable to instead check if the dynamic
1126                  * parking lot overlaps any existing lots like what is done for
1127                  * a reload.
1128                  */
1129                 if (!strcmp(parkinglot->cfg.parking_con, template_parkinglot->cfg.parking_con)) {
1130                         if (!strcmp(parkinglot->cfg.parkext, template_parkinglot->cfg.parkext)
1131                                 && parkinglot->cfg.parkext_exclusive) {
1132                                 ast_log(LOG_WARNING,
1133                                         "Parking lot '%s' conflicts with template parking lot '%s'!\n"
1134                                         "Change either PARKINGDYNCONTEXT or PARKINGDYNEXTEN.\n",
1135                                         parkinglot->name, template_parkinglot->name);
1136                         }
1137                         if ((template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_start
1138                                         && parkinglot->cfg.parking_start <= template_parkinglot->cfg.parking_stop)
1139                                 || (template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_stop
1140                                         && parkinglot->cfg.parking_stop <= template_parkinglot->cfg.parking_stop)
1141                                 || (parkinglot->cfg.parking_start < template_parkinglot->cfg.parking_start
1142                                         && template_parkinglot->cfg.parking_stop < parkinglot->cfg.parking_stop)) {
1143                                 ast_log(LOG_WARNING,
1144                                         "Parking lot '%s' parking spaces overlap template parking lot '%s'!\n"
1145                                         "Change PARKINGDYNPOS.\n",
1146                                         parkinglot->name, template_parkinglot->name);
1147                         }
1148                 }
1149
1150                 parkinglot_activate(parkinglot);
1151                 ao2_link(parkinglots, parkinglot);
1152         }
1153         parkinglot_unref(template_parkinglot);
1154
1155         return parkinglot;
1156 }
1157
1158 /*!
1159  * \internal
1160  * \brief Reserve a parking space in a parking lot for a call being parked.
1161  *
1162  * \param park_me Channel being parked.
1163  * \param parker Channel parking the call.
1164  * \param args Optional additional parking options when parking a call.
1165  *
1166  * \return Parked call descriptor or NULL if failed.
1167  * \note The parking lot list is locked if successful.
1168  */
1169 static struct parkeduser *park_space_reserve(struct ast_channel *park_me, struct ast_channel *parker, struct ast_park_call_args *args)
1170 {
1171         struct parkeduser *pu;
1172         int i;
1173         int parking_space = -1;
1174         const char *parkinglotname;
1175         const char *parkingexten;
1176         struct parkeduser *cur;
1177         struct ast_parkinglot *parkinglot = NULL;
1178
1179         if (args->parkinglot) {
1180                 parkinglot = parkinglot_addref(args->parkinglot);
1181                 parkinglotname = parkinglot->name;
1182         } else {
1183                 if (parker) {
1184                         parkinglotname = findparkinglotname(parker);
1185                 } else { /* parker was NULL, check park_me (ParkAndAnnounce / res_agi) */
1186                         parkinglotname = findparkinglotname(park_me);
1187                 }
1188                 if (!ast_strlen_zero(parkinglotname)) {
1189                         parkinglot = find_parkinglot(parkinglotname);
1190                 } else {
1191                         /* Parking lot is not specified, so use the default parking lot. */
1192                         ast_debug(4, "This could be an indication channel driver needs updating, using default lot.\n");
1193                         parkinglot = parkinglot_addref(default_parkinglot);
1194                 }
1195         }
1196
1197         /* Dynamically create parkinglot */
1198         if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
1199                 parkinglot = create_dynamic_parkinglot(parkinglotname, park_me);
1200         }
1201
1202         if (!parkinglot) {
1203                 ast_log(LOG_WARNING, "Parking lot not available to park %s.\n", park_me->name);
1204                 return NULL;
1205         }
1206
1207         ast_debug(1, "Parking lot: %s\n", parkinglot->name);
1208         if (parkinglot->disabled || parkinglot->cfg.is_invalid) {
1209                 ast_log(LOG_WARNING, "Parking lot %s is not in a useable state.\n",
1210                         parkinglot->name);
1211                 parkinglot_unref(parkinglot);
1212                 return NULL;
1213         }
1214
1215         /* Allocate memory for parking data */
1216         if (!(pu = ast_calloc(1, sizeof(*pu)))) {
1217                 parkinglot_unref(parkinglot);
1218                 return NULL;
1219         }
1220
1221         /* Lock parking list */
1222         AST_LIST_LOCK(&parkinglot->parkings);
1223
1224         /* Check for channel variable PARKINGEXTEN */
1225         parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(park_me, "PARKINGEXTEN"), ""));
1226         if (!ast_strlen_zero(parkingexten)) {
1227                 /*!
1228                  * \note The API forces us to specify a numeric parking slot, even
1229                  * though the architecture would tend to support non-numeric extensions
1230                  * (as are possible with SIP, for example).  Hence, we enforce that
1231                  * limitation here.  If extout was not numeric, we could permit
1232                  * arbitrary non-numeric extensions.
1233                  */
1234                 if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space <= 0) {
1235                         ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n",
1236                                 parkingexten);
1237                         AST_LIST_UNLOCK(&parkinglot->parkings);
1238                         parkinglot_unref(parkinglot);
1239                         ast_free(pu);
1240                         return NULL;
1241                 }
1242
1243                 if (parking_space < parkinglot->cfg.parking_start
1244                         || parkinglot->cfg.parking_stop < parking_space) {
1245                         /*
1246                          * Cannot allow park because parking lots are not setup for
1247                          * spaces outside of the lot.  (Things like dialplan hints don't
1248                          * exist for outside lot space.)
1249                          */
1250                         ast_log(LOG_WARNING, "PARKINGEXTEN=%d is not in %s (%d-%d).\n",
1251                                 parking_space, parkinglot->name, parkinglot->cfg.parking_start,
1252                                 parkinglot->cfg.parking_stop);
1253                         AST_LIST_UNLOCK(&parkinglot->parkings);
1254                         parkinglot_unref(parkinglot);
1255                         ast_free(pu);
1256                         return NULL;
1257                 }
1258
1259                 /* Check if requested parking space is in use. */
1260                 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
1261                         if (cur->parkingnum == parking_space) {
1262                                 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is already in use in %s\n",
1263                                         parking_space, parkinglot->name);
1264                                 AST_LIST_UNLOCK(&parkinglot->parkings);
1265                                 parkinglot_unref(parkinglot);
1266                                 ast_free(pu);
1267                                 return NULL;
1268                         }
1269                 }
1270         } else {
1271                 /* PARKINGEXTEN is empty, so find a usable extension in the lot to park the call */
1272                 int start; /* The first slot we look in the parkinglot. It can be randomized. */
1273                 int start_checked = 0; /* flag raised once the first slot is checked */
1274
1275                 /* If using randomize mode, set start to random position on parking range */
1276                 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
1277                         start = ast_random() % (parkinglot->cfg.parking_stop - parkinglot->cfg.parking_start + 1);
1278                         start += parkinglot->cfg.parking_start;
1279                 } else if (parkinglot->cfg.parkfindnext
1280                         && parkinglot->cfg.parking_start <= parkinglot->next_parking_space
1281                         && parkinglot->next_parking_space <= parkinglot->cfg.parking_stop) {
1282                         /* Start looking with the next parking space in the lot. */
1283                         start = parkinglot->next_parking_space;
1284                 } else {
1285                         /* Otherwise, just set it to the start position. */
1286                         start = parkinglot->cfg.parking_start;
1287                 }
1288
1289                 /* free parking extension linear search: O(n^2) */
1290                 for (i = start; ; i++) {
1291                         /* If we are past the end, wrap around to the first parking slot*/
1292                         if (i == parkinglot->cfg.parking_stop + 1) {
1293                                 i = parkinglot->cfg.parking_start;
1294                         }
1295
1296                         if (i == start) {
1297                                 /* At this point, if start_checked, we've exhausted all the possible slots. */
1298                                 if (start_checked) {
1299                                         break;
1300                                 } else {
1301                                         start_checked = 1;
1302                                 }
1303                         }
1304
1305                         /* Search the list of parked calls already in use for i. If we find it, it's in use. */
1306                         AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
1307                                 if (cur->parkingnum == i) {
1308                                         break;
1309                                 }
1310                         }
1311                         if (!cur) {
1312                                 /* We found a parking space. */
1313                                 parking_space = i;
1314                                 break;
1315                         }
1316                 }
1317                 if (parking_space == -1) {
1318                         /* We did not find a parking space.  Lot is full. */
1319                         ast_log(LOG_WARNING, "No more parking spaces in %s\n", parkinglot->name);
1320                         AST_LIST_UNLOCK(&parkinglot->parkings);
1321                         parkinglot_unref(parkinglot);
1322                         ast_free(pu);
1323                         return NULL;
1324                 }
1325         }
1326
1327         /* Prepare for next parking space search. */
1328         parkinglot->next_parking_space = parking_space + 1;
1329
1330         snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
1331         pu->notquiteyet = 1;
1332         pu->parkingnum = parking_space;
1333         pu->parkinglot = parkinglot;
1334         AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
1335
1336         return pu;
1337 }
1338
1339 /* Park a call */
1340 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
1341 {
1342         struct parkeduser *pu = args->pu;
1343         const char *event_from;
1344         char app_data[AST_MAX_EXTENSION + AST_MAX_CONTEXT];
1345
1346         if (pu == NULL) {
1347                 args->pu = pu = park_space_reserve(chan, peer, args);
1348                 if (pu == NULL) {
1349                         return -1;
1350                 }
1351         }
1352
1353         chan->appl = "Parked Call";
1354         chan->data = NULL;
1355
1356         pu->chan = chan;
1357
1358         /* Put the parked channel on hold if we have two different channels */
1359         if (chan != peer) {
1360                 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
1361                         pu->hold_method = AST_CONTROL_RINGING;
1362                         ast_indicate(pu->chan, AST_CONTROL_RINGING);
1363                 } else {
1364                         pu->hold_method = AST_CONTROL_HOLD;
1365                         ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
1366                                 S_OR(pu->parkinglot->cfg.mohclass, NULL),
1367                                 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
1368                 }
1369         }
1370         
1371         pu->start = ast_tvnow();
1372         pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->cfg.parkingtime;
1373         if (args->extout)
1374                 *(args->extout) = pu->parkingnum;
1375
1376         if (peer) { 
1377                 /*
1378                  * This is so ugly that it hurts, but implementing
1379                  * get_base_channel() on local channels could have ugly side
1380                  * effects.  We could have
1381                  * transferer<->local,1<->local,2<->parking and we need the
1382                  * callback name to be that of transferer.  Since local,1/2 have
1383                  * the same name we can be tricky and just grab the bridged
1384                  * channel from the other side of the local.
1385                  */
1386                 if (!strcasecmp(peer->tech->type, "Local")) {
1387                         struct ast_channel *tmpchan, *base_peer;
1388                         char other_side[AST_CHANNEL_NAME];
1389                         char *c;
1390
1391                         ast_copy_string(other_side, S_OR(args->orig_chan_name, peer->name), sizeof(other_side));
1392                         if ((c = strrchr(other_side, ';'))) {
1393                                 *++c = '1';
1394                         }
1395                         if ((tmpchan = ast_channel_get_by_name(other_side))) {
1396                                 ast_channel_lock(tmpchan);
1397                                 if ((base_peer = ast_bridged_channel(tmpchan))) {
1398                                         ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
1399                                 }
1400                                 ast_channel_unlock(tmpchan);
1401                                 tmpchan = ast_channel_unref(tmpchan);
1402                         }
1403                 } else {
1404                         ast_copy_string(pu->peername, S_OR(args->orig_chan_name, peer->name), sizeof(pu->peername));
1405                 }
1406         }
1407
1408         /*
1409          * Remember what had been dialed, so that if the parking
1410          * expires, we try to come back to the same place
1411          */
1412         pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
1413
1414         /*
1415          * If extension has options specified, they override all other
1416          * possibilities such as the returntoorigin flag and transferred
1417          * context.  Information on extension options is lost here, so
1418          * we set a flag
1419          */
1420         ast_copy_string(pu->context, 
1421                 S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)), 
1422                 sizeof(pu->context));
1423         ast_copy_string(pu->exten, 
1424                 S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)), 
1425                 sizeof(pu->exten));
1426         pu->priority = args->return_pri ? args->return_pri : 
1427                 (chan->macropriority ? chan->macropriority : chan->priority);
1428
1429         /*
1430          * If parking a channel directly, don't quite yet get parking
1431          * running on it.  All parking lot entries are put into the
1432          * parking lot with notquiteyet on.
1433          */
1434         if (peer != chan) {
1435                 pu->notquiteyet = 0;
1436         }
1437
1438         /* Wake up the (presumably select()ing) thread */
1439         pthread_kill(parking_thread, SIGURG);
1440         ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n",
1441                 pu->chan->name, pu->parkingnum, pu->parkinglot->name,
1442                 pu->context, pu->exten, pu->priority, (pu->parkingtime / 1000));
1443
1444         ast_cel_report_event(pu->chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
1445
1446         if (peer) {
1447                 event_from = peer->name;
1448         } else {
1449                 event_from = pbx_builtin_getvar_helper(chan, "BLINDTRANSFER");
1450         }
1451
1452         ast_manager_event(pu->chan, EVENT_FLAG_CALL, "ParkedCall",
1453                 "Exten: %s\r\n"
1454                 "Channel: %s\r\n"
1455                 "Parkinglot: %s\r\n"
1456                 "From: %s\r\n"
1457                 "Timeout: %ld\r\n"
1458                 "CallerIDNum: %s\r\n"
1459                 "CallerIDName: %s\r\n"
1460                 "ConnectedLineNum: %s\r\n"
1461                 "ConnectedLineName: %s\r\n"
1462                 "Uniqueid: %s\r\n",
1463                 pu->parkingexten, pu->chan->name, pu->parkinglot->name, event_from ? event_from : "",
1464                 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
1465                 S_COR(pu->chan->caller.id.number.valid, pu->chan->caller.id.number.str, "<unknown>"),
1466                 S_COR(pu->chan->caller.id.name.valid, pu->chan->caller.id.name.str, "<unknown>"),
1467                 S_COR(pu->chan->connected.id.number.valid, pu->chan->connected.id.number.str, "<unknown>"),
1468                 S_COR(pu->chan->connected.id.name.valid, pu->chan->connected.id.name.str, "<unknown>"),
1469                 pu->chan->uniqueid
1470                 );
1471
1472         if (peer && adsipark && ast_adsi_available(peer)) {
1473                 adsi_announce_park(peer, pu->parkingexten);     /* Only supports parking numbers */
1474                 ast_adsi_unload_session(peer);
1475         }
1476
1477         snprintf(app_data, sizeof(app_data), "%s,%s", pu->parkingexten,
1478                 pu->parkinglot->name);
1479         if (ast_add_extension(pu->parkinglot->cfg.parking_con, 1, pu->parkingexten, 1,
1480                 NULL, NULL, parkedcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
1481                 ast_log(LOG_ERROR, "Could not create parked call exten: %s@%s\n",
1482                         pu->parkingexten, pu->parkinglot->cfg.parking_con);
1483         } else {
1484                 notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con, AST_DEVICE_INUSE);
1485         }
1486
1487         AST_LIST_UNLOCK(&pu->parkinglot->parkings);
1488
1489         /* Only say number if it's a number and the channel hasn't been masqueraded away */
1490         if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE)
1491                 && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
1492                 /*
1493                  * If a channel is masqueraded into peer while playing back the
1494                  * parking space number do not continue playing it back.  This
1495                  * is the case if an attended transfer occurs.
1496                  */
1497                 ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
1498                 /* Tell the peer channel the number of the parking space */
1499                 ast_say_digits(peer, pu->parkingnum, "", peer->language);
1500                 ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
1501         }
1502         if (peer == chan) { /* pu->notquiteyet = 1 */
1503                 /* Wake up parking thread if we're really done */
1504                 pu->hold_method = AST_CONTROL_HOLD;
1505                 ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
1506                         S_OR(pu->parkinglot->cfg.mohclass, NULL),
1507                         !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
1508                 pu->notquiteyet = 0;
1509                 pthread_kill(parking_thread, SIGURG);
1510         }
1511         return 0;
1512 }
1513
1514 /*! \brief Park a call */
1515 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, const char *parkexten, int *extout)
1516 {
1517         struct ast_park_call_args args = {
1518                 .timeout = timeout,
1519                 .extout = extout,
1520         };
1521
1522         return park_call_full(chan, peer, &args);
1523 }
1524
1525 /*!
1526  * \param rchan the real channel to be parked
1527  * \param peer the channel to have the parking read to.
1528  * \param timeout is a timeout in milliseconds
1529  * \param extout is a parameter to an int that will hold the parked location, or NULL if you want.
1530  * \param play_announcement TRUE if to play which parking space call parked in to peer.
1531  * \param args Optional additional parking options when parking a call.
1532  *
1533  * \retval 0 on success.
1534  * \retval -1 on failure.
1535  */
1536 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)
1537 {
1538         struct ast_channel *chan;
1539         struct ast_park_call_args park_args = {0,};
1540
1541         if (!args) {
1542                 args = &park_args;
1543                 args->timeout = timeout;
1544                 args->extout = extout;
1545         }
1546
1547         /* Make a new, channel that we'll use to masquerade in the real one */
1548         chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten,
1549                 rchan->context, rchan->linkedid, rchan->amaflags, "Parked/%s", rchan->name);
1550         if (!chan) {
1551                 ast_log(LOG_WARNING, "Unable to create parked channel\n");
1552                 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1553                         if (peer == rchan) {
1554                                 /* Only have one channel to worry about. */
1555                                 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1556                         } else if (peer) {
1557                                 /* Have two different channels to worry about. */
1558                                 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1559                         }
1560                 }
1561                 return -1;
1562         }
1563
1564         args->pu = park_space_reserve(rchan, peer, args);
1565         if (!args->pu) {
1566                 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
1567                 ast_hangup(chan);
1568                 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1569                         if (peer == rchan) {
1570                                 /* Only have one channel to worry about. */
1571                                 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1572                         } else if (peer) {
1573                                 /* Have two different channels to worry about. */
1574                                 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1575                         }
1576                 }
1577                 return -1;
1578         }
1579
1580         /* Make formats okay */
1581         chan->readformat = rchan->readformat;
1582         chan->writeformat = rchan->writeformat;
1583         ast_channel_masquerade(chan, rchan);
1584
1585         /* Setup the extensions and such */
1586         set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
1587
1588         /* Setup the macro extension and such */
1589         ast_copy_string(chan->macrocontext,rchan->macrocontext,sizeof(chan->macrocontext));
1590         ast_copy_string(chan->macroexten,rchan->macroexten,sizeof(chan->macroexten));
1591         chan->macropriority = rchan->macropriority;
1592
1593         /* Manually do the masquerade to make sure it is complete. */
1594         ast_do_masquerade(chan);
1595
1596         if (peer == rchan) {
1597                 peer = chan;
1598         }
1599
1600         if (peer && (!play_announcement && args == &park_args)) {
1601                 args->orig_chan_name = ast_strdupa(peer->name);
1602         }
1603
1604         /* parking space reserved, return code check unnecessary */
1605         park_call_full(chan, peer, args);
1606
1607         return 0;
1608 }
1609
1610 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1611 {
1612         return masq_park_call(rchan, peer, timeout, extout, 0, NULL);
1613 }
1614
1615 /*!
1616  * \brief Park call via masqueraded channel and announce parking spot on peer channel.
1617  *
1618  * \param rchan the real channel to be parked
1619  * \param peer the channel to have the parking read to.
1620  * \param args Optional additional parking options when parking a call.
1621  *
1622  * \retval 0 on success.
1623  * \retval -1 on failure.
1624  */
1625 static int masq_park_call_announce(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
1626 {
1627         return masq_park_call(rchan, peer, 0, NULL, 1, args);
1628 }
1629
1630 static int finishup(struct ast_channel *chan)
1631 {
1632         ast_indicate(chan, AST_CONTROL_UNHOLD);
1633
1634         return ast_autoservice_stop(chan);
1635 }
1636
1637 /*!
1638  * \internal
1639  * \brief Builtin transfer park call helper.
1640  *
1641  * \param park_me Channel to be parked.
1642  * \param parker Channel parking the call.
1643  * \param park_exten Parking lot dialplan access ramp extension.
1644  *
1645  * \note Assumes park_me is on hold and in autoservice.
1646  *
1647  * \retval -1 on successful park.
1648  * \retval -1 on park_me hangup.
1649  * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
1650  */
1651 static int xfer_park_call_helper(struct ast_channel *park_me, struct ast_channel *parker, struct ast_exten *park_exten)
1652 {
1653         char *parse;
1654         const char *app_data;
1655         const char *pl_name;
1656         struct ast_park_call_args args = { 0, };
1657         struct park_app_args app_args;
1658         int res;
1659
1660         app_data = ast_get_extension_app_data(park_exten);
1661         if (!app_data) {
1662                 app_data = "";
1663         }
1664         parse = ast_strdupa(app_data);
1665         AST_STANDARD_APP_ARGS(app_args, parse);
1666
1667         /* Find the parking lot */
1668         if (!ast_strlen_zero(app_args.pl_name)) {
1669                 pl_name = app_args.pl_name;
1670         } else {
1671                 pl_name = findparkinglotname(parker);
1672         }
1673         if (ast_strlen_zero(pl_name)) {
1674                 /* Parking lot is not specified, so use the default parking lot. */
1675                 args.parkinglot = parkinglot_addref(default_parkinglot);
1676         } else {
1677                 args.parkinglot = find_parkinglot(pl_name);
1678                 if (!args.parkinglot && parkeddynamic) {
1679                         args.parkinglot = create_dynamic_parkinglot(pl_name, park_me);
1680                 }
1681         }
1682
1683         if (args.parkinglot) {
1684                 /* Park the call */
1685                 res = finishup(park_me);
1686                 if (res) {
1687                         /* park_me hungup on us. */
1688                         parkinglot_unref(args.parkinglot);
1689                         return -1;
1690                 }
1691                 res = masq_park_call_announce(park_me, parker, &args);
1692                 parkinglot_unref(args.parkinglot);
1693         } else {
1694                 /* Parking failed because parking lot does not exist. */
1695                 if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
1696                         ast_stream_and_wait(parker, "pbx-parkingfailed", "");
1697                 }
1698                 finishup(park_me);
1699                 res = -1;
1700         }
1701
1702         return res ? AST_FEATURE_RETURN_SUCCESS : -1;
1703 }
1704
1705 /*!
1706  * \brief set caller and callee according to the direction
1707  * \param caller, callee, peer, chan, sense
1708  *
1709  * Detect who triggered feature and set callee/caller variables accordingly
1710  */
1711 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
1712         struct ast_channel *peer, struct ast_channel *chan, int sense)
1713 {
1714         if (sense == FEATURE_SENSE_PEER) {
1715                 *caller = peer;
1716                 *callee = chan;
1717         } else {
1718                 *callee = peer;
1719                 *caller = chan;
1720         }
1721 }
1722
1723 /*!
1724  * \brief support routing for one touch call parking
1725  * \param chan channel parking call
1726  * \param peer channel to be parked
1727  * \param config unsed
1728  * \param code unused
1729  * \param sense feature options
1730  * \param data unused
1731  *
1732  * \retval -1 on successful park.
1733  * \retval -1 on chan hangup.
1734  * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
1735  */
1736 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1737 {
1738         struct ast_channel *parker;
1739         struct ast_channel *parkee;
1740
1741         /*
1742          * We used to set chan's exten and priority to "s" and 1 here,
1743          * but this generates (in some cases) an invalid extension, and
1744          * if "s" exists, could errantly cause execution of extensions
1745          * you don't expect.  It makes more sense to let nature take its
1746          * course when chan finishes, and let the pbx do its thing and
1747          * hang up when the park is over.
1748          */
1749
1750         /* Answer if call is not up */
1751         if (chan->_state != AST_STATE_UP) {
1752                 /*
1753                  * XXX Why are we doing this?  Both of the channels should be up
1754                  * since you cannot do DTMF features unless you are bridged.
1755                  */
1756                 if (ast_answer(chan)) {
1757                         return -1;
1758                 }
1759
1760                 /* Sleep to allow VoIP streams to settle down */
1761                 if (ast_safe_sleep(chan, 1000)) {
1762                         return -1;
1763                 }
1764         }
1765
1766         /* one direction used to call park_call.... */
1767         set_peers(&parker, &parkee, peer, chan, sense);
1768         return masq_park_call_announce(parkee, parker, NULL)
1769                 ? AST_FEATURE_RETURN_SUCCESS : -1;
1770 }
1771
1772 /*!
1773  * \internal
1774  * \brief Play file to specified channel.
1775  *
1776  * \param play_to Channel to play audiofile to.
1777  * \param other Channel to put in autoservice while playing file.
1778  * \param msg Descriptive name of message type being played.
1779  * \param audiofile Audio file to play.
1780  *
1781  * \retval 0 on success.
1782  * \retval -1 on error. (Couldn't play file, a channel hung up,...)
1783  */
1784 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile)
1785 {
1786         /* Put other channel in autoservice. */
1787         if (ast_autoservice_start(other)) {
1788                 return -1;
1789         }
1790         ast_autoservice_ignore(other, AST_FRAME_DTMF_BEGIN);
1791         ast_autoservice_ignore(other, AST_FRAME_DTMF_END);
1792         if (ast_stream_and_wait(play_to, audiofile, "")) {
1793                 ast_log(LOG_WARNING, "Failed to play %s '%s'!\n", msg, audiofile);
1794                 ast_autoservice_stop(other);
1795                 return -1;
1796         }
1797         if (ast_autoservice_stop(other)) {
1798                 return -1;
1799         }
1800         return 0;
1801 }
1802
1803 /*!
1804  * \internal
1805  * \brief Play file to specified channels.
1806  *
1807  * \param left Channel on left to play file.
1808  * \param right Channel on right to play file.
1809  * \param which Play file on indicated channels: which < 0 play left, which == 0 play both, which > 0 play right
1810  * \param msg Descriptive name of message type being played.
1811  * \param audiofile Audio file to play to channels.
1812  *
1813  * \note Plays file to the indicated channels in turn so please
1814  * don't use this for very long messages.
1815  *
1816  * \retval 0 on success.
1817  * \retval -1 on error. (Couldn't play file, channel hung up,...)
1818  */
1819 static int play_message_to_chans(struct ast_channel *left, struct ast_channel *right, int which, const char *msg, const char *audiofile)
1820 {
1821         /* First play the file to the left channel if requested. */
1822         if (which <= 0 && play_message_on_chan(left, right, msg, audiofile)) {
1823                 return -1;
1824         }
1825
1826         /* Then play the file to the right channel if requested. */
1827         if (which >= 0 && play_message_on_chan(right, left, msg, audiofile)) {
1828                 return -1;
1829         }
1830
1831         return 0;
1832 }
1833
1834 /*!
1835  * \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
1836  * other channel during the message, so please don't use this for very long messages
1837  */
1838 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
1839 {
1840         return play_message_to_chans(caller_chan, callee_chan, 0, "automon message",
1841                 audiofile);
1842 }
1843
1844 /*!
1845  * \brief Monitor a channel by DTMF
1846  * \param chan channel requesting monitor
1847  * \param peer channel to be monitored
1848  * \param config
1849  * \param code
1850  * \param sense feature options
1851  *
1852  * \param data
1853  * Check monitor app enabled, setup channels, both caller/callee chans not null
1854  * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
1855  * \retval AST_FEATURE_RETURN_SUCCESS on success.
1856  * \retval -1 on error.
1857  */
1858 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1859 {
1860         char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1861         int x = 0;
1862         size_t len;
1863         struct ast_channel *caller_chan, *callee_chan;
1864         const char *automon_message_start = NULL;
1865         const char *automon_message_stop = NULL;
1866
1867         if (!monitor_ok) {
1868                 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1869                 return -1;
1870         }
1871
1872         if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
1873                 monitor_ok = 0;
1874                 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1875                 return -1;
1876         }
1877
1878         set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1879         if (caller_chan) {      /* Find extra messages */
1880                 automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
1881                 automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
1882         }
1883
1884         if (!ast_strlen_zero(courtesytone)) {   /* Play courtesy tone if configured */
1885                 if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
1886                         return -1;
1887                 }
1888         }
1889         
1890         if (callee_chan->monitor) {
1891                 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
1892                 if (!ast_strlen_zero(automon_message_stop)) {
1893                         play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
1894                 }
1895                 callee_chan->monitor->stop(callee_chan, 1);
1896                 return AST_FEATURE_RETURN_SUCCESS;
1897         }
1898
1899         if (caller_chan && callee_chan) {
1900                 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
1901                 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
1902                 const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
1903
1904                 if (!touch_format)
1905                         touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
1906
1907                 if (!touch_monitor)
1908                         touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
1909         
1910                 if (!touch_monitor_prefix)
1911                         touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
1912         
1913                 if (touch_monitor) {
1914                         len = strlen(touch_monitor) + 50;
1915                         args = alloca(len);
1916                         touch_filename = alloca(len);
1917                         snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
1918                         snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
1919                 } else {
1920                         caller_chan_id = ast_strdupa(S_COR(caller_chan->caller.id.number.valid,
1921                                 caller_chan->caller.id.number.str, caller_chan->name));
1922                         callee_chan_id = ast_strdupa(S_COR(callee_chan->caller.id.number.valid,
1923                                 callee_chan->caller.id.number.str, callee_chan->name));
1924                         len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
1925                         args = alloca(len);
1926                         touch_filename = alloca(len);
1927                         snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
1928                         snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
1929                 }
1930
1931                 for(x = 0; x < strlen(args); x++) {
1932                         if (args[x] == '/')
1933                                 args[x] = '-';
1934                 }
1935                 
1936                 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
1937
1938                 pbx_exec(callee_chan, monitor_app, args);
1939                 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
1940                 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
1941
1942                 if (!ast_strlen_zero(automon_message_start)) {  /* Play start message for both channels */
1943                         play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
1944                 }
1945         
1946                 return AST_FEATURE_RETURN_SUCCESS;
1947         }
1948         
1949         ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");   
1950         return -1;
1951 }
1952
1953 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1954 {
1955         char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1956         int x = 0;
1957         size_t len;
1958         struct ast_channel *caller_chan, *callee_chan;
1959         const char *mixmonitor_spy_type = "MixMonitor";
1960         int count = 0;
1961
1962         if (!mixmonitor_ok) {
1963                 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
1964                 return -1;
1965         }
1966
1967         if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
1968                 mixmonitor_ok = 0;
1969                 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
1970                 return -1;
1971         }
1972
1973         set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1974
1975         if (!ast_strlen_zero(courtesytone)) {
1976                 if (ast_autoservice_start(callee_chan))
1977                         return -1;
1978                 ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
1979                 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
1980                         ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
1981                         ast_autoservice_stop(callee_chan);
1982                         return -1;
1983                 }
1984                 if (ast_autoservice_stop(callee_chan))
1985                         return -1;
1986         }
1987
1988         ast_channel_lock(callee_chan);
1989         count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
1990         ast_channel_unlock(callee_chan);
1991
1992         /* This means a mixmonitor is attached to the channel, running or not is unknown. */
1993         if (count > 0) {
1994                 
1995                 ast_verb(3, "User hit '%s' to stop recording call.\n", code);
1996
1997                 /* Make sure they are running */
1998                 ast_channel_lock(callee_chan);
1999                 count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
2000                 ast_channel_unlock(callee_chan);
2001                 if (count > 0) {
2002                         if (!stopmixmonitor_ok) {
2003                                 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
2004                                 return -1;
2005                         }
2006                         if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
2007                                 stopmixmonitor_ok = 0;
2008                                 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
2009                                 return -1;
2010                         } else {
2011                                 pbx_exec(callee_chan, stopmixmonitor_app, "");
2012                                 return AST_FEATURE_RETURN_SUCCESS;
2013                         }
2014                 }
2015                 
2016                 ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");      
2017         }                       
2018
2019         if (caller_chan && callee_chan) {
2020                 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
2021                 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
2022
2023                 if (!touch_format)
2024                         touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
2025
2026                 if (!touch_monitor)
2027                         touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
2028
2029                 if (touch_monitor) {
2030                         len = strlen(touch_monitor) + 50;
2031                         args = alloca(len);
2032                         touch_filename = alloca(len);
2033                         snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
2034                         snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
2035                 } else {
2036                         caller_chan_id = ast_strdupa(S_COR(caller_chan->caller.id.number.valid,
2037                                 caller_chan->caller.id.number.str, caller_chan->name));
2038                         callee_chan_id = ast_strdupa(S_COR(callee_chan->caller.id.number.valid,
2039                                 callee_chan->caller.id.number.str, callee_chan->name));
2040                         len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
2041                         args = alloca(len);
2042                         touch_filename = alloca(len);
2043                         snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
2044                         snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
2045                 }
2046
2047                 for( x = 0; x < strlen(args); x++) {
2048                         if (args[x] == '/')
2049                                 args[x] = '-';
2050                 }
2051
2052                 ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
2053
2054                 pbx_exec(callee_chan, mixmonitor_app, args);
2055                 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
2056                 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
2057                 return AST_FEATURE_RETURN_SUCCESS;
2058         
2059         }
2060
2061         ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
2062         return -1;
2063
2064 }
2065
2066 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2067 {
2068         ast_verb(4, "User hit '%s' to disconnect call.\n", code);
2069         return AST_FEATURE_RETURN_HANGUP;
2070 }
2071
2072 /*!
2073  * \brief Find the context for the transfer
2074  * \param transferer
2075  * \param transferee
2076  * 
2077  * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
2078  * \return a context string
2079  */
2080 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
2081 {
2082         const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
2083         if (ast_strlen_zero(s)) {
2084                 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
2085         }
2086         if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
2087                 s = transferer->macrocontext;
2088         }
2089         if (ast_strlen_zero(s)) {
2090                 s = transferer->context;
2091         }
2092         return s;  
2093 }
2094
2095 /*!
2096  * \brief Blind transfer user to another extension
2097  * \param chan channel to be transfered
2098  * \param peer channel initiated blind transfer
2099  * \param config
2100  * \param code
2101  * \param data
2102  * \param sense  feature options
2103  * 
2104  * Place chan on hold, check if transferred to parkinglot extension,
2105  * otherwise check extension exists and transfer caller.
2106  * \retval AST_FEATURE_RETURN_SUCCESS.
2107  * \retval -1 on failure.
2108  */
2109 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2110 {
2111         struct ast_channel *transferer;
2112         struct ast_channel *transferee;
2113         struct ast_exten *park_exten;
2114         const char *transferer_real_context;
2115         char xferto[256] = "";
2116         int res;
2117
2118         set_peers(&transferer, &transferee, peer, chan, sense);
2119         transferer_real_context = real_ctx(transferer, transferee);
2120
2121         /* Start autoservice on transferee while we talk to the transferer */
2122         ast_autoservice_start(transferee);
2123         ast_indicate(transferee, AST_CONTROL_HOLD);
2124
2125         /* Transfer */
2126         res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
2127         if (res < 0) {
2128                 finishup(transferee);
2129                 return -1; /* error ? */
2130         }
2131         if (res > 0) { /* If they've typed a digit already, handle it */
2132                 xferto[0] = (char) res;
2133         }
2134
2135         res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
2136         if (res < 0) {  /* hangup or error, (would be 0 for invalid and 1 for valid) */
2137                 finishup(transferee);
2138                 return -1;
2139         }
2140         if (res == 0) {
2141                 if (xferto[0]) {
2142                         ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
2143                                 xferto, transferer_real_context);
2144                 } else {
2145                         /* Does anyone care about this case? */
2146                         ast_log(LOG_WARNING, "No digits dialed.\n");
2147                 }
2148                 ast_stream_and_wait(transferer, "pbx-invalid", "");
2149                 finishup(transferee);
2150                 return AST_FEATURE_RETURN_SUCCESS;
2151         }
2152
2153         park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
2154         if (park_exten) {
2155                 /* We are transfering the transferee to a parking lot. */
2156                 return xfer_park_call_helper(transferee, transferer, park_exten);
2157         }
2158
2159         /* Do blind transfer. */
2160         ast_cel_report_event(transferer, AST_CEL_BLINDTRANSFER, NULL, xferto, transferee);
2161         pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
2162         pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
2163         res = finishup(transferee);
2164         if (!transferer->cdr) { /* this code should never get called (in a perfect world) */
2165                 transferer->cdr = ast_cdr_alloc();
2166                 if (transferer->cdr) {
2167                         ast_cdr_init(transferer->cdr, transferer); /* initialize our channel's cdr */
2168                         ast_cdr_start(transferer->cdr);
2169                 }
2170         }
2171         if (transferer->cdr) {
2172                 struct ast_cdr *swap = transferer->cdr;
2173
2174                 ast_debug(1,
2175                         "transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
2176                         transferer->name, transferee->name, transferer->cdr->lastapp,
2177                         transferer->cdr->lastdata, transferer->cdr->channel,
2178                         transferer->cdr->dstchannel);
2179                 ast_debug(1, "TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
2180                         transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel,
2181                         transferee->cdr->dstchannel);
2182                 ast_debug(1, "transferer_real_context=%s; xferto=%s\n",
2183                         transferer_real_context, xferto);
2184                 /* swap cdrs-- it will save us some time & work */
2185                 transferer->cdr = transferee->cdr;
2186                 transferee->cdr = swap;
2187         }
2188         if (!transferee->pbx) {
2189                 /* Doh!  Use our handy async_goto functions */
2190                 ast_verb(3, "Transferring %s to '%s' (context %s) priority 1\n",
2191                         transferee->name, xferto, transferer_real_context);
2192                 if (ast_async_goto(transferee, transferer_real_context, xferto, 1)) {
2193                         ast_log(LOG_WARNING, "Async goto failed :-(\n");
2194                 }
2195         } else {
2196                 /* Set the channel's new extension, since it exists, using transferer context */
2197                 ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
2198                 ast_debug(1,
2199                         "ABOUT TO AST_ASYNC_GOTO, have a pbx... set HANGUP_DONT on chan=%s\n",
2200                         transferee->name);
2201                 if (ast_channel_connected_line_macro(transferee, transferer, &transferer->connected, 1, 0)) {
2202                         ast_channel_update_connected_line(transferer, &transferer->connected, NULL);
2203                 }
2204                 set_c_e_p(transferee, transferer_real_context, xferto, 0);
2205         }
2206         check_goto_on_transfer(transferer);
2207         return res;
2208 }
2209
2210 /*!
2211  * \brief make channels compatible
2212  * \param c
2213  * \param newchan
2214  * \retval 0 on success.
2215  * \retval -1 on failure.
2216  */
2217 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
2218 {
2219         if (ast_channel_make_compatible(c, newchan) < 0) {
2220                 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
2221                         c->name, newchan->name);
2222                 ast_hangup(newchan);
2223                 return -1;
2224         }
2225         return 0;
2226 }
2227
2228 /*!
2229  * \internal
2230  * \brief Builtin attended transfer failed cleanup.
2231  * \since 10.0
2232  *
2233  * \param transferee Party A in the transfer.
2234  * \param transferer Party B in the transfer.
2235  * \param connected_line Saved connected line info about party A.
2236  *
2237  * \note The connected_line data is freed.
2238  *
2239  * \return Nothing
2240  */
2241 static void atxfer_fail_cleanup(struct ast_channel *transferee, struct ast_channel *transferer, struct ast_party_connected_line *connected_line)
2242 {
2243         finishup(transferee);
2244
2245         /*
2246          * Restore party B connected line info about party A.
2247          *
2248          * Party B was the caller to party C and is the last known mode
2249          * for party B.
2250          */
2251         if (ast_channel_connected_line_macro(transferee, transferer, connected_line, 1, 0)) {
2252                 ast_channel_update_connected_line(transferer, connected_line, NULL);
2253         }
2254         ast_party_connected_line_free(connected_line);
2255 }
2256
2257 /*!
2258  * \brief Attended transfer
2259  * \param chan transfered user
2260  * \param peer person transfering call
2261  * \param config
2262  * \param code
2263  * \param sense feature options
2264  *
2265  * \param data
2266  * Get extension to transfer to, if you cannot generate channel (or find extension)
2267  * return to host channel. After called channel answered wait for hangup of transferer,
2268  * bridge call between transfer peer (taking them off hold) to attended transfer channel.
2269  *
2270  * \return -1 on failure
2271  */
2272 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2273 {
2274         struct ast_channel *transferer;/* Party B */
2275         struct ast_channel *transferee;/* Party A */
2276         struct ast_exten *park_exten;
2277         const char *transferer_real_context;
2278         char xferto[256] = "";
2279         int res;
2280         int outstate=0;
2281         struct ast_channel *newchan;
2282         struct ast_channel *xferchan;
2283         struct ast_bridge_thread_obj *tobj;
2284         struct ast_bridge_config bconfig;
2285         int l;
2286         struct ast_party_connected_line connected_line;
2287         struct ast_datastore *features_datastore;
2288         struct ast_dial_features *dialfeatures = NULL;
2289         char *transferer_tech;
2290         char *transferer_name;
2291         char *transferer_name_orig;
2292         char *dash;
2293
2294         ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
2295         set_peers(&transferer, &transferee, peer, chan, sense);
2296         transferer_real_context = real_ctx(transferer, transferee);
2297
2298         /* Start autoservice on transferee while we talk to the transferer */
2299         ast_autoservice_start(transferee);
2300         ast_indicate(transferee, AST_CONTROL_HOLD);
2301
2302         /* Transfer */
2303         res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
2304         if (res < 0) {
2305                 finishup(transferee);
2306                 return -1;
2307         }
2308         if (res > 0) { /* If they've typed a digit already, handle it */
2309                 xferto[0] = (char) res;
2310         }
2311
2312         /* this is specific of atxfer */
2313         res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
2314         if (res < 0) {  /* hangup or error, (would be 0 for invalid and 1 for valid) */
2315                 finishup(transferee);
2316                 return -1;
2317         }
2318         l = strlen(xferto);
2319         if (res == 0) {
2320                 if (l) {
2321                         ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
2322                                 xferto, transferer_real_context);
2323                 } else {
2324                         /* Does anyone care about this case? */
2325                         ast_log(LOG_WARNING, "No digits dialed for atxfer.\n");
2326                 }
2327                 ast_stream_and_wait(transferer, "pbx-invalid", "");
2328                 finishup(transferee);
2329                 return AST_FEATURE_RETURN_SUCCESS;
2330         }
2331
2332         park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
2333         if (park_exten) {
2334                 /* We are transfering the transferee to a parking lot. */
2335                 return xfer_park_call_helper(transferee, transferer, park_exten);
2336         }
2337
2338         /* Append context to dialed transfer number. */
2339         snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);
2340
2341         /* If we are performing an attended transfer and we have two channels involved then
2342            copy sound file information to play upon attended transfer completion */
2343         if (transferee) {
2344                 const char *chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
2345                 const char *chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
2346
2347                 if (!ast_strlen_zero(chan1_attended_sound)) {
2348                         pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
2349                 }
2350                 if (!ast_strlen_zero(chan2_attended_sound)) {
2351                         pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
2352                 }
2353         }
2354
2355         /* Extract redial transferer information from the channel name. */
2356         transferer_name_orig = ast_strdupa(transferer->name);
2357         transferer_name = ast_strdupa(transferer_name_orig);
2358         transferer_tech = strsep(&transferer_name, "/");
2359         dash = strrchr(transferer_name, '-');
2360         if (dash) {
2361                 /* Trim off channel name sequence/serial number. */
2362                 *dash = '\0';
2363         }
2364
2365         /* Stop autoservice so we can monitor all parties involved in the transfer. */
2366         if (ast_autoservice_stop(transferee) < 0) {
2367                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2368                 return -1;
2369         }
2370
2371         /* Save connected line info for party B about party A in case transfer fails. */
2372         ast_party_connected_line_init(&connected_line);
2373         ast_channel_lock(transferer);
2374         ast_party_connected_line_copy(&connected_line, &transferer->connected);
2375         ast_channel_unlock(transferer);
2376         connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
2377
2378         /* Dial party C */
2379         newchan = feature_request_and_dial(transferer, transferer_name_orig, transferer,
2380                 transferee, "Local", transferer->nativeformats, xferto,
2381                 atxfernoanswertimeout, &outstate, transferer->language);
2382         ast_debug(2, "Dial party C result: newchan:%d, outstate:%d\n", !!newchan, outstate);
2383
2384         if (!ast_check_hangup(transferer)) {
2385                 int hangup_dont = 0;
2386
2387                 /* Transferer (party B) is up */
2388                 ast_debug(1, "Actually doing an attended transfer.\n");
2389
2390                 /* Start autoservice on transferee while the transferer deals with party C. */
2391                 ast_autoservice_start(transferee);
2392
2393                 ast_indicate(transferer, -1);
2394                 if (!newchan) {
2395                         /* any reason besides user requested cancel and busy triggers the failed sound */
2396                         switch (outstate) {
2397                         case AST_CONTROL_UNHOLD:/* Caller requested cancel or party C answer timeout. */
2398                         case AST_CONTROL_BUSY:
2399                         case AST_CONTROL_CONGESTION:
2400                                 if (ast_stream_and_wait(transferer, xfersound, "")) {
2401                                         ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
2402                                 }
2403                                 break;
2404                         default:
2405                                 if (ast_stream_and_wait(transferer, xferfailsound, "")) {
2406                                         ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
2407                                 }
2408                                 break;
2409                         }
2410                         atxfer_fail_cleanup(transferee, transferer, &connected_line);
2411                         return AST_FEATURE_RETURN_SUCCESS;
2412                 }
2413
2414                 if (check_compat(transferer, newchan)) {
2415                         if (ast_stream_and_wait(transferer, xferfailsound, "")) {
2416                                 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
2417                         }
2418                         atxfer_fail_cleanup(transferee, transferer, &connected_line);
2419                         return AST_FEATURE_RETURN_SUCCESS;
2420                 }
2421                 memset(&bconfig,0,sizeof(struct ast_bridge_config));
2422                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
2423                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
2424
2425                 /* ast_bridge_call clears AST_FLAG_BRIDGE_HANGUP_DONT, but we don't
2426                    want that to happen here because we're also in another bridge already
2427                  */
2428                 if (ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT)) {
2429                         hangup_dont = 1;
2430                 }
2431                 /* Let party B and party C talk as long as they want. */
2432                 ast_bridge_call(transferer, newchan, &bconfig);
2433                 if (hangup_dont) {
2434                         ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
2435                 }
2436
2437                 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
2438                         ast_hangup(newchan);
2439                         if (ast_stream_and_wait(transferer, xfersound, "")) {
2440                                 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
2441                         }
2442                         atxfer_fail_cleanup(transferee, transferer, &connected_line);
2443                         return AST_FEATURE_RETURN_SUCCESS;
2444                 }
2445
2446                 /* Transferer (party B) is confirmed hung up at this point. */
2447                 if (check_compat(transferee, newchan)) {
2448                         finishup(transferee);
2449                         ast_party_connected_line_free(&connected_line);
2450                         return -1;
2451                 }
2452
2453                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2454                 if ((ast_autoservice_stop(transferee) < 0)
2455                         || (ast_waitfordigit(transferee, 100) < 0)
2456                         || (ast_waitfordigit(newchan, 100) < 0)
2457                         || ast_check_hangup(transferee)
2458                         || ast_check_hangup(newchan)) {
2459                         ast_hangup(newchan);
2460                         ast_party_connected_line_free(&connected_line);
2461                         return -1;
2462                 }
2463         } else if (!ast_check_hangup(transferee)) {
2464                 /* Transferer (party B) has hung up at this point.  Doing blonde transfer. */
2465                 ast_debug(1, "Actually doing a blonde transfer.\n");
2466
2467                 if (!newchan && !atxferdropcall) {
2468                         /* Party C is not available, try to call party B back. */
2469                         unsigned int tries = 0;
2470
2471                         if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
2472                                 ast_log(LOG_WARNING,
2473                                         "Transferer channel name: '%s' cannot be used for callback.\n",
2474                                         transferer_name_orig);
2475                                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2476                                 ast_party_connected_line_free(&connected_line);
2477                                 return -1;
2478                         }
2479
2480                         tries = 0;
2481                         for (;;) {
2482                                 /* Try to get party B back. */
2483                                 ast_debug(1, "We're trying to callback %s/%s\n",
2484                                         transferer_tech, transferer_name);
2485                                 newchan = feature_request_and_dial(transferer, transferer_name_orig,
2486                                         transferee, transferee, transferer_tech,
2487                                         transferee->nativeformats, transferer_name,
2488                                         atxfernoanswertimeout, &outstate, transferer->language);
2489                                 ast_debug(2, "Dial party B result: newchan:%d, outstate:%d\n",
2490                                         !!newchan, outstate);
2491                                 if (newchan || ast_check_hangup(transferee)) {
2492                                         break;
2493                                 }
2494
2495                                 ++tries;
2496                                 if (atxfercallbackretries <= tries) {
2497                                         /* No more callback tries remaining. */
2498                                         break;
2499                                 }
2500
2501                                 if (atxferloopdelay) {
2502                                         /* Transfer failed, sleeping */
2503                                         ast_debug(1, "Sleeping for %d ms before retrying atxfer.\n",
2504                                                 atxferloopdelay);
2505                                         ast_safe_sleep(transferee, atxferloopdelay);
2506                                         if (ast_check_hangup(transferee)) {
2507                                                 ast_party_connected_line_free(&connected_line);
2508                                                 return -1;
2509                                         }
2510                                 }
2511
2512                                 /* Retry dialing party C. */
2513                                 ast_debug(1, "We're retrying to call %s/%s\n", "Local", xferto);
2514                                 newchan = feature_request_and_dial(transferer, transferer_name_orig,
2515                                         transferer, transferee, "Local",
2516                                         transferee->nativeformats, xferto,
2517                                         atxfernoanswertimeout, &outstate, transferer->language);
2518                                 ast_debug(2, "Redial party C result: newchan:%d, outstate:%d\n",
2519                                         !!newchan, outstate);
2520                                 if (newchan || ast_check_hangup(transferee)) {
2521                                         break;
2522                                 }
2523                         }
2524                 }
2525                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2526                 if (!newchan) {
2527                         /* No party C or could not callback party B. */
2528                         ast_party_connected_line_free(&connected_line);
2529                         return -1;
2530                 }
2531
2532                 /* newchan is up, we should prepare transferee and bridge them */
2533                 if (ast_check_hangup(newchan)) {
2534                         ast_hangup(newchan);
2535                         ast_party_connected_line_free(&connected_line);
2536                         return -1;
2537                 }
2538                 if (check_compat(transferee, newchan)) {
2539                         ast_party_connected_line_free(&connected_line);
2540                         return -1;
2541                 }
2542         } else {
2543                 /*
2544                  * Both the transferer and transferee have hungup.  If newchan
2545                  * is up, hang it up as it has no one to talk to.
2546                  */
2547                 ast_debug(1, "Everyone is hungup.\n");
2548                 if (newchan) {
2549                         ast_hangup(newchan);
2550                 }
2551                 ast_party_connected_line_free(&connected_line);
2552                 return -1;
2553         }
2554
2555         /* Initiate the channel transfer of party A to party C (or recalled party B). */
2556         ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
2557
2558         xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
2559         if (!xferchan) {
2560                 ast_hangup(newchan);
2561                 ast_party_connected_line_free(&connected_line);
2562                 return -1;
2563         }
2564
2565         /* Give party A a momentary ringback tone during transfer. */
2566         xferchan->visible_indication = AST_CONTROL_RINGING;
2567
2568         /* Make formats okay */
2569         xferchan->readformat = transferee->readformat;
2570         xferchan->writeformat = transferee->writeformat;
2571
2572         ast_channel_masquerade(xferchan, transferee);
2573         ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
2574         xferchan->_state = AST_STATE_UP;
2575         ast_clear_flag(xferchan, AST_FLAGS_ALL);
2576
2577         /* Do the masquerade manually to make sure that is is completed. */
2578         ast_do_masquerade(xferchan);
2579
2580         newchan->_state = AST_STATE_UP;
2581         ast_clear_flag(newchan, AST_FLAGS_ALL);
2582         tobj = ast_calloc(1, sizeof(*tobj));
2583         if (!tobj) {
2584                 ast_hangup(xferchan);
2585                 ast_hangup(newchan);
2586                 ast_party_connected_line_free(&connected_line);
2587                 return -1;
2588         }
2589
2590         ast_channel_lock(newchan);
2591         if ((features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL))) {
2592                 dialfeatures = features_datastore->data;
2593         }
2594         ast_channel_unlock(newchan);
2595
2596         if (dialfeatures) {
2597                 /* newchan should always be the callee and shows up as callee in dialfeatures, but for some reason
2598                    I don't currently understand, the abilities of newchan seem to be stored on the caller side */
2599                 ast_copy_flags(&(config->features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
2600                 dialfeatures = NULL;
2601         }
2602
2603         ast_channel_lock(xferchan);
2604         if ((features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL))) {
2605                 dialfeatures = features_datastore->data;
2606         }
2607         ast_channel_unlock(xferchan);
2608
2609         if (dialfeatures) {
2610                 ast_copy_flags(&(config->features_caller), &(dialfeatures->features_caller), AST_FLAGS_ALL);
2611         }
2612
2613         tobj->chan = newchan;
2614         tobj->peer = xferchan;
2615         tobj->bconfig = *config;
2616
2617         if (tobj->bconfig.end_bridge_callback_data_fixup) {
2618                 tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
2619         }
2620
2621         /*
2622          * xferchan is transferee, and newchan is the transfer target
2623          * So...in a transfer, who is the caller and who is the callee?
2624          *
2625          * When the call is originally made, it is clear who is caller and callee.
2626          * When a transfer occurs, it is my humble opinion that the transferee becomes
2627          * the caller, and the transfer target is the callee.
2628          *
2629          * The problem is that these macros were set with the intention of the original
2630          * caller and callee taking those roles.  A transfer can totally mess things up,
2631          * to be technical.  What sucks even more is that you can't effectively change
2632          * the macros in the dialplan during the call from the transferer to the transfer
2633          * target because the transferee is stuck with whatever role he originally had.
2634          *
2635          * I think the answer here is just to make sure that it is well documented that
2636          * during a transfer, the transferee is the "caller" and the transfer target
2637          * is the "callee."
2638          *
2639          * This means that if party B calls party A, and party B transfers party A to
2640          * party C, then A has switched roles for the call.  Now party A will have the
2641          * caller macro called on his channel instead of the callee macro.
2642          *
2643          * Luckily, the method by which the party B to party C bridge is
2644          * launched above ensures that the transferee is the "chan" on
2645          * the bridge and the transfer target is the "peer," so my idea
2646          * for the roles post-transfer does not require extensive code
2647          * changes.
2648          */
2649
2650         /* Transfer party C connected line to party A */
2651         ast_channel_lock(transferer);
2652         /*
2653          * Due to a limitation regarding when callerID is set on a Local channel,
2654          * we use the transferer's connected line information here.
2655          */
2656         ast_party_connected_line_copy(&connected_line, &transferer->connected);
2657         ast_channel_unlock(transferer);
2658         connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
2659         if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
2660                 ast_channel_update_connected_line(xferchan, &connected_line, NULL);
2661         }
2662
2663         /* Transfer party A connected line to party C */
2664         ast_channel_lock(xferchan);
2665         ast_connected_line_copy_from_caller(&connected_line, &xferchan->caller);
2666         ast_channel_unlock(xferchan);
2667         connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
2668         if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
2669                 ast_channel_update_connected_line(newchan, &connected_line, NULL);
2670         }
2671
2672         if (ast_stream_and_wait(newchan, xfersound, ""))
2673                 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
2674         bridge_call_thread_launch(tobj);
2675
2676         ast_party_connected_line_free(&connected_line);
2677         return -1;/* The transferee is masqueraded and the original bridged channels can be hungup. */
2678 }
2679
2680 /* add atxfer and automon as undefined so you can only use em if you configure them */
2681 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
2682
2683 AST_RWLOCK_DEFINE_STATIC(features_lock);
2684
2685 static struct ast_call_feature builtin_features[] = {
2686         { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
2687         { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
2688         { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
2689         { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
2690         { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
2691         { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
2692 };
2693
2694
2695 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
2696
2697 /*! \brief register new feature into feature_list*/
2698 void ast_register_feature(struct ast_call_feature *feature)
2699 {
2700         if (!feature) {
2701                 ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
2702                 return;
2703         }
2704   
2705         AST_RWLIST_WRLOCK(&feature_list);
2706         AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
2707         AST_RWLIST_UNLOCK(&feature_list);
2708
2709         ast_verb(2, "Registered Feature '%s'\n",feature->sname);
2710 }
2711
2712 /*! 
2713  * \brief Add new feature group
2714  * \param fgname feature group name.
2715  *
2716  * Add new feature group to the feature group list insert at head of list.
2717  * \note This function MUST be called while feature_groups is locked.
2718  */
2719 static struct feature_group *register_group(const char *fgname)
2720 {
2721         struct feature_group *fg;
2722
2723         if (!fgname) {
2724                 ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
2725                 return NULL;
2726         }
2727
2728         if (!(fg = ast_calloc_with_stringfields(1, struct feature_group, 128))) {
2729                 return NULL;
2730         }
2731
2732         ast_string_field_set(fg, gname, fgname);
2733
2734         AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
2735
2736         ast_verb(2, "Registered group '%s'\n", fg->gname);
2737
2738         return fg;
2739 }
2740
2741 /*! 
2742  * \brief Add feature to group
2743  * \param fg feature group
2744  * \param exten
2745  * \param feature feature to add.
2746  *
2747  * Check fg and feature specified, add feature to list
2748  * \note This function MUST be called while feature_groups is locked. 
2749  */
2750 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
2751 {
2752         struct feature_group_exten *fge;
2753
2754         if (!fg) {
2755                 ast_log(LOG_NOTICE, "You didn't pass a group!\n");
2756                 return;
2757         }
2758
2759         if (!feature) {
2760                 ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
2761                 return;
2762         }
2763
2764         if (!(fge = ast_calloc_with_stringfields(1, struct feature_group_exten, 128))) {
2765                 return;
2766         }
2767
2768         ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
2769
2770         fge->feature = feature;
2771
2772         AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
2773
2774         ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
2775                                         feature->sname, fg->gname, fge->exten);
2776 }
2777
2778 void ast_unregister_feature(struct ast_call_feature *feature)
2779 {
2780         if (!feature) {
2781                 return;
2782         }
2783
2784         AST_RWLIST_WRLOCK(&feature_list);
2785         AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
2786         AST_RWLIST_UNLOCK(&feature_list);
2787
2788         ast_free(feature);
2789 }
2790
2791 /*! \brief Remove all features in the list */
2792 static void ast_unregister_features(void)
2793 {
2794         struct ast_call_feature *feature;
2795
2796         AST_RWLIST_WRLOCK(&feature_list);
2797         while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
2798                 ast_free(feature);
2799         }
2800         AST_RWLIST_UNLOCK(&feature_list);
2801 }
2802
2803 /*! \brief find a call feature by name */
2804 static struct ast_call_feature *find_dynamic_feature(const char *name)
2805 {
2806         struct ast_call_feature *tmp;
2807
2808         AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
2809                 if (!strcasecmp(tmp->sname, name)) {
2810                         break;
2811                 }
2812         }
2813
2814         return tmp;
2815 }
2816
2817 /*! \brief Remove all feature groups in the list */
2818 static void ast_unregister_groups(void)
2819 {
2820         struct feature_group *fg;
2821         struct feature_group_exten *fge;
2822
2823         AST_RWLIST_WRLOCK(&feature_groups);
2824         while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
2825                 while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
2826                         ast_string_field_free_memory(fge);
2827                         ast_free(fge);
2828                 }
2829
2830                 ast_string_field_free_memory(fg);
2831                 ast_free(fg);
2832         }
2833         AST_RWLIST_UNLOCK(&feature_groups);
2834 }
2835
2836 /*! 
2837  * \brief Find a group by name 
2838  * \param name feature name
2839  * \retval feature group on success.
2840  * \retval NULL on failure.
2841  */
2842 static struct feature_group *find_group(const char *name)
2843 {
2844         struct feature_group *fg = NULL;
2845
2846         AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
2847                 if (!strcasecmp(fg->gname, name))
2848                         break;
2849         }
2850
2851         return fg;
2852 }
2853
2854 void ast_rdlock_call_features(void)
2855 {
2856         ast_rwlock_rdlock(&features_lock);
2857 }
2858
2859 void ast_unlock_call_features(void)
2860 {
2861         ast_rwlock_unlock(&features_lock);
2862 }
2863
2864 struct ast_call_feature *ast_find_call_feature(const char *name)
2865 {
2866         int x;
2867         for (x = 0; x < FEATURES_COUNT; x++) {
2868                 if (!strcasecmp(name, builtin_features[x].sname))
2869                         return &builtin_features[x];
2870         }
2871         return NULL;
2872 }
2873
2874 /*!
2875  * \brief exec an app by feature 
2876  * \param chan,peer,config,code,sense,data
2877  *
2878  * Find a feature, determine which channel activated
2879  * \retval AST_FEATURE_RETURN_NO_HANGUP_PEER
2880  * \retval -1 error.
2881  * \retval -2 when an application cannot be found.
2882  */
2883 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2884 {
2885         struct ast_app *app;
2886         struct ast_call_feature *feature = data;
2887         struct ast_channel *work, *idle;
2888         int res;
2889
2890         if (!feature) { /* shouldn't ever happen! */
2891                 ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
2892                 return -1; 
2893         }
2894
2895         if (sense == FEATURE_SENSE_CHAN) {
2896                 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
2897                         return AST_FEATURE_RETURN_KEEPTRYING;
2898                 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
2899                         work = chan;
2900                         idle = peer;
2901                 } else {
2902                         work = peer;
2903                         idle = chan;
2904                 }
2905         } else {
2906                 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
2907                         return AST_FEATURE_RETURN_KEEPTRYING;
2908                 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
2909                         work = peer;
2910                         idle = chan;
2911                 } else {
2912                         work = chan;
2913                         idle = peer;
2914                 }
2915         }
2916
2917         if (!(app = pbx_findapp(feature->app))) {
2918                 ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
2919                 return -2;
2920         }
2921
2922         ast_autoservice_start(idle);
2923         ast_autoservice_ignore(idle, AST_FRAME_DTMF_END);
2924         
2925         if(work && idle) {
2926                 pbx_builtin_setvar_helper(work, "DYNAMIC_PEERNAME", idle->name);
2927                 pbx_builtin_setvar_helper(idle, "DYNAMIC_PEERNAME", work->name);
2928                 pbx_builtin_setvar_helper(work, "DYNAMIC_FEATURENAME", feature->sname);
2929                 pbx_builtin_setvar_helper(idle, "DYNAMIC_FEATURENAME", feature->sname);
2930         }
2931
2932         if (!ast_strlen_zero(feature->moh_class))
2933                 ast_moh_start(idle, feature->moh_class, NULL);
2934
2935         res = pbx_exec(work, app, feature->app_args);
2936
2937         if (!ast_strlen_zero(feature->moh_class))
2938                 ast_moh_stop(idle);
2939
2940         ast_autoservice_stop(idle);
2941
2942         if (res) {
2943                 return AST_FEATURE_RETURN_SUCCESSBREAK;
2944         }
2945         return AST_FEATURE_RETURN_SUCCESS;      /*! \todo XXX should probably return res */
2946 }
2947
2948 static void unmap_features(void)
2949 {
2950         int x;
2951
2952         ast_rwlock_wrlock(&features_lock);
2953         for (x = 0; x < FEATURES_COUNT; x++)
2954                 strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
2955         ast_rwlock_unlock(&features_lock);
2956 }
2957
2958 static int remap_feature(const char *name, const char *value)
2959 {
2960         int x, res = -1;
2961
2962         ast_rwlock_wrlock(&features_lock);
2963         for (x = 0; x < FEATURES_COUNT; x++) {
2964                 if (strcasecmp(builtin_features[x].sname, name))
2965                         continue;
2966
2967                 ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
2968                 res = 0;
2969                 break;
2970         }
2971         ast_rwlock_unlock(&features_lock);
2972
2973         return res;
2974 }
2975
2976 /*!
2977  * \brief Helper function for feature_interpret and ast_feature_detect
2978  * \param chan,peer,config,code,sense,dynamic_features_buf,features,operation,feature
2979  *
2980  * Lock features list, browse for code, unlock list
2981  * If a feature is found and the operation variable is set, that feature's
2982  * operation is executed.  The first feature found is copied to the feature parameter.
2983  * \retval res on success.
2984  * \retval -1 on failure.
2985  */
2986 static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel *peer,
2987         struct ast_bridge_config *config, const char *code, int sense, char *dynamic_features_buf,
2988         struct ast_flags *features, feature_interpret_op operation, struct ast_call_feature *feature)
2989 {
2990         int x;
2991         struct feature_group *fg = NULL;
2992         struct feature_group_exten *fge;
2993         struct ast_call_feature *tmpfeature;
2994         char *tmp, *tok;
2995         int res = AST_FEATURE_RETURN_PASSDIGITS;
2996         int feature_detected = 0;
2997
2998         if (!(peer && chan && config) && operation == FEATURE_INTERPRET_DO) {
2999                 return -1; /* can not run feature operation */
3000         }
3001
3002         ast_rwlock_rdlock(&features_lock);
3003         for (x = 0; x < FEATURES_COUNT; x++) {
3004                 if ((ast_test_flag(features, builtin_features[x].feature_mask)) &&
3005                     !ast_strlen_zero(builtin_features[x].exten)) {
3006                         /* Feature is up for consideration */
3007                         if (!strcmp(builtin_features[x].exten, code)) {
3008                                 ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
3009                                 if (operation == FEATURE_INTERPRET_CHECK) {
3010                                         res = AST_FEATURE_RETURN_SUCCESS; /* We found something */
3011                                 } else if (operation == FEATURE_INTERPRET_DO) {
3012                                         res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
3013                                 }
3014                                 if (feature) {
3015                                         memcpy(feature, &builtin_features[x], sizeof(feature));
3016                                 }
3017                                 feature_detected = 1;
3018                                 break;
3019                         } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
3020                                 if (res == AST_FEATURE_RETURN_PASSDIGITS) {
3021                                         res = AST_FEATURE_RETURN_STOREDIGITS;
3022                                 }
3023                         }
3024                 }
3025         }
3026         ast_rwlock_unlock(&features_lock);
3027
3028         if (ast_strlen_zero(dynamic_features_buf) || feature_detected) {
3029                 return res;
3030         }
3031
3032         tmp = dynamic_features_buf;
3033
3034         while ((tok = strsep(&tmp, "#"))) {
3035                 AST_RWLIST_RDLOCK(&feature_groups);
3036
3037                 fg = find_group(tok);
3038
3039                 if (fg) {
3040                         AST_LIST_TRAVERSE(&fg->features, fge, entry) {
3041                                 if (!strcmp(fge->exten, code)) {
3042                                         if (operation) {
3043                                                 res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
3044                                         }
3045                                         memcpy(feature, fge->feature, sizeof(feature));
3046                                         if (res != AST_FEATURE_RETURN_KEEPTRYING) {
3047                                                 AST_RWLIST_UNLOCK(&feature_groups);
3048                                                 break;
3049                                         }
3050                                         res = AST_FEATURE_RETURN_PASSDIGITS;
3051                                 } else if (!strncmp(fge->exten, code, strlen(code))) {
3052                                         res = AST_FEATURE_RETURN_STOREDIGITS;
3053                                 }
3054                         }
3055                         if (fge) {
3056                                 break;
3057                         }
3058                 }
3059
3060                 AST_RWLIST_UNLOCK(&feature_groups);
3061
3062                 AST_RWLIST_RDLOCK(&feature_list);
3063
3064                 if (!(tmpfeature = find_dynamic_feature(tok))) {
3065                         AST_RWLIST_UNLOCK(&feature_list);
3066                         continue;
3067                 }
3068
3069                 /* Feature is up for consideration */
3070                 if (!strcmp(tmpfeature->exten, code)) {
3071                         ast_verb(3, " Feature Found: %s exten: %s\n",tmpfeature->sname, tok);
3072                         if (operation == FEATURE_INTERPRET_CHECK) {
3073                                 res = AST_FEATURE_RETURN_SUCCESS; /* We found something */
3074                         } else if (operation == FEATURE_INTERPRET_DO) {
3075                                 res = tmpfeature->operation(chan, peer, config, code, sense, tmpfeature);
3076                         }
3077                         if (feature) {
3078                                 memcpy(feature, tmpfeature, sizeof(feature));
3079                         }
3080                         if (res != AST_FEATURE_RETURN_KEEPTRYING) {
3081                                 AST_RWLIST_UNLOCK(&feature_list);
3082                                 break;
3083                         }
3084                         res = AST_FEATURE_RETURN_PASSDIGITS;
3085                 } else if (!strncmp(tmpfeature->exten, code, strlen(code)))
3086                         res = AST_FEATURE_RETURN_STOREDIGITS;
3087
3088                 AST_RWLIST_UNLOCK(&feature_list);
3089         }
3090
3091         return res;
3092 }
3093
3094 /*!
3095  * \brief Check the dynamic features
3096  * \param chan,peer,config,code,sense
3097  *
3098  * \retval res on success.
3099  * \retval -1 on failure.
3100  */
3101 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense) {
3102
3103         char dynamic_features_buf[128];
3104         const char *peer_dynamic_features, *chan_dynamic_features;
3105         struct ast_flags features;
3106         struct ast_call_feature feature;
3107         if (sense == FEATURE_SENSE_CHAN) {
3108                 ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
3109         }
3110         else {
3111                 ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
3112         }
3113
3114         ast_channel_lock(peer);
3115         peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
3116         ast_channel_unlock(peer);
3117
3118         ast_channel_lock(chan);
3119         chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
3120         ast_channel_unlock(chan);
3121
3122         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,""));
3123
3124         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);
3125
3126         return feature_interpret_helper(chan, peer, config, code, sense, dynamic_features_buf, &features, FEATURE_INTERPRET_DO, &feature);
3127 }
3128
3129
3130 int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
3131
3132         return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, FEATURE_INTERPRET_DETECT, feature);
3133 }
3134
3135 /*! \brief Check if a feature exists */
3136 static int feature_check(struct ast_channel *chan, struct ast_flags *features, char *code) {
3137         char *chan_dynamic_features;
3138         ast_channel_lock(chan);
3139         chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
3140         ast_channel_unlock(chan);
3141
3142         return feature_interpret_helper(chan, NULL, NULL, code, 0, chan_dynamic_features, features, FEATURE_INTERPRET_CHECK, NULL);
3143 }
3144
3145 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
3146 {
3147         int x;
3148
3149         ast_clear_flag(config, AST_FLAGS_ALL);
3150
3151         ast_rwlock_rdlock(&features_lock);
3152         for (x = 0; x < FEATURES_COUNT; x++) {
3153                 if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
3154                         continue;
3155
3156                 if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
3157                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
3158
3159                 if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
3160                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
3161         }
3162         ast_rwlock_unlock(&features_lock);
3163
3164         if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
3165                 const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
3166
3167                 if (dynamic_features) {
3168                         char *tmp = ast_strdupa(dynamic_features);
3169                         char *tok;
3170                         struct ast_call_feature *feature;
3171
3172                         /* while we have a feature */
3173                         while ((tok = strsep(&tmp, "#"))) {
3174                                 struct feature_group *fg;
3175
3176                                 AST_RWLIST_RDLOCK(&feature_groups);
3177                                 AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
3178                                         struct feature_group_exten *fge;
3179
3180                                         AST_LIST_TRAVERSE(&fg->features, fge, entry) {
3181                                                 if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLER)) {
3182                                                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
3183                                                 }
3184                                                 if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLEE)) {
3185                                                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
3186                                                 }
3187                                         }
3188                                 }
3189                                 AST_RWLIST_UNLOCK(&feature_groups);
3190
3191                                 AST_RWLIST_RDLOCK(&feature_list);
3192                                 if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
3193                                         if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER)) {
3194                                                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
3195                                         }
3196                                         if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE)) {
3197                                                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
3198                                         }
3199                                 }
3200                                 AST_RWLIST_UNLOCK(&feature_list);
3201                         }
3202                 }
3203         }
3204 }
3205
3206 /*!
3207  * \internal
3208  * \brief Get feature and dial.
3209  *
3210  * \param caller Channel to represent as the calling channel for the dialed channel.
3211  * \param caller_name Original caller channel name.
3212  * \param requestor Channel to say is requesting the dial (usually the caller).
3213  * \param transferee Channel that the dialed channel will be transferred to.
3214  * \param type Channel technology type to dial.
3215  * \param format Codec formats for dialed channel.
3216  * \param data Dialed channel extra parameters for ast_request() and ast_call().
3217  * \param timeout Time limit for dialed channel to answer in ms. Must be greater than zero.
3218  * \param outstate Status of dialed channel if unsuccessful.
3219  * \param language Language of the caller.
3220  *
3221  * \note
3222  * outstate can be:
3223  * 0, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION,
3224  * AST_CONTROL_ANSWER, or AST_CONTROL_UNHOLD.  If
3225  * AST_CONTROL_UNHOLD then the caller channel cancelled the
3226  * transfer or the dialed channel did not answer before the
3227  * timeout.
3228  *
3229  * \details
3230  * Request channel, set channel variables, initiate call,
3231  * check if they want to disconnect, go into loop, check if timeout has elapsed,
3232  * check if person to be transfered hung up, check for answer break loop,
3233  * set cdr return channel.
3234  *
3235  * \retval Channel Connected channel for transfer.
3236  * \retval NULL on failure to get third party connected.
3237  *
3238  * \note This is similar to __ast_request_and_dial() in channel.c
3239  */
3240 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
3241         const char *caller_name, struct ast_channel *requestor,
3242         struct ast_channel *transferee, const char *type, struct ast_format_cap *cap, void *data,
3243         int timeout, int *outstate, const char *language)
3244 {
3245         int state = 0;
3246         int cause = 0;
3247         int to;
3248         int caller_hungup;
3249         int transferee_hungup;
3250         struct ast_channel *chan;
3251         struct ast_channel *monitor_chans[3];
3252         struct ast_channel *active_channel;
3253         int res;
3254         int ready = 0;
3255         struct timeval started;
3256         int x, len = 0;
3257         char *disconnect_code = NULL, *dialed_code = NULL;
3258         struct ast_format_cap *tmp_cap;
3259         struct ast_format best_audio_fmt;
3260         struct ast_frame *f;
3261         AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
3262
3263         tmp_cap = ast_format_cap_alloc_nolock();
3264         if (!tmp_cap) {
3265                 if (outstate) {
3266                         *outstate = 0;
3267                 }
3268                 return NULL;
3269         }
3270         ast_best_codec(cap, &best_audio_fmt);
3271         ast_format_cap_add(tmp_cap, &best_audio_fmt);
3272
3273         caller_hungup = ast_check_hangup(caller);
3274
3275         if (!(chan = ast_request(type, tmp_cap, requestor, data, &cause))) {
3276                 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
3277                 switch (cause) {
3278                 case AST_CAUSE_BUSY:
3279                         state = AST_CONTROL_BUSY;
3280                         break;
3281                 case AST_CAUSE_CONGESTION:
3282                         state = AST_CONTROL_CONGESTION;
3283                         break;
3284                 default:
3285                         state = 0;
3286                         break;
3287                 }
3288                 goto done;
3289         }
3290
3291         ast_string_field_set(chan, language, language);
3292         ast_channel_inherit_variables(caller, chan);
3293         pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller_name);
3294
3295         ast_channel_lock(chan);
3296         ast_connected_line_copy_from_caller(&chan->connected, &requestor->caller);
3297         ast_channel_unlock(chan);
3298
3299         if (ast_call(chan, data, timeout)) {
3300                 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
3301                 switch (chan->hangupcause) {
3302                 case AST_CAUSE_BUSY:
3303                         state = AST_CONTROL_BUSY;
3304                         break;
3305                 case AST_CAUSE_CONGESTION:
3306                         state = AST_CONTROL_CONGESTION;
3307                         break;
3308                 default:
3309                         state = 0;
3310                         break;
3311                 }
3312                 goto done;
3313         }
3314
3315         /* support dialing of the featuremap disconnect code while performing an attended tranfer */
3316         ast_rwlock_rdlock(&features_lock);
3317         for (x = 0; x < FEATURES_COUNT; x++) {
3318                 if (strcasecmp(builtin_features[x].sname, "disconnect"))
3319                         continue;
3320
3321                 disconnect_code = builtin_features[x].exten;
3322                 len = strlen(disconnect_code) + 1;
3323                 dialed_code = alloca(len);
3324                 memset(dialed_code, 0, len);
3325                 break;
3326         }
3327         ast_rwlock_unlock(&features_lock);
3328         x = 0;
3329         started = ast_tvnow();
3330         to = timeout;
3331         AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
3332
3333         ast_poll_channel_add(caller, chan);
3334
3335         transferee_hungup = 0;
3336         while (!ast_check_hangup(transferee) && (chan->_state != AST_STATE_UP)) {
3337                 int num_chans = 0;
3338
3339                 monitor_chans[num_chans++] = transferee;
3340                 monitor_chans[num_chans++] = chan;
3341                 if (!caller_hungup) {
3342                         if (ast_check_hangup(caller)) {
3343                                 caller_hungup = 1;
3344
3345 #if defined(ATXFER_NULL_TECH)
3346                                 /* Change caller's name to ensure that it will remain unique. */
3347                                 set_new_chan_name(caller);
3348
3349                                 /*
3350                                  * Get rid of caller's physical technology so it is free for
3351                                  * other calls.
3352                                  */
3353                                 set_kill_chan_tech(caller);
3354 #endif  /* defined(ATXFER_NULL_TECH) */
3355                         } else {
3356                                 /* caller is not hungup so monitor it. */
3357                                 monitor_chans[num_chans++] = caller;
3358                         }
3359                 }
3360
3361                 /* see if the timeout has been violated */
3362                 if (ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
3363                         state = AST_CONTROL_UNHOLD;
3364                         ast_log(LOG_NOTICE, "We exceeded our AT-timeout for %s\n", chan->name);
3365                         break; /*doh! timeout*/
3366                 }
3367
3368                 active_channel = ast_waitfor_n(monitor_chans, num_chans, &to);
3369                 if (!active_channel)
3370                         continue;
3371
3372                 f = NULL;
3373                 if (transferee == active_channel) {
3374                         struct ast_frame *dup_f;
3375
3376                         f = ast_read(transferee);
3377                         if (f == NULL) { /*doh! where'd he go?*/
3378                                 transferee_hungup = 1;
3379                                 state = 0;
3380                                 break;
3381                         }
3382                         if (ast_is_deferrable_frame(f)) {
3383                                 dup_f = ast_frisolate(f);
3384                                 if (dup_f) {
3385                                         if (dup_f == f) {
3386                                                 f = NULL;
3387                                         }
3388                                         AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
3389                                 }
3390                         }
3391                 } else if (chan == active_channel) {
3392                         if (!ast_strlen_zero(chan->call_forward)) {
3393                                 state = 0;
3394                                 chan = ast_call_forward(caller, chan, NULL, tmp_cap, NULL, &state);
3395                                 if (!chan) {
3396                                         break;
3397                                 }
3398                                 continue;
3399                         }
3400                         f = ast_read(chan);
3401                         if (f == NULL) { /*doh! where'd he go?*/
3402                                 switch (chan->hangupcause) {
3403                                 case AST_CAUSE_BUSY:
3404                                         state = AST_CONTROL_BUSY;
3405                                         break;
3406                                 case AST_CAUSE_CONGESTION:
3407                                         state = AST_CONTROL_CONGESTION;
3408                                         break;
3409                                 default:
3410                                         state = 0;
3411                                         break;
3412                                 }
3413                                 break;
3414                         }
3415
3416                         if (f->frametype == AST_FRAME_CONTROL) {
3417                                 if (f->subclass.integer == AST_CONTROL_RINGING) {
3418                                         ast_verb(3, "%s is ringing\n", chan->name);
3419                                         ast_indicate(caller, AST_CONTROL_RINGING);
3420                                 } else if (f->subclass.integer == AST_CONTROL_BUSY) {
3421                                         state = f->subclass.integer;
3422                                         ast_verb(3, "%s is busy\n", chan->name);
3423                                         ast_indicate(caller, AST_CONTROL_BUSY);
3424                                         ast_frfree(f);
3425                                         break;
3426                                 } else if (f->subclass.integer == AST_CONTROL_INCOMPLETE) {
3427                                         ast_verb(3, "%s dialed incomplete extension %s; ignoring\n", chan->name, chan->exten);
3428                                 } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
3429                                         state = f->subclass.integer;
3430                                         ast_verb(3, "%s is congested\n", chan->name);
3431                                         ast_indicate(caller, AST_CONTROL_CONGESTION);
3432                                         ast_frfree(f);
3433                                         break;
3434                                 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
3435                                         /* This is what we are hoping for */
3436                                         state = f->subclass.integer;
3437                                         ast_frfree(f);
3438                                         ready=1;
3439                                         break;
3440                                 } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
3441                                         if (caller_hungup) {
3442                                                 struct ast_party_connected_line connected;
3443
3444                                                 /* Just save it for the transfer. */
3445                                                 ast_party_connected_line_set_init(&connected, &caller->connected);
3446                                                 res = ast_connected_line_parse_data(f->data.ptr, f->datalen,
3447                                                         &connected);
3448                                                 if (!res) {
3449                                                         ast_channel_set_connected_line(caller, &connected, NULL);
3450                                                 }
3451                                                 ast_party_connected_line_free(&connected);
3452                                         } else {
3453                                                 ast_autoservice_start(transferee);
3454                                                 if (ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
3455                                                         ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE,
3456                                                                 f->data.ptr, f->datalen);
3457                                                 }
3458                                                 ast_autoservice_stop(transferee);
3459                                         }
3460                                 } else if (f->subclass.integer == AST_CONTROL_REDIRECTING) {
3461                                         if (!caller_hungup) {
3462                                                 ast_autoservice_start(transferee);
3463                                                 if (ast_channel_redirecting_macro(chan, caller, f, 1, 1)) {
3464                                                         ast_indicate_data(caller, AST_CONTROL_REDIRECTING,
3465                                                                 f->data.ptr, f->datalen);
3466                                                 }
3467                                                 ast_autoservice_stop(transferee);
3468                                         }
3469                                 } else if (f->subclass.integer != -1 && f->subclass.integer != AST_CONTROL_PROGRESS) {
3470                                         ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
3471                                 }
3472                                 /* else who cares */
3473                         } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3474                                 ast_write(caller, f);
3475                         }
3476                 } else if (caller == active_channel) {
3477                         f = ast_read(caller);
3478                         if (f) {
3479                                 if (f->frametype == AST_FRAME_DTMF) {
3480                                         dialed_code[x++] = f->subclass.integer;
3481                                         dialed_code[x] = '\0';
3482                                         if (strlen(dialed_code) == len) {
3483                                                 x = 0;
3484                                         } else if (x && strncmp(dialed_code, disconnect_code, x)) {
3485                                                 x = 0;
3486                                                 dialed_code[x] = '\0';
3487                                         }
3488                                         if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
3489                                                 /* Caller Canceled the call */
3490                                                 state = AST_CONTROL_UNHOLD;
3491                                                 ast_frfree(f);
3492                                                 break;
3493                                         }
3494                                 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3495                                         ast_write(chan, f);
3496                                 }
3497                         }
3498                 }
3499                 if (f)
3500                         ast_frfree(f);
3501         } /*&nbs