func_sorcery.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Fairview 5 Engineering, LLC
  5. *
  6. * George Joseph <george.joseph@fairview5.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 a field from a sorcery object
  21. *
  22. * \author \verbatim George Joseph <george.joseph@fairview5.com> \endverbatim
  23. *
  24. * \ingroup functions
  25. *
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "")
  32. #include "asterisk/app.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/sorcery.h"
  36. /*** DOCUMENTATION
  37. <function name="AST_SORCERY" language="en_US">
  38. <synopsis>
  39. Get a field from a sorcery object
  40. </synopsis>
  41. <syntax>
  42. <parameter name="module_name" required="true">
  43. <para>The name of the module owning the sorcery instance.</para>
  44. </parameter>
  45. <parameter name="object_type" required="true">
  46. <para>The type of object to query.</para>
  47. </parameter>
  48. <parameter name="object_id" required="true">
  49. <para>The id of the object to query.</para>
  50. </parameter>
  51. <parameter name="field_name" required="true">
  52. <para>The name of the field.</para>
  53. </parameter>
  54. <parameter name="retrieval_method" required="false">
  55. <para>Fields that have multiple occurrences may be retrieved in two ways.</para>
  56. <enumlist>
  57. <enum name="concat"><para>Returns all matching fields concatenated
  58. in a single string separated by <replaceable>separator</replaceable>
  59. which defaults to <literal>,</literal>.</para></enum>
  60. <enum name="single"><para>Returns the nth occurrence of the field
  61. as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
  62. </para></enum>
  63. </enumlist>
  64. <para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
  65. </parameter>
  66. <parameter name="retrieval_details" required="false">
  67. <para>Specifies either the separator for <literal>concat</literal>
  68. or the occurrence number for <literal>single</literal>.</para>
  69. </parameter>
  70. </syntax>
  71. </function>
  72. ***/
  73. static int sorcery_function_read(struct ast_channel *chan,
  74. const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  75. {
  76. char *parsed_data = ast_strdupa(data);
  77. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  78. RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
  79. struct ast_variable *change_set;
  80. struct ast_variable *it_change_set;
  81. int found, field_number = 1, ix, method;
  82. char *separator = ",";
  83. enum methods {
  84. CONCAT,
  85. SINGLE,
  86. };
  87. AST_DECLARE_APP_ARGS(args,
  88. AST_APP_ARG(module_name);
  89. AST_APP_ARG(object_type);
  90. AST_APP_ARG(object_id);
  91. AST_APP_ARG(field_name);
  92. AST_APP_ARG(method);
  93. AST_APP_ARG(method_arg);
  94. );
  95. /* Check for zero arguments */
  96. if (ast_strlen_zero(parsed_data)) {
  97. ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
  98. return -1;
  99. }
  100. AST_STANDARD_APP_ARGS(args, parsed_data);
  101. if (ast_strlen_zero(args.module_name)) {
  102. ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
  103. return -1;
  104. }
  105. if (ast_strlen_zero(args.object_type)) {
  106. ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
  107. return -1;
  108. }
  109. if (ast_strlen_zero(args.object_id)) {
  110. ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
  111. return -1;
  112. }
  113. if (ast_strlen_zero(args.field_name)) {
  114. ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
  115. return -1;
  116. }
  117. if (ast_strlen_zero(args.method)) {
  118. method = CONCAT;
  119. } else {
  120. if (strcmp(args.method, "concat") == 0) {
  121. method = CONCAT;
  122. if (ast_strlen_zero(args.method_arg)) {
  123. separator = ",";
  124. } else {
  125. separator = args.method_arg;
  126. }
  127. } else if (strcmp(args.method, "single") == 0) {
  128. method = SINGLE;
  129. if (!ast_strlen_zero(args.method_arg)) {
  130. if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
  131. ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
  132. return -1;
  133. }
  134. }
  135. } else {
  136. ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
  137. return -1;
  138. }
  139. }
  140. sorcery = ast_sorcery_retrieve_by_module_name(args.module_name);
  141. if (!sorcery) {
  142. ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
  143. return -1;
  144. }
  145. sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
  146. if (!sorcery_obj) {
  147. return -1;
  148. }
  149. change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
  150. if (!change_set) {
  151. return -1;
  152. }
  153. ix=1;
  154. found = 0;
  155. for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
  156. if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
  157. ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
  158. found = 1;
  159. continue;
  160. }
  161. if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0 && ix++ == field_number) {
  162. ast_str_set(buf, len, "%s", it_change_set->value);
  163. found = 1;
  164. break;
  165. }
  166. }
  167. ast_variables_destroy(change_set);
  168. if (!found) {
  169. return -1;
  170. }
  171. if (method == CONCAT) {
  172. ast_str_truncate(*buf, -1);
  173. }
  174. return 0;
  175. }
  176. static struct ast_custom_function sorcery_function = {
  177. .name = "AST_SORCERY",
  178. .read2 = sorcery_function_read,
  179. };
  180. static int unload_module(void)
  181. {
  182. return ast_custom_function_unregister(&sorcery_function);
  183. }
  184. static int load_module(void)
  185. {
  186. return ast_custom_function_register(&sorcery_function);
  187. }
  188. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");