Add missing terminator to ast_event_subscribe to fix a crash.
[asterisk-bristuff.git] / funcs / func_dialgroup.c
blob746784ba9de7df3220cd640de0f4ad585a6a1715
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Tilghman Lesher
6 * Tilghman Lesher <func_dialgroup__200709@the-tilghman.com>
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.
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.
19 /*! \file
21 * \brief Dial group dialplan function
23 * \author Tilghman Lesher <func_dialgroup__200709@the-tilghman.com>
25 * \ingroup functions
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <sys/stat.h>
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39 #include "asterisk/astobj2.h"
41 static struct ao2_container *group_container = NULL;
43 struct group_entry {
44 char name[AST_CHANNEL_NAME];
47 struct group {
48 char name[AST_MAX_EXTENSION];
49 struct ao2_container *entries;
52 static void group_destroy(void *vgroup)
54 struct group *group = vgroup;
55 ao2_ref(group->entries, -1);
58 static int group_hash_fn(const void *obj, const int flags)
60 const struct group *g = obj;
61 return ast_str_hash(g->name);
64 static int group_cmp_fn(void *obj1, void *name2, int flags)
66 struct group *g1 = obj1, *g2 = name2;
67 char *name = name2;
68 if (flags & OBJ_POINTER)
69 return strcmp(g1->name, g2->name) ? 0 : CMP_MATCH;
70 else
71 return strcmp(g1->name, name) ? 0 : CMP_MATCH;
74 static int entry_hash_fn(const void *obj, const int flags)
76 const struct group_entry *e = obj;
77 return ast_str_hash(e->name);
80 static int entry_cmp_fn(void *obj1, void *name2, int flags)
82 struct group_entry *e1 = obj1, *e2 = name2;
83 char *name = name2;
84 if (flags & OBJ_POINTER)
85 return strcmp(e1->name, e2->name) ? 0 : CMP_MATCH;
86 else
87 return strcmp(e1->name, name) ? 0 : CMP_MATCH;
90 static int dialgroup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
92 struct ao2_iterator i;
93 struct group *grhead = ao2_find(group_container, data, 0);
94 struct group_entry *entry;
95 size_t bufused = 0;
96 int trunc_warning = 0;
98 if (!grhead) {
99 ast_log(LOG_WARNING, "No such dialgroup '%s'\n", data);
100 return -1;
103 i = ao2_iterator_init(grhead->entries, 0);
104 while ((entry = ao2_iterator_next(&i))) {
105 int tmp = strlen(entry->name);
106 /* Ensure that we copy only complete names, not partials */
107 if (len - bufused > tmp + 2) {
108 if (bufused != 0)
109 buf[bufused++] = '&';
110 ast_copy_string(buf + bufused, entry->name, len - bufused);
111 bufused += tmp;
112 } else if (trunc_warning++ == 0)
113 ast_log(LOG_WARNING, "Dialgroup '%s' is too large. Truncating list.\n", data);
116 return 0;
119 static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)
121 struct group *grhead;
122 struct group_entry *entry;
123 int j;
124 AST_DECLARE_APP_ARGS(args,
125 AST_APP_ARG(group);
126 AST_APP_ARG(op);
128 AST_DECLARE_APP_ARGS(inter,
129 AST_APP_ARG(faces)[100];
131 char *value = ast_strdupa(cvalue);
133 AST_STANDARD_APP_ARGS(args, data);
134 AST_NONSTANDARD_APP_ARGS(inter, value, '&');
136 if (!(grhead = ao2_find(group_container, data, 0))) {
137 /* Create group */
138 grhead = ao2_alloc(sizeof(*grhead), group_destroy);
139 if (!grhead)
140 return -1;
141 grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn);
142 if (!grhead->entries) {
143 ao2_ref(grhead, -1);
144 return -1;
146 ast_copy_string(grhead->name, args.group, sizeof(grhead->name));
147 ao2_link(group_container, grhead);
150 if (ast_strlen_zero(args.op)) {
151 /* Wholesale replacement of the group */
152 args.op = "add";
154 /* Remove all existing */
155 ao2_ref(grhead->entries, -1);
156 if (!(grhead->entries = ao2_container_alloc(37, entry_hash_fn, entry_cmp_fn)))
157 return -1;
160 if (strcasecmp(args.op, "add") == 0) {
161 for (j = 0; j < inter.argc; j++) {
162 if ((entry = ao2_alloc(sizeof(*entry), NULL))) {
163 ast_copy_string(entry->name, inter.faces[j], sizeof(entry->name));
164 ao2_link(grhead->entries, entry);
165 } else
166 ast_log(LOG_WARNING, "Unable to add '%s' to dialgroup '%s'\n", inter.faces[j], grhead->name);
168 } else if (strncasecmp(args.op, "del", 3) == 0) {
169 for (j = 0; j < inter.argc; j++) {
170 if ((entry = ao2_find(grhead->entries, inter.faces[j], OBJ_UNLINK)))
171 ao2_ref(entry, -1);
172 else
173 ast_log(LOG_WARNING, "Interface '%s' not found in dialgroup '%s'\n", inter.faces[j], grhead->name);
175 } else
176 ast_log(LOG_ERROR, "Unrecognized operation: %s\n", args.op);
178 return 0;
181 static struct ast_custom_function dialgroup_function = {
182 .name = "DIALGROUP",
183 .synopsis = "Manages a group of users for dialing",
184 .syntax = "DIALGROUP(<group>[,op])",
185 .desc =
186 " DIALGROUP presents an interface meant to be used in concert with the Dial\n"
187 "application, by presenting a list of channels which should be dialled when\n"
188 "referenced.\n"
189 " When DIALGROUP is read from, the argument is interpreted as the particular\n"
190 "group for which a dial should be attempted. When DIALGROUP is written to\n"
191 "with no arguments, the entire list is replaced with the argument specified.\n"
192 "Other operations are as follows:\n"
193 " add - add a channel name or interface (write-only)\n"
194 " del - remove a channel name or interface (write-only)\n\n"
195 "Functionality is similar to a queue, except that when no interfaces are\n"
196 "available, execution may continue in the dialplan. This is useful when\n"
197 "you want certain people to be the first to answer any calls, with immediate\n"
198 "fallback to a queue when the front line people are busy or unavailable, but\n"
199 "you still want front line people to log in and out of that group, just like\n"
200 "a queue.\n",
201 .read = dialgroup_read,
202 .write = dialgroup_write,
205 static int unload_module(void)
207 int res = ast_custom_function_unregister(&dialgroup_function);
208 ao2_ref(group_container, -1);
209 return res;
212 static int load_module(void)
214 if ((group_container = ao2_container_alloc(37, group_hash_fn, group_cmp_fn)))
215 return ast_custom_function_register(&dialgroup_function);
216 else
217 return AST_MODULE_LOAD_DECLINE;
220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialgroup dialplan function");