res_pjsip_pidf_digium_body_supplement.c 3.3 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[1024];
  39. /* The res_pjsip_exten_state module converts the user agent to lower case */
  40. if (ast_strlen_zero(state_data->user_agent) ||
  41. (!strstr(state_data->user_agent, "digium") &&
  42. !strstr(state_data->user_agent, "sangoma"))) {
  43. /* not a Sangoma phone */
  44. return 0;
  45. }
  46. if (!(node = ast_sip_presence_xml_create_node(
  47. state_data->pool, body, "tuple"))) {
  48. ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
  49. return -1;
  50. }
  51. ast_sip_presence_xml_create_attr(
  52. state_data->pool, node, "id", "digium-presence");
  53. if (!(node = ast_sip_presence_xml_create_node(
  54. state_data->pool, node, "status"))) {
  55. ast_log(LOG_WARNING, "Unable to create PIDF tuple status\n");
  56. return -1;
  57. }
  58. if (!(node = ast_sip_presence_xml_create_node(
  59. state_data->pool, node, "digium_presence"))) {
  60. ast_log(LOG_WARNING, "Unable to create digium presence\n");
  61. return -1;
  62. }
  63. if (!ast_strlen_zero(state_data->presence_message)) {
  64. ast_sip_sanitize_xml(state_data->presence_message, sanitized, sizeof(sanitized));
  65. pj_strdup2(state_data->pool, &node->content, sanitized);
  66. }
  67. ast_sip_presence_xml_create_attr(
  68. state_data->pool, node, "type", ast_presence_state2str(
  69. state_data->presence_state));
  70. if (!ast_strlen_zero(state_data->presence_subtype)) {
  71. ast_sip_sanitize_xml(state_data->presence_subtype, sanitized, sizeof(sanitized));
  72. ast_sip_presence_xml_create_attr(
  73. state_data->pool, node, "subtype", sanitized);
  74. }
  75. return 0;
  76. }
  77. static struct ast_sip_pubsub_body_supplement pidf_supplement = {
  78. .type = "application",
  79. .subtype = "pidf+xml",
  80. .supplement_body = pidf_supplement_body,
  81. };
  82. static int load_module(void)
  83. {
  84. if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
  85. return AST_MODULE_LOAD_DECLINE;
  86. }
  87. return AST_MODULE_LOAD_SUCCESS;
  88. }
  89. static int unload_module(void)
  90. {
  91. ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
  92. return 0;
  93. }
  94. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Sangoma presence supplement",
  95. .support_level = AST_MODULE_SUPPORT_CORE,
  96. .load = load_module,
  97. .unload = unload_module,
  98. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  99. .requires = "res_pjsip,res_pjsip_pubsub",
  100. );