Sat Jul 26 06:13:28 2008

Asterisk developer's documentation


app_dial.c File Reference

dial() & retrydial() - Trivial application to dial a channel and send an URL on answer More...

#include "asterisk.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <netinet/in.h>
#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/rtp.h"
#include "asterisk/cdr.h"
#include "asterisk/manager.h"
#include "asterisk/privacy.h"
#include "asterisk/stringfields.h"
#include "asterisk/global_datastores.h"
#include "asterisk/transcap.h"

Include dependency graph for app_dial.c:

Go to the source code of this file.

Data Structures

struct  dial_localuser

Defines

#define AST_MAX_WATCHERS   256
#define CAN_EARLY_BRIDGE(flags)
#define DIAL_NOFORWARDHTML   (1 << 31)
#define DIAL_STILLGOING   (1 << 30)
#define HANDLE_CAUSE(cause, chan)

Enumerations

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_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),
  OPT_OPERMODE = (1 << 24), OPT_CALLEE_PARK = (1 << 25), OPT_CALLER_PARK = (1 << 26), OPT_IGNORE_FORWARDING = (1 << 27),
  OPT_NOINBAND = (1 << 28), OPT_CALLBACK_INIT = (1 << 29)
}
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,
  OPT_ARG_OPERMODE, OPT_ARG_ARRAY_SIZE
}

Functions

 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('i', OPT_IGNORE_FORWARDING), AST_APP_OPTION('j', OPT_PRIORITY_JUMP), 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('R', OPT_NOINBAND), AST_APP_OPTION('c', OPT_CALLBACK_INIT), 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),})
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Dialing Application")
static int dial_exec (struct ast_channel *chan, void *data)
static const char * get_cid_name (char *name, int namelen, struct ast_channel *chan)
static void hanguptree (struct dial_localuser *outgoing, struct ast_channel *exception)
static int load_module (void)
static int onedigit_goto (struct ast_channel *chan, const char *context, char exten, int pri)
static void replace_macro_delimiter (char *s)
static int retrydial_exec (struct ast_channel *chan, void *data)
static void senddialevent (struct ast_channel *src, struct ast_channel *dst)
static int unload_module (void)
static int valid_priv_reply (struct ast_flags *opts, int res)
static struct ast_channelwait_for_answer (struct ast_channel *in, struct dial_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)

Variables

static char * app = "Dial"
static char * descrip
enum { ... }  dial_exec_option_args
enum { ... }  dial_exec_option_flags
static char * rapp = "RetryDial"
static char * rdescrip
static char * rsynopsis = "Place a call, retrying on failure allowing optional exit extension."
static char * synopsis = "Place a call and connect to the current channel"


Detailed Description

dial() & retrydial() - Trivial application to dial a channel and send an URL on answer

Author:
Mark Spencer <markster@digium.com>

Definition in file app_dial.c.


Define Documentation

#define AST_MAX_WATCHERS   256

Definition at line 343 of file app_dial.c.

#define CAN_EARLY_BRIDGE ( flags   ) 

Value:

Definition at line 314 of file app_dial.c.

Referenced by wait_for_answer().

#define DIAL_NOFORWARDHTML   (1 << 31)

Definition at line 265 of file app_dial.c.

Referenced by wait_for_answer().

#define DIAL_STILLGOING   (1 << 30)

Definition at line 264 of file app_dial.c.

Referenced by wait_for_answer().

#define HANDLE_CAUSE ( cause,
chan   ) 

Definition at line 345 of file app_dial.c.

Referenced by wait_for_answer().


Enumeration Type Documentation

anonymous enum

Enumerator:
OPT_ANNOUNCE 
OPT_RESETCDR 
OPT_DTMF_EXIT 
OPT_SENDDTMF 
OPT_FORCECLID 
OPT_GO_ON 
OPT_CALLEE_HANGUP 
OPT_CALLER_HANGUP 
OPT_PRIORITY_JUMP 
OPT_DURATION_LIMIT 
OPT_MUSICBACK 
OPT_CALLEE_MACRO 
OPT_SCREEN_NOINTRO 
OPT_SCREEN_NOCLID 
OPT_ORIGINAL_CLID 
OPT_SCREENING 
OPT_PRIVACY 
OPT_RINGBACK 
OPT_DURATION_STOP 
OPT_CALLEE_TRANSFER 
OPT_CALLER_TRANSFER 
OPT_CALLEE_MONITOR 
OPT_CALLER_MONITOR 
OPT_GOTO 
OPT_OPERMODE 
OPT_CALLEE_PARK 
OPT_CALLER_PARK 
OPT_IGNORE_FORWARDING 
OPT_NOINBAND 
OPT_CALLBACK_INIT 

Definition at line 231 of file app_dial.c.

