res_pjsip_mwi_body_generator.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(128);
  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. if (!ast_strlen_zero(counter->message_account)) {
  56. ast_str_append(mwi, 0, "Message-Account: %s\r\n", counter->message_account);
  57. }
  58. ast_str_append(mwi, 0, "Voice-Message: %d/%d (0/0)\r\n",
  59. counter->new_msgs, counter->old_msgs);
  60. return 0;
  61. }
  62. static void mwi_to_string(void *body, struct ast_str **str)
  63. {
  64. struct ast_str **mwi = body;
  65. ast_str_set(str, 0, "%s", ast_str_buffer(*mwi));
  66. }
  67. static void mwi_destroy_body(void *body)
  68. {
  69. struct ast_str **mwi = body;
  70. ast_free(*mwi);
  71. ast_free(mwi);
  72. }
  73. static struct ast_sip_pubsub_body_generator mwi_generator = {
  74. .type = MWI_TYPE,
  75. .subtype = MWI_SUBTYPE,
  76. .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
  77. .allocate_body = mwi_allocate_body,
  78. .generate_body_content = mwi_generate_body_content,
  79. .to_string = mwi_to_string,
  80. .destroy_body = mwi_destroy_body,
  81. };
  82. static int load_module(void)
  83. {
  84. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  85. if (ast_sip_pubsub_register_body_generator(&mwi_generator)) {
  86. return AST_MODULE_LOAD_DECLINE;
  87. }
  88. return AST_MODULE_LOAD_SUCCESS;
  89. }
  90. static int unload_module(void)
  91. {
  92. ast_sip_pubsub_unregister_body_generator(&mwi_generator);
  93. return 0;
  94. }
  95. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
  96. .support_level = AST_MODULE_SUPPORT_CORE,
  97. .load = load_module,
  98. .unload = unload_module,
  99. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  100. );