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