025ad3561cb23f1cf65d038a19be293a6df29519
[asterisk/asterisk.git] / apps / app_meetme.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * SLA Implementation by:
9  * Russell Bryant <russell@digium.com>
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief Meet me conference bridge and Shared Line Appearances
25  *
26  * \author Mark Spencer <markster@digium.com>
27  * \author (SLA) Russell Bryant <russell@digium.com>
28  * 
29  * \ingroup applications
30  */
31
32 /*** MODULEINFO
33         <depend>dahdi</depend>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include <dahdi/user.h>
41
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/config.h"
48 #include "asterisk/app.h"
49 #include "asterisk/dsp.h"
50 #include "asterisk/musiconhold.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/cli.h"
53 #include "asterisk/say.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/translate.h"
56 #include "asterisk/ulaw.h"
57 #include "asterisk/astobj2.h"
58 #include "asterisk/devicestate.h"
59 #include "asterisk/dial.h"
60 #include "asterisk/causes.h"
61 #include "asterisk/paths.h"
62
63 #include "enter.h"
64 #include "leave.h"
65
66 /*** DOCUMENTATION
67         <application name="MeetMe" language="en_US">
68                 <synopsis>
69                         MeetMe conference bridge.
70                 </synopsis>
71                 <syntax>
72                         <parameter name="confno">
73                                 <para>The conference number</para>
74                         </parameter>
75                         <parameter name="options">
76                                 <optionlist>
77                                         <option name="a">
78                                                 <para>Set admin mode.</para>
79                                         </option>
80                                         <option name="A">
81                                                 <para>Set marked mode.</para>
82                                         </option>
83                                         <option name="b">
84                                                 <para>Run AGI script specified in <variable>MEETME_AGI_BACKGROUND</variable>
85                                                 Default: <literal>conf-background.agi</literal>.</para>
86                                                 <note><para>This does not work with non-DAHDI channels in the same
87                                                 conference).</para></note>
88                                         </option>
89                                         <option name="c">
90                                                 <para>Announce user(s) count on joining a conference.</para>
91                                         </option>
92                                         <option name="C">
93                                                 <para>Continue in dialplan when kicked out of conference.</para>
94                                         </option>
95                                         <option name="d">
96                                                 <para>Dynamically add conference.</para>
97                                         </option>
98                                         <option name="D">
99                                                 <para>Dynamically add conference, prompting for a PIN.</para>
100                                         </option>
101                                         <option name="e">
102                                                 <para>Select an empty conference.</para>
103                                         </option>
104                                         <option name="E">
105                                                 <para>Select an empty pinless conference.</para>
106                                         </option>
107                                         <option name="F">
108                                                 <para>Pass DTMF through the conference.</para>
109                                         </option>
110                                         <option name="i">
111                                                 <para>Announce user join/leave with review.</para>
112                                         </option>
113                                         <option name="I">
114                                                 <para>Announce user join/leave without review.</para>
115                                         </option>
116                                         <option name="l">
117                                                 <para>Set listen only mode (Listen only, no talking).</para>
118                                         </option>
119                                         <option name="m">
120                                                 <para>Set initially muted.</para>
121                                         </option>
122                                         <option name="M" hasparams="optional">
123                                                 <para>Enable music on hold when the conference has a single caller. Optionally,
124                                                 specify a musiconhold class to use. If one is not provided, it will use the
125                                                 channel's currently set music class, or <literal>default</literal>.</para>
126                                                 <argument name="class" required="true" />
127                                         </option>
128                                         <option name="o">
129                                                 <para>Set talker optimization - treats talkers who aren't speaking as
130                                                 being muted, meaning (a) No encode is done on transmission and (b)
131                                                 Received audio that is not registered as talking is omitted causing no
132                                                 buildup in background noise.</para>
133                                         </option>
134                                         <option name="p" hasparams="optional">
135                                                 <para>Allow user to exit the conference by pressing <literal>#</literal> (default)
136                                                 or any of the defined keys. If keys contain <literal>*</literal> this will override
137                                                 option <literal>s</literal>. The key used is set to channel variable
138                                                 <variable>MEETME_EXIT_KEY</variable>.</para>
139                                                 <argument name="keys" required="true" />
140                                         </option>
141                                         <option name="P">
142                                                 <para>Always prompt for the pin even if it is specified.</para>
143                                         </option>
144                                         <option name="q">
145                                                 <para>Quiet mode (don't play enter/leave sounds).</para>
146                                         </option>
147                                         <option name="r">
148                                                 <para>Record conference (records as <variable>MEETME_RECORDINGFILE</variable>
149                                                 using format <variable>MEETME_RECORDINGFORMAT</variable>. Default filename is
150                                                 <literal>meetme-conf-rec-${CONFNO}-${UNIQUEID}</literal> and the default format is
151                                                 wav.</para>
152                                         </option>
153                                         <option name="s">
154                                                 <para>Present menu (user or admin) when <literal>*</literal> is received
155                                                 (send to menu).</para>
156                                         </option>
157                                         <option name="t">
158                                                 <para>Set talk only mode. (Talk only, no listening).</para>
159                                         </option>
160                                         <option name="T">
161                                                 <para>Set talker detection (sent to manager interface and meetme list).</para>
162                                         </option>
163                                         <option name="W" hasparams="optional">
164                                                 <para>Wait until the marked user enters the conference.</para>
165                                                 <argument name="secs" required="true" />
166                                         </option>
167                                         <option name="x">
168                                                 <para>Close the conference when last marked user exits</para>
169                                         </option>
170                                         <option name="X">
171                                                 <para>Allow user to exit the conference by entering a valid single digit
172                                                 extension <variable>MEETME_EXIT_CONTEXT</variable> or the current context
173                                                 if that variable is not defined.</para>
174                                         </option>
175                                         <option name="1">
176                                                 <para>Do not play message when first person enters</para>
177                                         </option>
178                                         <option name="S">
179                                                 <para>Kick the user <replaceable>x</replaceable> seconds <emphasis>after</emphasis> he entered into
180                                                 the conference.</para>
181                                                 <argument name="x" required="true" />
182                                         </option>
183                                         <option name="L" argsep=":">
184                                                 <para>Limit the conference to <replaceable>x</replaceable> ms. Play a warning when
185                                                 <replaceable>y</replaceable> ms are left. Repeat the warning every <replaceable>z</replaceable> ms.
186                                                 The following special variables can be used with this option:</para>
187                                                 <variablelist>
188                                                         <variable name="CONF_LIMIT_TIMEOUT_FILE">
189                                                                 <para>File to play when time is up.</para>
190                                                         </variable>
191                                                         <variable name="CONF_LIMIT_WARNING_FILE">
192                                                                 <para>File to play as warning if <replaceable>y</replaceable> is defined. The
193                                                                 default is to say the time remaining.</para>
194                                                         </variable>
195                                                 </variablelist>
196                                                 <argument name="x" />
197                                                 <argument name="y" />
198                                                 <argument name="z" />
199                                         </option>
200                                 </optionlist>
201                         </parameter>
202                         <parameter name="pin" />
203                 </syntax>
204                 <description>
205                         <para>Enters the user into a specified MeetMe conference.  If the <replaceable>confno</replaceable>
206                         is omitted, the user will be prompted to enter one.  User can exit the conference by hangup, or
207                         if the <literal>p</literal> option is specified, by pressing <literal>#</literal>.</para>
208                         <note><para>The DAHDI kernel modules and at least one hardware driver (or dahdi_dummy)
209                         must be present for conferencing to operate properly. In addition, the chan_dahdi channel driver
210                         must be loaded for the <literal>i</literal> and <literal>r</literal> options to operate at
211                         all.</para></note>
212                 </description>
213                 <see-also>
214                         <ref type="application">MeetMeCount</ref>
215                         <ref type="application">MeetMeAdmin</ref>
216                         <ref type="application">MeetMeChannelAdmin</ref>
217                 </see-also>
218         </application>
219         <application name="MeetMeCount" language="en_US">
220                 <synopsis>
221                         MeetMe participant count.
222                 </synopsis>
223                 <syntax>
224                         <parameter name="confno" required="true">
225                                 <para>Conference number.</para>
226                         </parameter>
227                         <parameter name="var" />
228                 </syntax>
229                 <description>
230                         <para>Plays back the number of users in the specified MeetMe conference.
231                         If <replaceable>var</replaceable> is specified, playback will be skipped and the value
232                         will be returned in the variable. Upon application completion, MeetMeCount will hangup
233                         the channel, unless priority <literal>n+1</literal> exists, in which case priority progress will
234                         continue.</para>
235                 </description>
236                 <see-also>
237                         <ref type="application">MeetMe</ref>
238                 </see-also>
239         </application>
240         <application name="MeetMeAdmin" language="en_US">
241                 <synopsis>
242                         MeetMe conference administration.
243                 </synopsis>
244                 <syntax>
245                         <parameter name="confno" required="true" />
246                         <parameter name="command" required="true">
247                                 <optionlist>
248                                         <option name="e">
249                                                 <para>Eject last user that joined.</para>
250                                         </option>
251                                         <option name="E">
252                                                 <para>Extend conference end time, if scheduled.</para>
253                                         </option>
254                                         <option name="k">
255                                                 <para>Kick one user out of conference.</para>
256                                         </option>
257                                         <option name="K">
258                                                 <para>Kick all users out of conference.</para>
259                                         </option>
260                                         <option name="l">
261                                                 <para>Unlock conference.</para>
262                                         </option>
263                                         <option name="L">
264                                                 <para>Lock conference.</para>
265                                         </option>
266                                         <option name="m">
267                                                 <para>Unmute one user.</para>
268                                         </option>
269                                         <option name="M">
270                                                 <para>Mute one user.</para>
271                                         </option>
272                                         <option name="n">
273                                                 <para>Unmute all users in the conference.</para>
274                                         </option>
275                                         <option name="N">
276                                                 <para>Mute all non-admin users in the conference.</para>
277                                         </option>
278                                         <option name="r">
279                                                 <para>Reset one user's volume settings.</para>
280                                         </option>
281                                         <option name="R">
282                                                 <para>Reset all users volume settings.</para>
283                                         </option>
284                                         <option name="s">
285                                                 <para>Lower entire conference speaking volume.</para>
286                                         </option>
287                                         <option name="S">
288                                                 <para>Raise entire conference speaking volume.</para>
289                                         </option>
290                                         <option name="t">
291                                                 <para>Lower one user's talk volume.</para>
292                                         </option>
293                                         <option name="T">
294                                                 <para>Raise one user's talk volume.</para>
295                                         </option>
296                                         <option name="u">
297                                                 <para>Lower one user's listen volume.</para>
298                                         </option>
299                                         <option name="U">
300                                                 <para>Raise one user's listen volume.</para>
301                                         </option>
302                                         <option name="v">
303                                                 <para>Lower entire conference listening volume.</para>
304                                         </option>
305                                         <option name="V">
306                                                 <para>Raise entire conference listening volume.</para>
307                                         </option>
308                                 </optionlist>
309                         </parameter>
310                         <parameter name="user" />
311                 </syntax>
312                 <description>
313                         <para>Run admin <replaceable>command</replaceable> for conference <replaceable>confno</replaceable>.</para>
314                         <para>Will additionally set the variable <variable>MEETMEADMINSTATUS</variable> with one of
315                         the following values:</para>
316                         <variablelist>
317                                 <variable name="MEETMEADMINSTATUS">
318                                         <value name="NOPARSE">
319                                                 Invalid arguments.
320                                         </value>
321                                         <value name="NOTFOUND">
322                                                 User specified was not found.
323                                         </value>
324                                         <value name="FAILED">
325                                                 Another failure occurred.
326                                         </value>
327                                         <value name="OK">
328                                                 The operation was completed successfully.
329                                         </value>
330                                 </variable>
331                         </variablelist>
332                 </description>
333                 <see-also>
334                         <ref type="application">MeetMe</ref>
335                 </see-also>
336         </application>
337         <application name="MeetMeChannelAdmin" language="en_US">
338                 <synopsis>
339                         MeetMe conference Administration (channel specific).
340                 </synopsis>
341                 <syntax>
342                         <parameter name="channel" required="true" />
343                         <parameter name="command" required="true">
344                                 <optionlist>
345                                         <option name="k">
346                                                 <para>Kick the specified user out of the conference he is in.</para>
347                                         </option>
348                                         <option name="m">
349                                                 <para>Unmute the specified user.</para>
350                                         </option>
351                                         <option name="M">
352                                                 <para>Mute the specified user.</para>
353                                         </option>
354                                 </optionlist>
355                         </parameter>
356                 </syntax>
357                 <description>
358                         <para>Run admin <replaceable>command</replaceable> for a specific
359                         <replaceable>channel</replaceable> in any coference.</para>
360                 </description>
361         </application>
362         <application name="SLAStation" language="en_US">
363                 <synopsis>
364                         Shared Line Appearance Station.
365                 </synopsis>
366                 <syntax>
367                         <parameter name="station" required="true">
368                                 <para>Station name</para>
369                         </parameter>
370                 </syntax>
371                 <description>
372                         <para>This application should be executed by an SLA station. The argument depends
373                         on how the call was initiated. If the phone was just taken off hook, then the argument
374                         <replaceable>station</replaceable> should be just the station name. If the call was
375                         initiated by pressing a line key, then the station name should be preceded by an underscore
376                         and the trunk name associated with that line button.</para>
377                         <para>For example: <literal>station1_line1</literal></para>
378                         <para>On exit, this application will set the variable <variable>SLASTATION_STATUS</variable> to
379                         one of the following values:</para>
380                         <variablelist>
381                                 <variable name="SLASTATION_STATUS">
382                                         <value name="FAILURE" />
383                                         <value name="CONGESTION" />
384                                         <value name="SUCCESS" />
385                                 </variable>
386                         </variablelist>
387                 </description>
388         </application>
389         <application name="SLATrunk" language="en_US">
390                 <synopsis>
391                         Shared Line Appearance Trunk.
392                 </synopsis>
393                 <syntax>
394                         <parameter name="trunk" required="true">
395                                 <para>Trunk name</para>
396                         </parameter>
397                         <parameter name="options">
398                                 <optionlist>
399                                         <option name="M" hasparams="optional">
400                                                 <para>Play back the specified MOH <replaceable>class</replaceable>
401                                                 instead of ringing</para>
402                                                 <argument name="class" required="true" />
403                                         </option>
404                                 </optionlist>
405                         </parameter>
406                 </syntax>
407                 <description>
408                         <para>This application should be executed by an SLA trunk on an inbound call. The channel calling
409                         this application should correspond to the SLA trunk with the name <replaceable>trunk</replaceable>
410                         that is being passed as an argument.</para>
411                         <para>On exit, this application will set the variable <variable>SLATRUNK_STATUS</variable> to
412                         one of the following values:</para>
413                         <variablelist>
414                                 <variable name="SLATRUNK_STATUS">
415                                         <value name="FAILURE" />
416                                         <value name="SUCCESS" />
417                                         <value name="UNANSWERED" />
418                                         <value name="RINGTIMEOUT" />
419                                 </variable>
420                         </variablelist>
421                 </description>
422         </application>
423  ***/
424
425 #define CONFIG_FILE_NAME "meetme.conf"
426 #define SLA_CONFIG_FILE  "sla.conf"
427
428 /*! each buffer is 20ms, so this is 640ms total */
429 #define DEFAULT_AUDIO_BUFFERS  32
430
431 /*! String format for scheduled conferences */
432 #define DATE_FORMAT "%Y-%m-%d %H:%M:%S"
433
434 enum {
435         ADMINFLAG_MUTED =     (1 << 1), /*!< User is muted */
436         ADMINFLAG_SELFMUTED = (1 << 2), /*!< User muted self */
437         ADMINFLAG_KICKME =    (1 << 3),  /*!< User has been kicked */
438         /*! User has requested to speak */
439         ADMINFLAG_T_REQUEST = (1 << 4),
440 };
441
442 #define MEETME_DELAYDETECTTALK     300
443 #define MEETME_DELAYDETECTENDTALK  1000
444
445 #define AST_FRAME_BITS  32
446
447 enum volume_action {
448         VOL_UP,
449         VOL_DOWN
450 };
451
452 enum entrance_sound {
453         ENTER,
454         LEAVE
455 };
456
457 enum recording_state {
458         MEETME_RECORD_OFF,
459         MEETME_RECORD_STARTED,
460         MEETME_RECORD_ACTIVE,
461         MEETME_RECORD_TERMINATE
462 };
463
464 #define CONF_SIZE  320
465
466 enum {
467         /*! user has admin access on the conference */
468         CONFFLAG_ADMIN = (1 << 0),
469         /*! If set the user can only receive audio from the conference */
470         CONFFLAG_MONITOR = (1 << 1),
471         /*! If set asterisk will exit conference when key defined in p() option is pressed */
472         CONFFLAG_KEYEXIT = (1 << 2),
473         /*! If set asterisk will provide a menu to the user when '*' is pressed */
474         CONFFLAG_STARMENU = (1 << 3),
475         /*! If set the use can only send audio to the conference */
476         CONFFLAG_TALKER = (1 << 4),
477         /*! If set there will be no enter or leave sounds */
478         CONFFLAG_QUIET = (1 << 5),
479         /*! If set, when user joins the conference, they will be told the number 
480          *  of users that are already in */
481         CONFFLAG_ANNOUNCEUSERCOUNT = (1 << 6),
482         /*! Set to run AGI Script in Background */
483         CONFFLAG_AGI = (1 << 7),
484         /*! Set to have music on hold when user is alone in conference */
485         CONFFLAG_MOH = (1 << 8),
486         /*! If set the MeetMe will return if all marked with this flag left */
487         CONFFLAG_MARKEDEXIT = (1 << 9),
488         /*! If set, the MeetMe will wait until a marked user enters */
489         CONFFLAG_WAITMARKED = (1 << 10),
490         /*! If set, the MeetMe will exit to the specified context */
491         CONFFLAG_EXIT_CONTEXT = (1 << 11),
492         /*! If set, the user will be marked */
493         CONFFLAG_MARKEDUSER = (1 << 12),
494         /*! If set, user will be ask record name on entry of conference */
495         CONFFLAG_INTROUSER = (1 << 13),
496         /*! If set, the MeetMe will be recorded */
497         CONFFLAG_RECORDCONF = (1<< 14),
498         /*! If set, the user will be monitored if the user is talking or not */
499         CONFFLAG_MONITORTALKER = (1 << 15),
500         CONFFLAG_DYNAMIC = (1 << 16),
501         CONFFLAG_DYNAMICPIN = (1 << 17),
502         CONFFLAG_EMPTY = (1 << 18),
503         CONFFLAG_EMPTYNOPIN = (1 << 19),
504         CONFFLAG_ALWAYSPROMPT = (1 << 20),
505         /*! If set, won't speak the extra prompt when the first person 
506          *  enters the conference */
507         CONFFLAG_NOONLYPERSON = (1 << 22),
508         /*! If set, user will be asked to record name on entry of conference 
509          *  without review */
510         CONFFLAG_INTROUSERNOREVIEW = (1 << 23),
511         /*! If set, the user will be initially self-muted */
512         CONFFLAG_STARTMUTED = (1 << 24),
513         /*! Pass DTMF through the conference */
514         CONFFLAG_PASS_DTMF = (1 << 25),
515         CONFFLAG_SLA_STATION = (1 << 26),
516         CONFFLAG_SLA_TRUNK = (1 << 27),
517         /*! If set, the user should continue in the dialplan if kicked out */
518         CONFFLAG_KICK_CONTINUE = (1 << 28),
519         CONFFLAG_DURATION_STOP = (1 << 29),
520         CONFFLAG_DURATION_LIMIT = (1 << 30),
521         /*! Do not write any audio to this channel until the state is up. */
522         CONFFLAG_NO_AUDIO_UNTIL_UP = (1 << 31),
523 };
524
525 enum {
526         OPT_ARG_WAITMARKED = 0,
527         OPT_ARG_EXITKEYS   = 1,
528         OPT_ARG_DURATION_STOP = 2,
529         OPT_ARG_DURATION_LIMIT = 3,
530         OPT_ARG_MOH_CLASS = 4,
531         OPT_ARG_ARRAY_SIZE = 5,
532 };
533
534 AST_APP_OPTIONS(meetme_opts, BEGIN_OPTIONS
535         AST_APP_OPTION('A', CONFFLAG_MARKEDUSER ),
536         AST_APP_OPTION('a', CONFFLAG_ADMIN ),
537         AST_APP_OPTION('b', CONFFLAG_AGI ),
538         AST_APP_OPTION('c', CONFFLAG_ANNOUNCEUSERCOUNT ),
539         AST_APP_OPTION('C', CONFFLAG_KICK_CONTINUE),
540         AST_APP_OPTION('D', CONFFLAG_DYNAMICPIN ),
541         AST_APP_OPTION('d', CONFFLAG_DYNAMIC ),
542         AST_APP_OPTION('E', CONFFLAG_EMPTYNOPIN ),
543         AST_APP_OPTION('e', CONFFLAG_EMPTY ),
544         AST_APP_OPTION('F', CONFFLAG_PASS_DTMF ),
545         AST_APP_OPTION('i', CONFFLAG_INTROUSER ),
546         AST_APP_OPTION('I', CONFFLAG_INTROUSERNOREVIEW ),
547         AST_APP_OPTION_ARG('M', CONFFLAG_MOH, OPT_ARG_MOH_CLASS ),
548         AST_APP_OPTION('m', CONFFLAG_STARTMUTED ),
549         AST_APP_OPTION('P', CONFFLAG_ALWAYSPROMPT ),
550         AST_APP_OPTION_ARG('p', CONFFLAG_KEYEXIT, OPT_ARG_EXITKEYS ),
551         AST_APP_OPTION('q', CONFFLAG_QUIET ),
552         AST_APP_OPTION('r', CONFFLAG_RECORDCONF ),
553         AST_APP_OPTION('s', CONFFLAG_STARMENU ),
554         AST_APP_OPTION('T', CONFFLAG_MONITORTALKER ),
555         AST_APP_OPTION('l', CONFFLAG_MONITOR ),
556         AST_APP_OPTION('t', CONFFLAG_TALKER ),
557         AST_APP_OPTION_ARG('w', CONFFLAG_WAITMARKED, OPT_ARG_WAITMARKED ),
558         AST_APP_OPTION('X', CONFFLAG_EXIT_CONTEXT ),
559         AST_APP_OPTION('x', CONFFLAG_MARKEDEXIT ),
560         AST_APP_OPTION('1', CONFFLAG_NOONLYPERSON ),
561         AST_APP_OPTION_ARG('S', CONFFLAG_DURATION_STOP, OPT_ARG_DURATION_STOP),
562         AST_APP_OPTION_ARG('L', CONFFLAG_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
563 END_OPTIONS );
564
565 static const char *app = "MeetMe";
566 static const char *app2 = "MeetMeCount";
567 static const char *app3 = "MeetMeAdmin";
568 static const char *app4 = "MeetMeChannelAdmin";
569 static const char *slastation_app = "SLAStation";
570 static const char *slatrunk_app = "SLATrunk";
571
572 /* Lookup RealTime conferences based on confno and current time */
573 static int rt_schedule;
574 static int fuzzystart;
575 static int earlyalert;
576 static int endalert;
577 static int extendby;
578
579 /* Log participant count to the RealTime backend */
580 static int rt_log_members;
581
582 #define MAX_CONFNUM 80
583 #define MAX_PIN     80
584 #define OPTIONS_LEN 32
585
586 enum announcetypes {
587         CONF_HASJOIN,
588         CONF_HASLEFT
589 };
590
591 struct announce_listitem {
592         AST_LIST_ENTRY(announce_listitem) entry;
593         char namerecloc[PATH_MAX];                              /*!< Name Recorded file Location */
594         char language[MAX_LANGUAGE];
595         struct ast_channel *confchan;
596         int confusers;
597         enum announcetypes announcetype;
598 };
599
600 /*! \brief The MeetMe Conference object */
601 struct ast_conference {
602         ast_mutex_t playlock;                   /*!< Conference specific lock (players) */
603         ast_mutex_t listenlock;                 /*!< Conference specific lock (listeners) */
604         char confno[MAX_CONFNUM];               /*!< Conference */
605         struct ast_channel *chan;               /*!< Announcements channel */
606         struct ast_channel *lchan;              /*!< Listen/Record channel */
607         int fd;                                 /*!< Announcements fd */
608         int dahdiconf;                            /*!< DAHDI Conf # */
609         int users;                              /*!< Number of active users */
610         int markedusers;                        /*!< Number of marked users */
611         int maxusers;                           /*!< Participant limit if scheduled */
612         int endalert;                           /*!< When to play conf ending message */
613         time_t start;                           /*!< Start time (s) */
614         int refcount;                           /*!< reference count of usage */
615         enum recording_state recording:2;       /*!< recording status */
616         unsigned int isdynamic:1;               /*!< Created on the fly? */
617         unsigned int locked:1;                  /*!< Is the conference locked? */
618         pthread_t recordthread;                 /*!< thread for recording */
619         ast_mutex_t recordthreadlock;           /*!< control threads trying to start recordthread */
620         pthread_attr_t attr;                    /*!< thread attribute */
621         char *recordingfilename;                /*!< Filename to record the Conference into */
622         char *recordingformat;                  /*!< Format to record the Conference in */
623         char pin[MAX_PIN];                      /*!< If protected by a PIN */
624         char pinadmin[MAX_PIN];                 /*!< If protected by a admin PIN */
625         char uniqueid[32];
626         long endtime;                           /*!< When to end the conf if scheduled */
627         const char *useropts;                   /*!< RealTime user flags */
628         const char *adminopts;                  /*!< RealTime moderator flags */
629         const char *bookid;                     /*!< RealTime conference id */
630         struct ast_frame *transframe[32];
631         struct ast_frame *origframe;
632         struct ast_trans_pvt *transpath[32];
633         AST_LIST_HEAD_NOLOCK(, ast_conf_user) userlist;
634         AST_LIST_ENTRY(ast_conference) list;
635         /* announce_thread related data */
636         pthread_t announcethread;
637         ast_mutex_t announcethreadlock;
638         unsigned int announcethread_stop:1;
639         ast_cond_t announcelist_addition;
640         AST_LIST_HEAD_NOLOCK(, announce_listitem) announcelist;
641         ast_mutex_t announcelistlock;
642 };
643
644 static AST_LIST_HEAD_STATIC(confs, ast_conference);
645
646 static unsigned int conf_map[1024] = {0, };
647
648 struct volume {
649         int desired;                            /*!< Desired volume adjustment */
650         int actual;                             /*!< Actual volume adjustment (for channels that can't adjust) */
651 };
652
653 /*! \brief The MeetMe User object */
654 struct ast_conf_user {
655         int user_no;                            /*!< User Number */
656         int userflags;                          /*!< Flags as set in the conference */
657         int adminflags;                         /*!< Flags set by the Admin */
658         struct ast_channel *chan;               /*!< Connected channel */
659         int talking;                            /*!< Is user talking */
660         int dahdichannel;                         /*!< Is a DAHDI channel */
661         char usrvalue[50];                      /*!< Custom User Value */
662         char namerecloc[PATH_MAX];                              /*!< Name Recorded file Location */
663         time_t jointime;                        /*!< Time the user joined the conference */
664         time_t kicktime;                        /*!< Time the user will be kicked from the conference */
665         struct timeval start_time;              /*!< Time the user entered into the conference */
666         long timelimit;                         /*!< Time limit for the user to be in the conference L(x:y:z) */
667         long play_warning;                      /*!< Play a warning when 'y' ms are left */
668         long warning_freq;                      /*!< Repeat the warning every 'z' ms */
669         const char *warning_sound;              /*!< File to play as warning if 'y' is defined */
670         const char *end_sound;                  /*!< File to play when time is up. */
671         struct volume talk;
672         struct volume listen;
673         AST_LIST_ENTRY(ast_conf_user) list;
674 };
675
676 enum sla_which_trunk_refs {
677         ALL_TRUNK_REFS,
678         INACTIVE_TRUNK_REFS,
679 };
680
681 enum sla_trunk_state {
682         SLA_TRUNK_STATE_IDLE,
683         SLA_TRUNK_STATE_RINGING,
684         SLA_TRUNK_STATE_UP,
685         SLA_TRUNK_STATE_ONHOLD,
686         SLA_TRUNK_STATE_ONHOLD_BYME,
687 };
688
689 enum sla_hold_access {
690         /*! This means that any station can put it on hold, and any station
691          * can retrieve the call from hold. */
692         SLA_HOLD_OPEN,
693         /*! This means that only the station that put the call on hold may
694          * retrieve it from hold. */
695         SLA_HOLD_PRIVATE,
696 };
697
698 struct sla_trunk_ref;
699
700 struct sla_station {
701         AST_RWLIST_ENTRY(sla_station) entry;
702         AST_DECLARE_STRING_FIELDS(
703                 AST_STRING_FIELD(name); 
704                 AST_STRING_FIELD(device);       
705                 AST_STRING_FIELD(autocontext);  
706         );
707         AST_LIST_HEAD_NOLOCK(, sla_trunk_ref) trunks;
708         struct ast_dial *dial;
709         /*! Ring timeout for this station, for any trunk.  If a ring timeout
710          *  is set for a specific trunk on this station, that will take
711          *  priority over this value. */
712         unsigned int ring_timeout;
713         /*! Ring delay for this station, for any trunk.  If a ring delay
714          *  is set for a specific trunk on this station, that will take
715          *  priority over this value. */
716         unsigned int ring_delay;
717         /*! This option uses the values in the sla_hold_access enum and sets the
718          * access control type for hold on this station. */
719         unsigned int hold_access:1;
720         /*! Use count for inside sla_station_exec */
721         unsigned int ref_count;
722 };
723
724 struct sla_station_ref {
725         AST_LIST_ENTRY(sla_station_ref) entry;
726         struct sla_station *station;
727 };
728
729 struct sla_trunk {
730         AST_RWLIST_ENTRY(sla_trunk) entry;
731         AST_DECLARE_STRING_FIELDS(
732                 AST_STRING_FIELD(name);
733                 AST_STRING_FIELD(device);
734                 AST_STRING_FIELD(autocontext);  
735         );
736         AST_LIST_HEAD_NOLOCK(, sla_station_ref) stations;
737         /*! Number of stations that use this trunk */
738         unsigned int num_stations;
739         /*! Number of stations currently on a call with this trunk */
740         unsigned int active_stations;
741         /*! Number of stations that have this trunk on hold. */
742         unsigned int hold_stations;
743         struct ast_channel *chan;
744         unsigned int ring_timeout;
745         /*! If set to 1, no station will be able to join an active call with
746          *  this trunk. */
747         unsigned int barge_disabled:1;
748         /*! This option uses the values in the sla_hold_access enum and sets the
749          * access control type for hold on this trunk. */
750         unsigned int hold_access:1;
751         /*! Whether this trunk is currently on hold, meaning that once a station
752          *  connects to it, the trunk channel needs to have UNHOLD indicated to it. */
753         unsigned int on_hold:1;
754         /*! Use count for inside sla_trunk_exec */
755         unsigned int ref_count;
756 };
757
758 struct sla_trunk_ref {
759         AST_LIST_ENTRY(sla_trunk_ref) entry;
760         struct sla_trunk *trunk;
761         enum sla_trunk_state state;
762         struct ast_channel *chan;
763         /*! Ring timeout to use when this trunk is ringing on this specific
764          *  station.  This takes higher priority than a ring timeout set at
765          *  the station level. */
766         unsigned int ring_timeout;
767         /*! Ring delay to use when this trunk is ringing on this specific
768          *  station.  This takes higher priority than a ring delay set at
769          *  the station level. */
770         unsigned int ring_delay;
771 };
772
773 static AST_RWLIST_HEAD_STATIC(sla_stations, sla_station);
774 static AST_RWLIST_HEAD_STATIC(sla_trunks, sla_trunk);
775
776 static const char sla_registrar[] = "SLA";
777
778 /*! \brief Event types that can be queued up for the SLA thread */
779 enum sla_event_type {
780         /*! A station has put the call on hold */
781         SLA_EVENT_HOLD,
782         /*! The state of a dial has changed */
783         SLA_EVENT_DIAL_STATE,
784         /*! The state of a ringing trunk has changed */
785         SLA_EVENT_RINGING_TRUNK,
786         /*! A reload of configuration has been requested */
787         SLA_EVENT_RELOAD,
788         /*! Poke the SLA thread so it can check if it can perform a reload */
789         SLA_EVENT_CHECK_RELOAD,
790 };
791
792 struct sla_event {
793         enum sla_event_type type;
794         struct sla_station *station;
795         struct sla_trunk_ref *trunk_ref;
796         AST_LIST_ENTRY(sla_event) entry;
797 };
798
799 /*! \brief A station that failed to be dialed 
800  * \note Only used by the SLA thread. */
801 struct sla_failed_station {
802         struct sla_station *station;
803         struct timeval last_try;
804         AST_LIST_ENTRY(sla_failed_station) entry;
805 };
806
807 /*! \brief A trunk that is ringing */
808 struct sla_ringing_trunk {
809         struct sla_trunk *trunk;
810         /*! The time that this trunk started ringing */
811         struct timeval ring_begin;
812         AST_LIST_HEAD_NOLOCK(, sla_station_ref) timed_out_stations;
813         AST_LIST_ENTRY(sla_ringing_trunk) entry;
814 };
815
816 enum sla_station_hangup {
817         SLA_STATION_HANGUP_NORMAL,
818         SLA_STATION_HANGUP_TIMEOUT,
819 };
820
821 /*! \brief A station that is ringing */
822 struct sla_ringing_station {
823         struct sla_station *station;
824         /*! The time that this station started ringing */
825         struct timeval ring_begin;
826         AST_LIST_ENTRY(sla_ringing_station) entry;
827 };
828
829 /*!
830  * \brief A structure for data used by the sla thread
831  */
832 static struct {
833         /*! The SLA thread ID */
834         pthread_t thread;
835         ast_cond_t cond;
836         ast_mutex_t lock;
837         AST_LIST_HEAD_NOLOCK(, sla_ringing_trunk) ringing_trunks;
838         AST_LIST_HEAD_NOLOCK(, sla_ringing_station) ringing_stations;
839         AST_LIST_HEAD_NOLOCK(, sla_failed_station) failed_stations;
840         AST_LIST_HEAD_NOLOCK(, sla_event) event_q;
841         unsigned int stop:1;
842         /*! Attempt to handle CallerID, even though it is known not to work
843          *  properly in some situations. */
844         unsigned int attempt_callerid:1;
845         /*! A reload has been requested */
846         unsigned int reload:1;
847 } sla = {
848         .thread = AST_PTHREADT_NULL,
849 };
850
851 /*! The number of audio buffers to be allocated on pseudo channels
852  *  when in a conference */
853 static int audio_buffers;
854
855 /*! Map 'volume' levels from -5 through +5 into
856  *  decibel (dB) settings for channel drivers
857  *  Note: these are not a straight linear-to-dB
858  *  conversion... the numbers have been modified
859  *  to give the user a better level of adjustability
860  */
861 static char const gain_map[] = {
862         -15,
863         -13,
864         -10,
865         -6,
866         0,
867         0,
868         0,
869         6,
870         10,
871         13,
872         15,
873 };
874
875
876 static int admin_exec(struct ast_channel *chan, void *data);
877 static void *recordthread(void *args);
878
879 static char *istalking(int x)
880 {
881         if (x > 0)
882                 return "(talking)";
883         else if (x < 0)
884                 return "(unmonitored)";
885         else 
886                 return "(not talking)";
887 }
888
889 static int careful_write(int fd, unsigned char *data, int len, int block)
890 {
891         int res;
892         int x;
893
894         while (len) {
895                 if (block) {
896                         x = DAHDI_IOMUX_WRITE | DAHDI_IOMUX_SIGEVENT;
897                         res = ioctl(fd, DAHDI_IOMUX, &x);
898                 } else
899                         res = 0;
900                 if (res >= 0)
901                         res = write(fd, data, len);
902                 if (res < 1) {
903                         if (errno != EAGAIN) {
904                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
905                                 return -1;
906                         } else
907                                 return 0;
908                 }
909                 len -= res;
910                 data += res;
911         }
912
913         return 0;
914 }
915
916 static int set_talk_volume(struct ast_conf_user *user, int volume)
917 {
918         char gain_adjust;
919
920         /* attempt to make the adjustment in the channel driver;
921            if successful, don't adjust in the frame reading routine
922         */
923         gain_adjust = gain_map[volume + 5];
924
925         return ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
926 }
927
928 static int set_listen_volume(struct ast_conf_user *user, int volume)
929 {
930         char gain_adjust;
931
932         /* attempt to make the adjustment in the channel driver;
933            if successful, don't adjust in the frame reading routine
934         */
935         gain_adjust = gain_map[volume + 5];
936
937         return ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
938 }
939
940 static void tweak_volume(struct volume *vol, enum volume_action action)
941 {
942         switch (action) {
943         case VOL_UP:
944                 switch (vol->desired) { 
945                 case 5:
946                         break;
947                 case 0:
948                         vol->desired = 2;
949                         break;
950                 case -2:
951                         vol->desired = 0;
952                         break;
953                 default:
954                         vol->desired++;
955                         break;
956                 }
957                 break;
958         case VOL_DOWN:
959                 switch (vol->desired) {
960                 case -5:
961                         break;
962                 case 2:
963                         vol->desired = 0;
964                         break;
965                 case 0:
966                         vol->desired = -2;
967                         break;
968                 default:
969                         vol->desired--;
970                         break;
971                 }
972         }
973 }
974
975 static void tweak_talk_volume(struct ast_conf_user *user, enum volume_action action)
976 {
977         tweak_volume(&user->talk, action);
978         /* attempt to make the adjustment in the channel driver;
979            if successful, don't adjust in the frame reading routine
980         */
981         if (!set_talk_volume(user, user->talk.desired))
982                 user->talk.actual = 0;
983         else
984                 user->talk.actual = user->talk.desired;
985 }
986
987 static void tweak_listen_volume(struct ast_conf_user *user, enum volume_action action)
988 {
989         tweak_volume(&user->listen, action);
990         /* attempt to make the adjustment in the channel driver;
991            if successful, don't adjust in the frame reading routine
992         */
993         if (!set_listen_volume(user, user->listen.desired))
994                 user->listen.actual = 0;
995         else
996                 user->listen.actual = user->listen.desired;
997 }
998
999 static void reset_volumes(struct ast_conf_user *user)
1000 {
1001         signed char zero_volume = 0;
1002
1003         ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
1004         ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &zero_volume, sizeof(zero_volume), 0);
1005 }
1006
1007 static void conf_play(struct ast_channel *chan, struct ast_conference *conf, enum entrance_sound sound)
1008 {
1009         unsigned char *data;
1010         int len;
1011         int res = -1;
1012
1013         if (!ast_check_hangup(chan))
1014                 res = ast_autoservice_start(chan);
1015
1016         AST_LIST_LOCK(&confs);
1017
1018         switch(sound) {
1019         case ENTER:
1020                 data = enter;
1021                 len = sizeof(enter);
1022                 break;
1023         case LEAVE:
1024                 data = leave;
1025                 len = sizeof(leave);
1026                 break;
1027         default:
1028                 data = NULL;
1029                 len = 0;
1030         }
1031         if (data) {
1032                 careful_write(conf->fd, data, len, 1);
1033         }
1034
1035         AST_LIST_UNLOCK(&confs);
1036
1037         if (!res) 
1038                 ast_autoservice_stop(chan);
1039 }
1040
1041 /*!
1042  * \brief Find or create a conference
1043  *
1044  * \param confno The conference name/number
1045  * \param pin The regular user pin
1046  * \param pinadmin The admin pin
1047  * \param make Make the conf if it doesn't exist
1048  * \param dynamic Mark the newly created conference as dynamic
1049  * \param refcount How many references to mark on the conference
1050  * \param chan The asterisk channel
1051  *
1052  * \return A pointer to the conference struct, or NULL if it wasn't found and
1053  *         make or dynamic were not set.
1054  */
1055 static struct ast_conference *build_conf(char *confno, char *pin, char *pinadmin, int make, int dynamic, int refcount, const struct ast_channel *chan)
1056 {
1057         struct ast_conference *cnf;
1058         struct dahdi_confinfo dahdic = { 0, };
1059         int confno_int = 0;
1060
1061         AST_LIST_LOCK(&confs);
1062
1063         AST_LIST_TRAVERSE(&confs, cnf, list) {
1064                 if (!strcmp(confno, cnf->confno)) 
1065                         break;
1066         }
1067
1068         if (cnf || (!make && !dynamic))
1069                 goto cnfout;
1070
1071         /* Make a new one */
1072         if (!(cnf = ast_calloc(1, sizeof(*cnf))))
1073                 goto cnfout;
1074
1075         ast_mutex_init(&cnf->playlock);
1076         ast_mutex_init(&cnf->listenlock);
1077         cnf->recordthread = AST_PTHREADT_NULL;
1078         ast_mutex_init(&cnf->recordthreadlock);
1079         cnf->announcethread = AST_PTHREADT_NULL;
1080         ast_mutex_init(&cnf->announcethreadlock);
1081         ast_copy_string(cnf->confno, confno, sizeof(cnf->confno));
1082         ast_copy_string(cnf->pin, pin, sizeof(cnf->pin));
1083         ast_copy_string(cnf->pinadmin, pinadmin, sizeof(cnf->pinadmin));
1084         ast_copy_string(cnf->uniqueid, chan->uniqueid, sizeof(cnf->uniqueid));
1085
1086         /* Setup a new dahdi conference */
1087         dahdic.confno = -1;
1088         dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
1089         cnf->fd = open("/dev/dahdi/pseudo", O_RDWR);
1090         if (cnf->fd < 0 || ioctl(cnf->fd, DAHDI_SETCONF, &dahdic)) {
1091                 ast_log(LOG_WARNING, "Unable to open pseudo device\n");
1092                 if (cnf->fd >= 0)
1093                         close(cnf->fd);
1094                 ast_free(cnf);
1095                 cnf = NULL;
1096                 goto cnfout;
1097         }
1098
1099         cnf->dahdiconf = dahdic.confno;
1100
1101         /* Setup a new channel for playback of audio files */
1102         cnf->chan = ast_request("DAHDI", AST_FORMAT_SLINEAR, "pseudo", NULL);
1103         if (cnf->chan) {
1104                 ast_set_read_format(cnf->chan, AST_FORMAT_SLINEAR);
1105                 ast_set_write_format(cnf->chan, AST_FORMAT_SLINEAR);
1106                 dahdic.chan = 0;
1107                 dahdic.confno = cnf->dahdiconf;
1108                 dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
1109                 if (ioctl(cnf->chan->fds[0], DAHDI_SETCONF, &dahdic)) {
1110                         ast_log(LOG_WARNING, "Error setting conference\n");
1111                         if (cnf->chan)
1112                                 ast_hangup(cnf->chan);
1113                         else
1114                                 close(cnf->fd);
1115
1116                         ast_free(cnf);
1117                         cnf = NULL;
1118                         goto cnfout;
1119                 }
1120         }
1121
1122         /* Fill the conference struct */
1123         cnf->start = time(NULL);
1124         cnf->maxusers = 0x7fffffff;
1125         cnf->isdynamic = dynamic ? 1 : 0;
1126         ast_verb(3, "Created MeetMe conference %d for conference '%s'\n", cnf->dahdiconf, cnf->confno);
1127         AST_LIST_INSERT_HEAD(&confs, cnf, list);
1128
1129         /* Reserve conference number in map */
1130         if ((sscanf(cnf->confno, "%d", &confno_int) == 1) && (confno_int >= 0 && confno_int < 1024))
1131                 conf_map[confno_int] = 1;
1132         
1133 cnfout:
1134         if (cnf)
1135                 ast_atomic_fetchadd_int(&cnf->refcount, refcount);
1136
1137         AST_LIST_UNLOCK(&confs);
1138
1139         return cnf;
1140 }
1141
1142 static char *complete_meetmecmd(const char *line, const char *word, int pos, int state)
1143 {
1144         static char *cmds[] = {"concise", "lock", "unlock", "mute", "unmute", "kick", "list", NULL};
1145
1146         int len = strlen(word);
1147         int which = 0;
1148         struct ast_conference *cnf = NULL;
1149         struct ast_conf_user *usr = NULL;
1150         char *confno = NULL;
1151         char usrno[50] = "";
1152         char *myline, *ret = NULL;
1153         
1154         if (pos == 1) {         /* Command */
1155                 return ast_cli_complete(word, cmds, state);
1156         } else if (pos == 2) {  /* Conference Number */
1157                 AST_LIST_LOCK(&confs);
1158                 AST_LIST_TRAVERSE(&confs, cnf, list) {
1159                         if (!strncasecmp(word, cnf->confno, len) && ++which > state) {
1160                                 ret = cnf->confno;
1161                                 break;
1162                         }
1163                 }
1164                 ret = ast_strdup(ret); /* dup before releasing the lock */
1165                 AST_LIST_UNLOCK(&confs);
1166                 return ret;
1167         } else if (pos == 3) {
1168                 /* User Number || Conf Command option*/
1169                 if (strstr(line, "mute") || strstr(line, "kick")) {
1170                         if (state == 0 && (strstr(line, "kick") || strstr(line, "mute")) && !strncasecmp(word, "all", len))
1171                                 return ast_strdup("all");
1172                         which++;
1173                         AST_LIST_LOCK(&confs);
1174
1175                         /* TODO: Find the conf number from the cmdline (ignore spaces) <- test this and make it fail-safe! */
1176                         myline = ast_strdupa(line);
1177                         if (strsep(&myline, " ") && strsep(&myline, " ") && !confno) {
1178                                 while((confno = strsep(&myline, " ")) && (strcmp(confno, " ") == 0))
1179                                         ;
1180                         }
1181                         
1182                         AST_LIST_TRAVERSE(&confs, cnf, list) {
1183                                 if (!strcmp(confno, cnf->confno))
1184                                     break;
1185                         }
1186
1187                         if (cnf) {
1188                                 /* Search for the user */
1189                                 AST_LIST_TRAVERSE(&cnf->userlist, usr, list) {
1190                                         snprintf(usrno, sizeof(usrno), "%d", usr->user_no);
1191                                         if (!strncasecmp(word, usrno, len) && ++which > state)
1192                                                 break;
1193                                 }
1194                         }
1195                         AST_LIST_UNLOCK(&confs);
1196                         return usr ? ast_strdup(usrno) : NULL;
1197                 }
1198         }
1199
1200         return NULL;
1201 }
1202
1203 static char *meetme_show_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1204 {
1205         /* Process the command */
1206         struct ast_conf_user *user;
1207         struct ast_conference *cnf;
1208         int hr, min, sec;
1209         int i = 0, total = 0;
1210         time_t now;
1211         struct ast_str *cmdline = NULL;
1212 #define MC_HEADER_FORMAT "%-14s %-14s %-10s %-8s  %-8s  %-6s\n"
1213 #define MC_DATA_FORMAT "%-12.12s   %4.4d              %4.4s       %02d:%02d:%02d  %-8s  %-6s\n"
1214
1215         switch (cmd) {
1216         case CLI_INIT:
1217                 e->command = "meetme list [concise]";
1218                 e->usage =
1219                         "Usage: meetme list [concise] <confno> \n"
1220                         "       List all or a specific conference.\n";
1221                 return NULL;
1222         case CLI_GENERATE:
1223                 return complete_meetmecmd(a->line, a->word, a->pos, a->n);
1224         }
1225
1226         /* Check for length so no buffer will overflow... */
1227         for (i = 0; i < a->argc; i++) {
1228                 if (strlen(a->argv[i]) > 100)
1229                         ast_cli(a->fd, "Invalid Arguments.\n");
1230         }
1231
1232         /* Max confno length */
1233         if (!(cmdline = ast_str_create(MAX_CONFNUM))) {
1234                 return CLI_FAILURE;
1235         }
1236
1237         if (a->argc == 2 || (a->argc == 3 && !strcasecmp(a->argv[2], "concise"))) {
1238                 /* List all the conferences */  
1239                 int concise = (a->argc == 3 && !strcasecmp(a->argv[2], "concise"));
1240                 now = time(NULL);
1241                 AST_LIST_LOCK(&confs);
1242                 if (AST_LIST_EMPTY(&confs)) {
1243                         if (!concise) {
1244                                 ast_cli(a->fd, "No active MeetMe conferences.\n");
1245                         }
1246                         AST_LIST_UNLOCK(&confs);
1247                         ast_free(cmdline);
1248                         return CLI_SUCCESS;
1249                 }
1250                 if (!concise) {
1251                         ast_cli(a->fd, MC_HEADER_FORMAT, "Conf Num", "Parties", "Marked", "Activity", "Creation", "Locked");
1252                 }
1253                 AST_LIST_TRAVERSE(&confs, cnf, list) {
1254                         if (cnf->markedusers == 0) {
1255                                 ast_str_set(&cmdline, 0, "N/A ");
1256                         } else {
1257                                 ast_str_set(&cmdline, 0, "%4.4d", cnf->markedusers);
1258                         }
1259                         hr = (now - cnf->start) / 3600;
1260                         min = ((now - cnf->start) % 3600) / 60;
1261                         sec = (now - cnf->start) % 60;
1262                         if (!concise) {
1263                                 ast_cli(a->fd, MC_DATA_FORMAT, cnf->confno, cnf->users, ast_str_buffer(cmdline), hr, min, sec, cnf->isdynamic ? "Dynamic" : "Static", cnf->locked ? "Yes" : "No");
1264                         } else {
1265                                 ast_cli(a->fd, "%s!%d!%d!%02d:%02d:%02d!%d!%d\n",
1266                                         cnf->confno,
1267                                         cnf->users,
1268                                         cnf->markedusers,
1269                                         hr, min, sec,
1270                                         cnf->isdynamic,
1271                                         cnf->locked);
1272                         }
1273
1274                         total += cnf->users;
1275                 }
1276                 AST_LIST_UNLOCK(&confs);
1277                 if (!concise) {
1278                         ast_cli(a->fd, "* Total number of MeetMe users: %d\n", total);
1279                 }
1280                 ast_free(cmdline);
1281                 return CLI_SUCCESS;
1282         } else if (strcmp(a->argv[1], "list") == 0) {
1283                 int concise = (a->argc == 4 && (!strcasecmp(a->argv[3], "concise")));
1284                 /* List all the users in a conference */
1285                 if (AST_LIST_EMPTY(&confs)) {
1286                         if (!concise) {
1287                                 ast_cli(a->fd, "No active MeetMe conferences.\n");
1288                         }
1289                         ast_free(cmdline);
1290                         return CLI_SUCCESS;     
1291                 }
1292                 /* Find the right conference */
1293                 AST_LIST_LOCK(&confs);
1294                 AST_LIST_TRAVERSE(&confs, cnf, list) {
1295                         if (strcmp(cnf->confno, a->argv[2]) == 0) {
1296                                 break;
1297                         }
1298                 }
1299                 if (!cnf) {
1300                         if (!concise)
1301                                 ast_cli(a->fd, "No such conference: %s.\n", a->argv[2]);
1302                         AST_LIST_UNLOCK(&confs);
1303                         ast_free(cmdline);
1304                         return CLI_SUCCESS;
1305                 }
1306                 /* Show all the users */
1307                 time(&now);
1308                 AST_LIST_TRAVERSE(&cnf->userlist, user, list) {
1309                         hr = (now - user->jointime) / 3600;
1310                         min = ((now - user->jointime) % 3600) / 60;
1311                         sec = (now - user->jointime) % 60;
1312                         if (!concise) {
1313                                 ast_cli(a->fd, "User #: %-2.2d %12.12s %-20.20s Channel: %s %s %s %s %s %s %02d:%02d:%02d\n",
1314                                         user->user_no,
1315                                         S_OR(user->chan->cid.cid_num, "<unknown>"),
1316                                         S_OR(user->chan->cid.cid_name, "<no name>"),
1317                                         user->chan->name,
1318                                         user->userflags & CONFFLAG_ADMIN ? "(Admin)" : "",
1319                                         user->userflags & CONFFLAG_MONITOR ? "(Listen only)" : "",
1320                                         user->adminflags & ADMINFLAG_MUTED ? "(Admin Muted)" : user->adminflags & ADMINFLAG_SELFMUTED ? "(Muted)" : "",
1321                                         user->adminflags & ADMINFLAG_T_REQUEST ? "(Request to Talk)" : "",
1322                                         istalking(user->talking), hr, min, sec); 
1323                         } else {
1324                                 ast_cli(a->fd, "%d!%s!%s!%s!%s!%s!%s!%s!%d!%02d:%02d:%02d\n",
1325                                         user->user_no,
1326                                         S_OR(user->chan->cid.cid_num, ""),
1327                                         S_OR(user->chan->cid.cid_name, ""),
1328                                         user->chan->name,
1329                                         user->userflags  & CONFFLAG_ADMIN   ? "1" : "",
1330                                         user->userflags  & CONFFLAG_MONITOR ? "1" : "",
1331                                         user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED) ? "1" : "",
1332                                         user->adminflags & ADMINFLAG_T_REQUEST ? "1" : "",
1333                                         user->talking, hr, min, sec);
1334                         }
1335                 }
1336                 if (!concise) {
1337                         ast_cli(a->fd, "%d users in that conference.\n", cnf->users);
1338                 }
1339                 AST_LIST_UNLOCK(&confs);
1340                 ast_free(cmdline);
1341                 return CLI_SUCCESS;
1342         }
1343         if (a->argc < 2) {
1344                 ast_free(cmdline);
1345                 return CLI_SHOWUSAGE;
1346         }
1347
1348         ast_debug(1, "Cmdline: %s\n", ast_str_buffer(cmdline));
1349
1350         admin_exec(NULL, ast_str_buffer(cmdline));
1351         ast_free(cmdline);
1352
1353         return CLI_SUCCESS;
1354 }
1355
1356
1357 static char *meetme_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1358 {
1359         /* Process the command */
1360         struct ast_str *cmdline = NULL;
1361         int i = 0;
1362
1363         switch (cmd) {
1364         case CLI_INIT:
1365                 e->command = "meetme {lock|unlock|mute|unmute|kick}";
1366                 e->usage =
1367                         "Usage: meetme (un)lock|(un)mute|kick <confno> <usernumber>\n"
1368                         "       Executes a command for the conference or on a conferee\n";
1369                 return NULL;
1370         case CLI_GENERATE:
1371                 return complete_meetmecmd(a->line, a->word, a->pos, a->n);
1372         }
1373
1374         if (a->argc > 8)
1375                 ast_cli(a->fd, "Invalid Arguments.\n");
1376         /* Check for length so no buffer will overflow... */
1377         for (i = 0; i < a->argc; i++) {
1378                 if (strlen(a->argv[i]) > 100)
1379                         ast_cli(a->fd, "Invalid Arguments.\n");
1380         }
1381
1382         /* Max confno length */
1383         if (!(cmdline = ast_str_create(MAX_CONFNUM))) {
1384                 return CLI_FAILURE;
1385         }
1386
1387         if (a->argc < 1) {
1388                 ast_free(cmdline);
1389                 return CLI_SHOWUSAGE;
1390         }
1391
1392         ast_str_set(&cmdline, 0, "%s", a->argv[2]);     /* Argv 2: conference number */
1393         if (strstr(a->argv[1], "lock")) {
1394                 if (strcmp(a->argv[1], "lock") == 0) {
1395                         /* Lock */
1396                         ast_str_append(&cmdline, 0, ",L");
1397                 } else {
1398                         /* Unlock */
1399                         ast_str_append(&cmdline, 0, ",l");
1400                 }
1401         } else if (strstr(a->argv[1], "mute")) { 
1402                 if (a->argc < 4) {
1403                         ast_free(cmdline);
1404                         return CLI_SHOWUSAGE;
1405                 }
1406                 if (strcmp(a->argv[1], "mute") == 0) {
1407                         /* Mute */
1408                         if (strcmp(a->argv[3], "all") == 0) {
1409                                 ast_str_append(&cmdline, 0, ",N");
1410                         } else {
1411                                 ast_str_append(&cmdline, 0, ",M,%s", a->argv[3]);       
1412                         }
1413                 } else {
1414                         /* Unmute */
1415                         if (strcmp(a->argv[3], "all") == 0) {
1416                                 ast_str_append(&cmdline, 0, ",n");
1417                         } else {
1418                                 ast_str_append(&cmdline, 0, ",m,%s", a->argv[3]);
1419                         }
1420                 }
1421         } else if (strcmp(a->argv[1], "kick") == 0) {
1422                 if (a->argc < 4) {
1423                         ast_free(cmdline);
1424                         return CLI_SHOWUSAGE;
1425                 }
1426                 if (strcmp(a->argv[3], "all") == 0) {
1427                         /* Kick all */
1428                         ast_str_append(&cmdline, 0, ",K");
1429                 } else {
1430                         /* Kick a single user */
1431                         ast_str_append(&cmdline, 0, ",k,%s", a->argv[3]);
1432                 }
1433         } else {
1434                 ast_free(cmdline);
1435                 return CLI_SHOWUSAGE;
1436         }
1437
1438         ast_debug(1, "Cmdline: %s\n", ast_str_buffer(cmdline));
1439
1440         admin_exec(NULL, ast_str_buffer(cmdline));
1441         ast_free(cmdline);
1442
1443         return CLI_SUCCESS;
1444 }
1445
1446 static const char *sla_hold_str(unsigned int hold_access)
1447 {
1448         const char *hold = "Unknown";
1449
1450         switch (hold_access) {
1451         case SLA_HOLD_OPEN:
1452                 hold = "Open";
1453                 break;
1454         case SLA_HOLD_PRIVATE:
1455                 hold = "Private";
1456         default:
1457                 break;
1458         }
1459
1460         return hold;
1461 }
1462
1463 static char *sla_show_trunks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1464 {
1465         const struct sla_trunk *trunk;
1466
1467         switch (cmd) {
1468         case CLI_INIT:
1469                 e->command = "sla show trunks";
1470                 e->usage =
1471                         "Usage: sla show trunks\n"
1472                         "       This will list all trunks defined in sla.conf\n";
1473                 return NULL;
1474         case CLI_GENERATE:
1475                 return NULL;
1476         }
1477
1478         ast_cli(a->fd, "\n"
1479                     "=============================================================\n"
1480                     "=== Configured SLA Trunks ===================================\n"
1481                     "=============================================================\n"
1482                     "===\n");
1483         AST_RWLIST_RDLOCK(&sla_trunks);
1484         AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
1485                 struct sla_station_ref *station_ref;
1486                 char ring_timeout[16] = "(none)";
1487                 if (trunk->ring_timeout)
1488                         snprintf(ring_timeout, sizeof(ring_timeout), "%u Seconds", trunk->ring_timeout);
1489                 ast_cli(a->fd, "=== ---------------------------------------------------------\n"
1490                             "=== Trunk Name:       %s\n"
1491                             "=== ==> Device:       %s\n"
1492                             "=== ==> AutoContext:  %s\n"
1493                             "=== ==> RingTimeout:  %s\n"
1494                             "=== ==> BargeAllowed: %s\n"
1495                             "=== ==> HoldAccess:   %s\n"
1496                             "=== ==> Stations ...\n",
1497                             trunk->name, trunk->device, 
1498                             S_OR(trunk->autocontext, "(none)"), 
1499                             ring_timeout,
1500                             trunk->barge_disabled ? "No" : "Yes",
1501                             sla_hold_str(trunk->hold_access));
1502                 AST_RWLIST_RDLOCK(&sla_stations);
1503                 AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry)
1504                         ast_cli(a->fd, "===    ==> Station name: %s\n", station_ref->station->name);
1505                 AST_RWLIST_UNLOCK(&sla_stations);
1506                 ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");
1507         }
1508         AST_RWLIST_UNLOCK(&sla_trunks);
1509         ast_cli(a->fd, "=============================================================\n\n");
1510
1511         return CLI_SUCCESS;
1512 }
1513
1514 static const char *trunkstate2str(enum sla_trunk_state state)
1515 {
1516 #define S(e) case e: return # e;
1517         switch (state) {
1518         S(SLA_TRUNK_STATE_IDLE)
1519         S(SLA_TRUNK_STATE_RINGING)
1520         S(SLA_TRUNK_STATE_UP)
1521         S(SLA_TRUNK_STATE_ONHOLD)
1522         S(SLA_TRUNK_STATE_ONHOLD_BYME)
1523         }
1524         return "Uknown State";
1525 #undef S
1526 }
1527
1528 static char *sla_show_stations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1529 {
1530         const struct sla_station *station;
1531
1532         switch (cmd) {
1533         case CLI_INIT:
1534                 e->command = "sla show stations";
1535                 e->usage =
1536                         "Usage: sla show stations\n"
1537                         "       This will list all stations defined in sla.conf\n";
1538                 return NULL;
1539         case CLI_GENERATE:
1540                 return NULL;
1541         }
1542
1543         ast_cli(a->fd, "\n" 
1544                     "=============================================================\n"
1545                     "=== Configured SLA Stations =================================\n"
1546                     "=============================================================\n"
1547                     "===\n");
1548         AST_RWLIST_RDLOCK(&sla_stations);
1549         AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
1550                 struct sla_trunk_ref *trunk_ref;
1551                 char ring_timeout[16] = "(none)";
1552                 char ring_delay[16] = "(none)";
1553                 if (station->ring_timeout) {
1554                         snprintf(ring_timeout, sizeof(ring_timeout), 
1555                                 "%u", station->ring_timeout);
1556                 }
1557                 if (station->ring_delay) {
1558                         snprintf(ring_delay, sizeof(ring_delay), 
1559                                 "%u", station->ring_delay);
1560                 }
1561                 ast_cli(a->fd, "=== ---------------------------------------------------------\n"
1562                             "=== Station Name:    %s\n"
1563                             "=== ==> Device:      %s\n"
1564                             "=== ==> AutoContext: %s\n"
1565                             "=== ==> RingTimeout: %s\n"
1566                             "=== ==> RingDelay:   %s\n"
1567                             "=== ==> HoldAccess:  %s\n"
1568                             "=== ==> Trunks ...\n",
1569                             station->name, station->device,
1570                             S_OR(station->autocontext, "(none)"), 
1571                             ring_timeout, ring_delay,
1572                             sla_hold_str(station->hold_access));
1573                 AST_RWLIST_RDLOCK(&sla_trunks);
1574                 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
1575                         if (trunk_ref->ring_timeout) {
1576                                 snprintf(ring_timeout, sizeof(ring_timeout),
1577                                         "%u", trunk_ref->ring_timeout);
1578                         } else
1579                                 strcpy(ring_timeout, "(none)");
1580                         if (trunk_ref->ring_delay) {
1581                                 snprintf(ring_delay, sizeof(ring_delay),
1582                                         "%u", trunk_ref->ring_delay);
1583                         } else
1584                                 strcpy(ring_delay, "(none)");
1585                                 ast_cli(a->fd, "===    ==> Trunk Name: %s\n"
1586                                     "===       ==> State:       %s\n"
1587                                     "===       ==> RingTimeout: %s\n"
1588                                     "===       ==> RingDelay:   %s\n",
1589                                     trunk_ref->trunk->name,
1590                                     trunkstate2str(trunk_ref->state),
1591                                     ring_timeout, ring_delay);
1592                 }
1593                 AST_RWLIST_UNLOCK(&sla_trunks);
1594                 ast_cli(a->fd, "=== ---------------------------------------------------------\n"
1595                             "===\n");
1596         }
1597         AST_RWLIST_UNLOCK(&sla_stations);
1598         ast_cli(a->fd, "============================================================\n"
1599                     "\n");
1600
1601         return CLI_SUCCESS;
1602 }
1603
1604 static struct ast_cli_entry cli_meetme[] = {
1605         AST_CLI_DEFINE(meetme_cmd, "Execute a command on a conference or conferee"),
1606         AST_CLI_DEFINE(meetme_show_cmd, "List all or one conference"),
1607         AST_CLI_DEFINE(sla_show_trunks, "Show SLA Trunks"),
1608         AST_CLI_DEFINE(sla_show_stations, "Show SLA Stations"),
1609 };
1610
1611 static void conf_flush(int fd, struct ast_channel *chan)
1612 {
1613         int x;
1614
1615         /* read any frames that may be waiting on the channel
1616            and throw them away
1617         */
1618         if (chan) {
1619                 struct ast_frame *f;
1620
1621                 /* when no frames are available, this will wait
1622                    for 1 millisecond maximum
1623                 */
1624                 while (ast_waitfor(chan, 1)) {
1625                         f = ast_read(chan);
1626                         if (f)
1627                                 ast_frfree(f);
1628                         else /* channel was hung up or something else happened */
1629                                 break;
1630                 }
1631         }
1632
1633         /* flush any data sitting in the pseudo channel */
1634         x = DAHDI_FLUSH_ALL;
1635         if (ioctl(fd, DAHDI_FLUSH, &x))
1636                 ast_log(LOG_WARNING, "Error flushing channel\n");
1637
1638 }
1639
1640 /* Remove the conference from the list and free it.
1641    We assume that this was called while holding conflock. */
1642 static int conf_free(struct ast_conference *conf)
1643 {
1644         int x;
1645         struct announce_listitem *item;
1646         
1647         AST_LIST_REMOVE(&confs, conf, list);
1648         manager_event(EVENT_FLAG_CALL, "MeetmeEnd", "Meetme: %s\r\n", conf->confno);
1649
1650         if (conf->recording == MEETME_RECORD_ACTIVE) {
1651                 conf->recording = MEETME_RECORD_TERMINATE;
1652                 AST_LIST_UNLOCK(&confs);
1653                 while (1) {
1654                         usleep(1);
1655                         AST_LIST_LOCK(&confs);
1656                         if (conf->recording == MEETME_RECORD_OFF)
1657                                 break;
1658                         AST_LIST_UNLOCK(&confs);
1659                 }
1660         }
1661
1662         for (x = 0; x < AST_FRAME_BITS; x++) {
1663                 if (conf->transframe[x])
1664                         ast_frfree(conf->transframe[x]);
1665                 if (conf->transpath[x])
1666                         ast_translator_free_path(conf->transpath[x]);
1667         }
1668         if (conf->announcethread != AST_PTHREADT_NULL) {
1669                 ast_mutex_lock(&conf->announcelistlock);
1670                 conf->announcethread_stop = 1;
1671                 ast_softhangup(conf->chan, AST_SOFTHANGUP_EXPLICIT);
1672                 ast_cond_signal(&conf->announcelist_addition);
1673                 ast_mutex_unlock(&conf->announcelistlock);
1674                 pthread_join(conf->announcethread, NULL);
1675         
1676                 while ((item = AST_LIST_REMOVE_HEAD(&conf->announcelist, entry))) {
1677                         ast_filedelete(item->namerecloc, NULL);
1678                         ao2_ref(item, -1);
1679                 }
1680                 ast_mutex_destroy(&conf->announcelistlock);
1681         }
1682         if (conf->origframe)
1683                 ast_frfree(conf->origframe);
1684         if (conf->lchan)
1685                 ast_hangup(conf->lchan);
1686         if (conf->chan)
1687                 ast_hangup(conf->chan);
1688         if (conf->fd >= 0)
1689                 close(conf->fd);
1690         if (conf->recordingfilename) {
1691                 ast_free(conf->recordingfilename);
1692         }
1693         if (conf->recordingformat) {
1694                 ast_free(conf->recordingformat);
1695         }
1696         ast_mutex_destroy(&conf->playlock);
1697         ast_mutex_destroy(&conf->listenlock);
1698         ast_mutex_destroy(&conf->recordthreadlock);
1699         ast_mutex_destroy(&conf->announcethreadlock);
1700         ast_free(conf);
1701
1702         return 0;
1703 }
1704
1705 static void conf_queue_dtmf(const struct ast_conference *conf,
1706         const struct ast_conf_user *sender, struct ast_frame *f)
1707 {
1708         struct ast_conf_user *user;
1709
1710         AST_LIST_TRAVERSE(&conf->userlist, user, list) {
1711                 if (user == sender)
1712                         continue;
1713                 if (ast_write(user->chan, f) < 0)
1714                         ast_log(LOG_WARNING, "Error writing frame to channel %s\n", user->chan->name);
1715         }
1716 }
1717
1718 static void sla_queue_event_full(enum sla_event_type type, 
1719         struct sla_trunk_ref *trunk_ref, struct sla_station *station, int lock)
1720 {
1721         struct sla_event *event;
1722
1723         if (sla.thread == AST_PTHREADT_NULL) {
1724                 return;
1725         }
1726
1727         if (!(event = ast_calloc(1, sizeof(*event))))
1728                 return;
1729
1730         event->type = type;
1731         event->trunk_ref = trunk_ref;
1732         event->station = station;
1733
1734         if (!lock) {
1735                 AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);
1736                 return;
1737         }
1738
1739         ast_mutex_lock(&sla.lock);
1740         AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);
1741         ast_cond_signal(&sla.cond);
1742         ast_mutex_unlock(&sla.lock);
1743 }
1744
1745 static void sla_queue_event_nolock(enum sla_event_type type)
1746 {
1747         sla_queue_event_full(type, NULL, NULL, 0);
1748 }
1749
1750 static void sla_queue_event(enum sla_event_type type)
1751 {
1752         sla_queue_event_full(type, NULL, NULL, 1);
1753 }
1754
1755 /*! \brief Queue a SLA event from the conference */
1756 static void sla_queue_event_conf(enum sla_event_type type, struct ast_channel *chan,
1757         struct ast_conference *conf)
1758 {
1759         struct sla_station *station;
1760         struct sla_trunk_ref *trunk_ref = NULL;
1761         char *trunk_name;
1762
1763         trunk_name = ast_strdupa(conf->confno);
1764         strsep(&trunk_name, "_");
1765         if (ast_strlen_zero(trunk_name)) {
1766                 ast_log(LOG_ERROR, "Invalid conference name for SLA - '%s'!\n", conf->confno);
1767                 return;
1768         }
1769
1770         AST_RWLIST_RDLOCK(&sla_stations);
1771         AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
1772                 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
1773                         if (trunk_ref->chan == chan && !strcmp(trunk_ref->trunk->name, trunk_name))
1774                                 break;
1775                 }
1776                 if (trunk_ref)
1777                         break;
1778         }
1779         AST_RWLIST_UNLOCK(&sla_stations);
1780
1781         if (!trunk_ref) {
1782                 ast_debug(1, "Trunk not found for event!\n");
1783                 return;
1784         }
1785
1786         sla_queue_event_full(type, trunk_ref, station, 1);
1787 }
1788
1789 /* Decrement reference counts, as incremented by find_conf() */
1790 static int dispose_conf(struct ast_conference *conf)
1791 {
1792         int res = 0;
1793         int confno_int = 0;
1794
1795         AST_LIST_LOCK(&confs);
1796         if (ast_atomic_dec_and_test(&conf->refcount)) {
1797                 /* Take the conference room number out of an inuse state */
1798                 if ((sscanf(conf->confno, "%d", &confno_int) == 1) && (confno_int >= 0 && confno_int < 1024)) {
1799                         conf_map[confno_int] = 0;
1800                 }
1801                 conf_free(conf);
1802                 res = 1;
1803         }
1804         AST_LIST_UNLOCK(&confs);
1805
1806         return res;
1807 }
1808
1809 static int rt_extend_conf(char *confno)
1810 {
1811         char currenttime[32];
1812         char endtime[32];
1813         struct timeval now;
1814         struct ast_tm tm;
1815         struct ast_variable *var;
1816         char bookid[8]; 
1817
1818         if (!extendby) {
1819                 return 0;
1820         }
1821
1822         now = ast_tvnow();
1823
1824         ast_localtime(&now, &tm, NULL);
1825         ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
1826
1827         var = ast_load_realtime("meetme", "confno",
1828                 confno, "startTime<= ", currenttime,
1829                 "endtime>= ", currenttime, NULL);
1830
1831         /* Identify the specific RealTime conference */
1832         while (var) {
1833                 if (!strcasecmp(var->name, "bookid")) {
1834                         ast_copy_string(bookid, var->value, sizeof(bookid));
1835                 }
1836                 if (!strcasecmp(var->name, "endtime")) {
1837                         ast_copy_string(endtime, var->value, sizeof(endtime));
1838                 }
1839
1840                 var = var->next;
1841         }
1842         ast_variables_destroy(var);
1843
1844         ast_strptime(endtime, DATE_FORMAT, &tm);
1845         now = ast_mktime(&tm, NULL);
1846
1847         now.tv_sec += extendby;
1848
1849         ast_localtime(&now, &tm, NULL);
1850         ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
1851         strcat(currenttime, "0"); /* Seconds needs to be 00 */
1852
1853         var = ast_load_realtime("meetme", "confno",
1854                 confno, "startTime<= ", currenttime,
1855                 "endtime>= ", currenttime, NULL);
1856
1857         /* If there is no conflict with extending the conference, update the DB */
1858         if (!var) {
1859                 ast_debug(3, "Trying to update the endtime of Conference %s to %s\n", confno, currenttime);
1860                 ast_update_realtime("meetme", "bookid", bookid, "endtime", currenttime, NULL);
1861                 return 0;
1862
1863         }
1864
1865         ast_variables_destroy(var);
1866         return -1;
1867 }
1868
1869 static void conf_start_moh(struct ast_channel *chan, const char *musicclass)
1870 {
1871         char *original_moh;
1872
1873         ast_channel_lock(chan);
1874         original_moh = ast_strdupa(chan->musicclass);
1875         ast_string_field_set(chan, musicclass, musicclass);
1876         ast_channel_unlock(chan);
1877
1878         ast_moh_start(chan, original_moh, NULL);
1879
1880         ast_channel_lock(chan);
1881         ast_string_field_set(chan, musicclass, original_moh);
1882         ast_channel_unlock(chan);
1883 }
1884
1885 static const char *get_announce_filename(enum announcetypes type)
1886 {
1887         switch (type) {
1888         case CONF_HASLEFT:
1889                 return "conf-hasleft";
1890                 break;
1891         case CONF_HASJOIN:
1892                 return "conf-hasjoin";
1893                 break;
1894         default:
1895                 return "";
1896         }
1897 }
1898
1899 static void *announce_thread(void *data)
1900 {
1901         struct announce_listitem *current;
1902         struct ast_conference *conf = data;
1903         int res;
1904         char filename[PATH_MAX] = "";
1905         AST_LIST_HEAD_NOLOCK(, announce_listitem) local_list;
1906         AST_LIST_HEAD_INIT_NOLOCK(&local_list);
1907
1908         while (!conf->announcethread_stop) {
1909                 ast_mutex_lock(&conf->announcelistlock);
1910                 if (conf->announcethread_stop) {
1911                         ast_mutex_unlock(&conf->announcelistlock);
1912                         break;
1913                 }
1914                 if (AST_LIST_EMPTY(&conf->announcelist))
1915                         ast_cond_wait(&conf->announcelist_addition, &conf->announcelistlock);
1916
1917                 AST_LIST_APPEND_LIST(&local_list, &conf->announcelist, entry);
1918                 AST_LIST_HEAD_INIT_NOLOCK(&conf->announcelist);
1919
1920                 ast_mutex_unlock(&conf->announcelistlock);
1921                 if (conf->announcethread_stop) {
1922                         break;
1923                 }
1924
1925                 for (res = 1; !conf->announcethread_stop && (current = AST_LIST_REMOVE_HEAD(&local_list, entry)); ao2_ref(current, -1)) {
1926                         ast_log(LOG_DEBUG, "About to play %s\n", current->namerecloc);
1927                         if (!ast_fileexists(current->namerecloc, NULL, NULL))
1928                                 continue;
1929                         if ((current->confchan) && (current->confusers > 1) && !ast_check_hangup(current->confchan)) {
1930                                 if (!ast_streamfile(current->confchan, current->namerecloc, current->language))
1931                                         res = ast_waitstream(current->confchan, "");
1932                                 if (!res) {
1933                                         ast_copy_string(filename, get_announce_filename(current->announcetype), sizeof(filename));
1934                                         if (!ast_streamfile(current->confchan, filename, current->language))
1935                                                 ast_waitstream(current->confchan, "");
1936                                 }
1937                         }
1938                         if (current->announcetype == CONF_HASLEFT) {
1939                                 ast_filedelete(current->namerecloc, NULL);
1940                         }
1941                 }
1942         }
1943
1944         /* thread marked to stop, clean up */
1945         while ((current = AST_LIST_REMOVE_HEAD(&local_list, entry))) {
1946                 ast_filedelete(current->namerecloc, NULL);
1947                 ao2_ref(current, -1);
1948         }
1949         return NULL;
1950 }
1951
1952 static int can_write(struct ast_channel *chan, int confflags)
1953 {
1954         if (!(confflags & CONFFLAG_NO_AUDIO_UNTIL_UP)) {
1955                 return 1;
1956         }
1957
1958         return (chan->_state == AST_STATE_UP);
1959 }
1960
1961 static int conf_run(struct ast_channel *chan, struct ast_conference *conf, int confflags, char *optargs[])
1962 {
1963         struct ast_conf_user *user = NULL;
1964         struct ast_conf_user *usr = NULL;
1965         int fd;
1966         struct dahdi_confinfo dahdic, dahdic_empty;
1967         struct ast_frame *f;
1968         struct ast_channel *c;
1969         struct ast_frame fr;
1970         int outfd;
1971         int ms;
1972         int nfds;
1973         int res;
1974         int flags;
1975         int retrydahdi;
1976         int origfd;
1977         int musiconhold = 0;
1978         int firstpass = 0;
1979         int lastmarked = 0;
1980         int currentmarked = 0;
1981         int ret = -1;
1982         int x;
1983         int menu_active = 0;
1984         int talkreq_manager = 0;
1985         int using_pseudo = 0;
1986         int duration = 20;
1987         int hr, min, sec;
1988         int sent_event = 0;
1989         int checked = 0;
1990         int announcement_played = 0;
1991         struct timeval now;
1992         struct ast_dsp *dsp = NULL;
1993         struct ast_app *agi_app;
1994         char *agifile;
1995         const char *agifiledefault = "conf-background.agi", *tmpvar;
1996         char meetmesecs[30] = "";
1997         char exitcontext[AST_MAX_CONTEXT] = "";
1998         char recordingtmp[AST_MAX_EXTENSION] = "";
1999         char members[10] = "";
2000         int dtmf, opt_waitmarked_timeout = 0;
2001         time_t timeout = 0;
2002         struct dahdi_bufferinfo bi;
2003         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
2004         char *buf = __buf + AST_FRIENDLY_OFFSET;
2005         char *exitkeys = NULL;
2006         unsigned int calldurationlimit = 0;
2007         long timelimit = 0;
2008         long play_warning = 0;
2009         long warning_freq = 0;
2010         const char *warning_sound = NULL;
2011         const char *end_sound = NULL;
2012         char *parse;    
2013         long time_left_ms = 0;
2014         struct timeval nexteventts = { 0, };
2015         int to;
2016         int setusercount = 0;
2017
2018         if (!(user = ast_calloc(1, sizeof(*user))))
2019                 return ret;
2020
2021         /* Possible timeout waiting for marked user */
2022         if ((confflags & CONFFLAG_WAITMARKED) &&
2023                 !ast_strlen_zero(optargs[OPT_ARG_WAITMARKED]) &&
2024                 (sscanf(optargs[OPT_ARG_WAITMARKED], "%d", &opt_waitmarked_timeout) == 1) &&
2025                 (opt_waitmarked_timeout > 0)) {
2026                 timeout = time(NULL) + opt_waitmarked_timeout;
2027         }
2028                 
2029         if ((confflags & CONFFLAG_DURATION_STOP) && !ast_strlen_zero(optargs[OPT_ARG_DURATION_STOP])) {
2030                 calldurationlimit = atoi(optargs[OPT_ARG_DURATION_STOP]);
2031                 ast_verb(3, "Setting call duration limit to %d seconds.\n", calldurationlimit);
2032         }
2033         
2034         if ((confflags & CONFFLAG_DURATION_LIMIT) && !ast_strlen_zero(optargs[OPT_ARG_DURATION_LIMIT])) {
2035                 char *limit_str, *warning_str, *warnfreq_str;
2036                 const char *var;
2037  
2038                 parse = optargs[OPT_ARG_DURATION_LIMIT];
2039                 limit_str = strsep(&parse, ":");
2040                 warning_str = strsep(&parse, ":");
2041                 warnfreq_str = parse;
2042  
2043                 timelimit = atol(limit_str);
2044                 if (warning_str)
2045                         play_warning = atol(warning_str);
2046                 if (warnfreq_str)
2047                         warning_freq = atol(warnfreq_str);
2048  
2049                 if (!timelimit) {
2050                         timelimit = play_warning = warning_freq = 0;
2051                         warning_sound = NULL;
2052                 } else if (play_warning > timelimit) {                  
2053                         if (!warning_freq) {
2054                                 play_warning = 0;
2055                         } else {
2056                                 while (play_warning > timelimit)
2057                                         play_warning -= warning_freq;
2058                                 if (play_warning < 1)
2059                                         play_warning = warning_freq = 0;
2060                         }
2061                 }
2062                 
2063                 ast_channel_lock(chan);
2064                 if ((var = pbx_builtin_getvar_helper(chan, "CONF_LIMIT_WARNING_FILE"))) {
2065                         var = ast_strdupa(var);
2066                 }
2067                 ast_channel_unlock(chan);
2068
2069                 warning_sound = var ? var : "timeleft";
2070                 
2071                 ast_channel_lock(chan);
2072                 if ((var = pbx_builtin_getvar_helper(chan, "CONF_LIMIT_TIMEOUT_FILE"))) {
2073                         var = ast_strdupa(var);
2074                 }
2075                 ast_channel_unlock(chan);
2076                 
2077                 end_sound = var ? var : NULL;
2078                         
2079                 /* undo effect of S(x) in case they are both used */
2080                 calldurationlimit = 0;
2081                 /* more efficient do it like S(x) does since no advanced opts */
2082                 if (!play_warning && !end_sound && timelimit) { 
2083                         calldurationlimit = timelimit / 1000;
2084                         timelimit = play_warning = warning_freq = 0;
2085                 } else {
2086                         ast_debug(2, "Limit Data for this call:\n");
2087                         ast_debug(2, "- timelimit     = %ld\n", timelimit);
2088                         ast_debug(2, "- play_warning  = %ld\n", play_warning);
2089                         ast_debug(2, "- warning_freq  = %ld\n", warning_freq);
2090                         ast_debug(2, "- warning_sound = %s\n", warning_sound ? warning_sound : "UNDEF");
2091                         ast_debug(2, "- end_sound     = %s\n", end_sound ? end_sound : "UNDEF");
2092                 }
2093         }
2094
2095         /* Get exit keys */
2096         if ((confflags & CONFFLAG_KEYEXIT)) {
2097                 if (!ast_strlen_zero(optargs[OPT_ARG_EXITKEYS]))
2098                         exitkeys = ast_strdupa(optargs[OPT_ARG_EXITKEYS]);
2099                 else
2100                         exitkeys = ast_strdupa("#"); /* Default */
2101         }
2102         
2103         if (confflags & CONFFLAG_RECORDCONF) {
2104                 if (!conf->recordingfilename) {
2105                         const char *var;
2106                         ast_channel_lock(chan);
2107                         if ((var = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFILE"))) {
2108                                 conf->recordingfilename = ast_strdup(var);
2109                         }
2110                         if ((var = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFORMAT"))) {
2111                                 conf->recordingformat = ast_strdup(var);
2112                         }
2113                         ast_channel_unlock(chan);
2114                         if (!conf->recordingfilename) {
2115                                 snprintf(recordingtmp, sizeof(recordingtmp), "meetme-conf-rec-%s-%s", conf->confno, chan->uniqueid);
2116                                 conf->recordingfilename = ast_strdup(recordingtmp);
2117                         }
2118                         if (!conf->recordingformat) {
2119                                 conf->recordingformat = ast_strdup("wav");
2120                         }
2121                         ast_verb(4, "Starting recording of MeetMe Conference %s into file %s.%s.\n",
2122                                     conf->confno, conf->recordingfilename, conf->recordingformat);
2123                 }
2124         }
2125
2126         ast_mutex_lock(&conf->recordthreadlock);
2127         if ((conf->recordthread == AST_PTHREADT_NULL) && (confflags & CONFFLAG_RECORDCONF) && ((conf->lchan = ast_request("DAHDI", AST_FORMAT_SLINEAR, "pseudo", NULL)))) {
2128                 ast_set_read_format(conf->lchan, AST_FORMAT_SLINEAR);
2129                 ast_set_write_format(conf->lchan, AST_FORMAT_SLINEAR);
2130                 dahdic.chan = 0;
2131                 dahdic.confno = conf->dahdiconf;
2132                 dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
2133                 if (ioctl(conf->lchan->fds[0], DAHDI_SETCONF, &dahdic)) {
2134                         ast_log(LOG_WARNING, "Error starting listen channel\n");
2135                         ast_hangup(conf->lchan);
2136                         conf->lchan = NULL;
2137                 } else {
2138                         ast_pthread_create_detached_background(&conf->recordthread, NULL, recordthread, conf);
2139                 }
2140         }
2141         ast_mutex_unlock(&conf->recordthreadlock);
2142
2143         ast_mutex_lock(&conf->announcethreadlock);
2144         if ((conf->announcethread == AST_PTHREADT_NULL) && !(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW))) {
2145                 ast_mutex_init(&conf->announcelistlock);
2146                 AST_LIST_HEAD_INIT_NOLOCK(&conf->announcelist);
2147                 ast_pthread_create_background(&conf->announcethread, NULL, announce_thread, conf);
2148         }
2149         ast_mutex_unlock(&conf->announcethreadlock);
2150
2151         time(&user->jointime);
2152         
2153         user->timelimit = timelimit;
2154         user->play_warning = play_warning;
2155         user->warning_freq = warning_freq;
2156         user->warning_sound = warning_sound;
2157         user->end_sound = end_sound;    
2158         
2159         if (calldurationlimit > 0) {
2160                 time(&user->kicktime);
2161                 user->kicktime = user->kicktime + calldurationlimit;
2162         }
2163         
2164         if (ast_tvzero(user->start_time))
2165                 user->start_time = ast_tvnow();
2166         time_left_ms = user->timelimit;
2167         
2168         if (user->timelimit) {
2169                 nexteventts = ast_tvadd(user->start_time, ast_samp2tv(user->timelimit, 1000));
2170                 nexteventts = ast_tvsub(nexteventts, ast_samp2tv(user->play_warning, 1000));
2171         }
2172
2173         if (conf->locked && (!(confflags & CONFFLAG_ADMIN))) {
2174                 /* Sorry, but this conference is locked! */     
2175                 if (!ast_streamfile(chan, "conf-locked", chan->language))
2176                         ast_waitstream(chan, "");
2177                 goto outrun;
2178         }
2179
2180         ast_mutex_lock(&conf->playlock);
2181
2182         if (AST_LIST_EMPTY(&conf->userlist))
2183                 user->user_no = 1;
2184         else
2185                 user->user_no = AST_LIST_LAST(&conf->userlist)->user_no + 1;
2186
2187         if (rt_schedule && conf->maxusers)
2188                 if (user->user_no > conf->maxusers) {
2189                         /* Sorry, but this confernce has reached the participant limit! */      
2190                         if (!ast_streamfile(chan, "conf-full", chan->language))
2191                                 ast_waitstream(chan, "");
2192                         goto outrun;
2193                 }
2194
2195         AST_LIST_INSERT_TAIL(&conf->userlist, user, list);
2196
2197         user->chan = chan;
2198         user->userflags = confflags;
2199         user->adminflags = (confflags & CONFFLAG_STARTMUTED) ? ADMINFLAG_SELFMUTED : 0;
2200         user->talking = -1;
2201
2202         ast_mutex_unlock(&conf->playlock);
2203
2204         if (!(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW))) {
2205                 char destdir[PATH_MAX];
2206
2207                 snprintf(destdir, sizeof(destdir), "%s/meetme", ast_config_AST_SPOOL_DIR);
2208
2209                 if (ast_mkdir(destdir, 0777) != 0) {
2210                         ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", destdir, strerror(errno));
2211                         goto outrun;
2212                 }
2213
2214                 snprintf(user->namerecloc, sizeof(user->namerecloc),
2215                          "%s/meetme-username-%s-%d", destdir,
2216                          conf->confno, user->user_no);
2217                 if (confflags & CONFFLAG_INTROUSERNOREVIEW)
2218                         res = ast_play_and_record(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL);
2219                 else
2220                         res = ast_record_review(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, NULL);
2221                 if (res == -1)
2222                         goto outrun;
2223         }
2224
2225         ast_mutex_lock(&conf->playlock);
2226
2227         if (confflags & CONFFLAG_MARKEDUSER)
2228                 conf->markedusers++;
2229         conf->users++;
2230         if (rt_log_members) {
2231                 /* Update table */
2232                 snprintf(members, sizeof(members), "%d", conf->users);
2233                 ast_realtime_require_field("meetme",
2234                         "confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
2235                         "members", RQ_UINTEGER1, strlen(members),
2236                         NULL);
2237                 ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
2238         }
2239         setusercount = 1;
2240
2241         /* This device changed state now - if this is the first user */
2242         if (conf->users == 1)
2243                 ast_devstate_changed(AST_DEVICE_INUSE, "meetme:%s", conf->confno);
2244
2245         ast_mutex_unlock(&conf->playlock);
2246
2247         /* return the unique ID of the conference */
2248         pbx_builtin_setvar_helper(chan, "MEETMEUNIQUEID", conf->uniqueid);
2249
2250         if (confflags & CONFFLAG_EXIT_CONTEXT) {
2251                 ast_channel_lock(chan);
2252                 if ((tmpvar = pbx_builtin_getvar_helper(chan, "MEETME_EXIT_CONTEXT"))) {
2253                         ast_copy_string(exitcontext, tmpvar, sizeof(exitcontext));
2254                 } else if (!ast_strlen_zero(chan->macrocontext)) {
2255                         ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext));
2256                 } else {
2257                         ast_copy_string(exitcontext, chan->context, sizeof(exitcontext));
2258                 }
2259                 ast_channel_unlock(chan);
2260         }
2261
2262         if (!(confflags & (CONFFLAG_QUIET | CONFFLAG_NOONLYPERSON))) {
2263                 if (conf->users == 1 && !(confflags & CONFFLAG_WAITMARKED))
2264                         if (!ast_streamfile(chan, "conf-onlyperson", chan->language))
2265                                 ast_waitstream(chan, "");
2266                 if ((confflags & CONFFLAG_WAITMARKED) && conf->markedusers == 0)
2267                         if (!ast_streamfile(chan, "conf-waitforleader", chan->language))
2268                                 ast_waitstream(chan, "");
2269         }
2270
2271         if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_ANNOUNCEUSERCOUNT) && conf->users > 1) {
2272                 int keepplaying = 1;
2273
2274                 if (conf->users == 2) { 
2275                         if (!ast_streamfile(chan, "conf-onlyone", chan->language)) {
2276                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
2277                                 ast_stopstream(chan);
2278                                 if (res > 0)
2279                                         keepplaying = 0;
2280                                 else if (res == -1)
2281                                         goto outrun;
2282                         }
2283                 } else { 
2284                         if (!ast_streamfile(chan, "conf-thereare", chan->language)) {
2285                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
2286                                 ast_stopstream(chan);
2287                                 if (res > 0)
2288                                         keepplaying = 0;
2289                                 else if (res == -1)
2290                                         goto outrun;
2291                         }
2292                         if (keepplaying) {
2293                                 res = ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
2294                                 if (res > 0)
2295                                         keepplaying = 0;
2296                                 else if (res == -1)
2297                                         goto outrun;
2298                         }
2299                         if (keepplaying && !ast_streamfile(chan, "conf-otherinparty", chan->language)) {
2300                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
2301                                 ast_stopstream(chan);
2302                                 if (res > 0)
2303                                         keepplaying = 0;
2304                                 else if (res == -1) 
2305                                         goto outrun;
2306                         }
2307                 }
2308         }
2309
2310         if (!(confflags & CONFFLAG_NO_AUDIO_UNTIL_UP)) {
2311                 /* We're leaving this alone until the state gets changed to up */
2312                 ast_indicate(chan, -1);
2313         }
2314
2315         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
2316                 ast_log(LOG_WARNING, "Unable to set '%s' to write linear mode\n", chan->name);
2317                 goto outrun;
2318         }
2319
2320         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
2321                 ast_log(LOG_WARNING, "Unable to set '%s' to read linear mode\n", chan->name);
2322                 goto outrun;
2323         }
2324
2325         retrydahdi = (strcasecmp(chan->tech->type, "DAHDI") || (chan->audiohooks || chan->monitor) ? 1 : 0);
2326         user->dahdichannel = !retrydahdi;
2327
2328  dahdiretry:
2329         origfd = chan->fds[0];
2330         if (retrydahdi) {
2331                 fd = open("/dev/dahdi/pseudo", O_RDWR);
2332                 if (fd < 0) {
2333                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
2334                         goto outrun;
2335                 }
2336                 using_pseudo = 1;
2337                 /* Make non-blocking */
2338                 flags = fcntl(fd, F_GETFL);
2339                 if (flags < 0) {
2340                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
2341                         close(fd);
2342                         goto outrun;
2343                 }
2344                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
2345                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
2346                         close(fd);
2347                         goto outrun;
2348                 }
2349                 /* Setup buffering information */
2350                 memset(&bi, 0, sizeof(bi));
2351                 bi.bufsize = CONF_SIZE / 2;
2352                 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
2353                 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
2354                 bi.numbufs = audio_buffers;
2355                 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
2356                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
2357                         close(fd);
2358                         goto outrun;
2359                 }
2360                 x = 1;
2361                 if (ioctl(fd, DAHDI_SETLINEAR, &x)) {
2362                         ast_log(LOG_WARNING, "Unable to set linear mode: %s\n", strerror(errno));
2363                         close(fd);
2364                         goto outrun;
2365                 }
2366                 nfds = 1;
2367         } else {
2368                 /* XXX Make sure we're not running on a pseudo channel XXX */
2369                 fd = chan->fds[0];
2370                 nfds = 0;
2371         }
2372         memset(&dahdic, 0, sizeof(dahdic));
2373         memset(&dahdic_empty, 0, sizeof(dahdic_empty));
2374         /* Check to see if we're in a conference... */
2375         dahdic.chan = 0;        
2376         if (ioctl(fd, DAHDI_GETCONF, &dahdic)) {
2377                 ast_log(LOG_WARNING, "Error getting conference\n");
2378                 close(fd);
2379                 goto outrun;
2380         }
2381         if (dahdic.confmode) {
2382                 /* Whoa, already in a conference...  Retry... */
2383                 if (!retrydahdi) {
2384                         ast_debug(1, "DAHDI channel is in a conference already, retrying with pseudo\n");
2385                         retrydahdi = 1;
2386                         goto dahdiretry;
2387                 }
2388         }
2389         memset(&dahdic, 0, sizeof(dahdic));
2390         /* Add us to the conference */
2391         dahdic.chan = 0;        
2392         dahdic.confno = conf->dahdiconf;
2393
2394         if (!(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW)) && conf->users > 1) {
2395                 struct announce_listitem *item;
2396                 if (!(item = ao2_alloc(sizeof(*item), NULL)))
2397                         return -1;
2398                 ast_copy_string(item->namerecloc, user->namerecloc, sizeof(item->namerecloc));
2399                 ast_copy_string(item->language, chan->language, sizeof(item->language));
2400                 item->confchan = conf->chan;
2401                 item->confusers = conf->users;
2402                 item->announcetype = CONF_HASJOIN;
2403                 ast_mutex_lock(&conf->announcelistlock);
2404                 ao2_ref(item, +1); /* add one more so we can determine when announce_thread is done playing it */
2405                 AST_LIST_INSERT_TAIL(&conf->announcelist, item, entry);
2406                 ast_cond_signal(&conf->announcelist_addition);
2407                 ast_mutex_unlock(&conf->announcelistlock);
2408
2409                 while (!ast_check_hangup(conf->chan) && ao2_ref(item, 0) == 2 && !ast_safe_sleep(chan, 1000)) {
2410                         ;
2411                 }
2412                 ao2_ref(item, -1);
2413         }
2414
2415         if (confflags & CONFFLAG_WAITMARKED && !conf->markedusers)
2416                 dahdic.confmode = DAHDI_CONF_CONF;
2417         else if (confflags & CONFFLAG_MONITOR)
2418                 dahdic.confmode = DAHDI_CONF_CONFMON | DAHDI_CONF_LISTENER;
2419         else if (confflags & CONFFLAG_TALKER)
2420                 dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER;
2421         else 
2422                 dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
2423
2424         if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
2425                 ast_log(LOG_WARNING, "Error setting conference\n");
2426                 close(fd);
2427                 goto outrun;
2428         }
2429         ast_debug(1, "Placed channel %s in DAHDI conf %d\n", chan->name, conf->dahdiconf);
2430
2431         if (!sent_event) {
2432                 manager_event(EVENT_FLAG_CALL, "MeetmeJoin", 
2433                                 "Channel: %s\r\n"
2434                                 "Uniqueid: %s\r\n"
2435                                 "Meetme: %s\r\n"
2436                                 "Usernum: %d\r\n"
2437                                 "CallerIDnum: %s\r\n"
2438                                 "CallerIDname: %s\r\n",
2439                                 chan->name, chan->uniqueid, conf->confno, 
2440                                 user->user_no,
2441                                 S_OR(user->chan->cid.cid_num, "<unknown>"),
2442                                 S_OR(user->chan->cid.cid_name, "<unknown>")
2443                                 );
2444                 sent_event = 1;
2445         }
2446
2447         if (!firstpass && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN)) {
2448                 firstpass = 1;
2449                 if (!(confflags & CONFFLAG_QUIET))
2450                         if (!(confflags & CONFFLAG_WAITMARKED) || ((confflags & CONFFLAG_MARKEDUSER) && (conf->markedusers >= 1)))
2451                                 conf_play(chan, conf, ENTER);
2452         }
2453
2454         conf_flush(fd, chan);
2455
2456         if (confflags & CONFFLAG_AGI) {
2457                 /* Get name of AGI file to run from $(MEETME_AGI_BACKGROUND)
2458                    or use default filename of conf-background.agi */
2459
2460                 ast_channel_lock(chan);
2461                 if ((tmpvar = pbx_builtin_getvar_helper(chan, "MEETME_AGI_BACKGROUND"))) {
2462                         agifile = ast_strdupa(tmpvar);
2463                 } else {
2464                         agifile = ast_strdupa(agifiledefault);
2465                 }
2466                 ast_channel_unlock(chan);
2467                 
2468                 if (user->dahdichannel) {
2469                         /*  Set CONFMUTE mode on DAHDI channel to mute DTMF tones */
2470                         x = 1;
2471                         ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
2472                 }
2473                 /* Find a pointer to the agi app and execute the script */
2474                 agi_app = pbx_findapp("agi");
2475                 if (agi_app) {
2476                         ret = pbx_exec(chan, agi_app, agifile);
2477                 } else {
2478                         ast_log(LOG_WARNING, "Could not find application (agi)\n");
2479                         ret = -2;
2480                 }
2481                 if (user->dahdichannel) {
2482                         /*  Remove CONFMUTE mode on DAHDI channel */
2483                         x = 0;
2484                         ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
2485                 }
2486         } else {
2487                 if (user->dahdichannel && (confflags & CONFFLAG_STARMENU)) {
2488                         /*  Set CONFMUTE mode on DAHDI channel to mute DTMF tones when the menu is enabled */
2489                         x = 1;
2490                         ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
2491                 }       
2492                 if (!(confflags & CONFFLAG_MONITOR) && !(dsp = ast_dsp_new())) {
2493                         ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
2494                         res = -1;
2495                 }
2496                 for (;;) {
2497                         int menu_was_active = 0;
2498
2499                         outfd = -1;
2500                         ms = -1;
2501                         now = ast_tvnow();
2502
2503                         if (rt_schedule && conf->endtime) {
2504                                 char currenttime[32];
2505                                 long localendtime = 0;
2506                                 int extended = 0;
2507                                 struct ast_tm tm;
2508                                 struct ast_variable *var, *origvar;
2509                                 struct timeval tmp;
2510
2511                                 if (now.tv_sec % 60 == 0) {
2512                                         if (!checked) {
2513                                                 ast_localtime(&now, &tm, NULL);
2514                                                 ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
2515                                                 var = origvar = ast_load_realtime("meetme", "confno",
2516                                                         conf->confno, "starttime <=", currenttime,
2517                                                          "endtime >=", currenttime, NULL);
2518
2519                                                 for ( ; var; var = var->next) {
2520                                                         if (!strcasecmp(var->name, "endtime")) {
2521                                                                 struct ast_tm endtime_tm;
2522                                                                 ast_strptime(var->value, "%Y-%m-%d %H:%M:%S", &endtime_tm);
2523                                                                 tmp = ast_mktime(&endtime_tm, NULL);
2524                                                                 localendtime = tmp.tv_sec;
2525                                                         }
2526                                                 }
2527                                                 ast_variables_destroy(origvar);
2528
2529                                                 /* A conference can be extended from the
2530                                                    Admin/User menu or by an external source */
2531                                                 if (localendtime > conf->endtime){
2532                                                         conf->endtime = localendtime;
2533                                                         extended = 1;
2534                                                 }
2535
2536                                                 if (conf->endtime && (now.tv_sec >= conf->endtime)) {
2537                                                         ast_verbose("Quitting time...\n");
2538                                                         goto outrun;
2539                                                 }
2540
2541                                                 if (!announcement_played && conf->endalert) {
2542                                                         if (now.tv_sec + conf->endalert >= conf->endtime) {
2543                                                                 if (!ast_streamfile(chan, "conf-will-end-in", chan->language))
2544                                                                         ast_waitstream(chan, "");
2545                                                                 ast_say_digits(chan, (conf->endtime - now.tv_sec) / 60, "", chan->language);
2546                                                                 if (!ast_streamfile(chan, "minutes", chan->language))
2547                                                                         ast_waitstream(chan, "");
2548                                                                 announcement_played = 1;
2549                                                         }
2550                                                 }
2551
2552                                                 if (extended) {
2553                                                         announcement_played = 0;
2554                                                 }
2555
2556                                                 checked = 1;
2557                                         }
2558                                 } else {
2559                                         checked = 0;
2560                                 }
2561                         }
2562
2563                         if (user->kicktime && (user->kicktime <= now.tv_sec)) {
2564                                 break;
2565                         }
2566   
2567                         to = -1;
2568                         if (user->timelimit) {
2569                                 int minutes = 0, seconds = 0, remain = 0;
2570  
2571                                 to = ast_tvdiff_ms(nexteventts, now);
2572                                 if (to < 0) {
2573                                         to = 0;
2574                                 }
2575                                 time_left_ms = user->timelimit - ast_tvdiff_ms(now, user->start_time);
2576                                 if (time_left_ms < to) {
2577                                         to = time_left_ms;
2578                                 }
2579         
2580                                 if (time_left_ms <= 0) {
2581                                         if (user->end_sound) {                                          
2582                                                 res = ast_streamfile(chan, user->end_sound, chan->language);
2583                                                 res = ast_waitstream(chan, "");
2584                                         }
2585                                         break;
2586                                 }
2587                                 
2588                                 if (!to) {
2589                                         if (time_left_ms >= 5000) {                                             
2590                                                 
2591                                                 remain = (time_left_ms + 500) / 1000;
2592                                                 if (remain / 60 >= 1) {
2593                                                         minutes = remain / 60;
2594                                                         seconds = remain % 60;
2595                                                 } else {
2596                                                         seconds = remain;
2597                                                 }
2598                                                 
2599                                                 /* force the time left to round up if appropriate */
2600                                                 if (user->warning_sound && user->play_warning) {
2601                                                         if (!strcmp(user->warning_sound, "timeleft")) {
2602                                                                 
2603                                                                 res = ast_streamfile(chan, "vm-youhave", chan->language);
2604                                                                 res = ast_waitstream(chan, "");
2605                                                                 if (minutes) {
2606                                                                         res = ast_say_number(chan, minutes, AST_DIGIT_ANY, chan->language, (char *) NULL);
2607                                                                         res = ast_streamfile(chan, "queue-minutes", chan->language);
2608                                                                         res = ast_waitstream(chan, "");
2609                                                                 }
2610                                                                 if (seconds) {
2611                                                                         res = ast_say_number(chan, seconds, AST_DIGIT_ANY, chan->language, (char *) NULL);
2612                                                                         res = ast_streamfile(chan, "queue-seconds", chan->language);
2613                                                                         res = ast_waitstream(chan, "");
2614                                                                 }
2615                                                         } else {
2616                                                                 res = ast_streamfile(chan, user->warning_sound, chan->language);
2617                                                                 res = ast_waitstream(chan, "");
2618                                                         }
2619                                                 }
2620                                         }
2621                                         if (user->warning_freq) {
2622                                                 nexteventts = ast_tvadd(nexteventts, ast_samp2tv(user->warning_freq, 1000));
2623                                         } else {
2624                                                 nexteventts = ast_tvadd(user->start_time, ast_samp2tv(user->timelimit, 1000));
2625                                         }
2626                                 }
2627                         }
2628
2629                         now = ast_tvnow();
2630                         if (timeout && now.tv_sec >= timeout) {
2631                                 break;
2632                         }
2633
2634                         /* if we have just exited from the menu, and the user had a channel-driver
2635                            volume adjustment, restore it
2636                         */
2637                         if (!menu_active && menu_was_active && user->listen.desired && !user->listen.actual) {
2638                                 set_talk_volume(user, user->listen.desired);
2639                         }
2640
2641                         menu_was_active = menu_active;
2642
2643                         currentmarked = conf->markedusers;
2644                         if (!(confflags & CONFFLAG_QUIET) &&
2645                             (confflags & CONFFLAG_MARKEDUSER) &&
2646                             (confflags & CONFFLAG_WAITMARKED) &&
2647                             lastmarked == 0) {
2648                                 if (currentmarked == 1 && conf->users > 1) {
2649                                         ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
2650                                         if (conf->users - 1 == 1) {
2651                                                 if (!ast_streamfile(chan, "conf-userwilljoin", chan->language)) {
2652                                                         ast_waitstream(chan, "");
2653                                                 }
2654                                         } else {
2655                                                 if (!ast_streamfile(chan, "conf-userswilljoin", chan->language)) {
2656                                                         ast_waitstream(chan, "");
2657                                                 }
2658                                         }
2659                                 }
2660                                 if (conf->users == 1 && ! (confflags & CONFFLAG_MARKEDUSER)) {
2661                                         if (!ast_streamfile(chan, "conf-onlyperson", chan->language)) {
2662                                                 ast_waitstream(chan, "");
2663                                         }
2664                                 }
2665                         }
2666
2667                         c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
2668
2669                         /* Update the struct with the actual confflags */
2670                         user->userflags = confflags;
2671
2672                         if (confflags & CONFFLAG_WAITMARKED) {
2673                                 if (currentmarked == 0) {
2674                                         if (lastmarked != 0) {
2675                                                 if (!(confflags & CONFFLAG_QUIET)) {
2676                                                         if (!ast_streamfile(chan, "conf-leaderhasleft", chan->language)) {
2677                                                                 ast_waitstream(chan, "");
2678                                                         }
2679                                                 }
2680                                                 if (confflags & CONFFLAG_MARKEDEXIT) {
2681                                                         if (confflags & CONFFLAG_KICK_CONTINUE) {
2682                                                                 ret = 0;
2683                                                         }
2684                                                         break;
2685                                                 } else {
2686                                                         dahdic.confmode = DAHDI_CONF_CONF;
2687                                                         if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
2688                                                                 ast_log(LOG_WARNING, "Error setting conference\n");
2689                                                                 close(fd);
2690                                                                 goto outrun;
2691                                                         }
2692                                                 }
2693                                         }
2694                                         if (!musiconhold && (confflags & CONFFLAG_MOH)) {
2695                                                 conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
2696                                                 musiconhold = 1;
2697                                         }
2698                                 } else if (currentmarked >= 1 && lastmarked == 0) {
2699                                         /* Marked user entered, so cancel timeout */
2700                                         timeout = 0;
2701                                         if (confflags & CONFFLAG_MONITOR) {
2702                                                 dahdic.confmode = DAHDI_CONF_CONFMON | DAHDI_CONF_LISTENER;
2703                                         } else if (confflags & CONFFLAG_TALKER) {
2704                                                 dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER;
2705                                         } else {
2706                                                 dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
2707                                         }
2708                                         if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
2709                                                 ast_log(LOG_WARNING, "Error setting conference\n");
2710                                                 close(fd);
2711                                                 goto outrun;
2712                                         }
2713                                         if (musiconhold && (confflags & CONFFLAG_MOH)) {
2714                                                 ast_moh_stop(chan);
2715                                                 musiconhold = 0;
2716                                         }
2717                                         if (!(confflags & CONFFLAG_QUIET) && !(confflags & CONFFLAG_MARKEDUSER)) {
2718                                                 if (!ast_streamfile(chan, "conf-placeintoconf", chan->language)) {
2719                                                         ast_waitstream(chan, "");
2720                                                 }
2721                                                 conf_play(chan, conf, ENTER);
2722                                         }
2723                                 }
2724                         }
2725
2726                         /* trying to add moh for single person conf */
2727                         if ((confflags & CONFFLAG_MOH) && !(confflags & CONFFLAG_WAITMARKED)) {
2728                                 if (conf->users == 1) {
2729                                         if (!musiconhold) {
2730                                                 conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
2731                                                 musiconhold = 1;
2732                                         } 
2733                                 } else {
2734                                         if (musiconhold) {
2735                                                 ast_moh_stop(chan);
2736                                                 musiconhold = 0;
2737                                         }
2738                                 }
2739                         }
2740                         
2741                         /* Leave if the last marked user left */
2742                         if (currentmarked == 0 && lastmarked != 0 && (confflags & CONFFLAG_MARKEDEXIT)) {
2743                                 if (confflags & CONFFLAG_KICK_CONTINUE) {
2744                                         ret = 0;
2745                                 } else {
2746                                         ret = -1;
2747                                 }
2748                                 break;
2749                         }
2750         
2751                         /* Check if my modes have changed */
2752
2753                         /* If I should be muted but am still talker, mute me */
2754                         if ((user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && (dahdic.confmode & DAHDI_CONF_TALKER)) {
2755                                 dahdic.confmode ^= DAHDI_CONF_TALKER;
2756                                 if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
2757                                         ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
2758                                         ret = -1;
2759                                         break;
2760                                 }
2761
2762                                 manager_event(EVENT_FLAG_CALL, "MeetmeMute", 
2763                                                 "Channel: %s\r\n"
2764                                                 "Uniqueid: %s\r\n"
2765                                                 "Meetme: %s\r\n"
2766                                                 "Usernum: %i\r\n"
2767                                                 "Status: on\r\n",
2768                                                 chan->name, chan->uniqueid, conf->confno, user->user_no);
2769                         }
2770
2771                         /* If I should be un-muted but am not talker, un-mute me */
2772                         if (!(user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && !(confflags & CONFFLAG_MONITOR) && !(dahdic.confmode & DAHDI_CONF_TALKER)) {
2773                                 dahdic.confmode |= DAHDI_CONF_TALKER;
2774                                 if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
2775                                         ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
2776                                         ret = -1;
2777                                         break;
2778                                 }
2779
2780                                 manager_event(EVENT_FLAG_CALL, "MeetmeMute", 
2781                                                 "Channel: %s\r\n"
2782                                                 "Uniqueid: %s\r\n"
2783                                                 "Meetme: %s\r\n"
2784                                                 "Usernum: %i\r\n"
2785                                                 "Status: off\r\n",
2786                                                 chan->name, chan->uniqueid, conf->confno, user->user_no);
2787                         }
2788                         
2789                         if ((user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && 
2790                                 (user->adminflags & ADMINFLAG_T_REQUEST) && !(talkreq_manager)) {
2791                                 talkreq_manager = 1;
2792
2793                                 manager_event(EVENT_FLAG_CALL, "MeetmeTalkRequest", 
2794                                               "Channel: %s\r\n"
2795                                                               "Uniqueid: %s\r\n"
2796                                                               "Meetme: %s\r\n"
2797                                                               "Usernum: %i\r\n"
2798                                                               "Status: on\r\n",
2799                                                               chan->name, chan->uniqueid, conf->confno, user->user_no);
2800                         }
2801
2802                         
2803                         if (!(user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && 
2804                                 !(user->adminflags & ADMINFLAG_T_REQUEST) && (talkreq_manager)) {
2805                                 talkreq_manager = 0;
2806                                 manager_event(EVENT_FLAG_CALL, "MeetmeTalkRequest", 
2807                                               "Channel: %s\r\n"
2808                                                               "Uniqueid: %s\r\n"
2809                                                               "Meetme: %s\r\n"
2810                                                               "Usernum: %i\r\n"
2811                                                               "Status: off\r\n",
2812                                                              chan->name, chan->uniqueid, conf->confno, user->user_no);
2813                         }
2814                         
2815                         /* If I have been kicked, exit the conference */
2816                         if (user->adminflags & ADMINFLAG_KICKME) {
2817                                 /* You have been kicked. */
2818                                 if (!(confflags & CONFFLAG_QUIET) && 
2819                                         !ast_streamfile(chan, "conf-kicked", chan->language)) {
2820                                         ast_waitstream(chan, "");
2821                                 }
2822                                 ret = 0;
2823                                 break;
2824                         }
2825
2826                         /* Perform an extra hangup check just in case */
2827                         if (ast_check_hangup(chan)) {
2828                                 break;
2829                         }
2830
2831                         if (c) {
2832                                 char dtmfstr[2] = "";
2833
2834                                 if (c->fds[0] != origfd || (user->dahdichannel && (c->audiohooks || c->monitor))) {
2835                                         if (using_pseudo) {
2836                                                 /* Kill old pseudo */
2837                                                 close(fd);
2838                                                 using_pseudo = 0;
2839                                         }
2840                                         ast_debug(1, "Ooh, something swapped out under us, starting over\n");
2841                                         retrydahdi = (strcasecmp(c->tech->type, "DAHDI") || (c->audiohooks || c->monitor) ? 1 : 0);
2842                                         user->dahdichannel = !retrydahdi;
2843                                         goto dahdiretry;
2844                                 }
2845                                 if ((confflags & CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
2846                                         f = ast_read_noaudio(c);
2847                                 } else {
2848                                         f = ast_read(c);
2849                                 }
2850                                 if (!f) {
2851                                         break;
2852                                 }
2853                                 if (f->frametype == AST_FRAME_DTMF) {
2854                                         dtmfstr[0] = f->subclass;
2855                                         dtmfstr[1] = '\0';
2856                                 }
2857
2858                                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
2859                                         if (user->talk.actual) {
2860                                                 ast_frame_adjust_volume(f, user->talk.actual);
2861                                         }
2862
2863                                         if (!(confflags & CONFFLAG_MONITOR)) {
2864                                                 int totalsilence;
2865
2866                                                 if (user->talking == -1) {
2867                                                         user->talking = 0;
2868                                                 }
2869
2870                                                 res = ast_dsp_silence(dsp, f, &totalsilence);
2871                                                 if (!user->talking && totalsilence < MEETME_DELAYDETECTTALK) {
2872                                                         user->talking = 1;
2873                                                         if (confflags & CONFFLAG_MONITORTALKER)
2874                                                                 manager_event(EVENT_FLAG_CALL, "MeetmeTalking",
2875                                                                       "Channel: %s\r\n"
2876                                                                       "Uniqueid: %s\r\n"
2877                                                                       "Meetme: %s\r\n"
2878                                                                       "Usernum: %d\r\n"
2879                                                                       "Status: on\r\n",
2880                                                                       chan->name, chan->uniqueid, conf->confno, user->user_no);
2881                                                 }
2882                                                 if (user->talking && totalsilence > MEETME_DELAYDETECTENDTALK) {
2883                                                         user->talking = 0;
2884                                                         if (confflags & CONFFLAG_MONITORTALKER) {
2885                                                                 manager_event(EVENT_FLAG_CALL, "MeetmeTalking",
2886                                                                       "Channel: %s\r\n"
2887                                                                       "Uniqueid: %s\r\n"
2888                                                                       "Meetme: %s\r\n"
2889                                                                       "Usernum: %d\r\n"
2890                                                                       "Status: off\r\n",
2891                                                                       chan->name, chan->uniqueid, conf->confno, user->user_no);
2892                                                         }
2893                                                 }
2894                                         }
2895                                         if (using_pseudo) {
2896                                                 /* Absolutely do _not_ use careful_write here...
2897                                                    it is important that we read data from the channel
2898                                                    as fast as it arrives, and feed it into the conference.
2899                                                    The buffering in the pseudo channel will take care of any
2900                                                    timing differences, unless they are so drastic as to lose
2901                                                    audio frames (in which case carefully writing would only
2902                                                    have delayed the audio even further).
2903                                                 */
2904                                                 /* As it turns out, we do want to use careful write.  We just
2905                                                    don't want to block, but we do want to at least *try*
2906                                                    to write out all the samples.
2907                                                  */
2908                                                 if (user->talking) {
2909                                                         careful_write(fd, f->data.ptr, f->datalen, 0);
2910                                                 }
2911                                         }
2912                                 } else if (((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*') && (confflags & CONFFLAG_STARMENU)) || ((f->frametype == AST_FRAME_DTMF) && menu_active)) {
2913                                         if (confflags & CONFFLAG_PASS_DTMF) {
2914                                                 conf_queue_dtmf(conf, user, f);
2915                                         }
2916                                         if (ioctl(fd, DAHDI_SETCONF, &dahdic_empty)) {
2917                                                 ast_log(LOG_WARNING, "Error setting conference\n");
2918                                                 close(fd);
2919                                                 ast_frfree(f);
2920                                                 goto outrun;
2921                                         }
2922
2923                                         /* if we are entering the menu, and the user has a channel-driver
2924                                            volume adjustment, clear it
2925                                         */
2926                                         if (!menu_active && user->talk.desired && !user->talk.actual) {
2927                                                 set_talk_volume(user, 0);
2928                                         }
2929
2930                                         if (musiconhold) {
2931                                                 ast_moh_stop(chan);
2932                                         }
2933                                         if ((confflags & CONFFLAG_ADMIN)) {
2934                                                 /* Admin menu */
2935                                                 if (!menu_active) {
2936                                                         menu_active = 1;
2937                                                         /* Record this sound! */
2938                                                         if (!ast_streamfile(chan, "conf-adminmenu-162", chan->language)) {
2939                                                                 dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
2940                                                                 ast_stopstream(chan);
2941                                                         } else {
2942                                                                 dtmf = 0;
2943                                                         }
2944                                                 } else {
2945                                                         dtmf = f->subclass;
2946                                                 }
2947                                                 if (dtmf) {
2948                                                         switch(dtmf) {
2949                                                         case '1': /* Un/Mute */
2950                                                                 menu_active = 0;
2951
2952                                                                 /* for admin, change both admin and use flags */
2953                                                                 if (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) {
2954                                                                         user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
2955                                                                 } else {
2956                                                                         user->adminflags |= (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
2957                                                                 }
2958
2959                                                                 if ((confflags & CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
2960                                                                         if (!ast_streamfile(chan, "conf-muted", chan->language)) {
2961                                                                                 ast_waitstream(chan, "");
2962                                                                         }
2963                                                                 } else {
2964                                                                         if (!ast_streamfile(chan, "conf-unmuted", chan->language)) {
2965                                                                                 ast_waitstream(chan, "");
2966                                                                         }
2967                                                                 }
2968                                                                 break;
2969                                                         case '2': /* Un/Lock the Conference */
2970                                                                 menu_active = 0;
2971                                                                 if (conf->locked) {
2972                                                                         conf->locked = 0;
2973                                                                         if (!ast_streamfile(chan, "conf-unlockednow", chan->language)) {
2974                                                                                 ast_waitstream(chan, "");
2975                                                                         }
2976                                                                 } else {
2977                                                                         conf->locked = 1;
2978                                                                         if (!ast_streamfile(chan, "conf-lockednow", chan->language)) {
2979                                                                                 ast_waitstream(chan, "");
2980                                                                         }
2981                                                                 }
2982                                                                 break;
2983                                                         case '3': /* Eject last user */
2984                                                                 menu_active = 0;
2985                                                                 usr = AST_LIST_LAST(&conf->userlist);
2986                                                                 if ((usr->chan->name == chan->name) || (usr->userflags & CONFFLAG_ADMIN)) {
2987                                                                         if (!ast_streamfile(chan, "conf-errormenu", chan->language)) {
2988                                                                                 ast_waitstream(chan, "");
2989                                                                         }
2990                                                                 } else {
2991                                                                         usr->adminflags |= ADMINFLAG_KICKME;
2992                                                                 }
2993                                                                 ast_stopstream(chan);
2994                                                                 break;  
2995                                                         case '4':
2996                                                                 tweak_listen_volume(user, VOL_DOWN);
2997                                                                 break;
2998                                                         case '5':
2999                                                                 /* Extend RT conference */
3000                                                                 if (rt_schedule) {
3001                                                                         if (!rt_extend_conf(conf->confno)) {
3002                                                                                 if (!ast_streamfile(chan, "conf-extended", chan->language)) {
3003                                                                                         ast_waitstream(chan, "");
3004                                                                                 }
3005                                                                         } else {
3006                                                                                 if (!ast_streamfile(chan, "conf-nonextended", chan->language)) {
3007                                                                                         ast_waitstream(chan, "");
3008                                                                                 }
3009                                                                         }
3010                                                                         ast_stopstream(chan);
3011                                                                 }
3012                                                                 menu_active = 0;
3013                                                                 break;
3014                                                         case '6':
3015                                                                 tweak_listen_volume(user, VOL_UP);
3016                                                                 break;
3017                                                         case '7':
3018                                                                 tweak_talk_volume(user, VOL_DOWN);
3019                                                                 break;
3020                                                         case '8':
3021                                                                 menu_active = 0;
3022                                                                 break;
3023                                                         case '9':
3024                                                                 tweak_talk_volume(user, VOL_UP);
3025                                                                 break;
3026                                                         default:
3027                                                                 menu_active = 0;
3028                                                                 /* Play an error message! */
3029                                                                 if (!ast_streamfile(chan, "conf-errormenu", chan->language)) {
3030                                                                         ast_waitstream(chan, "");
3031                                                                 }
3032                                                                 break;
3033                                                         }
3034                                                 }
3035                                         } else {
3036                                                 /* User menu */
3037                                                 if (!menu_active) {
3038                                                         menu_active = 1;
3039                                                         if (!ast_streamfile(chan, "conf-usermenu-162", chan->language)) {
3040                                  &nbs