Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_groupcount.c
Go to the documentation of this file.
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
110static 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
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))
140 }
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
155 .name = "GROUP_COUNT",
157 .read_max = 12,
158};
159
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
181 .name = "GROUP_MATCH_COUNT",
183 .read_max = 12,
184 .write = NULL,
185};
186
187static 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
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) {
211 ret = 0;
212 }
213
215
216 return ret;
217}
218
219static 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))
241 "Setting a group requires an argument (group name)\n");
242
243 return 0;
244}
245
247 .name = "GROUP",
248 .read = group_function_read,
249 .write = group_function_write,
250};
251
252static 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
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
282
283 ast_copy_string(buf, tmp1, len);
284
285 return 0;
286}
287
289 .name = "GROUP_LIST",
291 .write = NULL,
292};
293
294static int unload_module(void)
295{
296 int res = 0;
297
302
303 return res;
304}
305
306static int load_module(void)
307{
308 int res = 0;
309
314
315 return res;
316}
317
318AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int group_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int group_count_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int group_function_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static struct ast_custom_function group_list_function
static struct ast_custom_function group_count_function
static struct ast_custom_function group_match_count_function
static int load_module(void)
static struct ast_custom_function group_function
static int unload_module(void)
static int group_match_count_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int group_list_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int ast_app_group_get_count(const char *group, const char *category)
Get the current channel count of the specified group and category.
Definition: main/app.c:2240
int ast_app_group_match_get_count(const char *groupmatch, const char *category)
Get the current channel count of all groups that match the specified pattern and category.
Definition: main/app.c:2260
int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
Set the group for a channel, splitting the provided data into group and category, if specified.
Definition: main/app.c:2193
int ast_app_group_list_unlock(void)
Unlock the group count list.
Definition: main/app.c:2351
int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max)
Split a group string into group and category, returning a default category if none is provided.
Definition: main/app.c:2166
struct ast_group_info * ast_app_group_list_head(void)
Get the head of the group count list.
Definition: main/app.c:2346
int ast_app_group_list_rdlock(void)
Read Lock the group count list.
Definition: main/app.c:2341
#define LOG_NOTICE
#define LOG_WARNING
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
channel group info
Definition: channel.h:2963
char * category
Definition: channel.h:2965
char * group
Definition: channel.h:2966
struct ast_group_info::@212 group_list
struct ast_channel * chan
Definition: channel.h:2964
int value
Definition: syslog.c:37
Utility functions.