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.
19 * \brief Channel group related dialplan functions
25 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
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
)
44 char category
[80] = "";
47 ast_app_group_split_group(data
, group
, sizeof(group
), category
,
50 if (ast_strlen_zero(group
)) {
51 if ((grp
= pbx_builtin_getvar_helper(chan
, category
)))
52 ast_copy_string(group
, grp
, sizeof(group
));
54 ast_log(LOG_NOTICE
, "No group could be found for channel '%s'\n",
58 count
= ast_app_group_get_count(group
, category
);
59 snprintf(buf
, len
, "%d", count
);
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",
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
,
80 char category
[80] = "";
82 ast_app_group_split_group(data
, group
, sizeof(group
), category
,
85 if (!ast_strlen_zero(group
)) {
86 count
= ast_app_group_match_get_count(group
, category
);
87 snprintf(buf
, len
, "%d", count
);
93 static struct ast_custom_function group_match_count_function
= {
94 .name
= "GROUP_MATCH_COUNT",
95 .syntax
= "GROUP_MATCH_COUNT(groupmatch[@category])",
97 "Counts the number of channels in the groups matching the specified pattern",
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
,
105 static int group_function_read(struct ast_channel
*chan
, char *cmd
,
106 char *data
, char *buf
, size_t len
)
111 if (!ast_strlen_zero(data
)) {
112 snprintf(varname
, sizeof(varname
), "%s_%s", GROUP_CATEGORY_PREFIX
,
115 ast_copy_string(varname
, GROUP_CATEGORY_PREFIX
, sizeof(varname
));
118 group
= pbx_builtin_getvar_helper(chan
, varname
);
120 ast_copy_string(buf
, group
, len
);
125 static int group_function_write(struct ast_channel
*chan
, char *cmd
,
126 char *data
, const char *value
)
130 if (!ast_strlen_zero(data
)) {
131 snprintf(grpcat
, sizeof(grpcat
), "%s@%s", value
, data
);
133 ast_copy_string(grpcat
, value
, sizeof(grpcat
));
136 if (ast_app_group_set_channel(chan
, grpcat
))
138 "Setting a group requires an argument (group name)\n");
143 static struct ast_custom_function group_function
= {
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));
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
));
180 snprintf(tmp1
, sizeof(tmp1
), "%s", ast_var_value(current
));
184 ast_copy_string(buf
, tmp1
, len
);
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
,
198 static int unload_module(void)
202 res
|= ast_custom_function_unregister(&group_count_function
);
203 res
|= ast_custom_function_unregister(&group_match_count_function
);
204 res
|= ast_custom_function_unregister(&group_list_function
);
205 res
|= ast_custom_function_unregister(&group_function
);
210 static int load_module(void)
214 res
|= ast_custom_function_register(&group_count_function
);
215 res
|= ast_custom_function_register(&group_match_count_function
);
216 res
|= ast_custom_function_register(&group_list_function
);
217 res
|= ast_custom_function_register(&group_function
);
222 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Channel group dialplan functions");