Use read/write lock based lists for group counting.
[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 <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39
40 static int group_count_function_read(struct ast_channel *chan, const char *cmd,
41                                      char *data, char *buf, size_t len)
42 {
43         int count = -1;
44         char group[80] = "", category[80] = "";
45
46         ast_app_group_split_group(data, group, sizeof(group), category,
47                                   sizeof(category));
48
49         if ((count = ast_app_group_get_count(group, category)) == -1)
50                 ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
51         else
52                 snprintf(buf, len, "%d", count);
53
54         return 0;
55 }
56
57 static struct ast_custom_function group_count_function = {
58         .name = "GROUP_COUNT",
59         .syntax = "GROUP_COUNT([groupname][@category])",
60         .synopsis = "Counts the number of channels in the specified group",
61         .desc =
62                 "Calculates the group count for the specified group, or uses the\n"
63                 "channel's current group if not specifed (and non-empty).\n",
64         .read = group_count_function_read,
65 };
66
67 static int group_match_count_function_read(struct ast_channel *chan,
68                                            const char *cmd, char *data, char *buf,
69                                            size_t len)
70 {
71         int count;
72         char group[80] = "";
73         char category[80] = "";
74
75         ast_app_group_split_group(data, group, sizeof(group), category,
76                                   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 0;
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 =
90                 "Counts the number of channels in the groups matching the specified pattern",
91         .desc =
92                 "Calculates the group count for all groups that match the specified pattern.\n"
93                 "Uses standard regular expression matching (see regex(7)).\n",
94         .read = group_match_count_function_read,
95         .write = NULL,
96 };
97
98 static int group_function_read(struct ast_channel *chan, const char *cmd,
99                                char *data, char *buf, size_t len)
100 {
101         struct ast_group_info *gi = NULL;
102         
103         ast_app_group_list_rdlock();
104         
105         gi = ast_app_group_list_head();
106         while (gi) {
107                 if (gi->chan != chan)
108                         continue;
109                 if (ast_strlen_zero(data))
110                         break;
111                 if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
112                         break;
113                 gi = AST_LIST_NEXT(gi, list);
114         }
115         
116         if (gi)
117                 ast_copy_string(buf, gi->group, len);
118         
119         ast_app_group_list_unlock();
120         
121         return 0;
122 }
123
124 static int group_function_write(struct ast_channel *chan, const char *cmd,
125                                 char *data, const char *value)
126 {
127         char grpcat[256];
128
129         if (!ast_strlen_zero(data)) {
130                 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
131         } else {
132                 ast_copy_string(grpcat, value, sizeof(grpcat));
133         }
134
135         if (ast_app_group_set_channel(chan, grpcat))
136                 ast_log(LOG_WARNING,
137                                 "Setting a group requires an argument (group name)\n");
138
139         return 0;
140 }
141
142 static struct ast_custom_function group_function = {
143         .name = "GROUP",
144         .syntax = "GROUP([category])",
145         .synopsis = "Gets or sets the channel group.",
146         .desc = "Gets or sets the channel group.\n",
147         .read = group_function_read,
148         .write = group_function_write,
149 };
150
151 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
152                                     char *data, char *buf, size_t len)
153 {
154         struct ast_group_info *gi = NULL;
155         char tmp1[1024] = "";
156         char tmp2[1024] = "";
157
158         if (!chan)
159                 return -1;
160
161         ast_app_group_list_rdlock();
162
163         gi = ast_app_group_list_head();
164         while (gi) {
165                 if (gi->chan != chan)
166                         continue;
167                 if (!ast_strlen_zero(tmp1)) {
168                         ast_copy_string(tmp2, tmp1, sizeof(tmp2));
169                         if (!ast_strlen_zero(gi->category))
170                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
171                         else
172                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
173                 } else {
174                         if (!ast_strlen_zero(gi->category))
175                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
176                         else
177                                 snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
178                 }
179                 gi = AST_LIST_NEXT(gi, list);
180         }
181         
182         ast_app_group_list_unlock();
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");