00231      {
00232    OPT_ANNOUNCE =    (1 << 0),
00233    OPT_RESETCDR =    (1 << 1),
00234    OPT_DTMF_EXIT =      (1 << 2),
00235    OPT_SENDDTMF =    (1 << 3),
00236    OPT_FORCECLID =      (1 << 4),
00237    OPT_GO_ON =    (1 << 5),
00238    OPT_CALLEE_HANGUP =  (1 << 6),
00239    OPT_CALLER_HANGUP =  (1 << 7),
00240    OPT_PRIORITY_JUMP =  (1 << 8),
00241    OPT_DURATION_LIMIT = (1 << 9),
00242    OPT_MUSICBACK =      (1 << 10),
00243    OPT_CALLEE_MACRO =   (1 << 11),
00244    OPT_SCREEN_NOINTRO = (1 << 12),
00245    OPT_SCREEN_NOCLID =  (1 << 13),
00246    OPT_ORIGINAL_CLID =  (1 << 14),
00247    OPT_SCREENING =      (1 << 15),
00248    OPT_PRIVACY =     (1 << 16),
00249    OPT_RINGBACK =    (1 << 17),
00250    OPT_DURATION_STOP =  (1 << 18),
00251    OPT_CALLEE_TRANSFER =   (1 << 19),
00252    OPT_CALLER_TRANSFER =   (1 << 20),
00253    OPT_CALLEE_MONITOR = (1 << 21),
00254    OPT_CALLER_MONITOR = (1 << 22),
00255    OPT_GOTO =     (1 << 23),
00256    OPT_OPERMODE =       (1 << 24),
00257    OPT_CALLEE_PARK = (1 << 25),
00258    OPT_CALLER_PARK = (1 << 26),
00259    OPT_IGNORE_FORWARDING = (1 << 27),
00260    OPT_NOINBAND =       (1 << 28),
00261    OPT_CALLBACK_INIT =  (1 << 29),
00262 } dial_exec_option_flags;

anonymous enum

Enumerator:
OPT_ARG_ANNOUNCE 
OPT_ARG_SENDDTMF 
OPT_ARG_GOTO 
OPT_ARG_DURATION_LIMIT 
OPT_ARG_MUSICBACK 
OPT_ARG_CALLEE_MACRO 
OPT_ARG_PRIVACY 
OPT_ARG_DURATION_STOP 
OPT_ARG_OPERMODE 
OPT_ARG_ARRAY_SIZE 

Definition at line 267 of file app_dial.c.

00267      {
00268    OPT_ARG_ANNOUNCE = 0,
00269    OPT_ARG_SENDDTMF,
00270    OPT_ARG_GOTO,
00271    OPT_ARG_DURATION_LIMIT,
00272    OPT_ARG_MUSICBACK,
00273    OPT_ARG_CALLEE_MACRO,
00274    OPT_ARG_PRIVACY,
00275    OPT_ARG_DURATION_STOP,
00276    OPT_ARG_OPERMODE,
00277    /* note: this entry _MUST_ be the last one in the enum */
00278    OPT_ARG_ARRAY_SIZE,
00279 } dial_exec_option_args;


Function Documentation

AST_APP_OPTIONS ( dial_exec_options   ) 

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Dialing Application"   
)

static int dial_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 1770 of file app_dial.c.

Referenced by load_module().

01771 {
01772    struct ast_flags peerflags;
01773 
01774    memset(&peerflags, 0, sizeof(peerflags));
01775 
01776    return dial_exec_full(chan, data, &peerflags, NULL);
01777 }

static const char* get_cid_name ( char *  name,
int  namelen,
struct ast_channel chan 
) [static]

Definition at line 390 of file app_dial.c.

References ast_get_hint(), dial_localuser::chan, ast_channel::context, context, ast_channel::exten, exten, ast_channel::macrocontext, ast_channel::macroexten, and S_OR.

Referenced by wait_for_answer().

00391 {
00392    const char *context = S_OR(chan->macrocontext, chan->context);
00393    const char *exten = S_OR(chan->macroexten, chan->exten);
00394 
00395    return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
00396 }

static void hanguptree ( struct dial_localuser outgoing,
struct ast_channel exception 
) [static]

Definition at line 329 of file app_dial.c.

References ast_hangup(), dial_localuser::chan, free, and dial_localuser::next.

00330 {
00331    /* Hang up a tree of stuff */
00332    struct dial_localuser *oo;
00333    while (outgoing) {
00334       /* Hangup any existing lines we have open */
00335       if (outgoing->chan && (outgoing->chan != exception))
00336          ast_hangup(outgoing->chan);
00337       oo = outgoing;
00338       outgoing=outgoing->next;
00339       free(oo);
00340    }
00341 }

static int load_module ( void   )  [static]

Definition at line 1907 of file app_dial.c.

References ast_register_application(), dial_exec(), and retrydial_exec().

