Blocked revisions 53878 via svnmerge
[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 #include "asterisk.h"
25
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39
40 static int group_count_function_read(struct ast_channel *chan, const char *cmd,
41                                      char *data, char *buf, size_t len)
42 {
43         int count;
44         char group[80] = "";
45         char category[80] = "";
46         const char *grp;
47
48         ast_app_group_split_group(data, group, sizeof(group), category,
49                                   sizeof(category));
50
51         if (ast_strlen_zero(group)) {
52                 if ((grp = pbx_builtin_getvar_helper(chan, category)))
53                         ast_copy_string(group, grp, sizeof(group));
54                 else
55                         ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n",
56                                 chan->name);
57         }
58
59         count = ast_app_group_get_count(group, category);
60         snprintf(buf, len, "%d", count);
61
62         return 0;
63 }
64
65 static struct ast_custom_function group_count_function = {
66         .name = "GROUP_COUNT",
67         .syntax = "GROUP_COUNT([groupname][@category])",
68         .synopsis = "Counts the number of channels in the specified group",
69         .desc =
70                 "Calculates the group count for the specified group, or uses the\n"
71                 "channel's current group if not specifed (and non-empty).\n",
72         .read = group_count_function_read,
73 };
74
75 static int group_match_count_function_read(struct ast_channel *chan,
76                                            const char *cmd, char *data, char *buf,
77                                            size_t len)
78 {
79         int count;
80         char group[80] = "";
81         char category[80] = "";
82
83         ast_app_group_split_group(data, group, sizeof(group), category,
84                                   sizeof(category));
85
86         if (!ast_strlen_zero(group)) {
87                 count = ast_app_group_match_get_count(group, category);
88                 snprintf(buf, len, "%d", count);
89         }
90
91         return 0;
92 }
93
94 static struct ast_custom_function group_match_count_function = {
95         .name = "GROUP_MATCH_COUNT",
96         .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
97         .synopsis =
98                 "Counts the number of channels in the groups matching the specified pattern",
99         .desc =
100                 "Calculates the group count for all groups that match the specified pattern.\n"
101                 "Uses standard regular expression matching (see regex(7)).\n",
102         .read = group_match_count_function_read,
103         .write = NULL,
104 };
105
106 static int group_function_read(struct ast_channel *chan, const char *cmd,
107                                char *data, char *buf, size_t len)
108 {
109         char varname[256];
110         const char *group;
111
112         if (!ast_strlen_zero(data)) {
113                 snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX,
114                          data);
115         } else {
116                 ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
117         }
118
119         group = pbx_builtin_getvar_helper(chan, varname);
120         if (group)
121                 ast_copy_string(buf, group, len);
122
123         return 0;
124 }
125
126 static int group_function_write(struct ast_channel *chan, const char *cmd,
127                                 char *data, const char *value)
128 {
129         char grpcat[256];
130
131         if (!ast_strlen_zero(data)) {
132                 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
133         } else {
134                 ast_copy_string(grpcat, value, sizeof(grpcat));
135         }
136
137         if (ast_app_group_set_channel(chan, grpcat))
138                 ast_log(LOG_WARNING,
139                                 "Setting a group requires an argument (group name)\n");
140
141         return 0;
142 }
143
144 static struct ast_custom_function group_function = {
145         .name = "GROUP",
146         .syntax = "GROUP([category])",
147         .synopsis = "Gets or sets the channel group.",
148         .desc = "Gets or sets the channel group.\n",
149         .read = group_function_read,
150         .write = group_function_write,
151 };
152
153 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
154                                     char *data, char *buf, size_t len)
155 {
156         struct ast_var_t *current;
157         struct varshead *headp;
158         char tmp1[1024] = "";
159         char tmp2[1024] = "";
160
161         headp = &chan->varshead;
162         AST_LIST_TRAVERSE(headp, current, entries) {
163                 if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
164                         if (!ast_strlen_zero(tmp1)) {
165                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
166                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2,
167                                          ast_var_value(current),
168                                          (ast_var_name(current) +
169                                           strlen(GROUP_CATEGORY_PREFIX) + 1));
170                         } else {
171                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current),
172                                          (ast_var_name(current) +
173                                           strlen(GROUP_CATEGORY_PREFIX) + 1));
174                         }
175                 } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
176                         if (!ast_strlen_zero(tmp1)) {
177                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
178                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2,
179                                          ast_var_value(current));
180                         } else {
181                                 snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
182                         }
183                 }
184         }
185         ast_copy_string(buf, tmp1, len);
186
187         return 0;
188 }
189
190 static struct ast_custom_function group_list_function = {
191         .name = "GROUP_LIST",
192         .syntax = "GROUP_LIST()",
193         .synopsis = "Gets a list of the groups set on a channel.",
194         .desc = "Gets a list of the groups set on a channel.\n",
195         .read = group_list_function_read,
196         .write = NULL,
197 };
198
199 static int unload_module(void)
200 {
201         int res = 0;
202
203         res |= ast_custom_function_unregister(&group_count_function);
204         res |= ast_custom_function_unregister(&group_match_count_function);
205         res |= ast_custom_function_unregister(&group_list_function);
206         res |= ast_custom_function_unregister(&group_function);
207
208         return res;
209 }
210
211 static int load_module(void)
212 {
213         int res = 0;
214
215         res |= ast_custom_function_register(&group_count_function);
216         res |= ast_custom_function_register(&group_match_count_function);
217         res |= ast_custom_function_register(&group_list_function);
218         res |= ast_custom_function_register(&group_function);
219
220         return res;
221 }
222
223 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");