res_pjsip_send_to_voicemail.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 Module for managing send to voicemail requests in SIP
  21. * REFER messages against PJSIP channels
  22. *
  23. * \author Jonathan Rose <jrose@digium.com>
  24. */
  25. /*** MODULEINFO
  26. <depend>pjproject</depend>
  27. <depend>res_pjsip</depend>
  28. <depend>res_pjsip_session</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. #include <pjsip.h>
  33. #include <pjsip_ua.h>
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/res_pjsip.h"
  36. #include "asterisk/res_pjsip_session.h"
  37. #include "asterisk/module.h"
  38. #define DATASTORE_NAME "call_feature_send_to_vm_datastore"
  39. #define SEND_TO_VM_HEADER "PJSIP_HEADER(add,X-Digium-Call-Feature)"
  40. #define SEND_TO_VM_HEADER_VALUE "feature_send_to_vm"
  41. #define SEND_TO_VM_REDIRECT "REDIRECTING(reason)"
  42. #define SEND_TO_VM_REDIRECT_VALUE "send_to_vm"
  43. #define SEND_TO_VM_REDIRECT_QUOTED_VALUE "\"" SEND_TO_VM_REDIRECT_VALUE "\""
  44. static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
  45. {
  46. pjsip_tx_data *tdata;
  47. if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
  48. struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  49. pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
  50. }
  51. }
  52. static void channel_cleanup_wrapper(void *data)
  53. {
  54. struct ast_channel *chan = data;
  55. ast_channel_cleanup(chan);
  56. }
  57. static struct ast_datastore_info call_feature_info = {
  58. .type = "REFER call feature info",
  59. .destroy = channel_cleanup_wrapper,
  60. };
  61. static pjsip_param *get_diversion_reason(pjsip_fromto_hdr *hdr)
  62. {
  63. static const pj_str_t reason_str = { "reason", 6 };
  64. return pjsip_param_find(&hdr->other_param, &reason_str);
  65. }
  66. static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
  67. {
  68. static const pj_str_t from_str = { "From", 4 };
  69. static const pj_str_t diversion_str = { "Diversion", 9 };
  70. pjsip_generic_string_hdr *hdr;
  71. pj_str_t value;
  72. if (!(hdr = pjsip_msg_find_hdr_by_name(
  73. rdata->msg_info.msg, &diversion_str, NULL))) {
  74. return NULL;
  75. }
  76. pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
  77. /* parse as a fromto header */
  78. return pjsip_parse_hdr(rdata->tp_info.pool, &from_str, value.ptr,
  79. pj_strlen(&value), NULL);
  80. }
  81. static int has_diversion_reason(pjsip_rx_data *rdata)
  82. {
  83. pjsip_param *reason;
  84. pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
  85. if (!hdr) {
  86. return 0;
  87. }
  88. reason = get_diversion_reason(hdr);
  89. return reason
  90. && (!pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_QUOTED_VALUE)
  91. || !pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_VALUE));
  92. }
  93. static int has_call_feature(pjsip_rx_data *rdata)
  94. {
  95. static const pj_str_t call_feature_str = { "X-Digium-Call-Feature", 21 };
  96. pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(
  97. rdata->msg_info.msg, &call_feature_str, NULL);
  98. return hdr && !pj_stricmp2(&hdr->hvalue, SEND_TO_VM_HEADER_VALUE);
  99. }
  100. static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  101. {
  102. struct ast_datastore *sip_session_datastore;
  103. struct ast_channel *other_party;
  104. int has_feature;
  105. int has_reason;
  106. if (!session->channel) {
  107. return 0;
  108. }
  109. has_feature = has_call_feature(rdata);
  110. has_reason = has_diversion_reason(rdata);
  111. if (!has_feature && !has_reason) {
  112. /* If we don't have a call feature or diversion reason or if
  113. it's not a feature this module is related to then there
  114. is nothing to do. */
  115. return 0;
  116. }
  117. /* Check bridge status... */
  118. other_party = ast_channel_bridge_peer(session->channel);
  119. if (!other_party) {
  120. /* The channel wasn't in a two party bridge */
  121. ast_log(LOG_WARNING, "%s (%s) attempted to transfer to voicemail, "
  122. "but was not in a two party bridge.\n",
  123. ast_sorcery_object_get_id(session->endpoint),
  124. ast_channel_name(session->channel));
  125. send_response(session, 400, rdata);
  126. return -1;
  127. }
  128. sip_session_datastore = ast_sip_session_alloc_datastore(
  129. &call_feature_info, DATASTORE_NAME);
  130. if (!sip_session_datastore) {
  131. ast_channel_unref(other_party);
  132. send_response(session, 500, rdata);
  133. return -1;
  134. }
  135. sip_session_datastore->data = other_party;
  136. if (ast_sip_session_add_datastore(session, sip_session_datastore)) {
  137. ao2_ref(sip_session_datastore, -1);
  138. send_response(session, 500, rdata);
  139. return -1;
  140. }
  141. if (has_feature) {
  142. pbx_builtin_setvar_helper(other_party, SEND_TO_VM_HEADER,
  143. SEND_TO_VM_HEADER_VALUE);
  144. }
  145. if (has_reason) {
  146. pbx_builtin_setvar_helper(other_party, SEND_TO_VM_REDIRECT,
  147. SEND_TO_VM_REDIRECT_VALUE);
  148. }
  149. ao2_ref(sip_session_datastore, -1);
  150. return 0;
  151. }
  152. static void handle_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  153. {
  154. pjsip_status_line status = tdata->msg->line.status;
  155. struct ast_datastore *feature_datastore =
  156. ast_sip_session_get_datastore(session, DATASTORE_NAME);
  157. struct ast_channel *target_chan;
  158. if (!feature_datastore) {
  159. return;
  160. }
  161. /* Since we are handling the response, there is no need to keep the datastore in the session anymore. */
  162. ast_sip_session_remove_datastore(session, DATASTORE_NAME);
  163. /* If the response >= 300, the refer failed and we need to clear the feature. */
  164. if (status.code >= 300) {
  165. target_chan = feature_datastore->data;
  166. pbx_builtin_setvar_helper(target_chan, SEND_TO_VM_HEADER, NULL);
  167. pbx_builtin_setvar_helper(target_chan, SEND_TO_VM_REDIRECT, NULL);
  168. }
  169. ao2_ref(feature_datastore, -1);
  170. }
  171. static struct ast_sip_session_supplement refer_supplement = {
  172. .method = "REFER",
  173. .incoming_request = handle_incoming_request,
  174. .outgoing_response = handle_outgoing_response,
  175. };
  176. static int load_module(void)
  177. {
  178. CHECK_PJSIP_SESSION_MODULE_LOADED();
  179. if (ast_sip_session_register_supplement(&refer_supplement)) {
  180. ast_log(LOG_ERROR, "Unable to register Send to Voicemail supplement\n");
  181. return AST_MODULE_LOAD_DECLINE;
  182. }
  183. return AST_MODULE_LOAD_SUCCESS;
  184. }
  185. static int unload_module(void)
  186. {
  187. ast_sip_session_unregister_supplement(&refer_supplement);
  188. return 0;
  189. }
  190. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP REFER Send to Voicemail Support",
  191. .support_level = AST_MODULE_SUPPORT_CORE,
  192. .load = load_module,
  193. .unload = unload_module,
  194. .load_pri = AST_MODPRI_APP_DEPEND,
  195. );