01908 {
01909    int res;
01910 
01911    res = ast_register_application(app, dial_exec, synopsis, descrip);
01912    res |= ast_register_application(rapp, retrydial_exec, rsynopsis, rdescrip);
01913    
01914    return res;
01915 }

static int onedigit_goto ( struct ast_channel chan,
const char *  context,
char  exten,
int  pri 
) [static]

Definition at line 371 of file app_dial.c.

References ast_goto_if_exists(), ast_strlen_zero(), dial_localuser::chan, ast_channel::context, and ast_channel::macrocontext.

Referenced by retrydial_exec(), and wait_for_answer().

00372 {
00373    char rexten[2] = { exten, '\0' };
00374 
00375    if (context) {
00376       if (!ast_goto_if_exists(chan, context, rexten, pri))
00377          return 1;
00378    } else {
00379       if (!ast_goto_if_exists(chan, chan->context, rexten, pri))
00380          return 1;
00381       else if (!ast_strlen_zero(chan->macrocontext)) {
00382          if (!ast_goto_if_exists(chan, chan->macrocontext, rexten, pri))
00383             return 1;
00384       }
00385    }
00386    return 0;
00387 }

static void replace_macro_delimiter ( char *  s  )  [static]

Definition at line 791 of file app_dial.c.

00792 {
00793    for (; *s; s++)
00794       if (*s == '^')
00795          *s = '|';
00796 }

static int retrydial_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 1779 of file app_dial.c.

References AST_DIGIT_ANY, ast_fileexists(), AST_FLAG_MOH, ast_log(), ast_module_user_add, ast_module_user_remove, ast_moh_start(), ast_moh_stop(), ast_strdupa, ast_streamfile(), ast_strlen_zero(), ast_test_flag, ast_waitfordigit(), ast_waitstream(), context, ast_channel::data, LOG_ERROR, LOG_WARNING, onedigit_goto(), OPT_DTMF_EXIT, and pbx_builtin_getvar_helper().

Referenced by load_module().

01780 {
01781    char *announce = NULL, *dialdata = NULL;
01782    const char *context = NULL;
01783    int sleep = 0, loops = 0, res = -1;
01784    struct ast_module_user *u;
01785    struct ast_flags peerflags;
01786    
01787    if (ast_strlen_zero(data)) {
01788       ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
01789       return -1;
01790    }  
01791 
01792    u = ast_module_user_add(chan);
01793 
01794    announce = ast_strdupa(data);
01795 
01796    memset(&peerflags, 0, sizeof(peerflags));
01797 
01798    if ((dialdata = strchr(announce, '|'))) {
01799       *dialdata++ = '\0';
01800       if (sscanf(dialdata, "%d", &sleep) == 1) {
01801          sleep *= 1000;
01802       } else {
01803          ast_log(LOG_ERROR, "%s requires the numerical argument <sleep>\n",rapp);
01804          goto done;
01805       }
01806       if ((dialdata = strchr(dialdata, '|'))) {
01807          *dialdata++ = '\0';
01808          if (sscanf(dialdata, "%d", &loops) != 1) {
01809             ast_log(LOG_ERROR, "%s requires the numerical argument <loops>\n",rapp);
01810             goto done;
01811          }
01812       }
01813    }
01814    
01815    if ((dialdata = strchr(dialdata, '|'))) {
01816       *dialdata++ = '\0';
01817    } else {
01818       ast_log(LOG_ERROR, "%s requires more arguments\n",rapp);
01819       goto done;
01820    }
01821       
01822    if (sleep < 1000)
01823       sleep = 10000;
01824 
01825    if (!loops)
01826       loops = -1; /* run forever */
01827    
01828    context = pbx_builtin_getvar_helper(chan, "EXITCONTEXT");
01829 
01830    res = 0;
01831    while (loops) {
01832       int continue_exec;
01833 
01834       chan->data = "Retrying";
01835       if (ast_test_flag(chan, AST_FLAG_MOH))
01836          ast_moh_stop(chan);
01837 
01838       res = dial_exec_full(chan, dialdata, &peerflags, &continue_exec);
01839       if (continue_exec)
01840          break;
01841 
01842       if (res == 0) {
01843          if (ast_test_flag(&peerflags, OPT_DTMF_EXIT)) {
01844             if (!ast_strlen_zero(announce)) {
01845                if (ast_fileexists(announce, NULL, chan->language) > 0) {
01846                   if(!(res = ast_streamfile(chan, announce, chan->language)))                      
01847                      ast_waitstream(chan, AST_DIGIT_ANY);
01848                } else
01849                   ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", announce);
01850             }
01851             if (!res && sleep) {
01852                if (!ast_test_flag(chan, AST_FLAG_MOH))
01853                   ast_moh_start(chan, NULL, NULL);
01854                res = ast_waitfordigit(chan, sleep);
01855             }
01856          } else {
01857             if (!ast_strlen_zero(announce)) {
01858                if (ast_fileexists(announce, NULL, chan->language) > 0) {
01859                   if (!(res = ast_streamfile(chan, announce, chan->language)))
01860                      res = ast_waitstream(chan, "");
01861                } else
01862                   ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", announce);
01863             }
01864             if (sleep) {
01865                if (!ast_test_flag(chan, AST_FLAG_MOH))
01866                   ast_moh_start(chan, NULL, NULL);
01867                if (!res)
01868                   res = ast_waitfordigit(chan, sleep);
01869             }
01870          }
01871       }
01872 
01873       if (res < 0)
01874          break;
01875       else if (res > 0) { /* Trying to send the call elsewhere (1 digit ext) */
01876          if (onedigit_goto(chan, context, (char) res, 1)) {
01877             res = 0;
01878             break;
01879          }
01880       }
01881       loops--;
01882    }
01883    if (loops == 0)
01884       res = 0;
01885    else if (res == 1)
01886       res = 0;
01887 
01888    if (ast_test_flag(chan, AST_FLAG_MOH))
01889       ast_moh_stop(chan);
01890  done:
01891    ast_module_user_remove(u);
01892    return res;
01893 }

