Sat Apr 26 2014 22:01:26

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