res_pjsip_pidf_body_generator.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <depend>res_pjsip_pubsub</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_simple.h>
  27. #include <pjlib.h>
  28. #include "asterisk/module.h"
  29. #include "asterisk/res_pjsip.h"
  30. #include "asterisk/res_pjsip_pubsub.h"
  31. #include "asterisk/res_pjsip_presence_xml.h"
  32. #include "asterisk/res_pjsip_body_generator_types.h"
  33. static void *pidf_allocate_body(void *data)
  34. {
  35. struct ast_sip_exten_state_data *state_data = data;
  36. char *local = ast_strdupa(state_data->local);
  37. pjpidf_pres *pres;
  38. pj_str_t entity;
  39. pres = pjpidf_create(state_data->pool, pj_cstr(&entity, ast_strip_quoted(local, "<", ">")));
  40. return pres;
  41. }
  42. static int pidf_generate_body_content(void *body, void *data)
  43. {
  44. pjpidf_tuple *tuple;
  45. pj_str_t note, id, contact, priority;
  46. char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
  47. enum ast_sip_pidf_state local_state;
  48. char sanitized[PJSIP_MAX_URL_SIZE];
  49. pjpidf_pres *pres = body;
  50. struct ast_sip_exten_state_data *state_data = data;
  51. ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
  52. &pidfstate, &pidfnote, &local_state, 0);
  53. if (!pjpidf_pres_add_note(state_data->pool, pres, pj_cstr(&note, pidfnote))) {
  54. ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n");
  55. return -1;
  56. }
  57. if (!(tuple = pjpidf_pres_add_tuple(state_data->pool, pres,
  58. pj_cstr(&id, state_data->exten)))) {
  59. ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
  60. return -1;
  61. }
  62. ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
  63. pjpidf_tuple_set_contact(state_data->pool, tuple, pj_cstr(&contact, sanitized));
  64. pjpidf_tuple_set_contact_prio(state_data->pool, tuple, pj_cstr(&priority, "1"));
  65. pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),
  66. local_state == NOTIFY_OPEN || local_state == NOTIFY_INUSE);
  67. return 0;
  68. }
  69. #define MAX_STRING_GROWTHS 5
  70. static void pidf_to_string(void *body, struct ast_str **str)
  71. {
  72. pjpidf_pres *pres = body;
  73. int growths = 0;
  74. int size;
  75. do {
  76. size = pjpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
  77. if (size <= AST_PJSIP_XML_PROLOG_LEN) {
  78. ast_str_make_space(str, ast_str_size(*str) * 2);
  79. ++growths;
  80. }
  81. } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
  82. if (size <= AST_PJSIP_XML_PROLOG_LEN) {
  83. ast_log(LOG_WARNING, "PIDF body text too large\n");
  84. return;
  85. }
  86. *(ast_str_buffer(*str) + size) = '\0';
  87. ast_str_update(*str);
  88. }
  89. static struct ast_sip_pubsub_body_generator pidf_body_generator = {
  90. .type = "application",
  91. .subtype = "pidf+xml",
  92. .body_type = AST_SIP_EXTEN_STATE_DATA,
  93. .allocate_body = pidf_allocate_body,
  94. .generate_body_content = pidf_generate_body_content,
  95. .to_string = pidf_to_string,
  96. /* No need for a destroy_body callback since we use a pool */
  97. };
  98. static int load_module(void)
  99. {
  100. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  101. if (ast_sip_pubsub_register_body_generator(&pidf_body_generator)) {
  102. return AST_MODULE_LOAD_DECLINE;
  103. }
  104. return AST_MODULE_LOAD_SUCCESS;
  105. }
  106. static int unload_module(void)
  107. {
  108. ast_sip_pubsub_unregister_body_generator(&pidf_body_generator);
  109. return 0;
  110. }
  111. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
  112. .support_level = AST_MODULE_SUPPORT_CORE,
  113. .load = load_module,
  114. .unload = unload_module,
  115. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  116. );