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