Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_dial.c
blobd3a2aedd97b22fc5072ba198726025a5c8282cbc
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 retrying the call. After 'retries' 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.ptr, 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->data.uint32) {
793 in->hangupcause = f->data.uint32;
795 ast_frfree(f);
797 return NULL;
800 /* now f is guaranteed non-NULL */
801 if (f->frametype == AST_FRAME_DTMF) {
802 if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
803 const char *context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
804 if (onedigit_goto(in, context, (char) f->subclass, 1)) {
805 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
806 *to = 0;
807 ast_cdr_noanswer(in->cdr);
808 *result = f->subclass;
809 strcpy(pa->status, "CANCEL");
810 ast_frfree(f);
811 return NULL;
815 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) &&
816 (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
817 ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
818 *to = 0;
819 strcpy(pa->status, "CANCEL");
820 ast_cdr_noanswer(in->cdr);
821 ast_frfree(f);
822 return NULL;
826 /* Forward HTML stuff */
827 if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML))
828 if (ast_channel_sendhtml(outgoing->chan, f->subclass, f->data.ptr, f->datalen) == -1)
829 ast_log(LOG_WARNING, "Unable to send URL\n");
831 if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF_BEGIN) || (f->frametype == AST_FRAME_DTMF_END))) {
832 if (ast_write(outgoing->chan, f))
833 ast_log(LOG_WARNING, "Unable to forward voice or dtmf\n");
835 if (single && (f->frametype == AST_FRAME_CONTROL) &&
836 ((f->subclass == AST_CONTROL_HOLD) ||
837 (f->subclass == AST_CONTROL_UNHOLD) ||
838 (f->subclass == AST_CONTROL_VIDUPDATE) ||
839 (f->subclass == AST_CONTROL_SRCUPDATE))) {
840 ast_verb(3, "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
841 ast_indicate_data(outgoing->chan, f->subclass, f->data.ptr, f->datalen);
843 ast_frfree(f);
845 if (!*to)
846 ast_verb(3, "Nobody picked up in %d ms\n", orig);
847 if (!*to || ast_check_hangup(in))
848 ast_cdr_noanswer(in->cdr);
851 #ifdef HAVE_EPOLL
852 for (epollo = outgoing; epollo; epollo = epollo->next) {
853 if (epollo->chan)
854 ast_poll_channel_del(in, epollo->chan);
856 #endif
858 return peer;
861 static void replace_macro_delimiter(char *s)
863 for (; *s; s++)
864 if (*s == '^')
865 *s = ',';
868 /* returns true if there is a valid privacy reply */
869 static int valid_priv_reply(struct ast_flags64 *opts, int res)
871 if (res < '1')
872 return 0;
873 if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
874 return 1;
875 if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
876 return 1;
877 return 0;
880 static int do_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
881 char *parse, unsigned int *calldurationlimit)
883 char *stringp = ast_strdupa(parse);
884 char *limit_str, *warning_str, *warnfreq_str;
885 const char *var;
886 int play_to_caller = 0, play_to_callee = 0;
887 int delta;
889 limit_str = strsep(&stringp, ":");
890 warning_str = strsep(&stringp, ":");
891 warnfreq_str = strsep(&stringp, ":");
893 config->timelimit = atol(limit_str);
894 if (warning_str)
895 config->play_warning = atol(warning_str);
896 if (warnfreq_str)
897 config->warning_freq = atol(warnfreq_str);
899 if (!config->timelimit) {
900 ast_log(LOG_WARNING, "Dial does not accept L(%s), hanging up.\n", limit_str);
901 config->timelimit = config->play_warning = config->warning_freq = 0;
902 config->warning_sound = NULL;
903 return -1; /* error */
904 } else if ( (delta = config->play_warning - config->timelimit) > 0) {
905 int w = config->warning_freq;
907 /* If the first warning is requested _after_ the entire call would end,
908 and no warning frequency is requested, then turn off the warning. If
909 a warning frequency is requested, reduce the 'first warning' time by
910 that frequency until it falls within the call's total time limit.
911 Graphically:
912 timelim->| delta |<-playwarning
913 0__________________|_________________|
914 | w | | | |
916 so the number of intervals to cut is 1+(delta-1)/w
919 if (w == 0) {
920 config->play_warning = 0;
921 } else {
922 config->play_warning -= w * ( 1 + (delta-1)/w );
923 if (config->play_warning < 1)
924 config->play_warning = config->warning_freq = 0;
928 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
929 play_to_caller = var ? ast_true(var) : 1;
931 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
932 play_to_callee = var ? ast_true(var) : 0;
934 if (!play_to_caller && !play_to_callee)
935 play_to_caller = 1;
937 var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
938 config->warning_sound = S_OR(var, "timeleft");
940 /* The code looking at config wants a NULL, not just "", to decide
941 * that the message should not be played, so we replace "" with NULL.
942 * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
943 * not found.
945 var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
946 config->end_sound = S_OR(var, NULL);
947 var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
948 config->start_sound = S_OR(var, NULL);
950 /* undo effect of S(x) in case they are both used */
951 *calldurationlimit = 0;
952 /* more efficient to do it like S(x) does since no advanced opts */
953 if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
954 *calldurationlimit = config->timelimit / 1000;
955 ast_verb(3, "Setting call duration limit to %d seconds.\n",
956 *calldurationlimit);
957 config->timelimit = play_to_caller = play_to_callee =
958 config->play_warning = config->warning_freq = 0;
959 } else {
960 ast_verb(3, "Limit Data for this call:\n");
961 ast_verb(4, "timelimit = %ld\n", config->timelimit);
962 ast_verb(4, "play_warning = %ld\n", config->play_warning);
963 ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
964 ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
965 ast_verb(4, "warning_freq = %ld\n", config->warning_freq);
966 ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
967 ast_verb(4, "warning_sound = %s\n", config->warning_sound);
968 ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
970 if (play_to_caller)
971 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
972 if (play_to_callee)
973 ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
974 return 0;
977 static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
978 struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
981 int res2;
982 int loopcount = 0;
984 /* Get the user's intro, store it in priv-callerintros/$CID,
985 unless it is already there-- this should be done before the
986 call is actually dialed */
988 /* all ring indications and moh for the caller has been halted as soon as the
989 target extension was picked up. We are going to have to kill some
990 time and make the caller believe the peer hasn't picked up yet */
992 if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
993 char *original_moh = ast_strdupa(chan->musicclass);
994 ast_indicate(chan, -1);
995 ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
996 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
997 ast_string_field_set(chan, musicclass, original_moh);
998 } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
999 ast_indicate(chan, AST_CONTROL_RINGING);
1000 pa->sentringing++;
1003 /* Start autoservice on the other chan ?? */
1004 res2 = ast_autoservice_start(chan);
1005 /* Now Stream the File */
1006 for (loopcount = 0; loopcount < 3; loopcount++) {
1007 if (res2 && loopcount == 0) /* error in ast_autoservice_start() */
1008 break;
1009 if (!res2) /* on timeout, play the message again */
1010 res2 = ast_play_and_wait(peer, "priv-callpending");
1011 if (!valid_priv_reply(opts, res2))
1012 res2 = 0;
1013 /* priv-callpending script:
1014 "I have a caller waiting, who introduces themselves as:"
1016 if (!res2)
1017 res2 = ast_play_and_wait(peer, pa->privintro);
1018 if (!valid_priv_reply(opts, res2))
1019 res2 = 0;
1020 /* now get input from the called party, as to their choice */
1021 if (!res2) {
1022 /* XXX can we have both, or they are mutually exclusive ? */
1023 if (ast_test_flag64(opts, OPT_PRIVACY))
1024 res2 = ast_play_and_wait(peer, "priv-callee-options");
1025 if (ast_test_flag64(opts, OPT_SCREENING))
1026 res2 = ast_play_and_wait(peer, "screen-callee-options");
1028 /*! \page DialPrivacy Dial Privacy scripts
1029 \par priv-callee-options script:
1030 "Dial 1 if you wish this caller to reach you directly in the future,
1031 and immediately connect to their incoming call
1032 Dial 2 if you wish to send this caller to voicemail now and
1033 forevermore.
1034 Dial 3 to send this caller to the torture menus, now and forevermore.
1035 Dial 4 to send this caller to a simple "go away" menu, now and forevermore.
1036 Dial 5 to allow this caller to come straight thru to you in the future,
1037 but right now, just this once, send them to voicemail."
1038 \par screen-callee-options script:
1039 "Dial 1 if you wish to immediately connect to the incoming call
1040 Dial 2 if you wish to send this caller to voicemail.
1041 Dial 3 to send this caller to the torture menus.
1042 Dial 4 to send this caller to a simple "go away" menu.
1044 if (valid_priv_reply(opts, res2))
1045 break;
1046 /* invalid option */
1047 res2 = ast_play_and_wait(peer, "vm-sorry");
1050 if (ast_test_flag64(opts, OPT_MUSICBACK)) {
1051 ast_moh_stop(chan);
1052 } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
1053 ast_indicate(chan, -1);
1054 pa->sentringing = 0;
1056 ast_autoservice_stop(chan);
1057 if (ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
1058 /* map keypresses to various things, the index is res2 - '1' */
1059 static const char *_val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
1060 static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
1061 int i = res2 - '1';
1062 ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
1063 opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
1064 ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
1066 switch (res2) {
1067 case '1':
1068 break;
1069 case '2':
1070 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1071 break;
1072 case '3':
1073 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1074 break;
1075 case '4':
1076 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1077 break;
1078 case '5':
1079 /* XXX should we set status to DENY ? */
1080 if (ast_test_flag64(opts, OPT_PRIVACY))
1081 break;
1082 /* if not privacy, then 5 is the same as "default" case */
1083 default: /* bad input or -1 if failure to start autoservice */
1084 /* well, if the user messes up, ... he had his chance... What Is The Best Thing To Do? */
1085 /* well, there seems basically two choices. Just patch the caller thru immediately,
1086 or,... put 'em thru to voicemail. */
1087 /* since the callee may have hung up, let's do the voicemail thing, no database decision */
1088 ast_log(LOG_NOTICE, "privacy: no valid response from the callee. Sending the caller to voicemail, the callee isn't responding\n");
1089 /* XXX should we set status to DENY ? */
1090 /* XXX what about the privacy flags ? */
1091 break;
1094 if (res2 == '1') { /* the only case where we actually connect */
1095 /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll
1096 just clog things up, and it's not useful information, not being tied to a CID */
1097 if (strncmp(pa->privcid, "NOCALLERID", 10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO)) {
1098 ast_filedelete(pa->privintro, NULL);
1099 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1100 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1101 else
1102 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1104 return 0; /* the good exit path */
1105 } else {
1106 ast_hangup(peer); /* hang up on the callee -- he didn't want to talk anyway! */
1107 return -1;
1111 /*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
1112 static int setup_privacy_args(struct privacy_args *pa,
1113 struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
1115 char callerid[60];
1116 int res;
1117 char *l;
1118 int silencethreshold;
1120 if (!ast_strlen_zero(chan->cid.cid_num)) {
1121 l = ast_strdupa(chan->cid.cid_num);
1122 ast_shrink_phone_number(l);
1123 if (ast_test_flag64(opts, OPT_PRIVACY) ) {
1124 ast_verb(3, "Privacy DB is '%s', clid is '%s'\n", opt_args[OPT_ARG_PRIVACY], l);
1125 pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
1126 } else {
1127 ast_verb(3, "Privacy Screening, clid is '%s'\n", l);
1128 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1130 } else {
1131 char *tnam, *tn2;
1133 tnam = ast_strdupa(chan->name);
1134 /* clean the channel name so slashes don't try to end up in disk file name */
1135 for (tn2 = tnam; *tn2; tn2++) {
1136 if (*tn2 == '/') /* any other chars to be afraid of? */
1137 *tn2 = '=';
1139 ast_verb(3, "Privacy-- callerid is empty\n");
1141 snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
1142 l = callerid;
1143 pa->privdb_val = AST_PRIVACY_UNKNOWN;
1146 ast_copy_string(pa->privcid, l, sizeof(pa->privcid));
1148 if (strncmp(pa->privcid, "NOCALLERID", 10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCLID)) {
1149 /* if callerid is set and OPT_SCREEN_NOCLID is set also */
1150 ast_verb(3, "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
1151 pa->privdb_val = AST_PRIVACY_ALLOW;
1152 } else if (ast_test_flag64(opts, OPT_SCREEN_NOCLID) && strncmp(pa->privcid, "NOCALLERID", 10) == 0) {
1153 ast_verb(3, "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
1156 if (pa->privdb_val == AST_PRIVACY_DENY) {
1157 ast_verb(3, "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
1158 ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
1159 return 0;
1160 } else if (pa->privdb_val == AST_PRIVACY_KILL) {
1161 ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
1162 return 0; /* Is this right? */
1163 } else if (pa->privdb_val == AST_PRIVACY_TORTURE) {
1164 ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
1165 return 0; /* is this right??? */
1166 } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN) {
1167 /* Get the user's intro, store it in priv-callerintros/$CID,
1168 unless it is already there-- this should be done before the
1169 call is actually dialed */
1171 /* make sure the priv-callerintros dir actually exists */
1172 snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
1173 if ((res = ast_mkdir(pa->privintro, 0755))) {
1174 ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
1175 return -1;
1178 snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
1179 if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
1180 /* the DELUX version of this code would allow this caller the
1181 option to hear and retape their previously recorded intro.
1183 } else {
1184 int duration; /* for feedback from play_and_wait */
1185 /* the file doesn't exist yet. Let the caller submit his
1186 vocal intro for posterity */
1187 /* priv-recordintro script:
1189 "At the tone, please say your name:"
1192 silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
1193 ast_answer(chan);
1194 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 */
1195 /* don't think we'll need a lock removed, we took care of
1196 conflicts by naming the pa.privintro file */
1197 if (res == -1) {
1198 /* Delete the file regardless since they hung up during recording */
1199 ast_filedelete(pa->privintro, NULL);
1200 if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
1201 ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
1202 else
1203 ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
1204 return -1;
1206 if (!ast_streamfile(chan, "vm-dialout", chan->language) )
1207 ast_waitstream(chan, "");
1210 return 1; /* success */
1213 static void set_dial_features(struct ast_flags64 *opts, struct ast_dial_features *features)
1215 struct ast_flags64 perm_opts = {.flags = 0};
1217 ast_copy_flags64(&perm_opts, opts,
1218 OPT_CALLER_TRANSFER | OPT_CALLER_PARK | OPT_CALLER_MONITOR | OPT_CALLER_MIXMONITOR | OPT_CALLER_HANGUP |
1219 OPT_CALLEE_TRANSFER | OPT_CALLEE_PARK | OPT_CALLEE_MONITOR | OPT_CALLEE_MIXMONITOR | OPT_CALLEE_HANGUP);
1221 memset(features->options, 0, sizeof(features->options));
1223 ast_app_options2str64(dial_exec_options, &perm_opts, features->options, sizeof(features->options));
1226 static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags64 *peerflags, int *continue_exec)
1228 int res = -1; /* default: error */
1229 char *rest, *cur; /* scan the list of destinations */
1230 struct chanlist *outgoing = NULL; /* list of destinations */
1231 struct ast_channel *peer;
1232 int to; /* timeout */
1233 struct cause_args num = { chan, 0, 0, 0 };
1234 int cause;
1235 char numsubst[256];
1236 char cidname[AST_MAX_EXTENSION] = "";
1238 struct ast_bridge_config config = { { 0, } };
1239 unsigned int calldurationlimit = 0;
1240 char *dtmfcalled = NULL, *dtmfcalling = NULL;
1241 struct privacy_args pa = {
1242 .sentringing = 0,
1243 .privdb_val = 0,
1244 .status = "INVALIDARGS",
1246 int sentringing = 0, moh = 0;
1247 const char *outbound_group = NULL;
1248 int result = 0;
1249 time_t start_time;
1250 char *parse;
1251 int opermode = 0;
1252 AST_DECLARE_APP_ARGS(args,
1253 AST_APP_ARG(peers);
1254 AST_APP_ARG(timeout);
1255 AST_APP_ARG(options);
1256 AST_APP_ARG(url);
1258 struct ast_flags64 opts = { 0, };
1259 char *opt_args[OPT_ARG_ARRAY_SIZE];
1260 struct ast_datastore *datastore = NULL;
1261 struct ast_datastore *ds_caller_features = NULL;
1262 struct ast_datastore *ds_callee_features = NULL;
1263 struct ast_dial_features *caller_features;
1264 int fulldial = 0, num_dialed = 0;
1266 if (ast_strlen_zero(data)) {
1267 ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
1268 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1269 return -1;
1272 parse = ast_strdupa(data);
1274 AST_STANDARD_APP_ARGS(args, parse);
1276 if (!ast_strlen_zero(args.options) &&
1277 ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
1278 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1279 goto done;
1282 if (ast_strlen_zero(args.peers)) {
1283 ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
1284 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1285 goto done;
1288 if (ast_test_flag64(&opts, OPT_OPERMODE)) {
1289 opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
1290 ast_verb(3, "Setting operator services mode to %d.\n", opermode);
1293 if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
1294 calldurationlimit = atoi(opt_args[OPT_ARG_DURATION_STOP]);
1295 if (!calldurationlimit) {
1296 ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
1297 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1298 goto done;
1300 ast_verb(3, "Setting call duration limit to %d seconds.\n", calldurationlimit);
1303 if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
1304 dtmfcalling = opt_args[OPT_ARG_SENDDTMF];
1305 dtmfcalled = strsep(&dtmfcalling, ":");
1308 if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
1309 if (do_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
1310 goto done;
1313 if (ast_test_flag64(&opts, OPT_RESETCDR) && chan->cdr)
1314 ast_cdr_reset(chan->cdr, NULL);
1315 if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
1316 opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
1318 if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
1319 res = setup_privacy_args(&pa, &opts, opt_args, chan);
1320 if (res <= 0)
1321 goto out;
1322 res = -1; /* reset default */
1325 if (continue_exec)
1326 *continue_exec = 0;
1328 /* If a channel group has been specified, get it for use when we create peer channels */
1329 if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
1330 outbound_group = ast_strdupa(outbound_group);
1331 pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
1332 } else {
1333 outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP");
1336 ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING);
1338 /* Create datastore for channel dial features for caller */
1339 if (!(ds_caller_features = ast_channel_datastore_alloc(&dial_features_info, NULL))) {
1340 ast_log(LOG_WARNING, "Unable to create channel datastore for dial features. Aborting!\n");
1341 goto out;
1344 if (!(caller_features = ast_malloc(sizeof(*caller_features)))) {
1345 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags. Aborting!\n");
1346 goto out;
1349 ast_copy_flags(&(caller_features->features_callee), &(config.features_caller), AST_FLAGS_ALL);
1350 caller_features->is_caller = 1;
1351 set_dial_features(&opts, caller_features);
1353 ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
1354 ds_caller_features->data = caller_features;
1356 ast_channel_lock(chan);
1357 ast_channel_datastore_add(chan, ds_caller_features);
1358 ast_channel_unlock(chan);
1360 /* loop through the list of dial destinations */
1361 rest = args.peers;
1362 while ((cur = strsep(&rest, "&")) ) {
1363 struct chanlist *tmp;
1364 struct ast_channel *tc; /* channel for this destination */
1365 /* Get a technology/[device:]number pair */
1366 char *number = cur;
1367 char *interface = ast_strdupa(number);
1368 char *tech = strsep(&number, "/");
1369 /* find if we already dialed this interface */
1370 struct ast_dialed_interface *di;
1371 struct ast_dial_features *callee_features;
1372 AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
1373 num_dialed++;
1374 if (!number) {
1375 ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
1376 goto out;
1378 if (!(tmp = ast_calloc(1, sizeof(*tmp))))
1379 goto out;
1380 if (opts.flags) {
1381 ast_copy_flags64(tmp, &opts,
1382 OPT_CANCEL_ELSEWHERE |
1383 OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
1384 OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
1385 OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
1386 OPT_CALLEE_PARK | OPT_CALLER_PARK |
1387 OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
1388 OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID);
1389 ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);
1391 ast_copy_string(numsubst, number, sizeof(numsubst));
1392 /* Request the peer */
1394 ast_channel_lock(chan);
1395 datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL);
1396 ast_channel_unlock(chan);
1398 if (datastore)
1399 dialed_interfaces = datastore->data;
1400 else {
1401 if (!(datastore = ast_channel_datastore_alloc(&dialed_interface_info, NULL))) {
1402 ast_log(LOG_WARNING, "Unable to create channel datastore for dialed interfaces. Aborting!\n");
1403 ast_free(tmp);
1404 goto out;
1407 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
1409 if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
1410 ast_free(tmp);
1411 goto out;
1414 datastore->data = dialed_interfaces;
1415 AST_LIST_HEAD_INIT(dialed_interfaces);
1417 ast_channel_lock(chan);
1418 ast_channel_datastore_add(chan, datastore);
1419 ast_channel_unlock(chan);
1422 AST_LIST_LOCK(dialed_interfaces);
1423 AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
1424 if (!strcasecmp(di->interface, interface)) {
1425 ast_log(LOG_WARNING, "Skipping dialing interface '%s' again since it has already been dialed\n",
1426 di->interface);
1427 break;
1430 AST_LIST_UNLOCK(dialed_interfaces);
1432 if (di) {
1433 fulldial++;
1434 ast_free(tmp);
1435 continue;
1438 /* It is always ok to dial a Local interface. We only keep track of
1439 * which "real" interfaces have been dialed. The Local channel will
1440 * inherit this list so that if it ends up dialing a real interface,
1441 * it won't call one that has already been called. */
1442 if (strcasecmp(tech, "Local")) {
1443 if (!(di = ast_calloc(1, sizeof(*di) + strlen(interface)))) {
1444 AST_LIST_UNLOCK(dialed_interfaces);
1445 ast_free(tmp);
1446 goto out;
1448 strcpy(di->interface, interface);
1450 AST_LIST_LOCK(dialed_interfaces);
1451 AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
1452 AST_LIST_UNLOCK(dialed_interfaces);
1455 tc = ast_request(tech, chan->nativeformats, numsubst, &cause);
1456 if (!tc) {
1457 /* If we can't, just go on to the next call */
1458 ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
1459 tech, cause, ast_cause2str(cause));
1460 handle_cause(cause, &num);
1461 if (!rest) /* we are on the last destination */
1462 chan->hangupcause = cause;
1463 ast_free(tmp);
1464 continue;
1466 pbx_builtin_setvar_helper(tc, "DIALEDPEERNUMBER", numsubst);
1468 /* Setup outgoing SDP to match incoming one */
1469 ast_rtp_make_compatible(tc, chan, !outgoing && !rest);
1471 /* Inherit specially named variables from parent channel */
1472 ast_channel_inherit_variables(chan, tc);
1474 tc->appl = "AppDial";
1475 tc->data = "(Outgoing Line)";
1476 memset(&tc->whentohangup, 0, sizeof(tc->whentohangup));
1478 S_REPLACE(tc->cid.cid_num, ast_strdup(chan->cid.cid_num));
1479 S_REPLACE(tc->cid.cid_name, ast_strdup(chan->cid.cid_name));
1480 S_REPLACE(tc->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
1481 S_REPLACE(tc->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
1483 /* Copy language from incoming to outgoing */
1484 ast_string_field_set(tc, language, chan->language);
1485 ast_string_field_set(tc, accountcode, chan->accountcode);
1486 tc->cdrflags = chan->cdrflags;
1487 if (ast_strlen_zero(tc->musicclass))
1488 ast_string_field_set(tc, musicclass, chan->musicclass);
1489 /* Pass callingpres, type of number, tns, ADSI CPE, transfer capability */
1490 tc->cid.cid_pres = chan->cid.cid_pres;
1491 tc->cid.cid_ton = chan->cid.cid_ton;
1492 tc->cid.cid_tns = chan->cid.cid_tns;
1493 tc->cid.cid_ani2 = chan->cid.cid_ani2;
1494 tc->adsicpe = chan->adsicpe;
1495 tc->transfercapability = chan->transfercapability;
1497 /* If we have an outbound group, set this peer channel to it */
1498 if (outbound_group)
1499 ast_app_group_set_channel(tc, outbound_group);
1501 /* Inherit context and extension */
1502 if (!ast_strlen_zero(chan->macrocontext))
1503 ast_copy_string(tc->dialcontext, chan->macrocontext, sizeof(tc->dialcontext));
1504 else
1505 ast_copy_string(tc->dialcontext, chan->context, sizeof(tc->dialcontext));
1506 if (!ast_strlen_zero(chan->macroexten))
1507 ast_copy_string(tc->exten, chan->macroexten, sizeof(tc->exten));
1508 else
1509 ast_copy_string(tc->exten, chan->exten, sizeof(tc->exten));
1511 /* Save callee features */
1512 if (!(ds_callee_features = ast_channel_datastore_alloc(&dial_features_info, NULL))) {
1513 ast_log(LOG_WARNING, "Unable to create channel datastore for dial features. Aborting!\n");
1514 ast_free(tmp);
1515 goto out;
1518 if (!(callee_features = ast_malloc(sizeof(*callee_features)))) {
1519 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags. Aborting!\n");
1520 ast_free(tmp);
1521 goto out;
1524 ast_copy_flags(&(callee_features->features_callee), &(config.features_callee), AST_FLAGS_ALL);
1525 callee_features->is_caller = 0;
1526 set_dial_features(&opts, callee_features);
1528 ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
1529 ds_callee_features->data = callee_features;
1531 ast_channel_lock(chan);
1532 ast_channel_datastore_add(tc, ds_callee_features);
1533 ast_channel_unlock(chan);
1535 res = ast_call(tc, numsubst, 0); /* Place the call, but don't wait on the answer */
1537 /* Save the info in cdr's that we called them */
1538 if (chan->cdr)
1539 ast_cdr_setdestchan(chan->cdr, tc->name);
1541 /* check the results of ast_call */
1542 if (res) {
1543 /* Again, keep going even if there's an error */
1544 ast_debug(1, "ast call on peer returned %d\n", res);
1545 ast_verb(3, "Couldn't call %s\n", numsubst);
1546 ast_hangup(tc);
1547 tc = NULL;
1548 ast_free(tmp);
1549 continue;
1550 } else {
1551 senddialevent(chan, tc, numsubst);
1552 ast_verb(3, "Called %s\n", numsubst);
1553 if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID))
1554 ast_set_callerid(tc, S_OR(chan->macroexten, chan->exten), get_cid_name(cidname, sizeof(cidname), chan), NULL);
1556 /* Put them in the list of outgoing thingies... We're ready now.
1557 XXX If we're forcibly removed, these outgoing calls won't get
1558 hung up XXX */
1559 ast_set_flag64(tmp, DIAL_STILLGOING);
1560 tmp->chan = tc;
1561 tmp->next = outgoing;
1562 outgoing = tmp;
1563 /* If this line is up, don't try anybody else */
1564 if (outgoing->chan->_state == AST_STATE_UP)
1565 break;
1568 if (ast_strlen_zero(args.timeout)) {
1569 to = -1;
1570 } else {
1571 to = atoi(args.timeout);
1572 if (to > 0)
1573 to *= 1000;
1574 else
1575 ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", args.timeout);
1578 if (!outgoing) {
1579 strcpy(pa.status, "CHANUNAVAIL");
1580 if (fulldial == num_dialed) {
1581 res = -1;
1582 goto out;
1584 } else {
1585 /* Our status will at least be NOANSWER */
1586 strcpy(pa.status, "NOANSWER");
1587 if (ast_test_flag64(outgoing, OPT_MUSICBACK)) {
1588 moh = 1;
1589 if (!ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
1590 char *original_moh = ast_strdupa(chan->musicclass);
1591 ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
1592 ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
1593 ast_string_field_set(chan, musicclass, original_moh);
1594 } else {
1595 ast_moh_start(chan, NULL, NULL);
1597 ast_indicate(chan, AST_CONTROL_PROGRESS);
1598 } else if (ast_test_flag64(outgoing, OPT_RINGBACK)) {
1599 ast_indicate(chan, AST_CONTROL_RINGING);
1600 sentringing++;
1604 time(&start_time);
1605 peer = wait_for_answer(chan, outgoing, &to, peerflags, &pa, &num, &result);
1607 /* The ast_channel_datastore_remove() function could fail here if the
1608 * datastore was moved to another channel during a masquerade. If this is
1609 * the case, don't free the datastore here because later, when the channel
1610 * to which the datastore was moved hangs up, it will attempt to free this
1611 * datastore again, causing a crash
1613 if (!ast_channel_datastore_remove(chan, datastore))
1614 ast_channel_datastore_free(datastore);
1615 if (!peer) {
1616 if (result) {
1617 res = result;
1618 } else if (to) { /* Musta gotten hung up */
1619 res = -1;
1620 } else { /* Nobody answered, next please? */
1621 res = 0;
1624 /* SIP, in particular, sends back this error code to indicate an
1625 * overlap dialled number needs more digits. */
1626 if (chan->hangupcause == AST_CAUSE_INVALID_NUMBER_FORMAT) {
1627 res = AST_PBX_INCOMPLETE;
1630 /* almost done, although the 'else' block is 400 lines */
1631 } else {
1632 const char *number;
1633 time_t end_time, answer_time = time(NULL);
1634 char toast[80]; /* buffer to set variables */
1636 strcpy(pa.status, "ANSWER");
1637 /* Ah ha! Someone answered within the desired timeframe. Of course after this
1638 we will always return with -1 so that it is hung up properly after the
1639 conversation. */
1640 hanguptree(outgoing, peer, 1);
1641 outgoing = NULL;
1642 /* If appropriate, log that we have a destination channel */
1643 if (chan->cdr)
1644 ast_cdr_setdestchan(chan->cdr, peer->name);
1645 if (peer->name)
1646 pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", peer->name);
1648 number = pbx_builtin_getvar_helper(peer, "DIALEDPEERNUMBER");
1649 if (!number)
1650 number = numsubst;
1651 pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
1652 if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
1653 ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
1654 ast_channel_sendurl( peer, args.url );
1656 if ( (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
1657 if (do_privacy(chan, peer, &opts, opt_args, &pa)) {
1658 res = 0;
1659 goto out;
1662 if (!ast_test_flag64(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
1663 res = 0;
1664 } else {
1665 int digit = 0;
1666 /* Start autoservice on the other chan */
1667 res = ast_autoservice_start(chan);
1668 /* Now Stream the File */
1669 if (!res)
1670 res = ast_streamfile(peer, opt_args[OPT_ARG_ANNOUNCE], peer->language);
1671 if (!res) {
1672 digit = ast_waitstream(peer, AST_DIGIT_ANY);
1674 /* Ok, done. stop autoservice */
1675 res = ast_autoservice_stop(chan);
1676 if (digit > 0 && !res)
1677 res = ast_senddigit(chan, digit, 0);
1678 else
1679 res = digit;
1683 if (chan && peer && ast_test_flag64(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
1684 replace_macro_delimiter(opt_args[OPT_ARG_GOTO]);
1685 ast_parseable_goto(chan, opt_args[OPT_ARG_GOTO]);
1686 /* peer goes to the same context and extension as chan, so just copy info from chan*/
1687 ast_copy_string(peer->context, chan->context, sizeof(peer->context));
1688 ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
1689 peer->priority = chan->priority + 2;
1690 ast_pbx_start(peer);
1691 hanguptree(outgoing, NULL, ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0);
1692 if (continue_exec)
1693 *continue_exec = 1;
1694 res = 0;
1695 goto done;
1698 if (ast_test_flag64(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
1699 struct ast_app *theapp;
1700 const char *macro_result;
1702 res = ast_autoservice_start(chan);
1703 if (res) {
1704 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
1705 res = -1;
1708 theapp = pbx_findapp("Macro");
1710 if (theapp && !res) { /* XXX why check res here ? */
1711 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
1712 res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
1713 ast_debug(1, "Macro exited with status %d\n", res);
1714 res = 0;
1715 } else {
1716 ast_log(LOG_ERROR, "Could not find application Macro\n");
1717 res = -1;
1720 if (ast_autoservice_stop(chan) < 0) {
1721 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
1722 res = -1;
1725 if (!res && (macro_result = pbx_builtin_getvar_helper(peer, "MACRO_RESULT"))) {
1726 char *macro_transfer_dest;
1728 if (!strcasecmp(macro_result, "BUSY")) {
1729 ast_copy_string(pa.status, macro_result, sizeof(pa.status));
1730 ast_set_flag64(peerflags, OPT_GO_ON);
1731 res = -1;
1732 } else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
1733 ast_copy_string(pa.status, macro_result, sizeof(pa.status));
1734 ast_set_flag64(peerflags, OPT_GO_ON);
1735 res = -1;
1736 } else if (!strcasecmp(macro_result, "CONTINUE")) {
1737 /* hangup peer and keep chan alive assuming the macro has changed
1738 the context / exten / priority or perhaps
1739 the next priority in the current exten is desired.
1741 ast_set_flag64(peerflags, OPT_GO_ON);
1742 res = -1;
1743 } else if (!strcasecmp(macro_result, "ABORT")) {
1744 /* Hangup both ends unless the caller has the g flag */
1745 res = -1;
1746 } else if (!strncasecmp(macro_result, "GOTO:", 5) && (macro_transfer_dest = ast_strdupa(macro_result + 5))) {
1747 res = -1;
1748 /* perform a transfer to a new extension */
1749 if (strchr(macro_transfer_dest, '^')) { /* context^exten^priority*/
1750 replace_macro_delimiter(macro_transfer_dest);
1751 if (!ast_parseable_goto(chan, macro_transfer_dest))
1752 ast_set_flag64(peerflags, OPT_GO_ON);
1758 if (ast_test_flag64(&opts, OPT_CALLEE_GOSUB) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GOSUB])) {
1759 struct ast_app *theapp;
1760 const char *gosub_result;
1761 char *gosub_args, *gosub_argstart;
1763 res = ast_autoservice_start(chan);
1764 if (res) {
1765 ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
1766 res = -1;
1769 theapp = pbx_findapp("Gosub");
1771 if (theapp && !res) { /* XXX why check res here ? */
1772 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GOSUB]);
1774 /* Set where we came from */
1775 ast_copy_string(peer->context, "app_dial_gosub_virtual_context", sizeof(peer->context));
1776 ast_copy_string(peer->exten, "s", sizeof(peer->exten));
1777 peer->priority = 0;
1779 gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], ',');
1780 if (gosub_argstart) {
1781 *gosub_argstart = 0;
1782 asprintf(&gosub_args, "%s,s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], gosub_argstart + 1);
1783 *gosub_argstart = ',';
1784 } else {
1785 asprintf(&gosub_args, "%s,s,1", opt_args[OPT_ARG_CALLEE_GOSUB]);
1788 if (gosub_args) {
1789 res = pbx_exec(peer, theapp, gosub_args);
1790 ast_pbx_run(peer);
1791 ast_free(gosub_args);
1792 if (option_debug)
1793 ast_log(LOG_DEBUG, "Gosub exited with status %d\n", res);
1794 } else
1795 ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
1797 res = 0;
1798 } else {
1799 ast_log(LOG_ERROR, "Could not find application Gosub\n");
1800 res = -1;
1803 if (ast_autoservice_stop(chan) < 0) {
1804 ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
1805 res = -1;
1808 if (!res && (gosub_result = pbx_builtin_getvar_helper(peer, "GOSUB_RESULT"))) {
1809 char *gosub_transfer_dest;
1811 if (!strcasecmp(gosub_result, "BUSY")) {
1812 ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
1813 ast_set_flag64(peerflags, OPT_GO_ON);
1814 res = -1;
1815 } else if (!strcasecmp(gosub_result, "CONGESTION") || !strcasecmp(gosub_result, "CHANUNAVAIL")) {
1816 ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
1817 ast_set_flag64(peerflags, OPT_GO_ON);
1818 res = -1;
1819 } else if (!strcasecmp(gosub_result, "CONTINUE")) {
1820 /* hangup peer and keep chan alive assuming the macro has changed
1821 the context / exten / priority or perhaps
1822 the next priority in the current exten is desired.
1824 ast_set_flag64(peerflags, OPT_GO_ON);
1825 res = -1;
1826 } else if (!strcasecmp(gosub_result, "ABORT")) {
1827 /* Hangup both ends unless the caller has the g flag */
1828 res = -1;
1829 } else if (!strncasecmp(gosub_result, "GOTO:", 5) && (gosub_transfer_dest = ast_strdupa(gosub_result + 5))) {
1830 res = -1;
1831 /* perform a transfer to a new extension */
1832 if (strchr(gosub_transfer_dest, '^')) { /* context^exten^priority*/
1833 replace_macro_delimiter(gosub_transfer_dest);
1834 if (!ast_parseable_goto(chan, gosub_transfer_dest))
1835 ast_set_flag64(peerflags, OPT_GO_ON);
1841 if (!res) {
1842 if (calldurationlimit > 0) {
1843 struct timeval whentohangup = { calldurationlimit, 0 };
1844 peer->whentohangup = ast_tvadd(ast_tvnow(), whentohangup);
1846 if (!ast_strlen_zero(dtmfcalled)) {
1847 ast_verb(3, "Sending DTMF '%s' to the called party.\n", dtmfcalled);
1848 res = ast_dtmf_stream(peer, chan, dtmfcalled, 250, 0);
1850 if (!ast_strlen_zero(dtmfcalling)) {
1851 ast_verb(3, "Sending DTMF '%s' to the calling party.\n", dtmfcalling);
1852 res = ast_dtmf_stream(chan, peer, dtmfcalling, 250, 0);
1856 if (res) { /* some error */
1857 res = -1;
1858 end_time = time(NULL);
1859 } else {
1860 if (ast_test_flag64(peerflags, OPT_CALLEE_TRANSFER))
1861 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1862 if (ast_test_flag64(peerflags, OPT_CALLER_TRANSFER))
1863 ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
1864 if (ast_test_flag64(peerflags, OPT_CALLEE_HANGUP))
1865 ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
1866 if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP))
1867 ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
1868 if (ast_test_flag64(peerflags, OPT_CALLEE_MONITOR))
1869 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1870 if (ast_test_flag64(peerflags, OPT_CALLER_MONITOR))
1871 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1872 if (ast_test_flag64(peerflags, OPT_CALLEE_PARK))
1873 ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
1874 if (ast_test_flag64(peerflags, OPT_CALLER_PARK))
1875 ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
1876 if (ast_test_flag64(peerflags, OPT_CALLEE_MIXMONITOR))
1877 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMIXMON);
1878 if (ast_test_flag64(peerflags, OPT_CALLER_MIXMONITOR))
1879 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMIXMON);
1881 if (moh) {
1882 moh = 0;
1883 ast_moh_stop(chan);
1884 } else if (sentringing) {
1885 sentringing = 0;
1886 ast_indicate(chan, -1);
1888 /* Be sure no generators are left on it */
1889 ast_deactivate_generator(chan);
1890 /* Make sure channels are compatible */
1891 res = ast_channel_make_compatible(chan, peer);
1892 if (res < 0) {
1893 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
1894 ast_hangup(peer);
1895 res = -1;
1896 goto done;
1898 if (opermode && !strncmp(chan->name, "Zap", 3) && !strncmp(peer->name, "Zap", 3)) {
1899 /* what's this special handling for Zap <-> Zap ?
1900 * A: Zap to Zap calls are natively bridged at the kernel driver
1901 * level, so we need to ensure that this mode gets propagated
1902 * all the way down. */
1903 struct oprmode oprmode;
1905 oprmode.peer = peer;
1906 oprmode.mode = opermode;
1908 ast_channel_setoption(chan, AST_OPTION_OPRMODE, &oprmode, sizeof(oprmode), 0);
1910 res = ast_bridge_call(chan, peer, &config);
1911 end_time = time(NULL);
1912 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
1913 pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", toast);
1916 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
1917 pbx_builtin_setvar_helper(chan, "DIALEDTIME", toast);
1919 if (ast_test_flag64(&opts, OPT_PEER_H)) {
1920 ast_log(LOG_NOTICE, "PEER context: %s; PEER exten: %s; PEER priority: %d\n",
1921 peer->context, peer->exten, peer->priority);
1924 strcpy(peer->context, chan->context);
1926 if (ast_test_flag64(&opts, OPT_PEER_H) && ast_exists_extension(peer, peer->context, "h", 1, peer->cid.cid_num)) {
1927 int autoloopflag;
1928 int found;
1929 strcpy(peer->exten, "h");
1930 peer->priority = 1;
1931 autoloopflag = ast_test_flag(peer, AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
1932 ast_set_flag(peer, AST_FLAG_IN_AUTOLOOP);
1934 while ((res = ast_spawn_extension(peer, peer->context, peer->exten, peer->priority, peer->cid.cid_num, &found, 1)))
1935 peer->priority++;
1937 if (found && res) {
1938 /* Something bad happened, or a hangup has been requested. */
1939 ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
1940 ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
1942 ast_set2_flag(peer, autoloopflag, AST_FLAG_IN_AUTOLOOP); /* set it back the way it was */
1944 if (res != AST_PBX_NO_HANGUP_PEER) {
1945 if (!ast_check_hangup(peer) && ast_test_flag64(&opts, OPT_CALLEE_GO_ON) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GO_ON])) {
1946 replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GO_ON]);
1947 ast_parseable_goto(peer, opt_args[OPT_ARG_CALLEE_GO_ON]);
1948 ast_pbx_start(peer);
1949 } else {
1950 if (!ast_check_hangup(chan))
1951 chan->hangupcause = peer->hangupcause;
1952 ast_hangup(peer);
1956 out:
1957 if (moh) {
1958 moh = 0;
1959 ast_moh_stop(chan);
1960 } else if (sentringing) {
1961 sentringing = 0;
1962 ast_indicate(chan, -1);
1964 ast_channel_early_bridge(chan, NULL);
1965 hanguptree(outgoing, NULL, 0); /* In this case, there's no answer anywhere */
1966 pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
1967 senddialendevent(chan, pa.status);
1968 ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
1970 if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_KEEPALIVE) && (res != AST_PBX_INCOMPLETE)) {
1971 if (calldurationlimit)
1972 memset(&chan->whentohangup, 0, sizeof(chan->whentohangup));
1973 res = 0;
1976 done:
1977 return res;
1980 static int dial_exec(struct ast_channel *chan, void *data)
1982 struct ast_flags64 peerflags;
1984 memset(&peerflags, 0, sizeof(peerflags));
1986 return dial_exec_full(chan, data, &peerflags, NULL);
1989 static int retrydial_exec(struct ast_channel *chan, void *data)
1991 char *parse;
1992 const char *context = NULL;
1993 int sleep = 0, loops = 0, res = -1;
1994 struct ast_flags64 peerflags = { 0, };
1995 AST_DECLARE_APP_ARGS(args,
1996 AST_APP_ARG(announce);
1997 AST_APP_ARG(sleep);
1998 AST_APP_ARG(retries);
1999 AST_APP_ARG(dialdata);
2002 if (ast_strlen_zero(data)) {
2003 ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
2004 return -1;
2007 parse = ast_strdupa(data);
2008 AST_STANDARD_APP_ARGS(args, parse);
2010 if ((sleep = atoi(args.sleep)))
2011 sleep *= 1000;
2013 loops = atoi(args.retries);
2015 if (!args.dialdata) {
2016 ast_log(LOG_ERROR, "%s requires a 4th argument (dialdata)\n", rapp);
2017 goto done;
2020 if (sleep < 1000)
2021 sleep = 10000;
2023 if (!loops)
2024 loops = -1; /* run forever */
2026 context = pbx_builtin_getvar_helper(chan, "EXITCONTEXT");
2028 res = 0;
2029 while (loops) {
2030 int continue_exec;
2032 chan->data = "Retrying";
2033 if (ast_test_flag(chan, AST_FLAG_MOH))
2034 ast_moh_stop(chan);
2036 res = dial_exec_full(chan, args.dialdata, &peerflags, &continue_exec);
2037 if (continue_exec)
2038 break;
2040 if (res == 0) {
2041 if (ast_test_flag64(&peerflags, OPT_DTMF_EXIT)) {
2042 if (!ast_strlen_zero(args.announce)) {
2043 if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
2044 if (!(res = ast_streamfile(chan, args.announce, chan->language)))
2045 ast_waitstream(chan, AST_DIGIT_ANY);
2046 } else
2047 ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
2049 if (!res && sleep) {
2050 if (!ast_test_flag(chan, AST_FLAG_MOH))
2051 ast_moh_start(chan, NULL, NULL);
2052 res = ast_waitfordigit(chan, sleep);
2054 } else {
2055 if (!ast_strlen_zero(args.announce)) {
2056 if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
2057 if (!(res = ast_streamfile(chan, args.announce, chan->language)))
2058 res = ast_waitstream(chan, "");
2059 } else
2060 ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
2062 if (sleep) {
2063 if (!ast_test_flag(chan, AST_FLAG_MOH))
2064 ast_moh_start(chan, NULL, NULL);
2065 if (!res)
2066 res = ast_waitfordigit(chan, sleep);
2071 if (res < 0 || res == AST_PBX_INCOMPLETE) {
2072 break;
2073 } else if (res > 0) { /* Trying to send the call elsewhere (1 digit ext) */
2074 if (onedigit_goto(chan, context, (char) res, 1)) {
2075 res = 0;
2076 break;
2079 loops--;
2081 if (loops == 0)
2082 res = 0;
2083 else if (res == 1)
2084 res = 0;
2086 if (ast_test_flag(chan, AST_FLAG_MOH))
2087 ast_moh_stop(chan);
2088 done:
2089 return res;
2092 static int unload_module(void)
2094 int res;
2095 struct ast_context *con;
2097 res = ast_unregister_application(app);
2098 res |= ast_unregister_application(rapp);
2100 if ((con = ast_context_find("app_dial_gosub_virtual_context"))) {
2101 ast_context_remove_extension2(con, "s", 1, NULL);
2102 ast_context_destroy(con, "app_dial"); /* leave nothing behind */
2105 return res;
2108 static int load_module(void)
2110 int res;
2111 struct ast_context *con;
2113 con = ast_context_find_or_create(NULL, NULL, "app_dial_gosub_virtual_context", "app_dial");
2114 if (!con)
2115 ast_log(LOG_ERROR, "Dial virtual context 'app_dial_gosub_virtual_context' does not exist and unable to create\n");
2116 else
2117 ast_add_extension2(con, 1, "s", 1, NULL, NULL, "KeepAlive", ast_strdup(""), ast_free_ptr, "app_dial");
2119 res = ast_register_application(app, dial_exec, synopsis, descrip);
2120 res |= ast_register_application(rapp, retrydial_exec, rsynopsis, rdescrip);
2122 return res;
2125 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialing Application");