Debian Codegraph @

funcs/func_groupcount.c

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