asterisk: Audit locking of channel when manipulating flags.
[asterisk/asterisk.git] / apps / app_dial.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2012, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief dial() & retrydial() - Trivial application to dial a channel and send an URL on answer
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32
33 #include "asterisk.h"
34
35 #include <sys/time.h>
36 #include <signal.h>
37 #include <sys/stat.h>
38 #include <netinet/in.h>
39
40 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/module.h"
46 #include "asterisk/translate.h"
47 #include "asterisk/say.h"
48 #include "asterisk/config.h"
49 #include "asterisk/features.h"
50 #include "asterisk/musiconhold.h"
51 #include "asterisk/callerid.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/app.h"
54 #include "asterisk/causes.h"
55 #include "asterisk/rtp_engine.h"
56 #include "asterisk/manager.h"
57 #include "asterisk/privacy.h"
58 #include "asterisk/stringfields.h"
59 #include "asterisk/dsp.h"
60 #include "asterisk/aoc.h"
61 #include "asterisk/ccss.h"
62 #include "asterisk/indications.h"
63 #include "asterisk/framehook.h"
64 #include "asterisk/dial.h"
65 #include "asterisk/stasis_channels.h"
66 #include "asterisk/bridge_after.h"
67 #include "asterisk/features_config.h"
68 #include "asterisk/max_forwards.h"
69 #include "asterisk/stream.h"
70
71 /*** DOCUMENTATION
72         <application name="Dial" language="en_US">
73                 <synopsis>
74                         Attempt to connect to another device or endpoint and bridge the call.
75                 </synopsis>
76                 <syntax>
77                         <parameter name="Technology/Resource" required="true" argsep="&amp;">
78                                 <argument name="Technology/Resource" required="true">
79                                         <para>Specification of the device(s) to dial.  These must be in the format of
80                                         <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
81                                         represents a particular channel driver, and <replaceable>Resource</replaceable>
82                                         represents a resource available to that particular channel driver.</para>
83                                 </argument>
84                                 <argument name="Technology2/Resource2" required="false" multiple="true">
85                                         <para>Optional extra devices to dial in parallel</para>
86                                         <para>If you need more than one enter them as
87                                         Technology2/Resource2&amp;Technology3/Resource3&amp;.....</para>
88                                 </argument>
89                         </parameter>
90                         <parameter name="timeout" required="false">
91                                 <para>Specifies the number of seconds we attempt to dial the specified devices.</para>
92                                 <para>If not specified, this defaults to 136 years.</para>
93                         </parameter>
94                         <parameter name="options" required="false">
95                                 <optionlist>
96                                 <option name="A">
97                                         <argument name="x" required="true">
98                                                 <para>The file to play to the called party</para>
99                                         </argument>
100                                         <para>Play an announcement to the called party, where <replaceable>x</replaceable> is the prompt to be played</para>
101                                 </option>
102                                 <option name="a">
103                                         <para>Immediately answer the calling channel when the called channel answers in
104                                         all cases. Normally, the calling channel is answered when the called channel
105                                         answers, but when options such as <literal>A()</literal> and
106                                         <literal>M()</literal> are used, the calling channel is
107                                         not answered until all actions on the called channel (such as playing an
108                                         announcement) are completed.  This option can be used to answer the calling
109                                         channel before doing anything on the called channel. You will rarely need to use
110                                         this option, the default behavior is adequate in most cases.</para>
111                                 </option>
112                                 <option name="b" argsep="^">
113                                         <para>Before initiating an outgoing call, <literal>Gosub</literal> to the specified
114                                         location using the newly created channel.  The <literal>Gosub</literal> will be
115                                         executed for each destination channel.</para>
116                                         <argument name="context" required="false" />
117                                         <argument name="exten" required="false" />
118                                         <argument name="priority" required="true" hasparams="optional" argsep="^">
119                                                 <argument name="arg1" multiple="true" required="true" />
120                                                 <argument name="argN" />
121                                         </argument>
122                                 </option>
123                                 <option name="B" argsep="^">
124                                         <para>Before initiating the outgoing call(s), <literal>Gosub</literal> to the
125                                         specified location using the current channel.</para>
126                                         <argument name="context" required="false" />
127                                         <argument name="exten" required="false" />
128                                         <argument name="priority" required="true" hasparams="optional" argsep="^">
129                                                 <argument name="arg1" multiple="true" required="true" />
130                                                 <argument name="argN" />
131                                         </argument>
132                                 </option>
133                                 <option name="C">
134                                         <para>Reset the call detail record (CDR) for this call.</para>
135                                 </option>
136                                 <option name="c">
137                                         <para>If the Dial() application cancels this call, always set
138                                         <variable>HANGUPCAUSE</variable> to 'answered elsewhere'</para>
139                                 </option>
140                                 <option name="d">
141                                         <para>Allow the calling user to dial a 1 digit extension while waiting for
142                                         a call to be answered. Exit to that extension if it exists in the
143                                         current context, or the context defined in the <variable>EXITCONTEXT</variable> variable,
144                                         if it exists.</para>
145                                         <note>
146                                                 <para>Many SIP and ISDN phones cannot send DTMF digits until the call is
147                                                 connected.  If you wish to use this option with these phones, you
148                                                 can use the <literal>Answer</literal> application before dialing.</para>
149                                         </note>
150                                 </option>
151                                 <option name="D" argsep=":">
152                                         <argument name="called" />
153                                         <argument name="calling" />
154                                         <argument name="progress" />
155                                         <para>Send the specified DTMF strings <emphasis>after</emphasis> the called
156                                         party has answered, but before the call gets bridged.  The
157                                         <replaceable>called</replaceable> DTMF string is sent to the called party, and the
158                                         <replaceable>calling</replaceable> DTMF string is sent to the calling party.  Both arguments
159                                         can be used alone.  If <replaceable>progress</replaceable> is specified, its DTMF is sent
160                                         to the called party immediately after receiving a <literal>PROGRESS</literal> message.</para>
161                                         <para>See <literal>SendDTMF</literal> for valid digits.</para>
162                                 </option>
163                                 <option name="e">
164                                         <para>Execute the <literal>h</literal> extension for peer after the call ends</para>
165                                 </option>
166                                 <option name="f">
167                                         <argument name="x" required="false" />
168                                         <para>If <replaceable>x</replaceable> is not provided, force the CallerID sent on a call-forward or
169                                         deflection to the dialplan extension of this <literal>Dial()</literal> using a dialplan <literal>hint</literal>.
170                                         For example, some PSTNs do not allow CallerID to be set to anything
171                                         other than the numbers assigned to you.
172                                         If <replaceable>x</replaceable> is provided, force the CallerID sent to <replaceable>x</replaceable>.</para>
173                                 </option>
174                                 <option name="F" argsep="^">
175                                         <argument name="context" required="false" />
176                                         <argument name="exten" required="false" />
177                                         <argument name="priority" required="true" />
178                                         <para>When the caller hangs up, transfer the <emphasis>called</emphasis> party
179                                         to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
180                                         <note>
181                                                 <para>Any channel variables you want the called channel to inherit from the caller channel must be
182                                                 prefixed with one or two underbars ('_').</para>
183                                         </note>
184                                 </option>
185                                 <option name="F">
186                                         <para>When the caller hangs up, transfer the <emphasis>called</emphasis> party to the next priority of the current extension
187                                         and <emphasis>start</emphasis> execution at that location.</para>
188                                         <note>
189                                                 <para>Any channel variables you want the called channel to inherit from the caller channel must be
190                                                 prefixed with one or two underbars ('_').</para>
191                                         </note>
192                                         <note>
193                                                 <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
194                                         </note>
195                                 </option>
196                                 <option name="g">
197                                         <para>Proceed with dialplan execution at the next priority in the current extension if the
198                                         destination channel hangs up.</para>
199                                 </option>
200                                 <option name="G" argsep="^">
201                                         <argument name="context" required="false" />
202                                         <argument name="exten" required="false" />
203                                         <argument name="priority" required="true" />
204                                         <para>If the call is answered, transfer the calling party to
205                                         the specified <replaceable>priority</replaceable> and the called party to the specified
206                                         <replaceable>priority</replaceable> plus one.</para>
207                                         <note>
208                                                 <para>You cannot use any additional action post answer options in conjunction with this option.</para>
209                                         </note>
210                                 </option>
211                                 <option name="h">
212                                         <para>Allow the called party to hang up by sending the DTMF sequence
213                                         defined for disconnect in <filename>features.conf</filename>.</para>
214                                 </option>
215                                 <option name="H">
216                                         <para>Allow the calling party to hang up by sending the DTMF sequence
217                                         defined for disconnect in <filename>features.conf</filename>.</para>
218                                         <note>
219                                                 <para>Many SIP and ISDN phones cannot send DTMF digits until the call is
220                                                 connected.  If you wish to allow DTMF disconnect before the dialed
221                                                 party answers with these phones, you can use the <literal>Answer</literal>
222                                                 application before dialing.</para>
223                                         </note>
224                                 </option>
225                                 <option name="i">
226                                         <para>Asterisk will ignore any forwarding requests it may receive on this dial attempt.</para>
227                                 </option>
228                                 <option name="I">
229                                         <para>Asterisk will ignore any connected line update requests or any redirecting party
230                                         update requests it may receive on this dial attempt.</para>
231                                 </option>
232                                 <option name="k">
233                                         <para>Allow the called party to enable parking of the call by sending
234                                         the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
235                                 </option>
236                                 <option name="K">
237                                         <para>Allow the calling party to enable parking of the call by sending
238                                         the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
239                                 </option>
240                                 <option name="L" argsep=":">
241                                         <argument name="x" required="true">
242                                                 <para>Maximum call time, in milliseconds</para>
243                                         </argument>
244                                         <argument name="y">
245                                                 <para>Warning time, in milliseconds</para>
246                                         </argument>
247                                         <argument name="z">
248                                                 <para>Repeat time, in milliseconds</para>
249                                         </argument>
250                                         <para>Limit the call to <replaceable>x</replaceable> milliseconds. Play a warning when <replaceable>y</replaceable> milliseconds are
251                                         left. Repeat the warning every <replaceable>z</replaceable> milliseconds until time expires.</para>
252                                         <para>This option is affected by the following variables:</para>
253                                         <variablelist>
254                                                 <variable name="LIMIT_PLAYAUDIO_CALLER">
255                                                         <value name="yes" default="true" />
256                                                         <value name="no" />
257                                                         <para>If set, this variable causes Asterisk to play the prompts to the caller.</para>
258                                                 </variable>
259                                                 <variable name="LIMIT_PLAYAUDIO_CALLEE">
260                                                         <value name="yes" />
261                                                         <value name="no" default="true"/>
262                                                         <para>If set, this variable causes Asterisk to play the prompts to the callee.</para>
263                                                 </variable>
264                                                 <variable name="LIMIT_TIMEOUT_FILE">
265                                                         <value name="filename"/>
266                                                         <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the timeout is reached.
267                                                         If not set, the time remaining will be announced.</para>
268                                                 </variable>
269                                                 <variable name="LIMIT_CONNECT_FILE">
270                                                         <value name="filename"/>
271                                                         <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the call begins.
272                                                         If not set, the time remaining will be announced.</para>
273                                                 </variable>
274                                                 <variable name="LIMIT_WARNING_FILE">
275                                                         <value name="filename"/>
276                                                         <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play as
277                                                         a warning when time <replaceable>x</replaceable> is reached. If not set, the time remaining will be announced.</para>
278                                                 </variable>
279                                         </variablelist>
280                                 </option>
281                                 <option name="m">
282                                         <argument name="class" required="false"/>
283                                         <para>Provide hold music to the calling party until a requested
284                                         channel answers. A specific music on hold <replaceable>class</replaceable>
285                                         (as defined in <filename>musiconhold.conf</filename>) can be specified.</para>
286                                 </option>
287                                 <option name="M" argsep="^">
288                                         <argument name="macro" required="true">
289                                                 <para>Name of the macro that should be executed.</para>
290                                         </argument>
291                                         <argument name="arg" multiple="true">
292                                                 <para>Macro arguments</para>
293                                         </argument>
294                                         <para>Execute the specified <replaceable>macro</replaceable> for the <emphasis>called</emphasis> channel
295                                         before connecting to the calling channel. Arguments can be specified to the Macro
296                                         using <literal>^</literal> as a delimiter. The macro can set the variable
297                                         <variable>MACRO_RESULT</variable> to specify the following actions after the macro is
298                                         finished executing:</para>
299                                         <variablelist>
300                                                 <variable name="MACRO_RESULT">
301                                                         <para>If set, this action will be taken after the macro finished executing.</para>
302                                                         <value name="ABORT">
303                                                                 Hangup both legs of the call
304                                                         </value>
305                                                         <value name="CONGESTION">
306                                                                 Behave as if line congestion was encountered
307                                                         </value>
308                                                         <value name="BUSY">
309                                                                 Behave as if a busy signal was encountered
310                                                         </value>
311                                                         <value name="CONTINUE">
312                                                                 Hangup the called party and allow the calling party to continue dialplan execution at the next priority
313                                                         </value>
314                                                         <value name="GOTO:[[&lt;context&gt;^]&lt;exten&gt;^]&lt;priority&gt;">
315                                                                 Transfer the call to the specified destination.
316                                                         </value>
317                                                 </variable>
318                                         </variablelist>
319                                         <note>
320                                                 <para>You cannot use any additional action post answer options in conjunction
321                                                 with this option. Also, pbx services are run on the peer (called) channel,
322                                                 so you will not be able to set timeouts via the <literal>TIMEOUT()</literal> function in this macro.</para>
323                                         </note>
324                                         <warning><para>Be aware of the limitations that macros have, specifically with regards to use of
325                                         the <literal>WaitExten</literal> application. For more information, see the documentation for
326                                         <literal>Macro()</literal>.</para></warning>
327                                 </option>
328                                 <option name="n">
329                                         <argument name="delete">
330                                                 <para>With <replaceable>delete</replaceable> either not specified or set to <literal>0</literal>,
331                                                 the recorded introduction will not be deleted if the caller hangs up while the remote party has not
332                                                 yet answered.</para>
333                                                 <para>With <replaceable>delete</replaceable> set to <literal>1</literal>, the introduction will
334                                                 always be deleted.</para>
335                                         </argument>
336                                         <para>This option is a modifier for the call screening/privacy mode. (See the
337                                         <literal>p</literal> and <literal>P</literal> options.) It specifies
338                                         that no introductions are to be saved in the <directory>priv-callerintros</directory>
339                                         directory.</para>
340                                 </option>
341                                 <option name="N">
342                                         <para>This option is a modifier for the call screening/privacy mode. It specifies
343                                         that if CallerID is present, do not screen the call.</para>
344                                 </option>
345                                 <option name="o">
346                                         <argument name="x" required="false" />
347                                         <para>If <replaceable>x</replaceable> is not provided, specify that the CallerID that was present on the
348                                         <emphasis>calling</emphasis> channel be stored as the CallerID on the <emphasis>called</emphasis> channel.
349                                         This was the behavior of Asterisk 1.0 and earlier.
350                                         If <replaceable>x</replaceable> is provided, specify the CallerID stored on the <emphasis>called</emphasis> channel.
351                                         Note that <literal>o(${CALLERID(all)})</literal> is similar to option <literal>o</literal> without the parameter.</para>
352                                 </option>
353                                 <option name="O">
354                                         <argument name="mode">
355                                                 <para>With <replaceable>mode</replaceable> either not specified or set to <literal>1</literal>,
356                                                 the originator hanging up will cause the phone to ring back immediately.</para>
357                                                 <para>With <replaceable>mode</replaceable> set to <literal>2</literal>, when the operator
358                                                 flashes the trunk, it will ring their phone back.</para>
359                                         </argument>
360                                         <para>Enables <emphasis>operator services</emphasis> mode.  This option only
361                                         works when bridging a DAHDI channel to another DAHDI channel
362                                         only. if specified on non-DAHDI interfaces, it will be ignored.
363                                         When the destination answers (presumably an operator services
364                                         station), the originator no longer has control of their line.
365                                         They may hang up, but the switch will not release their line
366                                         until the destination party (the operator) hangs up.</para>
367                                 </option>
368                                 <option name="p">
369                                         <para>This option enables screening mode. This is basically Privacy mode
370                                         without memory.</para>
371                                 </option>
372                                 <option name="P">
373                                         <argument name="x" />
374                                         <para>Enable privacy mode. Use <replaceable>x</replaceable> as the family/key in the AstDB database if
375                                         it is provided. The current extension is used if a database family/key is not specified.</para>
376                                 </option>
377                                 <option name="Q">
378                                         <argument name="cause" required="true"/>
379                                         <para>Specify the Q.850/Q.931 <replaceable>cause</replaceable> to send on
380                                         unanswered channels when another channel answers the call.
381                                         As with <literal>Hangup()</literal>, <replaceable>cause</replaceable>
382                                         can be a numeric cause code or a name such as
383                                                 <literal>NO_ANSWER</literal>,
384                                                 <literal>USER_BUSY</literal>,
385                                                 <literal>CALL_REJECTED</literal> or
386                                                 <literal>ANSWERED_ELSEWHERE</literal> (the default if Q isn't specified).
387                                                 You can also specify <literal>0</literal> or <literal>NONE</literal>
388                                                 to send no cause.  See the <filename>causes.h</filename> file for the
389                                                 full list of valid causes and names.
390                                                 </para>
391                                         <note>
392                                                 <para>chan_sip does not support setting the cause on a CANCEL to anything
393                                                 other than ANSWERED_ELSEWHERE.</para>
394                                         </note>
395                                 </option>
396                                 <option name="r">
397                                         <para>Default: Indicate ringing to the calling party, even if the called party isn't actually ringing. Pass no audio to the calling
398                                         party until the called channel has answered.</para>
399                                         <argument name="tone" required="false">
400                                                 <para>Indicate progress to calling party. Send audio 'tone' from the <filename>indications.conf</filename> tonezone currently in use.</para>
401                                         </argument>
402                                 </option>
403                                 <option name="R">
404                                         <para>Default: Indicate ringing to the calling party, even if the called party isn't actually ringing. 
405                                         Allow interruption of the ringback if early media is received on the channel.</para>
406                                 </option>
407                                 <option name="S">
408                                         <argument name="x" required="true" />
409                                         <para>Hang up the call <replaceable>x</replaceable> seconds <emphasis>after</emphasis> the called party has
410                                         answered the call.</para>
411                                 </option>
412                                 <option name="s">
413                                         <argument name="x" required="true" />
414                                         <para>Force the outgoing CallerID tag parameter to be set to the string <replaceable>x</replaceable>.</para>
415                                         <para>Works with the <literal>f</literal> option.</para>
416                                 </option>
417                                 <option name="t">
418                                         <para>Allow the called party to transfer the calling party by sending the
419                                         DTMF sequence defined in <filename>features.conf</filename>. This setting does not perform policy enforcement on
420                                         transfers initiated by other methods.</para>
421                                 </option>
422                                 <option name="T">
423                                         <para>Allow the calling party to transfer the called party by sending the
424                                         DTMF sequence defined in <filename>features.conf</filename>. This setting does not perform policy enforcement on
425                                         transfers initiated by other methods.</para>
426                                 </option>
427                                 <option name="U" argsep="^">
428                                         <argument name="x" required="true">
429                                                 <para>Name of the subroutine to execute via <literal>Gosub</literal></para>
430                                         </argument>
431                                         <argument name="arg" multiple="true" required="false">
432                                                 <para>Arguments for the <literal>Gosub</literal> routine</para>
433                                         </argument>
434                                         <para>Execute via <literal>Gosub</literal> the routine <replaceable>x</replaceable> for the <emphasis>called</emphasis> channel before connecting
435                                         to the calling channel. Arguments can be specified to the <literal>Gosub</literal>
436                                         using <literal>^</literal> as a delimiter. The <literal>Gosub</literal> routine can set the variable
437                                         <variable>GOSUB_RESULT</variable> to specify the following actions after the <literal>Gosub</literal> returns.</para>
438                                         <variablelist>
439                                                 <variable name="GOSUB_RESULT">
440                                                         <value name="ABORT">
441                                                                 Hangup both legs of the call.
442                                                         </value>
443                                                         <value name="CONGESTION">
444                                                                 Behave as if line congestion was encountered.
445                                                         </value>
446                                                         <value name="BUSY">
447                                                                 Behave as if a busy signal was encountered.
448                                                         </value>
449                                                         <value name="CONTINUE">
450                                                                 Hangup the called party and allow the calling party
451                                                                 to continue dialplan execution at the next priority.
452                                                         </value>
453                                                         <value name="GOTO:[[&lt;context&gt;^]&lt;exten&gt;^]&lt;priority&gt;">
454                                                                 Transfer the call to the specified destination.
455                                                         </value>
456                                                 </variable>
457                                         </variablelist>
458                                         <note>
459                                                 <para>You cannot use any additional action post answer options in conjunction
460                                                 with this option. Also, pbx services are run on the peer (called) channel,
461                                                 so you will not be able to set timeouts via the <literal>TIMEOUT()</literal> function in this routine.</para>
462                                         </note>
463                                 </option>
464                                 <option name="u">
465                                         <argument name = "x" required="true">
466                                                 <para>Force the outgoing callerid presentation indicator parameter to be set
467                                                 to one of the values passed in <replaceable>x</replaceable>:
468                                                 <literal>allowed_not_screened</literal>
469                                                 <literal>allowed_passed_screen</literal>
470                                                 <literal>allowed_failed_screen</literal>
471                                                 <literal>allowed</literal>
472                                                 <literal>prohib_not_screened</literal>
473                                                 <literal>prohib_passed_screen</literal>
474                                                 <literal>prohib_failed_screen</literal>
475                                                 <literal>prohib</literal>
476                                                 <literal>unavailable</literal></para>
477                                         </argument>
478                                         <para>Works with the <literal>f</literal> option.</para>
479                                 </option>
480                                 <option name="w">
481                                         <para>Allow the called party to enable recording of the call by sending
482                                         the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
483                                 </option>
484                                 <option name="W">
485                                         <para>Allow the calling party to enable recording of the call by sending
486                                         the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
487                                 </option>
488                                 <option name="x">
489                                         <para>Allow the called party to enable recording of the call by sending
490                                         the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
491                                 </option>
492                                 <option name="X">
493                                         <para>Allow the calling party to enable recording of the call by sending
494                                         the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
495                                 </option>
496                                 <option name="z">
497                                         <para>On a call forward, cancel any dial timeout which has been set for this call.</para>
498                                 </option>
499                                 </optionlist>
500                         </parameter>
501                         <parameter name="URL">
502                                 <para>The optional URL will be sent to the called party if the channel driver supports it.</para>
503                         </parameter>
504                 </syntax>
505                 <description>
506                         <para>This application will place calls to one or more specified channels. As soon
507                         as one of the requested channels answers, the originating channel will be
508                         answered, if it has not already been answered. These two channels will then
509                         be active in a bridged call. All other channels that were requested will then
510                         be hung up.</para>
511
512                         <para>Unless there is a timeout specified, the Dial application will wait
513                         indefinitely until one of the called channels answers, the user hangs up, or
514                         if all of the called channels are busy or unavailable. Dialplan execution will
515                         continue if no requested channels can be called, or if the timeout expires.
516                         This application will report normal termination if the originating channel
517                         hangs up, or if the call is bridged and either of the parties in the bridge
518                         ends the call.</para>
519                         <para>If the <variable>OUTBOUND_GROUP</variable> variable is set, all peer channels created by this
520                         application will be put into that group (as in <literal>Set(GROUP()=...</literal>).
521                         If the <variable>OUTBOUND_GROUP_ONCE</variable> variable is set, all peer channels created by this
522                         application will be put into that group (as in <literal>Set(GROUP()=...</literal>). Unlike <variable>OUTBOUND_GROUP</variable>,
523                         however, the variable will be unset after use.</para>
524
525                         <example title="Dial with 30 second timeout">
526                          same => n,Dial(PJSIP/alice,30)
527                         </example>
528                         <example title="Parallel dial with 45 second timeout">
529                          same => n,Dial(PJSIP/alice&amp;PJIP/bob,45)
530                         </example>
531                         <example title="Dial with 'g' continuation option">
532                          same => n,Dial(PJSIP/alice,,g)
533                          same => n,Log(NOTICE, Alice call result: ${DIALSTATUS})
534                         </example>
535                         <example title="Dial with transfer/recording features for calling party">
536                          same => n,Dial(PJSIP/alice,,TX)
537                         </example>
538                         <example title="Dial with call length limit">
539                          same => n,Dial(PJSIP/alice,,L(60000:30000:10000))
540                         </example>
541                         <example title="Dial alice and bob and send NO_ANSWER to bob instead of ANSWERED_ELSEWHERE when alice answers">
542                          same => n,Dial(PJSIP/alice&amp;PJSIP/bob,,Q(NO_ANSWER))
543                         </example>
544                         <example title="Dial with pre-dial subroutines">
545                         [default]
546
547                         exten => callee_channel,1,NoOp()
548                          same => n,Log(NOTICE, I'm called on channel ${CHANNEL} prior to it starting the dial attempt)
549                          same => n,Return()
550
551                         exten => called_channel,1,NoOp()
552                          same => n,Log(NOTICE, I'm called on outbound channel ${CHANNEL} prior to it being used to dial someone)
553                          same => n,Return()
554
555                         exten => _X.,1,NoOp()
556                          same => n,Dial(PJSIP/alice,,b(default^called_channel^1)B(default^callee_channel^1))
557                          same => n,Hangup()
558                         </example>
559                         <example title="Dial with post-answer subroutine executed on outbound channel">
560                         [default]
561
562                         exten => called_channel,1,NoOp()
563                          same => n,Playback(hello)
564                          same => n,Return()
565
566                         exten => _X.,1,NoOp()
567                          same => n,Dial(PJSIP/alice,,U(default^called_channel^1))
568                          same => n,Hangup()
569                         </example>
570                         <example title="Dial into ConfBridge using 'G' option">
571                          same => n,Dial(PJSIP/alice,,G(jump_to_here))
572                          same => n(jump_to_here),Goto(confbridge)
573                          same => n,Goto(confbridge)
574                          same => n(confbridge),ConfBridge(${EXTEN})
575                         </example>
576                         <para>This application sets the following channel variables:</para>
577                         <variablelist>
578                                 <variable name="DIALEDTIME">
579                                         <para>This is the time from dialing a channel until when it is disconnected.</para>
580                                 </variable>
581                                 <variable name="ANSWEREDTIME">
582                                         <para>This is the amount of time for actual call.</para>
583                                 </variable>
584                                 <variable name="DIALEDPEERNAME">
585                                         <para>The name of the outbound channel that answered the call.</para>
586                                 </variable>
587                                 <variable name="DIALEDPEERNUMBER">
588                                         <para>The number that was dialed for the answered outbound channel.</para>
589                                 </variable>
590                                 <variable name="FORWARDERNAME">
591                                         <para>If a call forward occurred, the name of the forwarded channel.</para>
592                                 </variable>
593                                 <variable name="DIALSTATUS">
594                                         <para>This is the status of the call</para>
595                                         <value name="CHANUNAVAIL" />
596                                         <value name="CONGESTION" />
597                                         <value name="NOANSWER" />
598                                         <value name="BUSY" />
599                                         <value name="ANSWER" />
600                                         <value name="CANCEL" />
601                                         <value name="DONTCALL">
602                                                 For the Privacy and Screening Modes.
603                                                 Will be set if the called party chooses to send the calling party to the 'Go Away' script.
604                                         </value>
605                                         <value name="TORTURE">
606                                                 For the Privacy and Screening Modes.
607                                                 Will be set if the called party chooses to send the calling party to the 'torture' script.
608                                         </value>
609                                         <value name="INVALIDARGS" />
610                                 </variable>
611                         </variablelist>
612                 </description>
613                 <see-also>
614                         <ref type="application">RetryDial</ref>
615                         <ref type="application">SendDTMF</ref>
616                         <ref type="application">Gosub</ref>
617                         <ref type="application">Macro</ref>
618                 </see-also>
619         </application>
620         <application name="RetryDial" language="en_US">
621                 <synopsis>
622                         Place a call, retrying on failure allowing an optional exit extension.
623                 </synopsis>
624                 <syntax>
625                         <parameter name="announce" required="true">
626                                 <para>Filename of sound that will be played when no channel can be reached</para>
627                         </parameter>
628                         <parameter name="sleep" required="true">
629                                 <para>Number of seconds to wait after a dial attempt failed before a new attempt is made</para>
630                         </parameter>
631                         <parameter name="retries" required="true">
632                                 <para>Number of retries</para>
633                                 <para>When this is reached flow will continue at the next priority in the dialplan</para>
634                         </parameter>
635                         <parameter name="dialargs" required="true">
636                                 <para>Same format as arguments provided to the Dial application</para>
637                         </parameter>
638                 </syntax>
639                 <description>
640                         <para>This application will attempt to place a call using the normal Dial application.
641                         If no channel can be reached, the <replaceable>announce</replaceable> file will be played.
642                         Then, it will wait <replaceable>sleep</replaceable> number of seconds before retrying the call.
643                         After <replaceable>retries</replaceable> number of attempts, the calling channel will continue at the next priority in the dialplan.
644                         If the <replaceable>retries</replaceable> setting is set to 0, this application will retry endlessly.
645                         While waiting to retry a call, a 1 digit extension may be dialed. If that
646                         extension exists in either the context defined in <variable>EXITCONTEXT</variable> or the current
647                         one, The call will jump to that extension immediately.
648                         The <replaceable>dialargs</replaceable> are specified in the same format that arguments are provided
649                         to the Dial application.</para>
650                 </description>
651                 <see-also>
652                         <ref type="application">Dial</ref>
653                 </see-also>
654         </application>
655  ***/
656
657 static const char app[] = "Dial";
658 static const char rapp[] = "RetryDial";
659
660 enum {
661         OPT_ANNOUNCE =          (1 << 0),
662         OPT_RESETCDR =          (1 << 1),
663         OPT_DTMF_EXIT =         (1 << 2),
664         OPT_SENDDTMF =          (1 << 3),
665         OPT_FORCECLID =         (1 << 4),
666         OPT_GO_ON =             (1 << 5),
667         OPT_CALLEE_HANGUP =     (1 << 6),
668         OPT_CALLER_HANGUP =     (1 << 7),
669         OPT_ORIGINAL_CLID =     (1 << 8),
670         OPT_DURATION_LIMIT =    (1 << 9),
671         OPT_MUSICBACK =         (1 << 10),
672         OPT_CALLEE_MACRO =      (1 << 11),
673         OPT_SCREEN_NOINTRO =    (1 << 12),
674         OPT_SCREEN_NOCALLERID = (1 << 13),
675         OPT_IGNORE_CONNECTEDLINE = (1 << 14),
676         OPT_SCREENING =         (1 << 15),
677         OPT_PRIVACY =           (1 << 16),
678         OPT_RINGBACK =          (1 << 17),
679         OPT_DURATION_STOP =     (1 << 18),
680         OPT_CALLEE_TRANSFER =   (1 << 19),
681         OPT_CALLER_TRANSFER =   (1 << 20),
682         OPT_CALLEE_MONITOR =    (1 << 21),
683         OPT_CALLER_MONITOR =    (1 << 22),
684         OPT_GOTO =              (1 << 23),
685         OPT_OPERMODE =          (1 << 24),
686         OPT_CALLEE_PARK =       (1 << 25),
687         OPT_CALLER_PARK =       (1 << 26),
688         OPT_IGNORE_FORWARDING = (1 << 27),
689         OPT_CALLEE_GOSUB =      (1 << 28),
690         OPT_CALLEE_MIXMONITOR = (1 << 29),
691         OPT_CALLER_MIXMONITOR = (1 << 30),
692 };
693
694 /* flags are now 64 bits, so keep it up! */
695 #define DIAL_STILLGOING      (1LLU << 31)
696 #define DIAL_NOFORWARDHTML   (1LLU << 32)
697 #define DIAL_CALLERID_ABSENT (1LLU << 33) /* TRUE if caller id is not available for connected line. */
698 #define OPT_CANCEL_ELSEWHERE (1LLU << 34)
699 #define OPT_PEER_H           (1LLU << 35)
700 #define OPT_CALLEE_GO_ON     (1LLU << 36)
701 #define OPT_CANCEL_TIMEOUT   (1LLU << 37)
702 #define OPT_FORCE_CID_TAG    (1LLU << 38)
703 #define OPT_FORCE_CID_PRES   (1LLU << 39)
704 #define OPT_CALLER_ANSWER    (1LLU << 40)
705 #define OPT_PREDIAL_CALLEE   (1LLU << 41)
706 #define OPT_PREDIAL_CALLER   (1LLU << 42)
707 #define OPT_RING_WITH_EARLY_MEDIA (1LLU << 43)
708 #define OPT_HANGUPCAUSE      (1LLU << 44)
709
710 enum {
711         OPT_ARG_ANNOUNCE = 0,
712         OPT_ARG_SENDDTMF,
713         OPT_ARG_GOTO,
714         OPT_ARG_DURATION_LIMIT,
715         OPT_ARG_MUSICBACK,
716         OPT_ARG_CALLEE_MACRO,
717         OPT_ARG_RINGBACK,
718         OPT_ARG_CALLEE_GOSUB,
719         OPT_ARG_CALLEE_GO_ON,
720         OPT_ARG_PRIVACY,
721         OPT_ARG_DURATION_STOP,
722         OPT_ARG_OPERMODE,
723         OPT_ARG_SCREEN_NOINTRO,
724         OPT_ARG_ORIGINAL_CLID,
725         OPT_ARG_FORCECLID,
726         OPT_ARG_FORCE_CID_TAG,
727         OPT_ARG_FORCE_CID_PRES,
728         OPT_ARG_PREDIAL_CALLEE,
729         OPT_ARG_PREDIAL_CALLER,
730         OPT_ARG_HANGUPCAUSE,
731         /* note: this entry _MUST_ be the last one in the enum */
732         OPT_ARG_ARRAY_SIZE
733 };
734
735 AST_APP_OPTIONS(dial_exec_options, BEGIN_OPTIONS
736         AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
737         AST_APP_OPTION('a', OPT_CALLER_ANSWER),
738         AST_APP_OPTION_ARG('b', OPT_PREDIAL_CALLEE, OPT_ARG_PREDIAL_CALLEE),
739         AST_APP_OPTION_ARG('B', OPT_PREDIAL_CALLER, OPT_ARG_PREDIAL_CALLER),
740         AST_APP_OPTION('C', OPT_RESETCDR),
741         AST_APP_OPTION('c', OPT_CANCEL_ELSEWHERE),
742         AST_APP_OPTION('d', OPT_DTMF_EXIT),
743         AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
744         AST_APP_OPTION('e', OPT_PEER_H),
745         AST_APP_OPTION_ARG('f', OPT_FORCECLID, OPT_ARG_FORCECLID),
746         AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
747         AST_APP_OPTION('g', OPT_GO_ON),
748         AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
749         AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
750         AST_APP_OPTION('H', OPT_CALLER_HANGUP),
751         AST_APP_OPTION('i', OPT_IGNORE_FORWARDING),
752         AST_APP_OPTION('I', OPT_IGNORE_CONNECTEDLINE),
753         AST_APP_OPTION('k', OPT_CALLEE_PARK),
754         AST_APP_OPTION('K', OPT_CALLER_PARK),
755         AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
756         AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
757         AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
758         AST_APP_OPTION_ARG('n', OPT_SCREEN_NOINTRO, OPT_ARG_SCREEN_NOINTRO),
759         AST_APP_OPTION('N', OPT_SCREEN_NOCALLERID),
760         AST_APP_OPTION_ARG('o', OPT_ORIGINAL_CLID, OPT_ARG_ORIGINAL_CLID),
761         AST_APP_OPTION_ARG('O', OPT_OPERMODE, OPT_ARG_OPERMODE),
762         AST_APP_OPTION('p', OPT_SCREENING),
763         AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
764         AST_APP_OPTION_ARG('Q', OPT_HANGUPCAUSE, OPT_ARG_HANGUPCAUSE),
765         AST_APP_OPTION_ARG('r', OPT_RINGBACK, OPT_ARG_RINGBACK),
766         AST_APP_OPTION('R', OPT_RING_WITH_EARLY_MEDIA),
767         AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
768         AST_APP_OPTION_ARG('s', OPT_FORCE_CID_TAG, OPT_ARG_FORCE_CID_TAG),
769         AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
770         AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
771         AST_APP_OPTION_ARG('u', OPT_FORCE_CID_PRES, OPT_ARG_FORCE_CID_PRES),
772         AST_APP_OPTION_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
773         AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
774         AST_APP_OPTION('W', OPT_CALLER_MONITOR),
775         AST_APP_OPTION('x', OPT_CALLEE_MIXMONITOR),
776         AST_APP_OPTION('X', OPT_CALLER_MIXMONITOR),
777         AST_APP_OPTION('z', OPT_CANCEL_TIMEOUT),
778 END_OPTIONS );
779
780 #define CAN_EARLY_BRIDGE(flags,chan,peer) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
781         OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
782         OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK |  \
783         OPT_CALLER_PARK | OPT_ANNOUNCE | OPT_CALLEE_MACRO | OPT_CALLEE_GOSUB) && \
784         !ast_channel_audiohooks(chan) && !ast_channel_audiohooks(peer) && \
785         ast_framehook_list_is_empty(ast_channel_framehooks(chan)) && ast_framehook_list_is_empty(ast_channel_framehooks(peer)))
786
787 /*
788  * The list of active channels
789  */
790 struct chanlist {
791         AST_LIST_ENTRY(chanlist) node;
792         struct ast_channel *chan;
793         /*! Channel interface dialing string (is tech/number).  (Stored in stuff[]) */
794         const char *interface;
795         /*! Channel technology name.  (Stored in stuff[]) */
796         const char *tech;
797         /*! Channel device addressing.  (Stored in stuff[]) */
798         const char *number;
799         /*! Original channel name.  Must be freed.  Could be NULL if allocation failed. */
800         char *orig_chan_name;
801         uint64_t flags;
802         /*! Saved connected party info from an AST_CONTROL_CONNECTED_LINE. */
803         struct ast_party_connected_line connected;
804         /*! TRUE if an AST_CONTROL_CONNECTED_LINE update was saved to the connected element. */
805         unsigned int pending_connected_update:1;
806         struct ast_aoc_decoded *aoc_s_rate_list;
807         /*! The interface, tech, and number strings are stuffed here. */
808         char stuff[0];
809 };
810
811 AST_LIST_HEAD_NOLOCK(dial_head, chanlist);
812
813 static int detect_disconnect(struct ast_channel *chan, char code, struct ast_str **featurecode);
814
815 static void chanlist_free(struct chanlist *outgoing)
816 {
817         ast_party_connected_line_free(&outgoing->connected);
818         ast_aoc_destroy_decoded(outgoing->aoc_s_rate_list);
819         ast_free(outgoing->orig_chan_name);
820         ast_free(outgoing);
821 }
822
823 static void hanguptree(struct dial_head *out_chans, struct ast_channel *exception, int hangupcause)
824 {
825         /* Hang up a tree of stuff */
826         struct chanlist *outgoing;
827
828         while ((outgoing = AST_LIST_REMOVE_HEAD(out_chans, node))) {
829                 /* Hangup any existing lines we have open */
830                 if (outgoing->chan && (outgoing->chan != exception)) {
831                         if (hangupcause >= 0) {
832                                 /* This is for the channel drivers */
833                                 ast_channel_hangupcause_set(outgoing->chan, hangupcause);
834                         }
835                         ast_hangup(outgoing->chan);
836                 }
837                 chanlist_free(outgoing);
838         }
839 }
840
841 #define AST_MAX_WATCHERS 256
842
843 /*
844  * argument to handle_cause() and other functions.
845  */
846 struct cause_args {
847         struct ast_channel *chan;
848         int busy;
849         int congestion;
850         int nochan;
851 };
852
853 static void handle_cause(int cause, struct cause_args *num)
854 {
855         switch(cause) {
856         case AST_CAUSE_BUSY:
857                 num->busy++;
858                 break;
859         case AST_CAUSE_CONGESTION:
860                 num->congestion++;
861                 break;
862         case AST_CAUSE_NO_ROUTE_DESTINATION:
863         case AST_CAUSE_UNREGISTERED:
864                 num->nochan++;
865                 break;
866         case AST_CAUSE_NO_ANSWER:
867         case AST_CAUSE_NORMAL_CLEARING:
868                 break;
869         default:
870                 num->nochan++;
871                 break;
872         }
873 }
874
875 static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri)
876 {
877         char rexten[2] = { exten, '\0' };
878
879         if (context) {
880                 if (!ast_goto_if_exists(chan, context, rexten, pri))
881                         return 1;
882         } else {
883                 if (!ast_goto_if_exists(chan, ast_channel_context(chan), rexten, pri))
884                         return 1;
885                 else if (!ast_strlen_zero(ast_channel_macrocontext(chan))) {
886                         if (!ast_goto_if_exists(chan, ast_channel_macrocontext(chan), rexten, pri))
887                                 return 1;
888                 }
889         }
890         return 0;
891 }
892
893 /* do not call with chan lock held */
894 static const char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
895 {
896         const char *context;
897         const char *exten;
898
899         ast_channel_lock(chan);
900         context = ast_strdupa(S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)));
901         exten = ast_strdupa(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)));
902         ast_channel_unlock(chan);
903
904         return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
905 }
906
907 /*!
908  * helper function for wait_for_answer()
909  *
910  * \param o Outgoing call channel list.
911  * \param num Incoming call channel cause accumulation
912  * \param peerflags Dial option flags
913  * \param single TRUE if there is only one outgoing call.
914  * \param caller_entertained TRUE if the caller is being entertained by MOH or ringback.
915  * \param to Remaining call timeout time.
916  * \param forced_clid OPT_FORCECLID caller id to send
917  * \param stored_clid Caller id representing the called party if needed
918  *
919  * XXX this code is highly suspicious, as it essentially overwrites
920  * the outgoing channel without properly deleting it.
921  *
922  * \todo eventually this function should be intergrated into and replaced by ast_call_forward()
923  */
924 static void do_forward(struct chanlist *o, struct cause_args *num,
925         struct ast_flags64 *peerflags, int single, int caller_entertained, int *to,
926         struct ast_party_id *forced_clid, struct ast_party_id *stored_clid)
927 {
928         char tmpchan[256];
929         char forwarder[AST_CHANNEL_NAME];
930         struct ast_channel *original = o->chan;
931         struct ast_channel *c = o->chan; /* the winner */
932         struct ast_channel *in = num->chan; /* the input channel */
933         char *stuff;
934         char *tech;
935         int cause;
936         struct ast_party_caller caller;
937
938         ast_copy_string(forwarder, ast_channel_name(c), sizeof(forwarder));
939         ast_copy_string(tmpchan, ast_channel_call_forward(c), sizeof(tmpchan));
940         if ((stuff = strchr(tmpchan, '/'))) {
941                 *stuff++ = '\0';
942                 tech = tmpchan;
943         } else {
944                 const char *forward_context;
945                 ast_channel_lock(c);
946                 forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
947                 if (ast_strlen_zero(forward_context)) {
948                         forward_context = NULL;
949                 }
950                 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", ast_channel_call_forward(c), forward_context ? forward_context : ast_channel_context(c));
951                 ast_channel_unlock(c);
952                 stuff = tmpchan;
953                 tech = "Local";
954         }
955         if (!strcasecmp(tech, "Local")) {
956                 /*
957                  * Drop the connected line update block for local channels since
958                  * this is going to run dialplan and the user can change his
959                  * mind about what connected line information he wants to send.
960                  */
961                 ast_clear_flag64(o, OPT_IGNORE_CONNECTEDLINE);
962         }
963
964         /* Before processing channel, go ahead and check for forwarding */
965         ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", ast_channel_name(in), tech, stuff, ast_channel_name(c));
966         /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
967         if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
968                 ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", ast_channel_name(in), tech, stuff);
969                 ast_channel_publish_dial_forward(in, original, NULL, NULL, "CANCEL",
970                         ast_channel_call_forward(original));
971                 c = o->chan = NULL;
972                 cause = AST_CAUSE_BUSY;
973         } else {
974                 struct ast_stream_topology *topology;
975
976                 ast_channel_lock(in);
977                 topology = ast_stream_topology_clone(ast_channel_get_stream_topology(in));
978                 ast_channel_unlock(in);
979
980                 /* Setup parameters */
981                 c = o->chan = ast_request_with_stream_topology(tech, topology, NULL, in, stuff, &cause);
982
983                 ast_stream_topology_free(topology);
984
985                 if (c) {
986                         if (single && !caller_entertained) {
987                                 ast_channel_make_compatible(in, o->chan);
988                         }
989                         ast_channel_lock_both(in, o->chan);
990                         ast_channel_inherit_variables(in, o->chan);
991                         ast_channel_datastore_inherit(in, o->chan);
992                         pbx_builtin_setvar_helper(o->chan, "FORWARDERNAME", forwarder);
993                         ast_max_forwards_decrement(o->chan);
994                         ast_channel_unlock(in);
995                         ast_channel_unlock(o->chan);
996                         /* When a call is forwarded, we don't want to track new interfaces
997                          * dialed for CC purposes. Setting the done flag will ensure that
998                          * any Dial operations that happen later won't record CC interfaces.
999                          */
1000                         ast_ignore_cc(o->chan);
1001                         ast_log(LOG_NOTICE, "Not accepting call completion offers from call-forward recipient %s\n", ast_channel_name(o->chan));
1002                 } else
1003                         ast_log(LOG_NOTICE,
1004                                 "Forwarding failed to create channel to dial '%s/%s' (cause = %d)\n",
1005                                 tech, stuff, cause);
1006         }
1007         if (!c) {
1008                 ast_channel_publish_dial(in, original, stuff, "BUSY");
1009                 ast_clear_flag64(o, DIAL_STILLGOING);
1010                 handle_cause(cause, num);
1011                 ast_hangup(original);
1012         } else {
1013                 ast_channel_lock_both(c, original);
1014                 ast_party_redirecting_copy(ast_channel_redirecting(c),
1015                         ast_channel_redirecting(original));
1016                 ast_channel_unlock(c);
1017                 ast_channel_unlock(original);
1018
1019                 ast_channel_lock_both(c, in);
1020
1021                 if (single && !caller_entertained && CAN_EARLY_BRIDGE(peerflags, c, in)) {
1022                         ast_rtp_instance_early_bridge_make_compatible(c, in);
1023                 }
1024
1025                 if (!ast_channel_redirecting(c)->from.number.valid
1026                         || ast_strlen_zero(ast_channel_redirecting(c)->from.number.str)) {
1027                         /*
1028                          * The call was not previously redirected so it is
1029                          * now redirected from this number.
1030                          */
1031                         ast_party_number_free(&ast_channel_redirecting(c)->from.number);
1032                         ast_party_number_init(&ast_channel_redirecting(c)->from.number);
1033                         ast_channel_redirecting(c)->from.number.valid = 1;
1034                         ast_channel_redirecting(c)->from.number.str =
1035                                 ast_strdup(S_OR(ast_channel_macroexten(in), ast_channel_exten(in)));
1036                 }
1037
1038                 ast_channel_dialed(c)->transit_network_select = ast_channel_dialed(in)->transit_network_select;
1039
1040                 /* Determine CallerID to store in outgoing channel. */
1041                 ast_party_caller_set_init(&caller, ast_channel_caller(c));
1042                 if (ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
1043                         caller.id = *stored_clid;
1044                         ast_channel_set_caller_event(c, &caller, NULL);
1045                         ast_set_flag64(o, DIAL_CALLERID_ABSENT);
1046                 } else if (ast_strlen_zero(S_COR(ast_channel_caller(c)->id.number.valid,
1047                         ast_channel_caller(c)->id.number.str, NULL))) {
1048                         /*
1049                          * The new channel has no preset CallerID number by the channel
1050                          * driver.  Use the dialplan extension and hint name.
1051                          */
1052                         caller.id = *stored_clid;
1053                         ast_channel_set_caller_event(c, &caller, NULL);
1054                         ast_set_flag64(o, DIAL_CALLERID_ABSENT);
1055                 } else {
1056                         ast_clear_flag64(o, DIAL_CALLERID_ABSENT);
1057                 }
1058
1059                 /* Determine CallerID for outgoing channel to send. */
1060                 if (ast_test_flag64(o, OPT_FORCECLID)) {
1061                         struct ast_party_connected_line connected;
1062
1063                         ast_party_connected_line_init(&connected);
1064                         connected.id = *forced_clid;
1065                         ast_party_connected_line_copy(ast_channel_connected(c), &connected);
1066                 } else {
1067                         ast_connected_line_copy_from_caller(ast_channel_connected(c), ast_channel_caller(in));
1068                 }
1069
1070                 ast_channel_req_accountcodes(c, in, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
1071
1072                 ast_channel_appl_set(c, "AppDial");
1073                 ast_channel_data_set(c, "(Outgoing Line)");
1074                 ast_channel_publish_snapshot(c);
1075
1076                 ast_channel_unlock(in);
1077                 if (single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
1078                         struct ast_party_redirecting redirecting;
1079
1080                         /*
1081                          * Redirecting updates to the caller make sense only on single
1082                          * calls.
1083                          *
1084                          * We must unlock c before calling
1085                          * ast_channel_redirecting_macro, because we put c into
1086                          * autoservice there.  That is pretty much a guaranteed
1087                          * deadlock.  This is why the handling of c's lock may seem a
1088                          * bit unusual here.
1089                          */
1090                         ast_party_redirecting_init(&redirecting);
1091                         ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(c));
1092                         ast_channel_unlock(c);
1093                         if (ast_channel_redirecting_sub(c, in, &redirecting, 0) &&
1094                                 ast_channel_redirecting_macro(c, in, &redirecting, 1, 0)) {
1095                                 ast_channel_update_redirecting(in, &redirecting, NULL);
1096                         }
1097                         ast_party_redirecting_free(&redirecting);
1098                 } else {
1099                         ast_channel_unlock(c);
1100                 }
1101
1102                 if (ast_test_flag64(peerflags, OPT_CANCEL_TIMEOUT)) {
1103                         *to = -1;
1104                 }
1105
1106                 if (ast_call(c, stuff, 0)) {
1107                         ast_log(LOG_NOTICE, "Forwarding failed to dial '%s/%s'\n",
1108                                 tech, stuff);
1109                         ast_channel_publish_dial(in, original, stuff, "CONGESTION");
1110                         ast_clear_flag64(o, DIAL_STILLGOING);
1111                         ast_hangup(original);
1112                         ast_hangup(c);
1113                         c = o->chan = NULL;
1114                         num->nochan++;
1115                 } else {
1116                         ast_channel_publish_dial_forward(in, original, c, NULL, "CANCEL",
1117                                 ast_channel_call_forward(original));
1118
1119                         ast_channel_publish_dial(in, c, stuff, NULL);
1120
1121                         /* Hangup the original channel now, in case we needed it */
1122                         ast_hangup(original);
1123                 }
1124                 if (single && !caller_entertained) {
1125                         ast_indicate(in, -1);
1126                 }
1127         }
1128 }
1129
1130 /* argument used for some functions. */
1131 struct privacy_args {
1132         int sentringing;
1133         int privdb_val;
1134         char privcid[256];
1135         char privintro[1024];
1136         char status[256];
1137 };
1138
1139 static void publish_dial_end_event(struct ast_channel *in, struct dial_head *out_chans, struct ast_channel *exception, const char *status)
1140 {
1141         struct chanlist *outgoing;
1142         AST_LIST_TRAVERSE(out_chans, outgoing, node) {
1143                 if (!outgoing->chan || outgoing->chan == exception) {
1144                         continue;
1145                 }
1146                 ast_channel_publish_dial(in, outgoing->chan, NULL, status);
1147         }
1148 }
1149
1150 /*!
1151  * \internal
1152  * \brief Update connected line on chan from peer.
1153  * \since 13.6.0
1154  *
1155  * \param chan Channel to get connected line updated.
1156  * \param peer Channel providing connected line information.
1157  * \param is_caller Non-zero if chan is the calling channel.
1158  *
1159  * \return Nothing
1160  */
1161 static void update_connected_line_from_peer(struct ast_channel *chan, struct ast_channel *peer, int is_caller)
1162 {
1163         struct ast_party_connected_line connected_caller;
1164
1165         ast_party_connected_line_init(&connected_caller);
1166
1167         ast_channel_lock(peer);
1168         ast_connected_line_copy_from_caller(&connected_caller, ast_channel_caller(peer));
1169         ast_channel_unlock(peer);
1170         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
1171         if (ast_channel_connected_line_sub(peer, chan, &connected_caller, 0)
1172                 && ast_channel_connected_line_macro(peer, chan, &connected_caller, is_caller, 0)) {
1173                 ast_channel_update_connected_line(chan, &connected_caller, NULL);
1174         }
1175         ast_party_connected_line_free(&connected_caller);
1176 }
1177
1178 static struct ast_channel *wait_for_answer(struct ast_channel *in,
1179         struct dial_head *out_chans, int *to, struct ast_flags64 *peerflags,
1180         char *opt_args[],
1181         struct privacy_args *pa,
1182         const struct cause_args *num_in, int *result, char *dtmf_progress,
1183         const int ignore_cc,
1184         struct ast_party_id *forced_clid, struct ast_party_id *stored_clid)
1185 {
1186         struct cause_args num = *num_in;
1187         int prestart = num.busy + num.congestion + num.nochan;
1188         int orig = *to;
1189         struct ast_channel *peer = NULL;
1190         struct chanlist *outgoing = AST_LIST_FIRST(out_chans);
1191         /* single is set if only one destination is enabled */
1192         int single = outgoing && !AST_LIST_NEXT(outgoing, node);
1193         int caller_entertained = outgoing
1194                 && ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
1195         struct ast_str *featurecode = ast_str_alloca(AST_FEATURE_MAX_LEN + 1);
1196         int cc_recall_core_id;
1197         int is_cc_recall;
1198         int cc_frame_received = 0;
1199         int num_ringing = 0;
1200         struct timeval start = ast_tvnow();
1201
1202         if (single) {
1203                 /* Turn off hold music, etc */
1204                 if (!caller_entertained) {
1205                         ast_deactivate_generator(in);
1206                         /* If we are calling a single channel, and not providing ringback or music, */
1207                         /* then, make them compatible for in-band tone purpose */
1208                         if (ast_channel_make_compatible(in, outgoing->chan) < 0) {
1209                                 /* If these channels can not be made compatible,
1210                                  * there is no point in continuing.  The bridge
1211                                  * will just fail if it gets that far.
1212                                  */
1213                                 *to = -1;
1214                                 strcpy(pa->status, "CONGESTION");
1215                                 ast_channel_publish_dial(in, outgoing->chan, NULL, pa->status);
1216                                 return NULL;
1217                         }
1218                 }
1219
1220                 if (!ast_test_flag64(outgoing, OPT_IGNORE_CONNECTEDLINE)
1221                         && !ast_test_flag64(outgoing, DIAL_CALLERID_ABSENT)) {
1222                         update_connected_line_from_peer(in, outgoing->chan, 1);
1223                 }
1224         }
1225
1226         is_cc_recall = ast_cc_is_recall(in, &cc_recall_core_id, NULL);
1227
1228         while ((*to = ast_remaining_ms(start, orig)) && !peer) {
1229                 struct chanlist *o;
1230                 int pos = 0; /* how many channels do we handle */
1231                 int numlines = prestart;
1232                 struct ast_channel *winner;
1233                 struct ast_channel *watchers[AST_MAX_WATCHERS];
1234
1235                 watchers[pos++] = in;
1236                 AST_LIST_TRAVERSE(out_chans, o, node) {
1237                         /* Keep track of important channels */
1238                         if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
1239                                 watchers[pos++] = o->chan;
1240                         numlines++;
1241                 }
1242                 if (pos == 1) { /* only the input channel is available */
1243                         if (numlines == (num.busy + num.congestion + num.nochan)) {
1244                                 ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
1245                                 if (num.busy)
1246                                         strcpy(pa->status, "BUSY");
1247                                 else if (num.congestion)
1248                                         strcpy(pa->status, "CONGESTION");
1249                                 else if (num.nochan)
1250                                         strcpy(pa->status, "CHANUNAVAIL");
1251                         } else {
1252                                 ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
1253                         }
1254                         *to = 0;
1255                         if (is_cc_recall) {
1256                                 ast_cc_failed(cc_recall_core_id, "Everyone is busy/congested for the recall. How sad");
1257                         }
1258                         return NULL;
1259                 }
1260                 winner = ast_waitfor_n(watchers, pos, to);
1261                 AST_LIST_TRAVERSE(out_chans, o, node) {
1262                         struct ast_frame *f;
1263                         struct ast_channel *c = o->chan;
1264
1265                         if (c == NULL)
1266                                 continue;
1267                         if (ast_test_flag64(o, DIAL_STILLGOING) && ast_channel_state(c) == AST_STATE_UP) {
1268                                 if (!peer) {
1269                                         ast_verb(3, "%s answered %s\n", ast_channel_name(c), ast_channel_name(in));
1270                                         if (o->orig_chan_name
1271                                                 && strcmp(o->orig_chan_name, ast_channel_name(c))) {
1272                                                 /*
1273                                                  * The channel name changed so we must generate COLP update.
1274                                                  * Likely because a call pickup channel masqueraded in.
1275                                                  */
1276                                                 update_connected_line_from_peer(in, c, 1);
1277                                         } else if (!single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
1278                                                 if (o->pending_connected_update) {
1279                                                         if (ast_channel_connected_line_sub(c, in, &o->connected, 0) &&
1280                                                                 ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
1281                                                                 ast_channel_update_connected_line(in, &o->connected, NULL);
1282                                                         }
1283                                                 } else if (!ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
1284                                                         update_connected_line_from_peer(in, c, 1);
1285                                                 }
1286                                         }
1287                                         if (o->aoc_s_rate_list) {
1288                                                 size_t encoded_size;
1289                                                 struct ast_aoc_encoded *encoded;
1290                                                 if ((encoded = ast_aoc_encode(o->aoc_s_rate_list, &encoded_size, o->chan))) {
1291                                                         ast_indicate_data(in, AST_CONTROL_AOC, encoded, encoded_size);
1292                                                         ast_aoc_destroy_encoded(encoded);
1293                                                 }
1294                                         }
1295                                         peer = c;
1296                                         publish_dial_end_event(in, out_chans, peer, "CANCEL");
1297                                         ast_copy_flags64(peerflags, o,
1298                                                 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
1299                                                 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
1300                                                 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
1301                                                 OPT_CALLEE_PARK | OPT_CALLER_PARK |
1302                                                 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
1303                                                 DIAL_NOFORWARDHTML);
1304                                         ast_channel_dialcontext_set(c, "");
1305                                         ast_channel_exten_set(c, "");
1306                                 }
1307                                 continue;
1308                         }
1309                         if (c != winner)
1310                                 continue;
1311                         /* here, o->chan == c == winner */
1312                         if (!ast_strlen_zero(ast_channel_call_forward(c))) {
1313                                 pa->sentringing = 0;
1314                                 if (!ignore_cc && (f = ast_read(c))) {
1315                                         if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_CC) {
1316                                                 /* This channel is forwarding the call, and is capable of CC, so
1317                                                  * be sure to add the new device interface to the list
1318                                                  */
1319                                                 ast_handle_cc_control_frame(in, c, f->data.ptr);
1320                                         }
1321                                         ast_frfree(f);
1322                                 }
1323
1324                                 if (o->pending_connected_update) {
1325                                         /*
1326                                          * Re-seed the chanlist's connected line information with
1327                                          * previously acquired connected line info from the incoming
1328                                          * channel.  The previously acquired connected line info could
1329                                          * have been set through the CONNECTED_LINE dialplan function.
1330                                          */
1331                                         o->pending_connected_update = 0;
1332                                         ast_channel_lock(in);
1333                                         ast_party_connected_line_copy(&o->connected, ast_channel_connected(in));
1334                                         ast_channel_unlock(in);
1335                                 }
1336
1337                                 do_forward(o, &num, peerflags, single, caller_entertained, &orig,
1338                                         forced_clid, stored_clid);
1339
1340                                 if (o->chan) {
1341                                         ast_free(o->orig_chan_name);
1342                                         o->orig_chan_name = ast_strdup(ast_channel_name(o->chan));
1343                                         if (single
1344                                                 && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)
1345                                                 && !ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
1346                                                 update_connected_line_from_peer(in, o->chan, 1);
1347                                         }
1348                                 }
1349                                 continue;
1350                         }
1351                         f = ast_read(winner);
1352                         if (!f) {
1353                                 ast_channel_hangupcause_set(in, ast_channel_hangupcause(c));
1354                                 ast_channel_publish_dial(in, c, NULL, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(c)));
1355                                 ast_hangup(c);
1356                                 c = o->chan = NULL;
1357                                 ast_clear_flag64(o, DIAL_STILLGOING);
1358                                 handle_cause(ast_channel_hangupcause(in), &num);
1359                                 continue;
1360                         }
1361                         switch (f->frametype) {
1362                         case AST_FRAME_CONTROL:
1363                                 switch (f->subclass.integer) {
1364                                 case AST_CONTROL_ANSWER:
1365                                         /* This is our guy if someone answered. */
1366                                         if (!peer) {
1367                                                 ast_verb(3, "%s answered %s\n", ast_channel_name(c), ast_channel_name(in));
1368                                                 if (o->orig_chan_name
1369                                                         && strcmp(o->orig_chan_name, ast_channel_name(c))) {
1370                                                         /*
1371                                                          * The channel name changed so we must generate COLP update.
1372                                                          * Likely because a call pickup channel masqueraded in.
1373                                                          */
1374                                                         update_connected_line_from_peer(in, c, 1);
1375                                                 } else if (!single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
1376                                                         if (o->pending_connected_update) {
1377                                                                 if (ast_channel_connected_line_sub(c, in, &o->connected, 0) &&
1378                                                                         ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
1379                                                                         ast_channel_update_connected_line(in, &o->connected, NULL);
1380                                                                 }
1381                                                         } else if (!ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
1382                                                                 update_connected_line_from_peer(in, c, 1);
1383                                                         }
1384                                                 }
1385                                                 if (o->aoc_s_rate_list) {
1386                                                         size_t encoded_size;
1387                                                         struct ast_aoc_encoded *encoded;
1388                                                         if ((encoded = ast_aoc_encode(o->aoc_s_rate_list, &encoded_size, o->chan))) {
1389                                                                 ast_indicate_data(in, AST_CONTROL_AOC, encoded, encoded_size);
1390                                                                 ast_aoc_destroy_encoded(encoded);
1391                                                         }
1392                                                 }
1393                                                 peer = c;
1394                                                 /* Inform everyone else that they've been canceled.
1395                                                  * The dial end event for the peer will be sent out after
1396                                                  * other Dial options have been handled.
1397                                                  */
1398                                                 publish_dial_end_event(in, out_chans, peer, "CANCEL");
1399                                                 ast_copy_flags64(peerflags, o,
1400                                                         OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
1401                                                         OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
1402                                                         OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
1403                                                         OPT_CALLEE_PARK | OPT_CALLER_PARK |
1404                                                         OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
1405                                                         DIAL_NOFORWARDHTML);
1406                                                 ast_channel_dialcontext_set(c, "");
1407                                                 ast_channel_exten_set(c, "");
1408                                                 if (CAN_EARLY_BRIDGE(peerflags, in, peer)) {
1409                                                         /* Setup early bridge if appropriate */
1410                                                         ast_channel_early_bridge(in, peer);
1411                                                 }
1412                                         }
1413                                         /* If call has been answered, then the eventual hangup is likely to be normal hangup */
1414                                         ast_channel_hangupcause_set(in, AST_CAUSE_NORMAL_CLEARING);
1415                                         ast_channel_hangupcause_set(c, AST_CAUSE_NORMAL_CLEARING);
1416                                         break;
1417                                 case AST_CONTROL_BUSY:
1418                                         ast_verb(3, "%s is busy\n", ast_channel_name(c));
1419                                         ast_channel_hangupcause_set(in, ast_channel_hangupcause(c));
1420                                         ast_channel_publish_dial(in, c, NULL, "BUSY");
1421                                         ast_hangup(c);
1422                                         c = o->chan = NULL;
1423                                         ast_clear_flag64(o, DIAL_STILLGOING);
1424                                         handle_cause(AST_CAUSE_BUSY, &num);
1425                                         break;
1426                                 case AST_CONTROL_CONGESTION:
1427                                         ast_verb(3, "%s is circuit-busy\n", ast_channel_name(c));
1428                                         ast_channel_hangupcause_set(in, ast_channel_hangupcause(c));
1429                                         ast_channel_publish_dial(in, c, NULL, "CONGESTION");
1430                                         ast_hangup(c);
1431                                         c = o->chan = NULL;
1432                                         ast_clear_flag64(o, DIAL_STILLGOING);
1433                                         handle_cause(AST_CAUSE_CONGESTION, &num);
1434                                         break;
1435                                 case AST_CONTROL_RINGING:
1436                                         /* This is a tricky area to get right when using a native
1437                                          * CC agent. The reason is that we do the best we can to send only a
1438                                          * single ringing notification to the caller.
1439                                          *
1440                                          * Call completion complicates the logic used here. CCNR is typically
1441                                          * offered during a ringing message. Let's say that party A calls
1442                                          * parties B, C, and D. B and C do not support CC requests, but D
1443                                          * does. If we were to receive a ringing notification from B before
1444                                          * the others, then we would end up sending a ringing message to
1445                                          * A with no CCNR offer present.
1446                                          *
1447                                          * The approach that we have taken is that if we receive a ringing
1448                                          * response from a party and no CCNR offer is present, we need to
1449                                          * wait. Specifically, we need to wait until either a) a called party
1450                                          * offers CCNR in its ringing response or b) all called parties have
1451                                          * responded in some way to our call and none offers CCNR.
1452                                          *
1453                                          * The drawback to this is that if one of the parties has a delayed
1454                                          * response or, god forbid, one just plain doesn't respond to our
1455                                          * outgoing call, then this will result in a significant delay between
1456                                          * when the caller places the call and hears ringback.
1457                                          *
1458                                          * Note also that if CC is disabled for this call, then it is perfectly
1459                                          * fine for ringing frames to get sent through.
1460                                          */
1461                                         ++num_ringing;
1462                                         if (ignore_cc || cc_frame_received || num_ringing == numlines) {
1463                                                 ast_verb(3, "%s is ringing\n", ast_channel_name(c));
1464                                                 /* Setup early media if appropriate */
1465                                                 if (single && !caller_entertained
1466                                                         && CAN_EARLY_BRIDGE(peerflags, in, c)) {
1467                                                         ast_channel_early_bridge(in, c);
1468                                                 }
1469                                                 if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK) && ast_strlen_zero(opt_args[OPT_ARG_RINGBACK])) {
1470                                                         ast_indicate(in, AST_CONTROL_RINGING);
1471                                                         pa->sentringing++;
1472                                                 }
1473                                         }
1474                                         ast_channel_publish_dial(in, c, NULL, "RINGING");
1475                                         break;
1476                                 case AST_CONTROL_PROGRESS:
1477                                         ast_verb(3, "%s is making progress passing it to %s\n", ast_channel_name(c), ast_channel_name(in));
1478                                         /* Setup early media if appropriate */
1479                                         if (single && !caller_entertained
1480                                                 && CAN_EARLY_BRIDGE(peerflags, in, c)) {
1481                                                 ast_channel_early_bridge(in, c);
1482                                         }
1483                                         if (!ast_test_flag64(outgoing, OPT_RINGBACK)) {
1484                                                 if (single || (!single && !pa->sentringing)) {
1485                                                         ast_indicate(in, AST_CONTROL_PROGRESS);
1486                                                 }
1487                                         }
1488                                         if (!ast_strlen_zero(dtmf_progress)) {
1489                                                 ast_verb(3,
1490                                                         "Sending DTMF '%s' to the called party as result of receiving a PROGRESS message.\n",
1491                                                         dtmf_progress);
1492                                                 ast_dtmf_stream(c, in, dtmf_progress, 250, 0);
1493                                         }
1494                                         ast_channel_publish_dial(in, c, NULL, "PROGRESS");
1495                                         break;
1496                                 case AST_CONTROL_VIDUPDATE:
1497                                 case AST_CONTROL_SRCUPDATE:
1498                                 case AST_CONTROL_SRCCHANGE:
1499                                         if (!single || caller_entertained) {
1500                                                 break;
1501                                         }
1502                                         ast_verb(3, "%s requested media update control %d, passing it to %s\n",
1503                                                 ast_channel_name(c), f->subclass.integer, ast_channel_name(in));
1504                                         ast_indicate(in, f->subclass.integer);
1505                                         break;
1506                                 case AST_CONTROL_CONNECTED_LINE:
1507                                         if (ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
1508                                                 ast_verb(3, "Connected line update to %s prevented.\n", ast_channel_name(in));
1509                                                 break;
1510                                         }
1511                                         if (!single) {
1512                                                 struct ast_party_connected_line connected;
1513
1514                                                 ast_verb(3, "%s connected line has changed. Saving it until answer for %s\n",
1515                                                         ast_channel_name(c), ast_channel_name(in));
1516                                                 ast_party_connected_line_set_init(&connected, &o->connected);
1517                                                 ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected);
1518                                                 ast_party_connected_line_set(&o->connected, &connected, NULL);
1519                                                 ast_party_connected_line_free(&connected);
1520                                                 o->pending_connected_update = 1;
1521                                                 break;
1522                                         }
1523                                         if (ast_channel_connected_line_sub(c, in, f, 1) &&
1524                                                 ast_channel_connected_line_macro(c, in, f, 1, 1)) {
1525                                                 ast_indicate_data(in, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
1526                                         }
1527                                         break;
1528                                 case AST_CONTROL_AOC:
1529                                         {
1530                                                 struct ast_aoc_decoded *decoded = ast_aoc_decode(f->data.ptr, f->datalen, o->chan);
1531                                                 if (decoded && (ast_aoc_get_msg_type(decoded) == AST_AOC_S)) {
1532                                                         ast_aoc_destroy_decoded(o->aoc_s_rate_list);
1533                                                         o->aoc_s_rate_list = decoded;
1534                                                 } else {
1535                                                         ast_aoc_destroy_decoded(decoded);
1536                                                 }
1537                                         }
1538                                         break;
1539                                 case AST_CONTROL_REDIRECTING:
1540                                         if (!single) {
1541                                                 /*
1542                                                  * Redirecting updates to the caller make sense only on single
1543                                                  * calls.
1544                                                  */
1545                                                 break;
1546                                         }
1547                                         if (ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
1548                                                 ast_verb(3, "Redirecting update to %s prevented.\n", ast_channel_name(in));
1549                                                 break;
1550                                         }
1551                                         ast_verb(3, "%s redirecting info has changed, passing it to %s\n",
1552                                                 ast_channel_name(c), ast_channel_name(in));
1553                                         if (ast_channel_redirecting_sub(c, in, f, 1) &&
1554                                                 ast_channel_redirecting_macro(c, in, f, 1, 1)) {
1555                                                 ast_indicate_data(in, AST_CONTROL_REDIRECTING, f->data.ptr, f->datalen);
1556                                         }
1557                                         pa->sentringing = 0;
1558                                         break;
1559                                 case AST_CONTROL_PROCEEDING:
1560                                         ast_verb(3, "%s is proceeding passing it to %s\n", ast_channel_name(c), ast_channel_name(in));
1561                                         if (single && !caller_entertained
1562                                                 && CAN_EARLY_BRIDGE(peerflags, in, c)) {
1563                                                 ast_channel_early_bridge(in, c);
1564                                         }
1565                                         if (!ast_test_flag64(outgoing, OPT_RINGBACK))
1566                                                 ast_indicate(in, AST_CONTROL_PROCEEDING);
1567                                         ast_channel_publish_dial(in, c, NULL, "PROCEEDING");
1568                                         break;
1569                                 case AST_CONTROL_HOLD:
1570                                         /* XXX this should be saved like AST_CONTROL_CONNECTED_LINE for !single || caller_entertained */
1571                                         ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(c));
1572                                         ast_indicate_data(in, AST_CONTROL_HOLD, f->data.ptr, f->datalen);
1573                                         break;
1574                                 case AST_CONTROL_UNHOLD:
1575                                         /* XXX this should be saved like AST_CONTROL_CONNECTED_LINE for !single || caller_entertained */
1576                                         ast_verb(3, "Call on %s left from hold\n", ast_channel_name(c));
1577                                         ast_indicate(in, AST_CONTROL_UNHOLD);
1578                                         break;
1579                                 case AST_CONTROL_OFFHOOK:
1580                                 case AST_CONTROL_FLASH:
1581                                         /* Ignore going off hook and flash */
1582                                         break;
1583                                 case AST_CONTROL_CC:
1584                                         if (!ignore_cc) {
1585                                                 ast_handle_cc_control_frame(in, c, f->data.ptr);
1586                                                 cc_frame_received = 1;
1587                                         }
1588                                         break;
1589                                 case AST_CONTROL_PVT_CAUSE_CODE:
1590                                         ast_indicate_data(in, AST_CONTROL_PVT_CAUSE_CODE, f->data.ptr, f->datalen);
1591                                         break;
1592                                 case -1:
1593                                         if (single && !caller_entertained) {
1594                                                 ast_verb(3, "%s stopped sounds\n", ast_channel_name(c));
1595                                                 ast_indicate(in, -1);
1596                                                 pa->sentringing = 0;
1597                                         }
1598                                         break;
1599                                 default:
1600                                         ast_debug(1, "Dunno what to do with control type %d\n", f->subclass.integer);
1601                                         break;
1602                                 }
1603                                 break;
1604                         case AST_FRAME_VOICE:
1605                         case AST_FRAME_IMAGE:
1606                                 if (caller_entertained) {
1607                                         break;
1608                                 }
1609                                 /* Fall through */
1610                         case AST_FRAME_TEXT:
1611                                 if (single && ast_write(in, f)) {
1612                                         ast_log(LOG_WARNING, "Unable to write frametype: %u\n",
1613                                                 f->frametype);
1614                                 }
1615                                 break;
1616                         case AST_FRAME_HTML:
1617                                 if (single && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)
1618                                         && ast_channel_sendhtml(in, f->subclass.integer, f->data.ptr, f->datalen) == -1) {
1619                                         ast_log(LOG_WARNING, "Unable to send URL\n");
1620                                 }
1621                                 break;
1622                         default:
1623                                 break;
1624                         }
1625                         ast_frfree(f);
1626                 } /* end for */
1627                 if (winner == in) {
1628                         struct ast_frame *f = ast_read(in);
1629 #if 0
1630                         if (f && (f->frametype != AST_FRAME_VOICE))
1631                                 printf("Frame type: %d, %d\n", f->frametype, f->subclass);
1632                         else if (!f || (f->frametype != AST_FRAME_VOICE))
1633                                 printf("Hangup received on %s\n", in->name);
1634 #endif
1635                         if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_HANGUP))) {
1636                                 /* Got hung up */
1637                                 *to = -1;
1638                                 strcpy(pa->status, "CANCEL");
1639                                 publish_dial_end_event(in, out_chans, NULL, pa->status);
1640                                 if (f) {
1641                                         if (f->data.uint32) {
1642                                                 ast_channel_hangupcause_set(in, f->data.uint32);
1643                                         }
1644                                         ast_frfree(f);
1645                                 }
1646                                 if (is_cc_recall) {
1647                                         ast_cc_completed(in, "CC completed, although the caller hung up (cancelled)");
1648                                 }
1649                                 return NULL;
1650                         }
1651
1652                         /* now f is guaranteed non-NULL */
1653                         if (f->frametype == AST_FRAME_DTMF) {
1654                                 if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
1655                                         const char *context;
1656                                         ast_channel_lock(in);
1657                                         context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
1658                                         if (onedigit_goto(in, context, (char) f->subclass.integer, 1)) {
1659                                                 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass.integer);
1660                                                 *to = 0;
1661                                                 *result = f->subclass.integer;
1662                                                 strcpy(pa->status, "CANCEL");
1663                                                 publish_dial_end_event(in, out_chans, NULL, pa->status);
1664                                                 ast_frfree(f);
1665                                                 ast_channel_unlock(in);
1666                                                 if (is_cc_recall) {
1667                                                         ast_cc_completed(in, "CC completed, but the caller used DTMF to exit");
1668                                                 }
1669                                                 return NULL;
1670                                         }
1671                                         ast_channel_unlock(in);
1672                                 }
1673
1674                                 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) &&
1675                                         detect_disconnect(in, f->subclass.integer, &featurecode)) {
1676                                         ast_verb(3, "User requested call disconnect.\n");
1677                                         *to = 0;
1678                                         strcpy(pa->status, "CANCEL");
1679                                         publish_dial_end_event(in, out_chans, NULL, pa->status);
1680                                         ast_frfree(f);
1681                                         if (is_cc_recall) {
1682                                                 ast_cc_completed(in, "CC completed, but the caller hung up with DTMF");
1683                                         }
1684                                         return NULL;
1685                                 }
1686                         }
1687
1688                         /* Send the frame from the in channel to all outgoing channels. */
1689                         AST_LIST_TRAVERSE(out_chans, o, node) {
1690                                 if (!o->chan || !ast_test_flag64(o, DIAL_STILLGOING)) {
1691                                         /* This outgoing channel has died so don't send the frame to it. */
1692                                         continue;
1693                                 }
1694                                 switch (f->frametype) {
1695                                 case AST_FRAME_HTML:
1696                                         /* Forward HTML stuff */
1697                                         if (!ast_test_flag64(o, DIAL_NOFORWARDHTML)
1698                                                 && ast_channel_sendhtml(o->chan, f->subclass.integer, f->data.ptr, f->datalen) == -1) {
1699                                                 ast_log(LOG_WARNING, "Unable to send URL\n");
1700                                         }
1701                                         break;
1702                                 case AST_FRAME_VOICE:
1703                                 case AST_FRAME_IMAGE:
1704                                         if (!single || caller_entertained) {
1705                                                 /*
1706                                                  * We are calling multiple parties or caller is being
1707                                                  * entertained and has thus not been made compatible.
1708                                                  * No need to check any other called parties.
1709                                                  */
1710                                                 goto skip_frame;
1711                                         }
1712                                         /* Fall through */
1713                                 case AST_FRAME_TEXT:
1714                                 case AST_FRAME_DTMF_BEGIN:
1715                                 case AST_FRAME_DTMF_END:
1716                                         if (ast_write(o->chan, f)) {
1717                                                 ast_log(LOG_WARNING, "Unable to forward frametype: %u\n",
1718                                                         f->frametype);
1719                                         }
1720                                         break;
1721                                 case AST_FRAME_CONTROL:
1722                                         switch (f->subclass.integer) {
1723                                         case AST_CONTROL_HOLD:
1724                                                 ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(o->chan));
1725                                                 ast_indicate_data(o->chan, AST_CONTROL_HOLD, f->data.ptr, f->datalen);
1726                                                 break;
1727                                         case AST_CONTROL_UNHOLD:
1728                                                 ast_verb(3, "Call on %s left from hold\n", ast_channel_name(o->chan));
1729                                                 ast_indicate(o->chan, AST_CONTROL_UNHOLD);
1730                                                 break;
1731                                         case AST_CONTROL_VIDUPDATE:
1732                                         case AST_CONTROL_SRCUPDATE:
1733                                         case AST_CONTROL_SRCCHANGE:
1734                                                 if (!single || caller_entertained) {
1735                                                         /*
1736                                                          * We are calling multiple parties or caller is being
1737                                                          * entertained and has thus not been made compatible.
1738                                                          * No need to check any other called parties.
1739                                                          */
1740                                                         goto skip_frame;
1741                                                 }
1742                                                 ast_verb(3, "%s requested media update control %d, passing it to %s\n",
1743                                                         ast_channel_name(in), f->subclass.integer, ast_channel_name(o->chan));
1744                                                 ast_indicate(o->chan, f->subclass.integer);
1745                                                 break;
1746                                         case AST_CONTROL_CONNECTED_LINE:
1747                                                 if (ast_channel_connected_line_sub(in, o->chan, f, 1) &&
1748                                                         ast_channel_connected_line_macro(in, o->chan, f, 0, 1)) {
1749                                                         ast_indicate_data(o->chan, f->subclass.integer, f->data.ptr, f->datalen);
1750                                                 }
1751                                                 break;
1752                                         case AST_CONTROL_REDIRECTING:
1753                                                 if (ast_channel_redirecting_sub(in, o->chan, f, 1) &&
1754                                                         ast_channel_redirecting_macro(in, o->chan, f, 0, 1)) {
1755                                                         ast_indicate_data(o->chan, f->subclass.integer, f->data.ptr, f->datalen);
1756                                                 }
1757                                                 break;
1758                                         default:
1759                                                 /* We are not going to do anything with this frame. */
1760                                                 goto skip_frame;
1761                                         }
1762                                         break;
1763                                 default:
1764                                         /* We are not going to do anything with this frame. */
1765                                         goto skip_frame;
1766                                 }
1767                         }
1768 skip_frame:;
1769                         ast_frfree(f);
1770                 }
1771         }
1772
1773         if (!*to || ast_check_hangup(in)) {
1774                 ast_verb(3, "Nobody picked up in %d ms\n", orig);
1775                 publish_dial_end_event(in, out_chans, NULL, "NOANSWER");
1776         }
1777
1778         if (is_cc_recall) {
1779                 ast_cc_completed(in, "Recall completed!");
1780         }
1781         return peer;
1782 }
1783
1784 static int detect_disconnect(struct ast_channel *chan, char code, struct ast_str **featurecode)
1785 {
1786         char disconnect_code[AST_FEATURE_MAX_LEN];
1787         int res;
1788
1789         ast_str_append(featurecode, 1, "%c", code);
1790
1791         res = ast_get_builtin_feature(chan, "disconnect", disconnect_code, sizeof(disconnect_code));
1792         if (res) {
1793                 ast_str_reset(*featurecode);
1794                 return 0;
1795         }
1796
1797         if (strlen(disconnect_code) > ast_str_strlen(*featurecode)) {
1798                 /* Could be a partial match, anyway */
1799                 if (strncmp(disconnect_code, ast_str_buffer(*featurecode), ast_str_strlen(*featurecode))) {
1800                         ast_str_reset(*featurecode);
1801                 }
1802                 return 0;
1803         }
1804
1805         if (strcmp(disconnect_code, ast_str_buffer(*featurecode))) {
1806                 ast_str_reset(*featurecode);
1807                 return 0;
1808         }
1809
1810         return 1;
1811 }
1812
1813 /* returns true if there is a valid privacy reply */
1814 static int valid_priv_reply(struct ast_flags64 *opts, int res)
1815 {
1816         if (res < '1')
1817                 return 0;
1818         if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
1819                 return 1;
1820         if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
1821                 return 1;
1822         return 0;
1823 }
1824
1825 static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
1826         struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
1827 {
1828
1829         int res2;
1830         int loopcount = 0;
1831
1832         /* Get the user's intro, store it in priv-callerintros/$CID,
1833            unless it is already there-- this should be done before the
1834            call is actually dialed  */
1835
1836         /* all ring indications and moh for the caller has been halted as soon as the
1837            target extension was picked up. We are going to have to kill some
1838            time and make the caller believe the peer hasn't picked up yet */
1839
1840         if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
1841                 char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
1842                 ast_indicate(chan, -1);
1843                 ast_channel_musicclass_set(chan, opt_args[OPT_ARG_MUSICBACK]);
1844                 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
1845                 ast_channel_musicclass_set(chan, original_moh);
1846         } else if (ast_test_flag64(opts, OPT_RINGBACK) || ast_test_flag64(opts, OPT_RING_WITH_EARLY_MEDIA)) {
1847                 ast_indicate(chan, AST_CONTROL_RINGING);
1848                 pa->sentringing++;
1849         }
1850
1851         /* Start autoservice on the other chan ?? */
1852         res2 = ast_autoservice_start(chan);
1853         /* Now Stream the File */
1854         for (loopcount = 0; loopcount < 3; loopcount++) {
1855                 if (res2 && loopcount == 0) /* error in ast_autoservice_start() */
1856                         break;
1857                 if (!res2) /* on timeout, play the message again */
1858                         res2 = ast_play_and_wait(peer, "priv-callpending");
1859                 if (!valid_priv_reply(opts, res2))
1860                         res2 = 0;
1861                 /* priv-callpending script:
1862                    "I have a caller waiting, who introduces themselves as:"
1863                 */
1864                 if (!res2)
1865                         res2 = ast_play_and_wait(peer, pa->privintro);
1866                 if (!valid_priv_reply(opts, res2))
1867                         res2 = 0;
1868                 /* now get input from the called party, as to their choice */
1869                 if (!res2) {
1870                         /* XXX can we have both, or they are mutually exclusive ? */
1871                         if (ast_test_flag64(opts, OPT_PRIVACY))
1872                                 res2 = ast_play_and_wait(peer, "priv-callee-options");
1873                         if (ast_test_flag64(opts, OPT_SCREENING))
1874                                 res2 = ast_play_and_wait(peer, "screen-callee-options");
1875                 }
1876
1877                 /*! \page DialPrivacy Dial Privacy scripts
1878                  * \par priv-callee-options script:
1879                  * \li Dial 1 if you wish this caller to reach you directly in the future,
1880                  *      and immediately connect to their incoming call.
1881                  * \li Dial 2 if you wish to send this caller to voicemail now and forevermore.
1882                  * \li Dial 3 to send this caller to the torture menus, now and forevermore.
1883                  * \li Dial 4 to send this caller to a simple "go away" menu, now and forevermore.
1884                  * \li Dial 5 to allow this caller to come straight thru to you in the future,
1885                  *      but right now, just this once, send them to voicemail.
1886                  *
1887                  * \par screen-callee-options script:
1888                  * \li Dial 1 if you wish to immediately connect to the incoming call
1889                  * \li Dial 2 if you wish to send this caller to voicemail.
1890                  * \li Dial 3 to send this caller to the torture menus.
1891                  * \li Dial 4 to send this caller to a simple "go away" menu.
1892                  */
1893                 if (valid_priv_reply(opts, res2))
1894                         break;
1895                 /* invalid option */
1896                 res2 = ast_play_and_wait(peer, "vm-sorry");
1897         }
1898
1899         if (ast_test_flag64(opts, OPT_MUSICBACK)) {
1900                 ast_moh_stop(chan);
1901         } else if (ast_test_flag64(opts, OPT_RINGBACK) || ast_test_flag64(opts, OPT_RING_WITH_EARLY_MEDIA)) {
1902                 ast_indicate(chan, -1);
1903                 pa->sentringing = 0;
1904         }
1905         ast_autoservice_stop(chan);
1906         if (ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
1907                 /* map keypresses to various things, the index is res2 - '1' */
1908                 static const char * const _val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
1909                 static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
1910                 int i = res2 - '1';
1911                 ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
1912                         opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
1913                 ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
1914         }
1915         switch (res2) {
1916         case '1':
1917                 break;
1918         case '2':
1919                 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1920                 break;
1921         case '3':
1922                 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1923                 break;
1924         case '4':
1925                 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1926                 break;
1927         case '5':
1928                 if (ast_test_flag64(opts, OPT_PRIVACY)) {
1929                         ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1930                         break;
1931                 }
1932                 /* if not privacy, then 5 is the same as "default" case */
1933         default: /* bad input or -1 if failure to start autoservice */
1934                 /* well, if the user messes up, ... he had his chance... What Is The Best Thing To Do?  */
1935                 /* well, there seems basically two choices. Just patch the caller thru immediately,
1936                           or,... put 'em thru to voicemail. */
1937                 /* since the callee may have hung up, let's do the voicemail thing, no database decision */
1938                 ast_log(LOG_NOTICE, "privacy: no valid response from the callee. Sending the caller to voicemail, the callee isn't responding\n");
1939                 /* XXX should we set status to DENY ? */
1940                 /* XXX what about the privacy flags ? */
1941                 break;
1942         }
1943
1944         if (res2 == '1') { /* the only case where we actually connect */
1945                 /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll
1946                    just clog things up, and it's not useful information, not being tied to a CID */
1947                 if (strncmp(pa->privcid, "NOCALLERID", 10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO)) {
1948                         ast_filedelete(pa->privintro, NULL);
1949                         if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1950                                 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1951                         else
1952                                 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1953                 }
1954                 return 0; /* the good exit path */
1955         } else {
1956                 return -1;
1957         }
1958 }
1959
1960 /*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
1961 static int setup_privacy_args(struct privacy_args *pa,
1962         struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
1963 {
1964         char callerid[60];
1965         int res;
1966         char *l;
1967
1968         if (ast_channel_caller(chan)->id.number.valid
1969                 && !ast_strlen_zero(ast_channel_caller(chan)->id.number.str)) {
1970                 l = ast_strdupa(ast_channel_caller(chan)->id.number.str);
1971                 ast_shrink_phone_number(l);
1972                 if (ast_test_flag64(opts, OPT_PRIVACY) ) {
1973                         ast_verb(3, "Privacy DB is '%s', clid is '%s'\n", opt_args[OPT_ARG_PRIVACY], l);
1974                         pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
1975                 } else {
1976                         ast_verb(3, "Privacy Screening, clid is '%s'\n", l);
1977                         pa->privdb_val = AST_PRIVACY_UNKNOWN;
1978                 }
1979         } else {
1980                 char *tnam, *tn2;
1981
1982                 tnam = ast_strdupa(ast_channel_name(chan));
1983                 /* clean the channel name so slashes don't try to end up in disk file name */
1984                 for (tn2 = tnam; *tn2; tn2++) {
1985                         if (*tn2 == '/')  /* any other chars to be afraid of? */
1986                                 *tn2 = '=';
1987                 }
1988                 ast_verb(3, "Privacy-- callerid is empty\n");
1989
1990                 snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", ast_channel_exten(chan), tnam);
1991                 l = callerid;
1992                 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1993         }
1994
1995         ast_copy_string(pa->privcid, l, sizeof(pa->privcid));
1996
1997         if (strncmp(pa->privcid, "NOCALLERID", 10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCALLERID)) {
1998                 /* if callerid is set and OPT_SCREEN_NOCALLERID is set also */
1999                 ast_verb(3, "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
2000                 pa->privdb_val = AST_PRIVACY_ALLOW;
2001         } else if (ast_test_flag64(opts, OPT_SCREEN_NOCALLERID) && strncmp(pa->privcid, "NOCALLERID", 10) == 0) {
2002                 ast_verb(3, "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
2003         }
2004
2005         if (pa->privdb_val == AST_PRIVACY_DENY) {
2006                 ast_verb(3, "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
2007                 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
2008                 return 0;
2009         } else if (pa->privdb_val == AST_PRIVACY_KILL) {
2010                 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
2011                 return 0; /* Is this right? */
2012         } else if (pa->privdb_val == AST_PRIVACY_TORTURE) {
2013                 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
2014                 return 0; /* is this right??? */
2015         } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN) {
2016                 /* Get the user's intro, store it in priv-callerintros/$CID,
2017                    unless it is already there-- this should be done before the
2018                    call is actually dialed  */
2019
2020                 /* make sure the priv-callerintros dir actually exists */
2021                 snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
2022                 if ((res = ast_mkdir(pa->privintro, 0755))) {
2023                         ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
2024                         return -1;
2025                 }
2026
2027                 snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
2028                 if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
2029                         /* the DELUX version of this code would allow this caller the
2030                            option to hear and retape their previously recorded intro.
2031                         */
2032                 } else {
2033                         int duration; /* for feedback from play_and_wait */
2034                         /* the file doesn't exist yet. Let the caller submit his
2035                            vocal intro for posterity */
2036                         /* priv-recordintro script:
2037
2038                            "At the tone, please say your name:"
2039
2040                         */
2041                         int silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
2042                         ast_answer(chan);
2043                         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 */
2044                                                                         /* don't think we'll need a lock removed, we took care of
2045                                                                            conflicts by naming the pa.privintro file */
2046                         if (res == -1) {
2047                                 /* Delete the file regardless since they hung up during recording */
2048                                 ast_filedelete(pa->privintro, NULL);
2049                                 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
2050                                         ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
2051                                 else
2052                                         ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
2053                                 return -1;
2054                         }
2055                         if (!ast_streamfile(chan, "vm-dialout", ast_channel_language(chan)) )
2056                                 ast_waitstream(chan, "");
2057                 }
2058         }
2059         return 1; /* success */
2060 }
2061
2062 static void end_bridge_callback(void *data)
2063 {
2064         char buf[80];
2065         time_t end;
2066         struct ast_channel *chan = data;
2067
2068         time(&end);
2069
2070         ast_channel_lock(chan);
2071         ast_channel_stage_snapshot(chan);
2072         snprintf(buf, sizeof(buf), "%d", ast_channel_get_up_time(chan));
2073         pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
2074         snprintf(buf, sizeof(buf), "%d", ast_channel_get_duration(chan));
2075         pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
2076         ast_channel_stage_snapshot_done(chan);
2077         ast_channel_unlock(chan);
2078 }
2079
2080 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator) {
2081         bconfig->end_bridge_callback_data = originator;
2082 }
2083
2084 static int dial_handle_playtones(struct ast_channel *chan, const char *data)
2085 {
2086         struct ast_tone_zone_sound *ts = NULL;
2087         int res;
2088         const char *str = data;
2089
2090         if (ast_strlen_zero(str)) {
2091                 ast_debug(1,"Nothing to play\n");
2092                 return -1;
2093         }
2094
2095         ts = ast_get_indication_tone(ast_channel_zone(chan), str);
2096
2097         if (ts && ts->data[0]) {
2098                 res = ast_playtones_start(chan, 0, ts->data, 0);
2099         } else {
2100                 res = -1;
2101         }
2102
2103         if (ts) {
2104                 ts = ast_tone_zone_sound_unref(ts);
2105         }
2106
2107         if (res) {
2108                 ast_log(LOG_WARNING, "Unable to start playtone \'%s\'\n", str);
2109         }
2110
2111         return res;
2112 }
2113
2114 /*!
2115  * \internal
2116  * \brief Setup the after bridge goto location on the peer.
2117  * \since 12.0.0
2118  *
2119  * \param chan Calling channel for bridge.
2120  * \param peer Peer channel for bridge.
2121  * \param opts Dialing option flags.
2122  * \param opt_args Dialing option argument strings.
2123  *
2124  * \return Nothing
2125  */
2126 static void setup_peer_after_bridge_goto(struct ast_channel *chan, struct ast_channel *peer, struct ast_flags64 *opts, char *opt_args[])
2127 {
2128         const char *context;
2129         const char *extension;
2130         int priority;
2131
2132         if (ast_test_flag64(opts, OPT_PEER_H)) {
2133                 ast_channel_lock(chan);
2134                 context = ast_strdupa(ast_channel_context(chan));
2135                 ast_channel_unlock(chan);
2136                 ast_bridge_set_after_h(peer, context);
2137         } else if (ast_test_flag64(opts, OPT_CALLEE_GO_ON)) {
2138                 ast_channel_lock(chan);
2139                 context = ast_strdupa(ast_channel_context(chan));
2140                 extension = ast_strdupa(ast_channel_exten(chan));
2141                 priority = ast_channel_priority(chan);
2142                 ast_channel_unlock(chan);
2143                 ast_bridge_set_after_go_on(peer, context, extension, priority,
2144                         opt_args[OPT_ARG_CALLEE_GO_ON]);
2145         }
2146 }
2147
2148 static int dial_exec_full(struct ast_channel *chan, const char *data, struct ast_flags64 *peerflags, int *continue_exec)
2149 {
2150         int res = -1; /* default: error */
2151         char *rest, *cur; /* scan the list of destinations */
2152         struct dial_head out_chans = AST_LIST_HEAD_NOLOCK_INIT_VALUE; /* list of destinations */
2153         struct chanlist *outgoing;
2154         struct chanlist *tmp;
2155         struct ast_channel *peer;
2156         int to; /* timeout */
2157         struct cause_args num = { chan, 0, 0, 0 };
2158         int cause;
2159
2160         struct ast_bridge_config config = { { 0, } };
2161         struct timeval calldurationlimit = { 0, };
2162         char *dtmfcalled = NULL, *dtmfcalling = NULL, *dtmf_progress=NULL;
2163         struct privacy_args pa = {
2164                 .sentringing = 0,
2165                 .privdb_val = 0,
2166                 .status = "INVALIDARGS",
2167         };
2168         int sentringing = 0, moh = 0;
2169         const char *outbound_group = NULL;
2170         int result = 0;
2171         char *parse;
2172         int opermode = 0;
2173         int delprivintro = 0;
2174         AST_DECLARE_APP_ARGS(args,
2175                 AST_APP_ARG(peers);
2176                 AST_APP_ARG(timeout);
2177                 AST_APP_ARG(options);
2178                 AST_APP_ARG(url);
2179         );
2180         struct ast_flags64 opts = { 0, };
2181         char *opt_args[OPT_ARG_ARRAY_SIZE];
2182         int fulldial = 0, num_dialed = 0;
2183         int ignore_cc = 0;
2184         char device_name[AST_CHANNEL_NAME];
2185         char forced_clid_name[AST_MAX_EXTENSION];
2186         char stored_clid_name[AST_MAX_EXTENSION];
2187         int force_forwards_only;        /*!< TRUE if force CallerID on call forward only. Legacy behaviour.*/
2188         /*!
2189          * \brief Forced CallerID party information to send.
2190          * \note This will not have any malloced strings so do not free it.
2191          */
2192         struct ast_party_id forced_clid;
2193         /*!
2194          * \brief Stored CallerID information if needed.
2195          *
2196          * \note If OPT_ORIGINAL_CLID set then this is the o option
2197          * CallerID.  Otherwise it is the dialplan extension and hint
2198          * name.
2199          *
2200          * \note This will not have any malloced strings so do not free it.
2201          */
2202         struct ast_party_id stored_clid;
2203         /*!
2204          * \brief CallerID party information to store.
2205          * \note This will not have any malloced strings so do not free it.
2206          */
2207         struct ast_party_caller caller;
2208         int max_forwards;
2209
2210         /* Reset all DIAL variables back to blank, to prevent confusion (in case we don't reset all of them). */
2211         ast_channel_lock(chan);
2212         ast_channel_stage_snapshot(chan);
2213         pbx_builtin_setvar_helper(chan, "DIALSTATUS", "");
2214         pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", "");
2215         pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", "");
2216         pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", "");
2217         pbx_builtin_setvar_helper(chan, "DIALEDTIME", "");
2218         ast_channel_stage_snapshot_done(chan);
2219         max_forwards = ast_max_forwards_get(chan);
2220         ast_channel_unlock(chan);
2221
2222         if (max_forwards <= 0) {
2223                 ast_log(LOG_WARNING, "Cannot place outbound call from channel '%s'. Max forwards exceeded\n",
2224                                 ast_channel_name(chan));
2225                 pbx_builtin_setvar_helper(chan, "DIALSTATUS", "BUSY");
2226                 return -1;
2227         }
2228
2229         if (ast_strlen_zero(data)) {
2230                 ast_log(LOG_WARNING, "Dial requires an argument (technology/resource)\n");
2231                 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
2232                 return -1;
2233         }
2234
2235         if (ast_check_hangup_locked(chan)) {
2236                 /*
2237                  * Caller hung up before we could dial.  If dial is executed
2238                  * within an AGI then the AGI has likely eaten all queued
2239                  * frames before executing the dial in DeadAGI mode.  With
2240                  * the caller hung up and no pending frames from the caller's
2241                  * read queue, dial would not know that the call has hung up
2242                  * until a called channel answers.  It is rather annoying to
2243                  * whoever just answered the non-existent call.
2244                  *
2245                  * Dial should not continue execution in DeadAGI mode, hangup
2246                  * handlers, or the h exten.
2247                  */
2248                 ast_verb(3, "Caller hung up before dial.\n");
2249                 pbx_builtin_setvar_helper(chan, "DIALSTATUS", "CANCEL");
2250                 return -1;
2251         }
2252
2253         parse = ast_strdupa(data);
2254
2255         AST_STANDARD_APP_ARGS(args, parse);
2256
2257         if (!ast_strlen_zero(args.options) &&
2258                 ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
2259                 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
2260                 goto done;
2261         }
2262
2263         if (ast_strlen_zero(args.peers)) {
2264                 ast_log(LOG_WARNING, "Dial requires an argument (technology/resource)\n");
2265                 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
2266                 goto done;
2267         }
2268
2269         if (ast_cc_call_init(chan, &ignore_cc)) {
2270                 goto done;
2271         }
2272
2273         if (ast_test_flag64(&opts, OPT_SCREEN_NOINTRO) && !ast_strlen_zero(opt_args[OPT_ARG_SCREEN_NOINTRO])) {
2274                 delprivintro = atoi(opt_args[OPT_ARG_SCREEN_NOINTRO]);
2275
2276                 if (delprivintro < 0 || delprivintro > 1) {
2277                         ast_log(LOG_WARNING, "Unknown argument %d specified to n option, ignoring\n", delprivintro);
2278                         delprivintro = 0;
2279                 }
2280         }
2281
2282         if (!ast_test_flag64(&opts, OPT_RINGBACK)) {
2283                 opt_args[OPT_ARG_RINGBACK] = NULL;
2284         }
2285
2286         if (ast_test_flag64(&opts, OPT_OPERMODE)) {
2287                 opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
2288                 ast_verb(3, "Setting operator services mode to %d.\n", opermode);
2289         }
2290
2291         if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
2292                 calldurationlimit.tv_sec = atoi(opt_args[OPT_ARG_DURATION_STOP]);
2293                 if (!calldurationlimit.tv_sec) {
2294                         ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
2295                         pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
2296                         goto done;
2297                 }
2298                 ast_verb(3, "Setting call duration limit to %.3lf seconds.\n", calldurationlimit.tv_sec + calldurationlimit.tv_usec / 1000000.0);
2299         }
2300
2301         if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
2302                 dtmf_progress = opt_args[OPT_ARG_SENDDTMF];
2303                 dtmfcalled = strsep(&dtmf_progress, ":");
2304                 dtmfcalling = strsep(&dtmf_progress, ":");
2305         }
2306
2307         if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
2308                 if (ast_bridge_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
2309                         goto done;
2310         }
2311
2312         /* Setup the forced CallerID information to send if used. */
2313         ast_party_id_init(&forced_clid);
2314         force_forwards_only = 0;
2315         if (ast_test_flag64(&opts, OPT_FORCECLID)) {
2316                 if (ast_strlen_zero(opt_args[OPT_ARG_FORCECLID])) {
2317                         ast_channel_lock(chan);
2318                         forced_clid.number.str = ast_strdupa(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)));
2319                         ast_channel_unlock(chan);
2320                         forced_clid_name[0] = '\0';
2321                         forced_clid.name.str = (char *) get_cid_name(forced_clid_name,
2322                                 sizeof(forced_clid_name), chan);
2323                         force_forwards_only = 1;
2324                 } else {
2325                         /* Note: The opt_args[OPT_ARG_FORCECLID] string value is altered here. */
2326                         ast_callerid_parse(opt_args[OPT_ARG_FORCECLID], &forced_clid.name.str,
2327                                 &forced_clid.number.str);
2328                 }
2329                 if (!ast_strlen_zero(forced_clid.name.str)) {
2330                         forced_clid.name.valid = 1;
2331                 }
2332                 if (!ast_strlen_zero(forced_clid.number.str)) {
2333                         forced_clid.number.valid = 1;
2334                 }
2335         }
2336         if (ast_test_flag64(&opts, OPT_FORCE_CID_TAG)
2337                 && !ast_strlen_zero(opt_args[OPT_ARG_FORCE_CID_TAG])) {
2338                 forced_clid.tag = opt_args[OPT_ARG_FORCE_CID_TAG];
2339         }
2340         forced_clid.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
2341         if (ast_test_flag64(&opts, OPT_FORCE_CID_PRES)
2342                 && !ast_strlen_zero(opt_args[OPT_ARG_FORCE_CID_PRES])) {
2343                 int pres;
2344
2345                 pres = ast_parse_caller_presentation(opt_args[OPT_ARG_FORCE_CID_PRES]);
2346                 if (0 <= pres) {
2347                         forced_clid.number.presentation = pres;
2348                 }
2349         }
2350
2351         /* Setup the stored CallerID information if needed. */
2352         ast_party_id_init(&stored_clid);
2353         if (ast_test_flag64(&opts, OPT_ORIGINAL_CLID)) {
2354                 if (ast_strlen_zero(opt_args[OPT_ARG_ORIGINAL_CLID])) {
2355                         ast_channel_lock(chan);
2356                         ast_party_id_set_init(&stored_clid, &ast_channel_caller(chan)->id);
2357                         if (!ast_strlen_zero(ast_channel_caller(chan)->id.name.str)) {
2358                                 stored_clid.name.str = ast_strdupa(ast_channel_caller(chan)->id.name.str);
2359                         }
2360                         if (!ast_strlen_zero(ast_channel_caller(chan)->id.number.str)) {
2361                                 stored_clid.number.str = ast_strdupa(ast_channel_caller(chan)->id.number.str);
2362                         }
2363                         if (!ast_strlen_zero(ast_channel_caller(chan)->id.subaddress.str)) {
2364                                 stored_clid.subaddress.str = ast_strdupa(ast_channel_caller(chan)->id.subaddress.str);
2365                         }
2366                         if (!ast_strlen_zero(ast_channel_caller(chan)->id.tag)) {
2367                                 stored_clid.tag = ast_strdupa(ast_channel_caller(chan)->id.tag);
2368                         }
2369                         ast_channel_unlock(chan);
2370                 } else {
2371                         /* Note: The opt_args[OPT_ARG_ORIGINAL_CLID] string value is altered here. */
2372                         ast_callerid_parse(opt_args[OPT_ARG_ORIGINAL_CLID], &stored_clid.name.str,
2373                                 &stored_clid.number.str);
2374                         if (!ast_strlen_zero(stored_clid.name.str)) {
2375                                 stored_clid.name.valid = 1;
2376                         }
2377                         if (!ast_strlen_zero(stored_clid.number.str)) {
2378                                 stored_clid.number.valid = 1;
2379                         }
2380                 }
2381         } else {
2382                 /*
2383                  * In case the new channel has no preset CallerID number by the
2384                  * channel driver, setup the dialplan extension and hint name.
2385                  */
2386                 stored_clid_name[0] = '\0';
2387                 stored_clid.name.str = (char *) get_cid_name(stored_clid_name,
2388                         sizeof(stored_clid_name), chan);
2389                 if (ast_strlen_zero(stored_clid.name.str)) {
2390                         stored_clid.name.str = NULL;
2391                 } else {
2392                         stored_clid.name.valid = 1;
2393                 }
2394                 ast_channel_lock(chan);
2395                 stored_clid.number.str = ast_strdupa(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)));
2396                 stored_clid.number.valid = 1;
2397                 ast_channel_unlock(chan);
2398         }
2399
2400         if (ast_test_flag64(&opts, OPT_RESETCDR)) {
2401                 ast_cdr_reset(ast_channel_name(chan), 0);
2402         }
2403         if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
2404                 opt_args[OPT_ARG_PRIVACY] = ast_strdupa(ast_channel_exten(chan));
2405
2406         if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
2407                 res = setup_privacy_args(&pa, &opts, opt_args, chan);
2408                 if (res <= 0)
2409                         goto out;
2410                 res = -1; /* reset default */
2411         }
2412
2413         if (continue_exec)
2414                 *continue_exec = 0;
2415
2416         /* If a channel group has been specified, get it for use when we create peer channels */
2417
2418         ast_channel_lock(chan);
2419         if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
2420                 outbound_group = ast_strdupa(outbound_group);
2421                 pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
2422         } else if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP"))) {
2423                 outbound_group = ast_strdupa(outbound_group);
2424         }
2425         ast_channel_unlock(chan);
2426
2427         /* Set per dial instance flags.  These flags are also passed back to RetryDial. */
2428         ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID
2429                 | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING | OPT_CANCEL_TIMEOUT
2430                 | OPT_ANNOUNCE | OPT_CALLEE_MACRO | OPT_CALLEE_GOSUB | OPT_FORCECLID);
2431
2432         /* PREDIAL: Run gosub on the caller's channel */
2433         if (ast_test_flag64(&opts, OPT_PREDIAL_CALLER)
2434                 && !ast_strlen_zero(opt_args[OPT_ARG_PREDIAL_CALLER])) {
2435                 ast_replace_subargument_delimiter(opt_args[OPT_ARG_PREDIAL_CALLER]);
2436                 ast_app_exec_sub(NULL, chan, opt_args[OPT_ARG_PREDIAL_CALLER], 0);
2437         }
2438
2439         /* loop through the list of dial destinations */
2440         rest = args.peers;
2441         while ((cur = strsep(&rest, "&")) ) {
2442                 struct ast_channel *tc; /* channel for this destination */
2443                 /* Get a technology/resource pair */
2444                 char *number = cur;
2445                 char *tech = strsep(&number, "/");
2446                 size_t tech_len;
2447                 size_t number_len;
2448                 struct ast_stream_topology *topology;
2449
2450                 num_dialed++;
2451                 if (ast_strlen_zero(number)) {
2452                         ast_log(LOG_WARNING, "Dial argument takes format (technology/resource)\n");
2453                         goto out;
2454                 }
2455
2456                 tech_len = strlen(tech) + 1;
2457                 number_len = strlen(number) + 1;
2458                 tmp = ast_calloc(1, sizeof(*tmp) + (2 * tech_len) + number_len);
2459                 if (!tmp) {
2460                         goto out;
2461                 }
2462
2463                 /* Save tech, number, and interface. */
2464                 cur = tmp->stuff;
2465                 strcpy(cur, tech);
2466                 tmp->tech = cur;
2467                 cur += tech_len;
2468                 strcpy(cur, tech);
2469                 cur[tech_len - 1] = '/';
2470                 tmp->interface = cur;
2471                 cur += tech_len;
2472                 strcpy(cur, number);
2473                 tmp->number = cur;
2474
2475                 if (opts.flags) {
2476                         /* Set per outgoing call leg options. */
2477                         ast_copy_flags64(tmp, &opts,
2478                                 OPT_CANCEL_ELSEWHERE |
2479                                 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
2480                                 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
2481                                 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
2482                                 OPT_CALLEE_PARK | OPT_CALLER_PARK |
2483                                 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
2484                                 OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID | OPT_IGNORE_CONNECTEDLINE |
2485                                 OPT_RING_WITH_EARLY_MEDIA);
2486                         ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);
2487                 }
2488
2489                 /* Request the peer */
2490
2491                 ast_channel_lock(chan);
2492                 /*
2493                  * Seed the chanlist's connected line information with previously
2494                  * acquired connected line info from the incoming channel.  The
2495                  * previously acquired connected line info could have been set
2496                  * through the CONNECTED_LINE dialplan function.
2497                  */
2498                 ast_party_connected_line_copy(&tmp->connected, ast_channel_connected(chan));
2499
2500                 topology = ast_stream_topology_clone(ast_channel_get_stream_topology(chan));
2501
2502                 ast_channel_unlock(chan);
2503
2504                 tc = ast_request_with_stream_topology(tmp->tech, topology, NULL, chan, tmp->number, &cause);
2505
2506                 ast_stream_topology_free(topology);
2507
2508                 if (!tc) {
2509                         /* If we can't, just go on to the next call */
2510                         ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
2511                                 tmp->tech, cause, ast_cause2str(cause));
2512                         handle_cause(cause, &num);
2513                         if (!rest) {
2514                                 /* we are on the last destination */
2515                                 ast_channel_hangupcause_set(chan, cause);