Use find_user for existsmailbox
[asterisk/asterisk.git] / apps / app_groupcount.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Group Manipulation Applications
5  *
6  * Copyright (c) 2004 Digium
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <asterisk/file.h>
19 #include <asterisk/logger.h>
20 #include <asterisk/options.h>
21 #include <asterisk/channel.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/module.h>
24 #include <asterisk/utils.h>
25
26 static char *tdesc = "Group Management Routines";
27
28 static char *app_group_count = "GetGroupCount";
29 static char *app_group_set = "SetGroup";
30 static char *app_group_check = "CheckGroup";
31
32 static char *group_count_synopsis = "GetGroupCount([groupname])";
33 static char *group_set_synopsis = "SetGroup([groupname])";
34 static char *group_check_synopsis = "CheckGroup(max)";
35
36 static char *group_count_descrip =
37 "GetGroupCount([group])\n"
38 "  Calculates the group count for the specified group, or uses\n"
39 "the current channel's group if not specifed (and non-empty).\n"
40 "Stores result in GROUPCOUNT.  Always returns 0.\n";
41
42 static char *group_set_descrip =
43 "SetGroup(group)\n"
44 "  Sets the channel group to the specified value.  Equivalent to\n"
45 "SetVar(GROUP=group).  Always returns 0.\n";
46
47 static char *group_check_descrip =
48 "CheckGroup(max)\n"
49 "  Checks that the current number of total channels in the\n"
50 "current channel's group does not exceed 'max'.  If the number\n"
51 "does not exceed 'max', we continue to the next step. If the\n"
52 "number does in fact exceed max, if priority n+101 exists, then\n"
53 "execution continues at that step, otherwise -1 is returned.\n";
54
55 STANDARD_LOCAL_USER;
56
57 LOCAL_USER_DECL;
58
59 static int group_get_count(char *group)
60 {
61         struct ast_channel *chan;
62         int count = 0;
63         char *test;
64         if (group && !ast_strlen_zero(group)) {
65                 chan = ast_channel_walk_locked(NULL);
66                 while(chan) {
67                         test = pbx_builtin_getvar_helper(chan, "GROUP");
68                         if (test && !strcasecmp(test, group))
69                                 count++;
70                         ast_mutex_unlock(&chan->lock);
71                         chan = ast_channel_walk_locked(chan);
72                 }
73         }
74         return count;
75 }
76
77 static int group_count_exec(struct ast_channel *chan, void *data)
78 {
79         int res=0;
80         int count;
81         struct localuser *u;
82         char *group=NULL;
83         char ret[80];
84
85         LOCAL_USER_ADD(u);
86
87         /* Check and parse arguments */
88         if (data && !ast_strlen_zero(data)) {
89                 group = (char *)data;
90         } else {
91                 group = pbx_builtin_getvar_helper(chan, "GROUP");
92         }
93         count = group_get_count(group);
94         snprintf(ret, sizeof(ret), "%d", count);
95         pbx_builtin_setvar_helper(chan, "GROUPCOUNT", ret);
96         LOCAL_USER_REMOVE(u);
97         return res;
98 }
99
100 static int group_set_exec(struct ast_channel *chan, void *data)
101 {
102         int res=0;
103         struct localuser *u;
104
105         LOCAL_USER_ADD(u);
106         /* Check and parse arguments */
107         if (data && !ast_strlen_zero(data)) {
108                 pbx_builtin_setvar_helper(chan, "GROUP", (char *)data);
109         } else
110                 ast_log(LOG_WARNING, "GroupSet requires an argument (group name)\n");
111
112         LOCAL_USER_REMOVE(u);
113         return res;
114 }
115
116 static int group_check_exec(struct ast_channel *chan, void *data)
117 {
118         int res=0;
119         int max, count;
120         struct localuser *u;
121
122         LOCAL_USER_ADD(u);
123
124         if (data && (sscanf((char *)data, "%i", &max) == 1) && (max > -1)) {    
125                 count = group_get_count(pbx_builtin_getvar_helper(chan, "GROUP"));
126                 if (count > max) {
127                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
128                                 chan->priority += 100;
129                         else
130                                 res = -1;
131                 }
132         } else
133                 ast_log(LOG_WARNING, "GroupCheck requires a positive integer argument (max)\n");
134         LOCAL_USER_REMOVE(u);
135         return res;
136 }
137
138 int unload_module(void)
139 {
140         int res;
141         STANDARD_HANGUP_LOCALUSERS;
142         res = ast_unregister_application(app_group_count);
143         res |= ast_unregister_application(app_group_set);
144         res |= ast_unregister_application(app_group_check);
145         return res;
146 }
147
148 int load_module(void)
149 {
150         int res;
151         res = ast_register_application(app_group_count, group_count_exec, group_count_synopsis, group_count_descrip);
152         res |= ast_register_application(app_group_set, group_set_exec, group_set_synopsis, group_set_descrip);
153         res |= ast_register_application(app_group_check, group_check_exec, group_check_synopsis, group_check_descrip);
154         return res;
155 }
156
157 char *description(void)
158 {
159         return tdesc;
160 }
161
162 int usecount(void)
163 {
164         int res;
165         STANDARD_USECOUNT(res);
166         return res;
167 }
168
169 char *key()
170 {
171         return ASTERISK_GPL_KEY;
172 }