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