res_pjsip_mwi_body_generator.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/res_pjsip.h"
  29. #include "asterisk/res_pjsip_pubsub.h"
  30. #include "asterisk/res_pjsip_body_generator_types.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/strings.h"
  33. #define MWI_TYPE "application"
  34. #define MWI_SUBTYPE "simple-message-summary"
  35. static void *mwi_allocate_body(void *data)
  36. {
  37. struct ast_str **mwi_str;
  38. mwi_str = ast_malloc(sizeof(*mwi_str));
  39. if (!mwi_str) {
  40. return NULL;
  41. }
  42. *mwi_str = ast_str_create(64);
  43. if (!*mwi_str) {
  44. ast_free(mwi_str);
  45. return NULL;
  46. }
  47. return mwi_str;
  48. }
  49. static int mwi_generate_body_content(void *body, void *data)
  50. {
  51. struct ast_str **mwi = body;
  52. struct ast_sip_message_accumulator *counter = data;
  53. ast_str_append(mwi, 0, "Messages-Waiting: %s\r\n",
  54. counter->new_msgs ? "yes" : "no");
  55. ast_str_append(mwi, 0, "Voice-Message: %d/%d (0/0)\r\n",
  56. counter->new_msgs, counter->old_msgs);
  57. return 0;
  58. }
  59. static void mwi_to_string(void *body, struct ast_str **str)
  60. {
  61. struct ast_str **mwi = body;
  62. ast_str_set(str, 0, "%s", ast_str_buffer(*mwi));
  63. }
  64. static void mwi_destroy_body(void *body)
  65. {
  66. struct ast_str **mwi = body;
  67. ast_free(*mwi);
  68. ast_free(mwi);
  69. }
  70. static struct ast_sip_pubsub_body_generator mwi_generator = {
  71. .type = MWI_TYPE,
  72. .subtype = MWI_SUBTYPE,
  73. .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
  74. .allocate_body = mwi_allocate_body,
  75. .generate_body_content = mwi_generate_body_content,
  76. .to_string = mwi_to_string,
  77. .destroy_body = mwi_destroy_body,
  78. };
  79. static int load_module(void)
  80. {
  81. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  82. if (ast_sip_pubsub_register_body_generator(&mwi_generator)) {
  83. return AST_MODULE_LOAD_DECLINE;
  84. }
  85. return AST_MODULE_LOAD_SUCCESS;
  86. }
  87. static int unload_module(void)
  88. {
  89. ast_sip_pubsub_unregister_body_generator(&mwi_generator);
  90. return 0;
  91. }
  92. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
  93. .support_level = AST_MODULE_SUPPORT_CORE,
  94. .load = load_module,
  95. .unload = unload_module,
  96. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  97. );