parking_applications.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Call Parking Applications
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "res_parking.h"
  27. #include "asterisk/config.h"
  28. #include "asterisk/config_options.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/astobj2.h"
  31. #include "asterisk/features.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/say.h"
  35. #include "asterisk/bridge_basic.h"
  36. #include "asterisk/format_cache.h"
  37. /*** DOCUMENTATION
  38. <application name="Park" language="en_US">
  39. <synopsis>
  40. Park yourself.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="parking_lot_name">
  44. <para>Specify in which parking lot to park a call.</para>
  45. <para>The parking lot used is selected in the following order:</para>
  46. <para>1) parking_lot_name option to this application</para>
  47. <para>2) <variable>PARKINGLOT</variable> variable</para>
  48. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  49. (Possibly preset by the channel driver.)</para>
  50. <para>4) Default parking lot.</para>
  51. </parameter>
  52. <parameter name="options">
  53. <para>A list of options for this parked call.</para>
  54. <optionlist>
  55. <option name="r">
  56. <para>Send ringing instead of MOH to the parked call.</para>
  57. </option>
  58. <option name="R">
  59. <para>Randomize the selection of a parking space.</para>
  60. </option>
  61. <option name="s">
  62. <para>Silence announcement of the parking space number.</para>
  63. </option>
  64. <option name="c" argsep=",">
  65. <argument name="context" required="false" />
  66. <argument name="extension" required="false" />
  67. <argument name="priority" required="true" />
  68. <para>If the parking times out, go to this place in the dialplan
  69. instead of where the parking lot defines the call should go.
  70. </para>
  71. </option>
  72. <option name="t">
  73. <argument name="duration" required="true" />
  74. <para>Use a timeout of <literal>duration</literal> seconds instead
  75. of the timeout specified by the parking lot.</para>
  76. </option>
  77. </optionlist>
  78. </parameter>
  79. </syntax>
  80. <description>
  81. <para>Used to park yourself (typically in combination with an attended
  82. transfer to know the parking space).</para>
  83. <para>If you set the <variable>PARKINGEXTEN</variable> variable to a
  84. parking space extension in the parking lot, Park() will attempt to park the
  85. call on that extension. If the extension is already in use then execution
  86. will continue at the next priority.
  87. </para>
  88. <para>If the <literal>parkeddynamic</literal> option is enabled in
  89. <filename>res_parking.conf</filename> the following variables can be
  90. used to dynamically create new parking lots. When using dynamic parking
  91. lots, be aware of the conditions as explained in the notes section
  92. below.
  93. </para>
  94. <para>The <variable>PARKINGDYNAMIC</variable> variable specifies the
  95. parking lot to use as a template to create a dynamic parking lot. It
  96. is an error to specify a non-existent parking lot for the template.
  97. If not set then the default parking lot is used as the template.
  98. </para>
  99. <para>The <variable>PARKINGDYNCONTEXT</variable> variable specifies the
  100. dialplan context to use for the newly created dynamic parking lot. If
  101. not set then the context from the parking lot template is used. The
  102. context is created if it does not already exist and the new parking lot
  103. needs to create extensions.
  104. </para>
  105. <para>The <variable>PARKINGDYNEXTEN</variable> variable specifies the
  106. <literal>parkext</literal> to use for the newly created dynamic
  107. parking lot. If not set then the <literal>parkext</literal> is used from
  108. the parking lot template. If the template does not specify a
  109. <literal>parkext</literal> then no extensions are created for the newly
  110. created parking lot. The dynamic parking lot cannot be created if it
  111. needs to create extensions that overlap existing parking lot extensions.
  112. The only exception to this is for the <literal>parkext</literal>
  113. extension and only if neither of the overlaping parking lot's
  114. <literal>parkext</literal> is exclusive.
  115. </para>
  116. <para>The <variable>PARKINGDYNPOS</variable> variable specifies the
  117. parking positions to use for the newly created dynamic parking lot. If
  118. not set then the <literal>parkpos</literal> from the parking lot template
  119. is used.
  120. </para>
  121. <note>
  122. <para>This application must be used as the first extension priority
  123. to be recognized as a parking access extension for blind transfers.
  124. Blind transfers and the DTMF one-touch parking feature need this
  125. distinction to operate properly. The parking access extension in
  126. this case is treated like a dialplan hint.
  127. </para>
  128. </note>
  129. </description>
  130. <see-also>
  131. <ref type="application">ParkedCall</ref>
  132. </see-also>
  133. </application>
  134. <application name="ParkedCall" language="en_US">
  135. <synopsis>
  136. Retrieve a parked call.
  137. </synopsis>
  138. <syntax>
  139. <parameter name="parking_lot_name">
  140. <para>Specify from which parking lot to retrieve a parked call.</para>
  141. <para>The parking lot used is selected in the following order:</para>
  142. <para>1) parking_lot_name option</para>
  143. <para>2) <variable>PARKINGLOT</variable> variable</para>
  144. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  145. (Possibly preset by the channel driver.)</para>
  146. <para>4) Default parking lot.</para>
  147. </parameter>
  148. <parameter name="parking_space">
  149. <para>Parking space to retrieve a parked call from.
  150. If not provided then the first available parked call in the
  151. parking lot will be retrieved.</para>
  152. </parameter>
  153. </syntax>
  154. <description>
  155. <para>Used to retrieve a parked call from a parking lot.</para>
  156. <note>
  157. <para>If a parking lot's parkext option is set, then Parking lots
  158. will automatically create and manage dialplan extensions in
  159. the parking lot context. If that is the case then you will not
  160. need to manage parking extensions yourself, just include the
  161. parking context of the parking lot.</para>
  162. </note>
  163. </description>
  164. <see-also>
  165. <ref type="application">Park</ref>
  166. </see-also>
  167. </application>
  168. <application name="ParkAndAnnounce" language="en_US">
  169. <synopsis>
  170. Park and Announce.
  171. </synopsis>
  172. <syntax>
  173. <parameter name="parking_lot_name">
  174. <para>Specify in which parking lot to park a call.</para>
  175. <para>The parking lot used is selected in the following order:</para>
  176. <para>1) parking_lot_name option to this application</para>
  177. <para>2) <variable>PARKINGLOT</variable> variable</para>
  178. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  179. (Possibly preset by the channel driver.)</para>
  180. <para>4) Default parking lot.</para>
  181. </parameter>
  182. <parameter name="options">
  183. <para>A list of options for this parked call.</para>
  184. <optionlist>
  185. <option name="r">
  186. <para>Send ringing instead of MOH to the parked call.</para>
  187. </option>
  188. <option name="R">
  189. <para>Randomize the selection of a parking space.</para>
  190. </option>
  191. <option name="c" argsep=",">
  192. <argument name="context" required="false" />
  193. <argument name="extension" required="false" />
  194. <argument name="priority" required="true" />
  195. <para>If the parking times out, go to this place in the dialplan
  196. instead of where the parking lot defines the call should go.
  197. </para>
  198. </option>
  199. <option name="t">
  200. <argument name="duration" required="true" />
  201. <para>Use a timeout of <literal>duration</literal> seconds instead
  202. of the timeout specified by the parking lot.</para>
  203. </option>
  204. </optionlist>
  205. </parameter>
  206. <parameter name="announce_template" required="true" argsep=":">
  207. <argument name="announce" required="true">
  208. <para>Colon-separated list of files to announce. The word
  209. <literal>PARKED</literal> will be replaced by a say_digits of the extension in which
  210. the call is parked.</para>
  211. </argument>
  212. <argument name="announce1" multiple="true" />
  213. </parameter>
  214. <parameter name="dial" required="true">
  215. <para>The app_dial style resource to call to make the
  216. announcement. Console/dsp calls the console.</para>
  217. </parameter>
  218. </syntax>
  219. <description>
  220. <para>Park a call into the parkinglot and announce the call to another channel.</para>
  221. <para>The variable <variable>PARKEDAT</variable> will contain the parking extension
  222. into which the call was placed. Use with the Local channel to allow the dialplan to make
  223. use of this information.</para>
  224. </description>
  225. <see-also>
  226. <ref type="application">Park</ref>
  227. <ref type="application">ParkedCall</ref>
  228. </see-also>
  229. </application>
  230. ***/
  231. #define PARK_AND_ANNOUNCE_APPLICATION "ParkAndAnnounce"
  232. /* Park a call */
  233. enum park_args {
  234. OPT_ARG_COMEBACK,
  235. OPT_ARG_TIMEOUT,
  236. OPT_ARG_ARRAY_SIZE /* Always the last element of the enum */
  237. };
  238. enum park_flags {
  239. MUXFLAG_RINGING = (1 << 0),
  240. MUXFLAG_RANDOMIZE = (1 << 1),
  241. MUXFLAG_NOANNOUNCE = (1 << 2),
  242. MUXFLAG_COMEBACK_OVERRIDE = (1 << 3),
  243. MUXFLAG_TIMEOUT_OVERRIDE = (1 << 4),
  244. };
  245. AST_APP_OPTIONS(park_opts, {
  246. AST_APP_OPTION('r', MUXFLAG_RINGING),
  247. AST_APP_OPTION('R', MUXFLAG_RANDOMIZE),
  248. AST_APP_OPTION('s', MUXFLAG_NOANNOUNCE),
  249. AST_APP_OPTION_ARG('c', MUXFLAG_COMEBACK_OVERRIDE, OPT_ARG_COMEBACK),
  250. AST_APP_OPTION_ARG('t', MUXFLAG_TIMEOUT_OVERRIDE, OPT_ARG_TIMEOUT),
  251. });
  252. static int apply_option_timeout (int *var, char *timeout_arg)
  253. {
  254. if (ast_strlen_zero(timeout_arg)) {
  255. ast_log(LOG_ERROR, "No duration value provided for the timeout ('t') option.\n");
  256. return -1;
  257. }
  258. if (sscanf(timeout_arg, "%d", var) != 1 || *var < 0) {
  259. ast_log(LOG_ERROR, "Duration value provided for timeout ('t') option must be 0 or greater.\n");
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. static int park_app_parse_data(const char *data, int *disable_announce, int *use_ringing, int *randomize, int *time_limit, char **comeback_override, char **lot_name)
  265. {
  266. char *parse;
  267. struct ast_flags flags = { 0 };
  268. AST_DECLARE_APP_ARGS(args,
  269. AST_APP_ARG(lot_name);
  270. AST_APP_ARG(options);
  271. AST_APP_ARG(other); /* Any remaining unused arguments */
  272. );
  273. parse = ast_strdupa(data);
  274. AST_STANDARD_APP_ARGS(args, parse);
  275. if (args.options) {
  276. char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
  277. ast_app_parse_options(park_opts, &flags, opts, args.options);
  278. if (ast_test_flag(&flags, MUXFLAG_TIMEOUT_OVERRIDE)) {
  279. if (apply_option_timeout(time_limit, opts[OPT_ARG_TIMEOUT])) {
  280. return -1;
  281. }
  282. }
  283. if (ast_test_flag(&flags, MUXFLAG_COMEBACK_OVERRIDE)) {
  284. *comeback_override = ast_strdup(opts[OPT_ARG_COMEBACK]);
  285. }
  286. if (ast_test_flag(&flags, MUXFLAG_NOANNOUNCE)) {
  287. if (disable_announce) {
  288. *disable_announce = 1;
  289. }
  290. }
  291. if (ast_test_flag(&flags, MUXFLAG_RINGING)) {
  292. *use_ringing = 1;
  293. }
  294. if (ast_test_flag(&flags, MUXFLAG_RANDOMIZE)) {
  295. *randomize = 1;
  296. }
  297. }
  298. if (!ast_strlen_zero(args.lot_name)) {
  299. *lot_name = ast_strdup(args.lot_name);
  300. }
  301. return 0;
  302. }
  303. void park_common_datastore_free(struct park_common_datastore *datastore)
  304. {
  305. if (!datastore) {
  306. return;
  307. }
  308. ast_free(datastore->parker_uuid);
  309. ast_free(datastore->parker_dial_string);
  310. ast_free(datastore->comeback_override);
  311. ast_free(datastore);
  312. }
  313. static void park_common_datastore_destroy(void *data)
  314. {
  315. struct park_common_datastore *datastore = data;
  316. park_common_datastore_free(datastore);
  317. }
  318. static const struct ast_datastore_info park_common_info = {
  319. .type = "park entry data",
  320. .destroy = park_common_datastore_destroy,
  321. };
  322. static void wipe_park_common_datastore(struct ast_channel *chan)
  323. {
  324. struct ast_datastore *datastore;
  325. ast_channel_lock(chan);
  326. datastore = ast_channel_datastore_find(chan, &park_common_info, NULL);
  327. if (datastore) {
  328. ast_channel_datastore_remove(chan, datastore);
  329. ast_datastore_free(datastore);
  330. }
  331. ast_channel_unlock(chan);
  332. }
  333. static int setup_park_common_datastore(struct ast_channel *parkee, const char *parker_uuid, const char *comeback_override, int randomize, int time_limit, int silence_announce)
  334. {
  335. struct ast_datastore *datastore = NULL;
  336. struct park_common_datastore *park_datastore;
  337. const char *attended_transfer;
  338. const char *blind_transfer;
  339. char *parker_dial_string = NULL;
  340. wipe_park_common_datastore(parkee);
  341. if (!(datastore = ast_datastore_alloc(&park_common_info, NULL))) {
  342. return -1;
  343. }
  344. if (!(park_datastore = ast_calloc(1, sizeof(*park_datastore)))) {
  345. ast_datastore_free(datastore);
  346. return -1;
  347. }
  348. datastore->data = park_datastore;
  349. park_datastore->parker_uuid = ast_strdup(parker_uuid);
  350. if (!park_datastore->parker_uuid) {
  351. ast_datastore_free(datastore);
  352. return -1;
  353. }
  354. ast_channel_lock(parkee);
  355. attended_transfer = pbx_builtin_getvar_helper(parkee, "ATTENDEDTRANSFER");
  356. blind_transfer = pbx_builtin_getvar_helper(parkee, "BLINDTRANSFER");
  357. if (!ast_strlen_zero(attended_transfer)) {
  358. parker_dial_string = ast_strdupa(attended_transfer);
  359. } else if (!ast_strlen_zero(blind_transfer)) {
  360. parker_dial_string = ast_strdupa(blind_transfer);
  361. /* Ensure that attended_transfer is NULL and not an empty string. */
  362. attended_transfer = NULL;
  363. }
  364. ast_channel_unlock(parkee);
  365. if (!ast_strlen_zero(parker_dial_string)) {
  366. ast_channel_name_to_dial_string(parker_dial_string);
  367. ast_verb(4, "Setting Parker dial string to %s from %s value\n",
  368. parker_dial_string,
  369. attended_transfer ? "ATTENDEDTRANSFER" : "BLINDTRANSFER");
  370. park_datastore->parker_dial_string = ast_strdup(parker_dial_string);
  371. if (!park_datastore->parker_dial_string) {
  372. ast_datastore_free(datastore);
  373. return -1;
  374. }
  375. }
  376. park_datastore->randomize = randomize;
  377. park_datastore->time_limit = time_limit;
  378. park_datastore->silence_announce = silence_announce;
  379. if (comeback_override) {
  380. park_datastore->comeback_override = ast_strdup(comeback_override);
  381. if (!park_datastore->comeback_override) {
  382. ast_datastore_free(datastore);
  383. return -1;
  384. }
  385. }
  386. ast_channel_lock(parkee);
  387. ast_channel_datastore_add(parkee, datastore);
  388. ast_channel_unlock(parkee);
  389. return 0;
  390. }
  391. struct park_common_datastore *get_park_common_datastore_copy(struct ast_channel *parkee)
  392. {
  393. struct ast_datastore *datastore;
  394. struct park_common_datastore *data;
  395. struct park_common_datastore *data_copy;
  396. SCOPED_CHANNELLOCK(lock, parkee);
  397. if (!(datastore = ast_channel_datastore_find(parkee, &park_common_info, NULL))) {
  398. return NULL;
  399. }
  400. data = datastore->data;
  401. /* This data should always be populated if this datastore was appended to the channel */
  402. ast_assert(data != NULL);
  403. data_copy = ast_calloc(1, sizeof(*data_copy));
  404. if (!data_copy) {
  405. return NULL;
  406. }
  407. data_copy->parker_uuid = ast_strdup(data->parker_uuid);
  408. if (!data_copy->parker_uuid) {
  409. park_common_datastore_free(data_copy);
  410. return NULL;
  411. }
  412. data_copy->randomize = data->randomize;
  413. data_copy->time_limit = data->time_limit;
  414. data_copy->silence_announce = data->silence_announce;
  415. if (data->comeback_override) {
  416. data_copy->comeback_override = ast_strdup(data->comeback_override);
  417. if (!data_copy->comeback_override) {
  418. park_common_datastore_free(data_copy);
  419. return NULL;
  420. }
  421. }
  422. if (data->parker_dial_string) {
  423. data_copy->parker_dial_string = ast_strdup(data->parker_dial_string);
  424. if (!data_copy->parker_dial_string) {
  425. park_common_datastore_free(data_copy);
  426. return NULL;
  427. }
  428. }
  429. return data_copy;
  430. }
  431. struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
  432. const char *lot_name, const char *comeback_override,
  433. int use_ringing, int randomize, int time_limit, int silence_announcements)
  434. {
  435. struct ast_bridge *parking_bridge;
  436. RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
  437. if (!parker) {
  438. parker = parkee;
  439. }
  440. /* If the name of the parking lot isn't specified in the arguments, find it based on the channel. */
  441. if (ast_strlen_zero(lot_name)) {
  442. ast_channel_lock(parker);
  443. lot_name = ast_strdupa(find_channel_parking_lot_name(parker));
  444. ast_channel_unlock(parker);
  445. }
  446. lot = parking_lot_find_by_name(lot_name);
  447. if (!lot) {
  448. lot = parking_create_dynamic_lot(lot_name, parker);
  449. }
  450. if (!lot) {
  451. ast_log(LOG_ERROR, "Could not find parking lot: '%s'\n", lot_name);
  452. return NULL;
  453. }
  454. ao2_lock(lot);
  455. parking_bridge = parking_lot_get_bridge(lot);
  456. ao2_unlock(lot);
  457. if (!parking_bridge) {
  458. return NULL;
  459. }
  460. /* Apply relevant bridge roles and such to the parking channel */
  461. parking_channel_set_roles(parkee, lot, use_ringing);
  462. setup_park_common_datastore(parkee, ast_channel_uniqueid(parker), comeback_override, randomize, time_limit,
  463. silence_announcements);
  464. return parking_bridge;
  465. }
  466. struct ast_bridge *park_application_setup(struct ast_channel *parkee, struct ast_channel *parker, const char *app_data,
  467. int *silence_announcements)
  468. {
  469. int use_ringing = 0;
  470. int randomize = 0;
  471. int time_limit = -1;
  472. RAII_VAR(char *, comeback_override, NULL, ast_free);
  473. RAII_VAR(char *, lot_name_app_arg, NULL, ast_free);
  474. if (app_data) {
  475. park_app_parse_data(app_data, silence_announcements, &use_ringing, &randomize, &time_limit, &comeback_override, &lot_name_app_arg);
  476. }
  477. return park_common_setup(parkee, parker, lot_name_app_arg, comeback_override, use_ringing,
  478. randomize, time_limit, silence_announcements ? *silence_announcements : 0);
  479. }
  480. static int park_app_exec(struct ast_channel *chan, const char *data)
  481. {
  482. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  483. struct ast_bridge_features chan_features;
  484. int res;
  485. int silence_announcements = 0;
  486. int blind_transfer;
  487. /* Answer the channel if needed */
  488. if (ast_channel_state(chan) != AST_STATE_UP) {
  489. ast_answer(chan);
  490. }
  491. ast_channel_lock(chan);
  492. blind_transfer = !ast_strlen_zero(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"));
  493. ast_channel_unlock(chan);
  494. /* Handle the common parking setup stuff */
  495. if (!(parking_bridge = park_application_setup(chan, NULL, data, &silence_announcements))) {
  496. if (!silence_announcements && !blind_transfer) {
  497. ast_stream_and_wait(chan, "pbx-parkingfailed", "");
  498. }
  499. publish_parked_call_failure(chan);
  500. return 0;
  501. }
  502. /* Initialize bridge features for the channel. */
  503. res = ast_bridge_features_init(&chan_features);
  504. if (res) {
  505. ast_bridge_features_cleanup(&chan_features);
  506. publish_parked_call_failure(chan);
  507. return -1;
  508. }
  509. /* Now for the fun part... park it! */
  510. ast_bridge_join(parking_bridge, chan, NULL, &chan_features, NULL, 0);
  511. /*
  512. * If the bridge was broken for a hangup that isn't real, then
  513. * don't run the h extension, because the channel isn't really
  514. * hung up. This should only happen with AST_SOFTHANGUP_ASYNCGOTO.
  515. */
  516. res = -1;
  517. ast_channel_lock(chan);
  518. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  519. res = 0;
  520. }
  521. ast_channel_unlock(chan);
  522. ast_bridge_features_cleanup(&chan_features);
  523. return res;
  524. }
  525. /* Retrieve a parked call */
  526. static int parked_call_app_exec(struct ast_channel *chan, const char *data)
  527. {
  528. RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
  529. RAII_VAR(struct parked_user *, pu, NULL, ao2_cleanup); /* Parked user being retrieved */
  530. struct ast_bridge *retrieval_bridge;
  531. int res;
  532. int target_space = -1;
  533. struct ast_bridge_features chan_features;
  534. char *parse;
  535. const char *lot_name;
  536. AST_DECLARE_APP_ARGS(args,
  537. AST_APP_ARG(lot_name);
  538. AST_APP_ARG(parking_space);
  539. AST_APP_ARG(other); /* Any remaining unused arguments */
  540. );
  541. parse = ast_strdupa(data);
  542. AST_STANDARD_APP_ARGS(args, parse);
  543. /* Answer the channel if needed */
  544. if (ast_channel_state(chan) != AST_STATE_UP) {
  545. ast_answer(chan);
  546. }
  547. lot_name = args.lot_name;
  548. /* If the name of the parking lot isn't in the arguments, find it based on the channel. */
  549. if (ast_strlen_zero(lot_name)) {
  550. ast_channel_lock(chan);
  551. lot_name = ast_strdupa(find_channel_parking_lot_name(chan));
  552. ast_channel_unlock(chan);
  553. }
  554. lot = parking_lot_find_by_name(lot_name);
  555. if (!lot) {
  556. ast_log(LOG_ERROR, "Could not find the requested parking lot\n");
  557. ast_stream_and_wait(chan, "pbx-invalidpark", "");
  558. return -1;
  559. }
  560. if (!ast_strlen_zero(args.parking_space)) {
  561. if (sscanf(args.parking_space, "%d", &target_space) != 1 || target_space < 0) {
  562. ast_stream_and_wait(chan, "pbx-invalidpark", "");
  563. ast_log(LOG_ERROR, "value '%s' for parking_space argument is invalid. Must be an integer greater than 0.\n", args.parking_space);
  564. return -1;
  565. }
  566. }
  567. /* Attempt to get the parked user from the parking lot */
  568. pu = parking_lot_retrieve_parked_user(lot, target_space);
  569. if (!pu) {
  570. ast_stream_and_wait(chan, "pbx-invalidpark", "");
  571. return -1;
  572. }
  573. /* The parked call needs to know who is retrieving it before we move it out of the parking bridge */
  574. ast_assert(pu->retriever == NULL);
  575. pu->retriever = ast_channel_snapshot_create(chan);
  576. /* Create bridge */
  577. retrieval_bridge = ast_bridge_basic_new();
  578. if (!retrieval_bridge) {
  579. return -1;
  580. }
  581. /* Move the parkee into the new bridge */
  582. if (ast_bridge_move(retrieval_bridge, lot->parking_bridge, pu->chan, NULL, 0)) {
  583. ast_bridge_destroy(retrieval_bridge, 0);
  584. return -1;
  585. }
  586. /* Initialize our bridge features */
  587. res = ast_bridge_features_init(&chan_features);
  588. if (res) {
  589. ast_bridge_destroy(retrieval_bridge, 0);
  590. ast_bridge_features_cleanup(&chan_features);
  591. return -1;
  592. }
  593. /* Set the features */
  594. parked_call_retrieve_enable_features(chan, lot, AST_FEATURE_FLAG_BYCALLER);
  595. /* If the parkedplay option is set for the caller to hear, play that tone now. */
  596. if (lot->cfg->parkedplay & AST_FEATURE_FLAG_BYCALLER) {
  597. ast_stream_and_wait(chan, lot->cfg->courtesytone, NULL);
  598. }
  599. /* Now we should try to join the new bridge ourselves... */
  600. ast_bridge_join(retrieval_bridge, chan, NULL, &chan_features, NULL,
  601. AST_BRIDGE_JOIN_PASS_REFERENCE);
  602. ast_bridge_features_cleanup(&chan_features);
  603. /* Return -1 so that call does not continue in the dialplan. This is to make
  604. * behavior consistent with Asterisk versions prior to 12.
  605. */
  606. return -1;
  607. }
  608. struct park_announce_subscription_data {
  609. char *parkee_uuid;
  610. char *dial_string;
  611. char *announce_string;
  612. };
  613. static void park_announce_subscription_data_destroy(void *data)
  614. {
  615. struct park_announce_subscription_data *pa_data = data;
  616. ast_free(pa_data->parkee_uuid);
  617. ast_free(pa_data->dial_string);
  618. ast_free(pa_data->announce_string);
  619. ast_free(pa_data);
  620. }
  621. static struct park_announce_subscription_data *park_announce_subscription_data_create(const char *parkee_uuid,
  622. const char *dial_string,
  623. const char *announce_string)
  624. {
  625. struct park_announce_subscription_data *pa_data;
  626. if (!(pa_data = ast_calloc(1, sizeof(*pa_data)))) {
  627. return NULL;
  628. }
  629. if (!(pa_data->parkee_uuid = ast_strdup(parkee_uuid))
  630. || !(pa_data->dial_string = ast_strdup(dial_string))
  631. || !(pa_data->announce_string = ast_strdup(announce_string))) {
  632. park_announce_subscription_data_destroy(pa_data);
  633. return NULL;
  634. }
  635. return pa_data;
  636. }
  637. /*! \internal
  638. * \brief Gathers inheritable channel variables from a channel by name.
  639. *
  640. * \param oh outgoing helper struct we are bestowing inheritable variables to
  641. * \param channel_id name or uniqueID of the channel to inherit variables from
  642. *
  643. * \return Nothing
  644. */
  645. static void inherit_channel_vars_from_id(struct outgoing_helper *oh, const char *channel_id)
  646. {
  647. struct ast_channel *chan = ast_channel_get_by_name(channel_id);
  648. struct ast_var_t *current;
  649. struct ast_variable *newvar;
  650. const char *varname;
  651. int vartype;
  652. if (!chan) {
  653. /* Already gone */
  654. return;
  655. }
  656. ast_channel_lock(chan);
  657. AST_LIST_TRAVERSE(ast_channel_varshead((struct ast_channel *) chan), current, entries) {
  658. varname = ast_var_full_name(current);
  659. if (!varname) {
  660. continue;
  661. }
  662. vartype = 0;
  663. if (varname[0] == '_') {
  664. vartype = 1;
  665. if (varname[1] == '_') {
  666. vartype = 2;
  667. }
  668. }
  669. switch (vartype) {
  670. case 1:
  671. newvar = ast_variable_new(&varname[1], ast_var_value(current), "");
  672. break;
  673. case 2:
  674. newvar = ast_variable_new(varname, ast_var_value(current), "");
  675. break;
  676. default:
  677. continue;
  678. }
  679. if (newvar) {
  680. ast_debug(1, "Inheriting variable %s from %s.\n",
  681. newvar->name, ast_channel_name(chan));
  682. if (oh->vars) {
  683. newvar->next = oh->vars;
  684. oh->vars = newvar;
  685. }
  686. }
  687. }
  688. ast_channel_unlock(chan);
  689. ast_channel_cleanup(chan);
  690. }
  691. static void announce_to_dial(char *dial_string, char *announce_string, int parkingspace, struct ast_channel_snapshot *parkee_snapshot)
  692. {
  693. struct ast_channel *dchan;
  694. struct outgoing_helper oh = { 0, };
  695. int outstate;
  696. struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  697. char buf[13];
  698. char *dial_tech;
  699. char *cur_announce;
  700. dial_tech = strsep(&dial_string, "/");
  701. ast_verb(3, "Dial Tech,String: (%s,%s)\n", dial_tech, dial_string);
  702. if (!cap_slin) {
  703. ast_log(LOG_WARNING, "PARK: Failed to announce park.\n");
  704. goto announce_cleanup;
  705. }
  706. ast_format_cap_append(cap_slin, ast_format_slin, 0);
  707. snprintf(buf, sizeof(buf), "%d", parkingspace);
  708. oh.vars = ast_variable_new("_PARKEDAT", buf, "");
  709. inherit_channel_vars_from_id(&oh, parkee_snapshot->uniqueid);
  710. dchan = __ast_request_and_dial(dial_tech, cap_slin, NULL, NULL, dial_string, 30000,
  711. &outstate,
  712. parkee_snapshot->caller_number,
  713. parkee_snapshot->caller_name,
  714. &oh);
  715. ast_variables_destroy(oh.vars);
  716. if (!dchan) {
  717. ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n");
  718. goto announce_cleanup;
  719. }
  720. ast_verb(4, "Announce Template: %s\n", announce_string);
  721. for (cur_announce = strsep(&announce_string, ":"); cur_announce; cur_announce = strsep(&announce_string, ":")) {
  722. ast_verb(4, "Announce:%s\n", cur_announce);
  723. if (!strcmp(cur_announce, "PARKED")) {
  724. ast_say_digits(dchan, parkingspace, "", ast_channel_language(dchan));
  725. } else {
  726. int dres = ast_streamfile(dchan, cur_announce, ast_channel_language(dchan));
  727. if (!dres) {
  728. dres = ast_waitstream(dchan, "");
  729. } else {
  730. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", cur_announce, ast_channel_name(dchan));
  731. }
  732. }
  733. }
  734. ast_stopstream(dchan);
  735. ast_hangup(dchan);
  736. announce_cleanup:
  737. ao2_cleanup(cap_slin);
  738. }
  739. static void park_announce_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  740. {
  741. struct park_announce_subscription_data *pa_data = data;
  742. char *dial_string = pa_data->dial_string;
  743. struct ast_parked_call_payload *payload = stasis_message_data(message);
  744. if (stasis_subscription_final_message(sub, message)) {
  745. park_announce_subscription_data_destroy(data);
  746. return;
  747. }
  748. if (ast_parked_call_type() != stasis_message_type(message)) {
  749. return;
  750. }
  751. if (payload->event_type != PARKED_CALL) {
  752. /* We are only concerned with calls parked */
  753. return;
  754. }
  755. if (strcmp(payload->parkee->uniqueid, pa_data->parkee_uuid)) {
  756. /* We are only concerned with the parkee we are subscribed for. */
  757. return;
  758. }
  759. if (!ast_strlen_zero(dial_string)) {
  760. announce_to_dial(dial_string, pa_data->announce_string, payload->parkingspace, payload->parkee);
  761. }
  762. *dial_string = '\0'; /* If we observe this dial string on a second pass, we don't want to do anything with it. */
  763. }
  764. static int park_and_announce_app_exec(struct ast_channel *chan, const char *data)
  765. {
  766. struct ast_bridge_features chan_features;
  767. char *parse;
  768. int res;
  769. int silence_announcements = 1;
  770. struct stasis_subscription *parking_subscription;
  771. struct park_announce_subscription_data *pa_data;
  772. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  773. AST_DECLARE_APP_ARGS(args,
  774. AST_APP_ARG(lot_name);
  775. AST_APP_ARG(options);
  776. AST_APP_ARG(announce_template);
  777. AST_APP_ARG(dial);
  778. AST_APP_ARG(others);/* Any remaining unused arguments */
  779. );
  780. if (ast_strlen_zero(data)) {
  781. ast_log(LOG_ERROR, "ParkAndAnnounce has required arguments. No arguments were provided.\n");
  782. return -1;
  783. }
  784. parse = ast_strdupa(data);
  785. AST_STANDARD_APP_ARGS(args, parse);
  786. if (ast_strlen_zero(args.announce_template)) {
  787. /* improperly configured arguments for the application */
  788. ast_log(LOG_ERROR, "ParkAndAnnounce requires the announce_template argument.\n");
  789. return -1;
  790. }
  791. if (ast_strlen_zero(args.dial)) {
  792. /* improperly configured arguments */
  793. ast_log(LOG_ERROR, "ParkAndAnnounce requires the dial argument.\n");
  794. return -1;
  795. }
  796. if (!strchr(args.dial, '/')) {
  797. ast_log(LOG_ERROR, "ParkAndAnnounce dial string '%s' is improperly formed.\n", args.dial);
  798. return -1;
  799. }
  800. /* Handle the common parking setup stuff */
  801. if (!(parking_bridge = park_application_setup(chan, NULL, data, &silence_announcements))) {
  802. return 0;
  803. }
  804. /* Initialize bridge features for the channel. */
  805. res = ast_bridge_features_init(&chan_features);
  806. if (res) {
  807. ast_bridge_features_cleanup(&chan_features);
  808. return -1;
  809. }
  810. /* subscribe to the parking message so that we can announce once it is parked */
  811. pa_data = park_announce_subscription_data_create(ast_channel_uniqueid(chan), args.dial, args.announce_template);
  812. if (!pa_data) {
  813. return -1;
  814. }
  815. if (!(parking_subscription = stasis_subscribe_pool(ast_parking_topic(), park_announce_update_cb, pa_data))) {
  816. /* Failed to create subscription */
  817. park_announce_subscription_data_destroy(pa_data);
  818. return -1;
  819. }
  820. stasis_subscription_accept_message_type(parking_subscription, ast_parked_call_type());
  821. stasis_subscription_accept_message_type(parking_subscription, stasis_subscription_change_type());
  822. stasis_subscription_set_filter(parking_subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  823. /* Now for the fun part... park it! */
  824. ast_bridge_join(parking_bridge, chan, NULL, &chan_features, NULL, 0);
  825. /* Toss the subscription since we aren't bridged at this point. */
  826. stasis_unsubscribe(parking_subscription);
  827. /*
  828. * If the bridge was broken for a hangup that isn't real, then
  829. * don't run the h extension, because the channel isn't really
  830. * hung up. This should only happen with AST_SOFTHANGUP_ASYNCGOTO.
  831. */
  832. res = -1;
  833. ast_channel_lock(chan);
  834. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  835. res = 0;
  836. }
  837. ast_channel_unlock(chan);
  838. ast_bridge_features_cleanup(&chan_features);
  839. return res;
  840. }
  841. int load_parking_applications(void)
  842. {
  843. const struct ast_module_info *ast_module_info = parking_get_module_info();
  844. if (ast_register_application_xml(PARK_APPLICATION, park_app_exec)) {
  845. return -1;
  846. }
  847. if (ast_register_application_xml(PARKED_CALL_APPLICATION, parked_call_app_exec)) {
  848. return -1;
  849. }
  850. if (ast_register_application_xml(PARK_AND_ANNOUNCE_APPLICATION, park_and_announce_app_exec)) {
  851. return -1;
  852. }
  853. return 0;
  854. }
  855. void unload_parking_applications(void)
  856. {
  857. ast_unregister_application(PARK_APPLICATION);
  858. ast_unregister_application(PARKED_CALL_APPLICATION);
  859. ast_unregister_application(PARK_AND_ANNOUNCE_APPLICATION);
  860. }