c3322e3f9b451ccf5967accdc1856fbbd711b492
[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_FILE_VERSION(__FILE__, "$Revision$")
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
169         return res;
170 }
171
172 static int dialgroup_refreshdb(struct ast_channel *chan, const char *cdialgroup)
173 {
174         int len = 500, res = 0;
175         char *buf = NULL;
176         char *dialgroup = ast_strdupa(cdialgroup);
177
178         do {
179                 len *= 2;
180                 buf = ast_realloc(buf, len);
181
182                 if ((res = dialgroup_read(chan, "", dialgroup, buf, len)) < 0) {
183                         ast_free(buf);
184                         return -1;
185                 }
186         } while (res == 1);
187
188         if (ast_strlen_zero(buf)) {
189                 ast_db_del("dialgroup", cdialgroup);
190         } else {
191                 ast_db_put("dialgroup", cdialgroup, buf);
192         }
193         ast_free(buf);
194         return 0;
195 }
196
197 static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)
198 {
199         struct group *grhead;
200         struct group_entry *entry;
201         int j, needrefresh = 1;
202         AST_DECLARE_APP_ARGS(args,
203                 AST_APP_ARG(group);
204                 AST_APP_ARG(op);
205         );
206         AST_DECLARE_APP_ARGS(inter,
207                 AST_APP_ARG(faces)[100];
208         );
209         char *value = ast_strdupa(cvalue);
210
211         AST_STANDARD_APP_ARGS(args, data);
212         AST_NONSTANDARD_APP_ARGS(inter, value, '&');
213
214         if (!(grhead = ao2_find(group_container, args.group, 0))) {
215                 /* Create group */
216                 grhead = ao2_alloc(sizeof(*grhead), group_destroy);
217                 if (!grhead)
218                         return -1;
219                 grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn);
220                 if (!grhead->entries) {
221                         ao2_ref(grhead, -1);
222                         return -1;
223                 }
224                 ast_copy_string(grhead->name, args.group, sizeof(grhead->name));
225                 ao2_link(group_container, grhead);
226         }
227
228         if (ast_strlen_zero(args.op)) {
229                 /* Wholesale replacement of the group */
230                 args.op = "add";
231
232                 /* Remove all existing */
233                 ao2_ref(grhead->entries, -1);
234                 if (!(grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn))) {
235                         ao2_unlink(group_container, grhead);
236                         ao2_ref(grhead, -1);
237                         return -1;
238                 }
239         }
240
241         if (strcasecmp(args.op, "add") == 0) {
242                 for (j = 0; j < inter.argc; j++) {
243                         /* Eliminate duplicates */
244                         if ((entry = ao2_find(grhead->entries, inter.faces[j], 0))) {
245                                 ao2_ref(entry, -1);
246                                 continue;
247                         }
248                         if ((entry = ao2_alloc(sizeof(*entry), NULL))) {
249                                 ast_copy_string(entry->name, inter.faces[j], sizeof(entry->name));
250                                 ao2_link(grhead->entries, entry);
251                                 ao2_ref(entry, -1);
252                         } else {
253                                 ast_log(LOG_WARNING, "Unable to add '%s' to dialgroup '%s'\n", inter.faces[j], grhead->name);
254                         }
255                 }
256         } else if (strncasecmp(args.op, "del", 3) == 0) {
257                 for (j = 0; j < inter.argc; j++) {
258                         if ((entry = ao2_find(grhead->entries, inter.faces[j], OBJ_UNLINK))) {
259                                 ao2_ref(entry, -1);
260                         } else {
261                                 ast_log(LOG_WARNING, "Interface '%s' not found in dialgroup '%s'\n", inter.faces[j], grhead->name);
262                         }
263                 }
264         } else {
265                 ast_log(LOG_ERROR, "Unrecognized operation: %s\n", args.op);
266                 needrefresh = 0;
267         }
268         ao2_ref(grhead, -1);
269
270         if (needrefresh) {
271                 dialgroup_refreshdb(chan, args.group);
272         }
273
274         return 0;
275 }
276
277 static struct ast_custom_function dialgroup_function = {
278         .name = "DIALGROUP",
279         .read = dialgroup_read,
280         .write = dialgroup_write,
281 };
282
283 static int unload_module(void)
284 {
285         int res = ast_custom_function_unregister(&dialgroup_function);
286         ao2_ref(group_container, -1);
287         return res;
288 }
289
290 static int load_module(void)
291 {
292         struct ast_db_entry *dbtree, *tmp;
293         char groupname[AST_MAX_EXTENSION], *ptr;
294
295         if ((group_container = ao2_container_alloc(37, group_hash_fn, group_cmp_fn))) {
296                 /* Refresh groups from astdb */
297                 if ((dbtree = ast_db_gettree("dialgroup", NULL))) {
298                         for (tmp = dbtree; tmp; tmp = tmp->next) {
299                                 ast_copy_string(groupname, tmp->key, sizeof(groupname));
300                                 if ((ptr = strrchr(groupname, '/'))) {
301                                         ptr++;
302                                         dialgroup_write(NULL, "", ptr, tmp->data);
303                                 }
304                         }
305                         ast_db_freetree(dbtree);
306                 }
307                 return ast_custom_function_register(&dialgroup_function);
308         } else {
309                 return AST_MODULE_LOAD_DECLINE;
310         }
311 }
312
313 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialgroup dialplan function");