parking_applications.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. </description>
  89. <see-also>
  90. <ref type="application">ParkedCall</ref>
  91. </see-also>
  92. </application>
  93. <application name="ParkedCall" language="en_US">
  94. <synopsis>
  95. Retrieve a parked call.
  96. </synopsis>
  97. <syntax>
  98. <parameter name="parking_lot_name">
  99. <para>Specify from which parking lot to retrieve a parked call.</para>
  100. <para>The parking lot used is selected in the following order:</para>
  101. <para>1) parking_lot_name option</para>
  102. <para>2) <variable>PARKINGLOT</variable> variable</para>
  103. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  104. (Possibly preset by the channel driver.)</para>
  105. <para>4) Default parking lot.</para>
  106. </parameter>
  107. <parameter name="parking_space">
  108. <para>Parking space to retrieve a parked call from.
  109. If not provided then the first available parked call in the
  110. parking lot will be retrieved.</para>
  111. </parameter>
  112. </syntax>
  113. <description>
  114. <para>Used to retrieve a parked call from a parking lot.</para>
  115. <note>
  116. <para>If a parking lot's parkext option is set, then Parking lots
  117. will automatically create and manage dialplan extensions in
  118. the parking lot context. If that is the case then you will not
  119. need to manage parking extensions yourself, just include the
  120. parking context of the parking lot.</para>
  121. </note>
  122. </description>
  123. <see-also>
  124. <ref type="application">Park</ref>
  125. </see-also>
  126. </application>
  127. <application name="ParkAndAnnounce" language="en_US">
  128. <synopsis>
  129. Park and Announce.
  130. </synopsis>
  131. <syntax>
  132. <parameter name="parking_lot_name">
  133. <para>Specify in which parking lot to park a call.</para>
  134. <para>The parking lot used is selected in the following order:</para>
  135. <para>1) parking_lot_name option to this application</para>
  136. <para>2) <variable>PARKINGLOT</variable> variable</para>
  137. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  138. (Possibly preset by the channel driver.)</para>
  139. <para>4) Default parking lot.</para>
  140. </parameter>
  141. <parameter name="options">
  142. <para>A list of options for this parked call.</para>
  143. <optionlist>
  144. <option name="r">
  145. <para>Send ringing instead of MOH to the parked call.</para>
  146. </option>
  147. <option name="R">
  148. <para>Randomize the selection of a parking space.</para>
  149. </option>
  150. <option name="c" argsep=",">
  151. <argument name="context" required="false" />
  152. <argument name="extension" required="false" />
  153. <argument name="priority" required="true" />
  154. <para>If the parking times out, go to this place in the dialplan
  155. instead of where the parking lot defines the call should go.
  156. </para>
  157. </option>
  158. <option name="t">
  159. <argument name="duration" required="true" />
  160. <para>Use a timeout of <literal>duration</literal> seconds instead
  161. of the timeout specified by the parking lot.</para>
  162. </option>
  163. </optionlist>
  164. </parameter>
  165. <parameter name="announce_template" required="true" argsep=":">
  166. <argument name="announce" required="true">
  167. <para>Colon-separated list of files to announce. The word
  168. <literal>PARKED</literal> will be replaced by a say_digits of the extension in which
  169. the call is parked.</para>
  170. </argument>
  171. <argument name="announce1" multiple="true" />
  172. </parameter>
  173. <parameter name="dial" required="true">
  174. <para>The app_dial style resource to call to make the
  175. announcement. Console/dsp calls the console.</para>
  176. </parameter>
  177. </syntax>
  178. <description>
  179. <para>Park a call into the parkinglot and announce the call to another channel.</para>
  180. <para>The variable <variable>PARKEDAT</variable> will contain the parking extension
  181. into which the call was placed. Use with the Local channel to allow the dialplan to make
  182. use of this information.</para>
  183. </description>
  184. <see-also>
  185. <ref type="application">Park</ref>
  186. <ref type="application">ParkedCall</ref>
  187. </see-also>
  188. </application>
  189. ***/
  190. #define PARK_AND_ANNOUNCE_APPLICATION "ParkAndAnnounce"
  191. /* Park a call */
  192. enum park_args {
  193. OPT_ARG_COMEBACK,
  194. OPT_ARG_TIMEOUT,
  195. OPT_ARG_ARRAY_SIZE /* Always the last element of the enum */
  196. };
  197. enum park_flags {
  198. MUXFLAG_RINGING = (1 << 0),
  199. MUXFLAG_RANDOMIZE = (1 << 1),
  200. MUXFLAG_NOANNOUNCE = (1 << 2),
  201. MUXFLAG_COMEBACK_OVERRIDE = (1 << 3),
  202. MUXFLAG_TIMEOUT_OVERRIDE = (1 << 4),
  203. };
  204. AST_APP_OPTIONS(park_opts, {
  205. AST_APP_OPTION('r', MUXFLAG_RINGING),
  206. AST_APP_OPTION('R', MUXFLAG_RANDOMIZE),
  207. AST_APP_OPTION('s', MUXFLAG_NOANNOUNCE),
  208. AST_APP_OPTION_ARG('c', MUXFLAG_COMEBACK_OVERRIDE, OPT_ARG_COMEBACK),
  209. AST_APP_OPTION_ARG('t', MUXFLAG_TIMEOUT_OVERRIDE, OPT_ARG_TIMEOUT),
  210. });
  211. static int apply_option_timeout (int *var, char *timeout_arg)
  212. {
  213. if (ast_strlen_zero(timeout_arg)) {
  214. ast_log(LOG_ERROR, "No duration value provided for the timeout ('t') option.\n");
  215. return -1;
  216. }
  217. if (sscanf(timeout_arg, "%d", var) != 1 || *var < 0) {
  218. ast_log(LOG_ERROR, "Duration value provided for timeout ('t') option must be 0 or greater.\n");
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. 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)
  224. {
  225. char *parse;
  226. struct ast_flags flags = { 0 };
  227. AST_DECLARE_APP_ARGS(args,
  228. AST_APP_ARG(lot_name);
  229. AST_APP_ARG(options);
  230. AST_APP_ARG(other); /* Any remaining unused arguments */
  231. );
  232. parse = ast_strdupa(data);
  233. AST_STANDARD_APP_ARGS(args, parse);
  234. if (args.options) {
  235. char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
  236. ast_app_parse_options(park_opts, &flags, opts, args.options);
  237. if (ast_test_flag(&flags, MUXFLAG_TIMEOUT_OVERRIDE)) {
  238. if (apply_option_timeout(time_limit, opts[OPT_ARG_TIMEOUT])) {
  239. return -1;
  240. }
  241. }
  242. if (ast_test_flag(&flags, MUXFLAG_COMEBACK_OVERRIDE)) {
  243. *comeback_override = ast_strdup(opts[OPT_ARG_COMEBACK]);
  244. }
  245. if (ast_test_flag(&flags, MUXFLAG_NOANNOUNCE)) {
  246. if (disable_announce) {
  247. *disable_announce = 1;
  248. }
  249. }
  250. if (ast_test_flag(&flags, MUXFLAG_RINGING)) {
  251. *use_ringing = 1;
  252. }
  253. if (ast_test_flag(&flags, MUXFLAG_RANDOMIZE)) {
  254. *randomize = 1;
  255. }
  256. }
  257. if (!ast_strlen_zero(args.lot_name)) {
  258. *lot_name = ast_strdup(args.lot_name);
  259. }
  260. return 0;
  261. }
  262. void park_common_datastore_free(struct park_common_datastore *datastore)
  263. {
  264. if (!datastore) {
  265. return;
  266. }
  267. ast_free(datastore->parker_uuid);
  268. ast_free(datastore->parker_dial_string);
  269. ast_free(datastore->comeback_override);
  270. ast_free(datastore);
  271. }
  272. static void park_common_datastore_destroy(void *data)
  273. {
  274. struct park_common_datastore *datastore = data;
  275. park_common_datastore_free(datastore);
  276. }
  277. static const struct ast_datastore_info park_common_info = {
  278. .type = "park entry data",
  279. .destroy = park_common_datastore_destroy,
  280. };
  281. static void wipe_park_common_datastore(struct ast_channel *chan)
  282. {
  283. struct ast_datastore *datastore;
  284. ast_channel_lock(chan);
  285. datastore = ast_channel_datastore_find(chan, &park_common_info, NULL);
  286. if (datastore) {
  287. ast_channel_datastore_remove(chan, datastore);
  288. ast_datastore_free(datastore);
  289. }
  290. ast_channel_unlock(chan);
  291. }
  292. 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)
  293. {
  294. struct ast_datastore *datastore = NULL;
  295. struct park_common_datastore *park_datastore;
  296. const char *attended_transfer;
  297. const char *blind_transfer;
  298. char *parker_dial_string = NULL;
  299. wipe_park_common_datastore(parkee);
  300. if (!(datastore = ast_datastore_alloc(&park_common_info, NULL))) {
  301. return -1;
  302. }
  303. if (!(park_datastore = ast_calloc(1, sizeof(*park_datastore)))) {
  304. ast_datastore_free(datastore);
  305. return -1;
  306. }
  307. if (parker_uuid) {
  308. park_datastore->parker_uuid = ast_strdup(parker_uuid);
  309. }
  310. ast_channel_lock(parkee);
  311. attended_transfer = pbx_builtin_getvar_helper(parkee, "ATTENDEDTRANSFER");
  312. blind_transfer = pbx_builtin_getvar_helper(parkee, "BLINDTRANSFER");
  313. if (attended_transfer || blind_transfer) {
  314. parker_dial_string = ast_strdupa(S_OR(attended_transfer, blind_transfer));
  315. }
  316. ast_channel_unlock(parkee);
  317. if (!ast_strlen_zero(parker_dial_string)) {
  318. ast_channel_name_to_dial_string(parker_dial_string);
  319. ast_verb(4, "Setting Parker dial string to %s from %s value", parker_dial_string, attended_transfer ? "ATTENDEDTRANSFER" : "BLINDTRANSFER");
  320. park_datastore->parker_dial_string = ast_strdup(parker_dial_string);
  321. }
  322. park_datastore->randomize = randomize;
  323. park_datastore->time_limit = time_limit;
  324. park_datastore->silence_announce = silence_announce;
  325. if (comeback_override) {
  326. park_datastore->comeback_override = ast_strdup(comeback_override);
  327. }
  328. datastore->data = park_datastore;
  329. ast_channel_lock(parkee);
  330. ast_channel_datastore_add(parkee, datastore);
  331. ast_channel_unlock(parkee);
  332. return 0;
  333. }
  334. struct park_common_datastore *get_park_common_datastore_copy(struct ast_channel *parkee)
  335. {
  336. struct ast_datastore *datastore;
  337. struct park_common_datastore *data;
  338. struct park_common_datastore *data_copy;
  339. SCOPED_CHANNELLOCK(lock, parkee);
  340. if (!(datastore = ast_channel_datastore_find(parkee, &park_common_info, NULL))) {
  341. return NULL;
  342. }
  343. data = datastore->data;
  344. if (!data) {
  345. /* This data should always be populated if this datastore was appended to the channel */
  346. ast_assert(0);
  347. }
  348. data_copy = ast_calloc(1, sizeof(*data_copy));
  349. if (!data_copy) {
  350. return NULL;
  351. }
  352. if (!(data_copy->parker_uuid = ast_strdup(data->parker_uuid))) {
  353. park_common_datastore_free(data_copy);
  354. return NULL;
  355. }
  356. data_copy->randomize = data->randomize;
  357. data_copy->time_limit = data->time_limit;
  358. data_copy->silence_announce = data->silence_announce;
  359. if (data->comeback_override) {
  360. data_copy->comeback_override = ast_strdup(data->comeback_override);
  361. if (!data_copy->comeback_override) {
  362. park_common_datastore_free(data_copy);
  363. return NULL;
  364. }
  365. }
  366. if (data->parker_dial_string) {
  367. data_copy->parker_dial_string = ast_strdup(data->parker_dial_string);
  368. if (!data_copy->parker_dial_string) {
  369. park_common_datastore_free(data_copy);
  370. return NULL;
  371. }
  372. }
  373. return data_copy;
  374. }
  375. struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
  376. const char *lot_name, const char *comeback_override,
  377. int use_ringing, int randomize, int time_limit, int silence_announcements)
  378. {
  379. struct ast_bridge *parking_bridge;
  380. RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
  381. if (!parker) {
  382. parker = parkee;
  383. }
  384. /* If the name of the parking lot isn't specified in the arguments, find it based on the channel. */
  385. if (ast_strlen_zero(lot_name)) {
  386. ast_channel_lock(parker);
  387. lot_name = ast_strdupa(find_channel_parking_lot_name(parker));
  388. ast_channel_unlock(parker);
  389. }
  390. lot = parking_lot_find_by_name(lot_name);
  391. if (!lot) {
  392. lot = parking_create_dynamic_lot(lot_name, parkee);
  393. }
  394. if (!lot) {
  395. ast_log(LOG_ERROR, "Could not find parking lot: '%s'\n", lot_name);
  396. return NULL;
  397. }
  398. ao2_lock(lot);
  399. parking_bridge = parking_lot_get_bridge(lot);
  400. ao2_unlock(lot);
  401. if (!parking_bridge) {
  402. return NULL;
  403. }
  404. /* Apply relevant bridge roles and such to the parking channel */
  405. parking_channel_set_roles(parkee, lot, use_ringing);
  406. setup_park_common_datastore(parkee, ast_channel_uniqueid(parker), comeback_override, randomize, time_limit,
  407. silence_announcements);
  408. return parking_bridge;
  409. }
  410. struct ast_bridge *park_application_setup(struct ast_channel *parkee, struct ast_channel *parker, const char *app_data,
  411. int *silence_announcements)
  412. {
  413. int use_ringing = 0;
  414. int randomize = 0;
  415. int time_limit = -1;
  416. RAII_VAR(char *, comeback_override, NULL, ast_free);
  417. RAII_VAR(char *, lot_name_app_arg, NULL, ast_free);
  418. if (app_data) {
  419. park_app_parse_data(app_data, silence_announcements, &use_ringing, &randomize, &time_limit, &comeback_override, &lot_name_app_arg);
  420. }
  421. return park_common_setup(parkee, parker, lot_name_app_arg, comeback_override, use_ringing,
  422. randomize, time_limit, silence_announcements ? *silence_announcements : 0);
  423. }
  424. static int park_app_exec(struct ast_channel *chan, const char *data)
  425. {
  426. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  427. struct ast_bridge_features chan_features;
  428. int res;
  429. int silence_announcements = 0;
  430. const char *transferer;
  431. /* Answer the channel if needed */
  432. if (ast_channel_state(chan) != AST_STATE_UP) {
  433. ast_answer(chan);
  434. }
  435. ast_channel_lock(chan);
  436. if (!(transferer = pbx_builtin_getvar_helper(chan, "ATTENDEDTRANSFER"))) {
  437. transferer = pbx_builtin_getvar_helper(chan, "BLINDTRANSFER");
  438. }
  439. transferer = ast_strdupa(S_OR(transferer, ""));
  440. ast_channel_unlock(chan);
  441. /* Handle the common parking setup stuff */
  442. if (!(parking_bridge = park_application_setup(chan, NULL, data, &silence_announcements))) {
  443. if (!silence_announcements && !transferer) {
  444. ast_stream_and_wait(chan, "pbx-parkingfailed", "");
  445. }
  446. publish_parked_call_failure(chan);
  447. return 0;
  448. }
  449. /* Initialize bridge features for the channel. */
  450. res = ast_bridge_features_init(&chan_features);
  451. if (res) {
  452. ast_bridge_features_cleanup(&chan_features);
  453. publish_parked_call_failure(chan);
  454. return -1;
  455. }
  456. /* Now for the fun part... park it! */
  457. ast_bridge_join(parking_bridge, chan, NULL, &chan_features, NULL, 0);
  458. /*
  459. * If the bridge was broken for a hangup that isn't real, then
  460. * don't run the h extension, because the channel isn't really
  461. * hung up. This should only happen with AST_SOFTHANGUP_ASYNCGOTO.
  462. */
  463. res = -1;
  464. ast_channel_lock(chan);
  465. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  466. res = 0;
  467. }
  468. ast_channel_unlock(chan);
  469. ast_bridge_features_cleanup(&chan_features);
  470. return res;
  471. }
  472. /* Retrieve a parked call */
  473. static int parked_call_app_exec(struct ast_channel *chan, const char *data)
  474. {
  475. RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
  476. RAII_VAR(struct parked_user *, pu, NULL, ao2_cleanup); /* Parked user being retrieved */
  477. struct ast_bridge *retrieval_bridge;
  478. int res;
  479. int target_space = -1;
  480. struct ast_bridge_features chan_features;
  481. char *parse;
  482. char *lot_name;
  483. AST_DECLARE_APP_ARGS(args,
  484. AST_APP_ARG(lot_name);
  485. AST_APP_ARG(parking_space);
  486. AST_APP_ARG(other); /* Any remaining unused arguments */
  487. );
  488. parse = ast_strdupa(data);
  489. AST_STANDARD_APP_ARGS(args, parse);
  490. /* Answer the channel if needed */
  491. if (ast_channel_state(chan) != AST_STATE_UP) {
  492. ast_answer(chan);
  493. }
  494. lot_name = args.lot_name;
  495. /* If the name of the parking lot isn't in the arguments, find it based on the channel. */
  496. if (ast_strlen_zero(lot_name)) {
  497. ast_channel_lock(chan);
  498. lot_name = ast_strdupa(find_channel_parking_lot_name(chan));
  499. ast_channel_unlock(chan);
  500. }
  501. lot = parking_lot_find_by_name(lot_name);
  502. if (!lot) {
  503. ast_log(LOG_ERROR, "Could not find the requested parking lot\n");
  504. ast_stream_and_wait(chan, "pbx-invalidpark", "");
  505. return -1;
  506. }
  507. if (!ast_strlen_zero(args.parking_space)) {
  508. if (sscanf(args.parking_space, "%d", &target_space) != 1 || target_space < 0) {
  509. ast_stream_and_wait(chan, "pbx-invalidpark", "");
  510. ast_log(LOG_ERROR, "value '%s' for parking_space argument is invalid. Must be an integer greater than 0.\n", args.parking_space);
  511. return -1;
  512. }
  513. }
  514. /* Attempt to get the parked user from the parking lot */
  515. pu = parking_lot_retrieve_parked_user(lot, target_space);
  516. if (!pu) {
  517. ast_stream_and_wait(chan, "pbx-invalidpark", "");
  518. return -1;
  519. }
  520. /* The parked call needs to know who is retrieving it before we move it out of the parking bridge */
  521. pu->retriever = ast_channel_snapshot_create(chan);
  522. /* Create bridge */
  523. retrieval_bridge = ast_bridge_basic_new();
  524. if (!retrieval_bridge) {
  525. return -1;
  526. }
  527. /* Move the parkee into the new bridge */
  528. if (ast_bridge_move(retrieval_bridge, lot->parking_bridge, pu->chan, NULL, 0)) {
  529. ast_bridge_destroy(retrieval_bridge, 0);
  530. return -1;
  531. }
  532. /* Initialize our bridge features */
  533. res = ast_bridge_features_init(&chan_features);
  534. if (res) {
  535. ast_bridge_destroy(retrieval_bridge, 0);
  536. ast_bridge_features_cleanup(&chan_features);
  537. return -1;
  538. }
  539. /* Set the features */
  540. parked_call_retrieve_enable_features(chan, lot, AST_FEATURE_FLAG_BYCALLER);
  541. /* If the parkedplay option is set for the caller to hear, play that tone now. */
  542. if (lot->cfg->parkedplay & AST_FEATURE_FLAG_BYCALLER) {
  543. ast_stream_and_wait(chan, lot->cfg->courtesytone, NULL);
  544. }
  545. /* Now we should try to join the new bridge ourselves... */
  546. ast_bridge_join(retrieval_bridge, chan, NULL, &chan_features, NULL,
  547. AST_BRIDGE_JOIN_PASS_REFERENCE);
  548. ast_bridge_features_cleanup(&chan_features);
  549. return 0;
  550. }
  551. struct park_announce_subscription_data {
  552. char *parkee_uuid;
  553. char *dial_string;
  554. char *announce_string;
  555. };
  556. static void park_announce_subscription_data_destroy(void *data)
  557. {
  558. struct park_announce_subscription_data *pa_data = data;
  559. ast_free(pa_data->parkee_uuid);
  560. ast_free(pa_data->dial_string);
  561. ast_free(pa_data->announce_string);
  562. ast_free(pa_data);
  563. }
  564. static struct park_announce_subscription_data *park_announce_subscription_data_create(const char *parkee_uuid,
  565. const char *dial_string,
  566. const char *announce_string)
  567. {
  568. struct park_announce_subscription_data *pa_data;
  569. if (!(pa_data = ast_calloc(1, sizeof(*pa_data)))) {
  570. return NULL;
  571. }
  572. if (!(pa_data->parkee_uuid = ast_strdup(parkee_uuid))
  573. || !(pa_data->dial_string = ast_strdup(dial_string))
  574. || !(pa_data->announce_string = ast_strdup(announce_string))) {
  575. park_announce_subscription_data_destroy(pa_data);
  576. return NULL;
  577. }
  578. return pa_data;
  579. }
  580. static void announce_to_dial(char *dial_string, char *announce_string, int parkingspace, struct ast_channel_snapshot *parkee_snapshot)
  581. {
  582. struct ast_channel *dchan;
  583. struct outgoing_helper oh = { 0, };
  584. int outstate;
  585. struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  586. char buf[13];
  587. char *dial_tech;
  588. char *cur_announce;
  589. dial_tech = strsep(&dial_string, "/");
  590. ast_verb(3, "Dial Tech,String: (%s,%s)\n", dial_tech, dial_string);
  591. if (!cap_slin) {
  592. ast_log(LOG_WARNING, "PARK: Failed to announce park.\n");
  593. goto announce_cleanup;
  594. }
  595. ast_format_cap_append(cap_slin, ast_format_slin, 0);
  596. snprintf(buf, sizeof(buf), "%d", parkingspace);
  597. oh.vars = ast_variable_new("_PARKEDAT", buf, "");
  598. dchan = __ast_request_and_dial(dial_tech, cap_slin, NULL, NULL, dial_string, 30000,
  599. &outstate,
  600. parkee_snapshot->caller_number,
  601. parkee_snapshot->caller_name,
  602. &oh);
  603. ast_variables_destroy(oh.vars);
  604. if (!dchan) {
  605. ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n");
  606. goto announce_cleanup;
  607. }
  608. ast_verb(4, "Announce Template: %s\n", announce_string);
  609. for (cur_announce = strsep(&announce_string, ":"); cur_announce; cur_announce = strsep(&announce_string, ":")) {
  610. ast_verb(4, "Announce:%s\n", cur_announce);
  611. if (!strcmp(cur_announce, "PARKED")) {
  612. ast_say_digits(dchan, parkingspace, "", ast_channel_language(dchan));
  613. } else {
  614. int dres = ast_streamfile(dchan, cur_announce, ast_channel_language(dchan));
  615. if (!dres) {
  616. dres = ast_waitstream(dchan, "");
  617. } else {
  618. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", cur_announce, ast_channel_name(dchan));
  619. }
  620. }
  621. }
  622. ast_stopstream(dchan);
  623. ast_hangup(dchan);
  624. announce_cleanup:
  625. ao2_cleanup(cap_slin);
  626. }
  627. static void park_announce_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  628. {
  629. struct park_announce_subscription_data *pa_data = data;
  630. char *dial_string = pa_data->dial_string;
  631. struct ast_parked_call_payload *payload = stasis_message_data(message);
  632. if (stasis_subscription_final_message(sub, message)) {
  633. park_announce_subscription_data_destroy(data);
  634. return;
  635. }
  636. if (payload->event_type != PARKED_CALL) {
  637. /* We are only concerned with calls parked */
  638. return;
  639. }
  640. if (strcmp(payload->parkee->uniqueid, pa_data->parkee_uuid)) {
  641. /* We are only concerned with the parkee we are subscribed for. */
  642. return;
  643. }
  644. if (!ast_strlen_zero(dial_string)) {
  645. announce_to_dial(dial_string, pa_data->announce_string, payload->parkingspace, payload->parkee);
  646. }
  647. *dial_string = '\0'; /* If we observe this dial string on a second pass, we don't want to do anything with it. */
  648. }
  649. static int park_and_announce_app_exec(struct ast_channel *chan, const char *data)
  650. {
  651. struct ast_bridge_features chan_features;
  652. char *parse;
  653. int res;
  654. int silence_announcements = 1;
  655. struct stasis_subscription *parking_subscription;
  656. struct park_announce_subscription_data *pa_data;
  657. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  658. AST_DECLARE_APP_ARGS(args,
  659. AST_APP_ARG(lot_name);
  660. AST_APP_ARG(options);
  661. AST_APP_ARG(announce_template);
  662. AST_APP_ARG(dial);
  663. AST_APP_ARG(others);/* Any remaining unused arguments */
  664. );
  665. if (ast_strlen_zero(data)) {
  666. ast_log(LOG_ERROR, "ParkAndAnnounce has required arguments. No arguments were provided.\n");
  667. return -1;
  668. }
  669. parse = ast_strdupa(data);
  670. AST_STANDARD_APP_ARGS(args, parse);
  671. if (ast_strlen_zero(args.announce_template)) {
  672. /* improperly configured arguments for the application */
  673. ast_log(LOG_ERROR, "ParkAndAnnounce requires the announce_template argument.\n");
  674. return -1;
  675. }
  676. if (ast_strlen_zero(args.dial)) {
  677. /* improperly configured arguments */
  678. ast_log(LOG_ERROR, "ParkAndAnnounce requires the dial argument.\n");
  679. return -1;
  680. }
  681. if (!strchr(args.dial, '/')) {
  682. ast_log(LOG_ERROR, "ParkAndAnnounce dial string '%s' is improperly formed.\n", args.dial);
  683. return -1;
  684. }
  685. /* Handle the common parking setup stuff */
  686. if (!(parking_bridge = park_application_setup(chan, NULL, data, &silence_announcements))) {
  687. return 0;
  688. }
  689. /* Initialize bridge features for the channel. */
  690. res = ast_bridge_features_init(&chan_features);
  691. if (res) {
  692. ast_bridge_features_cleanup(&chan_features);
  693. return -1;
  694. }
  695. /* subscribe to the parking message so that we can announce once it is parked */
  696. pa_data = park_announce_subscription_data_create(ast_channel_uniqueid(chan), args.dial, args.announce_template);
  697. if (!pa_data) {
  698. return -1;
  699. }
  700. if (!(parking_subscription = stasis_subscribe(ast_parking_topic(), park_announce_update_cb, pa_data))) {
  701. /* Failed to create subscription */
  702. park_announce_subscription_data_destroy(pa_data);
  703. return -1;
  704. }
  705. /* Now for the fun part... park it! */
  706. ast_bridge_join(parking_bridge, chan, NULL, &chan_features, NULL, 0);
  707. /* Toss the subscription since we aren't bridged at this point. */
  708. stasis_unsubscribe(parking_subscription);
  709. /*
  710. * If the bridge was broken for a hangup that isn't real, then
  711. * don't run the h extension, because the channel isn't really
  712. * hung up. This should only happen with AST_SOFTHANGUP_ASYNCGOTO.
  713. */
  714. res = -1;
  715. ast_channel_lock(chan);
  716. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  717. res = 0;
  718. }
  719. ast_channel_unlock(chan);
  720. ast_bridge_features_cleanup(&chan_features);
  721. return res;
  722. }
  723. int load_parking_applications(void)
  724. {
  725. const struct ast_module_info *ast_module_info = parking_get_module_info();
  726. if (ast_register_application_xml(PARK_APPLICATION, park_app_exec)) {
  727. return -1;
  728. }
  729. if (ast_register_application_xml(PARKED_CALL_APPLICATION, parked_call_app_exec)) {
  730. return -1;
  731. }
  732. if (ast_register_application_xml(PARK_AND_ANNOUNCE_APPLICATION, park_and_announce_app_exec)) {
  733. return -1;
  734. }
  735. return 0;
  736. }
  737. void unload_parking_applications(void)
  738. {
  739. ast_unregister_application(PARK_APPLICATION);
  740. ast_unregister_application(PARKED_CALL_APPLICATION);
  741. ast_unregister_application(PARK_AND_ANNOUNCE_APPLICATION);
  742. }