minor simplification of a conditional statement
[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         for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
106                 if (gi->chan != chan)
107                         continue;
108                 if (ast_strlen_zero(data))
109                         break;
110                 if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
111                         break;
112         }
113         
114         if (gi)
115                 ast_copy_string(buf, gi->group, len);
116         
117         ast_app_group_list_unlock();
118         
119         return 0;
120 }
121
122 static int group_function_write(struct ast_channel *chan, const char *cmd,
123                                 char *data, const char *value)
124 {
125         char grpcat[256];
126
127         if (!ast_strlen_zero(data)) {
128                 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
129         } else {
130                 ast_copy_string(grpcat, value, sizeof(grpcat));
131         }
132
133         if (ast_app_group_set_channel(chan, grpcat))
134                 ast_log(LOG_WARNING,
135                                 "Setting a group requires an argument (group name)\n");
136
137         return 0;
138 }
139
140 static struct ast_custom_function group_function = {
141         .name = "GROUP",
142         .syntax = "GROUP([category])",
143         .synopsis = "Gets or sets the channel group.",
144         .desc = "Gets or sets the channel group.\n",
145         .read = group_function_read,
146         .write = group_function_write,
147 };
148
149 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
150                                     char *data, char *buf, size_t len)
151 {
152         struct ast_group_info *gi = NULL;
153         char tmp1[1024] = "";
154         char tmp2[1024] = "";
155
156         if (!chan)
157                 return -1;
158
159         ast_app_group_list_rdlock();
160
161         for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
162                 if (gi->chan != chan)
163                         continue;
164                 if (!ast_strlen_zero(tmp1)) {
165                         ast_copy_string(tmp2, tmp1, sizeof(tmp2));
166                         if (!ast_strlen_zero(gi->category))
167                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
168                         else
169                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
170                 } else {
171                         if (!ast_strlen_zero(gi->category))
172                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
173                         else
174                                 snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
175                 }
176         }
177         
178         ast_app_group_list_unlock();
179
180         ast_copy_string(buf, tmp1, len);
181
182         return 0;
183 }
184
185 static struct ast_custom_function group_list_function = {
186         .name = "GROUP_LIST",
187         .syntax = "GROUP_LIST()",
188         .synopsis = "Gets a list of the groups set on a channel.",
189         .desc = "Gets a list of the groups set on a channel.\n",
190         .read = group_list_function_read,
191         .write = NULL,
192 };
193
194 static int unload_module(void)
195 {
196         int res = 0;
197
198         res |= ast_custom_function_unregister(&group_count_function);
199         res |= ast_custom_function_unregister(&group_match_count_function);
200         res |= ast_custom_function_unregister(&group_list_function);
201         res |= ast_custom_function_unregister(&group_function);
202
203         return res;
204 }
205
206 static int load_module(void)
207 {
208         int res = 0;
209
210         res |= ast_custom_function_register(&group_count_function);
211         res |= ast_custom_function_register(&group_match_count_function);
212         res |= ast_custom_function_register(&group_list_function);
213         res |= ast_custom_function_register(&group_function);
214
215         return res;
216 }
217
218 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");