Doxygen documentation update from oej (issue #5505)
[asterisk/asterisk.git] / funcs / func_groupcount.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, 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  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26
27 #include "asterisk.h"
28
29 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
30
31 #include "asterisk/channel.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/logger.h"
34 #include "asterisk/utils.h"
35 #include "asterisk/app.h"
36
37 static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
38 {
39         int count;
40         char group[80] = "";
41         char category[80] = "";
42         char *grp;
43
44         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
45
46         if (ast_strlen_zero(group)) {
47                 if ((grp = pbx_builtin_getvar_helper(chan, category)))
48                         ast_copy_string(group, grp, sizeof(group));
49                 else
50                         ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);  
51         }
52
53         count = ast_app_group_get_count(group, category);
54         snprintf(buf, len, "%d", count);
55
56         return buf;
57 }
58
59 #ifndef BUILTIN_FUNC
60 static
61 #endif
62 struct ast_custom_function group_count_function = {
63         .name = "GROUP_COUNT",
64         .syntax = "GROUP_COUNT([groupname][@category])",
65         .synopsis = "Counts the number of channels in the specified group",
66         .desc = "Calculates the group count for the specified group, or uses the\n"
67         "channel's current group if not specifed (and non-empty).\n",
68         .read = group_count_function_read,
69 };
70
71 static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
72 {
73         int count;
74         char group[80] = "";
75         char category[80] = "";
76
77         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
78
79         if (!ast_strlen_zero(group)) {
80                 count = ast_app_group_match_get_count(group, category);
81                 snprintf(buf, len, "%d", count);
82         }
83
84         return buf;
85 }
86
87 #ifndef BUILTIN_FUNC
88 static
89 #endif
90 struct ast_custom_function group_match_count_function = {
91         .name = "GROUP_MATCH_COUNT",
92         .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
93         .synopsis = "Counts the number of channels in the groups matching the specified pattern",
94         .desc = "Calculates the group count for all groups that match the specified pattern.\n"
95         "Uses standard regular expression matching (see regex(7)).\n",
96         .read = group_match_count_function_read,
97         .write = NULL,
98 };
99
100 static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
101 {
102         char varname[256];
103         char *group;
104
105         if (data && !ast_strlen_zero(data)) {
106                 snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, data);
107         } else {
108                 ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
109         }
110
111         group = pbx_builtin_getvar_helper(chan, varname);
112         if (group)
113                 ast_copy_string(buf, group, len);
114
115         return buf;
116 }
117
118 static void group_function_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
119 {
120         char grpcat[256];
121
122         if (data && !ast_strlen_zero(data)) {
123                 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
124         } else {
125                 ast_copy_string(grpcat, value, sizeof(grpcat));
126         }
127
128         if (ast_app_group_set_channel(chan, grpcat))
129                 ast_log(LOG_WARNING, "Setting a group requires an argument (group name)\n");
130 }
131
132 #ifndef BUILTIN_FUNC
133 static
134 #endif
135 struct ast_custom_function group_function = {
136         .name = "GROUP",
137         .syntax = "GROUP([category])",
138         .synopsis = "Gets or sets the channel group.",
139         .desc = "Gets or sets the channel group.\n",
140         .read = group_function_read,
141         .write = group_function_write,
142 };
143
144 static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
145 {
146         struct ast_var_t *current;
147         struct varshead *headp;
148         char tmp1[1024] = "";
149         char tmp2[1024] = "";
150
151         headp=&chan->varshead;
152         AST_LIST_TRAVERSE(headp,current,entries) {
153                 if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
154                         if (!ast_strlen_zero(tmp1)) {
155                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
156                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
157                         } else {
158                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
159                         }
160                 } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
161                         if (!ast_strlen_zero(tmp1)) {
162                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
163                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, ast_var_value(current));
164                         } else {
165                                 snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
166                         }
167                 }
168         }
169         ast_copy_string(buf, tmp1, len);
170         return buf;
171 }
172
173 #ifndef BUILTIN_FUNC
174 static
175 #endif
176 struct ast_custom_function group_list_function = {
177         .name = "GROUP_LIST",
178         .syntax = "GROUP_LIST()",
179         .synopsis = "Gets a list of the groups set on a channel.",
180         .desc = "Gets a list of the groups set on a channel.\n",
181         .read = group_list_function_read,
182         .write = NULL,
183 };
184
185 /*
186 Local Variables:
187 mode: C
188 c-file-style: "linux"
189 indent-tabs-mode: nil
190 End:
191 */