res_pjsip_pidf_digium_body_supplement.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Kevin Harwell <kharwell@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/presencestate.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 int pidf_supplement_body(void *body, void *data)
  35. {
  36. struct ast_sip_exten_state_data *state_data = data;
  37. pj_xml_node *node;
  38. char sanitized[256];
  39. if (ast_strlen_zero(state_data->user_agent) ||
  40. !strstr(state_data->user_agent, "digium")) {
  41. /* not a digium phone */
  42. return 0;
  43. }
  44. if (!(node = ast_sip_presence_xml_create_node(
  45. state_data->pool, body, "tuple"))) {
  46. ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
  47. return -1;
  48. }
  49. ast_sip_presence_xml_create_attr(
  50. state_data->pool, node, "id", "digium-presence");
  51. if (!(node = ast_sip_presence_xml_create_node(
  52. state_data->pool, node, "status"))) {
  53. ast_log(LOG_WARNING, "Unable to create PIDF tuple status\n");
  54. return -1;
  55. }
  56. if (!(node = ast_sip_presence_xml_create_node(
  57. state_data->pool, node, "digium_presence"))) {
  58. ast_log(LOG_WARNING, "Unable to create digium presence\n");
  59. return -1;
  60. }
  61. if (!ast_strlen_zero(state_data->presence_message)) {
  62. ast_sip_sanitize_xml(state_data->presence_message, sanitized, sizeof(sanitized));
  63. pj_strdup2(state_data->pool, &node->content, sanitized);
  64. }
  65. ast_sip_presence_xml_create_attr(
  66. state_data->pool, node, "type", ast_presence_state2str(
  67. state_data->presence_state));
  68. if (!ast_strlen_zero(state_data->presence_subtype)) {
  69. ast_sip_sanitize_xml(state_data->presence_subtype, sanitized, sizeof(sanitized));
  70. ast_sip_presence_xml_create_attr(
  71. state_data->pool, node, "subtype", sanitized);
  72. }
  73. return 0;
  74. }
  75. static struct ast_sip_pubsub_body_supplement pidf_supplement = {
  76. .type = "application",
  77. .subtype = "pidf+xml",
  78. .supplement_body = pidf_supplement_body,
  79. };
  80. static int load_module(void)
  81. {
  82. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  83. if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
  84. return AST_MODULE_LOAD_DECLINE;
  85. }
  86. return AST_MODULE_LOAD_SUCCESS;
  87. }
  88. static int unload_module(void)
  89. {
  90. ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
  91. return 0;
  92. }
  93. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Digium presence supplement",
  94. .support_level = AST_MODULE_SUPPORT_CORE,
  95. .load = load_module,
  96. .unload = unload_module,
  97. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  98. );