func_pjsip_aor.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. /*! \file
  19. *
  20. * \brief Get information about a PJSIP AOR
  21. *
  22. * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
  23. *
  24. * \ingroup functions
  25. *
  26. */
  27. /*** MODULEINFO
  28. <depend>pjproject</depend>
  29. <depend>res_pjsip</depend>
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include <pjsip.h>
  34. #include <pjlib.h>
  35. #include "asterisk/app.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/sorcery.h"
  39. #include "asterisk/res_pjsip.h"
  40. /*** DOCUMENTATION
  41. <function name="PJSIP_AOR" language="en_US">
  42. <synopsis>
  43. Get information about a PJSIP AOR
  44. </synopsis>
  45. <syntax>
  46. <parameter name="name" required="true">
  47. <para>The name of the AOR to query.</para>
  48. </parameter>
  49. <parameter name="field" required="true">
  50. <para>The configuration option for the AOR to query for.
  51. Supported options are those fields on the
  52. <replaceable>aor</replaceable> object in
  53. <filename>pjsip.conf</filename>.</para>
  54. <enumlist>
  55. <configOptionToEnum>
  56. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption)"/>
  57. </configOptionToEnum>
  58. </enumlist>
  59. </parameter>
  60. </syntax>
  61. </function>
  62. ***/
  63. static int pjsip_aor_function_read(struct ast_channel *chan,
  64. const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  65. {
  66. struct ast_sorcery *pjsip_sorcery;
  67. char *parsed_data = ast_strdupa(data);
  68. RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
  69. int res = 0;
  70. AST_DECLARE_APP_ARGS(args,
  71. AST_APP_ARG(aor_name);
  72. AST_APP_ARG(field_name);
  73. );
  74. /* Check for zero arguments */
  75. if (ast_strlen_zero(parsed_data)) {
  76. ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
  77. return -1;
  78. }
  79. AST_STANDARD_APP_ARGS(args, parsed_data);
  80. if (ast_strlen_zero(args.aor_name)) {
  81. ast_log(AST_LOG_ERROR, "Cannot call %s without an AOR name to query\n", cmd);
  82. return -1;
  83. }
  84. if (ast_strlen_zero(args.field_name)) {
  85. ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
  86. return -1;
  87. }
  88. pjsip_sorcery = ast_sip_get_sorcery();
  89. if (!pjsip_sorcery) {
  90. ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
  91. return -1;
  92. }
  93. aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", args.aor_name);
  94. if (!aor_obj) {
  95. ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s'\n", args.aor_name);
  96. return -1;
  97. }
  98. if (!strcmp(args.field_name, "contact")) {
  99. /* The multiple fields handler for contact does not provide a list of contact object names, which is what we want, so we
  100. * handle contact specifically to provide this.
  101. */
  102. RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
  103. struct ao2_iterator i;
  104. struct ast_sip_contact *contact;
  105. int first = 1;
  106. contacts = ast_sip_location_retrieve_aor_contacts(aor_obj);
  107. if (!contacts) {
  108. ast_log(LOG_WARNING, "Failed to retrieve contacts for AOR '%s'\n", args.aor_name);
  109. return -1;
  110. }
  111. i = ao2_iterator_init(contacts, 0);
  112. while ((contact = ao2_iterator_next(&i))) {
  113. if (!first) {
  114. ast_str_append(buf, len, "%s", ",");
  115. }
  116. ast_str_append(buf, len, "%s", ast_sorcery_object_get_id(contact));
  117. first = 0;
  118. ao2_ref(contact, -1);
  119. }
  120. ao2_iterator_destroy(&i);
  121. } else {
  122. struct ast_variable *change_set;
  123. struct ast_variable *it_change_set;
  124. change_set = ast_sorcery_objectset_create(pjsip_sorcery, aor_obj);
  125. if (!change_set) {
  126. ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s': change set is NULL\n", args.aor_name);
  127. return -1;
  128. }
  129. for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
  130. if (!strcmp(it_change_set->name, args.field_name)) {
  131. ast_str_set(buf, len, "%s", it_change_set->value);
  132. break;
  133. }
  134. }
  135. if (!it_change_set) {
  136. ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP AOR\n", args.field_name);
  137. res = 1;
  138. }
  139. ast_variables_destroy(change_set);
  140. }
  141. return res;
  142. }
  143. static struct ast_custom_function pjsip_aor_function = {
  144. .name = "PJSIP_AOR",
  145. .read2 = pjsip_aor_function_read,
  146. };
  147. static int unload_module(void)
  148. {
  149. return ast_custom_function_unregister(&pjsip_aor_function);
  150. }
  151. static int load_module(void)
  152. {
  153. return ast_custom_function_register(&pjsip_aor_function);
  154. }
  155. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP AOR",
  156. .support_level = AST_MODULE_SUPPORT_CORE,
  157. .load = load_module,
  158. .unload = unload_module,
  159. .requires = "res_pjsip",
  160. );