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