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