Newer
Older
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Core PBX routines.
*
*
* Mark Spencer <markster@linux-support.net>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <asterisk/pbx.h>
#include <asterisk/channel.h>
#include <asterisk/options.h>
#include <asterisk/logger.h>
#include <asterisk/file.h>
#include <asterisk/config.h>
#include <asterisk/manager.h>
#include <asterisk/ast_expr.h>
#include <asterisk/channel_pvt.h>
#include <asterisk/linkedlists.h>
#include <asterisk/utils.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
/*
* I M P O R T A N T :
*
* 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
struct ast_context;
/* An extension */
struct ast_exten {
char exten[AST_MAX_EXTENSION];
int priority;
/* An extension */
struct ast_context *parent;
/* Application to execute */
char app[AST_MAX_EXTENSION];
/* Data to use */
void *data;
/* Data destructor */
void (*datad)(void *);
/* Extension with a greater ID */
struct ast_exten *next;
};
struct ast_include {
char name[AST_MAX_EXTENSION];
int hastime;
unsigned int monthmask;
unsigned int daymask;
unsigned int dowmask;
unsigned int minmask[24];
struct ast_sw {
char name[AST_MAX_EXTENSION];
char *registrar;
char data[AST_MAX_EXTENSION];
struct ast_sw *next;
};
struct ast_ignorepat {
char pattern[AST_MAX_EXTENSION];
char *registrar;
struct ast_ignorepat *next;
};
/* An extension context */
struct ast_context {
/* Name of the context */
char name[AST_MAX_EXTENSION];
/* A lock to prevent multiple threads from clobbering the context */
ast_mutex_t lock;
/* The root of the list of extensions */
struct ast_exten *root;
/* Link them together */
struct ast_context *next;
/* Include other contexts */
struct ast_include *includes;
/* Patterns for which to continue playing dialtone */
struct ast_ignorepat *ignorepats;
/* Alternative switches */
struct ast_sw *alts;
};
/* An application */
struct ast_app {
/* Name of the application */
char name[AST_MAX_APP];
int (*execute)(struct ast_channel *chan, void *data);
/* An extension state notify */
struct ast_state_cb {
int id;
void *data;
ast_state_cb_type callback;
struct ast_state_cb *next;
struct ast_hint {
struct ast_exten *exten;
int laststate;
struct ast_state_cb *callbacks;
struct ast_hint *next;
static int pbx_builtin_prefix(struct ast_channel *, void *);
static int pbx_builtin_suffix(struct ast_channel *, void *);
static int pbx_builtin_stripmsd(struct ast_channel *, 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_dtimeout(struct ast_channel *, void *);
static int pbx_builtin_rtimeout(struct ast_channel *, void *);
static int pbx_builtin_atimeout(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_setlanguage(struct ast_channel *, void *);
static int pbx_builtin_resetcdr(struct ast_channel *, void *);
Mark Spencer
committed
static int pbx_builtin_setaccount(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_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 *);
void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
char *pbx_builtin_getvar_helper(struct ast_channel *chan, char *name);
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
need separate modules
*/
{ "AbsoluteTimeout", pbx_builtin_atimeout,
Mark Spencer
committed
"Set absolute maximum time of call",
" AbsoluteTimeout(seconds): Set the absolute maximum amount of time permitted\n"
"for a call. A setting of 0 disables the timeout. Always returns 0.\n" },
"Answer a channel if ringing",
" Answer(): If the channel is ringing, answer it, otherwise do nothing. \n"
"Returns 0 unless it tries to answer the channel and fails.\n" },
Mark Spencer
committed
{ "BackGround", pbx_builtin_background,
"Play a file while awaiting extension",
" Background(filename): Plays a given file, while simultaneously waiting for\n"
"the user to begin typing an extension. The timeouts do not count until the\n"
"last BackGround application as ended. Always returns 0.\n" },
Mark Spencer
committed
{ "Busy", pbx_builtin_busy,
"Indicate busy condition and stop",
" Busy(): Requests that the channel indicate busy condition and then waits\n"
"for the user to hang up. Always returns -1." },
{ "Congestion", pbx_builtin_congestion,
"Indicate congestion and stop",
" Congestion(): Requests that the channel indicate congestion and then\n"
"waits for the user to hang up. Always returns -1." },
"Set maximum timeout between digits",
" DigitTimeout(seconds): Set the maximum amount of time permitted between\n"
"digits when the user is typing in an extension. When this timeout expires,\n"
"after the user has started to type in an extension, the extension will be\n"
"considered complete, and will be interpreted. Note that if an extension\n"
"typed in is valid, it will not have to timeout to be tested, so typically\n"
"at the expiry of this timeout, the extension will be considered invalid\n"
"(and thus control would be passed to the 'i' extension, or if it doesn't\n"
"exist the call would be terminated). Always returns 0.\n" },
Mark Spencer
committed
{ "Goto", pbx_builtin_goto,
"Goto a particular priority, extension, or context",
" Goto([[context|]extension|]priority): Set the priority to the specified\n"
"value, optionally setting the extension and optionally the context as well.\n"
"The extension BYEXTENSION is special in that it uses the current extension,\n"
"thus permitting you to go to a different context, without specifying a\n"
"specific extension. Always returns 0, even if the given context, extension,\n"
"or priority is invalid.\n" },
Mark Spencer
committed
{ "GotoIf", pbx_builtin_gotoif,
"Conditional goto",
" GotoIf(Condition?label1:label2): Go to label 1 if condition is\n"
"true, to label2 if condition is false. Either label1 or label2 may be\n"
"omitted (in that case, we just don't take the particular branch) but not\n"
"both. Look for the condition syntax in examples or documentation." },
{ "GotoIfTime", pbx_builtin_gotoiftime,
"Conditional goto on current time",
" GotoIfTime(<times>|<weekdays>|<mdays>|<months>?[[context|]extension|]pri):\n"
"If the current time matches the specified time, then branch to the specified\n"
"extension. Each of the elements may be specified either as '*' (for always)\n"
"or as a range. See the include syntax." },
Mark Spencer
committed
{ "Hangup", pbx_builtin_hangup,
"Unconditional hangup",
" Hangup(): Unconditionally hangs up a given channel by returning -1 always.\n" },
Mark Spencer
committed
{ "NoOp", pbx_builtin_noop,
"No operation",
" NoOp(): No-operation; Does nothing." },
{ "Prefix", pbx_builtin_prefix,
"Prepend leading digits",
" Prefix(digits): Prepends the digit string specified by digits to the\n"
"channel's associated extension. For example, the number 1212 when prefixed\n"
"with '555' will become 5551212. This app always returns 0, and the PBX will\n"
"continue processing at the next priority for the *new* extension.\n"
" So, for example, if priority 3 of 1212 is Prefix 555, the next step\n"
"executed will be priority 4 of 5551212. If you switch into an extension\n"
"which has no first step, the PBX will treat it as though the user dialed an\n"
"invalid extension.\n" },
{ "Progress", pbx_builtin_progress,
"Indicate progress",
" Progress(): Request that the channel indicate in-band progress is available to the user.\n"
"Always returns 0.\n" },
{ "ResetCDR", pbx_builtin_resetcdr,
"Resets the Call Data Record",
" ResetCDR([options]): Causes the Call Data Record to be reset, optionally\n"
"storing the current CDR before zeroing it out (if 'w' option is specifed).\n"
"record WILL be stored. Always returns 0.\n" },
Mark Spencer
committed
{ "ResponseTimeout", pbx_builtin_rtimeout,
"Set maximum timeout awaiting response",
" ResponseTimeout(seconds): Set the maximum amount of time permitted after\n"
"falling through a series of priorities for a channel in which the user may\n"
"begin typing an extension. If the user does not type an extension in this\n"
"amount of time, control will pass to the 't' extension if it exists, and\n"
"if not the call would be terminated. Always returns 0.\n" },
{ "Ringing", pbx_builtin_ringing,
"Indicate ringing tone",
" Ringing(): Request that the channel indicate ringing tone to the user.\n"
"Always returns 0.\n" },
Mark Spencer
committed
{ "SayNumber", pbx_builtin_saynumber,
"Say Number",
Mark Spencer
committed
" SayNumber(digits[,gender]): Says the passed number\n" },
Mark Spencer
committed
{ "SayDigits", pbx_builtin_saydigits,
"Say Digits",
" SayDigits(digits): Says the passed digits\n" },
{ "SayAlpha", pbx_builtin_saycharacters,
"Say Alpha",
" SayAlpha(string): Spells the passed string\n" },
{ "SayPhonetic", pbx_builtin_sayphonetic,
"Say Phonetic",
" SayPhonetic(string): Spells the passed string with phonetic alphabet\n" },
Mark Spencer
committed
{ "SetAccount", pbx_builtin_setaccount,
Mark Spencer
committed
" SetAccount([account]): Set the channel account code for billing\n"
"purposes. Always returns 0.\n" },
{ "SetAMAFlags", pbx_builtin_setamaflags,
"Sets AMA Flags",
" SetAMAFlags([flag]): Set the channel AMA Flags for billing\n"
Mark Spencer
committed
"purposes. Always returns 0.\n" },
{ "SetGlobalVar", pbx_builtin_setglobalvar,
"Set variable to value",
Mark Spencer
committed
{ "SetLanguage", pbx_builtin_setlanguage,
"Sets user language",
" SetLanguage(language): Set the channel language to 'language'. This\n"
"information is used for the generation of numbers, and to choose a natural\n"
Mark Spencer
committed
"language file when available. For example, if language is set to 'fr' and\n"
"the file 'demo-congrats' is requested to be played, if the file 'fr/demo-\n"
"congrats' exists, then it will play that file, and if not will play the\n"
Mark Spencer
committed
"normal 'demo-congrats'. Always returns 0.\n" },
Mark Spencer
committed
{ "SetVar", pbx_builtin_setvar,
"Set variable to value",
" Setvar(#n=value): Sets variable n to value" },
Mark Spencer
committed
{ "StripMSD", pbx_builtin_stripmsd,
"Strip leading digits",
" StripMSD(count): Strips the leading 'count' digits from the channel's\n"
"associated extension. For example, the number 5551212 when stripped with a\n"
"count of 3 would be changed to 1212. This app always returns 0, and the PBX\n"
"will continue processing at the next priority for the *new* extension.\n"
" So, for example, if priority 3 of 5551212 is StripMSD 3, the next step\n"
"executed will be priority 4 of 1212. If you switch into an extension which\n"
"has no first step, the PBX will treat it as though the user dialed an\n"
"invalid extension.\n" },
{ "Suffix", pbx_builtin_suffix,
"Append trailing digits",
" Suffix(digits): Appends the digit string specified by digits to the\n"
"channel's associated extension. For example, the number 555 when suffixed\n"
"with '1212' will become 5551212. This app always returns 0, and the PBX will\n"
"continue processing at the next priority for the *new* extension.\n"
" So, for example, if priority 3 of 555 is Suffix 1212, the next step\n"
"executed will be priority 4 of 5551212. If you switch into an extension\n"
"which has no first step, the PBX will treat it as though the user dialed an\n"
Mark Spencer
committed
"invalid extension.\n" },
Mark Spencer
committed
{ "Wait", pbx_builtin_wait,
"Waits for some time",
" Wait(seconds): Waits for a specified number of seconds, then returns 0.\n"
"seconds can be passed with fractions of a second. (eg: 1.5 = 1.5 seconds)\n" },
{ "WaitExten", pbx_builtin_waitexten,
"Waits for some time",
" Wait(seconds): Waits for the user to enter a new extension for the \n"
"specified number of seconds, then returns 0. Seconds can be passed with\n"
"fractions of a second. (eg: 1.5 = 1.5 seconds)\n" },
AST_MUTEX_DEFINE_STATIC(applock);
static struct ast_context *contexts = NULL;
/* Lock for the ast_context list */
AST_MUTEX_DEFINE_STATIC(conlock);
AST_MUTEX_DEFINE_STATIC(switchlock);
/* Lock for extension state notifys */
AST_MUTEX_DEFINE_STATIC(hintlock);
static int stateid = 1;
struct ast_hint *hints = NULL;
struct ast_state_cb *statecbs = NULL;
int pbx_exec(struct ast_channel *c, /* Channel */
struct ast_app *app,
void *data, /* Data for execution */
int newstack) /* Force stack increment */
{
/* 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 res;
char *saved_c_appl;
char *saved_c_data;
int (*execute)(struct ast_channel *chan, void *data) = app->execute;
if (newstack && stack > AST_CHANNEL_MAX_STACK - 2) {
/* Don't allow us to go over the max number of stacks we
permit saving. */
ast_log(LOG_WARNING, "Stack overflow, cannot create another stack\n");
return -1;
}
if (newstack && (res = setjmp(c->jmp[++c->stack]))) {
/* Okay, here's where it gets weird. If newstack is non-zero,
then we increase the stack increment, but setjmp is not going
to return until longjmp is called -- when the application
exec'd is finished running. */
if (res == 1)
res = 0;
if (c->stack != stack + 1)
ast_log(LOG_WARNING, "Stack returned to an unexpected place!\n");
else if (c->app[c->stack])
ast_log(LOG_WARNING, "Application may have forgotten to free its memory\n");
c->stack = stack;
return res;
} else {
if (c->cdr)
ast_cdr_setapp(c->cdr, app->name, data);
// save channel values
saved_c_appl= c->appl;
saved_c_data= c->data;
// restore channel values
c->appl= saved_c_appl;
c->data= saved_c_data;
/* Any application that returns, we longjmp back, just in case. */
if (c->stack != stack + 1)
ast_log(LOG_WARNING, "Stack is not at expected value\n");
longjmp(c->jmp[stack+1], res);
/* Never returns */
}
}
/* Go no deeper than this through includes (not counting loops) */
#define AST_PBX_MAX_STACK 64
#define HELPER_EXISTS 0
#define HELPER_SPAWN 1
#define HELPER_EXEC 2
if (ast_mutex_lock(&applock)) {
ast_log(LOG_WARNING, "Unable to obtain application lock\n");
return NULL;
}
tmp = apps;
while(tmp) {
if (!strcasecmp(tmp->name, app))
break;
tmp = tmp->next;
}
ast_mutex_unlock(&applock);
static struct ast_switch *pbx_findswitch(char *sw)
{
struct ast_switch *asw;
if (ast_mutex_lock(&switchlock)) {
ast_log(LOG_WARNING, "Unable to obtain application lock\n");
return NULL;
}
asw = switches;
while(asw) {
if (!strcasecmp(asw->name, sw))
break;
asw = asw->next;
}
ast_mutex_unlock(&switchlock);
static inline int include_valid(struct ast_include *i)
{
time_t t;
if (!i->hastime)
return 1;
time(&t);
if (!(i->monthmask & (1 << tm.tm_mon))) {
return 0;
}
/* If it's not that time of the month.... */
if (!(i->daymask & (1 << (tm.tm_mday-1))))
return 0;
/* If it's not the right day of the week */
if (!(i->dowmask & (1 << tm.tm_wday)))
return 0;
/* Sanity check the hour just to be safe */
if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) {
ast_log(LOG_WARNING, "Insane time...\n");
return 0;
}
/* Now the tough part, we calculate if it fits
in the right time based on min/hour */
if (!(i->minmask[tm.tm_hour] & (1 << (tm.tm_min / 2))))
return 0;
/* If we got this far, then we're good */
return 1;
}
static void pbx_destroy(struct ast_pbx *p)
{
free(p);
}
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
#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 != '/')) {\
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;\
case ' ':\
case '-':\
/* Ignore these characters */\
data--;\
break;\
default:\
if (*data != *pattern)\
match =0;\
}\
data++;\
pattern++;\
}\
int ast_extension_match(char *pattern, 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;
static int extension_close(char *pattern, 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, return non-zero, otlherwise it's a miss */
if (!needmore || *pattern) {
return match;
} else
return 0;
struct ast_context *ast_context_find(char *name)
{
struct ast_context *tmp;
ast_mutex_lock(&conlock);
if (name) {
tmp = contexts;
while(tmp) {
if (!strcasecmp(name, tmp->name))
break;
tmp = tmp->next;
}
} else
tmp = contexts;
ast_mutex_unlock(&conlock);
#define STATUS_NO_CONTEXT 1
#define STATUS_NO_EXTENSION 2
#define STATUS_NO_PRIORITY 3
#define STATUS_SUCCESS 4
static int matchcid(char *cidpattern, char *callerid)
char tmp[AST_MAX_EXTENSION];
int failresult;
char *name, *num;
/* 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;
/* Copy original Caller*ID */
/* Parse Number */
if (ast_callerid_parse(tmp, &name, &num))
return failresult;
if (!num)
return failresult;
ast_shrink_phone_number(num);
return ast_extension_match(cidpattern, num);
}
static struct ast_exten *pbx_find_extension(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, int action, char *incstack[], int *stacklen, int *status, struct ast_switch **swo, char **data)
{
int x, res;
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 */
for (x=0;x<*stacklen;x++) {
if (!strcasecmp(incstack[x], context))
return NULL;
}
tmp = contexts;
while(tmp) {
/* Match context */
if (*status < STATUS_NO_EXTENSION)
*status = STATUS_NO_EXTENSION;
eroot = tmp->root;
while(eroot) {
/* Match extension */
if ((((action != HELPER_MATCHMORE) && ast_extension_match(eroot->exten, exten)) ||
((action == HELPER_CANMATCH) && (extension_close(eroot->exten, exten, 0))) ||
((action == HELPER_MATCHMORE) && (extension_close(eroot->exten, exten, 1)))) &&
(!eroot->matchcid || matchcid(eroot->cidmatch, callerid))) {
e = eroot;
if (*status < STATUS_NO_PRIORITY)
*status = STATUS_NO_PRIORITY;
while(e) {
/* Match priority */
if (e->priority == priority) {
*status = STATUS_SUCCESS;
return e;
}
e = e->peer;
}
}
eroot = eroot->next;
}
/* Check alternative switches */
sw = tmp->alts;
while(sw) {
if ((asw = pbx_findswitch(sw->name))) {
if (action == HELPER_CANMATCH)
res = asw->canmatch ? asw->canmatch(chan, context, exten, priority, callerid, sw->data) : 0;
else if (action == HELPER_MATCHMORE)
res = asw->matchmore ? asw->matchmore(chan, context, exten, priority, callerid, sw->data) : 0;
else
res = asw->exists ? asw->exists(chan, context, exten, priority, callerid, sw->data) : 0;
if (res) {
/* Got a match */
*swo = asw;
*data = sw->data;
return NULL;
}
} else {
ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
}
sw = sw->next;
}
/* Setup the stack */
incstack[*stacklen] = tmp->name;
(*stacklen)++;
/* Now try any includes we have in this context */
i = tmp->includes;
while(i) {
if (include_valid(i)) {
if ((e = pbx_find_extension(chan, i->rname, exten, priority, callerid, action, incstack, stacklen, status, swo, data)))
return e;
if (*swo)
return NULL;
}
i = i->next;
}
}
tmp = tmp->next;
}
return NULL;
}
static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,char **ret, char *workspace, int workspacelen)
time_t thistime;
struct tm brokentime;
char *name, *num; /* for callerid name + num variables */
struct varshead *headp=NULL;
headp=&c->varshead;
*ret=NULL;
/* Now we have the variable name on cp3 */
if (!strncasecmp(var,"LEN(",4)) {
int len=strlen(var);
int len_len=4;
if (strrchr(var,')')) {
char cp3[80];
strncpy(cp3, var, sizeof(cp3) - 1);
cp3[len-len_len-1]='\0';
sprintf(workspace,"%d",(int)strlen(cp3));
*ret = workspace;
} else {
/* length is zero */
*ret = "0";
}
} else if ((first=strchr(var,':'))) {
strncpy(tmpvar, var, sizeof(tmpvar) - 1);
first = strchr(tmpvar, ':');
if (!first)
first = tmpvar + strlen(tmpvar);
pbx_substitute_variables_temp(c,tmpvar,ret,workspace,workspacelen - 1);
offset=atoi(first+1);
if ((second=strchr(first+1,':'))) {
*second='\0';
offset2=atoi(second+1);
offset2=strlen(*ret)-offset;
if (abs(offset)>strlen(*ret)) {
if (offset>=0)
offset=strlen(*ret);
else
offset=-strlen(*ret);
if ((offset<0 && offset2>-offset) || (offset>=0 && offset+offset2>strlen(*ret))) {
if (offset>=0)
offset2=strlen(*ret)-offset;
else
offset2=strlen(*ret)+offset;
*ret+=strlen(*ret)+offset;
(*ret)[offset2] = '\0';
} else if (c && !strcmp(var, "CALLERIDNUM")) {
strncpy(workspace, c->callerid, workspacelen - 1);
ast_callerid_parse(workspace, &name, &num);
if (num) {
ast_shrink_phone_number(num);
} else if (c && !strcmp(var, "CALLERIDNAME")) {
strncpy(workspace, c->callerid, workspacelen - 1);
ast_callerid_parse(workspace, &name, &num);
} else if (c && !strcmp(var, "CALLERID")) {
if (c->callerid) {
strncpy(workspace, c->callerid, workspacelen - 1);
*ret = workspace;
} else
*ret = NULL;
} else if (c && !strcmp(var, "DNID")) {
if (c->dnid) {
strncpy(workspace, c->dnid, workspacelen - 1);
*ret = workspace;
} else
*ret = NULL;
} else if (c && !strcmp(var, "HINT")) {
if (!ast_get_hint(workspace, workspacelen - 1, c, c->context, c->exten))
} else if (c && !strcmp(var, "EXTEN")) {
strncpy(workspace, c->exten, workspacelen - 1);
*ret = workspace;
} else if (c && !strncmp(var, "EXTEN-", strlen("EXTEN-")) &&
(sscanf(var + strlen("EXTEN-"), "%d", &offset) == 1)) {
if (offset < 0)
offset=0;
if (offset > strlen(c->exten))
offset = strlen(c->exten);
strncpy(workspace, c->exten + offset, workspacelen - 1);
*ret = workspace;
ast_log(LOG_WARNING, "The use of 'EXTEN-foo' has been deprecated in favor of 'EXTEN:foo'\n");
} else if (c && !strcmp(var, "RDNIS")) {
if (c->rdnis) {
strncpy(workspace, c->rdnis, workspacelen - 1);
*ret = workspace;
} else
*ret = NULL;
} else if (c && !strcmp(var, "CONTEXT")) {
strncpy(workspace, c->context, workspacelen - 1);
*ret = workspace;
} else if (c && !strcmp(var, "PRIORITY")) {
snprintf(workspace, workspacelen, "%d", c->priority);
} else if (c && !strcmp(var, "CHANNEL")) {
strncpy(workspace, c->name, workspacelen - 1);
*ret = workspace;
} else if (c && !strcmp(var, "EPOCH")) {
snprintf(workspace, workspacelen -1, "%u",(int)time(NULL));
*ret = workspace;
} else if (c && !strcmp(var, "DATETIME")) {
thistime=time(NULL);
localtime_r(&thistime, &brokentime);
snprintf(workspace, workspacelen -1, "%02d%02d%04d-%02d:%02d:%02d",
brokentime.tm_mday,
brokentime.tm_mon+1,
brokentime.tm_year+1900,
brokentime.tm_hour,
brokentime.tm_min,
brokentime.tm_sec
);
*ret = workspace;
} else if (c && !strcmp(var, "TIMESTAMP")) {
thistime=time(NULL);
localtime_r(&thistime, &brokentime);
/* 20031130-150612 */
snprintf(workspace, workspacelen -1, "%04d%02d%02d-%02d%02d%02d",
brokentime.tm_year+1900,
brokentime.tm_mon+1,
brokentime.tm_mday,
brokentime.tm_hour,
brokentime.tm_min,
brokentime.tm_sec
);
*ret = workspace;
} else if (c && !strcmp(var, "UNIQUEID")) {
snprintf(workspace, workspacelen -1, "%s", c->uniqueid);
*ret = workspace;
} else if (c && !strcmp(var, "HANGUPCAUSE")) {
snprintf(workspace, workspacelen -1, "%i", c->hangupcause);
*ret = workspace;
} else if (c && !strcmp(var, "ACCOUNTCODE")) {
strncpy(workspace, c->accountcode, workspacelen - 1);
*ret = workspace;
} else if (c && !strcmp(var, "LANGUAGE")) {
strncpy(workspace, c->language, workspacelen - 1);
*ret = workspace;
if (c) {
AST_LIST_TRAVERSE(headp,variables,entries) {
ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
if (strcasecmp(ast_var_name(variables),var)==0) {
*ret=ast_var_value(variables);
if (*ret) {
strncpy(workspace, *ret, workspacelen - 1);
*ret = workspace;
}
/* Try globals */
AST_LIST_TRAVERSE(&globals,variables,entries) {
#if 0
ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
if (strcasecmp(ast_var_name(variables),var)==0) {
*ret=ast_var_value(variables);
if (*ret) {
strncpy(workspace, *ret, workspacelen - 1);
*ret = workspace;
}
}
if (!(*ret)) {
int len=strlen(var);
if (len > (len_env+1) && !strncasecmp(var,"ENV(",len_env) && !strcmp(var+len-1,")")) {
char cp3[80] = "";
strncpy(cp3, var, sizeof(cp3) - 1);
*ret=getenv(cp3+len_env);
if (*ret) {
strncpy(workspace, *ret, workspacelen - 1);
*ret = workspace;
}
void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
char *cp4;
const char *tmp, *whereweare;
int length;
char workspace[4096];
char ltmp[4096], var[4096];
char *nextvar, *nextexp;
char *vars, *vare;
int pos, brackets, needsub, len;
/* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
zero-filled */
whereweare=tmp=cp1;
while(!ast_strlen_zero(whereweare) && count) {
/* Assume we're copying the whole remaining string */
pos = strlen(whereweare);
/* Look for a variable */
nextvar = strstr(whereweare, "${");
nextexp = strstr(whereweare, "$[");
if (nextvar && nextexp) {
if (nextvar < nextexp)
nextexp = NULL;
else
nextvar = NULL;
}
/* If there is one, we only go that far */
if (nextvar)
pos = nextvar - whereweare;
else if (nextexp)
pos = nextexp - whereweare;
/* Can't copy more than 'count' bytes */
if (pos > count)
pos = count;
/* Copy that many bytes */
memcpy(cp2, whereweare, pos);
count -= pos;
cp2 += pos;
whereweare += pos;
/* We have a variable. Find the start and end, and determine
if we are going to have to recursively call ourselves on the
contents */
vars = vare = nextvar + 2;
/* Find the end of it */
while(brackets && *vare) {
if ((vare[0] == '$') && (vare[1] == '{')) {
needsub++;
brackets++;
} else if (vare[0] == '}') {
brackets--;
} else if ((vare[0] == '$') && (vare[1] == '['))
if (brackets)
ast_log(LOG_NOTICE, "Error in extension logic (missing '}')\n");
len = vare - vars - 1;
/* Skip totally over variable name */
whereweare += ( len + 3);
/* Store variable name (and truncate) */
memset(var, 0, sizeof(var));
strncpy(var, vars, sizeof(var) - 1);
var[len] = '\0';
/* Substitute if necessary */
if (needsub) {
memset(ltmp, 0, sizeof(ltmp));
pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
vars = ltmp;
} else {
vars = var;
pbx_substitute_variables_temp(c,vars,&cp4, workspace, sizeof(workspace));
if (cp4) {
length = strlen(cp4);
if (length > count)
length = count;
memcpy(cp2, cp4, length);
count -= length;
cp2 += length;
} else if (nextexp) {
/* We have an expression. Find the start and end, and determine
if we are going to have to recursively call ourselves on the
contents */
vars = vare = nextexp + 2;
brackets = 1;
needsub = 0;
/* Find the end of it */
while(brackets && *vare) {
if ((vare[0] == '$') && (vare[1] == '[')) {
needsub++;
brackets++;
vare++;
} else if (vare[0] == '[') {
brackets++;
} else if (vare[0] == ']') {
brackets--;
} else if ((vare[0] == '$') && (vare[1] == '{')) {
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
vare++;
}
if (brackets)
ast_log(LOG_NOTICE, "Error in extension logic (missing ']')\n");
len = vare - vars - 1;
/* Skip totally over variable name */
whereweare += ( len + 3);
/* Store variable name (and truncate) */
memset(var, 0, sizeof(var));
strncpy(var, vars, sizeof(var) - 1);
var[len] = '\0';
/* Substitute if necessary */
if (needsub) {
memset(ltmp, 0, sizeof(ltmp));
pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
vars = ltmp;
} else {
vars = var;
}
/* Evaluate expression */
cp4 = ast_expr(vars);
ast_log(LOG_DEBUG, "Expression is '%s'\n", cp4);
if (cp4) {
length = strlen(cp4);
if (length > count)
length = count;
memcpy(cp2, cp4, length);
count -= length;
cp2 += length;
free(cp4);
}
static void pbx_substitute_variables(char *passdata, int datalen, struct ast_channel *c, struct ast_exten *e) {
memset(passdata, 0, datalen);
/* No variables or expressions in e->data, so why scan it? */
if (!strstr(e->data,"${") && !strstr(e->data,"$[")) {
strncpy(passdata, e->data, datalen - 1);
passdata[datalen-1] = '\0';
return;
}
pbx_substitute_variables_helper(c,e->data,passdata, datalen - 1);
}
static int pbx_extension_helper(struct ast_channel *c, char *context, char *exten, int priority, char *callerid, int action)
int status = 0;
char *incstack[AST_PBX_MAX_STACK];
char passdata[EXT_DATA_SIZE];
char tmp3[EXT_DATA_SIZE];
if (ast_mutex_lock(&conlock)) {
if ((action == HELPER_EXISTS) || (action == HELPER_CANMATCH) || (action == HELPER_MATCHMORE))
e = pbx_find_extension(c, context, exten, priority, callerid, action, incstack, &stacklen, &status, &sw, &data);
if (e) {
switch(action) {
case HELPER_CANMATCH:
ast_mutex_unlock(&conlock);
ast_mutex_unlock(&conlock);
ast_mutex_unlock(&conlock);
case HELPER_SPAWN:
newstack++;
/* Fall through */
case HELPER_EXEC:
app = pbx_findapp(e->app);
ast_mutex_unlock(&conlock);
strncpy(c->context, context, sizeof(c->context)-1);
if (c->exten != exten)
strncpy(c->exten, exten, sizeof(c->exten)-1);
pbx_substitute_variables(passdata, sizeof(passdata), c, e);
if (option_debug)
ast_log(LOG_DEBUG, "Launching '%s'\n", app->name);
else if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Executing %s(\"%s\", \"%s\") %s\n",
term_color(tmp, app->name, COLOR_BRCYAN, 0, sizeof(tmp)),
term_color(tmp2, c->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
term_color(tmp3, (!ast_strlen_zero(passdata) ? (char *)passdata : ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)),
res = pbx_exec(c, app, passdata, newstack);
return res;
} else {
ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority);
return -1;
}
default:
ast_log(LOG_WARNING, "Huh (%d)?\n", action); return -1;
} else if (sw) {
switch(action) {
case HELPER_CANMATCH:
ast_mutex_unlock(&conlock);
ast_mutex_unlock(&conlock);
ast_mutex_unlock(&conlock);
case HELPER_SPAWN:
newstack++;
/* Fall through */
case HELPER_EXEC:
ast_mutex_unlock(&conlock);
if (sw->exec)
res = sw->exec(c, context, exten, priority, callerid, newstack, data);
else {
ast_log(LOG_WARNING, "No execution engine for switch %s\n", sw->name);
res = -1;
}
return res;
default:
ast_log(LOG_WARNING, "Huh (%d)?\n", action);
return -1;
}
ast_mutex_unlock(&conlock);
if ((action != HELPER_EXISTS) && (action != HELPER_MATCHMORE))
ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", context);
break;
case STATUS_NO_EXTENSION:
if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, context);
break;
case STATUS_NO_PRIORITY:
if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, context);
break;
default:
ast_log(LOG_DEBUG, "Shouldn't happen!\n");
}
if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
static struct ast_exten *ast_hint_extension(struct ast_channel *c, char *context, char *exten)
{
struct ast_exten *e;
struct ast_switch *sw;
char *data;
int status = 0;
char *incstack[AST_PBX_MAX_STACK];
int stacklen = 0;
if (ast_mutex_lock(&conlock)) {
ast_log(LOG_WARNING, "Unable to obtain lock\n");
return NULL;
}
e = pbx_find_extension(c, context, exten, PRIORITY_HINT, "", HELPER_EXISTS, incstack, &stacklen, &status, &sw, &data);
ast_mutex_unlock(&conlock);
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
return e;
}
static int ast_extension_state2(struct ast_exten *e)
{
char hint[AST_MAX_EXTENSION] = "";
char *cur, *rest;
int res = -1;
int allunavailable = 1, allbusy = 1, allfree = 1;
int busy = 0;
strncpy(hint, ast_get_extension_app(e), sizeof(hint)-1);
cur = hint;
do {
rest = strchr(cur, '&');
if (rest) {
*rest = 0;
rest++;
}
res = ast_device_state(cur);
switch (res) {
allunavailable = 0;
allbusy = 0;
break;
return AST_EXTENSION_INUSE;
allunavailable = 0;
allfree = 0;
busy = 1;
break;
case AST_DEVICE_UNAVAILABLE:
case AST_DEVICE_INVALID:
allbusy = 0;
allfree = 0;
break;
allunavailable = 0;
allbusy = 0;
allfree = 0;
}
cur = rest;
} while (cur);
if (allfree)
if (allbusy)
if (allunavailable)
return AST_EXTENSION_NOT_INUSE;
}
int ast_extension_state(struct ast_channel *c, char *context, char *exten)
{
e = ast_hint_extension(c, context, exten);
if (!e)
return -1;
return ast_extension_state2(e);
int ast_device_state_changed(const char *fmt, ...)
struct ast_hint *list;
struct ast_state_cb *cblist;
char hint[AST_MAX_EXTENSION];
char device[AST_MAX_EXTENSION];
char *cur, *rest;
int state;
va_list ap;
va_start(ap, fmt);
vsnprintf(device, sizeof(device)-1, fmt, ap);
va_end(ap);
rest = strchr(device, '-');
if (rest) {
*rest = 0;
}
ast_mutex_lock(&hintlock);
list = hints;
while (list) {
strcpy(hint, ast_get_extension_app(list->exten));
cur = hint;
do {
rest = strchr(cur, '&');
if (rest) {
*rest = 0;
rest++;
}
// Found extension execute callbacks
state = ast_extension_state2(list->exten);
if ((state != -1) && (state != list->laststate)) {
// For general callbacks
cblist = statecbs;
while (cblist) {
cblist->callback(list->exten->parent->name, list->exten->exten, state, cblist->data);
cblist = cblist->next;
}
// For extension callbacks
cblist = list->callbacks;
while (cblist) {
cblist->callback(list->exten->parent->name, list->exten->exten, state, cblist->data);
cblist = cblist->next;
}
list->laststate = state;
}
break;
}
cur = rest;
} while (cur);
list = list->next;
}
ast_mutex_unlock(&hintlock);
return 1;
}
int ast_extension_state_add(char *context, char *exten,
ast_state_cb_type callback, void *data)
struct ast_hint *list;
struct ast_state_cb *cblist;
struct ast_exten *e;
/* No context and extension add callback to statecbs list */
if (!context && !exten) {
ast_mutex_lock(&hintlock);
cblist = statecbs;
while (cblist) {
if (cblist->callback == callback) {
cblist->data = data;
ast_mutex_unlock(&hintlock);
}
cblist = cblist->next;
}
/* Now inserts the callback */
cblist = malloc(sizeof(struct ast_state_cb));
if (!cblist) {
ast_mutex_unlock(&hintlock);
return -1;
}
memset(cblist, 0, sizeof(struct ast_state_cb));
cblist->id = 0;
cblist->callback = callback;
cblist->data = data;
cblist->next = statecbs;
statecbs = cblist;
ast_mutex_unlock(&hintlock);
return 0;
}
if (!context || !exten)
return -1;
/* This callback type is for only one hint */
e = ast_hint_extension(NULL, context, exten);
if (!e) {
return -1;
}
ast_mutex_lock(&hintlock);
list = hints;
while (list) {
break;
list = list->next;
}
if (!list) {
ast_mutex_unlock(&hintlock);
/* Now inserts the callback */
cblist = malloc(sizeof(struct ast_state_cb));
if (!cblist) {
ast_mutex_unlock(&hintlock);
return -1;
}
memset(cblist, 0, sizeof(struct ast_state_cb));
cblist->id = stateid++;
cblist->callback = callback;
cblist->data = data;
cblist->next = list->callbacks;
list->callbacks = cblist;
ast_mutex_unlock(&hintlock);
return cblist->id;
}
int ast_extension_state_del(int id, ast_state_cb_type callback)
{
struct ast_hint *list;
struct ast_state_cb *cblist, *cbprev;
if (!id && !callback)
return -1;
ast_mutex_lock(&hintlock);
/* id is zero is a callback without extension */
if (!id) {
cbprev = NULL;
cblist = statecbs;
while (cblist) {
if (cblist->callback == callback) {
if (!cbprev)
statecbs = cblist->next;
else
cbprev->next = cblist->next;
free(cblist);
ast_mutex_unlock(&hintlock);
return 0;
}
cbprev = cblist;
cblist = cblist->next;
}
ast_mutex_lock(&hintlock);
return -1;
}
/* id greater zero is a callback with extension */
list = hints;
while (list) {
cblist = list->callbacks;
cbprev = NULL;
while (cblist) {
if (cblist->id==id) {
if (!cbprev)
list->callbacks = cblist->next;
else
cbprev->next = cblist->next;
free(cblist);
ast_mutex_unlock(&hintlock);
return 0;
}
cbprev = cblist;
cblist = cblist->next;
}
list = list->next;
}
ast_mutex_unlock(&hintlock);
return -1;
}
static int ast_add_hint(struct ast_exten *e)
{
struct ast_hint *list;
if (!e) return -1;
ast_mutex_lock(&hintlock);
list = hints;
/* Search if hint exists, do nothing */
while (list) {
if (list->exten == e) {
ast_mutex_unlock(&hintlock);
return -1;
}
list = list->next;
}
list = malloc(sizeof(struct ast_hint));
if (!list) {
ast_mutex_unlock(&hintlock);
return -1;
}
/* Initialize and insert new item */
memset(list, 0, sizeof(struct ast_hint));
list->exten = e;
list->laststate = ast_extension_state2(e);
list->next = hints;
hints = list;
ast_mutex_unlock(&hintlock);
return 0;
}
static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
{
struct ast_hint *list;
ast_mutex_lock(&hintlock);
list = hints;
while(list) {
if (list->exten == oe) {
list->exten = ne;
ast_mutex_unlock(&hintlock);
return 0;
}
list = list->next;
}
ast_mutex_unlock(&hintlock);
return -1;
}
static int ast_remove_hint(struct ast_exten *e)
{
/* Cleanup the Notifys if hint is removed */
struct ast_hint *list, *prev = NULL;
struct ast_state_cb *cblist, *cbprev;
if (!e)
return -1;
ast_mutex_lock(&hintlock);
list = hints;
while(list) {
if (list->exten==e) {
cbprev = NULL;
cblist = list->callbacks;
while (cblist) {
/* Notify with -1 and remove all callbacks */
cbprev = cblist;
cblist = cblist->next;
cbprev->callback(list->exten->parent->name, list->exten->exten, -1, cbprev->data);
free(cbprev);
}
list->callbacks = NULL;
if (!prev)
hints = list->next;
else
prev->next = list->next;
free(list);
ast_mutex_unlock(&hintlock);
} else {
prev = list;
list = list->next;
}
}
ast_mutex_unlock(&hintlock);
}
int ast_get_hint(char *hint, int maxlen, struct ast_channel *c, char *context, char *exten)
{
struct ast_exten *e;
e = ast_hint_extension(c, context, exten);
if (e) {
strncpy(hint, ast_get_extension_app(e), maxlen);
return -1;
}
return 0;
}
int ast_exists_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_EXISTS);
int ast_canmatch_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_CANMATCH);
int ast_matchmore_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
{
return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_MATCHMORE);
}
int ast_spawn_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_SPAWN);
{
int firstpass = 1;
char digit;
char exten[256];
int pos;
int waittime;
/* A little initial setup here */
if (c->pbx)
ast_log(LOG_WARNING, "%s already has PBX structure??\n", c->name);
c->pbx = malloc(sizeof(struct ast_pbx));
if (!c->pbx) {
ast_log(LOG_ERROR, "Out of memory\n");
ast_log(LOG_WARNING, "%s already has a call record??\n", c->name);
} else {
c->cdr = ast_cdr_alloc();
if (!c->cdr) {
ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
free(c->pbx);
return -1;
}
ast_cdr_init(c->cdr, c);
}
}
memset(c->pbx, 0, sizeof(struct ast_pbx));
/* Set reasonable defaults */
c->pbx->rtimeout = 10;
c->pbx->dtimeout = 5;
/* Start by trying whatever the channel is set to */
if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
/* JK02: And finally back to default if everything else failed */
strncpy(c->context, "default", sizeof(c->context)-1);
}
while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
manager_event(EVENT_FLAG_CALL, "Newexten",
"Channel: %s\r\n"
"Context: %s\r\n"
"Extension: %s\r\n"
"Priority: %d\r\n"
"Uniqueid: %s\r\n",
c->name, c->context, c->exten, c->priority, c->uniqueid);
if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
/* Something bad happened, or a hangup has been requested. */
if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
(res == '*') || (res == '#')) {
ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
switch(res) {
case AST_PBX_KEEPALIVE:
if (option_debug)
ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
else if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
default:
if (option_debug)
ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
else if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
c->_softhangup =0;
break;
}
/* atimeout */
if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
break;
}
if (c->cdr) {
ast_cdr_update(c);
}
if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->callerid))) {
strncpy(c->exten,"T",sizeof(c->exten) - 1);
/* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
c->whentohangup = 0;
c->priority = 0;
c->_softhangup &= ~AST_SOFTHANGUP_TIMEOUT;
ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
if (!ast_exists_extension(c, c->context, c->exten, 1, c->callerid)) {
if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
c->priority = 1;
} else {
ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
c->name, c->exten, c->context);
} else if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
/* If we get this far with AST_SOFTHANGUP_TIMEOUT, then we know that the "T" extension is next. */
c->_softhangup = 0;
/* Done, wait for an extension */
if (digit)
waittime = c->pbx->dtimeout;
else
waittime = c->pbx->rtimeout;
while (ast_matchmore_extension(c, c->context, exten, 1, c->callerid)) {
/* As long as we're willing to wait, and as long as it's not defined,
keep reading digits until we can't possibly get a right answer anymore. */
digit = ast_waitfordigit(c, waittime * 1000);
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
c->_softhangup = 0;
} else {
if (!digit)
/* No entry */
break;
if (digit < 0)
/* Error, maybe a hangup */
goto out;
exten[pos++] = digit;
waittime = c->pbx->dtimeout;
}
if (ast_exists_extension(c, c->context, exten, 1, c->callerid)) {
if (!ast_strlen_zero(exten)) {
if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
pbx_builtin_setvar_helper(c, "INVALID_EXTEN", exten);
c->priority = 1;
} else {
ast_log(LOG_WARNING, "Invalid extension, but no rule 'i' in context '%s'\n", c->context);
goto out;
}
if (ast_exists_extension(c, c->context, "t", 1, c->callerid)) {
if (option_verbose > 2)
ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
c->priority = 1;
} else {
ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
goto out;
}
}
}
if (c->cdr) {
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_2 "CDR updated on %s\n",c->name);
}
}
if (firstpass)
ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
out:
if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->callerid)) {
strcpy(c->exten, "h");
c->priority = 1;
while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
/* Something bad happened, or a hangup has been requested. */
if (option_debug)
ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
else if (option_verbose > 1)
ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
break;
}
c->priority++;
}
}
if (res != AST_PBX_KEEPALIVE)
ast_hangup(c);
return 0;
}
static void *pbx_thread(void *data)
{
/* Oh joyeous kernel, we're a new thread, with nothing to do but
answer this channel and get it going. The setjmp stuff is fairly
confusing, but necessary to get smooth transitions between
the execution of different applications (without the use of
additional threads) */
struct ast_channel *c = data;
ast_pbx_run(c);
}
int ast_pbx_start(struct ast_channel *c)
{
pthread_t t;
if (!c) {
ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
return -1;
}
/* Start a new thread, and get something handling this channel. */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&t, &attr, pbx_thread, c)) {
ast_log(LOG_WARNING, "Failed to create new channel thread\n");
return -1;
}
return 0;
}
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
/*
* This function locks contexts list by &conlist, search for the rigt context
* structure, leave context list locked and call ast_context_remove_include2
* which removes include, unlock contexts list and return ...
*/
int ast_context_remove_include(char *context, char *include, char *registrar)
{
struct ast_context *c;
if (ast_lock_contexts()) return -1;
/* walk contexts and search for the right one ...*/
c = ast_walk_contexts(NULL);
while (c) {
/* we found one ... */
if (!strcmp(ast_get_context_name(c), context)) {
int ret;
/* remove include from this context ... */
ret = ast_context_remove_include2(c, include, registrar);
ast_unlock_contexts();
/* ... return results */
return ret;
}
c = ast_walk_contexts(c);
}
/* we can't find the right one context */
ast_unlock_contexts();
return -1;
}
/*
* When we call this function, &conlock lock must be locked, because when
* we giving *con argument, some process can remove/change this context
* and after that there can be segfault.
*
* This function locks given context, removes include, unlock context and
* return.
*/
int ast_context_remove_include2(struct ast_context *con, char *include, char *registrar)
{
struct ast_include *i, *pi = NULL;
if (ast_mutex_lock(&con->lock)) return -1;
/* walk includes */
i = con->includes;
while (i) {
/* find our include */
if (!strcmp(i->name, include) &&
(!strcmp(i->registrar, registrar) || !registrar)) {
/* remove from list */
if (pi)
pi->next = i->next;
else
con->includes = i->next;
/* free include and return */
free(i);
ast_mutex_unlock(&con->lock);
return 0;
}
pi = i;
i = i->next;
}
/* we can't find the right include */
ast_mutex_unlock(&con->lock);
return -1;
}
/*
* This function locks contexts list by &conlist, search for the rigt context
* structure, leave context list locked and call ast_context_remove_switch2
* which removes switch, unlock contexts list and return ...
*/
int ast_context_remove_switch(char *context, char *sw, char *data, char *registrar)
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
struct ast_context *c;
if (ast_lock_contexts()) return -1;
/* walk contexts and search for the right one ...*/
c = ast_walk_contexts(NULL);
while (c) {
/* we found one ... */
if (!strcmp(ast_get_context_name(c), context)) {
int ret;
/* remove switch from this context ... */
ret = ast_context_remove_switch2(c, sw, data, registrar);
ast_unlock_contexts();
/* ... return results */
return ret;
}
c = ast_walk_contexts(c);
}
/* we can't find the right one context */
ast_unlock_contexts();
/*
* When we call this function, &conlock lock must be locked, because when
* we giving *con argument, some process can remove/change this context
* and after that there can be segfault.
*
* This function locks given context, removes switch, unlock context and
* return.
*/
int ast_context_remove_switch2(struct ast_context *con, char *sw, char *data, char *registrar)
{
struct ast_sw *i, *pi = NULL;
if (ast_mutex_lock(&con->lock)) return -1;
/* walk switchs */
i = con->alts;
while (i) {
/* find our switch */
if (!strcmp(i->name, sw) && !strcmp(i->data, data) &&
(!strcmp(i->registrar, registrar) || !registrar)) {
/* remove from list */
if (pi)
pi->next = i->next;
else
con->alts = i->next;
/* free switch and return */
free(i);
ast_mutex_unlock(&con->lock);
return 0;
}
pi = i;
i = i->next;
}
/* we can't find the right switch */
ast_mutex_unlock(&con->lock);
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
return -1;
}
/*
* This functions lock contexts list, search for the right context,
* call ast_context_remove_extension2, unlock contexts list and return.
* In this function we are using
*/
int ast_context_remove_extension(char *context, char *extension, int priority, char *registrar)
{
struct ast_context *c;
if (ast_lock_contexts()) return -1;
/* walk contexts ... */
c = ast_walk_contexts(NULL);
while (c) {
/* ... search for the right one ... */
if (!strcmp(ast_get_context_name(c), context)) {
/* ... remove extension ... */
int ret = ast_context_remove_extension2(c, extension, priority,
registrar);
/* ... unlock contexts list and return */
ast_unlock_contexts();
return ret;
}
c = ast_walk_contexts(c);
}
/* we can't find the right context */
ast_unlock_contexts();
return -1;
}
/*
* When do you want to call this function, make sure that &conlock is locked,
* because some process can handle with your *con context before you lock
* it.
*
* This functionc locks given context, search for the right extension and
* fires out all peer in this extensions with given priority. If priority
* is set to 0, all peers are removed. After that, unlock context and
* return.
*/
int ast_context_remove_extension2(struct ast_context *con, char *extension, int priority, char *registrar)
{
struct ast_exten *exten, *prev_exten = NULL;
if (ast_mutex_lock(&con->lock)) return -1;
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
/* go through all extensions in context and search the right one ... */
exten = con->root;
while (exten) {
/* look for right extension */
if (!strcmp(exten->exten, extension) &&
(!strcmp(exten->registrar, registrar) || !registrar)) {
struct ast_exten *peer;
/* should we free all peers in this extension? (priority == 0)? */
if (priority == 0) {
/* remove this extension from context list */
if (prev_exten)
prev_exten->next = exten->next;
else
con->root = exten->next;
/* fire out all peers */
peer = exten;
while (peer) {
exten = peer->peer;
if (!peer->priority==PRIORITY_HINT)
ast_remove_hint(peer);
peer->datad(peer->data);
free(peer);
peer = exten;
}
ast_mutex_unlock(&con->lock);
return 0;
} else {
/* remove only extension with exten->priority == priority */
struct ast_exten *previous_peer = NULL;
peer = exten;
while (peer) {
/* is this our extension? */
if (peer->priority == priority &&
(!strcmp(peer->registrar, registrar) || !registrar)) {
/* we are first priority extension? */
if (!previous_peer) {
/* exists previous extension here? */
if (prev_exten) {
/* yes, so we must change next pointer in
* previous connection to next peer
Loading
Loading full blame...