df464d56654d0a80b8cba173bcddc7eb74dde3c0
[asterisk/asterisk.git] / main / features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2012, Digium, Inc.
5  * Copyright (C) 2012, Russell Bryant
6  *
7  * Mark Spencer <markster@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Routines implementing call features as call pickup, parking and transfer
23  *
24  * \author Mark Spencer <markster@digium.com>
25  */
26
27 /*! \li \ref features.c uses the configuration file \ref features.conf
28  * \addtogroup configuration_file Configuration Files
29  */
30
31 /*!
32  * \page features.conf features.conf
33  * \verbinclude features.conf.sample
34  */
35
36 /*** MODULEINFO
37         <support_level>core</support_level>
38  ***/
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include "asterisk/_private.h"
45
46 #include <pthread.h>
47 #include <signal.h>
48 #include <sys/time.h>
49 #include <sys/signal.h>
50 #include <netinet/in.h>
51
52 #include "asterisk/lock.h"
53 #include "asterisk/file.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/pbx.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/module.h"
58 #include "asterisk/translate.h"
59 #include "asterisk/app.h"
60 #include "asterisk/say.h"
61 #include "asterisk/features.h"
62 #include "asterisk/musiconhold.h"
63 #include "asterisk/config.h"
64 #include "asterisk/cli.h"
65 #include "asterisk/manager.h"
66 #include "asterisk/utils.h"
67 #include "asterisk/adsi.h"
68 #include "asterisk/devicestate.h"
69 #include "asterisk/monitor.h"
70 #include "asterisk/audiohook.h"
71 #include "asterisk/global_datastores.h"
72 #include "asterisk/astobj2.h"
73 #include "asterisk/test.h"
74 #include "asterisk/bridge.h"
75 #include "asterisk/bridge_features.h"
76 #include "asterisk/bridge_basic.h"
77 #include "asterisk/bridge_after.h"
78 #include "asterisk/stasis.h"
79 #include "asterisk/stasis_channels.h"
80 #include "asterisk/features_config.h"
81
82 /*** DOCUMENTATION
83         <application name="Bridge" language="en_US">
84                 <synopsis>
85                         Bridge two channels.
86                 </synopsis>
87                 <syntax>
88                         <parameter name="channel" required="true">
89                                 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
90                         </parameter>
91                         <parameter name="options">
92                                 <optionlist>
93                                         <option name="p">
94                                                 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
95                                         </option>
96                                         <option name="F" argsep="^">
97                                                 <argument name="context" required="false" />
98                                                 <argument name="exten" required="false" />
99                                                 <argument name="priority" required="true" />
100                                                 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
101                                                 to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
102                                                 <note>
103                                                         <para>Any channel variables you want the called channel to inherit from the caller channel must be
104                                                         prefixed with one or two underbars ('_').</para>
105                                                 </note>
106                                                 <note>
107                                                         <para>This option will override the 'x' option</para>
108                                                 </note>
109                                         </option>
110                                         <option name="F">
111                                                 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
112                                                 to the next priority of the current extension and <emphasis>start</emphasis> execution
113                                                 at that location.</para>
114                                                 <note>
115                                                         <para>Any channel variables you want the called channel to inherit from the caller channel must be
116                                                         prefixed with one or two underbars ('_').</para>
117                                                 </note>
118                                                 <note>
119                                                         <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
120                                                 </note>
121                                                 <note>
122                                                         <para>This option will override the 'x' option</para>
123                                                 </note>
124                                         </option>
125
126                                         <option name="h">
127                                                 <para>Allow the called party to hang up by sending the
128                                                 <replaceable>*</replaceable> DTMF digit.</para>
129                                         </option>
130                                         <option name="H">
131                                                 <para>Allow the calling party to hang up by pressing the
132                                                 <replaceable>*</replaceable> DTMF digit.</para>
133                                         </option>
134                                         <option name="k">
135                                                 <para>Allow the called party to enable parking of the call by sending
136                                                 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
137                                         </option>
138                                         <option name="K">
139                                                 <para>Allow the calling party to enable parking of the call by sending
140                                                  the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
141                                         </option>
142                                         <option name="L(x[:y][:z])">
143                                                 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
144                                                 when <replaceable>y</replaceable> ms are left. Repeat the warning every
145                                                 <replaceable>z</replaceable> ms. The following special variables can be
146                                                 used with this option:</para>
147                                                 <variablelist>
148                                                         <variable name="LIMIT_PLAYAUDIO_CALLER">
149                                                                 <para>Play sounds to the caller. yes|no (default yes)</para>
150                                                         </variable>
151                                                         <variable name="LIMIT_PLAYAUDIO_CALLEE">
152                                                                 <para>Play sounds to the callee. yes|no</para>
153                                                         </variable>
154                                                         <variable name="LIMIT_TIMEOUT_FILE">
155                                                                 <para>File to play when time is up.</para>
156                                                         </variable>
157                                                         <variable name="LIMIT_CONNECT_FILE">
158                                                                 <para>File to play when call begins.</para>
159                                                         </variable>
160                                                         <variable name="LIMIT_WARNING_FILE">
161                                                                 <para>File to play as warning if <replaceable>y</replaceable> is
162                                                                 defined. The default is to say the time remaining.</para>
163                                                         </variable>
164                                                 </variablelist>
165                                         </option>
166                                         <option name="S(x)">
167                                                 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
168                                         </option>
169                                         <option name="t">
170                                                 <para>Allow the called party to transfer the calling party by sending the
171                                                 DTMF sequence defined in <filename>features.conf</filename>.</para>
172                                         </option>
173                                         <option name="T">
174                                                 <para>Allow the calling party to transfer the called party by sending the
175                                                 DTMF sequence defined in <filename>features.conf</filename>.</para>
176                                         </option>
177                                         <option name="w">
178                                                 <para>Allow the called party to enable recording of the call by sending
179                                                 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
180                                         </option>
181                                         <option name="W">
182                                                 <para>Allow the calling party to enable recording of the call by sending
183                                                 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
184                                         </option>
185                                         <option name="x">
186                                                 <para>Cause the called party to be hung up after the bridge, instead of being
187                                                 restarted in the dialplan.</para>
188                                         </option>
189                                 </optionlist>
190                         </parameter>
191                 </syntax>
192                 <description>
193                         <para>Allows the ability to bridge two channels via the dialplan.</para>
194                         <para>This application sets the following channel variable upon completion:</para>
195                         <variablelist>
196                                 <variable name="BRIDGERESULT">
197                                         <para>The result of the bridge attempt as a text string.</para>
198                                         <value name="SUCCESS" />
199                                         <value name="FAILURE" />
200                                         <value name="LOOP" />
201                                         <value name="NONEXISTENT" />
202                                         <value name="INCOMPATIBLE" />
203                                 </variable>
204                         </variablelist>
205                 </description>
206         </application>
207         <manager name="Bridge" language="en_US">
208                 <synopsis>
209                         Bridge two channels already in the PBX.
210                 </synopsis>
211                 <syntax>
212                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
213                         <parameter name="Channel1" required="true">
214                                 <para>Channel to Bridge to Channel2.</para>
215                         </parameter>
216                         <parameter name="Channel2" required="true">
217                                 <para>Channel to Bridge to Channel1.</para>
218                         </parameter>
219                         <parameter name="Tone">
220                                 <para>Play courtesy tone to Channel 2.</para>
221                                 <enumlist>
222                                         <enum name="no" />
223                                         <enum name="Channel1" />
224                                         <enum name="Channel2" />
225                                         <enum name="Both" />
226                                 </enumlist>
227                         </parameter>
228                 </syntax>
229                 <description>
230                         <para>Bridge together two channels already in the PBX.</para>
231                 </description>
232         </manager>
233  ***/
234
235 typedef enum {
236         FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
237         FEATURE_INTERPRET_DO,     /* Used by feature_interpret */
238         FEATURE_INTERPRET_CHECK,  /* Used by feature_check */
239 } feature_interpret_op;
240
241 struct ast_dial_features {
242         /*! Channel's feature flags. */
243         struct ast_flags my_features;
244         /*! Bridge peer's feature flags. */
245         struct ast_flags peer_features;
246 };
247
248 static void *dial_features_duplicate(void *data)
249 {
250         struct ast_dial_features *df = data, *df_copy;
251
252         if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
253                 return NULL;
254         }
255
256         memcpy(df_copy, df, sizeof(*df));
257
258         return df_copy;
259 }
260
261 static const struct ast_datastore_info dial_features_info = {
262         .type = "dial-features",
263         .destroy = ast_free_ptr,
264         .duplicate = dial_features_duplicate,
265 };
266
267 /*!
268  * \internal
269  * \brief Set the features datastore if it doesn't exist.
270  *
271  * \param chan Channel to add features datastore
272  * \param my_features The channel's feature flags
273  * \param peer_features The channel's bridge peer feature flags
274  *
275  * \retval TRUE if features datastore already existed.
276  */
277 static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
278 {
279         struct ast_datastore *datastore;
280         struct ast_dial_features *dialfeatures;
281
282         ast_channel_lock(chan);
283         datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
284         ast_channel_unlock(chan);
285         if (datastore) {
286                 /* Already exists. */
287                 return 1;
288         }
289
290         /* Create a new datastore with specified feature flags. */
291         datastore = ast_datastore_alloc(&dial_features_info, NULL);
292         if (!datastore) {
293                 ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
294                 return 0;
295         }
296         dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
297         if (!dialfeatures) {
298                 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
299                 ast_datastore_free(datastore);
300                 return 0;
301         }
302         ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
303         ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
304         datastore->inheritance = DATASTORE_INHERIT_FOREVER;
305         datastore->data = dialfeatures;
306         ast_channel_lock(chan);
307         ast_channel_datastore_add(chan, datastore);
308         ast_channel_unlock(chan);
309         return 0;
310 }
311
312 struct ast_bridge_thread_obj
313 {
314         struct ast_bridge_config bconfig;
315         struct ast_channel *chan;
316         struct ast_channel *peer;
317         unsigned int return_to_pbx:1;
318 };
319
320 static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
321 {
322         ast_clear_flag(config, AST_FLAGS_ALL);
323
324         if (ast_test_flag(&config->features_caller, AST_FEATURE_DTMF_MASK)) {
325                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
326         }
327         if (ast_test_flag(&config->features_callee, AST_FEATURE_DTMF_MASK)) {
328                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
329         }
330
331         if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
332                 RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
333
334                 ast_channel_lock(chan);
335                 applicationmap = ast_get_chan_applicationmap(chan);
336                 ast_channel_unlock(chan);
337
338                 if (!applicationmap) {
339                         return;
340                 }
341
342                 /* If an applicationmap exists for this channel at all, then the channel needs the DTMF flag set */
343                 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
344         }
345 }
346
347 void ast_channel_log(char *title, struct ast_channel *chan);
348
349 void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
350 {
351         ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan);
352         ast_log(LOG_NOTICE, "CHAN: name: %s;  appl: %s; data: %s; contxt: %s;  exten: %s; pri: %d;\n",
353                 ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan),
354                 ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
355         ast_log(LOG_NOTICE, "CHAN: acctcode: %s;  dialcontext: %s; amaflags: %x; maccontxt: %s;  macexten: %s; macpri: %d;\n",
356                 ast_channel_accountcode(chan), ast_channel_dialcontext(chan), ast_channel_amaflags(chan),
357                 ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan));
358         ast_log(LOG_NOTICE, "CHAN: masq: %p;  masqr: %p; uniqueID: %s; linkedID:%s\n",
359                 ast_channel_masq(chan), ast_channel_masqr(chan),
360                 ast_channel_uniqueid(chan), ast_channel_linkedid(chan));
361         if (ast_channel_masqr(chan)) {
362                 ast_log(LOG_NOTICE, "CHAN: masquerading as: %s;  cdr: %p;\n",
363                         ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan)));
364         }
365
366         ast_log(LOG_NOTICE, "===== done ====\n");
367 }
368
369 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
370 {
371         const char *feature;
372
373         if (ast_strlen_zero(features)) {
374                 return;
375         }
376
377         for (feature = features; *feature; feature++) {
378                 struct ast_flags *party;
379
380                 if (isupper(*feature)) {
381                         party = &config->features_caller;
382                 } else {
383                         party = &config->features_callee;
384                 }
385
386                 switch (tolower(*feature)) {
387                 case 't' :
388                         ast_set_flag(party, AST_FEATURE_REDIRECT);
389                         break;
390                 case 'k' :
391                         ast_set_flag(party, AST_FEATURE_PARKCALL);
392                         break;
393                 case 'h' :
394                         ast_set_flag(party, AST_FEATURE_DISCONNECT);
395                         break;
396                 case 'w' :
397                         ast_set_flag(party, AST_FEATURE_AUTOMON);
398                         break;
399                 case 'x' :
400                         ast_set_flag(party, AST_FEATURE_AUTOMIXMON);
401                         break;
402                 default :
403                         ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
404                         break;
405                 }
406         }
407 }
408
409 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
410 {
411         if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
412                 /*
413                  * If we don't return here, then when we do a builtin_atxfer we
414                  * will copy the disconnect flags over from the atxfer to the
415                  * callee (Party C).
416                  */
417                 return;
418         }
419
420         add_features_datastore(callee, &config->features_callee, &config->features_caller);
421 }
422
423 static void clear_dialed_interfaces(struct ast_channel *chan)
424 {
425         struct ast_datastore *di_datastore;
426
427         ast_channel_lock(chan);
428         if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
429                 if (option_debug) {
430                         ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", ast_channel_name(chan));
431                 }
432                 if (!ast_channel_datastore_remove(chan, di_datastore)) {
433                         ast_datastore_free(di_datastore);
434                 }
435         }
436         ast_channel_unlock(chan);
437 }
438
439 static void bridge_config_set_limits_warning_values(struct ast_bridge_config *config, struct ast_bridge_features_limits *limits)
440 {
441         if (config->end_sound) {
442                 ast_string_field_set(limits, duration_sound, config->end_sound);
443         }
444
445         if (config->warning_sound) {
446                 ast_string_field_set(limits, warning_sound, config->warning_sound);
447         }
448
449         if (config->start_sound) {
450                 ast_string_field_set(limits, connect_sound, config->start_sound);
451         }
452
453         limits->frequency = config->warning_freq;
454         limits->warning = config->play_warning;
455 }
456
457 /*!
458  * \internal brief Setup limit hook structures on calls that need limits
459  *
460  * \param config ast_bridge_config which provides the limit data
461  * \param caller_limits pointer to an ast_bridge_features_limits struct which will store the caller side limits
462  * \param callee_limits pointer to an ast_bridge_features_limits struct which will store the callee side limits
463  */
464 static void bridge_config_set_limits(struct ast_bridge_config *config, struct ast_bridge_features_limits *caller_limits, struct ast_bridge_features_limits *callee_limits)
465 {
466         if (ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING)) {
467                 bridge_config_set_limits_warning_values(config, caller_limits);
468         }
469
470         if (ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING)) {
471                 bridge_config_set_limits_warning_values(config, callee_limits);
472         }
473
474         caller_limits->duration = config->timelimit;
475         callee_limits->duration = config->timelimit;
476 }
477
478 /*!
479  * \internal
480  * \brief Check if Monitor needs to be started on a channel.
481  * \since 12.0.0
482  *
483  * \param chan The bridge considers this channel the caller.
484  * \param peer The bridge considers this channel the callee.
485  *
486  * \return Nothing
487  */
488 static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *peer)
489 {
490         const char *value;
491         const char *monitor_args = NULL;
492         struct ast_channel *monitor_chan = NULL;
493
494         ast_channel_lock(chan);
495         value = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR");
496         if (!ast_strlen_zero(value)) {
497                 monitor_args = ast_strdupa(value);
498                 monitor_chan = chan;
499         }
500         ast_channel_unlock(chan);
501         if (!monitor_chan) {
502                 ast_channel_lock(peer);
503                 value = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR");
504                 if (!ast_strlen_zero(value)) {
505                         monitor_args = ast_strdupa(value);
506                         monitor_chan = peer;
507                 }
508                 ast_channel_unlock(peer);
509         }
510         if (monitor_chan) {
511                 struct ast_app *monitor_app;
512
513                 monitor_app = pbx_findapp("Monitor");
514                 if (monitor_app) {
515                         pbx_exec(monitor_chan, monitor_app, monitor_args);
516                 }
517         }
518 }
519
520 /*!
521  * \internal
522  * \brief Send the peer channel on its way on bridge start failure.
523  * \since 12.0.0
524  *
525  * \param chan Chan to put into autoservice.
526  * \param peer Chan to send to after bridge goto or run hangup handlers and hangup.
527  *
528  * \return Nothing
529  */
530 static void bridge_failed_peer_goto(struct ast_channel *chan, struct ast_channel *peer)
531 {
532         if (ast_bridge_setup_after_goto(peer)
533                 || ast_pbx_start(peer)) {
534                 ast_autoservice_chan_hangup_peer(chan, peer);
535         }
536 }
537
538 static int pre_bridge_setup(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config,
539                 struct ast_bridge_features *chan_features, struct ast_bridge_features *peer_features)
540 {
541         int res;
542
543         set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
544         add_features_datastores(chan, peer, config);
545
546         /*
547          * This is an interesting case.  One example is if a ringing
548          * channel gets redirected to an extension that picks up a
549          * parked call.  This will make sure that the call taken out of
550          * parking gets told that the channel it just got bridged to is
551          * still ringing.
552          */
553         if (ast_channel_state(chan) == AST_STATE_RINGING
554                 && ast_channel_visible_indication(peer) != AST_CONTROL_RINGING) {
555                 ast_indicate(peer, AST_CONTROL_RINGING);
556         }
557
558         bridge_check_monitor(chan, peer);
559
560         set_config_flags(chan, config);
561
562         /* Answer if need be */
563         if (ast_channel_state(chan) != AST_STATE_UP) {
564                 if (ast_raw_answer(chan)) {
565                         return -1;
566                 }
567         }
568
569 #ifdef FOR_DEBUG
570         /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
571         ast_channel_log("Pre-bridge CHAN Channel info", chan);
572         ast_channel_log("Pre-bridge PEER Channel info", peer);
573 #endif
574
575         /*
576          * If we are bridging a call, stop worrying about forwarding
577          * loops.  We presume that if a call is being bridged, that the
578          * humans in charge know what they're doing.  If they don't,
579          * well, what can we do about that?
580          */
581         clear_dialed_interfaces(chan);
582         clear_dialed_interfaces(peer);
583
584         res = 0;
585         ast_channel_lock(chan);
586         res |= ast_bridge_features_ds_append(chan, &config->features_caller);
587         ast_channel_unlock(chan);
588         ast_channel_lock(peer);
589         res |= ast_bridge_features_ds_append(peer, &config->features_callee);
590         ast_channel_unlock(peer);
591
592         if (res) {
593                 return -1;
594         }
595
596         if (config->timelimit) {
597                 struct ast_bridge_features_limits call_duration_limits_chan;
598                 struct ast_bridge_features_limits call_duration_limits_peer;
599                 int abandon_call = 0; /* TRUE if set limits fails so we can abandon the call. */
600
601                 if (ast_bridge_features_limits_construct(&call_duration_limits_chan)) {
602                         ast_log(LOG_ERROR, "Could not construct caller duration limits. Bridge canceled.\n");
603
604                         return -1;
605                 }
606
607                 if (ast_bridge_features_limits_construct(&call_duration_limits_peer)) {
608                         ast_log(LOG_ERROR, "Could not construct callee duration limits. Bridge canceled.\n");
609                         ast_bridge_features_limits_destroy(&call_duration_limits_chan);
610
611                         return -1;
612                 }
613
614                 bridge_config_set_limits(config, &call_duration_limits_chan, &call_duration_limits_peer);
615
616                 if (ast_bridge_features_set_limits(chan_features, &call_duration_limits_chan, 0)) {
617                         abandon_call = 1;
618                 }
619                 if (ast_bridge_features_set_limits(peer_features, &call_duration_limits_peer, 0)) {
620                         abandon_call = 1;
621                 }
622
623                 /* At this point we are done with the limits structs since they have been copied to the individual feature sets. */
624                 ast_bridge_features_limits_destroy(&call_duration_limits_chan);
625                 ast_bridge_features_limits_destroy(&call_duration_limits_peer);
626
627                 if (abandon_call) {
628                         ast_log(LOG_ERROR, "Could not set duration limits on one or more sides of the call. Bridge canceled.\n");
629                         return -1;
630                 }
631         }
632
633         return 0;
634 }
635
636 int ast_bridge_call_with_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, unsigned int flags)
637 {
638         int res;
639         struct ast_bridge *bridge;
640         struct ast_bridge_features chan_features;
641         struct ast_bridge_features *peer_features;
642
643         /* Setup features. */
644         res = ast_bridge_features_init(&chan_features);
645         peer_features = ast_bridge_features_new();
646         if (res || !peer_features) {
647                 ast_bridge_features_destroy(peer_features);
648                 ast_bridge_features_cleanup(&chan_features);
649                 bridge_failed_peer_goto(chan, peer);
650                 return -1;
651         }
652
653         if (pre_bridge_setup(chan, peer, config, &chan_features, peer_features)) {
654                 ast_bridge_features_destroy(peer_features);
655                 ast_bridge_features_cleanup(&chan_features);
656                 bridge_failed_peer_goto(chan, peer);
657                 return -1;
658         }
659
660         /* Create bridge */
661         bridge = ast_bridge_basic_new();
662         if (!bridge) {
663                 ast_bridge_features_destroy(peer_features);
664                 ast_bridge_features_cleanup(&chan_features);
665                 bridge_failed_peer_goto(chan, peer);
666                 return -1;
667         }
668
669         ast_bridge_basic_set_flags(bridge, flags);
670
671         /* Put peer into the bridge */
672         if (ast_bridge_impart(bridge, peer, NULL, peer_features,
673                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT | AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP)) {
674                 ast_bridge_destroy(bridge, 0);
675                 ast_bridge_features_cleanup(&chan_features);
676                 bridge_failed_peer_goto(chan, peer);
677                 return -1;
678         }
679
680         /* Join bridge */
681         ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
682                 AST_BRIDGE_JOIN_PASS_REFERENCE | AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);
683
684         /*
685          * If the bridge was broken for a hangup that isn't real, then
686          * don't run the h extension, because the channel isn't really
687          * hung up.  This should really only happen with
688          * AST_SOFTHANGUP_ASYNCGOTO.
689          */
690         res = -1;
691         ast_channel_lock(chan);
692         if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
693                 res = 0;
694         }
695         ast_channel_unlock(chan);
696
697         ast_bridge_features_cleanup(&chan_features);
698
699         if (res && config->end_bridge_callback) {
700                 config->end_bridge_callback(config->end_bridge_callback_data);
701         }
702
703         return res;
704 }
705
706 /*!
707  * \brief bridge the call and set CDR
708  *
709  * \param chan The bridge considers this channel the caller.
710  * \param peer The bridge considers this channel the callee.
711  * \param config Configuration for this bridge.
712  *
713  * Set start time, check for two channels,check if monitor on
714  * check for feature activation, create new CDR
715  * \retval res on success.
716  * \retval -1 on failure to bridge.
717  */
718 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
719 {
720         return ast_bridge_call_with_flags(chan, peer, config, 0);
721 }
722
723 enum play_tone_action {
724         PLAYTONE_NONE = 0,
725         PLAYTONE_CHANNEL1 = (1 << 0),
726         PLAYTONE_CHANNEL2 = (1 << 1),
727         PLAYTONE_BOTH = PLAYTONE_CHANNEL1 | PLAYTONE_CHANNEL2,
728 };
729
730 static enum play_tone_action parse_playtone(const char *playtone_val)
731 {
732         if (ast_strlen_zero(playtone_val) || ast_false(playtone_val)) {
733                 return PLAYTONE_NONE;
734         } if (!strcasecmp(playtone_val, "channel1")) {
735                 return PLAYTONE_CHANNEL1;
736         } else if (!strcasecmp(playtone_val, "channel2") || ast_true(playtone_val)) {
737                 return PLAYTONE_CHANNEL2;
738         } else if (!strcasecmp(playtone_val, "both")) {
739                 return PLAYTONE_BOTH;
740         } else {
741                 /* Invalid input. Assume none */
742                 return PLAYTONE_NONE;
743         }
744 }
745
746 /*!
747  * \brief Bridge channels together
748  * \param s
749  * \param m
750  *
751  * Make sure valid channels were specified,
752  * send errors if any of the channels could not be found/locked, answer channels if needed,
753  * create the placeholder channels and grab the other channels
754  * make the channels compatible, send error if we fail doing so
755  * setup the bridge thread object and start the bridge.
756  *
757  * \retval 0
758  */
759 static int action_bridge(struct mansession *s, const struct message *m)
760 {
761         const char *channela = astman_get_header(m, "Channel1");
762         const char *channelb = astman_get_header(m, "Channel2");
763         enum play_tone_action playtone = parse_playtone(astman_get_header(m, "Tone"));
764         RAII_VAR(struct ast_channel *, chana, NULL, ao2_cleanup);
765         RAII_VAR(struct ast_channel *, chanb, NULL, ao2_cleanup);
766         const char *chana_exten;
767         const char *chana_context;
768         int chana_priority;
769         const char *chanb_exten;
770         const char *chanb_context;
771         int chanb_priority;
772         struct ast_bridge *bridge;
773         char buf[256];
774         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_a, NULL, ao2_cleanup);
775         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_b, NULL, ao2_cleanup);
776
777         /* make sure valid channels were specified */
778         if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
779                 astman_send_error(s, m, "Missing channel parameter in request");
780                 return 0;
781         }
782
783         ast_debug(1, "Performing Bridge action on %s and %s\n", channela, channelb);
784
785         /* Start with chana */
786         chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
787         if (!chana) {
788                 snprintf(buf, sizeof(buf), "Channel1 does not exist: %s", channela);
789                 astman_send_error(s, m, buf);
790                 return 0;
791         }
792         xfer_cfg_a = ast_get_chan_features_xfer_config(chana);
793         ast_channel_lock(chana);
794         chana_exten = ast_strdupa(ast_channel_exten(chana));
795         chana_context = ast_strdupa(ast_channel_context(chana));
796         chana_priority = ast_channel_priority(chana);
797         if (!ast_test_flag(ast_channel_flags(chana), AST_FLAG_IN_AUTOLOOP)) {
798                 chana_priority++;
799         }
800         ast_channel_unlock(chana);
801
802         chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
803         if (!chanb) {
804                 snprintf(buf, sizeof(buf), "Channel2 does not exist: %s", channelb);
805                 astman_send_error(s, m, buf);
806                 return 0;
807         }
808         xfer_cfg_b = ast_get_chan_features_xfer_config(chanb);
809         ast_channel_lock(chanb);
810         chanb_exten = ast_strdupa(ast_channel_exten(chanb));
811         chanb_context = ast_strdupa(ast_channel_context(chanb));
812         chanb_priority = ast_channel_priority(chanb);
813         if (!ast_test_flag(ast_channel_flags(chanb), AST_FLAG_IN_AUTOLOOP)) {
814                 chanb_priority++;
815         }
816         ast_channel_unlock(chanb);
817
818         bridge = ast_bridge_basic_new();
819         if (!bridge) {
820                 astman_send_error(s, m, "Unable to create bridge\n");
821                 return 0;
822         }
823
824         ast_bridge_set_after_go_on(chana, chana_context, chana_exten, chana_priority, NULL);
825         if (ast_bridge_add_channel(bridge, chana, NULL, playtone & PLAYTONE_CHANNEL1, xfer_cfg_a ? xfer_cfg_a->xfersound : NULL)) {
826                 snprintf(buf, sizeof(buf), "Unable to add Channel1 to bridge: %s", ast_channel_name(chana));
827                 astman_send_error(s, m, buf);
828                 ast_bridge_destroy(bridge, 0);
829                 return 0;
830         }
831
832         ast_bridge_set_after_go_on(chanb, chanb_context, chanb_exten, chanb_priority, NULL);
833         if (ast_bridge_add_channel(bridge, chanb, NULL, playtone & PLAYTONE_CHANNEL2, xfer_cfg_b ? xfer_cfg_b->xfersound : NULL)) {
834                 snprintf(buf, sizeof(buf), "Unable to add Channel2 to bridge: %s", ast_channel_name(chanb));
835                 astman_send_error(s, m, buf);
836                 ast_bridge_destroy(bridge, 0);
837                 return 0;
838         }
839
840         astman_send_ack(s, m, "Channels have been bridged");
841         ao2_cleanup(bridge);
842
843         return 0;
844 }
845
846 static char *app_bridge = "Bridge";
847
848 enum {
849         BRIDGE_OPT_PLAYTONE = (1 << 0),
850         OPT_CALLEE_HANGUP =     (1 << 1),
851         OPT_CALLER_HANGUP =     (1 << 2),
852         OPT_DURATION_LIMIT = (1 << 3),
853         OPT_DURATION_STOP =     (1 << 4),
854         OPT_CALLEE_TRANSFER = (1 << 5),
855         OPT_CALLER_TRANSFER = (1 << 6),
856         OPT_CALLEE_MONITOR = (1 << 7),
857         OPT_CALLER_MONITOR = (1 << 8),
858         OPT_CALLEE_PARK = (1 << 9),
859         OPT_CALLER_PARK = (1 << 10),
860         OPT_CALLEE_KILL = (1 << 11),
861         OPT_CALLEE_GO_ON = (1 << 12),
862 };
863
864 enum {
865         OPT_ARG_DURATION_LIMIT = 0,
866         OPT_ARG_DURATION_STOP,
867         OPT_ARG_CALLEE_GO_ON,
868         /* note: this entry _MUST_ be the last one in the enum */
869         OPT_ARG_ARRAY_SIZE,
870 };
871
872 AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
873         AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
874         AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
875         AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
876         AST_APP_OPTION('H', OPT_CALLER_HANGUP),
877         AST_APP_OPTION('k', OPT_CALLEE_PARK),
878         AST_APP_OPTION('K', OPT_CALLER_PARK),
879         AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
880         AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
881         AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
882         AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
883         AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
884         AST_APP_OPTION('W', OPT_CALLER_MONITOR),
885         AST_APP_OPTION('x', OPT_CALLEE_KILL),
886 END_OPTIONS );
887
888 int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
889         char *parse, struct timeval *calldurationlimit)
890 {
891         char *stringp = ast_strdupa(parse);
892         char *limit_str, *warning_str, *warnfreq_str;
893         const char *var;
894         int play_to_caller = 0, play_to_callee = 0;
895         int delta;
896
897         limit_str = strsep(&stringp, ":");
898         warning_str = strsep(&stringp, ":");
899         warnfreq_str = strsep(&stringp, ":");
900
901         config->timelimit = atol(limit_str);
902         if (warning_str)
903                 config->play_warning = atol(warning_str);
904         if (warnfreq_str)
905                 config->warning_freq = atol(warnfreq_str);
906
907         if (!config->timelimit) {
908                 ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
909                 config->timelimit = config->play_warning = config->warning_freq = 0;
910                 config->warning_sound = NULL;
911                 return -1; /* error */
912         } else if ( (delta = config->play_warning - config->timelimit) > 0) {
913                 int w = config->warning_freq;
914
915                 /*
916                  * If the first warning is requested _after_ the entire call
917                  * would end, and no warning frequency is requested, then turn
918                  * off the warning. If a warning frequency is requested, reduce
919                  * the 'first warning' time by that frequency until it falls
920                  * within the call's total time limit.
921                  *
922                  * Graphically:
923                  *                timelim->|    delta        |<-playwarning
924                  *      0__________________|_________________|
925                  *                       | w  |    |    |    |
926                  *
927                  * so the number of intervals to cut is 1+(delta-1)/w
928                  */
929                 if (w == 0) {
930                         config->play_warning = 0;
931                 } else {
932                         config->play_warning -= w * ( 1 + (delta-1)/w );
933                         if (config->play_warning < 1)
934                                 config->play_warning = config->warning_freq = 0;
935                 }
936         }
937
938         ast_channel_lock(chan);
939
940         var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
941         play_to_caller = var ? ast_true(var) : 1;
942
943         var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
944         play_to_callee = var ? ast_true(var) : 0;
945
946         if (!play_to_caller && !play_to_callee)
947                 play_to_caller = 1;
948
949         var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
950         config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
951
952         /* The code looking at config wants a NULL, not just "", to decide
953          * that the message should not be played, so we replace "" with NULL.
954          * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
955          * not found.
956          */
957
958         var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
959         config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
960
961         var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
962         config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
963
964         ast_channel_unlock(chan);
965
966         /* undo effect of S(x) in case they are both used */
967         calldurationlimit->tv_sec = 0;
968         calldurationlimit->tv_usec = 0;
969
970         /* more efficient to do it like S(x) does since no advanced opts */
971         if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
972                 calldurationlimit->tv_sec = config->timelimit / 1000;
973                 calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
974                 ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
975                         calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
976                 play_to_caller = 0;
977                 play_to_callee = 0;
978                 config->timelimit = 0;
979                 config->play_warning = 0;
980                 config->warning_freq = 0;
981         } else {
982                 ast_verb(4, "Limit Data for this call:\n");
983                 ast_verb(4, "timelimit      = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
984                 ast_verb(4, "play_warning   = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
985                 ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
986                 ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
987                 ast_verb(4, "warning_freq   = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
988                 ast_verb(4, "start_sound    = %s\n", S_OR(config->start_sound, ""));
989                 ast_verb(4, "warning_sound  = %s\n", config->warning_sound);
990                 ast_verb(4, "end_sound      = %s\n", S_OR(config->end_sound, ""));
991         }
992         if (play_to_caller)
993                 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
994         if (play_to_callee)
995                 ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
996         return 0;
997 }
998
999
1000 /*!
1001  * \brief Bridge channels
1002  * \param chan
1003  * \param data channel to bridge with.
1004  *
1005  * Split data, check we aren't bridging with ourself, check valid channel,
1006  * answer call if not already, check compatible channels, setup bridge config
1007  * now bridge call, if transferred party hangs up return to PBX extension.
1008  */
1009 static int bridge_exec(struct ast_channel *chan, const char *data)
1010 {
1011         struct ast_channel *current_dest_chan;
1012         char *tmp_data  = NULL;
1013         struct ast_flags opts = { 0, };
1014         struct ast_bridge_config bconfig = { { 0, }, };
1015         char *opt_args[OPT_ARG_ARRAY_SIZE];
1016         struct timeval calldurationlimit = { 0, };
1017         const char *context;
1018         const char *extension;
1019         int priority;
1020         int bridge_add_failed;
1021         struct ast_bridge_features chan_features;
1022         struct ast_bridge_features *peer_features;
1023         struct ast_bridge *bridge;
1024         struct ast_features_xfer_config *xfer_cfg;
1025
1026         AST_DECLARE_APP_ARGS(args,
1027                 AST_APP_ARG(dest_chan);
1028                 AST_APP_ARG(options);
1029         );
1030
1031         if (ast_strlen_zero(data)) {
1032                 ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
1033                 return -1;
1034         }
1035
1036         tmp_data = ast_strdupa(data);
1037         AST_STANDARD_APP_ARGS(args, tmp_data);
1038         if (!ast_strlen_zero(args.options))
1039                 ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
1040
1041         /* make sure we have a valid end point */
1042         current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
1043                 strlen(args.dest_chan));
1044         if (!current_dest_chan) {
1045                 ast_log(LOG_WARNING, "Bridge failed because channel %s does not exist\n",
1046                         args.dest_chan);
1047                 return 0;
1048         }
1049
1050         /* avoid bridge with ourselves */
1051         if (chan == current_dest_chan) {
1052                 ast_channel_unref(current_dest_chan);
1053                 ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", ast_channel_name(chan));
1054                 return 0;
1055         }
1056
1057         if (ast_test_flag(&opts, OPT_DURATION_LIMIT)
1058                 && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])
1059                 && ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit)) {
1060                 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
1061                 goto done;
1062         }
1063
1064         if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
1065                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
1066         if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
1067                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
1068         if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
1069                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
1070         if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
1071                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
1072         if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
1073                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
1074         if (ast_test_flag(&opts, OPT_CALLER_MONITOR))
1075                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
1076         if (ast_test_flag(&opts, OPT_CALLEE_PARK))
1077                 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
1078         if (ast_test_flag(&opts, OPT_CALLER_PARK))
1079                 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
1080
1081         /* Setup after bridge goto location. */
1082         if (ast_test_flag(&opts, OPT_CALLEE_GO_ON)) {
1083                 ast_channel_lock(chan);
1084                 context = ast_strdupa(ast_channel_context(chan));
1085                 extension = ast_strdupa(ast_channel_exten(chan));
1086                 priority = ast_channel_priority(chan);
1087                 ast_channel_unlock(chan);
1088                 ast_bridge_set_after_go_on(current_dest_chan, context, extension, priority,
1089                         opt_args[OPT_ARG_CALLEE_GO_ON]);
1090         } else if (!ast_test_flag(&opts, OPT_CALLEE_KILL)) {
1091                 ast_channel_lock(current_dest_chan);
1092                 context = ast_strdupa(ast_channel_context(current_dest_chan));
1093                 extension = ast_strdupa(ast_channel_exten(current_dest_chan));
1094                 priority = ast_channel_priority(current_dest_chan);
1095                 ast_channel_unlock(current_dest_chan);
1096                 ast_bridge_set_after_go_on(current_dest_chan, context, extension, priority, NULL);
1097         }
1098
1099         if (ast_bridge_features_init(&chan_features)) {
1100                 ast_bridge_features_cleanup(&chan_features);
1101                 goto done;
1102         }
1103
1104         peer_features = ast_bridge_features_new();
1105         if (!peer_features) {
1106                 ast_bridge_features_cleanup(&chan_features);
1107                 goto done;
1108         }
1109
1110         if (pre_bridge_setup(chan, current_dest_chan, &bconfig, &chan_features, peer_features)) {
1111                 ast_bridge_features_destroy(peer_features);
1112                 ast_bridge_features_cleanup(&chan_features);
1113                 goto done;
1114         }
1115
1116         bridge = ast_bridge_basic_new();
1117         if (!bridge) {
1118                 ast_bridge_features_destroy(peer_features);
1119                 ast_bridge_features_cleanup(&chan_features);
1120                 goto done;
1121         }
1122
1123         xfer_cfg = ast_get_chan_features_xfer_config(current_dest_chan);
1124         bridge_add_failed = ast_bridge_add_channel(bridge, current_dest_chan, peer_features,
1125                 ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE),
1126                 xfer_cfg ? xfer_cfg->xfersound : NULL);
1127         ao2_cleanup(xfer_cfg);
1128         if (bridge_add_failed) {
1129                 ast_bridge_features_destroy(peer_features);
1130                 ast_bridge_features_cleanup(&chan_features);
1131                 ast_bridge_destroy(bridge, 0);
1132                 goto done;
1133         }
1134
1135         /* Don't keep the channel ref in case it was not already in a bridge. */
1136         current_dest_chan = ast_channel_unref(current_dest_chan);
1137
1138         ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
1139                 AST_BRIDGE_JOIN_PASS_REFERENCE);
1140
1141         ast_bridge_features_cleanup(&chan_features);
1142
1143         /* The bridge has ended, set BRIDGERESULT to SUCCESS. */
1144         pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
1145 done:
1146         ast_free((char *) bconfig.warning_sound);
1147         ast_free((char *) bconfig.end_sound);
1148         ast_free((char *) bconfig.start_sound);
1149
1150         ast_channel_cleanup(current_dest_chan);
1151         return 0;
1152 }
1153
1154 /*!
1155  * \internal
1156  * \brief Clean up resources on Asterisk shutdown
1157  */
1158 static void features_shutdown(void)
1159 {
1160         ast_features_config_shutdown();
1161
1162         ast_manager_unregister("Bridge");
1163
1164         ast_unregister_application(app_bridge);
1165
1166 }
1167
1168 int ast_features_init(void)
1169 {
1170         int res;
1171
1172         res = ast_features_config_init();
1173         if (res) {
1174                 return res;
1175         }
1176         res |= ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
1177         res |= ast_manager_register_xml_core("Bridge", EVENT_FLAG_CALL, action_bridge);
1178
1179         if (res) {
1180                 features_shutdown();
1181         } else {
1182                 ast_register_cleanup(features_shutdown);
1183         }
1184
1185         return res;
1186 }