parking_bridge_features.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 Parking Bridge DTMF and Interval features
  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/utils.h"
  28. #include "asterisk/astobj2.h"
  29. #include "asterisk/logger.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/bridge.h"
  32. #include "asterisk/bridge_internal.h"
  33. #include "asterisk/bridge_channel.h"
  34. #include "asterisk/bridge_features.h"
  35. #include "asterisk/features.h"
  36. #include "asterisk/say.h"
  37. #include "asterisk/datastore.h"
  38. #include "asterisk/stasis.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/core_local.h"
  41. #include "asterisk/causes.h"
  42. struct parked_subscription_datastore {
  43. struct stasis_subscription *parked_subscription;
  44. };
  45. struct parked_subscription_data {
  46. struct transfer_channel_data *transfer_data;
  47. char *parkee_uuid;
  48. int hangup_after:1;
  49. char parker_uuid[0];
  50. };
  51. static void parked_subscription_datastore_destroy(void *data)
  52. {
  53. struct parked_subscription_datastore *subscription_datastore = data;
  54. stasis_unsubscribe(subscription_datastore->parked_subscription);
  55. subscription_datastore->parked_subscription = NULL;
  56. ast_free(subscription_datastore);
  57. }
  58. static const struct ast_datastore_info parked_subscription_info = {
  59. .type = "park subscription",
  60. .destroy = parked_subscription_datastore_destroy,
  61. };
  62. static void wipe_subscription_datastore(struct ast_channel *chan)
  63. {
  64. struct ast_datastore *datastore;
  65. ast_channel_lock(chan);
  66. datastore = ast_channel_datastore_find(chan, &parked_subscription_info, NULL);
  67. if (datastore) {
  68. ast_channel_datastore_remove(chan, datastore);
  69. ast_datastore_free(datastore);
  70. }
  71. ast_channel_unlock(chan);
  72. }
  73. static void parker_parked_call_message_response(struct ast_parked_call_payload *message, struct parked_subscription_data *data,
  74. struct stasis_subscription *sub)
  75. {
  76. const char *parkee_to_act_on = data->parkee_uuid;
  77. char saynum_buf[16];
  78. struct ast_channel_snapshot *parkee_snapshot = message->parkee;
  79. RAII_VAR(struct ast_channel *, parker, NULL, ast_channel_cleanup);
  80. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  81. if (strcmp(parkee_to_act_on, parkee_snapshot->uniqueid)) {
  82. return;
  83. }
  84. if (message->event_type != PARKED_CALL && message->event_type != PARKED_CALL_FAILED) {
  85. /* We only care about these two event types */
  86. return;
  87. }
  88. parker = ast_channel_get_by_name(data->parker_uuid);
  89. if (!parker) {
  90. return;
  91. }
  92. ast_channel_lock(parker);
  93. bridge_channel = ast_channel_get_bridge_channel(parker);
  94. ast_channel_unlock(parker);
  95. if (!bridge_channel) {
  96. return;
  97. }
  98. /* This subscription callback will block for the duration of the announcement if
  99. * parked_subscription_data is tracking a transfer_channel_data struct. */
  100. if (message->event_type == PARKED_CALL) {
  101. /* queue the saynum on the bridge channel and hangup */
  102. snprintf(saynum_buf, sizeof(saynum_buf), "%d %u", data->hangup_after, message->parkingspace);
  103. if (!data->transfer_data) {
  104. ast_bridge_channel_queue_playfile(bridge_channel, say_parking_space, saynum_buf, NULL);
  105. } else {
  106. ast_bridge_channel_queue_playfile_sync(bridge_channel, say_parking_space, saynum_buf, NULL);
  107. data->transfer_data->completed = 1;
  108. }
  109. wipe_subscription_datastore(parker);
  110. } else if (message->event_type == PARKED_CALL_FAILED) {
  111. if (!data->transfer_data) {
  112. ast_bridge_channel_queue_playfile(bridge_channel, NULL, "pbx-parkingfailed", NULL);
  113. } else {
  114. ast_bridge_channel_queue_playfile_sync(bridge_channel, NULL, "pbx-parkingfailed", NULL);
  115. data->transfer_data->completed = 1;
  116. }
  117. wipe_subscription_datastore(parker);
  118. }
  119. }
  120. static void parker_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  121. {
  122. if (stasis_subscription_final_message(sub, message)) {
  123. struct parked_subscription_data *ps_data = data;
  124. ao2_cleanup(ps_data->transfer_data);
  125. ps_data->transfer_data = NULL;
  126. ast_free(data);
  127. return;
  128. }
  129. if (stasis_message_type(message) == ast_parked_call_type()) {
  130. struct ast_parked_call_payload *parked_call_message = stasis_message_data(message);
  131. parker_parked_call_message_response(parked_call_message, data, sub);
  132. }
  133. }
  134. static int create_parked_subscription_full(struct ast_channel *chan, const char *parkee_uuid, int hangup_after,
  135. struct transfer_channel_data *parked_channel_data)
  136. {
  137. struct ast_datastore *datastore;
  138. struct parked_subscription_datastore *parked_datastore;
  139. struct parked_subscription_data *subscription_data;
  140. char *parker_uuid = ast_strdupa(ast_channel_uniqueid(chan));
  141. size_t parker_uuid_size = strlen(parker_uuid) + 1;
  142. /* If there is already a subscription, get rid of it. */
  143. wipe_subscription_datastore(chan);
  144. if (!(datastore = ast_datastore_alloc(&parked_subscription_info, NULL))) {
  145. return -1;
  146. }
  147. if (!(parked_datastore = ast_calloc(1, sizeof(*parked_datastore)))) {
  148. ast_datastore_free(datastore);
  149. return -1;
  150. }
  151. if (!(subscription_data = ast_calloc(1, sizeof(*subscription_data) + parker_uuid_size +
  152. strlen(parkee_uuid) + 1))) {
  153. ast_datastore_free(datastore);
  154. ast_free(parked_datastore);
  155. return -1;
  156. }
  157. if (parked_channel_data) {
  158. subscription_data->transfer_data = parked_channel_data;
  159. ao2_ref(parked_channel_data, +1);
  160. }
  161. subscription_data->hangup_after = hangup_after;
  162. subscription_data->parkee_uuid = subscription_data->parker_uuid + parker_uuid_size;
  163. strcpy(subscription_data->parkee_uuid, parkee_uuid);
  164. strcpy(subscription_data->parker_uuid, parker_uuid);
  165. if (!(parked_datastore->parked_subscription = stasis_subscribe(ast_parking_topic(), parker_update_cb, subscription_data))) {
  166. return -1;
  167. }
  168. datastore->data = parked_datastore;
  169. ast_channel_lock(chan);
  170. ast_channel_datastore_add(chan, datastore);
  171. ast_channel_unlock(chan);
  172. return 0;
  173. }
  174. int create_parked_subscription(struct ast_channel *chan, const char *parkee_uuid, int hangup_after)
  175. {
  176. return create_parked_subscription_full(chan, parkee_uuid, hangup_after, NULL);
  177. }
  178. /*!
  179. * \internal
  180. * \brief Helper function that creates an outgoing channel and returns it immediately. This function is nearly
  181. * identical to the dial_transfer function in bridge_basic.c, however it doesn't swap the
  182. * local channel and the channel that instigated the park.
  183. */
  184. static struct ast_channel *park_local_transfer(struct ast_channel *parker, const char *context, const char *exten, struct transfer_channel_data *parked_channel_data)
  185. {
  186. char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
  187. struct ast_channel *parkee;
  188. struct ast_channel *parkee_side_2;
  189. int cause;
  190. /* Fill the variable with the extension and context we want to call */
  191. snprintf(destination, sizeof(destination), "%s@%s", exten, context);
  192. /* Now we request that chan_local prepare to call the destination */
  193. parkee = ast_request("Local", ast_channel_nativeformats(parker), NULL, parker, destination,
  194. &cause);
  195. if (!parkee) {
  196. return NULL;
  197. }
  198. /* Before we actually dial out let's inherit appropriate information. */
  199. ast_channel_lock_both(parker, parkee);
  200. ast_channel_req_accountcodes(parkee, parker, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  201. ast_connected_line_copy_from_caller(ast_channel_connected(parkee), ast_channel_caller(parker));
  202. ast_channel_inherit_variables(parker, parkee);
  203. ast_channel_datastore_inherit(parker, parkee);
  204. ast_channel_unlock(parker);
  205. parkee_side_2 = ast_local_get_peer(parkee);
  206. ast_assert(parkee_side_2 != NULL);
  207. ast_channel_unlock(parkee);
  208. /* We need to have the parker subscribe to the new local channel before hand. */
  209. if (create_parked_subscription_full(parker, ast_channel_uniqueid(parkee_side_2), 1, parked_channel_data)) {
  210. ast_channel_unref(parkee_side_2);
  211. ast_hangup(parkee);
  212. return NULL;
  213. }
  214. ast_bridge_set_transfer_variables(parkee_side_2, ast_channel_name(parker), 0);
  215. ast_channel_unref(parkee_side_2);
  216. /* Since the above worked fine now we actually call it and return the channel */
  217. if (ast_call(parkee, destination, 0)) {
  218. ast_hangup(parkee);
  219. return NULL;
  220. }
  221. return parkee;
  222. }
  223. /*!
  224. * \internal
  225. * \brief Determine if an extension is a parking extension
  226. */
  227. static int parking_is_exten_park(const char *context, const char *exten)
  228. {
  229. struct ast_exten *exten_obj;
  230. struct pbx_find_info info = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  231. const char *app_at_exten;
  232. ast_debug(4, "Checking if %s@%s is a parking exten\n", exten, context);
  233. exten_obj = pbx_find_extension(NULL, NULL, &info, context, exten, 1, NULL, NULL, E_MATCH);
  234. if (!exten_obj) {
  235. return 0;
  236. }
  237. app_at_exten = ast_get_extension_app(exten_obj);
  238. if (!app_at_exten || strcasecmp(PARK_APPLICATION, app_at_exten)) {
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. /*!
  244. * \internal
  245. * \since 12.0.0
  246. * \brief Perform a blind transfer to a parking lot
  247. *
  248. * In general, most parking features should work to call this function. This will safely
  249. * park either a channel in the bridge with \ref bridge_channel or will park the entire
  250. * bridge if more than one channel is in the bridge. It will create the correct data to
  251. * pass to the \ref AstBridging Bridging API to safely park the channel.
  252. *
  253. * \param bridge_channel The bridge_channel representing the channel performing the park
  254. * \param context The context to blind transfer to
  255. * \param exten The extension to blind transfer to
  256. * \param parked_channel_cb Optional callback executed prior to sending the parked channel into the bridge
  257. * \param parked_channel_data Data for the parked_channel_cb
  258. *
  259. * \retval 0 on success
  260. * \retval non-zero on error
  261. */
  262. static int parking_blind_transfer_park(struct ast_bridge_channel *bridge_channel,
  263. const char *context, const char *exten, transfer_channel_cb parked_channel_cb,
  264. struct transfer_channel_data *parked_channel_data)
  265. {
  266. RAII_VAR(struct ast_bridge_channel *, other, NULL, ao2_cleanup);
  267. RAII_VAR(struct ast_channel *, other_chan, NULL, ast_channel_cleanup);
  268. struct ast_exten *e;
  269. struct pbx_find_info find_info = { .stacklen = 0 };
  270. int peer_count;
  271. if (ast_strlen_zero(context) || ast_strlen_zero(exten)) {
  272. return -1;
  273. }
  274. if (!bridge_channel->in_bridge) {
  275. return -1;
  276. }
  277. if (!parking_is_exten_park(context, exten)) {
  278. return -1;
  279. }
  280. ast_bridge_channel_lock_bridge(bridge_channel);
  281. peer_count = bridge_channel->bridge->num_channels;
  282. if (peer_count == 2) {
  283. other = ast_bridge_channel_peer(bridge_channel);
  284. ao2_ref(other, +1);
  285. other_chan = other->chan;
  286. ast_channel_ref(other_chan);
  287. }
  288. ast_bridge_unlock(bridge_channel->bridge);
  289. if (peer_count < 2) {
  290. /* There is nothing to do if there is no one to park. */
  291. return -1;
  292. }
  293. /* With a multiparty bridge, we need to do a regular blind transfer. We link the
  294. * existing bridge to the parking lot with a Local channel rather than
  295. * transferring others. */
  296. if (peer_count > 2) {
  297. struct ast_channel *transfer_chan = NULL;
  298. transfer_chan = park_local_transfer(bridge_channel->chan, context, exten, parked_channel_data);
  299. if (!transfer_chan) {
  300. return -1;
  301. }
  302. ast_channel_ref(transfer_chan);
  303. if (parked_channel_cb) {
  304. parked_channel_cb(transfer_chan, parked_channel_data, AST_BRIDGE_TRANSFER_MULTI_PARTY);
  305. }
  306. if (ast_bridge_impart(bridge_channel->bridge, transfer_chan, NULL, NULL,
  307. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  308. ast_hangup(transfer_chan);
  309. ast_channel_unref(transfer_chan);
  310. return -1;
  311. }
  312. ast_channel_unref(transfer_chan);
  313. return 0;
  314. }
  315. /* Subscribe to park messages with the other channel entering */
  316. if (create_parked_subscription_full(bridge_channel->chan, ast_channel_uniqueid(other->chan), 1, parked_channel_data)) {
  317. return -1;
  318. }
  319. if (parked_channel_cb) {
  320. parked_channel_cb(other_chan, parked_channel_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
  321. }
  322. e = pbx_find_extension(NULL, NULL, &find_info, context, exten, 1, NULL, NULL, E_MATCH);
  323. /* Write the park frame with the intended recipient and other data out to the bridge. */
  324. ast_bridge_channel_write_park(bridge_channel,
  325. ast_channel_uniqueid(other_chan),
  326. ast_channel_uniqueid(bridge_channel->chan),
  327. e ? ast_get_extension_app_data(e) : NULL);
  328. return 0;
  329. }
  330. /*!
  331. * \internal
  332. * \since 12.0.0
  333. * \brief Perform a direct park on a channel in a bridge
  334. *
  335. * \note This will be called from within the \ref AstBridging Bridging API
  336. *
  337. * \param bridge_channel The bridge_channel representing the channel to be parked
  338. * \param uuid_parkee The UUID of the channel being parked
  339. * \param uuid_parker The UUID of the channel performing the park
  340. * \param app_data Application parseable data to pass to the parking application
  341. */
  342. static int parking_park_bridge_channel(struct ast_bridge_channel *bridge_channel, const char *uuid_parkee, const char *uuid_parker, const char *app_data)
  343. {
  344. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  345. RAII_VAR(struct ast_bridge *, original_bridge, NULL, ao2_cleanup);
  346. RAII_VAR(struct ast_channel *, parker, NULL, ao2_cleanup);
  347. if (strcmp(ast_channel_uniqueid(bridge_channel->chan), uuid_parkee)) {
  348. /* We aren't the parkee, so ignore this action. */
  349. return -1;
  350. }
  351. parker = ast_channel_get_by_name(uuid_parker);
  352. if (!parker) {
  353. ast_log(LOG_NOTICE, "Channel with uuid %s left before we could start parking the call. Parking canceled.\n", uuid_parker);
  354. publish_parked_call_failure(bridge_channel->chan);
  355. return -1;
  356. }
  357. if (!(parking_bridge = park_application_setup(bridge_channel->chan, parker, app_data, NULL))) {
  358. publish_parked_call_failure(bridge_channel->chan);
  359. return -1;
  360. }
  361. ast_bridge_set_transfer_variables(bridge_channel->chan, ast_channel_name(parker), 0);
  362. /* bridge_channel must be locked so we can get a reference to the bridge it is currently on */
  363. ao2_lock(bridge_channel);
  364. original_bridge = bridge_channel->bridge;
  365. if (!original_bridge) {
  366. ao2_unlock(bridge_channel);
  367. publish_parked_call_failure(bridge_channel->chan);
  368. return -1;
  369. }
  370. ao2_ref(original_bridge, +1); /* Cleaned by RAII_VAR */
  371. ao2_unlock(bridge_channel);
  372. if (ast_bridge_move(parking_bridge, original_bridge, bridge_channel->chan, NULL, 1)) {
  373. ast_log(LOG_ERROR, "Failed to move %s into the parking bridge.\n",
  374. ast_channel_name(bridge_channel->chan));
  375. return -1;
  376. }
  377. return 0;
  378. }
  379. /*!
  380. * \internal
  381. * \since 12.0.0
  382. * \brief Park a call
  383. *
  384. * \param parker The bridge_channel parking the call
  385. * \param exten Optional. The extension where the call was parked.
  386. * \param length Optional. If \c exten is specified, the length of the buffer.
  387. *
  388. * \note This will determine the context and extension to park the channel based on
  389. * the configuration of the \ref ast_channel associated with \ref parker. It will then
  390. * park either the channel or the entire bridge.
  391. *
  392. * \retval 0 on success
  393. * \retval -1 on error
  394. */
  395. static int parking_park_call(struct ast_bridge_channel *parker, char *exten, size_t length)
  396. {
  397. RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
  398. const char *lot_name = NULL;
  399. ast_channel_lock(parker->chan);
  400. lot_name = find_channel_parking_lot_name(parker->chan);
  401. if (!ast_strlen_zero(lot_name)) {
  402. lot_name = ast_strdupa(lot_name);
  403. }
  404. ast_channel_unlock(parker->chan);
  405. if (ast_strlen_zero(lot_name)) {
  406. return -1;
  407. }
  408. lot = parking_lot_find_by_name(lot_name);
  409. if (!lot) {
  410. ast_log(AST_LOG_WARNING, "Cannot Park %s: lot %s unknown\n",
  411. ast_channel_name(parker->chan), lot_name);
  412. return -1;
  413. }
  414. if (exten) {
  415. ast_copy_string(exten, lot->cfg->parkext, length);
  416. }
  417. return parking_blind_transfer_park(parker, lot->cfg->parking_con, lot->cfg->parkext, NULL, NULL);
  418. }
  419. static int feature_park_call(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  420. {
  421. SCOPED_MODULE_USE(parking_get_module_info()->self);
  422. return parking_park_call(bridge_channel, NULL, 0);
  423. }
  424. /*!
  425. * \internal
  426. * \brief Setup the caller features for when that channel is dialed.
  427. * \since 12.0.0
  428. *
  429. * \param chan Parked channel leaving the parking lot.
  430. * \param cfg Parking lot configuration.
  431. *
  432. * \return Nothing
  433. */
  434. static void parking_timeout_set_caller_features(struct ast_channel *chan, struct parking_lot_cfg *cfg)
  435. {
  436. char features[5];
  437. char *pos;
  438. /*
  439. * We are setting the callee Dial flag values because in the
  440. * timeout case, the caller is who is being called back.
  441. */
  442. pos = features;
  443. if (cfg->parkedcalltransfers & AST_FEATURE_FLAG_BYCALLER) {
  444. *pos++ = 't';
  445. }
  446. if (cfg->parkedcallreparking & AST_FEATURE_FLAG_BYCALLER) {
  447. *pos++ = 'k';
  448. }
  449. if (cfg->parkedcallhangup & AST_FEATURE_FLAG_BYCALLER) {
  450. *pos++ = 'h';
  451. }
  452. if (cfg->parkedcallrecording & AST_FEATURE_FLAG_BYCALLER) {
  453. *pos++ = 'x';
  454. }
  455. *pos = '\0';
  456. pbx_builtin_setvar_helper(chan, "BRIDGE_FEATURES", features);
  457. }
  458. /*! \internal
  459. * \brief Interval hook. Pulls a parked call from the parking bridge after the timeout is passed and sets the resolution to timeout.
  460. *
  461. * \param bridge_channel bridge channel this interval hook is being executed on
  462. * \param hook_pvt A pointer to the parked_user struct associated with the channel is stuffed in here
  463. */
  464. static int parking_duration_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  465. {
  466. struct parked_user *user = hook_pvt;
  467. struct ast_channel *chan = user->chan;
  468. struct ast_context *park_dial_context;
  469. const char *dial_string;
  470. char *dial_string_flat;
  471. char parking_space[AST_MAX_EXTENSION];
  472. char returnexten[AST_MAX_EXTENSION];
  473. char *duplicate_returnexten;
  474. struct ast_exten *existing_exten;
  475. struct pbx_find_info pbx_finder = { .stacklen = 0 }; /* The rest is reset in pbx_find_extension */
  476. /* We are still in the bridge, so it's possible for other stuff to mess with the parked call before we leave the bridge
  477. to deal with this, lock the parked user, check and set resolution. */
  478. ao2_lock(user);
  479. if (user->resolution != PARK_UNSET) {
  480. /* Abandon timeout since something else has resolved the parked user before we got to it. */
  481. ao2_unlock(user);
  482. return -1;
  483. }
  484. user->resolution = PARK_TIMEOUT;
  485. ao2_unlock(user);
  486. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
  487. AST_CAUSE_NORMAL_CLEARING);
  488. dial_string = user->parker_dial_string;
  489. dial_string_flat = ast_strdupa(dial_string);
  490. flatten_dial_string(dial_string_flat);
  491. /* Set parking timeout channel variables */
  492. snprintf(parking_space, sizeof(parking_space), "%d", user->parking_space);
  493. ast_channel_lock(chan);
  494. ast_channel_stage_snapshot(chan);
  495. pbx_builtin_setvar_helper(chan, "PARKING_SPACE", parking_space);
  496. pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parking_space); /* Deprecated version of PARKING_SPACE */
  497. pbx_builtin_setvar_helper(chan, "PARKEDLOT", user->lot->name);
  498. pbx_builtin_setvar_helper(chan, "PARKER", dial_string);
  499. pbx_builtin_setvar_helper(chan, "PARKER_FLAT", dial_string_flat);
  500. parking_timeout_set_caller_features(chan, user->lot->cfg);
  501. ast_channel_stage_snapshot_done(chan);
  502. ast_channel_unlock(chan);
  503. /* Dialplan generation for park-dial extensions */
  504. if (ast_wrlock_contexts()) {
  505. ast_log(LOG_ERROR, "Failed to lock the contexts list. Can't add the park-dial extension.\n");
  506. return -1;
  507. }
  508. if (!(park_dial_context = ast_context_find_or_create(NULL, NULL, PARK_DIAL_CONTEXT, BASE_REGISTRAR))) {
  509. ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", PARK_DIAL_CONTEXT);
  510. if (ast_unlock_contexts()) {
  511. ast_assert(0);
  512. }
  513. goto abandon_extension_creation;
  514. }
  515. if (ast_wrlock_context(park_dial_context)) {
  516. ast_log(LOG_ERROR, "failed to obtain write lock on context '%s'\n", PARK_DIAL_CONTEXT);
  517. if (ast_unlock_contexts()) {
  518. ast_assert(0);
  519. }
  520. goto abandon_extension_creation;
  521. }
  522. if (ast_unlock_contexts()) {
  523. ast_assert(0);
  524. }
  525. snprintf(returnexten, sizeof(returnexten), "%s,%u", dial_string,
  526. user->lot->cfg->comebackdialtime);
  527. duplicate_returnexten = ast_strdup(returnexten);
  528. if (!duplicate_returnexten) {
  529. ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n",
  530. dial_string_flat, PARK_DIAL_CONTEXT, returnexten);
  531. }
  532. /* If an extension already exists here because we registered it for another parked call timing out, then we may overwrite it. */
  533. if ((existing_exten = pbx_find_extension(NULL, NULL, &pbx_finder, PARK_DIAL_CONTEXT, dial_string_flat, 1, NULL, NULL, E_MATCH)) &&
  534. (strcmp(ast_get_extension_registrar(existing_exten), BASE_REGISTRAR))) {
  535. ast_debug(3, "An extension for '%s@%s' was already registered by another registrar '%s'\n",
  536. dial_string_flat, PARK_DIAL_CONTEXT, ast_get_extension_registrar(existing_exten));
  537. } else if (ast_add_extension2_nolock(park_dial_context, 1, dial_string_flat, 1, NULL, NULL,
  538. "Dial", duplicate_returnexten, ast_free_ptr, BASE_REGISTRAR)) {
  539. ast_free(duplicate_returnexten);
  540. ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n",
  541. dial_string_flat, PARK_DIAL_CONTEXT, returnexten);
  542. }
  543. if (ast_unlock_context(park_dial_context)) {
  544. ast_assert(0);
  545. }
  546. abandon_extension_creation:
  547. /* async_goto the proper PBX destination - this should happen when we come out of the bridge */
  548. if (!ast_strlen_zero(user->comeback)) {
  549. ast_async_parseable_goto(chan, user->comeback);
  550. } else {
  551. comeback_goto(user, user->lot);
  552. }
  553. return -1;
  554. }
  555. void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload)
  556. {
  557. unsigned int numeric_value;
  558. unsigned int hangup_after;
  559. if (sscanf(payload, "%u %u", &hangup_after, &numeric_value) != 2) {
  560. /* If say_parking_space is called with a non-numeric string, we have a problem. */
  561. ast_assert(0);
  562. ast_bridge_channel_leave_bridge(bridge_channel,
  563. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  564. return;
  565. }
  566. ast_say_digits(bridge_channel->chan, numeric_value, "",
  567. ast_channel_language(bridge_channel->chan));
  568. if (hangup_after) {
  569. ast_bridge_channel_leave_bridge(bridge_channel,
  570. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  571. }
  572. }
  573. void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user)
  574. {
  575. unsigned int time_limit;
  576. time_limit = user->time_limit * 1000;
  577. if (!time_limit) {
  578. /* There is no duration limit that we need to apply. */
  579. return;
  580. }
  581. /* If the time limit has already been passed, set a really low time limit so we can kick them out immediately. */
  582. time_limit = ast_remaining_ms(user->start, time_limit);
  583. if (time_limit <= 0) {
  584. time_limit = 1;
  585. }
  586. /* The interval hook is going to need a reference to the parked_user */
  587. ao2_ref(user, +1);
  588. if (ast_bridge_interval_hook(features, 0, time_limit,
  589. parking_duration_callback, user, __ao2_cleanup, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  590. ast_log(LOG_ERROR, "Failed to apply duration limit to the parked call.\n");
  591. ao2_ref(user, -1);
  592. }
  593. }
  594. struct ast_parking_bridge_feature_fn_table parking_provider = {
  595. .module_version = PARKING_MODULE_VERSION,
  596. .module_name = __FILE__,
  597. .parking_is_exten_park = parking_is_exten_park,
  598. .parking_blind_transfer_park = parking_blind_transfer_park,
  599. .parking_park_bridge_channel = parking_park_bridge_channel,
  600. .parking_park_call = parking_park_call,
  601. };
  602. void unload_parking_bridge_features(void)
  603. {
  604. ast_bridge_features_unregister(AST_BRIDGE_BUILTIN_PARKCALL);
  605. ast_parking_unregister_bridge_features(parking_provider.module_name);
  606. }
  607. int load_parking_bridge_features(void)
  608. {
  609. parking_provider.module_info = parking_get_module_info();
  610. if (ast_parking_register_bridge_features(&parking_provider)) {
  611. return -1;
  612. }
  613. if (ast_bridge_features_register(AST_BRIDGE_BUILTIN_PARKCALL, feature_park_call, NULL)) {
  614. return -1;
  615. }
  616. return 0;
  617. }