func_groupcount.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Channel group related dialplan functions
  19. *
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include "asterisk.h"
  26. /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
  27. #include "asterisk/channel.h"
  28. #include "asterisk/pbx.h"
  29. #include "asterisk/logger.h"
  30. #include "asterisk/utils.h"
  31. #include "asterisk/app.h"
  32. static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  33. {
  34. int count;
  35. char group[80] = "";
  36. char category[80] = "";
  37. char *grp;
  38. ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
  39. if (ast_strlen_zero(group)) {
  40. if ((grp = pbx_builtin_getvar_helper(chan, category)))
  41. ast_copy_string(group, grp, sizeof(group));
  42. else
  43. ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
  44. }
  45. count = ast_app_group_get_count(group, category);
  46. snprintf(buf, len, "%d", count);
  47. return buf;
  48. }
  49. #ifndef BUILTIN_FUNC
  50. static
  51. #endif
  52. struct ast_custom_function group_count_function = {
  53. .name = "GROUP_COUNT",
  54. .syntax = "GROUP_COUNT([groupname][@category])",
  55. .synopsis = "Counts the number of channels in the specified group",
  56. .desc = "Calculates the group count for the specified group, or uses the\n"
  57. "channel's current group if not specifed (and non-empty).\n",
  58. .read = group_count_function_read,
  59. };
  60. static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  61. {
  62. int count;
  63. char group[80] = "";
  64. char category[80] = "";
  65. ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
  66. if (!ast_strlen_zero(group)) {
  67. count = ast_app_group_match_get_count(group, category);
  68. snprintf(buf, len, "%d", count);
  69. }
  70. return buf;
  71. }
  72. #ifndef BUILTIN_FUNC
  73. static
  74. #endif
  75. struct ast_custom_function group_match_count_function = {
  76. .name = "GROUP_MATCH_COUNT",
  77. .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
  78. .synopsis = "Counts the number of channels in the groups matching the specified pattern",
  79. .desc = "Calculates the group count for all groups that match the specified pattern.\n"
  80. "Uses standard regular expression matching (see regex(7)).\n",
  81. .read = group_match_count_function_read,
  82. .write = NULL,
  83. };
  84. static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  85. {
  86. char varname[256];
  87. char *group;
  88. if (!ast_strlen_zero(data)) {
  89. snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, data);
  90. } else {
  91. ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
  92. }
  93. group = pbx_builtin_getvar_helper(chan, varname);
  94. if (group)
  95. ast_copy_string(buf, group, len);
  96. return buf;
  97. }
  98. static void group_function_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
  99. {
  100. char grpcat[256];
  101. if (!ast_strlen_zero(data)) {
  102. snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
  103. } else {
  104. ast_copy_string(grpcat, value, sizeof(grpcat));
  105. }
  106. if (ast_app_group_set_channel(chan, grpcat))
  107. ast_log(LOG_WARNING, "Setting a group requires an argument (group name)\n");
  108. }
  109. #ifndef BUILTIN_FUNC
  110. static
  111. #endif
  112. struct ast_custom_function group_function = {
  113. .name = "GROUP",
  114. .syntax = "GROUP([category])",
  115. .synopsis = "Gets or sets the channel group.",
  116. .desc = "Gets or sets the channel group.\n",
  117. .read = group_function_read,
  118. .write = group_function_write,
  119. };
  120. static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  121. {
  122. struct ast_var_t *current;
  123. struct varshead *headp;
  124. char tmp1[1024] = "";
  125. char tmp2[1024] = "";
  126. headp=&chan->varshead;
  127. AST_LIST_TRAVERSE(headp,current,entries) {
  128. if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
  129. if (!ast_strlen_zero(tmp1)) {
  130. ast_copy_string(tmp2, tmp1, sizeof(tmp2));
  131. snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
  132. } else {
  133. snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
  134. }
  135. } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
  136. if (!ast_strlen_zero(tmp1)) {
  137. ast_copy_string(tmp2, tmp1, sizeof(tmp2));
  138. snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, ast_var_value(current));
  139. } else {
  140. snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
  141. }
  142. }
  143. }
  144. ast_copy_string(buf, tmp1, len);
  145. return buf;
  146. }
  147. #ifndef BUILTIN_FUNC
  148. static
  149. #endif
  150. struct ast_custom_function group_list_function = {
  151. .name = "GROUP_LIST",
  152. .syntax = "GROUP_LIST()",
  153. .synopsis = "Gets a list of the groups set on a channel.",
  154. .desc = "Gets a list of the groups set on a channel.\n",
  155. .read = group_list_function_read,
  156. .write = NULL,
  157. };
  158. /*
  159. Local Variables:
  160. mode: C
  161. c-file-style: "linux"
  162. indent-tabs-mode: nil
  163. End:
  164. */