0e81e7e8e85d71e61df8ca9c988af64aa7d88f35
[asterisk/asterisk.git] / apps / confbridge / conf_config_parser.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, Digium, Inc.
5  *
6  * David Vossel <dvossel@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief ConfBridge config parser
22  *
23  * \author David Vossel <dvossel@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/logger.h"
34 #include "asterisk/config.h"
35 #include "asterisk/config_options.h"
36 #include "include/confbridge.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/bridge_features.h"
40 #include "asterisk/stringfields.h"
41 #include "asterisk/pbx.h"
42
43
44 /*** DOCUMENTATION
45         <configInfo name="app_confbridge" language="en_US">
46                 <synopsis>Conference Bridge Application</synopsis>
47                 <configFile name="confbridge.conf">
48                         <configObject name="global">
49                                 <synopsis>Unused, but reserved.</synopsis>
50                         </configObject>
51                         <configObject name="user_profile">
52                                 <synopsis>A named profile to apply to specific callers.</synopsis>
53                                 <description><para>Callers in a ConfBridge have a profile associated with them
54                                 that determine their options. A configuration section is determined to be a
55                                 user_profile when the <literal>type</literal> parameter has a value
56                                 of <literal>user</literal>.
57                                 </para></description>
58                                 <configOption name="type">
59                                         <synopsis>Define this configuration category as a user profile.</synopsis>
60                                         <description><para>The type parameter determines how a context in the
61                                         configuration file is interpreted.</para>
62                                         <enumlist>
63                                                 <enum name="user"><para>Configure the context as a <replaceable>user_profile</replaceable></para></enum>
64                                                 <enum name="bridge"><para>Configure the context as a <replaceable>bridge_profile</replaceable></para></enum>
65                                                 <enum name="menu"><para>Configure the context as a <replaceable>menu</replaceable></para></enum>
66                                         </enumlist>
67                                         </description>
68                                 </configOption>
69                                 <configOption name="admin">
70                                         <synopsis>Sets if the user is an admin or not</synopsis>
71                                 </configOption>
72                                 <configOption name="marked">
73                                         <synopsis>Sets if this is a marked user or not</synopsis>
74                                 </configOption>
75                                 <configOption name="startmuted">
76                                         <synopsis>Sets if all users should start out muted</synopsis>
77                                 </configOption>
78                                 <configOption name="music_on_hold_when_empty">
79                                         <synopsis>Play MOH when user is alone or waiting on a marked user</synopsis>
80                                 </configOption>
81                                 <configOption name="quiet">
82                                         <synopsis>Silence enter/leave prompts and user intros for this user</synopsis>
83                                 </configOption>
84                                 <configOption name="announce_user_count">
85                                         <synopsis>Sets if the number of users should be announced to the user</synopsis>
86                                 </configOption>
87                                 <configOption name="announce_user_count_all">
88                                         <synopsis>Announce user count to all the other users when this user joins</synopsis>
89                                         <description><para>Sets if the number of users should be announced to all the other users
90                                         in the conference when this user joins. This option can be either set to 'yes' or
91                                         a number. When set to a number, the announcement will only occur once the user
92                                         count is above the specified number.
93                                         </para></description>
94                                 </configOption>
95                                 <configOption name="announce_only_user">
96                                         <synopsis>Announce to a user when they join an empty conference</synopsis>
97                                 </configOption>
98                                 <configOption name="wait_marked">
99                                         <synopsis>Sets if the user must wait for a marked user to enter before joining a conference</synopsis>
100                                 </configOption>
101                                 <configOption name="end_marked">
102                                         <synopsis>Kick the user from the conference when the last marked user leaves</synopsis>
103                                 </configOption>
104                                 <configOption name="talk_detection_events">
105                                         <synopsis>Set whether or not notifications of when a user begins and ends talking should be sent out as events over AMI</synopsis>
106                                 </configOption>
107                                 <configOption name="dtmf_passthrough">
108                                         <synopsis>Sets whether or not DTMF should pass through the conference</synopsis>
109                                 </configOption>
110                                 <configOption name="announce_join_leave">
111                                         <synopsis>Prompt user for their name when joining a conference and play it to the conference when they enter</synopsis>
112                                 </configOption>
113                                 <configOption name="announce_join_leave_review">
114                                         <synopsis>Prompt user for their name when joining a conference and play it to the conference when they enter.
115                                         The user will be asked to review the recording of their name before entering the conference.</synopsis>
116                                 </configOption>
117                                 <configOption name="pin">
118                                         <synopsis>Sets a PIN the user must enter before joining the conference</synopsis>
119                                 </configOption>
120                                 <configOption name="music_on_hold_class">
121                                         <synopsis>The MOH class to use for this user</synopsis>
122                                 </configOption>
123                                 <configOption name="announcement">
124                                         <synopsis>Sound file to play to the user when they join a conference</synopsis>
125                                 </configOption>
126                                 <configOption name="denoise">
127                                         <synopsis>Apply a denoise filter to the audio before mixing</synopsis>
128                                         <description><para>Sets whether or not a denoise filter should be applied
129                                         to the audio before mixing or not.  Off by default. Requires
130                                         <literal>codec_speex</literal> to be built and installed.  Do not confuse this option
131                                         with <replaceable>drop_silence</replaceable>.  Denoise is useful if there is a lot of background
132                                         noise for a user as it attempts to remove the noise while preserving
133                                         the speech.  This option does NOT remove silence from being mixed into
134                                         the conference and does come at the cost of a slight performance hit.
135                                         </para></description>
136                                 </configOption>
137                                 <configOption name="dsp_drop_silence">
138                                         <synopsis>Drop what Asterisk detects as silence from audio sent to the bridge</synopsis>
139                                         <description><para>
140                                         This option drops what Asterisk detects as silence from
141                                         entering into the bridge.  Enabling this option will drastically
142                                         improve performance and help remove the buildup of background
143                                         noise from the conference. Highly recommended for large conferences
144                                         due to its performance enhancements.
145                                         </para></description>
146                                 </configOption>
147                                 <configOption name="dsp_silence_threshold">
148                                         <synopsis>The number of milliseconds of detected silence necessary to trigger silence detection</synopsis>
149                                         <description><para>
150                                         The time in milliseconds of sound falling within the what
151                                         the dsp has established as baseline silence before a user
152                                         is considered be silent.  This value affects several
153                                         operations and should not be changed unless the impact
154                                         on call quality is fully understood.</para>
155                                         <para>What this value affects internally:</para>
156                                         <para>
157                                                 1. When talk detection AMI events are enabled, this value
158                                                 determines when the user has stopped talking after a
159                                                 period of talking.  If this value is set too low
160                                                 AMI events indicating the user has stopped talking
161                                                 may get falsely sent out when the user briefly pauses
162                                                 during mid sentence.
163                                         </para>
164                                         <para>
165                                                 2. The <replaceable>drop_silence</replaceable> option depends on this value to
166                                                 determine when the user's audio should begin to be
167                                                 dropped from the conference bridge after the user
168                                                 stops talking.  If this value is set too low the user's
169                                                 audio stream may sound choppy to the other participants.
170                                                 This is caused by the user transitioning constantly from
171                                                 silence to talking during mid sentence.
172                                         </para>
173                                         <para>
174                                                 The best way to approach this option is to set it slightly above
175                                                 the maximum amount of ms of silence a user may generate during
176                                                 natural speech.
177                                         </para>
178                                         <para>By default this value is 2500ms. Valid values are 1 through 2^31.</para>
179                                         </description>
180                                 </configOption>
181                                 <configOption name="dsp_talking_threshold">
182                                         <synopsis>The number of milliseconds of detected non-silence necessary to triger talk detection</synopsis>
183                                         <description><para>
184                                                 The time in milliseconds of sound above what the dsp has
185                                                 established as base line silence for a user before a user
186                                                 is considered to be talking.  This value affects several
187                                                 operations and should not be changed unless the impact on
188                                                 call quality is fully understood.</para>
189                                                 <para>
190                                                 What this value affects internally:
191                                                 </para>
192                                                 <para>
193                                                 1. Audio is only mixed out of a user's incoming audio stream
194                                                 if talking is detected.  If this value is set too
195                                                 loose the user will hear themselves briefly each
196                                                 time they begin talking until the dsp has time to
197                                                 establish that they are in fact talking.
198                                                 </para>
199                                                 <para>
200                                                 2. When talk detection AMI events are enabled, this value
201                                                 determines when talking has begun which results in
202                                                 an AMI event to fire.  If this value is set too tight
203                                                 AMI events may be falsely triggered by variants in
204                                                 room noise.
205                                                 </para>
206                                                 <para>
207                                                 3. The <replaceable>drop_silence</replaceable> option depends on this value to determine
208                                                 when the user's audio should be mixed into the bridge
209                                                 after periods of silence.  If this value is too loose
210                                                 the beginning of a user's speech will get cut off as they
211                                                 transition from silence to talking.
212                                                 </para>
213                                                 <para>By default this value is 160 ms. Valid values are 1 through 2^31</para>
214                                         </description>
215                                 </configOption>
216                                 <configOption name="jitterbuffer">
217                                         <synopsis>Place a jitter buffer on the user's audio stream before audio mixing is performed</synopsis>
218                                         <description><para>
219                                                 Enabling this option places a jitterbuffer on the user's audio stream
220                                                 before audio mixing is performed.  This is highly recommended but will
221                                                 add a slight delay to the audio.  This option is using the <literal>JITTERBUFFER</literal>
222                                                 dialplan function's default adaptive jitterbuffer.  For a more fine tuned
223                                                 jitterbuffer, disable this option and use the <literal>JITTERBUFFER</literal> dialplan function
224                                                 on the user before entering the ConfBridge application.
225                                         </para></description>
226                                 </configOption>
227                                 <configOption name="template">
228                                         <synopsis>When using the CONFBRIDGE dialplan function, use a user profile as a template for creating a new temporary profile</synopsis>
229                                 </configOption>
230                         </configObject>
231                         <configObject name="bridge_profile">
232                                 <synopsis>A named profile to apply to specific bridges.</synopsis>
233                                 <description><para>ConfBridge bridges have a profile associated with them
234                                 that determine their options. A configuration section is determined to be a
235                                 <literal>bridge_profile</literal> when the <literal>type</literal> parameter has a value
236                                 of <literal>bridge</literal>.
237                                 </para></description>
238                                 <configOption name="type">
239                                         <synopsis>Define this configuration category as a bridge profile</synopsis>
240                                         <description><para>The type parameter determines how a context in the
241                                         configuration file is interpreted.</para>
242                                         <enumlist>
243                                                 <enum name="user"><para>Configure the context as a <replaceable>user_profile</replaceable></para></enum>
244                                                 <enum name="bridge"><para>Configure the context as a <replaceable>bridge_profile</replaceable></para></enum>
245                                                 <enum name="menu"><para>Configure the context as a <replaceable>menu</replaceable></para></enum>
246                                         </enumlist>
247                                         </description>
248                                 </configOption>
249                                 <configOption name="jitterbuffer">
250                                         <synopsis>Place a jitter buffer on the conference's audio stream</synopsis>
251                                 </configOption>
252                                 <configOption name="internal_sample_rate">
253                                         <synopsis>Set the internal native sample rate for mixing the conference</synopsis>
254                                         <description><para>
255                                                 Sets the internal native sample rate the
256                                                 conference is mixed at.  This is set to automatically
257                                                 adjust the sample rate to the best quality by default.
258                                                 Other values can be anything from 8000-192000.  If a
259                                                 sample rate is set that Asterisk does not support, the
260                                                 closest sample rate Asterisk does support to the one requested
261                                                 will be used.
262                                         </para></description>
263                                 </configOption>
264                                 <configOption name="language" default="en">
265                                         <synopsis>The language used for announcements to the conference.</synopsis>
266                                         <description><para>
267                                                 By default, announcements to a conference use English.  Which means
268                                                 the prompts played to all users within the conference will be
269                                                 English.  By changing the language of a bridge, this will change
270                                                 the language of the prompts played to all users.
271                                         </para></description>
272                                 </configOption>
273                                 <configOption name="mixing_interval">
274                                         <synopsis>Sets the internal mixing interval in milliseconds for the bridge</synopsis>
275                                         <description><para>
276                                                 Sets the internal mixing interval in milliseconds for the bridge.  This
277                                                 number reflects how tight or loose the mixing will be for the conference.
278                                                 In order to improve performance a larger mixing interval such as 40ms may
279                                                 be chosen.  Using a larger mixing interval comes at the cost of introducing
280                                                 larger amounts of delay into the bridge.  Valid values here are 10, 20, 40,
281                                                 or 80.
282                                         </para></description>
283                                 </configOption>
284                                 <configOption name="record_conference">
285                                         <synopsis>Record the conference starting with the first active user's entrance and ending with the last active user's exit</synopsis>
286                                         <description><para>
287                                                 Records the conference call starting when the first user
288                                                 enters the room, and ending when the last user exits the room.
289                                                 The default recorded filename is
290                                                 <filename>'confbridge-${name of conference bridge}-${start time}.wav'</filename>
291                                                 and the default format is 8khz slinear.  This file will be
292                                                 located in the configured monitoring directory in <filename>asterisk.conf</filename>.
293                                         </para></description>
294                                 </configOption>
295                                 <configOption name="record_file" default="confbridge-${name of conference bridge}-${start time}.wav">
296                                         <synopsis>The filename of the conference recording</synopsis>
297                                         <description><para>
298                                                 When <replaceable>record_conference</replaceable> is set to yes, the specific name of the
299                                                 record file can be set using this option.  Note that since multiple
300                                                 conferences may use the same bridge profile, this may cause issues
301                                                 depending on the configuration.  It is recommended to only use this
302                                                 option dynamically with the <literal>CONFBRIDGE()</literal> dialplan function. This
303                                                 allows the record name to be specified and a unique name to be chosen.
304                                                 By default, the record_file is stored in Asterisk's spool/monitor directory
305                                                 with a unique filename starting with the 'confbridge' prefix.
306                                         </para></description>
307                                 </configOption>
308                                 <configOption name="record_file_append" default="yes">
309                                         <synopsis>Append record file when starting/stopping on same conference recording</synopsis>
310                                         <description><para>
311                                                 When <replaceable>record_file_append</replaceable> is set to yes, stopping and starting recording on a
312                                                 conference adds the new portion to end of current record_file. When this is
313                                                 set to no, a new <replaceable>record_file</replaceable> is generated every time you start then stop recording
314                                                 on a conference.
315                                         </para></description>
316                                 </configOption>
317                                 <configOption name="video_mode">
318                                         <synopsis>Sets how confbridge handles video distribution to the conference participants</synopsis>
319                                         <description><para>
320                                                 Sets how confbridge handles video distribution to the conference participants.
321                                                 Note that participants wanting to view and be the source of a video feed
322                                                 <emphasis>MUST</emphasis> be sharing the same video codec.  Also, using video in conjunction with
323                                                 with the jitterbuffer currently results in the audio being slightly out of sync
324                                                 with the video.  This is a result of the jitterbuffer only working on the audio
325                                                 stream.  It is recommended to disable the jitterbuffer when video is used.</para>
326                                                 <enumlist>
327                                                         <enum name="none">
328                                                                 <para>No video sources are set by default in the conference. It is still
329                                                                 possible for a user to be set as a video source via AMI or DTMF action
330                                                                 at any time.</para>
331                                                         </enum>
332                                                         <enum name="follow_talker">
333                                                                 <para>The video feed will follow whoever is talking and providing video.</para>
334                                                         </enum>
335                                                         <enum name="last_marked">
336                                                                 <para>The last marked user to join the conference with video capabilities
337                                                                 will be the single source of video distributed to all participants.
338                                                                 If multiple marked users are capable of video, the last one to join
339                                                                 is always the source, when that user leaves it goes to the one who
340                                                                 joined before them.</para>
341                                                         </enum>
342                                                         <enum name="first_marked">
343                                                                 <para>The first marked user to join the conference with video capabilities
344                                                                 is the single source of video distribution among all participants. If
345                                                                 that user leaves, the marked user to join after them becomes the source.</para>
346                                                         </enum>
347                                                 </enumlist>
348                                         </description>
349                                 </configOption>
350                                 <configOption name="max_members">
351                                         <synopsis>Limit the maximum number of participants for a single conference</synopsis>
352                                         <description><para>
353                                                 This option limits the number of participants for a single
354                                                 conference to a specific number.  By default conferences
355                                                 have no participant limit. After the limit is reached, the
356                                                 conference will be locked until someone leaves.  Note however
357                                                 that an Admin user will always be alowed to join the conference
358                                                 regardless if this limit is reached or not.
359                                         </para></description>
360                                 </configOption>
361                                 <configOption name="^sound_">
362                                         <synopsis>Override the various conference bridge sound files</synopsis>
363                                         <description><para>
364                                                 All sounds in the conference are customizable using the bridge profile options below.
365                                                 Simply state the option followed by the filename or full path of the filename after
366                                                 the option.  Example: <literal>sound_had_joined=conf-hasjoin</literal>  This will play the <literal>conf-hasjoin</literal>
367                                                 sound file found in the sounds directory when announcing someone's name is joining the
368                                                 conference.</para>
369                                                 <enumlist>
370                                                         <enum name="sound_join"><para>The sound played to everyone when someone enters the conference.</para></enum>
371                                                         <enum name="sound_leave"><para>The sound played to everyone when someone leaves the conference.</para></enum>
372                                                         <enum name="sound_has_joined"><para>The sound played before announcing someone's name has
373                                                                                 joined the conference. This is used for user intros.
374                                                                                 Example <literal>"_____ has joined the conference"</literal></para></enum>
375                                                         <enum name="sound_has_left"><para>The sound played when announcing someone's name has
376                                                                                 left the conference. This is used for user intros.
377                                                                                 Example <literal>"_____ has left the conference"</literal></para></enum>
378                                                         <enum name="sound_kicked"><para>The sound played to a user who has been kicked from the conference.</para></enum>
379                                                         <enum name="sound_muted"><para>The sound played when the mute option it toggled on.</para></enum>
380                                                         <enum name="sound_unmuted"><para>The sound played when the mute option it toggled off.</para></enum>
381                                                         <enum name="sound_only_person"><para>The sound played when the user is the only person in the conference.</para></enum>
382                                                         <enum name="sound_only_one"><para>The sound played to a user when there is only one other
383                                                                                 person is in the conference.</para></enum>
384                                                         <enum name="sound_there_are"><para>The sound played when announcing how many users there
385                                                                                 are in a conference.</para></enum>
386                                                         <enum name="sound_other_in_party"><para>This file is used in conjunction with <literal>sound_there_are</literal>
387                                                                                 when announcing how many users there are in the conference.
388                                                                                 The sounds are stringed together like this.
389                                                                                 <literal>"sound_there_are" ${number of participants} "sound_other_in_party"</literal></para></enum>
390                                                         <enum name="sound_place_into_conference"><para>The sound played when someone is placed into the conference
391                                                                                 after waiting for a marked user.</para></enum>
392                                                         <enum name="sound_wait_for_leader"><para>The sound played when a user is placed into a conference that
393                                                                                 can not start until a marked user enters.</para></enum>
394                                                         <enum name="sound_leader_has_left"><para>The sound played when the last marked user leaves the conference.</para></enum>
395                                                         <enum name="sound_get_pin"><para>The sound played when prompting for a conference pin number.</para></enum>
396                                                         <enum name="sound_invalid_pin"><para>The sound played when an invalid pin is entered too many times.</para></enum>
397                                                         <enum name="sound_locked"><para>The sound played to a user trying to join a locked conference.</para></enum>
398                                                         <enum name="sound_locked_now"><para>The sound played to an admin after toggling the conference to locked mode.</para></enum>
399                                                         <enum name="sound_unlocked_now"><para>The sound played to an admin after toggling the conference to unlocked mode.</para></enum>
400                                                         <enum name="sound_error_menu"><para>The sound played when an invalid menu option is entered.</para></enum>
401                                                 </enumlist>
402                                         </description>
403                                 </configOption>
404                                 <configOption name="template">
405                                         <synopsis>When using the CONFBRIDGE dialplan function, use a bridge profile as a template for creating a new temporary profile</synopsis>
406                                 </configOption>
407                         </configObject>
408                         <configObject name="menu">
409                                 <synopsis>A conference user menu</synopsis>
410                                 <description>
411                                         <para>Conference users, as defined by a <replaceable>conf_user</replaceable>,
412                                         can have a DTMF menu assigned to their profile when they enter the
413                                         <literal>ConfBridge</literal> application.</para>
414                                 </description>
415                                 <configOption name="type">
416                                         <synopsis>Define this configuration category as a menu</synopsis>
417                                         <description><para>The type parameter determines how a context in the
418                                         configuration file is interpreted.</para>
419                                         <enumlist>
420                                                 <enum name="user"><para>Configure the context as a <replaceable>user_profile</replaceable></para></enum>
421                                                 <enum name="bridge"><para>Configure the context as a <replaceable>bridge_profile</replaceable></para></enum>
422                                                 <enum name="menu"><para>Configure the context as a <replaceable>menu</replaceable></para></enum>
423                                         </enumlist>
424                                         </description>
425                                 </configOption>
426                                 <configOption name="template">
427                                         <synopsis>When using the CONFBRIDGE dialplan function, use a menu profile as a template for creating a new temporary profile</synopsis>
428                                 </configOption>
429                                 <configOption name="^[0-9A-D*#]+$">
430                                         <synopsis>DTMF sequences to assign various confbridge actions to</synopsis>
431                                         <description>
432                                         <para>The ConfBridge application also has the ability to apply custom DTMF menus to
433                                         each channel using the application.  Like the User and Bridge profiles a menu
434                                         is passed in to ConfBridge as an argument in the dialplan.</para>
435                                         <para>Below is a list of menu actions that can be assigned to a DTMF sequence.</para>
436                                         <note><para>
437                                                 To have the first DTMF digit in a sequence be the '#' character, you need to
438                                                 escape it.  If it is not escaped then normal config file processing will
439                                                 think it is a directive like #include.  For example: The mute setting is
440                                                 toggled when <literal>#1</literal> is pressed.</para>
441                                                 <para><literal>\#1=toggle_mute</literal></para>
442                                         </note>
443                                         <note><para>
444                                         A single DTMF sequence can have multiple actions associated with it. This is
445                                         accomplished by stringing the actions together and using a <literal>,</literal> as the
446                                         delimiter.  Example:  Both listening and talking volume is reset when <literal>5</literal> is
447                                         pressed.  <literal>5=reset_talking_volume, reset_listening_volume</literal></para></note>
448                                         <enumlist>
449                                                 <enum name="playback(filename&amp;filename2&amp;...)"><para>
450                                                         <literal>playback</literal> will play back an audio file to a channel
451                                                         and then immediately return to the conference.
452                                                         This file can not be interupted by DTMF.
453                                                         Multiple files can be chained together using the
454                                                         <literal>&amp;</literal> character.</para></enum>
455                                                 <enum name="playback_and_continue(filename&amp;filename2&amp;...)"><para>
456                                                         <literal>playback_and_continue</literal> will
457                                                         play back a prompt while continuing to
458                                                         collect the dtmf sequence.  This is useful
459                                                         when using a menu prompt that describes all
460                                                         the menu options.  Note however that any DTMF
461                                                         during this action will terminate the prompts
462                                                         playback.  Prompt files can be chained together
463                                                         using the <literal>&amp;</literal> character as a delimiter.</para></enum>
464                                                 <enum name="toggle_mute"><para>
465                                                         Toggle turning on and off mute.  Mute will make the user silent
466                                                         to everyone else, but the user will still be able to listen in.
467                                                         </para></enum>
468                                                 <enum name="no_op"><para>
469                                                         This action does nothing (No Operation). Its only real purpose exists for
470                                                         being able to reserve a sequence in the config as a menu exit sequence.</para></enum>
471                                                 <enum name="decrease_listening_volume"><para>
472                                                         Decreases the channel's listening volume.</para></enum>
473                                                 <enum name="increase_listening_volume"><para>
474                                                         Increases the channel's listening volume.</para></enum>
475                                                 <enum name="reset_listening_volume"><para>
476                                                         Reset channel's listening volume to default level.</para></enum>
477                                                 <enum name="decrease_talking_volume"><para>
478                                                         Decreases the channel's talking volume.</para></enum>
479                                                 <enum name="increase_talking_volume"><para>
480                                                         Increases the channel's talking volume.</para></enum>
481                                                 <enum name="reset_talking_volume"><para>
482                                                         Reset channel's talking volume to default level.</para></enum>
483                                                 <enum name="dialplan_exec(context,exten,priority)"><para>
484                                                         The <literal>dialplan_exec</literal> action allows a user
485                                                         to escape from the conference and execute
486                                                         commands in the dialplan.  Once the dialplan
487                                                         exits the user will be put back into the
488                                                         conference.  The possibilities are endless!</para></enum>
489                                                 <enum name="leave_conference"><para>
490                                                         This action allows a user to exit the conference and continue
491                                                         execution in the dialplan.</para></enum>
492                                                 <enum name="admin_kick_last"><para>
493                                                         This action allows an Admin to kick the last participant from the
494                                                         conference. This action will only work for admins which allows
495                                                         a single menu to be used for both users and admins.</para></enum>
496                                                 <enum name="admin_toggle_conference_lock"><para>
497                                                         This action allows an Admin to toggle locking and
498                                                         unlocking the conference.  Non admins can not use
499                                                         this action even if it is in their menu.</para></enum>
500                                                 <enum name="set_as_single_video_src"><para>
501                                                         This action allows any user to set themselves as the
502                                                         single video source distributed to all participants.
503                                                         This will make the video feed stick to them regardless
504                                                         of what the <literal>video_mode</literal> is set to.</para></enum>
505                                                 <enum name="release_as_single_video_src"><para>
506                                                         This action allows a user to release themselves as
507                                                         the video source.  If <literal>video_mode</literal> is not set to <literal>none</literal>
508                                                         this action will result in the conference returning to
509                                                         whatever video mode the bridge profile is using.</para>
510                                                         <para>Note that this action will have no effect if the user
511                                                         is not currently the video source.  Also, the user is
512                                                         not guaranteed by using this action that they will not
513                                                         become the video source again.  The bridge will return
514                                                         to whatever operation the <literal>video_mode</literal> option is set to
515                                                         upon release of the video src.</para></enum>
516                                                 <enum name="admin_toggle_mute_participants"><para>
517                                                         This action allows an administrator to toggle the mute
518                                                         state for all non-admins within a conference.  All
519                                                         admin users are unaffected by this option.  Note that all
520                                                         users, regardless of their admin status, are notified
521                                                         that the conference is muted.</para></enum>
522                                                 <enum name="participant_count"><para>
523                                                         This action plays back the number of participants currently
524                                                         in a conference</para></enum>
525                                                 </enumlist>
526                                         </description>
527                                 </configOption>
528                         </configObject>
529                 </configFile>
530         </configInfo>
531 ***/
532
533 struct confbridge_cfg {
534         struct ao2_container *bridge_profiles;
535         struct ao2_container *user_profiles;
536         struct ao2_container *menus;
537 };
538
539 static int verify_default_profiles(void);
540 static void *bridge_profile_alloc(const char *category);
541 static void *bridge_profile_find(struct ao2_container *container, const char *category);
542 static struct bridge_profile_sounds *bridge_profile_sounds_alloc(void);
543
544 static void bridge_profile_destructor(void *obj)
545 {
546         struct bridge_profile *b_profile = obj;
547         ao2_cleanup(b_profile->sounds);
548 }
549
550 static void *bridge_profile_alloc(const char *category)
551 {
552         struct bridge_profile *b_profile;
553
554         if (!(b_profile = ao2_alloc(sizeof(*b_profile), bridge_profile_destructor))) {
555                 return NULL;
556         }
557
558         if (!(b_profile->sounds = bridge_profile_sounds_alloc())) {
559                 ao2_ref(b_profile, -1);
560                 return NULL;
561         }
562
563         ast_copy_string(b_profile->name, category, sizeof(b_profile->name));
564
565         return b_profile;
566 }
567
568 static void *bridge_profile_find(struct ao2_container *container, const char *category)
569 {
570         return ao2_find(container, category, OBJ_KEY);
571 }
572
573 static struct aco_type bridge_type = {
574         .type = ACO_ITEM,
575         .name = "bridge_profile",
576         .category_match = ACO_BLACKLIST,
577         .category = "^general$",
578         .matchfield = "type",
579         .matchvalue = "bridge",
580         .item_alloc = bridge_profile_alloc,
581         .item_find = bridge_profile_find,
582         .item_offset = offsetof(struct confbridge_cfg, bridge_profiles),
583 };
584
585 static void *user_profile_alloc(const char *category);
586 static void *user_profile_find(struct ao2_container *container, const char *category);
587 static void user_profile_destructor(void *obj)
588 {
589         return;
590 }
591
592 static void *user_profile_alloc(const char *category)
593 {
594         struct user_profile *u_profile;
595
596         if (!(u_profile = ao2_alloc(sizeof(*u_profile), user_profile_destructor))) {
597                 return NULL;
598         }
599
600         ast_copy_string(u_profile->name, category, sizeof(u_profile->name));
601
602         return u_profile;
603 }
604
605 static void *user_profile_find(struct ao2_container *container, const char *category)
606 {
607         return ao2_find(container, category, OBJ_KEY);
608 }
609
610 static struct aco_type user_type = {
611         .type = ACO_ITEM,
612         .name  = "user_profile",
613         .category_match = ACO_BLACKLIST,
614         .category = "^general$",
615         .matchfield = "type",
616         .matchvalue = "user",
617         .item_alloc = user_profile_alloc,
618         .item_find = user_profile_find,
619         .item_offset = offsetof(struct confbridge_cfg, user_profiles),
620 };
621
622 static void *menu_alloc(const char *category);
623 static void *menu_find(struct ao2_container *container, const char *category);
624 static void menu_destructor(void *obj);
625
626 static void *menu_alloc(const char *category)
627 {
628         struct conf_menu *menu;
629         if (!(menu = ao2_alloc(sizeof(*menu), menu_destructor))) {
630                 return NULL;
631         }
632         ast_copy_string(menu->name, category, sizeof(menu->name));
633         return menu;
634 }
635
636 static void *menu_find(struct ao2_container *container, const char *category)
637 {
638         return ao2_find(container, category, OBJ_KEY);
639 }
640
641 static struct aco_type menu_type = {
642         .type = ACO_ITEM,
643         .name = "menu",
644         .category_match = ACO_BLACKLIST,
645         .category = "^general$",
646         .matchfield = "type",
647         .matchvalue = "menu",
648         .item_alloc = menu_alloc,
649         .item_find = menu_find,
650         .item_offset = offsetof(struct confbridge_cfg, menus),
651 };
652
653 /* Used to pass to aco_option_register */
654 static struct aco_type *bridge_types[] = ACO_TYPES(&bridge_type);
655 static struct aco_type *menu_types[] = ACO_TYPES(&menu_type);
656 static struct aco_type *user_types[] = ACO_TYPES(&user_type);
657
658 /* The general category is reserved, but unused */
659 static struct aco_type general_type = {
660         .type = ACO_GLOBAL,
661         .name = "global",
662         .category_match = ACO_WHITELIST,
663         .category = "^general$",
664 };
665
666 static struct aco_file confbridge_conf = {
667         .filename = "confbridge.conf",
668         .types = ACO_TYPES(&bridge_type, &user_type, &menu_type, &general_type),
669 };
670
671 static AO2_GLOBAL_OBJ_STATIC(cfg_handle);
672
673 static void *confbridge_cfg_alloc(void);
674
675 CONFIG_INFO_STANDARD(cfg_info, cfg_handle, confbridge_cfg_alloc,
676         .files = ACO_FILES(&confbridge_conf),
677         .pre_apply_config = verify_default_profiles,
678 );
679
680 /*! bridge profile container functions */
681 static int bridge_cmp_cb(void *obj, void *arg, int flags)
682 {
683         const struct bridge_profile *left = obj;
684         const struct bridge_profile *right = arg;
685         const char *right_name = arg;
686         int cmp;
687
688         switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
689         default:
690         case OBJ_POINTER:
691                 right_name = right->name;
692                 /* Fall through */
693         case OBJ_KEY:
694                 cmp = strcasecmp(left->name, right_name);
695                 break;
696         case OBJ_PARTIAL_KEY:
697                 cmp = strncasecmp(left->name, right_name, strlen(right_name));
698                 break;
699         }
700         return cmp ? 0 : CMP_MATCH;
701 }
702
703 static int bridge_hash_cb(const void *obj, const int flags)
704 {
705         const struct bridge_profile *b_profile = obj;
706         const char *name = obj;
707         int hash;
708
709         switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
710         default:
711         case OBJ_POINTER:
712                 name = b_profile->name;
713                 /* Fall through */
714         case OBJ_KEY:
715                 hash = ast_str_case_hash(name);
716                 break;
717         case OBJ_PARTIAL_KEY:
718                 /* Should never happen in hash callback. */
719                 ast_assert(0);
720                 hash = 0;
721                 break;
722         }
723         return hash;
724 }
725
726 /*! menu container functions */
727 static int menu_cmp_cb(void *obj, void *arg, int flags)
728 {
729         const struct conf_menu *left = obj;
730         const struct conf_menu *right = arg;
731         const char *right_name = arg;
732         int cmp;
733
734         switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
735         default:
736         case OBJ_POINTER:
737                 right_name = right->name;
738                 /* Fall through */
739         case OBJ_KEY:
740                 cmp = strcasecmp(left->name, right_name);
741                 break;
742         case OBJ_PARTIAL_KEY:
743                 cmp = strncasecmp(left->name, right_name, strlen(right_name));
744                 break;
745         }
746         return cmp ? 0 : CMP_MATCH;
747 }
748
749 static int menu_hash_cb(const void *obj, const int flags)
750 {
751         const struct conf_menu *menu = obj;
752         const char *name = obj;
753         int hash;
754
755         switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
756         default:
757         case OBJ_POINTER:
758                 name = menu->name;
759                 /* Fall through */
760         case OBJ_KEY:
761                 hash = ast_str_case_hash(name);
762                 break;
763         case OBJ_PARTIAL_KEY:
764                 /* Should never happen in hash callback. */
765                 ast_assert(0);
766                 hash = 0;
767                 break;
768         }
769         return hash;
770 }
771
772 static void menu_destructor(void *obj)
773 {
774         struct conf_menu *menu = obj;
775         struct conf_menu_entry *entry = NULL;
776
777         while ((entry = AST_LIST_REMOVE_HEAD(&menu->entries, entry))) {
778                 conf_menu_entry_destroy(entry);
779                 ast_free(entry);
780         }
781 }
782
783 /*! User profile container functions */
784 static int user_cmp_cb(void *obj, void *arg, int flags)
785 {
786         const struct user_profile *left = obj;
787         const struct user_profile *right = arg;
788         const char *right_name = arg;
789         int cmp;
790
791         switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
792         default:
793         case OBJ_POINTER:
794                 right_name = right->name;
795                 /* Fall through */
796         case OBJ_KEY:
797                 cmp = strcasecmp(left->name, right_name);
798                 break;
799         case OBJ_PARTIAL_KEY:
800                 cmp = strncasecmp(left->name, right_name, strlen(right_name));
801                 break;
802         }
803         return cmp ? 0 : CMP_MATCH;
804 }
805
806 static int user_hash_cb(const void *obj, const int flags)
807 {
808         const struct user_profile *u_profile = obj;
809         const char *name = obj;
810         int hash;
811
812         switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
813         default:
814         case OBJ_POINTER:
815                 name = u_profile->name;
816                 /* Fall through */
817         case OBJ_KEY:
818                 hash = ast_str_case_hash(name);
819                 break;
820         case OBJ_PARTIAL_KEY:
821                 /* Should never happen in hash callback. */
822                 ast_assert(0);
823                 hash = 0;
824                 break;
825         }
826         return hash;
827 }
828
829 /*! Bridge Profile Sounds functions */
830 static void bridge_profile_sounds_destroy_cb(void *obj)
831 {
832         struct bridge_profile_sounds *sounds = obj;
833         ast_string_field_free_memory(sounds);
834 }
835
836 static struct bridge_profile_sounds *bridge_profile_sounds_alloc(void)
837 {
838         struct bridge_profile_sounds *sounds = ao2_alloc(sizeof(*sounds), bridge_profile_sounds_destroy_cb);
839
840         if (!sounds) {
841                 return NULL;
842         }
843         if (ast_string_field_init(sounds, 512)) {
844                 ao2_ref(sounds, -1);
845                 return NULL;
846         }
847
848         return sounds;
849 }
850
851 static int set_sound(const char *sound_name, const char *sound_file, struct bridge_profile *b_profile)
852 {
853         struct bridge_profile_sounds *sounds = b_profile->sounds;
854         if (ast_strlen_zero(sound_file)) {
855                 return -1;
856         }
857
858         if (!strcasecmp(sound_name, "sound_only_person")) {
859                 ast_string_field_set(sounds, onlyperson, sound_file);
860         } else if (!strcasecmp(sound_name, "sound_only_one")) {
861                 ast_string_field_set(sounds, onlyone, sound_file);
862         } else if (!strcasecmp(sound_name, "sound_has_joined")) {
863                 ast_string_field_set(sounds, hasjoin, sound_file);
864         } else if (!strcasecmp(sound_name, "sound_has_left")) {
865                 ast_string_field_set(sounds, hasleft, sound_file);
866         } else if (!strcasecmp(sound_name, "sound_kicked")) {
867                 ast_string_field_set(sounds, kicked, sound_file);
868         } else if (!strcasecmp(sound_name, "sound_muted")) {
869                 ast_string_field_set(sounds, muted, sound_file);
870         } else if (!strcasecmp(sound_name, "sound_unmuted")) {
871                 ast_string_field_set(sounds, unmuted, sound_file);
872         } else if (!strcasecmp(sound_name, "sound_there_are")) {
873                 ast_string_field_set(sounds, thereare, sound_file);
874         } else if (!strcasecmp(sound_name, "sound_other_in_party")) {
875                 ast_string_field_set(sounds, otherinparty, sound_file);
876         } else if (!strcasecmp(sound_name, "sound_place_into_conference")) {
877                 static int deprecation_warning = 1;
878                 if (deprecation_warning) {
879                         ast_log(LOG_WARNING, "sound_place_into_conference is deprecated"
880                                 " and unused. Use sound_begin for similar functionality.");
881                         deprecation_warning = 0;
882                 }
883                 ast_string_field_set(sounds, placeintoconf, sound_file);
884         } else if (!strcasecmp(sound_name, "sound_wait_for_leader")) {
885                 ast_string_field_set(sounds, waitforleader, sound_file);
886         } else if (!strcasecmp(sound_name, "sound_leader_has_left")) {
887                 ast_string_field_set(sounds, leaderhasleft, sound_file);
888         } else if (!strcasecmp(sound_name, "sound_get_pin")) {
889                 ast_string_field_set(sounds, getpin, sound_file);
890         } else if (!strcasecmp(sound_name, "sound_invalid_pin")) {
891                 ast_string_field_set(sounds, invalidpin, sound_file);
892         } else if (!strcasecmp(sound_name, "sound_locked")) {
893                 ast_string_field_set(sounds, locked, sound_file);
894         } else if (!strcasecmp(sound_name, "sound_unlocked_now")) {
895                 ast_string_field_set(sounds, unlockednow, sound_file);
896         } else if (!strcasecmp(sound_name, "sound_locked_now")) {
897                 ast_string_field_set(sounds, lockednow, sound_file);
898         } else if (!strcasecmp(sound_name, "sound_error_menu")) {
899                 ast_string_field_set(sounds, errormenu, sound_file);
900         } else if (!strcasecmp(sound_name, "sound_join")) {
901                 ast_string_field_set(sounds, join, sound_file);
902         } else if (!strcasecmp(sound_name, "sound_leave")) {
903                 ast_string_field_set(sounds, leave, sound_file);
904         } else if (!strcasecmp(sound_name, "sound_participants_muted")) {
905                 ast_string_field_set(sounds, participantsmuted, sound_file);
906         } else if (!strcasecmp(sound_name, "sound_participants_unmuted")) {
907                 ast_string_field_set(sounds, participantsunmuted, sound_file);
908         } else if (!strcasecmp(sound_name, "sound_begin")) {
909                 ast_string_field_set(sounds, begin, sound_file);
910         } else {
911                 return -1;
912         }
913
914         return 0;
915 }
916
917 /*! CONFBRIDGE dialplan function functions and channel datastore. */
918 struct func_confbridge_data {
919         struct bridge_profile b_profile;
920         struct user_profile u_profile;
921         struct conf_menu *menu;
922         unsigned int b_usable:1; /*!< Tells if bridge profile is usable or not */
923         unsigned int u_usable:1; /*!< Tells if user profile is usable or not */
924         unsigned int m_usable:1; /*!< Tells if menu profile is usable or not */
925 };
926
927 static void func_confbridge_data_destructor(struct func_confbridge_data *b_data)
928 {
929         conf_bridge_profile_destroy(&b_data->b_profile);
930         ao2_cleanup(b_data->menu);
931         ast_free(b_data);
932 }
933
934 static void func_confbridge_destroy_cb(void *data)
935 {
936         struct func_confbridge_data *b_data = data;
937         func_confbridge_data_destructor(b_data);
938 };
939
940 static const struct ast_datastore_info confbridge_datastore = {
941         .type = "confbridge",
942         .destroy = func_confbridge_destroy_cb
943 };
944 int func_confbridge_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
945 {
946         struct ast_datastore *datastore;
947         struct func_confbridge_data *b_data;
948         char *parse;
949         struct ast_variable tmpvar = { 0, };
950         AST_DECLARE_APP_ARGS(args,
951                 AST_APP_ARG(type);
952                 AST_APP_ARG(option);
953         );
954
955         /* parse all the required arguments and make sure they exist. */
956         if (ast_strlen_zero(data)) {
957                 return -1;
958         }
959         parse = ast_strdupa(data);
960         AST_STANDARD_APP_ARGS(args, parse);
961         if (ast_strlen_zero(args.type) || ast_strlen_zero(args.option)) {
962                 return -1;
963         }
964
965         ast_channel_lock(chan);
966         datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL);
967         if (!datastore) {
968                 datastore = ast_datastore_alloc(&confbridge_datastore, NULL);
969                 if (!datastore) {
970                         ast_channel_unlock(chan);
971                         return 0;
972                 }
973                 b_data = ast_calloc(1, sizeof(*b_data));
974                 if (!b_data) {
975                         ast_channel_unlock(chan);
976                         ast_datastore_free(datastore);
977                         return 0;
978                 }
979                 datastore->data = b_data;
980                 b_data->b_profile.sounds = bridge_profile_sounds_alloc();
981                 if (!b_data->b_profile.sounds) {
982                         ast_channel_unlock(chan);
983                         ast_datastore_free(datastore);
984                         return 0;
985                 }
986                 if (!(b_data->menu = menu_alloc("dialplan"))) {
987                         ast_channel_unlock(chan);
988                         ast_datastore_free(datastore);
989                         return 0;
990                 }
991                 ast_channel_datastore_add(chan, datastore);
992         } else {
993                 b_data = datastore->data;
994         }
995         ast_channel_unlock(chan);
996
997         /* SET(CONFBRIDGE(type,option)=value) */
998         if (!value) {
999                 value = "";
1000         }
1001         tmpvar.name = args.option;
1002         tmpvar.value = value;
1003         tmpvar.file = "CONFBRIDGE";
1004         if (!strcasecmp(args.type, "bridge")) {
1005                 if (!strcasecmp(args.option, "clear")) {
1006                         b_data->b_usable = 0;
1007                         conf_bridge_profile_destroy(&b_data->b_profile);
1008                         memset(&b_data->b_profile, 0, sizeof(b_data->b_profile)) ;
1009                         if (!(b_data->b_profile.sounds = bridge_profile_sounds_alloc())) {
1010                                 /* If this reallocation fails, the datastore has become unusable and must be destroyed. */
1011                                 ast_channel_lock(chan);
1012                                 ast_channel_datastore_remove(chan, datastore);
1013                                 ast_channel_unlock(chan);
1014                                 ast_datastore_free(datastore);
1015                         }
1016                         return 0;
1017                 } else if (!aco_process_var(&bridge_type, "dialplan", &tmpvar, &b_data->b_profile)) {
1018                         b_data->b_usable = 1;
1019                         return 0;
1020                 }
1021         } else if (!strcasecmp(args.type, "user")) {
1022                 if (!strcasecmp(args.option, "clear")) {
1023                         b_data->u_usable = 0;
1024                         user_profile_destructor(&b_data->u_profile);
1025                         memset(&b_data->u_profile, 0, sizeof(b_data->u_profile));
1026                         return 0;
1027                 } else if (!aco_process_var(&user_type, "dialplan", &tmpvar, &b_data->u_profile)) {
1028                         b_data->u_usable = 1;
1029                         return 0;
1030                 }
1031         } else if (!strcasecmp(args.type, "menu")) {
1032                 if (!strcasecmp(args.option, "clear")) {
1033                         b_data->m_usable = 0;
1034                         ao2_cleanup(b_data->menu);
1035                         if (!(b_data->menu = menu_alloc("dialplan"))) {
1036                                 /* If this reallocation fails, the datastore has become unusable and must be destroyed */
1037                                 ast_channel_lock(chan);
1038                                 ast_channel_datastore_remove(chan, datastore);
1039                                 ast_channel_unlock(chan);
1040                                 ast_datastore_free(datastore);
1041                         }
1042                         return 0;
1043                 } else if (!aco_process_var(&menu_type, "dialplan", &tmpvar, b_data->menu)) {
1044                         b_data->m_usable = 1;
1045                         return 0;
1046                 }
1047         }
1048
1049         ast_log(LOG_WARNING, "%s(%s,%s) cannot be set to '%s'. Invalid type, option, or value.\n",
1050                 cmd, args.type, args.option, value);
1051         return -1;
1052 }
1053
1054 static int add_action_to_menu_entry(struct conf_menu_entry *menu_entry, enum conf_menu_action_id id, char *databuf)
1055 {
1056         struct conf_menu_action *menu_action = ast_calloc(1, sizeof(*menu_action));
1057
1058         if (!menu_action) {
1059                 return -1;
1060         }
1061         menu_action->id = id;
1062
1063         switch (id) {
1064         case MENU_ACTION_NOOP:
1065         case MENU_ACTION_TOGGLE_MUTE:
1066         case MENU_ACTION_INCREASE_LISTENING:
1067         case MENU_ACTION_DECREASE_LISTENING:
1068         case MENU_ACTION_INCREASE_TALKING:
1069         case MENU_ACTION_DECREASE_TALKING:
1070         case MENU_ACTION_RESET_LISTENING:
1071         case MENU_ACTION_RESET_TALKING:
1072         case MENU_ACTION_ADMIN_TOGGLE_LOCK:
1073         case MENU_ACTION_ADMIN_TOGGLE_MUTE_PARTICIPANTS:
1074         case MENU_ACTION_PARTICIPANT_COUNT:
1075         case MENU_ACTION_ADMIN_KICK_LAST:
1076         case MENU_ACTION_LEAVE:
1077         case MENU_ACTION_SET_SINGLE_VIDEO_SRC:
1078         case MENU_ACTION_RELEASE_SINGLE_VIDEO_SRC:
1079                 break;
1080         case MENU_ACTION_PLAYBACK:
1081         case MENU_ACTION_PLAYBACK_AND_CONTINUE:
1082                 if (!(ast_strlen_zero(databuf))) {
1083                         ast_copy_string(menu_action->data.playback_file, databuf, sizeof(menu_action->data.playback_file));
1084                 } else {
1085                         ast_free(menu_action);
1086                         return -1;
1087                 }
1088                 break;
1089         case MENU_ACTION_DIALPLAN_EXEC:
1090                 if (!(ast_strlen_zero(databuf))) {
1091                         AST_DECLARE_APP_ARGS(args,
1092                                 AST_APP_ARG(context);
1093                                 AST_APP_ARG(exten);
1094                                 AST_APP_ARG(priority);
1095                         );
1096                         AST_STANDARD_APP_ARGS(args, databuf);
1097                         if (!ast_strlen_zero(args.context)) {
1098                                 ast_copy_string(menu_action->data.dialplan_args.context,
1099                                         args.context,
1100                                         sizeof(menu_action->data.dialplan_args.context));
1101                         }
1102                         if (!ast_strlen_zero(args.exten)) {
1103                                 ast_copy_string(menu_action->data.dialplan_args.exten,
1104                                         args.exten,
1105                                         sizeof(menu_action->data.dialplan_args.exten));
1106                         }
1107                         menu_action->data.dialplan_args.priority = 1; /* 1 by default */
1108                         if (!ast_strlen_zero(args.priority) &&
1109                                 (sscanf(args.priority, "%30u", &menu_action->data.dialplan_args.priority) != 1)) {
1110                                 /* invalid priority */
1111                                 ast_free(menu_action);
1112                                 return -1;
1113                         }
1114                 } else {
1115                         ast_free(menu_action);
1116                         return -1;
1117                 }
1118         };
1119
1120         AST_LIST_INSERT_TAIL(&menu_entry->actions, menu_action, action);
1121
1122         return 0;
1123 }
1124
1125 static int add_menu_entry(struct conf_menu *menu, const char *dtmf, const char *action_names)
1126 {
1127         struct conf_menu_entry *menu_entry = NULL, *cur = NULL;
1128         int res = 0;
1129         char *tmp_action_names = ast_strdupa(action_names);
1130         char *action = NULL;
1131         char *action_args;
1132         char *tmp;
1133         char buf[PATH_MAX];
1134         char *delimiter = ",";
1135
1136         if (!(menu_entry = ast_calloc(1, sizeof(*menu_entry)))) {
1137                 return -1;
1138         }
1139
1140         for (;;) {
1141                 char *comma;
1142                 char *startbrace;
1143                 char *endbrace;
1144                 unsigned int action_len;
1145
1146                 if (ast_strlen_zero(tmp_action_names)) {
1147                         break;
1148                 }
1149                 startbrace = strchr(tmp_action_names, '(');
1150                 endbrace = strchr(tmp_action_names, ')');
1151                 comma = strchr(tmp_action_names, ',');
1152
1153                 /* If the next action has brackets with comma delimited arguments in it,
1154                  * make the delimeter ')' instead of a comma to preserve the argments */
1155                 if (startbrace && endbrace && comma && (comma > startbrace && comma < endbrace)) {
1156                         delimiter = ")";
1157                 } else {
1158                         delimiter = ",";
1159                 }
1160
1161                 if (!(action = strsep(&tmp_action_names, delimiter))) {
1162                         break;
1163                 }
1164
1165                 action = ast_strip(action);
1166                 if (ast_strlen_zero(action)) {
1167                         continue;
1168                 }
1169
1170                 action_len = strlen(action);
1171                 ast_copy_string(menu_entry->dtmf, dtmf, sizeof(menu_entry->dtmf));
1172                 if (!strcasecmp(action, "toggle_mute")) {
1173                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_TOGGLE_MUTE, NULL);
1174                 } else if (!strcasecmp(action, "no_op")) {
1175                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_NOOP, NULL);
1176                 } else if (!strcasecmp(action, "increase_listening_volume")) {
1177                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_INCREASE_LISTENING, NULL);
1178                 } else if (!strcasecmp(action, "decrease_listening_volume")) {
1179                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_DECREASE_LISTENING, NULL);
1180                 } else if (!strcasecmp(action, "increase_talking_volume")) {
1181                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_INCREASE_TALKING, NULL);
1182                 } else if (!strcasecmp(action, "reset_listening_volume")) {
1183                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_RESET_LISTENING, NULL);
1184                 } else if (!strcasecmp(action, "reset_talking_volume")) {
1185                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_RESET_TALKING, NULL);
1186                 } else if (!strcasecmp(action, "decrease_talking_volume")) {
1187                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_DECREASE_TALKING, NULL);
1188                 } else if (!strcasecmp(action, "admin_toggle_conference_lock")) {
1189                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_ADMIN_TOGGLE_LOCK, NULL);
1190                 } else if (!strcasecmp(action, "admin_toggle_mute_participants")) {
1191                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_ADMIN_TOGGLE_MUTE_PARTICIPANTS, NULL);
1192                 } else if (!strcasecmp(action, "participant_count")) {
1193                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_PARTICIPANT_COUNT, NULL);
1194                 } else if (!strcasecmp(action, "admin_kick_last")) {
1195                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_ADMIN_KICK_LAST, NULL);
1196                 } else if (!strcasecmp(action, "leave_conference")) {
1197                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_LEAVE, NULL);
1198                 } else if (!strcasecmp(action, "set_as_single_video_src")) {
1199                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_SET_SINGLE_VIDEO_SRC, NULL);
1200                 } else if (!strcasecmp(action, "release_as_single_video_src")) {
1201                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_RELEASE_SINGLE_VIDEO_SRC, NULL);
1202                 } else if (!strncasecmp(action, "dialplan_exec(", 14)) {
1203                         ast_copy_string(buf, action, sizeof(buf));
1204                         action_args = buf;
1205                         if ((action_args = strchr(action, '('))) {
1206                                 action_args++;
1207                         }
1208                         /* it is possible that this argument may or may not
1209                          * have a closing brace at this point, it all depends on if
1210                          * comma delimited arguments were provided */
1211                         if ((tmp = strchr(action, ')'))) {
1212                                 *tmp = '\0';
1213                         }
1214                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_DIALPLAN_EXEC, action_args);
1215                 } else if (action_len >= 21 && !strncasecmp(action, "playback_and_continue(", 22)) {
1216                         ast_copy_string(buf, action, sizeof(buf));
1217                         action_args = buf;
1218                         if ((action_args = strchr(action, '(')) && (tmp = strrchr(action_args, ')'))) {
1219                                 *tmp = '\0';
1220                                 action_args++;
1221                         }
1222                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_PLAYBACK_AND_CONTINUE, action_args);
1223                 } else if (action_len >= 8 && !strncasecmp(action, "playback(", 9)) {
1224                         ast_copy_string(buf, action, sizeof(buf));
1225                         action_args = buf;
1226                         if ((action_args = strchr(action, '(')) && (tmp = strrchr(action_args, ')'))) {
1227                                 *tmp = '\0';
1228                                 action_args++;
1229                         }
1230                         res |= add_action_to_menu_entry(menu_entry, MENU_ACTION_PLAYBACK, action_args);
1231                 }
1232         }
1233
1234         /* if adding any of the actions failed, bail */
1235         if (res) {
1236                 struct conf_menu_action *menu_action;
1237                 while ((menu_action = AST_LIST_REMOVE_HEAD(&menu_entry->actions, action))) {
1238                         ast_free(menu_action);
1239                 }
1240                 ast_free(menu_entry);
1241                 return -1;
1242         }
1243
1244         /* remove any list entry with an identical DTMF sequence for overrides */
1245         AST_LIST_TRAVERSE_SAFE_BEGIN(&menu->entries, cur, entry) {
1246                 if (!strcasecmp(cur->dtmf, menu_entry->dtmf)) {
1247                         AST_LIST_REMOVE_CURRENT(entry);
1248                         ast_free(cur);
1249                         break;
1250                 }
1251         }
1252         AST_LIST_TRAVERSE_SAFE_END;
1253
1254         AST_LIST_INSERT_TAIL(&menu->entries, menu_entry, entry);
1255
1256         return 0;
1257 }
1258
1259 static char *complete_user_profile_name(const char *line, const char *word, int pos, int state)
1260 {
1261         int which = 0;
1262         char *res = NULL;
1263         int wordlen = strlen(word);
1264         struct ao2_iterator i;
1265         struct user_profile *u_profile = NULL;
1266         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
1267
1268         if (!cfg) {
1269                 return NULL;
1270         }
1271
1272         i = ao2_iterator_init(cfg->user_profiles, 0);
1273         while ((u_profile = ao2_iterator_next(&i))) {
1274                 if (!strncasecmp(u_profile->name, word, wordlen) && ++which > state) {
1275                         res = ast_strdup(u_profile->name);
1276                         ao2_ref(u_profile, -1);
1277                         break;
1278                 }
1279                 ao2_ref(u_profile, -1);
1280         }
1281         ao2_iterator_destroy(&i);
1282
1283         return res;
1284 }
1285
1286 static char *handle_cli_confbridge_show_user_profiles(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1287 {
1288         struct ao2_iterator it;
1289         struct user_profile *u_profile;
1290         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
1291
1292         switch (cmd) {
1293         case CLI_INIT:
1294                 e->command = "confbridge show profile users";
1295                 e->usage =
1296                         "Usage confbridge show profile users\n";
1297                 return NULL;
1298         case CLI_GENERATE:
1299                 return NULL;
1300         }
1301
1302         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
1303                 return NULL;
1304         }
1305
1306         ast_cli(a->fd,"--------- User Profiles -----------\n");
1307         ao2_lock(cfg->user_profiles);
1308         it = ao2_iterator_init(cfg->user_profiles, 0);
1309         while ((u_profile = ao2_iterator_next(&it))) {
1310                 ast_cli(a->fd,"%s\n", u_profile->name);
1311                 ao2_ref(u_profile, -1);
1312         }
1313         ao2_iterator_destroy(&it);
1314         ao2_unlock(cfg->user_profiles);
1315
1316         return CLI_SUCCESS;
1317 }
1318 static char *handle_cli_confbridge_show_user_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1319 {
1320         struct user_profile u_profile;
1321
1322         switch (cmd) {
1323         case CLI_INIT:
1324                 e->command = "confbridge show profile user";
1325                 e->usage =
1326                         "Usage confbridge show profile user [<profile name>]\n";
1327                 return NULL;
1328         case CLI_GENERATE:
1329                 if (a->pos == 4) {
1330                         return complete_user_profile_name(a->line, a->word, a->pos, a->n);
1331                 }
1332                 return NULL;
1333         }
1334
1335         if (a->argc != 5) {
1336                 return CLI_SHOWUSAGE;
1337         }
1338
1339         if (!(conf_find_user_profile(NULL, a->argv[4], &u_profile))) {
1340                 ast_cli(a->fd, "No conference user profile named '%s' found!\n", a->argv[4]);
1341                 return CLI_SUCCESS;
1342         }
1343
1344         ast_cli(a->fd,"--------------------------------------------\n");
1345         ast_cli(a->fd,"Name:                    %s\n",
1346                 u_profile.name);
1347         ast_cli(a->fd,"Admin:                   %s\n",
1348                 u_profile.flags & USER_OPT_ADMIN ?
1349                 "true" : "false");
1350         ast_cli(a->fd,"Marked User:             %s\n",
1351                 u_profile.flags & USER_OPT_MARKEDUSER ?
1352                 "true" : "false");
1353         ast_cli(a->fd,"Start Muted:             %s\n",
1354                 u_profile.flags & USER_OPT_STARTMUTED?
1355                 "true" : "false");
1356         ast_cli(a->fd,"MOH When Empty:          %s\n",
1357                 u_profile.flags & USER_OPT_MUSICONHOLD ?
1358                 "enabled" : "disabled");
1359         ast_cli(a->fd,"MOH Class:               %s\n",
1360                 ast_strlen_zero(u_profile.moh_class) ?
1361                 "default" : u_profile.moh_class);
1362         ast_cli(a->fd,"Announcement:            %s\n",
1363                 u_profile.announcement);
1364         ast_cli(a->fd,"Quiet:                   %s\n",
1365                 u_profile.flags & USER_OPT_QUIET ?
1366                 "enabled" : "disabled");
1367         ast_cli(a->fd,"Wait Marked:             %s\n",
1368                 u_profile.flags & USER_OPT_WAITMARKED ?
1369                 "enabled" : "disabled");
1370         ast_cli(a->fd,"END Marked:              %s\n",
1371                 u_profile.flags & USER_OPT_ENDMARKED ?
1372                 "enabled" : "disabled");
1373         ast_cli(a->fd,"Drop_silence:            %s\n",
1374                 u_profile.flags & USER_OPT_DROP_SILENCE ?
1375                 "enabled" : "disabled");
1376         ast_cli(a->fd,"Silence Threshold:       %dms\n",
1377                 u_profile.silence_threshold);
1378         ast_cli(a->fd,"Talking Threshold:       %dms\n",
1379                 u_profile.talking_threshold);
1380         ast_cli(a->fd,"Denoise:                 %s\n",
1381                 u_profile.flags & USER_OPT_DENOISE ?
1382                 "enabled" : "disabled");
1383         ast_cli(a->fd,"Jitterbuffer:            %s\n",
1384                 u_profile.flags & USER_OPT_JITTERBUFFER ?
1385                 "enabled" : "disabled");
1386         ast_cli(a->fd,"Talk Detect Events:      %s\n",
1387                 u_profile.flags & USER_OPT_TALKER_DETECT ?
1388                 "enabled" : "disabled");
1389         ast_cli(a->fd,"DTMF Pass Through:       %s\n",
1390                 u_profile.flags & USER_OPT_DTMF_PASS ?
1391                 "enabled" : "disabled");
1392         ast_cli(a->fd,"PIN:                     %s\n",
1393                 ast_strlen_zero(u_profile.pin) ?
1394                 "None" : u_profile.pin);
1395         ast_cli(a->fd,"Announce User Count:     %s\n",
1396                 u_profile.flags & USER_OPT_ANNOUNCEUSERCOUNT ?
1397                 "enabled" : "disabled");
1398         ast_cli(a->fd,"Announce join/leave:     %s\n",
1399                 u_profile.flags & (USER_OPT_ANNOUNCE_JOIN_LEAVE | USER_OPT_ANNOUNCE_JOIN_LEAVE_REVIEW) ?
1400                 u_profile.flags & USER_OPT_ANNOUNCE_JOIN_LEAVE_REVIEW ?
1401                 "enabled (with review)" : "enabled" : "disabled");
1402         ast_cli(a->fd,"Announce User Count all: %s\n",
1403                 u_profile.flags & USER_OPT_ANNOUNCEUSERCOUNTALL ?
1404                 "enabled" : "disabled");
1405                 ast_cli(a->fd,"\n");
1406
1407         return CLI_SUCCESS;
1408 }
1409
1410 static char *complete_bridge_profile_name(const char *line, const char *word, int pos, int state)
1411 {
1412         int which = 0;
1413         char *res = NULL;
1414         int wordlen = strlen(word);
1415         struct ao2_iterator i;
1416         struct bridge_profile *b_profile = NULL;
1417         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
1418
1419         if (!cfg) {
1420                 return NULL;
1421         }
1422
1423         i = ao2_iterator_init(cfg->bridge_profiles, 0);
1424         while ((b_profile = ao2_iterator_next(&i))) {
1425                 if (!strncasecmp(b_profile->name, word, wordlen) && ++which > state) {
1426                         res = ast_strdup(b_profile->name);
1427                         ao2_ref(b_profile, -1);
1428                         break;
1429                 }
1430                 ao2_ref(b_profile, -1);
1431         }
1432         ao2_iterator_destroy(&i);
1433
1434         return res;
1435 }
1436
1437 static char *handle_cli_confbridge_show_bridge_profiles(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1438 {
1439         struct ao2_iterator it;
1440         struct bridge_profile *b_profile;
1441         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
1442
1443         switch (cmd) {
1444         case CLI_INIT:
1445                 e->command = "confbridge show profile bridges";
1446                 e->usage =
1447                         "Usage confbridge show profile bridges\n";
1448                 return NULL;
1449         case CLI_GENERATE:
1450                 return NULL;
1451         }
1452
1453         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
1454                 return NULL;
1455         }
1456
1457         ast_cli(a->fd,"--------- Bridge Profiles -----------\n");
1458         ao2_lock(cfg->bridge_profiles);
1459         it = ao2_iterator_init(cfg->bridge_profiles, 0);
1460         while ((b_profile = ao2_iterator_next(&it))) {
1461                 ast_cli(a->fd,"%s\n", b_profile->name);
1462                 ao2_ref(b_profile, -1);
1463         }
1464         ao2_iterator_destroy(&it);
1465         ao2_unlock(cfg->bridge_profiles);
1466
1467         return CLI_SUCCESS;
1468 }
1469
1470 static char *handle_cli_confbridge_show_bridge_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1471 {
1472         struct bridge_profile b_profile;
1473         char tmp[64];
1474
1475         switch (cmd) {
1476         case CLI_INIT:
1477                 e->command = "confbridge show profile bridge";
1478                 e->usage =
1479                         "Usage confbridge show profile bridge <profile name>\n";
1480                 return NULL;
1481         case CLI_GENERATE:
1482                 if (a->pos == 4) {
1483                         return complete_bridge_profile_name(a->line, a->word, a->pos, a->n);
1484                 }
1485                 return NULL;
1486         }
1487
1488         if (a->argc != 5) {
1489                 return CLI_SHOWUSAGE;
1490         }
1491
1492         if (!(conf_find_bridge_profile(NULL, a->argv[4], &b_profile))) {
1493                 ast_cli(a->fd, "No conference bridge profile named '%s' found!\n", a->argv[4]);
1494                 return CLI_SUCCESS;
1495         }
1496
1497         ast_cli(a->fd,"--------------------------------------------\n");
1498         ast_cli(a->fd,"Name:                 %s\n", b_profile.name);
1499         ast_cli(a->fd,"Language:             %s\n", b_profile.language);
1500
1501         if (b_profile.internal_sample_rate) {
1502                 snprintf(tmp, sizeof(tmp), "%d", b_profile.internal_sample_rate);
1503         } else {
1504                 ast_copy_string(tmp, "auto", sizeof(tmp));
1505         }
1506         ast_cli(a->fd,"Internal Sample Rate: %s\n", tmp);
1507
1508         if (b_profile.mix_interval) {
1509                 ast_cli(a->fd,"Mixing Interval:      %d\n", b_profile.mix_interval);
1510         } else {
1511                 ast_cli(a->fd,"Mixing Interval:      Default 20ms\n");
1512         }
1513
1514         ast_cli(a->fd,"Record Conference:    %s\n",
1515                 b_profile.flags & BRIDGE_OPT_RECORD_CONFERENCE ?
1516                 "yes" : "no");
1517
1518         ast_cli(a->fd,"Record File Append:    %s\n",
1519                 b_profile.flags & BRIDGE_OPT_RECORD_FILE_APPEND ?
1520                 "yes" : "no");
1521
1522         ast_cli(a->fd,"Record File:          %s\n",
1523                 ast_strlen_zero(b_profile.rec_file) ? "Auto Generated" :
1524                 b_profile.rec_file);
1525
1526         if (b_profile.max_members) {
1527                 ast_cli(a->fd,"Max Members:          %d\n", b_profile.max_members);
1528         } else {
1529                 ast_cli(a->fd,"Max Members:          No Limit\n");
1530         }
1531
1532         switch (b_profile.flags
1533                 & (BRIDGE_OPT_VIDEO_SRC_LAST_MARKED | BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED
1534                         | BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER)) {
1535         case BRIDGE_OPT_VIDEO_SRC_LAST_MARKED:
1536                 ast_cli(a->fd, "Video Mode:           last_marked\n");
1537                 break;
1538         case BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED:
1539                 ast_cli(a->fd, "Video Mode:           first_marked\n");
1540                 break;
1541         case BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER:
1542                 ast_cli(a->fd, "Video Mode:           follow_talker\n");
1543                 break;
1544         case 0:
1545                 ast_cli(a->fd, "Video Mode:           no video\n");
1546                 break;
1547         default:
1548                 /* Opps.  We have more than one video mode flag set. */
1549                 ast_assert(0);
1550                 break;
1551         }
1552
1553         ast_cli(a->fd,"sound_only_person:    %s\n", conf_get_sound(CONF_SOUND_ONLY_PERSON, b_profile.sounds));
1554         ast_cli(a->fd,"sound_only_one:       %s\n", conf_get_sound(CONF_SOUND_ONLY_ONE, b_profile.sounds));
1555         ast_cli(a->fd,"sound_has_joined:     %s\n", conf_get_sound(CONF_SOUND_HAS_JOINED, b_profile.sounds));
1556         ast_cli(a->fd,"sound_has_left:       %s\n", conf_get_sound(CONF_SOUND_HAS_LEFT, b_profile.sounds));
1557         ast_cli(a->fd,"sound_kicked:         %s\n", conf_get_sound(CONF_SOUND_KICKED, b_profile.sounds));
1558         ast_cli(a->fd,"sound_muted:          %s\n", conf_get_sound(CONF_SOUND_MUTED, b_profile.sounds));
1559         ast_cli(a->fd,"sound_unmuted:        %s\n", conf_get_sound(CONF_SOUND_UNMUTED, b_profile.sounds));
1560         ast_cli(a->fd,"sound_there_are:      %s\n", conf_get_sound(CONF_SOUND_THERE_ARE, b_profile.sounds));
1561         ast_cli(a->fd,"sound_other_in_party: %s\n", conf_get_sound(CONF_SOUND_OTHER_IN_PARTY, b_profile.sounds));
1562         ast_cli(a->fd,"sound_place_into_conference: %s\n", conf_get_sound(CONF_SOUND_PLACE_IN_CONF, b_profile.sounds));
1563         ast_cli(a->fd,"sound_wait_for_leader:       %s\n", conf_get_sound(CONF_SOUND_WAIT_FOR_LEADER, b_profile.sounds));
1564         ast_cli(a->fd,"sound_leader_has_left:       %s\n", conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, b_profile.sounds));
1565         ast_cli(a->fd,"sound_get_pin:        %s\n", conf_get_sound(CONF_SOUND_GET_PIN, b_profile.sounds));
1566         ast_cli(a->fd,"sound_invalid_pin:    %s\n", conf_get_sound(CONF_SOUND_INVALID_PIN, b_profile.sounds));
1567         ast_cli(a->fd,"sound_locked:         %s\n", conf_get_sound(CONF_SOUND_LOCKED, b_profile.sounds));
1568         ast_cli(a->fd,"sound_unlocked_now:   %s\n", conf_get_sound(CONF_SOUND_UNLOCKED_NOW, b_profile.sounds));
1569         ast_cli(a->fd,"sound_lockednow:      %s\n", conf_get_sound(CONF_SOUND_LOCKED_NOW, b_profile.sounds));
1570         ast_cli(a->fd,"sound_error_menu:     %s\n", conf_get_sound(CONF_SOUND_ERROR_MENU, b_profile.sounds));
1571         ast_cli(a->fd,"sound_join:           %s\n", conf_get_sound(CONF_SOUND_JOIN, b_profile.sounds));
1572         ast_cli(a->fd,"sound_leave:          %s\n", conf_get_sound(CONF_SOUND_LEAVE, b_profile.sounds));
1573         ast_cli(a->fd,"sound_participants_muted:     %s\n", conf_get_sound(CONF_SOUND_PARTICIPANTS_MUTED, b_profile.sounds));
1574         ast_cli(a->fd,"sound_participants_unmuted:     %s\n", conf_get_sound(CONF_SOUND_PARTICIPANTS_UNMUTED, b_profile.sounds));
1575         ast_cli(a->fd,"sound_begin:          %s\n", conf_get_sound(CONF_SOUND_BEGIN, b_profile.sounds));
1576         ast_cli(a->fd,"\n");
1577
1578         conf_bridge_profile_destroy(&b_profile);
1579         return CLI_SUCCESS;
1580 }
1581
1582 static char *complete_menu_name(const char *line, const char *word, int pos, int state)
1583 {
1584         int which = 0;
1585         char *res = NULL;
1586         int wordlen = strlen(word);
1587         struct ao2_iterator i;
1588         struct conf_menu *menu = NULL;
1589         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
1590
1591         if (!cfg) {
1592                 return NULL;
1593         }
1594
1595         i = ao2_iterator_init(cfg->menus, 0);
1596         while ((menu = ao2_iterator_next(&i))) {
1597                 if (!strncasecmp(menu->name, word, wordlen) && ++which > state) {
1598                         res = ast_strdup(menu->name);
1599                         ao2_ref(menu, -1);
1600                         break;
1601                 }
1602                 ao2_ref(menu, -1);
1603         }
1604         ao2_iterator_destroy(&i);
1605
1606         return res;
1607 }
1608
1609 static char *handle_cli_confbridge_show_menus(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1610 {
1611         struct ao2_iterator it;
1612         struct conf_menu *menu;
1613         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
1614
1615         switch (cmd) {
1616         case CLI_INIT:
1617                 e->command = "confbridge show menus";
1618                 e->usage =
1619                         "Usage confbridge show profile menus\n";
1620                 return NULL;
1621         case CLI_GENERATE:
1622                 return NULL;
1623         }
1624
1625         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
1626                 return NULL;
1627         }
1628
1629         ast_cli(a->fd,"--------- Menus -----------\n");
1630         ao2_lock(cfg->menus);
1631         it = ao2_iterator_init(cfg->menus, 0);
1632         while ((menu = ao2_iterator_next(&it))) {
1633                 ast_cli(a->fd,"%s\n", menu->name);
1634                 ao2_ref(menu, -1);
1635         }
1636         ao2_iterator_destroy(&it);
1637         ao2_unlock(cfg->menus);
1638
1639         return CLI_SUCCESS;
1640 }
1641
1642 static char *handle_cli_confbridge_show_menu(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1643 {
1644         RAII_VAR(struct conf_menu *, menu, NULL, ao2_cleanup);
1645         RAII_VAR(struct confbridge_cfg *, cfg, NULL, ao2_cleanup);
1646         struct conf_menu_entry *menu_entry = NULL;
1647         struct conf_menu_action *menu_action = NULL;
1648
1649         switch (cmd) {
1650         case CLI_INIT:
1651                 e->command = "confbridge show menu";
1652                 e->usage =
1653                         "Usage confbridge show menu [<menu name>]\n";
1654                 return NULL;
1655         case CLI_GENERATE:
1656                 if (a->pos == 3) {
1657                         return complete_menu_name(a->line, a->word, a->pos, a->n);
1658                 }
1659                 return NULL;
1660         }
1661
1662         if (a->argc != 4) {
1663                 return CLI_SHOWUSAGE;
1664         }
1665
1666         if (!(cfg = ao2_global_obj_ref(cfg_handle))) {
1667                 return NULL;
1668         }
1669
1670         if (!(menu = menu_find(cfg->menus, a->argv[3]))) {
1671                 ast_cli(a->fd, "No conference menu named '%s' found!\n", a->argv[3]);
1672                 return CLI_SUCCESS;
1673         }
1674         ao2_lock(menu);
1675
1676         ast_cli(a->fd,"Name: %s\n", menu->name);
1677         AST_LIST_TRAVERSE(&menu->entries, menu_entry, entry) {
1678                 int action_num = 0;
1679                 ast_cli(a->fd, "%s=", menu_entry->dtmf);
1680                 AST_LIST_TRAVERSE(&menu_entry->actions, menu_action, action) {
1681                         if (action_num) {
1682                                 ast_cli(a->fd, ", ");
1683                         }
1684                         switch (menu_action->id) {
1685                         case MENU_ACTION_TOGGLE_MUTE:
1686                                 ast_cli(a->fd, "toggle_mute");
1687                                 break;
1688                         case MENU_ACTION_NOOP:
1689                                 ast_cli(a->fd, "no_op");
1690                                 break;
1691                         case MENU_ACTION_INCREASE_LISTENING:
1692                                 ast_cli(a->fd, "increase_listening_volume");
1693                                 break;
1694                         case MENU_ACTION_DECREASE_LISTENING:
1695                                 ast_cli(a->fd, "decrease_listening_volume");
1696                                 break;
1697                         case MENU_ACTION_RESET_LISTENING:
1698                                 ast_cli(a->fd, "reset_listening_volume");
1699                                 break;
1700                         case MENU_ACTION_RESET_TALKING:
1701                                 ast_cli(a->fd, "reset_talking_volume");
1702                                 break;
1703                         case MENU_ACTION_INCREASE_TALKING:
1704                                 ast_cli(a->fd, "increase_talking_volume");
1705                                 break;
1706                         case MENU_ACTION_DECREASE_TALKING:
1707                                 ast_cli(a->fd, "decrease_talking_volume");
1708                                 break;
1709                         case MENU_ACTION_PLAYBACK:
1710                                 ast_cli(a->fd, "playback(%s)", menu_action->data.playback_file);
1711                                 break;
1712                         case MENU_ACTION_PLAYBACK_AND_CONTINUE:
1713                                 ast_cli(a->fd, "playback_and_continue(%s)", menu_action->data.playback_file);
1714                                 break;
1715                         case MENU_ACTION_DIALPLAN_EXEC:
1716                                 ast_cli(a->fd, "dialplan_exec(%s,%s,%d)",
1717                                         menu_action->data.dialplan_args.context,
1718                                         menu_action->data.dialplan_args.exten,
1719                                         menu_action->data.dialplan_args.priority);
1720                                 break;
1721                         case MENU_ACTION_ADMIN_TOGGLE_LOCK:
1722                                 ast_cli(a->fd, "admin_toggle_conference_lock");
1723                                 break;
1724                         case MENU_ACTION_ADMIN_TOGGLE_MUTE_PARTICIPANTS:
1725                                 ast_cli(a->fd, "admin_toggle_mute_participants");
1726                                 break;
1727                         case MENU_ACTION_PARTICIPANT_COUNT:
1728                                 ast_cli(a->fd, "participant_count");
1729                                 break;
1730                         case MENU_ACTION_ADMIN_KICK_LAST:
1731                                 ast_cli(a->fd, "admin_kick_last");
1732                                 break;
1733                         case MENU_ACTION_LEAVE:
1734                                 ast_cli(a->fd, "leave_conference");
1735                                 break;
1736                         case MENU_ACTION_SET_SINGLE_VIDEO_SRC:
1737                                 ast_cli(a->fd, "set_as_single_video_src");
1738                                 break;
1739                         case MENU_ACTION_RELEASE_SINGLE_VIDEO_SRC:
1740                                 ast_cli(a->fd, "release_as_single_video_src");
1741                                 break;
1742                         }
1743                         action_num++;
1744                 }
1745                 ast_cli(a->fd,"\n");
1746         }
1747
1748
1749         ao2_unlock(menu);
1750         return CLI_SUCCESS;
1751 }
1752
1753 static struct ast_cli_entry cli_confbridge_parser[] = {
1754         AST_CLI_DEFINE(handle_cli_confbridge_show_user_profile, "Show a conference user profile."),
1755         AST_CLI_DEFINE(handle_cli_confbridge_show_bridge_profile, "Show a conference bridge profile."),
1756         AST_CLI_DEFINE(handle_cli_confbridge_show_menu, "Show a conference menu"),
1757         AST_CLI_DEFINE(handle_cli_confbridge_show_user_profiles, "Show a list of conference user profiles."),
1758         AST_CLI_DEFINE(handle_cli_confbridge_show_bridge_profiles, "Show a list of conference bridge profiles."),
1759         AST_CLI_DEFINE(handle_cli_confbridge_show_menus, "Show a list of conference menus"),
1760
1761 };
1762
1763 static void confbridge_cfg_destructor(void *obj)
1764 {
1765         struct confbridge_cfg *cfg = obj;
1766         ao2_cleanup(cfg->user_profiles);
1767         ao2_cleanup(cfg->bridge_profiles);
1768         ao2_cleanup(cfg->menus);
1769 }
1770
1771 void *confbridge_cfg_alloc(void)
1772 {
1773         struct confbridge_cfg *cfg;
1774
1775         if (!(cfg = ao2_alloc(sizeof(*cfg), confbridge_cfg_destructor))) {
1776                 return NULL;
1777         }
1778
1779         if (!(cfg->user_profiles = ao2_container_alloc(283, user_hash_cb, user_cmp_cb))) {
1780                 goto error;
1781         }
1782
1783         if (!(cfg->bridge_profiles = ao2_container_alloc(283, bridge_hash_cb, bridge_cmp_cb))) {
1784                 goto error;
1785         }
1786
1787         if (!(cfg->menus = ao2_container_alloc(283, menu_hash_cb, menu_cmp_cb))) {
1788                 goto error;
1789         }
1790
1791         return cfg;
1792 error:
1793         ao2_ref(cfg, -1);
1794         return NULL;
1795 }
1796
1797 static int announce_user_count_all_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1798 {
1799         struct user_profile *u_profile = obj;
1800
1801         if (strcasecmp(var->name, "announce_user_count_all")) {
1802                 return -1;
1803         }
1804         if (ast_true(var->value)) {
1805                 u_profile->flags = u_profile->flags | USER_OPT_ANNOUNCEUSERCOUNTALL;
1806         } else if (ast_false(var->value)) {
1807                 u_profile->flags = u_profile->flags & ~USER_OPT_ANNOUNCEUSERCOUNTALL;
1808         } else if (sscanf(var->value, "%30u", &u_profile->announce_user_count_all_after) == 1) {
1809                 u_profile->flags = u_profile->flags | USER_OPT_ANNOUNCEUSERCOUNTALL;
1810         } else {
1811                 return -1;
1812         }
1813         return 0;
1814 }
1815
1816 static int mix_interval_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1817 {
1818         struct bridge_profile *b_profile = obj;
1819
1820         if (strcasecmp(var->name, "mixing_interval")) {
1821                 return -1;
1822         }
1823         if (sscanf(var->value, "%30u", &b_profile->mix_interval) != 1) {
1824                 return -1;
1825         }
1826         switch (b_profile->mix_interval) {
1827         case 10:
1828         case 20:
1829         case 40:
1830         case 80:
1831                 return 0;
1832         default:
1833                 return -1;
1834         }
1835 }
1836
1837 static int video_mode_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1838 {
1839         struct bridge_profile *b_profile = obj;
1840
1841         if (strcasecmp(var->name, "video_mode")) {
1842                 return -1;
1843         }
1844         if (!strcasecmp(var->value, "first_marked")) {
1845                 ast_set_flags_to(b_profile,
1846                         BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED
1847                                 | BRIDGE_OPT_VIDEO_SRC_LAST_MARKED
1848                                 | BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER,
1849                         BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED);
1850         } else if (!strcasecmp(var->value, "last_marked")) {
1851                 ast_set_flags_to(b_profile,
1852                         BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED
1853                                 | BRIDGE_OPT_VIDEO_SRC_LAST_MARKED
1854                                 | BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER,
1855                         BRIDGE_OPT_VIDEO_SRC_LAST_MARKED);
1856         } else if (!strcasecmp(var->value, "follow_talker")) {
1857                 ast_set_flags_to(b_profile,
1858                         BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED
1859                                 | BRIDGE_OPT_VIDEO_SRC_LAST_MARKED
1860                                 | BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER,
1861                         BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER);
1862         } else if (!strcasecmp(var->value, "none")) {
1863                 ast_clear_flag(b_profile,
1864                         BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED
1865                                 | BRIDGE_OPT_VIDEO_SRC_LAST_MARKED
1866                                 | BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER);
1867         } else {
1868                 return -1;
1869         }
1870         return 0;
1871 }
1872
1873 static int user_template_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1874 {
1875         struct user_profile *u_profile = obj;
1876
1877         return conf_find_user_profile(NULL, var->value, u_profile) ? 0 : -1;
1878 }
1879
1880 static int bridge_template_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1881 {
1882         struct bridge_profile *b_profile = obj;
1883         struct bridge_profile_sounds *sounds = bridge_profile_sounds_alloc();
1884         struct bridge_profile_sounds *oldsounds = b_profile->sounds;
1885
1886         if (!sounds) {
1887                 return -1;
1888         }
1889         if (!(conf_find_bridge_profile(NULL, var->value, b_profile))) {
1890                 ao2_ref(sounds, -1);
1891                 return -1;
1892         }
1893         /* Using a bridge profile as a template is a little complicated due to the sounds. Since the sounds
1894          * structure of a dynamic profile will need to be altered, a completely new sounds structure must be
1895          * created instead of simply holding a reference to the one built by the config file. */
1896         ast_string_field_set(sounds, onlyperson, b_profile->sounds->onlyperson);
1897         ast_string_field_set(sounds, onlyone, b_profile->sounds->onlyone);
1898         ast_string_field_set(sounds, hasjoin, b_profile->sounds->hasjoin);
1899         ast_string_field_set(sounds, hasleft, b_profile->sounds->hasleft);
1900         ast_string_field_set(sounds, kicked, b_profile->sounds->kicked);
1901         ast_string_field_set(sounds, muted, b_profile->sounds->muted);
1902         ast_string_field_set(sounds, unmuted, b_profile->sounds->unmuted);
1903         ast_string_field_set(sounds, thereare, b_profile->sounds->thereare);
1904         ast_string_field_set(sounds, otherinparty, b_profile->sounds->otherinparty);
1905         ast_string_field_set(sounds, placeintoconf, b_profile->sounds->placeintoconf);
1906         ast_string_field_set(sounds, waitforleader, b_profile->sounds->waitforleader);
1907         ast_string_field_set(sounds, leaderhasleft, b_profile->sounds->leaderhasleft);
1908         ast_string_field_set(sounds, getpin, b_profile->sounds->getpin);
1909         ast_string_field_set(sounds, invalidpin, b_profile->sounds->invalidpin);
1910         ast_string_field_set(sounds, locked, b_profile->sounds->locked);
1911         ast_string_field_set(sounds, unlockednow, b_profile->sounds->unlockednow);
1912         ast_string_field_set(sounds, lockednow, b_profile->sounds->lockednow);
1913         ast_string_field_set(sounds, errormenu, b_profile->sounds->errormenu);
1914         ast_string_field_set(sounds, join, b_profile->sounds->join);
1915         ast_string_field_set(sounds, leave, b_profile->sounds->leave);
1916         ast_string_field_set(sounds, participantsmuted, b_profile->sounds->participantsmuted);
1917         ast_string_field_set(sounds, participantsunmuted, b_profile->sounds->participantsunmuted);
1918         ast_string_field_set(sounds, begin, b_profile->sounds->begin);
1919
1920         ao2_ref(b_profile->sounds, -1); /* sounds struct copied over to it from the template by reference only. */
1921         ao2_ref(oldsounds, -1);    /* original sounds struct we don't need anymore */
1922         b_profile->sounds = sounds;     /* the new sounds struct that is a deep copy of the one from the template. */
1923
1924         return 0;
1925 }
1926
1927 static int copy_menu_entry(struct conf_menu_entry *dst, struct conf_menu_entry *src)
1928 {
1929         struct conf_menu_action *menu_action;
1930         struct conf_menu_action *new_menu_action;
1931
1932         ast_copy_string(dst->dtmf, src->dtmf, sizeof(dst->dtmf));
1933         AST_LIST_HEAD_INIT_NOLOCK(&dst->actions);
1934
1935         AST_LIST_TRAVERSE(&src->actions, menu_action, action) {
1936                 if (!(new_menu_action = ast_calloc(1, sizeof(*new_menu_action)))) {
1937                         return -1;
1938                 }
1939                 memcpy(new_menu_action, menu_action, sizeof(*new_menu_action));
1940                 AST_LIST_NEXT(new_menu_action, action) = NULL;
1941                 AST_LIST_INSERT_TAIL(&dst->actions, new_menu_action, action);
1942         }
1943
1944         return 0;
1945 }
1946
1947 static int conf_menu_profile_copy(struct conf_menu *dst, struct conf_menu *src)
1948 {
1949         /* Copy each menu item to the dst struct */
1950         struct conf_menu_entry *cur;
1951
1952         AST_LIST_TRAVERSE(&src->entries, cur, entry) {
1953                 struct conf_menu_entry *cpy;
1954
1955                 if (!(cpy = ast_calloc(1, sizeof(*cpy)))) {
1956                         return -1;
1957                 }
1958
1959                 if (copy_menu_entry(cpy, cur)) {
1960                         conf_menu_entry_destroy(cpy);
1961                         ast_free(cpy);
1962                         return -1;
1963                 }
1964                 AST_LIST_INSERT_TAIL(&dst->entries, cpy, entry);
1965         }
1966
1967         return 0;
1968 }
1969
1970 static int menu_template_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1971 {
1972         struct conf_menu *dst_menu = obj;
1973         struct confbridge_cfg *cfg = aco_pending_config(&cfg_info);
1974         RAII_VAR(struct conf_menu *, src_menu, NULL, ao2_cleanup);
1975
1976         if (!cfg) {
1977                 return 0;
1978         }
1979
1980         if (!(src_menu = ao2_find(cfg->menus, var->value, OBJ_KEY))) {
1981                 return -1;
1982         }
1983
1984         if (conf_menu_profile_copy(dst_menu, src_menu)) {
1985                 return -1;
1986         }
1987
1988         return 0;
1989 }
1990
1991 static int sound_option_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1992 {
1993         set_sound(var->name, var->value, obj);
1994         return 0;
1995 }
1996
1997 static int menu_option_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1998 {
1999         add_menu_entry(obj, var->name, var->value);
2000         return 0;
2001 }
2002
2003 static int verify_default_profiles(void)
2004 {
2005         RAII_VAR(struct user_profile *, user_profile, NULL, ao2_cleanup);
2006         RAII_VAR(struct bridge_profile *, bridge_profile, NULL, ao2_cleanup);
2007         RAII_VAR(struct conf_menu *, menu_profile, NULL, ao2_cleanup);
2008         struct confbridge_cfg *cfg = aco_pending_config(&cfg_info);
2009
2010         if (!cfg) {
2011                 return 0;
2012         }
2013
2014         bridge_profile = ao2_find(cfg->bridge_profiles, DEFAULT_BRIDGE_PROFILE, OBJ_KEY);
2015         if (!bridge_profile) {
2016                 bridge_profile = bridge_profile_alloc(DEFAULT_BRIDGE_PROFILE);
2017                 if (!bridge_profile) {
2018                         return -1;
2019                 }
2020                 ast_log(AST_LOG_NOTICE, "Adding %s profile to app_confbridge\n", DEFAULT_BRIDGE_PROFILE);
2021                 aco_set_defaults(&bridge_type, DEFAULT_BRIDGE_PROFILE, bridge_profile);
2022                 ao2_link(cfg->bridge_profiles, bridge_profile);
2023         }
2024
2025         user_profile = ao2_find(cfg->user_profiles, DEFAULT_USER_PROFILE, OBJ_KEY);
2026         if (!user_profile) {
2027                 user_profile = user_profile_alloc(DEFAULT_USER_PROFILE);
2028                 if (!user_profile) {
2029                         return -1;
2030                 }
2031                 ast_log(AST_LOG_NOTICE, "Adding %s profile to app_confbridge\n", DEFAULT_USER_PROFILE);
2032                 aco_set_defaults(&user_type, DEFAULT_USER_PROFILE, user_profile);
2033                 ao2_link(cfg->user_profiles, user_profile);
2034         }
2035
2036         menu_profile = ao2_find(cfg->menus, DEFAULT_MENU_PROFILE, OBJ_KEY);
2037         if (!menu_profile) {
2038                 menu_profile = menu_alloc(DEFAULT_MENU_PROFILE);
2039                 if (!menu_profile) {
2040                         return -1;
2041                 }
2042                 ast_log(AST_LOG_NOTICE, "Adding %s menu to app_confbridge\n", DEFAULT_MENU_PROFILE);
2043                 aco_set_defaults(&menu_type, DEFAULT_MENU_PROFILE, menu_profile);
2044                 ao2_link(cfg->menus, menu_profile);
2045         }
2046
2047         return 0;
2048 }
2049
2050 int conf_load_config(void)
2051 {
2052         if (aco_info_init(&cfg_info)) {
2053                 return -1;
2054         }
2055
2056         /* User options */
2057         aco_option_register(&cfg_info, "type", ACO_EXACT, user_types, NULL, OPT_NOOP_T, 0, 0);
2058         aco_option_register(&cfg_info, "admin", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ADMIN);
2059         aco_option_register(&cfg_info, "marked", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_MARKEDUSER);
2060         aco_option_register(&cfg_info, "startmuted", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_STARTMUTED);
2061         aco_option_register(&cfg_info, "music_on_hold_when_empty", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_MUSICONHOLD);
2062         aco_option_register(&cfg_info, "quiet", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_QUIET);
2063         aco_option_register_custom(&cfg_info, "announce_user_count_all", ACO_EXACT, user_types, "no", announce_user_count_all_handler, 0);
2064         aco_option_register(&cfg_info, "announce_user_count", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ANNOUNCEUSERCOUNT);
2065         /* Negative logic. Defaults to "yes" and evaluates with ast_false(). If !ast_false(), USER_OPT_NOONLYPERSON is cleared */
2066         aco_option_register(&cfg_info, "announce_only_user", ACO_EXACT, user_types, "yes", OPT_BOOLFLAG_T, 0, FLDSET(struct user_profile, flags), USER_OPT_NOONLYPERSON);
2067         aco_option_register(&cfg_info, "wait_marked", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_WAITMARKED);
2068         aco_option_register(&cfg_info, "end_marked", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ENDMARKED);
2069         aco_option_register(&cfg_info, "talk_detection_events", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_TALKER_DETECT);
2070         aco_option_register(&cfg_info, "dtmf_passthrough", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_DTMF_PASS);
2071         aco_option_register(&cfg_info, "announce_join_leave", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ANNOUNCE_JOIN_LEAVE);
2072         aco_option_register(&cfg_info, "announce_join_leave_review", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_ANNOUNCE_JOIN_LEAVE_REVIEW);
2073         aco_option_register(&cfg_info, "pin", ACO_EXACT, user_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct user_profile, pin));
2074         aco_option_register(&cfg_info, "music_on_hold_class", ACO_EXACT, user_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct user_profile, moh_class));
2075         aco_option_register(&cfg_info, "announcement", ACO_EXACT, user_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct user_profile, announcement));
2076         aco_option_register(&cfg_info, "denoise", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_DENOISE);
2077         aco_option_register(&cfg_info, "dsp_drop_silence", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_DROP_SILENCE);
2078         aco_option_register(&cfg_info, "dsp_silence_threshold", ACO_EXACT, user_types, __stringify(DEFAULT_SILENCE_THRESHOLD), OPT_UINT_T, 0, FLDSET(struct user_profile, silence_threshold));
2079         aco_option_register(&cfg_info, "dsp_talking_threshold", ACO_EXACT, user_types, __stringify(DEFAULT_TALKING_THRESHOLD), OPT_UINT_T, 0, FLDSET(struct user_profile, silence_threshold));
2080         aco_option_register(&cfg_info, "jitterbuffer", ACO_EXACT, user_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct user_profile, flags), USER_OPT_JITTERBUFFER);
2081         /* This option should only be used with the CONFBRIDGE dialplan function */
2082         aco_option_register_custom(&cfg_info, "template", ACO_EXACT, user_types, NULL, user_template_handler, 0);
2083
2084 /* XXX ASTERISK-21271 need a user supplied bridge merge_priority to merge ConfBridges (default = 1, range 1-INT_MAX) */
2085         /* Bridge options */
2086         aco_option_register(&cfg_info, "type", ACO_EXACT, bridge_types, NULL, OPT_NOOP_T, 0, 0);
2087         aco_option_register(&cfg_info, "jitterbuffer", ACO_EXACT, bridge_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct bridge_profile, flags), USER_OPT_JITTERBUFFER);
2088         /* "auto" will fail to parse as a uint, but we use PARSE_DEFAULT to set the value to 0 in that case, which is the value that auto resolves to */
2089         aco_option_register(&cfg_info, "internal_sample_rate", ACO_EXACT, bridge_types, "0", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct bridge_profile, internal_sample_rate), 0);
2090         aco_option_register_custom(&cfg_info, "mixing_interval", ACO_EXACT, bridge_types, "20", mix_interval_handler, 0);
2091         aco_option_register(&cfg_info, "record_conference", ACO_EXACT, bridge_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct bridge_profile, flags), BRIDGE_OPT_RECORD_CONFERENCE);
2092         aco_option_register_custom(&cfg_info, "video_mode", ACO_EXACT, bridge_types, NULL, video_mode_handler, 0);
2093         aco_option_register(&cfg_info, "record_file_append", ACO_EXACT, bridge_types, "yes", OPT_BOOLFLAG_T, 1, FLDSET(struct bridge_profile, flags), BRIDGE_OPT_RECORD_FILE_APPEND);
2094         aco_option_register(&cfg_info, "max_members", ACO_EXACT, bridge_types, "0", OPT_UINT_T, 0, FLDSET(struct bridge_profile, max_members));
2095         aco_option_register(&cfg_info, "record_file", ACO_EXACT, bridge_types, NULL, OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct bridge_profile, rec_file));
2096         aco_option_register(&cfg_info, "language", ACO_EXACT, bridge_types, "en", OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct bridge_profile, language));
2097         aco_option_register_custom(&cfg_info, "^sound_", ACO_REGEX, bridge_types, NULL, sound_option_handler, 0);
2098         /* This option should only be used with the CONFBRIDGE dialplan function */
2099         aco_option_register_custom(&cfg_info, "template", ACO_EXACT, bridge_types, NULL, bridge_template_handler, 0);
2100
2101         /* Menu options */
2102         aco_option_register(&cfg_info, "type", ACO_EXACT, menu_types, NULL, OPT_NOOP_T, 0, 0);
2103         aco_option_register_custom(&cfg_info, "template", ACO_EXACT, menu_types, NULL, menu_template_handler, 0);
2104         aco_option_register_custom(&cfg_info, "^[0-9A-D*#]+$", ACO_REGEX, menu_types, NULL, menu_option_handler, 0);
2105
2106         if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
2107                 goto error;
2108         }
2109
2110         if (ast_cli_register_multiple(cli_confbridge_parser, ARRAY_LEN(cli_confbridge_parser))) {
2111                 goto error;
2112         }
2113
2114         return 0;
2115 error:
2116         conf_destroy_config();
2117         return -1;
2118 }
2119
2120 int conf_reload_config(void)
2121 {
2122         if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
2123                 /* On a reload, just keep the config we already have in place. */
2124                 return -1;
2125         }
2126         return 0;
2127 }
2128
2129 static void conf_user_profile_copy(struct user_profile *dst, struct user_profile *src)
2130 {
2131         *dst = *src;
2132 }
2133
2134 const struct user_profile *conf_find_user_profile(struct ast_channel *chan, const char *user_profile_name, struct user_profile *result)
2135 {
2136         struct user_profile *tmp2;
2137         struct ast_datastore *datastore = NULL;
2138         struct func_confbridge_data *b_data = NULL;
2139         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
2140
2141         if (chan && ast_strlen_zero(user_profile_name)) {
2142                 ast_channel_lock(chan);
2143                 datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL);
2144                 ast_channel_unlock(chan);
2145                 if (datastore) {
2146                         b_data = datastore->data;
2147                         if (b_data->u_usable) {
2148                                 conf_user_profile_copy(result, &b_data->u_profile);
2149                                 return result;
2150                         }
2151                 }
2152         }
2153
2154         if (!cfg) {
2155                 return NULL;
2156         }
2157         if (ast_strlen_zero(user_profile_name)) {
2158                 user_profile_name = DEFAULT_USER_PROFILE;
2159         }
2160         if (!(tmp2 = ao2_find(cfg->user_profiles, user_profile_name, OBJ_KEY))) {
2161                 return NULL;
2162         }
2163         ao2_lock(tmp2);
2164         conf_user_profile_copy(result, tmp2);
2165         ao2_unlock(tmp2);
2166         ao2_ref(tmp2, -1);
2167
2168         return result;
2169 }
2170
2171 void conf_bridge_profile_copy(struct bridge_profile *dst, struct bridge_profile *src)
2172 {
2173         *dst = *src;
2174         if (src->sounds) {
2175                 ao2_ref(src->sounds, +1);
2176         }
2177 }
2178
2179 void conf_bridge_profile_destroy(struct bridge_profile *b_profile)
2180 {
2181         if (b_profile->sounds) {
2182                 ao2_ref(b_profile->sounds, -1);
2183                 b_profile->sounds = NULL;
2184         }
2185 }
2186
2187 const struct bridge_profile *conf_find_bridge_profile(struct ast_channel *chan, const char *bridge_profile_name, struct bridge_profile *result)
2188 {
2189         struct bridge_profile *tmp2;
2190         struct ast_datastore *datastore = NULL;
2191         struct func_confbridge_data *b_data = NULL;
2192         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
2193
2194         if (chan && ast_strlen_zero(bridge_profile_name)) {
2195                 ast_channel_lock(chan);
2196                 datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL);
2197                 ast_channel_unlock(chan);
2198                 if (datastore) {
2199                         b_data = datastore->data;
2200                         if (b_data->b_usable) {
2201                                 conf_bridge_profile_copy(result, &b_data->b_profile);
2202                                 return result;
2203                         }
2204                 }
2205         }
2206
2207         if (!cfg) {
2208                 return NULL;
2209         }
2210         if (ast_strlen_zero(bridge_profile_name)) {
2211                 bridge_profile_name = DEFAULT_BRIDGE_PROFILE;
2212         }
2213         if (!(tmp2 = ao2_find(cfg->bridge_profiles, bridge_profile_name, OBJ_KEY))) {
2214                 return NULL;
2215         }
2216         ao2_lock(tmp2);
2217         conf_bridge_profile_copy(result, tmp2);
2218         ao2_unlock(tmp2);
2219         ao2_ref(tmp2, -1);
2220
2221         return result;
2222 }
2223
2224 struct dtmf_menu_hook_pvt {
2225         struct confbridge_user *user;
2226         struct conf_menu_entry menu_entry;
2227         struct conf_menu *menu;
2228 };
2229
2230 static void menu_hook_destroy(void *hook_pvt)
2231 {
2232         struct dtmf_menu_hook_pvt *pvt = hook_pvt;
2233         struct conf_menu_action *action = NULL;
2234
2235         ao2_cleanup(pvt->menu);
2236
2237         while ((action = AST_LIST_REMOVE_HEAD(&pvt->menu_entry.actions, action))) {
2238                 ast_free(action);
2239         }
2240         ast_free(pvt);
2241 }
2242
2243 static int menu_hook_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2244 {
2245         struct dtmf_menu_hook_pvt *pvt = hook_pvt;
2246
2247         return conf_handle_dtmf(bridge_channel, pvt->user, &pvt->menu_entry, pvt->menu);
2248 }
2249
2250 void conf_menu_entry_destroy(struct conf_menu_entry *menu_entry)
2251 {
2252         struct conf_menu_action *menu_action = NULL;
2253         while ((menu_action = AST_LIST_REMOVE_HEAD(&menu_entry->actions, action))) {
2254                 ast_free(menu_action);
2255         }
2256 }
2257
2258 int conf_find_menu_entry_by_sequence(const char *dtmf_sequence, struct conf_menu *menu, struct conf_menu_entry *result)
2259 {
2260         struct conf_menu_entry *menu_entry = NULL;
2261
2262         ao2_lock(menu);
2263         AST_LIST_TRAVERSE(&menu->entries, menu_entry, entry) {
2264                 if (!strcasecmp(menu_entry->dtmf, dtmf_sequence)) {
2265                         copy_menu_entry(result, menu_entry);
2266                         ao2_unlock(menu);
2267                         return 1;
2268                 }
2269         }
2270         ao2_unlock(menu);
2271
2272         return 0;
2273 }
2274
2275 static int apply_menu_hooks(struct confbridge_user *user, struct conf_menu *menu)
2276 {
2277         struct conf_menu_entry *menu_entry;
2278
2279         SCOPED_AO2LOCK(menu_lock, menu);
2280         AST_LIST_TRAVERSE(&menu->entries, menu_entry, entry) {
2281                 struct dtmf_menu_hook_pvt *pvt;
2282
2283                 if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
2284                         return -1;
2285                 }
2286                 pvt->user = user;
2287                 pvt->menu = ao2_bump(menu);
2288
2289                 if (copy_menu_entry(&pvt->menu_entry, menu_entry)) {
2290                         menu_hook_destroy(pvt);
2291                         return -1;
2292                 }
2293
2294                 if (ast_bridge_dtmf_hook(&user->features, pvt->menu_entry.dtmf,
2295                         menu_hook_callback, pvt, menu_hook_destroy, 0)) {
2296                         menu_hook_destroy(pvt);
2297                 }
2298         }
2299
2300         return 0;
2301 }
2302
2303 int conf_set_menu_to_user(struct ast_channel *chan, struct confbridge_user *user, const char *menu_profile_name)
2304 {
2305         RAII_VAR(struct confbridge_cfg *, cfg, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
2306         RAII_VAR(struct conf_menu *, menu, NULL, ao2_cleanup);
2307
2308         if (chan && ast_strlen_zero(menu_profile_name)) {
2309                 struct ast_datastore *datastore;
2310                 struct func_confbridge_data *b_data;
2311
2312                 ast_channel_lock(chan);
2313                 datastore = ast_channel_datastore_find(chan, &confbridge_datastore, NULL);
2314                 ast_channel_unlock(chan);
2315                 if (datastore) {
2316                         /* If a menu exists in the CONFBRIDGE function datastore, use it. */
2317                         b_data = datastore->data;
2318                         if (b_data->m_usable) {
2319                                 menu = ao2_bump(b_data->menu);
2320                                 return apply_menu_hooks(user, menu);
2321                         }
2322                 }
2323         }
2324
2325         /* Otherwise, we need to get whatever menu profile is specified to use (or default). */
2326         if (!cfg) {
2327                 return -1;
2328         }
2329
2330         if (ast_strlen_zero(menu_profile_name)) {
2331                 menu_profile_name = DEFAULT_MENU_PROFILE;
2332         }
2333
2334         if (!(menu = ao2_find(cfg->menus, menu_profile_name, OBJ_KEY))) {
2335                 return -1;
2336         }
2337
2338         return apply_menu_hooks(user, menu);
2339 }
2340
2341 void conf_destroy_config(void)
2342 {
2343         ast_cli_unregister_multiple(cli_confbridge_parser, ARRAY_LEN(cli_confbridge_parser));
2344         aco_info_destroy(&cfg_info);
2345         ao2_global_obj_release(cfg_handle);
2346 }