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