app_dial.c

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

Generated on Thu Apr 16 06:27:09 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6