Add incomplete matching to PBX code and app_dial
[asterisk-bristuff.git] / apps / app_dial.c
blob5730d99d7ff7d6dcb54de7012f47a2f3cdd706f6
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, 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.
19 /*! \file
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
28 /*** MODULEINFO
29 <depend>chan_local</depend>
30 ***/
33 #include "asterisk.h"
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <sys/time.h>
38 #include <sys/signal.h>
39 #include <sys/stat.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"
65 static char *app = "Dial";
67 static char *synopsis = "Place a call and connect to the current channel";
69 static char *descrip =
70 " Dial(Technology/resource[&Tech2/resource2...][,timeout][,options][,URL]):\n"
71 "This application will place calls to one or more specified channels. As soon\n"
72 "as one of the requested channels answers, the originating channel will be\n"
73 "answered, if it has not already been answered. These two channels will then\n"
74 "be active in a bridged call. All other channels that were requested will then\n"
75 "be hung up.\n"
76 " Unless there is a timeout specified, the Dial application will wait\n"
77 "indefinitely until one of the called channels answers, the user hangs up, or\n"
78 "if all of the called channels are busy or unavailable. Dialplan executing will\n"
79 "continue if no requested channels can be called, or if the timeout expires.\n\n"
80 " This application sets the following channel variables upon completion:\n"
81 " DIALEDTIME - This is the time from dialing a channel until when it\n"
82 " is disconnected.\n"
83 " ANSWEREDTIME - This is the amount of time for actual call.\n"
84 " DIALSTATUS - This is the status of the call:\n"
85 " CHANUNAVAIL | CONGESTION | NOANSWER | BUSY | ANSWER | CANCEL\n"
86 " DONTCALL | TORTURE | INVALIDARGS\n"
87 " For the Privacy and Screening Modes, the DIALSTATUS variable will be set to\n"
88 "DONTCALL if the called party chooses to send the calling party to the 'Go Away'\n"
89 "script. The DIALSTATUS variable will be set to TORTURE if the called party\n"
90 "wants to send the caller to the 'torture' script.\n"
91 " This application will report normal termination if the originating channel\n"
92 "hangs up, or if the call is bridged and either of the parties in the bridge\n"
93 "ends the call.\n"
94 " The optional URL will be sent to the called party if the channel supports it.\n"
95 " If the OUTBOUND_GROUP variable is set, all peer channels created by this\n"
96 "application will be put into that group (as in Set(GROUP()=...).\n"
97 " If the OUTBOUND_GROUP_ONCE variable is set, all peer channels created by this\n"
98 "application will be put into that group (as in Set(GROUP()=...). Unlike OUTBOUND_GROUP,\n"
99 "however, the variable will be unset after use.\n\n"
100 " Options:\n"
101 " A(x) - Play an announcement to the called party, using 'x' as the file.\n"
102 " C - Reset the CDR for this call.\n"
103 " c - If DIAL cancels this call, always set the flag to tell the channel\n"
104 " driver that the call is answered elsewhere.\n"
105 " d - Allow the calling user to dial a 1 digit extension while waiting for\n"
106 " a call to be answered. Exit to that extension if it exists in the\n"
107 " current context, or the context defined in the EXITCONTEXT variable,\n"
108 " if it exists.\n"
109 " D([called][:calling]) - Send the specified DTMF strings *after* the called\n"
110 " party has answered, but before the call gets bridged. The 'called'\n"
111 " DTMF string is sent to the called party, and the 'calling' DTMF\n"
112 " string is sent to the calling party. Both parameters can be used\n"
113 " alone.\n"
114 " e - execute the 'h' extension for peer after the call ends\n"
115 " f - Force the callerid of the *calling* channel to be set as the\n"
116 " extension associated with the channel using a dialplan 'hint'.\n"
117 " For example, some PSTNs do not allow CallerID to be set to anything\n"
118 " other than the number assigned to the caller.\n"
119 " F(context^exten^pri) - When the caller hangs up, transfer the called party\n"
120 " to the specified context and extension and continue execution.\n"
121 " g - Proceed with dialplan execution at the current extension if the\n"
122 " destination channel hangs up.\n"
123 " G(context^exten^pri) - If the call is answered, transfer the calling party to\n"
124 " the specified priority and the called party to the specified priority+1.\n"
125 " Optionally, an extension, or extension and context may be specified. \n"
126 " Otherwise, the current extension is used. You cannot use any additional\n"
127 " action post answer options in conjunction with this option.\n"
128 " h - Allow the called party to hang up by sending the '*' DTMF digit.\n"
129 " H - Allow the calling party to hang up by hitting the '*' DTMF digit.\n"
130 " i - Asterisk will ignore any forwarding requests it may receive on this\n"
131 " dial attempt.\n"
132 " k - Allow the called party to enable parking of the call by sending\n"
133 " the DTMF sequence defined for call parking in features.conf.\n"
134 " K - Allow the calling party to enable parking of the call by sending\n"
135 " the DTMF sequence defined for call parking in features.conf.\n"
136 " L(x[:y][:z]) - Limit the call to 'x' ms. Play a warning when 'y' ms are\n"
137 " left. Repeat the warning every 'z' ms. The following special\n"
138 " variables can be used with this option:\n"
139 " * LIMIT_PLAYAUDIO_CALLER yes|no (default yes)\n"
140 " Play sounds to the caller.\n"
141 " * LIMIT_PLAYAUDIO_CALLEE yes|no\n"
142 " Play sounds to the callee.\n"
143 " * LIMIT_TIMEOUT_FILE File to play when time is up.\n"
144 " * LIMIT_CONNECT_FILE File to play when call begins.\n"
145 " * LIMIT_WARNING_FILE File to play as warning if 'y' is defined.\n"
146 " The default is to say the time remaining.\n"
147 " m([class]) - Provide hold music to the calling party until a requested\n"
148 " channel answers. A specific MusicOnHold class can be\n"
149 " specified.\n"
150 " M(x[^arg]) - Execute the Macro for the *called* channel before connecting\n"
151 " to the calling channel. Arguments can be specified to the Macro\n"
152 " using '^' as a delimiter. The Macro can set the variable\n"
153 " MACRO_RESULT to specify the following actions after the Macro is\n"
154 " finished executing.\n"
155 " * ABORT Hangup both legs of the call.\n"
156 " * CONGESTION Behave as if line congestion was encountered.\n"
157 " * BUSY Behave as if a busy signal was encountered.\n"
158 " * CONTINUE Hangup the called party and allow the calling party\n"
159 " to continue dialplan execution at the next priority.\n"
160 " * GOTO:<context>^<exten>^<priority> - Transfer the call to the\n"
161 " specified priority. Optionally, an extension, or\n"
162 " extension and priority can be specified.\n"
163 " You cannot use any additional action post answer options in conjunction\n"
164 " with this option. Also, pbx services are not run on the peer (called) channel,\n"
165 " so you will not be able to set timeouts via the TIMEOUT() function in this macro.\n"
166 " n - This option is a modifier for the screen/privacy mode. It specifies\n"
167 " that no introductions are to be saved in the priv-callerintros\n"
168 " directory.\n"
169 " N - This option is a modifier for the screen/privacy mode. It specifies\n"
170 " that if callerID is present, do not screen the call.\n"
171 " o - Specify that the CallerID that was present on the *calling* channel\n"
172 " be set as the CallerID on the *called* channel. This was the\n"
173 " behavior of Asterisk 1.0 and earlier.\n"
174 " O([x]) - \"Operator Services\" mode (Zaptel channel to Zaptel channel\n"
175 " only, if specified on non-Zaptel interface, it will be ignored).\n"
176 " When the destination answers (presumably an operator services\n"
177 " station), the originator no longer has control of their line.\n"
178 " They may hang up, but the switch will not release their line\n"
179 " until the destination party hangs up (the operator). Specified\n"
180 " without an arg, or with 1 as an arg, the originator hanging up\n"
181 " will cause the phone to ring back immediately. With a 2 specified,\n"
182 " when the \"operator\" flashes the trunk, it will ring their phone\n"
183 " back.\n"
184 " p - This option enables screening mode. This is basically Privacy mode\n"
185 " without memory.\n"
186 " P([x]) - Enable privacy mode. Use 'x' as the family/key in the database if\n"
187 " it is provided. The current extension is used if a database\n"
188 " family/key is not specified.\n"
189 " r - Indicate ringing to the calling party. Pass no audio to the calling\n"
190 " party until the called channel has answered.\n"
191 " S(x) - Hang up the call after 'x' seconds *after* the called party has\n"
192 " answered the call.\n"
193 " t - Allow the called party to transfer the calling party by sending the\n"
194 " DTMF sequence defined in features.conf.\n"
195 " T - Allow the calling party to transfer the called party by sending the\n"
196 " DTMF sequence defined in features.conf.\n"
197 " U(x[^arg]) - Execute via Gosub the routine 'x' for the *called* channel before connecting\n"
198 " to the calling channel. Arguments can be specified to the Gosub\n"
199 " using '^' as a delimiter. The Gosub routine can set the variable\n"
200 " GOSUB_RESULT to specify the following actions after the Gosub returns.\n"
201 " * ABORT Hangup both legs of the call.\n"
202 " * CONGESTION Behave as if line congestion was encountered.\n"
203 " * BUSY Behave as if a busy signal was encountered.\n"
204 " * CONTINUE Hangup the called party and allow the calling party\n"
205 " to continue dialplan execution at the next priority.\n"
206 " * GOTO:<context>^<exten>^<priority> - Transfer the call to the\n"
207 " specified priority. Optionally, an extension, or\n"
208 " extension and priority can be specified.\n"
209 " You cannot use any additional action post answer options in conjunction\n"
210 " with this option. Also, pbx services are not run on the peer (called) channel,\n"
211 " so you will not be able to set timeouts via the TIMEOUT() function in this routine.\n"
212 " w - Allow the called party to enable recording of the call by sending\n"
213 " the DTMF sequence defined for one-touch recording in features.conf.\n"
214 " W - Allow the calling party to enable recording of the call by sending\n"
215 " the DTMF sequence defined for one-touch recording in features.conf.\n"
216 " x - Allow the called party to enable recording of the call by sending\n"
217 " the DTMF sequence defined for one-touch automixmonitor in features.conf\n"
218 " X - Allow the calling party to enable recording of the call by sending\n"
219 " the DTMF sequence defined for one-touch automixmonitor in features.conf\n";
221 /* RetryDial App by Anthony Minessale II <anthmct@yahoo.com> Jan/2005 */
222 static char *rapp = "RetryDial";
223 static char *rsynopsis = "Place a call, retrying on failure allowing optional exit extension.";
224 static char *rdescrip =
225 " RetryDial(announce,sleep,retries,dialargs): This application will attempt to\n"
226 "place a call using the normal Dial application. If no channel can be reached,\n"
227 "the 'announce' file will be played. Then, it will wait 'sleep' number of\n"
228 "seconds before retying the call. After 'retires' number of attempts, the\n"
229 "calling channel will continue at the next priority in the dialplan. If the\n"
230 "'retries' setting is set to 0, this application will retry endlessly.\n"
231 " While waiting to retry a call, a 1 digit extension may be dialed. If that\n"
232 "extension exists in either the context defined in ${EXITCONTEXT} or the current\n"
233 "one, The call will jump to that extension immediately.\n"
234 " The 'dialargs' are specified in the same format that arguments are provided\n"
235 "to the Dial application.\n";
237 enum {
238 OPT_ANNOUNCE = (1 << 0),
239 OPT_RESETCDR = (1 << 1),
240 OPT_DTMF_EXIT = (1 << 2),
241 OPT_SENDDTMF = (1 << 3),
242 OPT_FORCECLID = (1 << 4),
243 OPT_GO_ON = (1 << 5),
244 OPT_CALLEE_HANGUP = (1 << 6),
245 OPT_CALLER_HANGUP = (1 << 7),
246 OPT_DURATION_LIMIT = (1 << 9),
247 OPT_MUSICBACK = (1 << 10),
248 OPT_CALLEE_MACRO = (1 << 11),
249 OPT_SCREEN_NOINTRO = (1 << 12),
250 OPT_SCREEN_NOCLID = (1 << 13),
251 OPT_ORIGINAL_CLID = (1 << 14),
252 OPT_SCREENING = (1 << 15),
253 OPT_PRIVACY = (1 << 16),
254 OPT_RINGBACK = (1 << 17),
255 OPT_DURATION_STOP = (1 << 18),
256 OPT_CALLEE_TRANSFER = (1 << 19),
257 OPT_CALLER_TRANSFER = (1 << 20),
258 OPT_CALLEE_MONITOR = (1 << 21),
259 OPT_CALLER_MONITOR = (1 << 22),
260 OPT_GOTO = (1 << 23),
261 OPT_OPERMODE = (1 << 24),
262 OPT_CALLEE_PARK = (1 << 25),
263 OPT_CALLER_PARK = (1 << 26),
264 OPT_IGNORE_FORWARDING = (1 << 27),
265 OPT_CALLEE_GOSUB = (1 << 28),
266 OPT_CALLEE_MIXMONITOR = (1 << 29),
267 OPT_CALLER_MIXMONITOR = (1 << 30),
270 #define DIAL_STILLGOING (1 << 31)
271 #define DIAL_NOFORWARDHTML ((uint64_t)1 << 32) /* flags are now 64 bits, so keep it up! */
272 #define OPT_CANCEL_ELSEWHERE ((uint64_t)1 << 33)
273 #define OPT_PEER_H ((uint64_t)1 << 34)
274 #define OPT_CALLEE_GO_ON ((uint64_t)1 << 35)
276 enum {
277 OPT_ARG_ANNOUNCE = 0,
278 OPT_ARG_SENDDTMF,
279 OPT_ARG_GOTO,
280 OPT_ARG_DURATION_LIMIT,
281 OPT_ARG_MUSICBACK,
282 OPT_ARG_CALLEE_MACRO,
283 OPT_ARG_CALLEE_GOSUB,
284 OPT_ARG_CALLEE_GO_ON,
285 OPT_ARG_PRIVACY,
286 OPT_ARG_DURATION_STOP,
287 OPT_ARG_OPERMODE,
288 /* note: this entry _MUST_ be the last one in the enum */
289 OPT_ARG_ARRAY_SIZE,
292 AST_APP_OPTIONS(dial_exec_options, BEGIN_OPTIONS
293 AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
294 AST_APP_OPTION('C', OPT_RESETCDR),
295 AST_APP_OPTION('c', OPT_CANCEL_ELSEWHERE),
296 AST_APP_OPTION('d', OPT_DTMF_EXIT),
297 AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
298 AST_APP_OPTION('e', OPT_PEER_H),
299 AST_APP_OPTION('f', OPT_FORCECLID),
300 AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
301 AST_APP_OPTION('g', OPT_GO_ON),
302 AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
303 AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
304 AST_APP_OPTION('H', OPT_CALLER_HANGUP),
305 AST_APP_OPTION('i', OPT_IGNORE_FORWARDING),
306 AST_APP_OPTION('k', OPT_CALLEE_PARK),
307 AST_APP_OPTION('K', OPT_CALLER_PARK),
308 AST_APP_OPTION('k', OPT_CALLEE_PARK),
309 AST_APP_OPTION('K', OPT_CALLER_PARK),
310 AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
311 AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
312 AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
313 AST_APP_OPTION('n', OPT_SCREEN_NOINTRO),
314 AST_APP_OPTION('N', OPT_SCREEN_NOCLID),
315 AST_APP_OPTION('o', OPT_ORIGINAL_CLID),
316 AST_APP_OPTION_ARG('O', OPT_OPERMODE, OPT_ARG_OPERMODE),
317 AST_APP_OPTION('p', OPT_SCREENING),
318 AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
319 AST_APP_OPTION('r', OPT_RINGBACK),
320 AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
321 AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
322 AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
323 AST_APP_OPTION_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
324 AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
325 AST_APP_OPTION('W', OPT_CALLER_MONITOR),
326 AST_APP_OPTION('x', OPT_CALLEE_MIXMONITOR),
327 AST_APP_OPTION('X', OPT_CALLER_MIXMONITOR),
328 END_OPTIONS );
330 #define CAN_EARLY_BRIDGE(flags) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
331 OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
332 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK | OPT_CALLER_PARK))
335 * The list of active channels
337 struct chanlist {
338 struct chanlist *next;
339 struct ast_channel *chan;
340 uint64_t flags;
344 static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception, int answered_elsewhere)
346 /* Hang up a tree of stuff */
347 struct chanlist *oo;
348 while (outgoing) {
349 /* Hangup any existing lines we have open */
350 if (outgoing->chan && (outgoing->chan != exception)) {
351 if (answered_elsewhere)
352 ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
353 ast_hangup(outgoing->chan);
355 oo = outgoing;
356 outgoing = outgoing->next;
357 ast_free(oo);
361 #define AST_MAX_WATCHERS 256
364 * argument to handle_cause() and other functions.
366 struct cause_args {
367 struct ast_channel *chan;
368 int busy;
369 int congestion;
370 int nochan;
373 static void handle_cause(int cause, struct cause_args *num)
375 struct ast_cdr *cdr = num->chan->cdr;
377 switch(cause) {
378 case AST_CAUSE_BUSY:
379 if (cdr)
380 ast_cdr_busy(cdr);
381 num->busy++;
382 break;
384 case AST_CAUSE_CONGESTION:
385 if (cdr)
386 ast_cdr_failed(cdr);
387 num->congestion++;
388 break;
390 case AST_CAUSE_UNREGISTERED:
391 if (cdr)
392 ast_cdr_failed(cdr);
393 num->nochan++;
394 break;
396 case AST_CAUSE_NORMAL_CLEARING:
397 break;
399 default:
400 num->nochan++;
401 break;
405 /* free the buffer if allocated, and set the pointer to the second arg */
406 #define S_REPLACE(s, new_val) \
407 do { \
408 if (s) \
409 ast_free(s); \
410 s = (new_val); \
411 } while (0)
413 static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri)
415 char rexten[2] = { exten, '\0' };
417 if (context) {
418 if (!ast_goto_if_exists(chan, context, rexten, pri))
419 return 1;
420 } else {
421 if (!ast_goto_if_exists(chan, chan->context, rexten, pri))
422 return 1;
423 else if (!ast_strlen_zero(chan->macrocontext)) {
424 if (!ast_goto_if_exists(chan, chan->macrocontext, rexten, pri))
425 return 1;
428 return 0;
432 static const char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
434 const char *context = S_OR(chan->macrocontext, chan->context);
435 const char *exten = S_OR(chan->macroexten, chan->exten);
437 return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
440 static void senddialevent(struct ast_channel *src, struct ast_channel *dst, const char *dialstring)
442 manager_event(EVENT_FLAG_CALL, "Dial",
443 "SubEvent: Begin\r\n"
444 "Channel: %s\r\n"
445 "Destination: %s\r\n"
446 "CallerIDNum: %s\r\n"
447 "CallerIDName: %s\r\n"
448 "UniqueID: %s\r\n"
449 "DestUniqueID: %s\r\n"
450 "Dialstring: %s\r\n",
451 src->name, dst->name, S_OR(src->cid.cid_num, "<unknown>"),
452 S_OR(src->cid.cid_name, "<unknown>"), src->uniqueid,
453 dst->uniqueid, dialstring ? dialstring : "");
456 static void senddialendevent(const struct ast_channel *src, const char *dialstatus)
458 manager_event(EVENT_FLAG_CALL, "Dial",
459 "SubEvent: End\r\n"
460 "Channel: %s\r\n"
461 "UniqueID: %s\r\n"
462 "DialStatus: %s\r\n",
463 src->name, src->uniqueid, dialstatus);
467 * helper function for wait_for_answer()
469 * XXX this code is highly suspicious, as it essentially overwrites
470 * the outgoing channel without properly deleting it.
472 static void do_forward(struct chanlist *o,
473 struct cause_args *num, struct ast_flags64 *peerflags, int single)
475 char tmpchan[256];
476 struct ast_channel *original = o->chan;
477 struct ast_channel *c = o->chan; /* the winner */
478 struct ast_channel *in = num->chan; /* the input channel */
479 char *stuff;
480 char *tech;
481 int cause;
483 ast_copy_string(tmpchan, c->call_forward, sizeof(tmpchan));
484 if ((stuff = strchr(tmpchan, '/'))) {
485 *stuff++ = '\0';
486 tech = tmpchan;
487 } else {
488 const char *forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
489 snprintf(tmpchan, sizeof(tmpchan), "%s@%s", c->call_forward, forward_context ? forward_context : c->context);
490 stuff = tmpchan;
491 tech = "Local";
493 /* Before processing channel, go ahead and check for forwarding */
494 ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
495 /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
496 if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
497 ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
498 c = o->chan = NULL;
499 cause = AST_CAUSE_BUSY;
500 } else {
501 /* Setup parameters */
502 c = o->chan = ast_request(tech, in->nativeformats, stuff, &cause);
503 if (c) {
504 if (single)
505 ast_channel_make_compatible(o->chan, in);
506 ast_channel_inherit_variables(in, o->chan);
507 ast_channel_datastore_inherit(in, o->chan);
508 } else
509 ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
511 if (!c) {
512 ast_clear_flag64(o, DIAL_STILLGOING);
513 handle_cause(cause, num);
514 } else {
515 char *new_cid_num, *new_cid_name;
516 struct ast_channel *src;
518 ast_rtp_make_compatible(c, in, single);
519 if (ast_test_flag64(o, OPT_FORCECLID)) {
520 new_cid_num = ast_strdup(S_OR(in->macroexten, in->exten));
521 new_cid_name = NULL; /* XXX no name ? */
522 src = c; /* XXX possible bug in previous code, which used 'winner' ? it may have changed */
523 } else {
524 new_cid_num = ast_strdup(in->cid.cid_num);
525 new_cid_name = ast_strdup(in->cid.cid_name);
526 src = in;
528 ast_string_field_set(c, accountcode, src->accountcode);
529 c->cdrflags = src->cdrflags;
530 S_REPLACE(c->cid.cid_num, new_cid_num);
531 S_REPLACE(c->cid.cid_name, new_cid_name);
533 if (in->cid.cid_ani) { /* XXX or maybe unconditional ? */
534 S_REPLACE(c->cid.cid_ani, ast_strdup(in->cid.cid_ani));
536 S_REPLACE(c->cid.cid_rdnis, ast_strdup(S_OR(in->macroexten, in->exten)));
537 if (ast_call(c, tmpchan, 0)) {
538 ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
539 ast_clear_flag64(o, DIAL_STILLGOING);
540 ast_hangup(original);
541 c = o->chan = NULL;
542 num->nochan++;
543 } else {
544 senddialevent(in, c, stuff);
545 /* After calling, set callerid to extension */
546 if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
547 char cidname[AST_MAX_EXTENSION] = "";
548 ast_set_callerid(c, S_OR(in->macroexten, in->exten), get_cid_name(cidname, sizeof(cidname), in), NULL);
550 /* Hangup the original channel now, in case we needed it */
551 ast_hangup(original);
556 /* argument used for some functions. */
557 struct privacy_args {
558 int sentringing;
559 int privdb_val;
560 char privcid[256];
561 char privintro[1024];
562 char status[256];
565 static struct ast_channel *wait_for_answer(struct ast_channel *in,
566 struct chanlist *outgoing, int *to, struct ast_flags64 *peerflags,
567 struct privacy_args *pa,
568 const struct cause_args *num_in, int *result)
570 struct cause_args num = *num_in;
571 int prestart = num.busy + num.congestion + num.nochan;
572 int orig = *to;
573 struct ast_channel *peer = NULL;
574 /* single is set if only one destination is enabled */
575 int single = outgoing && !outgoing->next && !ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
576 #ifdef HAVE_EPOLL
577 struct chanlist *epollo;
578 #endif
580 if (single) {
581 /* Turn off hold music, etc */
582 ast_deactivate_generator(in);
583 /* If we are calling a single channel, make them compatible for in-band tone purpose */
584 ast_channel_make_compatible(outgoing->chan, in);
587 #ifdef HAVE_EPOLL
588 for (epollo = outgoing; epollo; epollo = epollo->next)
589 ast_poll_channel_add(in, epollo->chan);
590 #endif
592 while (*to && !peer) {
593 struct chanlist *o;
594 int pos = 0; /* how many channels do we handle */
595 int numlines = prestart;
596 struct ast_channel *winner;
597 struct ast_channel *watchers[AST_MAX_WATCHERS];
599 watchers[pos++] = in;
600 for (o = outgoing; o; o = o->next) {
601 /* Keep track of important channels */
602 if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
603 watchers[pos++] = o->chan;
604 numlines++;
606 if (pos == 1) { /* only the input channel is available */
607 if (numlines == (num.busy + num.congestion + num.nochan)) {
608 ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
609 if (num.busy)
610 strcpy(pa->status, "BUSY");
611 else if (num.congestion)
612 strcpy(pa->status, "CONGESTION");
613 else if (num.nochan)
614 strcpy(pa->status, "CHANUNAVAIL");
615 } else {
616 ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
618 *to = 0;
619 return NULL;
621 winner = ast_waitfor_n(watchers, pos, to);
622 for (o = outgoing; o; o = o->next) {
623 struct ast_frame *f;
624 struct ast_channel *c = o->chan;
626 if (c == NULL)
627 continue;
628 if (ast_test_flag64(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
629 if (!peer) {
630 ast_verb(3, "%s answered %s\n", c->name, in->name);
631 peer = c;
632 ast_copy_flags64(peerflags, o,
633 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
634 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
635 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
636 OPT_CALLEE_PARK | OPT_CALLER_PARK |
637 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
638 DIAL_NOFORWARDHTML);
639 ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
640 ast_copy_string(c->exten, "", sizeof(c->exten));
642 continue;
644 if (c != winner)
645 continue;
646 /* here, o->chan == c == winner */
647 if (!ast_strlen_zero(c->call_forward)) {
648 do_forward(o, &num, peerflags, single);
649 continue;
651 f = ast_read(winner);
652 if (!f) {
653 in->hangupcause = c->hangupcause;
654 #ifdef HAVE_EPOLL
655 ast_poll_channel_del(in, c);
656 #endif
657 ast_hangup(c);
658 c = o->chan = NULL;
659 ast_clear_flag64(o, DIAL_STILLGOING);
660 handle_cause(in->hangupcause, &num);
661 continue;
663 if (f->frametype == AST_FRAME_CONTROL) {
664 switch(f->subclass) {
665 case AST_CONTROL_ANSWER:
666 /* This is our guy if someone answered. */
667 if (!peer) {
668 ast_verb(3, "%s answered %s\n", c->name, in->name);
669 peer = c;
670 ast_copy_flags64(peerflags, o,
671 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
672 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
673 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
674 OPT_CALLEE_PARK | OPT_CALLER_PARK |
675 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
676 DIAL_NOFORWARDHTML);
677 ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
678 ast_copy_string(c->exten, "", sizeof(c->exten));
679 if (CAN_EARLY_BRIDGE(peerflags))
680 /* Setup early bridge if appropriate */
681 ast_channel_early_bridge(in, peer);
683 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
684 in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
685 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
686 break;
687 case AST_CONTROL_BUSY:
688 ast_verb(3, "%s is busy\n", c->name);
689 in->hangupcause = c->hangupcause;
690 ast_hangup(c);
691 c = o->chan = NULL;
692 ast_clear_flag64(o, DIAL_STILLGOING);
693 handle_cause(AST_CAUSE_BUSY, &num);
694 break;
695 case AST_CONTROL_CONGESTION:
696 ast_verb(3, "%s is circuit-busy\n", c->name);
697 in->hangupcause = c->hangupcause;
698 ast_hangup(c);
699 c = o->chan = NULL;
700 ast_clear_flag64(o, DIAL_STILLGOING);
701 handle_cause(AST_CAUSE_CONGESTION, &num);
702 break;
703 case AST_CONTROL_RINGING:
704 ast_verb(3, "%s is ringing\n", c->name);
705 /* Setup early media if appropriate */
706 if (single && CAN_EARLY_BRIDGE(peerflags))
707 ast_channel_early_bridge(in, c);
708 if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK)) {
709 ast_indicate(in, AST_CONTROL_RINGING);
710 pa->sentringing++;
712 break;
713 case AST_CONTROL_PROGRESS:
714 ast_verb(3, "%s is making progress passing it to %s\n", c->name, in->name);
715 /* Setup early media if appropriate */
716 if (single && CAN_EARLY_BRIDGE(peerflags))
717 ast_channel_early_bridge(in, c);
718 if (!ast_test_flag64(outgoing, OPT_RINGBACK))
719 ast_indicate(in, AST_CONTROL_PROGRESS);
720 break;
721 case AST_CONTROL_VIDUPDATE:
722 ast_verb(3, "%s requested a video update, passing it to %s\n", c->name, in->name);
723 ast_indicate(in, AST_CONTROL_VIDUPDATE);
724 break;
725 case AST_CONTROL_SRCUPDATE:
726 ast_verb(3, "%s requested a source update, passing it to %s\n", c->name, in->name);
727 ast_indicate(in, AST_CONTROL_SRCUPDATE);
728 break;
729 case AST_CONTROL_PROCEEDING:
730 ast_verb(3, "%s is proceeding passing it to %s\n", c->name, in->name);
731 if (single && CAN_EARLY_BRIDGE(peerflags))
732 ast_channel_early_bridge(in, c);
733 if (!ast_test_flag64(outgoing, OPT_RINGBACK))
734 ast_indicate(in, AST_CONTROL_PROCEEDING);
735 break;
736 case AST_CONTROL_HOLD:
737 ast_verb(3, "Call on %s placed on hold\n", c->name);
738 ast_indicate(in, AST_CONTROL_HOLD);
739 break;
740 case AST_CONTROL_UNHOLD:
741 ast_verb(3, "Call on %s left from hold\n", c->name);
742 ast_indicate(in, AST_CONTROL_UNHOLD);
743 break;
744 case AST_CONTROL_OFFHOOK:
745 case AST_CONTROL_FLASH:
746 /* Ignore going off hook and flash */
747 break;
748 case -1:
749 if (!ast_test_flag64(outgoing, OPT_RINGBACK | OPT_MUSICBACK)) {
750 ast_verb(3, "%s stopped sounds\n", c->name);
751 ast_indicate(in, -1);
752 pa->sentringing = 0;
754 break;
755 default:
756 ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
758 } else if (single) {
759 switch (f->frametype) {
760 case AST_FRAME_VOICE:
761 case AST_FRAME_IMAGE:
762 case AST_FRAME_TEXT:
763 if (ast_write(in, f)) {
764 ast_log(LOG_WARNING, "Unable to write frame\n");
766 break;
767 case AST_FRAME_HTML:
768 if (!ast_test_flag64(outgoing, DIAL_NOFORWARDHTML) && ast_channel_sendhtml(in, f->subclass, f->data, f->datalen) == -1) {
769 ast_log(LOG_WARNING, "Unable to send URL\n");
771 break;
772 default:
773 break;
776 ast_frfree(f);
777 } /* end for */
778 if (winner == in) {
779 struct ast_frame *f = ast_read(in);
780 #if 0
781 if (f && (f->frametype != AST_FRAME_VOICE))
782 printf("Frame type: %d, %d\n", f->frametype, f->subclass);
783 else if (!f || (f->frametype != AST_FRAME_VOICE))
784 printf("Hangup received on %s\n", in->name);
785 #endif
786 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
787 /* Got hung up */
788 *to = -1;
789 strcpy(pa->status, "CANCEL");
790 ast_cdr_noanswer(in->cdr);
791 if (f) {
792 if (f->seqno)
793 in->hangupcause = f->seqno;
794 ast_frfree(f);
796 return NULL;
799 /* now f is guaranteed non-NULL */
800 if (f->frametype == AST_FRAME_DTMF) {
801 if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
802 const char *context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
803 if (onedigit_goto(in, context, (char) f->subclass, 1)) {
804 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
805 *to = 0;
806 ast_cdr_noanswer(in->cdr);
807 *result = f->subclass;
808 strcpy(pa->status, "CANCEL");
809 ast_frfree(f);
810 return NULL;
814 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) &&
815 (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
816 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
817 *to = 0;
818 strcpy(pa->status, "CANCEL");
819 ast_cdr_noanswer(in->cdr);
820 ast_frfree(f);
821 return NULL;
825 /* Forward HTML stuff */
826 if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML))
827 if (ast_channel_sendhtml(outgoing->chan, f->subclass, f->data, f->datalen) == -1)
828 ast_log(LOG_WARNING, "Unable to send URL\n");
830 if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF_BEGIN) || (f->frametype == AST_FRAME_DTMF_END))) {
831 if (ast_write(outgoing->chan, f))
832 ast_log(LOG_WARNING, "Unable to forward voice or dtmf\n");
834 if (single && (f->frametype == AST_FRAME_CONTROL) &&
835 ((f->subclass == AST_CONTROL_HOLD) ||
836 (f->subclass == AST_CONTROL_UNHOLD) ||
837 (f->subclass == AST_CONTROL_VIDUPDATE) ||
838 (f->subclass == AST_CONTROL_SRCUPDATE))) {
839 ast_verb(3, "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
840 ast_indicate_data(outgoing->chan, f->subclass, f->data, f->datalen);
842 ast_frfree(f);
844 if (!*to)
845 ast_verb(3, "Nobody picked up in %d ms\n", orig);
846 if (!*to || ast_check_hangup(in))
847 ast_cdr_noanswer(in->cdr);
850 #ifdef HAVE_EPOLL
851 for (epollo = outgoing; epollo; epollo = epollo->next) {
852 if (epollo->chan)
853 ast_poll_channel_del(in, epollo->chan);
855 #endif
857 return peer;
860 static void replace_macro_delimiter(char *s)
862 for (; *s; s++)
863 if (*s == '^')
864 *s = ',';
867 /* returns true if there is a valid privacy reply */
868 static int valid_priv_reply(struct ast_flags64 *opts, int res)
870 if (res < '1')
871 return 0;
872 if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
873 return 1;
874 if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
875 return 1;
876 return 0;
879 static int do_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
880 char *parse, unsigned int *calldurationlimit)
882 char *stringp = ast_strdupa(parse);
883 char *limit_str, *warning_str, *warnfreq_str;
884 const char *var;
885 int play_to_caller = 0, play_to_callee = 0;
886 int delta;
888 limit_str = strsep(&stringp, ":");
889 warning_str = strsep(&stringp, ":");
890 warnfreq_str = strsep(&stringp, ":");
892 config->timelimit = atol(limit_str);
893 if (warning_str)
894 config->play_warning = atol(warning_str);
895 if (warnfreq_str)
896 config->warning_freq = atol(warnfreq_str);
898 if (!config->timelimit) {
899 ast_log(LOG_WARNING, "Dial does not accept L(%s), hanging up.\n", limit_str);
900 config->timelimit = config->play_warning = config->warning_freq = 0;
901 config->warning_sound = NULL;
902 return -1; /* error */
903 } else if ( (delta = config->play_warning - config->timelimit) > 0) {
904 int w = config->warning_freq;
906 /* If the first warning is requested _after_ the entire call would end,
907 and no warning frequency is requested, then turn off the warning. If
908 a warning frequency is requested, reduce the 'first warning' time by
909 that frequency until it falls within the call's total time limit.
910 Graphically:
911 timelim->| delta |<-playwarning
912 0__________________|_________________|
913 | w | | | |
915 so the number of intervals to cut is 1+(delta-1)/w
918 if (w == 0) {
919 config->play_warning = 0;
920 } else {
921 config->play_warning -= w * ( 1 + (delta-1)/w );
922 if (config->play_warning < 1)
923 config->play_warning = config->warning_freq = 0;
927 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
928 play_to_caller = var ? ast_true(var) : 1;
930 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
931 play_to_callee = var ? ast_true(var) : 0;
933 if (!play_to_caller && !play_to_callee)
934 play_to_caller = 1;
936 var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
937 config->warning_sound = S_OR(var, "timeleft");
939 /* The code looking at config wants a NULL, not just "", to decide
940 * that the message should not be played, so we replace "" with NULL.
941 * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
942 * not found.
944 var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
945 config->end_sound = S_OR(var, NULL);
946 var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
947 config->start_sound = S_OR(var, NULL);
949 /* undo effect of S(x) in case they are both used */
950 *calldurationlimit = 0;
951 /* more efficient to do it like S(x) does since no advanced opts */
952 if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
953 *calldurationlimit = config->timelimit / 1000;
954 ast_verb(3, "Setting call duration limit to %d seconds.\n",
955 *calldurationlimit);
956 config->timelimit = play_to_caller = play_to_callee =
957 config->play_warning = config->warning_freq = 0;
958 } else {
959 ast_verb(3, "Limit Data for this call:\n");
960 ast_verb(4, "timelimit = %ld\n", config->timelimit);
961 ast_verb(4, "play_warning = %ld\n", config->play_warning);
962 ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
963 ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
964 ast_verb(4, "warning_freq = %ld\n", config->warning_freq);
965 ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
966 ast_verb(4, "warning_sound = %s\n", config->warning_sound);
967 ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
969 if (play_to_caller)
970 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
971 if (play_to_callee)
972 ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
973 return 0;
976 static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
977 struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
980 int res2;
981 int loopcount = 0;
983 /* Get the user's intro, store it in priv-callerintros/$CID,
984 unless it is already there-- this should be done before the
985 call is actually dialed */
987 /* all ring indications and moh for the caller has been halted as soon as the
988 target extension was picked up. We are going to have to kill some
989 time and make the caller believe the peer hasn't picked up yet */
991 if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
992 char *original_moh = ast_strdupa(chan->musicclass);
993 ast_indicate(chan, -1);
994 ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
995 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
996 ast_string_field_set(chan, musicclass, original_moh);
997 } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
998 ast_indicate(chan, AST_CONTROL_RINGING);
999 pa->sentringing++;
1002 /* Start autoservice on the other chan ?? */
1003 res2 = ast_autoservice_start(chan);
1004 /* Now Stream the File */
1005 for (loopcount = 0; loopcount < 3; loopcount++) {
1006 if (res2 && loopcount == 0) /* error in ast_autoservice_start() */
1007 break;
1008 if (!res2) /* on timeout, play the message again */
1009 res2 = ast_play_and_wait(peer, "priv-callpending");
1010 if (!valid_priv_reply(opts, res2))
1011 res2 = 0;
1012 /* priv-callpending script:
1013 "I have a caller waiting, who introduces themselves as:"
1015 if (!res2)
1016 res2 = ast_play_and_wait(peer, pa->privintro);
1017 if (!valid_priv_reply(opts, res2))
1018 res2 = 0;
1019 /* now get input from the called party, as to their choice */
1020 if (!res2) {
1021 /* XXX can we have both, or they are mutually exclusive ? */
1022 if (ast_test_flag64(opts, OPT_PRIVACY))
1023 res2 = ast_play_and_wait(peer, "priv-callee-options");
1024 if (ast_test_flag64(opts, OPT_SCREENING))
1025 res2 = ast_play_and_wait(peer, "screen-callee-options");
1027 /*! \page DialPrivacy Dial Privacy scripts
1028 \par priv-callee-options script:
1029 "Dial 1 if you wish this caller to reach you directly in the future,
1030 and immediately connect to their incoming call
1031 Dial 2 if you wish to send this caller to voicemail now and
1032 forevermore.
1033 Dial 3 to send this caller to the torture menus, now and forevermore.
1034 Dial 4 to send this caller to a simple "go away" menu, now and forevermore.
1035 Dial 5 to allow this caller to come straight thru to you in the future,
1036 but right now, just this once, send them to voicemail."
1037 \par screen-callee-options script:
1038 "Dial 1 if you wish to immediately connect to the incoming call
1039 Dial 2 if you wish to send this caller to voicemail.
1040 Dial 3 to send this caller to the torture menus.
1041 Dial 4 to send this caller to a simple "go away" menu.
1043 if (valid_priv_reply(opts, res2))
1044 break;
1045 /* invalid option */
1046 res2 = ast_play_and_wait(peer, "vm-sorry");
1049 if (ast_test_flag64(opts, OPT_MUSICBACK)) {
1050 ast_moh_stop(chan);
1051 } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
1052 ast_indicate(chan, -1);
1053 pa->sentringing = 0;
1055 ast_autoservice_stop(chan);
1056 if (ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
1057 /* map keypresses to various things, the index is res2 - '1' */
1058 static const char *_val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
1059 static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
1060 int i = res2 - '1';
1061 ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
1062 opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
1063 ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
1065 switch (res2) {
1066 case '1':
1067 break;
1068 case '2':
1069 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1070 break;
1071 case '3':
1072 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1073 break;
1074 case '4':
1075 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1076 break;
1077 case '5':
1078 /* XXX should we set status to DENY ? */
1079 if (ast_test_flag64(opts, OPT_PRIVACY))
1080 break;
1081 /* if not privacy, then 5 is the same as "default" case */
1082 default: /* bad input or -1 if failure to start autoservice */
1083 /* well, if the user messes up, ... he had his chance... What Is The Best Thing To Do? */
1084 /* well, there seems basically two choices. Just patch the caller thru immediately,
1085 or,... put 'em thru to voicemail. */
1086 /* since the callee may have hung up, let's do the voicemail thing, no database decision */
1087 ast_log(LOG_NOTICE, "privacy: no valid response from the callee. Sending the caller to voicemail, the callee isn't responding\n");
1088 /* XXX should we set status to DENY ? */
1089 /* XXX what about the privacy flags ? */
1090 break;
1093 if (res2 == '1') { /* the only case where we actually connect */
1094 /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll
1095 just clog things up, and it's not useful information, not being tied to a CID */
1096 if (strncmp(pa->privcid, "NOCALLERID", 10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO)) {
1097 ast_filedelete(pa->privintro, NULL);
1098 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1099 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1100 else
1101 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1103 return 0; /* the good exit path */
1104 } else {
1105 ast_hangup(peer); /* hang up on the callee -- he didn't want to talk anyway! */
1106 return -1;
1110 /*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
1111 static int setup_privacy_args(struct privacy_args *pa,
1112 struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
1114 char callerid[60];
1115 int res;
1116 char *l;
1117 int silencethreshold;
1119 if (!ast_strlen_zero(chan->cid.cid_num)) {
1120 l = ast_strdupa(chan->cid.cid_num);
1121 ast_shrink_phone_number(l);
1122 if (ast_test_flag64(opts, OPT_PRIVACY) ) {
1123 ast_verb(3, "Privacy DB is '%s', clid is '%s'\n", opt_args[OPT_ARG_PRIVACY], l);
1124 pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
1125 } else {
1126 ast_verb(3, "Privacy Screening, clid is '%s'\n", l);
1127 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1129 } else {
1130 char *tnam, *tn2;
1132 tnam = ast_strdupa(chan->name);
1133 /* clean the channel name so slashes don't try to end up in disk file name */
1134 for (tn2 = tnam; *tn2; tn2++) {
1135 if (*tn2 == '/') /* any other chars to be afraid of? */
1136 *tn2 = '=';
1138 ast_verb(3, "Privacy-- callerid is empty\n");
1140 snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
1141 l = callerid;
1142 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1145 ast_copy_string(pa->privcid, l, sizeof(pa->privcid));
1147 if (strncmp(pa->privcid, "NOCALLERID", 10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCLID)) {
1148 /* if callerid is set and OPT_SCREEN_NOCLID is set also */
1149 ast_verb(3, "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
1150 pa->privdb_val = AST_PRIVACY_ALLOW;
1151 } else if (ast_test_flag64(opts, OPT_SCREEN_NOCLID) && strncmp(pa->privcid, "NOCALLERID", 10) == 0) {
1152 ast_verb(3, "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
1155 if (pa->privdb_val == AST_PRIVACY_DENY) {
1156 ast_verb(3, "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
1157 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1158 return 0;
1159 } else if (pa->privdb_val == AST_PRIVACY_KILL) {
1160 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1161 return 0; /* Is this right? */
1162 } else if (pa->privdb_val == AST_PRIVACY_TORTURE) {
1163 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1164 return 0; /* is this right??? */
1165 } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN) {
1166 /* Get the user's intro, store it in priv-callerintros/$CID,
1167 unless it is already there-- this should be done before the
1168 call is actually dialed */
1170 /* make sure the priv-callerintros dir actually exists */
1171 snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
1172 if ((res = ast_mkdir(pa->privintro, 0755))) {
1173 ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
1174 return -1;
1177 snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
1178 if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
1179 /* the DELUX version of this code would allow this caller the
1180 option to hear and retape their previously recorded intro.
1182 } else {
1183 int duration; /* for feedback from play_and_wait */
1184 /* the file doesn't exist yet. Let the caller submit his
1185 vocal intro for posterity */
1186 /* priv-recordintro script:
1188 "At the tone, please say your name:"
1191 silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
1192 ast_answer(chan);
1193 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 */
1194 /* don't think we'll need a lock removed, we took care of
1195 conflicts by naming the pa.privintro file */
1196 if (res == -1) {
1197 /* Delete the file regardless since they hung up during recording */
1198 ast_filedelete(pa->privintro, NULL);
1199 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1200 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1201 else
1202 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1203 return -1;
1205 if (!ast_streamfile(chan, "vm-dialout", chan->language) )
1206 ast_waitstream(chan, "");
1209 return 1; /* success */
1212 static void set_dial_features(struct ast_flags64 *opts, struct ast_dial_features *features)
1214 struct ast_flags64 perm_opts = {.flags = 0};
1216 ast_copy_flags64(&perm_opts, opts,
1217 OPT_CALLER_TRANSFER | OPT_CALLER_PARK | OPT_CALLER_MONITOR | OPT_CALLER_MIXMONITOR | OPT_CALLER_HANGUP |
1218 OPT_CALLEE_TRANSFER | OPT_CALLEE_PARK | OPT_CALLEE_MONITOR | OPT_CALLEE_MIXMONITOR | OPT_CALLEE_HANGUP);
1220 memset(features->options, 0, sizeof(features->options));
1222 ast_app_options2str64(dial_exec_options, &perm_opts, features->options, sizeof(features->options));
1225 static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags64 *peerflags, int *continue_exec)
1227 int res = -1; /* default: error */
1228 char *rest, *cur; /* scan the list of destinations */
1229 struct chanlist *outgoing = NULL; /* list of destinations */
1230 struct ast_channel *peer;
1231 int to; /* timeout */
1232 struct cause_args num = { chan, 0, 0, 0 };
1233 int cause;
1234 char numsubst[256];
1235 char cidname[AST_MAX_EXTENSION] = "";
1237 struct ast_bridge_config config = { { 0, } };
1238 unsigned int calldurationlimit = 0;
1239 char *dtmfcalled = NULL, *dtmfcalling = NULL;
1240 struct privacy_args pa = {
1241 .sentringing = 0,
1242 .privdb_val = 0,
1243 .status = "INVALIDARGS",
1245 int sentringing = 0, moh = 0;
1246 const char *outbound_group = NULL;
1247 int result = 0;
1248 time_t start_time;
1249 char *parse;
1250 int opermode = 0;
1251 AST_DECLARE_APP_ARGS(args,
1252 AST_APP_ARG(peers);
1253 AST_APP_ARG(timeout);
1254 AST_APP_ARG(options);
1255 AST_APP_ARG(url);
1257 struct ast_flags64 opts = { 0, };
1258 char *opt_args[OPT_ARG_ARRAY_SIZE];
1259 struct ast_datastore *datastore = NULL;
1260 struct ast_datastore *ds_caller_features = NULL;
1261 struct ast_datastore *ds_callee_features = NULL;
1262 struct ast_dial_features *caller_features;
1263 int fulldial = 0, num_dialed = 0;
1265 if (ast_strlen_zero(data)) {
1266 ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
1267 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1268 return -1;
1271 parse = ast_strdupa(data);
1273 AST_STANDARD_APP_ARGS(args, parse);
1275 if (!ast_strlen_zero(args.options) &&
1276 ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
1277 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1278 goto done;
1281 if (ast_strlen_zero(args.peers)) {
1282 ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
1283 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1284 goto done;
1287 if (ast_test_flag64(&opts, OPT_OPERMODE)) {
1288 opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
1289 ast_verb(3, "Setting operator services mode to %d.\n", opermode);
1292 if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
1293 calldurationlimit = atoi(opt_args[OPT_ARG_DURATION_STOP]);
1294 if (!calldurationlimit) {
1295 ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
1296 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1297 goto done;
1299 ast_verb(3, "Setting call duration limit to %d seconds.\n", calldurationlimit);
1302 if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
1303 dtmfcalling = opt_args[OPT_ARG_SENDDTMF];
1304 dtmfcalled = strsep(&dtmfcalling, ":");
1307 if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
1308 if (do_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
1309 goto done;
1312 if (ast_test_flag64(&opts, OPT_RESETCDR) && chan->cdr)
1313 ast_cdr_reset(chan->cdr, NULL);
1314 if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
1315 opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
1317 if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
1318 res = setup_privacy_args(&pa, &opts, opt_args, chan);
1319 if (res <= 0)
1320 goto out;
1321 res = -1; /* reset default */
1324 if (continue_exec)
1325 *continue_exec = 0;
1327 /* If a channel group has been specified, get it for use when we create peer channels */
1328 if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
1329 outbound_group = ast_strdupa(outbound_group);
1330 pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
1331 } else {
1332 outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP");
1335 ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING);
1337 /* Create datastore for channel dial features for caller */
1338 if (!(ds_caller_features = ast_channel_datastore_alloc(&dial_features_info, NULL))) {
1339 ast_log(LOG_WARNING, "Unable to create channel datastore for dial features. Aborting!\n");
1340 goto out;
1343 if (!(caller_features = ast_malloc(sizeof(*caller_features)))) {
1344 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags. Aborting!\n");
1345 goto out;
1348 ast_copy_flags(&(caller_features->features_callee), &(config.features_caller), AST_FLAGS_ALL);
1349 caller_features->is_caller = 1;
1350 set_dial_features(&opts, caller_features);
1352 ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
1353 ds_caller_features->data = caller_features;
1355 ast_channel_lock(chan);
1356 ast_channel_datastore_add(chan, ds_caller_features);
1357 ast_channel_unlock(chan);
1359 /* loop through the list of dial destinations */
1360 rest = args.peers;
1361 while ((cur = strsep(&rest, "&")) ) {
1362 struct chanlist *tmp;
1363 struct ast_channel *tc; /* channel for this destination */
1364 /* Get a technology/[device:]number pair */
1365 char *number = cur;
1366 char *interface = ast_strdupa(number);
1367 char *tech = strsep(&number, "/");
1368 /* find if we already dialed this interface */
1369 struct ast_dialed_interface *di;
1370 struct ast_dial_features *callee_features;
1371 AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
1372 num_dialed++;
1373 if (!number) {
1374 ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
1375 goto out;
1377 if (!(tmp = ast_calloc(1, sizeof(*tmp))))
1378 goto out;
1379 if (opts.flags) {
1380 ast_copy_flags64(tmp, &opts,
1381 OPT_CANCEL_ELSEWHERE |
1382 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
1383 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
1384 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
1385 OPT_CALLEE_PARK | OPT_CALLER_PARK |
1386 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
1387 OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID);
1388 ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);
1390 ast_copy_string(numsubst, number, sizeof(numsubst));
1391 /* Request the peer */
1393 ast_channel_lock(chan);
1394 datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL);
1395 ast_channel_unlock(chan);
1397 if (datastore)
1398 dialed_interfaces = datastore->data;
1399 else {
1400 if (!(datastore = ast_channel_datastore_alloc(&dialed_interface_info, NULL))) {
1401 ast_log(LOG_WARNING, "Unable to create channel datastore for dialed interfaces. Aborting!\n");
1402 ast_free(tmp);
1403 goto out;
1406 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
1408 if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
1409 ast_free(tmp);
1410 goto out;
1413 datastore->data = dialed_interfaces;
1414 AST_LIST_HEAD_INIT(dialed_interfaces);
1416 ast_channel_lock(chan);
1417 ast_channel_datastore_add(chan, datastore);
1418 ast_channel_unlock(chan);
1421 AST_LIST_LOCK(dialed_interfaces);
1422 AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
1423 if (!strcasecmp(di->interface, interface)) {
1424 ast_log(LOG_WARNING, "Skipping dialing interface '%s' again since it has already been dialed\n",
1425 di->interface);
1426 break;
1429 AST_LIST_UNLOCK(dialed_interfaces);
1431 if (di) {
1432 fulldial++;
1433 ast_free(tmp);
1434 continue;
1437 /* It is always ok to dial a Local interface. We only keep track of
1438 * which "real" interfaces have been dialed. The Local channel will
1439 * inherit this list so that if it ends up dialing a real interface,
1440 * it won't call one that has already been called. */
1441 if (strcasecmp(tech, "Local")) {
1442 if (!(di = ast_calloc(1, sizeof(*di) + strlen(interface)))) {
1443 AST_LIST_UNLOCK(dialed_interfaces);
1444 ast_free(tmp);
1445 goto out;
1447 strcpy(di->interface, interface);
1449 AST_LIST_LOCK(dialed_interfaces);
1450 AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
1451 AST_LIST_UNLOCK(dialed_interfaces);
1454 tc = ast_request(tech, chan->nativeformats, numsubst, &cause);
1455 if (!tc) {
1456 /* If we can't, just go on to the next call */
1457 ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
1458 tech, cause, ast_cause2str(cause));
1459 handle_cause(cause, &num);
1460 if (!rest) /* we are on the last destination */
1461 chan->hangupcause = cause;
1462 ast_free(tmp);
1463 continue;
1465 pbx_builtin_setvar_helper(tc, "DIALEDPEERNUMBER", numsubst);
1467 /* Setup outgoing SDP to match incoming one */
1468 ast_rtp_make_compatible(tc, chan, !outgoing && !rest);
1470 /* Inherit specially named variables from parent channel */
1471 ast_channel_inherit_variables(chan, tc);
1473 tc->appl = "AppDial";
1474 tc->data = "(Outgoing Line)";
1475 tc->whentohangup = 0;
1477 S_REPLACE(tc->cid.cid_num, ast_strdup(chan->cid.cid_num));
1478 S_REPLACE(tc->cid.cid_name, ast_strdup(chan->cid.cid_name));
1479 S_REPLACE(tc->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
1480 S_REPLACE(tc->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
1482 /* Copy language from incoming to outgoing */
1483 ast_string_field_set(tc, language, chan->language);
1484 ast_string_field_set(tc, accountcode, chan->accountcode);
1485 tc->cdrflags = chan->cdrflags;
1486 if (ast_strlen_zero(tc->musicclass))
1487 ast_string_field_set(tc, musicclass, chan->musicclass);
1488 /* Pass callingpres, type of number, tns, ADSI CPE, transfer capability */
1489 tc->cid.cid_pres = chan->cid.cid_pres;
1490 tc->cid.cid_ton = chan->cid.cid_ton;
1491 tc->cid.cid_tns = chan->cid.cid_tns;
1492 tc->cid.cid_ani2 = chan->cid.cid_ani2;
1493 tc->adsicpe = chan->adsicpe;
1494 tc->transfercapability = chan->transfercapability;
1496 /* If we have an outbound group, set this peer channel to it */
1497 if (outbound_group)
1498 ast_app_group_set_channel(tc, outbound_group);
1500 /* Inherit context and extension */
1501 if (!ast_strlen_zero(chan->macrocontext))
1502 ast_copy_string(tc->dialcontext, chan->macrocontext, sizeof(tc->dialcontext));
1503 else
1504 ast_copy_string(tc->dialcontext, chan->context, sizeof(tc->dialcontext));
1505 if (!ast_strlen_zero(chan->macroexten))
1506 ast_copy_string(tc->exten, chan->macroexten, sizeof(tc->exten));
1507 else
1508 ast_copy_string(tc->exten, chan->exten, sizeof(tc->exten));
1510 /* Save callee features */
1511 if (!(ds_callee_features = ast_channel_datastore_alloc(&dial_features_info, NULL))) {
1512 ast_log(LOG_WARNING, "Unable to create channel datastore for dial features. Aborting!\n");
1513 ast_free(tmp);
1514 goto out;
1517 if (!(callee_features = ast_malloc(sizeof(*callee_features)))) {
1518 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags. Aborting!\n");
1519 ast_free(tmp);
1520 goto out;
1523 ast_copy_flags(&(callee_features->features_callee), &(config.features_callee), AST_FLAGS_ALL);
1524 callee_features->is_caller = 0;
1525 set_dial_features(&opts, callee_features);
1527 ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
1528 ds_callee_features->data = callee_features;
1530 ast_channel_lock(chan);
1531 ast_channel_datastore_add(tc, ds_callee_features);
1532 ast_channel_unlock(chan);
1534 res = ast_call(tc, numsubst, 0); /* Place the call, but don't wait on the answer */
1536 /* Save the info in cdr's that we called them */
1537 if (chan->cdr)
1538 ast_cdr_setdestchan(chan->cdr, tc->name);
1540 /* check the results of ast_call */
1541 if (res) {
1542 /* Again, keep going even if there's an error */
1543 ast_debug(1, "ast call on peer returned %d\n", res);
1544 ast_verb(3, "Couldn't call %s\n", numsubst);
1545 ast_hangup(tc);
1546 tc = NULL;
1547 ast_free(tmp);
1548 continue;
1549 } else {
1550 senddialevent(chan, tc, numsubst);
1551 ast_verb(3, "Called %s\n", numsubst);
1552 if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID))
1553 ast_set_callerid(tc, S_OR(chan->macroexten, chan->exten), get_cid_name(cidname, sizeof(cidname), chan), NULL);
1555 /* Put them in the list of outgoing thingies... We're ready now.
1556 XXX If we're forcibly removed, these outgoing calls won't get
1557 hung up XXX */
1558 ast_set_flag64(tmp, DIAL_STILLGOING);
1559 tmp->chan = tc;
1560 tmp->next = outgoing;
1561 outgoing = tmp;
1562 /* If this line is up, don't try anybody else */
1563 if (outgoing->chan->_state == AST_STATE_UP)
1564 break;
1567 if (ast_strlen_zero(args.timeout)) {
1568 to = -1;
1569 } else {
1570 to = atoi(args.timeout);
1571 if (to > 0)
1572 to *= 1000;
1573 else
1574 ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", args.timeout);
1577 if (!outgoing) {
1578 strcpy(pa.status, "CHANUNAVAIL");
1579 if (fulldial == num_dialed) {
1580 res = -1;
1581 goto out;
1583 } else {
1584 /* Our status will at least be NOANSWER */
1585 strcpy(pa.status, "NOANSWER");
1586 if (ast_test_flag64(outgoing, OPT_MUSICBACK)) {
1587 moh = 1;
1588 if (!ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
1589 char *original_moh = ast_strdupa(chan->musicclass);
1590 ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
1591 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
1592 ast_string_field_set(chan, musicclass, original_moh);
1593 } else {
1594 ast_moh_start(chan, NULL, NULL);
1596 ast_indicate(chan, AST_CONTROL_PROGRESS);
1597 } else if (ast_test_flag64(outgoing, OPT_RINGBACK)) {
1598 ast_indicate(chan, AST_CONTROL_RINGING);
1599 sentringing++;
1603 time(&start_time);
1604 peer = wait_for_answer(chan, outgoing, &to, peerflags, &pa, &num, &result);
1606 /* The ast_channel_datastore_remove() function could fail here if the
1607 * datastore was moved to another channel during a masquerade. If this is
1608 * the case, don't free the datastore here because later, when the channel
1609 * to which the datastore was moved hangs up, it will attempt to free this
1610 * datastore again, causing a crash
1612 if (!ast_channel_datastore_remove(chan, datastore))
1613 ast_channel_datastore_free(datastore);
1614 if (!peer) {
1615 if (result) {
1616 res = result;
1617 } else if (to) { /* Musta gotten hung up */
1618 res = -1;
1619 } else { /* Nobody answered, next please? */
1620 res = 0;
1623 /* SIP, in particular, sends back this error code to indicate an
1624 * overlap dialled number needs more digits. */
1625 if (chan->hangupcause == AST_CAUSE_INVALID_NUMBER_FORMAT) {
1626 res = AST_PBX_INCOMPLETE;
1629 /* almost done, although the 'else' block is 400 lines */
1630 } else {
1631 const char *number;
1632 time_t end_time, answer_time = time(NULL);
1633 char toast[80]; /* buffer to set variables */
1635 strcpy(pa.status, "ANSWER");
1636 /* Ah ha! Someone answered within the desired timeframe. Of course after this
1637 we will always return with -1 so that it is hung up properly after the
1638 conversation. */
1639 hanguptree(outgoing, peer, 1);
1640 outgoing = NULL;
1641 /* If appropriate, log that we have a destination channel */
1642 if (chan->cdr)
1643 ast_cdr_setdestchan(chan->cdr, peer->name);
1644 if (peer->name)
1645 pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", peer->name);
1647 number = pbx_builtin_getvar_helper(peer, "DIALEDPEERNUMBER");
1648 if (!number)
1649 number = numsubst;
1650 pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
1651 if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
1652 ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
1653 ast_channel_sendurl( peer, args.url );
1655 if ( (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
1656 if (do_privacy(chan, peer, &opts, opt_args, &pa)) {
1657 res = 0;
1658 goto out;
1661 if (!ast_test_flag64(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
1662 res = 0;
1663 } else {
1664 int digit = 0;
1665 /* Start autoservice on the other chan */
1666 res = ast_autoservice_start(chan);
1667 /* Now Stream the File */
1668 if (!res)
1669 res = ast_streamfile(peer, opt_args[OPT_ARG_ANNOUNCE], peer->language);
1670 if (!res) {
1671 digit = ast_waitstream(peer, AST_DIGIT_ANY);
1673 /* Ok, done. stop autoservice */
1674 res = ast_autoservice_stop(chan);
1675 if (digit > 0 && !res)
1676 res = ast_senddigit(chan, digit, 0);
1677 else
1678 res = digit;
1682 if (chan && peer && ast_test_flag64(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
1683 replace_macro_delimiter(opt_args[OPT_ARG_GOTO]);
1684 ast_parseable_goto(chan, opt_args[OPT_ARG_GOTO]);
1685 /* peer goes to the same context and extension as chan, so just copy info from chan*/
1686 ast_copy_string(peer->context, chan->context, sizeof(peer->context));
1687 ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
1688 peer->priority = chan->priority + 2;
1689 ast_pbx_start(peer);
1690 hanguptree(outgoing, NULL, ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0);
1691 if (continue_exec)
1692 *continue_exec = 1;
1693 res = 0;
1694 goto done;
1697 if (ast_test_flag64(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
1698 struct ast_app *theapp;
1699 const char *macro_result;
1701 res = ast_autoservice_start(chan);
1702 if (res) {
1703 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
1704 res = -1;
1707 theapp = pbx_findapp("Macro");
1709 if (theapp && !res) { /* XXX why check res here ? */
1710 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
1711 res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
1712 ast_debug(1, "Macro exited with status %d\n", res);
1713 res = 0;
1714 } else {
1715 ast_log(LOG_ERROR, "Could not find application Macro\n");
1716 res = -1;
1719 if (ast_autoservice_stop(chan) < 0) {
1720 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
1721 res = -1;
1724 if (!res && (macro_result = pbx_builtin_getvar_helper(peer, "MACRO_RESULT"))) {
1725 char *macro_transfer_dest;
1727 if (!strcasecmp(macro_result, "BUSY")) {
1728 ast_copy_string(pa.status, macro_result, sizeof(pa.status));
1729 ast_set_flag64(peerflags, OPT_GO_ON);
1730 res = -1;
1731 } else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
1732 ast_copy_string(pa.status, macro_result, sizeof(pa.status));
1733 ast_set_flag64(peerflags, OPT_GO_ON);
1734 res = -1;
1735 } else if (!strcasecmp(macro_result, "CONTINUE")) {
1736 /* hangup peer and keep chan alive assuming the macro has changed
1737 the context / exten / priority or perhaps
1738 the next priority in the current exten is desired.
1740 ast_set_flag64(peerflags, OPT_GO_ON);
1741 res = -1;
1742 } else if (!strcasecmp(macro_result, "ABORT")) {
1743 /* Hangup both ends unless the caller has the g flag */
1744 res = -1;
1745 } else if (!strncasecmp(macro_result, "GOTO:", 5) && (macro_transfer_dest = ast_strdupa(macro_result + 5))) {
1746 res = -1;
1747 /* perform a transfer to a new extension */
1748 if (strchr(macro_transfer_dest, '^')) { /* context^exten^priority*/
1749 replace_macro_delimiter(macro_transfer_dest);
1750 if (!ast_parseable_goto(chan, macro_transfer_dest))
1751 ast_set_flag64(peerflags, OPT_GO_ON);
1757 if (ast_test_flag64(&opts, OPT_CALLEE_GOSUB) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GOSUB])) {
1758 struct ast_app *theapp;
1759 const char *gosub_result;
1760 char *gosub_args, *gosub_argstart;
1762 res = ast_autoservice_start(chan);
1763 if (res) {
1764 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
1765 res = -1;
1768 theapp = pbx_findapp("Gosub");
1770 if (theapp && !res) { /* XXX why check res here ? */
1771 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GOSUB]);
1773 /* Set where we came from */
1774 ast_copy_string(peer->context, "app_dial_gosub_virtual_context", sizeof(peer->context));
1775 ast_copy_string(peer->exten, "s", sizeof(peer->exten));
1776 peer->priority = 0;
1778 gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], '|');
1779 if (gosub_argstart) {
1780 *gosub_argstart = 0;
1781 asprintf(&gosub_args, "%s|s|1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], gosub_argstart + 1);
1782 *gosub_argstart = '|';
1783 } else {
1784 asprintf(&gosub_args, "%s|s|1", opt_args[OPT_ARG_CALLEE_GOSUB]);
1787 if (gosub_args) {
1788 res = pbx_exec(peer, theapp, gosub_args);
1789 ast_pbx_run(peer);
1790 ast_free(gosub_args);
1791 if (option_debug)
1792 ast_log(LOG_DEBUG, "Gosub exited with status %d\n", res);
1793 } else
1794 ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
1796 res = 0;
1797 } else {
1798 ast_log(LOG_ERROR, "Could not find application Gosub\n");
1799 res = -1;
1802 if (ast_autoservice_stop(chan) < 0) {
1803 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
1804 res = -1;
1807 if (!res && (gosub_result = pbx_builtin_getvar_helper(peer, "GOSUB_RESULT"))) {
1808 char *gosub_transfer_dest;
1810 if (!strcasecmp(gosub_result, "BUSY")) {
1811 ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
1812 ast_set_flag64(peerflags, OPT_GO_ON);
1813 res = -1;
1814 } else if (!strcasecmp(gosub_result, "CONGESTION") || !strcasecmp(gosub_result, "CHANUNAVAIL")) {
1815 ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
1816 ast_set_flag64(peerflags, OPT_GO_ON);
1817 res = -1;
1818 } else if (!strcasecmp(gosub_result, "CONTINUE")) {
1819 /* hangup peer and keep chan alive assuming the macro has changed
1820 the context / exten / priority or perhaps
1821 the next priority in the current exten is desired.
1823 ast_set_flag64(peerflags, OPT_GO_ON);
1824 res = -1;
1825 } else if (!strcasecmp(gosub_result, "ABORT")) {
1826 /* Hangup both ends unless the caller has the g flag */
1827 res = -1;
1828 } else if (!strncasecmp(gosub_result, "GOTO:", 5) && (gosub_transfer_dest = ast_strdupa(gosub_result + 5))) {
1829 res = -1;
1830 /* perform a transfer to a new extension */
1831 if (strchr(gosub_transfer_dest, '^')) { /* context^exten^priority*/
1832 replace_macro_delimiter(gosub_transfer_dest);
1833 if (!ast_parseable_goto(chan, gosub_transfer_dest))
1834 ast_set_flag64(peerflags, OPT_GO_ON);
1840 if (!res) {
1841 if (calldurationlimit > 0) {
1842 peer->whentohangup = time(NULL) + calldurationlimit;
1844 if (!ast_strlen_zero(dtmfcalled)) {
1845 ast_verb(3, "Sending DTMF '%s' to the called party.\n", dtmfcalled);
1846 res = ast_dtmf_stream(peer, chan, dtmfcalled, 250, 0);
1848 if (!ast_strlen_zero(dtmfcalling)) {
1849 ast_verb(3, "Sending DTMF '%s' to the calling party.\n", dtmfcalling);
1850 res = ast_dtmf_stream(chan, peer, dtmfcalling, 250, 0);
1854 if (res) { /* some error */
1855 res = -1;
1856 end_time = time(NULL);
1857 } else {
1858 if (ast_test_flag64(peerflags, OPT_CALLEE_TRANSFER))
1859 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1860 if (ast_test_flag64(peerflags, OPT_CALLER_TRANSFER))
1861 ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
1862 if (ast_test_flag64(peerflags, OPT_CALLEE_HANGUP))
1863 ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
1864 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP))
1865 ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
1866 if (ast_test_flag64(peerflags, OPT_CALLEE_MONITOR))
1867 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1868 if (ast_test_flag64(peerflags, OPT_CALLER_MONITOR))
1869 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1870 if (ast_test_flag64(peerflags, OPT_CALLEE_PARK))
1871 ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
1872 if (ast_test_flag64(peerflags, OPT_CALLER_PARK))
1873 ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
1874 if (ast_test_flag64(peerflags, OPT_CALLEE_MIXMONITOR))
1875 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMIXMON);
1876 if (ast_test_flag64(peerflags, OPT_CALLER_MIXMONITOR))
1877 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMIXMON);
1879 if (moh) {
1880 moh = 0;
1881 ast_moh_stop(chan);
1882 } else if (sentringing) {
1883 sentringing = 0;
1884 ast_indicate(chan, -1);
1886 /* Be sure no generators are left on it */
1887 ast_deactivate_generator(chan);
1888 /* Make sure channels are compatible */
1889 res = ast_channel_make_compatible(chan, peer);
1890 if (res < 0) {
1891 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
1892 ast_hangup(peer);
1893 res = -1;
1894 goto done;
1896 if (opermode && !strncmp(chan->name, "Zap", 3) && !strncmp(peer->name, "Zap", 3)) {
1897 /* what's this special handling for Zap <-> Zap ?
1898 * A: Zap to Zap calls are natively bridged at the kernel driver
1899 * level, so we need to ensure that this mode gets propagated
1900 * all the way down. */
1901 struct oprmode oprmode;
1903 oprmode.peer = peer;
1904 oprmode.mode = opermode;
1906 ast_channel_setoption(chan, AST_OPTION_OPRMODE, &oprmode, sizeof(oprmode), 0);
1908 res = ast_bridge_call(chan, peer, &config);
1909 end_time = time(NULL);
1910 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
1911 pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", toast);
1914 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
1915 pbx_builtin_setvar_helper(chan, "DIALEDTIME", toast);
1917 if (ast_test_flag64(&opts, OPT_PEER_H)) {
1918 ast_log(LOG_NOTICE, "PEER context: %s; PEER exten: %s; PEER priority: %d\n",
1919 peer->context, peer->exten, peer->priority);
1922 strcpy(peer->context, chan->context);
1924 if (ast_test_flag64(&opts, OPT_PEER_H) && ast_exists_extension(peer, peer->context, "h", 1, peer->cid.cid_num)) {
1925 int autoloopflag;
1926 int found;
1927 strcpy(peer->exten, "h");
1928 peer->priority = 1;
1929 autoloopflag = ast_test_flag(peer, AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
1930 ast_set_flag(peer, AST_FLAG_IN_AUTOLOOP);
1932 while ((res = ast_spawn_extension(peer, peer->context, peer->exten, peer->priority, peer->cid.cid_num, &found, 1)))
1933 peer->priority++;
1935 if (found && res) {
1936 /* Something bad happened, or a hangup has been requested. */
1937 ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
1938 ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
1940 ast_set2_flag(peer, autoloopflag, AST_FLAG_IN_AUTOLOOP); /* set it back the way it was */
1942 if (res != AST_PBX_NO_HANGUP_PEER) {
1943 if (!ast_check_hangup(peer) && ast_test_flag64(&opts, OPT_CALLEE_GO_ON) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GO_ON])) {
1944 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GO_ON]);
1945 ast_parseable_goto(peer, opt_args[OPT_ARG_CALLEE_GO_ON]);
1946 ast_pbx_start(peer);
1947 } else {
1948 if (!ast_check_hangup(chan))
1949 chan->hangupcause = peer->hangupcause;
1950 ast_hangup(peer);
1954 out:
1955 if (moh) {
1956 moh = 0;
1957 ast_moh_stop(chan);
1958 } else if (sentringing) {
1959 sentringing = 0;
1960 ast_indicate(chan, -1);
1962 ast_channel_early_bridge(chan, NULL);
1963 hanguptree(outgoing, NULL, 0); /* In this case, there's no answer anywhere */
1964 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1965 senddialendevent(chan, pa.status);
1966 ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
1968 if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_KEEPALIVE) && (res != AST_PBX_INCOMPLETE)) {
1969 if (calldurationlimit)
1970 chan->whentohangup = 0;
1971 res = 0;
1974 done:
1975 return res;
1978 static int dial_exec(struct ast_channel *chan, void *data)
1980 struct ast_flags64 peerflags;
1982 memset(&peerflags, 0, sizeof(peerflags));
1984 return dial_exec_full(chan, data, &peerflags, NULL);
1987 static int retrydial_exec(struct ast_channel *chan, void *data)
1989 char *parse;
1990 const char *context = NULL;
1991 int sleep = 0, loops = 0, res = -1;
1992 struct ast_flags64 peerflags = { 0, };
1993 AST_DECLARE_APP_ARGS(args,
1994 AST_APP_ARG(announce);
1995 AST_APP_ARG(sleep);
1996 AST_APP_ARG(retries);
1997 AST_APP_ARG(dialdata);
2000 if (ast_strlen_zero(data)) {
2001 ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
2002 return -1;
2005 parse = ast_strdupa(data);
2006 AST_STANDARD_APP_ARGS(args, parse);
2008 if ((sleep = atoi(args.sleep)))
2009 sleep *= 1000;
2011 loops = atoi(args.retries);
2013 if (!args.dialdata) {
2014 ast_log(LOG_ERROR, "%s requires a 4th argument (dialdata)\n", rapp);
2015 goto done;
2018 if (sleep < 1000)
2019 sleep = 10000;
2021 if (!loops)
2022 loops = -1; /* run forever */
2024 context = pbx_builtin_getvar_helper(chan, "EXITCONTEXT");
2026 res = 0;
2027 while (loops) {
2028 int continue_exec;
2030 chan->data = "Retrying";
2031 if (ast_test_flag(chan, AST_FLAG_MOH))
2032 ast_moh_stop(chan);
2034 res = dial_exec_full(chan, args.dialdata, &peerflags, &continue_exec);
2035 if (continue_exec)
2036 break;
2038 if (res == 0) {
2039 if (ast_test_flag64(&peerflags, OPT_DTMF_EXIT)) {
2040 if (!ast_strlen_zero(args.announce)) {
2041 if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
2042 if (!(res = ast_streamfile(chan, args.announce, chan->language)))
2043 ast_waitstream(chan, AST_DIGIT_ANY);
2044 } else
2045 ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
2047 if (!res && sleep) {
2048 if (!ast_test_flag(chan, AST_FLAG_MOH))
2049 ast_moh_start(chan, NULL, NULL);
2050 res = ast_waitfordigit(chan, sleep);
2052 } else {
2053 if (!ast_strlen_zero(args.announce)) {
2054 if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
2055 if (!(res = ast_streamfile(chan, args.announce, chan->language)))
2056 res = ast_waitstream(chan, "");
2057 } else
2058 ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
2060 if (sleep) {
2061 if (!ast_test_flag(chan, AST_FLAG_MOH))
2062 ast_moh_start(chan, NULL, NULL);
2063 if (!res)
2064 res = ast_waitfordigit(chan, sleep);
2069 if (res < 0 || res == AST_PBX_INCOMPLETE) {
2070 break;
2071 } else if (res > 0) { /* Trying to send the call elsewhere (1 digit ext) */
2072 if (onedigit_goto(chan, context, (char) res, 1)) {
2073 res = 0;
2074 break;
2077 loops--;
2079 if (loops == 0)
2080 res = 0;
2081 else if (res == 1)
2082 res = 0;
2084 if (ast_test_flag(chan, AST_FLAG_MOH))
2085 ast_moh_stop(chan);
2086 done:
2087 return res;
2090 static int unload_module(void)
2092 int res;
2093 struct ast_context *con;
2095 res = ast_unregister_application(app);
2096 res |= ast_unregister_application(rapp);
2098 if ((con = ast_context_find("app_dial_gosub_virtual_context"))) {
2099 ast_context_remove_extension2(con, "s", 1, NULL);
2100 ast_context_destroy(con, "app_dial"); /* leave nothing behind */
2103 return res;
2106 static int load_module(void)
2108 int res;
2109 struct ast_context *con;
2111 con = ast_context_find_or_create(NULL, NULL, "app_dial_gosub_virtual_context", "app_dial");
2112 if (!con)
2113 ast_log(LOG_ERROR, "Dial virtual context 'app_dial_gosub_virtual_context' does not exist and unable to create\n");
2114 else
2115 ast_add_extension2(con, 1, "s", 1, NULL, NULL, "KeepAlive", ast_strdup(""), ast_free_ptr, "app_dial");
2117 res = ast_register_application(app, dial_exec, synopsis, descrip);
2118 res |= ast_register_application(rapp, retrydial_exec, rsynopsis, rdescrip);
2120 return res;
2123 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialing Application");