Mon Aug 31 12:29:57 2015

Asterisk developer's documentation


app_dial.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief dial() & retrydial() - Trivial application to dial a channel and send an URL on answer
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <depend>chan_local</depend>
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 421232 $")
00037 
00038 #include <sys/time.h>
00039 #include <sys/signal.h>
00040 #include <sys/stat.h>
00041 #include <netinet/in.h>
00042 
00043 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
00044 #include "asterisk/lock.h"
00045 #include "asterisk/file.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/translate.h"
00050 #include "asterisk/say.h"
00051 #include "asterisk/config.h"
00052 #include "asterisk/features.h"
00053 #include "asterisk/musiconhold.h"
00054 #include "asterisk/callerid.h"
00055 #include "asterisk/utils.h"
00056 #include "asterisk/app.h"
00057 #include "asterisk/causes.h"
00058 #include "asterisk/rtp_engine.h"
00059 #include "asterisk/cdr.h"
00060 #include "asterisk/manager.h"
00061 #include "asterisk/privacy.h"
00062 #include "asterisk/stringfields.h"
00063 #include "asterisk/global_datastores.h"
00064 #include "asterisk/dsp.h"
00065 #include "asterisk/cel.h"
00066 #include "asterisk/aoc.h"
00067 #include "asterisk/ccss.h"
00068 #include "asterisk/indications.h"
00069 #include "asterisk/framehook.h"
00070 
00071 /*** DOCUMENTATION
00072    <application name="Dial" language="en_US">
00073       <synopsis>
00074          Attempt to connect to another device or endpoint and bridge the call.
00075       </synopsis>
00076       <syntax>
00077          <parameter name="Technology/Resource" required="true" argsep="&amp;">
00078             <argument name="Technology/Resource" required="true">
00079                <para>Specification of the device(s) to dial.  These must be in the format of
00080                <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
00081                represents a particular channel driver, and <replaceable>Resource</replaceable>
00082                represents a resource available to that particular channel driver.</para>
00083             </argument>
00084             <argument name="Technology2/Resource2" required="false" multiple="true">
00085                <para>Optional extra devices to dial in parallel</para>
00086                <para>If you need more then one enter them as
00087                Technology2/Resource2&amp;Technology3/Resourse3&amp;.....</para>
00088             </argument>
00089          </parameter>
00090          <parameter name="timeout" required="false">
00091             <para>Specifies the number of seconds we attempt to dial the specified devices</para>
00092             <para>If not specified, this defaults to 136 years.</para>
00093          </parameter>
00094          <parameter name="options" required="false">
00095             <optionlist>
00096             <option name="A">
00097                <argument name="x" required="true">
00098                   <para>The file to play to the called party</para>
00099                </argument>
00100                <para>Play an announcement to the called party, where <replaceable>x</replaceable> is the prompt to be played</para>
00101             </option>
00102             <option name="a">
00103                <para>Immediately answer the calling channel when the called channel answers in
00104                all cases. Normally, the calling channel is answered when the called channel
00105                answers, but when options such as A() and M() are used, the calling channel is
00106                not answered until all actions on the called channel (such as playing an
00107                announcement) are completed.  This option can be used to answer the calling
00108                channel before doing anything on the called channel. You will rarely need to use
00109                this option, the default behavior is adequate in most cases.</para>
00110             </option>
00111             <option name="C">
00112                <para>Reset the call detail record (CDR) for this call.</para>
00113             </option>
00114             <option name="c">
00115                <para>If the Dial() application cancels this call, always set the flag to tell the channel
00116                driver that the call is answered elsewhere.</para>
00117             </option>
00118             <option name="d">
00119                <para>Allow the calling user to dial a 1 digit extension while waiting for
00120                a call to be answered. Exit to that extension if it exists in the
00121                current context, or the context defined in the <variable>EXITCONTEXT</variable> variable,
00122                if it exists.</para>
00123                <note>
00124                   <para>Many SIP and ISDN phones cannot send DTMF digits until the call is
00125                   connected.  If you wish to use this option with these phones, you
00126                   can use the <literal>Answer</literal> application before dialing.</para>
00127                </note>
00128             </option>
00129             <option name="D" argsep=":">
00130                <argument name="called" />
00131                <argument name="calling" />
00132                <argument name="progress" />
00133                <para>Send the specified DTMF strings <emphasis>after</emphasis> the called
00134                party has answered, but before the call gets bridged. The 
00135                <replaceable>called</replaceable> DTMF string is sent to the called party, and the 
00136                <replaceable>calling</replaceable> DTMF string is sent to the calling party. Both arguments 
00137                can be used alone.  If <replaceable>progress</replaceable> is specified, its DTMF is sent
00138                immediately after receiving a PROGRESS message.</para>
00139             </option>
00140             <option name="e">
00141                <para>Execute the <literal>h</literal> extension for peer after the call ends</para>
00142             </option>
00143             <option name="f">
00144                <argument name="x" required="false" />
00145                <para>If <replaceable>x</replaceable> is not provided, force the CallerID sent on a call-forward or
00146                deflection to the dialplan extension of this Dial() using a dialplan <literal>hint</literal>.
00147                For example, some PSTNs do not allow CallerID to be set to anything
00148                other than the numbers assigned to you.
00149                If <replaceable>x</replaceable> is provided, force the CallerID sent to <replaceable>x</replaceable>.</para>
00150             </option>
00151             <option name="F" argsep="^">
00152                <argument name="context" required="false" />
00153                <argument name="exten" required="false" />
00154                <argument name="priority" required="true" />
00155                <para>When the caller hangs up, transfer the <emphasis>called</emphasis> party
00156                to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
00157                <note>
00158                   <para>Any channel variables you want the called channel to inherit from the caller channel must be
00159                   prefixed with one or two underbars ('_').</para>
00160                </note>
00161             </option>
00162             <option name="F">
00163                <para>When the caller hangs up, transfer the <emphasis>called</emphasis> party to the next priority of the current extension
00164                and <emphasis>start</emphasis> execution at that location.</para>
00165                <note>
00166                   <para>Any channel variables you want the called channel to inherit from the caller channel must be
00167                   prefixed with one or two underbars ('_').</para>
00168                </note>
00169                <note>
00170                   <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
00171                </note>
00172             </option>
00173             <option name="g">
00174                <para>Proceed with dialplan execution at the next priority in the current extension if the
00175                destination channel hangs up.</para>
00176             </option>
00177             <option name="G" argsep="^">
00178                <argument name="context" required="false" />
00179                <argument name="exten" required="false" />
00180                <argument name="priority" required="true" />
00181                <para>If the call is answered, transfer the calling party to
00182                the specified <replaceable>priority</replaceable> and the called party to the specified 
00183                <replaceable>priority</replaceable> plus one.</para>
00184                <note>
00185                   <para>You cannot use any additional action post answer options in conjunction with this option.</para>
00186                </note>
00187             </option>
00188             <option name="h">
00189                <para>Allow the called party to hang up by sending the DTMF sequence
00190                defined for disconnect in <filename>features.conf</filename>.</para>
00191             </option>
00192             <option name="H">
00193                <para>Allow the calling party to hang up by sending the DTMF sequence
00194                defined for disconnect in <filename>features.conf</filename>.</para>
00195                <note>
00196                   <para>Many SIP and ISDN phones cannot send DTMF digits until the call is
00197                   connected.  If you wish to allow DTMF disconnect before the dialed
00198                   party answers with these phones, you can use the <literal>Answer</literal>
00199                   application before dialing.</para>
00200                </note>
00201             </option>
00202             <option name="i">
00203                <para>Asterisk will ignore any forwarding requests it may receive on this dial attempt.</para>
00204             </option>
00205             <option name="I">
00206                <para>Asterisk will ignore any connected line update requests or any redirecting party
00207                update requests it may receive on this dial attempt.</para>
00208             </option>
00209             <option name="k">
00210                <para>Allow the called party to enable parking of the call by sending
00211                the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
00212             </option>
00213             <option name="K">
00214                <para>Allow the calling party to enable parking of the call by sending
00215                the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
00216             </option>
00217             <option name="L" argsep=":">
00218                <argument name="x" required="true">
00219                   <para>Maximum call time, in milliseconds</para>
00220                </argument>
00221                <argument name="y">
00222                   <para>Warning time, in milliseconds</para>
00223                </argument>
00224                <argument name="z">
00225                   <para>Repeat time, in milliseconds</para>
00226                </argument>
00227                <para>Limit the call to <replaceable>x</replaceable> milliseconds. Play a warning when <replaceable>y</replaceable> milliseconds are
00228                left. Repeat the warning every <replaceable>z</replaceable> milliseconds until time expires.</para>
00229                <para>This option is affected by the following variables:</para>
00230                <variablelist>
00231                   <variable name="LIMIT_PLAYAUDIO_CALLER">
00232                      <value name="yes" default="true" />
00233                      <value name="no" />
00234                      <para>If set, this variable causes Asterisk to play the prompts to the caller.</para>
00235                   </variable>
00236                   <variable name="LIMIT_PLAYAUDIO_CALLEE">
00237                      <value name="yes" />
00238                      <value name="no" default="true"/>
00239                      <para>If set, this variable causes Asterisk to play the prompts to the callee.</para>
00240                   </variable>
00241                   <variable name="LIMIT_TIMEOUT_FILE">
00242                      <value name="filename"/>
00243                      <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the timeout is reached.
00244                      If not set, the time remaining will be announced.</para>
00245                   </variable>
00246                   <variable name="LIMIT_CONNECT_FILE">
00247                      <value name="filename"/>
00248                      <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the call begins.
00249                      If not set, the time remaining will be announced.</para>
00250                   </variable>
00251                   <variable name="LIMIT_WARNING_FILE">
00252                      <value name="filename"/>
00253                      <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play as
00254                      a warning when time <replaceable>x</replaceable> is reached. If not set, the time remaining will be announced.</para>
00255                   </variable>
00256                </variablelist>
00257             </option>
00258             <option name="m">
00259                <argument name="class" required="false"/>
00260                <para>Provide hold music to the calling party until a requested
00261                channel answers. A specific music on hold <replaceable>class</replaceable>
00262                (as defined in <filename>musiconhold.conf</filename>) can be specified.</para>
00263             </option>
00264             <option name="M" argsep="^">
00265                <argument name="macro" required="true">
00266                   <para>Name of the macro that should be executed.</para>
00267                </argument>
00268                <argument name="arg" multiple="true">
00269                   <para>Macro arguments</para>
00270                </argument>
00271                <para>Execute the specified <replaceable>macro</replaceable> for the <emphasis>called</emphasis> channel 
00272                before connecting to the calling channel. Arguments can be specified to the Macro
00273                using <literal>^</literal> as a delimiter. The macro can set the variable
00274                <variable>MACRO_RESULT</variable> to specify the following actions after the macro is
00275                finished executing:</para>
00276                <variablelist>
00277                   <variable name="MACRO_RESULT">
00278                      <para>If set, this action will be taken after the macro finished executing.</para>
00279                      <value name="ABORT">
00280                         Hangup both legs of the call
00281                      </value>
00282                      <value name="CONGESTION">
00283                         Behave as if line congestion was encountered
00284                      </value>
00285                      <value name="BUSY">
00286                         Behave as if a busy signal was encountered
00287                      </value>
00288                      <value name="CONTINUE">
00289                         Hangup the called party and allow the calling party to continue dialplan execution at the next priority
00290                      </value>
00291                      <value name="GOTO:[[&lt;context&gt;^]&lt;exten&gt;^]&lt;priority&gt;">
00292                         Transfer the call to the specified destination.
00293                      </value>
00294                   </variable>
00295                </variablelist>
00296                <note>
00297                   <para>You cannot use any additional action post answer options in conjunction
00298                   with this option. Also, pbx services are run on the peer (called) channel,
00299                   so you will not be able to set timeouts via the TIMEOUT() function in this macro.</para>
00300                </note>
00301                <warning><para>Be aware of the limitations that macros have, specifically with regards to use of
00302                the <literal>WaitExten</literal> application. For more information, see the documentation for
00303                Macro()</para></warning>
00304             </option>
00305             <option name="n">
00306                     <argument name="delete">
00307                        <para>With <replaceable>delete</replaceable> either not specified or set to <literal>0</literal>,
00308                   the recorded introduction will not be deleted if the caller hangs up while the remote party has not
00309                   yet answered.</para>
00310                   <para>With <replaceable>delete</replaceable> set to <literal>1</literal>, the introduction will
00311                   always be deleted.</para>
00312                </argument>
00313                <para>This option is a modifier for the call screening/privacy mode. (See the 
00314                <literal>p</literal> and <literal>P</literal> options.) It specifies
00315                that no introductions are to be saved in the <directory>priv-callerintros</directory>
00316                directory.</para>
00317             </option>
00318             <option name="N">
00319                <para>This option is a modifier for the call screening/privacy mode. It specifies
00320                that if Caller*ID is present, do not screen the call.</para>
00321             </option>
00322             <option name="o">
00323                <argument name="x" required="false" />
00324                <para>If <replaceable>x</replaceable> is not provided, specify that the CallerID that was present on the
00325                <emphasis>calling</emphasis> channel be stored as the CallerID on the <emphasis>called</emphasis> channel.
00326                This was the behavior of Asterisk 1.0 and earlier.
00327                If <replaceable>x</replaceable> is provided, specify the CallerID stored on the <emphasis>called</emphasis> channel.
00328                Note that o(${CALLERID(all)}) is similar to option o without the parameter.</para>
00329             </option>
00330             <option name="O">
00331                <argument name="mode">
00332                   <para>With <replaceable>mode</replaceable> either not specified or set to <literal>1</literal>,
00333                   the originator hanging up will cause the phone to ring back immediately.</para>
00334                   <para>With <replaceable>mode</replaceable> set to <literal>2</literal>, when the operator 
00335                   flashes the trunk, it will ring their phone back.</para>
00336                </argument>
00337                <para>Enables <emphasis>operator services</emphasis> mode.  This option only
00338                works when bridging a DAHDI channel to another DAHDI channel
00339                only. if specified on non-DAHDI interfaces, it will be ignored.
00340                When the destination answers (presumably an operator services
00341                station), the originator no longer has control of their line.
00342                They may hang up, but the switch will not release their line
00343                until the destination party (the operator) hangs up.</para>
00344             </option>
00345             <option name="p">
00346                <para>This option enables screening mode. This is basically Privacy mode
00347                without memory.</para>
00348             </option>
00349             <option name="P">
00350                <argument name="x" />
00351                <para>Enable privacy mode. Use <replaceable>x</replaceable> as the family/key in the AstDB database if
00352                it is provided. The current extension is used if a database family/key is not specified.</para>
00353             </option>
00354             <option name="r">
00355                <para>Default: Indicate ringing to the calling party, even if the called party isn't actually ringing. Pass no audio to the calling
00356                party until the called channel has answered.</para>
00357                <argument name="tone" required="false">
00358                   <para>Indicate progress to calling party. Send audio 'tone' from indications.conf</para>
00359                </argument>
00360             </option>
00361             <option name="S">
00362                <argument name="x" required="true" />
00363                <para>Hang up the call <replaceable>x</replaceable> seconds <emphasis>after</emphasis> the called party has
00364                answered the call.</para>
00365             </option>
00366             <option name="s">
00367                <argument name="x" required="true" />
00368                <para>Force the outgoing callerid tag parameter to be set to the string <replaceable>x</replaceable>.</para>
00369                <para>Works with the f option.</para>
00370             </option>
00371             <option name="t">
00372                <para>Allow the called party to transfer the calling party by sending the
00373                DTMF sequence defined in <filename>features.conf</filename>. This setting does not perform policy enforcement on
00374                transfers initiated by other methods.</para>
00375             </option>
00376             <option name="T">
00377                <para>Allow the calling party to transfer the called party by sending the
00378                DTMF sequence defined in <filename>features.conf</filename>. This setting does not perform policy enforcement on
00379                transfers initiated by other methods.</para>
00380             </option>
00381             <option name="U" argsep="^">
00382                <argument name="x" required="true">
00383                   <para>Name of the subroutine to execute via Gosub</para>
00384                </argument>
00385                <argument name="arg" multiple="true" required="false">
00386                   <para>Arguments for the Gosub routine</para>
00387                </argument>
00388                <para>Execute via Gosub the routine <replaceable>x</replaceable> for the <emphasis>called</emphasis> channel before connecting
00389                to the calling channel. Arguments can be specified to the Gosub
00390                using <literal>^</literal> as a delimiter. The Gosub routine can set the variable
00391                <variable>GOSUB_RESULT</variable> to specify the following actions after the Gosub returns.</para>
00392                <variablelist>
00393                   <variable name="GOSUB_RESULT">
00394                      <value name="ABORT">
00395                         Hangup both legs of the call.
00396                      </value>
00397                      <value name="CONGESTION">
00398                         Behave as if line congestion was encountered.
00399                      </value>
00400                      <value name="BUSY">
00401                         Behave as if a busy signal was encountered.
00402                      </value>
00403                      <value name="CONTINUE">
00404                         Hangup the called party and allow the calling party
00405                         to continue dialplan execution at the next priority.
00406                      </value>
00407                      <value name="GOTO:[[&lt;context&gt;^]&lt;exten&gt;^]&lt;priority&gt;">
00408                         Transfer the call to the specified destination.
00409                      </value>
00410                   </variable>
00411                </variablelist>
00412                <note>
00413                   <para>You cannot use any additional action post answer options in conjunction
00414                   with this option. Also, pbx services are run on the peer (called) channel,
00415                   so you will not be able to set timeouts via the TIMEOUT() function in this routine.</para>
00416                </note>
00417             </option>
00418             <option name="u">
00419                <argument name = "x" required="true">
00420                   <para>Force the outgoing callerid presentation indicator parameter to be set
00421                   to one of the values passed in <replaceable>x</replaceable>:
00422                   <literal>allowed_not_screened</literal>
00423                   <literal>allowed_passed_screen</literal>
00424                   <literal>allowed_failed_screen</literal>
00425                   <literal>allowed</literal>
00426                   <literal>prohib_not_screened</literal>
00427                   <literal>prohib_passed_screen</literal>
00428                   <literal>prohib_failed_screen</literal>
00429                   <literal>prohib</literal>
00430                   <literal>unavailable</literal></para>
00431                </argument>
00432                <para>Works with the f option.</para>
00433             </option>
00434             <option name="w">
00435                <para>Allow the called party to enable recording of the call by sending
00436                the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
00437             </option>
00438             <option name="W">
00439                <para>Allow the calling party to enable recording of the call by sending
00440                the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
00441             </option>
00442             <option name="x">
00443                <para>Allow the called party to enable recording of the call by sending
00444                the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
00445             </option>
00446             <option name="X">
00447                <para>Allow the calling party to enable recording of the call by sending
00448                the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
00449             </option>
00450             <option name="z">
00451                <para>On a call forward, cancel any dial timeout which has been set for this call.</para>
00452             </option>
00453             </optionlist>
00454          </parameter>
00455          <parameter name="URL">
00456             <para>The optional URL will be sent to the called party if the channel driver supports it.</para>
00457          </parameter>
00458       </syntax>
00459       <description>
00460          <para>This application will place calls to one or more specified channels. As soon
00461          as one of the requested channels answers, the originating channel will be
00462          answered, if it has not already been answered. These two channels will then
00463          be active in a bridged call. All other channels that were requested will then
00464          be hung up.</para>
00465 
00466          <para>Unless there is a timeout specified, the Dial application will wait
00467          indefinitely until one of the called channels answers, the user hangs up, or
00468          if all of the called channels are busy or unavailable. Dialplan executing will
00469          continue if no requested channels can be called, or if the timeout expires.
00470          This application will report normal termination if the originating channel
00471          hangs up, or if the call is bridged and either of the parties in the bridge
00472          ends the call.</para>
00473          <para>If the <variable>OUTBOUND_GROUP</variable> variable is set, all peer channels created by this
00474          application will be put into that group (as in Set(GROUP()=...).
00475          If the <variable>OUTBOUND_GROUP_ONCE</variable> variable is set, all peer channels created by this
00476          application will be put into that group (as in Set(GROUP()=...). Unlike <variable>OUTBOUND_GROUP</variable>,
00477          however, the variable will be unset after use.</para>
00478 
00479          <para>This application sets the following channel variables:</para>
00480          <variablelist>
00481             <variable name="DIALEDTIME">
00482                <para>This is the time from dialing a channel until when it is disconnected.</para>
00483             </variable>
00484             <variable name="ANSWEREDTIME">
00485                <para>This is the amount of time for actual call.</para>
00486             </variable>
00487             <variable name="DIALSTATUS">
00488                <para>This is the status of the call</para>
00489                <value name="CHANUNAVAIL" />
00490                <value name="CONGESTION" />
00491                <value name="NOANSWER" />
00492                <value name="BUSY" />
00493                <value name="ANSWER" />
00494                <value name="CANCEL" />
00495                <value name="DONTCALL">
00496                   For the Privacy and Screening Modes.
00497                   Will be set if the called party chooses to send the calling party to the 'Go Away' script.
00498                </value>
00499                <value name="TORTURE">
00500                   For the Privacy and Screening Modes.
00501                   Will be set if the called party chooses to send the calling party to the 'torture' script.
00502                </value>
00503                <value name="INVALIDARGS" />
00504             </variable>
00505          </variablelist>
00506       </description>
00507    </application>
00508    <application name="RetryDial" language="en_US">
00509       <synopsis>
00510          Place a call, retrying on failure allowing an optional exit extension.
00511       </synopsis>
00512       <syntax>
00513          <parameter name="announce" required="true">
00514             <para>Filename of sound that will be played when no channel can be reached</para>
00515          </parameter>
00516          <parameter name="sleep" required="true">
00517             <para>Number of seconds to wait after a dial attempt failed before a new attempt is made</para>
00518          </parameter>
00519          <parameter name="retries" required="true">
00520             <para>Number of retries</para>
00521             <para>When this is reached flow will continue at the next priority in the dialplan</para>
00522          </parameter>
00523          <parameter name="dialargs" required="true">
00524             <para>Same format as arguments provided to the Dial application</para>
00525          </parameter>
00526       </syntax>
00527       <description>
00528          <para>This application will attempt to place a call using the normal Dial application.
00529          If no channel can be reached, the <replaceable>announce</replaceable> file will be played.
00530          Then, it will wait <replaceable>sleep</replaceable> number of seconds before retrying the call.
00531          After <replaceable>retries</replaceable> number of attempts, the calling channel will continue at the next priority in the dialplan.
00532          If the <replaceable>retries</replaceable> setting is set to 0, this application will retry endlessly.
00533          While waiting to retry a call, a 1 digit extension may be dialed. If that
00534          extension exists in either the context defined in <variable>EXITCONTEXT</variable> or the current
00535          one, The call will jump to that extension immediately.
00536          The <replaceable>dialargs</replaceable> are specified in the same format that arguments are provided
00537          to the Dial application.</para>
00538       </description>
00539    </application>
00540  ***/
00541 
00542 static const char app[] = "Dial";
00543 static const char rapp[] = "RetryDial";
00544 
00545 enum {
00546    OPT_ANNOUNCE =          (1 << 0),
00547    OPT_RESETCDR =          (1 << 1),
00548    OPT_DTMF_EXIT =         (1 << 2),
00549    OPT_SENDDTMF =          (1 << 3),
00550    OPT_FORCECLID =         (1 << 4),
00551    OPT_GO_ON =             (1 << 5),
00552    OPT_CALLEE_HANGUP =     (1 << 6),
00553    OPT_CALLER_HANGUP =     (1 << 7),
00554    OPT_ORIGINAL_CLID =     (1 << 8),
00555    OPT_DURATION_LIMIT =    (1 << 9),
00556    OPT_MUSICBACK =         (1 << 10),
00557    OPT_CALLEE_MACRO =      (1 << 11),
00558    OPT_SCREEN_NOINTRO =    (1 << 12),
00559    OPT_SCREEN_NOCALLERID = (1 << 13),
00560    OPT_IGNORE_CONNECTEDLINE = (1 << 14),
00561    OPT_SCREENING =         (1 << 15),
00562    OPT_PRIVACY =           (1 << 16),
00563    OPT_RINGBACK =          (1 << 17),
00564    OPT_DURATION_STOP =     (1 << 18),
00565    OPT_CALLEE_TRANSFER =   (1 << 19),
00566    OPT_CALLER_TRANSFER =   (1 << 20),
00567    OPT_CALLEE_MONITOR =    (1 << 21),
00568    OPT_CALLER_MONITOR =    (1 << 22),
00569    OPT_GOTO =              (1 << 23),
00570    OPT_OPERMODE =          (1 << 24),
00571    OPT_CALLEE_PARK =       (1 << 25),
00572    OPT_CALLER_PARK =       (1 << 26),
00573    OPT_IGNORE_FORWARDING = (1 << 27),
00574    OPT_CALLEE_GOSUB =      (1 << 28),
00575    OPT_CALLEE_MIXMONITOR = (1 << 29),
00576    OPT_CALLER_MIXMONITOR = (1 << 30),
00577 };
00578 
00579 /* flags are now 64 bits, so keep it up! */
00580 #define DIAL_STILLGOING      (1LLU << 31)
00581 #define DIAL_NOFORWARDHTML   (1LLU << 32)
00582 #define DIAL_CALLERID_ABSENT (1LLU << 33) /* TRUE if caller id is not available for connected line. */
00583 #define OPT_CANCEL_ELSEWHERE (1LLU << 34)
00584 #define OPT_PEER_H           (1LLU << 35)
00585 #define OPT_CALLEE_GO_ON     (1LLU << 36)
00586 #define OPT_CANCEL_TIMEOUT   (1LLU << 37)
00587 #define OPT_FORCE_CID_TAG    (1LLU << 38)
00588 #define OPT_FORCE_CID_PRES   (1LLU << 39)
00589 #define OPT_CALLER_ANSWER    (1LLU << 40)
00590 
00591 enum {
00592    OPT_ARG_ANNOUNCE = 0,
00593    OPT_ARG_SENDDTMF,
00594    OPT_ARG_GOTO,
00595    OPT_ARG_DURATION_LIMIT,
00596    OPT_ARG_MUSICBACK,
00597    OPT_ARG_CALLEE_MACRO,
00598    OPT_ARG_RINGBACK,
00599    OPT_ARG_CALLEE_GOSUB,
00600    OPT_ARG_CALLEE_GO_ON,
00601    OPT_ARG_PRIVACY,
00602    OPT_ARG_DURATION_STOP,
00603    OPT_ARG_OPERMODE,
00604    OPT_ARG_SCREEN_NOINTRO,
00605    OPT_ARG_ORIGINAL_CLID,
00606    OPT_ARG_FORCECLID,
00607    OPT_ARG_FORCE_CID_TAG,
00608    OPT_ARG_FORCE_CID_PRES,
00609    /* note: this entry _MUST_ be the last one in the enum */
00610    OPT_ARG_ARRAY_SIZE,
00611 };
00612 
00613 AST_APP_OPTIONS(dial_exec_options, BEGIN_OPTIONS
00614    AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
00615    AST_APP_OPTION('a', OPT_CALLER_ANSWER),
00616    AST_APP_OPTION('C', OPT_RESETCDR),
00617    AST_APP_OPTION('c', OPT_CANCEL_ELSEWHERE),
00618    AST_APP_OPTION('d', OPT_DTMF_EXIT),
00619    AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
00620    AST_APP_OPTION('e', OPT_PEER_H),
00621    AST_APP_OPTION_ARG('f', OPT_FORCECLID, OPT_ARG_FORCECLID),
00622    AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
00623    AST_APP_OPTION('g', OPT_GO_ON),
00624    AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
00625    AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
00626    AST_APP_OPTION('H', OPT_CALLER_HANGUP),
00627    AST_APP_OPTION('i', OPT_IGNORE_FORWARDING),
00628    AST_APP_OPTION('I', OPT_IGNORE_CONNECTEDLINE),
00629    AST_APP_OPTION('k', OPT_CALLEE_PARK),
00630    AST_APP_OPTION('K', OPT_CALLER_PARK),
00631    AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
00632    AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
00633    AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
00634    AST_APP_OPTION_ARG('n', OPT_SCREEN_NOINTRO, OPT_ARG_SCREEN_NOINTRO),
00635    AST_APP_OPTION('N', OPT_SCREEN_NOCALLERID),
00636    AST_APP_OPTION_ARG('o', OPT_ORIGINAL_CLID, OPT_ARG_ORIGINAL_CLID),
00637    AST_APP_OPTION_ARG('O', OPT_OPERMODE, OPT_ARG_OPERMODE),
00638    AST_APP_OPTION('p', OPT_SCREENING),
00639    AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
00640    AST_APP_OPTION_ARG('r', OPT_RINGBACK, OPT_ARG_RINGBACK),
00641    AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
00642    AST_APP_OPTION_ARG('s', OPT_FORCE_CID_TAG, OPT_ARG_FORCE_CID_TAG),
00643    AST_APP_OPTION_ARG('u', OPT_FORCE_CID_PRES, OPT_ARG_FORCE_CID_PRES),
00644    AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
00645    AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
00646    AST_APP_OPTION_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
00647    AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
00648    AST_APP_OPTION('W', OPT_CALLER_MONITOR),
00649    AST_APP_OPTION('x', OPT_CALLEE_MIXMONITOR),
00650    AST_APP_OPTION('X', OPT_CALLER_MIXMONITOR),
00651    AST_APP_OPTION('z', OPT_CANCEL_TIMEOUT),
00652 END_OPTIONS );
00653 
00654 #define CAN_EARLY_BRIDGE(flags,chan,peer) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
00655    OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
00656    OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK |  \
00657    OPT_CALLER_PARK | OPT_ANNOUNCE | OPT_CALLEE_MACRO | OPT_CALLEE_GOSUB) && \
00658    !chan->audiohooks && !peer->audiohooks && \
00659    ast_framehook_list_is_empty(chan->framehooks) && ast_framehook_list_is_empty(peer->framehooks))
00660 
00661 /*
00662  * The list of active channels
00663  */
00664 struct chanlist {
00665    struct chanlist *next;
00666    struct ast_channel *chan;
00667    uint64_t flags;
00668    /*! Saved connected party info from an AST_CONTROL_CONNECTED_LINE. */
00669    struct ast_party_connected_line connected;
00670    /*! TRUE if an AST_CONTROL_CONNECTED_LINE update was saved to the connected element. */
00671    unsigned int pending_connected_update:1;
00672    struct ast_aoc_decoded *aoc_s_rate_list;
00673 };
00674 
00675 static int detect_disconnect(struct ast_channel *chan, char code, struct ast_str **featurecode);
00676 
00677 static void chanlist_free(struct chanlist *outgoing)
00678 {
00679    ast_party_connected_line_free(&outgoing->connected);
00680    ast_aoc_destroy_decoded(outgoing->aoc_s_rate_list);
00681    ast_free(outgoing);
00682 }
00683 
00684 static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception, int answered_elsewhere)
00685 {
00686    /* Hang up a tree of stuff */
00687    struct chanlist *oo;
00688    while (outgoing) {
00689       /* Hangup any existing lines we have open */
00690       if (outgoing->chan && (outgoing->chan != exception)) {
00691          if (answered_elsewhere) {
00692             /* The flag is used for local channel inheritance and stuff */
00693             ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00694             /* This is for the channel drivers */
00695             outgoing->chan->hangupcause = AST_CAUSE_ANSWERED_ELSEWHERE;
00696          }
00697          ast_hangup(outgoing->chan);
00698       }
00699       oo = outgoing;
00700       outgoing = outgoing->next;
00701       chanlist_free(oo);
00702    }
00703 }
00704 
00705 #define AST_MAX_WATCHERS 256
00706 
00707 /*
00708  * argument to handle_cause() and other functions.
00709  */
00710 struct cause_args {
00711    struct ast_channel *chan;
00712    int busy;
00713    int congestion;
00714    int nochan;
00715 };
00716 
00717 static void handle_cause(int cause, struct cause_args *num)
00718 {
00719    struct ast_cdr *cdr = num->chan->cdr;
00720 
00721    switch(cause) {
00722    case AST_CAUSE_BUSY:
00723       if (cdr)
00724          ast_cdr_busy(cdr);
00725       num->busy++;
00726       break;
00727 
00728    case AST_CAUSE_CONGESTION:
00729       if (cdr)
00730          ast_cdr_failed(cdr);
00731       num->congestion++;
00732       break;
00733 
00734    case AST_CAUSE_NO_ROUTE_DESTINATION:
00735    case AST_CAUSE_UNREGISTERED:
00736       if (cdr)
00737          ast_cdr_failed(cdr);
00738       num->nochan++;
00739       break;
00740 
00741    case AST_CAUSE_NO_ANSWER:
00742       if (cdr) {
00743          ast_cdr_noanswer(cdr);
00744       }
00745       break;
00746    case AST_CAUSE_NORMAL_CLEARING:
00747       break;
00748 
00749    default:
00750       num->nochan++;
00751       break;
00752    }
00753 }
00754 
00755 static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri)
00756 {
00757    char rexten[2] = { exten, '\0' };
00758 
00759    if (context) {
00760       if (!ast_goto_if_exists(chan, context, rexten, pri))
00761          return 1;
00762    } else {
00763       if (!ast_goto_if_exists(chan, chan->context, rexten, pri))
00764          return 1;
00765       else if (!ast_strlen_zero(chan->macrocontext)) {
00766          if (!ast_goto_if_exists(chan, chan->macrocontext, rexten, pri))
00767             return 1;
00768       }
00769    }
00770    return 0;
00771 }
00772 
00773 /* do not call with chan lock held */
00774 static const char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
00775 {
00776    const char *context;
00777    const char *exten;
00778 
00779    ast_channel_lock(chan);
00780    context = ast_strdupa(S_OR(chan->macrocontext, chan->context));
00781    exten = ast_strdupa(S_OR(chan->macroexten, chan->exten));
00782    ast_channel_unlock(chan);
00783 
00784    return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
00785 }
00786 
00787 static void senddialevent(struct ast_channel *src, struct ast_channel *dst, const char *dialstring)
00788 {
00789    struct ast_channel *chans[] = { src, dst };
00790    ast_manager_event_multichan(EVENT_FLAG_CALL, "Dial", 2, chans,
00791       "SubEvent: Begin\r\n"
00792       "Channel: %s\r\n"
00793       "Destination: %s\r\n"
00794       "CallerIDNum: %s\r\n"
00795       "CallerIDName: %s\r\n"
00796       "ConnectedLineNum: %s\r\n"
00797       "ConnectedLineName: %s\r\n"
00798       "UniqueID: %s\r\n"
00799       "DestUniqueID: %s\r\n"
00800       "Dialstring: %s\r\n",
00801       src->name, dst->name,
00802       S_COR(src->caller.id.number.valid, src->caller.id.number.str, "<unknown>"),
00803       S_COR(src->caller.id.name.valid, src->caller.id.name.str, "<unknown>"),
00804       S_COR(src->connected.id.number.valid, src->connected.id.number.str, "<unknown>"),
00805       S_COR(src->connected.id.name.valid, src->connected.id.name.str, "<unknown>"),
00806       src->uniqueid, dst->uniqueid,
00807       dialstring ? dialstring : "");
00808 }
00809 
00810 static void senddialendevent(struct ast_channel *src, const char *dialstatus)
00811 {
00812    ast_manager_event(src, EVENT_FLAG_CALL, "Dial",
00813       "SubEvent: End\r\n"
00814       "Channel: %s\r\n"
00815       "UniqueID: %s\r\n"
00816       "DialStatus: %s\r\n",
00817       src->name, src->uniqueid, dialstatus);
00818 }
00819 
00820 /*!
00821  * helper function for wait_for_answer()
00822  *
00823  * \param o Outgoing call channel list.
00824  * \param num Incoming call channel cause accumulation
00825  * \param peerflags Dial option flags
00826  * \param single TRUE if there is only one outgoing call.
00827  * \param caller_entertained TRUE if the caller is being entertained by MOH or ringback.
00828  * \param to Remaining call timeout time.
00829  * \param forced_clid OPT_FORCECLID caller id to send
00830  * \param stored_clid Caller id representing the called party if needed
00831  *
00832  * XXX this code is highly suspicious, as it essentially overwrites
00833  * the outgoing channel without properly deleting it.
00834  *
00835  * \todo eventually this function should be intergrated into and replaced by ast_call_forward()
00836  */
00837 static void do_forward(struct chanlist *o, struct cause_args *num,
00838    struct ast_flags64 *peerflags, int single, int caller_entertained, int *to,
00839    struct ast_party_id *forced_clid, struct ast_party_id *stored_clid)
00840 {
00841    char tmpchan[256];
00842    struct ast_channel *original = o->chan;
00843    struct ast_channel *c = o->chan; /* the winner */
00844    struct ast_channel *in = num->chan; /* the input channel */
00845    char *stuff;
00846    char *tech;
00847    int cause;
00848    struct ast_party_caller caller;
00849 
00850    ast_copy_string(tmpchan, c->call_forward, sizeof(tmpchan));
00851    if ((stuff = strchr(tmpchan, '/'))) {
00852       *stuff++ = '\0';
00853       tech = tmpchan;
00854    } else {
00855       const char *forward_context;
00856       ast_channel_lock(c);
00857       forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
00858       if (ast_strlen_zero(forward_context)) {
00859          forward_context = NULL;
00860       }
00861       snprintf(tmpchan, sizeof(tmpchan), "%s@%s", c->call_forward, forward_context ? forward_context : c->context);
00862       ast_channel_unlock(c);
00863       stuff = tmpchan;
00864       tech = "Local";
00865    }
00866    if (!strcasecmp(tech, "Local")) {
00867       /*
00868        * Drop the connected line update block for local channels since
00869        * this is going to run dialplan and the user can change his
00870        * mind about what connected line information he wants to send.
00871        */
00872       ast_clear_flag64(o, OPT_IGNORE_CONNECTEDLINE);
00873    }
00874 
00875    ast_cel_report_event(in, AST_CEL_FORWARD, NULL, c->call_forward, NULL);
00876 
00877    /* Before processing channel, go ahead and check for forwarding */
00878    ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
00879    /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
00880    if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
00881       ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
00882       c = o->chan = NULL;
00883       cause = AST_CAUSE_BUSY;
00884    } else {
00885       /* Setup parameters */
00886       c = o->chan = ast_request(tech, in->nativeformats, in, stuff, &cause);
00887       if (c) {
00888          if (single && !caller_entertained) {
00889             ast_channel_make_compatible(o->chan, in);
00890          }
00891          ast_channel_lock_both(in, o->chan);
00892          ast_channel_inherit_variables(in, o->chan);
00893          ast_channel_datastore_inherit(in, o->chan);
00894          ast_channel_unlock(in);
00895          ast_channel_unlock(o->chan);
00896          /* When a call is forwarded, we don't want to track new interfaces
00897           * dialed for CC purposes. Setting the done flag will ensure that
00898           * any Dial operations that happen later won't record CC interfaces.
00899           */
00900          ast_ignore_cc(o->chan);
00901          ast_log(LOG_NOTICE, "Not accepting call completion offers from call-forward recipient %s\n", o->chan->name);
00902       } else
00903          ast_log(LOG_NOTICE,
00904             "Forwarding failed to create channel to dial '%s/%s' (cause = %d)\n",
00905             tech, stuff, cause);
00906    }
00907    if (!c) {
00908       ast_clear_flag64(o, DIAL_STILLGOING);
00909       handle_cause(cause, num);
00910       ast_hangup(original);
00911    } else {
00912       ast_channel_lock_both(c, original);
00913       ast_party_redirecting_copy(&c->redirecting, &original->redirecting);
00914       ast_channel_unlock(c);
00915       ast_channel_unlock(original);
00916 
00917       ast_channel_lock_both(c, in);
00918 
00919       if (single && !caller_entertained && CAN_EARLY_BRIDGE(peerflags, c, in)) {
00920          ast_rtp_instance_early_bridge_make_compatible(c, in);
00921       }
00922 
00923       if (!c->redirecting.from.number.valid
00924          || ast_strlen_zero(c->redirecting.from.number.str)) {
00925          /*
00926           * The call was not previously redirected so it is
00927           * now redirected from this number.
00928           */
00929          ast_party_number_free(&c->redirecting.from.number);
00930          ast_party_number_init(&c->redirecting.from.number);
00931          c->redirecting.from.number.valid = 1;
00932          c->redirecting.from.number.str =
00933             ast_strdup(S_OR(in->macroexten, in->exten));
00934       }
00935 
00936       c->dialed.transit_network_select = in->dialed.transit_network_select;
00937 
00938       /* Determine CallerID to store in outgoing channel. */
00939       ast_party_caller_set_init(&caller, &c->caller);
00940       if (ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
00941          caller.id = *stored_clid;
00942          ast_channel_set_caller_event(c, &caller, NULL);
00943          ast_set_flag64(o, DIAL_CALLERID_ABSENT);
00944       } else if (ast_strlen_zero(S_COR(c->caller.id.number.valid,
00945          c->caller.id.number.str, NULL))) {
00946          /*
00947           * The new channel has no preset CallerID number by the channel
00948           * driver.  Use the dialplan extension and hint name.
00949           */
00950          caller.id = *stored_clid;
00951          ast_channel_set_caller_event(c, &caller, NULL);
00952          ast_set_flag64(o, DIAL_CALLERID_ABSENT);
00953       } else {
00954          ast_clear_flag64(o, DIAL_CALLERID_ABSENT);
00955       }
00956 
00957       /* Determine CallerID for outgoing channel to send. */
00958       if (ast_test_flag64(o, OPT_FORCECLID)) {
00959          struct ast_party_connected_line connected;
00960 
00961          ast_party_connected_line_init(&connected);
00962          connected.id = *forced_clid;
00963          ast_party_connected_line_copy(&c->connected, &connected);
00964       } else {
00965          ast_connected_line_copy_from_caller(&c->connected, &in->caller);
00966       }
00967 
00968       ast_string_field_set(c, accountcode, in->accountcode);
00969 
00970       c->appl = "AppDial";
00971       c->data = "(Outgoing Line)";
00972 
00973       ast_channel_unlock(in);
00974       if (single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
00975          struct ast_party_redirecting redirecting;
00976 
00977          /*
00978           * Redirecting updates to the caller make sense only on single
00979           * calls.
00980           *
00981           * We must unlock c before calling
00982           * ast_channel_redirecting_macro, because we put c into
00983           * autoservice there.  That is pretty much a guaranteed
00984           * deadlock.  This is why the handling of c's lock may seem a
00985           * bit unusual here.
00986           */
00987          ast_party_redirecting_init(&redirecting);
00988          ast_party_redirecting_copy(&redirecting, &c->redirecting);
00989          ast_channel_unlock(c);
00990          if (ast_channel_redirecting_macro(c, in, &redirecting, 1, 0)) {
00991             ast_channel_update_redirecting(in, &redirecting, NULL);
00992          }
00993          ast_party_redirecting_free(&redirecting);
00994       } else {
00995          ast_channel_unlock(c);
00996       }
00997 
00998       if (ast_test_flag64(peerflags, OPT_CANCEL_TIMEOUT)) {
00999          *to = -1;
01000       }
01001 
01002       if (ast_call(c, stuff, 0)) {
01003          ast_log(LOG_NOTICE, "Forwarding failed to dial '%s/%s'\n",
01004             tech, stuff);
01005          ast_clear_flag64(o, DIAL_STILLGOING);
01006          ast_hangup(original);
01007          ast_hangup(c);
01008          c = o->chan = NULL;
01009          num->nochan++;
01010       } else {
01011          ast_channel_lock_both(c, in);
01012          senddialevent(in, c, stuff);
01013          ast_channel_unlock(in);
01014          ast_channel_unlock(c);
01015          /* Hangup the original channel now, in case we needed it */
01016          ast_hangup(original);
01017       }
01018       if (single && !caller_entertained) {
01019          ast_indicate(in, -1);
01020       }
01021    }
01022 }
01023 
01024 /* argument used for some functions. */
01025 struct privacy_args {
01026    int sentringing;
01027    int privdb_val;
01028    char privcid[256];
01029    char privintro[1024];
01030    char status[256];
01031 };
01032 
01033 static struct ast_channel *wait_for_answer(struct ast_channel *in,
01034    struct chanlist *outgoing, int *to, struct ast_flags64 *peerflags,
01035    char *opt_args[],
01036    struct privacy_args *pa,
01037    const struct cause_args *num_in, int *result, char *dtmf_progress,
01038    const int ignore_cc,
01039    struct ast_party_id *forced_clid, struct ast_party_id *stored_clid)
01040 {
01041    struct cause_args num = *num_in;
01042    int prestart = num.busy + num.congestion + num.nochan;
01043    int orig = *to;
01044    struct ast_channel *peer = NULL;
01045    /* single is set if only one destination is enabled */
01046    int single = outgoing && !outgoing->next;
01047    int caller_entertained = outgoing
01048       && ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
01049 #ifdef HAVE_EPOLL
01050    struct chanlist *epollo;
01051 #endif
01052    struct ast_party_connected_line connected_caller;
01053    struct ast_str *featurecode = ast_str_alloca(FEATURE_MAX_LEN + 1);
01054    int cc_recall_core_id;
01055    int is_cc_recall;
01056    int cc_frame_received = 0;
01057    int num_ringing = 0;
01058    struct timeval start = ast_tvnow();
01059 
01060    ast_party_connected_line_init(&connected_caller);
01061    if (single) {
01062       /* Turn off hold music, etc */
01063       if (!caller_entertained) {
01064          ast_deactivate_generator(in);
01065          /* If we are calling a single channel, and not providing ringback or music, */
01066          /* then, make them compatible for in-band tone purpose */
01067          if (ast_channel_make_compatible(outgoing->chan, in) < 0) {
01068             /* If these channels can not be made compatible, 
01069              * there is no point in continuing.  The bridge
01070              * will just fail if it gets that far.
01071              */
01072             *to = -1;
01073             strcpy(pa->status, "CONGESTION");
01074             ast_cdr_failed(in->cdr);
01075             return NULL;
01076          }
01077       }
01078 
01079       if (!ast_test_flag64(outgoing, OPT_IGNORE_CONNECTEDLINE)
01080          && !ast_test_flag64(outgoing, DIAL_CALLERID_ABSENT)) {
01081          ast_channel_lock(outgoing->chan);
01082          ast_connected_line_copy_from_caller(&connected_caller, &outgoing->chan->caller);
01083          ast_channel_unlock(outgoing->chan);
01084          connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
01085          if (ast_channel_connected_line_macro(outgoing->chan, in, &connected_caller, 1, 0)) {
01086             ast_channel_update_connected_line(in, &connected_caller, NULL);
01087          }
01088          ast_party_connected_line_free(&connected_caller);
01089       }
01090    }
01091 
01092    is_cc_recall = ast_cc_is_recall(in, &cc_recall_core_id, NULL);
01093 
01094 #ifdef HAVE_EPOLL
01095    for (epollo = outgoing; epollo; epollo = epollo->next)
01096       ast_poll_channel_add(in, epollo->chan);
01097 #endif
01098 
01099    while ((*to = ast_remaining_ms(start, orig)) && !peer) {
01100       struct chanlist *o;
01101       int pos = 0; /* how many channels do we handle */
01102       int numlines = prestart;
01103       struct ast_channel *winner;
01104       struct ast_channel *watchers[AST_MAX_WATCHERS];
01105 
01106       watchers[pos++] = in;
01107       for (o = outgoing; o; o = o->next) {
01108          /* Keep track of important channels */
01109          if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
01110             watchers[pos++] = o->chan;
01111          numlines++;
01112       }
01113       if (pos == 1) { /* only the input channel is available */
01114          if (numlines == (num.busy + num.congestion + num.nochan)) {
01115             ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
01116             if (num.busy)
01117                strcpy(pa->status, "BUSY");
01118             else if (num.congestion)
01119                strcpy(pa->status, "CONGESTION");
01120             else if (num.nochan)
01121                strcpy(pa->status, "CHANUNAVAIL");
01122          } else {
01123             ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
01124          }
01125          *to = 0;
01126          if (is_cc_recall) {
01127             ast_cc_failed(cc_recall_core_id, "Everyone is busy/congested for the recall. How sad");
01128          }
01129          return NULL;
01130       }
01131       winner = ast_waitfor_n(watchers, pos, to);
01132       for (o = outgoing; o; o = o->next) {
01133          struct ast_frame *f;
01134          struct ast_channel *c = o->chan;
01135 
01136          if (c == NULL)
01137             continue;
01138          if (ast_test_flag64(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
01139             if (!peer) {
01140                ast_verb(3, "%s answered %s\n", c->name, in->name);
01141                if (!single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
01142                   if (o->pending_connected_update) {
01143                      if (ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
01144                         ast_channel_update_connected_line(in, &o->connected, NULL);
01145                      }
01146                   } else if (!ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
01147                      ast_channel_lock(c);
01148                      ast_connected_line_copy_from_caller(&connected_caller, &c->caller);
01149                      ast_channel_unlock(c);
01150                      connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
01151                      if (ast_channel_connected_line_macro(c, in, &connected_caller, 1, 0)) {
01152                         ast_channel_update_connected_line(in, &connected_caller, NULL);
01153                      }
01154                      ast_party_connected_line_free(&connected_caller);
01155                   }
01156                }
01157                if (o->aoc_s_rate_list) {
01158                   size_t encoded_size;
01159                   struct ast_aoc_encoded *encoded;
01160                   if ((encoded = ast_aoc_encode(o->aoc_s_rate_list, &encoded_size, o->chan))) {
01161                      ast_indicate_data(in, AST_CONTROL_AOC, encoded, encoded_size);
01162                      ast_aoc_destroy_encoded(encoded);
01163                   }
01164                }
01165                peer = c;
01166                ast_copy_flags64(peerflags, o,
01167                   OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
01168                   OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
01169                   OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
01170                   OPT_CALLEE_PARK | OPT_CALLER_PARK |
01171                   OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
01172                   DIAL_NOFORWARDHTML);
01173                ast_string_field_set(c, dialcontext, "");
01174                ast_copy_string(c->exten, "", sizeof(c->exten));
01175             }
01176             continue;
01177          }
01178          if (c != winner)
01179             continue;
01180          /* here, o->chan == c == winner */
01181          if (!ast_strlen_zero(c->call_forward)) {
01182             pa->sentringing = 0;
01183             if (!ignore_cc && (f = ast_read(c))) {
01184                if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_CC) {
01185                   /* This channel is forwarding the call, and is capable of CC, so
01186                    * be sure to add the new device interface to the list
01187                    */
01188                   ast_handle_cc_control_frame(in, c, f->data.ptr);
01189                }
01190                ast_frfree(f);
01191             }
01192 
01193             if (o->pending_connected_update) {
01194                /*
01195                 * Re-seed the chanlist's connected line information with
01196                 * previously acquired connected line info from the incoming
01197                 * channel.  The previously acquired connected line info could
01198                 * have been set through the CONNECTED_LINE dialplan function.
01199                 */
01200                o->pending_connected_update = 0;
01201                ast_channel_lock(in);
01202                ast_party_connected_line_copy(&o->connected, &in->connected);
01203                ast_channel_unlock(in);
01204             }
01205 
01206             do_forward(o, &num, peerflags, single, caller_entertained, &orig,
01207                forced_clid, stored_clid);
01208 
01209             if (single && o->chan
01210                && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)
01211                && !ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
01212                ast_channel_lock(o->chan);
01213                ast_connected_line_copy_from_caller(&connected_caller, &o->chan->caller);
01214                ast_channel_unlock(o->chan);
01215                connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
01216                if (ast_channel_connected_line_macro(o->chan, in, &connected_caller, 1, 0)) {
01217                   ast_channel_update_connected_line(in, &connected_caller, NULL);
01218                }
01219                ast_party_connected_line_free(&connected_caller);
01220             }
01221             continue;
01222          }
01223          f = ast_read(winner);
01224          if (!f) {
01225             in->hangupcause = c->hangupcause;
01226 #ifdef HAVE_EPOLL
01227             ast_poll_channel_del(in, c);
01228 #endif
01229             ast_hangup(c);
01230             c = o->chan = NULL;
01231             ast_clear_flag64(o, DIAL_STILLGOING);
01232             handle_cause(in->hangupcause, &num);
01233             continue;
01234          }
01235          switch (f->frametype) {
01236          case AST_FRAME_CONTROL:
01237             switch (f->subclass.integer) {
01238             case AST_CONTROL_ANSWER:
01239                /* This is our guy if someone answered. */
01240                if (!peer) {
01241                   ast_verb(3, "%s answered %s\n", c->name, in->name);
01242                   if (!single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
01243                      if (o->pending_connected_update) {
01244                         if (ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
01245                            ast_channel_update_connected_line(in, &o->connected, NULL);
01246                         }
01247                      } else if (!ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
01248                         ast_channel_lock(c);
01249                         ast_connected_line_copy_from_caller(&connected_caller, &c->caller);
01250                         ast_channel_unlock(c);
01251                         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
01252                         if (ast_channel_connected_line_macro(c, in, &connected_caller, 1, 0)) {
01253                            ast_channel_update_connected_line(in, &connected_caller, NULL);
01254                         }
01255                         ast_party_connected_line_free(&connected_caller);
01256                      }
01257                   }
01258                   if (o->aoc_s_rate_list) {
01259                      size_t encoded_size;
01260                      struct ast_aoc_encoded *encoded;
01261                      if ((encoded = ast_aoc_encode(o->aoc_s_rate_list, &encoded_size, o->chan))) {
01262                         ast_indicate_data(in, AST_CONTROL_AOC, encoded, encoded_size);
01263                         ast_aoc_destroy_encoded(encoded);
01264                      }
01265                   }
01266                   peer = c;
01267                   if (peer->cdr) {
01268                      peer->cdr->answer = ast_tvnow();
01269                      peer->cdr->disposition = AST_CDR_ANSWERED;
01270                   }
01271                   ast_copy_flags64(peerflags, o,
01272                      OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
01273                      OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
01274                      OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
01275                      OPT_CALLEE_PARK | OPT_CALLER_PARK |
01276                      OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
01277                      DIAL_NOFORWARDHTML);
01278                   ast_string_field_set(c, dialcontext, "");
01279                   ast_copy_string(c->exten, "", sizeof(c->exten));
01280                   if (CAN_EARLY_BRIDGE(peerflags, in, peer))
01281                      /* Setup early bridge if appropriate */
01282                      ast_channel_early_bridge(in, peer);
01283                }
01284                /* If call has been answered, then the eventual hangup is likely to be normal hangup */
01285                in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
01286                c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
01287                break;
01288             case AST_CONTROL_BUSY:
01289                ast_verb(3, "%s is busy\n", c->name);
01290                in->hangupcause = c->hangupcause;
01291                ast_hangup(c);
01292                c = o->chan = NULL;
01293                ast_clear_flag64(o, DIAL_STILLGOING);
01294                handle_cause(AST_CAUSE_BUSY, &num);
01295                break;
01296             case AST_CONTROL_CONGESTION:
01297                ast_verb(3, "%s is circuit-busy\n", c->name);
01298                in->hangupcause = c->hangupcause;
01299                ast_hangup(c);
01300                c = o->chan = NULL;
01301                ast_clear_flag64(o, DIAL_STILLGOING);
01302                handle_cause(AST_CAUSE_CONGESTION, &num);
01303                break;
01304             case AST_CONTROL_RINGING:
01305                /* This is a tricky area to get right when using a native
01306                 * CC agent. The reason is that we do the best we can to send only a
01307                 * single ringing notification to the caller.
01308                 *
01309                 * Call completion complicates the logic used here. CCNR is typically
01310                 * offered during a ringing message. Let's say that party A calls
01311                 * parties B, C, and D. B and C do not support CC requests, but D
01312                 * does. If we were to receive a ringing notification from B before
01313                 * the others, then we would end up sending a ringing message to
01314                 * A with no CCNR offer present.
01315                 *
01316                 * The approach that we have taken is that if we receive a ringing
01317                 * response from a party and no CCNR offer is present, we need to
01318                 * wait. Specifically, we need to wait until either a) a called party
01319                 * offers CCNR in its ringing response or b) all called parties have
01320                 * responded in some way to our call and none offers CCNR.
01321                 *
01322                 * The drawback to this is that if one of the parties has a delayed
01323                 * response or, god forbid, one just plain doesn't respond to our
01324                 * outgoing call, then this will result in a significant delay between
01325                 * when the caller places the call and hears ringback.
01326                 *
01327                 * Note also that if CC is disabled for this call, then it is perfectly
01328                 * fine for ringing frames to get sent through.
01329                 */
01330                ++num_ringing;
01331                if (ignore_cc || cc_frame_received || num_ringing == numlines) {
01332                   ast_verb(3, "%s is ringing\n", c->name);
01333                   /* Setup early media if appropriate */
01334                   if (single && !caller_entertained
01335                      && CAN_EARLY_BRIDGE(peerflags, in, c)) {
01336                      ast_channel_early_bridge(in, c);
01337                   }
01338                   if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK) && ast_strlen_zero(opt_args[OPT_ARG_RINGBACK])) {
01339                      ast_indicate(in, AST_CONTROL_RINGING);
01340                      pa->sentringing++;
01341                   }
01342                }
01343                break;
01344             case AST_CONTROL_PROGRESS:
01345                ast_verb(3, "%s is making progress passing it to %s\n", c->name, in->name);
01346                /* Setup early media if appropriate */
01347                if (single && !caller_entertained
01348                   && CAN_EARLY_BRIDGE(peerflags, in, c)) {
01349                   ast_channel_early_bridge(in, c);
01350                }
01351                if (!ast_test_flag64(outgoing, OPT_RINGBACK)) {
01352                   if (single || (!single && !pa->sentringing)) {
01353                      ast_indicate(in, AST_CONTROL_PROGRESS);
01354                   }
01355                }
01356                if (!ast_strlen_zero(dtmf_progress)) {
01357                   ast_verb(3,
01358                      "Sending DTMF '%s' to the called party as result of receiving a PROGRESS message.\n",
01359                      dtmf_progress);
01360                   ast_dtmf_stream(c, in, dtmf_progress, 250, 0);
01361                }
01362                break;
01363             case AST_CONTROL_VIDUPDATE:
01364             case AST_CONTROL_SRCUPDATE:
01365             case AST_CONTROL_SRCCHANGE:
01366                if (!single || caller_entertained) {
01367                   break;
01368                }
01369                ast_verb(3, "%s requested media update control %d, passing it to %s\n",
01370                   c->name, f->subclass.integer, in->name);
01371                ast_indicate(in, f->subclass.integer);
01372                break;
01373             case AST_CONTROL_CONNECTED_LINE:
01374                if (ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
01375                   ast_verb(3, "Connected line update to %s prevented.\n", in->name);
01376                   break;
01377                }
01378                if (!single) {
01379                   struct ast_party_connected_line connected;
01380 
01381                   ast_verb(3, "%s connected line has changed. Saving it until answer for %s\n",
01382                      c->name, in->name);
01383                   ast_party_connected_line_set_init(&connected, &o->connected);
01384                   ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected);
01385                   ast_party_connected_line_set(&o->connected, &connected, NULL);
01386                   ast_party_connected_line_free(&connected);
01387                   o->pending_connected_update = 1;
01388                   break;
01389                }
01390                if (ast_channel_connected_line_macro(c, in, f, 1, 1)) {
01391                   ast_indicate_data(in, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
01392                }
01393                break;
01394             case AST_CONTROL_AOC:
01395                {
01396                   struct ast_aoc_decoded *decoded = ast_aoc_decode(f->data.ptr, f->datalen, o->chan);
01397                   if (decoded && (ast_aoc_get_msg_type(decoded) == AST_AOC_S)) {
01398                      ast_aoc_destroy_decoded(o->aoc_s_rate_list);
01399                      o->aoc_s_rate_list = decoded;
01400                   } else {
01401                      ast_aoc_destroy_decoded(decoded);
01402                   }
01403                }
01404                break;
01405             case AST_CONTROL_REDIRECTING:
01406                if (!single) {
01407                   /*
01408                    * Redirecting updates to the caller make sense only on single
01409                    * calls.
01410                    */
01411                   break;
01412                }
01413                if (ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
01414                   ast_verb(3, "Redirecting update to %s prevented.\n", in->name);
01415                   break;
01416                }
01417                ast_verb(3, "%s redirecting info has changed, passing it to %s\n",
01418                   c->name, in->name);
01419                if (ast_channel_redirecting_macro(c, in, f, 1, 1)) {
01420                   ast_indicate_data(in, AST_CONTROL_REDIRECTING, f->data.ptr, f->datalen);
01421                }
01422                pa->sentringing = 0;
01423                break;
01424             case AST_CONTROL_PROCEEDING:
01425                ast_verb(3, "%s is proceeding passing it to %s\n", c->name, in->name);
01426                if (single && !caller_entertained
01427                   && CAN_EARLY_BRIDGE(peerflags, in, c)) {
01428                   ast_channel_early_bridge(in, c);
01429                }
01430                if (!ast_test_flag64(outgoing, OPT_RINGBACK))
01431                   ast_indicate(in, AST_CONTROL_PROCEEDING);
01432                break;
01433             case AST_CONTROL_HOLD:
01434                /* XXX this should be saved like AST_CONTROL_CONNECTED_LINE for !single || caller_entertained */
01435                ast_verb(3, "Call on %s placed on hold\n", c->name);
01436                ast_indicate_data(in, AST_CONTROL_HOLD, f->data.ptr, f->datalen);
01437                break;
01438             case AST_CONTROL_UNHOLD:
01439                /* XXX this should be saved like AST_CONTROL_CONNECTED_LINE for !single || caller_entertained */
01440                ast_verb(3, "Call on %s left from hold\n", c->name);
01441                ast_indicate(in, AST_CONTROL_UNHOLD);
01442                break;
01443             case AST_CONTROL_OFFHOOK:
01444             case AST_CONTROL_FLASH:
01445                /* Ignore going off hook and flash */
01446                break;
01447             case AST_CONTROL_CC:
01448                if (!ignore_cc) {
01449                   ast_handle_cc_control_frame(in, c, f->data.ptr);
01450                   cc_frame_received = 1;
01451                }
01452                break;
01453             case -1:
01454                if (single && !caller_entertained) {
01455                   ast_verb(3, "%s stopped sounds\n", c->name);
01456                   ast_indicate(in, -1);
01457                   pa->sentringing = 0;
01458                }
01459                break;
01460             default:
01461                ast_debug(1, "Dunno what to do with control type %d\n", f->subclass.integer);
01462                break;
01463             }
01464             break;
01465          case AST_FRAME_VOICE:
01466          case AST_FRAME_IMAGE:
01467             if (caller_entertained) {
01468                break;
01469             }
01470             /* Fall through */
01471          case AST_FRAME_TEXT:
01472             if (single && ast_write(in, f)) {
01473                ast_log(LOG_WARNING, "Unable to write frametype: %u\n",
01474                   f->frametype);
01475             }
01476             break;
01477          case AST_FRAME_HTML:
01478             if (single && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)
01479                && ast_channel_sendhtml(in, f->subclass.integer, f->data.ptr, f->datalen) == -1) {
01480                ast_log(LOG_WARNING, "Unable to send URL\n");
01481             }
01482             break;
01483          default:
01484             break;
01485          }
01486          ast_frfree(f);
01487       } /* end for */
01488       if (winner == in) {
01489          struct ast_frame *f = ast_read(in);
01490 #if 0
01491          if (f && (f->frametype != AST_FRAME_VOICE))
01492             printf("Frame type: %d, %d\n", f->frametype, f->subclass);
01493          else if (!f || (f->frametype != AST_FRAME_VOICE))
01494             printf("Hangup received on %s\n", in->name);
01495 #endif
01496          if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_HANGUP))) {
01497             /* Got hung up */
01498             *to = -1;
01499             strcpy(pa->status, "CANCEL");
01500             ast_cdr_noanswer(in->cdr);
01501             if (f) {
01502                if (f->data.uint32) {
01503                   in->hangupcause = f->data.uint32;
01504                }
01505                ast_frfree(f);
01506             }
01507             if (is_cc_recall) {
01508                ast_cc_completed(in, "CC completed, although the caller hung up (cancelled)");
01509             }
01510             return NULL;
01511          }
01512 
01513          /* now f is guaranteed non-NULL */
01514          if (f->frametype == AST_FRAME_DTMF) {
01515             if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
01516                const char *context;
01517                ast_channel_lock(in);
01518                context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
01519                if (onedigit_goto(in, context, (char) f->subclass.integer, 1)) {
01520                   ast_verb(3, "User hit %c to disconnect call.\n", f->subclass.integer);
01521                   *to = 0;
01522                   ast_cdr_noanswer(in->cdr);
01523                   *result = f->subclass.integer;
01524                   strcpy(pa->status, "CANCEL");
01525                   ast_frfree(f);
01526                   ast_channel_unlock(in);
01527                   if (is_cc_recall) {
01528                      ast_cc_completed(in, "CC completed, but the caller used DTMF to exit");
01529                   }
01530                   return NULL;
01531                }
01532                ast_channel_unlock(in);
01533             }
01534 
01535             if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) &&
01536                detect_disconnect(in, f->subclass.integer, &featurecode)) {
01537                ast_verb(3, "User requested call disconnect.\n");
01538                *to = 0;
01539                strcpy(pa->status, "CANCEL");
01540                ast_cdr_noanswer(in->cdr);
01541                ast_frfree(f);
01542                if (is_cc_recall) {
01543                   ast_cc_completed(in, "CC completed, but the caller hung up with DTMF");
01544                }
01545                return NULL;
01546             }
01547          }
01548 
01549          /* Send the frame from the in channel to all outgoing channels. */
01550          for (o = outgoing; o; o = o->next) {
01551             if (!o->chan || !ast_test_flag64(o, DIAL_STILLGOING)) {
01552                /* This outgoing channel has died so don't send the frame to it. */
01553                continue;
01554             }
01555             switch (f->frametype) {
01556             case AST_FRAME_HTML:
01557                /* Forward HTML stuff */
01558                if (!ast_test_flag64(o, DIAL_NOFORWARDHTML)
01559                   && ast_channel_sendhtml(o->chan, f->subclass.integer, f->data.ptr, f->datalen) == -1) {
01560                   ast_log(LOG_WARNING, "Unable to send URL\n");
01561                }
01562                break;
01563             case AST_FRAME_VOICE:
01564             case AST_FRAME_IMAGE:
01565                if (!single || caller_entertained) {
01566                   /*
01567                    * We are calling multiple parties or caller is being
01568                    * entertained and has thus not been made compatible.
01569                    * No need to check any other called parties.
01570                    */
01571                   goto skip_frame;
01572                }
01573                /* Fall through */
01574             case AST_FRAME_TEXT:
01575             case AST_FRAME_DTMF_BEGIN:
01576             case AST_FRAME_DTMF_END:
01577                if (ast_write(o->chan, f)) {
01578                   ast_log(LOG_WARNING, "Unable to forward frametype: %u\n",
01579                      f->frametype);
01580                }
01581                break;
01582             case AST_FRAME_CONTROL:
01583                switch (f->subclass.integer) {
01584                case AST_CONTROL_HOLD:
01585                   ast_verb(3, "Call on %s placed on hold\n", o->chan->name);
01586                   ast_indicate_data(o->chan, AST_CONTROL_HOLD, f->data.ptr, f->datalen);
01587                   break;
01588                case AST_CONTROL_UNHOLD:
01589                   ast_verb(3, "Call on %s left from hold\n", o->chan->name);
01590                   ast_indicate(o->chan, AST_CONTROL_UNHOLD);
01591                   break;
01592                case AST_CONTROL_VIDUPDATE:
01593                case AST_CONTROL_SRCUPDATE:
01594                case AST_CONTROL_SRCCHANGE:
01595                   if (!single || caller_entertained) {
01596                      /*
01597                       * We are calling multiple parties or caller is being
01598                       * entertained and has thus not been made compatible.
01599                       * No need to check any other called parties.
01600                       */
01601                      goto skip_frame;
01602                   }
01603                   ast_verb(3, "%s requested media update control %d, passing it to %s\n",
01604                      in->name, f->subclass.integer, o->chan->name);
01605                   ast_indicate(o->chan, f->subclass.integer);
01606                   break;
01607                case AST_CONTROL_CONNECTED_LINE:
01608                   if (ast_channel_connected_line_macro(in, o->chan, f, 0, 1)) {
01609                      ast_indicate_data(o->chan, f->subclass.integer, f->data.ptr, f->datalen);
01610                   }
01611                   break;
01612                case AST_CONTROL_REDIRECTING:
01613                   if (ast_channel_redirecting_macro(in, o->chan, f, 0, 1)) {
01614                      ast_indicate_data(o->chan, f->subclass.integer, f->data.ptr, f->datalen);
01615                   }
01616                   break;
01617                default:
01618                   /* We are not going to do anything with this frame. */
01619                   goto skip_frame;
01620                }
01621                break;
01622             default:
01623                /* We are not going to do anything with this frame. */
01624                goto skip_frame;
01625             }
01626          }
01627 skip_frame:;
01628          ast_frfree(f);
01629       }
01630    }
01631 
01632    if (!*to) {
01633       ast_verb(3, "Nobody picked up in %d ms\n", orig);
01634    }
01635    if (!*to || ast_check_hangup(in)) {
01636       ast_cdr_noanswer(in->cdr);
01637    }
01638 
01639 #ifdef HAVE_EPOLL
01640    for (epollo = outgoing; epollo; epollo = epollo->next) {
01641       if (epollo->chan)
01642          ast_poll_channel_del(in, epollo->chan);
01643    }
01644 #endif
01645 
01646    if (is_cc_recall) {
01647       ast_cc_completed(in, "Recall completed!");
01648    }
01649    return peer;
01650 }
01651 
01652 static int detect_disconnect(struct ast_channel *chan, char code, struct ast_str **featurecode)
01653 {
01654    struct ast_flags features = { AST_FEATURE_DISCONNECT }; /* only concerned with disconnect feature */
01655    struct ast_call_feature feature = { 0, };
01656    int res;
01657 
01658    ast_str_append(featurecode, 1, "%c", code);
01659 
01660    res = ast_feature_detect(chan, &features, ast_str_buffer(*featurecode), &feature);
01661 
01662    if (res != AST_FEATURE_RETURN_STOREDIGITS) {
01663       ast_str_reset(*featurecode);
01664    }
01665    if (feature.feature_mask & AST_FEATURE_DISCONNECT) {
01666       return 1;
01667    }
01668 
01669    return 0;
01670 }
01671 
01672 static void replace_macro_delimiter(char *s)
01673 {
01674    for (; *s; s++)
01675       if (*s == '^')
01676          *s = ',';
01677 }
01678 
01679 /* returns true if there is a valid privacy reply */
01680 static int valid_priv_reply(struct ast_flags64 *opts, int res)
01681 {
01682    if (res < '1')
01683       return 0;
01684    if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
01685       return 1;
01686    if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
01687       return 1;
01688    return 0;
01689 }
01690 
01691 static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
01692    struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
01693 {
01694 
01695    int res2;
01696    int loopcount = 0;
01697 
01698    /* Get the user's intro, store it in priv-callerintros/$CID,
01699       unless it is already there-- this should be done before the
01700       call is actually dialed  */
01701 
01702    /* all ring indications and moh for the caller has been halted as soon as the
01703       target extension was picked up. We are going to have to kill some
01704       time and make the caller believe the peer hasn't picked up yet */
01705 
01706    if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
01707       char *original_moh = ast_strdupa(chan->musicclass);
01708       ast_indicate(chan, -1);
01709       ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
01710       ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
01711       ast_string_field_set(chan, musicclass, original_moh);
01712    } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
01713       ast_indicate(chan, AST_CONTROL_RINGING);
01714       pa->sentringing++;
01715    }
01716 
01717    /* Start autoservice on the other chan ?? */
01718    res2 = ast_autoservice_start(chan);
01719    /* Now Stream the File */
01720    for (loopcount = 0; loopcount < 3; loopcount++) {
01721       if (res2 && loopcount == 0) /* error in ast_autoservice_start() */
01722          break;
01723       if (!res2) /* on timeout, play the message again */
01724          res2 = ast_play_and_wait(peer, "priv-callpending");
01725       if (!valid_priv_reply(opts, res2))
01726          res2 = 0;
01727       /* priv-callpending script:
01728          "I have a caller waiting, who introduces themselves as:"
01729       */
01730       if (!res2)
01731          res2 = ast_play_and_wait(peer, pa->privintro);
01732       if (!valid_priv_reply(opts, res2))
01733          res2 = 0;
01734       /* now get input from the called party, as to their choice */
01735       if (!res2) {
01736          /* XXX can we have both, or they are mutually exclusive ? */
01737          if (ast_test_flag64(opts, OPT_PRIVACY))
01738             res2 = ast_play_and_wait(peer, "priv-callee-options");
01739          if (ast_test_flag64(opts, OPT_SCREENING))
01740             res2 = ast_play_and_wait(peer, "screen-callee-options");
01741       }
01742       /*! \page DialPrivacy Dial Privacy scripts
01743       \par priv-callee-options script:
01744          "Dial 1 if you wish this caller to reach you directly in the future,
01745             and immediately connect to their incoming call
01746           Dial 2 if you wish to send this caller to voicemail now and
01747             forevermore.
01748           Dial 3 to send this caller to the torture menus, now and forevermore.
01749           Dial 4 to send this caller to a simple "go away" menu, now and forevermore.
01750           Dial 5 to allow this caller to come straight thru to you in the future,
01751             but right now, just this once, send them to voicemail."
01752       \par screen-callee-options script:
01753          "Dial 1 if you wish to immediately connect to the incoming call
01754           Dial 2 if you wish to send this caller to voicemail.
01755           Dial 3 to send this caller to the torture menus.
01756           Dial 4 to send this caller to a simple "go away" menu.
01757       */
01758       if (valid_priv_reply(opts, res2))
01759          break;
01760       /* invalid option */
01761       res2 = ast_play_and_wait(peer, "vm-sorry");
01762    }
01763 
01764    if (ast_test_flag64(opts, OPT_MUSICBACK)) {
01765       ast_moh_stop(chan);
01766    } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
01767       ast_indicate(chan, -1);
01768       pa->sentringing = 0;
01769    }
01770    ast_autoservice_stop(chan);
01771    if (ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
01772       /* map keypresses to various things, the index is res2 - '1' */
01773       static const char * const _val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
01774       static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
01775       int i = res2 - '1';
01776       ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
01777          opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
01778       ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
01779    }
01780    switch (res2) {
01781    case '1':
01782       break;
01783    case '2':
01784       ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
01785       break;
01786    case '3':
01787       ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
01788       break;
01789    case '4':
01790       ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
01791       break;
01792    case '5':
01793       /* XXX should we set status to DENY ? */
01794       if (ast_test_flag64(opts, OPT_PRIVACY))
01795          break;
01796       /* if not privacy, then 5 is the same as "default" case */
01797    default: /* bad input or -1 if failure to start autoservice */
01798       /* well, if the user messes up, ... he had his chance... What Is The Best Thing To Do?  */
01799       /* well, there seems basically two choices. Just patch the caller thru immediately,
01800            or,... put 'em thru to voicemail. */
01801       /* since the callee may have hung up, let's do the voicemail thing, no database decision */
01802       ast_log(LOG_NOTICE, "privacy: no valid response from the callee. Sending the caller to voicemail, the callee isn't responding\n");
01803       /* XXX should we set status to DENY ? */
01804       /* XXX what about the privacy flags ? */
01805       break;
01806    }
01807 
01808    if (res2 == '1') { /* the only case where we actually connect */
01809       /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll
01810          just clog things up, and it's not useful information, not being tied to a CID */
01811       if (strncmp(pa->privcid, "NOCALLERID", 10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO)) {
01812          ast_filedelete(pa->privintro, NULL);
01813          if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
01814             ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
01815          else
01816             ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
01817       }
01818       return 0; /* the good exit path */
01819    } else {
01820       ast_hangup(peer); /* hang up on the callee -- he didn't want to talk anyway! */
01821       return -1;
01822    }
01823 }
01824 
01825 /*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
01826 static int setup_privacy_args(struct privacy_args *pa,
01827    struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
01828 {
01829    char callerid[60];
01830    int res;
01831    char *l;
01832    int silencethreshold;
01833 
01834    if (chan->caller.id.number.valid
01835       && !ast_strlen_zero(chan->caller.id.number.str)) {
01836       l = ast_strdupa(chan->caller.id.number.str);
01837       ast_shrink_phone_number(l);
01838       if (ast_test_flag64(opts, OPT_PRIVACY) ) {
01839          ast_verb(3, "Privacy DB is '%s', clid is '%s'\n", opt_args[OPT_ARG_PRIVACY], l);
01840          pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
01841       } else {
01842          ast_verb(3, "Privacy Screening, clid is '%s'\n", l);
01843          pa->privdb_val = AST_PRIVACY_UNKNOWN;
01844       }
01845    } else {
01846       char *tnam, *tn2;
01847 
01848       tnam = ast_strdupa(chan->name);
01849       /* clean the channel name so slashes don't try to end up in disk file name */
01850       for (tn2 = tnam; *tn2; tn2++) {
01851          if (*tn2 == '/')  /* any other chars to be afraid of? */
01852             *tn2 = '=';
01853       }
01854       ast_verb(3, "Privacy-- callerid is empty\n");
01855 
01856       snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
01857       l = callerid;
01858       pa->privdb_val = AST_PRIVACY_UNKNOWN;
01859    }
01860 
01861    ast_copy_string(pa->privcid, l, sizeof(pa->privcid));
01862 
01863    if (strncmp(pa->privcid, "NOCALLERID", 10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCALLERID)) {
01864       /* if callerid is set and OPT_SCREEN_NOCALLERID is set also */
01865       ast_verb(3, "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
01866       pa->privdb_val = AST_PRIVACY_ALLOW;
01867    } else if (ast_test_flag64(opts, OPT_SCREEN_NOCALLERID) && strncmp(pa->privcid, "NOCALLERID", 10) == 0) {
01868       ast_verb(3, "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
01869    }
01870    
01871    if (pa->privdb_val == AST_PRIVACY_DENY) {
01872       ast_verb(3, "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
01873       ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
01874       return 0;
01875    } else if (pa->privdb_val == AST_PRIVACY_KILL) {
01876       ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
01877       return 0; /* Is this right? */
01878    } else if (pa->privdb_val == AST_PRIVACY_TORTURE) {
01879       ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
01880       return 0; /* is this right??? */
01881    } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN) {
01882       /* Get the user's intro, store it in priv-callerintros/$CID,
01883          unless it is already there-- this should be done before the
01884          call is actually dialed  */
01885 
01886       /* make sure the priv-callerintros dir actually exists */
01887       snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
01888       if ((res = ast_mkdir(pa->privintro, 0755))) {
01889          ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
01890          return -1;
01891       }
01892 
01893       snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
01894       if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
01895          /* the DELUX version of this code would allow this caller the
01896             option to hear and retape their previously recorded intro.
01897          */
01898       } else {
01899          int duration; /* for feedback from play_and_wait */
01900          /* the file doesn't exist yet. Let the caller submit his
01901             vocal intro for posterity */
01902          /* priv-recordintro script:
01903 
01904             "At the tone, please say your name:"
01905 
01906          */
01907          silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
01908          ast_answer(chan);
01909          res = ast_play_and_record(chan, "priv-recordintro", pa->privintro, 4, "sln", &duration, NULL, silencethreshold, 2000, 0);  /* NOTE: I've reduced the total time to 4 sec */
01910                            /* don't think we'll need a lock removed, we took care of
01911                               conflicts by naming the pa.privintro file */
01912          if (res == -1) {
01913             /* Delete the file regardless since they hung up during recording */
01914             ast_filedelete(pa->privintro, NULL);
01915             if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
01916                ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
01917             else
01918                ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
01919             return -1;
01920          }
01921          if (!ast_streamfile(chan, "vm-dialout", chan->language) )
01922             ast_waitstream(chan, "");
01923       }
01924    }
01925    return 1; /* success */
01926 }
01927 
01928 static void end_bridge_callback(void *data)
01929 {
01930    char buf[80];
01931    time_t end;
01932    struct ast_channel *chan = data;
01933 
01934    if (!chan->cdr) {
01935       return;
01936    }
01937 
01938    time(&end);
01939 
01940    ast_channel_lock(chan);
01941    if (chan->cdr->answer.tv_sec) {
01942       snprintf(buf, sizeof(buf), "%ld", (long) end - chan->cdr->answer.tv_sec);
01943       pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
01944    }
01945 
01946    if (chan->cdr->start.tv_sec) {
01947       snprintf(buf, sizeof(buf), "%ld", (long) end - chan->cdr->start.tv_sec);
01948       pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
01949    }
01950    ast_channel_unlock(chan);
01951 }
01952 
01953 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator) {
01954    bconfig->end_bridge_callback_data = originator;
01955 }
01956 
01957 static int dial_handle_playtones(struct ast_channel *chan, const char *data)
01958 {
01959    struct ast_tone_zone_sound *ts = NULL;
01960    int res;
01961    const char *str = data;
01962 
01963    if (ast_strlen_zero(str)) {
01964       ast_debug(1,"Nothing to play\n");
01965       return -1;
01966    }
01967 
01968    ts = ast_get_indication_tone(chan->zone, str);
01969 
01970    if (ts && ts->data[0]) {
01971       res = ast_playtones_start(chan, 0, ts->data, 0);
01972    } else {
01973       res = -1;
01974    }
01975 
01976    if (ts) {
01977       ts = ast_tone_zone_sound_unref(ts);
01978    }
01979 
01980    if (res) {
01981       ast_log(LOG_WARNING, "Unable to start playtone \'%s\'\n", str);
01982    }
01983 
01984    return res;
01985 }
01986 
01987 static int dial_exec_full(struct ast_channel *chan, const char *data, struct ast_flags64 *peerflags, int *continue_exec)
01988 {
01989    int res = -1; /* default: error */
01990    char *rest, *cur; /* scan the list of destinations */
01991    struct chanlist *outgoing = NULL; /* list of destinations */
01992    struct ast_channel *peer;
01993    int to; /* timeout */
01994    struct cause_args num = { chan, 0, 0, 0 };
01995    int cause;
01996    char numsubst[256];
01997 
01998    struct ast_bridge_config config = { { 0, } };
01999    struct timeval calldurationlimit = { 0, };
02000    char *dtmfcalled = NULL, *dtmfcalling = NULL, *dtmf_progress=NULL;
02001    struct privacy_args pa = {
02002       .sentringing = 0,
02003       .privdb_val = 0,
02004       .status = "INVALIDARGS",
02005    };
02006    int sentringing = 0, moh = 0;
02007    const char *outbound_group = NULL;
02008    int result = 0;
02009    char *parse;
02010    int opermode = 0;
02011    int delprivintro = 0;
02012    AST_DECLARE_APP_ARGS(args,
02013       AST_APP_ARG(peers);
02014       AST_APP_ARG(timeout);
02015       AST_APP_ARG(options);
02016       AST_APP_ARG(url);
02017    );
02018    struct ast_flags64 opts = { 0, };
02019    char *opt_args[OPT_ARG_ARRAY_SIZE];
02020    struct ast_datastore *datastore = NULL;
02021    int fulldial = 0, num_dialed = 0;
02022    int ignore_cc = 0;
02023    char device_name[AST_CHANNEL_NAME];
02024    char forced_clid_name[AST_MAX_EXTENSION];
02025    char stored_clid_name[AST_MAX_EXTENSION];
02026    int force_forwards_only;   /*!< TRUE if force CallerID on call forward only. Legacy behaviour.*/
02027    /*!
02028     * \brief Forced CallerID party information to send.
02029     * \note This will not have any malloced strings so do not free it.
02030     */
02031    struct ast_party_id forced_clid;
02032    /*!
02033     * \brief Stored CallerID information if needed.
02034     *
02035     * \note If OPT_ORIGINAL_CLID set then this is the o option
02036     * CallerID.  Otherwise it is the dialplan extension and hint
02037     * name.
02038     *
02039     * \note This will not have any malloced strings so do not free it.
02040     */
02041    struct ast_party_id stored_clid;
02042    /*!
02043     * \brief CallerID party information to store.
02044     * \note This will not have any malloced strings so do not free it.
02045     */
02046    struct ast_party_caller caller;
02047 
02048    /* Reset all DIAL variables back to blank, to prevent confusion (in case we don't reset all of them). */
02049    pbx_builtin_setvar_helper(chan, "DIALSTATUS", "");
02050    pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", "");
02051    pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", "");
02052    pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", "");
02053    pbx_builtin_setvar_helper(chan, "DIALEDTIME", "");
02054 
02055    if (ast_strlen_zero(data)) {
02056       ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
02057       pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
02058       return -1;
02059    }
02060 
02061    parse = ast_strdupa(data);
02062 
02063    AST_STANDARD_APP_ARGS(args, parse);
02064 
02065    if (!ast_strlen_zero(args.options) &&
02066       ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
02067       pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
02068       goto done;
02069    }
02070 
02071    if (ast_strlen_zero(args.peers)) {
02072       ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
02073       pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
02074       goto done;
02075    }
02076 
02077    if (ast_cc_call_init(chan, &ignore_cc)) {
02078       goto done;
02079    }
02080 
02081    if (ast_test_flag64(&opts, OPT_SCREEN_NOINTRO) && !ast_strlen_zero(opt_args[OPT_ARG_SCREEN_NOINTRO])) {
02082       delprivintro = atoi(opt_args[OPT_ARG_SCREEN_NOINTRO]);
02083 
02084       if (delprivintro < 0 || delprivintro > 1) {
02085          ast_log(LOG_WARNING, "Unknown argument %d specified to n option, ignoring\n", delprivintro);
02086          delprivintro = 0;
02087       }
02088    }
02089 
02090    if (!ast_test_flag64(&opts, OPT_RINGBACK)) {
02091       opt_args[OPT_ARG_RINGBACK] = NULL;
02092    }
02093 
02094    if (ast_test_flag64(&opts, OPT_OPERMODE)) {
02095       opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
02096       ast_verb(3, "Setting operator services mode to %d.\n", opermode);
02097    }
02098 
02099    if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
02100       calldurationlimit.tv_sec = atoi(opt_args[OPT_ARG_DURATION_STOP]);
02101       if (!calldurationlimit.tv_sec) {
02102          ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
02103          pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
02104          goto done;
02105       }
02106       ast_verb(3, "Setting call duration limit to %.3lf seconds.\n", calldurationlimit.tv_sec + calldurationlimit.tv_usec / 1000000.0);
02107    }
02108 
02109    if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
02110       dtmf_progress = opt_args[OPT_ARG_SENDDTMF];
02111       dtmfcalled = strsep(&dtmf_progress, ":");
02112       dtmfcalling = strsep(&dtmf_progress, ":");
02113    }
02114 
02115    if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
02116       if (ast_bridge_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
02117          goto done;
02118    }
02119 
02120    /* Setup the forced CallerID information to send if used. */
02121    ast_party_id_init(&forced_clid);
02122    force_forwards_only = 0;
02123    if (ast_test_flag64(&opts, OPT_FORCECLID)) {
02124       if (ast_strlen_zero(opt_args[OPT_ARG_FORCECLID])) {
02125          ast_channel_lock(chan);
02126          forced_clid.number.str = ast_strdupa(S_OR(chan->macroexten, chan->exten));
02127          ast_channel_unlock(chan);
02128          forced_clid_name[0] = '\0';
02129          forced_clid.name.str = (char *) get_cid_name(forced_clid_name,
02130             sizeof(forced_clid_name), chan);
02131          force_forwards_only = 1;
02132       } else {
02133          /* Note: The opt_args[OPT_ARG_FORCECLID] string value is altered here. */
02134          ast_callerid_parse(opt_args[OPT_ARG_FORCECLID], &forced_clid.name.str,
02135             &forced_clid.number.str);
02136       }
02137       if (!ast_strlen_zero(forced_clid.name.str)) {
02138          forced_clid.name.valid = 1;
02139       }
02140       if (!ast_strlen_zero(forced_clid.number.str)) {
02141          forced_clid.number.valid = 1;
02142       }
02143    }
02144    if (ast_test_flag64(&opts, OPT_FORCE_CID_TAG)
02145       && !ast_strlen_zero(opt_args[OPT_ARG_FORCE_CID_TAG])) {
02146       forced_clid.tag = opt_args[OPT_ARG_FORCE_CID_TAG];
02147    }
02148    forced_clid.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
02149    if (ast_test_flag64(&opts, OPT_FORCE_CID_PRES)
02150       && !ast_strlen_zero(opt_args[OPT_ARG_FORCE_CID_PRES])) {
02151       int pres;
02152 
02153       pres = ast_parse_caller_presentation(opt_args[OPT_ARG_FORCE_CID_PRES]);
02154       if (0 <= pres) {
02155          forced_clid.number.presentation = pres;
02156       }
02157    }
02158 
02159    /* Setup the stored CallerID information if needed. */
02160    ast_party_id_init(&stored_clid);
02161    if (ast_test_flag64(&opts, OPT_ORIGINAL_CLID)) {
02162       if (ast_strlen_zero(opt_args[OPT_ARG_ORIGINAL_CLID])) {
02163          ast_channel_lock(chan);
02164          ast_party_id_set_init(&stored_clid, &chan->caller.id);
02165          if (!ast_strlen_zero(chan->caller.id.name.str)) {
02166             stored_clid.name.str = ast_strdupa(chan->caller.id.name.str);
02167          }
02168          if (!ast_strlen_zero(chan->caller.id.number.str)) {
02169             stored_clid.number.str = ast_strdupa(chan->caller.id.number.str);
02170          }
02171          if (!ast_strlen_zero(chan->caller.id.subaddress.str)) {
02172             stored_clid.subaddress.str = ast_strdupa(chan->caller.id.subaddress.str);
02173          }
02174          if (!ast_strlen_zero(chan->caller.id.tag)) {
02175             stored_clid.tag = ast_strdupa(chan->caller.id.tag);
02176          }
02177          ast_channel_unlock(chan);
02178       } else {
02179          /* Note: The opt_args[OPT_ARG_ORIGINAL_CLID] string value is altered here. */
02180          ast_callerid_parse(opt_args[OPT_ARG_ORIGINAL_CLID], &stored_clid.name.str,
02181             &stored_clid.number.str);
02182          if (!ast_strlen_zero(stored_clid.name.str)) {
02183             stored_clid.name.valid = 1;
02184          }
02185          if (!ast_strlen_zero(stored_clid.number.str)) {
02186             stored_clid.number.valid = 1;
02187          }
02188       }
02189    } else {
02190       /*
02191        * In case the new channel has no preset CallerID number by the
02192        * channel driver, setup the dialplan extension and hint name.
02193        */
02194       stored_clid_name[0] = '\0';
02195       stored_clid.name.str = (char *) get_cid_name(stored_clid_name,
02196          sizeof(stored_clid_name), chan);
02197       if (ast_strlen_zero(stored_clid.name.str)) {
02198          stored_clid.name.str = NULL;
02199       } else {
02200          stored_clid.name.valid = 1;
02201       }
02202       ast_channel_lock(chan);
02203       stored_clid.number.str = ast_strdupa(S_OR(chan->macroexten, chan->exten));
02204       stored_clid.number.valid = 1;
02205       ast_channel_unlock(chan);
02206    }
02207 
02208    if (ast_test_flag64(&opts, OPT_RESETCDR) && chan->cdr)
02209       ast_cdr_reset(chan->cdr, NULL);
02210    if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
02211       opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
02212 
02213    if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
02214       res = setup_privacy_args(&pa, &opts, opt_args, chan);
02215       if (res <= 0)
02216          goto out;
02217       res = -1; /* reset default */
02218    }
02219 
02220    if (continue_exec)
02221       *continue_exec = 0;
02222 
02223    /* If a channel group has been specified, get it for use when we create peer channels */
02224 
02225    ast_channel_lock(chan);
02226    if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
02227       outbound_group = ast_strdupa(outbound_group);   
02228       pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
02229    } else if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP"))) {
02230       outbound_group = ast_strdupa(outbound_group);
02231    }
02232    ast_channel_unlock(chan);
02233 
02234    /* Set per dial instance flags.  These flags are also passed back to RetryDial. */
02235    ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID
02236       | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING | OPT_CANCEL_TIMEOUT
02237       | OPT_ANNOUNCE | OPT_CALLEE_MACRO | OPT_CALLEE_GOSUB | OPT_FORCECLID);
02238 
02239    /* loop through the list of dial destinations */
02240    rest = args.peers;
02241    while ((cur = strsep(&rest, "&")) ) {
02242       struct chanlist *tmp;
02243       struct ast_channel *tc; /* channel for this destination */
02244       /* Get a technology/[device:]number pair */
02245       char *number = cur;
02246       char *interface = ast_strdupa(number);
02247       char *tech = strsep(&number, "/");
02248       /* find if we already dialed this interface */
02249       struct ast_dialed_interface *di;
02250       AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
02251       num_dialed++;
02252       if (ast_strlen_zero(number)) {
02253          ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
02254          goto out;
02255       }
02256       if (!(tmp = ast_calloc(1, sizeof(*tmp))))
02257          goto out;
02258       if (opts.flags) {
02259          /* Set per outgoing call leg options. */
02260          ast_copy_flags64(tmp, &opts,
02261             OPT_CANCEL_ELSEWHERE |
02262             OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
02263             OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
02264             OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
02265             OPT_CALLEE_PARK | OPT_CALLER_PARK |
02266             OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
02267             OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID | OPT_IGNORE_CONNECTEDLINE);
02268          ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);
02269       }
02270       ast_copy_string(numsubst, number, sizeof(numsubst));
02271       /* Request the peer */
02272 
02273       ast_channel_lock(chan);
02274       datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL);
02275       /*
02276        * Seed the chanlist's connected line information with previously
02277        * acquired connected line info from the incoming channel.  The
02278        * previously acquired connected line info could have been set
02279        * through the CONNECTED_LINE dialplan function.
02280        */
02281       ast_party_connected_line_copy(&tmp->connected, &chan->connected);
02282       ast_channel_unlock(chan);
02283 
02284       if (datastore)
02285          dialed_interfaces = datastore->data;
02286       else {
02287          if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
02288             ast_log(LOG_WARNING, "Unable to create channel datastore for dialed interfaces. Aborting!\n");
02289             chanlist_free(tmp);
02290             goto out;
02291          }
02292 
02293          datastore->inheritance = DATASTORE_INHERIT_FOREVER;
02294 
02295          if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
02296             ast_datastore_free(datastore);
02297             chanlist_free(tmp);
02298             goto out;
02299          }
02300 
02301          datastore->data = dialed_interfaces;
02302          AST_LIST_HEAD_INIT(dialed_interfaces);
02303 
02304          ast_channel_lock(chan);
02305          ast_channel_datastore_add(chan, datastore);
02306          ast_channel_unlock(chan);
02307       }
02308 
02309       AST_LIST_LOCK(dialed_interfaces);
02310       AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
02311          if (!strcasecmp(di->interface, interface)) {
02312             ast_log(LOG_WARNING, "Skipping dialing interface '%s' again since it has already been dialed\n",
02313                di->interface);
02314             break;
02315          }
02316       }
02317       AST_LIST_UNLOCK(dialed_interfaces);
02318 
02319       if (di) {
02320          fulldial++;
02321          chanlist_free(tmp);
02322          continue;
02323       }
02324 
02325       /* It is always ok to dial a Local interface.  We only keep track of
02326        * which "real" interfaces have been dialed.  The Local channel will
02327        * inherit this list so that if it ends up dialing a real interface,
02328        * it won't call one that has already been called. */
02329       if (strcasecmp(tech, "Local")) {
02330          if (!(di = ast_calloc(1, sizeof(*di) + strlen(interface)))) {
02331             AST_LIST_UNLOCK(dialed_interfaces);
02332             chanlist_free(tmp);
02333             goto out;
02334          }
02335          strcpy(di->interface, interface);
02336 
02337          AST_LIST_LOCK(dialed_interfaces);
02338          AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
02339          AST_LIST_UNLOCK(dialed_interfaces);
02340       }
02341 
02342       tc = ast_request(tech, chan->nativeformats, chan, numsubst, &cause);
02343       if (!tc) {
02344          /* If we can't, just go on to the next call */
02345          ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
02346             tech, cause, ast_cause2str(cause));
02347          handle_cause(cause, &num);
02348          if (!rest) /* we are on the last destination */
02349             chan->hangupcause = cause;
02350          chanlist_free(tmp);
02351          if (!ignore_cc && (cause == AST_CAUSE_BUSY || cause == AST_CAUSE_CONGESTION)) {
02352             if (!ast_cc_callback(chan, tech, numsubst, ast_cc_busy_interface)) {
02353                ast_cc_extension_monitor_add_dialstring(chan, interface, "");
02354             }
02355          }
02356          continue;
02357       }
02358       ast_channel_get_device_name(tc, device_name, sizeof(device_name));
02359       if (!ignore_cc) {
02360          ast_cc_extension_monitor_add_dialstring(chan, interface, device_name);
02361       }
02362       pbx_builtin_setvar_helper(tc, "DIALEDPEERNUMBER", numsubst);
02363 
02364       ast_channel_lock(tc);
02365       while (ast_channel_trylock(chan)) {
02366          CHANNEL_DEADLOCK_AVOIDANCE(tc);
02367       }
02368       /* Setup outgoing SDP to match incoming one */
02369       if (!outgoing && !rest && CAN_EARLY_BRIDGE(peerflags, chan, tc)) {
02370          ast_rtp_instance_early_bridge_make_compatible(tc, chan);
02371       }
02372       
02373       /* Inherit specially named variables from parent channel */
02374       ast_channel_inherit_variables(chan, tc);
02375       ast_channel_datastore_inherit(chan, tc);
02376 
02377       tc->appl = "AppDial";
02378       tc->data = "(Outgoing Line)";
02379       memset(&tc->whentohangup, 0, sizeof(tc->whentohangup));
02380 
02381       /* Determine CallerID to store in outgoing channel. */
02382       ast_party_caller_set_init(&caller, &tc->caller);
02383       if (ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
02384          caller.id = stored_clid;
02385          ast_channel_set_caller_event(tc, &caller, NULL);
02386          ast_set_flag64(tmp, DIAL_CALLERID_ABSENT);
02387       } else if (ast_strlen_zero(S_COR(tc->caller.id.number.valid,
02388          tc->caller.id.number.str, NULL))) {
02389          /*
02390           * The new channel has no preset CallerID number by the channel
02391           * driver.  Use the dialplan extension and hint name.
02392           */
02393          caller.id = stored_clid;
02394          if (!caller.id.name.valid
02395             && !ast_strlen_zero(S_COR(chan->connected.id.name.valid,
02396                chan->connected.id.name.str, NULL))) {
02397             /*
02398              * No hint name available.  We have a connected name supplied by
02399              * the dialplan we can use instead.
02400              */
02401             caller.id.name.valid = 1;
02402             caller.id.name = chan->connected.id.name;
02403          }
02404          ast_channel_set_caller_event(tc, &caller, NULL);
02405          ast_set_flag64(tmp, DIAL_CALLERID_ABSENT);
02406       } else if (ast_strlen_zero(S_COR(tc->caller.id.name.valid, tc->caller.id.name.str,
02407          NULL))) {
02408          /* The new channel has no preset CallerID name by the channel driver. */
02409          if (!ast_strlen_zero(S_COR(chan->connected.id.name.valid,
02410             chan->connected.id.name.str, NULL))) {
02411             /*
02412              * We have a connected name supplied by the dialplan we can
02413              * use instead.
02414              */
02415             caller.id.name.valid = 1;
02416             caller.id.name = chan->connected.id.name;
02417             ast_channel_set_caller_event(tc, &caller, NULL);
02418          }
02419       }
02420 
02421       /* Determine CallerID for outgoing channel to send. */
02422       if (ast_test_flag64(peerflags, OPT_FORCECLID) && !force_forwards_only) {
02423          struct ast_party_connected_line connected;
02424 
02425          ast_party_connected_line_set_init(&connected, &tc->connected);
02426          connected.id = forced_clid;
02427          ast_channel_set_connected_line(tc, &connected, NULL);
02428       } else {
02429          ast_connected_line_copy_from_caller(&tc->connected, &chan->caller);
02430       }
02431 
02432       ast_party_redirecting_copy(&tc->redirecting, &chan->redirecting);
02433 
02434       tc->dialed.transit_network_select = chan->dialed.transit_network_select;
02435 
02436       if (!ast_strlen_zero(chan->accountcode)) {
02437          ast_string_field_set(tc, accountcode, chan->accountcode);
02438       }
02439       if (ast_strlen_zero(tc->musicclass))
02440          ast_string_field_set(tc, musicclass, chan->musicclass);
02441 
02442       /* Pass ADSI CPE and transfer capability */
02443       tc->adsicpe = chan->adsicpe;
02444       tc->transfercapability = chan->transfercapability;
02445 
02446       /* If we have an outbound group, set this peer channel to it */
02447       if (outbound_group)
02448          ast_app_group_set_channel(tc, outbound_group);
02449       /* If the calling channel has the ANSWERED_ELSEWHERE flag set, inherit it. This is to support local channels */
02450       if (ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE))
02451          ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
02452 
02453       /* Check if we're forced by configuration */
02454       if (ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE))
02455           ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
02456 
02457 
02458       /* Inherit context and extension */
02459       ast_string_field_set(tc, dialcontext, ast_strlen_zero(chan->macrocontext) ? chan->context : chan->macrocontext);
02460       if (!ast_strlen_zero(chan->macroexten))
02461          ast_copy_string(tc->exten, chan->macroexten, sizeof(tc->exten));
02462       else
02463          ast_copy_string(tc->exten, chan->exten, sizeof(tc->exten));
02464 
02465       ast_channel_unlock(tc);
02466       ast_channel_unlock(chan);
02467       res = ast_call(tc, numsubst, 0); /* Place the call, but don't wait on the answer */
02468       ast_channel_lock(chan);
02469 
02470       /* Save the info in cdr's that we called them */
02471       if (chan->cdr)
02472          ast_cdr_setdestchan(chan->cdr, tc->name);
02473 
02474       /* check the results of ast_call */
02475       if (res) {
02476          /* Again, keep going even if there's an error */
02477          ast_debug(1, "ast call on peer returned %d\n", res);
02478          ast_verb(3, "Couldn't call %s/%s\n", tech, numsubst);
02479          if (tc->hangupcause) {
02480             chan->hangupcause = tc->hangupcause;
02481          }
02482          ast_channel_unlock(chan);
02483          ast_cc_call_failed(chan, tc, interface);
02484          ast_hangup(tc);
02485          tc = NULL;
02486          chanlist_free(tmp);
02487          continue;
02488       } else {
02489          senddialevent(chan, tc, numsubst);
02490          ast_verb(3, "Called %s/%s\n", tech, numsubst);
02491          ast_channel_unlock(chan);
02492       }
02493       /* Put them in the list of outgoing thingies...  We're ready now.
02494          XXX If we're forcibly removed, these outgoing calls won't get
02495          hung up XXX */
02496       ast_set_flag64(tmp, DIAL_STILLGOING);
02497       tmp->chan = tc;
02498       tmp->next = outgoing;
02499       outgoing = tmp;
02500       /* If this line is up, don't try anybody else */
02501       if (outgoing->chan->_state == AST_STATE_UP)
02502          break;
02503    }
02504    
02505    if (ast_strlen_zero(args.timeout)) {
02506       to = -1;
02507    } else {
02508       to = atoi(args.timeout);
02509       if (to > 0)
02510          to *= 1000;
02511       else {
02512          ast_log(LOG_WARNING, "Invalid timeout specified: '%s'. Setting timeout to infinite\n", args.timeout);
02513          to = -1;
02514       }
02515    }
02516 
02517    if (!outgoing) {
02518       strcpy(pa.status, "CHANUNAVAIL");
02519       if (fulldial == num_dialed) {
02520          res = -1;
02521          goto out;
02522       }
02523    } else {
02524       /* Our status will at least be NOANSWER */
02525       strcpy(pa.status, "NOANSWER");
02526       if (ast_test_flag64(outgoing, OPT_MUSICBACK)) {
02527          moh = 1;
02528          if (!ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
02529             char *original_moh = ast_strdupa(chan->musicclass);
02530             ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
02531             ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
02532             ast_string_field_set(chan, musicclass, original_moh);
02533          } else {
02534             ast_moh_start(chan, NULL, NULL);
02535          }
02536          ast_indicate(chan, AST_CONTROL_PROGRESS);
02537       } else if (ast_test_flag64(outgoing, OPT_RINGBACK)) {
02538          if (!ast_strlen_zero(opt_args[OPT_ARG_RINGBACK])) {
02539             if (dial_handle_playtones(chan, opt_args[OPT_ARG_RINGBACK])){
02540                ast_indicate(chan, AST_CONTROL_RINGING);
02541                sentringing++;
02542             } else {
02543                ast_indicate(chan, AST_CONTROL_PROGRESS);
02544             }
02545          } else {
02546             ast_indicate(chan, AST_CONTROL_RINGING);
02547             sentringing++;
02548          }
02549       }
02550    }
02551 
02552    peer = wait_for_answer(chan, outgoing, &to, peerflags, opt_args, &pa, &num, &result,
02553       dtmf_progress, ignore_cc, &forced_clid, &stored_clid);
02554 
02555    /* The ast_channel_datastore_remove() function could fail here if the
02556     * datastore was moved to another channel during a masquerade. If this is
02557     * the case, don't free the datastore here because later, when the channel
02558     * to which the datastore was moved hangs up, it will attempt to free this
02559     * datastore again, causing a crash
02560     */
02561    ast_channel_lock(chan);
02562    datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL); /* make sure we weren't cleaned up already */
02563    if (datastore && !ast_channel_datastore_remove(chan, datastore)) {
02564       ast_datastore_free(datastore);
02565    }
02566    ast_channel_unlock(chan);
02567    if (!peer) {
02568       if (result) {
02569          res = result;
02570       } else if (to) { /* Musta gotten hung up */
02571          res = -1;
02572       } else { /* Nobody answered, next please? */
02573          res = 0;
02574       }
02575    } else {
02576       const char *number;
02577 
02578       if (ast_test_flag64(&opts, OPT_CALLER_ANSWER))
02579          ast_answer(chan);
02580 
02581       strcpy(pa.status, "ANSWER");
02582       pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
02583       /* Ah ha!  Someone answered within the desired timeframe.  Of course after this
02584          we will always return with -1 so that it is hung up properly after the
02585          conversation.  */
02586       hanguptree(outgoing, peer, 1);
02587       outgoing = NULL;
02588       /* If appropriate, log that we have a destination channel and set the answer time */
02589       if (chan->cdr) {
02590          ast_cdr_setdestchan(chan->cdr, peer->name);
02591          ast_cdr_setanswer(chan->cdr, peer->cdr->answer);
02592       }
02593       if (peer->name)
02594          pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", peer->name);
02595       
02596       ast_channel_lock(peer);
02597       number = pbx_builtin_getvar_helper(peer, "DIALEDPEERNUMBER"); 
02598       if (!number)
02599          number = numsubst;
02600       pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
02601       ast_channel_unlock(peer);
02602 
02603       if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
02604          ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
02605          ast_channel_sendurl( peer, args.url );
02606       }
02607       if ( (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
02608          if (do_privacy(chan, peer, &opts, opt_args, &pa)) {
02609             res = 0;
02610             goto out;
02611          }
02612       }
02613       if (!ast_test_flag64(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
02614          res = 0;
02615       } else {
02616          int digit = 0;
02617          struct ast_channel *chans[2];
02618          struct ast_channel *active_chan;
02619 
02620          chans[0] = chan;
02621          chans[1] = peer;
02622 
02623          /* we need to stream the announcment while monitoring the caller for a hangup */
02624 
02625          /* stream the file */
02626          res = ast_streamfile(peer, opt_args[OPT_ARG_ANNOUNCE], peer->language);
02627          if (res) {
02628             res = 0;
02629             ast_log(LOG_ERROR, "error streaming file '%s' to callee\n", opt_args[OPT_ARG_ANNOUNCE]);
02630          }
02631 
02632          ast_set_flag(peer, AST_FLAG_END_DTMF_ONLY);
02633          while (peer->stream) {
02634             int ms;
02635 
02636             ms = ast_sched_wait(peer->sched);
02637 
02638             if (ms < 0 && !peer->timingfunc) {
02639                ast_stopstream(peer);
02640                break;
02641             }
02642             if (ms < 0)
02643                ms = 1000;
02644 
02645             active_chan = ast_waitfor_n(chans, 2, &ms);
02646             if (active_chan) {
02647                struct ast_frame *fr = ast_read(active_chan);
02648                if (!fr) {
02649                   ast_hangup(peer);
02650                   res = -1;
02651                   goto done;
02652                }
02653                switch(fr->frametype) {
02654                   case AST_FRAME_DTMF_END:
02655                      digit = fr->subclass.integer;
02656                      if (active_chan == peer && strchr(AST_DIGIT_ANY, res)) {
02657                         ast_stopstream(peer);
02658                         res = ast_senddigit(chan, digit, 0);
02659                      }
02660                      break;
02661                   case AST_FRAME_CONTROL:
02662                      switch (fr->subclass.integer) {
02663                         case AST_CONTROL_HANGUP:
02664                            ast_frfree(fr);
02665                            ast_hangup(peer);
02666                            res = -1;
02667                            goto done;
02668                         default:
02669                            break;
02670                      }
02671                      break;
02672                   default:
02673                      /* Ignore all others */
02674                      break;
02675                }
02676                ast_frfree(fr);
02677             }
02678             ast_sched_runq(peer->sched);
02679          }
02680          ast_clear_flag(peer, AST_FLAG_END_DTMF_ONLY);
02681       }
02682 
02683       if (chan && peer && ast_test_flag64(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
02684          /* chan and peer are going into the PBX, they both
02685           * should probably get CDR records. */
02686          ast_clear_flag(chan->cdr, AST_CDR_FLAG_DIALED);
02687          ast_clear_flag(peer->cdr, AST_CDR_FLAG_DIALED);
02688 
02689          replace_macro_delimiter(opt_args[OPT_ARG_GOTO]);
02690          ast_parseable_goto(chan, opt_args[OPT_ARG_GOTO]);
02691          /* peer goes to the same context and extension as chan, so just copy info from chan*/
02692          ast_copy_string(peer->context, chan->context, sizeof(peer->context));
02693          ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
02694          peer->priority = chan->priority + 2;
02695          if (ast_pbx_start(peer)) {
02696             ast_hangup(peer);
02697          }
02698          hanguptree(outgoing, NULL, ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0);
02699          if (continue_exec)
02700             *continue_exec = 1;
02701          res = 0;
02702          goto done;
02703       }
02704 
02705       if (ast_test_flag64(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
02706          struct ast_app *theapp;
02707          const char *macro_result;
02708 
02709          res = ast_autoservice_start(chan);
02710          if (res) {
02711             ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
02712             res = -1;
02713          }
02714 
02715          theapp = pbx_findapp("Macro");
02716 
02717          if (theapp && !res) { /* XXX why check res here ? */
02718             /* Set peer->exten and peer->context so that MACRO_EXTEN and MACRO_CONTEXT get set */
02719             ast_copy_string(peer->context, chan->context, sizeof(peer->context));
02720             ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
02721 
02722             replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
02723             res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
02724             ast_debug(1, "Macro exited with status %d\n", res);
02725             res = 0;
02726          } else {
02727             ast_log(LOG_ERROR, "Could not find application Macro\n");
02728             res = -1;
02729          }
02730 
02731          if (ast_autoservice_stop(chan) < 0) {
02732             res = -1;
02733          }
02734 
02735          ast_channel_lock(peer);
02736 
02737          if (!res && (macro_result = pbx_builtin_getvar_helper(peer, "MACRO_RESULT"))) {
02738             char *macro_transfer_dest;
02739 
02740             if (!strcasecmp(macro_result, "BUSY")) {
02741                ast_copy_string(pa.status, macro_result, sizeof(pa.status));
02742                ast_set_flag64(peerflags, OPT_GO_ON);
02743                res = -1;
02744             } else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
02745                ast_copy_string(pa.status, macro_result, sizeof(pa.status));
02746                ast_set_flag64(peerflags, OPT_GO_ON);
02747                res = -1;
02748             } else if (!strcasecmp(macro_result, "CONTINUE")) {
02749                /* hangup peer and keep chan alive assuming the macro has changed
02750                   the context / exten / priority or perhaps
02751                   the next priority in the current exten is desired.
02752                */
02753                ast_set_flag64(peerflags, OPT_GO_ON);
02754                res = -1;
02755             } else if (!strcasecmp(macro_result, "ABORT")) {
02756                /* Hangup both ends unless the caller has the g flag */
02757                res = -1;
02758             } else if (!strncasecmp(macro_result, "GOTO:", 5) && (macro_transfer_dest = ast_strdupa(macro_result + 5))) {
02759                res = -1;
02760                /* perform a transfer to a new extension */
02761                if (strchr(macro_transfer_dest, '^')) { /* context^exten^priority*/
02762                   replace_macro_delimiter(macro_transfer_dest);
02763                }
02764                if (!ast_parseable_goto(chan, macro_transfer_dest)) {
02765                   ast_set_flag64(peerflags, OPT_GO_ON);
02766                }
02767             }
02768          }
02769 
02770          ast_channel_unlock(peer);
02771       }
02772 
02773       if (ast_test_flag64(&opts, OPT_CALLEE_GOSUB) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GOSUB])) {
02774          struct ast_app *theapp;
02775          const char *gosub_result;
02776          char *gosub_args, *gosub_argstart;
02777          int res9 = -1;
02778 
02779          res9 = ast_autoservice_start(chan);
02780          if (res9) {
02781             ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
02782             res9 = -1;
02783          }
02784 
02785          theapp = pbx_findapp("Gosub");
02786 
02787          if (theapp && !res9) {
02788             replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GOSUB]);
02789 
02790             /* Set where we came from */
02791             ast_copy_string(peer->context, "app_dial_gosub_virtual_context", sizeof(peer->context));
02792             ast_copy_string(peer->exten, "s", sizeof(peer->exten));
02793             peer->priority = 0;
02794 
02795             gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], ',');
02796             if (gosub_argstart) {
02797                const char *what_is_s = "s";
02798                *gosub_argstart = 0;
02799                if (!ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "s", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL)) &&
02800                    ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "~~s~~", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL))) {
02801                   what_is_s = "~~s~~";
02802                }
02803                if (ast_asprintf(&gosub_args, "%s,%s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s, gosub_argstart + 1) < 0) {
02804                   gosub_args = NULL;
02805                }
02806                *gosub_argstart = ',';
02807             } else {
02808                const char *what_is_s = "s";
02809                if (!ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "s", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL)) &&
02810                    ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "~~s~~", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL))) {
02811                   what_is_s = "~~s~~";
02812                }
02813                if (ast_asprintf(&gosub_args, "%s,%s,1", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s) < 0) {
02814                   gosub_args = NULL;
02815                }
02816             }
02817 
02818             if (gosub_args) {
02819                res9 = pbx_exec(peer, theapp, gosub_args);
02820                if (!res9) {
02821                   struct ast_pbx_args args;
02822                   /* A struct initializer fails to compile for this case ... */
02823                   memset(&args, 0, sizeof(args));
02824                   args.no_hangup_chan = 1;
02825                   ast_pbx_run_args(peer, &args);
02826                }
02827                ast_free(gosub_args);
02828                ast_debug(1, "Gosub exited with status %d\n", res9);
02829             } else {
02830                ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
02831             }
02832 
02833          } else if (!res9) {
02834             ast_log(LOG_ERROR, "Could not find application Gosub\n");
02835             res9 = -1;
02836          }
02837 
02838          if (ast_autoservice_stop(chan) < 0) {
02839             res9 = -1;
02840          }
02841          
02842          ast_channel_lock(peer);
02843 
02844          if (!res9 && (gosub_result = pbx_builtin_getvar_helper(peer, "GOSUB_RESULT"))) {
02845             char *gosub_transfer_dest;
02846             const char *gosub_retval = pbx_builtin_getvar_helper(peer, "GOSUB_RETVAL");
02847 
02848             /* Inherit return value from the peer, so it can be used in the master */
02849             if (gosub_retval) {
02850                pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", gosub_retval);
02851             }
02852 
02853             if (!strcasecmp(gosub_result, "BUSY")) {
02854                ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
02855                ast_set_flag64(peerflags, OPT_GO_ON);
02856                res = -1;
02857             } else if (!strcasecmp(gosub_result, "CONGESTION") || !strcasecmp(gosub_result, "CHANUNAVAIL")) {
02858                ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
02859                ast_set_flag64(peerflags, OPT_GO_ON);
02860                res = -1;
02861             } else if (!strcasecmp(gosub_result, "CONTINUE")) {
02862                /* hangup peer and keep chan alive assuming the macro has changed
02863                   the context / exten / priority or perhaps
02864                   the next priority in the current exten is desired.
02865                */
02866                ast_set_flag64(peerflags, OPT_GO_ON);
02867                res = -1;
02868             } else if (!strcasecmp(gosub_result, "ABORT")) {
02869                /* Hangup both ends unless the caller has the g flag */
02870                res = -1;
02871             } else if (!strncasecmp(gosub_result, "GOTO:", 5) && (gosub_transfer_dest = ast_strdupa(gosub_result + 5))) {
02872                res = -1;
02873                /* perform a transfer to a new extension */
02874                if (strchr(gosub_transfer_dest, '^')) { /* context^exten^priority*/
02875                   replace_macro_delimiter(gosub_transfer_dest);
02876                }
02877                if (!ast_parseable_goto(chan, gosub_transfer_dest)) {
02878                   ast_set_flag64(peerflags, OPT_GO_ON);
02879                }
02880             }
02881          }
02882 
02883          ast_channel_unlock(peer);  
02884       }
02885 
02886       if (!res) {
02887          if (!ast_tvzero(calldurationlimit)) {
02888             struct timeval whentohangup = calldurationlimit;
02889             peer->whentohangup = ast_tvadd(ast_tvnow(), whentohangup);
02890          }
02891          if (!ast_strlen_zero(dtmfcalled)) {
02892             ast_verb(3, "Sending DTMF '%s' to the called party.\n", dtmfcalled);
02893             res = ast_dtmf_stream(peer, chan, dtmfcalled, 250, 0);
02894          }
02895          if (!ast_strlen_zero(dtmfcalling)) {
02896             ast_verb(3, "Sending DTMF '%s' to the calling party.\n", dtmfcalling);
02897             res = ast_dtmf_stream(chan, peer, dtmfcalling, 250, 0);
02898          }
02899       }
02900 
02901       if (res) { /* some error */
02902          res = -1;
02903       } else {
02904          if (ast_test_flag64(peerflags, OPT_CALLEE_TRANSFER))
02905             ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
02906          if (ast_test_flag64(peerflags, OPT_CALLER_TRANSFER))
02907             ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
02908          if (ast_test_flag64(peerflags, OPT_CALLEE_HANGUP))
02909             ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
02910          if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP))
02911             ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
02912          if (ast_test_flag64(peerflags, OPT_CALLEE_MONITOR))
02913             ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
02914          if (ast_test_flag64(peerflags, OPT_CALLER_MONITOR))
02915             ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
02916          if (ast_test_flag64(peerflags, OPT_CALLEE_PARK))
02917             ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
02918          if (ast_test_flag64(peerflags, OPT_CALLER_PARK))
02919             ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
02920          if (ast_test_flag64(peerflags, OPT_CALLEE_MIXMONITOR))
02921             ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMIXMON);
02922          if (ast_test_flag64(peerflags, OPT_CALLER_MIXMONITOR))
02923             ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMIXMON);
02924          if (ast_test_flag64(peerflags, OPT_GO_ON))
02925             ast_set_flag(&(config.features_caller), AST_FEATURE_NO_H_EXTEN);
02926 
02927          config.end_bridge_callback = end_bridge_callback;
02928          config.end_bridge_callback_data = chan;
02929          config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
02930          
02931          if (moh) {
02932             moh = 0;
02933             ast_moh_stop(chan);
02934          } else if (sentringing) {
02935             sentringing = 0;
02936             ast_indicate(chan, -1);
02937          }
02938          /* Be sure no generators are left on it and reset the visible indication */
02939          ast_deactivate_generator(chan);
02940          chan->visible_indication = 0;
02941          /* Make sure channels are compatible */
02942          res = ast_channel_make_compatible(chan, peer);
02943          if (res < 0) {
02944             ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
02945             ast_hangup(peer);
02946             res = -1;
02947             goto done;
02948          }
02949          if (opermode) {
02950             struct oprmode oprmode;
02951 
02952             oprmode.peer = peer;
02953             oprmode.mode = opermode;
02954 
02955             ast_channel_setoption(chan, AST_OPTION_OPRMODE, &oprmode, sizeof(oprmode), 0);
02956          }
02957          res = ast_bridge_call(chan, peer, &config);
02958       }
02959 
02960       strcpy(peer->context, chan->context);
02961 
02962       if (ast_test_flag64(&opts, OPT_PEER_H)
02963          && ast_exists_extension(peer, peer->context, "h", 1,
02964             S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL))) {
02965          int autoloopflag;
02966          int found;
02967          int res9;
02968          
02969          strcpy(peer->exten, "h");
02970          peer->priority = 1;
02971          autoloopflag = ast_test_flag(peer, AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
02972          ast_set_flag(peer, AST_FLAG_IN_AUTOLOOP);
02973 
02974          while ((res9 = ast_spawn_extension(peer, peer->context, peer->exten,
02975             peer->priority,
02976             S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL),
02977             &found, 1)) == 0) {
02978             peer->priority++;
02979          }
02980 
02981          if (found && res9) {
02982             /* Something bad happened, or a hangup has been requested. */
02983             ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
02984             ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
02985          }
02986          ast_set2_flag(peer, autoloopflag, AST_FLAG_IN_AUTOLOOP);  /* set it back the way it was */
02987       }
02988       if (!ast_check_hangup(peer)) {
02989          if (ast_test_flag64(&opts, OPT_CALLEE_GO_ON)) {
02990             int goto_res;
02991 
02992             if (!ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GO_ON])) {
02993                replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GO_ON]);
02994                goto_res = ast_parseable_goto(peer, opt_args[OPT_ARG_CALLEE_GO_ON]);
02995             } else { /* F() */
02996                goto_res = ast_goto_if_exists(peer, chan->context,
02997                   chan->exten, chan->priority + 1);
02998             }
02999             if (!goto_res && !ast_pbx_start(peer)) {
03000                /* The peer is now running its own PBX. */
03001                goto out;
03002             }
03003          }
03004       } else if (!ast_check_hangup(chan)) {
03005          chan->hangupcause = peer->hangupcause;
03006       }
03007       ast_hangup(peer);
03008    }
03009 out:
03010    if (moh) {
03011       moh = 0;
03012       ast_moh_stop(chan);
03013    } else if (sentringing) {
03014       sentringing = 0;
03015       ast_indicate(chan, -1);
03016    }
03017 
03018    if (delprivintro && ast_fileexists(pa.privintro, NULL, NULL) > 0) {
03019       ast_filedelete(pa.privintro, NULL);
03020       if (ast_fileexists(pa.privintro, NULL, NULL) > 0) {
03021          ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa.privintro);
03022       } else {
03023          ast_verb(3, "Successfully deleted %s intro file\n", pa.privintro);
03024       }
03025    }
03026 
03027    ast_channel_early_bridge(chan, NULL);
03028    /* When dialing local channels, the hangupcause of the parent channel
03029     * tells us whether the call was answered elsewhere. */
03030    hanguptree(outgoing, NULL, chan->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE ? 1 : 0);
03031    pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
03032    senddialendevent(chan, pa.status);
03033    ast_debug(1, "Exiting with DIALSTATUS=%s (HANGUPCAUSE=%d).\n", pa.status, chan->hangupcause);
03034    
03035    if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_INCOMPLETE)) {
03036       if (!ast_tvzero(calldurationlimit))
03037          memset(&chan->whentohangup, 0, sizeof(chan->whentohangup));
03038       res = 0;
03039    }
03040 
03041 done:
03042    if (config.warning_sound) {
03043       ast_free((char *)config.warning_sound);
03044    }
03045    if (config.end_sound) {
03046       ast_free((char *)config.end_sound);
03047    }
03048    if (config.start_sound) {
03049       ast_free((char *)config.start_sound);
03050    }
03051    ast_ignore_cc(chan);
03052    return res;
03053 }
03054 
03055 static int dial_exec(struct ast_channel *chan, const char *data)
03056 {
03057    struct ast_flags64 peerflags;
03058 
03059    memset(&peerflags, 0, sizeof(peerflags));
03060 
03061    return dial_exec_full(chan, data, &peerflags, NULL);
03062 }
03063 
03064 static int retrydial_exec(struct ast_channel *chan, const char *data)
03065 {
03066    char *parse;
03067    const char *context = NULL;
03068    int sleepms = 0, loops = 0, res = -1;
03069    struct ast_flags64 peerflags = { 0, };
03070    AST_DECLARE_APP_ARGS(args,
03071       AST_APP_ARG(announce);
03072       AST_APP_ARG(sleep);
03073       AST_APP_ARG(retries);
03074       AST_APP_ARG(dialdata);
03075    );
03076 
03077    if (ast_strlen_zero(data)) {
03078       ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
03079       return -1;
03080    }
03081 
03082    parse = ast_strdupa(data);
03083    AST_STANDARD_APP_ARGS(args, parse);
03084 
03085    if (!ast_strlen_zero(args.sleep) && (sleepms = atoi(args.sleep)))
03086       sleepms *= 1000;
03087 
03088    if (!ast_strlen_zero(args.retries)) {
03089       loops = atoi(args.retries);
03090    }
03091 
03092    if (!args.dialdata) {
03093       ast_log(LOG_ERROR, "%s requires a 4th argument (dialdata)\n", rapp);
03094       goto done;
03095    }
03096 
03097    if (sleepms < 1000)
03098       sleepms = 10000;
03099 
03100    if (!loops)
03101       loops = -1; /* run forever */
03102 
03103    ast_channel_lock(chan);
03104    context = pbx_builtin_getvar_helper(chan, "EXITCONTEXT");
03105    context = !ast_strlen_zero(context) ? ast_strdupa(context) : NULL;
03106    ast_channel_unlock(chan);
03107 
03108    res = 0;
03109    while (loops) {
03110       int continue_exec;
03111 
03112       chan->data = "Retrying";
03113       if (ast_test_flag(chan, AST_FLAG_MOH))
03114          ast_moh_stop(chan);
03115 
03116       res = dial_exec_full(chan, args.dialdata, &peerflags, &continue_exec);
03117       if (continue_exec)
03118          break;
03119 
03120       if (res == 0) {
03121          if (ast_test_flag64(&peerflags, OPT_DTMF_EXIT)) {
03122             if (!ast_strlen_zero(args.announce)) {
03123                if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
03124                   if (!(res = ast_streamfile(chan, args.announce, chan->language)))
03125                      ast_waitstream(chan, AST_DIGIT_ANY);
03126                } else
03127                   ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
03128             }
03129             if (!res && sleepms) {
03130                if (!ast_test_flag(chan, AST_FLAG_MOH))
03131                   ast_moh_start(chan, NULL, NULL);
03132                res = ast_waitfordigit(chan, sleepms);
03133             }
03134          } else {
03135             if (!ast_strlen_zero(args.announce)) {
03136                if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
03137                   if (!(res = ast_streamfile(chan, args.announce, chan->language)))
03138                      res = ast_waitstream(chan, "");
03139                } else
03140                   ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
03141             }
03142             if (sleepms) {
03143                if (!ast_test_flag(chan, AST_FLAG_MOH))
03144                   ast_moh_start(chan, NULL, NULL);
03145                if (!res)
03146                   res = ast_waitfordigit(chan, sleepms);
03147             }
03148          }
03149       }
03150 
03151       if (res < 0 || res == AST_PBX_INCOMPLETE) {
03152          break;
03153       } else if (res > 0) { /* Trying to send the call elsewhere (1 digit ext) */
03154          if (onedigit_goto(chan, context, (char) res, 1)) {
03155             res = 0;
03156             break;
03157          }
03158       }
03159       loops--;
03160    }
03161    if (loops == 0)
03162       res = 0;
03163    else if (res == 1)
03164       res = 0;
03165 
03166    if (ast_test_flag(chan, AST_FLAG_MOH))
03167       ast_moh_stop(chan);
03168  done:
03169    return res;
03170 }
03171 
03172 static int unload_module(void)
03173 {
03174    int res;
03175    struct ast_context *con;
03176 
03177    res = ast_unregister_application(app);
03178    res |= ast_unregister_application(rapp);
03179 
03180    if ((con = ast_context_find("app_dial_gosub_virtual_context"))) {
03181       ast_context_remove_extension2(con, "s", 1, NULL, 0);
03182       ast_context_destroy(con, "app_dial"); /* leave nothing behind */
03183    }
03184 
03185    return res;
03186 }
03187 
03188 static int load_module(void)
03189 {
03190    int res;
03191    struct ast_context *con;
03192 
03193    con = ast_context_find_or_create(NULL, NULL, "app_dial_gosub_virtual_context", "app_dial");
03194    if (!con)
03195       ast_log(LOG_ERROR, "Dial virtual context 'app_dial_gosub_virtual_context' does not exist and unable to create\n");
03196    else
03197       ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "app_dial");
03198 
03199    res = ast_register_application_xml(app, dial_exec);
03200    res |= ast_register_application_xml(rapp, retrydial_exec);
03201 
03202    return res;
03203 }
03204 
03205 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialing Application");

Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1