func_pjsip_aor.c 4.9 KB

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