Merge "res_rtp_asterisk: Add ice_blacklist option"
[asterisk/asterisk.git] / funcs / func_dialgroup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Tilghman Lesher
5  *
6  * Tilghman Lesher <func_dialgroup__200709@the-tilghman.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Dial group dialplan function
22  *
23  * \author Tilghman Lesher <func_dialgroup__200709@the-tilghman.com>
24  *
25  * \ingroup functions
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_REGISTER_FILE()
35
36 #include <sys/stat.h>
37
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/astobj2.h"
44 #include "asterisk/astdb.h"
45
46 /*** DOCUMENTATION
47         <function name="DIALGROUP" language="en_US">
48                 <synopsis>
49                         Manages a group of users for dialing.
50                 </synopsis>
51                 <syntax>
52                         <parameter name="group" required="true" />
53                         <parameter name="op">
54                                 <para>The operation name, possible values are:</para>
55                                 <para><literal>add</literal> - add a channel name or interface (write-only)</para>
56                                 <para><literal>del</literal> - remove a channel name or interface (write-only)</para>
57                         </parameter>
58                 </syntax>
59                 <description>
60                         <para>Presents an interface meant to be used in concert with the Dial
61                         application, by presenting a list of channels which should be dialled when
62                         referenced.</para>
63                         <para>When DIALGROUP is read from, the argument is interpreted as the particular
64                         <replaceable>group</replaceable> for which a dial should be attempted.  When DIALGROUP is written to
65                         with no arguments, the entire list is replaced with the argument specified.</para>
66                         <para>Functionality is similar to a queue, except that when no interfaces are
67                         available, execution may continue in the dialplan.  This is useful when
68                         you want certain people to be the first to answer any calls, with immediate
69                         fallback to a queue when the front line people are busy or unavailable, but
70                         you still want front line people to log in and out of that group, just like
71                         a queue.</para>
72                         <para>Example:</para>
73                         <para>exten => 1,1,Set(DIALGROUP(mygroup,add)=SIP/10)</para>
74                         <para>exten => 1,n,Set(DIALGROUP(mygroup,add)=SIP/20)</para>
75                         <para>exten => 1,n,Dial(${DIALGROUP(mygroup)})</para>
76                 </description>
77         </function>
78  ***/
79
80 static struct ao2_container *group_container = NULL;
81
82 struct group_entry {
83         char name[AST_CHANNEL_NAME];
84 };
85
86 struct group {
87         char name[AST_MAX_EXTENSION];
88         struct ao2_container *entries;
89 };
90
91 static void group_destroy(void *vgroup)
92 {
93         struct group *group = vgroup;
94         ao2_ref(group->entries, -1);
95 }
96
97 static int group_hash_fn(const void *obj, const int flags)
98 {
99         const struct group *g = obj;
100         return ast_str_hash(g->name);
101 }
102
103 static int group_cmp_fn(void *obj1, void *name2, int flags)
104 {
105         struct group *g1 = obj1, *g2 = name2;
106         char *name = name2;
107         if (flags & OBJ_POINTER)
108                 return strcmp(g1->name, g2->name) ? 0 : CMP_MATCH | CMP_STOP;
109         else
110                 return strcmp(g1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
111 }
112
113 static int entry_hash_fn(const void *obj, const int flags)
114 {
115         const struct group_entry *e = obj;
116         return ast_str_hash(e->name);
117 }
118
119 static int entry_cmp_fn(void *obj1, void *name2, int flags)
120 {
121         struct group_entry *e1 = obj1, *e2 = name2;
122         char *name = name2;
123         if (flags & OBJ_POINTER)
124                 return strcmp(e1->name, e2->name) ? 0 : CMP_MATCH | CMP_STOP;
125         else
126                 return strcmp(e1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
127 }
128
129 static int dialgroup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
130 {
131         struct ao2_iterator i;
132         struct group *grhead = ao2_find(group_container, data, 0);
133         struct group_entry *entry;
134         size_t bufused = 0;
135         int trunc_warning = 0;
136         int res = 0;
137
138         if (!grhead) {
139                 if (!ast_strlen_zero(cmd)) {
140                         ast_log(LOG_WARNING, "No such dialgroup '%s'\n", data);
141                 }
142                 return -1;
143         }
144
145         buf[0] = '\0';
146
147         i = ao2_iterator_init(grhead->entries, 0);
148         while ((entry = ao2_iterator_next(&i))) {
149                 int tmp = strlen(entry->name);
150                 /* Ensure that we copy only complete names, not partials */
151                 if (len - bufused > tmp + 2) {
152                         if (bufused != 0)
153                                 buf[bufused++] = '&';
154                         ast_copy_string(buf + bufused, entry->name, len - bufused);
155                         bufused += tmp;
156                 } else if (trunc_warning++ == 0) {
157                         if (!ast_strlen_zero(cmd)) {
158                                 ast_log(LOG_WARNING, "Dialgroup '%s' is too large.  Truncating list.\n", data);
159                         } else {
160                                 res = 1;
161                                 ao2_ref(entry, -1);
162                                 break;
163                         }
164                 }
165                 ao2_ref(entry, -1);
166         }
167         ao2_iterator_destroy(&i);
168         ao2_ref(grhead, -1);
169
170         return res;
171 }
172
173 static int dialgroup_refreshdb(struct ast_channel *chan, const char *cdialgroup)
174 {
175         int len = 500, res = 0;
176         char *buf = NULL;
177         char *new_buf;
178         char *dialgroup = ast_strdupa(cdialgroup);
179
180         do {
181                 len *= 2;
182                 new_buf = ast_realloc(buf, len);
183                 if (!new_buf) {
184                         ast_free(buf);
185                         return -1;
186                 }
187                 buf = new_buf;
188
189                 if ((res = dialgroup_read(chan, "", dialgroup, buf, len)) < 0) {
190                         ast_free(buf);
191                         return -1;
192                 }
193         } while (res == 1);
194
195         if (ast_strlen_zero(buf)) {
196                 ast_db_del("dialgroup", cdialgroup);
197         } else {
198                 ast_db_put("dialgroup", cdialgroup, buf);
199         }
200         ast_free(buf);
201         return 0;
202 }
203
204 static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)
205 {
206         struct group *grhead;
207         struct group_entry *entry;
208         int j, needrefresh = 1;
209         AST_DECLARE_APP_ARGS(args,
210                 AST_APP_ARG(group);
211                 AST_APP_ARG(op);
212         );
213         AST_DECLARE_APP_ARGS(inter,
214                 AST_APP_ARG(faces)[100];
215         );
216         char *value = ast_strdupa(cvalue);
217
218         AST_STANDARD_APP_ARGS(args, data);
219         AST_NONSTANDARD_APP_ARGS(inter, value, '&');
220
221         if (!(grhead = ao2_find(group_container, args.group, 0))) {
222                 /* Create group */
223                 grhead = ao2_alloc(sizeof(*grhead), group_destroy);
224                 if (!grhead)
225                         return -1;
226                 grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn);
227                 if (!grhead->entries) {
228                         ao2_ref(grhead, -1);
229                         return -1;
230                 }
231                 ast_copy_string(grhead->name, args.group, sizeof(grhead->name));
232                 ao2_link(group_container, grhead);
233         }
234
235         if (ast_strlen_zero(args.op)) {
236                 /* Wholesale replacement of the group */
237                 args.op = "add";
238
239                 /* Remove all existing */
240                 ao2_ref(grhead->entries, -1);
241                 if (!(grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn))) {
242                         ao2_unlink(group_container, grhead);
243                         ao2_ref(grhead, -1);
244                         return -1;
245                 }
246         }
247
248         if (strcasecmp(args.op, "add") == 0) {
249                 for (j = 0; j < inter.argc; j++) {
250                         /* Eliminate duplicates */
251                         if ((entry = ao2_find(grhead->entries, inter.faces[j], 0))) {
252                                 ao2_ref(entry, -1);
253                                 continue;
254                         }
255                         if ((entry = ao2_alloc(sizeof(*entry), NULL))) {
256                                 ast_copy_string(entry->name, inter.faces[j], sizeof(entry->name));
257                                 ao2_link(grhead->entries, entry);
258                                 ao2_ref(entry, -1);
259                         } else {
260                                 ast_log(LOG_WARNING, "Unable to add '%s' to dialgroup '%s'\n", inter.faces[j], grhead->name);
261                         }
262                 }
263         } else if (strncasecmp(args.op, "del", 3) == 0) {
264                 for (j = 0; j < inter.argc; j++) {
265                         if ((entry = ao2_find(grhead->entries, inter.faces[j], OBJ_UNLINK))) {
266                                 ao2_ref(entry, -1);
267                         } else {
268                                 ast_log(LOG_WARNING, "Interface '%s' not found in dialgroup '%s'\n", inter.faces[j], grhead->name);
269                         }
270                 }
271         } else {
272                 ast_log(LOG_ERROR, "Unrecognized operation: %s\n", args.op);
273                 needrefresh = 0;
274         }
275         ao2_ref(grhead, -1);
276
277         if (needrefresh) {
278                 dialgroup_refreshdb(chan, args.group);
279         }
280
281         return 0;
282 }
283
284 static struct ast_custom_function dialgroup_function = {
285         .name = "DIALGROUP",
286         .read = dialgroup_read,
287         .write = dialgroup_write,
288 };
289
290 static int unload_module(void)
291 {
292         int res = ast_custom_function_unregister(&dialgroup_function);
293         ao2_ref(group_container, -1);
294         return res;
295 }
296
297 static int load_module(void)
298 {
299         struct ast_db_entry *dbtree, *tmp;
300         char groupname[AST_MAX_EXTENSION], *ptr;
301
302         if ((group_container = ao2_container_alloc(37, group_hash_fn, group_cmp_fn))) {
303                 /* Refresh groups from astdb */
304                 if ((dbtree = ast_db_gettree("dialgroup", NULL))) {
305                         for (tmp = dbtree; tmp; tmp = tmp->next) {
306                                 ast_copy_string(groupname, tmp->key, sizeof(groupname));
307                                 if ((ptr = strrchr(groupname, '/'))) {
308                                         ptr++;
309                                         dialgroup_write(NULL, "", ptr, tmp->data);
310                                 }
311                         }
312                         ast_db_freetree(dbtree);
313                 }
314                 return ast_custom_function_register(&dialgroup_function);
315         } else {
316                 return AST_MODULE_LOAD_DECLINE;
317         }
318 }
319
320 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialgroup dialplan function");