fix compilation warnings
[asterisk-bristuff.git] / funcs / func_groupcount.c
blobbb2bcf49c1927c3d8cd3283bb2337757ae53a01e
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
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.
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.
17 /*! \file
19 * \brief Channel group related dialplan functions
23 #include "asterisk.h"
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
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"
39 static int group_count_function_read(struct ast_channel *chan, char *cmd,
40 char *data, char *buf, size_t len)
42 int count = -1;
43 char group[80] = "", category[80] = "";
45 ast_app_group_split_group(data, group, sizeof(group), category,
46 sizeof(category));
48 /* If no group has been provided let's find one */
49 if (ast_strlen_zero(group)) {
50 struct ast_group_info *gi = NULL;
52 ast_app_group_list_lock();
53 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
54 if (gi->chan != chan)
55 continue;
56 if (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))
57 break;
59 if (gi) {
60 ast_copy_string(group, gi->group, sizeof(group));
61 if (!ast_strlen_zero(gi->category))
62 ast_copy_string(category, gi->category, sizeof(category));
64 ast_app_group_list_unlock();
67 if ((count = ast_app_group_get_count(group, category)) == -1)
68 ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
69 else
70 snprintf(buf, len, "%d", count);
72 return 0;
75 static struct ast_custom_function group_count_function = {
76 .name = "GROUP_COUNT",
77 .syntax = "GROUP_COUNT([groupname][@category])",
78 .synopsis = "Counts the number of channels in the specified group",
79 .desc =
80 "Calculates the group count for the specified group, or uses the\n"
81 "channel's current group if not specifed (and non-empty).\n",
82 .read = group_count_function_read,
85 static int group_match_count_function_read(struct ast_channel *chan,
86 char *cmd, char *data, char *buf,
87 size_t len)
89 int count;
90 char group[80] = "";
91 char category[80] = "";
93 ast_app_group_split_group(data, group, sizeof(group), category,
94 sizeof(category));
96 if (!ast_strlen_zero(group)) {
97 count = ast_app_group_match_get_count(group, category);
98 snprintf(buf, len, "%d", count);
101 return 0;
104 static struct ast_custom_function group_match_count_function = {
105 .name = "GROUP_MATCH_COUNT",
106 .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
107 .synopsis =
108 "Counts the number of channels in the groups matching the specified pattern",
109 .desc =
110 "Calculates the group count for all groups that match the specified pattern.\n"
111 "Uses standard regular expression matching (see regex(7)).\n",
112 .read = group_match_count_function_read,
113 .write = NULL,
116 static int group_function_read(struct ast_channel *chan, char *cmd,
117 char *data, char *buf, size_t len)
119 struct ast_group_info *gi = NULL;
121 ast_app_group_list_lock();
123 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
124 if (gi->chan != chan)
125 continue;
126 if (ast_strlen_zero(data))
127 break;
128 if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
129 break;
132 if (gi)
133 ast_copy_string(buf, gi->group, len);
135 ast_app_group_list_unlock();
137 return 0;
140 static int group_function_write(struct ast_channel *chan, char *cmd,
141 char *data, const char *value)
143 char grpcat[256];
145 if (!ast_strlen_zero(data)) {
146 snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
147 } else {
148 ast_copy_string(grpcat, value, sizeof(grpcat));
151 if (ast_app_group_set_channel(chan, grpcat))
152 ast_log(LOG_WARNING,
153 "Setting a group requires an argument (group name)\n");
155 return 0;
158 static struct ast_custom_function group_function = {
159 .name = "GROUP",
160 .syntax = "GROUP([category])",
161 .synopsis = "Gets or sets the channel group.",
162 .desc = "Gets or sets the channel group.\n",
163 .read = group_function_read,
164 .write = group_function_write,
167 static int group_list_function_read(struct ast_channel *chan, char *cmd,
168 char *data, char *buf, size_t len)
170 struct ast_group_info *gi = NULL;
171 char tmp1[1024] = "";
172 char tmp2[1024] = "";
174 if (!chan)
175 return -1;
177 ast_app_group_list_lock();
179 for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
180 if (gi->chan != chan)
181 continue;
182 if (!ast_strlen_zero(tmp1)) {
183 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
184 if (!ast_strlen_zero(gi->category))
185 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
186 else
187 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
188 } else {
189 if (!ast_strlen_zero(gi->category))
190 snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
191 else
192 snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
196 ast_app_group_list_unlock();
198 ast_copy_string(buf, tmp1, len);
200 return 0;
203 static struct ast_custom_function group_list_function = {
204 .name = "GROUP_LIST",
205 .syntax = "GROUP_LIST()",
206 .synopsis = "Gets a list of the groups set on a channel.",
207 .desc = "Gets a list of the groups set on a channel.\n",
208 .read = group_list_function_read,
209 .write = NULL,
212 static int unload_module(void)
214 int res = 0;
216 res |= ast_custom_function_unregister(&group_count_function);
217 res |= ast_custom_function_unregister(&group_match_count_function);
218 res |= ast_custom_function_unregister(&group_list_function);
219 res |= ast_custom_function_unregister(&group_function);
221 return res;
224 static int load_module(void)
226 int res = 0;
228 res |= ast_custom_function_register(&group_count_function);
229 res |= ast_custom_function_register(&group_match_count_function);
230 res |= ast_custom_function_register(&group_list_function);
231 res |= ast_custom_function_register(&group_function);
233 return res;
236 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");