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