res_pjsip_xpidf_body_generator.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. <depend>res_pjsip_exten_state</depend>
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. #include <pjsip.h>
  27. #include <pjsip_simple.h>
  28. #include <pjlib.h>
  29. #include "asterisk/module.h"
  30. #include "asterisk/res_pjsip.h"
  31. #include "asterisk/res_pjsip_pubsub.h"
  32. #include "asterisk/res_pjsip_presence_xml.h"
  33. #include "asterisk/res_pjsip_body_generator_types.h"
  34. static void *xpidf_allocate_body(void *data)
  35. {
  36. struct ast_sip_exten_state_data *state_data = data;
  37. char *local = ast_strdupa(state_data->local);
  38. pjxpidf_pres *pres;
  39. pj_str_t name;
  40. pres = pjxpidf_create(state_data->pool, pj_cstr(&name, ast_strip_quoted(local, "<", ">")));
  41. return pres;
  42. }
  43. static int xpidf_generate_body_content(void *body, void *data)
  44. {
  45. pjxpidf_pres *pres = body;
  46. struct ast_sip_exten_state_data *state_data = data;
  47. static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
  48. char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
  49. pj_xml_attr *attr;
  50. enum ast_sip_pidf_state local_state;
  51. pj_str_t uri;
  52. char sanitized[PJSIP_MAX_URL_SIZE];
  53. pj_xml_node *atom;
  54. pj_xml_node *address;
  55. pj_xml_node *status;
  56. pj_xml_node *msnsubstatus;
  57. ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
  58. &pidfstate, &pidfnote, &local_state);
  59. ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id",
  60. &atom, &attr);
  61. pj_strdup2(state_data->pool, &attr->value, state_data->exten);
  62. ast_sip_presence_xml_find_node_attr(state_data->pool, atom, "address",
  63. "uri", &address, &attr);
  64. ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
  65. uri.ptr = (char*) pj_pool_alloc(state_data->pool,
  66. strlen(sanitized) + STR_ADDR_PARAM.slen);
  67. pj_strcpy2( &uri, sanitized);
  68. pj_strcat( &uri, &STR_ADDR_PARAM);
  69. pj_strdup(state_data->pool, &attr->value, &uri);
  70. ast_sip_presence_xml_create_attr(state_data->pool, address, "priority", "0.80000");
  71. ast_sip_presence_xml_find_node_attr(state_data->pool, address,
  72. "status", "status", &status, &attr);
  73. pj_strdup2(state_data->pool, &attr->value,
  74. (local_state == NOTIFY_OPEN) ? "open" :
  75. (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
  76. ast_sip_presence_xml_find_node_attr(state_data->pool, address,
  77. "msnsubstatus", "substatus", &msnsubstatus, &attr);
  78. pj_strdup2(state_data->pool, &attr->value,
  79. (local_state == NOTIFY_OPEN) ? "online" :
  80. (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
  81. return 0;
  82. }
  83. #define MAX_STRING_GROWTHS 5
  84. static void xpidf_to_string(void *body, struct ast_str **str)
  85. {
  86. pjxpidf_pres *pres = body;
  87. int growths = 0;
  88. int size;
  89. do {
  90. size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str));
  91. if (size == AST_PJSIP_XML_PROLOG_LEN) {
  92. ast_str_make_space(str, ast_str_size(*str) * 2);
  93. ++growths;
  94. }
  95. } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
  96. if (size == AST_PJSIP_XML_PROLOG_LEN) {
  97. ast_log(LOG_WARNING, "XPIDF body text too large\n");
  98. return;
  99. }
  100. *(ast_str_buffer(*str) + size) = '\0';
  101. ast_str_update(*str);
  102. }
  103. static struct ast_sip_pubsub_body_generator xpidf_body_generator = {
  104. .type = "application",
  105. .subtype = "xpidf+xml",
  106. .body_type = AST_SIP_EXTEN_STATE_DATA,
  107. .allocate_body = xpidf_allocate_body,
  108. .generate_body_content = xpidf_generate_body_content,
  109. .to_string = xpidf_to_string,
  110. /* No need for a destroy_body callback since we use a pool */
  111. };
  112. static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator = {
  113. .type = "application",
  114. .subtype = "cpim-pidf+xml",
  115. .body_type = AST_SIP_EXTEN_STATE_DATA,
  116. .allocate_body = xpidf_allocate_body,
  117. .generate_body_content = xpidf_generate_body_content,
  118. .to_string = xpidf_to_string,
  119. /* No need for a destroy_body callback since we use a pool */
  120. };
  121. static void unregister_all(void)
  122. {
  123. ast_sip_pubsub_unregister_body_generator(&cpim_pidf_body_generator);
  124. ast_sip_pubsub_unregister_body_generator(&xpidf_body_generator);
  125. }
  126. static int load_module(void)
  127. {
  128. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  129. if (ast_sip_pubsub_register_body_generator(&xpidf_body_generator)) {
  130. goto fail;
  131. }
  132. if (ast_sip_pubsub_register_body_generator(&cpim_pidf_body_generator)) {
  133. goto fail;
  134. }
  135. return AST_MODULE_LOAD_SUCCESS;
  136. fail:
  137. unregister_all();
  138. return AST_MODULE_LOAD_DECLINE;
  139. }
  140. static int unload_module(void)
  141. {
  142. unregister_all();
  143. return 0;
  144. }
  145. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
  146. .support_level = AST_MODULE_SUPPORT_CORE,
  147. .load = load_module,
  148. .unload = unload_module,
  149. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  150. );