Merged revisions 61681 via svnmerge from
[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         if (!chan)
162                 return -1;
163
164         headp = &chan->varshead;
165         AST_LIST_TRAVERSE(headp, current, entries) {
166                 if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
167                         if (!ast_strlen_zero(tmp1)) {
168                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
169                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2,
170                                          ast_var_value(current),
171                                          (ast_var_name(current) +
172                                           strlen(GROUP_CATEGORY_PREFIX) + 1));
173                         } else {
174                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current),
175                                          (ast_var_name(current) +
176                                           strlen(GROUP_CATEGORY_PREFIX) + 1));
177                         }
178                 } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
179                         if (!ast_strlen_zero(tmp1)) {
180                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
181                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2,
182                                          ast_var_value(current));
183                         } else {
184                                 snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
185                         }
186                 }
187         }
188         ast_copy_string(buf, tmp1, len);
189
190         return 0;
191 }
192
193 static struct ast_custom_function group_list_function = {
194         .name = "GROUP_LIST",
195         .syntax = "GROUP_LIST()",
196         .synopsis = "Gets a list of the groups set on a channel.",
197         .desc = "Gets a list of the groups set on a channel.\n",
198         .read = group_list_function_read,
199         .write = NULL,
200 };
201
202 static int unload_module(void)
203 {
204         int res = 0;
205
206         res |= ast_custom_function_unregister(&group_count_function);
207         res |= ast_custom_function_unregister(&group_match_count_function);
208         res |= ast_custom_function_unregister(&group_list_function);
209         res |= ast_custom_function_unregister(&group_function);
210
211         return res;
212 }
213
214 static int load_module(void)
215 {
216         int res = 0;
217
218         res |= ast_custom_function_register(&group_count_function);
219         res |= ast_custom_function_register(&group_match_count_function);
220         res |= ast_custom_function_register(&group_list_function);
221         res |= ast_custom_function_register(&group_function);
222
223         return res;
224 }
225
226 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");