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