Skip to content
Snippets Groups Projects
app_dial.c 74.4 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 - 2006, 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 "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include <sys/time.h>
    #include <sys/signal.h>
    
    #include <sys/stat.h>
    
    #include <netinet/in.h>
    
    
    #include "asterisk/paths.h"	/* use ast_config_AST_DATA_DIR */
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.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/cdr.h"
    
    #include "asterisk/privacy.h"
    
    #include "asterisk/stringfields.h"
    
    #include "asterisk/global_datastores.h"
    
    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 application 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 | INVALIDARGS\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"
    "  If the OUTBOUND_GROUP_ONCE variable is set, all peer channels created by this\n"
    "application will be put into that group (as in Set(GROUP()=...). Unlike OUTBOUND_GROUP,\n"
    "however, the variable will be unset after use.\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"
    
    "    c    - If DIAL cancels this call, always set the flag to tell the channel\n"
    "           driver that the call is answered elsewhere.\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"
    "    e    - execute the 'h' extension for peer after the call ends\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 the calling party to\n"
    "           the specified priority and the called party to the specified priority+1.\n"
    "           Optionally, an extension, or extension and context may be specified. \n"
    
    "           Otherwise, the current extension is used. You cannot use any additional\n"
    "           action post answer options in conjunction with this option.\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"
    
    "    i    - Asterisk will ignore any forwarding requests it may receive on this\n"
    "           dial attempt.\n"
    
    "    k    - Allow the called party to enable parking of the call by sending\n"
    "           the DTMF sequence defined for call parking in features.conf.\n"
    "    K    - Allow the calling party to enable parking of the call by sending\n"
    "           the DTMF sequence defined for call parking in features.conf.\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.\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"
    
    "           You cannot use any additional action post answer options in conjunction\n"
    
    "           with this option. Also, pbx services are not run on the peer (called) channel,\n"
    "           so you will not be able to set timeouts via the TIMEOUT() function in this macro.\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"
    
    "    O([x]) - \"Operator Services\" mode (Zaptel channel to Zaptel channel\n"
    "             only, if specified on non-Zaptel interface, it will be ignored).\n"
    "             When the destination answers (presumably an operator services\n"
    "             station), the originator no longer has control of their line.\n"
    "             They may hang up, but the switch will not release their line\n"
    "             until the destination party hangs up (the operator). Specified\n"
    "             without an arg, or with 1 as an arg, the originator hanging up\n"
    "             will cause the phone to ring back immediately. With a 2 specified,\n"
    "             when the \"operator\" flashes the trunk, it will ring their phone\n"
    "             back.\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"
    
    "    U(x[^arg]) - Execute via Gosub the routine 'x' for the *called* channel before connecting\n"
    "           to the calling channel. Arguments can be specified to the Gosub\n"
    "           using '^' as a delimeter. The Gosub routine can set the variable\n"
    "           GOSUB_RESULT to specify the following actions after the Gosub returns.\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.\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"
    "           You cannot use any additional action post answer options in conjunction\n"
    "           with this option. Also, pbx services are not run on the peer (called) channel,\n"
    "           so you will not be able to set timeouts via the TIMEOUT() function in this routine.\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"
    "    x    - Allow the called party to enable recording of the call by sending\n"
    "           the DTMF sequence defined for one-touch automixmonitor in features.conf\n"
    "    X    - Allow the calling party to enable recording of the call by sending\n"
    "           the DTMF sequence defined for one-touch automixmonitor 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 {
    
    	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_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),
    	OPT_OPERMODE =          (1 << 24),
    	OPT_CALLEE_PARK =       (1 << 25),
    	OPT_CALLER_PARK =       (1 << 26),
    
    	OPT_CALLEE_MIXMONITOR = (1 << 29),
    	OPT_CALLER_MIXMONITOR = (1 << 30),
    
    #define DIAL_STILLGOING			(1 << 31)
    #define DIAL_NOFORWARDHTML		((uint64_t)1 << 32) /* flags are now 64 bits, so keep it up! */
    
    #define OPT_CANCEL_ELSEWHERE	((uint64_t)1 << 33)
    #define OPT_PEER_H   			((uint64_t)1 << 34)
    
    
    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,
    
    AST_APP_OPTIONS(dial_exec_options, BEGIN_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('i', OPT_IGNORE_FORWARDING),
    
    	AST_APP_OPTION('k', OPT_CALLEE_PARK),
    	AST_APP_OPTION('K', OPT_CALLER_PARK),
    
    	AST_APP_OPTION('k', OPT_CALLEE_PARK),
    	AST_APP_OPTION('K', OPT_CALLER_PARK),
    
    	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_ARG('O', OPT_OPERMODE, OPT_ARG_OPERMODE),
    
    	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_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
    
    	AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
    	AST_APP_OPTION('W', OPT_CALLER_MONITOR),
    
    	AST_APP_OPTION('x', OPT_CALLEE_MIXMONITOR),
    	AST_APP_OPTION('X', OPT_CALLER_MIXMONITOR),
    
    #define CAN_EARLY_BRIDGE(flags) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
    	OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
    	OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK | OPT_CALLER_PARK))
    
    /*
     * The list of active channels
     */
    struct chanlist {
    	struct chanlist *next;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct ast_channel *chan;
    
    static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception, int answered_elsewhere)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	/* Hang up a tree of stuff */
    
    	struct chanlist *oo;
    
    	while (outgoing) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* Hangup any existing lines we have open */
    
    		if (outgoing->chan && (outgoing->chan != exception)) {
    			if (answered_elsewhere)
    				ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_hangup(outgoing->chan);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		oo = outgoing;
    
    /*
     * argument to handle_cause() and other functions.
     */
    struct cause_args {
    	struct ast_channel *chan;
    	int busy;
    	int congestion;
    	int nochan;
    };
    
    static void handle_cause(int cause, struct cause_args *num)
    {
    	struct ast_cdr *cdr = num->chan->cdr;
    
    	switch(cause) {
    	case AST_CAUSE_BUSY:
    		if (cdr)
    			ast_cdr_busy(cdr);
    		num->busy++;
    		break;
    
    	case AST_CAUSE_CONGESTION:
    		if (cdr)
    			ast_cdr_failed(cdr);
    		num->congestion++;
    		break;
    
    	case AST_CAUSE_UNREGISTERED:
    		if (cdr)
    			ast_cdr_failed(cdr);
    		num->nochan++;
    		break;
    
    	case AST_CAUSE_NORMAL_CLEARING:
    		break;
    
    	default:
    		num->nochan++;
    		break;
    	}
    }
    
    /* free the buffer if allocated, and set the pointer to the second arg */
    #define S_REPLACE(s, new_val)		\
    	do {				\
    		if (s)			\
    
    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))
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    static const char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    	const char *context = S_OR(chan->macrocontext, chan->context);
    	const char *exten = S_OR(chan->macroexten, chan->exten);
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    	return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
    
    Olle Johansson's avatar
    Olle Johansson committed
    static void senddialevent(struct ast_channel *src, struct ast_channel *dst, const char *dialstring)
    
    {
    	manager_event(EVENT_FLAG_CALL, "Dial", 
    
    Olle Johansson's avatar
    Olle Johansson committed
    			   "Channel: %s\r\n"
    
    Russell Bryant's avatar
    Russell Bryant committed
    			   "Destination: %s\r\n"
    
    Russell Bryant's avatar
    Russell Bryant committed
    			   "CallerIDName: %s\r\n"
    
    Olle Johansson's avatar
    Olle Johansson committed
    			   "UniqueID: %s\r\n"
    			   "DestUniqueID: %s\r\n"
    			   "Dialstring: %s\r\n",
    
    			   src->name, dst->name, S_OR(src->cid.cid_num, "<unknown>"),
    			   S_OR(src->cid.cid_name, "<unknown>"), src->uniqueid,
    
    Olle Johansson's avatar
    Olle Johansson committed
    			   dst->uniqueid, dialstring ? dialstring : "");
    
    static void senddialendevent(const struct ast_channel *src, const char *dialstatus)
    {
    	manager_event(EVENT_FLAG_CALL, "Dial",
    			   		"SubEvent: End\r\n"
    					"Channel: %s\r\n"
    
    Olle Johansson's avatar
    Olle Johansson committed
    			   		"UniqueID: %s\r\n"
    
    Olle Johansson's avatar
    Olle Johansson committed
    					src->name, src->uniqueid, dialstatus);
    
    /*!
     * helper function for wait_for_answer()
     *
     * XXX this code is highly suspicious, as it essentially overwrites
     * the outgoing channel without properly deleting it.
     */
    
    static void do_forward(struct chanlist *o,
    
    	struct cause_args *num, struct ast_flags64 *peerflags, int single)
    
    {
    	char tmpchan[256];
    
    	struct ast_channel *original = o->chan;
    
    	struct ast_channel *c = o->chan; /* the winner */
    	struct ast_channel *in = num->chan; /* the input channel */
    	char *stuff;
    	char *tech;
    	int cause;
    
    	ast_copy_string(tmpchan, c->call_forward, sizeof(tmpchan));
    	if ((stuff = strchr(tmpchan, '/'))) {
    		*stuff++ = '\0';
    		tech = tmpchan;
    	} else {
    		const char *forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
    		snprintf(tmpchan, sizeof(tmpchan), "%s@%s", c->call_forward, forward_context ? forward_context : c->context);
    		stuff = tmpchan;
    		tech = "Local";
    	}
    	/* Before processing channel, go ahead and check for forwarding */
    
    	ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
    	/* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
    	if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
    		ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
    
    		c = o->chan = NULL;
    
    		cause = AST_CAUSE_BUSY;
    	} else {
    		/* Setup parameters */
    		c = o->chan = ast_request(tech, in->nativeformats, stuff, &cause);
    		if (c) {
    			if (single)
    				ast_channel_make_compatible(o->chan, in);
    			ast_channel_inherit_variables(in, o->chan);
    			ast_channel_datastore_inherit(in, o->chan);
    		} else
    			ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
    
    		ast_clear_flag64(o, DIAL_STILLGOING);	
    
    		handle_cause(cause, num);
    	} else {
    		char *new_cid_num, *new_cid_name;
    		struct ast_channel *src;
    
    		ast_rtp_make_compatible(c, in, single);
    
    		if (ast_test_flag64(o, OPT_FORCECLID)) {
    
    			new_cid_num = ast_strdup(S_OR(in->macroexten, in->exten));
    			new_cid_name = NULL; /* XXX no name ? */
    			src = c;	/* XXX possible bug in previous code, which used 'winner' ? it may have changed */
    		} else {
    			new_cid_num = ast_strdup(in->cid.cid_num);
    			new_cid_name = ast_strdup(in->cid.cid_name);
    			src = in;
    		}
    		ast_string_field_set(c, accountcode, src->accountcode);
    		c->cdrflags = src->cdrflags;
    		S_REPLACE(c->cid.cid_num, new_cid_num);
    		S_REPLACE(c->cid.cid_name, new_cid_name);
    
    		if (in->cid.cid_ani) { /* XXX or maybe unconditional ? */
    			S_REPLACE(c->cid.cid_ani, ast_strdup(in->cid.cid_ani));
    		}
    		S_REPLACE(c->cid.cid_rdnis, ast_strdup(S_OR(in->macroexten, in->exten)));
    		if (ast_call(c, tmpchan, 0)) {
    			ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
    
    			ast_clear_flag64(o, DIAL_STILLGOING);	
    
    			c = o->chan = NULL;
    			num->nochan++;
    		} else {
    
    Olle Johansson's avatar
    Olle Johansson committed
    			senddialevent(in, c, stuff);
    
    			/* After calling, set callerid to extension */
    
    			if (!ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
    
    				char cidname[AST_MAX_EXTENSION] = "";
    
    				ast_set_callerid(c, S_OR(in->macroexten, in->exten), get_cid_name(cidname, sizeof(cidname), in), NULL);
    			}
    
    			/* Hangup the original channel now, in case we needed it */
    
    /* argument used for some functions. */
    struct privacy_args {
            int sentringing;
            int privdb_val;
            char privcid[256];
            char privintro[1024];
            char status[256];
    };
    
    static struct ast_channel *wait_for_answer(struct ast_channel *in,
    
    	struct chanlist *outgoing, int *to, struct ast_flags64 *peerflags,
    
    	const struct cause_args *num_in, int *result)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct cause_args num = *num_in;
    	int prestart = num.busy + num.congestion + num.nochan;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int orig = *to;
    	struct ast_channel *peer = NULL;
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    	/* single is set if only one destination is enabled */
    
    	int single = outgoing && !outgoing->next && !ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
    
    #ifdef HAVE_EPOLL
    	struct chanlist *epollo;
    #endif
    
    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);
    	}
    
    
    #ifdef HAVE_EPOLL
    	for (epollo = outgoing; epollo; epollo = epollo->next)
    		ast_poll_channel_add(in, epollo->chan);
    #endif	
    
    	while (*to && !peer) {
    
    		struct chanlist *o;
    
    		int pos = 0;	/* how many channels do we handle */
    		int numlines = prestart;
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    		struct ast_channel *winner;
    		struct ast_channel *watchers[AST_MAX_WATCHERS];
    
    
    		watchers[pos++] = in;
    		for (o = outgoing; o; o = o->next) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			/* Keep track of important channels */
    
    			if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
    
    Mark Spencer's avatar
    Mark Spencer committed
    				watchers[pos++] = o->chan;
    			numlines++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    		if (pos == 1) {	/* only the input channel is available */
    
    			if (numlines == (num.busy + num.congestion + num.nochan)) {
    
    				ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
    
    				if (num.busy)
    
    					strcpy(pa->status, "BUSY");	
    
    				else if (num.congestion)
    
    					strcpy(pa->status, "CONGESTION");
    
    				else if (num.nochan)
    
    					strcpy(pa->status, "CHANUNAVAIL");
    
    Mark Spencer's avatar
    Mark Spencer committed
    			} else {
    
    				ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
    
    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);
    
    		for (o = outgoing; o; o = o->next) {
    			struct ast_frame *f;
    			struct ast_channel *c = o->chan;
    
    			if (c == NULL)
    				continue;
    
    			if (ast_test_flag64(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if (!peer) {
    
    					ast_verb(3, "%s answered %s\n", c->name, in->name);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    						       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
    						       OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
    						       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
    
    						       OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    						       DIAL_NOFORWARDHTML);
    
    					ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
    
    					ast_copy_string(c->exten, "", sizeof(c->exten));
    
    Mark Spencer's avatar
    Mark Spencer committed
    				}
    
    			/* here, o->chan == c == winner */
    
    			if (!ast_strlen_zero(c->call_forward)) {
    
    				do_forward(o, &num, peerflags, single);
    
    				in->hangupcause = c->hangupcause;
    
    #ifdef HAVE_EPOLL
    				ast_poll_channel_del(in, c);
    #endif
    
    				ast_hangup(c);
    				c = o->chan = NULL;
    
    				ast_clear_flag64(o, DIAL_STILLGOING);
    
    				handle_cause(in->hangupcause, &num);
    
    				continue;
    			}
    			if (f->frametype == AST_FRAME_CONTROL) {
    				switch(f->subclass) {
    				case AST_CONTROL_ANSWER:
    					/* This is our guy if someone answered. */
    					if (!peer) {
    
    						ast_verb(3, "%s answered %s\n", c->name, in->name);
    
    							       OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
    							       OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
    							       OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
    
    							       OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
    
    						ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
    
    						ast_copy_string(c->exten, "", sizeof(c->exten));
    
    						if (CAN_EARLY_BRIDGE(peerflags))
    							/* Setup early bridge if appropriate */
    							ast_channel_early_bridge(in, peer);
    
    					}
    					/* If call has been answered, then the eventual hangup is likely to be normal hangup */
    					in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
    					c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
    					break;
    				case AST_CONTROL_BUSY:
    
    					ast_verb(3, "%s is busy\n", c->name);
    
    					in->hangupcause = c->hangupcause;
    					ast_hangup(c);
    					c = o->chan = NULL;
    
    					ast_clear_flag64(o, DIAL_STILLGOING);	
    
    					handle_cause(AST_CAUSE_BUSY, &num);
    
    					break;
    				case AST_CONTROL_CONGESTION:
    
    					ast_verb(3, "%s is circuit-busy\n", c->name);
    
    					in->hangupcause = c->hangupcause;
    					ast_hangup(c);
    					c = o->chan = NULL;
    
    					ast_clear_flag64(o, DIAL_STILLGOING);
    
    					handle_cause(AST_CAUSE_CONGESTION, &num);
    
    					break;
    				case AST_CONTROL_RINGING:
    
    					ast_verb(3, "%s is ringing\n", c->name);
    
    					/* Setup early media if appropriate */
    
    					if (single && CAN_EARLY_BRIDGE(peerflags))
    
    					if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK)) {
    
    						ast_indicate(in, AST_CONTROL_RINGING);
    
    					}
    					break;
    				case AST_CONTROL_PROGRESS:
    
    					ast_verb(3, "%s is making progress passing it to %s\n", c->name, in->name);
    
    					/* Setup early media if appropriate */
    
    					if (single && CAN_EARLY_BRIDGE(peerflags))
    
    					if (!ast_test_flag64(outgoing, OPT_RINGBACK))
    
    						ast_indicate(in, AST_CONTROL_PROGRESS);
    					break;
    				case AST_CONTROL_VIDUPDATE:
    
    					ast_verb(3, "%s requested a video update, passing it to %s\n", c->name, in->name);
    
    					ast_indicate(in, AST_CONTROL_VIDUPDATE);
    					break;
    				case AST_CONTROL_PROCEEDING:
    
    					ast_verb(3, "%s is proceeding passing it to %s\n", c->name, in->name);
    
    					if (single && CAN_EARLY_BRIDGE(peerflags))
    
    					if (!ast_test_flag64(outgoing, OPT_RINGBACK))
    
    						ast_indicate(in, AST_CONTROL_PROCEEDING);
    					break;
    				case AST_CONTROL_HOLD:
    
    					ast_verb(3, "Call on %s placed on hold\n", c->name);
    
    					ast_indicate(in, AST_CONTROL_HOLD);
    					break;
    				case AST_CONTROL_UNHOLD:
    
    					ast_verb(3, "Call on %s left from hold\n", c->name);
    
    					ast_indicate(in, AST_CONTROL_UNHOLD);
    					break;
    				case AST_CONTROL_OFFHOOK:
    				case AST_CONTROL_FLASH:
    					/* Ignore going off hook and flash */
    					break;
    				case -1:
    
    					if (!ast_test_flag64(outgoing, OPT_RINGBACK | OPT_MUSICBACK)) {
    
    						ast_verb(3, "%s stopped sounds\n", c->name);
    
    					ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    			} else if (single) {
    				/* XXX are we sure the logic is correct ? or we should just switch on f->frametype ? */
    
    				if (f->frametype == AST_FRAME_VOICE && !ast_test_flag64(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    					if (ast_write(in, f)) 
    						ast_log(LOG_WARNING, "Unable to forward voice frame\n");
    
    				} else if (f->frametype == AST_FRAME_IMAGE && !ast_test_flag64(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    					if (ast_write(in, f))
    						ast_log(LOG_WARNING, "Unable to forward image\n");
    
    				} else if (f->frametype == AST_FRAME_TEXT && !ast_test_flag64(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    					if (ast_write(in, f))
    						ast_log(LOG_WARNING, "Unable to send text\n");
    
    				} else if (f->frametype == AST_FRAME_HTML && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)) {
    
    Luigi Rizzo's avatar
    Luigi Rizzo 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
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (winner == in) {
    
    			struct ast_frame *f = ast_read(in);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #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(pa->status, "CANCEL");
    
    				ast_cdr_noanswer(in->cdr);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				return NULL;
    			}
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    			/* now f is guaranteed non-NULL */
    			if (f->frametype == AST_FRAME_DTMF) {
    
    				if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    					const char *context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
    
    					if (onedigit_goto(in, context, (char) f->subclass, 1)) {
    
    						ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
    
    						ast_cdr_noanswer(in->cdr);
    
    						*result = f->subclass;
    
    						strcpy(pa->status, "CANCEL");
    
    				if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) && 
    
    Olle Johansson's avatar
    Olle Johansson committed
    						  (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
    
    					ast_verb(3, "User hit %c to disconnect call.\n", f->subclass);
    
    					strcpy(pa->status, "CANCEL");
    
    					ast_cdr_noanswer(in->cdr);
    
    					ast_frfree(f);
    					return NULL;
    				}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    
    			/* Forward HTML stuff */
    
    			if (single && (f->frametype == AST_FRAME_HTML) && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)) 
    
    				if (ast_channel_sendhtml(outgoing->chan, f->subclass, f->data, f->datalen) == -1)
    
    Olle Johansson's avatar
    Olle Johansson committed
    					ast_log(LOG_WARNING, "Unable to send URL\n");
    
    			if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF_BEGIN) || (f->frametype == AST_FRAME_DTMF_END)))  {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if (ast_write(outgoing->chan, f))
    
    					ast_log(LOG_WARNING, "Unable to forward voice or dtmf\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    
    			if (single && (f->frametype == AST_FRAME_CONTROL) && 
    				((f->subclass == AST_CONTROL_HOLD) || 
    				 (f->subclass == AST_CONTROL_UNHOLD) || 
    				 (f->subclass == AST_CONTROL_VIDUPDATE))) {
    
    				ast_verb(3, "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
    
    				ast_indicate_data(outgoing->chan, f->subclass, f->data, f->datalen);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    
    		if (!*to)
    			ast_verb(3, "Nobody picked up in %d ms\n", orig);
    
    		if (!*to || ast_check_hangup(in)) {
    			ast_cdr_noanswer(in->cdr);
    		}
    		
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (peer && !ast_cdr_log_unanswered()) {
    		/* suppress the CDR's that didn't win */
    		struct chanlist *o;
    		for (o = outgoing; o; o = o->next) {
    			struct ast_channel *c = o->chan;
    			if (c && c != peer && c->cdr) {
    				ast_set_flag(c->cdr, AST_CDR_FLAG_POST_DISABLED);
    			}
    		}
    	} else if (!peer && !ast_cdr_log_unanswered()) {
    			/* suppress the CDR's that didn't win */
    		struct chanlist *o;
    		for (o = outgoing; o; o = o->next) {
    			struct ast_channel *c = o->chan;
    			if (c && c->cdr) {
    				ast_set_flag(c->cdr, AST_CDR_FLAG_POST_DISABLED);		
    			}
    		}
    	}
    	
    
    	for (epollo = outgoing; epollo; epollo = epollo->next) {
    		if (epollo->chan)
    			ast_poll_channel_del(in, epollo->chan);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return peer;
    
    }
    
    static void replace_macro_delimiter(char *s)
    {
    	for (; *s; s++)
    		if (*s == '^')
    
    
    /* returns true if there is a valid privacy reply */
    
    static int valid_priv_reply(struct ast_flags64 *opts, int res)
    
    	if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
    
    	if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
    
    static int do_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
    	char *parse, unsigned int *calldurationlimit)
    {
    
    	char *stringp = ast_strdupa(parse);
    
    	char *limit_str, *warning_str, *warnfreq_str;
    	const char *var;
    
    	limit_str = strsep(&stringp, ":");
    	warning_str = strsep(&stringp, ":");
    	warnfreq_str = strsep(&stringp, ":");
    
    
    	config->timelimit = atol(limit_str);
    	if (warning_str)
    		config->play_warning = atol(warning_str);
    	if (warnfreq_str)
    		config->warning_freq = atol(warnfreq_str);
    
    	if (!config->timelimit) {
    		ast_log(LOG_WARNING, "Dial does not accept L(%s), hanging up.\n", limit_str);
    		config->timelimit = config->play_warning = config->warning_freq = 0;
    		config->warning_sound = NULL;
    		return -1;	/* error */
    	} else if ( (delta = config->play_warning - config->timelimit) > 0) {
    		int w = config->warning_freq;
    
    		/* If the first warning is requested _after_ the entire call would end,
    		   and no warning frequency is requested, then turn off the warning. If
    		   a warning frequency is requested, reduce the 'first warning' time by
    		   that frequency until it falls within the call's total time limit.
    		   Graphically:
    				  timelim->|    delta        |<-playwarning
    			0__________________|_________________|
    					 | w  |    |    |    |
    
    		   so the number of intervals to cut is 1+(delta-1)/w
    		*/
    
    		if (w == 0) {
    			config->play_warning = 0;
    		} else {
    			config->play_warning -= w * ( 1 + (delta-1)/w );
    			if (config->play_warning < 1)
    				config->play_warning = config->warning_freq = 0;
    		}
    	}
    
    
    	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");
    
    	config->warning_sound = S_OR(var, "timeleft");
    
    	/* The code looking at config wants a NULL, not just "", to decide
    	 * that the message should not be played, so we replace "" with NULL.
    	 * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
    	 * not found.
    	 */
    
    	var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
    
    	config->end_sound = S_OR(var, NULL);
    
    	var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
    
    	config->start_sound = S_OR(var, NULL);
    
    	/* undo effect of S(x) in case they are both used */
    	*calldurationlimit = 0;
    	/* more efficient to do it like S(x) does since no advanced opts */
    	if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
    		*calldurationlimit = config->timelimit / 1000;
    
    		ast_verb(3, "Setting call duration limit to %d seconds.\n",
    
    				*calldurationlimit);
    		config->timelimit = play_to_caller = play_to_callee =
    		config->play_warning = config->warning_freq = 0;
    
    	} else {
    		ast_verb(3, "Limit Data for this call:\n");
    		ast_verb(4, "timelimit      = %ld\n", config->timelimit);
    		ast_verb(4, "play_warning   = %ld\n", config->play_warning);
    		ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
    		ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
    		ast_verb(4, "warning_freq   = %ld\n", config->warning_freq);
    		ast_verb(4, "start_sound    = %s\n", S_OR(config->start_sound, ""));
    		ast_verb(4, "warning_sound  = %s\n", config->warning_sound);
    		ast_verb(4, "end_sound      = %s\n", S_OR(config->end_sound, ""));
    
    	}
            if (play_to_caller)
                    ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
            if (play_to_callee)
                    ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
    	return 0;
    }
    
    
    static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
    
        struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
    
    {
    
    	int res2;
    	int loopcount = 0;
    
    	/* 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  */
    
    	/* all ring indications and moh for the caller has been halted as soon as the 
    	   target extension was picked up. We are going to have to kill some
    	   time and make the caller believe the peer hasn't picked up yet */
    
    
    	if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
    
    		char *original_moh = ast_strdupa(chan->musicclass);
    
    		ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
    
    		ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
    
    		ast_string_field_set(chan, musicclass, original_moh);
    
    	} else if (ast_test_flag64(opts, OPT_RINGBACK)) {
    
    		ast_indicate(chan, AST_CONTROL_RINGING);