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
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28 #include "asterisk/module.h"
29 #include "asterisk/channel.h"
30 #include "asterisk/pbx.h"
31 #include "asterisk/utils.h"
32 #include "asterisk/app.h"
34 static int group_count_function_read(struct ast_channel *chan, const char *cmd,
35 char *data, char *buf, size_t len)
38 char group[80] = "", category[80] = "";
40 ast_app_group_split_group(data, group, sizeof(group), category,
43 /* If no group has been provided let's find one */
44 if (ast_strlen_zero(group)) {
45 struct ast_group_info *gi = NULL;
47 ast_app_group_list_rdlock();
48 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
51 if (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))
55 ast_copy_string(group, gi->group, sizeof(group));
56 if (!ast_strlen_zero(gi->category))
57 ast_copy_string(category, gi->category, sizeof(category));
59 ast_app_group_list_unlock();
62 if ((count = ast_app_group_get_count(group, category)) == -1)
63 ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
65 snprintf(buf, len, "%d", count);
70 static struct ast_custom_function group_count_function = {
71 .name = "GROUP_COUNT",
72 .syntax = "GROUP_COUNT([groupname][@category])",
73 .synopsis = "Counts the number of channels in the specified group",
75 "Calculates the group count for the specified group, or uses the\n"
76 "channel's current group if not specifed (and non-empty).\n",
77 .read = group_count_function_read,
80 static int group_match_count_function_read(struct ast_channel *chan,
81 const char *cmd, char *data, char *buf,
86 char category[80] = "";
88 ast_app_group_split_group(data, group, sizeof(group), category,
91 if (!ast_strlen_zero(group)) {
92 count = ast_app_group_match_get_count(group, category);
93 snprintf(buf, len, "%d", count);
99 static struct ast_custom_function group_match_count_function = {
100 .name = "GROUP_MATCH_COUNT",
101 .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
103 "Counts the number of channels in the groups matching the specified pattern",
105 "Calculates the group count for all groups that match the specified pattern.\n"
106 "Uses standard regular expression matching (see regex(7)).\n",
107 .read = group_match_count_function_read,
111 static int group_function_read(struct ast_channel *chan, const char *cmd,
112 char *data, char *buf, size_t len)
114 struct ast_group_info *gi = NULL;
116 ast_app_group_list_rdlock();
118 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
119 if (gi->chan != chan)
121 if (ast_strlen_zero(data))
123 if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
128 ast_copy_string(buf, gi->group, len);
130 ast_app_group_list_unlock();
135 static int group_function_write(struct ast_channel *chan, const char *cmd,
136 char *data, const char *value)
140 if (!ast_strlen_zero(data)) {
141 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
143 ast_copy_string(grpcat, value, sizeof(grpcat));
146 if (ast_app_group_set_channel(chan, grpcat))
148 "Setting a group requires an argument (group name)\n");
153 static struct ast_custom_function group_function = {
155 .syntax = "GROUP([category])",
156 .synopsis = "Gets or sets the channel group.",
157 .desc = "Gets or sets the channel group.\n",
158 .read = group_function_read,
159 .write = group_function_write,
162 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
163 char *data, char *buf, size_t len)
165 struct ast_group_info *gi = NULL;
166 char tmp1[1024] = "";
167 char tmp2[1024] = "";
172 ast_app_group_list_rdlock();
174 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
175 if (gi->chan != chan)
177 if (!ast_strlen_zero(tmp1)) {
178 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
179 if (!ast_strlen_zero(gi->category))
180 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
182 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
184 if (!ast_strlen_zero(gi->category))
185 snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
187 snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
191 ast_app_group_list_unlock();
193 ast_copy_string(buf, tmp1, len);
198 static struct ast_custom_function group_list_function = {
199 .name = "GROUP_LIST",
200 .syntax = "GROUP_LIST()",
201 .synopsis = "Gets a list of the groups set on a channel.",
202 .desc = "Gets a list of the groups set on a channel.\n",
203 .read = group_list_function_read,
207 static int unload_module(void)
211 res |= ast_custom_function_unregister(&group_count_function);
212 res |= ast_custom_function_unregister(&group_match_count_function);
213 res |= ast_custom_function_unregister(&group_list_function);
214 res |= ast_custom_function_unregister(&group_function);
219 static int load_module(void)
223 res |= ast_custom_function_register(&group_count_function);
224 res |= ast_custom_function_register(&group_match_count_function);
225 res |= ast_custom_function_register(&group_list_function);
226 res |= ast_custom_function_register(&group_function);
231 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");