2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief dial() & retrydial() - Trivial application to dial a channel and send an URL on answer
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
29 <depend>chan_local</depend>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <sys/signal.h>
40 #include <netinet/in.h>
42 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
43 #include "asterisk/lock.h"
44 #include "asterisk/file.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/say.h"
50 #include "asterisk/config.h"
51 #include "asterisk/features.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/callerid.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/app.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/rtp.h"
58 #include "asterisk/cdr.h"
59 #include "asterisk/manager.h"
60 #include "asterisk/privacy.h"
61 #include "asterisk/stringfields.h"
62 #include "asterisk/global_datastores.h"
63 #include "asterisk/dsp.h"
66 <application name="Dial" language="en_US">
68 Attempt to connect to another device or endpoint and bridge the call.
71 <parameter name="Technology/Resource" required="true" argsep="&">
72 <argument name="Technology/Resource" required="true">
73 <para>Specification of the device(s) to dial. These must be in the format of
74 <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
75 represents a particular channel driver, and <replaceable>Resource</replaceable>
76 represents a resource available to that particular channel driver.</para>
78 <argument name="Technology2/Resource2" required="false" multiple="true">
79 <para>Optional extra devices to dial in parallel</para>
80 <para>If you need more then one enter them as
81 Technology2/Resource2&Technology3/Resourse3&.....</para>
84 <parameter name="timeout" required="false">
85 <para>Specifies the number of seconds we attempt to dial the specified devices</para>
86 <para>If not specified, this defaults to 136 years.</para>
88 <parameter name="options" required="false">
91 <argument name="x" required="true">
92 <para>The file to play to the called party</para>
94 <para>Play an announcement to the called party, where <replaceable>x</replaceable> is the prompt to be played</para>
97 <para>Reset the call detail record (CDR) for this call.</para>
100 <para>If the Dial() application cancels this call, always set the flag to tell the channel
101 driver that the call is answered elsewhere.</para>
104 <para>Allow the calling user to dial a 1 digit extension while waiting for
105 a call to be answered. Exit to that extension if it exists in the
106 current context, or the context defined in the <variable>EXITCONTEXT</variable> variable,
109 <option name="D" argsep=":">
110 <argument name="called" />
111 <argument name="calling" />
112 <para>Send the specified DTMF strings <emphasis>after</emphasis> the called
113 party has answered, but before the call gets bridged. The
114 <replaceable>called</replaceable> DTMF string is sent to the called party, and the
115 <replaceable>calling</replaceable> DTMF string is sent to the calling party. Both arguments
116 can be used alone.</para>
119 <para>Execute the <literal>h</literal> extension for peer after the call ends</para>
122 <para>Force the callerid of the <emphasis>calling</emphasis> channel to be set as the
123 extension associated with the channel using a dialplan <literal>hint</literal>.
124 For example, some PSTNs do not allow CallerID to be set to anything
125 other than the number assigned to the caller.</para>
127 <option name="F" argsep="^">
128 <argument name="context" required="false" />
129 <argument name="exten" required="false" />
130 <argument name="priority" required="true" />
131 <para>When the caller hangs up, transfer the called party
132 to the specified destination and continue execution at that location.</para>
135 <para>Proceed with dialplan execution at the next priority in the current extension if the
136 destination channel hangs up.</para>
138 <option name="G" argsep="^">
139 <argument name="context" required="false" />
140 <argument name="exten" required="false" />
141 <argument name="priority" required="true" />
142 <para>If the call is answered, transfer the calling party to
143 the specified <replaceable>priority</replaceable> and the called party to the specified
144 <replaceable>priority</replaceable> plus one.</para>
146 <para>You cannot use any additional action post answer options in conjunction with this option.</para>
150 <para>Allow the called party to hang up by sending the <literal>*</literal> DTMF digit.</para>
153 <para>Allow the calling party to hang up by hitting the <literal>*</literal> DTMF digit.</para>
156 <para>Asterisk will ignore any forwarding requests it may receive on this dial attempt.</para>
159 <para>Allow the called party to enable parking of the call by sending
160 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
163 <para>Allow the calling party to enable parking of the call by sending
164 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
166 <option name="L" argsep=":">
167 <argument name="x" required="true">
168 <para>Maximum call time, in milliseconds</para>
171 <para>Warning time, in milliseconds</para>
174 <para>Repeat time, in milliseconds</para>
176 <para>Limit the call to <replaceable>x</replaceable> milliseconds. Play a warning when <replaceable>y</replaceable> milliseconds are
177 left. Repeat the warning every <replaceable>z</replaceable> milliseconds until time expires.</para>
178 <para>This option is affected by the following variables:</para>
180 <variable name="LIMIT_PLAYAUDIO_CALLER">
181 <value name="yes" default="true" />
183 <para>If set, this variable causes Asterisk to play the prompts to the caller.</para>
185 <variable name="LIMIT_PLAYAUDIO_CALLEE">
187 <value name="no" default="true"/>
188 <para>If set, this variable causes Asterisk to play the prompts to the callee.</para>
190 <variable name="LIMIT_TIMEOUT_FILE">
191 <value name="filename"/>
192 <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the timeout is reached.
193 If not set, the time remaining will be announced.</para>
195 <variable name="LIMIT_CONNECT_FILE">
196 <value name="filename"/>
197 <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the call begins.
198 If not set, the time remaining will be announced.</para>
200 <variable name="LIMIT_WARNING_FILE">
201 <value name="filename"/>
202 <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play as
203 a warning when time <replaceable>x</replaceable> is reached. If not set, the time remaining will be announced.</para>
208 <argument name="class" required="false"/>
209 <para>Provide hold music to the calling party until a requested
210 channel answers. A specific music on hold <replaceable>class</replaceable>
211 (as defined in <filename>musiconhold.conf</filename>) can be specified.</para>
213 <option name="M" argsep="^">
214 <argument name="macro" required="true">
215 <para>Name of the macro that should be executed.</para>
217 <argument name="arg" multiple="true">
218 <para>Macro arguments</para>
220 <para>Execute the specified <replaceable>macro</replaceable> for the <emphasis>called</emphasis> channel
221 before connecting to the calling channel. Arguments can be specified to the Macro
222 using <literal>^</literal> as a delimiter. The macro can set the variable
223 <variable>MACRO_RESULT</variable> to specify the following actions after the macro is
224 finished executing:</para>
226 <variable name="MACRO_RESULT">
227 <para>If set, this action will be taken after the macro finished executing.</para>
229 Hangup both legs of the call
231 <value name="CONGESTION">
232 Behave as if line congestion was encountered
235 Behave as if a busy signal was encountered
237 <value name="CONTINUE">
238 Hangup the called party and allow the calling party to continue dialplan execution at the next priority
240 <!-- TODO: Fix this syntax up, once we've figured out how to specify the GOTO syntax -->
241 <value name="GOTO:<context>^<exten>^<priority>">
242 Transfer the call to the specified destination.
247 <para>You cannot use any additional action post answer options in conjunction
248 with this option. Also, pbx services are not run on the peer (called) channel,
249 so you will not be able to set timeouts via the TIMEOUT() function in this macro.</para>
253 <para>This option is a modifier for the call screening/privacy mode. (See the
254 <literal>p</literal> and <literal>P</literal> options.) It specifies
255 that no introductions are to be saved in the <directory>priv-callerintros</directory>
259 <para>This option is a modifier for the call screening/privacy mode. It specifies
260 that if Caller*ID is present, do not screen the call.</para>
263 <para>Specify that the Caller*ID that was present on the <emphasis>calling</emphasis> channel
264 be set as the Caller*ID on the <emphasis>called</emphasis> channel. This was the
265 behavior of Asterisk 1.0 and earlier.</para>
268 <argument name="mode">
269 <para>With <replaceable>mode</replaceable> either not specified or set to <literal>1</literal>,
270 the originator hanging up will cause the phone to ring back immediately.</para>
271 <para>With <replaceable>mode</replaceable> set to <literal>2</literal>, when the operator
272 flashes the trunk, it will ring their phone back.</para>
274 <para>Enables <emphasis>operator services</emphasis> mode. This option only
275 works when bridging a DAHDI channel to another DAHDI channel
276 only. if specified on non-DAHDI interfaces, it will be ignored.
277 When the destination answers (presumably an operator services
278 station), the originator no longer has control of their line.
279 They may hang up, but the switch will not release their line
280 until the destination party (the operator) hangs up.</para>
283 <para>This option enables screening mode. This is basically Privacy mode
284 without memory.</para>
287 <argument name="x" />
288 <para>Enable privacy mode. Use <replaceable>x</replaceable> as the family/key in the AstDB database if
289 it is provided. The current extension is used if a database family/key is not specified.</para>
292 <para>Indicate ringing to the calling party, even if the called party isn't actually ringing. Pass no audio to the calling
293 party until the called channel has answered.</para>
296 <argument name="x" required="true" />
297 <para>Hang up the call <replaceable>x</replaceable> seconds <emphasis>after</emphasis> the called party has
298 answered the call.</para>
301 <para>Allow the called party to transfer the calling party by sending the
302 DTMF sequence defined in <filename>features.conf</filename>.</para>
305 <para>Allow the calling party to transfer the called party by sending the
306 DTMF sequence defined in <filename>features.conf</filename>.</para>
308 <option name="U" argsep="^">
309 <argument name="x" required="true">
310 <para>Name of the subroutine to execute via Gosub</para>
312 <argument name="arg" multiple="true" required="false">
313 <para>Arguments for the Gosub routine</para>
315 <para>Execute via Gosub the routine <replaceable>x</replaceable> for the <emphasis>called</emphasis> channel before connecting
316 to the calling channel. Arguments can be specified to the Gosub
317 using <literal>^</literal> as a delimiter. The Gosub routine can set the variable
318 <variable>GOSUB_RESULT</variable> to specify the following actions after the Gosub returns.</para>
320 <variable name="GOSUB_RESULT">
322 Hangup both legs of the call.
324 <value name="CONGESTION">
325 Behave as if line congestion was encountered.
328 Behave as if a busy signal was encountered.
330 <value name="CONTINUE">
331 Hangup the called party and allow the calling party
332 to continue dialplan execution at the next priority.
334 <!-- TODO: Fix this syntax up, once we've figured out how to specify the GOTO syntax -->
335 <value name="GOTO:<context>^<exten>^<priority>">
336 Transfer the call to the specified priority. Optionally, an extension, or
337 extension and priority can be specified.
342 <para>You cannot use any additional action post answer options in conjunction
343 with this option. Also, pbx services are not run on the peer (called) channel,
344 so you will not be able to set timeouts via the TIMEOUT() function in this routine.</para>
348 <para>Allow the called party to enable recording of the call by sending
349 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
352 <para>Allow the calling party to enable recording of the call by sending
353 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
356 <para>Allow the called party to enable recording of the call by sending
357 the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
360 <para>Allow the calling party to enable recording of the call by sending
361 the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
365 <parameter name="URL">
366 <para>The optional URL will be sent to the called party if the channel driver supports it.</para>
370 <para>This application will place calls to one or more specified channels. As soon
371 as one of the requested channels answers, the originating channel will be
372 answered, if it has not already been answered. These two channels will then
373 be active in a bridged call. All other channels that were requested will then
376 <para>Unless there is a timeout specified, the Dial application will wait
377 indefinitely until one of the called channels answers, the user hangs up, or
378 if all of the called channels are busy or unavailable. Dialplan executing will
379 continue if no requested channels can be called, or if the timeout expires.
380 This application will report normal termination if the originating channel
381 hangs up, or if the call is bridged and either of the parties in the bridge
382 ends the call.</para>
384 <para>If the <variable>OUTBOUND_GROUP</variable> variable is set, all peer channels created by this
385 application will be put into that group (as in Set(GROUP()=...).
386 If the <variable>OUTBOUND_GROUP_ONCE</variable> variable is set, all peer channels created by this
387 application will be put into that group (as in Set(GROUP()=...). Unlike OUTBOUND_GROUP,
388 however, the variable will be unset after use.</para>
390 <para>This application sets the following channel variables:</para>
392 <variable name="DIALEDTIME">
393 <para>This is the time from dialing a channel until when it is disconnected.</para>
395 <variable name="ANSWEREDTIME">
396 <para>This is the amount of time for actual call.</para>
398 <variable name="DIALSTATUS">
399 <para>This is the status of the call</para>
400 <value name="CHANUNAVAIL" />
401 <value name="CONGESTION" />
402 <value name="NOANSWER" />
403 <value name="BUSY" />
404 <value name="ANSWER" />
405 <value name="CANCEL" />
406 <value name="DONTCALL">
407 For the Privacy and Screening Modes.
408 Will be set if the called party chooses to send the calling party to the 'Go Away' script.
410 <value name="TORTURE">
411 For the Privacy and Screening Modes.
412 Will be set if the called party chooses to send the calling party to the 'torture' script.
414 <value name="INVALIDARGS" />
419 <application name="RetryDial" language="en_US">
421 Place a call, retrying on failure allowing an optional exit extension.
424 <parameter name="announce" required="true">
425 <para>Filename of sound that will be played when no channel can be reached</para>
427 <parameter name="sleep" required="true">
428 <para>Number of seconds to wait after a dial attempt failed before a new attempt is made</para>
430 <parameter name="retries" required="true">
431 <para>Number of retries</para>
432 <para>When this is reached flow will continue at the next priority in the dialplan</para>
434 <parameter name="dialargs" required="true">
435 <para>Same format as arguments provided to the Dial application</para>
439 <para>This application will attempt to place a call using the normal Dial application.
440 If no channel can be reached, the <replaceable>announce</replaceable> file will be played.
441 Then, it will wait <replaceable>sleep</replaceable> number of seconds before retrying the call.
442 After <replaceable>retries</replaceable> number of attempts, the calling channel will continue at the next priority in the dialplan.
443 If the <replaceable>retries</replaceable> setting is set to 0, this application will retry endlessly.
444 While waiting to retry a call, a 1 digit extension may be dialed. If that
445 extension exists in either the context defined in <variable>EXITCONTEXT</variable> or the current
446 one, The call will jump to that extension immediately.
447 The <replaceable>dialargs</replaceable> are specified in the same format that arguments are provided
448 to the Dial application.</para>
453 static char *app = "Dial";
454 static char *rapp = "RetryDial";
457 OPT_ANNOUNCE = (1 << 0),
458 OPT_RESETCDR = (1 << 1),
459 OPT_DTMF_EXIT = (1 << 2),
460 OPT_SENDDTMF = (1 << 3),
461 OPT_FORCECLID = (1 << 4),
462 OPT_GO_ON = (1 << 5),
463 OPT_CALLEE_HANGUP = (1 << 6),
464 OPT_CALLER_HANGUP = (1 << 7),
465 OPT_DURATION_LIMIT = (1 << 9),
466 OPT_MUSICBACK = (1 << 10),
467 OPT_CALLEE_MACRO = (1 << 11),
468 OPT_SCREEN_NOINTRO = (1 << 12),
469 OPT_SCREEN_NOCLID = (1 << 13),
470 OPT_ORIGINAL_CLID = (1 << 14),
471 OPT_SCREENING = (1 << 15),
472 OPT_PRIVACY = (1 << 16),
473 OPT_RINGBACK = (1 << 17),
474 OPT_DURATION_STOP = (1 << 18),
475 OPT_CALLEE_TRANSFER = (1 << 19),
476 OPT_CALLER_TRANSFER = (1 << 20),
477 OPT_CALLEE_MONITOR = (1 << 21),
478 OPT_CALLER_MONITOR = (1 << 22),
479 OPT_GOTO = (1 << 23),
480 OPT_OPERMODE = (1 << 24),
481 OPT_CALLEE_PARK = (1 << 25),
482 OPT_CALLER_PARK = (1 << 26),
483 OPT_IGNORE_FORWARDING = (1 << 27),
484 OPT_CALLEE_GOSUB = (1 << 28),
485 OPT_CALLEE_MIXMONITOR = (1 << 29),
486 OPT_CALLER_MIXMONITOR = (1 << 30),
489 #define DIAL_STILLGOING (1 << 31)
490 #define DIAL_NOFORWARDHTML ((uint64_t)1 << 32) /* flags are now 64 bits, so keep it up! */
491 #define OPT_CANCEL_ELSEWHERE ((uint64_t)1 << 33)
492 #define OPT_PEER_H ((uint64_t)1 << 34)
493 #define OPT_CALLEE_GO_ON ((uint64_t)1 << 35)
496 OPT_ARG_ANNOUNCE = 0,
499 OPT_ARG_DURATION_LIMIT,
501 OPT_ARG_CALLEE_MACRO,
502 OPT_ARG_CALLEE_GOSUB,
503 OPT_ARG_CALLEE_GO_ON,
505 OPT_ARG_DURATION_STOP,
507 /* note: this entry _MUST_ be the last one in the enum */
511 AST_APP_OPTIONS(dial_exec_options, BEGIN_OPTIONS
512 AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
513 AST_APP_OPTION('C', OPT_RESETCDR),
514 AST_APP_OPTION('c', OPT_CANCEL_ELSEWHERE),
515 AST_APP_OPTION('d', OPT_DTMF_EXIT),
516 AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
517 AST_APP_OPTION('e', OPT_PEER_H),
518 AST_APP_OPTION('f', OPT_FORCECLID),
519 AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
520 AST_APP_OPTION('g', OPT_GO_ON),
521 AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
522 AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
523 AST_APP_OPTION('H', OPT_CALLER_HANGUP),
524 AST_APP_OPTION('i', OPT_IGNORE_FORWARDING),
525 AST_APP_OPTION('k', OPT_CALLEE_PARK),
526 AST_APP_OPTION('K', OPT_CALLER_PARK),
527 AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
528 AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
529 AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
530 AST_APP_OPTION('n', OPT_SCREEN_NOINTRO),
531 AST_APP_OPTION('N', OPT_SCREEN_NOCLID),
532 AST_APP_OPTION('o', OPT_ORIGINAL_CLID),
533 AST_APP_OPTION_ARG('O', OPT_OPERMODE, OPT_ARG_OPERMODE),
534 AST_APP_OPTION('p', OPT_SCREENING),
535 AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
536 AST_APP_OPTION('r', OPT_RINGBACK),
537 AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
538 AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
539 AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
540 AST_APP_OPTION_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
541 AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
542 AST_APP_OPTION('W', OPT_CALLER_MONITOR),
543 AST_APP_OPTION('x', OPT_CALLEE_MIXMONITOR),
544 AST_APP_OPTION('X', OPT_CALLER_MIXMONITOR),
547 #define CAN_EARLY_BRIDGE(flags,chan,peer) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
548 OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
549 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK | OPT_CALLER_PARK) && \
550 !chan->audiohooks && !peer->audiohooks)
553 * The list of active channels
556 struct chanlist *next;
557 struct ast_channel *chan;
562 static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception, int answered_elsewhere)
564 /* Hang up a tree of stuff */
567 /* Hangup any existing lines we have open */
568 if (outgoing->chan && (outgoing->chan != exception)) {
569 if (answered_elsewhere) {
570 /* The flag is used for local channel inheritance and stuff */
571 ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
572 /* This is for the channel drivers */
573 outgoing->chan->hangupcause = AST_CAUSE_ANSWERED_ELSEWHERE;
575 ast_hangup(outgoing->chan);
578 outgoing = outgoing->next;
583 #define AST_MAX_WATCHERS 256
586 * argument to handle_cause() and other functions.
589 struct ast_channel *chan;
595 static void handle_cause(int cause, struct cause_args *num)
597 struct ast_cdr *cdr = num->chan->cdr;
606 case AST_CAUSE_CONGESTION:
612 case AST_CAUSE_NO_ROUTE_DESTINATION:
613 case AST_CAUSE_UNREGISTERED:
619 case AST_CAUSE_NORMAL_CLEARING:
628 /* free the buffer if allocated, and set the pointer to the second arg */
629 #define S_REPLACE(s, new_val) \
636 static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri)
638 char rexten[2] = { exten, '\0' };
641 if (!ast_goto_if_exists(chan, context, rexten, pri))
644 if (!ast_goto_if_exists(chan, chan->context, rexten, pri))
646 else if (!ast_strlen_zero(chan->macrocontext)) {
647 if (!ast_goto_if_exists(chan, chan->macrocontext, rexten, pri))
655 static const char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
657 const char *context = S_OR(chan->macrocontext, chan->context);
658 const char *exten = S_OR(chan->macroexten, chan->exten);
660 return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
663 static void senddialevent(struct ast_channel *src, struct ast_channel *dst, const char *dialstring)
665 manager_event(EVENT_FLAG_CALL, "Dial",
666 "SubEvent: Begin\r\n"
668 "Destination: %s\r\n"
669 "CallerIDNum: %s\r\n"
670 "CallerIDName: %s\r\n"
672 "DestUniqueID: %s\r\n"
673 "Dialstring: %s\r\n",
674 src->name, dst->name, S_OR(src->cid.cid_num, "<unknown>"),
675 S_OR(src->cid.cid_name, "<unknown>"), src->uniqueid,
676 dst->uniqueid, dialstring ? dialstring : "");
679 static void senddialendevent(const struct ast_channel *src, const char *dialstatus)
681 manager_event(EVENT_FLAG_CALL, "Dial",
685 "DialStatus: %s\r\n",
686 src->name, src->uniqueid, dialstatus);
690 * helper function for wait_for_answer()
692 * XXX this code is highly suspicious, as it essentially overwrites
693 * the outgoing channel without properly deleting it.
695 static void do_forward(struct chanlist *o,
696 struct cause_args *num, struct ast_flags64 *peerflags, int single)
699 struct ast_channel *original = o->chan;
700 struct ast_channel *c = o->chan; /* the winner */
701 struct ast_channel *in = num->chan; /* the input channel */
706 ast_copy_string(tmpchan, c->call_forward, sizeof(tmpchan));
707 if ((stuff = strchr(tmpchan, '/'))) {
711 const char *forward_context;
713 forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
714 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", c->call_forward, forward_context ? forward_context : c->context);
715 ast_channel_unlock(c);
719 /* Before processing channel, go ahead and check for forwarding */
720 ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
721 /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
722 if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
723 ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
725 cause = AST_CAUSE_BUSY;
727 /* Setup parameters */
728 c = o->chan = ast_request(tech, in->nativeformats, stuff, &cause);
731 ast_channel_make_compatible(o->chan, in);
732 ast_channel_inherit_variables(in, o->chan);
733 ast_channel_datastore_inherit(in, o->chan);
735 ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
738 ast_clear_flag64(o, DIAL_STILLGOING);
739 handle_cause(cause, num);
740 ast_hangup(original);
742 char *new_cid_num, *new_cid_name;
743 struct ast_channel *src;
745 ast_rtp_make_compatible(c, in, single);
746 if (ast_test_flag64(o, OPT_FORCECLID)) {
747 new_cid_num = ast_strdup(S_OR(in->macroexten, in->exten));
748 new_cid_name = NULL; /* XXX no name ? */
749 src = c; /* XXX possible bug in previous code, which used 'winner' ? it may have changed */
751 new_cid_num = ast_strdup(in->cid.cid_num);
752 new_cid_name = ast_strdup(in->cid.cid_name);
755 ast_string_field_set(c, accountcode, src->accountcode);
756 c->cdrflags = src->cdrflags;
757 S_REPLACE(c->cid.cid_num, new_cid_num);
758 S_REPLACE(c->cid.cid_name, new_cid_name);
760 if (in->cid.cid_ani) { /* XXX or maybe unconditional ? */
761 S_REPLACE(c->cid.cid_ani, ast_strdup(in->cid.cid_ani));
763 S_REPLACE(c->cid.cid_rdnis, ast_strdup(S_OR(in->macroexten, in->exten)));
764 if (ast_call(c, tmpchan, 0)) {
765 ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
766 ast_clear_flag64(o, DIAL_STILLGOING);
767 ast_hangup(original);
772 senddialevent(in, c, stuff);
773 /* After calling, set callerid to extension */
774 if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
775 char cidname[AST_MAX_EXTENSION] = "";
776 ast_set_callerid(c, S_OR(in->macroexten, in->exten), get_cid_name(cidname, sizeof(cidname), in), NULL);
778 /* Hangup the original channel now, in case we needed it */
779 ast_hangup(original);
782 ast_indicate(in, -1);
787 /* argument used for some functions. */
788 struct privacy_args {
792 char privintro[1024];
796 static struct ast_channel *wait_for_answer(struct ast_channel *in,
797 struct chanlist *outgoing, int *to, struct ast_flags64 *peerflags,
798 struct privacy_args *pa,
799 const struct cause_args *num_in, int *result)
801 struct cause_args num = *num_in;
802 int prestart = num.busy + num.congestion + num.nochan;
804 struct ast_channel *peer = NULL;
805 /* single is set if only one destination is enabled */
806 int single = outgoing && !outgoing->next && !ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
808 struct chanlist *epollo;
812 /* Turn off hold music, etc */
813 ast_deactivate_generator(in);
814 /* If we are calling a single channel, make them compatible for in-band tone purpose */
815 ast_channel_make_compatible(outgoing->chan, in);
819 for (epollo = outgoing; epollo; epollo = epollo->next)
820 ast_poll_channel_add(in, epollo->chan);
823 while (*to && !peer) {
825 int pos = 0; /* how many channels do we handle */
826 int numlines = prestart;
827 struct ast_channel *winner;
828 struct ast_channel *watchers[AST_MAX_WATCHERS];
830 watchers[pos++] = in;
831 for (o = outgoing; o; o = o->next) {
832 /* Keep track of important channels */
833 if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
834 watchers[pos++] = o->chan;
837 if (pos == 1) { /* only the input channel is available */
838 if (numlines == (num.busy + num.congestion + num.nochan)) {
839 ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
841 strcpy(pa->status, "BUSY");
842 else if (num.congestion)
843 strcpy(pa->status, "CONGESTION");
845 strcpy(pa->status, "CHANUNAVAIL");
847 ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
852 winner = ast_waitfor_n(watchers, pos, to);
853 for (o = outgoing; o; o = o->next) {
855 struct ast_channel *c = o->chan;
859 if (ast_test_flag64(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
861 ast_verb(3, "%s answered %s\n", c->name, in->name);
863 ast_copy_flags64(peerflags, o,
864 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
865 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
866 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
867 OPT_CALLEE_PARK | OPT_CALLER_PARK |
868 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
870 ast_string_field_set(c, dialcontext, "");
871 ast_copy_string(c->exten, "", sizeof(c->exten));
877 /* here, o->chan == c == winner */
878 if (!ast_strlen_zero(c->call_forward)) {
879 do_forward(o, &num, peerflags, single);
882 f = ast_read(winner);
884 in->hangupcause = c->hangupcause;
886 ast_poll_channel_del(in, c);
890 ast_clear_flag64(o, DIAL_STILLGOING);
891 handle_cause(in->hangupcause, &num);
894 if (f->frametype == AST_FRAME_CONTROL) {
895 switch(f->subclass) {
896 case AST_CONTROL_ANSWER:
897 /* This is our guy if someone answered. */
899 ast_verb(3, "%s answered %s\n", c->name, in->name);
902 peer->cdr->answer = ast_tvnow();
903 peer->cdr->disposition = AST_CDR_ANSWERED;
905 ast_copy_flags64(peerflags, o,
906 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
907 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
908 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
909 OPT_CALLEE_PARK | OPT_CALLER_PARK |
910 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
912 ast_string_field_set(c, dialcontext, "");
913 ast_copy_string(c->exten, "", sizeof(c->exten));
914 if (CAN_EARLY_BRIDGE(peerflags, in, peer))
915 /* Setup early bridge if appropriate */
916 ast_channel_early_bridge(in, peer);
918 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
919 in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
920 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
922 case AST_CONTROL_BUSY:
923 ast_verb(3, "%s is busy\n", c->name);
924 in->hangupcause = c->hangupcause;
927 ast_clear_flag64(o, DIAL_STILLGOING);
928 handle_cause(AST_CAUSE_BUSY, &num);
930 case AST_CONTROL_CONGESTION:
931 ast_verb(3, "%s is circuit-busy\n", c->name);
932 in->hangupcause = c->hangupcause;
935 ast_clear_flag64(o, DIAL_STILLGOING);
936 handle_cause(AST_CAUSE_CONGESTION, &num);
938 case AST_CONTROL_RINGING:
939 ast_verb(3, "%s is ringing\n", c->name);
940 /* Setup early media if appropriate */
941 if (single && CAN_EARLY_BRIDGE(peerflags, in, c))
942 ast_channel_early_bridge(in, c);
943 if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK)) {
944 ast_indicate(in, AST_CONTROL_RINGING);
948 case AST_CONTROL_PROGRESS:
949 ast_verb(3, "%s is making progress passing it to %s\n", c->name, in->name);
950 /* Setup early media if appropriate */
951 if (single && CAN_EARLY_BRIDGE(peerflags, in, c))
952 ast_channel_early_bridge(in, c);
953 if (!ast_test_flag64(outgoing, OPT_RINGBACK))
954 ast_indicate(in, AST_CONTROL_PROGRESS);
956 case AST_CONTROL_VIDUPDATE:
957 ast_verb(3, "%s requested a video update, passing it to %s\n", c->name, in->name);
958 ast_indicate(in, AST_CONTROL_VIDUPDATE);
960 case AST_CONTROL_SRCUPDATE:
961 ast_verb(3, "%s requested a source update, passing it to %s\n", c->name, in->name);
962 ast_indicate(in, AST_CONTROL_SRCUPDATE);
964 case AST_CONTROL_PROCEEDING:
965 ast_verb(3, "%s is proceeding passing it to %s\n", c->name, in->name);
966 if (single && CAN_EARLY_BRIDGE(peerflags, in, c))
967 ast_channel_early_bridge(in, c);
968 if (!ast_test_flag64(outgoing, OPT_RINGBACK))
969 ast_indicate(in, AST_CONTROL_PROCEEDING);
971 case AST_CONTROL_HOLD:
972 ast_verb(3, "Call on %s placed on hold\n", c->name);
973 ast_indicate(in, AST_CONTROL_HOLD);
975 case AST_CONTROL_UNHOLD:
976 ast_verb(3, "Call on %s left from hold\n", c->name);
977 ast_indicate(in, AST_CONTROL_UNHOLD);
979 case AST_CONTROL_OFFHOOK:
980 case AST_CONTROL_FLASH:
981 /* Ignore going off hook and flash */
984 if (!ast_test_flag64(outgoing, OPT_RINGBACK | OPT_MUSICBACK)) {
985 ast_verb(3, "%s stopped sounds\n", c->name);
986 ast_indicate(in, -1);
991 ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
994 switch (f->frametype) {
995 case AST_FRAME_VOICE:
996 case AST_FRAME_IMAGE:
998 if (ast_write(in, f)) {
999 ast_log(LOG_WARNING, "Unable to write frame\n");
1002 case AST_FRAME_HTML:
1003 if (!ast_test_flag64(outgoing, DIAL_NOFORWARDHTML) && ast_channel_sendhtml(in, f->subclass, f->data.ptr, f->datalen) == -1) {
1004 ast_log(LOG_WARNING, "Unable to send URL\n");
1014 struct ast_frame *f = ast_read(in);
1016 if (f && (f->frametype != AST_FRAME_VOICE))
1017 printf("Frame type: %d, %d\n", f->frametype, f->subclass);
1018 else if (!f || (f->frametype != AST_FRAME_VOICE))
1019 printf("Hangup received on %s\n", in->name);
1021 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
1024 strcpy(pa->status, "CANCEL");
1025 ast_cdr_noanswer(in->cdr);
1027 if (f->data.uint32) {
1028 in->hangupcause = f->data.uint32;
1035 /* now f is guaranteed non-NULL */
1036 if (f->frametype == AST_FRAME_DTMF) {
1037 if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
1038 const char *context;
1039 ast_channel_lock(in);
1040 context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
1041 if (onedigit_goto(in, context, (char) f->subclass, 1)) {
1042 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
1044 ast_cdr_noanswer(in->cdr);
1045 *result = f->subclass;
1046 strcpy(pa->status, "CANCEL");
1048 ast_channel_unlock(in);
1051 ast_channel_unlock(in);
1054 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) &&
1055 (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
1056 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
1058 strcpy(pa->status, "CANCEL");
1059 ast_cdr_noanswer(in->cdr);
1065 /* Forward HTML stuff */
1066 if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML))
1067 if (ast_channel_sendhtml(outgoing->chan, f->subclass, f->data.ptr, f->datalen) == -1)
1068 ast_log(LOG_WARNING, "Unable to send URL\n");
1070 if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF_BEGIN) || (f->frametype == AST_FRAME_DTMF_END))) {
1071 if (ast_write(outgoing->chan, f))
1072 ast_log(LOG_WARNING, "Unable to forward voice or dtmf\n");
1074 if (single && (f->frametype == AST_FRAME_CONTROL) &&
1075 ((f->subclass == AST_CONTROL_HOLD) ||
1076 (f->subclass == AST_CONTROL_UNHOLD) ||
1077 (f->subclass == AST_CONTROL_VIDUPDATE) ||
1078 (f->subclass == AST_CONTROL_SRCUPDATE))) {
1079 ast_verb(3, "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
1080 ast_indicate_data(outgoing->chan, f->subclass, f->data.ptr, f->datalen);
1085 ast_verb(3, "Nobody picked up in %d ms\n", orig);
1086 if (!*to || ast_check_hangup(in))
1087 ast_cdr_noanswer(in->cdr);
1091 for (epollo = outgoing; epollo; epollo = epollo->next) {
1093 ast_poll_channel_del(in, epollo->chan);
1100 static void replace_macro_delimiter(char *s)
1107 /* returns true if there is a valid privacy reply */
1108 static int valid_priv_reply(struct ast_flags64 *opts, int res)
1112 if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
1114 if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
1119 static int do_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
1120 char *parse, struct timeval *calldurationlimit)
1122 char *stringp = ast_strdupa(parse);
1123 char *limit_str, *warning_str, *warnfreq_str;
1125 int play_to_caller = 0, play_to_callee = 0;
1128 limit_str = strsep(&stringp, ":");
1129 warning_str = strsep(&stringp, ":");
1130 warnfreq_str = strsep(&stringp, ":");
1132 config->timelimit = atol(limit_str);
1134 config->play_warning = atol(warning_str);
1136 config->warning_freq = atol(warnfreq_str);
1138 if (!config->timelimit) {
1139 ast_log(LOG_WARNING, "Dial does not accept L(%s), hanging up.\n", limit_str);
1140 config->timelimit = config->play_warning = config->warning_freq = 0;
1141 config->warning_sound = NULL;
1142 return -1; /* error */
1143 } else if ( (delta = config->play_warning - config->timelimit) > 0) {
1144 int w = config->warning_freq;
1146 /* If the first warning is requested _after_ the entire call would end,
1147 and no warning frequency is requested, then turn off the warning. If
1148 a warning frequency is requested, reduce the 'first warning' time by
1149 that frequency until it falls within the call's total time limit.
1151 timelim->| delta |<-playwarning
1152 0__________________|_________________|
1155 so the number of intervals to cut is 1+(delta-1)/w
1159 config->play_warning = 0;
1161 config->play_warning -= w * ( 1 + (delta-1)/w );
1162 if (config->play_warning < 1)
1163 config->play_warning = config->warning_freq = 0;
1167 ast_channel_lock(chan);
1169 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
1171 play_to_caller = var ? ast_true(var) : 1;
1173 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
1174 play_to_callee = var ? ast_true(var) : 0;
1176 if (!play_to_caller && !play_to_callee)
1179 var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
1180 config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
1182 /* The code looking at config wants a NULL, not just "", to decide
1183 * that the message should not be played, so we replace "" with NULL.
1184 * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
1188 var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
1189 config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
1191 var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
1192 config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
1194 ast_channel_unlock(chan);
1196 /* undo effect of S(x) in case they are both used */
1197 calldurationlimit->tv_sec = 0;
1198 calldurationlimit->tv_usec = 0;
1200 /* more efficient to do it like S(x) does since no advanced opts */
1201 if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
1202 calldurationlimit->tv_sec = config->timelimit / 1000;
1203 calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
1204 ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
1205 calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
1206 config->timelimit = play_to_caller = play_to_callee =
1207 config->play_warning = config->warning_freq = 0;
1209 ast_verb(3, "Limit Data for this call:\n");
1210 ast_verb(4, "timelimit = %ld\n", config->timelimit);
1211 ast_verb(4, "play_warning = %ld\n", config->play_warning);
1212 ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
1213 ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
1214 ast_verb(4, "warning_freq = %ld\n", config->warning_freq);
1215 ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
1216 ast_verb(4, "warning_sound = %s\n", config->warning_sound);
1217 ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
1220 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
1222 ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
1226 static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
1227 struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
1233 /* Get the user's intro, store it in priv-callerintros/$CID,
1234 unless it is already there-- this should be done before the
1235 call is actually dialed */
1237 /* all ring indications and moh for the caller has been halted as soon as the
1238 target extension was picked up. We are going to have to kill some
1239 time and make the caller believe the peer hasn't picked up yet */
1241 if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
1242 char *original_moh = ast_strdupa(chan->musicclass);
1243 ast_indicate(chan, -1);
1244 ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
1245 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
1246 ast_string_field_set(chan, musicclass, original_moh);
1247 } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
1248 ast_indicate(chan, AST_CONTROL_RINGING);
1252 /* Start autoservice on the other chan ?? */
1253 res2 = ast_autoservice_start(chan);
1254 /* Now Stream the File */
1255 for (loopcount = 0; loopcount < 3; loopcount++) {
1256 if (res2 && loopcount == 0) /* error in ast_autoservice_start() */
1258 if (!res2) /* on timeout, play the message again */
1259 res2 = ast_play_and_wait(peer, "priv-callpending");
1260 if (!valid_priv_reply(opts, res2))
1262 /* priv-callpending script:
1263 "I have a caller waiting, who introduces themselves as:"
1266 res2 = ast_play_and_wait(peer, pa->privintro);
1267 if (!valid_priv_reply(opts, res2))
1269 /* now get input from the called party, as to their choice */
1271 /* XXX can we have both, or they are mutually exclusive ? */
1272 if (ast_test_flag64(opts, OPT_PRIVACY))
1273 res2 = ast_play_and_wait(peer, "priv-callee-options");
1274 if (ast_test_flag64(opts, OPT_SCREENING))
1275 res2 = ast_play_and_wait(peer, "screen-callee-options");
1277 /*! \page DialPrivacy Dial Privacy scripts
1278 \par priv-callee-options script:
1279 "Dial 1 if you wish this caller to reach you directly in the future,
1280 and immediately connect to their incoming call
1281 Dial 2 if you wish to send this caller to voicemail now and
1283 Dial 3 to send this caller to the torture menus, now and forevermore.
1284 Dial 4 to send this caller to a simple "go away" menu, now and forevermore.
1285 Dial 5 to allow this caller to come straight thru to you in the future,
1286 but right now, just this once, send them to voicemail."
1287 \par screen-callee-options script:
1288 "Dial 1 if you wish to immediately connect to the incoming call
1289 Dial 2 if you wish to send this caller to voicemail.
1290 Dial 3 to send this caller to the torture menus.
1291 Dial 4 to send this caller to a simple "go away" menu.
1293 if (valid_priv_reply(opts, res2))
1295 /* invalid option */
1296 res2 = ast_play_and_wait(peer, "vm-sorry");
1299 if (ast_test_flag64(opts, OPT_MUSICBACK)) {
1301 } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
1302 ast_indicate(chan, -1);
1303 pa->sentringing = 0;
1305 ast_autoservice_stop(chan);
1306 if (ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
1307 /* map keypresses to various things, the index is res2 - '1' */
1308 static const char *_val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
1309 static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
1311 ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
1312 opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
1313 ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
1319 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1322 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1325 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1328 /* XXX should we set status to DENY ? */
1329 if (ast_test_flag64(opts, OPT_PRIVACY))
1331 /* if not privacy, then 5 is the same as "default" case */
1332 default: /* bad input or -1 if failure to start autoservice */
1333 /* well, if the user messes up, ... he had his chance... What Is The Best Thing To Do? */
1334 /* well, there seems basically two choices. Just patch the caller thru immediately,
1335 or,... put 'em thru to voicemail. */
1336 /* since the callee may have hung up, let's do the voicemail thing, no database decision */
1337 ast_log(LOG_NOTICE, "privacy: no valid response from the callee. Sending the caller to voicemail, the callee isn't responding\n");
1338 /* XXX should we set status to DENY ? */
1339 /* XXX what about the privacy flags ? */
1343 if (res2 == '1') { /* the only case where we actually connect */
1344 /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll
1345 just clog things up, and it's not useful information, not being tied to a CID */
1346 if (strncmp(pa->privcid, "NOCALLERID", 10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO)) {
1347 ast_filedelete(pa->privintro, NULL);
1348 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1349 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1351 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1353 return 0; /* the good exit path */
1355 ast_hangup(peer); /* hang up on the callee -- he didn't want to talk anyway! */
1360 /*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
1361 static int setup_privacy_args(struct privacy_args *pa,
1362 struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
1367 int silencethreshold;
1369 if (!ast_strlen_zero(chan->cid.cid_num)) {
1370 l = ast_strdupa(chan->cid.cid_num);
1371 ast_shrink_phone_number(l);
1372 if (ast_test_flag64(opts, OPT_PRIVACY) ) {
1373 ast_verb(3, "Privacy DB is '%s', clid is '%s'\n", opt_args[OPT_ARG_PRIVACY], l);
1374 pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
1376 ast_verb(3, "Privacy Screening, clid is '%s'\n", l);
1377 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1382 tnam = ast_strdupa(chan->name);
1383 /* clean the channel name so slashes don't try to end up in disk file name */
1384 for (tn2 = tnam; *tn2; tn2++) {
1385 if (*tn2 == '/') /* any other chars to be afraid of? */
1388 ast_verb(3, "Privacy-- callerid is empty\n");
1390 snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
1392 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1395 ast_copy_string(pa->privcid, l, sizeof(pa->privcid));
1397 if (strncmp(pa->privcid, "NOCALLERID", 10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCLID)) {
1398 /* if callerid is set and OPT_SCREEN_NOCLID is set also */
1399 ast_verb(3, "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
1400 pa->privdb_val = AST_PRIVACY_ALLOW;
1401 } else if (ast_test_flag64(opts, OPT_SCREEN_NOCLID) && strncmp(pa->privcid, "NOCALLERID", 10) == 0) {
1402 ast_verb(3, "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
1405 if (pa->privdb_val == AST_PRIVACY_DENY) {
1406 ast_verb(3, "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
1407 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1409 } else if (pa->privdb_val == AST_PRIVACY_KILL) {
1410 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1411 return 0; /* Is this right? */
1412 } else if (pa->privdb_val == AST_PRIVACY_TORTURE) {
1413 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1414 return 0; /* is this right??? */
1415 } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN) {
1416 /* Get the user's intro, store it in priv-callerintros/$CID,
1417 unless it is already there-- this should be done before the
1418 call is actually dialed */
1420 /* make sure the priv-callerintros dir actually exists */
1421 snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
1422 if ((res = ast_mkdir(pa->privintro, 0755))) {
1423 ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
1427 snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
1428 if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
1429 /* the DELUX version of this code would allow this caller the
1430 option to hear and retape their previously recorded intro.
1433 int duration; /* for feedback from play_and_wait */
1434 /* the file doesn't exist yet. Let the caller submit his
1435 vocal intro for posterity */
1436 /* priv-recordintro script:
1438 "At the tone, please say your name:"
1441 silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
1443 res = ast_play_and_record(chan, "priv-recordintro", pa->privintro, 4, "gsm", &duration, silencethreshold, 2000, 0); /* NOTE: I've reduced the total time to 4 sec */
1444 /* don't think we'll need a lock removed, we took care of
1445 conflicts by naming the pa.privintro file */
1447 /* Delete the file regardless since they hung up during recording */
1448 ast_filedelete(pa->privintro, NULL);
1449 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1450 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1452 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1455 if (!ast_streamfile(chan, "vm-dialout", chan->language) )
1456 ast_waitstream(chan, "");
1459 return 1; /* success */
1462 static void end_bridge_callback(void *data)
1466 struct ast_channel *chan = data;
1474 ast_channel_lock(chan);
1475 if (chan->cdr->answer.tv_sec) {
1476 snprintf(buf, sizeof(buf), "%ld", end - chan->cdr->answer.tv_sec);
1477 pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
1480 if (chan->cdr->start.tv_sec) {
1481 snprintf(buf, sizeof(buf), "%ld", end - chan->cdr->start.tv_sec);
1482 pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
1484 ast_channel_unlock(chan);
1487 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator) {
1488 bconfig->end_bridge_callback_data = originator;
1491 static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags64 *peerflags, int *continue_exec)
1493 int res = -1; /* default: error */
1494 char *rest, *cur; /* scan the list of destinations */
1495 struct chanlist *outgoing = NULL; /* list of destinations */
1496 struct ast_channel *peer;
1497 int to; /* timeout */
1498 struct cause_args num = { chan, 0, 0, 0 };
1501 char cidname[AST_MAX_EXTENSION] = "";
1503 struct ast_bridge_config config = { { 0, } };
1504 struct timeval calldurationlimit = { 0, };
1505 char *dtmfcalled = NULL, *dtmfcalling = NULL;
1506 struct privacy_args pa = {
1509 .status = "INVALIDARGS",
1511 int sentringing = 0, moh = 0;
1512 const char *outbound_group = NULL;
1516 AST_DECLARE_APP_ARGS(args,
1518 AST_APP_ARG(timeout);
1519 AST_APP_ARG(options);
1522 struct ast_flags64 opts = { 0, };
1523 char *opt_args[OPT_ARG_ARRAY_SIZE];
1524 struct ast_datastore *datastore = NULL;
1525 int fulldial = 0, num_dialed = 0;
1527 /* Reset all DIAL variables back to blank, to prevent confusion (in case we don't reset all of them). */
1528 pbx_builtin_setvar_helper(chan, "DIALSTATUS", "");
1529 pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", "");
1530 pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", "");
1531 pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", "");
1532 pbx_builtin_setvar_helper(chan, "DIALEDTIME", "");
1534 if (ast_strlen_zero(data)) {
1535 ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
1536 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1540 parse = ast_strdupa(data);
1542 AST_STANDARD_APP_ARGS(args, parse);
1544 if (!ast_strlen_zero(args.options) &&
1545 ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
1546 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1550 if (ast_strlen_zero(args.peers)) {
1551 ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
1552 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1557 if (ast_test_flag64(&opts, OPT_OPERMODE)) {
1558 opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
1559 ast_verb(3, "Setting operator services mode to %d.\n", opermode);
1562 if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
1563 calldurationlimit.tv_sec = atoi(opt_args[OPT_ARG_DURATION_STOP]);
1564 if (!calldurationlimit.tv_sec) {
1565 ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
1566 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1569 ast_verb(3, "Setting call duration limit to %.3lf seconds.\n", calldurationlimit.tv_sec + calldurationlimit.tv_usec / 1000000.0);
1572 if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
1573 dtmfcalling = opt_args[OPT_ARG_SENDDTMF];
1574 dtmfcalled = strsep(&dtmfcalling, ":");
1577 if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
1578 if (do_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
1582 if (ast_test_flag64(&opts, OPT_RESETCDR) && chan->cdr)
1583 ast_cdr_reset(chan->cdr, NULL);
1584 if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
1585 opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
1587 if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
1588 res = setup_privacy_args(&pa, &opts, opt_args, chan);
1591 res = -1; /* reset default */
1594 if (ast_test_flag64(&opts, OPT_DTMF_EXIT)) {
1595 __ast_answer(chan, 0, 0);
1601 /* If a channel group has been specified, get it for use when we create peer channels */
1603 ast_channel_lock(chan);
1604 if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
1605 outbound_group = ast_strdupa(outbound_group);
1606 pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
1607 } else if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP"))) {
1608 outbound_group = ast_strdupa(outbound_group);
1610 ast_channel_unlock(chan);
1611 ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING);
1613 /* loop through the list of dial destinations */
1615 while ((cur = strsep(&rest, "&")) ) {
1616 struct chanlist *tmp;
1617 struct ast_channel *tc; /* channel for this destination */
1618 /* Get a technology/[device:]number pair */
1620 char *interface = ast_strdupa(number);
1621 char *tech = strsep(&number, "/");
1622 /* find if we already dialed this interface */
1623 struct ast_dialed_interface *di;
1624 AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
1627 ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
1630 if (!(tmp = ast_calloc(1, sizeof(*tmp))))
1633 ast_copy_flags64(tmp, &opts,
1634 OPT_CANCEL_ELSEWHERE |
1635 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
1636 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
1637 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
1638 OPT_CALLEE_PARK | OPT_CALLER_PARK |
1639 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
1640 OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID);
1641 ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);
1643 ast_copy_string(numsubst, number, sizeof(numsubst));
1644 /* Request the peer */
1646 ast_channel_lock(chan);
1647 datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL);
1648 ast_channel_unlock(chan);
1651 dialed_interfaces = datastore->data;
1653 if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
1654 ast_log(LOG_WARNING, "Unable to create channel datastore for dialed interfaces. Aborting!\n");
1659 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
1661 if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
1666 datastore->data = dialed_interfaces;
1667 AST_LIST_HEAD_INIT(dialed_interfaces);
1669 ast_channel_lock(chan);
1670 ast_channel_datastore_add(chan, datastore);
1671 ast_channel_unlock(chan);
1674 AST_LIST_LOCK(dialed_interfaces);
1675 AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
1676 if (!strcasecmp(di->interface, interface)) {
1677 ast_log(LOG_WARNING, "Skipping dialing interface '%s' again since it has already been dialed\n",
1682 AST_LIST_UNLOCK(dialed_interfaces);
1690 /* It is always ok to dial a Local interface. We only keep track of
1691 * which "real" interfaces have been dialed. The Local channel will
1692 * inherit this list so that if it ends up dialing a real interface,
1693 * it won't call one that has already been called. */
1694 if (strcasecmp(tech, "Local")) {
1695 if (!(di = ast_calloc(1, sizeof(*di) + strlen(interface)))) {
1696 AST_LIST_UNLOCK(dialed_interfaces);
1700 strcpy(di->interface, interface);
1702 AST_LIST_LOCK(dialed_interfaces);
1703 AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
1704 AST_LIST_UNLOCK(dialed_interfaces);
1707 tc = ast_request(tech, chan->nativeformats, numsubst, &cause);
1709 /* If we can't, just go on to the next call */
1710 ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
1711 tech, cause, ast_cause2str(cause));
1712 handle_cause(cause, &num);
1713 if (!rest) /* we are on the last destination */
1714 chan->hangupcause = cause;
1718 pbx_builtin_setvar_helper(tc, "DIALEDPEERNUMBER", numsubst);
1720 /* Setup outgoing SDP to match incoming one */
1721 ast_rtp_make_compatible(tc, chan, !outgoing && !rest);
1723 /* Inherit specially named variables from parent channel */
1724 ast_channel_inherit_variables(chan, tc);
1725 ast_channel_datastore_inherit(chan, tc);
1727 tc->appl = "AppDial";
1728 tc->data = "(Outgoing Line)";
1729 memset(&tc->whentohangup, 0, sizeof(tc->whentohangup));
1731 S_REPLACE(tc->cid.cid_num, ast_strdup(chan->cid.cid_num));
1732 S_REPLACE(tc->cid.cid_name, ast_strdup(chan->cid.cid_name));
1733 S_REPLACE(tc->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
1734 S_REPLACE(tc->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
1736 ast_string_field_set(tc, accountcode, chan->accountcode);
1737 tc->cdrflags = chan->cdrflags;
1738 if (ast_strlen_zero(tc->musicclass))
1739 ast_string_field_set(tc, musicclass, chan->musicclass);
1740 /* Pass callingpres, type of number, tns, ADSI CPE, transfer capability */
1741 tc->cid.cid_pres = chan->cid.cid_pres;
1742 tc->cid.cid_ton = chan->cid.cid_ton;
1743 tc->cid.cid_tns = chan->cid.cid_tns;
1744 tc->cid.cid_ani2 = chan->cid.cid_ani2;
1745 tc->adsicpe = chan->adsicpe;
1746 tc->transfercapability = chan->transfercapability;
1748 /* If we have an outbound group, set this peer channel to it */
1750 ast_app_group_set_channel(tc, outbound_group);
1751 /* If the calling channel has the ANSWERED_ELSEWHERE flag set, inherit it. This is to support local channels */
1752 if (ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE))
1753 ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
1755 /* Check if we're forced by configuration */
1756 if (ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE))
1757 ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
1760 /* Inherit context and extension */
1761 ast_string_field_set(tc, dialcontext, ast_strlen_zero(chan->macrocontext) ? chan->context : chan->macrocontext);
1762 if (!ast_strlen_zero(chan->macroexten))
1763 ast_copy_string(tc->exten, chan->macroexten, sizeof(tc->exten));
1765 ast_copy_string(tc->exten, chan->exten, sizeof(tc->exten));
1767 res = ast_call(tc, numsubst, 0); /* Place the call, but don't wait on the answer */
1769 /* Save the info in cdr's that we called them */
1771 ast_cdr_setdestchan(chan->cdr, tc->name);
1773 /* check the results of ast_call */
1775 /* Again, keep going even if there's an error */
1776 ast_debug(1, "ast call on peer returned %d\n", res);
1777 ast_verb(3, "Couldn't call %s\n", numsubst);
1778 if (tc->hangupcause) {
1779 chan->hangupcause = tc->hangupcause;
1786 senddialevent(chan, tc, numsubst);
1787 ast_verb(3, "Called %s\n", numsubst);
1788 if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID))
1789 ast_set_callerid(tc, S_OR(chan->macroexten, chan->exten), get_cid_name(cidname, sizeof(cidname), chan), NULL);
1791 /* Put them in the list of outgoing thingies... We're ready now.
1792 XXX If we're forcibly removed, these outgoing calls won't get
1794 ast_set_flag64(tmp, DIAL_STILLGOING);
1796 tmp->next = outgoing;
1798 /* If this line is up, don't try anybody else */
1799 if (outgoing->chan->_state == AST_STATE_UP)
1803 if (ast_strlen_zero(args.timeout)) {
1806 to = atoi(args.timeout);
1810 ast_log(LOG_WARNING, "Invalid timeout specified: '%s'. Setting timeout to infinite\n", args.timeout);
1816 strcpy(pa.status, "CHANUNAVAIL");
1817 if (fulldial == num_dialed) {
1822 /* Our status will at least be NOANSWER */
1823 strcpy(pa.status, "NOANSWER");
1824 if (ast_test_flag64(outgoing, OPT_MUSICBACK)) {
1826 if (!ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
1827 char *original_moh = ast_strdupa(chan->musicclass);
1828 ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
1829 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
1830 ast_string_field_set(chan, musicclass, original_moh);
1832 ast_moh_start(chan, NULL, NULL);
1834 ast_indicate(chan, AST_CONTROL_PROGRESS);
1835 } else if (ast_test_flag64(outgoing, OPT_RINGBACK)) {
1836 ast_indicate(chan, AST_CONTROL_RINGING);
1841 peer = wait_for_answer(chan, outgoing, &to, peerflags, &pa, &num, &result);
1843 /* The ast_channel_datastore_remove() function could fail here if the
1844 * datastore was moved to another channel during a masquerade. If this is
1845 * the case, don't free the datastore here because later, when the channel
1846 * to which the datastore was moved hangs up, it will attempt to free this
1847 * datastore again, causing a crash
1849 if (!ast_channel_datastore_remove(chan, datastore))
1850 ast_datastore_free(datastore);
1854 } else if (to) { /* Musta gotten hung up */
1856 } else { /* Nobody answered, next please? */
1860 /* SIP, in particular, sends back this error code to indicate an
1861 * overlap dialled number needs more digits. */
1862 if (chan->hangupcause == AST_CAUSE_INVALID_NUMBER_FORMAT) {
1863 res = AST_PBX_INCOMPLETE;
1866 /* almost done, although the 'else' block is 400 lines */
1870 strcpy(pa.status, "ANSWER");
1871 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1872 /* Ah ha! Someone answered within the desired timeframe. Of course after this
1873 we will always return with -1 so that it is hung up properly after the
1875 hanguptree(outgoing, peer, 1);
1877 /* If appropriate, log that we have a destination channel */
1879 ast_cdr_setdestchan(chan->cdr, peer->name);
1881 pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", peer->name);
1883 ast_channel_lock(peer);
1884 number = pbx_builtin_getvar_helper(peer, "DIALEDPEERNUMBER");
1887 pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
1888 ast_channel_unlock(peer);
1890 if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
1891 ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
1892 ast_channel_sendurl( peer, args.url );
1894 if ( (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
1895 if (do_privacy(chan, peer, &opts, opt_args, &pa)) {
1900 if (!ast_test_flag64(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
1904 /* Start autoservice on the other chan */
1905 res = ast_autoservice_start(chan);
1906 /* Now Stream the File */
1908 res = ast_streamfile(peer, opt_args[OPT_ARG_ANNOUNCE], peer->language);
1910 digit = ast_waitstream(peer, AST_DIGIT_ANY);
1912 /* Ok, done. stop autoservice */
1913 res = ast_autoservice_stop(chan);
1914 if (digit > 0 && !res)
1915 res = ast_senddigit(chan, digit, 0);
1921 if (chan && peer && ast_test_flag64(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
1922 replace_macro_delimiter(opt_args[OPT_ARG_GOTO]);
1923 ast_parseable_goto(chan, opt_args[OPT_ARG_GOTO]);
1924 /* peer goes to the same context and extension as chan, so just copy info from chan*/
1925 ast_copy_string(peer->context, chan->context, sizeof(peer->context));
1926 ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
1927 peer->priority = chan->priority + 2;
1928 ast_pbx_start(peer);
1929 hanguptree(outgoing, NULL, ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0);
1936 if (ast_test_flag64(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
1937 struct ast_app *theapp;
1938 const char *macro_result;
1940 res = ast_autoservice_start(chan);
1942 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
1946 theapp = pbx_findapp("Macro");
1948 if (theapp && !res) { /* XXX why check res here ? */
1949 /* Set peer->exten and peer->context so that MACRO_EXTEN and MACRO_CONTEXT get set */
1950 ast_copy_string(peer->context, chan->context, sizeof(peer->context));
1951 ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
1953 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
1954 res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
1955 ast_debug(1, "Macro exited with status %d\n", res);
1958 ast_log(LOG_ERROR, "Could not find application Macro\n");
1962 if (ast_autoservice_stop(chan) < 0) {
1963 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
1967 ast_channel_lock(peer);
1969 if (!res && (macro_result = pbx_builtin_getvar_helper(peer, "MACRO_RESULT"))) {
1970 char *macro_transfer_dest;
1972 if (!strcasecmp(macro_result, "BUSY")) {
1973 ast_copy_string(pa.status, macro_result, sizeof(pa.status));
1974 ast_set_flag64(peerflags, OPT_GO_ON);
1976 } else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
1977 ast_copy_string(pa.status, macro_result, sizeof(pa.status));
1978 ast_set_flag64(peerflags, OPT_GO_ON);
1980 } else if (!strcasecmp(macro_result, "CONTINUE")) {
1981 /* hangup peer and keep chan alive assuming the macro has changed
1982 the context / exten / priority or perhaps
1983 the next priority in the current exten is desired.
1985 ast_set_flag64(peerflags, OPT_GO_ON);
1987 } else if (!strcasecmp(macro_result, "ABORT")) {
1988 /* Hangup both ends unless the caller has the g flag */
1990 } else if (!strncasecmp(macro_result, "GOTO:", 5) && (macro_transfer_dest = ast_strdupa(macro_result + 5))) {
1992 /* perform a transfer to a new extension */
1993 if (strchr(macro_transfer_dest, '^')) { /* context^exten^priority*/
1994 replace_macro_delimiter(macro_transfer_dest);
1995 if (!ast_parseable_goto(chan, macro_transfer_dest))
1996 ast_set_flag64(peerflags, OPT_GO_ON);
2001 ast_channel_unlock(peer);
2004 if (ast_test_flag64(&opts, OPT_CALLEE_GOSUB) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GOSUB])) {
2005 struct ast_app *theapp;
2006 const char *gosub_result;
2007 char *gosub_args, *gosub_argstart;
2010 res9 = ast_autoservice_start(chan);
2012 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
2016 theapp = pbx_findapp("Gosub");
2018 if (theapp && !res9) {
2019 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GOSUB]);
2021 /* Set where we came from */
2022 ast_copy_string(peer->context, "app_dial_gosub_virtual_context", sizeof(peer->context));
2023 ast_copy_string(peer->exten, "s", sizeof(peer->exten));
2026 gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], ',');
2027 if (gosub_argstart) {
2028 *gosub_argstart = 0;
2029 if (asprintf(&gosub_args, "%s,s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], gosub_argstart + 1) < 0) {
2030 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
2033 *gosub_argstart = ',';
2035 if (asprintf(&gosub_args, "%s,s,1", opt_args[OPT_ARG_CALLEE_GOSUB]) < 0) {
2036 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
2042 res9 = pbx_exec(peer, theapp, gosub_args);
2044 struct ast_pbx_args args;
2045 /* A struct initializer fails to compile for this case ... */
2046 memset(&args, 0, sizeof(args));
2047 args.no_hangup_chan = 1;
2048 ast_pbx_run_args(peer, &args);
2050 ast_free(gosub_args);
2051 ast_debug(1, "Gosub exited with status %d\n", res9);
2053 ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
2057 ast_log(LOG_ERROR, "Could not find application Gosub\n");
2061 if (ast_autoservice_stop(chan) < 0) {
2062 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
2066 ast_channel_lock(peer);
2068 if (!res9 && (gosub_result = pbx_builtin_getvar_helper(peer, "GOSUB_RESULT"))) {
2069 char *gosub_transfer_dest;
2071 if (!strcasecmp(gosub_result, "BUSY")) {
2072 ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
2073 ast_set_flag64(peerflags, OPT_GO_ON);
2075 } else if (!strcasecmp(gosub_result, "CONGESTION") || !strcasecmp(gosub_result, "CHANUNAVAIL")) {
2076 ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
2077 ast_set_flag64(peerflags, OPT_GO_ON);
2079 } else if (!strcasecmp(gosub_result, "CONTINUE")) {
2080 /* hangup peer and keep chan alive assuming the macro has changed
2081 the context / exten / priority or perhaps
2082 the next priority in the current exten is desired.
2084 ast_set_flag64(peerflags, OPT_GO_ON);
2086 } else if (!strcasecmp(gosub_result, "ABORT")) {
2087 /* Hangup both ends unless the caller has the g flag */
2089 } else if (!strncasecmp(gosub_result, "GOTO:", 5) && (gosub_transfer_dest = ast_strdupa(gosub_result + 5))) {
2091 /* perform a transfer to a new extension */
2092 if (strchr(gosub_transfer_dest, '^')) { /* context^exten^priority*/
2093 replace_macro_delimiter(gosub_transfer_dest);
2094 if (!ast_parseable_goto(chan, gosub_transfer_dest))
2095 ast_set_flag64(peerflags, OPT_GO_ON);
2100 ast_channel_unlock(peer);
2104 if (!ast_tvzero(calldurationlimit)) {
2105 struct timeval whentohangup = calldurationlimit;
2106 peer->whentohangup = ast_tvadd(ast_tvnow(), whentohangup);
2108 if (!ast_strlen_zero(dtmfcalled)) {
2109 ast_verb(3, "Sending DTMF '%s' to the called party.\n", dtmfcalled);
2110 res = ast_dtmf_stream(peer, chan, dtmfcalled, 250, 0);
2112 if (!ast_strlen_zero(dtmfcalling)) {
2113 ast_verb(3, "Sending DTMF '%s' to the calling party.\n", dtmfcalling);
2114 res = ast_dtmf_stream(chan, peer, dtmfcalling, 250, 0);
2118 if (res) { /* some error */
2121 if (ast_test_flag64(peerflags, OPT_CALLEE_TRANSFER))
2122 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
2123 if (ast_test_flag64(peerflags, OPT_CALLER_TRANSFER))
2124 ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
2125 if (ast_test_flag64(peerflags, OPT_CALLEE_HANGUP))
2126 ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
2127 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP))
2128 ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
2129 if (ast_test_flag64(peerflags, OPT_CALLEE_MONITOR))
2130 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
2131 if (ast_test_flag64(peerflags, OPT_CALLER_MONITOR))
2132 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
2133 if (ast_test_flag64(peerflags, OPT_CALLEE_PARK))
2134 ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
2135 if (ast_test_flag64(peerflags, OPT_CALLER_PARK))
2136 ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
2137 if (ast_test_flag64(peerflags, OPT_CALLEE_MIXMONITOR))
2138 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMIXMON);
2139 if (ast_test_flag64(peerflags, OPT_CALLER_MIXMONITOR))
2140 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMIXMON);
2141 if (ast_test_flag64(peerflags, OPT_GO_ON))
2142 ast_set_flag(&(config.features_caller), AST_FEATURE_NO_H_EXTEN);
2144 config.end_bridge_callback = end_bridge_callback;
2145 config.end_bridge_callback_data = chan;
2146 config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
2151 } else if (sentringing) {
2153 ast_indicate(chan, -1);
2155 /* Be sure no generators are left on it */
2156 ast_deactivate_generator(chan);
2157 /* Make sure channels are compatible */
2158 res = ast_channel_make_compatible(chan, peer);
2160 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
2166 struct oprmode oprmode;
2168 oprmode.peer = peer;
2169 oprmode.mode = opermode;
2171 ast_channel_setoption(chan, AST_OPTION_OPRMODE, &oprmode, sizeof(oprmode), 0);
2173 res = ast_bridge_call(chan, peer, &config);
2176 strcpy(peer->context, chan->context);
2178 if (ast_test_flag64(&opts, OPT_PEER_H) && ast_exists_extension(peer, peer->context, "h", 1, peer->cid.cid_num)) {
2183 strcpy(peer->exten, "h");
2185 autoloopflag = ast_test_flag(peer, AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
2186 ast_set_flag(peer, AST_FLAG_IN_AUTOLOOP);
2188 while ((res9 = ast_spawn_extension(peer, peer->context, peer->exten, peer->priority, peer->cid.cid_num, &found, 1)) == 0)
2191 if (found && res9) {
2192 /* Something bad happened, or a hangup has been requested. */
2193 ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
2194 ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
2196 ast_set2_flag(peer, autoloopflag, AST_FLAG_IN_AUTOLOOP); /* set it back the way it was */
2198 if (!ast_check_hangup(peer) && ast_test_flag64(&opts, OPT_CALLEE_GO_ON) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GO_ON])) {
2199 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GO_ON]);
2200 ast_parseable_goto(peer, opt_args[OPT_ARG_CALLEE_GO_ON]);
2201 ast_pbx_start(peer);
2203 if (!ast_check_hangup(chan))
2204 chan->hangupcause = peer->hangupcause;
2212 } else if (sentringing) {
2214 ast_indicate(chan, -1);
2216 ast_channel_early_bridge(chan, NULL);
2217 hanguptree(outgoing, NULL, 0); /* In this case, there's no answer anywhere */
2218 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
2219 senddialendevent(chan, pa.status);
2220 ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
2222 if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_INCOMPLETE)) {
2223 if (!ast_tvzero(calldurationlimit))
2224 memset(&chan->whentohangup, 0, sizeof(chan->whentohangup));
2229 if (config.warning_sound) {
2230 ast_free((char *)config.warning_sound);
2232 if (config.end_sound) {
2233 ast_free((char *)config.end_sound);
2235 if (config.start_sound) {
2236 ast_free((char *)config.start_sound);
2241 static int dial_exec(struct ast_channel *chan, void *data)
2243 struct ast_flags64 peerflags;
2245 memset(&peerflags, 0, sizeof(peerflags));
2247 return dial_exec_full(chan, data, &peerflags, NULL);
2250 static int retrydial_exec(struct ast_channel *chan, void *data)
2253 const char *context = NULL;
2254 int sleepms = 0, loops = 0, res = -1;
2255 struct ast_flags64 peerflags = { 0, };
2256 AST_DECLARE_APP_ARGS(args,
2257 AST_APP_ARG(announce);
2259 AST_APP_ARG(retries);
2260 AST_APP_ARG(dialdata);
2263 if (ast_strlen_zero(data)) {
2264 ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
2268 parse = ast_strdupa(data);
2269 AST_STANDARD_APP_ARGS(args, parse);
2271 if (!ast_strlen_zero(args.sleep) && (sleepms = atoi(args.sleep)))
2274 if (!ast_strlen_zero(args.retries)) {
2275 loops = atoi(args.retries);
2278 if (!args.dialdata) {
2279 ast_log(LOG_ERROR, "%s requires a 4th argument (dialdata)\n", rapp);
2287 loops = -1; /* run forever */
2289 ast_channel_lock(chan);
2290 context = pbx_builtin_getvar_helper(chan, "EXITCONTEXT");
2291 context = !ast_strlen_zero(context) ? ast_strdupa(context) : NULL;
2292 ast_channel_unlock(chan);
2298 chan->data = "Retrying";
2299 if (ast_test_flag(chan, AST_FLAG_MOH))
2302 res = dial_exec_full(chan, args.dialdata, &peerflags, &continue_exec);
2307 if (ast_test_flag64(&peerflags, OPT_DTMF_EXIT)) {
2308 if (!ast_strlen_zero(args.announce)) {
2309 if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
2310 if (!(res = ast_streamfile(chan, args.announce, chan->language)))
2311 ast_waitstream(chan, AST_DIGIT_ANY);
2313 ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
2315 if (!res && sleepms) {
2316 if (!ast_test_flag(chan, AST_FLAG_MOH))
2317 ast_moh_start(chan, NULL, NULL);
2318 res = ast_waitfordigit(chan, sleepms);
2321 if (!ast_strlen_zero(args.announce)) {
2322 if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
2323 if (!(res = ast_streamfile(chan, args.announce, chan->language)))
2324 res = ast_waitstream(chan, "");
2326 ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
2329 if (!ast_test_flag(chan, AST_FLAG_MOH))
2330 ast_moh_start(chan, NULL, NULL);
2332 res = ast_waitfordigit(chan, sleepms);
2337 if (res < 0 || res == AST_PBX_INCOMPLETE) {
2339 } else if (res > 0) { /* Trying to send the call elsewhere (1 digit ext) */
2340 if (onedigit_goto(chan, context, (char) res, 1)) {
2352 if (ast_test_flag(chan, AST_FLAG_MOH))
2358 static int unload_module(void)
2361 struct ast_context *con;
2363 res = ast_unregister_application(app);
2364 res |= ast_unregister_application(rapp);
2366 if ((con = ast_context_find("app_dial_gosub_virtual_context"))) {
2367 ast_context_remove_extension2(con, "s", 1, NULL, 0);
2368 ast_context_destroy(con, "app_dial"); /* leave nothing behind */
2374 static int load_module(void)
2377 struct ast_context *con;
2379 con = ast_context_find_or_create(NULL, NULL, "app_dial_gosub_virtual_context", "app_dial");
2381 ast_log(LOG_ERROR, "Dial virtual context 'app_dial_gosub_virtual_context' does not exist and unable to create\n");
2383 ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "app_dial");
2385 res = ast_register_application_xml(app, dial_exec);
2386 res |= ast_register_application_xml(rapp, retrydial_exec);
2391 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialing Application");