Fri Jan 18 19:57:04 2013

Asterisk developer's documentation


app_meetme.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2007, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * SLA Implementation by:
00009  * Russell Bryant <russell@digium.com>
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  * This program is free software, distributed under the terms of
00018  * the GNU General Public License Version 2. See the LICENSE file
00019  * at the top of the source tree.
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Meet me conference bridge and Shared Line Appearances
00025  *
00026  * \author Mark Spencer <markster@digium.com>
00027  * \author (SLA) Russell Bryant <russell@digium.com>
00028  * 
00029  * \ingroup applications
00030  */
00031 
00032 /*** MODULEINFO
00033    <depend>dahdi</depend>
00034    <support_level>core</support_level>
00035  ***/
00036 
00037 #include "asterisk.h"
00038 
00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 378781 $")
00040 
00041 #include <dahdi/user.h>
00042 
00043 #include "asterisk/lock.h"
00044 #include "asterisk/file.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/pbx.h"
00047 #include "asterisk/module.h"
00048 #include "asterisk/config.h"
00049 #include "asterisk/app.h"
00050 #include "asterisk/dsp.h"
00051 #include "asterisk/musiconhold.h"
00052 #include "asterisk/manager.h"
00053 #include "asterisk/cli.h"
00054 #include "asterisk/say.h"
00055 #include "asterisk/utils.h"
00056 #include "asterisk/translate.h"
00057 #include "asterisk/ulaw.h"
00058 #include "asterisk/astobj2.h"
00059 #include "asterisk/devicestate.h"
00060 #include "asterisk/dial.h"
00061 #include "asterisk/causes.h"
00062 #include "asterisk/paths.h"
00063 #include "asterisk/data.h"
00064 #include "asterisk/test.h"
00065 
00066 #include "enter.h"
00067 #include "leave.h"
00068 
00069 /*** DOCUMENTATION
00070    <application name="MeetMe" language="en_US">
00071       <synopsis>
00072          MeetMe conference bridge.
00073       </synopsis>
00074       <syntax>
00075          <parameter name="confno">
00076             <para>The conference number</para>
00077          </parameter>
00078          <parameter name="options">
00079             <optionlist>
00080                <option name="a">
00081                   <para>Set admin mode.</para>
00082                </option>
00083                <option name="A">
00084                   <para>Set marked mode.</para>
00085                </option>
00086                <option name="b">
00087                   <para>Run AGI script specified in <variable>MEETME_AGI_BACKGROUND</variable>
00088                   Default: <literal>conf-background.agi</literal>.</para>
00089                   <note><para>This does not work with non-DAHDI channels in the same
00090                   conference).</para></note>
00091                </option>
00092                <option name="c">
00093                   <para>Announce user(s) count on joining a conference.</para>
00094                </option>
00095                <option name="C">
00096                   <para>Continue in dialplan when kicked out of conference.</para>
00097                </option>
00098                <option name="d">
00099                   <para>Dynamically add conference.</para>
00100                </option>
00101                <option name="D">
00102                   <para>Dynamically add conference, prompting for a PIN.</para>
00103                </option>
00104                <option name="e">
00105                   <para>Select an empty conference.</para>
00106                </option>
00107                <option name="E">
00108                   <para>Select an empty pinless conference.</para>
00109                </option>
00110                <option name="F">
00111                   <para>Pass DTMF through the conference.</para>
00112                </option>
00113                <option name="G">
00114                   <argument name="x" required="true">
00115                      <para>The file to playback</para>
00116                   </argument>
00117                   <para>Play an intro announcement in conference.</para>
00118                </option>
00119                <option name="i">
00120                   <para>Announce user join/leave with review.</para>
00121                </option>
00122                <option name="I">
00123                   <para>Announce user join/leave without review.</para>
00124                </option>
00125                <option name="l">
00126                   <para>Set listen only mode (Listen only, no talking).</para>
00127                </option>
00128                <option name="m">
00129                   <para>Set initially muted.</para>
00130                </option>
00131                <option name="M" hasparams="optional">
00132                   <para>Enable music on hold when the conference has a single caller. Optionally,
00133                   specify a musiconhold class to use. If one is not provided, it will use the
00134                   channel's currently set music class, or <literal>default</literal>.</para>
00135                   <argument name="class" required="true" />
00136                </option>
00137                <option name="o">
00138                   <para>Set talker optimization - treats talkers who aren't speaking as
00139                   being muted, meaning (a) No encode is done on transmission and (b)
00140                   Received audio that is not registered as talking is omitted causing no
00141                   buildup in background noise.</para>
00142                </option>
00143                <option name="p" hasparams="optional">
00144                   <para>Allow user to exit the conference by pressing <literal>#</literal> (default)
00145                   or any of the defined keys. Dial plan execution will continue at the next
00146                   priority following MeetMe. The key used is set to channel variable
00147                   <variable>MEETME_EXIT_KEY</variable>.</para>
00148                   <argument name="keys" required="true" />
00149                   <note>
00150                      <para>Option <literal>s</literal> has priority for <literal>*</literal>
00151                      since it cannot change its activation code.</para>
00152                   </note>
00153                </option>
00154                <option name="P">
00155                   <para>Always prompt for the pin even if it is specified.</para>
00156                </option>
00157                <option name="q">
00158                   <para>Quiet mode (don't play enter/leave sounds).</para>
00159                </option>
00160                <option name="r">
00161                   <para>Record conference (records as <variable>MEETME_RECORDINGFILE</variable>
00162                   using format <variable>MEETME_RECORDINGFORMAT</variable>. Default filename is
00163                   <literal>meetme-conf-rec-${CONFNO}-${UNIQUEID}</literal> and the default format is
00164                   wav.</para>
00165                </option>
00166                <option name="s">
00167                   <para>Present menu (user or admin) when <literal>*</literal> is received
00168                   (send to menu).</para>
00169                </option>
00170                <option name="t">
00171                   <para>Set talk only mode. (Talk only, no listening).</para>
00172                </option>
00173                <option name="T">
00174                   <para>Set talker detection (sent to manager interface and meetme list).</para>
00175                </option>
00176                <option name="w" hasparams="optional">
00177                   <para>Wait until the marked user enters the conference.</para>
00178                   <argument name="secs" required="true" />
00179                </option>
00180                <option name="x">
00181                   <para>Leave the conference when the last marked user leaves.</para>
00182                </option>
00183                <option name="X">
00184                   <para>Allow user to exit the conference by entering a valid single digit
00185                   extension <variable>MEETME_EXIT_CONTEXT</variable> or the current context
00186                   if that variable is not defined.</para>
00187                   <note>
00188                      <para>Option <literal>s</literal> has priority for <literal>*</literal>
00189                      since it cannot change its activation code.</para>
00190                   </note>
00191                </option>
00192                <option name="1">
00193                   <para>Do not play message when first person enters</para>
00194                </option>
00195                <option name="S">
00196                   <para>Kick the user <replaceable>x</replaceable> seconds <emphasis>after</emphasis> he entered into
00197                   the conference.</para>
00198                   <argument name="x" required="true" />
00199                </option>
00200                <option name="L" argsep=":">
00201                   <para>Limit the conference to <replaceable>x</replaceable> ms. Play a warning when
00202                   <replaceable>y</replaceable> ms are left. Repeat the warning every <replaceable>z</replaceable> ms.
00203                   The following special variables can be used with this option:</para>
00204                   <variablelist>
00205                      <variable name="CONF_LIMIT_TIMEOUT_FILE">
00206                         <para>File to play when time is up.</para>
00207                      </variable>
00208                      <variable name="CONF_LIMIT_WARNING_FILE">
00209                         <para>File to play as warning if <replaceable>y</replaceable> is defined. The
00210                         default is to say the time remaining.</para>
00211                      </variable>
00212                   </variablelist>
00213                   <argument name="x" />
00214                   <argument name="y" />
00215                   <argument name="z" />
00216                </option>
00217             </optionlist>
00218          </parameter>
00219          <parameter name="pin" />
00220       </syntax>
00221       <description>
00222          <para>Enters the user into a specified MeetMe conference.  If the <replaceable>confno</replaceable>
00223          is omitted, the user will be prompted to enter one.  User can exit the conference by hangup, or
00224          if the <literal>p</literal> option is specified, by pressing <literal>#</literal>.</para>
00225          <note><para>The DAHDI kernel modules and a functional DAHDI timing source (see dahdi_test)
00226          must be present for conferencing to operate properly. In addition, the chan_dahdi channel driver
00227          must be loaded for the <literal>i</literal> and <literal>r</literal> options to operate at
00228          all.</para></note>
00229       </description>
00230       <see-also>
00231          <ref type="application">MeetMeCount</ref>
00232          <ref type="application">MeetMeAdmin</ref>
00233          <ref type="application">MeetMeChannelAdmin</ref>
00234       </see-also>
00235    </application>
00236    <application name="MeetMeCount" language="en_US">
00237       <synopsis>
00238          MeetMe participant count.
00239       </synopsis>
00240       <syntax>
00241          <parameter name="confno" required="true">
00242             <para>Conference number.</para>
00243          </parameter>
00244          <parameter name="var" />
00245       </syntax>
00246       <description>
00247          <para>Plays back the number of users in the specified MeetMe conference.
00248          If <replaceable>var</replaceable> is specified, playback will be skipped and the value
00249          will be returned in the variable. Upon application completion, MeetMeCount will hangup
00250          the channel, unless priority <literal>n+1</literal> exists, in which case priority progress will
00251          continue.</para>
00252       </description>
00253       <see-also>
00254          <ref type="application">MeetMe</ref>
00255       </see-also>
00256    </application>
00257    <application name="MeetMeAdmin" language="en_US">
00258       <synopsis>
00259          MeetMe conference administration.
00260       </synopsis>
00261       <syntax>
00262          <parameter name="confno" required="true" />
00263          <parameter name="command" required="true">
00264             <optionlist>
00265                <option name="e">
00266                   <para>Eject last user that joined.</para>
00267                </option>
00268                <option name="E">
00269                   <para>Extend conference end time, if scheduled.</para>
00270                </option>
00271                <option name="k">
00272                   <para>Kick one user out of conference.</para>
00273                </option>
00274                <option name="K">
00275                   <para>Kick all users out of conference.</para>
00276                </option>
00277                <option name="l">
00278                   <para>Unlock conference.</para>
00279                </option>
00280                <option name="L">
00281                   <para>Lock conference.</para>
00282                </option>
00283                <option name="m">
00284                   <para>Unmute one user.</para>
00285                </option>
00286                <option name="M">
00287                   <para>Mute one user.</para>
00288                </option>
00289                <option name="n">
00290                   <para>Unmute all users in the conference.</para>
00291                </option>
00292                <option name="N">
00293                   <para>Mute all non-admin users in the conference.</para>
00294                </option>
00295                <option name="r">
00296                   <para>Reset one user's volume settings.</para>
00297                </option>
00298                <option name="R">
00299                   <para>Reset all users volume settings.</para>
00300                </option>
00301                <option name="s">
00302                   <para>Lower entire conference speaking volume.</para>
00303                </option>
00304                <option name="S">
00305                   <para>Raise entire conference speaking volume.</para>
00306                </option>
00307                <option name="t">
00308                   <para>Lower one user's talk volume.</para>
00309                </option>
00310                <option name="T">
00311                   <para>Raise one user's talk volume.</para>
00312                </option>
00313                <option name="u">
00314                   <para>Lower one user's listen volume.</para>
00315                </option>
00316                <option name="U">
00317                   <para>Raise one user's listen volume.</para>
00318                </option>
00319                <option name="v">
00320                   <para>Lower entire conference listening volume.</para>
00321                </option>
00322                <option name="V">
00323                   <para>Raise entire conference listening volume.</para>
00324                </option>
00325             </optionlist>
00326          </parameter>
00327          <parameter name="user" />
00328       </syntax>
00329       <description>
00330          <para>Run admin <replaceable>command</replaceable> for conference <replaceable>confno</replaceable>.</para>
00331          <para>Will additionally set the variable <variable>MEETMEADMINSTATUS</variable> with one of
00332          the following values:</para>
00333          <variablelist>
00334             <variable name="MEETMEADMINSTATUS">
00335                <value name="NOPARSE">
00336                   Invalid arguments.
00337                </value>
00338                <value name="NOTFOUND">
00339                   User specified was not found.
00340                </value>
00341                <value name="FAILED">
00342                   Another failure occurred.
00343                </value>
00344                <value name="OK">
00345                   The operation was completed successfully.
00346                </value>
00347             </variable>
00348          </variablelist>
00349       </description>
00350       <see-also>
00351          <ref type="application">MeetMe</ref>
00352       </see-also>
00353    </application>
00354    <application name="MeetMeChannelAdmin" language="en_US">
00355       <synopsis>
00356          MeetMe conference Administration (channel specific).
00357       </synopsis>
00358       <syntax>
00359          <parameter name="channel" required="true" />
00360          <parameter name="command" required="true">
00361             <optionlist>
00362                <option name="k">
00363                   <para>Kick the specified user out of the conference he is in.</para>
00364                </option>
00365                <option name="m">
00366                   <para>Unmute the specified user.</para>
00367                </option>
00368                <option name="M">
00369                   <para>Mute the specified user.</para>
00370                </option>
00371             </optionlist>
00372          </parameter>
00373       </syntax>
00374       <description>
00375          <para>Run admin <replaceable>command</replaceable> for a specific
00376          <replaceable>channel</replaceable> in any conference.</para>
00377       </description>
00378    </application>
00379    <application name="SLAStation" language="en_US">
00380       <synopsis>
00381          Shared Line Appearance Station.
00382       </synopsis>
00383       <syntax>
00384          <parameter name="station" required="true">
00385             <para>Station name</para>
00386          </parameter>
00387       </syntax>
00388       <description>
00389          <para>This application should be executed by an SLA station. The argument depends
00390          on how the call was initiated. If the phone was just taken off hook, then the argument
00391          <replaceable>station</replaceable> should be just the station name. If the call was
00392          initiated by pressing a line key, then the station name should be preceded by an underscore
00393          and the trunk name associated with that line button.</para>
00394          <para>For example: <literal>station1_line1</literal></para>
00395          <para>On exit, this application will set the variable <variable>SLASTATION_STATUS</variable> to
00396          one of the following values:</para>
00397          <variablelist>
00398             <variable name="SLASTATION_STATUS">
00399                <value name="FAILURE" />
00400                <value name="CONGESTION" />
00401                <value name="SUCCESS" />
00402             </variable>
00403          </variablelist>
00404       </description>
00405    </application>
00406    <application name="SLATrunk" language="en_US">
00407       <synopsis>
00408          Shared Line Appearance Trunk.
00409       </synopsis>
00410       <syntax>
00411          <parameter name="trunk" required="true">
00412             <para>Trunk name</para>
00413          </parameter>
00414          <parameter name="options">
00415             <optionlist>
00416                <option name="M" hasparams="optional">
00417                   <para>Play back the specified MOH <replaceable>class</replaceable>
00418                   instead of ringing</para>
00419                   <argument name="class" required="true" />
00420                </option>
00421             </optionlist>
00422          </parameter>
00423       </syntax>
00424       <description>
00425          <para>This application should be executed by an SLA trunk on an inbound call. The channel calling
00426          this application should correspond to the SLA trunk with the name <replaceable>trunk</replaceable>
00427          that is being passed as an argument.</para>
00428          <para>On exit, this application will set the variable <variable>SLATRUNK_STATUS</variable> to
00429          one of the following values:</para>
00430          <variablelist>
00431             <variable name="SLATRUNK_STATUS">
00432                <value name="FAILURE" />
00433                <value name="SUCCESS" />
00434                <value name="UNANSWERED" />
00435                <value name="RINGTIMEOUT" />
00436             </variable>
00437          </variablelist>
00438       </description>
00439    </application>
00440    <function name="MEETME_INFO" language="en_US">
00441       <synopsis>
00442          Query a given conference of various properties.
00443       </synopsis>
00444       <syntax>
00445          <parameter name="keyword" required="true">
00446             <para>Options:</para>
00447             <enumlist>
00448                <enum name="lock">
00449                   <para>Boolean of whether the corresponding conference is locked.</para>
00450                </enum>
00451                <enum name="parties">
00452                   <para>Number of parties in a given conference</para>
00453                </enum>
00454                <enum name="activity">
00455                   <para>Duration of conference in seconds.</para>
00456                </enum>
00457                <enum name="dynamic">
00458                   <para>Boolean of whether the corresponding conference is dynamic.</para>
00459                </enum>
00460             </enumlist>
00461          </parameter>
00462          <parameter name="confno" required="true">
00463             <para>Conference number to retrieve information from.</para>
00464          </parameter>
00465       </syntax>
00466       <description />
00467       <see-also>
00468          <ref type="application">MeetMe</ref>
00469          <ref type="application">MeetMeCount</ref>
00470          <ref type="application">MeetMeAdmin</ref>
00471          <ref type="application">MeetMeChannelAdmin</ref>
00472       </see-also>
00473    </function>
00474    <manager name="MeetmeMute" language="en_US">
00475       <synopsis>
00476          Mute a Meetme user.
00477       </synopsis>
00478       <syntax>
00479          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00480          <parameter name="Meetme" required="true" />
00481          <parameter name="Usernum" required="true" />
00482       </syntax>
00483       <description>
00484       </description>
00485    </manager>
00486    <manager name="MeetmeUnmute" language="en_US">
00487       <synopsis>
00488          Unmute a Meetme user.
00489       </synopsis>
00490       <syntax>
00491          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00492          <parameter name="Meetme" required="true" />
00493          <parameter name="Usernum" required="true" />
00494       </syntax>
00495       <description>
00496       </description>
00497    </manager>
00498    <manager name="MeetmeList" language="en_US">
00499       <synopsis>
00500          List participants in a conference.
00501       </synopsis>
00502       <syntax>
00503          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00504          <parameter name="Conference" required="false">
00505             <para>Conference number.</para>
00506          </parameter>
00507       </syntax>
00508       <description>
00509          <para>Lists all users in a particular MeetMe conference.
00510          MeetmeList will follow as separate events, followed by a final event called
00511          MeetmeListComplete.</para>
00512       </description>
00513    </manager>
00514  ***/
00515 
00516 #define CONFIG_FILE_NAME   "meetme.conf"
00517 #define SLA_CONFIG_FILE    "sla.conf"
00518 #define STR_CONCISE        "concise"
00519 
00520 /*! each buffer is 20ms, so this is 640ms total */
00521 #define DEFAULT_AUDIO_BUFFERS  32
00522 
00523 /*! String format for scheduled conferences */
00524 #define DATE_FORMAT "%Y-%m-%d %H:%M:%S"
00525 
00526 enum {
00527    ADMINFLAG_MUTED =     (1 << 1), /*!< User is muted */
00528    ADMINFLAG_SELFMUTED = (1 << 2), /*!< User muted self */
00529    ADMINFLAG_KICKME =    (1 << 3),  /*!< User has been kicked */
00530    /*! User has requested to speak */
00531    ADMINFLAG_T_REQUEST = (1 << 4),
00532 };
00533 
00534 #define MEETME_DELAYDETECTTALK     300
00535 #define MEETME_DELAYDETECTENDTALK  1000
00536 
00537 #define AST_FRAME_BITS  32
00538 
00539 enum volume_action {
00540    VOL_UP,
00541    VOL_DOWN
00542 };
00543 
00544 enum entrance_sound {
00545    ENTER,
00546    LEAVE
00547 };
00548 
00549 enum recording_state {
00550    MEETME_RECORD_OFF,
00551    MEETME_RECORD_STARTED,
00552    MEETME_RECORD_ACTIVE,
00553    MEETME_RECORD_TERMINATE
00554 };
00555 
00556 #define CONF_SIZE  320
00557 
00558 enum {
00559    /*! user has admin access on the conference */
00560    CONFFLAG_ADMIN = (1 << 0),
00561    /*! If set the user can only receive audio from the conference */
00562    CONFFLAG_MONITOR = (1 << 1),
00563    /*! If set asterisk will exit conference when key defined in p() option is pressed */
00564    CONFFLAG_KEYEXIT = (1 << 2),
00565    /*! If set asterisk will provide a menu to the user when '*' is pressed */
00566    CONFFLAG_STARMENU = (1 << 3),
00567    /*! If set the use can only send audio to the conference */
00568    CONFFLAG_TALKER = (1 << 4),
00569    /*! If set there will be no enter or leave sounds */
00570    CONFFLAG_QUIET = (1 << 5),
00571    /*! If set, when user joins the conference, they will be told the number 
00572     *  of users that are already in */
00573    CONFFLAG_ANNOUNCEUSERCOUNT = (1 << 6),
00574    /*! Set to run AGI Script in Background */
00575    CONFFLAG_AGI = (1 << 7),
00576    /*! Set to have music on hold when user is alone in conference */
00577    CONFFLAG_MOH = (1 << 8),
00578    /*! If set, the channel will leave the conference if all marked users leave */
00579    CONFFLAG_MARKEDEXIT = (1 << 9),
00580    /*! If set, the MeetMe will wait until a marked user enters */
00581    CONFFLAG_WAITMARKED = (1 << 10),
00582    /*! If set, the MeetMe will exit to the specified context */
00583    CONFFLAG_EXIT_CONTEXT = (1 << 11),
00584    /*! If set, the user will be marked */
00585    CONFFLAG_MARKEDUSER = (1 << 12),
00586    /*! If set, user will be ask record name on entry of conference */
00587    CONFFLAG_INTROUSER = (1 << 13),
00588    /*! If set, the MeetMe will be recorded */
00589    CONFFLAG_RECORDCONF = (1<< 14),
00590    /*! If set, the user will be monitored if the user is talking or not */
00591    CONFFLAG_MONITORTALKER = (1 << 15),
00592    CONFFLAG_DYNAMIC = (1 << 16),
00593    CONFFLAG_DYNAMICPIN = (1 << 17),
00594    CONFFLAG_EMPTY = (1 << 18),
00595    CONFFLAG_EMPTYNOPIN = (1 << 19),
00596    CONFFLAG_ALWAYSPROMPT = (1 << 20),
00597    /*! If set, treat talking users as muted users */
00598    CONFFLAG_OPTIMIZETALKER = (1 << 21),
00599    /*! If set, won't speak the extra prompt when the first person 
00600     *  enters the conference */
00601    CONFFLAG_NOONLYPERSON = (1 << 22),
00602    /*! If set, user will be asked to record name on entry of conference 
00603     *  without review */
00604    CONFFLAG_INTROUSERNOREVIEW = (1 << 23),
00605    /*! If set, the user will be initially self-muted */
00606    CONFFLAG_STARTMUTED = (1 << 24),
00607    /*! Pass DTMF through the conference */
00608    CONFFLAG_PASS_DTMF = (1 << 25),
00609    CONFFLAG_SLA_STATION = (1 << 26),
00610    CONFFLAG_SLA_TRUNK = (1 << 27),
00611    /*! If set, the user should continue in the dialplan if kicked out */
00612    CONFFLAG_KICK_CONTINUE = (1 << 28),
00613    CONFFLAG_DURATION_STOP = (1 << 29),
00614    CONFFLAG_DURATION_LIMIT = (1 << 30),
00615 };
00616 
00617 /*! Do not write any audio to this channel until the state is up. */
00618 #define CONFFLAG_NO_AUDIO_UNTIL_UP  (1ULL << 31)
00619 /*! If set play an intro announcement at start of conference */
00620 #define CONFFLAG_INTROMSG           (1ULL << 32)
00621 
00622 enum {
00623    OPT_ARG_WAITMARKED = 0,
00624    OPT_ARG_EXITKEYS   = 1,
00625    OPT_ARG_DURATION_STOP = 2,
00626    OPT_ARG_DURATION_LIMIT = 3,
00627    OPT_ARG_MOH_CLASS = 4,
00628    OPT_ARG_INTROMSG = 5,
00629    OPT_ARG_ARRAY_SIZE = 6,
00630 };
00631 
00632 AST_APP_OPTIONS(meetme_opts, BEGIN_OPTIONS
00633    AST_APP_OPTION('A', CONFFLAG_MARKEDUSER ),
00634    AST_APP_OPTION('a', CONFFLAG_ADMIN ),
00635    AST_APP_OPTION('b', CONFFLAG_AGI ),
00636    AST_APP_OPTION('c', CONFFLAG_ANNOUNCEUSERCOUNT ),
00637    AST_APP_OPTION('C', CONFFLAG_KICK_CONTINUE),
00638    AST_APP_OPTION('D', CONFFLAG_DYNAMICPIN ),
00639    AST_APP_OPTION('d', CONFFLAG_DYNAMIC ),
00640    AST_APP_OPTION('E', CONFFLAG_EMPTYNOPIN ),
00641    AST_APP_OPTION('e', CONFFLAG_EMPTY ),
00642    AST_APP_OPTION('F', CONFFLAG_PASS_DTMF ),
00643    AST_APP_OPTION_ARG('G', CONFFLAG_INTROMSG, OPT_ARG_INTROMSG ),
00644    AST_APP_OPTION('i', CONFFLAG_INTROUSER ),
00645    AST_APP_OPTION('I', CONFFLAG_INTROUSERNOREVIEW ),
00646    AST_APP_OPTION_ARG('M', CONFFLAG_MOH, OPT_ARG_MOH_CLASS ),
00647    AST_APP_OPTION('m', CONFFLAG_STARTMUTED ),
00648    AST_APP_OPTION('o', CONFFLAG_OPTIMIZETALKER ),
00649    AST_APP_OPTION('P', CONFFLAG_ALWAYSPROMPT ),
00650    AST_APP_OPTION_ARG('p', CONFFLAG_KEYEXIT, OPT_ARG_EXITKEYS ),
00651    AST_APP_OPTION('q', CONFFLAG_QUIET ),
00652    AST_APP_OPTION('r', CONFFLAG_RECORDCONF ),
00653    AST_APP_OPTION('s', CONFFLAG_STARMENU ),
00654    AST_APP_OPTION('T', CONFFLAG_MONITORTALKER ),
00655    AST_APP_OPTION('l', CONFFLAG_MONITOR ),
00656    AST_APP_OPTION('t', CONFFLAG_TALKER ),
00657    AST_APP_OPTION_ARG('w', CONFFLAG_WAITMARKED, OPT_ARG_WAITMARKED ),
00658    AST_APP_OPTION('X', CONFFLAG_EXIT_CONTEXT ),
00659    AST_APP_OPTION('x', CONFFLAG_MARKEDEXIT ),
00660    AST_APP_OPTION('1', CONFFLAG_NOONLYPERSON ),
00661    AST_APP_OPTION_ARG('S', CONFFLAG_DURATION_STOP, OPT_ARG_DURATION_STOP),
00662    AST_APP_OPTION_ARG('L', CONFFLAG_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
00663 END_OPTIONS );
00664 
00665 static const char * const app = "MeetMe";
00666 static const char * const app2 = "MeetMeCount";
00667 static const char * const app3 = "MeetMeAdmin";
00668 static const char * const app4 = "MeetMeChannelAdmin";
00669 static const char * const slastation_app = "SLAStation";
00670 static const char * const slatrunk_app = "SLATrunk";
00671 
00672 /* Lookup RealTime conferences based on confno and current time */
00673 static int rt_schedule;
00674 static int fuzzystart;
00675 static int earlyalert;
00676 static int endalert;
00677 static int extendby;
00678 
00679 /*! Log participant count to the RealTime backend */
00680 static int rt_log_members;
00681 
00682 #define MAX_CONFNUM 80
00683 #define MAX_PIN     80
00684 #define OPTIONS_LEN 100
00685 
00686 /* Enough space for "<conference #>,<pin>,<admin pin>" followed by a 0 byte. */
00687 #define MAX_SETTINGS (MAX_CONFNUM + MAX_PIN + MAX_PIN + 3)
00688 
00689 enum announcetypes {
00690    CONF_HASJOIN,
00691    CONF_HASLEFT
00692 };
00693 
00694 struct announce_listitem {
00695    AST_LIST_ENTRY(announce_listitem) entry;
00696    char namerecloc[PATH_MAX];          /*!< Name Recorded file Location */
00697    char language[MAX_LANGUAGE];
00698    struct ast_channel *confchan;
00699    int confusers;
00700    enum announcetypes announcetype;
00701 };
00702 
00703 /*! \brief The MeetMe Conference object */
00704 struct ast_conference {
00705    ast_mutex_t playlock;                   /*!< Conference specific lock (players) */
00706    ast_mutex_t listenlock;                 /*!< Conference specific lock (listeners) */
00707    char confno[MAX_CONFNUM];               /*!< Conference */
00708    struct ast_channel *chan;               /*!< Announcements channel */
00709    struct ast_channel *lchan;              /*!< Listen/Record channel */
00710    int fd;                                 /*!< Announcements fd */
00711    int dahdiconf;                            /*!< DAHDI Conf # */
00712    int users;                              /*!< Number of active users */
00713    int markedusers;                        /*!< Number of marked users */
00714    int maxusers;                           /*!< Participant limit if scheduled */
00715    int endalert;                           /*!< When to play conf ending message */
00716    time_t start;                           /*!< Start time (s) */
00717    int refcount;                           /*!< reference count of usage */
00718    enum recording_state recording:2;       /*!< recording status */
00719    unsigned int isdynamic:1;               /*!< Created on the fly? */
00720    unsigned int locked:1;                  /*!< Is the conference locked? */
00721    unsigned int gmuted:1;                  /*!< Is the conference globally muted? (all non-admins) */
00722    pthread_t recordthread;                 /*!< thread for recording */
00723    ast_mutex_t recordthreadlock;           /*!< control threads trying to start recordthread */
00724    pthread_attr_t attr;                    /*!< thread attribute */
00725    char *recordingfilename;                /*!< Filename to record the Conference into */
00726    char *recordingformat;                  /*!< Format to record the Conference in */
00727    char pin[MAX_PIN];                      /*!< If protected by a PIN */
00728    char pinadmin[MAX_PIN];                 /*!< If protected by a admin PIN */
00729    char uniqueid[32];
00730    long endtime;                           /*!< When to end the conf if scheduled */
00731    const char *useropts;                   /*!< RealTime user flags */
00732    const char *adminopts;                  /*!< RealTime moderator flags */
00733    const char *bookid;                     /*!< RealTime conference id */
00734    struct ast_frame *transframe[32];
00735    struct ast_frame *origframe;
00736    struct ast_trans_pvt *transpath[32];
00737    struct ao2_container *usercontainer;
00738    AST_LIST_ENTRY(ast_conference) list;
00739    /* announce_thread related data */
00740    pthread_t announcethread;
00741    ast_mutex_t announcethreadlock;
00742    unsigned int announcethread_stop:1;
00743    ast_cond_t announcelist_addition;
00744    AST_LIST_HEAD_NOLOCK(, announce_listitem) announcelist;
00745    ast_mutex_t announcelistlock;
00746 };
00747 
00748 static AST_LIST_HEAD_STATIC(confs, ast_conference);
00749 
00750 static unsigned int conf_map[1024] = {0, };
00751 
00752 struct volume {
00753    int desired;                            /*!< Desired volume adjustment */
00754    int actual;                             /*!< Actual volume adjustment (for channels that can't adjust) */
00755 };
00756 
00757 /*! \brief The MeetMe User object */
00758 struct ast_conf_user {
00759    int user_no;                            /*!< User Number */
00760    struct ast_flags64 userflags;           /*!< Flags as set in the conference */
00761    int adminflags;                         /*!< Flags set by the Admin */
00762    struct ast_channel *chan;               /*!< Connected channel */
00763    int talking;                            /*!< Is user talking */
00764    int dahdichannel;                       /*!< Is a DAHDI channel */
00765    char usrvalue[50];                      /*!< Custom User Value */
00766    char namerecloc[PATH_MAX];    /*!< Name Recorded file Location */
00767    time_t jointime;                        /*!< Time the user joined the conference */
00768    time_t kicktime;                        /*!< Time the user will be kicked from the conference */
00769    struct timeval start_time;              /*!< Time the user entered into the conference */
00770    long timelimit;                         /*!< Time limit for the user to be in the conference L(x:y:z) */
00771    long play_warning;                      /*!< Play a warning when 'y' ms are left */
00772    long warning_freq;                      /*!< Repeat the warning every 'z' ms */
00773    const char *warning_sound;              /*!< File to play as warning if 'y' is defined */
00774    const char *end_sound;                  /*!< File to play when time is up. */
00775    struct volume talk;
00776    struct volume listen;
00777    AST_LIST_ENTRY(ast_conf_user) list;
00778 };
00779 
00780 enum sla_which_trunk_refs {
00781    ALL_TRUNK_REFS,
00782    INACTIVE_TRUNK_REFS,
00783 };
00784 
00785 enum sla_trunk_state {
00786    SLA_TRUNK_STATE_IDLE,
00787    SLA_TRUNK_STATE_RINGING,
00788    SLA_TRUNK_STATE_UP,
00789    SLA_TRUNK_STATE_ONHOLD,
00790    SLA_TRUNK_STATE_ONHOLD_BYME,
00791 };
00792 
00793 enum sla_hold_access {
00794    /*! This means that any station can put it on hold, and any station
00795     * can retrieve the call from hold. */
00796    SLA_HOLD_OPEN,
00797    /*! This means that only the station that put the call on hold may
00798     * retrieve it from hold. */
00799    SLA_HOLD_PRIVATE,
00800 };
00801 
00802 struct sla_trunk_ref;
00803 
00804 struct sla_station {
00805    AST_RWLIST_ENTRY(sla_station) entry;
00806    AST_DECLARE_STRING_FIELDS(
00807       AST_STRING_FIELD(name); 
00808       AST_STRING_FIELD(device);  
00809       AST_STRING_FIELD(autocontext);   
00810    );
00811    AST_LIST_HEAD_NOLOCK(, sla_trunk_ref) trunks;
00812    struct ast_dial *dial;
00813    /*! Ring timeout for this station, for any trunk.  If a ring timeout
00814     *  is set for a specific trunk on this station, that will take
00815     *  priority over this value. */
00816    unsigned int ring_timeout;
00817    /*! Ring delay for this station, for any trunk.  If a ring delay
00818     *  is set for a specific trunk on this station, that will take
00819     *  priority over this value. */
00820    unsigned int ring_delay;
00821    /*! This option uses the values in the sla_hold_access enum and sets the
00822     * access control type for hold on this station. */
00823    unsigned int hold_access:1;
00824    /*! Use count for inside sla_station_exec */
00825    unsigned int ref_count;
00826 };
00827 
00828 struct sla_station_ref {
00829    AST_LIST_ENTRY(sla_station_ref) entry;
00830    struct sla_station *station;
00831 };
00832 
00833 struct sla_trunk {
00834    AST_RWLIST_ENTRY(sla_trunk) entry;
00835    AST_DECLARE_STRING_FIELDS(
00836       AST_STRING_FIELD(name);
00837       AST_STRING_FIELD(device);
00838       AST_STRING_FIELD(autocontext);   
00839    );
00840    AST_LIST_HEAD_NOLOCK(, sla_station_ref) stations;
00841    /*! Number of stations that use this trunk */
00842    unsigned int num_stations;
00843    /*! Number of stations currently on a call with this trunk */
00844    unsigned int active_stations;
00845    /*! Number of stations that have this trunk on hold. */
00846    unsigned int hold_stations;
00847    struct ast_channel *chan;
00848    unsigned int ring_timeout;
00849    /*! If set to 1, no station will be able to join an active call with
00850     *  this trunk. */
00851    unsigned int barge_disabled:1;
00852    /*! This option uses the values in the sla_hold_access enum and sets the
00853     * access control type for hold on this trunk. */
00854    unsigned int hold_access:1;
00855    /*! Whether this trunk is currently on hold, meaning that once a station
00856     *  connects to it, the trunk channel needs to have UNHOLD indicated to it. */
00857    unsigned int on_hold:1;
00858    /*! Use count for inside sla_trunk_exec */
00859    unsigned int ref_count;
00860 };
00861 
00862 struct sla_trunk_ref {
00863    AST_LIST_ENTRY(sla_trunk_ref) entry;
00864    struct sla_trunk *trunk;
00865    enum sla_trunk_state state;
00866    struct ast_channel *chan;
00867    /*! Ring timeout to use when this trunk is ringing on this specific
00868     *  station.  This takes higher priority than a ring timeout set at
00869     *  the station level. */
00870    unsigned int ring_timeout;
00871    /*! Ring delay to use when this trunk is ringing on this specific
00872     *  station.  This takes higher priority than a ring delay set at
00873     *  the station level. */
00874    unsigned int ring_delay;
00875 };
00876 
00877 static AST_RWLIST_HEAD_STATIC(sla_stations, sla_station);
00878 static AST_RWLIST_HEAD_STATIC(sla_trunks, sla_trunk);
00879 
00880 static const char sla_registrar[] = "SLA";
00881 
00882 /*! \brief Event types that can be queued up for the SLA thread */
00883 enum sla_event_type {
00884    /*! A station has put the call on hold */
00885    SLA_EVENT_HOLD,
00886    /*! The state of a dial has changed */
00887    SLA_EVENT_DIAL_STATE,
00888    /*! The state of a ringing trunk has changed */
00889    SLA_EVENT_RINGING_TRUNK,
00890    /*! A reload of configuration has been requested */
00891    SLA_EVENT_RELOAD,
00892    /*! Poke the SLA thread so it can check if it can perform a reload */
00893    SLA_EVENT_CHECK_RELOAD,
00894 };
00895 
00896 struct sla_event {
00897    enum sla_event_type type;
00898    struct sla_station *station;
00899    struct sla_trunk_ref *trunk_ref;
00900    AST_LIST_ENTRY(sla_event) entry;
00901 };
00902 
00903 /*! \brief A station that failed to be dialed 
00904  * \note Only used by the SLA thread. */
00905 struct sla_failed_station {
00906    struct sla_station *station;
00907    struct timeval last_try;
00908    AST_LIST_ENTRY(sla_failed_station) entry;
00909 };
00910 
00911 /*! \brief A trunk that is ringing */
00912 struct sla_ringing_trunk {
00913    struct sla_trunk *trunk;
00914    /*! The time that this trunk started ringing */
00915    struct timeval ring_begin;
00916    AST_LIST_HEAD_NOLOCK(, sla_station_ref) timed_out_stations;
00917    AST_LIST_ENTRY(sla_ringing_trunk) entry;
00918 };
00919 
00920 enum sla_station_hangup {
00921    SLA_STATION_HANGUP_NORMAL,
00922    SLA_STATION_HANGUP_TIMEOUT,
00923 };
00924 
00925 /*! \brief A station that is ringing */
00926 struct sla_ringing_station {
00927    struct sla_station *station;
00928    /*! The time that this station started ringing */
00929    struct timeval ring_begin;
00930    AST_LIST_ENTRY(sla_ringing_station) entry;
00931 };
00932 
00933 /*!
00934  * \brief A structure for data used by the sla thread
00935  */
00936 static struct {
00937    /*! The SLA thread ID */
00938    pthread_t thread;
00939    ast_cond_t cond;
00940    ast_mutex_t lock;
00941    AST_LIST_HEAD_NOLOCK(, sla_ringing_trunk) ringing_trunks;
00942    AST_LIST_HEAD_NOLOCK(, sla_ringing_station) ringing_stations;
00943    AST_LIST_HEAD_NOLOCK(, sla_failed_station) failed_stations;
00944    AST_LIST_HEAD_NOLOCK(, sla_event) event_q;
00945    unsigned int stop:1;
00946    /*! Attempt to handle CallerID, even though it is known not to work
00947     *  properly in some situations. */
00948    unsigned int attempt_callerid:1;
00949    /*! A reload has been requested */
00950    unsigned int reload:1;
00951 } sla = {
00952    .thread = AST_PTHREADT_NULL,
00953 };
00954 
00955 /*! \brief The number of audio buffers to be allocated on pseudo channels
00956  *  when in a conference */
00957 static int audio_buffers;
00958 
00959 /*! \brief Map 'volume' levels from -5 through +5 into decibel (dB) 
00960  *    settings for channel drivers.
00961  *
00962  *  \note these are not a straight linear-to-dB
00963  *  conversion... the numbers have been modified
00964  *  to give the user a better level of adjustability.
00965  */
00966 static const char gain_map[] = {
00967    -15,
00968    -13,
00969    -10,
00970    -6,
00971    0,
00972    0,
00973    0,
00974    6,
00975    10,
00976    13,
00977    15,
00978 };
00979 
00980 
00981 static int admin_exec(struct ast_channel *chan, const char *data);
00982 static void *recordthread(void *args);
00983 
00984 static const char *istalking(int x)
00985 {
00986    if (x > 0)
00987       return "(talking)";
00988    else if (x < 0)
00989       return "(unmonitored)";
00990    else 
00991       return "(not talking)";
00992 }
00993 
00994 static int careful_write(int fd, unsigned char *data, int len, int block)
00995 {
00996    int res;
00997    int x;
00998 
00999    while (len) {
01000       if (block) {
01001          x = DAHDI_IOMUX_WRITE | DAHDI_IOMUX_SIGEVENT;
01002          res = ioctl(fd, DAHDI_IOMUX, &x);
01003       } else
01004          res = 0;
01005       if (res >= 0)
01006          res = write(fd, data, len);
01007       if (res < 1) {
01008          if (errno != EAGAIN) {
01009             ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
01010             return -1;
01011          } else
01012             return 0;
01013       }
01014       len -= res;
01015       data += res;
01016    }
01017 
01018    return 0;
01019 }
01020 
01021 static int set_talk_volume(struct ast_conf_user *user, int volume)
01022 {
01023    char gain_adjust;
01024 
01025    /* attempt to make the adjustment in the channel driver;
01026       if successful, don't adjust in the frame reading routine
01027    */
01028    gain_adjust = gain_map[volume + 5];
01029 
01030    return ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
01031 }
01032 
01033 static int set_listen_volume(struct ast_conf_user *user, int volume)
01034 {
01035    char gain_adjust;
01036 
01037    /* attempt to make the adjustment in the channel driver;
01038       if successful, don't adjust in the frame reading routine
01039    */
01040    gain_adjust = gain_map[volume + 5];
01041 
01042    return ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
01043 }
01044 
01045 static void tweak_volume(struct volume *vol, enum volume_action action)
01046 {
01047    switch (action) {
01048    case VOL_UP:
01049       switch (vol->desired) { 
01050       case 5:
01051          break;
01052       case 0:
01053          vol->desired = 2;
01054          break;
01055       case -2:
01056          vol->desired = 0;
01057          break;
01058       default:
01059          vol->desired++;
01060          break;
01061       }
01062       break;
01063    case VOL_DOWN:
01064       switch (vol->desired) {
01065       case -5:
01066          break;
01067       case 2:
01068          vol->desired = 0;
01069          break;
01070       case 0:
01071          vol->desired = -2;
01072          break;
01073       default:
01074          vol->desired--;
01075          break;
01076       }
01077    }
01078 }
01079 
01080 static void tweak_talk_volume(struct ast_conf_user *user, enum volume_action action)
01081 {
01082    tweak_volume(&user->talk, action);
01083    /* attempt to make the adjustment in the channel driver;
01084       if successful, don't adjust in the frame reading routine
01085    */
01086    if (!set_talk_volume(user, user->talk.desired))
01087       user->talk.actual = 0;
01088    else
01089       user->talk.actual = user->talk.desired;
01090 }
01091 
01092 static void tweak_listen_volume(struct ast_conf_user *user, enum volume_action action)
01093 {
01094    tweak_volume(&user->listen, action);
01095    /* attempt to make the adjustment in the channel driver;
01096       if successful, don't adjust in the frame reading routine
01097    */
01098    if (!set_listen_volume(user, user->listen.desired))
01099       user->listen.actual = 0;
01100    else
01101       user->listen.actual = user->listen.desired;
01102 }
01103 
01104 static void reset_volumes(struct ast_conf_user *user)
01105 {
01106    signed char zero_volume = 0;
01107 
01108    ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
01109    ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &zero_volume, sizeof(zero_volume), 0);
01110 }
01111 
01112 static void conf_play(struct ast_channel *chan, struct ast_conference *conf, enum entrance_sound sound)
01113 {
01114    unsigned char *data;
01115    int len;
01116    int res = -1;
01117 
01118    ast_test_suite_event_notify("CONFPLAY", "Channel: %s\r\n"
01119                            "Conference: %s\r\n"
01120                            "Marked: %d",
01121                            chan->name,
01122                            conf->confno,
01123                            conf->markedusers);
01124 
01125    if (!ast_check_hangup(chan))
01126       res = ast_autoservice_start(chan);
01127 
01128    AST_LIST_LOCK(&confs);
01129 
01130    switch(sound) {
01131    case ENTER:
01132       data = enter;
01133       len = sizeof(enter);
01134       break;
01135    case LEAVE:
01136       data = leave;
01137       len = sizeof(leave);
01138       break;
01139    default:
01140       data = NULL;
01141       len = 0;
01142    }
01143    if (data) {
01144       careful_write(conf->fd, data, len, 1);
01145    }
01146 
01147    AST_LIST_UNLOCK(&confs);
01148 
01149    if (!res) 
01150       ast_autoservice_stop(chan);
01151 }
01152 
01153 static int user_no_cmp(void *obj, void *arg, int flags)
01154 {
01155    struct ast_conf_user *user = obj;
01156    int *user_no = arg;
01157 
01158    if (user->user_no == *user_no) {
01159       return (CMP_MATCH | CMP_STOP);
01160    }
01161 
01162    return 0;
01163 }
01164 
01165 static int user_max_cmp(void *obj, void *arg, int flags)
01166 {
01167    struct ast_conf_user *user = obj;
01168    int *max_no = arg;
01169 
01170    if (user->user_no > *max_no) {
01171       *max_no = user->user_no;
01172    }
01173 
01174    return 0;
01175 }
01176 
01177 /*!
01178  * \brief Find or create a conference
01179  *
01180  * \param confno The conference name/number
01181  * \param pin The regular user pin
01182  * \param pinadmin The admin pin
01183  * \param make Make the conf if it doesn't exist
01184  * \param dynamic Mark the newly created conference as dynamic
01185  * \param refcount How many references to mark on the conference
01186  * \param chan The asterisk channel
01187  *
01188  * \return A pointer to the conference struct, or NULL if it wasn't found and
01189  *         make or dynamic were not set.
01190  */
01191 static struct ast_conference *build_conf(const char *confno, const char *pin,
01192    const char *pinadmin, int make, int dynamic, int refcount,
01193    const struct ast_channel *chan, struct ast_test *test)
01194 {
01195    struct ast_conference *cnf;
01196    struct dahdi_confinfo dahdic = { 0, };
01197    int confno_int = 0;
01198 
01199    AST_LIST_LOCK(&confs);
01200 
01201    AST_LIST_TRAVERSE(&confs, cnf, list) {
01202       if (!strcmp(confno, cnf->confno)) 
01203          break;
01204    }
01205 
01206    if (cnf || (!make && !dynamic))
01207       goto cnfout;
01208 
01209    /* Make a new one */
01210    if (!(cnf = ast_calloc(1, sizeof(*cnf))) ||
01211       !(cnf->usercontainer = ao2_container_alloc(1, NULL, user_no_cmp))) {
01212       goto cnfout;
01213    }
01214 
01215    ast_mutex_init(&cnf->playlock);
01216    ast_mutex_init(&cnf->listenlock);
01217    cnf->recordthread = AST_PTHREADT_NULL;
01218    ast_mutex_init(&cnf->recordthreadlock);
01219    cnf->announcethread = AST_PTHREADT_NULL;
01220    ast_mutex_init(&cnf->announcethreadlock);
01221    ast_copy_string(cnf->confno, confno, sizeof(cnf->confno));
01222    ast_copy_string(cnf->pin, pin, sizeof(cnf->pin));
01223    ast_copy_string(cnf->pinadmin, pinadmin, sizeof(cnf->pinadmin));
01224    ast_copy_string(cnf->uniqueid, chan->uniqueid, sizeof(cnf->uniqueid));
01225 
01226    /* Setup a new dahdi conference */
01227    dahdic.confno = -1;
01228    dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
01229    cnf->fd = open("/dev/dahdi/pseudo", O_RDWR);
01230    if (cnf->fd < 0 || ioctl(cnf->fd, DAHDI_SETCONF, &dahdic)) {
01231       if (test) {
01232          /* if we are creating a conference for a unit test, it is not neccesary
01233           * to open a pseudo channel, so, if we fail continue creating
01234           * the conference. */
01235          ast_test_status_update(test, "Unable to open DAHDI pseudo device\n");
01236       } else {
01237          ast_log(LOG_WARNING, "Unable to open DAHDI pseudo device\n");
01238          if (cnf->fd >= 0)
01239             close(cnf->fd);
01240          ao2_ref(cnf->usercontainer, -1);
01241          ast_mutex_destroy(&cnf->playlock);
01242          ast_mutex_destroy(&cnf->listenlock);
01243          ast_mutex_destroy(&cnf->recordthreadlock);
01244          ast_mutex_destroy(&cnf->announcethreadlock);
01245          ast_free(cnf);
01246          cnf = NULL;
01247          goto cnfout;
01248       }
01249    }
01250 
01251    cnf->dahdiconf = dahdic.confno;
01252 
01253    /* Setup a new channel for playback of audio files */
01254    cnf->chan = ast_request("DAHDI", AST_FORMAT_SLINEAR, chan, "pseudo", NULL);
01255    if (cnf->chan) {
01256       ast_set_read_format(cnf->chan, AST_FORMAT_SLINEAR);
01257       ast_set_write_format(cnf->chan, AST_FORMAT_SLINEAR);
01258       dahdic.chan = 0;
01259       dahdic.confno = cnf->dahdiconf;
01260       dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
01261       if (ioctl(cnf->chan->fds[0], DAHDI_SETCONF, &dahdic)) {
01262          if (test) {
01263             ast_test_status_update(test, "Error setting conference on pseudo channel\n");
01264          }
01265          ast_log(LOG_WARNING, "Error setting conference\n");
01266          if (cnf->chan)
01267             ast_hangup(cnf->chan);
01268          else
01269             close(cnf->fd);
01270          ao2_ref(cnf->usercontainer, -1);
01271          ast_mutex_destroy(&cnf->playlock);
01272          ast_mutex_destroy(&cnf->listenlock);
01273          ast_mutex_destroy(&cnf->recordthreadlock);
01274          ast_mutex_destroy(&cnf->announcethreadlock);
01275          ast_free(cnf);
01276          cnf = NULL;
01277          goto cnfout;
01278       }
01279    }
01280 
01281    /* Fill the conference struct */
01282    cnf->start = time(NULL);
01283    cnf->maxusers = 0x7fffffff;
01284    cnf->isdynamic = dynamic ? 1 : 0;
01285    ast_verb(3, "Created MeetMe conference %d for conference '%s'\n", cnf->dahdiconf, cnf->confno);
01286    AST_LIST_INSERT_HEAD(&confs, cnf, list);
01287 
01288    /* Reserve conference number in map */
01289    if ((sscanf(cnf->confno, "%30d", &confno_int) == 1) && (confno_int >= 0 && confno_int < 1024))
01290       conf_map[confno_int] = 1;
01291    
01292 cnfout:
01293    if (cnf)
01294       ast_atomic_fetchadd_int(&cnf->refcount, refcount);
01295 
01296    AST_LIST_UNLOCK(&confs);
01297 
01298    return cnf;
01299 }
01300 
01301 static char *complete_confno(const char *word, int state)
01302 {
01303    struct ast_conference *cnf;
01304    char *ret = NULL;
01305    int which = 0;
01306    int len = strlen(word);
01307 
01308    AST_LIST_LOCK(&confs);
01309    AST_LIST_TRAVERSE(&confs, cnf, list) {
01310       if (!strncmp(word, cnf->confno, len) && ++which > state) {
01311          /* dup before releasing the lock */
01312          ret = ast_strdup(cnf->confno);
01313          break;
01314       }
01315    }
01316    AST_LIST_UNLOCK(&confs);
01317    return ret;
01318 }
01319 
01320 static char *complete_userno(struct ast_conference *cnf, const char *word, int state)
01321 {
01322    char usrno[50];
01323    struct ao2_iterator iter;
01324    struct ast_conf_user *usr;
01325    char *ret = NULL;
01326    int which = 0;
01327    int len = strlen(word);
01328 
01329    iter = ao2_iterator_init(cnf->usercontainer, 0);
01330    for (; (usr = ao2_iterator_next(&iter)); ao2_ref(usr, -1)) {
01331       snprintf(usrno, sizeof(usrno), "%d", usr->user_no);
01332       if (!strncmp(word, usrno, len) && ++which > state) {
01333          ao2_ref(usr, -1);
01334          ret = ast_strdup(usrno);
01335          break;
01336       }
01337    }
01338    ao2_iterator_destroy(&iter);
01339    return ret;
01340 }
01341 
01342 static char *complete_meetmecmd_mute_kick(const char *line, const char *word, int pos, int state)
01343 {
01344    if (pos == 2) {
01345       return complete_confno(word, state);
01346    }
01347    if (pos == 3) {
01348       int len = strlen(word);
01349       char *ret = NULL;
01350       char *saved = NULL;
01351       char *myline;
01352       char *confno;
01353       struct ast_conference *cnf;
01354 
01355       if (!strncasecmp(word, "all", len)) {
01356          if (state == 0) {
01357             return ast_strdup("all");
01358          }
01359          --state;
01360       }
01361 
01362       /* Extract the confno from the command line. */
01363       myline = ast_strdupa(line);
01364       strtok_r(myline, " ", &saved);
01365       strtok_r(NULL, " ", &saved);
01366       confno = strtok_r(NULL, " ", &saved);
01367 
01368       AST_LIST_LOCK(&confs);
01369       AST_LIST_TRAVERSE(&confs, cnf, list) {
01370          if (!strcmp(confno, cnf->confno)) {
01371             ret = complete_userno(cnf, word, state);
01372             break;
01373          }
01374       }
01375       AST_LIST_UNLOCK(&confs);
01376 
01377       return ret;
01378    }
01379    return NULL;
01380 }
01381 
01382 static char *complete_meetmecmd_lock(const char *word, int pos, int state)
01383 {
01384    if (pos == 2) {
01385       return complete_confno(word, state);
01386    }
01387    return NULL;
01388 }
01389 
01390 static char *complete_meetmecmd_list(const char *line, const char *word, int pos, int state)
01391 {
01392    int len;
01393 
01394    if (pos == 2) {
01395       len = strlen(word);
01396       if (!strncasecmp(word, STR_CONCISE, len)) {
01397          if (state == 0) {
01398             return ast_strdup(STR_CONCISE);
01399          }
01400          --state;
01401       }
01402 
01403       return complete_confno(word, state);
01404    }
01405    if (pos == 3 && state == 0) {
01406       char *saved = NULL;
01407       char *myline;
01408       char *confno;
01409 
01410       /* Extract the confno from the command line. */
01411       myline = ast_strdupa(line);
01412       strtok_r(myline, " ", &saved);
01413       strtok_r(NULL, " ", &saved);
01414       confno = strtok_r(NULL, " ", &saved);
01415 
01416       if (!strcasecmp(confno, STR_CONCISE)) {
01417          /* There is nothing valid in this position now. */
01418          return NULL;
01419       }
01420 
01421       len = strlen(word);
01422       if (!strncasecmp(word, STR_CONCISE, len)) {
01423          return ast_strdup(STR_CONCISE);
01424       }
01425    }
01426    return NULL;
01427 }
01428 
01429 static char *meetme_show_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01430 {
01431    /* Process the command */
01432    struct ast_conf_user *user;
01433    struct ast_conference *cnf;
01434    int hr, min, sec;
01435    int total = 0;
01436    time_t now;
01437 #define MC_HEADER_FORMAT "%-14s %-14s %-10s %-8s  %-8s  %-6s\n"
01438 #define MC_DATA_FORMAT "%-12.12s   %4.4d        %4.4s       %02d:%02d:%02d  %-8s  %-6s\n"
01439 
01440    switch (cmd) {
01441    case CLI_INIT:
01442       e->command = "meetme list";
01443       e->usage =
01444          "Usage: meetme list [<confno>] [" STR_CONCISE "]\n"
01445          "       List all conferences or a specific conference.\n";
01446       return NULL;
01447    case CLI_GENERATE:
01448       return complete_meetmecmd_list(a->line, a->word, a->pos, a->n);
01449    }
01450 
01451    if (a->argc == 2 || (a->argc == 3 && !strcasecmp(a->argv[2], STR_CONCISE))) {
01452       /* List all the conferences */
01453       int concise = (a->argc == 3);
01454       struct ast_str *marked_users;
01455 
01456       if (!(marked_users = ast_str_create(30))) {
01457          return CLI_FAILURE;
01458       }
01459 
01460       now = time(NULL);
01461       AST_LIST_LOCK(&confs);
01462       if (AST_LIST_EMPTY(&confs)) {
01463          if (!concise) {
01464             ast_cli(a->fd, "No active MeetMe conferences.\n");
01465          }
01466          AST_LIST_UNLOCK(&confs);
01467          ast_free(marked_users);
01468          return CLI_SUCCESS;
01469       }
01470       if (!concise) {
01471          ast_cli(a->fd, MC_HEADER_FORMAT, "Conf Num", "Parties", "Marked", "Activity", "Creation", "Locked");
01472       }
01473       AST_LIST_TRAVERSE(&confs, cnf, list) {
01474          hr = (now - cnf->start) / 3600;
01475          min = ((now - cnf->start) % 3600) / 60;
01476          sec = (now - cnf->start) % 60;
01477          if (!concise) {
01478             if (cnf->markedusers == 0) {
01479                ast_str_set(&marked_users, 0, "N/A ");
01480             } else {
01481                ast_str_set(&marked_users, 0, "%4.4d", cnf->markedusers);
01482             }
01483             ast_cli(a->fd, MC_DATA_FORMAT, cnf->confno, cnf->users,
01484                ast_str_buffer(marked_users), hr, min, sec,
01485                cnf->isdynamic ? "Dynamic" : "Static", cnf->locked ? "Yes" : "No");
01486          } else {
01487             ast_cli(a->fd, "%s!%d!%d!%02d:%02d:%02d!%d!%d\n",
01488                cnf->confno,
01489                cnf->users,
01490                cnf->markedusers,
01491                hr, min, sec,
01492                cnf->isdynamic,
01493                cnf->locked);
01494          }
01495 
01496          total += cnf->users;
01497       }
01498       AST_LIST_UNLOCK(&confs);
01499       if (!concise) {
01500          ast_cli(a->fd, "* Total number of MeetMe users: %d\n", total);
01501       }
01502       ast_free(marked_users);
01503       return CLI_SUCCESS;
01504    }
01505    if (a->argc == 3 || (a->argc == 4 && !strcasecmp(a->argv[3], STR_CONCISE))) {
01506       struct ao2_iterator user_iter;
01507       int concise = (a->argc == 4);
01508 
01509       /* List all the users in a conference */
01510       if (AST_LIST_EMPTY(&confs)) {
01511          if (!concise) {
01512             ast_cli(a->fd, "No active MeetMe conferences.\n");
01513          }
01514          return CLI_SUCCESS;
01515       }
01516       /* Find the right conference */
01517       AST_LIST_LOCK(&confs);
01518       AST_LIST_TRAVERSE(&confs, cnf, list) {
01519          if (strcmp(cnf->confno, a->argv[2]) == 0) {
01520             break;
01521          }
01522       }
01523       if (!cnf) {
01524          if (!concise)
01525             ast_cli(a->fd, "No such conference: %s.\n", a->argv[2]);
01526          AST_LIST_UNLOCK(&confs);
01527          return CLI_SUCCESS;
01528       }
01529       /* Show all the users */
01530       time(&now);
01531       user_iter = ao2_iterator_init(cnf->usercontainer, 0);
01532       while((user = ao2_iterator_next(&user_iter))) {
01533          hr = (now - user->jointime) / 3600;
01534          min = ((now - user->jointime) % 3600) / 60;
01535          sec = (now - user->jointime) % 60;
01536          if (!concise) {
01537             ast_cli(a->fd, "User #: %-2.2d %12.12s %-20.20s Channel: %s %s %s %s %s %s %02d:%02d:%02d\n",
01538                user->user_no,
01539                S_COR(user->chan->caller.id.number.valid, user->chan->caller.id.number.str, "<unknown>"),
01540                S_COR(user->chan->caller.id.name.valid, user->chan->caller.id.name.str, "<no name>"),
01541                user->chan->name,
01542                ast_test_flag64(&user->userflags, CONFFLAG_ADMIN) ? "(Admin)" : "",
01543                ast_test_flag64(&user->userflags, CONFFLAG_MONITOR) ? "(Listen only)" : "",
01544                user->adminflags & ADMINFLAG_MUTED ? "(Admin Muted)" : user->adminflags & ADMINFLAG_SELFMUTED ? "(Muted)" : "",
01545                user->adminflags & ADMINFLAG_T_REQUEST ? "(Request to Talk)" : "",
01546                istalking(user->talking), hr, min, sec); 
01547          } else {
01548             ast_cli(a->fd, "%d!%s!%s!%s!%s!%s!%s!%s!%d!%02d:%02d:%02d\n",
01549                user->user_no,
01550                S_COR(user->chan->caller.id.number.valid, user->chan->caller.id.number.str, ""),
01551                S_COR(user->chan->caller.id.name.valid, user->chan->caller.id.name.str, ""),
01552                user->chan->name,
01553                ast_test_flag64(&user->userflags, CONFFLAG_ADMIN) ? "1" : "",
01554                ast_test_flag64(&user->userflags, CONFFLAG_MONITOR) ? "1" : "",
01555                user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED) ? "1" : "",
01556                user->adminflags & ADMINFLAG_T_REQUEST ? "1" : "",
01557                user->talking, hr, min, sec);
01558          }
01559          ao2_ref(user, -1);
01560       }
01561       ao2_iterator_destroy(&user_iter);
01562       if (!concise) {
01563          ast_cli(a->fd, "%d users in that conference.\n", cnf->users);
01564       }
01565       AST_LIST_UNLOCK(&confs);
01566       return CLI_SUCCESS;
01567    }
01568    return CLI_SHOWUSAGE;
01569 }
01570 
01571 
01572 static char *meetme_cmd_helper(struct ast_cli_args *a)
01573 {
01574    /* Process the command */
01575    struct ast_str *cmdline;
01576 
01577    /* Max confno length */
01578    if (!(cmdline = ast_str_create(MAX_CONFNUM))) {
01579       return CLI_FAILURE;
01580    }
01581 
01582    ast_str_set(&cmdline, 0, "%s", a->argv[2]);  /* Argv 2: conference number */
01583    if (strcasestr(a->argv[1], "lock")) {
01584       if (strcasecmp(a->argv[1], "lock") == 0) {
01585          /* Lock */
01586          ast_str_append(&cmdline, 0, ",L");
01587       } else {
01588          /* Unlock */
01589          ast_str_append(&cmdline, 0, ",l");
01590       }
01591    } else if (strcasestr(a->argv[1], "mute")) { 
01592       if (strcasecmp(a->argv[1], "mute") == 0) {
01593          /* Mute */
01594          if (strcasecmp(a->argv[3], "all") == 0) {
01595             ast_str_append(&cmdline, 0, ",N");
01596          } else {
01597             ast_str_append(&cmdline, 0, ",M,%s", a->argv[3]);  
01598          }
01599       } else {
01600          /* Unmute */
01601          if (strcasecmp(a->argv[3], "all") == 0) {
01602             ast_str_append(&cmdline, 0, ",n");
01603          } else {
01604             ast_str_append(&cmdline, 0, ",m,%s", a->argv[3]);
01605          }
01606       }
01607    } else if (strcasecmp(a->argv[1], "kick") == 0) {
01608       if (strcasecmp(a->argv[3], "all") == 0) {
01609          /* Kick all */
01610          ast_str_append(&cmdline, 0, ",K");
01611       } else {
01612          /* Kick a single user */
01613          ast_str_append(&cmdline, 0, ",k,%s", a->argv[3]);
01614       }
01615    } else {
01616       /*
01617        * Should never get here because it is already filtered by the
01618        * callers.
01619        */
01620       ast_free(cmdline);
01621       return CLI_SHOWUSAGE;
01622    }
01623 
01624    ast_debug(1, "Cmdline: %s\n", ast_str_buffer(cmdline));
01625 
01626    admin_exec(NULL, ast_str_buffer(cmdline));
01627    ast_free(cmdline);
01628 
01629    return CLI_SUCCESS;
01630 }
01631 
01632 static char *meetme_lock_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01633 {
01634    switch (cmd) {
01635    case CLI_INIT:
01636       e->command = "meetme {lock|unlock}";
01637       e->usage =
01638          "Usage: meetme lock|unlock <confno>\n"
01639          "       Lock or unlock a conference to new users.\n";
01640       return NULL;
01641    case CLI_GENERATE:
01642       return complete_meetmecmd_lock(a->word, a->pos, a->n);
01643    }
01644 
01645    if (a->argc != 3) {
01646       return CLI_SHOWUSAGE;
01647    }
01648 
01649    return meetme_cmd_helper(a);
01650 }
01651 
01652 static char *meetme_kick_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01653 {
01654    switch (cmd) {
01655    case CLI_INIT:
01656       e->command = "meetme kick";
01657       e->usage =
01658          "Usage: meetme kick <confno> all|<userno>\n"
01659          "       Kick a conference or a user in a conference.\n";
01660       return NULL;
01661    case CLI_GENERATE:
01662       return complete_meetmecmd_mute_kick(a->line, a->word, a->pos, a->n);
01663    }
01664 
01665    if (a->argc != 4) {
01666       return CLI_SHOWUSAGE;
01667    }
01668 
01669    return meetme_cmd_helper(a);
01670 }
01671 
01672 static char *meetme_mute_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01673 {
01674    switch (cmd) {
01675    case CLI_INIT:
01676       e->command = "meetme {mute|unmute}";
01677       e->usage =
01678          "Usage: meetme mute|unmute <confno> all|<userno>\n"
01679          "       Mute or unmute a conference or a user in a conference.\n";
01680       return NULL;
01681    case CLI_GENERATE:
01682       return complete_meetmecmd_mute_kick(a->line, a->word, a->pos, a->n);
01683    }
01684 
01685    if (a->argc != 4) {
01686       return CLI_SHOWUSAGE;
01687    }
01688 
01689    return meetme_cmd_helper(a);
01690 }
01691 
01692 static const char *sla_hold_str(unsigned int hold_access)
01693 {
01694    const char *hold = "Unknown";
01695 
01696    switch (hold_access) {
01697    case SLA_HOLD_OPEN:
01698       hold = "Open";
01699       break;
01700    case SLA_HOLD_PRIVATE:
01701       hold = "Private";
01702    default:
01703       break;
01704    }
01705 
01706    return hold;
01707 }
01708 
01709 static char *sla_show_trunks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01710 {
01711    const struct sla_trunk *trunk;
01712 
01713    switch (cmd) {
01714    case CLI_INIT:
01715       e->command = "sla show trunks";
01716       e->usage =
01717          "Usage: sla show trunks\n"
01718          "       This will list all trunks defined in sla.conf\n";
01719       return NULL;
01720    case CLI_GENERATE:
01721       return NULL;
01722    }
01723 
01724    ast_cli(a->fd, "\n"
01725                "=============================================================\n"
01726                "=== Configured SLA Trunks ===================================\n"
01727                "=============================================================\n"
01728                "===\n");
01729    AST_RWLIST_RDLOCK(&sla_trunks);
01730    AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
01731       struct sla_station_ref *station_ref;
01732       char ring_timeout[16] = "(none)";
01733       if (trunk->ring_timeout)
01734          snprintf(ring_timeout, sizeof(ring_timeout), "%u Seconds", trunk->ring_timeout);
01735       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
01736                   "=== Trunk Name:       %s\n"
01737                   "=== ==> Device:       %s\n"
01738                   "=== ==> AutoContext:  %s\n"
01739                   "=== ==> RingTimeout:  %s\n"
01740                   "=== ==> BargeAllowed: %s\n"
01741                   "=== ==> HoldAccess:   %s\n"
01742                   "=== ==> Stations ...\n",
01743                   trunk->name, trunk->device, 
01744                   S_OR(trunk->autocontext, "(none)"), 
01745                   ring_timeout,
01746                   trunk->barge_disabled ? "No" : "Yes",
01747                   sla_hold_str(trunk->hold_access));
01748       AST_RWLIST_RDLOCK(&sla_stations);
01749       AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry)
01750          ast_cli(a->fd, "===    ==> Station name: %s\n", station_ref->station->name);
01751       AST_RWLIST_UNLOCK(&sla_stations);
01752       ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");
01753    }
01754    AST_RWLIST_UNLOCK(&sla_trunks);
01755    ast_cli(a->fd, "=============================================================\n\n");
01756 
01757    return CLI_SUCCESS;
01758 }
01759 
01760 static const char *trunkstate2str(enum sla_trunk_state state)
01761 {
01762 #define S(e) case e: return # e;
01763    switch (state) {
01764    S(SLA_TRUNK_STATE_IDLE)
01765    S(SLA_TRUNK_STATE_RINGING)
01766    S(SLA_TRUNK_STATE_UP)
01767    S(SLA_TRUNK_STATE_ONHOLD)
01768    S(SLA_TRUNK_STATE_ONHOLD_BYME)
01769    }
01770    return "Uknown State";
01771 #undef S
01772 }
01773 
01774 static char *sla_show_stations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01775 {
01776    const struct sla_station *station;
01777 
01778    switch (cmd) {
01779    case CLI_INIT:
01780       e->command = "sla show stations";
01781       e->usage =
01782          "Usage: sla show stations\n"
01783          "       This will list all stations defined in sla.conf\n";
01784       return NULL;
01785    case CLI_GENERATE:
01786       return NULL;
01787    }
01788 
01789    ast_cli(a->fd, "\n" 
01790                "=============================================================\n"
01791                "=== Configured SLA Stations =================================\n"
01792                "=============================================================\n"
01793                "===\n");
01794    AST_RWLIST_RDLOCK(&sla_stations);
01795    AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
01796       struct sla_trunk_ref *trunk_ref;
01797       char ring_timeout[16] = "(none)";
01798       char ring_delay[16] = "(none)";
01799       if (station->ring_timeout) {
01800          snprintf(ring_timeout, sizeof(ring_timeout), 
01801             "%u", station->ring_timeout);
01802       }
01803       if (station->ring_delay) {
01804          snprintf(ring_delay, sizeof(ring_delay), 
01805             "%u", station->ring_delay);
01806       }
01807       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
01808                   "=== Station Name:    %s\n"
01809                   "=== ==> Device:      %s\n"
01810                   "=== ==> AutoContext: %s\n"
01811                   "=== ==> RingTimeout: %s\n"
01812                   "=== ==> RingDelay:   %s\n"
01813                   "=== ==> HoldAccess:  %s\n"
01814                   "=== ==> Trunks ...\n",
01815                   station->name, station->device,
01816                   S_OR(station->autocontext, "(none)"), 
01817                   ring_timeout, ring_delay,
01818                   sla_hold_str(station->hold_access));
01819       AST_RWLIST_RDLOCK(&sla_trunks);
01820       AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
01821          if (trunk_ref->ring_timeout) {
01822             snprintf(ring_timeout, sizeof(ring_timeout),
01823                "%u", trunk_ref->ring_timeout);
01824          } else
01825             strcpy(ring_timeout, "(none)");
01826          if (trunk_ref->ring_delay) {
01827             snprintf(ring_delay, sizeof(ring_delay),
01828                "%u", trunk_ref->ring_delay);
01829          } else
01830             strcpy(ring_delay, "(none)");
01831             ast_cli(a->fd, "===    ==> Trunk Name: %s\n"
01832                      "===       ==> State:       %s\n"
01833                      "===       ==> RingTimeout: %s\n"
01834                      "===       ==> RingDelay:   %s\n",
01835                      trunk_ref->trunk->name,
01836                      trunkstate2str(trunk_ref->state),
01837                      ring_timeout, ring_delay);
01838       }
01839       AST_RWLIST_UNLOCK(&sla_trunks);
01840       ast_cli(a->fd, "=== ---------------------------------------------------------\n"
01841                   "===\n");
01842    }
01843    AST_RWLIST_UNLOCK(&sla_stations);
01844    ast_cli(a->fd, "============================================================\n"
01845                "\n");
01846 
01847    return CLI_SUCCESS;
01848 }
01849 
01850 static struct ast_cli_entry cli_meetme[] = {
01851    AST_CLI_DEFINE(meetme_kick_cmd, "Kick a conference or a user in a conference."),
01852    AST_CLI_DEFINE(meetme_show_cmd, "List all conferences or a specific conference."),
01853    AST_CLI_DEFINE(meetme_lock_cmd, "Lock or unlock a conference to new users."),
01854    AST_CLI_DEFINE(meetme_mute_cmd, "Mute or unmute a conference or a user in a conference."),
01855    AST_CLI_DEFINE(sla_show_trunks, "Show SLA Trunks"),
01856    AST_CLI_DEFINE(sla_show_stations, "Show SLA Stations"),
01857 };
01858 
01859 static void conf_flush(int fd, struct ast_channel *chan)
01860 {
01861    int x;
01862 
01863    /* read any frames that may be waiting on the channel
01864       and throw them away
01865    */
01866    if (chan) {
01867       struct ast_frame *f;
01868 
01869       /* when no frames are available, this will wait
01870          for 1 millisecond maximum
01871       */
01872       while (ast_waitfor(chan, 1) > 0) {
01873          f = ast_read(chan);
01874          if (f)
01875             ast_frfree(f);
01876          else /* channel was hung up or something else happened */
01877             break;
01878       }
01879    }
01880 
01881    /* flush any data sitting in the pseudo channel */
01882    x = DAHDI_FLUSH_ALL;
01883    if (ioctl(fd, DAHDI_FLUSH, &x))
01884       ast_log(LOG_WARNING, "Error flushing channel\n");
01885 
01886 }
01887 
01888 /*! \brief Remove the conference from the list and free it.
01889 
01890    We assume that this was called while holding conflock. */
01891 static int conf_free(struct ast_conference *conf)
01892 {
01893    int x;
01894    struct announce_listitem *item;
01895    
01896    AST_LIST_REMOVE(&confs, conf, list);
01897    manager_event(EVENT_FLAG_CALL, "MeetmeEnd", "Meetme: %s\r\n", conf->confno);
01898 
01899    if (conf->recording == MEETME_RECORD_ACTIVE) {
01900       conf->recording = MEETME_RECORD_TERMINATE;
01901       AST_LIST_UNLOCK(&confs);
01902       while (1) {
01903          usleep(1);
01904          AST_LIST_LOCK(&confs);
01905          if (conf->recording == MEETME_RECORD_OFF)
01906             break;
01907          AST_LIST_UNLOCK(&confs);
01908       }
01909    }
01910 
01911    for (x = 0; x < AST_FRAME_BITS; x++) {
01912       if (conf->transframe[x])
01913          ast_frfree(conf->transframe[x]);
01914       if (conf->transpath[x])
01915          ast_translator_free_path(conf->transpath[x]);
01916    }
01917    if (conf->announcethread != AST_PTHREADT_NULL) {
01918       ast_mutex_lock(&conf->announcelistlock);
01919       conf->announcethread_stop = 1;
01920       ast_softhangup(conf->chan, AST_SOFTHANGUP_EXPLICIT);
01921       ast_cond_signal(&conf->announcelist_addition);
01922       ast_mutex_unlock(&conf->announcelistlock);
01923       pthread_join(conf->announcethread, NULL);
01924    
01925       while ((item = AST_LIST_REMOVE_HEAD(&conf->announcelist, entry))) {
01926          ast_filedelete(item->namerecloc, NULL);
01927          ao2_ref(item, -1);
01928       }
01929       ast_mutex_destroy(&conf->announcelistlock);
01930    }
01931 
01932    if (conf->origframe)
01933       ast_frfree(conf->origframe);
01934    if (conf->lchan)
01935       ast_hangup(conf->lchan);
01936    if (conf->chan)
01937       ast_hangup(conf->chan);
01938    if (conf->fd >= 0)
01939       close(conf->fd);
01940    if (conf->recordingfilename) {
01941       ast_free(conf->recordingfilename);
01942    }
01943    if (conf->usercontainer) {
01944       ao2_ref(conf->usercontainer, -1);
01945    }
01946    if (conf->recordingformat) {
01947       ast_free(conf->recordingformat);
01948    }
01949    ast_mutex_destroy(&conf->playlock);
01950    ast_mutex_destroy(&conf->listenlock);
01951    ast_mutex_destroy(&conf->recordthreadlock);
01952    ast_mutex_destroy(&conf->announcethreadlock);
01953    ast_free(conf);
01954 
01955    return 0;
01956 }
01957 
01958 static void conf_queue_dtmf(const struct ast_conference *conf,
01959    const struct ast_conf_user *sender, struct ast_frame *f)
01960 {
01961    struct ast_conf_user *user;
01962    struct ao2_iterator user_iter;
01963 
01964    user_iter = ao2_iterator_init(conf->usercontainer, 0);
01965    while ((user = ao2_iterator_next(&user_iter))) {
01966       if (user == sender) {
01967          ao2_ref(user, -1);
01968          continue;
01969       }
01970       if (ast_write(user->chan, f) < 0)
01971          ast_log(LOG_WARNING, "Error writing frame to channel %s\n", user->chan->name);
01972       ao2_ref(user, -1);
01973    }
01974    ao2_iterator_destroy(&user_iter);
01975 }
01976 
01977 static void sla_queue_event_full(enum sla_event_type type, 
01978    struct sla_trunk_ref *trunk_ref, struct sla_station *station, int lock)
01979 {
01980    struct sla_event *event;
01981 
01982    if (sla.thread == AST_PTHREADT_NULL) {
01983       return;
01984    }
01985 
01986    if (!(event = ast_calloc(1, sizeof(*event))))
01987       return;
01988 
01989    event->type = type;
01990    event->trunk_ref = trunk_ref;
01991    event->station = station;
01992 
01993    if (!lock) {
01994       AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);
01995       return;
01996    }
01997 
01998    ast_mutex_lock(&sla.lock);
01999    AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);
02000    ast_cond_signal(&sla.cond);
02001    ast_mutex_unlock(&sla.lock);
02002 }
02003 
02004 static void sla_queue_event_nolock(enum sla_event_type type)
02005 {
02006    sla_queue_event_full(type, NULL, NULL, 0);
02007 }
02008 
02009 static void sla_queue_event(enum sla_event_type type)
02010 {
02011    sla_queue_event_full(type, NULL, NULL, 1);
02012 }
02013 
02014 /*! \brief Queue a SLA event from the conference */
02015 static void sla_queue_event_conf(enum sla_event_type type, struct ast_channel *chan,
02016    struct ast_conference *conf)
02017 {
02018    struct sla_station *station;
02019    struct sla_trunk_ref *trunk_ref = NULL;
02020    char *trunk_name;
02021 
02022    trunk_name = ast_strdupa(conf->confno);
02023    strsep(&trunk_name, "_");
02024    if (ast_strlen_zero(trunk_name)) {
02025       ast_log(LOG_ERROR, "Invalid conference name for SLA - '%s'!\n", conf->confno);
02026       return;
02027    }
02028 
02029    AST_RWLIST_RDLOCK(&sla_stations);
02030    AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
02031       AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
02032          if (trunk_ref->chan == chan && !strcmp(trunk_ref->trunk->name, trunk_name))
02033             break;
02034       }
02035       if (trunk_ref)
02036          break;
02037    }
02038    AST_RWLIST_UNLOCK(&sla_stations);
02039 
02040    if (!trunk_ref) {
02041       ast_debug(1, "Trunk not found for event!\n");
02042       return;
02043    }
02044 
02045    sla_queue_event_full(type, trunk_ref, station, 1);
02046 }
02047 
02048 /*! \brief Decrement reference counts, as incremented by find_conf() */
02049 static int dispose_conf(struct ast_conference *conf)
02050 {
02051    int res = 0;
02052    int confno_int = 0;
02053 
02054    AST_LIST_LOCK(&confs);
02055    if (ast_atomic_dec_and_test(&conf->refcount)) {
02056       /* Take the conference room number out of an inuse state */
02057       if ((sscanf(conf->confno, "%4d", &confno_int) == 1) && (confno_int >= 0 && confno_int < 1024)) {
02058          conf_map[confno_int] = 0;
02059       }
02060       conf_free(conf);
02061       res = 1;
02062    }
02063    AST_LIST_UNLOCK(&confs);
02064 
02065    return res;
02066 }
02067 
02068 static int rt_extend_conf(const char *confno)
02069 {
02070    char currenttime[32];
02071    char endtime[32];
02072    struct timeval now;
02073    struct ast_tm tm;
02074    struct ast_variable *var, *orig_var;
02075    char bookid[51];
02076 
02077    if (!extendby) {
02078       return 0;
02079    }
02080 
02081    now = ast_tvnow();
02082 
02083    ast_localtime(&now, &tm, NULL);
02084    ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
02085 
02086    var = ast_load_realtime("meetme", "confno",
02087       confno, "startTime<= ", currenttime,
02088       "endtime>= ", currenttime, NULL);
02089 
02090    orig_var = var;
02091 
02092    /* Identify the specific RealTime conference */
02093    while (var) {
02094       if (!strcasecmp(var->name, "bookid")) {
02095          ast_copy_string(bookid, var->value, sizeof(bookid));
02096       }
02097       if (!strcasecmp(var->name, "endtime")) {
02098          ast_copy_string(endtime, var->value, sizeof(endtime));
02099       }
02100 
02101       var = var->next;
02102    }
02103    ast_variables_destroy(orig_var);
02104 
02105    ast_strptime(endtime, DATE_FORMAT, &tm);
02106    now = ast_mktime(&tm, NULL);
02107 
02108    now.tv_sec += extendby;
02109 
02110    ast_localtime(&now, &tm, NULL);
02111    ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
02112    strcat(currenttime, "0"); /* Seconds needs to be 00 */
02113 
02114    var = ast_load_realtime("meetme", "confno",
02115       confno, "startTime<= ", currenttime,
02116       "endtime>= ", currenttime, NULL);
02117 
02118    /* If there is no conflict with extending the conference, update the DB */
02119    if (!var) {
02120       ast_debug(3, "Trying to update the endtime of Conference %s to %s\n", confno, currenttime);
02121       ast_update_realtime("meetme", "bookid", bookid, "endtime", currenttime, NULL);
02122       return 0;
02123 
02124    }
02125 
02126    ast_variables_destroy(var);
02127    return -1;
02128 }
02129 
02130 static void conf_start_moh(struct ast_channel *chan, const char *musicclass)
02131 {
02132    char *original_moh;
02133 
02134    ast_channel_lock(chan);
02135    original_moh = ast_strdupa(chan->musicclass);
02136    ast_string_field_set(chan, musicclass, musicclass);
02137    ast_channel_unlock(chan);
02138 
02139    ast_moh_start(chan, original_moh, NULL);
02140 
02141    ast_channel_lock(chan);
02142    ast_string_field_set(chan, musicclass, original_moh);
02143    ast_channel_unlock(chan);
02144 }
02145 
02146 static const char *get_announce_filename(enum announcetypes type)
02147 {
02148    switch (type) {
02149    case CONF_HASLEFT:
02150       return "conf-hasleft";
02151       break;
02152    case CONF_HASJOIN:
02153       return "conf-hasjoin";
02154       break;
02155    default:
02156       return "";
02157    }
02158 }
02159 
02160 static void *announce_thread(void *data)
02161 {
02162    struct announce_listitem *current;
02163    struct ast_conference *conf = data;
02164    int res;
02165    char filename[PATH_MAX] = "";
02166    AST_LIST_HEAD_NOLOCK(, announce_listitem) local_list;
02167    AST_LIST_HEAD_INIT_NOLOCK(&local_list);
02168 
02169    while (!conf->announcethread_stop) {
02170       ast_mutex_lock(&conf->announcelistlock);
02171       if (conf->announcethread_stop) {
02172          ast_mutex_unlock(&conf->announcelistlock);
02173          break;
02174       }
02175       if (AST_LIST_EMPTY(&conf->announcelist))
02176          ast_cond_wait(&conf->announcelist_addition, &conf->announcelistlock);
02177 
02178       AST_LIST_APPEND_LIST(&local_list, &conf->announcelist, entry);
02179       AST_LIST_HEAD_INIT_NOLOCK(&conf->announcelist);
02180 
02181       ast_mutex_unlock(&conf->announcelistlock);
02182       if (conf->announcethread_stop) {
02183          break;
02184       }
02185 
02186       for (res = 1; !conf->announcethread_stop && (current = AST_LIST_REMOVE_HEAD(&local_list, entry)); ao2_ref(current, -1)) {
02187          ast_log(LOG_DEBUG, "About to play %s\n", current->namerecloc);
02188          if (!ast_fileexists(current->namerecloc, NULL, NULL))
02189             continue;
02190          if ((current->confchan) && (current->confusers > 1) && !ast_check_hangup(current->confchan)) {
02191             if (!ast_streamfile(current->confchan, current->namerecloc, current->language))
02192                res = ast_waitstream(current->confchan, "");
02193             if (!res) {
02194                ast_copy_string(filename, get_announce_filename(current->announcetype), sizeof(filename));
02195                if (!ast_streamfile(current->confchan, filename, current->language))
02196                   ast_waitstream(current->confchan, "");
02197             }
02198          }
02199          if (current->announcetype == CONF_HASLEFT) {
02200             ast_filedelete(current->namerecloc, NULL);
02201          }
02202       }
02203    }
02204 
02205    /* thread marked to stop, clean up */
02206    while ((current = AST_LIST_REMOVE_HEAD(&local_list, entry))) {
02207       ast_filedelete(current->namerecloc, NULL);
02208       ao2_ref(current, -1);
02209    }
02210    return NULL;
02211 }
02212 
02213 static int can_write(struct ast_channel *chan, struct ast_flags64 *confflags)
02214 {
02215    if (!ast_test_flag64(confflags, CONFFLAG_NO_AUDIO_UNTIL_UP)) {
02216       return 1;
02217    }
02218 
02219    return (chan->_state == AST_STATE_UP);
02220 }
02221 
02222 static void send_talking_event(struct ast_channel *chan, struct ast_conference *conf, struct ast_conf_user *user, int talking)
02223 {
02224    ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeTalking",
02225       "Channel: %s\r\n"
02226       "Uniqueid: %s\r\n"
02227       "Meetme: %s\r\n"
02228       "Usernum: %d\r\n"
02229       "Status: %s\r\n",
02230       chan->name, chan->uniqueid, conf->confno, user->user_no, talking ? "on" : "off");
02231 }
02232 
02233 static void set_user_talking(struct ast_channel *chan, struct ast_conference *conf, struct ast_conf_user *user, int talking, int monitor)
02234 {
02235    int last_talking = user->talking;
02236    if (last_talking == talking)
02237       return;
02238 
02239    user->talking = talking;
02240 
02241    if (monitor) {
02242       /* Check if talking state changed. Take care of -1 which means unmonitored */
02243       int was_talking = (last_talking > 0);
02244       int now_talking = (talking > 0);
02245       if (was_talking != now_talking) {
02246          send_talking_event(chan, conf, user, now_talking);
02247       }
02248    }
02249 }
02250 
02251 static int user_set_kickme_cb(void *obj, void *check_admin_arg, int flags)
02252 {
02253    struct ast_conf_user *user = obj;
02254    /* actual pointer contents of check_admin_arg is irrelevant */
02255 
02256    if (!check_admin_arg || (check_admin_arg && !ast_test_flag64(&user->userflags, CONFFLAG_ADMIN))) {
02257       user->adminflags |= ADMINFLAG_KICKME;
02258    }
02259    return 0;
02260 }
02261 
02262 static int user_set_unmuted_cb(void *obj, void *check_admin_arg, int flags)
02263 {
02264    struct ast_conf_user *user = obj;
02265    /* actual pointer contents of check_admin_arg is irrelevant */
02266 
02267    if (!check_admin_arg || !ast_test_flag64(&user->userflags, CONFFLAG_ADMIN)) {
02268       user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED | ADMINFLAG_T_REQUEST);
02269    }
02270    return 0;
02271 }
02272 
02273 static int user_set_muted_cb(void *obj, void *check_admin_arg, int flags)
02274 {
02275    struct ast_conf_user *user = obj;
02276    /* actual pointer contents of check_admin_arg is irrelevant */
02277 
02278    if (!check_admin_arg || !ast_test_flag64(&user->userflags, CONFFLAG_ADMIN)) {
02279       user->adminflags |= ADMINFLAG_MUTED;
02280    }
02281    return 0;
02282 }
02283 
02284 static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struct ast_flags64 *confflags, char *optargs[])
02285 {
02286    struct ast_conf_user *user = NULL;
02287    int fd;
02288    struct dahdi_confinfo dahdic, dahdic_empty;
02289    struct ast_frame *f;
02290    struct ast_channel *c;
02291    struct ast_frame fr;
02292    int outfd;
02293    int ms;
02294    int nfds;
02295    int res;
02296    int retrydahdi;
02297    int origfd;
02298    int musiconhold = 0, mohtempstopped = 0;
02299    int firstpass = 0;
02300    int lastmarked = 0;
02301    int currentmarked = 0;
02302    int ret = -1;
02303    int x;
02304    int menu_active = 0;
02305    int menu8_active = 0;
02306    int talkreq_manager = 0;
02307    int using_pseudo = 0;
02308    int duration = 20;
02309    int sent_event = 0;
02310    int checked = 0;
02311    int announcement_played = 0;
02312    struct timeval now;
02313    struct ast_dsp *dsp = NULL;
02314    struct ast_app *agi_app;
02315    char *agifile, *mod_speex;
02316    const char *agifiledefault = "conf-background.agi", *tmpvar;
02317    char meetmesecs[30] = "";
02318    char exitcontext[AST_MAX_CONTEXT] = "";
02319    char recordingtmp[AST_MAX_EXTENSION] = "";
02320    char members[10] = "";
02321    int dtmf, opt_waitmarked_timeout = 0;
02322    time_t timeout = 0;
02323    struct dahdi_bufferinfo bi;
02324    char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
02325    char *buf = __buf + AST_FRIENDLY_OFFSET;
02326    char *exitkeys = NULL;
02327    unsigned int calldurationlimit = 0;
02328    long timelimit = 0;
02329    long play_warning = 0;
02330    long warning_freq = 0;
02331    const char *warning_sound = NULL;
02332    const char *end_sound = NULL;
02333    char *parse;   
02334    long time_left_ms = 0;
02335    struct timeval nexteventts = { 0, };
02336    int to;
02337    int setusercount = 0;
02338    int confsilence = 0, totalsilence = 0;
02339 
02340    if (!(user = ao2_alloc(sizeof(*user), NULL))) {
02341       return ret;
02342    }
02343 
02344    /* Possible timeout waiting for marked user */
02345    if (ast_test_flag64(confflags, CONFFLAG_WAITMARKED) &&
02346       !ast_strlen_zero(optargs[OPT_ARG_WAITMARKED]) &&
02347       (sscanf(optargs[OPT_ARG_WAITMARKED], "%30d", &opt_waitmarked_timeout) == 1) &&
02348       (opt_waitmarked_timeout > 0)) {
02349       timeout = time(NULL) + opt_waitmarked_timeout;
02350    }
02351       
02352    if (ast_test_flag64(confflags, CONFFLAG_DURATION_STOP) && !ast_strlen_zero(optargs[OPT_ARG_DURATION_STOP])) {
02353       calldurationlimit = atoi(optargs[OPT_ARG_DURATION_STOP]);
02354       ast_verb(3, "Setting call duration limit to %d seconds.\n", calldurationlimit);
02355    }
02356    
02357    if (ast_test_flag64(confflags, CONFFLAG_DURATION_LIMIT) && !ast_strlen_zero(optargs[OPT_ARG_DURATION_LIMIT])) {
02358       char *limit_str, *warning_str, *warnfreq_str;
02359       const char *var;
02360  
02361       parse = optargs[OPT_ARG_DURATION_LIMIT];
02362       limit_str = strsep(&parse, ":");
02363       warning_str = strsep(&parse, ":");
02364       warnfreq_str = parse;
02365  
02366       timelimit = atol(limit_str);
02367       if (warning_str)
02368          play_warning = atol(warning_str);
02369       if (warnfreq_str)
02370          warning_freq = atol(warnfreq_str);
02371  
02372       if (!timelimit) {
02373          timelimit = play_warning = warning_freq = 0;
02374          warning_sound = NULL;
02375       } else if (play_warning > timelimit) {       
02376          if (!warning_freq) {
02377             play_warning = 0;
02378          } else {
02379             while (play_warning > timelimit)
02380                play_warning -= warning_freq;
02381             if (play_warning < 1)
02382                play_warning = warning_freq = 0;
02383          }
02384       }
02385 
02386       ast_verb(3, "Setting conference duration limit to: %ldms.\n", timelimit);
02387       if (play_warning) {
02388          ast_verb(3, "Setting warning time to %ldms from the conference duration limit.\n", play_warning);
02389       }
02390       if (warning_freq) {
02391          ast_verb(3, "Setting warning frequency to %ldms.\n", warning_freq);
02392       }
02393 
02394       ast_channel_lock(chan);
02395       if ((var = pbx_builtin_getvar_helper(chan, "CONF_LIMIT_WARNING_FILE"))) {
02396          var = ast_strdupa(var);
02397       }
02398       ast_channel_unlock(chan);
02399 
02400       warning_sound = var ? var : "timeleft";
02401       
02402       ast_channel_lock(chan);
02403       if ((var = pbx_builtin_getvar_helper(chan, "CONF_LIMIT_TIMEOUT_FILE"))) {
02404          var = ast_strdupa(var);
02405       }
02406       ast_channel_unlock(chan);
02407       
02408       end_sound = var ? var : NULL;
02409          
02410       /* undo effect of S(x) in case they are both used */
02411       calldurationlimit = 0;
02412       /* more efficient do it like S(x) does since no advanced opts */
02413       if (!play_warning && !end_sound && timelimit) { 
02414          calldurationlimit = timelimit / 1000;
02415          timelimit = play_warning = warning_freq = 0;
02416       } else {
02417          ast_debug(2, "Limit Data for this call:\n");
02418          ast_debug(2, "- timelimit     = %ld\n", timelimit);
02419          ast_debug(2, "- play_warning  = %ld\n", play_warning);
02420          ast_debug(2, "- warning_freq  = %ld\n", warning_freq);
02421          ast_debug(2, "- warning_sound = %s\n", warning_sound ? warning_sound : "UNDEF");
02422          ast_debug(2, "- end_sound     = %s\n", end_sound ? end_sound : "UNDEF");
02423       }
02424    }
02425 
02426    /* Get exit keys */
02427    if (ast_test_flag64(confflags, CONFFLAG_KEYEXIT)) {
02428       if (!ast_strlen_zero(optargs[OPT_ARG_EXITKEYS]))
02429          exitkeys = ast_strdupa(optargs[OPT_ARG_EXITKEYS]);
02430       else
02431          exitkeys = ast_strdupa("#"); /* Default */
02432    }
02433    
02434    if (ast_test_flag64(confflags, CONFFLAG_RECORDCONF)) {
02435       if (!conf->recordingfilename) {
02436          const char *var;
02437          ast_channel_lock(chan);
02438          if ((var = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFILE"))) {
02439             conf->recordingfilename = ast_strdup(var);
02440          }
02441          if ((var = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFORMAT"))) {
02442             conf->recordingformat = ast_strdup(var);
02443          }
02444          ast_channel_unlock(chan);
02445          if (!conf->recordingfilename) {
02446             snprintf(recordingtmp, sizeof(recordingtmp), "meetme-conf-rec-%s-%s", conf->confno, chan->uniqueid);
02447             conf->recordingfilename = ast_strdup(recordingtmp);
02448          }
02449          if (!conf->recordingformat) {
02450             conf->recordingformat = ast_strdup("wav");
02451          }
02452          ast_verb(4, "Starting recording of MeetMe Conference %s into file %s.%s.\n",
02453                 conf->confno, conf->recordingfilename, conf->recordingformat);
02454       }
02455    }
02456 
02457    ast_mutex_lock(&conf->recordthreadlock);
02458    if ((conf->recordthread == AST_PTHREADT_NULL) && ast_test_flag64(confflags, CONFFLAG_RECORDCONF) &&
02459       ((conf->lchan = ast_request("DAHDI", AST_FORMAT_SLINEAR, chan, "pseudo", NULL)))) {
02460       ast_set_read_format(conf->lchan, AST_FORMAT_SLINEAR);
02461       ast_set_write_format(conf->lchan, AST_FORMAT_SLINEAR);
02462       dahdic.chan = 0;
02463       dahdic.confno = conf->dahdiconf;
02464       dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
02465       if (ioctl(conf->lchan->fds[0], DAHDI_SETCONF, &dahdic)) {
02466          ast_log(LOG_WARNING, "Error starting listen channel\n");
02467          ast_hangup(conf->lchan);
02468          conf->lchan = NULL;
02469       } else {
02470          ast_pthread_create_detached_background(&conf->recordthread, NULL, recordthread, conf);
02471       }
02472    }
02473    ast_mutex_unlock(&conf->recordthreadlock);
02474 
02475    ast_mutex_lock(&conf->announcethreadlock);
02476    if ((conf->announcethread == AST_PTHREADT_NULL) && !ast_test_flag64(confflags, CONFFLAG_QUIET) &&
02477       (ast_test_flag64(confflags, CONFFLAG_INTROUSER) || ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW))) {
02478       ast_mutex_init(&conf->announcelistlock);
02479       AST_LIST_HEAD_INIT_NOLOCK(&conf->announcelist);
02480       ast_pthread_create_background(&conf->announcethread, NULL, announce_thread, conf);
02481    }
02482    ast_mutex_unlock(&conf->announcethreadlock);
02483 
02484    time(&user->jointime);
02485    
02486    user->timelimit = timelimit;
02487    user->play_warning = play_warning;
02488    user->warning_freq = warning_freq;
02489    user->warning_sound = warning_sound;
02490    user->end_sound = end_sound;  
02491    
02492    if (calldurationlimit > 0) {
02493       time(&user->kicktime);
02494       user->kicktime = user->kicktime + calldurationlimit;
02495    }
02496    
02497    if (ast_tvzero(user->start_time))
02498       user->start_time = ast_tvnow();
02499    time_left_ms = user->timelimit;
02500    
02501    if (user->timelimit) {
02502       nexteventts = ast_tvadd(user->start_time, ast_samp2tv(user->timelimit, 1000));
02503       nexteventts = ast_tvsub(nexteventts, ast_samp2tv(user->play_warning, 1000));
02504    }
02505 
02506    if (conf->locked && (!ast_test_flag64(confflags, CONFFLAG_ADMIN))) {
02507       /* Sorry, but this conference is locked! */  
02508       if (!ast_streamfile(chan, "conf-locked", chan->language))
02509          ast_waitstream(chan, "");
02510       goto outrun;
02511    }
02512 
02513       ast_mutex_lock(&conf->playlock);
02514 
02515    if (rt_schedule && conf->maxusers) {
02516       if (conf->users >= conf->maxusers) {
02517          /* Sorry, but this confernce has reached the participant limit! */   
02518          if (!ast_streamfile(chan, "conf-full", chan->language))
02519             ast_waitstream(chan, "");
02520          ast_mutex_unlock(&conf->playlock);
02521          goto outrun;
02522       }
02523    }
02524 
02525    ao2_lock(conf->usercontainer);
02526    ao2_callback(conf->usercontainer, OBJ_NODATA, user_max_cmp, &user->user_no);
02527    user->user_no++;
02528    ao2_link(conf->usercontainer, user);
02529    ao2_unlock(conf->usercontainer);
02530 
02531    user->chan = chan;
02532    user->userflags = *confflags;
02533    user->adminflags = ast_test_flag64(confflags, CONFFLAG_STARTMUTED) ? ADMINFLAG_SELFMUTED : 0;
02534    user->adminflags |= (conf->gmuted) ? ADMINFLAG_MUTED : 0;
02535    user->talking = -1;
02536 
02537    ast_mutex_unlock(&conf->playlock);
02538 
02539    if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && (ast_test_flag64(confflags, CONFFLAG_INTROUSER) ||
02540       ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW))) {
02541       char destdir[PATH_MAX];
02542 
02543       snprintf(destdir, sizeof(destdir), "%s/meetme", ast_config_AST_SPOOL_DIR);
02544 
02545       if (ast_mkdir(destdir, 0777) != 0) {
02546          ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", destdir, strerror(errno));
02547          goto outrun;
02548       }
02549 
02550       snprintf(user->namerecloc, sizeof(user->namerecloc),
02551           "%s/meetme-username-%s-%d", destdir,
02552           conf->confno, user->user_no);
02553       if (ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW))
02554          res = ast_play_and_record(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, NULL, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL);
02555       else
02556          res = ast_record_review(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, NULL);
02557       if (res == -1)
02558          goto outrun;
02559    }
02560 
02561    ast_mutex_lock(&conf->playlock);
02562 
02563    if (ast_test_flag64(confflags, CONFFLAG_MARKEDUSER))
02564       conf->markedusers++;
02565    conf->users++;
02566    if (rt_log_members) {
02567       /* Update table */
02568       snprintf(members, sizeof(members), "%d", conf->users);
02569       ast_realtime_require_field("meetme",
02570          "confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
02571          "members", RQ_UINTEGER1, strlen(members),
02572          NULL);
02573       ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
02574    }
02575    setusercount = 1;
02576 
02577    /* This device changed state now - if this is the first user */
02578    if (conf->users == 1)
02579       ast_devstate_changed(AST_DEVICE_INUSE, (conf->isdynamic ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), "meetme:%s", conf->confno);
02580 
02581    ast_mutex_unlock(&conf->playlock);
02582 
02583    /* return the unique ID of the conference */
02584    pbx_builtin_setvar_helper(chan, "MEETMEUNIQUEID", conf->uniqueid);
02585 
02586    if (ast_test_flag64(confflags, CONFFLAG_EXIT_CONTEXT)) {
02587       ast_channel_lock(chan);
02588       if ((tmpvar = pbx_builtin_getvar_helper(chan, "MEETME_EXIT_CONTEXT"))) {
02589          ast_copy_string(exitcontext, tmpvar, sizeof(exitcontext));
02590       } else if (!ast_strlen_zero(chan->macrocontext)) {
02591          ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext));
02592       } else {
02593          ast_copy_string(exitcontext, chan->context, sizeof(exitcontext));
02594       }
02595       ast_channel_unlock(chan);
02596    }
02597 
02598    /* Play an arbitrary intro message */
02599    if (ast_test_flag64(confflags, CONFFLAG_INTROMSG) &&
02600          !ast_strlen_zero(optargs[OPT_ARG_INTROMSG])) {
02601       if (!ast_streamfile(chan, optargs[OPT_ARG_INTROMSG], chan->language)) {
02602          ast_waitstream(chan, "");
02603       }
02604    }
02605 
02606    if (!ast_test_flag64(confflags, (CONFFLAG_QUIET | CONFFLAG_NOONLYPERSON))) {
02607       if (conf->users == 1 && !ast_test_flag64(confflags, CONFFLAG_WAITMARKED))
02608          if (!ast_streamfile(chan, "conf-onlyperson", chan->language))
02609             ast_waitstream(chan, "");
02610       if (ast_test_flag64(confflags, CONFFLAG_WAITMARKED) && conf->markedusers == 0)
02611          if (!ast_streamfile(chan, "conf-waitforleader", chan->language))
02612             ast_waitstream(chan, "");
02613    }
02614 
02615    if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && ast_test_flag64(confflags, CONFFLAG_ANNOUNCEUSERCOUNT) &&
02616       conf->users > 1) {
02617       int keepplaying = 1;
02618 
02619       if (conf->users == 2) { 
02620          if (!ast_streamfile(chan, "conf-onlyone", chan->language)) {
02621             res = ast_waitstream(chan, AST_DIGIT_ANY);
02622             ast_stopstream(chan);
02623             if (res > 0)
02624                keepplaying = 0;
02625             else if (res == -1)
02626                goto outrun;
02627          }
02628       } else { 
02629          if (!ast_streamfile(chan, "conf-thereare", chan->language)) {
02630             res = ast_waitstream(chan, AST_DIGIT_ANY);
02631             ast_stopstream(chan);
02632             if (res > 0)
02633                keepplaying = 0;
02634             else if (res == -1)
02635                goto outrun;
02636          }
02637          if (keepplaying) {
02638             res = ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
02639             if (res > 0)
02640                keepplaying = 0;
02641             else if (res == -1)
02642                goto outrun;
02643          }
02644          if (keepplaying && !ast_streamfile(chan, "conf-otherinparty", chan->language)) {
02645             res = ast_waitstream(chan, AST_DIGIT_ANY);
02646             ast_stopstream(chan);
02647             if (res > 0)
02648                keepplaying = 0;
02649             else if (res == -1) 
02650                goto outrun;
02651          }
02652       }
02653    }
02654 
02655    if (!ast_test_flag64(confflags, CONFFLAG_NO_AUDIO_UNTIL_UP)) {
02656       /* We're leaving this alone until the state gets changed to up */
02657       ast_indicate(chan, -1);
02658    }
02659 
02660    if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
02661       ast_log(LOG_WARNING, "Unable to set '%s' to write linear mode\n", chan->name);
02662       goto outrun;
02663    }
02664 
02665    if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
02666       ast_log(LOG_WARNING, "Unable to set '%s' to read linear mode\n", chan->name);
02667       goto outrun;
02668    }
02669 
02670    /* Reduce background noise from each participant */
02671    if ((mod_speex = ast_module_helper("", "codec_speex", 0, 0, 0, 0))) {
02672       ast_free(mod_speex);
02673       ast_func_write(chan, "DENOISE(rx)", "on");
02674    }
02675 
02676    retrydahdi = (strcasecmp(chan->tech->type, "DAHDI") || (chan->audiohooks || chan->monitor) ? 1 : 0);
02677    user->dahdichannel = !retrydahdi;
02678 
02679  dahdiretry:
02680    origfd = chan->fds[0];
02681    if (retrydahdi) {
02682       /* open pseudo in non-blocking mode */
02683       fd = open("/dev/dahdi/pseudo", O_RDWR | O_NONBLOCK);
02684       if (fd < 0) {
02685          ast_log(LOG_WARNING, "Unable to open DAHDI pseudo channel: %s\n", strerror(errno));
02686          goto outrun;
02687       }
02688       using_pseudo = 1;
02689       /* Setup buffering information */
02690       memset(&bi, 0, sizeof(bi));
02691       bi.bufsize = CONF_SIZE / 2;
02692       bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
02693       bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
02694       bi.numbufs = audio_buffers;
02695       if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
02696          ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
02697          close(fd);
02698          goto outrun;
02699       }
02700       x = 1;
02701       if (ioctl(fd, DAHDI_SETLINEAR, &x)) {
02702          ast_log(LOG_WARNING, "Unable to set linear mode: %s\n", strerror(errno));
02703          close(fd);
02704          goto outrun;
02705       }
02706       nfds = 1;
02707    } else {
02708       /* XXX Make sure we're not running on a pseudo channel XXX */
02709       fd = chan->fds[0];
02710       nfds = 0;
02711    }
02712    memset(&dahdic, 0, sizeof(dahdic));
02713    memset(&dahdic_empty, 0, sizeof(dahdic_empty));
02714    /* Check to see if we're in a conference... */
02715    dahdic.chan = 0;  
02716    if (ioctl(fd, DAHDI_GETCONF, &dahdic)) {
02717       ast_log(LOG_WARNING, "Error getting conference\n");
02718       close(fd);
02719       goto outrun;
02720    }
02721    if (dahdic.confmode) {
02722       /* Whoa, already in a conference...  Retry... */
02723       if (!retrydahdi) {
02724          ast_debug(1, "DAHDI channel is in a conference already, retrying with pseudo\n");
02725          retrydahdi = 1;
02726          goto dahdiretry;
02727       }
02728    }
02729    memset(&dahdic, 0, sizeof(dahdic));
02730    /* Add us to the conference */
02731    dahdic.chan = 0;  
02732    dahdic.confno = conf->dahdiconf;
02733 
02734    if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && (ast_test_flag64(confflags, CONFFLAG_INTROUSER) ||
02735       ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW)) && conf->users > 1) {
02736       struct announce_listitem *item;
02737       if (!(item = ao2_alloc(sizeof(*item), NULL)))
02738          goto outrun;
02739       ast_copy_string(item->namerecloc, user->namerecloc, sizeof(item->namerecloc));
02740       ast_copy_string(item->language, chan->language, sizeof(item->language));
02741       item->confchan = conf->chan;
02742       item->confusers = conf->users;
02743       item->announcetype = CONF_HASJOIN;
02744       ast_mutex_lock(&conf->announcelistlock);
02745       ao2_ref(item, +1); /* add one more so we can determine when announce_thread is done playing it */
02746       AST_LIST_INSERT_TAIL(&conf->announcelist, item, entry);
02747       ast_cond_signal(&conf->announcelist_addition);
02748       ast_mutex_unlock(&conf->announcelistlock);
02749 
02750       while (!ast_check_hangup(conf->chan) && ao2_ref(item, 0) == 2 && !ast_safe_sleep(chan, 1000)) {
02751          ;
02752       }
02753       ao2_ref(item, -1);
02754    }
02755 
02756    if (ast_test_flag64(confflags, CONFFLAG_WAITMARKED) && !conf->markedusers)
02757       dahdic.confmode = DAHDI_CONF_CONF;
02758    else if (ast_test_flag64(confflags, CONFFLAG_MONITOR))
02759       dahdic.confmode = DAHDI_CONF_CONFMON | DAHDI_CONF_LISTENER;
02760    else if (ast_test_flag64(confflags, CONFFLAG_TALKER))
02761       dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER;
02762    else 
02763       dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
02764 
02765    if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
02766       ast_log(LOG_WARNING, "Error setting conference\n");
02767       close(fd);
02768       goto outrun;
02769    }
02770    ast_debug(1, "Placed channel %s in DAHDI conf %d\n", chan->name, conf->dahdiconf);
02771 
02772    if (!sent_event) {
02773       ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeJoin",
02774          "Channel: %s\r\n"
02775          "Uniqueid: %s\r\n"
02776          "Meetme: %s\r\n"
02777          "Usernum: %d\r\n"
02778          "CallerIDnum: %s\r\n"
02779          "CallerIDname: %s\r\n"
02780          "ConnectedLineNum: %s\r\n"
02781          "ConnectedLineName: %s\r\n",
02782          chan->name, chan->uniqueid, conf->confno,
02783          user->user_no,
02784          S_COR(user->chan->caller.id.number.valid, user->chan->caller.id.number.str, "<unknown>"),
02785          S_COR(user->chan->caller.id.name.valid, user->chan->caller.id.name.str, "<unknown>"),
02786          S_COR(user->chan->connected.id.number.valid, user->chan->connected.id.number.str, "<unknown>"),
02787          S_COR(user->chan->connected.id.name.valid, user->chan->connected.id.name.str, "<unknown>")
02788          );
02789       sent_event = 1;
02790    }
02791 
02792    if (!firstpass && !ast_test_flag64(confflags, CONFFLAG_MONITOR) &&
02793       !ast_test_flag64(confflags, CONFFLAG_ADMIN)) {
02794       firstpass = 1;
02795       if (!ast_test_flag64(confflags, CONFFLAG_QUIET))
02796          if (!ast_test_flag64(confflags, CONFFLAG_WAITMARKED) || (ast_test_flag64(confflags, CONFFLAG_MARKEDUSER) &&
02797             (conf->markedusers >= 1))) {
02798             conf_play(chan, conf, ENTER);
02799          }
02800    }
02801 
02802    conf_flush(fd, chan);
02803 
02804    if (dsp)
02805       ast_dsp_free(dsp);
02806 
02807    if (!(dsp = ast_dsp_new())) {
02808       ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
02809       res = -1;
02810    }
02811 
02812    if (ast_test_flag64(confflags, CONFFLAG_AGI)) {
02813       /* Get name of AGI file to run from $(MEETME_AGI_BACKGROUND)
02814          or use default filename of conf-background.agi */
02815 
02816       ast_channel_lock(chan);
02817       if ((tmpvar = pbx_builtin_getvar_helper(chan, "MEETME_AGI_BACKGROUND"))) {
02818          agifile = ast_strdupa(tmpvar);
02819       } else {
02820          agifile = ast_strdupa(agifiledefault);
02821       }
02822       ast_channel_unlock(chan);
02823       
02824       if (user->dahdichannel) {
02825          /*  Set CONFMUTE mode on DAHDI channel to mute DTMF tones */
02826          x = 1;
02827          ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
02828       }
02829       /* Find a pointer to the agi app and execute the script */
02830       agi_app = pbx_findapp("agi");
02831       if (agi_app) {
02832          ret = pbx_exec(chan, agi_app, agifile);
02833       } else {
02834          ast_log(LOG_WARNING, "Could not find application (agi)\n");
02835          ret = -2;
02836       }
02837       if (user->dahdichannel) {
02838          /*  Remove CONFMUTE mode on DAHDI channel */
02839          x = 0;
02840          ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
02841       }
02842    } else {
02843       int lastusers = conf->users;
02844       if (user->dahdichannel && ast_test_flag64(confflags, CONFFLAG_STARMENU)) {
02845          /*  Set CONFMUTE mode on DAHDI channel to mute DTMF tones when the menu is enabled */
02846          x = 1;
02847          ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
02848       }  
02849       for (;;) {
02850          int menu_was_active = 0;
02851 
02852          outfd = -1;
02853          ms = -1;
02854          now = ast_tvnow();
02855 
02856          if (rt_schedule && conf->endtime) {
02857             char currenttime[32];
02858             long localendtime = 0;
02859             int extended = 0;
02860             struct ast_tm tm;
02861             struct ast_variable *var, *origvar;
02862             struct timeval tmp;
02863 
02864             if (now.tv_sec % 60 == 0) {
02865                if (!checked) {
02866                   ast_localtime(&now, &tm, NULL);
02867                   ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
02868                   var = origvar = ast_load_realtime("meetme", "confno",
02869                      conf->confno, "starttime <=", currenttime,
02870                       "endtime >=", currenttime, NULL);
02871 
02872                   for ( ; var; var = var->next) {
02873                      if (!strcasecmp(var->name, "endtime")) {
02874                         struct ast_tm endtime_tm;
02875                         ast_strptime(var->value, "%Y-%m-%d %H:%M:%S", &endtime_tm);
02876                         tmp = ast_mktime(&endtime_tm, NULL);
02877                         localendtime = tmp.tv_sec;
02878                      }
02879                   }
02880                   ast_variables_destroy(origvar);
02881 
02882                   /* A conference can be extended from the
02883                      Admin/User menu or by an external source */
02884                   if (localendtime > conf->endtime){
02885                      conf->endtime = localendtime;
02886                      extended = 1;
02887                   }
02888 
02889                   if (conf->endtime && (now.tv_sec >= conf->endtime)) {
02890                      ast_verbose("Quitting time...\n");
02891                      goto outrun;
02892                   }
02893 
02894                   if (!announcement_played && conf->endalert) {
02895                      if (now.tv_sec + conf->endalert >= conf->endtime) {
02896                         if (!ast_streamfile(chan, "conf-will-end-in", chan->language))
02897                            ast_waitstream(chan, "");
02898                         ast_say_digits(chan, (conf->endtime - now.tv_sec) / 60, "", chan->language);
02899                         if (!ast_streamfile(chan, "minutes", chan->language))
02900                            ast_waitstream(chan, "");
02901                         if (musiconhold) {
02902                            conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
02903                         }
02904                         announcement_played = 1;
02905                      }
02906                   }
02907 
02908                   if (extended) {
02909                      announcement_played = 0;
02910                   }
02911 
02912                   checked = 1;
02913                }
02914             } else {
02915                checked = 0;
02916             }
02917          }
02918 
02919          if (user->kicktime && (user->kicktime <= now.tv_sec)) {
02920             if (ast_test_flag64(confflags, CONFFLAG_KICK_CONTINUE)) {
02921                ret = 0;
02922             } else {
02923                ret = -1;
02924             }
02925             break;
02926          }
02927   
02928          to = -1;
02929          if (user->timelimit) {
02930             int minutes = 0, seconds = 0, remain = 0;
02931  
02932             to = ast_tvdiff_ms(nexteventts, now);
02933             if (to < 0) {
02934                to = 0;
02935             }
02936             time_left_ms = user->timelimit - ast_tvdiff_ms(now, user->start_time);
02937             if (time_left_ms < to) {
02938                to = time_left_ms;
02939             }
02940    
02941             if (time_left_ms <= 0) {
02942                if (user->end_sound) {                 
02943                   res = ast_streamfile(chan, user->end_sound, chan->language);
02944                   res = ast_waitstream(chan, "");
02945                }
02946                if (ast_test_flag64(confflags, CONFFLAG_KICK_CONTINUE)) {
02947                   ret = 0;
02948                } else {
02949                   ret = -1;
02950                }
02951                break;
02952             }
02953             
02954             if (!to) {
02955                if (time_left_ms >= 5000) {                  
02956                   
02957                   remain = (time_left_ms + 500) / 1000;
02958                   if (remain / 60 >= 1) {
02959                      minutes = remain / 60;
02960                      seconds = remain % 60;
02961                   } else {
02962                      seconds = remain;
02963                   }
02964                   
02965                   /* force the time left to round up if appropriate */
02966                   if (user->warning_sound && user->play_warning) {
02967                      if (!strcmp(user->warning_sound, "timeleft")) {
02968                         
02969                         res = ast_streamfile(chan, "vm-youhave", chan->language);
02970                         res = ast_waitstream(chan, "");
02971                         if (minutes) {
02972                            res = ast_say_number(chan, minutes, AST_DIGIT_ANY, chan->language, (char *) NULL);
02973                            res = ast_streamfile(chan, "queue-minutes", chan->language);
02974                            res = ast_waitstream(chan, "");
02975                         }
02976                         if (seconds) {
02977                            res = ast_say_number(chan, seconds, AST_DIGIT_ANY, chan->language, (char *) NULL);
02978                            res = ast_streamfile(chan, "queue-seconds", chan->language);
02979                            res = ast_waitstream(chan, "");
02980                         }
02981                      } else {
02982                         res = ast_streamfile(chan, user->warning_sound, chan->language);
02983                         res = ast_waitstream(chan, "");
02984                      }
02985                      if (musiconhold) {
02986                         conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
02987                      }
02988                   }
02989                }
02990                if (user->warning_freq) {
02991                   nexteventts = ast_tvadd(nexteventts, ast_samp2tv(user->warning_freq, 1000));
02992                } else {
02993                   nexteventts = ast_tvadd(user->start_time, ast_samp2tv(user->timelimit, 1000));
02994                }
02995             }
02996          }
02997 
02998          now = ast_tvnow();
02999          if (timeout && now.tv_sec >= timeout) {
03000             if (ast_test_flag64(confflags, CONFFLAG_KICK_CONTINUE)) {
03001                ret = 0;
03002             } else {
03003                ret = -1;
03004             }
03005             break;
03006          }
03007 
03008          /* if we have just exited from the menu, and the user had a channel-driver
03009             volume adjustment, restore it
03010          */
03011          if (!menu_active && menu_was_active && user->listen.desired && !user->listen.actual) {
03012             set_talk_volume(user, user->listen.desired);
03013          }
03014 
03015          menu_was_active = menu_active;
03016 
03017          currentmarked = conf->markedusers;
03018          if (!ast_test_flag64(confflags, CONFFLAG_QUIET) &&
03019              ast_test_flag64(confflags, CONFFLAG_MARKEDUSER) &&
03020              ast_test_flag64(confflags, CONFFLAG_WAITMARKED) &&
03021              lastmarked == 0) {
03022             if (currentmarked == 1 && conf->users > 1) {
03023                ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
03024                if (conf->users - 1 == 1) {
03025                   if (!ast_streamfile(chan, "conf-userwilljoin", chan->language)) {
03026                      ast_waitstream(chan, "");
03027                   }
03028                } else {
03029                   if (!ast_streamfile(chan, "conf-userswilljoin", chan->language)) {
03030                      ast_waitstream(chan, "");
03031                   }
03032                }
03033             }
03034             if (conf->users == 1 && !ast_test_flag64(confflags, CONFFLAG_MARKEDUSER)) {
03035                if (!ast_streamfile(chan, "conf-onlyperson", chan->language)) {
03036                   ast_waitstream(chan, "");
03037                }
03038             }
03039          }
03040 
03041          /* Update the struct with the actual confflags */
03042          user->userflags = *confflags;
03043 
03044          if (ast_test_flag64(confflags, CONFFLAG_WAITMARKED)) {
03045             if (currentmarked == 0) {
03046                if (lastmarked != 0) {
03047                   if (!ast_test_flag64(confflags, CONFFLAG_QUIET)) {
03048                      if (!ast_streamfile(chan, "conf-leaderhasleft", chan->language)) {
03049                         ast_waitstream(chan, "");
03050                      }
03051                   }
03052                   if (ast_test_flag64(confflags, CONFFLAG_MARKEDEXIT)) {
03053                      if (ast_test_flag64(confflags, CONFFLAG_KICK_CONTINUE)) {
03054                         ret = 0;
03055                      }
03056                      break;
03057                   } else {
03058                      dahdic.confmode = DAHDI_CONF_CONF;
03059                      if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
03060                         ast_log(LOG_WARNING, "Error setting conference\n");
03061                         close(fd);
03062                         goto outrun;
03063                      }
03064                   }
03065                }
03066                if (!musiconhold && (ast_test_flag64(confflags, CONFFLAG_MOH))) {
03067                   conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
03068                   musiconhold = 1;
03069                }
03070             } else if (currentmarked >= 1 && lastmarked == 0) {
03071                /* Marked user entered, so cancel timeout */
03072                timeout = 0;
03073                if (ast_test_flag64(confflags, CONFFLAG_MONITOR)) {
03074                   dahdic.confmode = DAHDI_CONF_CONFMON | DAHDI_CONF_LISTENER;
03075                } else if (ast_test_flag64(confflags, CONFFLAG_TALKER)) {
03076                   dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER;
03077                } else {
03078                   dahdic.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
03079                }
03080                if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
03081                   ast_log(LOG_WARNING, "Error setting conference\n");
03082                   close(fd);
03083                   goto outrun;
03084                }
03085                if (musiconhold && (ast_test_flag64(confflags, CONFFLAG_MOH))) {
03086                   ast_moh_stop(chan);
03087                   musiconhold = 0;
03088                }
03089                if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && 
03090                   !ast_test_flag64(confflags, CONFFLAG_MARKEDUSER)) {
03091                   if (!ast_streamfile(chan, "conf-placeintoconf", chan->language)) {
03092                      ast_waitstream(chan, "");
03093                   }
03094                   conf_play(chan, conf, ENTER);
03095                }
03096             }
03097          }
03098 
03099          /* trying to add moh for single person conf */
03100          if (ast_test_flag64(confflags, CONFFLAG_MOH) && !ast_test_flag64(confflags, CONFFLAG_WAITMARKED)) {
03101             if (conf->users == 1) {
03102                if (!musiconhold) {
03103                   conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
03104                   musiconhold = 1;
03105                } 
03106             } else {
03107                if (musiconhold) {
03108                   ast_moh_stop(chan);
03109                   musiconhold = 0;
03110                }
03111             }
03112          }
03113          
03114          /* Leave if the last marked user left */
03115          if (currentmarked == 0 && lastmarked != 0 && ast_test_flag64(confflags, CONFFLAG_MARKEDEXIT)) {
03116             if (ast_test_flag64(confflags, CONFFLAG_KICK_CONTINUE)) {
03117                ret = 0;
03118             } else {
03119                ret = -1;
03120             }
03121             break;
03122          }
03123 
03124          /* Throw a TestEvent if a user exit did not cause this user to leave the conference */
03125          if (conf->users != lastusers) {
03126             if (conf->users < lastusers) {
03127                ast_test_suite_event_notify("NOEXIT", "Message: CONFFLAG_MARKEDEXIT\r\nLastUsers: %d\r\nUsers: %d", lastusers, conf->users);
03128             }
03129             lastusers = conf->users;
03130          }
03131 
03132          /* Check if my modes have changed */
03133 
03134          /* If I should be muted but am still talker, mute me */
03135          if ((user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && (dahdic.confmode & DAHDI_CONF_TALKER)) {
03136             dahdic.confmode ^= DAHDI_CONF_TALKER;
03137             if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
03138                ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
03139                ret = -1;
03140                break;
03141             }
03142 
03143             /* Indicate user is not talking anymore - change him to unmonitored state */
03144             if (ast_test_flag64(confflags,  (CONFFLAG_MONITORTALKER | CONFFLAG_OPTIMIZETALKER))) {
03145                set_user_talking(chan, conf, user, -1, ast_test_flag64(confflags, CONFFLAG_MONITORTALKER));
03146             }
03147 
03148             ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeMute",
03149                "Channel: %s\r\n"
03150                "Uniqueid: %s\r\n"
03151                "Meetme: %s\r\n"
03152                "Usernum: %d\r\n"
03153                "Status: on\r\n",
03154                chan->name, chan->uniqueid, conf->confno, user->user_no);
03155          }
03156 
03157          /* If I should be un-muted but am not talker, un-mute me */
03158          if (!(user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && !ast_test_flag64(confflags, CONFFLAG_MONITOR) && !(dahdic.confmode & DAHDI_CONF_TALKER)) {
03159             dahdic.confmode |= DAHDI_CONF_TALKER;
03160             if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
03161                ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
03162                ret = -1;
03163                break;
03164             }
03165 
03166             ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeMute",
03167                "Channel: %s\r\n"
03168                "Uniqueid: %s\r\n"
03169                "Meetme: %s\r\n"
03170                "Usernum: %d\r\n"
03171                "Status: off\r\n",
03172                chan->name, chan->uniqueid, conf->confno, user->user_no);
03173          }
03174          
03175          if ((user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && 
03176             (user->adminflags & ADMINFLAG_T_REQUEST) && !(talkreq_manager)) {
03177             talkreq_manager = 1;
03178 
03179             ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeTalkRequest",
03180                "Channel: %s\r\n"
03181                "Uniqueid: %s\r\n"
03182                "Meetme: %s\r\n"
03183                "Usernum: %d\r\n"
03184                "Status: on\r\n",
03185                chan->name, chan->uniqueid, conf->confno, user->user_no);
03186          }
03187 
03188          
03189          if (!(user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && 
03190             !(user->adminflags & ADMINFLAG_T_REQUEST) && (talkreq_manager)) {
03191             talkreq_manager = 0;
03192             ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeTalkRequest",
03193                "Channel: %s\r\n"
03194                "Uniqueid: %s\r\n"
03195                "Meetme: %s\r\n"
03196                "Usernum: %d\r\n"
03197                "Status: off\r\n",
03198                chan->name, chan->uniqueid, conf->confno, user->user_no);
03199          }
03200          
03201          /* If I have been kicked, exit the conference */
03202          if (user->adminflags & ADMINFLAG_KICKME) {
03203             /* You have been kicked. */
03204             if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && 
03205                !ast_streamfile(chan, "conf-kicked", chan->language)) {
03206                ast_waitstream(chan, "");
03207             }
03208             ret = 0;
03209             break;
03210          }
03211 
03212          /* Perform a hangup check here since ast_waitfor_nandfds will not always be able to get a channel after a hangup has occurred */
03213          if (ast_check_hangup(chan)) {
03214             break;
03215          }
03216 
03217          c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
03218 
03219          if (c) {
03220             char dtmfstr[2] = "";
03221 
03222             if (c->fds[0] != origfd || (user->dahdichannel && (c->audiohooks || c->monitor))) {
03223                if (using_pseudo) {
03224                   /* Kill old pseudo */
03225                   close(fd);
03226                   using_pseudo = 0;
03227                }
03228                ast_debug(1, "Ooh, something swapped out under us, starting over\n");
03229                retrydahdi = (strcasecmp(c->tech->type, "DAHDI") || (c->audiohooks || c->monitor) ? 1 : 0);
03230                user->dahdichannel = !retrydahdi;
03231                goto dahdiretry;
03232             }
03233             if (ast_test_flag64(confflags, CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
03234                f = ast_read_noaudio(c);
03235             } else {
03236                f = ast_read(c);
03237             }
03238             if (!f) {
03239                break;
03240             }
03241             if (f->frametype == AST_FRAME_DTMF) {
03242                dtmfstr[0] = f->subclass.integer;
03243                dtmfstr[1] = '\0';
03244             }
03245 
03246             if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.codec == AST_FORMAT_SLINEAR)) {
03247                if (user->talk.actual) {
03248                   ast_frame_adjust_volume(f, user->talk.actual);
03249                }
03250 
03251                if (ast_test_flag64(confflags, (CONFFLAG_OPTIMIZETALKER | CONFFLAG_MONITORTALKER))) {
03252                   if (user->talking == -1) {
03253                      user->talking = 0;
03254                   }
03255 
03256                   res = ast_dsp_silence(dsp, f, &totalsilence);
03257                   if (!user->talking && totalsilence < MEETME_DELAYDETECTTALK) {
03258                      set_user_talking(chan, conf, user, 1, ast_test_flag64(confflags, CONFFLAG_MONITORTALKER));
03259                   }
03260 
03261                   if (user->talking && totalsilence > MEETME_DELAYDETECTENDTALK) {
03262                      set_user_talking(chan, conf, user, 0, ast_test_flag64(confflags, CONFFLAG_MONITORTALKER));
03263                   }
03264                }
03265                if (using_pseudo) {
03266                   /* Absolutely do _not_ use careful_write here...
03267                      it is important that we read data from the channel
03268                      as fast as it arrives, and feed it into the conference.
03269                      The buffering in the pseudo channel will take care of any
03270                      timing differences, unless they are so drastic as to lose
03271                      audio frames (in which case carefully writing would only
03272                      have delayed the audio even further).
03273                   */
03274                   /* As it turns out, we do want to use careful write.  We just
03275                      don't want to block, but we do want to at least *try*
03276                      to write out all the samples.
03277                    */
03278                   if (user->talking || !ast_test_flag64(confflags, CONFFLAG_OPTIMIZETALKER)) {
03279                      careful_write(fd, f->data.ptr, f->datalen, 0);
03280                   }
03281                }
03282             } else if (((f->frametype == AST_FRAME_DTMF) && (f->subclass.integer == '*') && ast_test_flag64(confflags, CONFFLAG_STARMENU)) || ((f->frametype == AST_FRAME_DTMF) && menu_active)) {
03283                if (ast_test_flag64(confflags, CONFFLAG_PASS_DTMF)) {
03284                   conf_queue_dtmf(conf, user, f);
03285                }
03286                if (ioctl(fd, DAHDI_SETCONF, &dahdic_empty)) {
03287                   ast_log(LOG_WARNING, "Error setting conference\n");
03288                   close(fd);
03289                   ast_frfree(f);
03290                   goto outrun;
03291                }
03292 
03293                /* if we are entering the menu, and the user has a channel-driver
03294                   volume adjustment, clear it
03295                */
03296                if (!menu_active && user->talk.desired && !user->talk.actual) {
03297                   set_talk_volume(user, 0);
03298                }
03299 
03300                if (musiconhold) {
03301                   ast_moh_stop(chan);
03302                }
03303                if (menu8_active) {
03304                   /* *8 Submenu */
03305                   dtmf = f->subclass.integer;
03306                   if (dtmf > 0) {
03307                      int keepplaying;
03308                      int playednamerec;
03309                      struct ao2_iterator user_iter;
03310                      struct ast_conf_user *usr = NULL;
03311                      switch(dtmf) {
03312                      case '1': /* *81 Roll call */
03313                         keepplaying = 1;
03314                         playednamerec = 0;
03315                         if (conf->users == 1) {
03316                            if (keepplaying && !ast_streamfile(chan, "conf-onlyperson", chan->language)) {
03317                               res = ast_waitstream(chan, AST_DIGIT_ANY);
03318                               ast_stopstream(chan);
03319                               if (res > 0)
03320                                  keepplaying = 0;
03321                            }
03322                         } else if (conf->users == 2) {
03323                            if (keepplaying && !ast_streamfile(chan, "conf-onlyone", chan->language)) {
03324                               res = ast_waitstream(chan, AST_DIGIT_ANY);
03325                               ast_stopstream(chan);
03326                               if (res > 0)
03327                                  keepplaying = 0;
03328                            }
03329                         } else {
03330                            if (keepplaying && !ast_streamfile(chan, "conf-thereare", chan->language)) {
03331                               res = ast_waitstream(chan, AST_DIGIT_ANY);
03332                               ast_stopstream(chan);
03333                               if (res > 0)
03334                                  keepplaying = 0;
03335                            }
03336                            if (keepplaying) {
03337                               res = ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
03338                               if (res > 0)
03339                                  keepplaying = 0;
03340                            }
03341                            if (keepplaying && !ast_streamfile(chan, "conf-otherinparty", chan->language)) {
03342                               res = ast_waitstream(chan, AST_DIGIT_ANY);
03343                               ast_stopstream(chan);
03344                               if (res > 0)
03345                                  keepplaying = 0;
03346                            }
03347                         }
03348                         user_iter = ao2_iterator_init(conf->usercontainer, 0);
03349                         while((usr = ao2_iterator_next(&user_iter))) {
03350                            if (ast_fileexists(usr->namerecloc, NULL, NULL)) {
03351                               if (keepplaying && !ast_streamfile(chan, usr->namerecloc, chan->language)) {
03352                                  res = ast_waitstream(chan, AST_DIGIT_ANY);
03353                                  ast_stopstream(chan);
03354                                  if (res > 0)
03355                                     keepplaying = 0;
03356                               }
03357                               playednamerec = 1;
03358                            }
03359                            ao2_ref(usr, -1);
03360                         }
03361                         ao2_iterator_destroy(&user_iter);
03362                         if (keepplaying && playednamerec && !ast_streamfile(chan, "conf-roll-callcomplete", chan->language)) {
03363                            res = ast_waitstream(chan, AST_DIGIT_ANY);
03364                            ast_stopstream(chan);
03365                            if (res > 0)
03366                               keepplaying = 0;
03367                         }
03368                         break;
03369                      case '2': /* *82 Eject all non-admins */
03370                         if (conf->users == 1) {
03371                            if(!ast_streamfile(chan, "conf-errormenu", chan->language))
03372                               ast_waitstream(chan, "");
03373                         } else {
03374                            ao2_callback(conf->usercontainer, OBJ_NODATA, user_set_kickme_cb, &conf);
03375                         }
03376                         ast_stopstream(chan);
03377                         break;
03378                      case '3': /* *83 (Admin) mute/unmute all non-admins */
03379                         if(conf->gmuted) {
03380                            conf->gmuted = 0;
03381                            ao2_callback(conf->usercontainer, OBJ_NODATA, user_set_unmuted_cb, &conf);
03382                            if (!ast_streamfile(chan, "conf-now-unmuted", chan->language))
03383                               ast_waitstream(chan, "");
03384                         } else {
03385                            conf->gmuted = 1;
03386                            ao2_callback(conf->usercontainer, OBJ_NODATA, user_set_muted_cb, &conf);
03387                            if (!ast_streamfile(chan, "conf-now-muted", chan->language))
03388                               ast_waitstream(chan, "");
03389                         }
03390                         ast_stopstream(chan);
03391                         break;
03392                      case '4': /* *84 Record conference */
03393                         if (conf->recording != MEETME_RECORD_ACTIVE) {
03394                            ast_set_flag64(confflags, CONFFLAG_RECORDCONF);
03395 
03396                            if (!conf->recordingfilename) {
03397                               const char *var;
03398                               ast_channel_lock(chan);
03399                               if ((var = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFILE"))) {
03400                                  conf->recordingfilename = ast_strdup(var);
03401                               }
03402                               if ((var = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFORMAT"))) {
03403                                  conf->recordingformat = ast_strdup(var);
03404                               }
03405                               ast_channel_unlock(chan);
03406                               if (!conf->recordingfilename) {
03407                                  snprintf(recordingtmp, sizeof(recordingtmp), "meetme-conf-rec-%s-%s", conf->confno, chan->uniqueid);
03408                                  conf->recordingfilename = ast_strdup(recordingtmp);
03409                               }
03410                               if (!conf->recordingformat) {
03411                                  conf->recordingformat = ast_strdup("wav");
03412                               }
03413                               ast_verb(4, "Starting recording of MeetMe Conference %s into file %s.%s.\n",
03414                                     conf->confno, conf->recordingfilename, conf->recordingformat);
03415                            }
03416 
03417                            ast_mutex_lock(&conf->recordthreadlock);
03418                            if ((conf->recordthread == AST_PTHREADT_NULL) && ast_test_flag64(confflags, CONFFLAG_RECORDCONF) && ((conf->lchan = ast_request("DAHDI", AST_FORMAT_SLINEAR, chan, "pseudo", NULL)))) {
03419                               ast_set_read_format(conf->lchan, AST_FORMAT_SLINEAR);
03420                               ast_set_write_format(conf->lchan, AST_FORMAT_SLINEAR);
03421                               dahdic.chan = 0;
03422                               dahdic.confno = conf->dahdiconf;
03423                               dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
03424                               if (ioctl(conf->lchan->fds[0], DAHDI_SETCONF, &dahdic)) {
03425                                  ast_log(LOG_WARNING, "Error starting listen channel\n");
03426                                  ast_hangup(conf->lchan);
03427                                  conf->lchan = NULL;
03428                               } else {
03429                                  ast_pthread_create_detached_background(&conf->recordthread, NULL, recordthread, conf);
03430                               }
03431                            }
03432                            ast_mutex_unlock(&conf->recordthreadlock);
03433 
03434                            if (!ast_streamfile(chan, "conf-now-recording", chan->language))
03435                               ast_waitstream(chan, "");
03436 
03437                         }
03438 
03439                         ast_stopstream(chan);
03440                         break;
03441                      default:
03442                         if (!ast_streamfile(chan, "conf-errormenu", chan->language))
03443                            ast_waitstream(chan, "");
03444                         ast_stopstream(chan);
03445                         break;
03446                      }
03447                   }
03448 
03449                   menu8_active = 0;
03450                   menu_active = 0;
03451                } else if (ast_test_flag64(confflags, CONFFLAG_ADMIN)) {
03452                   /* Admin menu */
03453                   if (!menu_active) {
03454                      menu_active = 1;
03455                      /* Record this sound! */
03456                      if (!ast_streamfile(chan, "conf-adminmenu-162", chan->language)) {
03457                         dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
03458                         ast_stopstream(chan);
03459                      } else {
03460                         dtmf = 0;
03461                      }
03462                   } else {
03463                      dtmf = f->subclass.integer;
03464                   }
03465                   if (dtmf > 0) {
03466                      switch(dtmf) {
03467                      case '1': /* Un/Mute */
03468                         menu_active = 0;
03469 
03470                         /* for admin, change both admin and use flags */
03471                         if (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) {
03472                            user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
03473                         } else {
03474                            user->adminflags |= (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
03475                         }
03476 
03477                         if (ast_test_flag64(confflags, CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
03478                            if (!ast_streamfile(chan, "conf-muted", chan->language)) {
03479                               ast_waitstream(chan, "");
03480                            }
03481                         } else {
03482                            if (!ast_streamfile(chan, "conf-unmuted", chan->language)) {
03483                               ast_waitstream(chan, "");
03484                            }
03485                         }
03486                         break;
03487                      case '2': /* Un/Lock the Conference */
03488                         menu_active = 0;
03489                         if (conf->locked) {
03490                            conf->locked = 0;
03491                            if (!ast_streamfile(chan, "conf-unlockednow", chan->language)) {
03492                               ast_waitstream(chan, "");
03493                            }
03494                         } else {
03495                            conf->locked = 1;
03496                            if (!ast_streamfile(chan, "conf-lockednow", chan->language)) {
03497                               ast_waitstream(chan, "");
03498                            }
03499                         }
03500                         break;
03501                      case '3': /* Eject last user */
03502                      {
03503                         struct ast_conf_user *usr = NULL;
03504                         int max_no = 0;
03505                         ao2_callback(conf->usercontainer, OBJ_NODATA, user_max_cmp, &max_no);
03506                         menu_active = 0;
03507                         usr = ao2_find(conf->usercontainer, &max_no, 0);
03508                         if ((usr->chan->name == chan->name) || ast_test_flag64(&usr->userflags, CONFFLAG_ADMIN)) {
03509                            if (!ast_streamfile(chan, "conf-errormenu", chan->language)) {
03510                               ast_waitstream(chan, "");
03511                            }
03512                         } else {
03513                            usr->adminflags |= ADMINFLAG_KICKME;
03514                         }
03515                         ao2_ref(usr, -1);
03516                         ast_stopstream(chan);
03517                         break;   
03518                      }
03519                      case '4':
03520                         tweak_listen_volume(user, VOL_DOWN);
03521                         break;
03522                      case '5':
03523                         /* Extend RT conference */
03524                         if (rt_schedule) {
03525                            if (!rt_extend_conf(conf->confno)) {
03526                               if (!ast_streamfile(chan, "conf-extended", chan->language)) {
03527                                  ast_waitstream(chan, "");
03528                               }
03529                            } else {
03530                               if (!ast_streamfile(chan, "conf-nonextended", chan->language)) {
03531                                  ast_waitstream(chan, "");
03532                               }
03533                            }
03534                            ast_stopstream(chan);
03535                         }
03536                         menu_active = 0;
03537                         break;
03538                      case '6':
03539                         tweak_listen_volume(user, VOL_UP);
03540                         break;
03541                      case '7':
03542                         tweak_talk_volume(user, VOL_DOWN);
03543                         break;
03544                      case '8':
03545                         menu8_active = 1;
03546                         break;
03547                      case '9':
03548                         tweak_talk_volume(user, VOL_UP);
03549                         break;
03550                      default:
03551                         menu_active = 0;
03552                         /* Play an error message! */
03553                         if (!ast_streamfile(chan, "conf-errormenu", chan->language)) {
03554                            ast_waitstream(chan, "");
03555                         }
03556                         break;
03557                      }
03558                   }
03559                } else {
03560                   /* User menu */
03561                   if (!menu_active) {
03562                      menu_active = 1;
03563                      if (!ast_streamfile(chan, "conf-usermenu-162", chan->language)) {
03564                         dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
03565                         ast_stopstream(chan);
03566                      } else {
03567                         dtmf = 0;
03568                      }
03569                   } else {
03570                      dtmf = f->subclass.integer;
03571                   }
03572                   if (dtmf > 0) {
03573                      switch (dtmf) {
03574                      case '1': /* Un/Mute */
03575                         menu_active = 0;
03576 
03577                         /* user can only toggle the self-muted state */
03578                         user->adminflags ^= ADMINFLAG_SELFMUTED;
03579 
03580                         /* they can't override the admin mute state */
03581                         if (ast_test_flag64(confflags, CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
03582                            if (!ast_streamfile(chan, "conf-muted", chan->language)) {
03583                               ast_waitstream(chan, "");
03584                            }
03585                         } else {
03586                            if (!ast_streamfile(chan, "conf-unmuted", chan->language)) {
03587                               ast_waitstream(chan, "");
03588                            }
03589                         }
03590                         break;
03591                      case '2':
03592                         menu_active = 0;
03593                         if (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) {
03594                            user->adminflags |= ADMINFLAG_T_REQUEST;
03595                         }
03596                            
03597                         if (user->adminflags & ADMINFLAG_T_REQUEST) {
03598                            if (!ast_streamfile(chan, "beep", chan->language)) {
03599                               ast_waitstream(chan, "");
03600                            }
03601                         }
03602                         break;
03603                      case '4':
03604                         tweak_listen_volume(user, VOL_DOWN);
03605                         break;
03606                      case '5':
03607                         /* Extend RT conference */
03608                         if (rt_schedule) {
03609                            rt_extend_conf(conf->confno);
03610                         }
03611                         menu_active = 0;
03612                         break;
03613                      case '6':
03614                         tweak_listen_volume(user, VOL_UP);
03615                         break;
03616                      case '7':
03617                         tweak_talk_volume(user, VOL_DOWN);
03618                         break;
03619                      case '8':
03620                         menu_active = 0;
03621                         break;
03622                      case '9':
03623                         tweak_talk_volume(user, VOL_UP);
03624                         break;
03625                      default:
03626                         menu_active = 0;
03627                         if (!ast_streamfile(chan, "conf-errormenu", chan->language)) {
03628                            ast_waitstream(chan, "");
03629                         }
03630                         break;
03631                      }
03632                   }
03633                }
03634                if (musiconhold && !menu_active) {
03635                   conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
03636                }
03637 
03638                if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
03639                   ast_log(LOG_WARNING, "Error setting conference\n");
03640                   close(fd);
03641                   ast_frfree(f);
03642                   goto outrun;
03643                }
03644 
03645                conf_flush(fd, chan);
03646             /*
03647              * Since options using DTMF could absorb DTMF meant for the
03648              * conference menu, we have to check them after the menu.
03649              */
03650             } else if ((f->frametype == AST_FRAME_DTMF) && ast_test_flag64(confflags, CONFFLAG_EXIT_CONTEXT) && ast_exists_extension(chan, exitcontext, dtmfstr, 1, "")) {
03651                if (ast_test_flag64(confflags, CONFFLAG_PASS_DTMF)) {
03652                   conf_queue_dtmf(conf, user, f);
03653                }
03654 
03655                if (!ast_goto_if_exists(chan, exitcontext, dtmfstr, 1)) {
03656                   ast_debug(1, "Got DTMF %c, goto context %s\n", dtmfstr[0], exitcontext);
03657                   ret = 0;
03658                   ast_frfree(f);
03659                   break;
03660                } else {
03661                   ast_debug(2, "Exit by single digit did not work in meetme. Extension %s does not exist in context %s\n", dtmfstr, exitcontext);
03662                }
03663             } else if ((f->frametype == AST_FRAME_DTMF) && ast_test_flag64(confflags, CONFFLAG_KEYEXIT) &&
03664                (strchr(exitkeys, f->subclass.integer))) {
03665                pbx_builtin_setvar_helper(chan, "MEETME_EXIT_KEY", dtmfstr);
03666                   
03667                if (ast_test_flag64(confflags, CONFFLAG_PASS_DTMF)) {
03668                   conf_queue_dtmf(conf, user, f);
03669                }
03670                ret = 0;
03671                ast_frfree(f);
03672                break;
03673             } else if ((f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END)
03674                && ast_test_flag64(confflags, CONFFLAG_PASS_DTMF)) {
03675                conf_queue_dtmf(conf, user, f);
03676             } else if (ast_test_flag64(confflags, CONFFLAG_SLA_STATION) && f->frametype == AST_FRAME_CONTROL) {
03677                switch (f->subclass.integer) {
03678                case AST_CONTROL_HOLD:
03679                   sla_queue_event_conf(SLA_EVENT_HOLD, chan, conf);
03680                   break;
03681                default:
03682                   break;
03683                }
03684             } else if (f->frametype == AST_FRAME_NULL) {
03685                /* Ignore NULL frames. It is perfectly normal to get these if the person is muted. */
03686             } else if (f->frametype == AST_FRAME_CONTROL) {
03687                switch (f->subclass.integer) {
03688                case AST_CONTROL_BUSY:
03689                case AST_CONTROL_CONGESTION:
03690                   ast_frfree(f);
03691                   goto outrun;
03692                   break;
03693                default:
03694                   ast_debug(1, 
03695                      "Got ignored control frame on channel %s, f->frametype=%d,f->subclass=%d\n",
03696                      chan->name, f->frametype, f->subclass.integer);
03697                }
03698             } else {
03699                ast_debug(1, 
03700                   "Got unrecognized frame on channel %s, f->frametype=%d,f->subclass=%d\n",
03701                   chan->name, f->frametype, f->subclass.integer);
03702             }
03703             ast_frfree(f);
03704          } else if (outfd > -1) {
03705             res = read(outfd, buf, CONF_SIZE);
03706             if (res > 0) {
03707                memset(&fr, 0, sizeof(fr));
03708                fr.frametype = AST_FRAME_VOICE;
03709                fr.subclass.codec = AST_FORMAT_SLINEAR;
03710                fr.datalen = res;
03711                fr.samples = res / 2;
03712                fr.data.ptr = buf;
03713                fr.offset = AST_FRIENDLY_OFFSET;
03714                if (!user->listen.actual &&
03715                   (ast_test_flag64(confflags, CONFFLAG_MONITOR) ||
03716                    (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) ||
03717                    (!user->talking && ast_test_flag64(confflags, CONFFLAG_OPTIMIZETALKER))
03718                    )) {
03719                   int idx;
03720                   for (idx = 0; idx < AST_FRAME_BITS; idx++) {
03721                      if (chan->rawwriteformat & (1 << idx)) {
03722                         break;
03723                      }
03724                   }
03725                   if (idx >= AST_FRAME_BITS) {
03726                      goto bailoutandtrynormal;
03727                   }
03728                   ast_mutex_lock(&conf->listenlock);
03729                   if (!conf->transframe[idx]) {
03730                      if (conf->origframe) {
03731                         if (musiconhold && !ast_dsp_silence(dsp, conf->origframe, &confsilence) && confsilence < MEETME_DELAYDETECTTALK) {
03732                            ast_moh_stop(chan);
03733                            mohtempstopped = 1;
03734                         }
03735                         if (!conf->transpath[idx]) {
03736                            conf->transpath[idx] = ast_translator_build_path((1 << idx), AST_FORMAT_SLINEAR);
03737                         }
03738                         if (conf->transpath[idx]) {
03739                            conf->transframe[idx] = ast_translate(conf->transpath[idx], conf->origframe, 0);
03740                            if (!conf->transframe[idx]) {
03741                               conf->transframe[idx] = &ast_null_frame;
03742                            }
03743                         }
03744                      }
03745                   }
03746                   if (conf->transframe[idx]) {
03747                      if ((conf->transframe[idx]->frametype != AST_FRAME_NULL) &&
03748                          can_write(chan, confflags)) {
03749                         struct ast_frame *cur;
03750                         /* the translator may have returned a list of frames, so
03751                            write each one onto the channel
03752                         */
03753                         for (cur = conf->transframe[idx]; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
03754                            if (ast_write(chan, cur)) {
03755                               ast_log(LOG_WARNING, "Unable to write frame to channel %s\n", chan->name);
03756                               break;
03757                            }
03758                         }
03759                         if (musiconhold && mohtempstopped && confsilence > MEETME_DELAYDETECTENDTALK) {
03760                            mohtempstopped = 0;
03761                            conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
03762                         }
03763                      }
03764                   } else {
03765                      ast_mutex_unlock(&conf->listenlock);
03766                      goto bailoutandtrynormal;
03767                   }
03768                   ast_mutex_unlock(&conf->listenlock);
03769                } else {
03770 bailoutandtrynormal:
03771                   if (musiconhold && !ast_dsp_silence(dsp, &fr, &confsilence) && confsilence < MEETME_DELAYDETECTTALK) {
03772                      ast_moh_stop(chan);
03773                      mohtempstopped = 1;
03774                   }
03775                   if (user->listen.actual) {
03776                      ast_frame_adjust_volume(&fr, user->listen.actual);
03777                   }
03778                   if (can_write(chan, confflags) && ast_write(chan, &fr) < 0) {
03779                      ast_log(LOG_WARNING, "Unable to write frame to channel %s\n", chan->name);
03780                   }
03781                   if (musiconhold && mohtempstopped && confsilence > MEETME_DELAYDETECTENDTALK) {
03782                      mohtempstopped = 0;
03783                      conf_start_moh(chan, optargs[OPT_ARG_MOH_CLASS]);
03784                   }
03785                }
03786             } else {
03787                ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
03788             }
03789          }
03790          lastmarked = currentmarked;
03791       }
03792    }
03793 
03794    if (musiconhold) {
03795       ast_moh_stop(chan);
03796    }
03797    
03798    if (using_pseudo) {
03799       close(fd);
03800    } else {
03801       /* Take out of conference */
03802       dahdic.chan = 0;  
03803       dahdic.confno = 0;
03804       dahdic.confmode = 0;
03805       if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
03806          ast_log(LOG_WARNING, "Error setting conference\n");
03807       }
03808    }
03809 
03810    reset_volumes(user);
03811 
03812    if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && !ast_test_flag64(confflags, CONFFLAG_MONITOR) &&
03813       !ast_test_flag64(confflags, CONFFLAG_ADMIN)) {
03814       conf_play(chan, conf, LEAVE);
03815    }
03816 
03817    if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && (ast_test_flag64(confflags, CONFFLAG_INTROUSER) || ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW)) && conf->users > 1) {
03818       struct announce_listitem *item;
03819       if (!(item = ao2_alloc(sizeof(*item), NULL)))
03820          goto outrun;
03821       ast_copy_string(item->namerecloc, user->namerecloc, sizeof(item->namerecloc));
03822       ast_copy_string(item->language, chan->language, sizeof(item->language));
03823       item->confchan = conf->chan;
03824       item->confusers = conf->users;
03825       item->announcetype = CONF_HASLEFT;
03826       ast_mutex_lock(&conf->announcelistlock);
03827       AST_LIST_INSERT_TAIL(&conf->announcelist, item, entry);
03828       ast_cond_signal(&conf->announcelist_addition);
03829       ast_mutex_unlock(&conf->announcelistlock);
03830    } else if (!ast_test_flag64(confflags, CONFFLAG_QUIET) && (ast_test_flag64(confflags, CONFFLAG_INTROUSER) ||
03831       ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW)) && conf->users == 1) {
03832       /* Last person is leaving, so no reason to try and announce, but should delete the name recording */
03833       ast_filedelete(user->namerecloc, NULL);
03834    }
03835 
03836  outrun:
03837    AST_LIST_LOCK(&confs);
03838 
03839    if (dsp) {
03840       ast_dsp_free(dsp);
03841    }
03842    
03843    if (user->user_no) {
03844       /* Only cleanup users who really joined! */
03845       now = ast_tvnow();
03846 
03847       if (sent_event) {
03848          ast_manager_event(chan, EVENT_FLAG_CALL, "MeetmeLeave",
03849             "Channel: %s\r\n"
03850             "Uniqueid: %s\r\n"
03851             "Meetme: %s\r\n"
03852             "Usernum: %d\r\n"
03853             "CallerIDNum: %s\r\n"
03854             "CallerIDName: %s\r\n"
03855             "ConnectedLineNum: %s\r\n"
03856             "ConnectedLineName: %s\r\n"
03857             "Duration: %ld\r\n",
03858             chan->name, chan->uniqueid, conf->confno,
03859             user->user_no,
03860             S_COR(user->chan->caller.id.number.valid, user->chan->caller.id.number.str, "<unknown>"),
03861             S_COR(user->chan->caller.id.name.valid, user->chan->caller.id.name.str, "<unknown>"),
03862             S_COR(user->chan->connected.id.number.valid, user->chan->connected.id.number.str, "<unknown>"),
03863             S_COR(user->chan->connected.id.name.valid, user->chan->connected.id.name.str, "<unknown>"),
03864             (long)(now.tv_sec - user->jointime));
03865       }
03866 
03867       if (setusercount) {
03868          conf->users--;
03869          if (rt_log_members) {
03870             /* Update table */
03871             snprintf(members, sizeof(members), "%d", conf->users);
03872             ast_realtime_require_field("meetme",
03873                "confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
03874                "members", RQ_UINTEGER1, strlen(members),
03875                NULL);
03876             ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
03877          }
03878          if (ast_test_flag64(confflags, CONFFLAG_MARKEDUSER)) {
03879             conf->markedusers--;
03880          }
03881       }
03882       /* Remove ourselves from the container */
03883       ao2_unlink(conf->usercontainer, user); 
03884 
03885       /* Change any states */
03886       if (!conf->users) {
03887          ast_devstate_changed(AST_DEVICE_NOT_INUSE, (conf->isdynamic ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), "meetme:%s", conf->confno);
03888       }
03889 
03890       /* Return the number of seconds the user was in the conf */
03891       snprintf(meetmesecs, sizeof(meetmesecs), "%d", (int) (time(NULL) - user->jointime));
03892       pbx_builtin_setvar_helper(chan, "MEETMESECS", meetmesecs);
03893 
03894       /* Return the RealTime bookid for CDR linking */
03895       if (rt_schedule) {
03896          pbx_builtin_setvar_helper(chan, "MEETMEBOOKID", conf->bookid);
03897       }
03898    }
03899    ao2_ref(user, -1);
03900    AST_LIST_UNLOCK(&confs);
03901 
03902    return ret;
03903 }
03904 
03905 static struct ast_conference *find_conf_realtime(struct ast_channel *chan, char *confno, int make, int dynamic,
03906             char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags64 *confflags, int *too_early, char **optargs)
03907 {
03908    struct ast_variable *var, *origvar;
03909    struct ast_conference *cnf;
03910 
03911    *too_early = 0;
03912 
03913    /* Check first in the conference list */
03914    AST_LIST_LOCK(&confs);
03915    AST_LIST_TRAVERSE(&confs, cnf, list) {
03916       if (!strcmp(confno, cnf->confno)) {
03917          break;
03918       }
03919    }
03920    if (cnf) {
03921       cnf->refcount += refcount;
03922    }
03923    AST_LIST_UNLOCK(&confs);
03924 
03925    if (!cnf) {
03926       char *pin = NULL, *pinadmin = NULL; /* For temp use */
03927       int maxusers = 0;
03928       struct timeval now;
03929       char recordingfilename[256] = "";
03930       char recordingformat[11] = "";
03931       char currenttime[32] = "";
03932       char eatime[32] = "";
03933       char bookid[51] = "";
03934       char recordingtmp[AST_MAX_EXTENSION] = "";
03935       char useropts[OPTIONS_LEN + 1] = ""; /* Used for RealTime conferences */
03936       char adminopts[OPTIONS_LEN + 1] = "";
03937       struct ast_tm tm, etm;
03938       struct timeval endtime = { .tv_sec = 0 };
03939       const char *var2;
03940 
03941       if (rt_schedule) {
03942          now = ast_tvnow();
03943 
03944          ast_localtime(&now, &tm, NULL);
03945          ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
03946 
03947          ast_debug(1, "Looking for conference %s that starts after %s\n", confno, currenttime);
03948 
03949          var = ast_load_realtime("meetme", "confno",
03950             confno, "starttime <= ", currenttime, "endtime >= ",
03951             currenttime, NULL);
03952 
03953          if (!var && fuzzystart) {
03954             now = ast_tvnow();
03955             now.tv_sec += fuzzystart;
03956 
03957             ast_localtime(&now, &tm, NULL);
03958             ast_strftime(currenttime, sizeof(currenttime), DATE_FORMAT, &tm);
03959             var = ast_load_realtime("meetme", "confno",
03960                confno, "starttime <= ", currenttime, "endtime >= ",
03961                currenttime, NULL);
03962          }
03963 
03964          if (!var && earlyalert) {
03965             now = ast_tvnow();
03966             now.tv_sec += earlyalert;
03967             ast_localtime(&now, &etm, NULL);
03968             ast_strftime(eatime, sizeof(eatime), DATE_FORMAT, &etm);
03969             var = ast_load_realtime("meetme", "confno",
03970                confno, "starttime <= ", eatime, "endtime >= ",
03971                currenttime, NULL);
03972             if (var) {
03973                *too_early = 1;
03974             }
03975          }
03976 
03977       } else {
03978           var = ast_load_realtime("meetme", "confno", confno, NULL);
03979       }
03980 
03981       if (!var) {
03982          return NULL;
03983       }
03984 
03985       if (rt_schedule && *too_early) {
03986          /* Announce that the caller is early and exit */
03987          if (!ast_streamfile(chan, "conf-has-not-started", chan->language)) {
03988             ast_waitstream(chan, "");
03989          }
03990          ast_variables_destroy(var);
03991          return NULL;
03992       }
03993 
03994       for (origvar = var; var; var = var->next) {
03995          if (!strcasecmp(var->name, "pin")) {
03996             pin = ast_strdupa(var->value);
03997          } else if (!strcasecmp(var->name, "adminpin")) {
03998             pinadmin = ast_strdupa(var->value);
03999          } else if (!strcasecmp(var->name, "bookId")) {
04000             ast_copy_string(bookid, var->value, sizeof(bookid));
04001          } else if (!strcasecmp(var->name, "opts")) {
04002             ast_copy_string(useropts, var->value, sizeof(char[OPTIONS_LEN + 1]));
04003          } else if (!strcasecmp(var->name, "maxusers")) {
04004             maxusers = atoi(var->value);
04005          } else if (!strcasecmp(var->name, "adminopts")) {
04006             ast_copy_string(adminopts, var->value, sizeof(char[OPTIONS_LEN + 1]));
04007          } else if (!strcasecmp(var->name, "recordingfilename")) {
04008             ast_copy_string(recordingfilename, var->value, sizeof(recordingfilename));
04009          } else if (!strcasecmp(var->name, "recordingformat")) {
04010             ast_copy_string(recordingformat, var->value, sizeof(recordingformat));
04011          } else if (!strcasecmp(var->name, "endtime")) {
04012             struct ast_tm endtime_tm;
04013             ast_strptime(var->value, "%Y-%m-%d %H:%M:%S", &endtime_tm);
04014             endtime = ast_mktime(&endtime_tm, NULL);
04015          }
04016       }
04017 
04018       ast_variables_destroy(origvar);
04019 
04020       cnf = build_conf(confno, pin ? pin : "", pinadmin ? pinadmin : "", make, dynamic, refcount, chan, NULL);
04021 
04022       if (cnf) {
04023          struct ast_flags64 tmp_flags;
04024 
04025          cnf->maxusers = maxusers;
04026          cnf->endalert = endalert;
04027          cnf->endtime = endtime.tv_sec;
04028          cnf->useropts = ast_strdup(useropts);
04029          cnf->adminopts = ast_strdup(adminopts);
04030          cnf->bookid = ast_strdup(bookid);
04031          if (!ast_strlen_zero(recordingfilename)) {
04032             cnf->recordingfilename = ast_strdup(recordingfilename);
04033          }
04034          if (!ast_strlen_zero(recordingformat)) {
04035             cnf->recordingformat = ast_strdup(recordingformat);
04036          }
04037 
04038          /* Parse the other options into confflags -- need to do this in two
04039           * steps, because the parse_options routine zeroes the buffer. */
04040          ast_app_parse_options64(meetme_opts, &tmp_flags, optargs, useropts);
04041          ast_copy_flags64(confflags, &tmp_flags, tmp_flags.flags);
04042 
04043          if (strchr(cnf->useropts, 'r')) {
04044             if (ast_strlen_zero(recordingfilename)) { /* If the recordingfilename in the database is empty, use the channel definition or use the default. */
04045                ast_channel_lock(chan);
04046                if ((var2 = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFILE"))) {
04047                   ast_free(cnf->recordingfilename);
04048                   cnf->recordingfilename = ast_strdup(var2);
04049                }
04050                ast_channel_unlock(chan);
04051                if (ast_strlen_zero(cnf->recordingfilename)) {
04052                   snprintf(recordingtmp, sizeof(recordingtmp), "meetme-conf-rec-%s-%s", cnf->confno, chan->uniqueid);
04053                   ast_free(cnf->recordingfilename);
04054                   cnf->recordingfilename = ast_strdup(recordingtmp);
04055                }
04056             }
04057             if (ast_strlen_zero(cnf->recordingformat)) {/* If the recording format is empty, use the wav as default */
04058                ast_channel_lock(chan);
04059                if ((var2 = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFORMAT"))) {
04060                   ast_free(cnf->recordingformat);
04061                   cnf->recordingformat = ast_strdup(var2);
04062                }
04063                ast_channel_unlock(chan);
04064                if (ast_strlen_zero(cnf->recordingformat)) {
04065                   ast_free(cnf->recordingformat);
04066                   cnf->recordingformat = ast_strdup("wav");
04067                }
04068             }
04069             ast_verb(4, "Starting recording of MeetMe Conference %s into file %s.%s.\n", cnf->confno, cnf->recordingfilename, cnf->recordingformat);
04070          }
04071       }
04072    }
04073 
04074    if (cnf) {
04075       if (confflags->flags && !cnf->chan &&
04076           !ast_test_flag64(confflags, CONFFLAG_QUIET) &&
04077           ast_test_flag64(confflags, CONFFLAG_INTROUSER | CONFFLAG_INTROUSERNOREVIEW)) {
04078          ast_log(LOG_WARNING, "No DAHDI channel available for conference, user introduction disabled (is chan_dahdi loaded?)\n");
04079          ast_clear_flag64(confflags, CONFFLAG_INTROUSER | CONFFLAG_INTROUSERNOREVIEW);
04080       }
04081 
04082       if (confflags && !cnf->chan &&
04083           ast_test_flag64(confflags, CONFFLAG_RECORDCONF)) {
04084          ast_log(LOG_WARNING, "No DAHDI channel available for conference, conference recording disabled (is chan_dahdi loaded?)\n");
04085          ast_clear_flag64(confflags, CONFFLAG_RECORDCONF);
04086       }
04087    }
04088 
04089    return cnf;
04090 }
04091 
04092 
04093 static struct ast_conference *find_conf(struct ast_channel *chan, char *confno, int make, int dynamic,
04094                char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags64 *confflags)
04095 {
04096    struct ast_config *cfg;
04097    struct ast_variable *var;
04098    struct ast_flags config_flags = { 0 };
04099    struct ast_conference *cnf;
04100 
04101    AST_DECLARE_APP_ARGS(args,
04102       AST_APP_ARG(confno);
04103       AST_APP_ARG(pin);
04104       AST_APP_ARG(pinadmin);
04105    );
04106 
04107    /* Check first in the conference list */
04108    ast_debug(1, "The requested confno is '%s'?\n", confno);
04109    AST_LIST_LOCK(&confs);
04110    AST_LIST_TRAVERSE(&confs, cnf, list) {
04111       ast_debug(3, "Does conf %s match %s?\n", confno, cnf->confno);
04112       if (!strcmp(confno, cnf->confno)) 
04113          break;
04114    }
04115    if (cnf) {
04116       cnf->refcount += refcount;
04117    }
04118    AST_LIST_UNLOCK(&confs);
04119 
04120    if (!cnf) {
04121       if (dynamic) {
04122          /* No need to parse meetme.conf */
04123          ast_debug(1, "Building dynamic conference '%s'\n", confno);
04124          if (dynamic_pin) {
04125             if (dynamic_pin[0] == 'q') {
04126                /* Query the user to enter a PIN */
04127                if (ast_app_getdata(chan, "conf-getpin", dynamic_pin, pin_buf_len - 1, 0) < 0)
04128                   return NULL;
04129             }
04130             cnf = build_conf(confno, dynamic_pin, "", make, dynamic, refcount, chan, NULL);
04131          } else {
04132             cnf = build_conf(confno, "", "", make, dynamic, refcount, chan, NULL);
04133          }
04134       } else {
04135          /* Check the config */
04136          cfg = ast_config_load(CONFIG_FILE_NAME, config_flags);
04137          if (!cfg) {
04138             ast_log(LOG_WARNING, "No %s file :(\n", CONFIG_FILE_NAME);
04139             return NULL;
04140          } else if (cfg == CONFIG_STATUS_FILEINVALID) {
04141             ast_log(LOG_ERROR, "Config file " CONFIG_FILE_NAME " is in an invalid format.  Aborting.\n");
04142             return NULL;
04143          }
04144 
04145          for (var = ast_variable_browse(cfg, "rooms"); var; var = var->next) {
04146             char parse[MAX_SETTINGS];
04147 
04148             if (strcasecmp(var->name, "conf"))
04149                continue;
04150 
04151             ast_copy_string(parse, var->value, sizeof(parse));
04152 
04153             AST_STANDARD_APP_ARGS(args, parse);
04154             ast_debug(3, "Will conf %s match %s?\n", confno, args.confno);
04155             if (!strcasecmp(args.confno, confno)) {
04156                /* Bingo it's a valid conference */
04157                cnf = build_conf(args.confno,
04158                      S_OR(args.pin, ""),
04159                      S_OR(args.pinadmin, ""),
04160                      make, dynamic, refcount, chan, NULL);
04161                break;
04162             }
04163          }
04164          if (!var) {
04165             ast_debug(1, "%s isn't a valid conference\n", confno);
04166          }
04167          ast_config_destroy(cfg);
04168       }
04169    } else if (dynamic_pin) {
04170       /* Correct for the user selecting 'D' instead of 'd' to have
04171          someone join into a conference that has already been created
04172          with a pin. */
04173       if (dynamic_pin[0] == 'q') {
04174          dynamic_pin[0] = '\0';
04175       }
04176    }
04177 
04178    if (cnf) {
04179       if (confflags && !cnf->chan &&
04180           !ast_test_flag64(confflags, CONFFLAG_QUIET) &&
04181           ast_test_flag64(confflags, CONFFLAG_INTROUSER | CONFFLAG_INTROUSERNOREVIEW)) {
04182          ast_log(LOG_WARNING, "No DAHDI channel available for conference, user introduction disabled (is chan_dahdi loaded?)\n");
04183          ast_clear_flag64(confflags, CONFFLAG_INTROUSER | CONFFLAG_INTROUSERNOREVIEW);
04184       }
04185       
04186       if (confflags && !cnf->chan &&
04187           ast_test_flag64(confflags, CONFFLAG_RECORDCONF)) {
04188          ast_log(LOG_WARNING, "No DAHDI channel available for conference, conference recording disabled (is chan_dahdi loaded?)\n");
04189          ast_clear_flag64(confflags, CONFFLAG_RECORDCONF);
04190       }
04191    }
04192 
04193    return cnf;
04194 }
04195 
04196 /*! \brief The MeetmeCount application */
04197 static int count_exec(struct ast_channel *chan, const char *data)
04198 {
04199    int res = 0;
04200    struct ast_conference *conf;
04201    int count;
04202    char *localdata;
04203    char val[80] = "0"; 
04204    AST_DECLARE_APP_ARGS(args,
04205       AST_APP_ARG(confno);
04206       AST_APP_ARG(varname);
04207    );
04208 
04209    if (ast_strlen_zero(data)) {
04210       ast_log(LOG_WARNING, "MeetMeCount requires an argument (conference number)\n");
04211       return -1;
04212    }
04213    
04214    localdata = ast_strdupa(data);
04215 
04216    AST_STANDARD_APP_ARGS(args, localdata);
04217    
04218    conf = find_conf(chan, args.confno, 0, 0, NULL, 0, 1, NULL);
04219 
04220    if (conf) {
04221       count = conf->users;
04222       dispose_conf(conf);
04223       conf = NULL;
04224    } else
04225       count = 0;
04226 
04227    if (!ast_strlen_zero(args.varname)) {
04228       /* have var so load it and exit */
04229       snprintf(val, sizeof(val), "%d", count);
04230       pbx_builtin_setvar_helper(chan, args.varname, val);
04231    } else {
04232       if (chan->_state != AST_STATE_UP) {
04233          ast_answer(chan);
04234       }
04235       res = ast_say_number(chan, count, "", chan->language, (char *) NULL); /* Needs gender */
04236    }
04237 
04238    return res;
04239 }
04240 
04241 /*! \brief The meetme() application */
04242 static int conf_exec(struct ast_channel *chan, const char *data)
04243 {
04244    int res = -1;
04245    char confno[MAX_CONFNUM] = "";
04246    int allowretry = 0;
04247    int retrycnt = 0;
04248    struct ast_conference *cnf = NULL;
04249    struct ast_flags64 confflags = {0};
04250    struct ast_flags config_flags = { 0 };
04251    int dynamic = 0;
04252    int empty = 0, empty_no_pin = 0;
04253    int always_prompt = 0;
04254    const char *notdata;
04255    char *info, the_pin[MAX_PIN] = "";
04256    AST_DECLARE_APP_ARGS(args,
04257       AST_APP_ARG(confno);
04258       AST_APP_ARG(options);
04259       AST_APP_ARG(pin);
04260    );
04261    char *optargs[OPT_ARG_ARRAY_SIZE] = { NULL, };
04262 
04263    if (ast_strlen_zero(data)) {
04264       allowretry = 1;
04265       notdata = "";
04266    } else {
04267       notdata = data;
04268    }
04269    
04270    if (chan->_state != AST_STATE_UP)
04271       ast_answer(chan);
04272 
04273    info = ast_strdupa(notdata);
04274 
04275    AST_STANDARD_APP_ARGS(args, info);  
04276 
04277    if (args.confno) {
04278       ast_copy_string(confno, args.confno, sizeof(confno));
04279       if (ast_strlen_zero(confno)) {
04280          allowretry = 1;
04281       }
04282    }
04283    
04284    if (args.pin)
04285       ast_copy_string(the_pin, args.pin, sizeof(the_pin));
04286 
04287    if (args.options) {
04288       ast_app_parse_options64(meetme_opts, &confflags, optargs, args.options);
04289       dynamic = ast_test_flag64(&confflags, CONFFLAG_DYNAMIC | CONFFLAG_DYNAMICPIN);
04290       if (ast_test_flag64(&confflags, CONFFLAG_DYNAMICPIN) && ast_strlen_zero(args.pin))
04291          strcpy(the_pin, "q");
04292 
04293       empty = ast_test_flag64(&confflags, CONFFLAG_EMPTY | CONFFLAG_EMPTYNOPIN);
04294       empty_no_pin = ast_test_flag64(&confflags, CONFFLAG_EMPTYNOPIN);
04295       always_prompt = ast_test_flag64(&confflags, CONFFLAG_ALWAYSPROMPT | CONFFLAG_DYNAMICPIN);
04296    }
04297 
04298    do {
04299       if (retrycnt > 3)
04300          allowretry = 0;
04301       if (empty) {
04302          int i;
04303          struct ast_config *cfg;
04304          struct ast_variable *var;
04305          int confno_int;
04306 
04307          /* We only need to load the config file for static and empty_no_pin (otherwise we don't care) */
04308          if ((empty_no_pin) || (!dynamic)) {
04309             cfg = ast_config_load(CONFIG_FILE_NAME, config_flags);
04310             if (cfg && cfg != CONFIG_STATUS_FILEINVALID) {
04311                var = ast_variable_browse(cfg, "rooms");
04312                while (var) {
04313                   char parse[MAX_SETTINGS], *stringp = parse, *confno_tmp;
04314                   if (!strcasecmp(var->name, "conf")) {
04315                      int found = 0;
04316                      ast_copy_string(parse, var->value, sizeof(parse));
04317                      confno_tmp = strsep(&stringp, "|,");
04318                      if (!dynamic) {
04319                         /* For static:  run through the list and see if this conference is empty */
04320                         AST_LIST_LOCK(&confs);
04321                         AST_LIST_TRAVERSE(&confs, cnf, list) {
04322                            if (!strcmp(confno_tmp, cnf->confno)) {
04323                               /* The conference exists, therefore it's not empty */
04324                               found = 1;
04325                               break;
04326                            }
04327                         }
04328                         AST_LIST_UNLOCK(&confs);
04329                         cnf = NULL;
04330                         if (!found) {
04331                            /* At this point, we have a confno_tmp (static conference) that is empty */
04332                            if ((empty_no_pin && ast_strlen_zero(stringp)) || (!empty_no_pin)) {
04333                               /* Case 1:  empty_no_pin and pin is nonexistent (NULL)
04334                                * Case 2:  empty_no_pin and pin is blank (but not NULL)
04335                                * Case 3:  not empty_no_pin
04336                                */
04337                               ast_copy_string(confno, confno_tmp, sizeof(confno));
04338                               break;
04339                            }
04340                         }
04341                      }
04342                   }
04343                   var = var->next;
04344                }
04345                ast_config_destroy(cfg);
04346             }
04347 
04348             if (ast_strlen_zero(confno) && (cfg = ast_load_realtime_multientry("meetme", "confno LIKE", "%", SENTINEL))) {
04349                const char *catg;
04350                for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
04351                   const char *confno_tmp = ast_variable_retrieve(cfg, catg, "confno");
04352                   const char *pin_tmp = ast_variable_retrieve(cfg, catg, "pin");
04353                   if (ast_strlen_zero(confno_tmp)) {
04354                      continue;
04355                   }
04356                   if (!dynamic) {
04357                      int found = 0;
04358                      /* For static:  run through the list and see if this conference is empty */
04359                      AST_LIST_LOCK(&confs);
04360                      AST_LIST_TRAVERSE(&confs, cnf, list) {
04361                         if (!strcmp(confno_tmp, cnf->confno)) {
04362                            /* The conference exists, therefore it's not empty */
04363                            found = 1;
04364                            break;
04365                         }
04366                      }
04367                      AST_LIST_UNLOCK(&confs);
04368                      if (!found) {
04369                         /* At this point, we have a confno_tmp (realtime conference) that is empty */
04370                         if ((empty_no_pin && ast_strlen_zero(pin_tmp)) || (!empty_no_pin)) {
04371                            /* Case 1:  empty_no_pin and pin is nonexistent (NULL)
04372                             * Case 2:  empty_no_pin and pin is blank (but not NULL)
04373                             * Case 3:  not empty_no_pin
04374                             */
04375                            ast_copy_string(confno, confno_tmp, sizeof(confno));
04376                            break;
04377                         }
04378                      }
04379                   }
04380                }
04381                ast_config_destroy(cfg);
04382             }
04383          }
04384 
04385          /* Select first conference number not in use */
04386          if (ast_strlen_zero(confno) && dynamic) {
04387             AST_LIST_LOCK(&confs);
04388             for (i = 0; i < ARRAY_LEN(conf_map); i++) {
04389                if (!conf_map[i]) {
04390                   snprintf(confno, sizeof(confno), "%d", i);
04391                   conf_map[i] = 1;
04392                   break;
04393                }
04394             }
04395             AST_LIST_UNLOCK(&confs);
04396          }
04397 
04398          /* Not found? */
04399          if (ast_strlen_zero(confno)) {
04400             ast_test_suite_event_notify("PLAYBACK", "Message: conf-noempty");
04401             res = ast_streamfile(chan, "conf-noempty", chan->language);
04402             if (!res)
04403                ast_waitstream(chan, "");
04404          } else {
04405             if (sscanf(confno, "%30d", &confno_int) == 1) {
04406                if (!ast_test_flag64(&confflags, CONFFLAG_QUIET)) {
04407                   res = ast_streamfile(chan, "conf-enteringno", chan->language);
04408                   if (!res) {
04409                      ast_waitstream(chan, "");
04410                      res = ast_say_digits(chan, confno_int, "", chan->language);
04411                   }
04412                }
04413             } else {
04414                ast_log(LOG_ERROR, "Could not scan confno '%s'\n", confno);
04415             }
04416          }
04417       }
04418 
04419       while (allowretry && (ast_strlen_zero(confno)) && (++retrycnt < 4)) {
04420          /* Prompt user for conference number */
04421          res = ast_app_getdata(chan, "conf-getconfno", confno, sizeof(confno) - 1, 0);
04422          if (res < 0) {
04423             /* Don't try to validate when we catch an error */
04424             confno[0] = '\0';
04425             allowretry = 0;
04426             break;
04427          }
04428       }
04429       if (!ast_strlen_zero(confno)) {
04430          /* Check the validity of the conference */
04431          cnf = find_conf(chan, confno, 1, dynamic, the_pin, 
04432             sizeof(the_pin), 1, &confflags);
04433          if (!cnf) {
04434             int too_early = 0;
04435 
04436             cnf = find_conf_realtime(chan, confno, 1, dynamic, 
04437                the_pin, sizeof(the_pin), 1, &confflags, &too_early, optargs);
04438             if (rt_schedule && too_early)
04439                allowretry = 0;
04440          }
04441 
04442          if (!cnf) {
04443             if (allowretry) {
04444                confno[0] = '\0';
04445                res = ast_streamfile(chan, "conf-invalid", chan->language);
04446                if (!res)
04447                   ast_waitstream(chan, "");
04448                res = -1;
04449             }
04450          } else {
04451             /* Conference requires a pin for specified access level */
04452             int req_pin = !ast_strlen_zero(cnf->pin) ||
04453                (!ast_strlen_zero(cnf->pinadmin) &&
04454                   ast_test_flag64(&confflags, CONFFLAG_ADMIN));
04455             /* The following logic was derived from a
04456              * 4 variable truth table and defines which
04457              * circumstances are not exempt from pin
04458              * checking.
04459              * If this needs to be modified, write the
04460              * truth table back out from the boolean
04461              * expression AB+A'D+C', change the erroneous
04462              * result, and rederive the expression.
04463              * Variables:
04464              *  A: pin provided?
04465              *  B: always prompt?
04466              *  C: dynamic?
04467              *  D: has users? */
04468             int not_exempt = !cnf->isdynamic;
04469             not_exempt = not_exempt || (!ast_strlen_zero(args.pin) && ast_test_flag64(&confflags, CONFFLAG_ALWAYSPROMPT));
04470             not_exempt = not_exempt || (ast_strlen_zero(args.pin) && cnf->users);
04471             if (req_pin && not_exempt) {
04472                char pin[MAX_PIN] = "";
04473                int j;
04474 
04475                /* Allow the pin to be retried up to 3 times */
04476                for (j = 0; j < 3; j++) {
04477                   if (*the_pin && (always_prompt == 0)) {
04478                      ast_copy_string(pin, the_pin, sizeof(pin));
04479                      res = 0;
04480                   } else {
04481                      /* Prompt user for pin if pin is required */
04482                      ast_test_suite_event_notify("PLAYBACK", "Message: conf-getpin\r\n"
04483                         "Channel: %s",
04484                         chan->name);
04485                      res = ast_app_getdata(chan, "conf-getpin", pin + strlen(pin), sizeof(pin) - 1 - strlen(pin), 0);
04486                   }
04487                   if (res >= 0) {
04488                      if ((!strcasecmp(pin, cnf->pin) &&
04489                           (ast_strlen_zero(cnf->pinadmin) ||
04490                            !ast_test_flag64(&confflags, CONFFLAG_ADMIN))) ||
04491                           (!ast_strlen_zero(cnf->pinadmin) &&
04492                            !strcasecmp(pin, cnf->pinadmin))) {
04493                         /* Pin correct */
04494                         allowretry = 0;
04495                         if (!ast_strlen_zero(cnf->pinadmin) && !strcasecmp(pin, cnf->pinadmin)) {
04496                            if (!ast_strlen_zero(cnf->adminopts)) {
04497                               char *opts = ast_strdupa(cnf->adminopts);
04498                               ast_app_parse_options64(meetme_opts, &confflags, optargs, opts);
04499                            }
04500                         } else {
04501                            if (!ast_strlen_zero(cnf->useropts)) {
04502                               char *opts = ast_strdupa(cnf->useropts);
04503                               ast_app_parse_options64(meetme_opts, &confflags, optargs, opts);
04504                            }
04505                         }
04506                         /* Run the conference */
04507                         ast_verb(4, "Starting recording of MeetMe Conference %s into file %s.%s.\n", cnf->confno, cnf->recordingfilename, cnf->recordingformat);
04508                         res = conf_run(chan, cnf, &confflags, optargs);
04509                         break;
04510                      } else {
04511                         /* Pin invalid */
04512                         if (!ast_streamfile(chan, "conf-invalidpin", chan->language)) {
04513                            res = ast_waitstream(chan, AST_DIGIT_ANY);
04514                            ast_stopstream(chan);
04515                         } else {
04516                            ast_log(LOG_WARNING, "Couldn't play invalid pin msg!\n");
04517                            break;
04518                         }
04519                         if (res < 0)
04520                            break;
04521                         pin[0] = res;
04522                         pin[1] = '\0';
04523                         res = -1;
04524                         if (allowretry)
04525                            confno[0] = '\0';
04526                      }
04527                   } else {
04528                      /* failed when getting the pin */
04529                      res = -1;
04530                      allowretry = 0;
04531                      /* see if we need to get rid of the conference */
04532                      break;
04533                   }
04534 
04535                   /* Don't retry pin with a static pin */
04536                   if (*the_pin && (always_prompt == 0)) {
04537                      break;
04538                   }
04539                }
04540             } else {
04541                /* No pin required */
04542                allowretry = 0;
04543 
04544                /* For RealTime conferences without a pin 
04545                 * should still support loading options
04546                 */
04547                if (!ast_strlen_zero(cnf->useropts)) {
04548                   char *opts = ast_strdupa(cnf->useropts);
04549                   ast_app_parse_options64(meetme_opts, &confflags, optargs, opts);
04550                }
04551 
04552                /* Run the conference */
04553                res = conf_run(chan, cnf, &confflags, optargs);
04554             }
04555             dispose_conf(cnf);
04556             cnf = NULL;
04557          }
04558       }
04559    } while (allowretry);
04560 
04561    if (cnf)
04562       dispose_conf(cnf);
04563    
04564    return res;
04565 }
04566 
04567 static struct ast_conf_user *find_user(struct ast_conference *conf, const char *callerident)
04568 {
04569    struct ast_conf_user *user = NULL;
04570    int cid;
04571 
04572    if (conf && callerident && sscanf(callerident, "%30d", &cid) == 1) {
04573       user = ao2_find(conf->usercontainer, &cid, 0);
04574       /* reference decremented later in admin_exec */
04575       return user;
04576    }
04577    return NULL;
04578 }
04579 
04580 static int user_listen_volup_cb(void *obj, void *unused, int flags)
04581 {
04582    struct ast_conf_user *user = obj;
04583    tweak_listen_volume(user, VOL_UP);
04584    return 0;
04585 }
04586 
04587 static int user_listen_voldown_cb(void *obj, void *unused, int flags)
04588 {
04589    struct ast_conf_user *user = obj;
04590    tweak_listen_volume(user, VOL_DOWN);
04591    return 0;
04592 }
04593 
04594 static int user_talk_volup_cb(void *obj, void *unused, int flags)
04595 {
04596    struct ast_conf_user *user = obj;
04597    tweak_talk_volume(user, VOL_UP);
04598    return 0;
04599 }
04600 
04601 static int user_talk_voldown_cb(void *obj, void *unused, int flags)
04602 {
04603    struct ast_conf_user *user = obj;
04604    tweak_talk_volume(user, VOL_DOWN);
04605    return 0;
04606 }
04607 
04608 static int user_reset_vol_cb(void *obj, void *unused, int flags)
04609 {
04610    struct ast_conf_user *user = obj;
04611    reset_volumes(user);
04612    return 0;
04613 }
04614 
04615 static int user_chan_cb(void *obj, void *args, int flags)
04616 {
04617    struct ast_conf_user *user = obj;
04618    const char *channel = args;
04619 
04620    if (!strcmp(user->chan->name, channel)) {
04621       return (CMP_MATCH | CMP_STOP);
04622    }
04623 
04624    return 0;
04625 }
04626 
04627 /*! \brief The MeetMeadmin application 
04628 
04629   MeetMeAdmin(confno, command, caller) */
04630 static int admin_exec(struct ast_channel *chan, const char *data) {
04631    char *params;
04632    struct ast_conference *cnf;
04633    struct ast_conf_user *user = NULL;
04634    AST_DECLARE_APP_ARGS(args,
04635       AST_APP_ARG(confno);
04636       AST_APP_ARG(command);
04637       AST_APP_ARG(user);
04638    );
04639    int res = 0;
04640 
04641    if (ast_strlen_zero(data)) {
04642       ast_log(LOG_WARNING, "MeetMeAdmin requires an argument!\n");
04643       pbx_builtin_setvar_helper(chan, "MEETMEADMINSTATUS", "NOPARSE");
04644       return -1;
04645    }
04646 
04647    params = ast_strdupa(data);
04648    AST_STANDARD_APP_ARGS(args, params);
04649 
04650    if (!args.command) {
04651       ast_log(LOG_WARNING, "MeetmeAdmin requires a command!\n");
04652       pbx_builtin_setvar_helper(chan, "MEETMEADMINSTATUS", "NOPARSE");
04653       return -1;
04654    }
04655 
04656    AST_LIST_LOCK(&confs);
04657    AST_LIST_TRAVERSE(&confs, cnf, list) {
04658       if (!strcmp(cnf->confno, args.confno))
04659          break;
04660    }
04661 
04662    if (!cnf) {
04663       ast_log(LOG_WARNING, "Conference number '%s' not found!\n", args.confno);
04664       AST_LIST_UNLOCK(&confs);
04665       pbx_builtin_setvar_helper(chan, "MEETMEADMINSTATUS", "NOTFOUND");
04666       return 0;
04667    }
04668 
04669    ast_atomic_fetchadd_int(&cnf->refcount, 1);
04670 
04671    if (args.user) {
04672       user = find_user(cnf, args.user);
04673       if (!user) {
04674          ast_log(LOG_NOTICE, "Specified User not found!\n");
04675          res = -2;
04676          goto usernotfound;
04677       }
04678    }
04679 
04680    switch (*args.command) {
04681    case 76: /* L: Lock */ 
04682       cnf->locked = 1;
04683       break;
04684    case 108: /* l: Unlock */ 
04685       cnf->locked = 0;
04686       break;
04687    case 75: /* K: kick all users */
04688       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_set_kickme_cb, NULL);
04689       break;
04690    case 101: /* e: Eject last user*/
04691    {
04692       int max_no = 0;
04693 
04694       /* If they passed in a user, disregard it */
04695       if (user) {
04696          ao2_ref(user, -1);
04697       }
04698 
04699       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_max_cmp, &max_no);
04700       user = ao2_find(cnf->usercontainer, &max_no, 0);
04701       if (!ast_test_flag64(&user->userflags, CONFFLAG_ADMIN))
04702          user->adminflags |= ADMINFLAG_KICKME;
04703       else {
04704          res = -1;
04705          ast_log(LOG_NOTICE, "Not kicking last user, is an Admin!\n");
04706       }
04707       ao2_ref(user, -1);
04708       break;
04709    }
04710    case 77: /* M: Mute */ 
04711       user->adminflags |= ADMINFLAG_MUTED;
04712       break;
04713    case 78: /* N: Mute all (non-admin) users */
04714       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_set_muted_cb, &cnf);
04715       break;               
04716    case 109: /* m: Unmute */ 
04717       user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED | ADMINFLAG_T_REQUEST);
04718       break;
04719    case 110: /* n: Unmute all users */
04720       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_set_unmuted_cb, NULL);
04721       break;
04722    case 107: /* k: Kick user */ 
04723       user->adminflags |= ADMINFLAG_KICKME;
04724       break;
04725    case 118: /* v: Lower all users listen volume */
04726       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_listen_voldown_cb, NULL);
04727       break;
04728    case 86: /* V: Raise all users listen volume */
04729       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_listen_volup_cb, NULL);
04730       break;
04731    case 115: /* s: Lower all users speaking volume */
04732       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_talk_voldown_cb, NULL);
04733       break;
04734    case 83: /* S: Raise all users speaking volume */
04735       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_talk_volup_cb, NULL);
04736       break;
04737    case 82: /* R: Reset all volume levels */
04738       ao2_callback(cnf->usercontainer, OBJ_NODATA, user_reset_vol_cb, NULL);
04739       break;
04740    case 114: /* r: Reset user's volume level */
04741       reset_volumes(user);
04742       break;
04743    case 85: /* U: Raise user's listen volume */
04744       tweak_listen_volume(user, VOL_UP);
04745       break;
04746    case 117: /* u: Lower user's listen volume */
04747       tweak_listen_volume(user, VOL_DOWN);
04748       break;
04749    case 84: /* T: Raise user's talk volume */
04750       tweak_talk_volume(user, VOL_UP);
04751       break;
04752    case 116: /* t: Lower user's talk volume */
04753       tweak_talk_volume(user, VOL_DOWN);
04754       break;
04755    case 'E': /* E: Extend conference */
04756       if (rt_extend_conf(args.confno)) {
04757          res = -1;
04758       }
04759       break;
04760    }
04761 
04762    if (args.user) {
04763       /* decrement reference from find_user */
04764       ao2_ref(user, -1);
04765    }
04766 usernotfound:
04767    AST_LIST_UNLOCK(&confs);
04768 
04769    dispose_conf(cnf);
04770    pbx_builtin_setvar_helper(chan, "MEETMEADMINSTATUS", res == -2 ? "NOTFOUND" : res ? "FAILED" : "OK");
04771 
04772    return 0;
04773 }
04774 
04775 /*! \brief The MeetMeChannelAdmin application 
04776    MeetMeChannelAdmin(channel, command) */
04777 static int channel_admin_exec(struct ast_channel *chan, const char *data) {
04778    char *params;
04779    struct ast_conference *conf = NULL;
04780    struct ast_conf_user *user = NULL;
04781    AST_DECLARE_APP_ARGS(args,
04782       AST_APP_ARG(channel);
04783       AST_APP_ARG(command);
04784    );
04785 
04786    if (ast_strlen_zero(data)) {
04787       ast_log(LOG_WARNING, "MeetMeChannelAdmin requires two arguments!\n");
04788       return -1;
04789    }
04790    
04791    params = ast_strdupa(data);
04792    AST_STANDARD_APP_ARGS(args, params);
04793 
04794    if (!args.channel) {
04795       ast_log(LOG_WARNING, "MeetMeChannelAdmin requires a channel name!\n");
04796       return -1;
04797    }
04798 
04799    if (!args.command) {
04800       ast_log(LOG_WARNING, "MeetMeChannelAdmin requires a command!\n");
04801       return -1;
04802    }
04803 
04804    AST_LIST_LOCK(&confs);
04805    AST_LIST_TRAVERSE(&confs, conf, list) {
04806       if ((user = ao2_callback(conf->usercontainer, 0, user_chan_cb, args.channel))) {
04807          break;
04808       }
04809    }
04810    
04811    if (!user) {
04812       ast_log(LOG_NOTICE, "Specified user (%s) not found\n", args.channel);
04813       AST_LIST_UNLOCK(&confs);
04814       return 0;
04815    }
04816    
04817    /* perform the specified action */
04818    switch (*args.command) {
04819       case 77: /* M: Mute */ 
04820          user->adminflags |= ADMINFLAG_MUTED;
04821          break;
04822       case 109: /* m: Unmute */ 
04823          user->adminflags &= ~ADMINFLAG_MUTED;
04824          break;
04825       case 107: /* k: Kick user */ 
04826          user->adminflags |= ADMINFLAG_KICKME;
04827          break;
04828       default: /* unknown command */
04829          ast_log(LOG_WARNING, "Unknown MeetMeChannelAdmin command '%s'\n", args.command);
04830          break;
04831    }
04832    ao2_ref(user, -1);
04833    AST_LIST_UNLOCK(&confs);
04834    
04835    return 0;
04836 }
04837 
04838 static int meetmemute(struct mansession *s, const struct message *m, int mute)
04839 {
04840    struct ast_conference *conf;
04841    struct ast_conf_user *user;
04842    const char *confid = astman_get_header(m, "Meetme");
04843    char *userid = ast_strdupa(astman_get_header(m, "Usernum"));
04844    int userno;
04845 
04846    if (ast_strlen_zero(confid)) {
04847       astman_send_error(s, m, "Meetme conference not specified");
04848       return 0;
04849    }
04850 
04851    if (ast_strlen_zero(userid)) {
04852       astman_send_error(s, m, "Meetme user number not specified");
04853       return 0;
04854    }
04855 
04856    userno = strtoul(userid, &userid, 10);
04857 
04858    if (*userid) {
04859       astman_send_error(s, m, "Invalid user number");
04860       return 0;
04861    }
04862 
04863    /* Look in the conference list */
04864    AST_LIST_LOCK(&confs);
04865    AST_LIST_TRAVERSE(&confs, conf, list) {
04866       if (!strcmp(confid, conf->confno))
04867          break;
04868    }
04869 
04870    if (!conf) {
04871       AST_LIST_UNLOCK(&confs);
04872       astman_send_error(s, m, "Meetme conference does not exist");
04873       return 0;
04874    }
04875 
04876    user = ao2_find(conf->usercontainer, &userno, 0);
04877 
04878    if (!user) {
04879       AST_LIST_UNLOCK(&confs);
04880       astman_send_error(s, m, "User number not found");
04881       return 0;
04882    }
04883 
04884    if (mute)
04885       user->adminflags |= ADMINFLAG_MUTED;   /* request user muting */
04886    else
04887       user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED | ADMINFLAG_T_REQUEST); /* request user unmuting */
04888 
04889    AST_LIST_UNLOCK(&confs);
04890 
04891    ast_log(LOG_NOTICE, "Requested to %smute conf %s user %d userchan %s uniqueid %s\n", mute ? "" : "un", conf->confno, user->user_no, user->chan->name, user->chan->uniqueid);
04892 
04893    ao2_ref(user, -1);
04894    astman_send_ack(s, m, mute ? "User muted" : "User unmuted");
04895    return 0;
04896 }
04897 
04898 static int action_meetmemute(struct mansession *s, const struct message *m)
04899 {
04900    return meetmemute(s, m, 1);
04901 }
04902 
04903 static int action_meetmeunmute(struct mansession *s, const struct message *m)
04904 {
04905    return meetmemute(s, m, 0);
04906 }
04907 
04908 static int action_meetmelist(struct mansession *s, const struct message *m)
04909 {
04910    const char *actionid = astman_get_header(m, "ActionID");
04911    const char *conference = astman_get_header(m, "Conference");
04912    char idText[80] = "";
04913    struct ast_conference *cnf;
04914    struct ast_conf_user *user;
04915    struct ao2_iterator user_iter;
04916    int total = 0;
04917 
04918    if (!ast_strlen_zero(actionid))
04919       snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
04920 
04921    if (AST_LIST_EMPTY(&confs)) {
04922       astman_send_error(s, m, "No active conferences.");
04923       return 0;
04924    }
04925 
04926    astman_send_listack(s, m, "Meetme user list will follow", "start");
04927 
04928    /* Find the right conference */
04929    AST_LIST_LOCK(&confs);
04930    AST_LIST_TRAVERSE(&confs, cnf, list) {
04931       /* If we ask for one particular, and this isn't it, skip it */
04932       if (!ast_strlen_zero(conference) && strcmp(cnf->confno, conference))
04933          continue;
04934 
04935       /* Show all the users */
04936       user_iter = ao2_iterator_init(cnf->usercontainer, 0);
04937       while ((user = ao2_iterator_next(&user_iter))) {
04938          total++;
04939          astman_append(s,
04940             "Event: MeetmeList\r\n"
04941             "%s"
04942             "Conference: %s\r\n"
04943             "UserNumber: %d\r\n"
04944             "CallerIDNum: %s\r\n"
04945             "CallerIDName: %s\r\n"
04946             "ConnectedLineNum: %s\r\n"
04947             "ConnectedLineName: %s\r\n"
04948             "Channel: %s\r\n"
04949             "Admin: %s\r\n"
04950             "Role: %s\r\n"
04951             "MarkedUser: %s\r\n"
04952             "Muted: %s\r\n"
04953             "Talking: %s\r\n"
04954             "\r\n",
04955             idText,
04956             cnf->confno,
04957             user->user_no,
04958             S_COR(user->chan->caller.id.number.valid, user->chan->caller.id.number.str, "<unknown>"),
04959             S_COR(user->chan->caller.id.name.valid, user->chan->caller.id.name.str, "<no name>"),
04960             S_COR(user->chan->connected.id.number.valid, user->chan->connected.id.number.str, "<unknown>"),
04961             S_COR(user->chan->connected.id.name.valid, user->chan->connected.id.name.str, "<no name>"),
04962             user->chan->name,
04963             ast_test_flag64(&user->userflags, CONFFLAG_ADMIN) ? "Yes" : "No",
04964             ast_test_flag64(&user->userflags, CONFFLAG_MONITOR) ? "Listen only" : ast_test_flag64(&user->userflags, CONFFLAG_TALKER) ? "Talk only" : "Talk and listen",
04965             ast_test_flag64(&user->userflags, CONFFLAG_MARKEDUSER) ? "Yes" : "No",
04966             user->adminflags & ADMINFLAG_MUTED ? "By admin" : user->adminflags & ADMINFLAG_SELFMUTED ? "By self" : "No",
04967             user->talking > 0 ? "Yes" : user->talking == 0 ? "No" : "Not monitored");
04968          ao2_ref(user, -1);
04969       }
04970       ao2_iterator_destroy(&user_iter);
04971    }
04972    AST_LIST_UNLOCK(&confs);
04973    /* Send final confirmation */
04974    astman_append(s,
04975    "Event: MeetmeListComplete\r\n"
04976    "EventList: Complete\r\n"
04977    "ListItems: %d\r\n"
04978    "%s"
04979    "\r\n", total, idText);
04980    return 0;
04981 }
04982 
04983 /*! \internal
04984  * \brief creates directory structure and assigns absolute path from relative paths for filenames
04985  *
04986  * \param filename contains the absolute or relative path to the desired file
04987  * \param buffer stores completed filename, absolutely must be a buffer of PATH_MAX length
04988  */
04989 static void filename_parse(char *filename, char *buffer)
04990 {
04991    char *slash;
04992    if (ast_strlen_zero(filename)) {
04993       ast_log(LOG_WARNING, "No file name was provided for a file save option.\n");
04994    } else if (filename[0] != '/') {
04995       snprintf(buffer, PATH_MAX, "%s/meetme/%s", ast_config_AST_SPOOL_DIR, filename);
04996    } else {
04997       ast_copy_string(buffer, filename, PATH_MAX);
04998    }
04999 
05000    slash = buffer;
05001    if ((slash = strrchr(slash, '/'))) {
05002       *slash = '\0';
05003       ast_mkdir(buffer, 0777);
05004       *slash = '/';
05005    }
05006 }
05007 
05008 static void *recordthread(void *args)
05009 {
05010    struct ast_conference *cnf = args;
05011    struct ast_frame *f = NULL;
05012    int flags;
05013    struct ast_filestream *s = NULL;
05014    int res = 0;
05015    int x;
05016    const char *oldrecordingfilename = NULL;
05017    char filename_buffer[PATH_MAX];
05018 
05019    if (!cnf || !cnf->lchan) {
05020       pthread_exit(0);
05021    }
05022 
05023    filename_buffer[0] = '\0';
05024    filename_parse(cnf->recordingfilename, filename_buffer);
05025 
05026    ast_stopstream(cnf->lchan);
05027    flags = O_CREAT | O_TRUNC | O_WRONLY;
05028 
05029 
05030    cnf->recording = MEETME_RECORD_ACTIVE;
05031    while (ast_waitfor(cnf->lchan, -1) > -1) {
05032       if (cnf->recording == MEETME_RECORD_TERMINATE) {
05033          AST_LIST_LOCK(&confs);
05034          AST_LIST_UNLOCK(&confs);
05035          break;
05036       }
05037       if (!s && !(ast_strlen_zero(filename_buffer)) && (filename_buffer != oldrecordingfilename)) {
05038          s = ast_writefile(filename_buffer, cnf->recordingformat, NULL, flags, 0, AST_FILE_MODE);
05039          oldrecordingfilename = filename_buffer;
05040       }
05041       
05042       f = ast_read(cnf->lchan);
05043       if (!f) {
05044          res = -1;
05045          break;
05046       }
05047       if (f->frametype == AST_FRAME_VOICE) {
05048          ast_mutex_lock(&cnf->listenlock);
05049          for (x = 0; x < AST_FRAME_BITS; x++) {
05050             /* Free any translations that have occured */
05051             if (cnf->transframe[x]) {
05052                ast_frfree(cnf->transframe[x]);
05053                cnf->transframe[x] = NULL;
05054             }
05055          }
05056          if (cnf->origframe)
05057             ast_frfree(cnf->origframe);
05058          cnf->origframe = ast_frdup(f);
05059          ast_mutex_unlock(&cnf->listenlock);
05060          if (s)
05061             res = ast_writestream(s, f);
05062          if (res) {
05063             ast_frfree(f);
05064             break;
05065          }
05066       }
05067       ast_frfree(f);
05068    }
05069    cnf->recording = MEETME_RECORD_OFF;
05070    if (s)
05071       ast_closestream(s);
05072    
05073    pthread_exit(0);
05074 }
05075 
05076 /*! \brief Callback for devicestate providers */
05077 static enum ast_device_state meetmestate(const char *data)
05078 {
05079    struct ast_conference *conf;
05080 
05081    /* Find conference */
05082    AST_LIST_LOCK(&confs);
05083    AST_LIST_TRAVERSE(&confs, conf, list) {
05084       if (!strcmp(data, conf->confno))
05085          break;
05086    }
05087    AST_LIST_UNLOCK(&confs);
05088    if (!conf)
05089       return AST_DEVICE_INVALID;
05090 
05091 
05092    /* SKREP to fill */
05093    if (!conf->users)
05094       return AST_DEVICE_NOT_INUSE;
05095 
05096    return AST_DEVICE_INUSE;
05097 }
05098 
05099 static void load_config_meetme(void)
05100 {
05101    struct ast_config *cfg;
05102    struct ast_flags config_flags = { 0 };
05103    const char *val;
05104 
05105    if (!(cfg = ast_config_load(CONFIG_FILE_NAME, config_flags))) {
05106       return;
05107    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
05108       ast_log(LOG_ERROR, "Config file " CONFIG_FILE_NAME " is in an invalid format.  Aborting.\n");
05109       return;
05110    }
05111 
05112    audio_buffers = DEFAULT_AUDIO_BUFFERS;
05113 
05114    /*  Scheduling support is off by default */
05115    rt_schedule = 0;
05116    fuzzystart = 0;
05117    earlyalert = 0;
05118    endalert = 0;
05119    extendby = 0;
05120 
05121    /*  Logging of participants defaults to ON for compatibility reasons */
05122    rt_log_members = 1;  
05123 
05124    if ((val = ast_variable_retrieve(cfg, "general", "audiobuffers"))) {
05125       if ((sscanf(val, "%30d", &audio_buffers) != 1)) {
05126          ast_log(LOG_WARNING, "audiobuffers setting must be a number, not '%s'\n", val);
05127          audio_buffers = DEFAULT_AUDIO_BUFFERS;
05128       } else if ((audio_buffers < DAHDI_DEFAULT_NUM_BUFS) || (audio_buffers > DAHDI_MAX_NUM_BUFS)) {
05129          ast_log(LOG_WARNING, "audiobuffers setting must be between %d and %d\n",
05130             DAHDI_DEFAULT_NUM_BUFS, DAHDI_MAX_NUM_BUFS);
05131          audio_buffers = DEFAULT_AUDIO_BUFFERS;
05132       }
05133       if (audio_buffers != DEFAULT_AUDIO_BUFFERS)
05134          ast_log(LOG_NOTICE, "Audio buffers per channel set to %d\n", audio_buffers);
05135    }
05136 
05137    if ((val = ast_variable_retrieve(cfg, "general", "schedule")))
05138       rt_schedule = ast_true(val);
05139    if ((val = ast_variable_retrieve(cfg, "general", "logmembercount")))
05140       rt_log_members = ast_true(val);
05141    if ((val = ast_variable_retrieve(cfg, "general", "fuzzystart"))) {
05142       if ((sscanf(val, "%30d", &fuzzystart) != 1)) {
05143          ast_log(LOG_WARNING, "fuzzystart must be a number, not '%s'\n", val);
05144          fuzzystart = 0;
05145       } 
05146    }
05147    if ((val = ast_variable_retrieve(cfg, "general", "earlyalert"))) {
05148       if ((sscanf(val, "%30d", &earlyalert) != 1)) {
05149          ast_log(LOG_WARNING, "earlyalert must be a number, not '%s'\n", val);
05150          earlyalert = 0;
05151       } 
05152    }
05153    if ((val = ast_variable_retrieve(cfg, "general", "endalert"))) {
05154       if ((sscanf(val, "%30d", &endalert) != 1)) {
05155          ast_log(LOG_WARNING, "endalert must be a number, not '%s'\n", val);
05156          endalert = 0;
05157       } 
05158    }
05159    if ((val = ast_variable_retrieve(cfg, "general", "extendby"))) {
05160       if ((sscanf(val, "%30d", &extendby) != 1)) {
05161          ast_log(LOG_WARNING, "extendby must be a number, not '%s'\n", val);
05162          extendby = 0;
05163       } 
05164    }
05165 
05166    ast_config_destroy(cfg);
05167 }
05168 
05169 /*! \brief Find an SLA trunk by name
05170  * \note This must be called with the sla_trunks container locked
05171  */
05172 static struct sla_trunk *sla_find_trunk(const char *name)
05173 {
05174    struct sla_trunk *trunk = NULL;
05175 
05176    AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
05177       if (!strcasecmp(trunk->name, name))
05178          break;
05179    }
05180 
05181    return trunk;
05182 }
05183 
05184 /*! \brief Find an SLA station by name
05185  * \note This must be called with the sla_stations container locked
05186  */
05187 static struct sla_station *sla_find_station(const char *name)
05188 {
05189    struct sla_station *station = NULL;
05190 
05191    AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
05192       if (!strcasecmp(station->name, name))
05193          break;
05194    }
05195 
05196    return station;
05197 }
05198 
05199 static int sla_check_station_hold_access(const struct sla_trunk *trunk,
05200    const struct sla_station *station)
05201 {
05202    struct sla_station_ref *station_ref;
05203    struct sla_trunk_ref *trunk_ref;
05204 
05205    /* For each station that has this call on hold, check for private hold. */
05206    AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {
05207       AST_LIST_TRAVERSE(&station_ref->station->trunks, trunk_ref, entry) {
05208          if (trunk_ref->trunk != trunk || station_ref->station == station)
05209             continue;
05210          if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME &&
05211             station_ref->station->hold_access == SLA_HOLD_PRIVATE)
05212             return 1;
05213          return 0;
05214       }
05215    }
05216 
05217    return 0;
05218 }
05219 
05220 /*! \brief Find a trunk reference on a station by name
05221  * \param station the station
05222  * \param name the trunk's name
05223  * \return a pointer to the station's trunk reference.  If the trunk
05224  *         is not found, it is not idle and barge is disabled, or if
05225  *         it is on hold and private hold is set, then NULL will be returned.
05226  */
05227 static struct sla_trunk_ref *sla_find_trunk_ref_byname(const struct sla_station *station,
05228    const char *name)
05229 {
05230    struct sla_trunk_ref *trunk_ref = NULL;
05231 
05232    AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
05233       if (strcasecmp(trunk_ref->trunk->name, name))
05234          continue;
05235 
05236       if ( (trunk_ref->trunk->barge_disabled 
05237          && trunk_ref->state == SLA_TRUNK_STATE_UP) ||
05238          (trunk_ref->trunk->hold_stations 
05239          && trunk_ref->trunk->hold_access == SLA_HOLD_PRIVATE
05240          && trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) ||
05241          sla_check_station_hold_access(trunk_ref->trunk, station) ) 
05242       {
05243          trunk_ref = NULL;
05244       }
05245 
05246       break;
05247    }
05248 
05249    return trunk_ref;
05250 }
05251 
05252 static struct sla_station_ref *sla_create_station_ref(struct sla_station *station)
05253 {
05254    struct sla_station_ref *station_ref;
05255 
05256    if (!(station_ref = ast_calloc(1, sizeof(*station_ref))))
05257       return NULL;
05258 
05259    station_ref->station = station;
05260 
05261    return station_ref;
05262 }
05263 
05264 static struct sla_ringing_station *sla_create_ringing_station(struct sla_station *station)
05265 {
05266    struct sla_ringing_station *ringing_station;
05267 
05268    if (!(ringing_station = ast_calloc(1, sizeof(*ringing_station))))
05269       return NULL;
05270 
05271    ringing_station->station = station;
05272    ringing_station->ring_begin = ast_tvnow();
05273 
05274    return ringing_station;
05275 }
05276 
05277 static enum ast_device_state sla_state_to_devstate(enum sla_trunk_state state)
05278 {
05279    switch (state) {
05280    case SLA_TRUNK_STATE_IDLE:
05281       return AST_DEVICE_NOT_INUSE;
05282    case SLA_TRUNK_STATE_RINGING:
05283       return AST_DEVICE_RINGING;
05284    case SLA_TRUNK_STATE_UP:
05285       return AST_DEVICE_INUSE;
05286    case SLA_TRUNK_STATE_ONHOLD:
05287    case SLA_TRUNK_STATE_ONHOLD_BYME:
05288       return AST_DEVICE_ONHOLD;
05289    }
05290 
05291    return AST_DEVICE_UNKNOWN;
05292 }
05293 
05294 static void sla_change_trunk_state(const struct sla_trunk *trunk, enum sla_trunk_state state, 
05295    enum sla_which_trunk_refs inactive_only, const struct sla_trunk_ref *exclude)
05296 {
05297    struct sla_station *station;
05298    struct sla_trunk_ref *trunk_ref;
05299 
05300    AST_LIST_TRAVERSE(&sla_stations, station, entry) {
05301       AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
05302          if (trunk_ref->trunk != trunk || (inactive_only ? trunk_ref->chan : 0)
05303             || trunk_ref == exclude)
05304             continue;
05305          trunk_ref->state = state;
05306          ast_devstate_changed(sla_state_to_devstate(state), AST_DEVSTATE_CACHABLE,
05307                     "SLA:%s_%s", station->name, trunk->name);
05308          break;
05309       }
05310    }
05311 }
05312 
05313 struct run_station_args {
05314    struct sla_station *station;
05315    struct sla_trunk_ref *trunk_ref;
05316    ast_mutex_t *cond_lock;
05317    ast_cond_t *cond;
05318 };
05319 
05320 static void answer_trunk_chan(struct ast_channel *chan)
05321 {
05322    ast_answer(chan);
05323    ast_indicate(chan, -1);
05324 }
05325 
05326 static void *run_station(void *data)
05327 {
05328    struct sla_station *station;
05329    struct sla_trunk_ref *trunk_ref;
05330    struct ast_str *conf_name = ast_str_create(16);
05331    struct ast_flags64 conf_flags = { 0 };
05332    struct ast_conference *conf;
05333 
05334    {
05335       struct run_station_args *args = data;
05336       station = args->station;
05337       trunk_ref = args->trunk_ref;
05338       ast_mutex_lock(args->cond_lock);
05339       ast_cond_signal(args->cond);
05340       ast_mutex_unlock(args->cond_lock);
05341       /* args is no longer valid here. */
05342    }
05343 
05344    ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1);
05345    ast_str_set(&conf_name, 0, "SLA_%s", trunk_ref->trunk->name);
05346    ast_set_flag64(&conf_flags, 
05347       CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);
05348    answer_trunk_chan(trunk_ref->chan);
05349    conf = build_conf(ast_str_buffer(conf_name), "", "", 0, 0, 1, trunk_ref->chan, NULL);
05350    if (conf) {
05351       conf_run(trunk_ref->chan, conf, &conf_flags, NULL);
05352       dispose_conf(conf);
05353       conf = NULL;
05354    }
05355    trunk_ref->chan = NULL;
05356    if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&
05357       trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {
05358       ast_str_append(&conf_name, 0, ",K");
05359       admin_exec(NULL, ast_str_buffer(conf_name));
05360       trunk_ref->trunk->hold_stations = 0;
05361       sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
05362    }
05363 
05364    ast_dial_join(station->dial);
05365    ast_dial_destroy(station->dial);
05366    station->dial = NULL;
05367    ast_free(conf_name);
05368 
05369    return NULL;
05370 }
05371 
05372 static void sla_stop_ringing_trunk(struct sla_ringing_trunk *ringing_trunk)
05373 {
05374    char buf[80];
05375    struct sla_station_ref *station_ref;
05376 
05377    snprintf(buf, sizeof(buf), "SLA_%s,K", ringing_trunk->trunk->name);
05378    admin_exec(NULL, buf);
05379    sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
05380 
05381    while ((station_ref = AST_LIST_REMOVE_HEAD(&ringing_trunk->timed_out_stations, entry)))
05382       ast_free(station_ref);
05383 
05384    ast_free(ringing_trunk);
05385 }
05386 
05387 static void sla_stop_ringing_station(struct sla_ringing_station *ringing_station,
05388    enum sla_station_hangup hangup)
05389 {
05390    struct sla_ringing_trunk *ringing_trunk;
05391    struct sla_trunk_ref *trunk_ref;
05392    struct sla_station_ref *station_ref;
05393 
05394    ast_dial_join(ringing_station->station->dial);
05395    ast_dial_destroy(ringing_station->station->dial);
05396    ringing_station->station->dial = NULL;
05397 
05398    if (hangup == SLA_STATION_HANGUP_NORMAL)
05399       goto done;
05400 
05401    /* If the station is being hung up because of a timeout, then add it to the
05402     * list of timed out stations on each of the ringing trunks.  This is so
05403     * that when doing further processing to figure out which stations should be
05404     * ringing, which trunk to answer, determining timeouts, etc., we know which
05405     * ringing trunks we should ignore. */
05406    AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
05407       AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {
05408          if (ringing_trunk->trunk == trunk_ref->trunk)
05409             break;
05410       }
05411       if (!trunk_ref)
05412          continue;
05413       if (!(station_ref = sla_create_station_ref(ringing_station->station)))
05414          continue;
05415       AST_LIST_INSERT_TAIL(&ringing_trunk->timed_out_stations, station_ref, entry);
05416    }
05417 
05418 done:
05419    ast_free(ringing_station);
05420 }
05421 
05422 static void sla_dial_state_callback(struct ast_dial *dial)
05423 {
05424    sla_queue_event(SLA_EVENT_DIAL_STATE);
05425 }
05426 
05427 /*! \brief Check to see if dialing this station already timed out for this ringing trunk
05428  * \note Assumes sla.lock is locked
05429  */
05430 static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk,
05431    const struct sla_station *station)
05432 {
05433    struct sla_station_ref *timed_out_station;
05434 
05435    AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) {
05436       if (station == timed_out_station->station)
05437          return 1;
05438    }
05439 
05440    return 0;
05441 }
05442 
05443 /*! \brief Choose the highest priority ringing trunk for a station
05444  * \param station the station
05445  * \param rm remove the ringing trunk once selected
05446  * \param trunk_ref a place to store the pointer to this stations reference to
05447  *        the selected trunk
05448  * \return a pointer to the selected ringing trunk, or NULL if none found
05449  * \note Assumes that sla.lock is locked
05450  */
05451 static struct sla_ringing_trunk *sla_choose_ringing_trunk(struct sla_station *station, 
05452    struct sla_trunk_ref **trunk_ref, int rm)
05453 {
05454    struct sla_trunk_ref *s_trunk_ref;
05455    struct sla_ringing_trunk *ringing_trunk = NULL;
05456 
05457    AST_LIST_TRAVERSE(&station->trunks, s_trunk_ref, entry) {
05458       AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
05459          /* Make sure this is the trunk we're looking for */
05460          if (s_trunk_ref->trunk != ringing_trunk->trunk)
05461             continue;
05462 
05463          /* This trunk on the station is ringing.  But, make sure this station
05464           * didn't already time out while this trunk was ringing. */
05465          if (sla_check_timed_out_station(ringing_trunk, station))
05466             continue;
05467 
05468          if (rm)
05469             AST_LIST_REMOVE_CURRENT(entry);
05470 
05471          if (trunk_ref)
05472             *trunk_ref = s_trunk_ref;
05473 
05474          break;
05475       }
05476       AST_LIST_TRAVERSE_SAFE_END;
05477    
05478       if (ringing_trunk)
05479          break;
05480    }
05481 
05482    return ringing_trunk;
05483 }
05484 
05485 static void sla_handle_dial_state_event(void)
05486 {
05487    struct sla_ringing_station *ringing_station;
05488 
05489    AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {
05490       struct sla_trunk_ref *s_trunk_ref = NULL;
05491       struct sla_ringing_trunk *ringing_trunk = NULL;
05492       struct run_station_args args;
05493       enum ast_dial_result dial_res;
05494       pthread_t dont_care;
05495       ast_mutex_t cond_lock;
05496       ast_cond_t cond;
05497 
05498       switch ((dial_res = ast_dial_state(ringing_station->station->dial))) {
05499       case AST_DIAL_RESULT_HANGUP:
05500       case AST_DIAL_RESULT_INVALID:
05501       case AST_DIAL_RESULT_FAILED:
05502       case AST_DIAL_RESULT_TIMEOUT:
05503       case AST_DIAL_RESULT_UNANSWERED:
05504          AST_LIST_REMOVE_CURRENT(entry);
05505          sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_NORMAL);
05506          break;
05507       case AST_DIAL_RESULT_ANSWERED:
05508          AST_LIST_REMOVE_CURRENT(entry);
05509          /* Find the appropriate trunk to answer. */
05510          ast_mutex_lock(&sla.lock);
05511          ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, &s_trunk_ref, 1);
05512          ast_mutex_unlock(&sla.lock);
05513          if (!ringing_trunk) {
05514             ast_debug(1, "Found no ringing trunk for station '%s' to answer!\n", ringing_station->station->name);
05515             break;
05516          }
05517          /* Track the channel that answered this trunk */
05518          s_trunk_ref->chan = ast_dial_answered(ringing_station->station->dial);
05519          /* Actually answer the trunk */
05520          answer_trunk_chan(ringing_trunk->trunk->chan);
05521          sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
05522          /* Now, start a thread that will connect this station to the trunk.  The rest of
05523           * the code here sets up the thread and ensures that it is able to save the arguments
05524           * before they are no longer valid since they are allocated on the stack. */
05525          args.trunk_ref = s_trunk_ref;
05526          args.station = ringing_station->station;
05527          args.cond = &cond;
05528          args.cond_lock = &cond_lock;
05529          ast_free(ringing_trunk);
05530          ast_free(ringing_station);
05531          ast_mutex_init(&cond_lock);
05532          ast_cond_init(&cond, NULL);
05533          ast_mutex_lock(&cond_lock);
05534          ast_pthread_create_detached_background(&dont_care, NULL, run_station, &args);
05535          ast_cond_wait(&cond, &cond_lock);
05536          ast_mutex_unlock(&cond_lock);
05537          ast_mutex_destroy(&cond_lock);
05538          ast_cond_destroy(&cond);
05539          break;
05540       case AST_DIAL_RESULT_TRYING:
05541       case AST_DIAL_RESULT_RINGING:
05542       case AST_DIAL_RESULT_PROGRESS:
05543       case AST_DIAL_RESULT_PROCEEDING:
05544          break;
05545       }
05546       if (dial_res == AST_DIAL_RESULT_ANSWERED) {
05547          /* Queue up reprocessing ringing trunks, and then ringing stations again */
05548          sla_queue_event(SLA_EVENT_RINGING_TRUNK);
05549          sla_queue_event(SLA_EVENT_DIAL_STATE);
05550          break;
05551       }
05552    }
05553    AST_LIST_TRAVERSE_SAFE_END;
05554 }
05555 
05556 /*! \brief Check to see if this station is already ringing 
05557  * \note Assumes sla.lock is locked 
05558  */
05559 static int sla_check_ringing_station(const struct sla_station *station)
05560 {
05561    struct sla_ringing_station *ringing_station;
05562 
05563    AST_LIST_TRAVERSE(&sla.ringing_stations, ringing_station, entry) {
05564       if (station == ringing_station->station)
05565          return 1;
05566    }
05567 
05568    return 0;
05569 }
05570 
05571 /*! \brief Check to see if this station has failed to be dialed in the past minute
05572  * \note assumes sla.lock is locked
05573  */
05574 static int sla_check_failed_station(const struct sla_station *station)
05575 {
05576    struct sla_failed_station *failed_station;
05577    int res = 0;
05578 
05579    AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.failed_stations, failed_station, entry) {
05580       if (station != failed_station->station)
05581          continue;
05582       if (ast_tvdiff_ms(ast_tvnow(), failed_station->last_try) > 1000) {
05583          AST_LIST_REMOVE_CURRENT(entry);
05584          ast_free(failed_station);
05585          break;
05586       }
05587       res = 1;
05588    }
05589    AST_LIST_TRAVERSE_SAFE_END
05590 
05591    return res;
05592 }
05593 
05594 /*! \brief Ring a station
05595  * \note Assumes sla.lock is locked
05596  */
05597 static int sla_ring_station(struct sla_ringing_trunk *ringing_trunk, struct sla_station *station)
05598 {
05599    char *tech, *tech_data;
05600    struct ast_dial *dial;
05601    struct sla_ringing_station *ringing_station;
05602    enum ast_dial_result res;
05603    int caller_is_saved;
05604    struct ast_party_caller caller;
05605 
05606    if (!(dial = ast_dial_create()))
05607       return -1;
05608 
05609    ast_dial_set_state_callback(dial, sla_dial_state_callback);
05610    tech_data = ast_strdupa(station->device);
05611    tech = strsep(&tech_data, "/");
05612 
05613    if (ast_dial_append(dial, tech, tech_data) == -1) {
05614       ast_dial_destroy(dial);
05615       return -1;
05616    }
05617 
05618    /* Do we need to save off the caller ID data? */
05619    caller_is_saved = 0;
05620    if (!sla.attempt_callerid) {
05621       caller_is_saved = 1;
05622       caller = ringing_trunk->trunk->chan->caller;
05623       ast_party_caller_init(&ringing_trunk->trunk->chan->caller);
05624    }
05625 
05626    res = ast_dial_run(dial, ringing_trunk->trunk->chan, 1);
05627    
05628    /* Restore saved caller ID */
05629    if (caller_is_saved) {
05630       ast_party_caller_free(&ringing_trunk->trunk->chan->caller);
05631       ringing_trunk->trunk->chan->caller = caller;
05632    }
05633    
05634    if (res != AST_DIAL_RESULT_TRYING) {
05635       struct sla_failed_station *failed_station;
05636       ast_dial_destroy(dial);
05637       if (!(failed_station = ast_calloc(1, sizeof(*failed_station))))
05638          return -1;
05639       failed_station->station = station;
05640       failed_station->last_try = ast_tvnow();
05641       AST_LIST_INSERT_HEAD(&sla.failed_stations, failed_station, entry);
05642       return -1;
05643    }
05644    if (!(ringing_station = sla_create_ringing_station(station))) {
05645       ast_dial_join(dial);
05646       ast_dial_destroy(dial);
05647       return -1;
05648    }
05649 
05650    station->dial = dial;
05651 
05652    AST_LIST_INSERT_HEAD(&sla.ringing_stations, ringing_station, entry);
05653 
05654    return 0;
05655 }
05656 
05657 /*! \brief Check to see if a station is in use
05658  */
05659 static int sla_check_inuse_station(const struct sla_station *station)
05660 {
05661    struct sla_trunk_ref *trunk_ref;
05662 
05663    AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
05664       if (trunk_ref->chan)
05665          return 1;
05666    }
05667 
05668    return 0;
05669 }
05670 
05671 static struct sla_trunk_ref *sla_find_trunk_ref(const struct sla_station *station,
05672    const struct sla_trunk *trunk)
05673 {
05674    struct sla_trunk_ref *trunk_ref = NULL;
05675 
05676    AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
05677       if (trunk_ref->trunk == trunk)
05678          break;
05679    }
05680 
05681    return trunk_ref;
05682 }
05683 
05684 /*! \brief Calculate the ring delay for a given ringing trunk on a station
05685  * \param station the station
05686  * \param ringing_trunk the trunk.  If NULL, the highest priority ringing trunk will be used
05687  * \return the number of ms left before the delay is complete, or INT_MAX if there is no delay
05688  */
05689 static int sla_check_station_delay(struct sla_station *station, 
05690    struct sla_ringing_trunk *ringing_trunk)
05691 {
05692    struct sla_trunk_ref *trunk_ref;
05693    unsigned int delay = UINT_MAX;
05694    int time_left, time_elapsed;
05695 
05696    if (!ringing_trunk)
05697       ringing_trunk = sla_choose_ringing_trunk(station, &trunk_ref, 0);
05698    else
05699       trunk_ref = sla_find_trunk_ref(station, ringing_trunk->trunk);
05700 
05701    if (!ringing_trunk || !trunk_ref)
05702       return delay;
05703 
05704    /* If this station has a ring delay specific to the highest priority
05705     * ringing trunk, use that.  Otherwise, use the ring delay specified
05706     * globally for the station. */
05707    delay = trunk_ref->ring_delay;
05708    if (!delay)
05709       delay = station->ring_delay;
05710    if (!delay)
05711       return INT_MAX;
05712 
05713    time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);
05714    time_left = (delay * 1000) - time_elapsed;
05715 
05716    return time_left;
05717 }
05718 
05719 /*! \brief Ring stations based on current set of ringing trunks
05720  * \note Assumes that sla.lock is locked
05721  */
05722 static void sla_ring_stations(void)
05723 {
05724    struct sla_station_ref *station_ref;
05725    struct sla_ringing_trunk *ringing_trunk;
05726 
05727    /* Make sure that every station that uses at least one of the ringing
05728     * trunks, is ringing. */
05729    AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
05730       AST_LIST_TRAVERSE(&ringing_trunk->trunk->stations, station_ref, entry) {
05731          int time_left;
05732 
05733          /* Is this station already ringing? */
05734          if (sla_check_ringing_station(station_ref->station))
05735             continue;
05736 
05737          /* Is this station already in a call? */
05738          if (sla_check_inuse_station(station_ref->station))
05739             continue;
05740 
05741          /* Did we fail to dial this station earlier?  If so, has it been
05742           * a minute since we tried? */
05743          if (sla_check_failed_station(station_ref->station))
05744             continue;
05745 
05746          /* If this station already timed out while this trunk was ringing,
05747           * do not dial it again for this ringing trunk. */
05748          if (sla_check_timed_out_station(ringing_trunk, station_ref->station))
05749             continue;
05750 
05751          /* Check for a ring delay in progress */
05752          time_left = sla_check_station_delay(station_ref->station, ringing_trunk);
05753          if (time_left != INT_MAX && time_left > 0)
05754             continue;
05755 
05756          /* It is time to make this station begin to ring.  Do it! */
05757          sla_ring_station(ringing_trunk, station_ref->station);
05758       }
05759    }
05760    /* Now, all of the stations that should be ringing, are ringing. */
05761 }
05762 
05763 static void sla_hangup_stations(void)
05764 {
05765    struct sla_trunk_ref *trunk_ref;
05766    struct sla_ringing_station *ringing_station;
05767 
05768    AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {
05769       AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {
05770          struct sla_ringing_trunk *ringing_trunk;
05771          ast_mutex_lock(&sla.lock);
05772          AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
05773             if (trunk_ref->trunk == ringing_trunk->trunk)
05774                break;
05775          }
05776          ast_mutex_unlock(&sla.lock);
05777          if (ringing_trunk)
05778             break;
05779       }
05780       if (!trunk_ref) {
05781          AST_LIST_REMOVE_CURRENT(entry);
05782          ast_dial_join(ringing_station->station->dial);
05783          ast_dial_destroy(ringing_station->station->dial);
05784          ringing_station->station->dial = NULL;
05785          ast_free(ringing_station);
05786       }
05787    }
05788    AST_LIST_TRAVERSE_SAFE_END
05789 }
05790 
05791 static void sla_handle_ringing_trunk_event(void)
05792 {
05793    ast_mutex_lock(&sla.lock);
05794    sla_ring_stations();
05795    ast_mutex_unlock(&sla.lock);
05796 
05797    /* Find stations that shouldn't be ringing anymore. */
05798    sla_hangup_stations();
05799 }
05800 
05801 static void sla_handle_hold_event(struct sla_event *event)
05802 {
05803    ast_atomic_fetchadd_int((int *) &event->trunk_ref->trunk->hold_stations, 1);
05804    event->trunk_ref->state = SLA_TRUNK_STATE_ONHOLD_BYME;
05805    ast_devstate_changed(AST_DEVICE_ONHOLD, AST_DEVSTATE_CACHABLE, "SLA:%s_%s",
05806               event->station->name, event->trunk_ref->trunk->name);
05807    sla_change_trunk_state(event->trunk_ref->trunk, SLA_TRUNK_STATE_ONHOLD, 
05808       INACTIVE_TRUNK_REFS, event->trunk_ref);
05809 
05810    if (event->trunk_ref->trunk->active_stations == 1) {
05811       /* The station putting it on hold is the only one on the call, so start
05812        * Music on hold to the trunk. */
05813       event->trunk_ref->trunk->on_hold = 1;
05814       ast_indicate(event->trunk_ref->trunk->chan, AST_CONTROL_HOLD);
05815    }
05816 
05817    ast_softhangup(event->trunk_ref->chan, AST_SOFTHANGUP_DEV);
05818    event->trunk_ref->chan = NULL;
05819 }
05820 
05821 /*! \brief Process trunk ring timeouts
05822  * \note Called with sla.lock locked
05823  * \return non-zero if a change to the ringing trunks was made
05824  */
05825 static int sla_calc_trunk_timeouts(unsigned int *timeout)
05826 {
05827    struct sla_ringing_trunk *ringing_trunk;
05828    int res = 0;
05829 
05830    AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
05831       int time_left, time_elapsed;
05832       if (!ringing_trunk->trunk->ring_timeout)
05833          continue;
05834       time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);
05835       time_left = (ringing_trunk->trunk->ring_timeout * 1000) - time_elapsed;
05836       if (time_left <= 0) {
05837          pbx_builtin_setvar_helper(ringing_trunk->trunk->chan, "SLATRUNK_STATUS", "RINGTIMEOUT");
05838          AST_LIST_REMOVE_CURRENT(entry);
05839          sla_stop_ringing_trunk(ringing_trunk);
05840          res = 1;
05841          continue;
05842       }
05843       if (time_left < *timeout)
05844          *timeout = time_left;
05845    }
05846    AST_LIST_TRAVERSE_SAFE_END;
05847 
05848    return res;
05849 }
05850 
05851 /*! \brief Process station ring timeouts
05852  * \note Called with sla.lock locked
05853  * \return non-zero if a change to the ringing stations was made
05854  */
05855 static int sla_calc_station_timeouts(unsigned int *timeout)
05856 {
05857    struct sla_ringing_trunk *ringing_trunk;
05858    struct sla_ringing_station *ringing_station;
05859    int res = 0;
05860 
05861    AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {
05862       unsigned int ring_timeout = 0;
05863       int time_elapsed, time_left = INT_MAX, final_trunk_time_left = INT_MIN;
05864       struct sla_trunk_ref *trunk_ref;
05865 
05866       /* If there are any ring timeouts specified for a specific trunk
05867        * on the station, then use the highest per-trunk ring timeout.
05868        * Otherwise, use the ring timeout set for the entire station. */
05869       AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {
05870          struct sla_station_ref *station_ref;
05871          int trunk_time_elapsed, trunk_time_left;
05872 
05873          AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
05874             if (ringing_trunk->trunk == trunk_ref->trunk)
05875                break;
05876          }
05877          if (!ringing_trunk)
05878             continue;
05879 
05880          /* If there is a trunk that is ringing without a timeout, then the
05881           * only timeout that could matter is a global station ring timeout. */
05882          if (!trunk_ref->ring_timeout)
05883             break;
05884 
05885          /* This trunk on this station is ringing and has a timeout.
05886           * However, make sure this trunk isn't still ringing from a
05887           * previous timeout.  If so, don't consider it. */
05888          AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, station_ref, entry) {
05889             if (station_ref->station == ringing_station->station)
05890                break;
05891          }
05892          if (station_ref)
05893             continue;
05894 
05895          trunk_time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);
05896          trunk_time_left = (trunk_ref->ring_timeout * 1000) - trunk_time_elapsed;
05897          if (trunk_time_left > final_trunk_time_left)
05898             final_trunk_time_left = trunk_time_left;
05899       }
05900 
05901       /* No timeout was found for ringing trunks, and no timeout for the entire station */
05902       if (final_trunk_time_left == INT_MIN && !ringing_station->station->ring_timeout)
05903          continue;
05904 
05905       /* Compute how much time is left for a global station timeout */
05906       if (ringing_station->station->ring_timeout) {
05907          ring_timeout = ringing_station->station->ring_timeout;
05908          time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_station->ring_begin);
05909          time_left = (ring_timeout * 1000) - time_elapsed;
05910       }
05911 
05912       /* If the time left based on the per-trunk timeouts is smaller than the
05913        * global station ring timeout, use that. */
05914       if (final_trunk_time_left > INT_MIN && final_trunk_time_left < time_left)
05915          time_left = final_trunk_time_left;
05916 
05917       /* If there is no time left, the station needs to stop ringing */
05918       if (time_left <= 0) {
05919          AST_LIST_REMOVE_CURRENT(entry);
05920          sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_TIMEOUT);
05921          res = 1;
05922          continue;
05923       }
05924 
05925       /* There is still some time left for this station to ring, so save that
05926        * timeout if it is the first event scheduled to occur */
05927       if (time_left < *timeout)
05928          *timeout = time_left;
05929    }
05930    AST_LIST_TRAVERSE_SAFE_END;
05931 
05932    return res;
05933 }
05934 
05935 /*! \brief Calculate the ring delay for a station
05936  * \note Assumes sla.lock is locked
05937  */
05938 static int sla_calc_station_delays(unsigned int *timeout)
05939 {
05940    struct sla_station *station;
05941    int res = 0;
05942 
05943    AST_LIST_TRAVERSE(&sla_stations, station, entry) {
05944       struct sla_ringing_trunk *ringing_trunk;
05945       int time_left;
05946 
05947       /* Ignore stations already ringing */
05948       if (sla_check_ringing_station(station))
05949          continue;
05950 
05951       /* Ignore stations already on a call */
05952       if (sla_check_inuse_station(station))
05953          continue;
05954 
05955       /* Ignore stations that don't have one of their trunks ringing */
05956       if (!(ringing_trunk = sla_choose_ringing_trunk(station, NULL, 0)))
05957          continue;
05958 
05959       if ((time_left = sla_check_station_delay(station, ringing_trunk)) == INT_MAX)
05960          continue;
05961 
05962       /* If there is no time left, then the station needs to start ringing.
05963        * Return non-zero so that an event will be queued up an event to 
05964        * make that happen. */
05965       if (time_left <= 0) {
05966          res = 1;
05967          continue;
05968       }
05969 
05970       if (time_left < *timeout)
05971          *timeout = time_left;
05972    }
05973 
05974    return res;
05975 }
05976 
05977 /*! \brief Calculate the time until the next known event
05978  *  \note Called with sla.lock locked */
05979 static int sla_process_timers(struct timespec *ts)
05980 {
05981    unsigned int timeout = UINT_MAX;
05982    struct timeval wait;
05983    unsigned int change_made = 0;
05984 
05985    /* Check for ring timeouts on ringing trunks */
05986    if (sla_calc_trunk_timeouts(&timeout))
05987       change_made = 1;
05988 
05989    /* Check for ring timeouts on ringing stations */
05990    if (sla_calc_station_timeouts(&timeout))
05991       change_made = 1;
05992 
05993    /* Check for station ring delays */
05994    if (sla_calc_station_delays(&timeout))
05995       change_made = 1;
05996 
05997    /* queue reprocessing of ringing trunks */
05998    if (change_made)
05999       sla_queue_event_nolock(SLA_EVENT_RINGING_TRUNK);
06000 
06001    /* No timeout */
06002    if (timeout == UINT_MAX)
06003       return 0;
06004 
06005    if (ts) {
06006       wait = ast_tvadd(ast_tvnow(), ast_samp2tv(timeout, 1000));
06007       ts->tv_sec = wait.tv_sec;
06008       ts->tv_nsec = wait.tv_usec * 1000;
06009    }
06010 
06011    return 1;
06012 }
06013 
06014 static int sla_load_config(int reload);
06015 
06016 /*! \brief Check if we can do a reload of SLA, and do it if we can */
06017 static void sla_check_reload(void)
06018 {
06019    struct sla_station *station;
06020    struct sla_trunk *trunk;
06021 
06022    ast_mutex_lock(&sla.lock);
06023 
06024    if (!AST_LIST_EMPTY(&sla.event_q) || !AST_LIST_EMPTY(&sla.ringing_trunks) 
06025       || !AST_LIST_EMPTY(&sla.ringing_stations)) {
06026       ast_mutex_unlock(&sla.lock);
06027       return;
06028    }
06029 
06030    AST_RWLIST_RDLOCK(&sla_stations);
06031    AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
06032       if (station->ref_count)
06033          break;
06034    }
06035    AST_RWLIST_UNLOCK(&sla_stations);
06036    if (station) {
06037       ast_mutex_unlock(&sla.lock);
06038       return;
06039    }
06040 
06041    AST_RWLIST_RDLOCK(&sla_trunks);
06042    AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
06043       if (trunk->ref_count)
06044          break;
06045    }
06046    AST_RWLIST_UNLOCK(&sla_trunks);
06047    if (trunk) {
06048       ast_mutex_unlock(&sla.lock);
06049       return;
06050    }
06051 
06052    /* yay */
06053    sla_load_config(1);
06054    sla.reload = 0;
06055 
06056    ast_mutex_unlock(&sla.lock);
06057 }
06058 
06059 static void *sla_thread(void *data)
06060 {
06061    struct sla_failed_station *failed_station;
06062    struct sla_ringing_station *ringing_station;
06063 
06064    ast_mutex_lock(&sla.lock);
06065 
06066    while (!sla.stop) {
06067       struct sla_event *event;
06068       struct timespec ts = { 0, };
06069       unsigned int have_timeout = 0;
06070 
06071       if (AST_LIST_EMPTY(&sla.event_q)) {
06072          if ((have_timeout = sla_process_timers(&ts)))
06073             ast_cond_timedwait(&sla.cond, &sla.lock, &ts);
06074          else
06075             ast_cond_wait(&sla.cond, &sla.lock);
06076          if (sla.stop)
06077             break;
06078       }
06079 
06080       if (have_timeout)
06081          sla_process_timers(NULL);
06082 
06083       while ((event = AST_LIST_REMOVE_HEAD(&sla.event_q, entry))) {
06084          ast_mutex_unlock(&sla.lock);
06085          switch (event->type) {
06086          case SLA_EVENT_HOLD:
06087             sla_handle_hold_event(event);
06088             break;
06089          case SLA_EVENT_DIAL_STATE:
06090             sla_handle_dial_state_event();
06091             break;
06092          case SLA_EVENT_RINGING_TRUNK:
06093             sla_handle_ringing_trunk_event();
06094             break;
06095          case SLA_EVENT_RELOAD:
06096             sla.reload = 1;
06097          case SLA_EVENT_CHECK_RELOAD:
06098             break;
06099          }
06100          ast_free(event);
06101          ast_mutex_lock(&sla.lock);
06102       }
06103 
06104       if (sla.reload) {
06105          sla_check_reload();
06106       }
06107    }
06108 
06109    ast_mutex_unlock(&sla.lock);
06110 
06111    while ((ringing_station = AST_LIST_REMOVE_HEAD(&sla.ringing_stations, entry)))
06112       ast_free(ringing_station);
06113 
06114    while ((failed_station = AST_LIST_REMOVE_HEAD(&sla.failed_stations, entry)))
06115       ast_free(failed_station);
06116 
06117    return NULL;
06118 }
06119 
06120 struct dial_trunk_args {
06121    struct sla_trunk_ref *trunk_ref;
06122    struct sla_station *station;
06123    ast_mutex_t *cond_lock;
06124    ast_cond_t *cond;
06125 };
06126 
06127 static void *dial_trunk(void *data)
06128 {
06129    struct dial_trunk_args *args = data;
06130    struct ast_dial *dial;
06131    char *tech, *tech_data;
06132    enum ast_dial_result dial_res;
06133    char conf_name[MAX_CONFNUM];
06134    struct ast_conference *conf;
06135    struct ast_flags64 conf_flags = { 0 };
06136    struct sla_trunk_ref *trunk_ref = args->trunk_ref;
06137    int caller_is_saved;
06138    struct ast_party_caller caller;
06139 
06140    if (!(dial = ast_dial_create())) {
06141       ast_mutex_lock(args->cond_lock);
06142       ast_cond_signal(args->cond);
06143       ast_mutex_unlock(args->cond_lock);
06144       return NULL;
06145    }
06146 
06147    tech_data = ast_strdupa(trunk_ref->trunk->device);
06148    tech = strsep(&tech_data, "/");
06149    if (ast_dial_append(dial, tech, tech_data) == -1) {
06150       ast_mutex_lock(args->cond_lock);
06151       ast_cond_signal(args->cond);
06152       ast_mutex_unlock(args->cond_lock);
06153       ast_dial_destroy(dial);
06154       return NULL;
06155    }
06156 
06157    /* Do we need to save of the caller ID data? */
06158    caller_is_saved = 0;
06159    if (!sla.attempt_callerid) {
06160       caller_is_saved = 1;
06161       caller = trunk_ref->chan->caller;
06162       ast_party_caller_init(&trunk_ref->chan->caller);
06163    }
06164 
06165    dial_res = ast_dial_run(dial, trunk_ref->chan, 1);
06166 
06167    /* Restore saved caller ID */
06168    if (caller_is_saved) {
06169       ast_party_caller_free(&trunk_ref->chan->caller);
06170       trunk_ref->chan->caller = caller;
06171    }
06172 
06173    if (dial_res != AST_DIAL_RESULT_TRYING) {
06174       ast_mutex_lock(args->cond_lock);
06175       ast_cond_signal(args->cond);
06176       ast_mutex_unlock(args->cond_lock);
06177       ast_dial_destroy(dial);
06178       return NULL;
06179    }
06180 
06181    for (;;) {
06182       unsigned int done = 0;
06183       switch ((dial_res = ast_dial_state(dial))) {
06184       case AST_DIAL_RESULT_ANSWERED:
06185          trunk_ref->trunk->chan = ast_dial_answered(dial);
06186       case AST_DIAL_RESULT_HANGUP:
06187       case AST_DIAL_RESULT_INVALID:
06188       case AST_DIAL_RESULT_FAILED:
06189       case AST_DIAL_RESULT_TIMEOUT:
06190       case AST_DIAL_RESULT_UNANSWERED:
06191          done = 1;
06192       case AST_DIAL_RESULT_TRYING:
06193       case AST_DIAL_RESULT_RINGING:
06194       case AST_DIAL_RESULT_PROGRESS:
06195       case AST_DIAL_RESULT_PROCEEDING:
06196          break;
06197       }
06198       if (done)
06199          break;
06200    }
06201 
06202    if (!trunk_ref->trunk->chan) {
06203       ast_mutex_lock(args->cond_lock);
06204       ast_cond_signal(args->cond);
06205       ast_mutex_unlock(args->cond_lock);
06206       ast_dial_join(dial);
06207       ast_dial_destroy(dial);
06208       return NULL;
06209    }
06210 
06211    snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);
06212    ast_set_flag64(&conf_flags, 
06213       CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | 
06214       CONFFLAG_PASS_DTMF | CONFFLAG_SLA_TRUNK);
06215    conf = build_conf(conf_name, "", "", 1, 1, 1, trunk_ref->trunk->chan, NULL);
06216 
06217    ast_mutex_lock(args->cond_lock);
06218    ast_cond_signal(args->cond);
06219    ast_mutex_unlock(args->cond_lock);
06220 
06221    if (conf) {
06222       conf_run(trunk_ref->trunk->chan, conf, &conf_flags, NULL);
06223       dispose_conf(conf);
06224       conf = NULL;
06225    }
06226 
06227    /* If the trunk is going away, it is definitely now IDLE. */
06228    sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
06229 
06230    trunk_ref->trunk->chan = NULL;
06231    trunk_ref->trunk->on_hold = 0;
06232 
06233    ast_dial_join(dial);
06234    ast_dial_destroy(dial);
06235 
06236    return NULL;
06237 }
06238 
06239 /*! \brief For a given station, choose the highest priority idle trunk
06240  */
06241 static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station)
06242 {
06243    struct sla_trunk_ref *trunk_ref = NULL;
06244 
06245    AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
06246       if (trunk_ref->state == SLA_TRUNK_STATE_IDLE)
06247          break;
06248    }
06249 
06250    return trunk_ref;
06251 }
06252 
06253 static int sla_station_exec(struct ast_channel *chan, const char *data)
06254 {
06255    char *station_name, *trunk_name;
06256    struct sla_station *station;
06257    struct sla_trunk_ref *trunk_ref = NULL;
06258    char conf_name[MAX_CONFNUM];
06259    struct ast_flags64 conf_flags = { 0 };
06260    struct ast_conference *conf;
06261 
06262    if (ast_strlen_zero(data)) {
06263       ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");
06264       pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");
06265       return 0;
06266    }
06267 
06268    trunk_name = ast_strdupa(data);
06269    station_name = strsep(&trunk_name, "_");
06270 
06271    if (ast_strlen_zero(station_name)) {
06272       ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");
06273       pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");
06274       return 0;
06275    }
06276 
06277    AST_RWLIST_RDLOCK(&sla_stations);
06278    station = sla_find_station(station_name);
06279    if (station)
06280       ast_atomic_fetchadd_int((int *) &station->ref_count, 1);
06281    AST_RWLIST_UNLOCK(&sla_stations);
06282 
06283    if (!station) {
06284       ast_log(LOG_WARNING, "Station '%s' not found!\n", station_name);
06285       pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");
06286       sla_queue_event(SLA_EVENT_CHECK_RELOAD);
06287       return 0;
06288    }
06289 
06290    AST_RWLIST_RDLOCK(&sla_trunks);
06291    if (!ast_strlen_zero(trunk_name)) {
06292       trunk_ref = sla_find_trunk_ref_byname(station, trunk_name);
06293    } else
06294       trunk_ref = sla_choose_idle_trunk(station);
06295    AST_RWLIST_UNLOCK(&sla_trunks);
06296 
06297    if (!trunk_ref) {
06298       if (ast_strlen_zero(trunk_name))
06299          ast_log(LOG_NOTICE, "No trunks available for call.\n");
06300       else {
06301          ast_log(LOG_NOTICE, "Can't join existing call on trunk "
06302             "'%s' due to access controls.\n", trunk_name);
06303       }
06304       pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");
06305       ast_atomic_fetchadd_int((int *) &station->ref_count, -1);
06306       sla_queue_event(SLA_EVENT_CHECK_RELOAD);
06307       return 0;
06308    }
06309 
06310    if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME) {
06311       if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->hold_stations) == 1)
06312          sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
06313       else {
06314          trunk_ref->state = SLA_TRUNK_STATE_UP;
06315          ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE,
06316                     "SLA:%s_%s", station->name, trunk_ref->trunk->name);
06317       }
06318    } else if (trunk_ref->state == SLA_TRUNK_STATE_RINGING) {
06319       struct sla_ringing_trunk *ringing_trunk;
06320 
06321       ast_mutex_lock(&sla.lock);
06322       AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
06323          if (ringing_trunk->trunk == trunk_ref->trunk) {
06324             AST_LIST_REMOVE_CURRENT(entry);
06325             break;
06326          }
06327       }
06328       AST_LIST_TRAVERSE_SAFE_END
06329       ast_mutex_unlock(&sla.lock);
06330 
06331       if (ringing_trunk) {
06332          answer_trunk_chan(ringing_trunk->trunk->chan);
06333          sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
06334 
06335          free(ringing_trunk);
06336 
06337          /* Queue up reprocessing ringing trunks, and then ringing stations again */
06338          sla_queue_event(SLA_EVENT_RINGING_TRUNK);
06339          sla_queue_event(SLA_EVENT_DIAL_STATE);
06340       }
06341    }
06342 
06343    trunk_ref->chan = chan;
06344 
06345    if (!trunk_ref->trunk->chan) {
06346       ast_mutex_t cond_lock;
06347       ast_cond_t cond;
06348       pthread_t dont_care;
06349       struct dial_trunk_args args = {
06350          .trunk_ref = trunk_ref,
06351          .station = station,
06352          .cond_lock = &cond_lock,
06353          .cond = &cond,
06354       };
06355       sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
06356       /* Create a thread to dial the trunk and dump it into the conference.
06357        * However, we want to wait until the trunk has been dialed and the
06358        * conference is created before continuing on here. */
06359       ast_autoservice_start(chan);
06360       ast_mutex_init(&cond_lock);
06361       ast_cond_init(&cond, NULL);
06362       ast_mutex_lock(&cond_lock);
06363       ast_pthread_create_detached_background(&dont_care, NULL, dial_trunk, &args);
06364       ast_cond_wait(&cond, &cond_lock);
06365       ast_mutex_unlock(&cond_lock);
06366       ast_mutex_destroy(&cond_lock);
06367       ast_cond_destroy(&cond);
06368       ast_autoservice_stop(chan);
06369       if (!trunk_ref->trunk->chan) {
06370          ast_debug(1, "Trunk didn't get created. chan: %lx\n", (long) trunk_ref->trunk->chan);
06371          pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");
06372          sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
06373          trunk_ref->chan = NULL;
06374          ast_atomic_fetchadd_int((int *) &station->ref_count, -1);
06375          sla_queue_event(SLA_EVENT_CHECK_RELOAD);
06376          return 0;
06377       }
06378    }
06379 
06380    if (ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1) == 0 &&
06381       trunk_ref->trunk->on_hold) {
06382       trunk_ref->trunk->on_hold = 0;
06383       ast_indicate(trunk_ref->trunk->chan, AST_CONTROL_UNHOLD);
06384       sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
06385    }
06386 
06387    snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);
06388    ast_set_flag64(&conf_flags, 
06389       CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);
06390    ast_answer(chan);
06391    conf = build_conf(conf_name, "", "", 0, 0, 1, chan, NULL);
06392    if (conf) {
06393       conf_run(chan, conf, &conf_flags, NULL);
06394       dispose_conf(conf);
06395       conf = NULL;
06396    }
06397    trunk_ref->chan = NULL;
06398    if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&
06399       trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {
06400       strncat(conf_name, ",K", sizeof(conf_name) - strlen(conf_name) - 1);
06401       admin_exec(NULL, conf_name);
06402       trunk_ref->trunk->hold_stations = 0;
06403       sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
06404    }
06405    
06406    pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "SUCCESS");
06407 
06408    ast_atomic_fetchadd_int((int *) &station->ref_count, -1);
06409    sla_queue_event(SLA_EVENT_CHECK_RELOAD);
06410 
06411    return 0;
06412 }
06413 
06414 static struct sla_trunk_ref *create_trunk_ref(struct sla_trunk *trunk)
06415 {
06416    struct sla_trunk_ref *trunk_ref;
06417 
06418    if (!(trunk_ref = ast_calloc(1, sizeof(*trunk_ref))))
06419       return NULL;
06420 
06421    trunk_ref->trunk = trunk;
06422 
06423    return trunk_ref;
06424 }
06425 
06426 static struct sla_ringing_trunk *queue_ringing_trunk(struct sla_trunk *trunk)
06427 {
06428    struct sla_ringing_trunk *ringing_trunk;
06429 
06430    if (!(ringing_trunk = ast_calloc(1, sizeof(*ringing_trunk))))
06431       return NULL;
06432    
06433    ringing_trunk->trunk = trunk;
06434    ringing_trunk->ring_begin = ast_tvnow();
06435 
06436    sla_change_trunk_state(trunk, SLA_TRUNK_STATE_RINGING, ALL_TRUNK_REFS, NULL);
06437 
06438    ast_mutex_lock(&sla.lock);
06439    AST_LIST_INSERT_HEAD(&sla.ringing_trunks, ringing_trunk, entry);
06440    ast_mutex_unlock(&sla.lock);
06441 
06442    sla_queue_event(SLA_EVENT_RINGING_TRUNK);
06443 
06444    return ringing_trunk;
06445 }
06446 
06447 enum {
06448    SLA_TRUNK_OPT_MOH = (1 << 0),
06449 };
06450 
06451 enum {
06452    SLA_TRUNK_OPT_ARG_MOH_CLASS = 0,
06453    SLA_TRUNK_OPT_ARG_ARRAY_SIZE = 1,
06454 };
06455 
06456 AST_APP_OPTIONS(sla_trunk_opts, BEGIN_OPTIONS
06457    AST_APP_OPTION_ARG('M', SLA_TRUNK_OPT_MOH, SLA_TRUNK_OPT_ARG_MOH_CLASS),
06458 END_OPTIONS );
06459 
06460 static int sla_trunk_exec(struct ast_channel *chan, const char *data)
06461 {
06462    char conf_name[MAX_CONFNUM];
06463    struct ast_conference *conf;
06464    struct ast_flags64 conf_flags = { 0 };
06465    struct sla_trunk *trunk;
06466    struct sla_ringing_trunk *ringing_trunk;
06467    AST_DECLARE_APP_ARGS(args,
06468       AST_APP_ARG(trunk_name);
06469       AST_APP_ARG(options);
06470    );
06471    char *opts[SLA_TRUNK_OPT_ARG_ARRAY_SIZE] = { NULL, };
06472    struct ast_flags opt_flags = { 0 };
06473    char *parse;
06474 
06475    if (ast_strlen_zero(data)) {
06476       ast_log(LOG_ERROR, "The SLATrunk application requires an argument, the trunk name\n");
06477       return -1;
06478    }
06479 
06480    parse = ast_strdupa(data);
06481    AST_STANDARD_APP_ARGS(args, parse);
06482    if (args.argc == 2) {
06483       if (ast_app_parse_options(sla_trunk_opts, &opt_flags, opts, args.options)) {
06484          ast_log(LOG_ERROR, "Error parsing options for SLATrunk\n");
06485          return -1;
06486       }
06487    }
06488 
06489    AST_RWLIST_RDLOCK(&sla_trunks);
06490    trunk = sla_find_trunk(args.trunk_name);
06491    if (trunk)
06492       ast_atomic_fetchadd_int((int *) &trunk->ref_count, 1);
06493    AST_RWLIST_UNLOCK(&sla_trunks);
06494 
06495    if (!trunk) {
06496       ast_log(LOG_ERROR, "SLA Trunk '%s' not found!\n", args.trunk_name);
06497       pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
06498       sla_queue_event(SLA_EVENT_CHECK_RELOAD);  
06499       return 0;
06500    }
06501 
06502    if (trunk->chan) {
06503       ast_log(LOG_ERROR, "Call came in on %s, but the trunk is already in use!\n",
06504          args.trunk_name);
06505       pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
06506       ast_atomic_fetchadd_int((int *) &trunk->ref_count, -1);
06507       sla_queue_event(SLA_EVENT_CHECK_RELOAD);  
06508       return 0;
06509    }
06510 
06511    trunk->chan = chan;
06512 
06513    if (!(ringing_trunk = queue_ringing_trunk(trunk))) {
06514       pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
06515       ast_atomic_fetchadd_int((int *) &trunk->ref_count, -1);
06516       sla_queue_event(SLA_EVENT_CHECK_RELOAD);  
06517       return 0;
06518    }
06519 
06520    snprintf(conf_name, sizeof(conf_name), "SLA_%s", args.trunk_name);
06521    conf = build_conf(conf_name, "", "", 1, 1, 1, chan, NULL);
06522    if (!conf) {
06523       pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
06524       ast_atomic_fetchadd_int((int *) &trunk->ref_count, -1);
06525       sla_queue_event(SLA_EVENT_CHECK_RELOAD);  
06526       return 0;
06527    }
06528    ast_set_flag64(&conf_flags, 
06529       CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF | CONFFLAG_NO_AUDIO_UNTIL_UP);
06530 
06531    if (ast_test_flag(&opt_flags, SLA_TRUNK_OPT_MOH)) {
06532       ast_indicate(chan, -1);
06533       ast_set_flag64(&conf_flags, CONFFLAG_MOH);
06534    } else
06535       ast_indicate(chan, AST_CONTROL_RINGING);
06536 
06537    conf_run(chan, conf, &conf_flags, opts);
06538    dispose_conf(conf);
06539    conf = NULL;
06540    trunk->chan = NULL;
06541    trunk->on_hold = 0;
06542 
06543    sla_change_trunk_state(trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
06544 
06545    if (!pbx_builtin_getvar_helper(chan, "SLATRUNK_STATUS"))
06546       pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "SUCCESS");
06547 
06548    /* Remove the entry from the list of ringing trunks if it is still there. */
06549    ast_mutex_lock(&sla.lock);
06550    AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
06551       if (ringing_trunk->trunk == trunk) {
06552          AST_LIST_REMOVE_CURRENT(entry);
06553          break;
06554       }
06555    }
06556    AST_LIST_TRAVERSE_SAFE_END;
06557    ast_mutex_unlock(&sla.lock);
06558    if (ringing_trunk) {
06559       ast_free(ringing_trunk);
06560       pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "UNANSWERED");
06561       /* Queue reprocessing of ringing trunks to make stations stop ringing
06562        * that shouldn't be ringing after this trunk stopped. */
06563       sla_queue_event(SLA_EVENT_RINGING_TRUNK);
06564    }
06565 
06566    ast_atomic_fetchadd_int((int *) &trunk->ref_count, -1);
06567    sla_queue_event(SLA_EVENT_CHECK_RELOAD);  
06568 
06569    return 0;
06570 }
06571 
06572 static enum ast_device_state sla_state(const char *data)
06573 {
06574    char *buf, *station_name, *trunk_name;
06575    struct sla_station *station;
06576    struct sla_trunk_ref *trunk_ref;
06577    enum ast_device_state res = AST_DEVICE_INVALID;
06578 
06579    trunk_name = buf = ast_strdupa(data);
06580    station_name = strsep(&trunk_name, "_");
06581 
06582    AST_RWLIST_RDLOCK(&sla_stations);
06583    AST_LIST_TRAVERSE(&sla_stations, station, entry) {
06584       if (strcasecmp(station_name, station->name))
06585          continue;
06586       AST_RWLIST_RDLOCK(&sla_trunks);
06587       AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
06588          if (!strcasecmp(trunk_name, trunk_ref->trunk->name))
06589             break;
06590       }
06591       if (!trunk_ref) {
06592          AST_RWLIST_UNLOCK(&sla_trunks);
06593          break;
06594       }
06595       res = sla_state_to_devstate(trunk_ref->state);
06596       AST_RWLIST_UNLOCK(&sla_trunks);
06597    }
06598    AST_RWLIST_UNLOCK(&sla_stations);
06599 
06600    if (res == AST_DEVICE_INVALID) {
06601       ast_log(LOG_ERROR, "Could not determine state for trunk %s on station %s!\n",
06602          trunk_name, station_name);
06603    }
06604 
06605    return res;
06606 }
06607 
06608 static void destroy_trunk(struct sla_trunk *trunk)
06609 {
06610    struct sla_station_ref *station_ref;
06611 
06612    if (!ast_strlen_zero(trunk->autocontext))
06613       ast_context_remove_extension(trunk->autocontext, "s", 1, sla_registrar);
06614 
06615    while ((station_ref = AST_LIST_REMOVE_HEAD(&trunk->stations, entry)))
06616       ast_free(station_ref);
06617 
06618    ast_string_field_free_memory(trunk);
06619    ast_free(trunk);
06620 }
06621 
06622 static void destroy_station(struct sla_station *station)
06623 {
06624    struct sla_trunk_ref *trunk_ref;
06625 
06626    if (!ast_strlen_zero(station->autocontext)) {
06627       AST_RWLIST_RDLOCK(&sla_trunks);
06628       AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
06629          char exten[AST_MAX_EXTENSION];
06630          char hint[AST_MAX_APP];
06631          snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);
06632          snprintf(hint, sizeof(hint), "SLA:%s", exten);
06633          ast_context_remove_extension(station->autocontext, exten, 
06634             1, sla_registrar);
06635          ast_context_remove_extension(station->autocontext, hint, 
06636             PRIORITY_HINT, sla_registrar);
06637       }
06638       AST_RWLIST_UNLOCK(&sla_trunks);
06639    }
06640 
06641    while ((trunk_ref = AST_LIST_REMOVE_HEAD(&station->trunks, entry)))
06642       ast_free(trunk_ref);
06643 
06644    ast_string_field_free_memory(station);
06645    ast_free(station);
06646 }
06647 
06648 static void sla_destroy(void)
06649 {
06650    struct sla_trunk *trunk;
06651    struct sla_station *station;
06652 
06653    AST_RWLIST_WRLOCK(&sla_trunks);
06654    while ((trunk = AST_RWLIST_REMOVE_HEAD(&sla_trunks, entry)))
06655       destroy_trunk(trunk);
06656    AST_RWLIST_UNLOCK(&sla_trunks);
06657 
06658    AST_RWLIST_WRLOCK(&sla_stations);
06659    while ((station = AST_RWLIST_REMOVE_HEAD(&sla_stations, entry)))
06660       destroy_station(station);
06661    AST_RWLIST_UNLOCK(&sla_stations);
06662 
06663    if (sla.thread != AST_PTHREADT_NULL) {
06664       ast_mutex_lock(&sla.lock);
06665       sla.stop = 1;
06666       ast_cond_signal(&sla.cond);
06667       ast_mutex_unlock(&sla.lock);
06668       pthread_join(sla.thread, NULL);
06669    }
06670 
06671    /* Drop any created contexts from the dialplan */
06672    ast_context_destroy(NULL, sla_registrar);
06673 
06674    ast_mutex_destroy(&sla.lock);
06675    ast_cond_destroy(&sla.cond);
06676 }
06677 
06678 static int sla_check_device(const char *device)
06679 {
06680    char *tech, *tech_data;
06681 
06682    tech_data = ast_strdupa(device);
06683    tech = strsep(&tech_data, "/");
06684 
06685    if (ast_strlen_zero(tech) || ast_strlen_zero(tech_data))
06686       return -1;
06687 
06688    return 0;
06689 }
06690 
06691 static int sla_build_trunk(struct ast_config *cfg, const char *cat)
06692 {
06693    struct sla_trunk *trunk;
06694    struct ast_variable *var;
06695    const char *dev;
06696 
06697    if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {
06698       ast_log(LOG_ERROR, "SLA Trunk '%s' defined with no device!\n", cat);
06699       return -1;
06700    }
06701 
06702    if (sla_check_device(dev)) {
06703       ast_log(LOG_ERROR, "SLA Trunk '%s' define with invalid device '%s'!\n",
06704          cat, dev);
06705       return -1;
06706    }
06707 
06708    if (!(trunk = ast_calloc_with_stringfields(1, struct sla_trunk, 32))) {
06709       return -1;
06710    }
06711 
06712    ast_string_field_set(trunk, name, cat);
06713    ast_string_field_set(trunk, device, dev);
06714 
06715    for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
06716       if (!strcasecmp(var->name, "autocontext"))
06717          ast_string_field_set(trunk, autocontext, var->value);
06718       else if (!strcasecmp(var->name, "ringtimeout")) {
06719          if (sscanf(var->value, "%30u", &trunk->ring_timeout) != 1) {
06720             ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for trunk '%s'\n",
06721                var->value, trunk->name);
06722             trunk->ring_timeout = 0;
06723          }
06724       } else if (!strcasecmp(var->name, "barge"))
06725          trunk->barge_disabled = ast_false(var->value);
06726       else if (!strcasecmp(var->name, "hold")) {
06727          if (!strcasecmp(var->value, "private"))
06728             trunk->hold_access = SLA_HOLD_PRIVATE;
06729          else if (!strcasecmp(var->value, "open"))
06730             trunk->hold_access = SLA_HOLD_OPEN;
06731          else {
06732             ast_log(LOG_WARNING, "Invalid value '%s' for hold on trunk %s\n",
06733                var->value, trunk->name);
06734          }
06735       } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {
06736          ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n",
06737             var->name, var->lineno, SLA_CONFIG_FILE);
06738       }
06739    }
06740 
06741    if (!ast_strlen_zero(trunk->autocontext)) {
06742       struct ast_context *context;
06743       context = ast_context_find_or_create(NULL, NULL, trunk->autocontext, sla_registrar);
06744       if (!context) {
06745          ast_log(LOG_ERROR, "Failed to automatically find or create "
06746             "context '%s' for SLA!\n", trunk->autocontext);
06747          destroy_trunk(trunk);
06748          return -1;
06749       }
06750       if (ast_add_extension2(context, 0 /* don't replace */, "s", 1,
06751          NULL, NULL, slatrunk_app, ast_strdup(trunk->name), ast_free_ptr, sla_registrar)) {
06752          ast_log(LOG_ERROR, "Failed to automatically create extension "
06753             "for trunk '%s'!\n", trunk->name);
06754          destroy_trunk(trunk);
06755          return -1;
06756       }
06757    }
06758 
06759    AST_RWLIST_WRLOCK(&sla_trunks);
06760    AST_RWLIST_INSERT_TAIL(&sla_trunks, trunk, entry);
06761    AST_RWLIST_UNLOCK(&sla_trunks);
06762 
06763    return 0;
06764 }
06765 
06766 static void sla_add_trunk_to_station(struct sla_station *station, struct ast_variable *var)
06767 {
06768    struct sla_trunk *trunk;
06769    struct sla_trunk_ref *trunk_ref;
06770    struct sla_station_ref *station_ref;
06771    char *trunk_name, *options, *cur;
06772 
06773    options = ast_strdupa(var->value);
06774    trunk_name = strsep(&options, ",");
06775    
06776    AST_RWLIST_RDLOCK(&sla_trunks);
06777    AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
06778       if (!strcasecmp(trunk->name, trunk_name))
06779          break;
06780    }
06781 
06782    AST_RWLIST_UNLOCK(&sla_trunks);
06783    if (!trunk) {
06784       ast_log(LOG_ERROR, "Trunk '%s' not found!\n", var->value);
06785       return;
06786    }
06787    if (!(trunk_ref = create_trunk_ref(trunk)))
06788       return;
06789    trunk_ref->state = SLA_TRUNK_STATE_IDLE;
06790 
06791    while ((cur = strsep(&options, ","))) {
06792       char *name, *value = cur;
06793       name = strsep(&value, "=");
06794       if (!strcasecmp(name, "ringtimeout")) {
06795          if (sscanf(value, "%30u", &trunk_ref->ring_timeout) != 1) {
06796             ast_log(LOG_WARNING, "Invalid ringtimeout value '%s' for "
06797                "trunk '%s' on station '%s'\n", value, trunk->name, station->name);
06798             trunk_ref->ring_timeout = 0;
06799          }
06800       } else if (!strcasecmp(name, "ringdelay")) {
06801          if (sscanf(value, "%30u", &trunk_ref->ring_delay) != 1) {
06802             ast_log(LOG_WARNING, "Invalid ringdelay value '%s' for "
06803                "trunk '%s' on station '%s'\n", value, trunk->name, station->name);
06804             trunk_ref->ring_delay = 0;
06805          }
06806       } else {
06807          ast_log(LOG_WARNING, "Invalid option '%s' for "
06808             "trunk '%s' on station '%s'\n", name, trunk->name, station->name);
06809       }
06810    }
06811 
06812    if (!(station_ref = sla_create_station_ref(station))) {
06813       ast_free(trunk_ref);
06814       return;
06815    }
06816    ast_atomic_fetchadd_int((int *) &trunk->num_stations, 1);
06817    AST_RWLIST_WRLOCK(&sla_trunks);
06818    AST_LIST_INSERT_TAIL(&trunk->stations, station_ref, entry);
06819    AST_RWLIST_UNLOCK(&sla_trunks);
06820    AST_LIST_INSERT_TAIL(&station->trunks, trunk_ref, entry);
06821 }
06822 
06823 static int sla_build_station(struct ast_config *cfg, const char *cat)
06824 {
06825    struct sla_station *station;
06826    struct ast_variable *var;
06827    const char *dev;
06828 
06829    if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {
06830       ast_log(LOG_ERROR, "SLA Station '%s' defined with no device!\n", cat);
06831       return -1;
06832    }
06833 
06834    if (!(station = ast_calloc_with_stringfields(1, struct sla_station, 32))) {
06835       return -1;
06836    }
06837 
06838    ast_string_field_set(station, name, cat);
06839    ast_string_field_set(station, device, dev);
06840 
06841    for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
06842       if (!strcasecmp(var->name, "trunk"))
06843          sla_add_trunk_to_station(station, var);
06844       else if (!strcasecmp(var->name, "autocontext"))
06845          ast_string_field_set(station, autocontext, var->value);
06846       else if (!strcasecmp(var->name, "ringtimeout")) {
06847          if (sscanf(var->value, "%30u", &station->ring_timeout) != 1) {
06848             ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for station '%s'\n",
06849                var->value, station->name);
06850             station->ring_timeout = 0;
06851          }
06852       } else if (!strcasecmp(var->name, "ringdelay")) {
06853          if (sscanf(var->value, "%30u", &station->ring_delay) != 1) {
06854             ast_log(LOG_WARNING, "Invalid ringdelay '%s' specified for station '%s'\n",
06855                var->value, station->name);
06856             station->ring_delay = 0;
06857          }
06858       } else if (!strcasecmp(var->name, "hold")) {
06859          if (!strcasecmp(var->value, "private"))
06860             station->hold_access = SLA_HOLD_PRIVATE;
06861          else if (!strcasecmp(var->value, "open"))
06862             station->hold_access = SLA_HOLD_OPEN;
06863          else {
06864             ast_log(LOG_WARNING, "Invalid value '%s' for hold on station %s\n",
06865                var->value, station->name);
06866          }
06867 
06868       } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {
06869          ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n",
06870             var->name, var->lineno, SLA_CONFIG_FILE);
06871       }
06872    }
06873 
06874    if (!ast_strlen_zero(station->autocontext)) {
06875       struct ast_context *context;
06876       struct sla_trunk_ref *trunk_ref;
06877       context = ast_context_find_or_create(NULL, NULL, station->autocontext, sla_registrar);
06878       if (!context) {
06879          ast_log(LOG_ERROR, "Failed to automatically find or create "
06880             "context '%s' for SLA!\n", station->autocontext);
06881          destroy_station(station);
06882          return -1;
06883       }
06884       /* The extension for when the handset goes off-hook.
06885        * exten => station1,1,SLAStation(station1) */
06886       if (ast_add_extension2(context, 0 /* don't replace */, station->name, 1,
06887          NULL, NULL, slastation_app, ast_strdup(station->name), ast_free_ptr, sla_registrar)) {
06888          ast_log(LOG_ERROR, "Failed to automatically create extension "
06889             "for trunk '%s'!\n", station->name);
06890          destroy_station(station);
06891          return -1;
06892       }
06893       AST_RWLIST_RDLOCK(&sla_trunks);
06894       AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
06895          char exten[AST_MAX_EXTENSION];
06896          char hint[AST_MAX_APP];
06897          snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);
06898          snprintf(hint, sizeof(hint), "SLA:%s", exten);
06899          /* Extension for this line button 
06900           * exten => station1_line1,1,SLAStation(station1_line1) */
06901          if (ast_add_extension2(context, 0 /* don't replace */, exten, 1,
06902             NULL, NULL, slastation_app, ast_strdup(exten), ast_free_ptr, sla_registrar)) {
06903             ast_log(LOG_ERROR, "Failed to automatically create extension "
06904                "for trunk '%s'!\n", station->name);
06905             destroy_station(station);
06906             return -1;
06907          }
06908          /* Hint for this line button 
06909           * exten => station1_line1,hint,SLA:station1_line1 */
06910          if (ast_add_extension2(context, 0 /* don't replace */, exten, PRIORITY_HINT,
06911             NULL, NULL, hint, NULL, NULL, sla_registrar)) {
06912             ast_log(LOG_ERROR, "Failed to automatically create hint "
06913                "for trunk '%s'!\n", station->name);
06914             destroy_station(station);
06915             return -1;
06916          }
06917       }
06918       AST_RWLIST_UNLOCK(&sla_trunks);
06919    }
06920 
06921    AST_RWLIST_WRLOCK(&sla_stations);
06922    AST_RWLIST_INSERT_TAIL(&sla_stations, station, entry);
06923    AST_RWLIST_UNLOCK(&sla_stations);
06924 
06925    return 0;
06926 }
06927 
06928 static int sla_load_config(int reload)
06929 {
06930    struct ast_config *cfg;
06931    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
06932    const char *cat = NULL;
06933    int res = 0;
06934    const char *val;
06935 
06936    if (!reload) {
06937       ast_mutex_init(&sla.lock);
06938       ast_cond_init(&sla.cond, NULL);
06939    }
06940 
06941    if (!(cfg = ast_config_load(SLA_CONFIG_FILE, config_flags))) {
06942       return 0; /* Treat no config as normal */
06943    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
06944       return 0;
06945    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
06946       ast_log(LOG_ERROR, "Config file " SLA_CONFIG_FILE " is in an invalid format.  Aborting.\n");
06947       return 0;
06948    }
06949 
06950    if (reload) {
06951       struct sla_station *station;
06952       struct sla_trunk *trunk;
06953 
06954       /* We need to actually delete the previous versions of trunks and stations now */
06955       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&sla_stations, station, entry) {
06956          AST_RWLIST_REMOVE_CURRENT(entry);
06957          ast_free(station);
06958       }
06959       AST_RWLIST_TRAVERSE_SAFE_END;
06960 
06961       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&sla_trunks, trunk, entry) {
06962          AST_RWLIST_REMOVE_CURRENT(entry);
06963          ast_free(trunk);
06964       }
06965       AST_RWLIST_TRAVERSE_SAFE_END;
06966    }
06967 
06968    if ((val = ast_variable_retrieve(cfg, "general", "attemptcallerid")))
06969       sla.attempt_callerid = ast_true(val);
06970 
06971    while ((cat = ast_category_browse(cfg, cat)) && !res) {
06972       const char *type;
06973       if (!strcasecmp(cat, "general"))
06974          continue;
06975       if (!(type = ast_variable_retrieve(cfg, cat, "type"))) {
06976          ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n",
06977             SLA_CONFIG_FILE);
06978          continue;
06979       }
06980       if (!strcasecmp(type, "trunk"))
06981          res = sla_build_trunk(cfg, cat);
06982       else if (!strcasecmp(type, "station"))
06983          res = sla_build_station(cfg, cat);
06984       else {
06985          ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'!\n",
06986             SLA_CONFIG_FILE, type);
06987       }
06988    }
06989 
06990    ast_config_destroy(cfg);
06991 
06992    /* Even if we don't have any stations, we may after a reload and we need to
06993     * be able to process the SLA_EVENT_RELOAD event in that case */
06994    if (sla.thread == AST_PTHREADT_NULL && (!AST_LIST_EMPTY(&sla_stations) || !AST_LIST_EMPTY(&sla_trunks))) {
06995       ast_pthread_create(&sla.thread, NULL, sla_thread, NULL);
06996    }
06997 
06998    return res;
06999 }
07000 
07001 static int acf_meetme_info_eval(const char *keyword, const struct ast_conference *conf)
07002 {
07003    if (!strcasecmp("lock", keyword)) {
07004       return conf->locked;
07005    } else if (!strcasecmp("parties", keyword)) {
07006       return conf->users;
07007    } else if (!strcasecmp("activity", keyword)) {
07008       time_t now;
07009       now = time(NULL);
07010       return (now - conf->start);
07011    } else if (!strcasecmp("dynamic", keyword)) {
07012       return conf->isdynamic;
07013    } else {
07014       return -1;
07015    }
07016 
07017 }
07018 
07019 static int acf_meetme_info(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
07020 {
07021    struct ast_conference *conf;
07022    char *parse;
07023    int result = -2; /* only non-negative numbers valid, -1 is used elsewhere */
07024    AST_DECLARE_APP_ARGS(args,
07025       AST_APP_ARG(keyword);
07026       AST_APP_ARG(confno);
07027    );
07028 
07029    if (ast_strlen_zero(data)) {
07030       ast_log(LOG_ERROR, "Syntax: MEETME_INFO() requires two arguments\n");
07031       return -1;
07032    }
07033 
07034    parse = ast_strdupa(data);
07035    AST_STANDARD_APP_ARGS(args, parse);
07036 
07037    if (ast_strlen_zero(args.keyword)) {
07038       ast_log(LOG_ERROR, "Syntax: MEETME_INFO() requires a keyword\n");
07039       return -1;
07040    }
07041 
07042    if (ast_strlen_zero(args.confno)) {
07043       ast_log(LOG_ERROR, "Syntax: MEETME_INFO() requires a conference number\n");
07044       return -1;
07045    }
07046 
07047    AST_LIST_LOCK(&confs);
07048    AST_LIST_TRAVERSE(&confs, conf, list) {
07049       if (!strcmp(args.confno, conf->confno)) {
07050          result = acf_meetme_info_eval(args.keyword, conf);
07051          break;
07052       }
07053    }
07054    AST_LIST_UNLOCK(&confs);
07055 
07056    if (result > -1) {
07057       snprintf(buf, len, "%d", result);
07058    } else if (result == -1) {
07059       ast_log(LOG_NOTICE, "Error: invalid keyword: '%s'\n", args.keyword);
07060       snprintf(buf, len, "0");
07061    } else if (result == -2) {
07062       ast_log(LOG_NOTICE, "Error: conference (%s) not found\n", args.confno); 
07063       snprintf(buf, len, "0");
07064    }
07065 
07066    return 0;
07067 }
07068 
07069 
07070 static struct ast_custom_function meetme_info_acf = {
07071    .name = "MEETME_INFO",
07072    .read = acf_meetme_info,
07073 };
07074 
07075 
07076 static int load_config(int reload)
07077 {
07078    load_config_meetme();
07079 
07080    if (reload && sla.thread != AST_PTHREADT_NULL) {
07081       sla_queue_event(SLA_EVENT_RELOAD);
07082       ast_log(LOG_NOTICE, "A reload of the SLA configuration has been requested "
07083          "and will be completed when the system is idle.\n");
07084       return 0;
07085    }
07086    
07087    return sla_load_config(0);
07088 }
07089 
07090 #define MEETME_DATA_EXPORT(MEMBER)              \
07091    MEMBER(ast_conference, confno, AST_DATA_STRING)       \
07092    MEMBER(ast_conference, dahdiconf, AST_DATA_INTEGER)      \
07093    MEMBER(ast_conference, users, AST_DATA_INTEGER)       \
07094    MEMBER(ast_conference, markedusers, AST_DATA_INTEGER)    \
07095    MEMBER(ast_conference, maxusers, AST_DATA_INTEGER)    \
07096    MEMBER(ast_conference, isdynamic, AST_DATA_BOOLEAN)      \
07097    MEMBER(ast_conference, locked, AST_DATA_BOOLEAN)      \
07098    MEMBER(ast_conference, recordingfilename, AST_DATA_STRING)  \
07099    MEMBER(ast_conference, recordingformat, AST_DATA_STRING) \
07100    MEMBER(ast_conference, pin, AST_DATA_PASSWORD)        \
07101    MEMBER(ast_conference, pinadmin, AST_DATA_PASSWORD)      \
07102    MEMBER(ast_conference, start, AST_DATA_TIMESTAMP)     \
07103    MEMBER(ast_conference, endtime, AST_DATA_TIMESTAMP)
07104 
07105 AST_DATA_STRUCTURE(ast_conference, MEETME_DATA_EXPORT);
07106 
07107 #define MEETME_USER_DATA_EXPORT(MEMBER)               \
07108    MEMBER(ast_conf_user, user_no, AST_DATA_INTEGER)      \
07109    MEMBER(ast_conf_user, talking, AST_DATA_BOOLEAN)      \
07110    MEMBER(ast_conf_user, dahdichannel, AST_DATA_BOOLEAN)    \
07111    MEMBER(ast_conf_user, jointime, AST_DATA_TIMESTAMP)      \
07112    MEMBER(ast_conf_user, kicktime, AST_DATA_TIMESTAMP)      \
07113    MEMBER(ast_conf_user, timelimit, AST_DATA_MILLISECONDS)     \
07114    MEMBER(ast_conf_user, play_warning, AST_DATA_MILLISECONDS)  \
07115    MEMBER(ast_conf_user, warning_freq, AST_DATA_MILLISECONDS)
07116 
07117 AST_DATA_STRUCTURE(ast_conf_user, MEETME_USER_DATA_EXPORT);
07118 
07119 static int user_add_provider_cb(void *obj, void *arg, int flags)
07120 {
07121    struct ast_data *data_meetme_user;
07122    struct ast_data *data_meetme_user_channel;
07123    struct ast_data *data_meetme_user_volume;
07124 
07125    struct ast_conf_user *user = obj;
07126    struct ast_data *data_meetme_users = arg;
07127 
07128    data_meetme_user = ast_data_add_node(data_meetme_users, "user");
07129    if (!data_meetme_user) {
07130       return 0;
07131    }
07132    /* user structure */
07133    ast_data_add_structure(ast_conf_user, data_meetme_user, user);
07134 
07135    /* user's channel */
07136    data_meetme_user_channel = ast_data_add_node(data_meetme_user, "channel");
07137    if (!data_meetme_user_channel) {
07138       return 0;
07139    }
07140 
07141    ast_channel_data_add_structure(data_meetme_user_channel, user->chan, 1);
07142 
07143    /* volume structure */
07144    data_meetme_user_volume = ast_data_add_node(data_meetme_user, "listen-volume");
07145    if (!data_meetme_user_volume) {
07146       return 0;
07147    }
07148    ast_data_add_int(data_meetme_user_volume, "desired", user->listen.desired);
07149    ast_data_add_int(data_meetme_user_volume, "actual", user->listen.actual);
07150 
07151    data_meetme_user_volume = ast_data_add_node(data_meetme_user, "talk-volume");
07152    if (!data_meetme_user_volume) {
07153       return 0;
07154    }
07155    ast_data_add_int(data_meetme_user_volume, "desired", user->talk.desired);
07156    ast_data_add_int(data_meetme_user_volume, "actual", user->talk.actual);
07157 
07158    return 0;
07159 }
07160 
07161 /*!
07162  * \internal
07163  * \brief Implements the meetme data provider.
07164  */
07165 static int meetme_data_provider_get(const struct ast_data_search *search,
07166    struct ast_data *data_root)
07167 {
07168    struct ast_conference *cnf;
07169    struct ast_data *data_meetme, *data_meetme_users;
07170 
07171    AST_LIST_LOCK(&confs);
07172    AST_LIST_TRAVERSE(&confs, cnf, list) {
07173       data_meetme = ast_data_add_node(data_root, "meetme");
07174       if (!data_meetme) {
07175          continue;
07176       }
07177 
07178       ast_data_add_structure(ast_conference, data_meetme, cnf);
07179 
07180       if (ao2_container_count(cnf->usercontainer)) {
07181          data_meetme_users = ast_data_add_node(data_meetme, "users");
07182          if (!data_meetme_users) {
07183             ast_data_remove_node(data_root, data_meetme);
07184             continue;
07185          }
07186 
07187          ao2_callback(cnf->usercontainer, OBJ_NODATA, user_add_provider_cb, data_meetme_users); 
07188       }
07189 
07190       if (!ast_data_search_match(search, data_meetme)) {
07191          ast_data_remove_node(data_root, data_meetme);
07192       }
07193    }
07194    AST_LIST_UNLOCK(&confs);
07195 
07196    return 0;
07197 }
07198 
07199 static const struct ast_data_handler meetme_data_provider = {
07200    .version = AST_DATA_HANDLER_VERSION,
07201    .get = meetme_data_provider_get
07202 };
07203 
07204 static const struct ast_data_entry meetme_data_providers[] = {
07205    AST_DATA_ENTRY("asterisk/application/meetme/list", &meetme_data_provider),
07206 };
07207 
07208 #ifdef TEST_FRAMEWORK
07209 AST_TEST_DEFINE(test_meetme_data_provider)
07210 {
07211    struct ast_channel *chan;
07212    struct ast_conference *cnf;
07213    struct ast_data *node;
07214    struct ast_data_query query = {
07215       .path = "/asterisk/application/meetme/list",
07216       .search = "list/meetme/confno=9898"
07217    };
07218 
07219    switch (cmd) {
07220    case TEST_INIT:
07221       info->name = "meetme_get_data_test";
07222       info->category = "/main/data/app_meetme/list/";
07223       info->summary = "Meetme data provider unit test";
07224       info->description =
07225          "Tests whether the Meetme data provider implementation works as expected.";
07226       return AST_TEST_NOT_RUN;
07227    case TEST_EXECUTE:
07228       break;
07229    }
07230 
07231    chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, 0, 0, "MeetMeTest");
07232    if (!chan) {
07233       ast_test_status_update(test, "Channel allocation failed\n");
07234       return AST_TEST_FAIL;
07235    }
07236 
07237    cnf = build_conf("9898", "", "1234", 1, 1, 1, chan, test);
07238    if (!cnf) {
07239       ast_test_status_update(test, "Build of test conference 9898 failed\n");
07240       ast_hangup(chan);
07241       return AST_TEST_FAIL;
07242    }
07243 
07244    node = ast_data_get(&query);
07245    if (!node) {
07246       ast_test_status_update(test, "Data query for test conference 9898 failed\n");
07247       dispose_conf(cnf);
07248       ast_hangup(chan);
07249       return AST_TEST_FAIL;
07250    }
07251 
07252    if (strcmp(ast_data_retrieve_string(node, "meetme/confno"), "9898")) {
07253       ast_test_status_update(test, "Query returned the wrong conference\n");
07254       dispose_conf(cnf);
07255       ast_hangup(chan);
07256       ast_data_free(node);
07257       return AST_TEST_FAIL;
07258    }
07259 
07260    ast_data_free(node);
07261    dispose_conf(cnf);
07262    ast_hangup(chan);
07263 
07264    return AST_TEST_PASS;
07265 }
07266 #endif
07267 
07268 static int unload_module(void)
07269 {
07270    int res = 0;
07271    
07272    ast_cli_unregister_multiple(cli_meetme, ARRAY_LEN(cli_meetme));
07273    res = ast_manager_unregister("MeetmeMute");
07274    res |= ast_manager_unregister("MeetmeUnmute");
07275    res |= ast_manager_unregister("MeetmeList");
07276    res |= ast_unregister_application(app4);
07277    res |= ast_unregister_application(app3);
07278    res |= ast_unregister_application(app2);
07279    res |= ast_unregister_application(app);
07280    res |= ast_unregister_application(slastation_app);
07281    res |= ast_unregister_application(slatrunk_app);
07282 
07283 #ifdef TEST_FRAMEWORK
07284    AST_TEST_UNREGISTER(test_meetme_data_provider);
07285 #endif
07286    ast_data_unregister(NULL);
07287 
07288    ast_devstate_prov_del("Meetme");
07289    ast_devstate_prov_del("SLA");
07290    
07291    sla_destroy();
07292    
07293    res |= ast_custom_function_unregister(&meetme_info_acf);
07294    ast_unload_realtime("meetme");
07295 
07296    return res;
07297 }
07298 
07299 
07300 
07301 static int load_module(void)
07302 {
07303    int res = 0;
07304 
07305    res |= load_config(0);
07306 
07307    ast_cli_register_multiple(cli_meetme, ARRAY_LEN(cli_meetme));
07308    res |= ast_manager_register_xml("MeetmeMute", EVENT_FLAG_CALL, action_meetmemute);
07309    res |= ast_manager_register_xml("MeetmeUnmute", EVENT_FLAG_CALL, action_meetmeunmute);
07310    res |= ast_manager_register_xml("MeetmeList", EVENT_FLAG_REPORTING, action_meetmelist);
07311    res |= ast_register_application_xml(app4, channel_admin_exec);
07312    res |= ast_register_application_xml(app3, admin_exec);
07313    res |= ast_register_application_xml(app2, count_exec);
07314    res |= ast_register_application_xml(app, conf_exec);
07315    res |= ast_register_application_xml(slastation_app, sla_station_exec);
07316    res |= ast_register_application_xml(slatrunk_app, sla_trunk_exec);
07317 
07318 #ifdef TEST_FRAMEWORK
07319    AST_TEST_REGISTER(test_meetme_data_provider);
07320 #endif
07321    ast_data_register_multiple(meetme_data_providers, ARRAY_LEN(meetme_data_providers));
07322 
07323    res |= ast_devstate_prov_add("Meetme", meetmestate);
07324    res |= ast_devstate_prov_add("SLA", sla_state);
07325 
07326    res |= ast_custom_function_register(&meetme_info_acf);
07327    ast_realtime_require_field("meetme", "confno", RQ_UINTEGER2, 3, "members", RQ_UINTEGER1, 3, NULL);
07328 
07329    return res;
07330 }
07331 
07332 static int reload(void)
07333 {
07334    ast_unload_realtime("meetme");
07335    return load_config(1);
07336 }
07337 
07338 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "MeetMe conference bridge",
07339       .load = load_module,
07340       .unload = unload_module,
07341       .reload = reload,
07342       .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
07343           );
07344 

Generated on 18 Jan 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1