Skip to content
Snippets Groups Projects
app_dial.c 61.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2005, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
     * \brief dial() & retrydial() - Trivial application to dial a channel and send an URL on answer
    
     *
     * \author Mark Spencer <markster@digium.com>
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \ingroup applications
    
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/time.h>
    #include <sys/signal.h>
    #include <netinet/in.h>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/options.h"
    #include "asterisk/module.h"
    #include "asterisk/translate.h"
    #include "asterisk/say.h"
    #include "asterisk/config.h"
    #include "asterisk/features.h"
    #include "asterisk/musiconhold.h"
    #include "asterisk/callerid.h"
    #include "asterisk/utils.h"
    #include "asterisk/app.h"
    #include "asterisk/causes.h"
    
    #include "asterisk/privacy.h"
    
    Mark Spencer's avatar
    Mark Spencer committed
    static char *tdesc = "Dialing Application";
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    static char *app = "Dial";
    
    
    static char *synopsis = "Place a call and connect to the current channel";
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    static char *descrip =
    
    "  Dial(Technology/resource[&Tech2/resource2...][|timeout][|options][|URL]):\n"
    "This applicaiton will place calls to one or more specified channels. As soon\n"
    "as one of the requested channels answers, the originating channel will be\n"
    "answered, if it has not already been answered. These two channels will then\n"
    "be active in a bridged call. All other channels that were requested will then\n"
    "be hung up.\n"
    "  Unless there is a timeout specified, the Dial application will wait\n"
    "indefinitely until one of the called channels answers, the user hangs up, or\n"
    "if all of the called channels are busy or unavailable. Dialplan executing will\n"
    
    "continue if no requested channels can be called, or if the timeout expires.\n\n"
    
    "  This application sets the following channel variables upon completion:\n"
    "    DIALEDTIME   - This is the time from dialing a channel until when it\n"
    
    "    ANSWEREDTIME - This is the amount of time for actual call.\n"
    "    DIALSTATUS   - This is the status of the call:\n"
    "                   CHANUNAVAIL | CONGESTION | NOANSWER | BUSY | ANSWER | CANCEL\n" 
    "                   DONTCALL | TORTURE\n"
    "  For the Privacy and Screening Modes, the DIALSTATUS variable will be set to\n"
    "DONTCALL if the called party chooses to send the calling party to the 'Go Away'\n"
    "script. The DIALSTATUS variable will be set to TORTURE if the called party\n"
    "wants to send the caller to the 'torture' script.\n"
    "  This application will report normal termination if the originating channel\n"
    "hangs up, or if the call is bridged and either of the parties in the bridge\n"
    "ends the call.\n"
    
    "  The optional URL will be sent to the called party if the channel supports it.\n"
    
    "  If the OUTBOUND_GROUP variable is set, all peer channels created by this\n"
    
    "application will be put into that group (as in Set(GROUP()=...).\n\n"
    
    "  Options:\n"
    "    A(x) - Play an announcement to the called party, using 'x' as the file.\n"
    "    C    - Reset the CDR for this call.\n"
    "    d    - Allow the calling user to dial a 1 digit extension while waiting for\n"
    "           a call to be answered. Exit to that extension if it exists in the\n"
    "           current context, or the context defined in the EXITCONTEXT variable,\n"
    "           if it exists.\n"
    "    D([called][:calling]) - Send the specified DTMF strings *after* the called\n"
    "           party has answered, but before the call gets bridged. The 'called'\n"
    "           DTMF string is sent to the called party, and the 'calling' DTMF\n"
    "           string is sent to the calling party. Both parameters can be used\n"
    "           alone.\n"  	
    "    f    - Force the callerid of the *calling* channel to be set as the\n"
    "           extension associated with the channel using a dialplan 'hint'.\n"
    "           For example, some PSTNs do not allow CallerID to be set to anything\n"
    "           other than the number assigned to the caller.\n"
    "    g    - Proceed with dialplan execution at the current extension if the\n"
    "           destination channel hangs up.\n"
    "    G(context^exten^pri) - If the call is answered, transfer both parties to\n"
    "           the specified priority. Optionally, an extension, or extension and\n"
    "           context may be specified. Otherwise, the current extension is used.\n"
    "    h    - Allow the called party to hang up by sending the '*' DTMF digit.\n"
    "    H    - Allow the calling party to hang up by hitting the '*' DTMF digit.\n"
    "    j    - Jump to priority n+101 if all of the requested channels were busy.\n"
    "    L(x[:y][:z]) - Limit the call to 'x' ms. Play a warning when 'y' ms are\n"
    "           left. Repeat the warning every 'z' ms. The following special\n"
    "           variables can be used with this option:\n"
    "           * LIMIT_PLAYAUDIO_CALLER   yes|no (default yes)\n"
    "                                      Play sounds to the caller.\n"
    "           * LIMIT_PLAYAUDIO_CALLEE   yes|no\n"
    "                                      Play sounds to the callee.\n"
    "           * LIMIT_TIMEOUT_FILE       File to play when time is up.\n"
    "           * LIMIT_CONNECT_FILE       File to play when call begins.\n"
    "           * LIMIT_WARNING_FILE       File to play as warning if 'y' is defined.\n"
    "                                      The default is to say the time remaining.\n"
    "    m([class]) - Provide hold music to the calling party until a requested\n"
    "           channel answers. A specific MusicOnHold class can be\n"
    "           specified.\n"
    "    M(x[^arg]) - Execute the Macro for the *called* channel before connecting\n"
    "           to the calling channel. Arguments can be specified to the Macro\n"
    "           using '^' as a delimeter. The Macro can set the variable\n"
    "           MACRO_RESULT to specify the following actions after the Macro is\n" 
    "           finished executing.\n"
    "           * ABORT        Hangup both legs of the call.\n"
    "           * CONGESTION   Behave as if line congestion was encountered.\n"
    "           * BUSY         Behave as if a busy signal was encountered. This will also\n"
    "                          have the application jump to priority n+101 if the\n"
    "                          'j' option is set.\n"
    "           * CONTINUE     Hangup the called party and allow the calling party\n"
    "                          to continue dialplan execution at the next priority.\n"
    "           * GOTO:<context>^<exten>^<priority> - Transfer the call to the\n"
    "                          specified priority. Optionally, an extension, or\n"
    "                          extension and priority can be specified.\n"
    "    n    - This option is a modifier for the screen/privacy mode. It specifies\n"
    "           that no introductions are to be saved in the priv-callerintros\n"
    "           directory.\n"
    "    N    - This option is a modifier for the screen/privacy mode. It specifies\n"
    "           that if callerID is present, do not screen the call.\n"
    "    o    - Specify that the CallerID that was present on the *calling* channel\n"
    "           be set as the CallerID on the *called* channel. This was the\n"
    "           behavior of Asterisk 1.0 and earlier.\n"
    "    p    - This option enables screening mode. This is basically Privacy mode\n"
    "           without memory.\n"
    "    P([x]) - Enable privacy mode. Use 'x' as the family/key in the database if\n"
    "           it is provided. The current extension is used if a database\n"
    "           family/key is not specified.\n"
    "    r    - Indicate ringing to the calling party. Pass no audio to the calling\n"
    "           party until the called channel has answered.\n"
    "    S(x) - Hang up the call after 'x' seconds *after* the called party has\n"
    "           answered the call.\n"  	
    "    t    - Allow the called party to transfer the calling party by sending the\n"
    
    "           DTMF sequence defined in features.conf.\n"
    
    "    T    - Allow the calling party to transfer the called party by sending the\n"
    "           DTMF sequence defined in features.conf.\n"
    "    w    - Allow the called party to enable recording of the call by sending\n"
    "           the DTMF sequence defined for one-touch recording in features.conf.\n"
    "    W    - Allow the calling party to enable recording of the call by sending\n"
    "           the DTMF sequence defined for one-touch recording in features.conf.\n";
    
    /* RetryDial App by Anthony Minessale II <anthmct@yahoo.com> Jan/2005 */
    static char *rapp = "RetryDial";
    
    static char *rsynopsis = "Place a call, retrying on failure allowing optional exit extension.";
    
    static char *rdescrip =
    
    "  RetryDial(announce|sleep|retries|dialargs): This application will attempt to\n"
    "place a call using the normal Dial application. If no channel can be reached,\n"
    
    "the 'announce' file will be played. Then, it will wait 'sleep' number of\n"
    
    "seconds before retying the call. After 'retires' number of attempts, the\n"
    "calling channel will continue at the next priority in the dialplan. If the\n"
    "'retries' setting is set to 0, this application will retry endlessly.\n"
    "  While waiting to retry a call, a 1 digit extension may be dialed. If that\n"
    "extension exists in either the context defined in ${EXITCONTEXT} or the current\n"
    "one, The call will jump to that extension immediately.\n"
    "  The 'dialargs' are specified in the same format that arguments are provided\n"
    "to the Dial application.\n";
    
    enum {
    
    Olle Johansson's avatar
    Olle Johansson committed
    	OPT_ANNOUNCE =		(1 << 0),
    	OPT_RESETCDR =		(1 << 1),
    	OPT_DTMF_EXIT =		(1 << 2),
    	OPT_SENDDTMF =		(1 << 3),
    	OPT_FORCECLID =		(1 << 4),
    	OPT_GO_ON =		(1 << 5),
    	OPT_CALLEE_HANGUP =	(1 << 6),
    	OPT_CALLER_HANGUP =	(1 << 7),
    	OPT_PRIORITY_JUMP =	(1 << 8),
    	OPT_DURATION_LIMIT =	(1 << 9),
    	OPT_MUSICBACK =		(1 << 10),
    	OPT_CALLEE_MACRO =	(1 << 11),
    	OPT_SCREEN_NOINTRO =	(1 << 12),
    	OPT_SCREEN_NOCLID =	(1 << 13),
    	OPT_ORIGINAL_CLID =	(1 << 14),
    	OPT_SCREENING =		(1 << 15),
    	OPT_PRIVACY =		(1 << 16),
    	OPT_RINGBACK =		(1 << 17),
    	OPT_DURATION_STOP =	(1 << 18),
    	OPT_CALLEE_TRANSFER =	(1 << 19),
    	OPT_CALLER_TRANSFER =	(1 << 20),
    	OPT_CALLEE_MONITOR =	(1 << 21),
    	OPT_CALLER_MONITOR =	(1 << 22),
    	OPT_GOTO =		(1 << 23),
    
    } dial_exec_option_flags;
    
    #define DIAL_STILLGOING			(1 << 30)
    #define DIAL_NOFORWARDHTML		(1 << 31)
    
    enum {
    	OPT_ARG_ANNOUNCE = 0,
    	OPT_ARG_SENDDTMF,
    	OPT_ARG_GOTO,
    	OPT_ARG_DURATION_LIMIT,
    	OPT_ARG_MUSICBACK,
    	OPT_ARG_CALLEE_MACRO,
    	OPT_ARG_PRIVACY,
    	OPT_ARG_DURATION_STOP,
    	/* note: this entry _MUST_ be the last one in the enum */
    	OPT_ARG_ARRAY_SIZE,
    } dial_exec_option_args;
    
    AST_APP_OPTIONS(dial_exec_options, {
    	AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
    	AST_APP_OPTION('C', OPT_RESETCDR),
    	AST_APP_OPTION('d', OPT_DTMF_EXIT),
    	AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
    	AST_APP_OPTION('f', OPT_FORCECLID),
    	AST_APP_OPTION('g', OPT_GO_ON),
    	AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
    	AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
    	AST_APP_OPTION('H', OPT_CALLER_HANGUP),
    	AST_APP_OPTION('j', OPT_PRIORITY_JUMP),
    	AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
    	AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
    	AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
    	AST_APP_OPTION('n', OPT_SCREEN_NOINTRO),
    	AST_APP_OPTION('N', OPT_SCREEN_NOCLID),
    	AST_APP_OPTION('o', OPT_ORIGINAL_CLID),
    	AST_APP_OPTION('p', OPT_SCREENING),
    	AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
    	AST_APP_OPTION('r', OPT_RINGBACK),
    	AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
    	AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
    	AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
    	AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
    	AST_APP_OPTION('W', OPT_CALLER_MONITOR),
    });
    
    /* We define a custom "local user" structure because we
    
    Mark Spencer's avatar
    Mark Spencer committed
       use it not only for keeping track of what is in use but
       also for keeping track of who we're dialing. */
    
    struct localuser {
    	struct ast_channel *chan;
    
    	unsigned int flags;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct localuser *next;
    };
    
    LOCAL_USER_DECL;
    
    static void hanguptree(struct localuser *outgoing, struct ast_channel *exception)
    {
    	/* Hang up a tree of stuff */
    	struct localuser *oo;
    
    	while (outgoing) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* Hangup any existing lines we have open */
    
    		if (outgoing->chan && (outgoing->chan != exception))
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_hangup(outgoing->chan);
    		oo = outgoing;
    		outgoing=outgoing->next;
    		free(oo);
    	}
    }
    
    
    #define HANDLE_CAUSE(cause, chan) do { \
    
    		if (chan->cdr) \
    			ast_cdr_busy(chan->cdr); \
    
    Mark Spencer's avatar
    Mark Spencer committed
    		break; \
    
    		if (chan->cdr) \
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    			ast_cdr_failed(chan->cdr); \
    
    Mark Spencer's avatar
    Mark Spencer committed
    		break; \
    
    	case AST_CAUSE_UNREGISTERED: \
    		if (chan->cdr) \
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    			ast_cdr_failed(chan->cdr); \
    
    Mark Spencer's avatar
    Mark Spencer committed
    		break; \
    
    static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri) 
    
    		if (!ast_goto_if_exists(chan, context, rexten, pri))
    
    		if (!ast_goto_if_exists(chan, chan->context, rexten, pri))
    
    		else if (!ast_strlen_zero(chan->macrocontext)) {
    
    			if (!ast_goto_if_exists(chan, chan->macrocontext, rexten, pri))
    
    static char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
    {
    	char *context;
    	char *exten;
    	if (!ast_strlen_zero(chan->macrocontext))
    		context = chan->macrocontext;
    	else
    		context = chan->context;
    
    	if (!ast_strlen_zero(chan->macroexten))
    		exten = chan->macroexten;
    	else
    		exten = chan->exten;
    
    	if (ast_get_hint(NULL, 0, name, namelen, chan, context, exten))
    		return name;
    	else
    		return "";
    }
    
    
    static void senddialevent(struct ast_channel *src, struct ast_channel *dst)
    {
    	manager_event(EVENT_FLAG_CALL, "Dial", 
    
    Russell Bryant's avatar
    Russell Bryant committed
    			   "Source: %s\r\n"
    			   "Destination: %s\r\n"
    			   "CallerID: %s\r\n"
    			   "CallerIDName: %s\r\n"
    			   "SrcUniqueID: %s\r\n"
    			   "DestUniqueID: %s\r\n",
    			   src->name, dst->name, src->cid.cid_num ? src->cid.cid_num : "<unknown>",
    			   src->cid.cid_name ? src->cid.cid_name : "<unknown>", src->uniqueid,
    			   dst->uniqueid);
    
    static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, struct ast_flags *peerflags, int *sentringing, char *status, size_t statussize, int busystart, int nochanstart, int congestionstart, int priority_jump, int *result)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct localuser *o;
    	int found;
    	int numlines;
    
    	int numbusy = busystart;
    	int numcongestion = congestionstart;
    	int numnochan = nochanstart;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int prestart = busystart + congestionstart + nochanstart;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int orig = *to;
    	struct ast_frame *f;
    	struct ast_channel *peer = NULL;
    
    	struct ast_channel *watchers[AST_MAX_WATCHERS];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int pos;
    	int single;
    	struct ast_channel *winner;
    
    	char cidname[AST_MAX_EXTENSION];
    
    	single = (outgoing && !outgoing->next && !ast_test_flag(outgoing, OPT_MUSICBACK | OPT_RINGBACK));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	
    	if (single) {
    
    		/* Turn off hold music, etc */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* If we are calling a single channel, make them compatible for in-band tone purpose */
    		ast_channel_make_compatible(outgoing->chan, in);
    	}
    	
    
    	while (*to && !peer) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		o = outgoing;
    		found = -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		pos = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		numlines = prestart;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		watchers[0] = in;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			/* Keep track of important channels */
    
    			if (ast_test_flag(o, DIAL_STILLGOING) && o->chan) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				watchers[pos++] = o->chan;
    				found = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    			o = o->next;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			numlines++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (found < 0) {
    
    			if (numlines == (numbusy + numcongestion + numnochan)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if (option_verbose > 2)
    
    					ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, numbusy, numcongestion, numnochan);
    
    					strcpy(status, "BUSY");	
    
    					strcpy(status, "CONGESTION");
    
    					strcpy(status, "CHANUNAVAIL");
    
    				if (ast_opt_priority_jumping || priority_jump)
    
    					ast_goto_if_exists(in, in->context, in->exten, in->priority + 101);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			} else {
    				if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    					ast_verbose(VERBOSE_PREFIX_3 "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, numbusy, numcongestion, numnochan);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			*to = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			return NULL;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		winner = ast_waitfor_n(watchers, pos, to);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		o = outgoing;
    
    			if (ast_test_flag(o, DIAL_STILLGOING) && o->chan && (o->chan->_state == AST_STATE_UP)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if (!peer) {
    					if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    						ast_verbose(VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    					peer = o->chan;
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    					ast_copy_flags(peerflags, o,
    						       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
    						       OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
    						       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
    						       DIAL_NOFORWARDHTML);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				}
    
    			} else if (o->chan && (o->chan == winner)) {
    
    				if (!ast_strlen_zero(o->chan->call_forward)) {
    
    					char *stuff;
    					char *tech;
    
    					const char *forward_context;
    
    
    					ast_copy_string(tmpchan, o->chan->call_forward, sizeof(tmpchan));
    
    					if ((stuff = strchr(tmpchan, '/'))) {
    						*stuff = '\0';
    						stuff++;
    						tech = tmpchan;
    					} else {
    
    						forward_context = pbx_builtin_getvar_helper(o->chan, "FORWARD_CONTEXT");
    						snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, forward_context ? forward_context : o->chan->context);
    
    						stuff = tmpchan;
    						tech = "Local";
    					}
    
    					/* Before processing channel, go ahead and check for forwarding */
    
    					o->forwards++;
    					if (o->forwards < AST_MAX_FORWARDS) {
    						if (option_verbose > 2)
    							ast_verbose(VERBOSE_PREFIX_3 "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, o->chan->name);
    						/* Setup parameters */
    						o->chan = ast_request(tech, in->nativeformats, stuff, &cause);
    						if (!o->chan)
    							ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
    					} else {
    						if (option_verbose > 2)
    							ast_verbose(VERBOSE_PREFIX_3 "Too many forwards from %s\n", o->chan->name);
    						cause = AST_CAUSE_CONGESTION;
    						o->chan = NULL;
    					}
    
    						ast_clear_flag(o, DIAL_STILLGOING);	
    
    						ast_rtp_make_compatible(o->chan, in);
    
    						if (o->chan->cid.cid_num)
    							free(o->chan->cid.cid_num);
    						o->chan->cid.cid_num = NULL;
    						if (o->chan->cid.cid_name)
    							free(o->chan->cid.cid_name);
    						o->chan->cid.cid_name = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    
    						if (ast_test_flag(o, OPT_FORCECLID)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    							char *newcid = NULL;
    
    
    							if (!ast_strlen_zero(in->macroexten))
    
    Mark Spencer's avatar
    Mark Spencer committed
    								newcid = in->macroexten;
    
    Mark Spencer's avatar
    Mark Spencer committed
    								newcid = in->exten;
    
    							o->chan->cid.cid_num = strdup(newcid);
    
    							ast_copy_string(o->chan->accountcode, winner->accountcode, sizeof(o->chan->accountcode));
    
    							o->chan->cdrflags = winner->cdrflags;
    
    							if (!o->chan->cid.cid_num)
    
    Mark Spencer's avatar
    Mark Spencer committed
    								ast_log(LOG_WARNING, "Out of memory\n");
    						} else {
    
    							if (in->cid.cid_num) {
    								o->chan->cid.cid_num = strdup(in->cid.cid_num);
    								if (!o->chan->cid.cid_num)
    									ast_log(LOG_WARNING, "Out of memory\n");	
    							}
    							if (in->cid.cid_name) {
    								o->chan->cid.cid_name = strdup(in->cid.cid_name);
    								if (!o->chan->cid.cid_name)
    
    							ast_copy_string(o->chan->accountcode, in->accountcode, sizeof(o->chan->accountcode));
    
    							o->chan->cdrflags = in->cdrflags;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    
    						if (in->cid.cid_ani) {
    							if (o->chan->cid.cid_ani)
    								free(o->chan->cid.cid_ani);
    
    Russell Bryant's avatar
    Russell Bryant committed
    							o->chan->cid.cid_ani = strdup(in->cid.cid_ani);
    							if (!o->chan->cid.cid_ani)
    
    								ast_log(LOG_WARNING, "Out of memory\n");
    
    						if (o->chan->cid.cid_rdnis) 
    							free(o->chan->cid.cid_rdnis);
    
    							o->chan->cid.cid_rdnis = strdup(in->macroexten);
    
    							o->chan->cid.cid_rdnis = strdup(in->exten);
    
    						if (ast_call(o->chan, tmpchan, 0)) {
    							ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
    
    							ast_clear_flag(o, DIAL_STILLGOING);	
    
    							/* After calling, set callerid to extension */
    
    							if (!ast_test_flag(peerflags, OPT_ORIGINAL_CLID))
    
    								ast_set_callerid(o->chan, ast_strlen_zero(in->macroexten) ? in->exten : in->macroexten, get_cid_name(cidname, sizeof(cidname), in), NULL);
    
    					/* Hangup the original channel now, in case we needed it */
    					ast_hangup(winner);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				f = ast_read(winner);
    				if (f) {
    					if (f->frametype == AST_FRAME_CONTROL) {
    						switch(f->subclass) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    							/* This is our guy if someone answered. */
    							if (!peer) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    								if (option_verbose > 2)
    
    Mark Spencer's avatar
    Mark Spencer committed
    									ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
    								peer = o->chan;
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    								ast_copy_flags(peerflags, o,
    									       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
    									       OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
    									       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
    									       DIAL_NOFORWARDHTML);
    
    Mark Spencer's avatar
    Mark Spencer committed
    							}
    
    							/* If call has been answered, then the eventual hangup is likely to be normal hangup */
    							in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
    							o->chan->hangupcause = AST_CAUSE_NORMAL_CLEARING;
    
    Mark Spencer's avatar
    Mark Spencer committed
    							break;
    						case AST_CONTROL_BUSY:
    							if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    								ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
    
    Martin Pycko's avatar
    Martin Pycko committed
    							ast_hangup(o->chan);
    
    							ast_clear_flag(o, DIAL_STILLGOING);	
    
    Mark Spencer's avatar
    Mark Spencer committed
    							break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    						case AST_CONTROL_CONGESTION:
    							if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    								ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", o->chan->name);
    
    Martin Pycko's avatar
    Martin Pycko committed
    							ast_hangup(o->chan);
    
    							ast_clear_flag(o, DIAL_STILLGOING);
    
    							HANDLE_CAUSE(AST_CAUSE_CONGESTION, in);
    
    Mark Spencer's avatar
    Mark Spencer committed
    							break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    						case AST_CONTROL_RINGING:
    							if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    								ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
    
    							if (!(*sentringing) && !ast_test_flag(outgoing, OPT_MUSICBACK)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    								ast_indicate(in, AST_CONTROL_RINGING);
    
    Mark Spencer's avatar
    Mark Spencer committed
    								(*sentringing)++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    							}
    							break;
    
    						case AST_CONTROL_PROGRESS:
    							if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    								ast_verbose (VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", o->chan->name,in->name);
    
    							if (!ast_test_flag(outgoing, OPT_RINGBACK))
    
    								ast_indicate(in, AST_CONTROL_PROGRESS);
    
    Mark Spencer's avatar
    Mark Spencer committed
    						case AST_CONTROL_VIDUPDATE:
    							if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    								ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", o->chan->name,in->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    							ast_indicate(in, AST_CONTROL_VIDUPDATE);
    							break;
    
    						case AST_CONTROL_PROCEEDING:
    							if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    								ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", o->chan->name,in->name);
    
    							if (!ast_test_flag(outgoing, OPT_RINGBACK))
    
    								ast_indicate(in, AST_CONTROL_PROCEEDING);
    							break;
    
    						case AST_CONTROL_HOLD:
    							if (option_verbose > 2)
    								ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", o->chan->name);
    							ast_indicate(in, AST_CONTROL_HOLD);
    							break;
    						case AST_CONTROL_UNHOLD:
    							if (option_verbose > 2)
    								ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", o->chan->name);
    							ast_indicate(in, AST_CONTROL_UNHOLD);
    							break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    						case AST_CONTROL_OFFHOOK:
    
    Mark Spencer's avatar
    Mark Spencer committed
    						case AST_CONTROL_FLASH:
    							/* Ignore going off hook and flash */
    
    Mark Spencer's avatar
    Mark Spencer committed
    							break;
    
    							if (!ast_test_flag(outgoing, OPT_RINGBACK | OPT_MUSICBACK)) {
    
    Olle Johansson's avatar
    Olle Johansson committed
    									ast_verbose(VERBOSE_PREFIX_3 "%s stopped sounds\n", o->chan->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    								(*sentringing) = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    						default:
    
    Olle Johansson's avatar
    Olle Johansson committed
    							if (option_debug)
    								ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
    
    Mark Spencer's avatar
    Mark Spencer committed
    						}
    
    Mark Spencer's avatar
    Mark Spencer committed
    					} else if (single && (f->frametype == AST_FRAME_VOICE) && 
    
    								!(ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    						if (ast_write(in, f)) 
    
    Olle Johansson's avatar
    Olle Johansson committed
    							ast_log(LOG_WARNING, "Unable to forward voice frame\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    					} else if (single && (f->frametype == AST_FRAME_IMAGE) && 
    
    								!(ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    						if (ast_write(in, f))
    
    Olle Johansson's avatar
    Olle Johansson committed
    							ast_log(LOG_WARNING, "Unable to forward image\n");
    
    					} else if (single && (f->frametype == AST_FRAME_TEXT) && 
    
    								!(ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK))) {
    
    						if (ast_write(in, f))
    
    Olle Johansson's avatar
    Olle Johansson committed
    							ast_log(LOG_WARNING, "Unable to send text\n");
    
    					} else if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag(outgoing, DIAL_NOFORWARDHTML))
    
    Olle Johansson's avatar
    Olle Johansson committed
    						if(ast_channel_sendhtml(in, f->subclass, f->data, f->datalen) == -1)
    							ast_log(LOG_WARNING, "Unable to send URL\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    					ast_frfree(f);
    				} else {
    
    Martin Pycko's avatar
    Martin Pycko committed
    					ast_hangup(o->chan);
    
    					ast_clear_flag(o, DIAL_STILLGOING);
    
    					HANDLE_CAUSE(in->hangupcause, in);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				}
    			}
    			o = o->next;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (winner == in) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			f = ast_read(in);
    #if 0
    			if (f && (f->frametype != AST_FRAME_VOICE))
    
    				printf("Frame type: %d, %d\n", f->frametype, f->subclass);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			else if (!f || (f->frametype != AST_FRAME_VOICE))
    				printf("Hangup received on %s\n", in->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				/* Got hung up */
    
    Olle Johansson's avatar
    Olle Johansson committed
    				*to = -1;
    
    				strcpy(status, "CANCEL");
    
    Mark Spencer's avatar
    Mark Spencer committed
    				return NULL;
    			}
    
    
    			if (f && (f->frametype == AST_FRAME_DTMF)) {
    
    				if (ast_test_flag(peerflags, OPT_DTMF_EXIT)) {
    
    					context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
    
    					if (onedigit_goto(in, context, (char) f->subclass, 1)) {
    
    Olle Johansson's avatar
    Olle Johansson committed
    						if (option_verbose > 2)
    
    							ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
    						*to=0;
    						*result = f->subclass;
    						strcpy(status, "CANCEL");
    						ast_frfree(f);
    						return NULL;
    					}
    				}
    
    
    				if (ast_test_flag(peerflags, OPT_CALLER_HANGUP) && 
    
    Olle Johansson's avatar
    Olle Johansson committed
    						  (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
    					if (option_verbose > 2)
    
    						ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
    					*to=0;
    					strcpy(status, "CANCEL");
    					ast_frfree(f);
    					return NULL;
    				}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    
    			/* Forward HTML stuff */
    			if (single && f && (f->frametype == AST_FRAME_HTML) && !ast_test_flag(outgoing, DIAL_NOFORWARDHTML)) 
    
    Olle Johansson's avatar
    Olle Johansson committed
    				if(ast_channel_sendhtml(outgoing->chan, f->subclass, f->data, f->datalen) == -1)
    					ast_log(LOG_WARNING, "Unable to send URL\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF)))  {
    				if (ast_write(outgoing->chan, f))
    					ast_log(LOG_WARNING, "Unable to forward voice\n");
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (single && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_VIDUPDATE)) {
    				if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    					ast_verbose(VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", in->name,outgoing->chan->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ast_indicate(outgoing->chan, AST_CONTROL_VIDUPDATE);
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!*to && (option_verbose > 2))
    
    Olle Johansson's avatar
    Olle Johansson committed
    			ast_verbose(VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return peer;
    
    static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags *peerflags)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	int res=-1;
    	struct localuser *u;
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	char *tech, *number, *rest, *cur;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct localuser *outgoing=NULL, *tmp;
    	struct ast_channel *peer;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int to;
    
    	int numbusy = 0;
    	int numcongestion = 0;
    	int numnochan = 0;
    	int cause;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char numsubst[AST_MAX_EXTENSION];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char restofit[AST_MAX_EXTENSION];
    
    	char cidname[AST_MAX_EXTENSION];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char *newnum;
    
    	struct ast_bridge_config config;
    	long timelimit = 0;
    	long play_warning = 0;
    	long warning_freq=0;
    
    	const char *warning_sound=NULL;
    	const char *end_sound=NULL;
    	const char *start_sound=NULL;
    
    	char *dtmfcalled=NULL, *dtmfcalling=NULL;
    
    	int play_to_caller=0,play_to_callee=0;
    
    	const char *outbound_group = NULL;
    	const char *macro_result = NULL;
    	char *macro_transfer_dest = NULL;
    
    	int digit = 0, result = 0;
    
    	time_t start_time, answer_time, end_time;
    
    	struct ast_app *app = NULL;
    
    
    	char *parse;
    	AST_DECLARE_APP_ARGS(args,
    			     AST_APP_ARG(peers);
    			     AST_APP_ARG(timeout);
    			     AST_APP_ARG(options);
    			     AST_APP_ARG(url);
    	);
    	struct ast_flags opts = { 0, };
    	char *opt_args[OPT_ARG_ARRAY_SIZE];
    
    	if (ast_strlen_zero(data)) {
    
    		ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    	}
    
    	if (!(parse = ast_strdupa(data))) {
    		LOCAL_USER_REMOVE(u);
    		return -1;
    	}
    
    	AST_STANDARD_APP_ARGS(args, parse);
    
    	if (!ast_strlen_zero(args.options)) {
    
    		if (ast_app_parse_options(dial_exec_options, &opts, opt_args, args.options)) {
    
    			LOCAL_USER_REMOVE(u);
    			return -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (ast_strlen_zero(args.peers)) {
    		ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
    		LOCAL_USER_REMOVE(u);
    		return -1;
    	}
    
    	if (ast_test_flag(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
    		calldurationlimit = atoi(opt_args[OPT_ARG_DURATION_STOP]);
    		if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    			ast_verbose(VERBOSE_PREFIX_3 "Setting call duration limit to %d seconds.\n", calldurationlimit);			
    
    	if (ast_test_flag(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
    		parse = opt_args[OPT_ARG_SENDDTMF];
    		dtmfcalled = strsep(&parse, ":");
    		dtmfcalling = parse;
    	}
    
    	if (ast_test_flag(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
    		char *limit_str, *warning_str, *warnfreq_str;
    
    		parse = opt_args[OPT_ARG_DURATION_LIMIT];
    		limit_str = strsep(&parse, ":");
    		warning_str = strsep(&parse, ":");
    		warnfreq_str = parse;
    
    		timelimit = atol(limit_str);
    		if (warning_str)
    			play_warning = atol(warning_str);
    		if (warnfreq_str)
    			warning_freq = atol(warnfreq_str);
    
    		if (!timelimit) {
    			timelimit = play_to_caller = play_to_callee = play_warning = warning_freq = 0;
    			warning_sound = NULL;
    
    
    		var = pbx_builtin_getvar_helper(chan,"LIMIT_PLAYAUDIO_CALLER");
    		play_to_caller = var ? ast_true(var) : 1;
    		
    		var = pbx_builtin_getvar_helper(chan,"LIMIT_PLAYAUDIO_CALLEE");
    		play_to_callee = var ? ast_true(var) : 0;
    		
    		if (!play_to_caller && !play_to_callee)
    			play_to_caller=1;
    		
    		var = pbx_builtin_getvar_helper(chan,"LIMIT_WARNING_FILE");
    		warning_sound = var ? var : "timeleft";
    		
    		var = pbx_builtin_getvar_helper(chan,"LIMIT_TIMEOUT_FILE");
    		end_sound = var ? var : NULL;
    		
    		var = pbx_builtin_getvar_helper(chan,"LIMIT_CONNECT_FILE");
    		start_sound = var ? var : NULL;
    
    		/* undo effect of S(x) in case they are both used */
    		calldurationlimit = 0; 
    		/* more efficient do it like S(x) does since no advanced opts*/
    		if (!play_warning && !start_sound && !end_sound && timelimit) { 
    			calldurationlimit = timelimit/1000;
    			timelimit = play_to_caller = play_to_callee = play_warning = warning_freq = 0;
    		} else if (option_verbose > 2) {
    			ast_verbose(VERBOSE_PREFIX_3 "Limit Data for this call:\n");
    			ast_verbose(VERBOSE_PREFIX_3 "- timelimit     = %ld\n", timelimit);
    			ast_verbose(VERBOSE_PREFIX_3 "- play_warning  = %ld\n", play_warning);
    			ast_verbose(VERBOSE_PREFIX_3 "- play_to_caller= %s\n", play_to_caller ? "yes" : "no");
    			ast_verbose(VERBOSE_PREFIX_3 "- play_to_callee= %s\n", play_to_callee ? "yes" : "no");
    			ast_verbose(VERBOSE_PREFIX_3 "- warning_freq  = %ld\n", warning_freq);
    			ast_verbose(VERBOSE_PREFIX_3 "- start_sound   = %s\n", start_sound ? start_sound : "UNDEF");
    			ast_verbose(VERBOSE_PREFIX_3 "- warning_sound = %s\n", warning_sound ? warning_sound : "UNDEF");
    			ast_verbose(VERBOSE_PREFIX_3 "- end_sound     = %s\n", end_sound ? end_sound : "UNDEF");
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    
    	if (ast_test_flag(&opts, OPT_RESETCDR) && chan->cdr)
    
    		ast_cdr_reset(chan->cdr, NULL);
    
    	if (ast_test_flag(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
    		opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
    	if (ast_test_flag(&opts, OPT_PRIVACY) || ast_test_flag(&opts, OPT_SCREENING)) {
    
    		l = chan->cid.cid_num;
    
    		if (!ast_strlen_zero(l)) {
    
    			ast_shrink_phone_number(l);
    
    			if( ast_test_flag(&opts, OPT_PRIVACY) ) {
    
    Olle Johansson's avatar
    Olle Johansson committed
    					ast_verbose(VERBOSE_PREFIX_3  "Privacy DB is '%s', clid is '%s'\n",
    
    						     opt_args[OPT_ARG_PRIVACY], l);
    				privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
    
    Olle Johansson's avatar
    Olle Johansson committed
    					ast_verbose(VERBOSE_PREFIX_3  "Privacy Screening, clid is '%s'\n", l);
    
    				privdb_val = AST_PRIVACY_UNKNOWN;
    			}
    		} else {
    			char *tnam, *tn2;
    
    			tnam = ast_strdupa(chan->name);
    			/* clean the channel name so slashes don't try to end up in disk file name */
    			for(tn2 = tnam; *tn2; tn2++) {
    				if( *tn2=='/')
    					*tn2 = '=';  /* any other chars to be afraid of? */
    			}
    			if (option_verbose > 2)
    
    Olle Johansson's avatar
    Olle Johansson committed
    				ast_verbose(VERBOSE_PREFIX_3  "Privacy-- callerid is empty\n");
    
    
    			snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
    			l = callerid;
    			privdb_val = AST_PRIVACY_UNKNOWN;
    		}
    		
    		ast_copy_string(privcid,l,sizeof(privcid));
    
    
    		if( strncmp(privcid,"NOCALLERID",10) != 0 && ast_test_flag(&opts, OPT_SCREEN_NOCLID) ) { /* if callerid is set, and ast_test_flag(&opts, OPT_SCREEN_NOCLID) is set also */  
    
    			if (option_verbose > 2)
    				ast_verbose( VERBOSE_PREFIX_3  "CallerID set (%s); N option set; Screening should be off\n", privcid);
    			privdb_val = AST_PRIVACY_ALLOW;
    		}
    
    Olle Johansson's avatar
    Olle Johansson committed
    		else if(ast_test_flag(&opts, OPT_SCREEN_NOCLID) && strncmp(privcid,"NOCALLERID",10) == 0 ) {
    
    			if (option_verbose > 2)
    				ast_verbose( VERBOSE_PREFIX_3  "CallerID blank; N option set; Screening should happen; dbval is %d\n", privdb_val);
    		}
    		
    
    Olle Johansson's avatar
    Olle Johansson committed
    		if(privdb_val == AST_PRIVACY_DENY ) {
    			if (option_verbose > 2)
    				ast_verbose( VERBOSE_PREFIX_3  "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
    
    Olle Johansson's avatar
    Olle Johansson committed
    		else if(privdb_val == AST_PRIVACY_KILL ) {
    
    			ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 201);
    
    			res = 0;
    			goto out; /* Is this right? */
    		}
    
    Olle Johansson's avatar
    Olle Johansson committed
    		else if(privdb_val == AST_PRIVACY_TORTURE ) {
    
    			ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 301);
    
    			res = 0;
    			goto out; /* is this right??? */
    
    		}
    
    Olle Johansson's avatar
    Olle Johansson committed
    		else if(privdb_val == AST_PRIVACY_UNKNOWN ) {
    
    			/* Get the user's intro, store it in priv-callerintros/$CID, 
    			   unless it is already there-- this should be done before the 
    			   call is actually dialed  */
    
    			/* make sure the priv-callerintros dir exists? */
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    			snprintf(privintro,sizeof(privintro), "priv-callerintros/%s", privcid);
    
    			if( ast_fileexists(privintro,NULL,NULL ) > 0 && strncmp(privcid,"NOCALLERID",10) != 0) {
    				/* the DELUX version of this code would allow this caller the
    				   option to hear and retape their previously recorded intro.
    				*/
    			}
    			else {
    				int duration; /* for feedback from play_and_wait */
    				/* the file doesn't exist yet. Let the caller submit his
    				   vocal intro for posterity */
    				/* priv-recordintro script:
    
    				   "At the tone, please say your name:"
    
    				*/
    				ast_play_and_record(chan, "priv-recordintro", privintro, 4, "gsm", &duration, 128, 2000, 0);  /* NOTE: I've reduced the total time to 4 sec */
    
    Olle Johansson's avatar
    Olle Johansson committed
    										/* don't think we'll need a lock removed, we took care of
    										   conflicts by naming the privintro file */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    
    	/* If a channel group has been specified, get it for use when we create peer channels */
    	outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP");
    
    
    	ast_copy_flags(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID | OPT_CALLER_HANGUP);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	cur = args.peers;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	do {
    		/* Remember where to start next time */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		rest = strchr(cur, '&');
    		if (rest) {
    			*rest = 0;
    			rest++;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* Get a technology/[device:]number pair */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		tech = cur;
    		number = strchr(tech, '/');
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!number) {
    
    			ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    			goto out;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		*number = '\0';
    
    		number++;		
    		if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			goto out;
    		}
    
    			ast_copy_flags(tmp, &opts,
    				       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |