From 67c11acbbf3e2dbf2cc7c06233e9dd73e4ec284b Mon Sep 17 00:00:00 2001 From: Mark Spencer Date: Tue, 4 May 2004 16:09:35 +0000 Subject: [PATCH] Add app_groupcount to manage groups git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2888 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- apps/Makefile | 3 +- apps/app_groupcount.c | 174 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 1 deletion(-) create mode 100755 apps/app_groupcount.c diff --git a/apps/Makefile b/apps/Makefile index 35e58f92fe..4a608c4203 100755 --- a/apps/Makefile +++ b/apps/Makefile @@ -26,7 +26,8 @@ APPS=app_dial.so app_playback.so app_voicemail.so app_directory.so app_mp3.so\ app_enumlookup.so app_transfer.so app_setcidnum.so app_cdr.so \ app_hasnewvoicemail.so app_sayunixtime.so app_cut.so app_read.so \ app_setcdruserfield.so app_random.so app_ices.so app_eval.so \ - app_nbscat.so app_sendtext.so app_exec.so app_sms.so + app_nbscat.so app_sendtext.so app_exec.so app_sms.so \ + app_groupcount.so ifneq (${OSARCH},Darwin) APPS+=app_intercom.so diff --git a/apps/app_groupcount.c b/apps/app_groupcount.c new file mode 100755 index 0000000000..094822f6d5 --- /dev/null +++ b/apps/app_groupcount.c @@ -0,0 +1,174 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * Group Manipulation Applications + * + * Copyright (c) 2004 Digium + * + * Mark Spencer + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static char *tdesc = "Group Management Routines"; + +static char *app_group_count = "GetGroupCount"; +static char *app_group_set = "SetGroup"; +static char *app_group_check = "CheckGroup"; + +static char *group_count_synopsis = "GetGroupCount([groupname])"; +static char *group_set_synopsis = "SetGroup([groupname])"; +static char *group_check_synopsis = "CheckGroup(max)"; + +static char *group_count_descrip = +"GetGroupCount([group])\n" +" Calculates the group count for the specified group, or uses\n" +"the current channel's group if not specifed (and non-empty).\n" +"Stores result in GROUPCOUNT. Always returns 0.\n"; + +static char *group_set_descrip = +"SetGroup(group)\n" +" Sets the channel group to the specified value. Equivalent to\n" +"SetVar(GROUP=group). Always returns 0.\n"; + +static char *group_check_descrip = +"CheckGroup(max)\n" +" Checks that the current number of total channels in the\n" +"current channel's group does not exceed 'max'. If the number\n" +"does not exceed 'max', we continue to the next step. If the\n" +"number does in fact exceed max, if priority n+101 exists, then\n" +"execution continues at that step, otherwise -1 is returned.\n"; + +STANDARD_LOCAL_USER; + +LOCAL_USER_DECL; + +static int group_get_count(char *group) +{ + /* XXX ast_channel_walk needs to be modified to + prevent a race in which after we return the channel + is no longer valid (or ast_channel_free can be modified + just as well) XXX */ + struct ast_channel *chan; + int count = 0; + char *test; + if (group && strlen(group)) { + chan = ast_channel_walk(NULL); + while(chan) { + test = pbx_builtin_getvar_helper(chan, "GROUP"); + if (!strcasecmp(test, group)) + count++; + chan = ast_channel_walk(chan); + } + } + return count; +} + +static int group_count_exec(struct ast_channel *chan, void *data) +{ + int res=0; + int count; + struct localuser *u; + char *group=NULL; + char ret[80]; + + LOCAL_USER_ADD(u); + + /* Check and parse arguments */ + if (data && strlen(data)) { + group = (char *)data; + } else { + group = pbx_builtin_getvar_helper(chan, "GROUP"); + } + count = group_get_count(group); + snprintf(ret, sizeof(ret), "%d", count); + pbx_builtin_setvar_helper(chan, "GROUPCOUNT", ret); + LOCAL_USER_REMOVE(u); + return res; +} + +static int group_set_exec(struct ast_channel *chan, void *data) +{ + int res=0; + struct localuser *u; + + LOCAL_USER_ADD(u); + /* Check and parse arguments */ + if (data && strlen(data)) { + pbx_builtin_setvar_helper(chan, "GROUP", (char *)data); + } else + ast_log(LOG_WARNING, "GroupSet requires an argument (group name)\n"); + + LOCAL_USER_REMOVE(u); + return res; +} + +static int group_check_exec(struct ast_channel *chan, void *data) +{ + int res=0; + int max, count; + struct localuser *u; + + LOCAL_USER_ADD(u); + + if (data && (sscanf((char *)data, "%i", &max) == 1) && (max > -1)) { + count = group_get_count(pbx_builtin_getvar_helper(chan, "GROUP")); + if (count > max) { + if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) + chan->priority += 100; + else + res = -1; + } + } else + ast_log(LOG_WARNING, "GroupCheck requires a positive integer argument (max)\n"); + LOCAL_USER_REMOVE(u); + return res; +} + +int unload_module(void) +{ + int res; + STANDARD_HANGUP_LOCALUSERS; + res = ast_unregister_application(app_group_count); + res |= ast_unregister_application(app_group_set); + res |= ast_unregister_application(app_group_check); + return res; +} + +int load_module(void) +{ + int res; + res = ast_register_application(app_group_count, group_count_exec, group_count_synopsis, group_count_descrip); + res |= ast_register_application(app_group_set, group_set_exec, group_set_synopsis, group_set_descrip); + res |= ast_register_application(app_group_check, group_check_exec, group_check_synopsis, group_check_descrip); + return res; +} + +char *description(void) +{ + return tdesc; +} + +int usecount(void) +{ + int res; + STANDARD_USECOUNT(res); + return res; +} + +char *key() +{ + return ASTERISK_GPL_KEY; +}