Make evaluation of channel variables consistently case-sensitive.
[asterisk/asterisk.git] / funcs / func_groupcount.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
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.
11  *
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.
15  */
16
17 /*! \file
18  *
19  * \brief Channel group related dialplan functions
20  * 
21  * \ingroup functions
22  */
23
24 /*** MODULEINFO
25         <support_level>core</support_level>
26  ***/
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/app.h"
37
38 /*** DOCUMENTATION
39         <function name="GROUP_COUNT" language="en_US">
40                 <synopsis>
41                         Counts the number of channels in the specified group.
42                 </synopsis>
43                 <syntax argsep="@">
44                         <parameter name="groupname">
45                                 <para>Group name.</para>
46                         </parameter>
47                         <parameter name="category">
48                                 <para>Category name</para>
49                         </parameter>
50                 </syntax>
51                 <description>
52                         <para>Calculates the group count for the specified group, or uses the
53                         channel's current group if not specifed (and non-empty).</para>
54                 </description>
55         </function>
56         <function name="GROUP_MATCH_COUNT" language="en_US">
57                 <synopsis>
58                         Counts the number of channels in the groups matching the specified pattern.
59                 </synopsis>
60                 <syntax argsep="@">
61                         <parameter name="groupmatch" required="true">
62                                 <para>A standard regular expression used to match a group name.</para>
63                         </parameter>
64                         <parameter name="category">
65                                 <para>A standard regular expression used to match a category name.</para>
66                         </parameter>
67                 </syntax>
68                 <description>
69                         <para>Calculates the group count for all groups that match the specified pattern.
70                         Note: category matching is applied after matching based on group.
71                         Uses standard regular expression matching on both (see regex(7)).</para>
72                 </description>
73         </function>
74         <function name="GROUP" language="en_US">
75                 <synopsis>
76                         Gets or sets the channel group.
77                 </synopsis>
78                 <syntax>
79                         <parameter name="category">
80                                 <para>Category name.</para>
81                         </parameter>
82                 </syntax>
83                 <description>
84                         <para><replaceable>category</replaceable> can be employed for more fine grained group management. Each channel 
85                         can only be member of exactly one group per <replaceable>category</replaceable>.</para>
86                 </description>
87         </function>
88         <function name="GROUP_LIST" language="en_US">
89                 <synopsis>
90                         Gets a list of the groups set on a channel.
91                 </synopsis>
92                 <syntax />
93                 <description>
94                         <para>Gets a list of the groups set on a channel.</para>
95                 </description>
96         </function>
97
98  ***/
99
100 static int group_count_function_read(struct ast_channel *chan, const char *cmd,
101                                      char *data, char *buf, size_t len)
102 {
103         int ret = -1;
104         int count = -1;
105         char group[80] = "", category[80] = "";
106
107         ast_app_group_split_group(data, group, sizeof(group), category,
108                                   sizeof(category));
109
110         /* If no group has been provided let's find one */
111         if (ast_strlen_zero(group)) {
112                 struct ast_group_info *gi = NULL;
113
114                 ast_app_group_list_rdlock();
115                 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
116                         if (gi->chan != chan)
117                                 continue;
118                         if (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))
119                                 break;
120                 }
121                 if (gi) {
122                         ast_copy_string(group, gi->group, sizeof(group));
123                         if (!ast_strlen_zero(gi->category))
124                                 ast_copy_string(category, gi->category, sizeof(category));
125                 }
126                 ast_app_group_list_unlock();
127         }
128
129         if ((count = ast_app_group_get_count(group, category)) == -1) {
130                 ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", ast_channel_name(chan));
131         } else {
132                 snprintf(buf, len, "%d", count);
133                 ret = 0;
134         }
135
136         return ret;
137 }
138
139 static struct ast_custom_function group_count_function = {
140         .name = "GROUP_COUNT",
141         .read = group_count_function_read,
142         .read_max = 12,
143 };
144
145 static int group_match_count_function_read(struct ast_channel *chan,
146                                            const char *cmd, char *data, char *buf,
147                                            size_t len)
148 {
149         char group[80] = "";
150         char category[80] = "";
151
152         ast_app_group_split_group(data, group, sizeof(group), category,
153                                   sizeof(category));
154
155         if (!ast_strlen_zero(group)) {
156                 int count;
157                 count = ast_app_group_match_get_count(group, category);
158                 snprintf(buf, len, "%d", count);
159                 return 0;
160         }
161
162         return -1;
163 }
164
165 static struct ast_custom_function group_match_count_function = {
166         .name = "GROUP_MATCH_COUNT",
167         .read = group_match_count_function_read,
168         .read_max = 12,
169         .write = NULL,
170 };
171
172 static int group_function_read(struct ast_channel *chan, const char *cmd,
173                                char *data, char *buf, size_t len)
174 {
175         int ret = -1;
176         struct ast_group_info *gi = NULL;
177         
178         ast_app_group_list_rdlock();
179         
180         for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
181                 if (gi->chan != chan)
182                         continue;
183                 if (ast_strlen_zero(data))
184                         break;
185                 if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
186                         break;
187         }
188         
189         if (gi) {
190                 ast_copy_string(buf, gi->group, len);
191                 ret = 0;
192         }
193         
194         ast_app_group_list_unlock();
195         
196         return ret;
197 }
198
199 static int group_function_write(struct ast_channel *chan, const char *cmd,
200                                 char *data, const char *value)
201 {
202         char grpcat[256];
203
204         if (!value) {
205                 return -1;
206         }
207
208         if (!ast_strlen_zero(data)) {
209                 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
210         } else {
211                 ast_copy_string(grpcat, value, sizeof(grpcat));
212         }
213
214         if (ast_app_group_set_channel(chan, grpcat))
215                 ast_log(LOG_WARNING,
216                                 "Setting a group requires an argument (group name)\n");
217
218         return 0;
219 }
220
221 static struct ast_custom_function group_function = {
222         .name = "GROUP",
223         .read = group_function_read,
224         .write = group_function_write,
225 };
226
227 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
228                                     char *data, char *buf, size_t len)
229 {
230         struct ast_group_info *gi = NULL;
231         char tmp1[1024] = "";
232         char tmp2[1024] = "";
233
234         if (!chan)
235                 return -1;
236
237         ast_app_group_list_rdlock();
238
239         for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
240                 if (gi->chan != chan)
241                         continue;
242                 if (!ast_strlen_zero(tmp1)) {
243                         ast_copy_string(tmp2, tmp1, sizeof(tmp2));
244                         if (!ast_strlen_zero(gi->category))
245                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
246                         else
247                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
248                 } else {
249                         if (!ast_strlen_zero(gi->category))
250                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
251                         else
252                                 snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
253                 }
254         }
255         
256         ast_app_group_list_unlock();
257
258         ast_copy_string(buf, tmp1, len);
259
260         return 0;
261 }
262
263 static struct ast_custom_function group_list_function = {
264         .name = "GROUP_LIST",
265         .read = group_list_function_read,
266         .write = NULL,
267 };
268
269 static int unload_module(void)
270 {
271         int res = 0;
272
273         res |= ast_custom_function_unregister(&group_count_function);
274         res |= ast_custom_function_unregister(&group_match_count_function);
275         res |= ast_custom_function_unregister(&group_list_function);
276         res |= ast_custom_function_unregister(&group_function);
277
278         return res;
279 }
280
281 static int load_module(void)
282 {
283         int res = 0;
284
285         res |= ast_custom_function_register(&group_count_function);
286         res |= ast_custom_function_register(&group_match_count_function);
287         res |= ast_custom_function_register(&group_list_function);
288         res |= ast_custom_function_register(&group_function);
289
290         return res;
291 }
292
293 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");