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