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
)
43 char group
[80] = "", category
[80] = "";
45 ast_app_group_split_group(data
, group
, sizeof(group
), category
,
48 /* If no group has been provided let's find one */
49 if (ast_strlen_zero(group
)) {
50 struct ast_group_info
*gi
= NULL
;
52 ast_app_group_list_lock();
53 for (gi
= ast_app_group_list_head(); gi
; gi
= AST_LIST_NEXT(gi
, list
)) {
56 if (ast_strlen_zero(category
) || (!ast_strlen_zero(gi
->category
) && !strcasecmp(gi
->category
, category
)))
60 ast_copy_string(group
, gi
->group
, sizeof(group
));
61 if (!ast_strlen_zero(gi
->category
))
62 ast_copy_string(category
, gi
->category
, sizeof(category
));
64 ast_app_group_list_unlock();
67 if ((count
= ast_app_group_get_count(group
, category
)) == -1)
68 ast_log(LOG_NOTICE
, "No group could be found for channel '%s'\n", chan
->name
);
70 snprintf(buf
, len
, "%d", count
);
75 static struct ast_custom_function group_count_function
= {
76 .name
= "GROUP_COUNT",
77 .syntax
= "GROUP_COUNT([groupname][@category])",
78 .synopsis
= "Counts the number of channels in the specified group",
80 "Calculates the group count for the specified group, or uses the\n"
81 "channel's current group if not specifed (and non-empty).\n",
82 .read
= group_count_function_read
,
85 static int group_match_count_function_read(struct ast_channel
*chan
,
86 char *cmd
, char *data
, char *buf
,
91 char category
[80] = "";
93 ast_app_group_split_group(data
, group
, sizeof(group
), category
,
96 if (!ast_strlen_zero(group
)) {
97 count
= ast_app_group_match_get_count(group
, category
);
98 snprintf(buf
, len
, "%d", count
);
104 static struct ast_custom_function group_match_count_function
= {
105 .name
= "GROUP_MATCH_COUNT",
106 .syntax
= "GROUP_MATCH_COUNT(groupmatch[@category])",
108 "Counts the number of channels in the groups matching the specified pattern",
110 "Calculates the group count for all groups that match the specified pattern.\n"
111 "Uses standard regular expression matching (see regex(7)).\n",
112 .read
= group_match_count_function_read
,
116 static int group_function_read(struct ast_channel
*chan
, char *cmd
,
117 char *data
, char *buf
, size_t len
)
119 struct ast_group_info
*gi
= NULL
;
121 ast_app_group_list_lock();
123 for (gi
= ast_app_group_list_head(); gi
; gi
= AST_LIST_NEXT(gi
, list
)) {
124 if (gi
->chan
!= chan
)
126 if (ast_strlen_zero(data
))
128 if (!ast_strlen_zero(gi
->category
) && !strcasecmp(gi
->category
, data
))
133 ast_copy_string(buf
, gi
->group
, len
);
135 ast_app_group_list_unlock();
140 static int group_function_write(struct ast_channel
*chan
, char *cmd
,
141 char *data
, const char *value
)
145 if (!ast_strlen_zero(data
)) {
146 snprintf(grpcat
, sizeof(grpcat
), "%s@%s", value
, data
);
148 ast_copy_string(grpcat
, value
, sizeof(grpcat
));
151 if (ast_app_group_set_channel(chan
, grpcat
))
153 "Setting a group requires an argument (group name)\n");
158 static struct ast_custom_function group_function
= {
160 .syntax
= "GROUP([category])",
161 .synopsis
= "Gets or sets the channel group.",
162 .desc
= "Gets or sets the channel group.\n",
163 .read
= group_function_read
,
164 .write
= group_function_write
,
167 static int group_list_function_read(struct ast_channel
*chan
, char *cmd
,
168 char *data
, char *buf
, size_t len
)
170 struct ast_group_info
*gi
= NULL
;
171 char tmp1
[1024] = "";
172 char tmp2
[1024] = "";
177 ast_app_group_list_lock();
179 for (gi
= ast_app_group_list_head(); gi
; gi
= AST_LIST_NEXT(gi
, list
)) {
180 if (gi
->chan
!= chan
)
182 if (!ast_strlen_zero(tmp1
)) {
183 ast_copy_string(tmp2
, tmp1
, sizeof(tmp2
));
184 if (!ast_strlen_zero(gi
->category
))
185 snprintf(tmp1
, sizeof(tmp1
), "%s %s@%s", tmp2
, gi
->group
, gi
->category
);
187 snprintf(tmp1
, sizeof(tmp1
), "%s %s", tmp2
, gi
->group
);
189 if (!ast_strlen_zero(gi
->category
))
190 snprintf(tmp1
, sizeof(tmp1
), "%s@%s", gi
->group
, gi
->category
);
192 snprintf(tmp1
, sizeof(tmp1
), "%s", gi
->group
);
196 ast_app_group_list_unlock();
198 ast_copy_string(buf
, tmp1
, len
);
203 static struct ast_custom_function group_list_function
= {
204 .name
= "GROUP_LIST",
205 .syntax
= "GROUP_LIST()",
206 .synopsis
= "Gets a list of the groups set on a channel.",
207 .desc
= "Gets a list of the groups set on a channel.\n",
208 .read
= group_list_function_read
,
212 static int unload_module(void)
216 res
|= ast_custom_function_unregister(&group_count_function
);
217 res
|= ast_custom_function_unregister(&group_match_count_function
);
218 res
|= ast_custom_function_unregister(&group_list_function
);
219 res
|= ast_custom_function_unregister(&group_function
);
224 static int load_module(void)
228 res
|= ast_custom_function_register(&group_count_function
);
229 res
|= ast_custom_function_register(&group_match_count_function
);
230 res
|= ast_custom_function_register(&group_list_function
);
231 res
|= ast_custom_function_register(&group_function
);
236 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Channel group dialplan functions");