Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


func_dialgroup.c
Go to the documentation of this file.
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: 398757 $")
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 {
84 };
85 
86 struct group {
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  }
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;
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 
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;
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 
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 {
317  }
318 }
319 
320 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialgroup dialplan function");
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
static struct ast_custom_function dialgroup_function
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
static int unload_module(void)
#define ao2_iterator_next(arg1)
Definition: astobj2.h:1126
#define LOG_WARNING
Definition: logger.h:144
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: db.c:656
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
int value
Definition: syslog.c:39
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
Utility functions.
static struct ao2_container * group_container
struct ast_db_entry * next
Definition: astdb.h:31
static int group_hash_fn(const void *obj, const int flags)
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Data structure associated with a custom dialplan function.
Definition: pbx.h:95
char name[AST_CHANNEL_NAME]
#define AST_MAX_EXTENSION
Definition: channel.h:135
#define ao2_ref(o, delta)
Definition: astobj2.h:472
static int dialgroup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Core PBX routines and definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree If family is specified, only those keys will be returned...
Definition: db.c:631
static int load_module(void)
char name[AST_MAX_EXTENSION]
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
Definition: astdb.h:30
static int entry_hash_fn(const void *obj, const int flags)
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
static const char name[]
char data[0]
Definition: astdb.h:33
#define ast_free(a)
Definition: astmm.h:97
#define AST_CHANNEL_NAME
Definition: channel.h:137
static int entry_cmp_fn(void *obj1, void *name2, int flags)
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
static int dialgroup_refreshdb(struct ast_channel *chan, const char *cdialgroup)
static int group_cmp_fn(void *obj1, void *name2, int flags)
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: db.c:365
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
#define ast_realloc(a, b)
Definition: astmm.h:103
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static void group_destroy(void *vgroup)
const char * name
Definition: pbx.h:96
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
static int dialgroup_write(struct ast_channel *chan, const char *cmd, char *data, const char *cvalue)
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
Persistant data storage (akin to *doze registry)
struct ao2_container * entries
#define ao2_unlink(arg1, arg2)
Definition: astobj2.h:817
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
char * key
Definition: astdb.h:32
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:949