static void senddialevent ( struct ast_channel src,
struct ast_channel dst 
) [static]

Definition at line 398 of file app_dial.c.

References ast_channel::cid, ast_callerid::cid_name, ast_callerid::cid_num, EVENT_FLAG_CALL, manager_event(), and S_OR.

Referenced by wait_for_answer().

00399 {
00400    /* XXX do we need also CallerIDnum ? */
00401    manager_event(EVENT_FLAG_CALL, "Dial", 
00402             "Source: %s\r\n"
00403             "Destination: %s\r\n"
00404             "CallerID: %s\r\n"
00405             "CallerIDName: %s\r\n"
00406             "SrcUniqueID: %s\r\n"
00407             "DestUniqueID: %s\r\n",
00408             src->name, dst->name, S_OR(src->cid.cid_num, "<unknown>"),
00409             S_OR(src->cid.cid_name, "<unknown>"), src->uniqueid,
00410             dst->uniqueid);
00411 }

static int unload_module ( void   )  [static]

Definition at line 1895 of file app_dial.c.

References ast_module_user_hangup_all, and ast_unregister_application().

01896 {
01897    int res;
01898 
01899    res = ast_unregister_application(app);
01900    res |= ast_unregister_application(rapp);
01901 
01902    ast_module_user_hangup_all();
01903    
01904    return res;
01905 }

static int valid_priv_reply ( struct ast_flags opts,
int  res 
) [static]

Definition at line 800 of file app_dial.c.

References ast_test_flag, OPT_PRIVACY, and OPT_SCREENING.

00801 {
00802    if (res < '1')
00803       return 0;
00804    if (ast_test_flag(opts, OPT_PRIVACY) && res <= '5')
00805       return 1;
00806    if (ast_test_flag(opts, OPT_SCREENING) && res <= '4')
00807       return 1;
00808    return 0;
00809 }

static struct ast_channel* wait_for_answer ( struct ast_channel in,
struct dial_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 
) [static]

Definition at line 413 of file app_dial.c.

References ast_channel::_state, accountcode, ast_call(), AST_CAUSE_BUSY, AST_CAUSE_CONGESTION, AST_CAUSE_NORMAL_CLEARING, ast_cdr_noanswer(), ast_channel_datastore_inherit(), ast_channel_inherit_variables(), ast_channel_make_compatible(), ast_channel_sendhtml(), ast_check_hangup(), ast_clear_flag, AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_FLASH, AST_CONTROL_HANGUP, AST_CONTROL_HOLD, AST_CONTROL_OFFHOOK, AST_CONTROL_PROCEEDING, AST_CONTROL_PROGRESS, AST_CONTROL_RINGING, AST_CONTROL_SRCUPDATE, AST_CONTROL_UNHOLD, AST_CONTROL_VIDUPDATE, ast_copy_flags, ast_deactivate_generator(), AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_HTML, AST_FRAME_IMAGE, AST_FRAME_TEXT, AST_FRAME_VOICE, ast_frfree, ast_goto_if_exists(), ast_hangup(), ast_indicate(), ast_indicate_data(), ast_log(), AST_MAX_EXTENSION, AST_MAX_WATCHERS, ast_opt_priority_jumping, ast_read(), ast_request(), ast_rtp_early_bridge(), ast_rtp_make_compatible(), ast_set_callerid(), AST_STATE_UP, ast_strdup, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_verbose(), ast_waitfor_n(), ast_write(), CAN_EARLY_BRIDGE, ast_channel::cdr, ast_channel::cdrflags, dial_localuser::chan, ast_channel::cid, ast_callerid::cid_ani, ast_callerid::cid_name, ast_callerid::cid_num, context, ast_channel::context, DIAL_NOFORWARDHTML, DIAL_STILLGOING, ast_channel::dialcontext, ast_channel::exten, f, free, get_cid_name(), HANDLE_CAUSE, ast_channel::hangupcause, LOG_DEBUG, LOG_NOTICE, LOG_WARNING, ast_channel::macroexten, ast_channel::nativeformats, dial_localuser::next, onedigit_goto(), OPT_CALLBACK_INIT, OPT_CALLEE_HANGUP, OPT_CALLEE_MONITOR, OPT_CALLEE_PARK, OPT_CALLEE_TRANSFER, OPT_CALLER_HANGUP, OPT_CALLER_MONITOR, OPT_CALLER_PARK, OPT_CALLER_TRANSFER, OPT_DTMF_EXIT, OPT_FORCECLID, OPT_IGNORE_FORWARDING, OPT_MUSICBACK, OPT_NOINBAND, OPT_ORIGINAL_CLID, OPT_RINGBACK, option_debug, option_verbose, pbx_builtin_getvar_helper(), S_OR, senddialevent(), ast_channel::tech, VERBOSE_PREFIX_2, and VERBOSE_PREFIX_3.

