func_groupcount.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, 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. * \ingroup functions
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. #include "asterisk/module.h"
  27. #include "asterisk/channel.h"
  28. #include "asterisk/pbx.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/app.h"
  31. /*** DOCUMENTATION
  32. <function name="GROUP_COUNT" language="en_US">
  33. <synopsis>
  34. Counts the number of channels in the specified group.
  35. </synopsis>
  36. <syntax argsep="@">
  37. <parameter name="groupname">
  38. <para>Group name.</para>
  39. </parameter>
  40. <parameter name="category">
  41. <para>Category name</para>
  42. </parameter>
  43. </syntax>
  44. <description>
  45. <para>Calculates the group count for the specified group, or uses the
  46. channel's current group if not specified (and non-empty).</para>
  47. </description>
  48. </function>
  49. <function name="GROUP_MATCH_COUNT" language="en_US">
  50. <synopsis>
  51. Counts the number of channels in the groups matching the specified pattern.
  52. </synopsis>
  53. <syntax argsep="@">
  54. <parameter name="groupmatch" required="true">
  55. <para>A standard regular expression used to match a group name.</para>
  56. </parameter>
  57. <parameter name="category">
  58. <para>A standard regular expression used to match a category name.</para>
  59. </parameter>
  60. </syntax>
  61. <description>
  62. <para>Calculates the group count for all groups that match the specified pattern.
  63. Note: category matching is applied after matching based on group.
  64. Uses standard regular expression matching on both (see regex(7)).</para>
  65. </description>
  66. </function>
  67. <function name="GROUP" language="en_US">
  68. <synopsis>
  69. Gets or sets the channel group.
  70. </synopsis>
  71. <syntax>
  72. <parameter name="category">
  73. <para>Category name.</para>
  74. </parameter>
  75. </syntax>
  76. <description>
  77. <para><replaceable>category</replaceable> can be employed for more fine grained group management. Each channel
  78. can only be member of exactly one group per <replaceable>category</replaceable>.</para>
  79. </description>
  80. </function>
  81. <function name="GROUP_LIST" language="en_US">
  82. <synopsis>
  83. Gets a list of the groups set on a channel.
  84. </synopsis>
  85. <syntax />
  86. <description>
  87. <para>Gets a list of the groups set on a channel.</para>
  88. </description>
  89. </function>
  90. ***/
  91. static int group_count_function_read(struct ast_channel *chan, const char *cmd,
  92. char *data, char *buf, size_t len)
  93. {
  94. int ret = -1;
  95. int count = -1;
  96. char group[80] = "", category[80] = "";
  97. if (!chan) {
  98. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  99. return -1;
  100. }
  101. ast_app_group_split_group(data, group, sizeof(group), category,
  102. sizeof(category));
  103. /* If no group has been provided let's find one */
  104. if (ast_strlen_zero(group)) {
  105. struct ast_group_info *gi = NULL;
  106. ast_app_group_list_rdlock();
  107. for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
  108. if (gi->chan != chan)
  109. continue;
  110. if (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))
  111. break;
  112. }
  113. if (gi) {
  114. ast_copy_string(group, gi->group, sizeof(group));
  115. if (!ast_strlen_zero(gi->category))
  116. ast_copy_string(category, gi->category, sizeof(category));
  117. }
  118. ast_app_group_list_unlock();
  119. }
  120. if ((count = ast_app_group_get_count(group, category)) == -1) {
  121. ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", ast_channel_name(chan));
  122. } else {
  123. snprintf(buf, len, "%d", count);
  124. ret = 0;
  125. }
  126. return ret;
  127. }
  128. static struct ast_custom_function group_count_function = {
  129. .name = "GROUP_COUNT",
  130. .read = group_count_function_read,
  131. .read_max = 12,
  132. };
  133. static int group_match_count_function_read(struct ast_channel *chan,
  134. const char *cmd, char *data, char *buf,
  135. size_t len)
  136. {
  137. char group[80] = "";
  138. char category[80] = "";
  139. ast_app_group_split_group(data, group, sizeof(group), category,
  140. sizeof(category));
  141. if (!ast_strlen_zero(group)) {
  142. int count;
  143. count = ast_app_group_match_get_count(group, category);
  144. snprintf(buf, len, "%d", count);
  145. return 0;
  146. }
  147. return -1;
  148. }
  149. static struct ast_custom_function group_match_count_function = {
  150. .name = "GROUP_MATCH_COUNT",
  151. .read = group_match_count_function_read,
  152. .read_max = 12,
  153. .write = NULL,
  154. };
  155. static int group_function_read(struct ast_channel *chan, const char *cmd,
  156. char *data, char *buf, size_t len)
  157. {
  158. int ret = -1;
  159. struct ast_group_info *gi = NULL;
  160. if (!chan) {
  161. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  162. return -1;
  163. }
  164. ast_app_group_list_rdlock();
  165. for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
  166. if (gi->chan != chan)
  167. continue;
  168. if (ast_strlen_zero(data))
  169. break;
  170. if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
  171. break;
  172. }
  173. if (gi) {
  174. ast_copy_string(buf, gi->group, len);
  175. ret = 0;
  176. }
  177. ast_app_group_list_unlock();
  178. return ret;
  179. }
  180. static int group_function_write(struct ast_channel *chan, const char *cmd,
  181. char *data, const char *value)
  182. {
  183. char grpcat[256];
  184. if (!chan) {
  185. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  186. return -1;
  187. }
  188. if (!value) {
  189. return -1;
  190. }
  191. if (!ast_strlen_zero(data)) {
  192. snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
  193. } else {
  194. ast_copy_string(grpcat, value, sizeof(grpcat));
  195. }
  196. if (ast_app_group_set_channel(chan, grpcat))
  197. ast_log(LOG_WARNING,
  198. "Setting a group requires an argument (group name)\n");
  199. return 0;
  200. }
  201. static struct ast_custom_function group_function = {
  202. .name = "GROUP",
  203. .read = group_function_read,
  204. .write = group_function_write,
  205. };
  206. static int group_list_function_read(struct ast_channel *chan, const char *cmd,
  207. char *data, char *buf, size_t len)
  208. {
  209. struct ast_group_info *gi = NULL;
  210. char tmp1[1024] = "";
  211. char tmp2[1024] = "";
  212. if (!chan)
  213. return -1;
  214. ast_app_group_list_rdlock();
  215. for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
  216. if (gi->chan != chan)
  217. continue;
  218. if (!ast_strlen_zero(tmp1)) {
  219. ast_copy_string(tmp2, tmp1, sizeof(tmp2));
  220. if (!ast_strlen_zero(gi->category))
  221. snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
  222. else
  223. snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
  224. } else {
  225. if (!ast_strlen_zero(gi->category))
  226. snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
  227. else
  228. snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
  229. }
  230. }
  231. ast_app_group_list_unlock();
  232. ast_copy_string(buf, tmp1, len);
  233. return 0;
  234. }
  235. static struct ast_custom_function group_list_function = {
  236. .name = "GROUP_LIST",
  237. .read = group_list_function_read,
  238. .write = NULL,
  239. };
  240. static int unload_module(void)
  241. {
  242. int res = 0;
  243. res |= ast_custom_function_unregister(&group_count_function);
  244. res |= ast_custom_function_unregister(&group_match_count_function);
  245. res |= ast_custom_function_unregister(&group_list_function);
  246. res |= ast_custom_function_unregister(&group_function);
  247. return res;
  248. }
  249. static int load_module(void)
  250. {
  251. int res = 0;
  252. res |= ast_custom_function_register(&group_count_function);
  253. res |= ast_custom_function_register(&group_match_count_function);
  254. res |= ast_custom_function_register(&group_list_function);
  255. res |= ast_custom_function_register(&group_function);
  256. return res;
  257. }
  258. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");