add dialplan functions for Caller ID, language and timeouts (bug #4219, with mods)
[asterisk/asterisk.git] / funcs / func_groupcount.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Channel group related dialplan functions
5  * 
6  * Copyright (C) 2005, Digium, Inc.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15
16 #include "asterisk/channel.h"
17 #include "asterisk/pbx.h"
18 #include "asterisk/logger.h"
19 #include "asterisk/utils.h"
20 #include "asterisk/app.h"
21
22 static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
23 {
24         int count;
25         char group[80] = "";
26         char category[80] = "";
27         char *grp;
28
29         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
30
31         if (ast_strlen_zero(group)) {
32                 grp = pbx_builtin_getvar_helper(chan, category);
33                 ast_copy_string(group, grp, sizeof(group));
34         }
35
36         count = ast_app_group_get_count(group, category);
37         snprintf(buf, len, "%d", count);
38
39         return buf;
40 }
41
42 #ifndef BUILTIN_FUNC
43 static
44 #endif
45 struct ast_custom_function group_count_function = {
46         .name = "GROUP_COUNT",
47         .syntax = "GROUP_COUNT([groupname][@category])",
48         .synopsis = "Counts the number of channels in the specified group",
49         .desc = "Calculates the group count for the specified group, or uses the\n"
50         "channel's current group if not specifed (and non-empty).\n",
51         .read = group_count_function_read,
52 };
53
54 static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
55 {
56         int count;
57         char group[80] = "";
58         char category[80] = "";
59
60         ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
61
62         if (!ast_strlen_zero(group)) {
63                 count = ast_app_group_match_get_count(group, category);
64                 snprintf(buf, len, "%d", count);
65         }
66
67         return buf;
68 }
69
70 #ifndef BUILTIN_FUNC
71 static
72 #endif
73 struct ast_custom_function group_match_count_function = {
74         .name = "GROUP_MATCH_COUNT",
75         .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
76         .synopsis = "Counts the number of channels in the groups matching the specified pattern",
77         .desc = "Calculates the group count for all groups that match the specified pattern.\n"
78         "Uses standard regular expression matching (see regex(7)).\n",
79         .read = group_match_count_function_read,
80         .write = NULL,
81 };
82
83 static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
84 {
85         char varname[256];
86         char *group;
87
88         if (data && !ast_strlen_zero(data)) {
89                 snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, data);
90         } else {
91                 ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
92         }
93
94         group = pbx_builtin_getvar_helper(chan, varname);
95         if (group)
96                 ast_copy_string(buf, group, len);
97
98         return buf;
99 }
100
101 static void group_function_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
102 {
103         char grpcat[256];
104
105         if (data && !ast_strlen_zero(data)) {
106                 snprintf(grpcat, sizeof(grpcat), "%s@%s", data, value);
107         } else {
108                 ast_copy_string(grpcat, value, sizeof(grpcat));
109         }
110
111         if (ast_app_group_set_channel(chan, grpcat))
112                 ast_log(LOG_WARNING, "Setting a group requires an argument (group name)\n");
113 }
114
115 #ifndef BUILTIN_FUNC
116 static
117 #endif
118 struct ast_custom_function group_function = {
119         .name = "GROUP",
120         .syntax = "GROUP([category])",
121         .synopsis = "Gets or sets the channel group.",
122         .desc = "Gets or sets the channel group.\n",
123         .read = group_function_read,
124         .write = group_function_write,
125 };
126
127 static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
128 {
129         struct ast_var_t *current;
130         struct varshead *headp;
131         char tmp1[1024] = "";
132         char tmp2[1024] = "";
133
134         headp=&chan->varshead;
135         AST_LIST_TRAVERSE(headp,current,entries) {
136                 if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
137                         if (!ast_strlen_zero(tmp1)) {
138                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
139                                 snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
140                         } else {
141                                 snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
142                         }
143                 } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
144                         if (!ast_strlen_zero(tmp1)) {
145                                 ast_copy_string(tmp2, tmp1, sizeof(tmp2));
146                                 snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, ast_var_value(current));
147                         } else {
148                                 snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
149                         }
150                 }
151         }
152         ast_copy_string(buf, tmp1, len);
153         return buf;
154 }
155
156 #ifndef BUILTIN_FUNC
157 static
158 #endif
159 struct ast_custom_function group_list_function = {
160         .name = "GROUP_LIST",
161         .syntax = "GROUP_LIST()",
162         .synopsis = "Gets a list of the groups set on a channel.",
163         .desc = "Gets a list of the groups set on a channel.\n",
164         .read = group_list_function_read,
165         .write = NULL,
166 };
167
168 /*
169 Local Variables:
170 mode: C
171 c-file-style: "linux"
172 indent-tabs-mode: nil
173 End:
174 */