Referenced by try_calling().

00414 {
00415    int numbusy = busystart;
00416    int numcongestion = congestionstart;
00417    int numnochan = nochanstart;
00418    int prestart = busystart + congestionstart + nochanstart;
00419    int orig = *to;
00420    struct ast_channel *peer = NULL;
00421    /* single is set if only one destination is enabled */
00422    int single = (outgoing && !outgoing->next && !ast_test_flag(outgoing, OPT_MUSICBACK | OPT_RINGBACK | OPT_NOINBAND));
00423    
00424    if (single) {
00425       /* Turn off hold music, etc */
00426       ast_deactivate_generator(in);
00427       /* If we are calling a single channel, make them compatible for in-band tone purpose */
00428       ast_channel_make_compatible(outgoing->chan, in);
00429    }
00430    
00431    
00432    while (*to && !peer) {
00433       struct dial_localuser *o;
00434       int pos = 0;   /* how many channels do we handle */
00435       int numlines = prestart;
00436       struct ast_channel *winner;
00437       struct ast_channel *watchers[AST_MAX_WATCHERS];
00438 
00439       watchers[pos++] = in;
00440       for (o = outgoing; o; o = o->next) {
00441          /* Keep track of important channels */
00442          if (ast_test_flag(o, DIAL_STILLGOING) && o->chan)
00443             watchers[pos++] = o->chan;
00444          numlines++;
00445       }
00446       if (pos == 1) {   /* only the input channel is available */
00447          if (numlines == (numbusy + numcongestion + numnochan)) {
00448             if (option_verbose > 2)
00449                ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, numbusy, numcongestion, numnochan);
00450             if (numbusy)
00451                strcpy(status, "BUSY"); 
00452             else if (numcongestion)
00453                strcpy(status, "CONGESTION");
00454             else if (numnochan)
00455                strcpy(status, "CHANUNAVAIL");
00456             if (ast_opt_priority_jumping || priority_jump)
00457                ast_goto_if_exists(in, in->context, in->exten, in->priority + 101);
00458          } else {
00459             if (option_verbose > 2)
00460                ast_verbose(VERBOSE_PREFIX_3 "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, numbusy, numcongestion, numnochan);
00461          }
00462          *to = 0;
00463          return NULL;
00464       }
00465       winner = ast_waitfor_n(watchers, pos, to);
00466       for (o = outgoing; o; o = o->next) {
00467          struct ast_frame *f;
00468          struct ast_channel *c = o->chan;
00469 
00470          if (c == NULL)
00471             continue;
00472          if (ast_test_flag(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
00473             if (!peer) {
00474                if (option_verbose > 2)
00475                   ast_verbose(VERBOSE_PREFIX_3 "%s answered %s\n", c->name, in->name);
00476                peer = c;
00477                ast_copy_flags(peerflags, o,
00478                          OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
00479                          OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
00480                          OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
00481                          OPT_CALLEE_PARK | OPT_CALLER_PARK |
00482                          DIAL_NOFORWARDHTML);
00483                ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
00484                ast_copy_string(c->exten, "", sizeof(c->exten));
00485             }
00486             continue;
00487          }
00488          if (c != winner)
00489             continue;
00490          if (!ast_strlen_zero(c->call_forward)) {
00491             char tmpchan[256];
00492             char *stuff;
00493             char *tech;
00494             int cause;
00495 
00496             ast_copy_string(tmpchan, c->call_forward, sizeof(tmpchan));
00497             if ((stuff = strchr(tmpchan, '/'))) {
00498                *stuff++ = '\0';
00499                tech = tmpchan;
00500             } else {
00501                const char *forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
00502                snprintf(tmpchan, sizeof(tmpchan), "%s@%s", c->call_forward, forward_context ? forward_context : c->context);
00503                stuff = tmpchan;
00504                tech = "Local";
00505             }
00506             /* Before processing channel, go ahead and check for forwarding */
00507             if (option_verbose > 2)
00508                ast_verbose(VERBOSE_PREFIX_3 "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
00509             /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
00510             if (ast_test_flag(peerflags, OPT_IGNORE_FORWARDING)) {
00511                if (option_verbose > 2)
00512                   ast_verbose(VERBOSE_PREFIX_3 "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
00513                c = o->chan = NULL;
00514                cause = AST_CAUSE_BUSY;
00515             } else {
00516                /* Setup parameters */
00517                if ((c = o->chan = ast_request(tech, in->nativeformats, stuff, &cause))) {
00518                   if (single)
00519                      ast_channel_make_compatible(o->chan, in);
00520                   ast_channel_inherit_variables(in, o->chan);
00521                   ast_channel_datastore_inherit(in, o->chan);
00522                } else
00523                   ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
00524             }
00525             if (!c) {
00526                ast_clear_flag(o, DIAL_STILLGOING); 
00527                HANDLE_CAUSE(cause, in);
00528             } else {
00529                ast_rtp_make_compatible(c, in, single);
00530                if (c->cid.cid_num)
00531                   free(c->cid.cid_num);
00532                c->cid.cid_num = NULL;
00533                if (c->cid.cid_name)
00534                   free(c->cid.cid_name);
00535                c->cid.cid_name = NULL;
00536 
00537                if (ast_test_flag(o, OPT_FORCECLID)) {
00538                   c->cid.cid_num = ast_strdup(S_OR(in->macroexten, in->exten));
00539                   ast_string_field_set(c, accountcode, winner->accountcode);
00540                   c->cdrflags = winner->cdrflags;
00541                } else {
00542                   c->cid.cid_num = ast_strdup(in->cid.cid_num);
00543                   c->cid.cid_name = ast_strdup(in->cid.cid_name);
00544                   ast_string_field_set(c, accountcode, in->accountcode);
00545                   c->cdrflags = in->cdrflags;
00546                }
00547 
00548                if (in->cid.cid_ani) {
00549                   if (c->cid.cid_ani)
00550                      free(c->cid.cid_ani);
00551                   c->cid.cid_ani = ast_strdup(in->cid.cid_ani);
00552                }
00553                if (c->cid.cid_rdnis) 
00554                   free(c->cid.cid_rdnis);
00555                c->cid.cid_rdnis = ast_strdup(S_OR(in->macroexten, in->exten));
00556                if (ast_call(c, tmpchan, 0)) {
00557                   ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
00558                   ast_clear_flag(o, DIAL_STILLGOING); 
00559                   ast_hangup(c);
00560                   c = o->chan = NULL;
00561                   numnochan++;
00562                } else {
00563                   senddialevent(in, c);
00564                   /* After calling, set callerid to extension */
00565                   if (!ast_test_flag(peerflags, OPT_ORIGINAL_CLID)) {
00566                      char cidname[AST_MAX_EXTENSION] = "";
00567                      ast_set_callerid(c, S_OR(in->macroexten, in->exten), get_cid_name(cidname, sizeof(cidname), in), NULL);
00568                   }
00569                }
00570             }
00571             /* Hangup the original channel now, in case we needed it */
00572             ast_hangup(winner);
00573             continue;
00574          }
00575          f = ast_read(winner);
00576          if (!f) {
00577             in->hangupcause = c->hangupcause;
00578             ast_hangup(c);
00579             c = o->chan = NULL;
00580             ast_clear_flag(o, DIAL_STILLGOING);
00581             HANDLE_CAUSE(in->hangupcause, in);
00582             continue;
00583          }
00584          if (f->frametype == AST_FRAME_CONTROL) {
00585             switch(f->subclass) {
00586             case AST_CONTROL_ANSWER:
00587                /* This is our guy if someone answered. */
00588                if (!peer) {
00589                   if (option_verbose > 2)
00590                      ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", c->name, in->name);
00591                   peer = c;
00592                   ast_copy_flags(peerflags, o,
00593                             OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
00594                             OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
00595                             OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
00596                             OPT_CALLEE_PARK | OPT_CALLER_PARK |
00597                             DIAL_NOFORWARDHTML);
00598                   ast_copy_string(c->dialcontext, "", sizeof(c->dialcontext));
00599                   ast_copy_string(c->exten, "", sizeof(c->exten));
00600                   /* Setup RTP early bridge if appropriate */
00601                   if (CAN_EARLY_BRIDGE(peerflags))
00602                      ast_rtp_early_bridge(in, peer);
00603                }
00604                /* If call has been answered, then the eventual hangup is likely to be normal hangup */
00605                in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
00606                c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
00607                break;
00608             case AST_CONTROL_BUSY:
00609                if (option_verbose > 2)
00610                   ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", c->name);
00611                in->hangupcause = c->hangupcause;
00612                ast_hangup(c);
00613                c = o->chan = NULL;
00614                ast_clear_flag(o, DIAL_STILLGOING); 
00615                HANDLE_CAUSE(AST_CAUSE_BUSY, in);
00616                break;
00617             case AST_CONTROL_CONGESTION:
00618                if (option_verbose > 2)
00619                   ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", c->name);
00620                in->hangupcause = c->hangupcause;
00621                ast_hangup(c);
00622                c = o->chan = NULL;
00623                ast_clear_flag(o, DIAL_STILLGOING);
00624                HANDLE_CAUSE(AST_CAUSE_CONGESTION, in);
00625                break;
00626             case AST_CONTROL_RINGING:
00627                                          if (ast_test_flag(peerflags, OPT_CALLBACK_INIT)) {
00628                                              if (option_verbose > 2)
00629                                                  ast_verbose( VERBOSE_PREFIX_3 "%s is ringing, hanging up.\n", o->chan->name);
00630                                              return NULL;
00631                                          } else {
00632                    if (option_verbose > 2)
00633                   ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", c->name);
00634                    /* Setup early media if appropriate */
00635                    if (single && CAN_EARLY_BRIDGE(peerflags))
00636                   ast_rtp_early_bridge(in, c);
00637                    if (!(*sentringing) && !ast_test_flag(outgoing, OPT_MUSICBACK)) {
00638                   ast_indicate(in, AST_CONTROL_RINGING);
00639                   (*sentringing)++;
00640                    }
00641                }
00642                break;
00643             case AST_CONTROL_PROGRESS:
00644                if (option_verbose > 2)
00645                   ast_verbose (VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", c->name, in->name);
00646                /* Setup early media if appropriate */
00647                if (single && CAN_EARLY_BRIDGE(peerflags))
00648                   ast_rtp_early_bridge(in, c);
00649                if (!ast_test_flag(outgoing, OPT_RINGBACK | OPT_NOINBAND))
00650                   ast_indicate(in, AST_CONTROL_PROGRESS);
00651                break;
00652             case AST_CONTROL_VIDUPDATE:
00653                if (option_verbose > 2)
00654                   ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", c->name, in->name);
00655                ast_indicate(in, AST_CONTROL_VIDUPDATE);
00656                break;
00657             case AST_CONTROL_SRCUPDATE:
00658                if (option_verbose > 2)
00659                   ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", c->name, in->name);
00660                ast_indicate(in, AST_CONTROL_SRCUPDATE);
00661                break;
00662             case AST_CONTROL_PROCEEDING:
00663                if (option_verbose > 2)
00664                   ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", c->name, in->name);
00665                if (single && CAN_EARLY_BRIDGE(peerflags))
00666                   ast_rtp_early_bridge(in, c);
00667                if (!ast_test_flag(outgoing, OPT_RINGBACK | OPT_NOINBAND))
00668                   ast_indicate(in, AST_CONTROL_PROCEEDING);
00669                break;
00670             case AST_CONTROL_HOLD:
00671                if (option_verbose > 2)
00672                   ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", c->name);
00673                ast_indicate(in, AST_CONTROL_HOLD);
00674                break;
00675             case AST_CONTROL_UNHOLD:
00676                if (option_verbose > 2)
00677                   ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", c->name);
00678                ast_indicate(in, AST_CONTROL_UNHOLD);
00679                break;
00680             case AST_CONTROL_OFFHOOK:
00681             case AST_CONTROL_FLASH:
00682                /* Ignore going off hook and flash */
00683                break;
00684             case -1:
00685                if (!ast_test_flag(outgoing, OPT_RINGBACK | OPT_MUSICBACK | OPT_NOINBAND)) {
00686                   if (option_verbose > 2)
00687                      ast_verbose(VERBOSE_PREFIX_3 "%s stopped sounds\n", c->name);
00688                   ast_indicate(in, -1);
00689                   (*sentringing) = 0;
00690                }
00691                break;
00692             default:
00693                if (option_debug)
00694                   ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
00695             }
00696          } else if (single) {
00697             /* XXX are we sure the logic is correct ? or we should just switch on f->frametype ? */
00698             if (f->frametype == AST_FRAME_VOICE && !ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
00699                if (ast_write(in, f)) 
00700                   ast_log(LOG_WARNING, "Unable to forward voice frame\n");
00701             } else if (f->frametype == AST_FRAME_IMAGE && !ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
00702                if (ast_write(in, f))
00703                   ast_log(LOG_WARNING, "Unable to forward image\n");
00704             } else if (f->frametype == AST_FRAME_TEXT && !ast_test_flag(outgoing, OPT_RINGBACK|OPT_MUSICBACK)) {
00705                if (ast_write(in, f))
00706                   ast_log(LOG_WARNING, "Unable to send text\n");
00707             } else if (f->frametype == AST_FRAME_HTML && !ast_test_flag(outgoing, DIAL_NOFORWARDHTML)) {
00708                if (ast_channel_sendhtml(in, f->subclass, f->data, f->datalen) == -1)
00709                   ast_log(LOG_WARNING, "Unable to send URL\n");
00710             }
00711          }
00712          ast_frfree(f);
00713       } /* end for */
00714       if (winner == in) {
00715          struct ast_frame *f = ast_read(in);
00716 #if 0
00717          if (f && (f->frametype != AST_FRAME_VOICE))
00718             printf("Frame type: %d, %d\n", f->frametype, f->subclass);
00719          else if (!f || (f->frametype != AST_FRAME_VOICE))
00720             printf("Hangup received on %s\n", in->name);
00721 #endif
00722          if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
00723             /* Got hung up */
00724             *to = -1;
00725             ast_cdr_noanswer(in->cdr);
00726             strcpy(status, "CANCEL");
00727             if (f)
00728                ast_frfree(f);
00729             return NULL;
00730          }
00731 
00732          if (f && (f->frametype == AST_FRAME_DTMF)) {
00733             if (ast_test_flag(peerflags, OPT_DTMF_EXIT)) {
00734                const char *context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
00735                if (onedigit_goto(in, context, (char) f->subclass, 1)) {
00736                   if (option_verbose > 2)
00737                      ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
00738                   *to=0;
00739                   ast_cdr_noanswer(in->cdr);
00740                   *result = f->subclass;
00741                   strcpy(status, "CANCEL");
00742                   ast_frfree(f);
00743                   return NULL;
00744                }
00745             }
00746 
00747             if (ast_test_flag(peerflags, OPT_CALLER_HANGUP) && 
00748                     (f->subclass == '*')) { /* hmm it it not guaranteed to be '*' anymore. */
00749                if (option_verbose > 2)
00750                   ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
00751                *to=0;
00752                ast_cdr_noanswer(in->cdr);
00753                strcpy(status, "CANCEL");
00754                ast_frfree(f);
00755                return NULL;
00756             }
00757          }
00758 
00759          /* Forward HTML stuff */
00760          if (single && f && (f->frametype == AST_FRAME_HTML) && !ast_test_flag(outgoing, DIAL_NOFORWARDHTML)) 
00761             if(ast_channel_sendhtml(outgoing->chan, f->subclass, f->data, f->datalen) == -1)
00762                ast_log(LOG_WARNING, "Unable to send URL\n");
00763          
00764 
00765          if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF_BEGIN) || (f->frametype == AST_FRAME_DTMF_END)))  {
00766             if (ast_write(outgoing->chan, f))
00767                ast_log(LOG_WARNING, "Unable to forward voice or dtmf\n");
00768          }
00769          if (single && (f->frametype == AST_FRAME_CONTROL) && 
00770             ((f->subclass == AST_CONTROL_HOLD) || 
00771              (f->subclass == AST_CONTROL_UNHOLD) || 
00772              (f->subclass == AST_CONTROL_VIDUPDATE) ||
00773              (f->subclass == AST_CONTROL_SRCUPDATE))) {
00774             if (option_verbose > 2)
00775                ast_verbose(VERBOSE_PREFIX_3 "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
00776             ast_indicate_data(outgoing->chan, f->subclass, f->data, f->datalen);
00777          }
00778          ast_frfree(f);
00779       }
00780       if (!*to && (option_verbose > 2))
00781          ast_verbose(VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
00782       if (!*to || ast_check_hangup(in)) {
00783          ast_cdr_noanswer(in->cdr);
00784       }
00785       
00786    }
00787    
00788    return peer;
00789 }


Variable Documentation

char* app = "Dial" [static]

Definition at line 76 of file app_dial.c.

char* descrip [static]

Definition at line 80 of file app_dial.c.

enum { ... } dial_exec_option_args

enum { ... } dial_exec_option_flags

char* rapp = "RetryDial" [static]

Definition at line 216 of file app_dial.c.

char* rdescrip [static]

Definition at line 218 of file app_dial.c.

char* rsynopsis = "Place a call, retrying on failure allowing optional exit extension." [static]

Definition at line 217 of file app_dial.c.

char* synopsis = "Place a call and connect to the current channel" [static]

Definition at line 78 of file app_dial.c.


Generated on Sat Jul 26 06:13:28 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1