parking_bridge_features.c 24 KB

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