Don't do SIP contact/route DNS if we're not using the result.
[asterisk/asterisk.git] / apps / confbridge / conf_config_parser.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, Digium, Inc.
5  *
6  * David Vossel <dvossel@digium.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 ConfBridge config parser
22  *
23  * \author David Vossel <dvossel@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/logger.h"
34 #include "asterisk/config.h"
35 #include "asterisk/config_options.h"
36 #include "include/confbridge.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/bridging_features.h"
40 #include "asterisk/stringfields.h"
41 #include "asterisk/pbx.h"
42
43 struct confbridge_cfg {
44         struct ao2_container *bridge_profiles;
45         struct ao2_container *user_profiles;
46         struct ao2_container *menus;
47 };
48
49 static void *bridge_profile_alloc(const char *category);
50 static void *bridge_profile_find(struct ao2_container *container, const char *category);
51 static struct bridge_profile_sounds *bridge_profile_sounds_alloc(void);
52
53 static void bridge_profile_destructor(void *obj)
54 {
55         struct bridge_profile *b_profile = obj;
56         ao2_cleanup(b_profile->sounds);
57 }
58
59 static void *bridge_profile_alloc(const char *category)
60 {
61         struct bridge_profile *b_profile;
62
63         if (!(b_profile = ao2_alloc(sizeof(*b_profile), bridge_profile_destructor))) {
64                 return NULL;
65         }
66
67         if (!(b_profile->sounds = bridge_profile_sounds_alloc())) {
68                 ao2_ref(b_profile, -1);
69                 return NULL;
70         }
71
72         ast_copy_string(b_profile->name, category, sizeof(b_profile->name));
73
74         return b_profile;
75 }
76
77 static void *bridge_profile_find(struct ao2_container *container, const char *category)
78 {
79         return ao2_find(container, category, OBJ_KEY);
80 }
81
82 static struct aco_type bridge_type = {
83         .type = ACO_ITEM,
84         .category_match = ACO_BLACKLIST,
85         .category = "^general$",
86         .matchfield = "type",
87         .matchvalue = "bridge",
88         .item_alloc = bridge_profile_alloc,
89         .item_find = bridge_profile_find,
90         .item_offset = offsetof(struct confbridge_cfg, bridge_profiles),
91 };
92
93 static void *user_profile_alloc(const char *category);
94 static void *user_profile_find(struct ao2_container *container, const char *category);
95 static void user_profile_destructor(void *obj)
96 {
97         return;
98 }
99
100 static void *user_profile_alloc(const char *category)
101 {
102         struct user_profile *u_profile;
103
104         if (!(u_profile = ao2_alloc(sizeof(*u_profile), user_profile_destructor))) {
105                 return NULL;
106         }
107
108         ast_copy_string(u_profile->name, category, sizeof(u_profile->name));
109
110         return u_profile;
111 }
112
113 static void *user_profile_find(struct ao2_container *container, const char *category)
114 {
115         return ao2_find(container, category, OBJ_KEY);
116 }
117
118 static struct aco_type user_type = {
119         .type = ACO_ITEM,
120         .category_match = ACO_BLACKLIST,
121         .category = "^general$",
122         .matchfield = "type",
123         .matchvalue = "user",
124         .item_alloc = user_profile_alloc,
125         .item_find = user_profile_find,
126         .item_offset = offsetof(struct confbridge_cfg, user_profiles),
127 };
128
129 static void *menu_alloc(const char *category);
130 static void *menu_find(struct ao2_container *container, const char *category);
131 static void menu_destructor(void *obj);
132
133 static void *menu_alloc(const char *category)
134 {
135         struct conf_menu *menu;
136         if (!(menu = ao2_alloc(sizeof(*menu), menu_destructor))) {
137                 return NULL;
138         }
139         ast_copy_string(menu->name, category, sizeof(menu->name));
140         return menu;
141 }
142
143 static void *menu_find(struct ao2_container *container, const char *category)
144 {
145         return ao2_find(container, category, OBJ_KEY);
146 }
147
148 static struct aco_type menu_type = {
149         .type = ACO_ITEM,
150         .category_match = ACO_BLACKLIST,
151         .category = "^general$",
152         .matchfield = "type",
153         .matchvalue = "menu",
154         .item_alloc = menu_alloc,
155         .item_find = menu_find,
156         .item_offset = offsetof(struct confbridge_cfg, menus),
157 };
158
159 /* Used to pass to aco_option_register */
160 static struct aco_type *bridge_types[] = ACO_TYPES(&bridge_type);
161 static struct aco_type *menu_types[] = ACO_TYPES(&menu_type);
162 static struct aco_type *user_types[] = ACO_TYPES(&user_type);
163
164 /* The general category is reserved, but unused */
165 static struct aco_type general_type = {
166         .type = ACO_GLOBAL,
167         .category_match = ACO_WHITELIST,
168         .category = "^general$",
169 };
170
171 static struct aco_file confbridge_conf = {
172         .filename = "confbridge.conf",
173         .types = ACO_TYPES(&bridge_type, &user_type, &menu_type, &general_type),
174 };
175
176 static AO2_GLOBAL_OBJ_STATIC(cfg_handle);
177
178 static void *confbridge_cfg_alloc(void);
179
180 CONFIG_INFO_STANDARD(cfg_info, cfg_handle, confbridge_cfg_alloc,
181         .files = ACO_FILES(&confbridge_conf),
182 );
183
184 /*! bridge profile container functions */
185 static int bridge_cmp_cb(void *obj, void *arg, int flags)
186 {
187         const struct bridge_profile *entry1 = obj, *entry2 = arg;
188         const char *name = arg;
189         return (!strcasecmp(entry1->name, flags & OBJ_KEY ? name : entry2->name)) ?
190                 CMP_MATCH | CMP_STOP : 0;
191 }
192 static int bridge_hash_cb(const void *obj, const int flags)
193 {
194         const struct bridge_profile *b_profile = obj;
195         const char *name = obj;
196         return ast_str_case_hash(flags & OBJ_KEY ? name : b_profile->name);
197 }
198
199 /*! menu container functions */
200 static int menu_cmp_cb(void *obj, void *arg, int flags)
201 {
202         const struct conf_menu *entry1 = obj, *entry2 = arg;
203         const char *name = arg;
204         return (!strcasecmp(entry1->name, flags & OBJ_KEY ? name : entry2->name)) ?
205                 CMP_MATCH | CMP_STOP : 0;
206 }
207 static int menu_hash_cb(const void *obj, const int flags)
208 {
209         const struct conf_menu *menu = obj;
210         const char *name = obj;
211         return ast_str_case_hash(flags & OBJ_KEY ? name : menu->name);
212 }
213 static void menu_destructor(void *obj)
214 {
215         struct conf_menu *menu = obj;
216         struct conf_menu_entry *entry = NULL;
217
218         while ((entry = AST_LIST_REMOVE_HEAD(&menu->entries, entry))) {
219                 conf_menu_entry_destroy(entry);
220                 ast_free(entry);
221         }
222 }
223
224 /*! User profile container functions */
225 static int user_cmp_cb(void *obj, void *arg, int flags)
226 {
227         const struct user_profile *entry1 = obj, *entry2 = arg;
228         const char *name = arg;
229         return (!strcasecmp(entry1->name, flags & OBJ_KEY ? name : entry2->name)) ?
230                 CMP_MATCH | CMP_STOP : 0;
231 }
232 static int user_hash_cb(const void *obj, const int flags)
233 {
234         const struct user_profile *u_profile = obj;
235         const char *name = obj;
236         return ast_str_case_hash(flags & OBJ_KEY ? name : u_profile->name);
237 }
238
239 /*! Bridge Profile Sounds functions */
240 static void bridge_profile_sounds_destroy_cb(void *obj)
241 {
242         struct bridge_profile_sounds *sounds = obj;
243         ast_string_field_free_memory(sounds);
244 }
245
246 static struct bridge_profile_sounds *bridge_profile_sounds_alloc(void)
247 {
248         struct bridge_profile_sounds *sounds = ao2_alloc(sizeof(*sounds), bridge_profile_sounds_destroy_cb);
249
250         if (!sounds) {
251                 return NULL;
252         }
253         if (ast_string_field_init(sounds, 512)) {
254                 ao2_ref(sounds, -1);
255                 return NULL;
256         }
257
258         return sounds;
259 }
260
261 static int set_sound(const char *sound_name, const char *sound_file, struct bridge_profile *b_profile)
262 {
263         struct bridge_profile_sounds *sounds = b_profile->sounds;
264         if (ast_strlen_zero(sound_file)) {
265                 return -1;
266         }
267
268         if (!strcasecmp(sound_name, "sound_only_person")) {
269                 ast_string_field_set(sounds, onlyperson, sound_file);
270         } else if (!strcasecmp(sound_name, "sound_only_one")) {
271                 ast_string_field_set(sounds, onlyone, sound_file);
272         } else if (!strcasecmp(sound_name, "sound_has_joined")) {
273                 ast_string_field_set(sounds, hasjoin, sound_file);
274         } else if (!strcasecmp(sound_name, "sound_has_left")) {
275                 ast_string_field_set(sounds, hasleft, sound_file);
276         } else if (!strcasecmp(sound_name, "sound_kicked")) {
277                 ast_string_field_set(sounds, kicked, sound_file);
278         } else if (!strcasecmp(sound_name, "sound_muted")) {
279                 ast_string_field_set(sounds, muted, sound_file);
280         } else if (!strcasecmp(sound_name, "sound_unmuted")) {
281                 ast_string_field_set(sounds, unmuted, sound_file);
282         } else if (!strcasecmp(sound_name, "sound_there_are")) {
283                 ast_string_field_set(sounds, thereare, sound_file);
284         } else if (!strcasecmp(sound_name, "sound_other_in_party")) {
285                 ast_string_field_set(sounds, otherinparty, sound_file);
286         } else if (!strcasecmp(sound_name, "sound_place_into_conference")) {
287                 ast_string_field_set(sounds, placeintoconf, sound_file);
288         } else if (!strcasecmp(sound_name, "sound_wait_for_leader")) {
289                 ast_string_field_set(sounds, waitforleader, sound_file);
290         } else if (!strcasecmp(sound_name, "sound_leader_has_left")) {
291                 ast_string_field_set(sounds, leaderhasleft, sound_file);
292         } else if (!strcasecmp(sound_name, "sound_get_pin")) {
293                 ast_string_field_set(sounds, getpin, sound_file);
294         } else if (!strcasecmp(sound_name, "sound_invalid_pin")) {
295                 ast_string_field_set(sounds, invalidpin, sound_file);
296         } else if (!strcasecmp(sound_name, "sound_locked")) {
297                 ast_string_field_set(sounds, locked, sound_file);
298         } else if (!strcasecmp(sound_name, "sound_unlocked_now")) {
299                 ast_string_field_set(sounds, unlockednow, sound_file);
300         } else if (!strcasecmp(sound_name, "sound_locked_now")) {
301                 ast_string_field_set(sounds, lockednow, sound_file);
302         } else if (!strcasecmp(sound_name, "sound_error_menu")) {
303                 ast_string_field_set(sounds, errormenu, sound_file);
304         } else if (!strcasecmp(sound_name, "sound_join")) {
305                 ast_string_field_set(sounds, join, sound_file);
306         } else if (!strcasecmp(sound_name, "sound_leave")) {
307                 ast_string_field_set(sounds, leave, sound_file);
308         } else if (!strcasecmp(sound_name, "sound_participants_muted")) {
309                 ast_string_field_set(sounds, participantsmuted, sound_file);
310         } else if (!strcasecmp(sound_name, "sound_participants_unmuted")) {
311                 ast_string_field_set(sounds, participantsunmuted, sound_file);
312         } else {
313                 return -1;
314         }
315
316         return 0;
317 }
318
319 /*! CONFBRIDGE dialplan function functions and channel datastore. */
320 struct func_confbridge_data {
321         struct bridge_profile b_profile;
322         struct user_profile u_profile;
323         unsigned int b_usable:1; /*!< Tells if bridge profile is usable or not */
324         unsigned int u_usable:1; /*!< Tells if user profile is usable or not */
325 };
326 static void func_confbridge_destroy_cb(void *data)
327 {
328         struct func_confbridge_data *b_data = data;
329         conf_bridge_profile_destroy(&b_data->b_profile);
330         ast_free(b_data);
331 };
332 static const struct ast_datastore_info confbridge_datastore = {
333         .type = "confbridge",
334         .destroy = func_confbridge_destroy_cb
335 };
336 int func_confbridge_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
337 {
338         struct ast_datastore *datastore = NULL;
339         struct func_confbridge_data *b_data = NULL;
340         char *parse = NULL;
341         int new = 0;
342         struct ast_variable tmpvar = { 0, };
343         AST_DECLARE_APP_ARGS(args,
344                 AST_APP_ARG(type);
345                 AST_APP_ARG(option);
346         );
347
348         /* parse all the required arguments and make sure they exist. */
349         if (ast_strlen_zero(data) || ast_strlen_zero(value)) {
350                 return -1;
351         }
352         parse = ast_strdupa(data);
353         AST_STANDARD_APP_ARGS(args, parse);
354         if (ast_strlen_zero(args.type) || ast_strlen_zero(args.option)) {
355                 return -1;
356         }
357
358         ast_channel_lock(chan);
359         if (!(datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL))) {
360                 ast_channel_unlock(chan);
361
362                 if (!(datastore = ast_datastore_alloc(&confbridge_datastore, NULL))) {
363                         return 0;
364                 }
365                 if (!(b_data = ast_calloc(1, sizeof(*b_data)))) {
366                         ast_datastore_free(datastore);
367                         return 0;
368                 }
369                 if (!(b_data->b_profile.sounds = bridge_profile_sounds_alloc())) {
370                         ast_datastore_free(datastore);
371                         ast_free(b_data);
372                         return 0;
373                 }
374                 datastore->data = b_data;
375                 new = 1;
376         } else {
377                 ast_channel_unlock(chan);
378                 b_data = datastore->data;
379         }
380
381         tmpvar.name = args.option;
382         tmpvar.value = value;
383         tmpvar.file = "CONFBRIDGE";
384         /* SET(CONFBRIDGE(type,option)=value) */
385         if (!strcasecmp(args.type, "bridge") && !aco_process_var(&bridge_type, "dialplan", &tmpvar, &b_data->b_profile)) {
386                 b_data->b_usable = 1;
387         } else if (!strcasecmp(args.type, "user") && !aco_process_var(&user_type, "dialplan", &tmpvar, &b_data->u_profile)) {
388                 b_data->u_usable = 1;
389         } else {
390                 ast_log(LOG_WARNING, "Profile type \"%s\" can not be set in CONFBRIDGE function with option \"%s\" and value \"%s\"\n",
391                         args.type, args.option, value);
392                 goto cleanup_error;
393         }
394         if (new) {
395                 ast_channel_lock(chan);
396                 ast_channel_datastore_add(chan, datastore);
397                 ast_channel_unlock(chan);
398         }
399         return 0;
400
401 cleanup_error:
402         ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
403         if (new) {
404                 ast_datastore_free(datastore);
405         }
406         return -1;
407 }
408
409 static int add_action_to_menu_entry(struct conf_menu_entry *menu_entry, enum conf_menu_action_id id, char *databuf)
410 {
411         struct conf_menu_action *menu_action = ast_calloc(1, sizeof(*menu_action));
412
413         if (!menu_action) {
414                 return -1;
415         }
416         menu_action->id = id;
417
418         switch (id) {
419         case MENU_ACTION_NOOP:
420         case MENU_ACTION_TOGGLE_MUTE:
421         case MENU_ACTION_INCREASE_LISTENING:
422         case MENU_ACTION_DECREASE_LISTENING:
423         case MENU_ACTION_INCREASE_TALKING:
424         case MENU_ACTION_DECREASE_TALKING:
425         case MENU_ACTION_RESET_LISTENING:
426         case MENU_ACTION_RESET_TALKING:
427         case MENU_ACTION_ADMIN_TOGGLE_LOCK:
428         case MENU_ACTION_ADMIN_TOGGLE_MUTE_PARTICIPANTS:
429         case MENU_ACTION_PARTICIPANT_COUNT:
430         case MENU_ACTION_ADMIN_KICK_LAST:
431         case MENU_ACTION_LEAVE:
432         case MENU_ACTION_SET_SINGLE_VIDEO_SRC:
433         case MENU_ACTION_RELEASE_SINGLE_VIDEO_SRC:
434                 break;
435         case MENU_ACTION_PLAYBACK:
436         case MENU_ACTION_PLAYBACK_AND_CONTINUE:
437                 if (!(ast_strlen_zero(databuf))) {
438                         ast_copy_string(menu_action->data.playback_file, databuf, sizeof(menu_action->data.playback_file));
439                 } else {
440                         ast_free(menu_action);
441                         return -1;
442                 }
443                 break;
444         case MENU_ACTION_DIALPLAN_EXEC:
445                 if (!(ast_strlen_zero(databuf))) {
446                         AST_DECLARE_APP_ARGS(args,
447                                 AST_APP_ARG(context);
448                                 AST_APP_ARG(exten);
449                                 AST_APP_ARG(priority);
450                         );
451                         AST_STANDARD_APP_ARGS(args, databuf);
452                         if (!ast_strlen_zero(args.context)) {
453                                 ast_copy_string(menu_action->data.dialplan_args.context,
454                                         args.context,
455                                         sizeof(menu_action->data.dialplan_args.context));
456                         }
457                         if (!ast_strlen_zero(args.exten)) {
458                                 ast_copy_string(menu_action->data.dialplan_args.exten,
459                                         args.exten,
460                                         sizeof(menu_action->data.dialplan_args.exten));
461                         }
462                         menu_action->data.dialplan_args.priority = 1; /* 1 by default */
463                         if (!ast_strlen_zero(args.priority) &&
464                                 (sscanf(args.priority, "%30u", &menu_action->data.dialplan_args.priority) != 1)) {
465                                 /* invalid priority */
466                                 ast_free(menu_action);
467                                 return -1;
468                         }
469                 } else {
470                         ast_free(menu_action);
471                         return -1;
472                 }
473         };
474
475         AST_LIST_INSERT_TAIL(&menu_entry->actions, menu_action, action);
476
477         return 0;
478 }
479
480 static int add_menu_entry(struct conf_menu *menu, const char *dtmf, const char *action_names)
481 {
482         struct conf_menu_entry *menu_entry = NULL, *cur = NULL;
483         int res = 0;
484         char *tmp_action_names = ast_strdupa(action_names);
485         char *action = NULL;
486         char *action_args;
487         char *tmp;
488         char buf[PATH_MAX];
489         char *delimiter = ",";
490
491         if (!(menu_entry = ast_calloc(1, sizeof(*menu_entry)))) {
492                 return -1;
493         }
494
495         for (;;) {
496                 char *comma;
497                 char *startbrace;
498                 char *endbrace;
499                 unsigned int action_len;
500
501                 if (ast_strlen_zero(tmp_action_names)) {
502                         break;
503                 }
504                 startbrace = strchr(tmp_action_names, '(');
505                 endbrace = strchr(tmp_action_names, ')');
506                 comma = strchr(tmp_action_names, ',');
507
508                 /* If the next action has brackets with comma delimited arguments in it,
509                  * make the delimeter ')' instead of a comma to preserve the argments */
510                 if (startbrace && endbrace && comma && (comma > startbrace && comma < endbrace)) {
511                         delimiter = ")";
512                 } else {
513                         delimiter = ",";
514                 }
515
516                 if (!(action = strsep(&tmp_action_names, delimiter))) {
517                         break;
518                 }
519
520                 action = ast_strip(action);
521                 if (ast_strlen_zero(action)) {
522                         continue;
523                 }
524
525                 action_len = strlen(action);
526                 ast_copy_string(menu_entry->dtmf, dtmf, sizeof(menu_entry->dtmf));
527                 if (!strcasecmp(action, "toggle_mute")) {
528                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_TOGGLE_MUTE, NULL);
529                 } else if (!strcasecmp(action, "no_op")) {
530                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_NOOP, NULL);
531                 } else if (!strcasecmp(action, "increase_listening_volume")) {
532                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_INCREASE_LISTENING, NULL);
533                 } else if (!strcasecmp(action, "decrease_listening_volume")) {
534                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_DECREASE_LISTENING, NULL);
535                 } else if (!strcasecmp(action, "increase_talking_volume")) {
536                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_INCREASE_TALKING, NULL);
537                 } else if (!strcasecmp(action, "reset_listening_volume")) {
538                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_RESET_LISTENING, NULL);
539                 } else if (!strcasecmp(action, "reset_talking_volume")) {
540                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_RESET_TALKING, NULL);
541                 } else if (!strcasecmp(action, "decrease_talking_volume")) {
542                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_DECREASE_TALKING, NULL);
543                 } else if (!strcasecmp(action, "admin_toggle_conference_lock")) {
544                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_ADMIN_TOGGLE_LOCK, NULL);
545                 } else if (!strcasecmp(action, "admin_toggle_mute_participants")) {
546                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_ADMIN_TOGGLE_MUTE_PARTICIPANTS, NULL);
547                 } else if (!strcasecmp(action, "participant_count")) {
548                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_PARTICIPANT_COUNT, NULL);
549                 } else if (!strcasecmp(action, "admin_kick_last")) {
550                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_ADMIN_KICK_LAST, NULL);
551                 } else if (!strcasecmp(action, "leave_conference")) {
552                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_LEAVE, NULL);
553                 } else if (!strcasecmp(action, "set_as_single_video_src")) {
554                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_SET_SINGLE_VIDEO_SRC, NULL);
555                 } else if (!strcasecmp(action, "release_as_single_video_src")) {
556                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_RELEASE_SINGLE_VIDEO_SRC, NULL);
557                 } else if (!strncasecmp(action, "dialplan_exec(", 14)) {
558                         ast_copy_string(buf, action, sizeof(buf));
559                         action_args = buf;
560                         if ((action_args = strchr(action, '('))) {
561                                 action_args++;
562                         }
563                         /* it is possible that this argument may or may not
564                          * have a closing brace at this point, it all depends on if
565                          * comma delimited arguments were provided */
566                         if ((tmp = strchr(action, ')'))) {
567                                 *tmp = '\0';
568                         }
569                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_DIALPLAN_EXEC, action_args);
570                 } else if (action_len >= 21 && !strncasecmp(action, "playback_and_continue(", 22)) {
571                         ast_copy_string(buf, action, sizeof(buf));
572                         action_args = buf;
573                         if ((action_args = strchr(action, '(')) && (tmp = strrchr(action_args, ')'))) {
574                                 *tmp = '\0';
575                                 action_args++;
576                         }
577                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_PLAYBACK_AND_CONTINUE, action_args);
578                 } else if (action_len >= 8 && !strncasecmp(action, "playback(", 9)) {
579                         ast_copy_string(buf, action, sizeof(buf));
580                         action_args = buf;
581                         if ((action_args = strchr(action, '(')) && (tmp = strrchr(action_args, ')'))) {
582                                 *tmp = '\0';
583                                 action_args++;
584                         }
585                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_PLAYBACK, action_args);
586                 }
587         }
588
589         /* if adding any of the actions failed, bail */
590         if (res) {
591                 struct conf_menu_action *menu_action;
592                 while ((menu_action = AST_LIST_REMOVE_HEAD(&menu_entry->actions, action))) {
593                         ast_free(menu_action);
594                 }
595                 ast_free(menu_entry);
596                 return -1;
597         }
598
599         /* remove any list entry with an identical DTMF sequence for overrides */
600         AST_LIST_TRAVERSE_SAFE_BEGIN(&menu->entries, cur, entry) {
601                 if (!strcasecmp(cur->dtmf, menu_entry->dtmf)) {
602                         AST_LIST_REMOVE_CURRENT(entry);
603                         ast_free(cur);
604                         break;
605                 }
606         }
607         AST_LIST_TRAVERSE_SAFE_END;
608
609         AST_LIST_INSERT_TAIL(&menu->entries, menu_entry, entry);
610
611         return 0;
612 }
613
614 static char *complete_user_profile_name(const char *line, const char *word, int pos, int state)
615 {
616         int which = 0;
617         char *res = NULL;
618         int wordlen = strlen(word);
619         struct ao2_iterator i;
620         struct user_profile *u_profile = NULL;
621         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
622
623         if (!cfg) {
624                 return NULL;
625         }
626
627         i = ao2_iterator_init(cfg->user_profiles, 0);
628         while ((u_profile = ao2_iterator_next(&i))) {
629                 if (!strncasecmp(u_profile->name, word, wordlen) && ++which > state) {
630                         res = ast_strdup(u_profile->name);
631                         ao2_ref(u_profile, -1);
632                         break;
633                 }
634                 ao2_ref(u_profile, -1);
635         }
636         ao2_iterator_destroy(&i);
637
638         return res;
639 }
640
641 static char *handle_cli_confbridge_show_user_profiles(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
642 {
643         struct ao2_iterator it;
644         struct user_profile *u_profile;
645         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
646
647         switch (cmd) {
648         case CLI_INIT:
649                 e->command = "confbridge show profile users";
650                 e->usage =
651                         "Usage confbridge show profile users\n";
652                 return NULL;
653         case CLI_GENERATE:
654                 return NULL;
655         }
656
657         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
658                 return NULL;
659         }
660
661         ast_cli(a->fd,"--------- User Profiles -----------\n");
662         ao2_lock(cfg->user_profiles);
663         it = ao2_iterator_init(cfg->user_profiles, 0);
664         while ((u_profile = ao2_iterator_next(&it))) {
665                 ast_cli(a->fd,"%s\n", u_profile->name);
666                 ao2_ref(u_profile, -1);
667         }
668         ao2_iterator_destroy(&it);
669         ao2_unlock(cfg->user_profiles);
670
671         return CLI_SUCCESS;
672 }
673 static char *handle_cli_confbridge_show_user_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
674 {
675         struct user_profile u_profile;
676
677         switch (cmd) {
678         case CLI_INIT:
679                 e->command = "confbridge show profile user";
680                 e->usage =
681                         "Usage confbridge show profile user [<profile name>]\n";
682                 return NULL;
683         case CLI_GENERATE:
684                 if (a->pos == 4) {
685                         return complete_user_profile_name(a->line, a->word, a->pos, a->n);
686                 }
687                 return NULL;
688         }
689
690         if (a->argc != 5) {
691                 return CLI_SHOWUSAGE;
692         }
693
694         if (!(conf_find_user_profile(NULL, a->argv[4], &u_profile))) {
695                 ast_cli(a->fd, "No conference user profile named '%s' found!\n", a->argv[4]);
696                 return CLI_SUCCESS;
697         }
698
699         ast_cli(a->fd,"--------------------------------------------\n");
700         ast_cli(a->fd,"Name:                    %s\n",
701                 u_profile.name);
702         ast_cli(a->fd,"Admin:                   %s\n",
703                 u_profile.flags & USER_OPT_ADMIN ?
704                 "true" : "false");
705         ast_cli(a->fd,"Marked User:             %s\n",
706                 u_profile.flags & USER_OPT_MARKEDUSER ?
707                 "true" : "false");
708         ast_cli(a->fd,"Start Muted:             %s\n",
709                 u_profile.flags & USER_OPT_STARTMUTED?
710                 "true" : "false");
711         ast_cli(a->fd,"MOH When Empty:          %s\n",
712                 u_profile.flags & USER_OPT_MUSICONHOLD ?
713                 "enabled" : "disabled");
714         ast_cli(a->fd,"MOH Class:               %s\n",
715                 ast_strlen_zero(u_profile.moh_class) ?
716                 "default" : u_profile.moh_class);
717         ast_cli(a->fd,"Announcement:            %s\n",
718                 u_profile.announcement);
719         ast_cli(a->fd,"Quiet:                   %s\n",
720                 u_profile.flags & USER_OPT_QUIET ?
721                 "enabled" : "disabled");
722         ast_cli(a->fd,"Wait Marked:             %s\n",
723                 u_profile.flags & USER_OPT_WAITMARKED ?
724                 "enabled" : "disabled");
725         ast_cli(a->fd,"END Marked:              %s\n",
726                 u_profile.flags & USER_OPT_ENDMARKED ?
727                 "enabled" : "disabled");
728         ast_cli(a->fd,"Drop_silence:            %s\n",
729                 u_profile.flags & USER_OPT_DROP_SILENCE ?
730                 "enabled" : "disabled");
731         ast_cli(a->fd,"Silence Threshold:       %dms\n",
732                 u_profile.silence_threshold);
733         ast_cli(a->fd,"Talking Threshold:       %dms\n",
734                 u_profile.talking_threshold);
735         ast_cli(a->fd,"Denoise:                 %s\n",
736                 u_profile.flags & USER_OPT_DENOISE ?
737                 "enabled" : "disabled");
738         ast_cli(a->fd,"Jitterbuffer:            %s\n",
739                 u_profile.flags & USER_OPT_JITTERBUFFER ?
740                 "enabled" : "disabled");
741         ast_cli(a->fd,"Talk Detect Events:      %s\n",
742                 u_profile.flags & USER_OPT_TALKER_DETECT ?
743                 "enabled" : "disabled");
744         ast_cli(a->fd,"DTMF Pass Through:       %s\n",
745                 u_profile.flags & USER_OPT_DTMF_PASS ?
746                 "enabled" : "disabled");
747         ast_cli(a->fd,"PIN:                     %s\n",
748                 ast_strlen_zero(u_profile.pin) ?
749                 "None" : u_profile.pin);
750         ast_cli(a->fd,"Announce User Count:     %s\n",
751                 u_profile.flags & USER_OPT_ANNOUNCEUSERCOUNT ?
752                 "enabled" : "disabled");
753         ast_cli(a->fd,"Announce join/leave:     %s\n",
754                 u_profile.flags & USER_OPT_ANNOUNCE_JOIN_LEAVE ?
755                 "enabled" : "disabled");
756         ast_cli(a->fd,"Announce User Count all: %s\n",
757                 u_profile.flags & USER_OPT_ANNOUNCEUSERCOUNTALL ?
758                 "enabled" : "disabled");
759                 ast_cli(a->fd,"\n");
760
761         return CLI_SUCCESS;
762 }
763
764 static char *complete_bridge_profile_name(const char *line, const char *word, int pos, int state)
765 {
766         int which = 0;
767         char *res = NULL;
768         int wordlen = strlen(word);
769         struct ao2_iterator i;
770         struct bridge_profile *b_profile = NULL;
771         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
772
773         if (!cfg) {
774                 return NULL;
775         }
776
777         i = ao2_iterator_init(cfg->bridge_profiles, 0);
778         while ((b_profile = ao2_iterator_next(&i))) {
779                 if (!strncasecmp(b_profile->name, word, wordlen) && ++which > state) {
780                         res = ast_strdup(b_profile->name);
781                         ao2_ref(b_profile, -1);
782                         break;
783                 }
784                 ao2_ref(b_profile, -1);
785         }
786         ao2_iterator_destroy(&i);
787
788         return res;
789 }
790
791 static char *handle_cli_confbridge_show_bridge_profiles(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
792 {
793         struct ao2_iterator it;
794         struct bridge_profile *b_profile;
795         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
796
797         switch (cmd) {
798         case CLI_INIT:
799                 e->command = "confbridge show profile bridges";
800                 e->usage =
801                         "Usage confbridge show profile bridges\n";
802                 return NULL;
803         case CLI_GENERATE:
804                 return NULL;
805         }
806
807         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
808                 return NULL;
809         }
810
811         ast_cli(a->fd,"--------- Bridge Profiles -----------\n");
812         ao2_lock(cfg->bridge_profiles);
813         it = ao2_iterator_init(cfg->bridge_profiles, 0);
814         while ((b_profile = ao2_iterator_next(&it))) {
815                 ast_cli(a->fd,"%s\n", b_profile->name);
816                 ao2_ref(b_profile, -1);
817         }
818         ao2_iterator_destroy(&it);
819         ao2_unlock(cfg->bridge_profiles);
820
821         return CLI_SUCCESS;
822 }
823
824 static char *handle_cli_confbridge_show_bridge_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
825 {
826         struct bridge_profile b_profile;
827         char tmp[64];
828
829         switch (cmd) {
830         case CLI_INIT:
831                 e->command = "confbridge show profile bridge";
832                 e->usage =
833                         "Usage confbridge show profile bridge <profile name>\n";
834                 return NULL;
835         case CLI_GENERATE:
836                 if (a->pos == 4) {
837                         return complete_bridge_profile_name(a->line, a->word, a->pos, a->n);
838                 }
839                 return NULL;
840         }
841
842         if (a->argc != 5) {
843                 return CLI_SHOWUSAGE;
844         }
845
846         if (!(conf_find_bridge_profile(NULL, a->argv[4], &b_profile))) {
847                 ast_cli(a->fd, "No conference bridge profile named '%s' found!\n", a->argv[4]);
848                 return CLI_SUCCESS;
849         }
850
851         ast_cli(a->fd,"--------------------------------------------\n");
852         ast_cli(a->fd,"Name:                 %s\n", b_profile.name);
853
854         if (b_profile.internal_sample_rate) {
855                 snprintf(tmp, sizeof(tmp), "%d", b_profile.internal_sample_rate);
856         } else {
857                 ast_copy_string(tmp, "auto", sizeof(tmp));
858         }
859         ast_cli(a->fd,"Internal Sample Rate: %s\n", tmp);
860
861         if (b_profile.mix_interval) {
862                 ast_cli(a->fd,"Mixing Interval:      %d\n", b_profile.mix_interval);
863         } else {
864                 ast_cli(a->fd,"Mixing Interval:      Default 20ms\n");
865         }
866
867         ast_cli(a->fd,"Record Conference:    %s\n",
868                 b_profile.flags & BRIDGE_OPT_RECORD_CONFERENCE ?
869                 "yes" : "no");
870
871         ast_cli(a->fd,"Record File:          %s\n",
872                 ast_strlen_zero(b_profile.rec_file) ? "Auto Generated" :
873                 b_profile.rec_file);
874
875         if (b_profile.max_members) {
876                 ast_cli(a->fd,"Max Members:          %d\n", b_profile.max_members);
877         } else {
878                 ast_cli(a->fd,"Max Members:          No Limit\n");
879         }
880
881         if (b_profile.flags & BRIDGE_OPT_VIDEO_SRC_LAST_MARKED) {
882                 ast_cli(a->fd, "Video Mode:           last_marked\n");
883         } else if (b_profile.flags & BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED) {
884                 ast_cli(a->fd, "Video Mode:           first_marked\n");
885         } else if (b_profile.flags & BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER) {
886                 ast_cli(a->fd, "Video Mode:           follow_talker\n");
887         } else {
888                 ast_cli(a->fd, "Video Mode:           no video\n");
889         }
890
891         ast_cli(a->fd,"sound_join:           %s\n", conf_get_sound(CONF_SOUND_JOIN, b_profile.sounds));
892         ast_cli(a->fd,"sound_leave:          %s\n", conf_get_sound(CONF_SOUND_LEAVE, b_profile.sounds));
893         ast_cli(a->fd,"sound_only_person:    %s\n", conf_get_sound(CONF_SOUND_ONLY_PERSON, b_profile.sounds));
894         ast_cli(a->fd,"sound_has_joined:     %s\n", conf_get_sound(CONF_SOUND_HAS_JOINED, b_profile.sounds));
895         ast_cli(a->fd,"sound_has_left:       %s\n", conf_get_sound(CONF_SOUND_HAS_LEFT, b_profile.sounds));
896         ast_cli(a->fd,"sound_kicked:         %s\n", conf_get_sound(CONF_SOUND_KICKED, b_profile.sounds));
897         ast_cli(a->fd,"sound_muted:          %s\n", conf_get_sound(CONF_SOUND_MUTED, b_profile.sounds));
898         ast_cli(a->fd,"sound_unmuted:        %s\n", conf_get_sound(CONF_SOUND_UNMUTED, b_profile.sounds));
899         ast_cli(a->fd,"sound_there_are:      %s\n", conf_get_sound(CONF_SOUND_THERE_ARE, b_profile.sounds));
900         ast_cli(a->fd,"sound_other_in_party: %s\n", conf_get_sound(CONF_SOUND_OTHER_IN_PARTY, b_profile.sounds));
901         ast_cli(a->fd,"sound_place_into_conference: %s\n", conf_get_sound(CONF_SOUND_PLACE_IN_CONF, b_profile.sounds));
902         ast_cli(a->fd,"sound_wait_for_leader:       %s\n", conf_get_sound(CONF_SOUND_WAIT_FOR_LEADER, b_profile.sounds));
903         ast_cli(a->fd,"sound_leader_has_left:       %s\n", conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, b_profile.sounds));
904         ast_cli(a->fd,"sound_get_pin:        %s\n", conf_get_sound(CONF_SOUND_GET_PIN, b_profile.sounds));
905         ast_cli(a->fd,"sound_invalid_pin:    %s\n", conf_get_sound(CONF_SOUND_INVALID_PIN, b_profile.sounds));
906         ast_cli(a->fd,"sound_locked:         %s\n", conf_get_sound(CONF_SOUND_LOCKED, b_profile.sounds));
907         ast_cli(a->fd,"sound_unlocked_now:   %s\n", conf_get_sound(CONF_SOUND_UNLOCKED_NOW, b_profile.sounds));
908         ast_cli(a->fd,"sound_lockednow:      %s\n", conf_get_sound(CONF_SOUND_LOCKED_NOW, b_profile.sounds));
909         ast_cli(a->fd,"sound_error_menu:     %s\n", conf_get_sound(CONF_SOUND_ERROR_MENU, b_profile.sounds));
910         ast_cli(a->fd,"sound_participants_muted:     %s\n", conf_get_sound(CONF_SOUND_PARTICIPANTS_MUTED, b_profile.sounds));
911         ast_cli(a->fd,"sound_participants_unmuted:     %s\n", conf_get_sound(CONF_SOUND_PARTICIPANTS_UNMUTED, b_profile.sounds));
912         ast_cli(a->fd,"\n");
913
914         conf_bridge_profile_destroy(&b_profile);
915         return CLI_SUCCESS;
916 }
917
918 static char *complete_menu_name(const char *line, const char *word, int pos, int state)
919 {
920         int which = 0;
921         char *res = NULL;
922         int wordlen = strlen(word);
923         struct ao2_iterator i;
924         struct conf_menu *menu = NULL;
925         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
926
927         if (!cfg) {
928                 return NULL;
929         }
930
931         i = ao2_iterator_init(cfg->menus, 0);
932         while ((menu = ao2_iterator_next(&i))) {
933                 if (!strncasecmp(menu->name, word, wordlen) && ++which > state) {
934                         res = ast_strdup(menu->name);
935                         ao2_ref(menu, -1);
936                         break;
937                 }
938                 ao2_ref(menu, -1);
939         }
940         ao2_iterator_destroy(&i);
941
942         return res;
943 }
944
945 static char *handle_cli_confbridge_show_menus(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
946 {
947         struct ao2_iterator it;
948         struct conf_menu *menu;
949         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
950
951         switch (cmd) {
952         case CLI_INIT:
953                 e->command = "confbridge show menus";
954                 e->usage =
955                         "Usage confbridge show profile menus\n";
956                 return NULL;
957         case CLI_GENERATE:
958                 return NULL;
959         }
960
961         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
962                 return NULL;
963         }
964
965         ast_cli(a->fd,"--------- Menus -----------\n");
966         ao2_lock(cfg->menus);
967         it = ao2_iterator_init(cfg->menus, 0);
968         while ((menu = ao2_iterator_next(&it))) {
969                 ast_cli(a->fd,"%s\n", menu->name);
970                 ao2_ref(menu, -1);
971         }
972         ao2_iterator_destroy(&it);
973         ao2_unlock(cfg->menus);
974
975         return CLI_SUCCESS;
976 }
977
978 static char *handle_cli_confbridge_show_menu(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
979 {
980         RAII_VAR(struct conf_menu *, menu, NULL, ao2_cleanup);
981         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
982         struct conf_menu_entry *menu_entry = NULL;
983         struct conf_menu_action *menu_action = NULL;
984
985         switch (cmd) {
986         case CLI_INIT:
987                 e->command = "confbridge show menu";
988                 e->usage =
989                         "Usage confbridge show menu [<menu name>]\n";
990                 return NULL;
991         case CLI_GENERATE:
992                 if (a->pos == 3) {
993                         return complete_menu_name(a->line, a->word, a->pos, a->n);
994                 }
995                 return NULL;
996         }
997
998         if (a->argc != 4) {
999                 return CLI_SHOWUSAGE;
1000         }
1001
1002         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
1003                 return NULL;
1004         }
1005
1006         if (!(menu = menu_find(cfg->menus, a->argv[3]))) {
1007                 ast_cli(a->fd, "No conference menu named '%s' found!\n", a->argv[3]);
1008                 return CLI_SUCCESS;
1009         }
1010         ao2_lock(menu);
1011
1012         ast_cli(a->fd,"Name: %s\n", menu->name);
1013         AST_LIST_TRAVERSE(&menu->entries, menu_entry, entry) {
1014                 int action_num = 0;
1015                 ast_cli(a->fd, "%s=", menu_entry->dtmf);
1016                 AST_LIST_TRAVERSE(&menu_entry->actions, menu_action, action) {
1017                         if (action_num) {
1018                                 ast_cli(a->fd, ", ");
1019                         }
1020                         switch (menu_action->id) {
1021                         case MENU_ACTION_TOGGLE_MUTE:
1022                                 ast_cli(a->fd, "toggle_mute");
1023                                 break;
1024                         case MENU_ACTION_NOOP:
1025                                 ast_cli(a->fd, "no_op");
1026                                 break;
1027                         case MENU_ACTION_INCREASE_LISTENING:
1028                                 ast_cli(a->fd, "increase_listening_volume");
1029                                 break;
1030                         case MENU_ACTION_DECREASE_LISTENING:
1031                                 ast_cli(a->fd, "decrease_listening_volume");
1032                                 break;
1033                         case MENU_ACTION_RESET_LISTENING:
1034                                 ast_cli(a->fd, "reset_listening_volume");
1035                                 break;
1036                         case MENU_ACTION_RESET_TALKING:
1037                                 ast_cli(a->fd, "reset_talking_volume");
1038                                 break;
1039                         case MENU_ACTION_INCREASE_TALKING:
1040                                 ast_cli(a->fd, "increase_talking_volume");
1041                                 break;
1042                         case MENU_ACTION_DECREASE_TALKING:
1043                                 ast_cli(a->fd, "decrease_talking_volume");
1044                                 break;
1045                         case MENU_ACTION_PLAYBACK:
1046                                 ast_cli(a->fd, "playback(%s)", menu_action->data.playback_file);
1047                                 break;
1048                         case MENU_ACTION_PLAYBACK_AND_CONTINUE:
1049                                 ast_cli(a->fd, "playback_and_continue(%s)", menu_action->data.playback_file);
1050                                 break;
1051                         case MENU_ACTION_DIALPLAN_EXEC:
1052                                 ast_cli(a->fd, "dialplan_exec(%s,%s,%d)",
1053                                         menu_action->data.dialplan_args.context,
1054                                         menu_action->data.dialplan_args.exten,
1055                                         menu_action->data.dialplan_args.priority);
1056                                 break;
1057                         case MENU_ACTION_ADMIN_TOGGLE_LOCK:
1058                                 ast_cli(a->fd, "admin_toggle_conference_lock");
1059                                 break;
1060                         case MENU_ACTION_ADMIN_TOGGLE_MUTE_PARTICIPANTS:
1061                                 ast_cli(a->fd, "admin_toggle_mute_participants");
1062                                 break;
1063                         case MENU_ACTION_PARTICIPANT_COUNT:
1064                                 ast_cli(a->fd, "participant_count");
1065                                 break;
1066                         case MENU_ACTION_ADMIN_KICK_LAST:
1067                                 ast_cli(a->fd, "admin_kick_last");
1068                                 break;
1069                         case MENU_ACTION_LEAVE:
1070                                 ast_cli(a->fd, "leave_conference");
1071                                 break;
1072                         case MENU_ACTION_SET_SINGLE_VIDEO_SRC:
1073                                 ast_cli(a->fd, "set_as_single_video_src");
1074                                 break;
1075                         case MENU_ACTION_RELEASE_SINGLE_VIDEO_SRC:
1076                                 ast_cli(a->fd, "release_as_single_video_src");
1077                                 break;
1078                         }
1079                         action_num++;
1080                 }
1081                 ast_cli(a->fd,"\n");
1082         }
1083
1084
1085         ao2_unlock(menu);
1086         return CLI_SUCCESS;
1087 }
1088
1089 static struct ast_cli_entry cli_confbridge_parser[] = {
1090         AST_CLI_DEFINE(handle_cli_confbridge_show_user_profile, "Show a conference user profile."),
1091         AST_CLI_DEFINE(handle_cli_confbridge_show_bridge_profile, "Show a conference bridge profile."),
1092         AST_CLI_DEFINE(handle_cli_confbridge_show_menu, "Show a conference menu"),
1093         AST_CLI_DEFINE(handle_cli_confbridge_show_user_profiles, "Show a list of conference user profiles."),
1094         AST_CLI_DEFINE(handle_cli_confbridge_show_bridge_profiles, "Show a list of conference bridge profiles."),
1095         AST_CLI_DEFINE(handle_cli_confbridge_show_menus, "Show a list of conference menus"),
1096
1097 };
1098
1099 static void confbridge_cfg_destructor(void *obj)
1100 {
1101         struct confbridge_cfg *cfg = obj;
1102         ao2_cleanup(cfg->user_profiles);
1103         ao2_cleanup(cfg->bridge_profiles);
1104         ao2_cleanup(cfg->menus);
1105 }
1106
1107 void *confbridge_cfg_alloc(void)
1108 {
1109         struct confbridge_cfg *cfg;
1110
1111         if (!(cfg = ao2_alloc(sizeof(*cfg), confbridge_cfg_destructor))) {
1112                 return NULL;
1113         }
1114
1115         if (!(cfg->user_profiles = ao2_container_alloc(283, user_hash_cb, user_cmp_cb))) {
1116                 goto error;
1117         }
1118
1119         if (!(cfg->bridge_profiles = ao2_container_alloc(283, bridge_hash_cb, bridge_cmp_cb))) {
1120                 goto error;
1121         }
1122
1123         if (!(cfg->menus = ao2_container_alloc(283, menu_hash_cb, menu_cmp_cb))) {
1124                 goto error;
1125         }
1126
1127         return cfg;
1128 error:
1129         ao2_ref(cfg, -1);
1130         return NULL;
1131 }
1132
1133 static int announce_user_count_all_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1134 {
1135         struct user_profile *u_profile = obj;
1136
1137         if (strcasecmp(var->name, "announce_user_count_all")) {
1138                 return -1;
1139         }
1140         if (ast_true(var->value)) {
1141                 u_profile->flags = u_profile->flags | USER_OPT_ANNOUNCEUSERCOUNTALL;
1142         } else if (ast_false(var->value)) {
1143                 u_profile->flags = u_profile->flags & ~USER_OPT_ANNOUNCEUSERCOUNTALL;
1144         } else if (sscanf(var->value, "%30u", &u_profile->announce_user_count_all_after) == 1) {
1145                 u_profile->flags = u_profile->flags | USER_OPT_ANNOUNCEUSERCOUNTALL;
1146         } else {
1147                 return -1;
1148         }
1149         return 0;
1150 }
1151
1152 static int mix_interval_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1153 {
1154         struct bridge_profile *b_profile = obj;
1155
1156         if (strcasecmp(var->name, "mixing_interval")) {
1157                 return -1;
1158         }
1159         if (sscanf(var->value, "%30u", &b_profile->mix_interval) != 1) {
1160                 return -1;
1161         }
1162         switch (b_profile->mix_interval) {
1163         case 10:
1164         case 20:
1165         case 40:
1166         case 80:
1167                 return 0;
1168         default:
1169                 return -1;
1170         }
1171 }
1172
1173 static int video_mode_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1174 {
1175         struct bridge_profile *b_profile = obj;
1176
1177         if (strcasecmp(var->name, "video_mode")) {
1178                 return -1;
1179         }
1180         if (!strcasecmp(var->value, "first_marked")) {
1181                 ast_set_flag(b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED);
1182         } else if (!strcasecmp(var->value, "last_marked")) {
1183                 ast_set_flag(b_profile, BRIDGE_OPT_VIDEO_SRC_LAST_MARKED);
1184         } else if (!strcasecmp(var->value, "follow_talker")) {
1185                 ast_set_flag(b_profile, BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER);
1186         } else if (!strcasecmp(var->value, "none")) {
1187                 return 0;
1188         } else {
1189                 return -1;
1190         }
1191         return 0;
1192 }
1193
1194 static int user_template_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1195 {
1196         struct user_profile *u_profile = obj;
1197
1198         return conf_find_user_profile(NULL, var->value, u_profile) ? 0 : -1;
1199 }
1200
1201 static int bridge_template_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1202 {
1203         struct bridge_profile *b_profile = obj;
1204         struct bridge_profile_sounds *sounds = bridge_profile_sounds_alloc();
1205         struct bridge_profile_sounds *oldsounds = b_profile->sounds;
1206
1207         if (!sounds) {
1208                 return -1;
1209         }
1210         if (!(conf_find_bridge_profile(NULL, var->value, b_profile))) {
1211                 ao2_ref(sounds, -1);
1212                 return -1;
1213         }
1214         /* Using a bridge profile as a template is a little complicated due to the sounds. Since the sounds
1215          * structure of a dynamic profile will need to be altered, a completely new sounds structure must be
1216          * created instead of simply holding a reference to the one built by the config file. */
1217         ast_string_field_set(sounds, onlyperson, b_profile->sounds->onlyperson);
1218         ast_string_field_set(sounds, hasjoin, b_profile->sounds->hasjoin);
1219         ast_string_field_set(sounds, hasleft, b_profile->sounds->hasleft);
1220         ast_string_field_set(sounds, kicked, b_profile->sounds->kicked);
1221         ast_string_field_set(sounds, muted, b_profile->sounds->muted);
1222         ast_string_field_set(sounds, unmuted, b_profile->sounds->unmuted);
1223         ast_string_field_set(sounds, thereare, b_profile->sounds->thereare);
1224         ast_string_field_set(sounds, otherinparty, b_profile->sounds->otherinparty);
1225         ast_string_field_set(sounds, placeintoconf, b_profile->sounds->placeintoconf);
1226         ast_string_field_set(sounds, waitforleader, b_profile->sounds->waitforleader);
1227         ast_string_field_set(sounds, leaderhasleft, b_profile->sounds->leaderhasleft);
1228         ast_string_field_set(sounds, getpin, b_profile->sounds->getpin);
1229         ast_string_field_set(sounds, invalidpin, b_profile->sounds->invalidpin);
1230         ast_string_field_set(sounds, locked, b_profile->sounds->locked);
1231         ast_string_field_set(sounds, unlockednow, b_profile->sounds->unlockednow);
1232         ast_string_field_set(sounds, lockednow, b_profile->sounds->lockednow);
1233         ast_string_field_set(sounds, errormenu, b_profile->sounds->errormenu);
1234         ast_string_field_set(sounds, participantsmuted, b_profile->sounds->participantsmuted);
1235         ast_string_field_set(sounds, participantsunmuted, b_profile->sounds->participantsunmuted);
1236
1237         ao2_ref(b_profile->sounds, -1); /* sounds struct copied over to it from the template by reference only. */
1238         ao2_ref(oldsounds,-1);    /* original sounds struct we don't need anymore */
1239         b_profile->sounds = sounds;     /* the new sounds struct that is a deep copy of the one from the template. */
1240
1241         return 0;
1242 }
1243
1244 static int sound_option_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1245 {
1246         set_sound(var->name, var->value, obj);
1247         return 0;
1248 }
1249
1250 static int menu_option_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1251 {
1252         add_menu_entry(obj, var->name, var->value);
1253         return 0;
1254 }
1255
1256 int conf_load_config(int reload)
1257 {
1258         if (!reload) {
1259                 if (aco_info_init(&cfg_info)) {
1260                         return -1;
1261                 }
1262         }
1263
1264         /* User options */
1265         aco_option_register(&cfg_info, "type", ACO_EXACT, user_types, NULL, OPT_NOOP_T, 0, 0);
1266         aco_option_register(&cfg_info, "type", ACO_EXACT, bridge_types, NULL, OPT_NOOP_T, 0, 0);
1267         aco_option_register(&cfg_info, "type", ACO_EXACT, menu_types, NULL, OPT_NOOP_T, 0, 0);
1268         aco_option_register(&cfg_info, "admin", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ADMIN);
1269         aco_option_register(&cfg_info, "marked", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_MARKEDUSER);
1270         aco_option_register(&cfg_info, "startmuted", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_STARTMUTED);
1271         aco_option_register(&cfg_info, "music_on_hold_when_empty", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_MUSICONHOLD);
1272         aco_option_register(&cfg_info, "quiet", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_QUIET);
1273         aco_option_register_custom(&cfg_info, "announce_user_count_all", ACO_EXACT, user_types, "no", announce_user_count_all_handler, 0);
1274         aco_option_register(&cfg_info, "announce_user_count", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ANNOUNCEUSERCOUNT);
1275         /* Negative logic. Defaults to "yes" and evaluates with ast_false(). If !ast_false(), USER_OPT_NOONLYPERSON is cleared */
1276         aco_option_register(&cfg_info, "announce_only_user", ACO_EXACT, user_types, "yes", OPT_BOOLFLAG_T, 0, FLDSET(struct user_profile, flags), USER_OPT_NOONLYPERSON);
1277         aco_option_register(&cfg_info, "wait_marked", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_WAITMARKED);
1278         aco_option_register(&cfg_info, "end_marked", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ENDMARKED);
1279         aco_option_register(&cfg_info, "talk_detection_events", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_TALKER_DETECT);
1280         aco_option_register(&cfg_info, "dtmf_passthrough", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_DTMF_PASS);
1281         aco_option_register(&cfg_info, "announce_join_leave", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ANNOUNCE_JOIN_LEAVE);
1282         aco_option_register(&cfg_info, "pin", ACO_EXACT, user_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct user_profile, pin));
1283         aco_option_register(&cfg_info, "music_on_hold_class", ACO_EXACT, user_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct user_profile, moh_class));
1284         aco_option_register(&cfg_info, "announcement", ACO_EXACT, user_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct user_profile, announcement));
1285         aco_option_register(&cfg_info, "denoise", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_DENOISE);
1286         aco_option_register(&cfg_info, "dsp_drop_silence", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_DROP_SILENCE);
1287         aco_option_register(&cfg_info, "dsp_silence_threshold", ACO_EXACT, user_types, __stringify(DEFAULT_SILENCE_THRESHOLD), OPT_UINT_T, 0, FLDSET(struct user_profile, silence_threshold));
1288         aco_option_register(&cfg_info, "dsp_talking_threshold", ACO_EXACT, user_types, __stringify(DEFAULT_TALKING_THRESHOLD), OPT_UINT_T, 0, FLDSET(struct user_profile, silence_threshold));
1289         aco_option_register(&cfg_info, "jitterbuffer", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_JITTERBUFFER);
1290         /* This option should only be used with the CONFBRIDGE dialplan function */
1291         aco_option_register_custom(&cfg_info, "template", ACO_EXACT, user_types, NULL, user_template_handler, 0);
1292
1293         /* Bridge options */
1294         aco_option_register(&cfg_info, "jitterbuffer", ACO_EXACT, bridge_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct bridge_profile, flags), USER_OPT_JITTERBUFFER);
1295         /* "auto" will fail to parse as a uint, but we use PARSE_DEFAULT to set the value to 0 in that case, which is the value that auto resolves to */
1296         aco_option_register(&cfg_info, "internal_sample_rate", ACO_EXACT, bridge_types, "0", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct bridge_profile, internal_sample_rate), 0);
1297         aco_option_register_custom(&cfg_info, "mixing_interval", ACO_EXACT, bridge_types, "20", mix_interval_handler, 0);
1298         aco_option_register(&cfg_info, "record_conference", ACO_EXACT, bridge_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct bridge_profile, flags), BRIDGE_OPT_RECORD_CONFERENCE);
1299         aco_option_register_custom(&cfg_info, "video_mode", ACO_EXACT, bridge_types, NULL, video_mode_handler, 0);
1300         aco_option_register(&cfg_info, "max_members", ACO_EXACT, bridge_types, "0", OPT_UINT_T, 0, FLDSET(struct bridge_profile, max_members));
1301         aco_option_register(&cfg_info, "record_file", ACO_EXACT, bridge_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct bridge_profile, rec_file));
1302         aco_option_register_custom(&cfg_info, "^sound_", ACO_REGEX, bridge_types, NULL, sound_option_handler, 0);
1303         /* This option should only be used with the CONFBRIDGE dialplan function */
1304         aco_option_register_custom(&cfg_info, "template", ACO_EXACT, bridge_types, NULL, bridge_template_handler, 0);
1305
1306         /* Menu options */
1307         aco_option_register_custom(&cfg_info, "^[0-9A-D*#]+$", ACO_REGEX, menu_types, NULL, menu_option_handler, 0);
1308
1309         if (aco_process_config(&cfg_info, reload) == ACO_PROCESS_ERROR) {
1310                 goto error;
1311         }
1312
1313         if (!reload && ast_cli_register_multiple(cli_confbridge_parser, ARRAY_LEN(cli_confbridge_parser))) {
1314                 goto error;
1315         }
1316
1317         return 0;
1318 error:
1319         /* On a reload, just keep the config we already have in place. */
1320         if (!reload) {
1321                 conf_destroy_config();
1322         }
1323         return -1;
1324 }
1325
1326 static void conf_user_profile_copy(struct user_profile *dst, struct user_profile *src)
1327 {
1328         memcpy(dst, src, sizeof(*dst));
1329 }
1330
1331 const struct user_profile *conf_find_user_profile(struct ast_channel *chan, const char *user_profile_name, struct user_profile *result)
1332 {
1333         struct user_profile *tmp2;
1334         struct ast_datastore *datastore = NULL;
1335         struct func_confbridge_data *b_data = NULL;
1336         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
1337
1338         if (!cfg) {
1339                 return NULL;
1340         }
1341
1342         if (chan) {
1343                 ast_channel_lock(chan);
1344                 if ((datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL))) {
1345                         ast_channel_unlock(chan);
1346                         b_data = datastore->data;
1347                         if (b_data->u_usable) {
1348                                 conf_user_profile_copy(result, &b_data->u_profile);
1349                                 return result;
1350                         }
1351                 } else {
1352                         ast_channel_unlock(chan);
1353                 }
1354         }
1355
1356         if (ast_strlen_zero(user_profile_name)) {
1357                 user_profile_name = DEFAULT_USER_PROFILE;
1358         }
1359         if (!(tmp2 = ao2_find(cfg->user_profiles, user_profile_name, OBJ_KEY))) {
1360                 return NULL;
1361         }
1362         ao2_lock(tmp2);
1363         conf_user_profile_copy(result, tmp2);
1364         ao2_unlock(tmp2);
1365         ao2_ref(tmp2, -1);
1366
1367         return result;
1368 }
1369
1370 void conf_bridge_profile_copy(struct bridge_profile *dst, struct bridge_profile *src)
1371 {
1372         memcpy(dst, src, sizeof(*dst));
1373         if (src->sounds) {
1374                 ao2_ref(src->sounds, +1);
1375         }
1376 }
1377
1378 void conf_bridge_profile_destroy(struct bridge_profile *b_profile)
1379 {
1380         if (b_profile->sounds) {
1381                 ao2_ref(b_profile->sounds, -1);
1382                 b_profile->sounds = NULL;
1383         }
1384 }
1385
1386 const struct bridge_profile *conf_find_bridge_profile(struct ast_channel *chan, const char *bridge_profile_name, struct bridge_profile *result)
1387 {
1388         struct bridge_profile *tmp2;
1389         struct ast_datastore *datastore = NULL;
1390         struct func_confbridge_data *b_data = NULL;
1391         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
1392
1393         if (!cfg) {
1394                 return NULL;
1395         }
1396
1397         if (chan) {
1398                 ast_channel_lock(chan);
1399                 if ((datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL))) {
1400                         ast_channel_unlock(chan);
1401                         b_data = datastore->data;
1402                         if (b_data->b_usable) {
1403                                 conf_bridge_profile_copy(result, &b_data->b_profile);
1404                                 return result;
1405                         }
1406                 } else {
1407                         ast_channel_unlock(chan);
1408                 }
1409         }
1410         if (ast_strlen_zero(bridge_profile_name)) {
1411                 bridge_profile_name = DEFAULT_BRIDGE_PROFILE;
1412         }
1413         if (!(tmp2 = ao2_find(cfg->bridge_profiles, bridge_profile_name, OBJ_KEY))) {
1414                 return NULL;
1415         }
1416         ao2_lock(tmp2);
1417         conf_bridge_profile_copy(result, tmp2);
1418         ao2_unlock(tmp2);
1419         ao2_ref(tmp2, -1);
1420
1421         return result;
1422 }
1423
1424 struct dtmf_menu_hook_pvt {
1425         struct conference_bridge_user *conference_bridge_user;
1426         struct conf_menu_entry menu_entry;
1427         struct conf_menu *menu;
1428 };
1429
1430 static void menu_hook_destroy(void *hook_pvt)
1431 {
1432         struct dtmf_menu_hook_pvt *pvt = hook_pvt;
1433         struct conf_menu_action *action = NULL;
1434
1435         ao2_ref(pvt->menu, -1);
1436
1437         while ((action = AST_LIST_REMOVE_HEAD(&pvt->menu_entry.actions, action))) {
1438                 ast_free(action);
1439         }
1440         ast_free(pvt);
1441 }
1442
1443 static int menu_hook_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
1444 {
1445         struct dtmf_menu_hook_pvt *pvt = hook_pvt;
1446         return conf_handle_dtmf(bridge_channel, pvt->conference_bridge_user, &pvt->menu_entry, pvt->menu);
1447 }
1448
1449 static int copy_menu_entry(struct conf_menu_entry *dst, struct conf_menu_entry *src)
1450 {
1451         struct conf_menu_action *menu_action = NULL;
1452         struct conf_menu_action *new_menu_action = NULL;
1453
1454         memcpy(dst, src, sizeof(*dst));
1455         AST_LIST_HEAD_INIT_NOLOCK(&dst->actions);
1456
1457         AST_LIST_TRAVERSE(&src->actions, menu_action, action) {
1458                 if (!(new_menu_action = ast_calloc(1, sizeof(*new_menu_action)))) {
1459                         return -1;
1460                 }
1461                 memcpy(new_menu_action, menu_action, sizeof(*new_menu_action));
1462                 AST_LIST_INSERT_HEAD(&dst->actions, new_menu_action, action);
1463         }
1464         return 0;
1465 }
1466
1467 void conf_menu_entry_destroy(struct conf_menu_entry *menu_entry)
1468 {
1469         struct conf_menu_action *menu_action = NULL;
1470         while ((menu_action = AST_LIST_REMOVE_HEAD(&menu_entry->actions, action))) {
1471                 ast_free(menu_action);
1472         }
1473 }
1474
1475 int conf_find_menu_entry_by_sequence(const char *dtmf_sequence, struct conf_menu *menu, struct conf_menu_entry *result)
1476 {
1477         struct conf_menu_entry *menu_entry = NULL;
1478
1479         ao2_lock(menu);
1480         AST_LIST_TRAVERSE(&menu->entries, menu_entry, entry) {
1481                 if (!strcasecmp(menu_entry->dtmf, dtmf_sequence)) {
1482                         copy_menu_entry(result, menu_entry);
1483                         ao2_unlock(menu);
1484                         return 1;
1485                 }
1486         }
1487         ao2_unlock(menu);
1488
1489         return 0;
1490 }
1491
1492 int conf_set_menu_to_user(const char *menu_name, struct conference_bridge_user *conference_bridge_user)
1493 {
1494         struct conf_menu *menu;
1495         struct conf_menu_entry *menu_entry = NULL;
1496         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
1497
1498         if (!cfg) {
1499                 return -1;
1500         }
1501
1502         if (!(menu = menu_find(cfg->menus, menu_name))) {
1503                 return -1;
1504         }
1505         ao2_lock(menu);
1506         AST_LIST_TRAVERSE(&menu->entries, menu_entry, entry) {
1507                 struct dtmf_menu_hook_pvt *pvt;
1508                 if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
1509                         ao2_unlock(menu);
1510                         ao2_ref(menu, -1);
1511                         return -1;
1512                 }
1513                 if (copy_menu_entry(&pvt->menu_entry, menu_entry)) {
1514                         ast_free(pvt);
1515                         ao2_unlock(menu);
1516                         ao2_ref(menu, -1);
1517                         return -1;
1518                 }
1519                 pvt->conference_bridge_user = conference_bridge_user;
1520                 ao2_ref(menu, +1);
1521                 pvt->menu = menu;
1522
1523                 ast_bridge_features_hook(&conference_bridge_user->features, pvt->menu_entry.dtmf, menu_hook_callback, pvt, menu_hook_destroy);
1524         }
1525
1526         ao2_unlock(menu);
1527         ao2_ref(menu, -1);
1528
1529         return 0;
1530 }
1531
1532 void conf_destroy_config(void)
1533 {
1534         ast_cli_unregister_multiple(cli_confbridge_parser, ARRAY_LEN(cli_confbridge_parser));
1535         aco_info_destroy(&cfg_info);
1536         ao2_global_obj_release(cfg_handle);
1537 }