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