func_dialgroup.c 8.5 KB

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