add GROUP_COUNT and GROU_MATCH_COUNT functions, deprecate GroupCount and GroupMatchCo...
[asterisk/asterisk.git] / apps / app_groupcount.c
index 067fae5..1531f90 100755 (executable)
@@ -17,6 +17,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <regex.h>
+
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/options.h"
@@ -31,6 +32,70 @@ STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
+static int deprecation_warning = 0;
+
+static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
+{
+       int count;
+       struct localuser *u;
+       char group[80] = "";
+       char category[80] = "";
+       char *grp;
+
+       LOCAL_USER_ADD(u);
+
+       ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
+
+       if (ast_strlen_zero(group)) {
+               grp = pbx_builtin_getvar_helper(chan, category);
+               strncpy(group, grp, sizeof(group) - 1);
+       }
+
+       count = ast_app_group_get_count(group, category);
+       snprintf(buf, len, "%d", count);
+
+       LOCAL_USER_REMOVE(u);
+
+       return buf;
+}
+
+static struct ast_custom_function_obj group_count_function_obj = {
+       .name = "GROUP_COUNT",
+       .desc = "Calculates the group count for the specified group, or uses the current channel's group if not specifed (and non-empty).",
+       .syntax = "GROUP_COUNT([groupname][@category])",
+       .read = group_count_function_read,
+       .write = NULL,
+};
+
+static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
+{
+       int count;
+       struct localuser *u;
+       char group[80] = "";
+       char category[80] = "";
+
+       LOCAL_USER_ADD(u);
+
+       ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
+
+       if (!ast_strlen_zero(group)) {
+               count = ast_app_group_match_get_count(group, category);
+               snprintf(buf, len, "%d", count);
+       }
+
+       LOCAL_USER_REMOVE(u);
+
+       return buf;
+}
+
+static struct ast_custom_function_obj group_match_count_function_obj = {
+       .name = "GROUP_MATCH_COUNT",
+       .desc = "Calculates the group count for all groups that match the specified pattern. Uses standard regular expression matching (see regex(7)).",
+       .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
+       .read = group_match_count_function_read,
+       .write = NULL,
+};
+
 static int group_count_exec(struct ast_channel *chan, void *data)
 {
        int res = 0;
@@ -43,6 +108,11 @@ static int group_count_exec(struct ast_channel *chan, void *data)
 
        LOCAL_USER_ADD(u);
 
+       if (!deprecation_warning) {
+               ast_log(LOG_WARNING, "The GetGroupCount and GetGroupMatchCount applications have been deprecated, please use the GROUP_COUNT and GROUP_MATCH_COUNT functions.\n");
+               deprecation_warning = 1;
+       }
+
        ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
 
        if (ast_strlen_zero(group)) {
@@ -70,6 +140,11 @@ static int group_match_count_exec(struct ast_channel *chan, void *data)
 
        LOCAL_USER_ADD(u);
 
+       if (!deprecation_warning) {
+               ast_log(LOG_WARNING, "The GetGroupCount and GetGroupMatchCount applications have been deprecated, please use the GROUP_COUNT and GROUP_MATCH_COUNT functions.\n");
+               deprecation_warning = 1;
+       }
+
        ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
 
        if (!ast_strlen_zero(group)) {
@@ -114,7 +189,7 @@ static int group_check_exec(struct ast_channel *chan, void *data)
 
        ast_app_group_split_group(data, limit, sizeof(limit), category, sizeof(category));
 
-       if ((sscanf(limit, "%i", &max) == 1) && (max > -1)) {
+       if ((sscanf(limit, "%d", &max) == 1) && (max > -1)) {
                count = ast_app_group_get_count(pbx_builtin_getvar_helper(chan, category), category);
                if (count > max) {
                        if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
@@ -186,24 +261,26 @@ static char *app_group_set = "SetGroup";
 static char *app_group_check = "CheckGroup";
 static char *app_group_match_count = "GetGroupMatchCount";
 
-static char *group_count_synopsis = "GetGroupCount([groupname][@category])";
-static char *group_set_synopsis = "SetGroup(groupname[@category])";
-static char *group_check_synopsis = "CheckGroup(max[@category])";
-static char *group_match_count_synopsis = "GetGroupMatchCount(groupmatch[@category])";
+static char *group_count_synopsis = "Get the channel count of a group";
+static char *group_set_synopsis = "Set the channel's group";
+static char *group_check_synopsis = "Check the channel count of a group against a limit";
+static char *group_match_count_synopsis = "Get the channel count of all groups that match a pattern";
 
 static char *group_count_descrip =
-"GetGroupCount([group][@category])\n"
+"Usage: GetGroupCount([groupname][@category])\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";
+"Stores result in GROUPCOUNT.  Always returns 0.\n"
+"This application has been deprecated, please use the function\n"
+"GroupCount.\n";
 
 static char *group_set_descrip =
-"SetGroup(group)\n"
+"Usage: SetGroup(groupname[@category])\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[@category])\n"
+"Usage: CheckGroup(max[@category])\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"
@@ -211,10 +288,12 @@ static char *group_check_descrip =
 "execution continues at that step, otherwise -1 is returned.\n";
 
 static char *group_match_count_descrip =
-"GetGroupMatchCount(groupmatch[@category])\n"
+"Usage: GetGroupMatchCount(groupmatch[@category])\n"
 "  Calculates the group count for all groups that match the specified\n"
 "pattern. Uses standard regular expression matching (see regex(7)).\n"
-"Stores result in GROUPCOUNT.  Always returns 0.\n";
+"Stores result in GROUPCOUNT.  Always returns 0.\n"
+"This application has been deprecated, please use the function\n"
+"GroupMatchCount.\n";
 
 static char show_channels_usage[] = 
 "Usage: group show channels [pattern]\n"
@@ -232,6 +311,8 @@ int unload_module(void)
        res |= ast_unregister_application(app_group_set);
        res |= ast_unregister_application(app_group_check);
        res |= ast_unregister_application(app_group_match_count);
+       res |= ast_custom_function_unregister(&group_count_function_obj);
+       res |= ast_custom_function_unregister(&group_match_count_function_obj);
        return res;
 }
 
@@ -242,6 +323,8 @@ int load_module(void)
        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);
        res |= ast_register_application(app_group_match_count, group_match_count_exec, group_match_count_synopsis, group_match_count_descrip);
+       res |= ast_custom_function_register(&group_count_function_obj);
+       res |= ast_custom_function_register(&group_match_count_function_obj);
        ast_cli_register(&cli_show_channels);
        return res;
 }