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