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