features.c 38 KB

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