app_groupcount.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Group Manipulation Applications
  5. *
  6. * Copyright (c) 2004 Digium
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <asterisk/file.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/options.h>
  20. #include <asterisk/channel.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/module.h>
  23. #include <asterisk/utils.h>
  24. static char *tdesc = "Group Management Routines";
  25. static char *app_group_count = "GetGroupCount";
  26. static char *app_group_set = "SetGroup";
  27. static char *app_group_check = "CheckGroup";
  28. static char *group_count_synopsis = "GetGroupCount([groupname][@category])";
  29. static char *group_set_synopsis = "SetGroup(groupname[@category])";
  30. static char *group_check_synopsis = "CheckGroup(max[@category])";
  31. static char *group_count_descrip =
  32. "GetGroupCount([group][@category])\n"
  33. " Calculates the group count for the specified group, or uses\n"
  34. "the current channel's group if not specifed (and non-empty).\n"
  35. "Stores result in GROUPCOUNT. Always returns 0.\n";
  36. static char *group_set_descrip =
  37. "SetGroup(group)\n"
  38. " Sets the channel group to the specified value. Equivalent to\n"
  39. "SetVar(GROUP=group). Always returns 0.\n";
  40. static char *group_check_descrip =
  41. "CheckGroup(max)\n"
  42. " Checks that the current number of total channels in the\n"
  43. "current channel's group does not exceed 'max'. If the number\n"
  44. "does not exceed 'max', we continue to the next step. If the\n"
  45. "number does in fact exceed max, if priority n+101 exists, then\n"
  46. "execution continues at that step, otherwise -1 is returned.\n";
  47. STANDARD_LOCAL_USER;
  48. LOCAL_USER_DECL;
  49. #define DEFAULT_CATEGORY "GROUP"
  50. static int group_get_count(char *group, char *category)
  51. {
  52. struct ast_channel *chan;
  53. int count = 0;
  54. char *test;
  55. if (group && !ast_strlen_zero(group)) {
  56. chan = ast_channel_walk_locked(NULL);
  57. while(chan) {
  58. test = pbx_builtin_getvar_helper(chan, category);
  59. if (test && !strcasecmp(test, group))
  60. count++;
  61. ast_mutex_unlock(&chan->lock);
  62. chan = ast_channel_walk_locked(chan);
  63. }
  64. }
  65. return count;
  66. }
  67. static int group_count_exec(struct ast_channel *chan, void *data)
  68. {
  69. int res=0;
  70. int count;
  71. struct localuser *u;
  72. char *group=NULL;
  73. char *cat = NULL;
  74. char ret[80]="";
  75. char tmp[256]="";
  76. LOCAL_USER_ADD(u);
  77. /* Check and parse arguments */
  78. if (data && !ast_strlen_zero(data)) {
  79. strncpy(tmp, data, sizeof(tmp) - 1);
  80. group = tmp;
  81. cat = strchr(tmp, '@');
  82. if (cat) {
  83. *cat = '\0';
  84. cat++;
  85. }
  86. }
  87. if (cat)
  88. snprintf(ret, sizeof(ret), "GROUP_%s", cat);
  89. else
  90. strncpy(ret, DEFAULT_CATEGORY, sizeof(ret) - 1);
  91. if (!group || ast_strlen_zero(group)) {
  92. group = pbx_builtin_getvar_helper(chan, ret);
  93. }
  94. count = group_get_count(group, ret);
  95. snprintf(ret, sizeof(ret), "%d", count);
  96. pbx_builtin_setvar_helper(chan, "GROUPCOUNT", ret);
  97. LOCAL_USER_REMOVE(u);
  98. return res;
  99. }
  100. static int group_set_exec(struct ast_channel *chan, void *data)
  101. {
  102. int res=0;
  103. struct localuser *u;
  104. char ret[80] = "";
  105. char tmp[256] = "";
  106. char *cat=NULL, *group=NULL;
  107. LOCAL_USER_ADD(u);
  108. /* Check and parse arguments */
  109. if (data && !ast_strlen_zero(data)) {
  110. strncpy(tmp, data, sizeof(tmp) - 1);
  111. group = tmp;
  112. cat = strchr(tmp, '@');
  113. if (cat) {
  114. *cat = '\0';
  115. cat++;
  116. }
  117. }
  118. if (cat)
  119. snprintf(ret, sizeof(ret), "GROUP_%s", cat);
  120. else
  121. strncpy(ret, DEFAULT_CATEGORY, sizeof(ret) - 1);
  122. if (group && !ast_strlen_zero(group)) {
  123. pbx_builtin_setvar_helper(chan, ret, group);
  124. } else
  125. ast_log(LOG_WARNING, "SetGroup requires an argument (group name)\n");
  126. LOCAL_USER_REMOVE(u);
  127. return res;
  128. }
  129. static int group_check_exec(struct ast_channel *chan, void *data)
  130. {
  131. int res=0;
  132. int max, count;
  133. struct localuser *u;
  134. char ret[80] = "";
  135. char tmp[256] = "";
  136. char *cat, *group;
  137. LOCAL_USER_ADD(u);
  138. if (data && !ast_strlen_zero(data)) {
  139. strncpy(tmp, data, sizeof(tmp) - 1);
  140. group = tmp;
  141. cat = strchr(tmp, '@');
  142. if (cat) {
  143. *cat = '\0';
  144. cat++;
  145. }
  146. if ((sscanf((char *)tmp, "%i", &max) == 1) && (max > -1)) {
  147. if (cat)
  148. snprintf(ret, sizeof(ret), "GROUP_%s", cat);
  149. else
  150. strncpy(ret, DEFAULT_CATEGORY, sizeof(ret) - 1);
  151. count = group_get_count(pbx_builtin_getvar_helper(chan, ret), ret);
  152. if (count > max) {
  153. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
  154. chan->priority += 100;
  155. else
  156. res = -1;
  157. }
  158. } else
  159. ast_log(LOG_WARNING, "CheckGroup requires a positive integer argument (max)\n");
  160. } else
  161. ast_log(LOG_WARNING, "CheckGroup requires an argument(max)\n");
  162. LOCAL_USER_REMOVE(u);
  163. return res;
  164. }
  165. int unload_module(void)
  166. {
  167. int res;
  168. STANDARD_HANGUP_LOCALUSERS;
  169. res = ast_unregister_application(app_group_count);
  170. res |= ast_unregister_application(app_group_set);
  171. res |= ast_unregister_application(app_group_check);
  172. return res;
  173. }
  174. int load_module(void)
  175. {
  176. int res;
  177. res = ast_register_application(app_group_count, group_count_exec, group_count_synopsis, group_count_descrip);
  178. res |= ast_register_application(app_group_set, group_set_exec, group_set_synopsis, group_set_descrip);
  179. res |= ast_register_application(app_group_check, group_check_exec, group_check_synopsis, group_check_descrip);
  180. return res;
  181. }
  182. char *description(void)
  183. {
  184. return tdesc;
  185. }
  186. int usecount(void)
  187. {
  188. int res;
  189. STANDARD_USECOUNT(res);
  190. return res;
  191. }
  192. char *key()
  193. {
  194. return ASTERISK_GPL_KEY;
  195. }