PageRenderTime 35ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/callweaver-1.2.1/apps/app_groupcount.c

https://github.com/claymodel/voip-foip
C | 325 lines | 238 code | 65 blank | 22 comment | 30 complexity | 0f734e15345c61405dc0898752cdadf0 MD5 | raw file
  1. /*
  2. * CallWeaver -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.callweaver.org for more information about
  9. * the CallWeaver 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 Group Manipulation Applications
  21. *
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "confdefs.h"
  25. #endif
  26. #include <stdio.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <regex.h>
  33. #include "callweaver.h"
  34. CALLWEAVER_FILE_VERSION("$HeadURL: http://svn.callweaver.org/callweaver/tags/rel/1.2.1/apps/app_groupcount.c $", "$Revision: 5374 $")
  35. #include "callweaver/file.h"
  36. #include "callweaver/logger.h"
  37. #include "callweaver/options.h"
  38. #include "callweaver/channel.h"
  39. #include "callweaver/pbx.h"
  40. #include "callweaver/module.h"
  41. #include "callweaver/utils.h"
  42. #include "callweaver/cli.h"
  43. #include "callweaver/app.h"
  44. STANDARD_LOCAL_USER;
  45. LOCAL_USER_DECL;
  46. static char *tdesc = "Group Management Routines";
  47. static void *group_count_app;
  48. static void *group_set_app;
  49. static void *group_check_app;
  50. static void *group_match_count_app;
  51. static char *group_count_name = "GetGroupCount";
  52. static char *group_set_name = "SetGroup";
  53. static char *group_check_name = "CheckGroup";
  54. static char *group_match_count_name = "GetGroupMatchCount";
  55. static char *group_count_synopsis = "Get the channel count of a group";
  56. static char *group_set_synopsis = "Set the channel's group";
  57. static char *group_check_synopsis = "Check the channel count of a group against a limit";
  58. static char *group_match_count_synopsis = "Get the channel count of all groups that match a pattern";
  59. static char *group_count_syntax = "GetGroupCount([groupname][@category])";
  60. static char *group_set_syntax = "SetGroup(groupname[@category])";
  61. static char *group_check_syntax = "CheckGroup(max[@category])";
  62. static char *group_match_count_syntax = "GetGroupMatchCount(groupmatch[@category])";
  63. static char *group_count_descrip =
  64. "Usage: GetGroupCount([groupname][@category])\n"
  65. " Calculates the group count for the specified group, or uses\n"
  66. "the current channel's group if not specifed (and non-empty).\n"
  67. "Stores result in GROUPCOUNT. Always returns 0.\n"
  68. "This application has been deprecated, please use the function\n"
  69. "GroupCount.\n";
  70. static char *group_set_descrip =
  71. "Usage: SetGroup(groupname[@category])\n"
  72. " Sets the channel group to the specified value. Equivalent to\n"
  73. "Set(GROUP=group). Always returns 0.\n";
  74. static char *group_check_descrip =
  75. "Usage: CheckGroup(max[@category])\n"
  76. " Checks that the current number of total channels in the\n"
  77. "current channel's group does not exceed 'max'. If the number\n"
  78. "does not exceed 'max', set the GROUPSTATUS variable to OK.\n"
  79. "Otherwise set GROUPSTATUS to MAX_EXCEEDED.\n"
  80. "Always return 0\n";
  81. static char *group_match_count_descrip =
  82. "Usage: GetGroupMatchCount(groupmatch[@category])\n"
  83. " Calculates the group count for all groups that match the specified\n"
  84. "pattern. Uses standard regular expression matching (see regex(7)).\n"
  85. "Stores result in GROUPCOUNT. Always returns 0.\n"
  86. "This application has been deprecated, please use the function\n"
  87. "GroupMatchCount.\n";
  88. static int group_count_exec(struct cw_channel *chan, int argc, char **argv)
  89. {
  90. static int deprecation_warning = 0;
  91. char group[80] = "";
  92. char category[80] = "";
  93. char ret[80] = "";
  94. struct localuser *u;
  95. char *grp;
  96. int res = 0;
  97. int count;
  98. if (!deprecation_warning) {
  99. cw_log(LOG_WARNING, "The GetGroupCount application has been deprecated, please use the GROUP_COUNT function.\n");
  100. deprecation_warning = 1;
  101. }
  102. if (argc != 1) {
  103. cw_log(LOG_ERROR, "Syntax: %s\n", group_count_syntax);
  104. return -1;
  105. }
  106. LOCAL_USER_ADD(u);
  107. cw_app_group_split_group(argv[0], group, sizeof(group), category, sizeof(category));
  108. if (cw_strlen_zero(group)) {
  109. grp = pbx_builtin_getvar_helper(chan, category);
  110. strncpy(group, grp, sizeof(group) - 1);
  111. }
  112. count = cw_app_group_get_count(group, category);
  113. snprintf(ret, sizeof(ret), "%d", count);
  114. pbx_builtin_setvar_helper(chan, "GROUPCOUNT", ret);
  115. LOCAL_USER_REMOVE(u);
  116. return res;
  117. }
  118. static int group_match_count_exec(struct cw_channel *chan, int argc, char **argv)
  119. {
  120. static int deprecation_warning = 0;
  121. char group[80] = "";
  122. char category[80] = "";
  123. char ret[80] = "";
  124. struct localuser *u;
  125. int res = 0;
  126. int count;
  127. if (!deprecation_warning) {
  128. cw_log(LOG_WARNING, "The GetGroupMatchCount application has been deprecated, please use the GROUP_MATCH_COUNT function.\n");
  129. deprecation_warning = 1;
  130. }
  131. if (argc != 1) {
  132. cw_log(LOG_ERROR, "Syntax: %s\n", group_match_count_syntax);
  133. return -1;
  134. }
  135. LOCAL_USER_ADD(u);
  136. cw_app_group_split_group(argv[0], group, sizeof(group), category, sizeof(category));
  137. if (!cw_strlen_zero(group)) {
  138. count = cw_app_group_match_get_count(group, category);
  139. snprintf(ret, sizeof(ret), "%d", count);
  140. pbx_builtin_setvar_helper(chan, "GROUPCOUNT", ret);
  141. }
  142. LOCAL_USER_REMOVE(u);
  143. return res;
  144. }
  145. static int group_set_exec(struct cw_channel *chan, int argc, char **argv)
  146. {
  147. static int deprecation_warning = 0;
  148. struct localuser *u;
  149. int res = 0;
  150. if (!deprecation_warning) {
  151. cw_log(LOG_WARNING, "The SetGroup application has been deprecated, please use the GROUP() function.\n");
  152. deprecation_warning = 1;
  153. }
  154. if (argc != 1) {
  155. cw_log(LOG_ERROR, "Syntax: %s\n", group_set_syntax);
  156. return -1;
  157. }
  158. LOCAL_USER_ADD(u);
  159. if (cw_app_group_set_channel(chan, argv[0]))
  160. cw_log(LOG_WARNING, "SetGroup requires an argument (group name)\n");
  161. LOCAL_USER_REMOVE(u);
  162. return res;
  163. }
  164. static int group_check_exec(struct cw_channel *chan, int argc, char **argv)
  165. {
  166. static int deprecation_warning = 0;
  167. char limit[80]="";
  168. char category[80]="";
  169. struct localuser *u;
  170. int res = 0;
  171. int max, count;
  172. if (!deprecation_warning) {
  173. cw_log(LOG_WARNING, "The CheckGroup application has been deprecated, please use a combination of the GotoIf application and the GROUP_COUNT() function.\n");
  174. deprecation_warning = 1;
  175. }
  176. if (argc != 1) {
  177. cw_log(LOG_ERROR, "Syntax: %s\n", group_check_syntax);
  178. return -1;
  179. }
  180. LOCAL_USER_ADD(u);
  181. cw_app_group_split_group(argv[0], limit, sizeof(limit), category, sizeof(category));
  182. if ((sscanf(limit, "%d", &max) == 1) && (max > -1)) {
  183. count = cw_app_group_get_count(pbx_builtin_getvar_helper(chan, category), category);
  184. if (count > max) {
  185. pbx_builtin_setvar_helper(chan, "GROUPSTATUS", "OK");
  186. } else {
  187. pbx_builtin_setvar_helper(chan, "GROUPSTATUS", "MAX_EXCEEDED");
  188. }
  189. } else
  190. cw_log(LOG_WARNING, "CheckGroup requires a positive integer argument (max)\n");
  191. LOCAL_USER_REMOVE(u);
  192. return res;
  193. }
  194. static int group_show_channels(int fd, int argc, char *argv[])
  195. {
  196. #define FORMAT_STRING "%-25s %-20s %-20s\n"
  197. int numchans = 0;
  198. struct cw_group_info *gi = NULL;
  199. regex_t regexbuf;
  200. int havepattern = 0;
  201. if (argc < 3 || argc > 4)
  202. return RESULT_SHOWUSAGE;
  203. if (argc == 4) {
  204. if (regcomp(&regexbuf, argv[3], REG_EXTENDED | REG_NOSUB))
  205. return RESULT_SHOWUSAGE;
  206. havepattern = 1;
  207. }
  208. cw_cli(fd, FORMAT_STRING, "Channel", "Group", "Category");
  209. cw_app_group_list_lock();
  210. gi = cw_app_group_list_head();
  211. while (gi) {
  212. if (!havepattern || !regexec(&regexbuf, gi->group, 0, NULL, 0)) {
  213. cw_cli(fd, FORMAT_STRING, gi->chan->name, gi->group, (cw_strlen_zero(gi->category) ? "(default)" : gi->category));
  214. numchans++;
  215. }
  216. gi = CW_LIST_NEXT(gi, list);
  217. }
  218. cw_app_group_list_unlock();
  219. if (havepattern)
  220. regfree(&regexbuf);
  221. cw_cli(fd, "%d active channel%s\n", numchans, (numchans != 1) ? "s" : "");
  222. return RESULT_SUCCESS;
  223. #undef FORMAT_STRING
  224. }
  225. static char show_channels_usage[] =
  226. "Usage: group show channels [pattern]\n"
  227. " Lists all currently active channels with channel group(s) specified.\n"
  228. " Optional regular expression pattern is matched to group names for each channel.\n";
  229. static struct cw_cli_entry cli_show_channels =
  230. { { "group", "show", "channels", NULL }, group_show_channels, "Show active channels with group(s)", show_channels_usage};
  231. int unload_module(void)
  232. {
  233. int res = 0;
  234. STANDARD_HANGUP_LOCALUSERS;
  235. cw_cli_unregister(&cli_show_channels);
  236. res |= cw_unregister_application(group_count_app);
  237. res |= cw_unregister_application(group_set_app);
  238. res |= cw_unregister_application(group_check_app);
  239. res |= cw_unregister_application(group_match_count_app);
  240. return res;
  241. }
  242. int load_module(void)
  243. {
  244. group_count_app = cw_register_application(group_count_name, group_count_exec, group_count_synopsis, group_count_syntax, group_count_descrip);
  245. group_set_app = cw_register_application(group_set_name, group_set_exec, group_set_synopsis, group_set_syntax, group_set_descrip);
  246. group_check_app = cw_register_application(group_check_name, group_check_exec, group_check_synopsis, group_check_syntax, group_check_descrip);
  247. group_match_count_app = cw_register_application(group_match_count_name, group_match_count_exec, group_match_count_syntax, group_match_count_synopsis, group_match_count_descrip);
  248. cw_cli_register(&cli_show_channels);
  249. return 0;
  250. }
  251. char *description(void)
  252. {
  253. return tdesc;
  254. }
  255. int usecount(void)
  256. {
  257. int res;
  258. STANDARD_USECOUNT(res);
  259. return res;
  260. }