addition of dynamic parkinglots feature
[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 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/_private.h"
31
32 #include <pthread.h>
33 #include <sys/time.h>
34 #include <sys/signal.h>
35 #include <netinet/in.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/causes.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/app.h"
45 #include "asterisk/say.h"
46 #include "asterisk/features.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/manager.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/adsi.h"
53 #include "asterisk/devicestate.h"
54 #include "asterisk/monitor.h"
55 #include "asterisk/audiohook.h"
56 #include "asterisk/global_datastores.h"
57 #include "asterisk/astobj2.h"
58 #include "asterisk/cel.h"
59
60 /*** DOCUMENTATION
61         <application name="Bridge" language="en_US">
62                 <synopsis>
63                         Bridge two channels.
64                 </synopsis>
65                 <syntax>
66                         <parameter name="channel" required="true">
67                                 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
68                         </parameter>
69                         <parameter name="options">
70                                 <optionlist>
71                                         <option name="p">
72                                                 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
73                                         </option>
74                                         <option name="h">
75                                                 <para>Allow the called party to hang up by sending the
76                                                 <replaceable>*</replaceable> DTMF digit.</para>
77                                         </option>
78                                         <option name="H">
79                                                 <para>Allow the calling party to hang up by pressing the
80                                                 <replaceable>*</replaceable> DTMF digit.</para>
81                                         </option>
82                                         <option name="k">
83                                                 <para>Allow the called party to enable parking of the call by sending
84                                                 the DTMF sequence defined for call parking in features.conf.</para>
85                                         </option>
86                                         <option name="K">
87                                                 <para>Allow the calling party to enable parking of the call by sending
88                                                  the DTMF sequence defined for call parking in features.conf.</para>
89                                         </option>
90                                         <option name="L(x[:y][:z])">
91                                                 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
92                                                 when <replaceable>y</replaceable> ms are left. Repeat the warning every
93                                                 <replaceable>z</replaceable> ms. The following special variables can be
94                                                 used with this option:</para>
95                                                 <variablelist>
96                                                         <variable name="LIMIT_PLAYAUDIO_CALLER">
97                                                                 <para>Play sounds to the caller. yes|no (default yes)</para>
98                                                         </variable>
99                                                         <variable name="LIMIT_PLAYAUDIO_CALLEE">   
100                                                                 <para>Play sounds to the callee. yes|no</para>
101                                                         </variable>
102                                                         <variable name="LIMIT_TIMEOUT_FILE">
103                                                                 <para>File to play when time is up.</para>
104                                                         </variable>
105                                                         <variable name="LIMIT_CONNECT_FILE">
106                                                                 <para>File to play when call begins.</para>
107                                                         </variable>
108                                                         <variable name="LIMIT_WARNING_FILE">
109                                                                 <para>File to play as warning if <replaceable>y</replaceable> is
110                                                                 defined. The default is to say the time remaining.</para>
111                                                         </variable>
112                                                 </variablelist>
113                                         </option>
114                                         <option name="S(x)">
115                                                 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
116                                         </option>
117                                         <option name="t">
118                                                 <para>Allow the called party to transfer the calling party by sending the
119                                                 DTMF sequence defined in features.conf.</para>
120                                         </option>
121                                         <option name="T">
122                                                 <para>Allow the calling party to transfer the called party by sending the
123                                                 DTMF sequence defined in features.conf.</para>
124                                         </option>
125                                         <option name="w">
126                                                 <para>Allow the called party to enable recording of the call by sending
127                                                 the DTMF sequence defined for one-touch recording in features.conf.</para>
128                                         </option>
129                                         <option name="W">
130                                                 <para>Allow the calling party to enable recording of the call by sending
131                                                 the DTMF sequence defined for one-touch recording in features.conf.</para>
132                                         </option>
133                                         <option name="x">
134                                                 <para>Cause the called party to be hung up after the bridge, instead of being
135                                                 restarted in the dialplan.</para>
136                                         </option>
137                                 </optionlist>
138                         </parameter>
139                 </syntax>
140                 <description>
141                         <para>Allows the ability to bridge two channels via the dialplan.</para>
142                         <para>This application sets the following channel variable upon completion:</para>
143                         <variablelist>
144                                 <variable name="BRIDGERESULT">
145                                         <para>The result of the bridge attempt as a text string.</para>
146                                         <value name="SUCCESS" />
147                                         <value name="FAILURE" />
148                                         <value name="LOOP" />
149                                         <value name="NONEXISTENT" />
150                                         <value name="INCOMPATIBLE" />
151                                 </variable>
152                         </variablelist>
153                 </description>
154         </application>
155         <application name="ParkedCall" language="en_US">
156                 <synopsis>
157                         Answer a parked call.
158                 </synopsis>
159                 <syntax>
160                         <parameter name="exten" required="true" />
161                 </syntax>
162                 <description>
163                         <para>Used to connect to a parked call. This application is always
164                         registered internally and does not need to be explicitly added
165                         into the dialplan, although you should include the <literal>parkedcalls</literal>
166                         context. If no extension is provided, then the first available
167                         parked call will be acquired.</para>
168                 </description>
169                 <see-also>
170                         <ref type="application">Park</ref>
171                         <ref type="application">ParkAndAnnounce</ref>
172                 </see-also>
173         </application>
174         <application name="Park" language="en_US">
175                 <synopsis>
176                         Park yourself.
177                 </synopsis>
178                 <syntax>
179                         <parameter name="timeout">
180                                 <para>A custom parking timeout for this parked call.</para>
181                         </parameter>
182                         <parameter name="return_context">
183                                 <para>The context to return the call to after it times out.</para>
184                         </parameter>
185                         <parameter name="return_exten">
186                                 <para>The extension to return the call to after it times out.</para>
187                         </parameter>
188                         <parameter name="return_priority">
189                                 <para>The priority to return the call to after it times out.</para>
190                         </parameter>
191                         <parameter name="options">
192                                 <para>A list of options for this parked call.</para>
193                                 <optionlist>
194                                         <option name="r">
195                                                 <para>Send ringing instead of MOH to the parked call.</para>
196                                         </option>
197                                         <option name="R">
198                                                 <para>Randomize the selection of a parking space.</para>
199                                         </option>
200                                         <option name="s">
201                                                 <para>Silence announcement of the parking space number.</para>
202                                         </option>
203                                 </optionlist>
204                         </parameter>
205                 </syntax>
206                 <description>
207                         <para>Used to park yourself (typically in combination with a supervised
208                         transfer to know the parking space). This application is always
209                         registered internally and does not need to be explicitly added
210                         into the dialplan, although you should include the <literal>parkedcalls</literal>
211                         context (or the context specified in <filename>features.conf</filename>).</para>
212                         <para>If you set the <variable>PARKINGEXTEN</variable> variable to an extension in your
213                         parking context, Park() will park the call on that extension, unless
214                         it already exists. In that case, execution will continue at next priority.</para>
215                         <para>If you set the <variable>PARKINGLOT</variable> variable, Park() will park the call
216                         in that parkinglot.</para>
217                         <para>If you set the <variable>PARKINGDYNAMIC</variable> variable, this parkinglot from features.conf
218                         will be used as template for the newly created dynamic lot.</para>
219                         <para>If you set the <variable>PARKINGDYNCONTEXT</variable> variable the newly created dynamic
220                         parking lot will use this context.</para>
221                         <para>If you set the <variable>PARKINGDYNPOS</variable> variable the newly created dynamic parkinglot
222                         will use those parking postitions.</para>
223                 </description>
224                 <see-also>
225                         <ref type="application">ParkAndAnnounce</ref>
226                         <ref type="application">ParkedCall</ref>
227                 </see-also>
228         </application>
229         <manager name="ParkedCalls" language="en_US">
230                 <synopsis>
231                         List parked calls.
232                 </synopsis>
233                 <syntax>
234                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
235                 </syntax>
236                 <description>
237                         <para>List parked calls.</para>
238                 </description>
239         </manager>
240         <manager name="Park" language="en_US">
241                 <synopsis>
242                         Park a channel.
243                 </synopsis>
244                 <syntax>
245                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
246                         <parameter name="Channel" required="true">
247                                 <para>Channel name to park.</para>
248                         </parameter>
249                         <parameter name="Channel2" required="true">
250                                 <para>Channel to announce park info to (and return to if timeout).</para>
251                         </parameter>
252                         <parameter name="Timeout">
253                                 <para>Number of milliseconds to wait before callback.</para>
254                         </parameter>
255                 </syntax>
256                 <description>
257                         <para>Park a channel.</para>
258                 </description>
259         </manager>
260         <manager name="Bridge" language="en_US">
261                 <synopsis>
262                         Bridge two channels already in the PBX.
263                 </synopsis>
264                 <syntax>
265                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
266                         <parameter name="Channel1" required="true">
267                                 <para>Channel to Bridge to Channel2.</para>
268                         </parameter>
269                         <parameter name="Channel2" required="true">
270                                 <para>Channel to Bridge to Channel1.</para>
271                         </parameter>
272                         <parameter name="Tone">
273                                 <para>Play courtesy tone to Channel 2.</para>
274                                 <enumlist>
275                                         <enum name="yes" />
276                                         <enum name="no" />
277                                 </enumlist>
278                         </parameter>
279                 </syntax>
280                 <description>
281                         <para>Bridge together two channels already in the PBX.</para>
282                 </description>
283         </manager>
284  ***/
285
286 #define DEFAULT_PARK_TIME 45000
287 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
288 #define DEFAULT_FEATURE_DIGIT_TIMEOUT 1000
289 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000
290 #define DEFAULT_PARKINGLOT "default"                    /*!< Default parking lot */
291 #define DEFAULT_ATXFER_DROP_CALL 0
292 #define DEFAULT_ATXFER_LOOP_DELAY 10000
293 #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
294
295 #define AST_MAX_WATCHERS 256
296 #define MAX_DIAL_FEATURE_OPTIONS 30
297
298 struct feature_group_exten {
299         AST_LIST_ENTRY(feature_group_exten) entry;
300         AST_DECLARE_STRING_FIELDS(
301                 AST_STRING_FIELD(exten);
302         );
303         struct ast_call_feature *feature;
304 };
305
306 struct feature_group {
307         AST_LIST_ENTRY(feature_group) entry;
308         AST_DECLARE_STRING_FIELDS(
309                 AST_STRING_FIELD(gname);
310         );
311         AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
312 };
313
314 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
315
316 static char *parkedcall = "ParkedCall";
317
318 static char pickup_ext[AST_MAX_EXTENSION];                 /*!< Call pickup extension */
319
320 /*! \brief Description of one parked call, added to a list while active, then removed.
321         The list belongs to a parkinglot 
322 */
323 struct parkeduser {
324         struct ast_channel *chan;                   /*!< Parking channel */
325         struct timeval start;                       /*!< Time the parking started */
326         int parkingnum;                             /*!< Parking lot */
327         char parkingexten[AST_MAX_EXTENSION];       /*!< If set beforehand, parking extension used for this call */
328         char context[AST_MAX_CONTEXT];              /*!< Where to go if our parking time expires */
329         char exten[AST_MAX_EXTENSION];
330         int priority;
331         int parkingtime;                            /*!< Maximum length in parking lot before return */
332         unsigned int notquiteyet:1;
333         unsigned int options_specified:1;
334         char peername[1024];
335         unsigned char moh_trys;
336         struct ast_parkinglot *parkinglot;
337         AST_LIST_ENTRY(parkeduser) list;
338 };
339
340 /*! \brief Structure for parking lots which are put in a container. */
341 struct ast_parkinglot {
342         char name[AST_MAX_CONTEXT];
343         char parking_con[AST_MAX_EXTENSION];            /*!< Context for which parking is made accessible */
344         char parking_con_dial[AST_MAX_EXTENSION];       /*!< Context for dialback for parking (KLUDGE) */
345         int parking_start;                              /*!< First available extension for parking */
346         int parking_stop;                               /*!< Last available extension for parking */
347         int parking_offset;
348         int parkfindnext;
349         int parkingtime;                                /*!< Default parking time */
350         char mohclass[MAX_MUSICCLASS];                  /*!< Music class used for parking */
351         int parkaddhints;                               /*!< Add parking hints automatically */
352         int parkedcalltransfers;                        /*!< Enable DTMF based transfers on bridge when picking up parked calls */
353         int parkedcallreparking;                        /*!< Enable DTMF based parking on bridge when picking up parked calls */
354         int parkedcallhangup;                           /*!< Enable DTMF based hangup on a bridge when pickup up parked calls */
355         int parkedcallrecording;                        /*!< Enable DTMF based recording on a bridge when picking up parked calls */
356         AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings; /*!< List of active parkings in this parkinglot */
357 };
358
359 /*! \brief The list of parking lots configured. Always at least one  - the default parking lot */
360 static struct ao2_container *parkinglots;
361  
362 struct ast_parkinglot *default_parkinglot;
363 char parking_ext[AST_MAX_EXTENSION];            /*!< Extension you type to park the call */
364
365 static char courtesytone[256];                             /*!< Courtesy tone */
366 static int parkedplay = 0;                                 /*!< Who to play the courtesy tone to */
367 static int parkeddynamic = 0;                              /*!< Enable creation of parkinglots dynamically */
368 static char xfersound[256];                                /*!< Call transfer sound */
369 static char xferfailsound[256];                            /*!< Call transfer failure sound */
370 static char pickupsound[256];                              /*!< Pickup sound */
371 static char pickupfailsound[256];                          /*!< Pickup failure sound */
372
373 static int adsipark;
374
375 static int transferdigittimeout;
376 static int featuredigittimeout;
377 static int comebacktoorigin = 1;
378
379 static int atxfernoanswertimeout;
380 static unsigned int atxferdropcall;
381 static unsigned int atxferloopdelay;
382 static unsigned int atxfercallbackretries;
383
384 static char *registrar = "features";               /*!< Registrar for operations */
385
386 /* module and CLI command definitions */
387 static char *parkcall = PARK_APP_NAME;
388
389 static struct ast_app *monitor_app = NULL;
390 static int monitor_ok = 1;
391
392 static struct ast_app *mixmonitor_app = NULL;
393 static int mixmonitor_ok = 1;
394
395 static struct ast_app *stopmixmonitor_app = NULL;
396 static int stopmixmonitor_ok = 1;
397
398 static pthread_t parking_thread;
399 struct ast_dial_features {
400         struct ast_flags features_caller;
401         struct ast_flags features_callee;
402         int is_caller;
403 };
404
405 static void *dial_features_duplicate(void *data)
406 {
407         struct ast_dial_features *df = data, *df_copy;
408  
409         if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
410                 return NULL;
411         }
412  
413         memcpy(df_copy, df, sizeof(*df));
414  
415         return df_copy;
416 }
417
418 static void dial_features_destroy(void *data)
419 {
420         struct ast_dial_features *df = data;
421         if (df) {
422                 ast_free(df);
423         }
424 }
425
426 static const struct ast_datastore_info dial_features_info = {
427         .type = "dial-features",
428         .destroy = dial_features_destroy,
429         .duplicate = dial_features_duplicate,
430  };
431  
432 /* Forward declarations */
433 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
434 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
435 static void parkinglot_destroy(void *obj);
436 int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, fd_set *nrfds, fd_set *nefds, int *fs, int *max);
437 struct ast_parkinglot *find_parkinglot(const char *name);
438 static struct ast_parkinglot *create_parkinglot(const char *name);
439 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
440
441 const char *ast_parking_ext(void)
442 {
443         return parking_ext;
444 }
445
446 const char *ast_pickup_ext(void)
447 {
448         return pickup_ext;
449 }
450
451 struct ast_bridge_thread_obj 
452 {
453         struct ast_bridge_config bconfig;
454         struct ast_channel *chan;
455         struct ast_channel *peer;
456         unsigned int return_to_pbx:1;
457 };
458
459 static int parkinglot_hash_cb(const void *obj, const int flags)
460 {
461         const struct ast_parkinglot *parkinglot = obj;
462
463         return ast_str_case_hash(parkinglot->name);
464 }
465
466 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
467 {
468         struct ast_parkinglot *parkinglot = obj, *parkinglot2 = arg;
469
470         return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
471 }
472
473 /*!
474  * \brief store context, extension and priority 
475  * \param chan, context, ext, pri
476 */
477 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
478 {
479         ast_copy_string(chan->context, context, sizeof(chan->context));
480         ast_copy_string(chan->exten, ext, sizeof(chan->exten));
481         chan->priority = pri;
482 }
483
484 /*!
485  * \brief Check goto on transfer
486  * \param chan
487  *
488  * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
489  * When found make sure the types are compatible. Check if channel is valid
490  * if so start the new channel else hangup the call. 
491 */
492 static void check_goto_on_transfer(struct ast_channel *chan) 
493 {
494         struct ast_channel *xferchan;
495         const char *val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
496         char *x, *goto_on_transfer;
497         struct ast_frame *f;
498
499         if (ast_strlen_zero(val))
500                 return;
501
502         goto_on_transfer = ast_strdupa(val);
503
504         if (!(xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", chan->linkedid, 0, "%s", chan->name)))
505                 return;
506
507         for (x = goto_on_transfer; x && *x; x++) {
508                 if (*x == '^')
509                         *x = '|';
510         }
511         /* Make formats okay */
512         xferchan->readformat = chan->readformat;
513         xferchan->writeformat = chan->writeformat;
514         ast_channel_masquerade(xferchan, chan);
515         ast_parseable_goto(xferchan, goto_on_transfer);
516         xferchan->_state = AST_STATE_UP;
517         ast_clear_flag(xferchan, AST_FLAGS_ALL);        
518         xferchan->_softhangup = 0;
519         if ((f = ast_read(xferchan))) {
520                 ast_frfree(f);
521                 f = NULL;
522                 ast_pbx_start(xferchan);
523         } else {
524                 ast_hangup(xferchan);
525         }
526 }
527
528 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate, const char *language);
529
530 /*!
531  * \brief bridge the call 
532  * \param data thread bridge.
533  *
534  * Set Last Data for respective channels, reset cdr for channels
535  * bridge call, check if we're going back to dialplan
536  * if not hangup both legs of the call
537 */
538 static void *bridge_call_thread(void *data)
539 {
540         struct ast_bridge_thread_obj *tobj = data;
541         int res;
542
543         tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
544         tobj->chan->data = tobj->peer->name;
545         tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
546         tobj->peer->data = tobj->chan->name;
547
548         ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
549
550         if (tobj->return_to_pbx) {
551                 if (!ast_check_hangup(tobj->peer)) {
552                         ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
553                         res = ast_pbx_start(tobj->peer);
554                         if (res != AST_PBX_SUCCESS)
555                                 ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
556                 } else
557                         ast_hangup(tobj->peer);
558                 if (!ast_check_hangup(tobj->chan)) {
559                         ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
560                         res = ast_pbx_start(tobj->chan);
561                         if (res != AST_PBX_SUCCESS)
562                                 ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
563                 } else
564                         ast_hangup(tobj->chan);
565         } else {
566                 ast_hangup(tobj->chan);
567                 ast_hangup(tobj->peer);
568         }
569
570         ast_free(tobj);
571
572         return NULL;
573 }
574
575 /*!
576  * \brief create thread for the parked call
577  * \param data
578  *
579  * Create thread and attributes, call bridge_call_thread
580 */
581 static void bridge_call_thread_launch(void *data) 
582 {
583         pthread_t thread;
584         pthread_attr_t attr;
585         struct sched_param sched;
586
587         pthread_attr_init(&attr);
588         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
589         ast_pthread_create(&thread, &attr, bridge_call_thread, data);
590         pthread_attr_destroy(&attr);
591         memset(&sched, 0, sizeof(sched));
592         pthread_setschedparam(thread, SCHED_RR, &sched);
593 }
594
595 /*!
596  * \brief Announce call parking by ADSI
597  * \param chan .
598  * \param parkingexten .
599  * Create message to show for ADSI, display message.
600  * \retval 0 on success.
601  * \retval -1 on failure.
602 */
603 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
604 {
605         int res;
606         int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
607         char tmp[256];
608         char *message[5] = {NULL, NULL, NULL, NULL, NULL};
609
610         snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
611         message[0] = tmp;
612         res = ast_adsi_load_session(chan, NULL, 0, 1);
613         if (res == -1)
614                 return res;
615         return ast_adsi_print(chan, message, justify, 1);
616 }
617
618 /*! \brief Find parking lot name from channel */
619 static const char *findparkinglotname(struct ast_channel *chan)
620 {
621         const char *temp, *parkinglot = NULL;
622
623         /* Check if the channel has a parking lot */
624         if (!ast_strlen_zero(chan->parkinglot))
625                 parkinglot = chan->parkinglot;
626
627         /* Channel variables override everything */
628
629         if ((temp  = pbx_builtin_getvar_helper(chan, "PARKINGLOT")))
630                 return temp;
631
632         return parkinglot;
633 }
634
635 /*! \brief Notify metermaids that we've changed an extension */
636 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
637 {
638         ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'", 
639                 exten, context, ast_devstate2str(state));
640
641         ast_devstate_changed(state, "park:%s@%s", exten, context);
642 }
643
644 /*! \brief metermaids callback from devicestate.c */
645 static enum ast_device_state metermaidstate(const char *data)
646 {
647         char *context;
648         char *exten;
649
650         context = ast_strdupa(data);
651
652         exten = strsep(&context, "@");
653         if (!context)
654                 return AST_DEVICE_INVALID;
655         
656         ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
657
658         if (!ast_exists_extension(NULL, context, exten, 1, NULL))
659                 return AST_DEVICE_NOT_INUSE;
660
661         return AST_DEVICE_INUSE;
662 }
663
664 /*! Options to pass to park_call_full */
665 enum ast_park_call_options {
666         /*! Provide ringing to the parked caller instead of music on hold */
667         AST_PARK_OPT_RINGING =   (1 << 0),
668         /*! Randomly choose a parking spot for the caller instead of choosing
669          *  the first one that is available. */
670         AST_PARK_OPT_RANDOMIZE = (1 << 1),
671         /*! Do not announce the parking number */
672         AST_PARK_OPT_SILENCE = (1 << 2),
673 };
674
675 struct ast_park_call_args {
676         /*! How long to wait in the parking lot before the call gets sent back
677          *  to the specified return extension (or a best guess at where it came
678          *  from if not explicitly specified). */
679         int timeout;
680         /*! An output parameter to store the parking space where the parked caller
681          *  was placed. */
682         int *extout;
683         const char *orig_chan_name;
684         const char *return_con;
685         const char *return_ext;
686         int return_pri;
687         uint32_t flags;
688         /*! Parked user that has already obtained a parking space */
689         struct parkeduser *pu;
690 };
691
692 static struct parkeduser *park_space_reserve(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
693 {
694         struct parkeduser *pu;
695         int i, parking_space = -1, parking_range;
696         const char *parkinglotname = NULL;
697         const char *parkingexten;
698         struct ast_parkinglot *parkinglot = NULL;
699
700         if (peer)
701                 parkinglotname = findparkinglotname(peer);
702
703         if (parkinglotname) {
704                 ast_debug(1, "Found chanvar Parkinglot: %s\n", parkinglotname);
705                 parkinglot = find_parkinglot(parkinglotname);
706
707         }
708
709         /* Dynamically create parkinglot */
710         if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
711                 const char *dyn_context, *dyn_range;
712                 const char *parkinglotname_copy = NULL;
713                 struct ast_parkinglot *parkinglot_copy = NULL;
714                 int dyn_start, dyn_end;
715
716                 ast_channel_lock(chan);
717                 parkinglotname_copy = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
718                 dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
719                 dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
720                 ast_channel_unlock(chan);
721
722                 if (!ast_strlen_zero(parkinglotname_copy)) {
723                         parkinglot_copy = find_parkinglot(parkinglotname_copy);
724                 }
725                 if (!parkinglot_copy) {
726                         parkinglot_copy = parkinglot_addref(default_parkinglot);
727                         ast_debug(1, "Using default parking lot for copy\n");
728                 }
729                 if (!(parkinglot = copy_parkinglot(parkinglotname, parkinglot_copy))) {
730                         ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
731                 } else {
732                         if (!ast_strlen_zero(dyn_context)) {
733                                 ast_copy_string(parkinglot->parking_con, dyn_context, sizeof(parkinglot->parking_con));
734                         }
735                         if (!ast_strlen_zero(dyn_range)) {
736                                 if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
737                                         ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers\n");
738                                 } else {
739                                         parkinglot->parking_start = dyn_start;
740                                         parkinglot->parking_stop = dyn_end;
741                                 }
742                         }
743                         ao2_link(parkinglots, parkinglot);
744                 }
745
746                 if (parkinglot_copy) {
747                         /* unref our tempory copy */
748                         parkinglot_unref(parkinglot_copy);
749                         parkinglot_copy = NULL;
750                 }
751         }
752
753         if (!parkinglot) {
754                 parkinglot = parkinglot_addref(default_parkinglot);
755         }
756
757         if (option_debug)
758                 ast_log(LOG_DEBUG, "Parkinglot: %s\n", parkinglot->name);
759
760         /* Allocate memory for parking data */
761         if (!(pu = ast_calloc(1, sizeof(*pu)))) {
762                 parkinglot_unref(parkinglot);
763                 return NULL;
764         }
765
766         /* Lock parking list */
767         AST_LIST_LOCK(&parkinglot->parkings);
768         /* Check for channel variable PARKINGEXTEN */
769         ast_channel_lock(chan);
770         parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGEXTEN"), ""));
771         ast_channel_unlock(chan);
772         if (!ast_strlen_zero(parkingexten)) {
773                 /*!\note The API forces us to specify a numeric parking slot, even
774                  * though the architecture would tend to support non-numeric extensions
775                  * (as are possible with SIP, for example).  Hence, we enforce that
776                  * limitation here.  If extout was not numeric, we could permit
777                  * arbitrary non-numeric extensions.
778                  */
779         if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space < 0) {
780                         AST_LIST_UNLOCK(&parkinglot->parkings);
781                         parkinglot_unref(parkinglot);
782             free(pu);
783             ast_log(LOG_WARNING, "PARKINGEXTEN does not indicate a valid parking slot: '%s'.\n", parkingexten);
784             return NULL;
785         }
786         snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
787
788                 if (ast_exists_extension(NULL, parkinglot->parking_con, pu->parkingexten, 1, NULL)) {
789                         ast_log(LOG_WARNING, "Requested parking extension already exists: %s@%s\n", parkingexten, parkinglot->parking_con);
790                         AST_LIST_UNLOCK(&parkinglot->parkings);
791                         parkinglot_unref(parkinglot);
792                         ast_free(pu);
793                         return NULL;
794                 }
795         } else {
796                 int start;
797                 struct parkeduser *cur = NULL;
798
799                 /* Select parking space within range */
800                 parking_range = parkinglot->parking_stop - parkinglot->parking_start + 1;
801
802                 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
803                         start = ast_random() % (parkinglot->parking_stop - parkinglot->parking_start + 1);
804                 } else {
805                         start = parkinglot->parking_start;
806                 }
807
808                 for (i = start; 1; i++) {
809                         if (i == parkinglot->parking_stop + 1) {
810                                 i = parkinglot->parking_start - 1;
811                                 break;
812                         }
813
814                         AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
815                                 if (cur->parkingnum == i) {
816                                         break;
817                                 }
818                         }
819                         if (!cur) {
820                                 parking_space = i;
821                                 break;
822                         }
823                 }
824
825                 if (i == start - 1 && cur) {
826                         ast_log(LOG_WARNING, "No more parking spaces\n");
827                         ast_free(pu);
828                         AST_LIST_UNLOCK(&parkinglot->parkings);
829                         parkinglot_unref(parkinglot);
830                         return NULL;
831                 }
832                 /* Set pointer for next parking */
833                 if (parkinglot->parkfindnext) 
834                         parkinglot->parking_offset = parking_space - parkinglot->parking_start + 1;
835                 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
836         }
837
838         pu->notquiteyet = 1;
839         pu->parkingnum = parking_space;
840         pu->parkinglot = parkinglot_addref(parkinglot);
841         AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
842         parkinglot_unref(parkinglot);
843
844         return pu;
845 }
846
847 /* Park a call */
848 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
849 {
850         struct ast_context *con;
851         int parkingnum_copy;
852         struct parkeduser *pu = args->pu;
853         const char *event_from;
854
855         if (pu == NULL)
856                 pu = park_space_reserve(chan, peer, args);
857         if (pu == NULL)
858                 return 1; /* Continue execution if possible */
859
860         snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", pu->parkingnum);
861         
862         chan->appl = "Parked Call";
863         chan->data = NULL; 
864
865         pu->chan = chan;
866         
867         /* Put the parked channel on hold if we have two different channels */
868         if (chan != peer) {
869                 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
870                         ast_indicate(pu->chan, AST_CONTROL_RINGING);
871                 } else {
872                         ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
873                                 S_OR(pu->parkinglot->mohclass, NULL),
874                                 !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
875                 }
876         }
877         
878         pu->start = ast_tvnow();
879         pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->parkingtime;
880         parkingnum_copy = pu->parkingnum;
881         if (args->extout)
882                 *(args->extout) = pu->parkingnum;
883
884         if (peer) { 
885                 /* This is so ugly that it hurts, but implementing get_base_channel() on local channels
886                         could have ugly side effects.  We could have transferer<->local,1<->local,2<->parking
887                         and we need the callback name to be that of transferer.  Since local,1/2 have the same
888                         name we can be tricky and just grab the bridged channel from the other side of the local
889                 */
890                 if (!strcasecmp(peer->tech->type, "Local")) {
891                         struct ast_channel *tmpchan, *base_peer;
892                         char other_side[AST_CHANNEL_NAME];
893                         char *c;
894                         ast_copy_string(other_side, S_OR(args->orig_chan_name, peer->name), sizeof(other_side));
895                         if ((c = strrchr(other_side, ';'))) {
896                                 *++c = '1';
897                         }
898                         if ((tmpchan = ast_channel_get_by_name(other_side))) {
899                                 ast_channel_lock(tmpchan);
900                                 if ((base_peer = ast_bridged_channel(tmpchan))) {
901                                         ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
902                                 }
903                                 ast_channel_unlock(tmpchan);
904                                 tmpchan = ast_channel_unref(tmpchan);
905                         }
906                 } else {
907                         ast_copy_string(pu->peername, S_OR(args->orig_chan_name, peer->name), sizeof(pu->peername));
908                 }
909         }
910
911         /* Remember what had been dialed, so that if the parking
912            expires, we try to come back to the same place */
913
914         pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
915
916         /* If extension has options specified, they override all other possibilities
917         such as the returntoorigin flag and transferred context. Information on
918         extension options is lost here, so we set a flag */
919
920         ast_copy_string(pu->context, 
921                 S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)), 
922                 sizeof(pu->context));
923         ast_copy_string(pu->exten, 
924                 S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)), 
925                 sizeof(pu->exten));
926         pu->priority = args->return_pri ? args->return_pri : 
927                 (chan->macropriority ? chan->macropriority : chan->priority);
928
929         /* If parking a channel directly, don't quiet yet get parking running on it.
930          * All parking lot entries are put into the parking lot with notquiteyet on. */
931         if (peer != chan) 
932                 pu->notquiteyet = 0;
933
934         /* Wake up the (presumably select()ing) thread */
935         pthread_kill(parking_thread, SIGURG);
936         ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n", pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->context, pu->exten, pu->priority, (pu->parkingtime/1000));
937
938         ast_cel_report_event(pu->chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
939
940         if (peer) {
941                 event_from = peer->name;
942         } else {
943                 event_from = pbx_builtin_getvar_helper(chan, "BLINDTRANSFER");
944         }
945
946         ast_manager_event(pu->chan, EVENT_FLAG_CALL, "ParkedCall",
947                 "Exten: %s\r\n"
948                 "Channel: %s\r\n"
949                 "Parkinglot: %s\r\n"
950                 "From: %s\r\n"
951                 "Timeout: %ld\r\n"
952                 "CallerIDNum: %s\r\n"
953                 "CallerIDName: %s\r\n"
954                 "Uniqueid: %s\r\n",
955                 pu->parkingexten, pu->chan->name, pu->parkinglot->name, event_from ? event_from : "",
956                 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
957                 S_OR(pu->chan->cid.cid_num, "<unknown>"),
958                 S_OR(pu->chan->cid.cid_name, "<unknown>"),
959                 pu->chan->uniqueid
960                 );
961
962         if (peer && adsipark && ast_adsi_available(peer)) {
963                 adsi_announce_park(peer, pu->parkingexten);     /* Only supports parking numbers */
964                 ast_adsi_unload_session(peer);
965         }
966
967         con = ast_context_find_or_create(NULL, NULL, pu->parkinglot->parking_con, registrar);
968         if (!con)       /* Still no context? Bad */
969                 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", pu->parkinglot->parking_con);
970         if (con) {
971                 if (!ast_add_extension2(con, 1, pu->parkingexten, 1, NULL, NULL, parkedcall, ast_strdup(pu->parkingexten), ast_free_ptr, registrar))
972                         notify_metermaids(pu->parkingexten, pu->parkinglot->parking_con, AST_DEVICE_INUSE);
973         }
974
975         AST_LIST_UNLOCK(&pu->parkinglot->parkings);
976
977         /* Only say number if it's a number and the channel hasn't been masqueraded away */
978         if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE) && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
979                 /* If a channel is masqueraded into peer while playing back the parking slot number do not continue playing it back. This is the case if an attended transfer occurs. */
980                 ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
981                 /* Tell the peer channel the number of the parking space */
982                 ast_say_digits(peer, pu->parkingnum, "", peer->language);
983                 ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
984         }
985         if (peer == chan) { /* pu->notquiteyet = 1 */
986                 /* Wake up parking thread if we're really done */
987                 ast_indicate_data(pu->chan, AST_CONTROL_HOLD, 
988                         S_OR(pu->parkinglot->mohclass, NULL),
989                         !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
990                 pu->notquiteyet = 0;
991                 pthread_kill(parking_thread, SIGURG);
992         }
993         return 0;
994 }
995
996 /*! \brief Park a call */
997 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
998 {
999         struct ast_park_call_args args = {
1000                 .timeout = timeout,
1001                 .extout = extout,
1002         };
1003
1004         return park_call_full(chan, peer, &args);
1005 }
1006
1007 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)
1008 {
1009         struct ast_channel *chan;
1010         struct ast_frame *f;
1011         int park_status;
1012         struct ast_park_call_args park_args = {0,};
1013
1014         if (!args) {
1015                 args = &park_args;
1016                 args->timeout = timeout;
1017                 args->extout = extout;
1018         }
1019
1020         if ((args->pu = park_space_reserve(rchan, peer, args)) == NULL) {
1021                 if (peer)
1022                         ast_stream_and_wait(peer, "beeperr", "");
1023                 return AST_FEATURE_RETURN_PARKFAILED;
1024         }
1025
1026         /* Make a new, fake channel that we'll use to masquerade in the real one */
1027         if (!(chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten, rchan->context, rchan->linkedid, rchan->amaflags, "Parked/%s",rchan->name))) {
1028                 ast_log(LOG_WARNING, "Unable to create parked channel\n");
1029                 return -1;
1030         }
1031
1032         /* Make formats okay */
1033         chan->readformat = rchan->readformat;
1034         chan->writeformat = rchan->writeformat;
1035         ast_channel_masquerade(chan, rchan);
1036
1037         /* Setup the extensions and such */
1038         set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
1039
1040         /* Setup the macro extension and such */
1041         ast_copy_string(chan->macrocontext,rchan->macrocontext,sizeof(chan->macrocontext));
1042         ast_copy_string(chan->macroexten,rchan->macroexten,sizeof(chan->macroexten));
1043         chan->macropriority = rchan->macropriority;
1044
1045         /* Make the masq execute */
1046         if ((f = ast_read(chan)))
1047                 ast_frfree(f);
1048
1049         if (peer == rchan) {
1050                 peer = chan;
1051         }
1052
1053         if (peer && (!play_announcement && args == &park_args)) {
1054                 args->orig_chan_name = ast_strdupa(peer->name);
1055         }
1056
1057         park_status = park_call_full(chan, peer, args);
1058         if (park_status == 1) {
1059         /* would be nice to play "invalid parking extension" */
1060                 ast_hangup(chan);
1061                 return -1;
1062         }
1063
1064         return 0;
1065 }
1066
1067 /* Park call via masquraded channel */
1068 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1069 {
1070         return masq_park_call(rchan, peer, timeout, extout, 0, NULL);
1071 }
1072
1073 static int masq_park_call_announce_args(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
1074 {
1075         return masq_park_call(rchan, peer, 0, NULL, 1, args);
1076 }
1077
1078 static int masq_park_call_announce(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1079 {
1080         return masq_park_call(rchan, peer, timeout, extout, 1, NULL);
1081 }
1082
1083 /*! 
1084  * \brief set caller and callee according to the direction 
1085  * \param caller, callee, peer, chan, sense
1086  *
1087  * Detect who triggered feature and set callee/caller variables accordingly
1088 */
1089 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
1090         struct ast_channel *peer, struct ast_channel *chan, int sense)
1091 {
1092         if (sense == FEATURE_SENSE_PEER) {
1093                 *caller = peer;
1094                 *callee = chan;
1095         } else {
1096                 *callee = peer;
1097                 *caller = chan;
1098         }
1099 }
1100
1101 /*! 
1102  * \brief support routing for one touch call parking
1103  * \param chan channel parking call
1104  * \param peer channel to be parked
1105  * \param config unsed
1106  * \param code unused
1107  * \param sense feature options
1108  *
1109  * \param data
1110  * Setup channel, set return exten,priority to 's,1'
1111  * answer chan, sleep chan, park call
1112 */
1113 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1114 {
1115         struct ast_channel *parker;
1116         struct ast_channel *parkee;
1117         int res = 0;
1118
1119         set_peers(&parker, &parkee, peer, chan, sense);
1120         /* we used to set chan's exten and priority to "s" and 1
1121            here, but this generates (in some cases) an invalid
1122            extension, and if "s" exists, could errantly
1123            cause execution of extensions you don't expect. It
1124            makes more sense to let nature take its course
1125            when chan finishes, and let the pbx do its thing
1126            and hang up when the park is over.
1127         */
1128         if (chan->_state != AST_STATE_UP)
1129                 res = ast_answer(chan);
1130         if (!res)
1131                 res = ast_safe_sleep(chan, 1000);
1132
1133         if (!res) { /* one direction used to call park_call.... */
1134                 res = masq_park_call_announce(parkee, parker, 0, NULL);
1135                 /* PBX should hangup zombie channel if a masquerade actually occurred (res=0) */
1136         }
1137
1138         return res;
1139 }
1140
1141 /*! \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
1142         other channel during the message, so please don't use this for very long messages
1143  */
1144 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
1145 {
1146         /* First play for caller, put other channel on auto service */
1147         if (ast_autoservice_start(callee_chan))
1148                 return -1;
1149         if (ast_stream_and_wait(caller_chan, audiofile, "")) {
1150                 ast_log(LOG_WARNING, "Failed to play automon message!\n");
1151                 ast_autoservice_stop(callee_chan);
1152                 return -1;
1153         }
1154         if (ast_autoservice_stop(callee_chan))
1155                 return -1;
1156         /* Then play for callee, put other channel on auto service */
1157         if (ast_autoservice_start(caller_chan))
1158                 return -1;
1159         if (ast_stream_and_wait(callee_chan, audiofile, "")) {
1160                 ast_log(LOG_WARNING, "Failed to play automon message !\n");
1161                 ast_autoservice_stop(caller_chan);
1162                 return -1;
1163         }
1164         if (ast_autoservice_stop(caller_chan))
1165                 return -1;
1166         return(0);
1167 }
1168
1169 /*!
1170  * \brief Monitor a channel by DTMF
1171  * \param chan channel requesting monitor
1172  * \param peer channel to be monitored
1173  * \param config
1174  * \param code
1175  * \param sense feature options
1176  *
1177  * \param data
1178  * Check monitor app enabled, setup channels, both caller/callee chans not null
1179  * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
1180  * \retval AST_FEATURE_RETURN_SUCCESS on success.
1181  * \retval -1 on error.
1182 */
1183 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1184 {
1185         char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1186         int x = 0;
1187         size_t len;
1188         struct ast_channel *caller_chan, *callee_chan;
1189         const char *automon_message_start = NULL;
1190         const char *automon_message_stop = NULL;
1191
1192         if (!monitor_ok) {
1193                 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1194                 return -1;
1195         }
1196
1197         if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
1198                 monitor_ok = 0;
1199                 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1200                 return -1;
1201         }
1202
1203         set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1204         if (caller_chan) {      /* Find extra messages */
1205                 automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
1206                 automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
1207         }
1208
1209         if (!ast_strlen_zero(courtesytone)) {   /* Play courtesy tone if configured */
1210                 if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
1211                         return -1;
1212                 }
1213         }
1214         
1215         if (callee_chan->monitor) {
1216                 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
1217                 if (!ast_strlen_zero(automon_message_stop)) {
1218                         play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
1219                 }
1220                 callee_chan->monitor->stop(callee_chan, 1);
1221                 return AST_FEATURE_RETURN_SUCCESS;
1222         }
1223
1224         if (caller_chan && callee_chan) {
1225                 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
1226                 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
1227                 const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
1228
1229                 if (!touch_format)
1230                         touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
1231
1232                 if (!touch_monitor)
1233                         touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
1234         
1235                 if (!touch_monitor_prefix)
1236                         touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
1237         
1238                 if (touch_monitor) {
1239                         len = strlen(touch_monitor) + 50;
1240                         args = alloca(len);
1241                         touch_filename = alloca(len);
1242                         snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
1243                         snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
1244                 } else {
1245                         caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
1246                         callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
1247                         len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
1248                         args = alloca(len);
1249                         touch_filename = alloca(len);
1250                         snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
1251                         snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
1252                 }
1253
1254                 for(x = 0; x < strlen(args); x++) {
1255                         if (args[x] == '/')
1256                                 args[x] = '-';
1257                 }
1258                 
1259                 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
1260
1261                 pbx_exec(callee_chan, monitor_app, args);
1262                 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
1263                 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
1264
1265                 if (!ast_strlen_zero(automon_message_start)) {  /* Play start message for both channels */
1266                         play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
1267                 }
1268         
1269                 return AST_FEATURE_RETURN_SUCCESS;
1270         }
1271         
1272         ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");   
1273         return -1;
1274 }
1275
1276 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1277 {
1278         char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1279         int x = 0;
1280         size_t len;
1281         struct ast_channel *caller_chan, *callee_chan;
1282         const char *mixmonitor_spy_type = "MixMonitor";
1283         int count = 0;
1284
1285         if (!mixmonitor_ok) {
1286                 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
1287                 return -1;
1288         }
1289
1290         if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
1291                 mixmonitor_ok = 0;
1292                 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
1293                 return -1;
1294         }
1295
1296         set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1297
1298         if (!ast_strlen_zero(courtesytone)) {
1299                 if (ast_autoservice_start(callee_chan))
1300                         return -1;
1301                 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
1302                         ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
1303                         ast_autoservice_stop(callee_chan);
1304                         return -1;
1305                 }
1306                 if (ast_autoservice_stop(callee_chan))
1307                         return -1;
1308         }
1309
1310         ast_channel_lock(callee_chan);
1311         count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
1312         ast_channel_unlock(callee_chan);
1313
1314         /* This means a mixmonitor is attached to the channel, running or not is unknown. */
1315         if (count > 0) {
1316                 
1317                 ast_verb(3, "User hit '%s' to stop recording call.\n", code);
1318
1319                 /* Make sure they are running */
1320                 ast_channel_lock(callee_chan);
1321                 count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
1322                 ast_channel_unlock(callee_chan);
1323                 if (count > 0) {
1324                         if (!stopmixmonitor_ok) {
1325                                 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
1326                                 return -1;
1327                         }
1328                         if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
1329                                 stopmixmonitor_ok = 0;
1330                                 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
1331                                 return -1;
1332                         } else {
1333                                 pbx_exec(callee_chan, stopmixmonitor_app, "");
1334                                 return AST_FEATURE_RETURN_SUCCESS;
1335                         }
1336                 }
1337                 
1338                 ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");      
1339         }                       
1340
1341         if (caller_chan && callee_chan) {
1342                 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
1343                 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
1344
1345                 if (!touch_format)
1346                         touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
1347
1348                 if (!touch_monitor)
1349                         touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
1350
1351                 if (touch_monitor) {
1352                         len = strlen(touch_monitor) + 50;
1353                         args = alloca(len);
1354                         touch_filename = alloca(len);
1355                         snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
1356                         snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
1357                 } else {
1358                         caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
1359                         callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
1360                         len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
1361                         args = alloca(len);
1362                         touch_filename = alloca(len);
1363                         snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
1364                         snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
1365                 }
1366
1367                 for( x = 0; x < strlen(args); x++) {
1368                         if (args[x] == '/')
1369                                 args[x] = '-';
1370                 }
1371
1372                 ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
1373
1374                 pbx_exec(callee_chan, mixmonitor_app, args);
1375                 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
1376                 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
1377                 return AST_FEATURE_RETURN_SUCCESS;
1378         
1379         }
1380
1381         ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
1382         return -1;
1383
1384 }
1385
1386 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1387 {
1388         ast_verb(4, "User hit '%s' to disconnect call.\n", code);
1389         return AST_FEATURE_RETURN_HANGUP;
1390 }
1391
1392 static int finishup(struct ast_channel *chan)
1393 {
1394         ast_indicate(chan, AST_CONTROL_UNHOLD);
1395
1396         return ast_autoservice_stop(chan);
1397 }
1398
1399 /*!
1400  * \brief Find the context for the transfer
1401  * \param transferer
1402  * \param transferee
1403  * 
1404  * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
1405  * \return a context string
1406 */
1407 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
1408 {
1409         const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
1410         if (ast_strlen_zero(s)) {
1411                 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
1412         }
1413         if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
1414                 s = transferer->macrocontext;
1415         }
1416         if (ast_strlen_zero(s)) {
1417                 s = transferer->context;
1418         }
1419         return s;  
1420 }
1421
1422 /*!
1423  * \brief Blind transfer user to another extension
1424  * \param chan channel to be transfered
1425  * \param peer channel initiated blind transfer
1426  * \param config
1427  * \param code
1428  * \param data
1429  * \param sense  feature options
1430  * 
1431  * Place chan on hold, check if transferred to parkinglot extension,
1432  * otherwise check extension exists and transfer caller.
1433  * \retval AST_FEATURE_RETURN_SUCCESS.
1434  * \retval -1 on failure.
1435 */
1436 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1437 {
1438         struct ast_channel *transferer;
1439         struct ast_channel *transferee;
1440         const char *transferer_real_context;
1441         char xferto[256];
1442         int res, parkstatus = 0;
1443
1444         set_peers(&transferer, &transferee, peer, chan, sense);
1445         transferer_real_context = real_ctx(transferer, transferee);
1446         /* Start autoservice on chan while we talk to the originator */
1447         ast_autoservice_start(transferee);
1448         ast_indicate(transferee, AST_CONTROL_HOLD);
1449
1450         memset(xferto, 0, sizeof(xferto));
1451
1452         /* Transfer */
1453         res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
1454         if (res < 0) {
1455                 finishup(transferee);
1456                 return -1; /* error ? */
1457         }
1458         if (res > 0)    /* If they've typed a digit already, handle it */
1459                 xferto[0] = (char) res;
1460
1461         ast_stopstream(transferer);
1462         res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
1463         if (res < 0) {  /* hangup, would be 0 for invalid and 1 for valid */
1464                 finishup(transferee);
1465                 return res;
1466         }
1467         if (!strcmp(xferto, ast_parking_ext())) {
1468                 res = finishup(transferee);
1469                 if (res)
1470                         res = -1;
1471                 else if (!(parkstatus = masq_park_call_announce(transferee, transferer, 0, NULL))) {    /* success */
1472                         /* We return non-zero, but tell the PBX not to hang the channel when
1473                            the thread dies -- We have to be careful now though.  We are responsible for 
1474                            hanging up the channel, else it will never be hung up! */
1475
1476                         return 0;
1477                 } else {
1478                         ast_log(LOG_WARNING, "Unable to park call %s, parkstatus = %d\n", transferee->name, parkstatus);
1479                 }
1480                 /*! \todo XXX Maybe we should have another message here instead of invalid extension XXX */
1481         } else if (ast_exists_extension(transferee, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
1482                 ast_cel_report_event(transferer, AST_CEL_BLINDTRANSFER, NULL, xferto, transferee);
1483                 pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
1484                 pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
1485                 res=finishup(transferee);
1486                 if (!transferer->cdr) { /* this code should never get called (in a perfect world) */
1487                         transferer->cdr=ast_cdr_alloc();
1488                         if (transferer->cdr) {
1489                                 ast_cdr_init(transferer->cdr, transferer); /* initialize our channel's cdr */
1490                                 ast_cdr_start(transferer->cdr);
1491                         }
1492                 }
1493                 if (transferer->cdr) {
1494                         struct ast_cdr *swap = transferer->cdr;
1495                         ast_log(LOG_DEBUG,"transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
1496                                         transferer->name, transferee->name, transferer->cdr->lastapp, transferer->cdr->lastdata, 
1497                                         transferer->cdr->channel, transferer->cdr->dstchannel);
1498                         ast_log(LOG_DEBUG,"TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
1499                                         transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel, transferee->cdr->dstchannel);
1500                         ast_log(LOG_DEBUG,"transferer_real_context=%s; xferto=%s\n", transferer_real_context, xferto);
1501                         /* swap cdrs-- it will save us some time & work */
1502                         transferer->cdr = transferee->cdr;
1503                         transferee->cdr = swap;
1504                 }
1505                 if (!transferee->pbx) {
1506                         /* Doh!  Use our handy async_goto functions */
1507                         ast_verb(3, "Transferring %s to '%s' (context %s) priority 1\n"
1508                                                                 ,transferee->name, xferto, transferer_real_context);
1509                         if (ast_async_goto(transferee, transferer_real_context, xferto, 1))
1510                                 ast_log(LOG_WARNING, "Async goto failed :-(\n");
1511                 } else {
1512                         /* Set the channel's new extension, since it exists, using transferer context */
1513                         ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
1514                         ast_log(LOG_DEBUG,"ABOUT TO AST_ASYNC_GOTO, have a pbx... set HANGUP_DONT on chan=%s\n", transferee->name);
1515                         if (ast_channel_connected_line_macro(transferee, transferer, &transferer->connected, 1, 0)) {
1516                                 ast_channel_update_connected_line(transferee, &transferer->connected);
1517                         }
1518                         set_c_e_p(transferee, transferer_real_context, xferto, 0);
1519                 }
1520                 check_goto_on_transfer(transferer);
1521                 return res;
1522         } else {
1523                 ast_verb(3, "Unable to find extension '%s' in context '%s'\n", xferto, transferer_real_context);
1524         }
1525         if (parkstatus != AST_FEATURE_RETURN_PARKFAILED && ast_stream_and_wait(transferer, xferfailsound, AST_DIGIT_ANY) < 0) {
1526                 finishup(transferee);
1527                 return -1;
1528         }
1529         ast_stopstream(transferer);
1530         res = finishup(transferee);
1531         if (res) {
1532                 ast_verb(2, "Hungup during autoservice stop on '%s'\n", transferee->name);
1533                 return res;
1534         }
1535         return AST_FEATURE_RETURN_SUCCESS;
1536 }
1537
1538 /*!
1539  * \brief make channels compatible
1540  * \param c
1541  * \param newchan
1542  * \retval 0 on success.
1543  * \retval -1 on failure.
1544 */
1545 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
1546 {
1547         if (ast_channel_make_compatible(c, newchan) < 0) {
1548                 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
1549                         c->name, newchan->name);
1550                 ast_hangup(newchan);
1551                 return -1;
1552         }
1553         return 0;
1554 }
1555
1556 /*!
1557  * \brief Attended transfer
1558  * \param chan transfered user
1559  * \param peer person transfering call
1560  * \param config
1561  * \param code
1562  * \param sense feature options
1563  * 
1564  * \param data
1565  * Get extension to transfer to, if you cannot generate channel (or find extension) 
1566  * return to host channel. After called channel answered wait for hangup of transferer,
1567  * bridge call between transfer peer (taking them off hold) to attended transfer channel.
1568  *
1569  * \return -1 on failure
1570 */
1571 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1572 {
1573         struct ast_channel *transferer;
1574         struct ast_channel *transferee;
1575         const char *transferer_real_context;
1576         char xferto[256] = "";
1577         int res;
1578         int outstate=0;
1579         struct ast_channel *newchan;
1580         struct ast_channel *xferchan;
1581         struct ast_bridge_thread_obj *tobj;
1582         struct ast_bridge_config bconfig;
1583         struct ast_frame *f;
1584         int l;
1585         struct ast_party_connected_line connected_line;
1586         struct ast_datastore *features_datastore;
1587         struct ast_dial_features *dialfeatures = NULL;
1588
1589         ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
1590         set_peers(&transferer, &transferee, peer, chan, sense);
1591         transferer_real_context = real_ctx(transferer, transferee);
1592         /* Start autoservice on chan while we talk to the originator */
1593         ast_autoservice_start(transferee);
1594         ast_indicate(transferee, AST_CONTROL_HOLD);
1595         
1596         /* Transfer */
1597         res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
1598         if (res < 0) {
1599                 finishup(transferee);
1600                 return res;
1601         }
1602         if (res > 0) /* If they've typed a digit already, handle it */
1603                 xferto[0] = (char) res;
1604
1605         /* this is specific of atxfer */
1606         res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
1607         if (res < 0) {  /* hangup, would be 0 for invalid and 1 for valid */
1608                 finishup(transferee);
1609                 return res;
1610         }
1611         if (res == 0) {
1612                 ast_log(LOG_WARNING, "Did not read data.\n");
1613                 finishup(transferee);
1614                 if (ast_stream_and_wait(transferer, "beeperr", ""))
1615                         return -1;
1616                 return AST_FEATURE_RETURN_SUCCESS;
1617         }
1618
1619         /* valid extension, res == 1 */
1620         if (!ast_exists_extension(transferer, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
1621                 ast_log(LOG_WARNING, "Extension %s does not exist in context %s\n",xferto,transferer_real_context);
1622                 finishup(transferee);
1623                 if (ast_stream_and_wait(transferer, "beeperr", ""))
1624                         return -1;
1625                 return AST_FEATURE_RETURN_SUCCESS;
1626         }
1627
1628         /* If we are attended transfering to parking, just use builtin_parkcall instead of trying to track all of
1629          * the different variables for handling this properly with a builtin_atxfer */
1630         if (!strcmp(xferto, ast_parking_ext())) {
1631                 finishup(transferee);
1632                 return builtin_parkcall(chan, peer, config, code, sense, data);
1633         }
1634
1635         l = strlen(xferto);
1636         snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);     /* append context */
1637
1638         /* If we are performing an attended transfer and we have two channels involved then
1639            copy sound file information to play upon attended transfer completion */
1640         if (transferee) {
1641                 const char *chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
1642                 const char *chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
1643
1644                 if (!ast_strlen_zero(chan1_attended_sound)) {
1645                         pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
1646                 }
1647                 if (!ast_strlen_zero(chan2_attended_sound)) {
1648                         pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
1649                 }
1650         }
1651
1652         newchan = feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
1653                 xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1, transferer->language);
1654
1655         ast_party_connected_line_init(&connected_line);
1656         if (!ast_check_hangup(transferer)) {
1657                 /* Transferer is up - old behaviour */
1658                 ast_indicate(transferer, -1);
1659                 if (!newchan) {
1660                         finishup(transferee);
1661                         /* any reason besides user requested cancel and busy triggers the failed sound */
1662                         if (outstate != AST_CONTROL_UNHOLD && outstate != AST_CONTROL_BUSY &&
1663                                 ast_stream_and_wait(transferer, xferfailsound, ""))
1664                                 return -1;
1665                         if (ast_stream_and_wait(transferer, xfersound, ""))
1666                                 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1667                         return AST_FEATURE_RETURN_SUCCESS;
1668                 }
1669
1670                 if (check_compat(transferer, newchan)) {
1671                         /* we do mean transferee here, NOT transferer */
1672                         finishup(transferee);
1673                         return -1;
1674                 }
1675                 memset(&bconfig,0,sizeof(struct ast_bridge_config));
1676                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
1677                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
1678                 /* We need to get the transferer's connected line information copied
1679                  * at this point because he is likely to hang up during the bridge with
1680                  * newchan. This info will be used down below before bridging the 
1681                  * transferee and newchan
1682                  *
1683                  * As a result, we need to be sure to free this data before returning
1684                  * or overwriting it.
1685                  */
1686                 ast_channel_lock(transferer);
1687                 ast_party_connected_line_copy(&connected_line, &transferer->connected);
1688                 ast_channel_unlock(transferer);
1689                 res = ast_bridge_call(transferer, newchan, &bconfig);
1690                 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
1691                         ast_hangup(newchan);
1692                         if (ast_stream_and_wait(transferer, xfersound, ""))
1693                                 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1694                         finishup(transferee);
1695                         transferer->_softhangup = 0;
1696                         ast_party_connected_line_free(&connected_line);
1697                         return AST_FEATURE_RETURN_SUCCESS;
1698                 }
1699
1700                 ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
1701
1702                 if (check_compat(transferee, newchan)) {
1703                         finishup(transferee);
1704                         ast_party_connected_line_free(&connected_line);
1705                         return -1;
1706                 }
1707                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
1708
1709                 if ((ast_autoservice_stop(transferee) < 0)
1710                  || (ast_waitfordigit(transferee, 100) < 0)
1711                  || (ast_waitfordigit(newchan, 100) < 0)
1712                  || ast_check_hangup(transferee)
1713                  || ast_check_hangup(newchan)) {
1714                         ast_hangup(newchan);
1715                         ast_party_connected_line_free(&connected_line);
1716                         return -1;
1717                 }
1718                 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
1719                 if (!xferchan) {
1720                         ast_hangup(newchan);
1721                         ast_party_connected_line_free(&connected_line);
1722                         return -1;
1723                 }
1724                 /* Make formats okay */
1725                 xferchan->visible_indication = transferer->visible_indication;
1726                 xferchan->readformat = transferee->readformat;
1727                 xferchan->writeformat = transferee->writeformat;
1728                 ast_channel_masquerade(xferchan, transferee);
1729                 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
1730                 xferchan->_state = AST_STATE_UP;
1731                 ast_clear_flag(xferchan, AST_FLAGS_ALL);
1732                 xferchan->_softhangup = 0;
1733                 if ((f = ast_read(xferchan)))
1734                         ast_frfree(f);
1735                 newchan->_state = AST_STATE_UP;
1736                 ast_clear_flag(newchan, AST_FLAGS_ALL);
1737                 newchan->_softhangup = 0;
1738                 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
1739                         ast_hangup(xferchan);
1740                         ast_hangup(newchan);
1741                         ast_party_connected_line_free(&connected_line);
1742                         return -1;
1743                 }
1744
1745                 ast_channel_lock(newchan);
1746                 if ((features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL))) {
1747                                 dialfeatures = features_datastore->data;
1748                 }
1749                 ast_channel_unlock(newchan);
1750
1751                 if (dialfeatures) {
1752                         /* newchan should always be the callee and shows up as callee in dialfeatures, but for some reason
1753                            I don't currently understand, the abilities of newchan seem to be stored on the caller side */
1754                         ast_copy_flags(&(config->features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
1755                         dialfeatures = NULL;
1756                 }
1757
1758                 ast_channel_lock(xferchan);
1759                 if ((features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL))) {
1760                         dialfeatures = features_datastore->data;
1761                 }
1762                 ast_channel_unlock(xferchan);
1763          
1764                 if (dialfeatures) {
1765                         ast_copy_flags(&(config->features_caller), &(dialfeatures->features_caller), AST_FLAGS_ALL);
1766                 }
1767          
1768                 tobj->chan = newchan;
1769                 tobj->peer = xferchan;
1770                 tobj->bconfig = *config;
1771
1772                 if (tobj->bconfig.end_bridge_callback_data_fixup) {
1773                         tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
1774                 }
1775
1776                 /* Due to a limitation regarding when callerID is set on a Local channel,
1777                  * we use the transferer's connected line information here.
1778                  */
1779
1780                 /* xferchan is transferee, and newchan is the transfer target
1781                  * So...in a transfer, who is the caller and who is the callee?
1782                  *
1783                  * When the call is originally made, it is clear who is caller and callee.
1784                  * When a transfer occurs, it is my humble opinion that the transferee becomes
1785                  * the caller, and the transfer target is the callee.
1786                  *
1787                  * The problem is that these macros were set with the intention of the original
1788                  * caller and callee taking those roles. A transfer can totally mess things up,
1789                  * to be technical. What sucks even more is that you can't effectively change
1790                  * the macros in the dialplan during the call from the transferer to the transfer
1791                  * target because the transferee is stuck with whatever role he originally had.
1792                  *
1793                  * I think the answer here is just to make sure that it is well documented that
1794                  * during a transfer, the transferee is the "caller" and the transfer target
1795                  * is the "callee."
1796                  *
1797                  * This means that if party A calls party B, and party A transfers party B to
1798                  * party C, then B has switched roles for the call. Now party B will have the
1799                  * caller macro called on his channel instead of the callee macro.
1800                  *
1801                  * Luckily, the method by which the bridge is launched here ensures that the 
1802                  * transferee is the "chan" on the bridge and the transfer target is the "peer,"
1803                  * so my idea for the roles post-transfer does not require extensive code changes.
1804                  */
1805                 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1806                 if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
1807                         ast_channel_update_connected_line(xferchan, &connected_line);
1808                 }
1809                 ast_channel_lock(xferchan);
1810                 ast_connected_line_copy_from_caller(&connected_line, &xferchan->cid);
1811                 ast_channel_unlock(xferchan);
1812                 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1813                 if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
1814                         ast_channel_update_connected_line(newchan, &connected_line);
1815                 }
1816                 ast_party_connected_line_free(&connected_line);
1817
1818                 if (ast_stream_and_wait(newchan, xfersound, ""))
1819                         ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1820                 bridge_call_thread_launch(tobj);
1821                 return -1;      /* XXX meaning the channel is bridged ? */
1822         } else if (!ast_check_hangup(transferee)) {
1823                 /* act as blind transfer */
1824                 if (ast_autoservice_stop(transferee) < 0) {
1825                         ast_hangup(newchan);
1826                         return -1;
1827                 }
1828
1829                 if (!newchan) {
1830                         unsigned int tries = 0;
1831                         char *transferer_tech, *transferer_name = ast_strdupa(transferer->name);
1832
1833                         transferer_tech = strsep(&transferer_name, "/");
1834                         transferer_name = strsep(&transferer_name, "-");
1835
1836                         if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
1837                                 ast_log(LOG_WARNING, "Transferer has invalid channel name: '%s'\n", transferer->name);
1838                                 if (ast_stream_and_wait(transferee, "beeperr", ""))
1839                                         return -1;
1840                                 return AST_FEATURE_RETURN_SUCCESS;
1841                         }
1842
1843                         ast_log(LOG_NOTICE, "We're trying to call %s/%s\n", transferer_tech, transferer_name);
1844                         newchan = feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
1845                                 transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0, transferer->language);
1846                         while (!newchan && !atxferdropcall && tries < atxfercallbackretries) {
1847                                 /* Trying to transfer again */
1848                                 ast_autoservice_start(transferee);
1849                                 ast_indicate(transferee, AST_CONTROL_HOLD);
1850
1851                                 newchan = feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
1852                                         xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1, transferer->language);
1853                                 if (ast_autoservice_stop(transferee) < 0) {
1854                                         if (newchan)
1855                                                 ast_hangup(newchan);
1856                                         return -1;
1857                                 }
1858                                 if (!newchan) {
1859                                         /* Transfer failed, sleeping */
1860                                         ast_debug(1, "Sleeping for %d ms before callback.\n", atxferloopdelay);
1861                                         ast_safe_sleep(transferee, atxferloopdelay);
1862                                         ast_debug(1, "Trying to callback...\n");
1863                                         newchan = feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
1864                                                 transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0, transferer->language);
1865                                 }
1866                                 tries++;
1867                         }
1868                 }
1869                 if (!newchan)
1870                         return -1;
1871
1872                 ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
1873
1874                 /* newchan is up, we should prepare transferee and bridge them */
1875                 if (check_compat(transferee, newchan)) {
1876                         finishup(transferee);
1877                         return -1;
1878                 }
1879                 ast_indicate(transferee, AST_CONTROL_UNHOLD);
1880
1881                 if ((ast_waitfordigit(transferee, 100) < 0)
1882                    || (ast_waitfordigit(newchan, 100) < 0)
1883                    || ast_check_hangup(transferee)
1884                    || ast_check_hangup(newchan)) {
1885                         ast_hangup(newchan);
1886                         return -1;
1887                 }
1888
1889                 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
1890                 if (!xferchan) {
1891                         ast_hangup(newchan);
1892                         return -1;
1893                 }
1894                 /* Make formats okay */
1895                 xferchan->visible_indication = transferer->visible_indication;
1896                 xferchan->readformat = transferee->readformat;
1897                 xferchan->writeformat = transferee->writeformat;
1898                 ast_channel_masquerade(xferchan, transferee);
1899                 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
1900                 xferchan->_state = AST_STATE_UP;
1901                 ast_clear_flag(xferchan, AST_FLAGS_ALL);
1902                 xferchan->_softhangup = 0;
1903                 if ((f = ast_read(xferchan)))
1904                         ast_frfree(f);
1905                 newchan->_state = AST_STATE_UP;
1906                 ast_clear_flag(newchan, AST_FLAGS_ALL);
1907                 newchan->_softhangup = 0;
1908                 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
1909                         ast_hangup(xferchan);
1910                         ast_hangup(newchan);
1911                         return -1;
1912                 }
1913                 tobj->chan = newchan;
1914                 tobj->peer = xferchan;
1915                 tobj->bconfig = *config;
1916                 
1917                 if (tobj->bconfig.end_bridge_callback_data_fixup) {
1918                         tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
1919                 }
1920
1921                 ast_channel_lock(newchan);
1922                 ast_connected_line_copy_from_caller(&connected_line, &newchan->cid);
1923                 ast_channel_unlock(newchan);
1924                 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1925                 if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
1926                         ast_channel_update_connected_line(xferchan, &connected_line);
1927                 }
1928                 ast_channel_lock(xferchan);
1929                 ast_connected_line_copy_from_caller(&connected_line, &xferchan->cid);
1930                 ast_channel_unlock(xferchan);
1931                 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1932                 if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
1933                         ast_channel_update_connected_line(newchan, &connected_line);
1934                 }
1935
1936                 ast_party_connected_line_free(&connected_line);
1937                 
1938                 if (ast_stream_and_wait(newchan, xfersound, ""))
1939                         ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1940                 bridge_call_thread_launch(tobj);
1941                 return -1;      /* XXX meaning the channel is bridged ? */
1942         } else {
1943                 /* Transferee hung up */
1944                 finishup(transferee);
1945                 return -1;
1946         }
1947 }
1948
1949 /* add atxfer and automon as undefined so you can only use em if you configure them */
1950 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
1951
1952 AST_RWLOCK_DEFINE_STATIC(features_lock);
1953
1954 static struct ast_call_feature builtin_features[] = {
1955         { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1956         { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1957         { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1958         { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1959         { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1960         { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1961 };
1962
1963
1964 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
1965
1966 /*! \brief register new feature into feature_list*/
1967 void ast_register_feature(struct ast_call_feature *feature)
1968 {
1969         if (!feature) {
1970                 ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
1971                 return;
1972         }
1973   
1974         AST_RWLIST_WRLOCK(&feature_list);
1975         AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
1976         AST_RWLIST_UNLOCK(&feature_list);
1977
1978         ast_verb(2, "Registered Feature '%s'\n",feature->sname);
1979 }
1980
1981 /*! 
1982  * \brief Add new feature group
1983  * \param fgname feature group name.
1984  *
1985  * Add new feature group to the feature group list insert at head of list.
1986  * \note This function MUST be called while feature_groups is locked.
1987 */
1988 static struct feature_group* register_group(const char *fgname)
1989 {
1990         struct feature_group *fg;
1991
1992         if (!fgname) {
1993                 ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
1994                 return NULL;
1995         }
1996
1997         if (!(fg = ast_calloc_with_stringfields(1, struct feature_group, 128)))
1998                 return NULL;
1999
2000         ast_string_field_set(fg, gname, fgname);
2001
2002         AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
2003
2004         ast_verb(2, "Registered group '%s'\n", fg->gname);
2005
2006         return fg;
2007 }
2008
2009 /*! 
2010  * \brief Add feature to group
2011  * \param fg feature group
2012  * \param exten
2013  * \param feature feature to add.
2014  *
2015  * Check fg and feature specified, add feature to list
2016  * \note This function MUST be called while feature_groups is locked. 
2017 */
2018 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature) 
2019 {
2020         struct feature_group_exten *fge;
2021
2022         if (!fg) {
2023                 ast_log(LOG_NOTICE, "You didn't pass a group!\n");
2024                 return;
2025         }
2026
2027         if (!feature) {
2028                 ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
2029                 return;
2030         }
2031
2032         if (!(fge = ast_calloc_with_stringfields(1, struct feature_group_exten, 128)))
2033                 return;
2034
2035         ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
2036
2037         fge->feature = feature;
2038
2039         AST_LIST_INSERT_HEAD(&fg->features, fge, entry);                
2040
2041         ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
2042                                         feature->sname, fg->gname, exten);
2043 }
2044
2045 void ast_unregister_feature(struct ast_call_feature *feature)
2046 {
2047         if (!feature) {
2048                 return;
2049         }
2050
2051         AST_RWLIST_WRLOCK(&feature_list);
2052         AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
2053         AST_RWLIST_UNLOCK(&feature_list);
2054
2055         ast_free(feature);
2056 }
2057
2058 /*! \brief Remove all features in the list */
2059 static void ast_unregister_features(void)
2060 {
2061         struct ast_call_feature *feature;
2062
2063         AST_RWLIST_WRLOCK(&feature_list);
2064         while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
2065                 ast_free(feature);
2066         }
2067         AST_RWLIST_UNLOCK(&feature_list);
2068 }
2069
2070 /*! \brief find a call feature by name */
2071 static struct ast_call_feature *find_dynamic_feature(const char *name)
2072 {
2073         struct ast_call_feature *tmp;
2074
2075         AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
2076                 if (!strcasecmp(tmp->sname, name)) {
2077                         break;
2078                 }
2079         }
2080
2081         return tmp;
2082 }
2083
2084 /*! \brief Remove all feature groups in the list */
2085 static void ast_unregister_groups(void)
2086 {
2087         struct feature_group *fg;
2088         struct feature_group_exten *fge;
2089
2090         AST_RWLIST_WRLOCK(&feature_groups);
2091         while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
2092                 while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
2093                         ast_string_field_free_memory(fge);
2094                         ast_free(fge);
2095                 }
2096
2097                 ast_string_field_free_memory(fg);
2098                 ast_free(fg);
2099         }
2100         AST_RWLIST_UNLOCK(&feature_groups);
2101 }
2102
2103 /*! 
2104  * \brief Find a group by name 
2105  * \param name feature name
2106  * \retval feature group on success.
2107  * \retval NULL on failure.
2108 */
2109 static struct feature_group *find_group(const char *name) {
2110         struct feature_group *fg = NULL;
2111
2112         AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
2113                 if (!strcasecmp(fg->gname, name))
2114                         break;
2115         }
2116
2117         return fg;
2118 }
2119
2120 void ast_rdlock_call_features(void)
2121 {
2122         ast_rwlock_rdlock(&features_lock);
2123 }
2124
2125 void ast_unlock_call_features(void)
2126 {
2127         ast_rwlock_unlock(&features_lock);
2128 }
2129
2130 struct ast_call_feature *ast_find_call_feature(const char *name)
2131 {
2132         int x;
2133         for (x = 0; x < FEATURES_COUNT; x++) {
2134                 if (!strcasecmp(name, builtin_features[x].sname))
2135                         return &builtin_features[x];
2136         }
2137         return NULL;
2138 }
2139
2140 /*!
2141  * \brief exec an app by feature 
2142  * \param chan,peer,config,code,sense,data
2143  *
2144  * Find a feature, determine which channel activated
2145  * \retval AST_FEATURE_RETURN_NO_HANGUP_PEER
2146  * \retval -1 error.
2147  * \retval -2 when an application cannot be found.
2148 */
2149 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)
2150 {
2151         struct ast_app *app;
2152         struct ast_call_feature *feature = data;
2153         struct ast_channel *work, *idle;
2154         int res;
2155
2156         if (!feature) { /* shouldn't ever happen! */
2157                 ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
2158                 return -1; 
2159         }
2160
2161         if (sense == FEATURE_SENSE_CHAN) {
2162                 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
2163                         return AST_FEATURE_RETURN_KEEPTRYING;
2164                 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
2165                         work = chan;
2166                         idle = peer;
2167                 } else {
2168                         work = peer;
2169                         idle = chan;
2170                 }
2171         } else {
2172                 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
2173                         return AST_FEATURE_RETURN_KEEPTRYING;
2174                 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
2175                         work = peer;
2176                         idle = chan;
2177                 } else {
2178                         work = chan;
2179                         idle = peer;
2180                 }
2181         }
2182
2183         if (!(app = pbx_findapp(feature->app))) {
2184                 ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
2185                 return -2;
2186         }
2187
2188         ast_autoservice_start(idle);
2189         
2190         if(work && idle) {
2191                 pbx_builtin_setvar_helper(work, "DYNAMIC_PEERNAME", idle->name);
2192                 pbx_builtin_setvar_helper(idle, "DYNAMIC_PEERNAME", work->name);
2193                 pbx_builtin_setvar_helper(work, "DYNAMIC_FEATURENAME", feature->sname);
2194                 pbx_builtin_setvar_helper(idle, "DYNAMIC_FEATURENAME", feature->sname);
2195         }
2196
2197         if (!ast_strlen_zero(feature->moh_class))
2198                 ast_moh_start(idle, feature->moh_class, NULL);
2199
2200         res = pbx_exec(work, app, feature->app_args);
2201
2202         if (!ast_strlen_zero(feature->moh_class))
2203                 ast_moh_stop(idle);
2204
2205         ast_autoservice_stop(idle);
2206
2207         if (res) {
2208                 return AST_FEATURE_RETURN_SUCCESSBREAK;
2209         }
2210         return AST_FEATURE_RETURN_SUCCESS;      /*! \todo XXX should probably return res */
2211 }
2212
2213 static void unmap_features(void)
2214 {
2215         int x;
2216
2217         ast_rwlock_wrlock(&features_lock);
2218         for (x = 0; x < FEATURES_COUNT; x++)
2219                 strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
2220         ast_rwlock_unlock(&features_lock);
2221 }
2222
2223 static int remap_feature(const char *name, const char *value)
2224 {
2225         int x, res = -1;
2226
2227         ast_rwlock_wrlock(&features_lock);
2228         for (x = 0; x < FEATURES_COUNT; x++) {
2229                 if (strcasecmp(builtin_features[x].sname, name))
2230                         continue;
2231
2232                 ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
2233                 res = 0;
2234                 break;
2235         }
2236         ast_rwlock_unlock(&features_lock);
2237
2238         return res;
2239 }
2240
2241 /*!
2242  * \brief Helper function for feature_interpret and ast_feature_detect
2243  * \param chan,peer,config,code,sense,dynamic_features char buf,feature flags,operation,feature
2244  *
2245  * Lock features list, browse for code, unlock list
2246  * If a feature is found and the operation variable is set, that feature's
2247  * operation is executed.  The first feature found is copied to the feature parameter.
2248  * \retval res on success.
2249  * \retval -1 on failure.
2250 */
2251 static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel *peer,
2252         struct ast_bridge_config *config, const char *code, int sense, char *dynamic_features_buf,
2253         struct ast_flags *features, int operation, struct ast_call_feature *feature)
2254 {
2255         int x;
2256         struct feature_group *fg = NULL;
2257         struct feature_group_exten *fge;
2258         struct ast_call_feature *tmpfeature;
2259         char *tmp, *tok;
2260         int res = AST_FEATURE_RETURN_PASSDIGITS;
2261         int feature_detected = 0;
2262
2263         if (!(peer && chan && config) && operation) {
2264                 return -1; /* can not run feature operation */
2265         }
2266
2267         ast_rwlock_rdlock(&features_lock);
2268         for (x = 0; x < FEATURES_COUNT; x++) {
2269                 if ((ast_test_flag(features, builtin_features[x].feature_mask)) &&
2270                     !ast_strlen_zero(builtin_features[x].exten)) {
2271                         /* Feature is up for consideration */
2272                         if (!strcmp(builtin_features[x].exten, code)) {
2273                                 ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
2274                                 if (operation) {
2275                                         res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
2276                                 }
2277                                 memcpy(feature, &builtin_features[x], sizeof(feature));
2278                                 feature_detected = 1;
2279                                 break;
2280                         } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
2281                                 if (res == AST_FEATURE_RETURN_PASSDIGITS)
2282                                         res = AST_FEATURE_RETURN_STOREDIGITS;
2283                         }
2284                 }
2285         }
2286         ast_rwlock_unlock(&features_lock);
2287
2288         if (ast_strlen_zero(dynamic_features_buf) || feature_detected) {
2289                 return res;
2290         }
2291
2292         tmp = dynamic_features_buf;
2293
2294         while ((tok = strsep(&tmp, "#"))) {
2295                 AST_RWLIST_RDLOCK(&feature_groups);
2296
2297                 fg = find_group(tok);
2298
2299                 if (fg) {
2300                         AST_LIST_TRAVERSE(&fg->features, fge, entry) {
2301                                 if (strcasecmp(fge->exten, code))
2302                                         continue;
2303                                 if (operation) {
2304                                         res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
2305                                 }
2306                                 memcpy(feature, fge->feature, sizeof(feature));
2307                                 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
2308                                         AST_RWLIST_UNLOCK(&feature_groups);
2309                                         break;
2310                                 }
2311                                 res = AST_FEATURE_RETURN_PASSDIGITS;
2312                         }
2313                         if (fge)
2314                                 break;
2315                 }
2316
2317                 AST_RWLIST_UNLOCK(&feature_groups);
2318
2319                 AST_RWLIST_RDLOCK(&feature_list);
2320
2321                 if (!(tmpfeature = find_dynamic_feature(tok))) {
2322                         AST_RWLIST_UNLOCK(&feature_list);
2323                         continue;
2324                 }
2325
2326                 /* Feature is up for consideration */
2327                 if (!strcmp(tmpfeature->exten, code)) {
2328                         ast_verb(3, " Feature Found: %s exten: %s\n",tmpfeature->sname, tok);
2329                         if (operation) {
2330                                 res = tmpfeature->operation(chan, peer, config, code, sense, tmpfeature);
2331                         }
2332                         memcpy(feature, tmpfeature, sizeof(feature));
2333                         if (res != AST_FEATURE_RETURN_KEEPTRYING) {
2334                                 AST_RWLIST_UNLOCK(&feature_list);
2335                                 break;
2336                         }
2337                         res = AST_FEATURE_RETURN_PASSDIGITS;
2338                 } else if (!strncmp(tmpfeature->exten, code, strlen(code)))
2339                         res = AST_FEATURE_RETURN_STOREDIGITS;
2340
2341                 AST_RWLIST_UNLOCK(&feature_list);
2342         }
2343
2344         return res;
2345 }
2346
2347 /*!
2348  * \brief Check the dynamic features
2349  * \param chan,peer,config,code,sense
2350  *
2351  * \retval res on success.
2352  * \retval -1 on failure.
2353 */
2354
2355 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense) {
2356
2357         char dynamic_features_buf[128];
2358         const char *peer_dynamic_features, *chan_dynamic_features;
2359         struct ast_flags features;
2360         struct ast_call_feature feature;
2361         if (sense == FEATURE_SENSE_CHAN) {
2362                 ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
2363         }
2364         else {
2365                 ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
2366         }
2367
2368         ast_channel_lock(peer);
2369         peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
2370         ast_channel_unlock(peer);
2371
2372         ast_channel_lock(chan);
2373         chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
2374         ast_channel_unlock(chan);
2375
2376         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,""));
2377
2378         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);
2379
2380         return feature_interpret_helper(chan, peer, config, code, sense, dynamic_features_buf, &features, 1, &feature);
2381 }
2382
2383
2384 int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
2385
2386         return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, 0, feature);
2387 }
2388
2389 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
2390 {
2391         int x;
2392
2393         ast_clear_flag(config, AST_FLAGS_ALL);
2394
2395         ast_rwlock_rdlock(&features_lock);
2396         for (x = 0; x < FEATURES_COUNT; x++) {
2397                 if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
2398                         continue;
2399
2400                 if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
2401                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
2402
2403                 if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
2404                         ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
2405         }
2406         ast_rwlock_unlock(&features_lock);
2407
2408         if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
2409                 const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
2410
2411                 if (dynamic_features) {
2412                         char *tmp = ast_strdupa(dynamic_features);
2413                         char *tok;
2414                         struct ast_call_feature *feature;
2415
2416                         /* while we have a feature */
2417                         while ((tok = strsep(&tmp, "#"))) {
2418                                 AST_RWLIST_RDLOCK(&feature_list);
2419                                 if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
2420                                         if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
2421                                                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
2422                                         if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
2423                                                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
2424                                 }
2425                                 AST_RWLIST_UNLOCK(&feature_list);
2426                         }
2427                 }
2428         }
2429 }
2430
2431 /*! 
2432  * \brief Get feature and dial
2433  * \param caller,transferee,type,format,data,timeout,outstate,cid_num,cid_name,igncallerstate
2434  *
2435  * Request channel, set channel variables, initiate call,check if they want to disconnect
2436  * go into loop, check if timeout has elapsed, check if person to be transfered hung up,
2437  * check for answer break loop, set cdr return channel.
2438  *
2439  * \todo XXX Check - this is very similar to the code in channel.c 
2440  * \return always a channel
2441 */
2442 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate, const char *language)
2443 {
2444         int state = 0;
2445         int cause = 0;
2446         int to;
2447         struct ast_channel *chan;
2448         struct ast_channel *monitor_chans[2];
2449         struct ast_channel *active_channel;
2450         int res = 0, ready = 0;
2451         struct timeval started;
2452         int x, len = 0;
2453         char *disconnect_code = NULL, *dialed_code = NULL;
2454
2455         if (!(chan = ast_request(type, format, caller, data, &cause))) {
2456                 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
2457                 switch(cause) {
2458                 case AST_CAUSE_BUSY:
2459                         state = AST_CONTROL_BUSY;
2460                         break;
2461                 case AST_CAUSE_CONGESTION:
2462                         state = AST_CONTROL_CONGESTION;
2463                         break;
2464                 }
2465                 goto done;
2466         }
2467
2468         ast_set_callerid(chan, cid_num, cid_name, cid_num);
2469         ast_string_field_set(chan, language, language);
2470         ast_channel_inherit_variables(caller, chan);    
2471         pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller->name);
2472                 
2473         ast_channel_lock(chan);
2474         ast_connected_line_copy_from_caller(&chan->connected, &caller->cid);
2475         ast_channel_unlock(chan);
2476         
2477         if (ast_call(chan, data, timeout)) {
2478                 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
2479                 goto done;
2480         }
2481         
2482         ast_indicate(caller, AST_CONTROL_RINGING);
2483         /* support dialing of the featuremap disconnect code while performing an attended tranfer */
2484         ast_rwlock_rdlock(&features_lock);
2485         for (x = 0; x < FEATURES_COUNT; x++) {
2486                 if (strcasecmp(builtin_features[x].sname, "disconnect"))
2487                         continue;
2488
2489                 disconnect_code = builtin_features[x].exten;
2490                 len = strlen(disconnect_code) + 1;
2491                 dialed_code = alloca(len);
2492                 memset(dialed_code, 0, len);
2493                 break;
2494         }
2495         ast_rwlock_unlock(&features_lock);
2496         x = 0;
2497         started = ast_tvnow();
2498         to = timeout;
2499
2500         ast_poll_channel_add(caller, chan);
2501
2502         while (!((transferee && ast_check_hangup(transferee)) && (!igncallerstate && ast_check_hangup(caller))) && timeout && (chan->_state != AST_STATE_UP)) {
2503                 struct ast_frame *f = NULL;
2504
2505                 monitor_chans[0] = caller;
2506                 monitor_chans[1] = chan;
2507                 active_channel = ast_waitfor_n(monitor_chans, 2, &to);
2508
2509                 /* see if the timeout has been violated */
2510                 if(ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
2511                         state = AST_CONTROL_UNHOLD;
2512                         ast_log(LOG_NOTICE, "We exceeded our AT-timeout\n");
2513                         break; /*doh! timeout*/
2514                 }
2515
2516                 if (!active_channel)
2517                         continue;
2518
2519                 if (chan && (chan == active_channel)){
2520                         if (!ast_strlen_zero(chan->call_forward)) {
2521                                 if (!(chan = ast_call_forward(caller, chan, &to, format, NULL, outstate))) {
2522                                         return NULL;
2523                                 }
2524                                 continue;
2525                         }
2526                         f = ast_read(chan);
2527                         if (f == NULL) { /*doh! where'd he go?*/
2528                                 state = AST_CONTROL_HANGUP;
2529                                 res = 0;
2530                                 break;
2531                         }
2532                         
2533                         if (f->frametype == AST_FRAME_CONTROL || f->frametype == AST_FRAME_DTMF || f->frametype == AST_FRAME_TEXT) {
2534                                 if (f->subclass.integer == AST_CONTROL_RINGING) {
2535                                         state = f->subclass.integer;
2536                                         ast_verb(3, "%s is ringing\n", chan->name);
2537                                         ast_indicate(caller, AST_CONTROL_RINGING);
2538                                 } else if ((f->subclass.integer == AST_CONTROL_BUSY) || (f->subclass.integer == AST_CONTROL_CONGESTION)) {
2539                                         state = f->subclass.integer;
2540                                         ast_verb(3, "%s is busy\n", chan->name);
2541                                         ast_indicate(caller, AST_CONTROL_BUSY);
2542                                         ast_frfree(f);
2543                                         f = NULL;
2544                                         break;
2545                                 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
2546                                         /* This is what we are hoping for */
2547                                         state = f->subclass.integer;
2548                                         ast_frfree(f);
2549                                         f = NULL;
2550                                         ready=1;
2551                                         break;
2552                                 } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
2553                                         if (ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
2554                                                 ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
2555                                         }
2556                                 } else if (f->subclass.integer != -1 && f->subclass.integer != AST_CONTROL_PROGRESS) {
2557                                         ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
2558                                 }
2559                                 /* else who cares */
2560                         } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
2561                                 ast_write(caller, f);
2562                         }
2563
2564                 } else if (caller && (active_channel == caller)) {
2565                         f = ast_read(caller);
2566                         if (f == NULL) { /*doh! where'd he go?*/
2567                                 if (!igncallerstate) {
2568                                         if (ast_check_hangup(caller) && !ast_check_hangup(chan)) {
2569                                                 /* make this a blind transfer */
2570                                                 ready = 1;
2571                                                 break;
2572                                         }
2573                                         state = AST_CONTROL_HANGUP;
2574                                         res = 0;
2575                                         break;
2576                                 }
2577                         } else {
2578                         
2579                                 if (f->frametype == AST_FRAME_DTMF) {
2580                                         dialed_code[x++] = f->subclass.integer;
2581                                         dialed_code[x] = '\0';
2582                                         if (strlen(dialed_code) == len) {
2583                                                 x = 0;
2584                                         } else if (x && strncmp(dialed_code, disconnect_code, x)) {
2585                                                 x = 0;
2586                                                 dialed_code[x] = '\0';
2587                                         }
2588                                         if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
2589                                                 /* Caller Canceled the call */
2590                                                 state = AST_CONTROL_UNHOLD;
2591                                                 ast_frfree(f);
2592                                                 f = NULL;
2593                                                 break;
2594                                         }
2595                                 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
2596                                         ast_write(chan, f);
2597                                 }
2598                         }
2599                 }
2600                 if (f)
2601                         ast_frfree(f);
2602         } /* end while */
2603
2604         ast_poll_channel_del(caller, chan);
2605                 
2606 done:
2607         ast_indicate(caller, -1);
2608         if (chan && ready) {
2609                 if (chan->_state == AST_STATE_UP) 
2610                         state = AST_CONTROL_ANSWER;
2611                 res = 0;
2612         } else if (chan) {
2613                 res = -1;
2614                 ast_hangup(chan);
2615                 chan = NULL;
2616         } else {
2617                 res = -1;
2618         }
2619         
2620         if (outstate)
2621                 *outstate = state;
2622
2623         return chan;
2624 }
2625
2626 void ast_channel_log(char *title, struct ast_channel *chan);
2627
2628 void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
2629 {
2630        ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long)chan);
2631        ast_log(LOG_NOTICE, "CHAN: name: %s;  appl: %s; data: %s; contxt: %s;  exten: %s; pri: %d;\n",
2632                        chan->name, chan->appl, chan->data, chan->context, chan->exten, chan->priority);
2633        ast_log(LOG_NOTICE, "CHAN: acctcode: %s;  dialcontext: %s; amaflags: %x; maccontxt: %s;  macexten: %s; macpri: %d;\n",
2634                        chan->accountcode, chan->dialcontext, chan->amaflags, chan->macrocontext, chan->macroexten, chan->macropriority);
2635        ast_log(LOG_NOTICE, "CHAN: masq: %p;  masqr: %p; _bridge: %p; uniqueID: %s; linkedID:%s\n",
2636                        chan->masq, chan->masqr,
2637                        chan->_bridge, chan->uniqueid, chan->linkedid);
2638        if (chan->masqr)
2639                ast_log(LOG_NOTICE, "CHAN: masquerading as: %s;  cdr: %p;\n",
2640                                chan->masqr->name, chan->masqr->cdr);
2641        if (chan->_bridge)
2642                ast_log(LOG_NOTICE, "CHAN: Bridged to %s\n", chan->_bridge->name);
2643
2644         ast_log(LOG_NOTICE, "===== done ====\n");
2645 }
2646
2647 /*!
2648  * \brief return the first unlocked cdr in a possible chain
2649 */
2650 static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
2651 {
2652         struct ast_cdr *cdr_orig = cdr;
2653         while (cdr) {
2654                 if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
2655                         return cdr;
2656                 cdr = cdr->next;
2657         }
2658         return cdr_orig; /* everybody LOCKED or some other weirdness, like a NULL */
2659 }
2660
2661 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
2662 {
2663         const char *feature;
2664
2665         if (ast_strlen_zero(features)) {
2666                 return;
2667         }
2668
2669         for (feature = features; *feature; feature++) {
2670                 switch (*feature) {
2671                 case 'T' :
2672                 case 't' :
2673                         ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
2674                         break;
2675                 case 'K' :
2676                 case 'k' :
2677                         ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
2678                         break;
2679                 case 'H' :
2680                 case 'h' :
2681                         ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
2682                         break;
2683                 case 'W' :
2684                 case 'w' :
2685                         ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
2686                         break;
2687                 default :
2688                         ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
2689                 }
2690         }
2691 }
2692
2693 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
2694 {
2695         struct ast_datastore *ds_callee_features = NULL, *ds_caller_features = NULL;
2696         struct ast_dial_features *callee_features = NULL, *caller_features = NULL;
2697
2698         ast_channel_lock(caller);
2699         ds_caller_features = ast_channel_datastore_find(caller, &dial_features_info, NULL);
2700         ast_channel_unlock(caller);
2701         if (!ds_caller_features) {
2702                 if (!(ds_caller_features = ast_datastore_alloc(&dial_features_info, NULL))) {
2703                         ast_log(LOG_WARNING, "Unable to create channel datastore for caller features. Aborting!\n");
2704                         return;
2705                 }
2706                 if (!(caller_features = ast_calloc(1, sizeof(*caller_features)))) {
2707                         ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
2708                         ast_datastore_free(ds_caller_features);
2709                         return;
2710                 }
2711                 ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
2712                 caller_features->is_caller = 1;
2713                 ast_copy_flags(&(caller_features->features_callee), &(config->features_callee), AST_FLAGS_ALL);
2714                 ast_copy_flags(&(caller_features->features_caller), &(config->features_caller), AST_FLAGS_ALL);
2715                 ds_caller_features->data = caller_features;
2716                 ast_channel_lock(caller);
2717                 ast_channel_datastore_add(caller, ds_caller_features);
2718                 ast_channel_unlock(caller);
2719         } else {
2720                 /* If we don't return here, then when we do a builtin_atxfer we will copy the disconnect
2721                  * flags over from the atxfer to the caller */
2722                 return;
2723         }
2724
2725         ast_channel_lock(callee);
2726         ds_callee_features = ast_channel_datastore_find(callee, &dial_features_info, NULL);
2727         ast_channel_unlock(callee);
2728         if (!ds_callee_features) {
2729                 if (!(ds_callee_features = ast_datastore_alloc(&dial_features_info, NULL))) {
2730                         ast_log(LOG_WARNING, "Unable to create channel datastore for callee features. Aborting!\n");
2731                         return;
2732                 }
2733                 if (!(callee_features = ast_calloc(1, sizeof(*callee_features)))) {
2734                         ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
2735                         ast_datastore_free(ds_callee_features);
2736                         return;
2737                 }
2738                 ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
2739                 callee_features->is_caller = 0;
2740                 ast_copy_flags(&(callee_features->features_callee), &(config->features_caller), AST_FLAGS_ALL);
2741                 ast_copy_flags(&(callee_features->features_caller), &(config->features_callee), AST_FLAGS_ALL);
2742                 ds_callee_features->data = callee_features;
2743                 ast_channel_lock(callee);
2744                 ast_channel_datastore_add(callee, ds_callee_features);
2745                 ast_channel_unlock(callee);
2746         }
2747
2748         return;
2749 }
2750
2751 /*!
2752  * \brief bridge the call and set CDR
2753  * \param chan,peer,config
2754  * 
2755  * Set start time, check for two channels,check if monitor on
2756  * check for feature activation, create new CDR
2757  * \retval res on success.
2758  * \retval -1 on failure to bridge.
2759 */
2760 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
2761 {
2762         /* Copy voice back and forth between the two channels.  Give the peer
2763            the ability to transfer calls with '#<extension' syntax. */
2764         struct ast_frame *f;
2765         struct ast_channel *who;
2766         char chan_featurecode[FEATURE_MAX_LEN + 1]="";
2767         char peer_featurecode[FEATURE_MAX_LEN + 1]="";
2768         char orig_channame[AST_MAX_EXTENSION];
2769         char orig_peername[AST_MAX_EXTENSION];
2770         int res;
2771         int diff;
2772         int hasfeatures=0;
2773         int hadfeatures=0;
2774         int autoloopflag;
2775         struct ast_option_header *aoh;
2776         struct ast_cdr *bridge_cdr = NULL;
2777         struct ast_cdr *orig_peer_cdr = NULL;
2778         struct ast_cdr *chan_cdr = pick_unlocked_cdr(chan->cdr); /* the proper chan cdr, if there are forked cdrs */
2779         struct ast_cdr *peer_cdr = pick_unlocked_cdr(peer->cdr); /* the proper chan cdr, if there are forked cdrs */
2780         struct ast_cdr *new_chan_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
2781         struct ast_cdr *new_peer_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
2782
2783         if (chan && peer) {
2784                 pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
2785                 pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
2786         } else if (chan) {
2787                 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
2788         }
2789
2790         set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
2791         add_features_datastores(chan, peer, config);
2792
2793         /* This is an interesting case.  One example is if a ringing channel gets redirected to
2794          * an extension that picks up a parked call.  This will make sure that the call taken
2795          * out of parking gets told that the channel it just got bridged to is still ringing. */
2796         if (chan->_state == AST_STATE_RINGING && peer->visible_indication != AST_CONTROL_RINGING) {
2797                 ast_indicate(peer, AST_CONTROL_RINGING);
2798         }
2799
2800         if (monitor_ok) {
2801                 const char *monitor_exec;
2802                 struct ast_channel *src = NULL;
2803                 if (!monitor_app) {
2804                         if (!(monitor_app = pbx_findapp("Monitor")))
2805                                 monitor_ok=0;
2806                 }
2807                 if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR"))) 
2808                         src = chan;
2809                 else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
2810                         src = peer;
2811                 if (monitor_app && src) {
2812                         char *tmp = ast_strdupa(monitor_exec);
2813                         pbx_exec(src, monitor_app, tmp);
2814                 }
2815         }
2816
2817         set_config_flags(chan, peer, config);
2818
2819         /* Answer if need be */
2820         if (chan->_state != AST_STATE_UP) {
2821                 if (ast_raw_answer(chan, 1)) {
2822                         return -1;
2823                 }
2824         }
2825
2826 #ifdef FOR_DEBUG
2827         /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
2828         ast_channel_log("Pre-bridge CHAN Channel info", chan);
2829         ast_channel_log("Pre-bridge PEER Channel info", peer);
2830 #endif
2831         /* two channels are being marked as linked here */
2832         ast_channel_set_linkgroup(chan,peer);
2833
2834         /* copy the userfield from the B-leg to A-leg if applicable */
2835         if (chan->cdr && peer->cdr && !ast_strlen_zero(peer->cdr->userfield)) {
2836                 char tmp[256];
2837                 if (!ast_strlen_zero(chan->cdr->userfield)) {
2838                         snprintf(tmp, sizeof(tmp), "%s;%s", chan->cdr->userfield, peer->cdr->userfield);
2839                         ast_cdr_appenduserfield(chan, tmp);
2840                 } else
2841                         ast_cdr_setuserfield(chan, peer->cdr->userfield);
2842                 /* free the peer's cdr without ast_cdr_free complaining */
2843                 ast_free(peer->cdr);
2844                 peer->cdr = NULL;
2845         }
2846         ast_copy_string(orig_channame,chan->name,sizeof(orig_channame));
2847         ast_copy_string(orig_peername,peer->name,sizeof(orig_peername));
2848         orig_peer_cdr = peer_cdr;
2849         
2850         if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
2851                 
2852                 if (chan_cdr) {
2853                         ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
2854                         ast_cdr_update(chan);
2855                         bridge_cdr = ast_cdr_dup_unique_swap(chan_cdr);
2856                         ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
2857                         ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
2858                         if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
2859                                 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
2860                         }
2861
2862                 } else {
2863                         /* better yet, in a xfer situation, find out why the chan cdr got zapped (pun unintentional) */
2864                         bridge_cdr = ast_cdr_alloc(); /* this should be really, really rare/impossible? */
2865                         ast_copy_string(bridge_cdr->channel, chan->name, sizeof(bridge_cdr->channel));
2866                         ast_copy_string(bridge_cdr->dstchannel, peer->name, sizeof(bridge_cdr->dstchannel));
2867                         ast_copy_string(bridge_cdr->uniqueid, chan->uniqueid, sizeof(bridge_cdr->uniqueid));
2868                         ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
2869                         ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
2870                         ast_cdr_setcid(bridge_cdr, chan);
2871                         bridge_cdr->disposition = (chan->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NULL;
2872                         bridge_cdr->amaflags = chan->amaflags ? chan->amaflags :  ast_default_amaflags;
2873                         ast_copy_string(bridge_cdr->accountcode, chan->accountcode, sizeof(bridge_cdr->accountcode));
2874                         /* Destination information */
2875                         ast_copy_string(bridge_cdr->dst, chan->exten, sizeof(bridge_cdr->dst));
2876                         ast_copy_string(bridge_cdr->dcontext, chan->context, sizeof(bridge_cdr->dcontext));
2877                         if (peer_cdr) {
2878                                 bridge_cdr->start = peer_cdr->start;
2879                                 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
2880                         } else {
2881                                 ast_cdr_start(bridge_cdr);
2882                         }
2883                 }
2884                 ast_debug(4,"bridge answer set, chan answer set\n");
2885                 /* peer_cdr->answer will be set when a macro runs on the peer;
2886                    in that case, the bridge answer will be delayed while the
2887                    macro plays on the peer channel. The peer answered the call
2888                    before the macro started playing. To the phone system,
2889                    this is billable time for the call, even tho the caller
2890                    hears nothing but ringing while the macro does its thing. */
2891
2892                 /* Another case where the peer cdr's time will be set, is when
2893                    A self-parks by pickup up phone and dialing 700, then B
2894                    picks up A by dialing its parking slot; there may be more 
2895                    practical paths that get the same result, tho... in which
2896                    case you get the previous answer time from the Park... which
2897                    is before the bridge's start time, so I added in the 
2898                    tvcmp check to the if below */
2899
2900                 if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
2901                         bridge_cdr->answer = peer_cdr->answer;
2902                         bridge_cdr->disposition = peer_cdr->disposition;
2903                         if (chan_cdr) {
2904                                 chan_cdr->answer = peer_cdr->answer;
2905                                 chan_cdr->disposition = peer_cdr->disposition;
2906                         }
2907                 } else {
2908                         ast_cdr_answer(bridge_cdr);
2909                         if (chan_cdr) {
2910                                 ast_cdr_answer(chan_cdr); /* for the sake of cli status checks */
2911                         }
2912                 }
2913                 if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
2914                         if (chan_cdr) {
2915                                 ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
2916                         }
2917                         if (peer_cdr) {
2918                                 ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
2919                         }
2920                 }
2921         }
2922         ast_cel_report_event(chan, AST_CEL_BRIDGE_START, NULL, NULL, NULL);
2923         for (;;) {
2924                 struct ast_channel *other;      /* used later */
2925         
2926                 res = ast_channel_bridge(chan, peer, config, &f, &who);
2927                 
2928                 /* When frame is not set, we are probably involved in a situation
2929                    where we've timed out.
2930                    When frame is set, we'll come this code twice; once for DTMF_BEGIN
2931                    and also for DTMF_END. If we flow into the following 'if' for both, then 
2932                    our wait times are cut in half, as both will subtract from the
2933                    feature_timer. Not good!
2934                 */
2935                 if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
2936                         /* Update feature timer for next pass */
2937                         diff = ast_tvdiff_ms(ast_tvnow(), config->feature_start_time);
2938                         if (res == AST_BRIDGE_RETRY) {
2939                                 /* The feature fully timed out but has not been updated. Skip
2940                                  * the potential round error from the diff calculation and
2941                                  * explicitly set to expired. */
2942                                 config->feature_timer = -1;
2943                         } else {
2944                                 config->feature_timer -= diff;
2945                         }
2946
2947                         if (hasfeatures) {
2948                                 if (config->feature_timer <= 0) {
2949                                         /* Not *really* out of time, just out of time for
2950                                            digits to come in for features. */
2951                                         ast_debug(1, "Timed out for feature!\n");
2952                                         if (!ast_strlen_zero(peer_featurecode)) {
2953                                                 ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
2954                                                 memset(peer_featurecode, 0, sizeof(peer_featurecode));
2955                                         }
2956                                         if (!ast_strlen_zero(chan_featurecode)) {
2957                                                 ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
2958                                                 memset(chan_featurecode, 0, sizeof(chan_featurecode));
2959                                         }
2960                                         if (f)
2961                                                 ast_frfree(f);
2962                                         hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
2963                                         if (!hasfeatures) {
2964                                                 /* No more digits expected - reset the timer */
2965                                                 config->feature_timer = 0;
2966                                         }
2967                                         hadfeatures = hasfeatures;
2968                                         /* Continue as we were */
2969                                         continue;
2970                                 } else if (!f) {
2971                                         /* The bridge returned without a frame and there is a feature in progress.
2972                                          * However, we don't think the feature has quite yet timed out, so just
2973                                          * go back into the bridge. */
2974                                         continue;
2975                                 }
2976                         } else {
2977                                 if (config->feature_timer <=0) {
2978                                         /* We ran out of time */
2979                                         config->feature_timer = 0;
2980                                         who = chan;
2981                                         if (f)
2982                                                 ast_frfree(f);
2983                                         f = NULL;
2984                                         res = 0;
2985                                 }
2986                         }
2987                 }
2988                 if (res < 0) {
2989                         if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_