Merged revisions 37441-37442 via svnmerge from
[asterisk-bristuff.git] / funcs / func_groupcount.c
blob8988ec73988bba674b5f58d7268cb1d3910b42e2
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
17 /*! \file
19 * \brief Channel group related dialplan functions
23 #include "asterisk.h"
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/app.h"
39 static int group_count_function_read(struct ast_channel *chan, char *cmd,
40 char *data, char *buf, size_t len)
42 int count;
43 char group[80] = "";
44 char category[80] = "";
45 const char *grp;
47 ast_app_group_split_group(data, group, sizeof(group), category,
48 sizeof(category));
50 if (ast_strlen_zero(group)) {
51 if ((grp = pbx_builtin_getvar_helper(chan, category)))
52 ast_copy_string(group, grp, sizeof(group));
53 else
54 ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n",
55 chan->name);
58 count = ast_app_group_get_count(group, category);
59 snprintf(buf, len, "%d", count);
61 return 0;
64 static struct ast_custom_function group_count_function = {
65 .name = "GROUP_COUNT",
66 .syntax = "GROUP_COUNT([groupname][@category])",
67 .synopsis = "Counts the number of channels in the specified group",
68 .desc =
69 "Calculates the group count for the specified group, or uses the\n"
70 "channel's current group if not specifed (and non-empty).\n",
71 .read = group_count_function_read,
74 static int group_match_count_function_read(struct ast_channel *chan,
75 char *cmd, char *data, char *buf,
76 size_t len)
78 int count;
79 char group[80] = "";
80 char category[80] = "";
82 ast_app_group_split_group(data, group, sizeof(group), category,
83 sizeof(category));
85 if (!ast_strlen_zero(group)) {
86 count = ast_app_group_match_get_count(group, category);
87 snprintf(buf, len, "%d", count);
90 return 0;
93 static struct ast_custom_function group_match_count_function = {
94 .name = "GROUP_MATCH_COUNT",
95 .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
96 .synopsis =
97 "Counts the number of channels in the groups matching the specified pattern",
98 .desc =
99 "Calculates the group count for all groups that match the specified pattern.\n"
100 "Uses standard regular expression matching (see regex(7)).\n",
101 .read = group_match_count_function_read,
102 .write = NULL,
105 static int group_function_read(struct ast_channel *chan, char *cmd,
106 char *data, char *buf, size_t len)
108 char varname[256];
109 const char *group;
111 if (!ast_strlen_zero(data)) {
112 snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX,
113 data);
114 } else {
115 ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
118 group = pbx_builtin_getvar_helper(chan, varname);
119 if (group)
120 ast_copy_string(buf, group, len);
122 return 0;
125 static int group_function_write(struct ast_channel *chan, char *cmd,
126 char *data, const char *value)
128 char grpcat[256];
130 if (!ast_strlen_zero(data)) {
131 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
132 } else {
133 ast_copy_string(grpcat, value, sizeof(grpcat));
136 if (ast_app_group_set_channel(chan, grpcat))
137 ast_log(LOG_WARNING,
138 "Setting a group requires an argument (group name)\n");
140 return 0;
143 static struct ast_custom_function group_function = {
144 .name = "GROUP",
145 .syntax = "GROUP([category])",
146 .synopsis = "Gets or sets the channel group.",
147 .desc = "Gets or sets the channel group.\n",
148 .read = group_function_read,
149 .write = group_function_write,
152 static int group_list_function_read(struct ast_channel *chan, char *cmd,
153 char *data, char *buf, size_t len)
155 struct ast_var_t *current;
156 struct varshead *headp;
157 char tmp1[1024] = "";
158 char tmp2[1024] = "";
160 headp = &chan->varshead;
161 AST_LIST_TRAVERSE(headp, current, entries) {
162 if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
163 if (!ast_strlen_zero(tmp1)) {
164 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
165 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2,
166 ast_var_value(current),
167 (ast_var_name(current) +
168 strlen(GROUP_CATEGORY_PREFIX) + 1));
169 } else {
170 snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current),
171 (ast_var_name(current) +
172 strlen(GROUP_CATEGORY_PREFIX) + 1));
174 } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
175 if (!ast_strlen_zero(tmp1)) {
176 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
177 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2,
178 ast_var_value(current));
179 } else {
180 snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
184 ast_copy_string(buf, tmp1, len);
186 return 0;
189 static struct ast_custom_function group_list_function = {
190 .name = "GROUP_LIST",
191 .syntax = "GROUP_LIST()",
192 .synopsis = "Gets a list of the groups set on a channel.",
193 .desc = "Gets a list of the groups set on a channel.\n",
194 .read = group_list_function_read,
195 .write = NULL,
198 static char *tdesc = "Channel group dialplan functions";
200 static int unload_module(void *mod)
202 int res = 0;
204 res |= ast_custom_function_unregister(&group_count_function);
205 res |= ast_custom_function_unregister(&group_match_count_function);
206 res |= ast_custom_function_unregister(&group_list_function);
207 res |= ast_custom_function_unregister(&group_function);
209 return res;
212 static int load_module(void *mod)
214 int res = 0;
216 res |= ast_custom_function_register(&group_count_function);
217 res |= ast_custom_function_register(&group_match_count_function);
218 res |= ast_custom_function_register(&group_list_function);
219 res |= ast_custom_function_register(&group_function);
221 return res;
224 static const char *description(void)
226 return tdesc;
229 static const char *key(void)
231 return ASTERISK_GPL_KEY;
234 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);