Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2005, Digium, Inc.
* 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.
*
* 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 Core PBX routines.
*
* \author Mark Spencer <markster@digium.com>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
Kevin P. Fleming
committed
#include "asterisk/lock.h"
#include "asterisk/cli.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/logger.h"
#include "asterisk/file.h"
#include "asterisk/callerid.h"
#include "asterisk/cdr.h"
#include "asterisk/config.h"
#include "asterisk/term.h"
#include "asterisk/manager.h"
#include "asterisk/ast_expr.h"
#include "asterisk/linkedlists.h"
#include "asterisk/say.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/musiconhold.h"
#include "asterisk/app.h"
Kevin P. Fleming
committed
#include "asterisk/devicestate.h"
*
* The speed of extension handling will likely be among the most important
* aspects of this PBX. The switching scheme as it exists right now isn't
* terribly bad (it's O(N+M), where N is the # of extensions and M is the avg #
* of priorities, but a constant search time here would be great ;-)
*
*/
#ifdef LOW_MEMORY
#define EXT_DATA_SIZE 256
#else
#define EXT_DATA_SIZE 8192
#endif
#define SWITCH_DATA_LENGTH 256
#define VAR_NORMAL 1
#define VAR_SOFTTRAN 2
#define VAR_HARDTRAN 3
Mark Spencer
committed
#define BACKGROUND_SKIP (1 << 0)
#define BACKGROUND_NOANSWER (1 << 1)
Kevin P. Fleming
committed
#define BACKGROUND_MATCHEXTEN (1 << 2)
#define BACKGROUND_PLAYBACK (1 << 3)
Mark Spencer
committed
AST_APP_OPTIONS(background_opts, {
AST_APP_OPTION('s', BACKGROUND_SKIP),
AST_APP_OPTION('n', BACKGROUND_NOANSWER),
AST_APP_OPTION('m', BACKGROUND_MATCHEXTEN),
AST_APP_OPTION('p', BACKGROUND_PLAYBACK),
Mark Spencer
committed
});
#define WAITEXTEN_MOH (1 << 0)
AST_APP_OPTIONS(waitexten_opts, {
AST_APP_OPTION_ARG('m', WAITEXTEN_MOH, 1),
Mark Spencer
committed
});
The dialplan is saved as a linked list with each context
having it's own linked list of extensions - one item per
priority.
*/
char *exten; /*!< Extension name */
int matchcid; /*!< Match caller id ? */
char *cidmatch; /*!< Caller id to match for this extension */
int priority; /*!< Priority */
char *label; /*!< Label */
struct ast_context *parent; /*!< The context this extension belongs to */
char *app; /*!< Application to execute */
void *data; /*!< Data to use (arguments) */
void (*datad)(void *); /*!< Data destructor */
struct ast_exten *peer; /*!< Next higher priority with our extension */
const char *registrar; /*!< Registrar */
struct ast_exten *next; /*!< Extension with a greater ID */
/*! \brief ast_include: include= support in extensions.conf */
char *rname; /*!< Context to include */
const char *registrar; /*!< Registrar */
int hastime; /*!< If time construct exists */
struct ast_timing timing; /*!< time construct */
struct ast_include *next; /*!< Link them together */
/*! \brief ast_sw: Switch statement in extensions.conf */
const char *registrar; /*!< Registrar */
char *data; /*!< Data load */
int eval;
char *tmpdata;
/*! \brief ast_ignorepat: Ignore patterns in dial plan */
const char *registrar;
/*! \brief ast_context: An extension context */
ast_mutex_t lock; /*!< A lock to prevent multiple threads from clobbering the context */
struct ast_exten *root; /*!< The root of the list of extensions */
struct ast_context *next; /*!< Link them together */
struct ast_include *includes; /*!< Include other contexts */
struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */
const char *registrar; /*!< Registrar */
struct ast_sw *alts; /*!< Alternative switches */
char name[0]; /*!< Name of the context */
/*! \brief ast_app: A registered application */
struct ast_app {
int (*execute)(struct ast_channel *chan, void *data);
const char *synopsis; /*!< Synopsis text for 'show applications' */
const char *description; /*!< Description (help text) for 'show application <name>' */
struct ast_app *next; /*!< Next app in list */
char name[0]; /*!< Name of the application */
/*! \brief ast_state_cb: An extension state notify register item */
struct ast_state_cb {
int id;
void *data;
ast_state_cb_type callback;
struct ast_state_cb *next;
/*! \brief Structure for dial plan hints
Hints are pointers from an extension in the dialplan to one or
more devices (tech/name) */
struct ast_hint {
struct ast_exten *exten; /*!< Extension */
int laststate; /*!< Last known state */
struct ast_state_cb *callbacks; /*!< Callback list for this extension */
struct ast_hint *next; /*!< Pointer to next hint in list */
int ast_pbx_outgoing_cdr_failed(void);
static int pbx_builtin_answer(struct ast_channel *, void *);
static int pbx_builtin_goto(struct ast_channel *, void *);
static int pbx_builtin_hangup(struct ast_channel *, void *);
static int pbx_builtin_background(struct ast_channel *, void *);
static int pbx_builtin_wait(struct ast_channel *, void *);
static int pbx_builtin_waitexten(struct ast_channel *, void *);
static int pbx_builtin_resetcdr(struct ast_channel *, void *);
static int pbx_builtin_setamaflags(struct ast_channel *, void *);
static int pbx_builtin_ringing(struct ast_channel *, void *);
static int pbx_builtin_progress(struct ast_channel *, void *);
static int pbx_builtin_congestion(struct ast_channel *, void *);
static int pbx_builtin_busy(struct ast_channel *, void *);
static int pbx_builtin_setglobalvar(struct ast_channel *, void *);
static int pbx_builtin_noop(struct ast_channel *, void *);
static int pbx_builtin_gotoif(struct ast_channel *, void *);
static int pbx_builtin_gotoiftime(struct ast_channel *, void *);
static int pbx_builtin_execiftime(struct ast_channel *, void *);
static int pbx_builtin_saynumber(struct ast_channel *, void *);
static int pbx_builtin_saydigits(struct ast_channel *, void *);
static int pbx_builtin_saycharacters(struct ast_channel *, void *);
static int pbx_builtin_sayphonetic(struct ast_channel *, void *);
Martin Pycko
committed
int pbx_builtin_setvar(struct ast_channel *, void *);
static int pbx_builtin_importvar(struct ast_channel *, void *);
AST_MUTEX_DEFINE_STATIC(maxcalllock);
static int countcalls = 0;
AST_MUTEX_DEFINE_STATIC(acflock); /*!< Lock for the custom function list */
static struct ast_custom_function *acf_root = NULL;
/*! \brief Declaration of builtin applications */
static struct pbx_builtin {
char name[AST_MAX_APP];
int (*execute)(struct ast_channel *chan, void *data);
} builtins[] =
{
/* These applications are built into the PBX core and do not
Mark Spencer
committed
" Answer([delay]): If the call has not been answered, this application will\n"
"answer it. Otherwise, it has no effect on the call. If a delay is specified,\n"
"Asterisk will wait this number of milliseconds before answering the call.\n"
Mark Spencer
committed
{ "BackGround", pbx_builtin_background,
"Play a file while awaiting extension",
Kevin P. Fleming
committed
" Background(filename1[&filename2...][|options[|langoverride][|context]]):\n"
"This application will play the given list of files while waiting for an\n"
"extension to be dialed by the calling channel. To continue waiting for digits\n"
"after this application has finished playing files, the WaitExten application\n"
"should be used. The 'langoverride' option explicity specifies which language\n"
"to attempt to use for the requested sound files. If a 'context' is specified,\n"
"this is the dialplan context that this application will use when exiting to a\n"
"dialed extension."
" If one of the requested sound files does not exist, call processing will be\n"
"terminated.\n"
Mark Spencer
committed
" Options:\n"
" s - causes the playback of the message to be skipped\n"
Mark Spencer
committed
" if the channel is not in the 'up' state (i.e. it\n"
" hasn't been answered yet.) If this happens, the\n"
" application will return immediately.\n"
" n - don't answer the channel before playing the files\n"
" m - only break if a digit hit matches a one digit\n"
" extension in the destination context\n"
Mark Spencer
committed
{ "Busy", pbx_builtin_busy,
"Indicate the Busy condition",
" Busy([timeout]): This application will indicate the busy condition to\n"
"the calling channel. If the optional timeout is specified, the calling channel\n"
"will be hung up after the specified number of seconds. Otherwise, this\n"
"application will wait until the calling channel hangs up.\n"
Mark Spencer
committed
{ "Congestion", pbx_builtin_congestion,
"Indicate the Congestion condition",
" Congestion([timeout]): This application will indicate the congenstion\n"
"condition to the calling channel. If the optional timeout is specified, the\n"
"calling channel will be hung up after the specified number of seconds.\n"
"Otherwise, this application will wait until the calling channel hangs up.\n"
Mark Spencer
committed
{ "Goto", pbx_builtin_goto,
"Jump to a particular priority, extension, or context",
" Goto([[context|]extension|]priority): This application will cause the\n"
"calling channel to continue dialplan execution at the specified priority.\n"
"If no specific extension, or extension and context, are specified, then this\n"
"application will jump to the specified priority of the current extension.\n"
" If the attempt to jump to another location in the dialplan is not successful,\n"
"then the channel will continue at the next priority of the current extension.\n"
Mark Spencer
committed
{ "GotoIf", pbx_builtin_gotoif,
" GotoIf(Condition?[label1]:[label2]): This application will cause the calling\n"
"channel to jump to the speicifed location in the dialplan based on the\n"
"evaluation of the given condition. The channel will continue at 'label1' if the\n"
"condition is true, or 'label2' if the condition is false. The labels are\n"
"specified in the same syntax that is used with the Goto application.\n"
"Conditional Goto based on the current time",
" GotoIfTime(<times>|<weekdays>|<mdays>|<months>?[[context|]exten|]priority):\n"
"This application will have the calling channel jump to the speicified location\n"
"int the dialplan if the current time matches the given time specification.\n"
"Further information on the time specification can be found in examples\n"
"illustrating how to do time-based context includes in the dialplan.\n"
{ "ExecIfTime", pbx_builtin_execiftime,
"Conditional application execution based on the current time",
" ExecIfTime(<times>|<weekdays>|<mdays>|<months>?appname[|appargs]):\n"
"This application will execute the specified dialplan application, with optional\n"
"arguments, if the current time matches the given time specification. Further\n"
"information on the time speicification can be found in examples illustrating\n"
"how to do time-based context includes in the dialplan.\n"
Mark Spencer
committed
{ "Hangup", pbx_builtin_hangup,
"Hang up the calling channel",
" Hangup(): This application will hang up the calling channel.\n"
Mark Spencer
committed
{ "NoOp", pbx_builtin_noop,
"Do Nothing",
" NoOp(): This applicatiion does nothing. However, it is useful for debugging\n"
"purposes. Any text that is provided as arguments to this application can be\n"
"viewed at the Asterisk CLI. This method can be used to see the evaluations of\n"
"variables or functions without having any effect."
" Progress(): This application will request that in-band progress information\n"
"be provided to the calling channel.\n"
{ "ResetCDR", pbx_builtin_resetcdr,
" ResetCDR([options]): This application causes the Call Data Record to be\n"
"reset.\n"
" Options:\n"
" w -- Store the current CDR record before resetting it.\n"
" a -- Store any stacked records.\n"
" v -- Save CDR variables.\n"
" Ringing(): This application will request that the channel indicate a ringing\n"
"tone to the user.\n"
Mark Spencer
committed
{ "SayNumber", pbx_builtin_saynumber,
" SayNumber(digits[,gender]): This application will play the sounds that\n"
"correspond to the given number. Optionally, a gender may be specified.\n"
"This will use the language that is currently set for the channel. See the\n"
"LANGUAGE function for more information on setting the language for the channel.\n"
Mark Spencer
committed
{ "SayDigits", pbx_builtin_saydigits,
" SayDigits(digits): This application will play the sounds that correspond\n"
"to the digits of the given number. This will use the language that is currently\n"
"set for the channel. See the LANGUAGE function for more information on setting\n"
"the language for the channel.\n"
{ "SayAlpha", pbx_builtin_saycharacters,
" SayAlpha(string): This application will play the sounds that correspond to\n"
"the letters of the given string.\n"
{ "SayPhonetic", pbx_builtin_sayphonetic,
" SayPhonetic(string): This application will play the sounds from the phonetic\n"
"alphabet that correspond to the letters in the given string.\n"
{ "SetAMAFlags", pbx_builtin_setamaflags,
"Set the AMA Flags",
" SetAMAFlags([flag]): This channel will set the channel's AMA Flags for billing\n"
"purposes.\n"
{ "SetGlobalVar", pbx_builtin_setglobalvar,
"Set a global variable to a given value",
" SetGlobalVar(variable=value): This application sets a given global variable to\n"
"the specified value.\n"
{ "Set", pbx_builtin_setvar,
"Set channel variable(s) or function value(s)",
" Set(name1=value1|name2=value2|..[|options])\n"
"This function can be used to set the value of channel variables or dialplan\n"
"functions. It will accept up to 24 name/value pairs. When setting variables,\n"
"if the variable name is prefixed with _, the variable will be inherited into\n"
"channels created from the current channel. If the variable name is prefixed\n"
"with __, the variable will be inherited into channels created from the current\n"
"channel and all children channels.\n"
" Options:\n"
" g - Set variable globally instead of on the channel\n"
" (applies only to variables, not functions)\n"
"Import a variable from a channel into a new variable",
" ImportVar(newvar=channelname|variable): This application imports a variable\n"
"from the specified channel (as opposed to the current one) and stores it as\n"
"a variable in the current channel (the channel that is calling this\n"
"application). Variables created by this application have the same inheritance\n"
"properties as those created with the Set application. See the documentation for\n"
"Set for more information.\n"
Mark Spencer
committed
{ "Wait", pbx_builtin_wait,
" Wait(seconds): This application waits for a specified number of seconds.\n"
"Then, dialplan execution will continue at the next priority.\n"
" Note that the seconds can be passed with fractions of a second. For example,\n"
"'1.5' will ask the application to wait for 1.5 seconds.\n"
"Waits for an extension to be entered",
" WaitExten([seconds][|options]): This application waits for the user to enter\n"
"a new extension for a specified number of seconds.\n"
" Note that the seconds can be passed with fractions of a second. For example,\n"
"'1.5' will ask the application to wait for 1.5 seconds.\n"
Mark Spencer
committed
" Options:\n"
" m[(x)] - Provide music on hold to the caller while waiting for an extension.\n"
Mark Spencer
committed
" Optionally, specify the class for music on hold within parenthesis.\n"
};
static struct ast_context *contexts = NULL;
AST_MUTEX_DEFINE_STATIC(conlock); /*!< Lock for the ast_context list */
AST_MUTEX_DEFINE_STATIC(applock); /*!< Lock for the application list */
AST_MUTEX_DEFINE_STATIC(switchlock); /*!< Lock for switches */
AST_MUTEX_DEFINE_STATIC(hintlock); /*!< Lock for extension state notifys */
static int stateid = 1;
struct ast_hint *hints = NULL;
struct ast_state_cb *statecbs = NULL;
/*
\note This function is special. It saves the stack so that no matter
how many times it is called, it returns to the same place */
int pbx_exec(struct ast_channel *c, /*!< Channel */
struct ast_app *app, /*!< Application */
void *data, /*!< Data for execution */
int newstack) /*!< Force stack increment */
char *saved_c_appl;
char *saved_c_data;
int (*execute)(struct ast_channel *chan, void *data) = app->execute;
if (c->cdr)
ast_cdr_setapp(c->cdr, app->name, data);
saved_c_appl= c->appl;
saved_c_data= c->data;
c->appl= saved_c_appl;
c->data= saved_c_data;
return res;
} else
ast_log(LOG_WARNING, "You really didn't want to call this function with newstack set to 0\n");
return -1;
/*! Go no deeper than this through includes (not counting loops) */
#define AST_PBX_MAX_STACK 128
#define HELPER_EXISTS 0
#define HELPER_SPAWN 1
#define HELPER_EXEC 2
#define HELPER_FINDLABEL 5
/*! \brief Find application handle in linked list
*/
struct ast_app *pbx_findapp(const char *app)
if (ast_mutex_lock(&applock)) {
ast_log(LOG_WARNING, "Unable to obtain application lock\n");
return NULL;
}
ast_mutex_unlock(&applock);
static struct ast_switch *pbx_findswitch(const char *sw)
if (ast_mutex_lock(&switchlock)) {
ast_log(LOG_WARNING, "Unable to obtain application lock\n");
return NULL;
}
ast_mutex_unlock(&switchlock);
static inline int include_valid(struct ast_include *i)
{
if (!i->hastime)
return 1;
return ast_check_timing(&(i->timing));
static void pbx_destroy(struct ast_pbx *p)
{
free(p);
}
#define EXTENSION_MATCH_CORE(data,pattern,match) {\
/* All patterns begin with _ */\
if (pattern[0] != '_') \
return 0;\
/* Start optimistic */\
match=1;\
pattern++;\
while(match && *data && *pattern && (*pattern != '/')) {\
while (*data == '-' && (*(data+1) != '\0')) data++;\
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
switch(toupper(*pattern)) {\
case '[': \
{\
int i,border=0;\
char *where;\
match=0;\
pattern++;\
where=strchr(pattern,']');\
if (where)\
border=(int)(where-pattern);\
if (!where || border > strlen(pattern)) {\
ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");\
return match;\
}\
for (i=0; i<border; i++) {\
int res=0;\
if (i+2<border)\
if (pattern[i+1]=='-') {\
if (*data >= pattern[i] && *data <= pattern[i+2]) {\
res=1;\
} else {\
i+=2;\
continue;\
}\
}\
if (res==1 || *data==pattern[i]) {\
match = 1;\
break;\
}\
}\
pattern+=border;\
break;\
}\
case 'N':\
if ((*data < '2') || (*data > '9'))\
match=0;\
break;\
case 'X':\
if ((*data < '0') || (*data > '9'))\
match = 0;\
break;\
case 'Z':\
if ((*data < '1') || (*data > '9'))\
match = 0;\
break;\
case '.':\
/* Must match */\
return 1;\
Kevin P. Fleming
committed
case '!':\
/* Early match */\
return 2;\
case ' ':\
case '-':\
/* Ignore these characters */\
data--;\
break;\
default:\
if (*data != *pattern)\
match =0;\
}\
data++;\
pattern++;\
}\
Kevin P. Fleming
committed
/* If we ran off the end of the data and the pattern ends in '!', match */\
if (match && !*data && (*pattern == '!'))\
return 2;\
int ast_extension_match(const char *pattern, const char *data)
/* If they're the same return */
if (!strcmp(pattern, data))
return 1;
EXTENSION_MATCH_CORE(data,pattern,match);
/* Must be at the end of both */
if (*data || (*pattern && (*pattern != '/')))
match = 0;
int ast_extension_close(const char *pattern, const char *data, int needmore)
/* If "data" is longer, it can'be a subset of pattern unless
pattern is a pattern match */
if ((strlen(pattern) < strlen(data)) && (pattern[0] != '_'))
if ((ast_strlen_zero((char *)data) || !strncasecmp(pattern, data, strlen(data))) &&
(!needmore || (strlen(pattern) > strlen(data)))) {
/* If there's more or we don't care about more, or if it's a possible early match,
return non-zero; otherwise it's a miss */
if (!needmore || *pattern || match == 2) {
struct ast_context *ast_context_find(const char *name)
ast_mutex_lock(&conlock);
if (!strcasecmp(name, tmp->name))
break;
}
} else
tmp = contexts;
ast_mutex_unlock(&conlock);
#define STATUS_NO_CONTEXT 1
#define STATUS_NO_EXTENSION 2
#define STATUS_NO_PRIORITY 3
#define STATUS_NO_LABEL 4
static int matchcid(const char *cidpattern, const char *callerid)
int failresult;
/* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
failing to get a number should count as a match, otherwise not */
if (!ast_strlen_zero(cidpattern))
failresult = 0;
else
failresult = 1;
if (!callerid)
return failresult;
return ast_extension_match(cidpattern, callerid);
static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast_context *bypass, const char *context, const char *exten, int priority, const char *label, const char *callerid, int action, char *incstack[], int *stacklen, int *status, struct ast_switch **swo, char **data, const char **foundcontext)
struct ast_exten *e, *eroot;
struct ast_include *i;
/* Check for stack overflow */
if (*stacklen >= AST_PBX_MAX_STACK) {
ast_log(LOG_WARNING, "Maximum PBX stack exceeded\n");
return NULL;
}
/* Check first to see if we've already been checked */
if (!strcasecmp(incstack[x], context))
return NULL;
}
if (bypass)
tmp = bypass;
else
tmp = contexts;
if (bypass || !strcmp(tmp->name, context)) {
struct ast_exten *earlymatch = NULL;
if (*status < STATUS_NO_EXTENSION)
*status = STATUS_NO_EXTENSION;
for (eroot = tmp->root; eroot; eroot = eroot->next) {
int match = 0;
if ((((action != HELPER_MATCHMORE) && ast_extension_match(eroot->exten, exten)) ||
((action == HELPER_CANMATCH) && (ast_extension_close(eroot->exten, exten, 0))) ||
((action == HELPER_MATCHMORE) && (match = ast_extension_close(eroot->exten, exten, 1)))) &&
(!eroot->matchcid || matchcid(eroot->cidmatch, callerid))) {
if (action == HELPER_MATCHMORE && match == 2 && !earlymatch) {
Kevin P. Fleming
committed
/* It matched an extension ending in a '!' wildcard
So ignore it for now, unless there's a better match */
earlymatch = eroot;
} else {
if (*status < STATUS_NO_PRIORITY)
*status = STATUS_NO_PRIORITY;
if (action == HELPER_FINDLABEL) {
if (*status < STATUS_NO_LABEL)
*status = STATUS_NO_LABEL;
if (label && e->label && !strcmp(label, e->label)) {
*status = STATUS_SUCCESS;
*foundcontext = context;
return e;
}
} else if (e->priority == priority) {
*foundcontext = context;
}
if (earlymatch) {
/* Bizarre logic for HELPER_MATCHMORE. We return zero to break out
of the loop waiting for more digits, and _then_ match (normally)
the extension we ended up with. We got an early-matching wildcard
pattern, so return NULL to break out of the loop. */
return NULL;
/* Substitute variables now */
if (sw->eval)
pbx_substitute_variables_helper(chan, sw->data, sw->tmpdata, SWITCH_DATA_LENGTH - 1);
res = asw->canmatch ? asw->canmatch(chan, context, exten, priority, callerid, sw->eval ? sw->tmpdata : sw->data) : 0;
res = asw->matchmore ? asw->matchmore(chan, context, exten, priority, callerid, sw->eval ? sw->tmpdata : sw->data) : 0;
res = asw->exists ? asw->exists(chan, context, exten, priority, callerid, sw->eval ? sw->tmpdata : sw->data) : 0;
*data = sw->eval ? sw->tmpdata : sw->data;
*foundcontext = context;
return NULL;
}
} else {
ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
}
}
/* Setup the stack */
incstack[*stacklen] = tmp->name;
(*stacklen)++;
/* Now try any includes we have in this context */
if ((e = pbx_find_extension(chan, bypass, i->rname, exten, priority, label, callerid, action, incstack, stacklen, status, swo, data, foundcontext)))
Kevin P. Fleming
committed
/* Note that it's negative -- that's important later. */
#define DONT_HAVE_LENGTH 0x80000000
static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
{
char *varchar, *offsetchar = NULL;
int parens=0;
*offset = 0;
*length = DONT_HAVE_LENGTH;
*isfunc = 0;
Kevin P. Fleming
committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
switch (*varchar) {
case '(':
(*isfunc)++;
parens++;
break;
case ')':
parens--;
break;
case ':':
if (parens == 0) {
offsetchar = varchar + 1;
*varchar = '\0';
goto pvn_endfor;
}
}
}
pvn_endfor:
if (offsetchar) {
sscanf(offsetchar, "%d:%d", offset, length);
return 1;
} else {
return 0;
}
}
static char *substring(char *value, int offset, int length, char *workspace, size_t workspace_len)
{
char *ret = workspace;
/* No need to do anything */
if (offset == 0 && length==-1) {
return value;
}
ast_copy_string(workspace, value, workspace_len);
if (abs(offset) > strlen(ret)) { /* Offset beyond string */
if (offset >= 0)
offset = strlen(ret);
else
offset =- strlen(ret);
}
/* Detect too-long length */
if ((offset < 0 && length > -offset) || (offset >= 0 && offset+length > strlen(ret))) {
if (offset >= 0)
length = strlen(ret)-offset;
else
length = strlen(ret)+offset;
}
/* Bounce up to the right offset */
if (offset >= 0)
ret += offset;
else
ret += strlen(ret)+offset;
/* Chop off at the requisite length */
if (length >= 0)
ret[length] = '\0';
return ret;
}
/*! \brief pbx_retrieve_variable: Support for Asterisk built-in variables and
void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp)
time_t thistime;
struct tm brokentime;
Kevin P. Fleming
committed
int offset, offset2, isfunc;
char *deprecated = NULL;
headp=&c->varshead;
Kevin P. Fleming
committed
ast_copy_string(tmpvar, var, sizeof(tmpvar));
if (parse_variable_name(tmpvar, &offset, &offset2, &isfunc)) {
pbx_retrieve_variable(c, tmpvar, ret, workspace, workspacelen, headp);
Kevin P. Fleming
committed
*ret = substring(*ret, offset, offset2, workspace, workspacelen);
} else if (c && !strncmp(var, "CALL", 4)) {
if (!strncmp(var + 4, "ER", 2)) {
if (!strncmp(var + 6, "ID", 2)) {
if (c->cid.cid_num) {
if (c->cid.cid_name) {
snprintf(workspace, workspacelen, "\"%s\" <%s>", c->cid.cid_name, c->cid.cid_num);
} else {
ast_copy_string(workspace, c->cid.cid_num, workspacelen);
}
*ret = workspace;
} else if (c->cid.cid_name) {
ast_copy_string(workspace, c->cid.cid_name, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(all)";
} else if (!strcmp(var + 8, "NUM")) {
/* CALLERIDNUM */
if (c->cid.cid_num) {
ast_copy_string(workspace, c->cid.cid_num, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(num)";
} else if (!strcmp(var + 8, "NAME")) {
/* CALLERIDNAME */
if (c->cid.cid_name) {
ast_copy_string(workspace, c->cid.cid_name, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(name)";
} else
goto icky;
} else if (!strcmp(var + 6, "ANI")) {
/* CALLERANI */
if (c->cid.cid_ani) {
ast_copy_string(workspace, c->cid.cid_ani, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(ANI)";
} else if (!strncmp(var + 4, "ING", 3)) {
if (!strcmp(var + 7, "PRES")) {
/* CALLINGPRES */
snprintf(workspace, workspacelen, "%d", c->cid.cid_pres);
*ret = workspace;
} else if (!strcmp(var + 7, "ANI2")) {
/* CALLINGANI2 */
snprintf(workspace, workspacelen, "%d", c->cid.cid_ani2);
*ret = workspace;
} else if (!strcmp(var + 7, "TON")) {
/* CALLINGTON */
snprintf(workspace, workspacelen, "%d", c->cid.cid_ton);
*ret = workspace;
} else if (!strcmp(var + 7, "TNS")) {
/* CALLINGTNS */
snprintf(workspace, workspacelen, "%d", c->cid.cid_tns);
*ret = workspace;
} else
goto icky;
ast_copy_string(workspace, c->cid.cid_dnid, workspacelen);
deprecated = "CALLERID(DNID)";
} else if (c && !strcmp(var, "HINT")) {
if (!ast_get_hint(workspace, workspacelen, NULL, 0, c, c->context, c->exten))
*ret = NULL;
else
*ret = workspace;
} else if (c && !strcmp(var, "HINTNAME")) {
if (!ast_get_hint(NULL, 0, workspace, workspacelen, c, c->context, c->exten))