more file version tags
[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.h"
17
18 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
19
20 #include "asterisk/channel.h"
21 #include "asterisk/pbx.h"
22 #include "asterisk/logger.h"
23 #include "asterisk/utils.h"
24 #include "asterisk/app.h"
25
26 static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
27 {
28         int count;
29         char group[80] = "";
30         char category[80] = "";
31         char *grp;
32
33         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
34
35         if (ast_strlen_zero(group)) {
36                 grp = pbx_builtin_getvar_helper(chan, category);
37                 ast_copy_string(group, grp, sizeof(group));
38         }
39
40         count = ast_app_group_get_count(group, category);
41         snprintf(buf, len, "%d", count);
42
43         return buf;
44 }
45
46 #ifndef BUILTIN_FUNC
47 static
48 #endif
49 struct ast_custom_function group_count_function = {
50         .name = "GROUP_COUNT",
51         .syntax = "GROUP_COUNT([groupname][@category])",
52         .synopsis = "Counts the number of channels in the specified group",
53         .desc = "Calculates the group count for the specified group, or uses the\n"
54         "channel's current group if not specifed (and non-empty).\n",
55         .read = group_count_function_read,
56 };
57
58 static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
59 {
60         int count;
61         char group[80] = "";
62         char category[80] = "";
63
64         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
65
66         if (!ast_strlen_zero(group)) {
67                 count = ast_app_group_match_get_count(group, category);
68                 snprintf(buf, len, "%d", count);
69         }
70
71         return buf;
72 }
73
74 #ifndef BUILTIN_FUNC
75 static
76 #endif
77 struct ast_custom_function group_match_count_function = {
78         .name = "GROUP_MATCH_COUNT",
79         .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
80         .synopsis = "Counts the number of channels in the groups matching the specified pattern",
81         .desc = "Calculates the group count for all groups that match the specified pattern.\n"
82         "Uses standard regular expression matching (see regex(7)).\n",
83         .read = group_match_count_function_read,
84         .write = NULL,
85 };
86
87 static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
88 {
89         char varname[256];
90         char *group;
91
92         if (data && !ast_strlen_zero(data)) {
93                 snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, data);
94         } else {
95                 ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
96         }
97
98         group = pbx_builtin_getvar_helper(chan, varname);
99         if (group)
100                 ast_copy_string(buf, group, len);
101
102         return buf;
103 }
104
105 static void group_function_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
106 {
107         char grpcat[256];
108
109         if (data && !ast_strlen_zero(data)) {
110                 snprintf(grpcat, sizeof(grpcat), "%s@%s", data, value);
111         } else {
112                 ast_copy_string(grpcat, value, sizeof(grpcat));
113         }
114
115         if (ast_app_group_set_channel(chan, grpcat))
116                 ast_log(LOG_WARNING, "Setting a group requires an argument (group name)\n");
117 }
118
119 #ifndef BUILTIN_FUNC
120 static
121 #endif
122 struct ast_custom_function group_function = {
123         .name = "GROUP",
124         .syntax = "GROUP([category])",
125         .synopsis = "Gets or sets the channel group.",
126         .desc = "Gets or sets the channel group.\n",
127         .read = group_function_read,
128         .write = group_function_write,
129 };
130
131 static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
132 {
133         struct ast_var_t *current;
134         struct varshead *headp;
135         char tmp1[1024] = "";
136         char tmp2[1024] = "";
137
138         headp=&chan->varshead;
139         AST_LIST_TRAVERSE(headp,current,entries) {
140                 if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
141                         if (!ast_strlen_zero(tmp1)) {
142                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
143                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
144                         } else {
145                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
146                         }
147                 } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
148                         if (!ast_strlen_zero(tmp1)) {
149                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
150                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, ast_var_value(current));
151                         } else {
152                                 snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
153                         }
154                 }
155         }
156         ast_copy_string(buf, tmp1, len);
157         return buf;
158 }
159
160 #ifndef BUILTIN_FUNC
161 static
162 #endif
163 struct ast_custom_function group_list_function = {
164         .name = "GROUP_LIST",
165         .syntax = "GROUP_LIST()",
166         .synopsis = "Gets a list of the groups set on a channel.",
167         .desc = "Gets a list of the groups set on a channel.\n",
168         .read = group_list_function_read,
169         .write = NULL,
170 };
171
172 /*
173 Local Variables:
174 mode: C
175 c-file-style: "linux"
176 indent-tabs-mode: nil
177 End:
178 */