major re-work of dialplan functions, including:
[asterisk/asterisk.git] / funcs / func_groupcount.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Channel group related dialplan functions
5  * 
6  * Copyright (C) 2005, Digium, Inc.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15
16 #include "asterisk/channel.h"
17 #include "asterisk/pbx.h"
18 #include "asterisk/logger.h"
19 #include "asterisk/utils.h"
20 #include "asterisk/app.h"
21
22 static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
23 {
24         int count;
25         char group[80] = "";
26         char category[80] = "";
27         char *grp;
28
29         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
30
31         if (ast_strlen_zero(group)) {
32                 grp = pbx_builtin_getvar_helper(chan, category);
33                 strncpy(group, grp, sizeof(group) - 1);
34         }
35
36         count = ast_app_group_get_count(group, category);
37         snprintf(buf, len, "%d", count);
38
39         return buf;
40 }
41
42 #ifndef BUILTIN_FUNC
43 static
44 #endif
45 struct ast_custom_function group_count_function = {
46         .name = "GROUP_COUNT",
47         .syntax = "GROUP_COUNT([groupname][@category])",
48         .synopsis = "Counts the number of channels in the specified group",
49         .desc = "Calculates the group count for the specified group, or uses the\n"
50         "channel's current group if not specifed (and non-empty).\n",
51         .read = group_count_function_read,
52 };
53
54 static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
55 {
56         int count;
57         char group[80] = "";
58         char category[80] = "";
59
60         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
61
62         if (!ast_strlen_zero(group)) {
63                 count = ast_app_group_match_get_count(group, category);
64                 snprintf(buf, len, "%d", count);
65         }
66
67         return buf;
68 }
69
70 #ifndef BUILTIN_FUNC
71 static
72 #endif
73 struct ast_custom_function group_match_count_function = {
74         .name = "GROUP_MATCH_COUNT",
75         .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
76         .synopsis = "Counts the number of channels in the groups matching the specified pattern",
77         .desc = "Calculates the group count for all groups that match the specified pattern.\n"
78         "Uses standard regular expression matching (see regex(7)).\n",
79         .read = group_match_count_function_read,
80         .write = NULL,
81 };