res_pjsip_one_touch_record_info.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, malleable, llc.
  5. *
  6. * Sean Bright <sean@malleable.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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <depend>res_pjsip_session</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_ua.h>
  27. #include "asterisk/features.h"
  28. #include "asterisk/res_pjsip.h"
  29. #include "asterisk/res_pjsip_session.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/features_config.h"
  32. static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
  33. {
  34. pjsip_tx_data *tdata;
  35. if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
  36. struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  37. pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
  38. }
  39. }
  40. static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  41. {
  42. static const pj_str_t rec_str = { "Record", 6 };
  43. pjsip_generic_string_hdr *record;
  44. int feature_res;
  45. char feature_code[AST_FEATURE_MAX_LEN];
  46. const char *feature;
  47. char *digit;
  48. record = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &rec_str, NULL);
  49. /* If we don't have Record header, we have nothing to do */
  50. if (!record) {
  51. return 0;
  52. }
  53. if (!pj_stricmp2(&record->hvalue, "on")) {
  54. feature = session->endpoint->info.recording.onfeature;
  55. } else if (!pj_stricmp2(&record->hvalue, "off")) {
  56. feature = session->endpoint->info.recording.offfeature;
  57. } else {
  58. /* Don't send response because another module may handle this */
  59. return 0;
  60. }
  61. if (!session->channel) {
  62. send_response(session, 481, rdata);
  63. return 0;
  64. }
  65. /* Is this endpoint configured with One Touch Recording? */
  66. if (!session->endpoint->info.recording.enabled || ast_strlen_zero(feature)) {
  67. send_response(session, 403, rdata);
  68. return 0;
  69. }
  70. ast_channel_lock(session->channel);
  71. feature_res = ast_get_feature(session->channel, feature, feature_code, sizeof(feature_code));
  72. ast_channel_unlock(session->channel);
  73. if (feature_res || ast_strlen_zero(feature_code)) {
  74. send_response(session, 403, rdata);
  75. return 0;
  76. }
  77. for (digit = feature_code; *digit; ++digit) {
  78. struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = *digit, .len = 100 };
  79. ast_queue_frame(session->channel, &f);
  80. }
  81. send_response(session, 200, rdata);
  82. return 0;
  83. }
  84. static struct ast_sip_session_supplement info_supplement = {
  85. .method = "INFO",
  86. .incoming_request = handle_incoming_request,
  87. };
  88. static int load_module(void)
  89. {
  90. CHECK_PJSIP_SESSION_MODULE_LOADED();
  91. if (ast_sip_session_register_supplement(&info_supplement)) {
  92. ast_log(LOG_ERROR, "Unable to register One Touch Recording supplement\n");
  93. return AST_MODULE_LOAD_FAILURE;
  94. }
  95. return AST_MODULE_LOAD_SUCCESS;
  96. }
  97. static int unload_module(void)
  98. {
  99. ast_sip_session_unregister_supplement(&info_supplement);
  100. return 0;
  101. }
  102. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP INFO One Touch Recording Support",
  103. .support_level = AST_MODULE_SUPPORT_CORE,
  104. .load = load_module,
  105. .unload = unload_module,
  106. .load_pri = AST_MODPRI_APP_DEPEND,
  107. );