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